ec27b746b59371fe1b3f7ba0ffb03eac30e2fe71
[navit-package] / src / gui / gtk / gui_gtk_toolbar.c
1 #include <gtk/gtk.h>
2 #include "graphics.h"
3 #include "gui_gtk.h"
4 #include "menu.h"
5 #include "coord.h"
6 #include "destination.h"
7 #include "container.h"
8
9 struct toolbar_gui {
10         struct container *co;
11 };
12
13 #include "toolbar.h"
14
15 static void
16 zoom_in(GtkWidget *w, struct toolbar *tb)
17 {
18         unsigned long scale;
19         graphics_get_view(tb->gui->co, NULL, NULL, &scale);
20         scale/=2;
21         if (scale < 1)
22                 scale=1;
23         graphics_set_view(tb->gui->co, NULL, NULL, &scale);
24 }
25
26 static void
27 zoom_out(GtkWidget *w, struct toolbar *tb)
28 {
29         unsigned long scale;
30         graphics_get_view(tb->gui->co, NULL, NULL, &scale);
31         scale*=2;
32         graphics_set_view(tb->gui->co, NULL, NULL, &scale);
33 }
34
35 static void
36 refresh_route(GtkWidget *w, struct toolbar *tb)
37 {
38         menu_route_update(tb->gui->co);
39 }
40
41 static void
42 track(GtkWidget *w, struct toolbar *tb)
43 {
44         if (tb->gui->co->flags->track) {
45                 tb->gui->co->flags->track=0;
46                 gtk_widget_set_state(w, GTK_STATE_ACTIVE);
47         } else {
48                 tb->gui->co->flags->track=1;
49                 gtk_widget_set_state(w, GTK_STATE_ACTIVE);
50         }
51 }
52
53 static void
54 orient_north(GtkWidget *w, struct toolbar *tb)
55 {
56         if (tb->gui->co->flags->orient_north) {
57                 tb->gui->co->flags->orient_north=0;
58                 gtk_widget_set_state(w, GTK_STATE_ACTIVE);
59         } else {
60                 tb->gui->co->flags->orient_north=1;
61                 gtk_widget_set_state(w, GTK_STATE_ACTIVE);
62         }
63 }
64
65 static void
66 destination(GtkWidget *w, struct toolbar *tb)
67 {
68         destination_address(tb->gui->co);
69 }
70
71
72 /* XPM */
73 /* Drawn  by Mark Donohoe for the K Desktop Environment */
74 /* See http://www.kde.org */
75 static char*viewmag_plus_xpm[]={
76 "22 22 5 1",
77 "# c #000000",
78 "c c #a0a0a4",
79 "a c #dcdcdc",
80 "b c #ffffff",
81 ". c None",
82 "......................",
83 "......................",
84 "......................",
85 "......................",
86 "......................",
87 ".......####...........",
88 "......#abba#..........",
89 ".....#abcbba#.........",
90 ".....#bcbbbb#.........",
91 ".....#bbbbbb#.........",
92 ".....#abbbba#.........",
93 "......#abba##.........",
94 ".......#######........",
95 "............###.......",
96 ".....#.......###......",
97 ".....#........###.....",
98 "...#####.......##.....",
99 ".....#................",
100 ".....#................",
101 "......................",
102 "......................",
103 "......................"};
104
105
106 /* XPM */
107 /* Drawn  by Mark Donohoe for the K Desktop Environment */
108 /* See http://www.kde.org */
109 static char*viewmag_minus_xpm[]={
110 "22 22 5 1",
111 "# c #000000",
112 "c c #a0a0a4",
113 "a c #dcdcdc",
114 "b c #ffffff",
115 ". c None",
116 "......................",
117 "......................",
118 "......................",
119 "......................",
120 "......................",
121 ".......####...........",
122 "......#abba#..........",
123 ".....#abcbba#.........",
124 ".....#bcbbbb#.........",
125 ".....#bbbbbb#.........",
126 ".....#abbbba#.........",
127 "......#abba##.........",
128 ".......#######........",
129 "............###.......",
130 ".............###......",
131 "..............###.....",
132 "...#####.......##.....",
133 "......................",
134 "......................",
135 "......................",
136 "......................",
137 "......................"};
138
139
140 /* XPM */
141 /* Drawn  by Mark Donohoe for the K Desktop Environment */
142 /* See http://www.kde.org */
143 static char*reload_xpm[]={
144 "22 22 3 1",
145 "# c #808080",
146 "a c #000000",
147 ". c None",
148 "......................",
149 "......................",
150 "......................",
151 "......................",
152 "........##aaa#........",
153 ".......#aaaaaaa.......",
154 "......#aa#....#a......",
155 "......aa#.............",
156 ".....aaa.......a......",
157 "...aaaaaaa....aaa.....",
158 "....aaaaa....aaaaa....",
159 ".....aaa....aaaaaaa...",
160 "......a.......aaa.....",
161 ".............#aa......",
162 "......a#....#aa#......",
163 ".......aaaaaaa#.......",
164 "........#aaa##........",
165 "......................",
166 "......................",
167 "......................",
168 "......................",
169 "......................"};
170
171
172 /* XPM */
173 static char * cursor_xpm[] = {
174 "22 22 2 1",
175 "       c None",
176 ".      c #0000FF",
177 "                      ",
178 "                      ",
179 "                      ",
180 "          ..          ",
181 "        ..  ..        ",
182 "      ..      ..      ",
183 "     .          .     ",
184 "     .          .     ",
185 "    .        ... .    ",
186 "    .     ... .  .    ",
187 "   .   ...   .    .   ",
188 "   . ..     .     .   ",
189 "    .      .     .    ",
190 "    .     .      .    ",
191 "     .   .      .     ",
192 "     .  .       .     ",
193 "      ..      ..      ",
194 "        ..  ..        ",
195 "          ..          ",
196 "                      ",
197 "                      ",
198 "                      "};
199
200
201 /* XPM */
202 static char * north_xpm[] = {
203 "22 22 2 1",
204 "       c None",
205 ".      c #000000",
206 "                      ",
207 "                      ",
208 "           .          ",
209 "          ...         ",
210 "         . . .        ",
211 "        .  .  .       ",
212 "           .          ",
213 "     ....  .  ....    ",
214 "     ....  .  ....    ",
215 "      .... .   ..     ",
216 "      .. ..    ..     ",
217 "      ..  ..   ..     ",
218 "      ..   ..  ..     ",
219 "      ..    .. ..     ",
220 "      ..   . ....     ",
221 "     ....  .  ....    ",
222 "     ....  .  ....    ",
223 "           .          ",
224 "           .          ",
225 "           .          ",
226 "                      ",
227 "                      "};
228
229
230 /* XPM */
231 static char * flag_xpm[] = {
232 "22 22 2 1",
233 "       c None",
234 "+      c #000000",
235 "+++++++               ",
236 "+   +++++++++         ",
237 "+   +++   +++++++++   ",
238 "+   +++   +++   +++   ",
239 "++++      +++   +++   ",
240 "++++   +++      +++   ",
241 "++++   +++   +++  +   ",
242 "+   ++++++   +++  +   ",
243 "+   +++   ++++++  +   ",
244 "+   +++   +++   +++   ",
245 "++++      +++   +++   ",
246 "++++   +++      +++   ",
247 "++++++++++   +++  +   ",
248 "+      +++++++++  +   ",
249 "+            ++++++   ",
250 "+                     ",
251 "+                     ",
252 "+                     ",
253 "+                     ",
254 "+                     ",
255 "+                     ",
256 "+                     "};
257
258 static GtkWidget *
259 xpm_to_widget(GtkWidget *draw, gchar **xpm_data)
260 {
261         /* GtkWidget is the storage type for widgets */
262         GtkWidget *imagewid;
263         GdkPixmap *pixmap;
264         GdkBitmap *mask;
265         GtkStyle *style;
266
267         style = gtk_widget_get_style(draw);
268
269         /* In order for this to not create a warning, window has to be a
270         * gtk_realize_widget (realized) widget
271         */
272         pixmap = gdk_pixmap_create_from_xpm_d(draw->window,  &mask,
273                                    &style->bg[GTK_STATE_NORMAL],
274                                    (gchar **)xpm_data );
275
276         /* a pixmap widget to contain the pixmap */
277         imagewid = gtk_image_new_from_pixmap(pixmap, mask);
278         gtk_widget_show(imagewid);
279
280         return(imagewid);
281 }
282
283 int tst_stat=1;
284
285 static void
286 toolbar_button(GtkWidget *window, GtkWidget *toolbar, char **icon_data, char *text, void (*func)(GtkWidget *w, struct toolbar *tb), void *data)
287 {
288         GtkWidget *icon;
289         GtkToolItem *toolitem;
290         
291         icon=xpm_to_widget(window, icon_data);
292         toolitem=gtk_tool_button_new(icon,text);
293         gtk_toolbar_insert(GTK_TOOLBAR(toolbar),toolitem,-1);
294         g_signal_connect(G_OBJECT(toolitem), "clicked", G_CALLBACK(func), data);
295 }
296
297 static void
298 toolbar_button_toggle(GtkWidget *window, GtkWidget *toolbar, char **icon_data, char *text, void (*func)(GtkWidget *w, struct toolbar *tb), void *data, int *flag)
299 {
300         GtkWidget *icon;
301         GtkToolItem *toggleitem;
302         
303         icon=xpm_to_widget(window, icon_data);
304         toggleitem=gtk_toggle_tool_button_new();
305         gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(toggleitem),icon);
306         gtk_tool_button_set_label(GTK_TOOL_BUTTON(toggleitem),text);
307         gtk_toolbar_insert(GTK_TOOLBAR(toolbar),toggleitem,-1);
308         if(*flag)
309             gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(toggleitem),TRUE);
310             
311         g_signal_connect(G_OBJECT(toggleitem), "clicked", G_CALLBACK(func), data);
312 }
313
314
315 struct toolbar *
316 gui_gtk_toolbar_new(struct container *co, GtkWidget **widget)
317 {
318         GtkWidget *toolbar,*window;
319         struct toolbar *this=g_new0(struct toolbar, 1);
320
321         this->gui=g_new0(struct toolbar_gui, 1);        
322         this->gui->co=co;
323
324         toolbar=gtk_toolbar_new();
325         window=(GtkWidget *)(co->win);
326
327         co->flags->track=1;
328         co->flags->orient_north=1;
329
330         toolbar_button(window, toolbar, viewmag_plus_xpm, "Zoom In", zoom_in, this);
331         toolbar_button(window, toolbar, viewmag_minus_xpm, "Zoom Out", zoom_out, this);
332         toolbar_button(window, toolbar, reload_xpm, "Refresh Route", refresh_route, this);
333         toolbar_button_toggle(window, toolbar, cursor_xpm, "Cursor on/off", track, this, &co->flags->track);
334         toolbar_button_toggle(window, toolbar, north_xpm, "Orientate North on/off", orient_north, this, &co->flags->orient_north);
335         toolbar_button(window, toolbar, flag_xpm, "Destination", destination, this);
336
337         *widget=toolbar;
338
339         return this;
340 }