]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
fix implicit declaration warning
[andy/viking.git] / src / vikwindow.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include "viking.h"
22 #include "background.h"
23
24 #define VIKING_TITLE " - Viking " VIKING_VERSION " " VIKING_VERSION_NAME " " VIKING_URL
25
26 #include <glib/gprintf.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #ifdef WINDOWS
33 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
34 #define make_dir(dir) mkdir(dir)
35 #else
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #define make_dir(dir) mkdir(dir,0777)
39 #endif
40
41 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
42 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
43 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
44
45 static void window_finalize ( GObject *gob );
46 static GObjectClass *parent_class;
47
48 static void window_init ( VikWindow *vw );
49 static void window_class_init ( VikWindowClass *klass );
50
51 static void draw_update ( VikWindow *vw );
52
53 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
54
55 /* Drawing & stuff */
56
57 static gboolean delete_event( VikWindow *vw );
58
59 static void draw_sync ( VikWindow *vw );
60 static void draw_redraw ( VikWindow *vw );
61 static void draw_scroll  ( VikWindow *vw, GdkEvent *event );
62 static void draw_click  ( VikWindow *vw, GdkEventButton *event );
63 static void draw_release ( VikWindow *vw, GdkEventButton *event );
64 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
65 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
66 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
67
68 static void draw_status ();
69
70 /* End Drawing Functions */
71
72 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
73 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
74 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
75 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
76
77 static void window_create_ui( VikWindow *window );
78
79 static void load_file ( GtkAction *a, VikWindow *vw );
80 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
81 static gboolean save_file ( GtkAction *a, VikWindow *vw );
82 static gboolean window_save ( VikWindow *vw );
83 static void register_vik_icons (GtkIconFactory *icon_factory);
84
85
86 struct _VikWindow {
87   GtkWindow gtkwindow;
88   VikViewport *viking_vvp;
89   VikLayersPanel *viking_vlp;
90   VikStatusbar *viking_vs;
91
92   GtkToolbar *toolbar;
93
94   GtkItemFactory *item_factory;
95
96   /* ruler variables */
97   VikCoord oldcoord;
98   gboolean has_oldcoord;
99   
100
101   guint current_tool;
102
103   guint16 tool_layer_id;
104   guint16 tool_tool_id;
105
106   gint pan_x, pan_y;
107
108   guint draw_image_width, draw_image_height;
109   gboolean draw_image_save_as_png;
110
111   gchar *filename;
112   gboolean modified;
113
114   GtkWidget *open_dia, *save_dia;
115
116   gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
117   GtkUIManager *uim;
118 };
119
120 enum {
121  TOOL_ZOOM = 0,
122  TOOL_RULER,
123  TOOL_LAYER,
124  NUMBER_OF_TOOLS
125 };
126
127 enum {
128   VW_NEWWINDOW_SIGNAL,
129   VW_OPENWINDOW_SIGNAL,
130   VW_LAST_SIGNAL
131 };
132
133 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
134
135 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
136
137 GType vik_window_get_type (void)
138 {
139   static GType vw_type = 0;
140
141   if (!vw_type)
142   {
143     static const GTypeInfo vw_info = 
144     {
145       sizeof (VikWindowClass),
146       NULL, /* base_init */
147       NULL, /* base_finalize */
148       (GClassInitFunc) window_class_init, /* class_init */
149       NULL, /* class_finalize */
150       NULL, /* class_data */
151       sizeof (VikWindow),
152       0,
153       (GInstanceInitFunc) window_init,
154     };
155     vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
156   }
157
158   return vw_type;
159 }
160
161 static void window_finalize ( GObject *gob )
162 {
163   VikWindow *vw = VIK_WINDOW(gob);
164   g_return_if_fail ( vw != NULL );
165
166   a_background_remove_status ( vw->viking_vs );
167
168   G_OBJECT_CLASS(parent_class)->finalize(gob);
169 }
170
171 static void window_class_init ( VikWindowClass *klass )
172 {
173   /* destructor */
174   GObjectClass *object_class;
175
176   window_signals[VW_NEWWINDOW_SIGNAL] = g_signal_new ( "newwindow", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikWindowClass, newwindow), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
177   window_signals[VW_OPENWINDOW_SIGNAL] = g_signal_new ( "openwindow", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikWindowClass, openwindow), NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
178
179   object_class = G_OBJECT_CLASS (klass);
180
181   object_class->finalize = window_finalize;
182
183   parent_class = g_type_class_peek_parent (klass);
184
185 }
186
187 static void window_init ( VikWindow *vw )
188 {
189   GtkWidget *main_vbox;
190   GtkWidget *hpaned;
191
192   window_create_ui(vw);
193   gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
194
195   vw->viking_vvp = vik_viewport_new();
196   vw->viking_vlp = vik_layers_panel_new();
197   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
198   vw->viking_vs = vik_statusbar_new();
199
200   vw->filename = NULL;
201   vw->item_factory = NULL;
202
203   vw->has_oldcoord = FALSE;
204   vw->modified = FALSE;
205   vw->only_updating_coord_mode_ui = FALSE;
206
207   vw->pan_x = vw->pan_y = -1;
208   vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
209   vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
210   vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
211
212   main_vbox = gtk_vbox_new(FALSE, 1);
213   gtk_container_add (GTK_CONTAINER (vw), main_vbox);
214
215   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
216   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
217   gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
218   gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
219
220   g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
221
222   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
223   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
224   gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
225   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
226   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
227   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
228   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
229   g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
230
231   gtk_window_set_default_size ( GTK_WINDOW(vw), 1000, 800);
232
233   hpaned = gtk_hpaned_new ();
234   gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
235   gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
236
237   /* This packs the button into the window (a gtk container). */
238   gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
239
240   gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
241
242   a_background_add_status(vw->viking_vs);
243
244   vw->open_dia = NULL;
245   vw->save_dia = NULL;
246 }
247
248 VikWindow *vik_window_new ()
249 {
250   return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
251 }
252
253 static gboolean delete_event( VikWindow *vw )
254 {
255   if ( vw->modified )
256   {
257     GtkDialog *dia;
258     dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
259       "Do you want to save the changes you made to the document \"%s\"?\n\nYour changes will be lost if you don't save them.",
260       vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
261     gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
262     switch ( gtk_dialog_run ( dia ) )
263     {
264       case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
265       case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
266       default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
267     }
268   }
269   return FALSE;
270 }
271
272 /* Drawing stuff */
273 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
274 {
275   g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
276 }
277
278 static void draw_update ( VikWindow *vw )
279 {
280   draw_redraw (vw);
281   draw_sync (vw);
282 }
283
284 static void draw_sync ( VikWindow *vw )
285 {
286   vik_viewport_sync(vw->viking_vvp);
287   draw_status ( vw );
288   /* other things may be necc here later. */
289 }
290
291 static void draw_status ( VikWindow *vw )
292 {
293   static gchar zoom_level[22];
294   g_snprintf ( zoom_level, 22, "%.3f/%.3f %s", vik_viewport_get_xmpp (vw->viking_vvp), vik_viewport_get_ympp(vw->viking_vvp), vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM ? "mpp" : "pixelfact" );
295   if ( vw->current_tool == TOOL_LAYER )
296     vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
297   else
298     vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
299
300   vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
301 }
302
303 static void draw_redraw ( VikWindow *vw )
304 {
305   vik_viewport_clear ( vw->viking_vvp);
306   vik_layers_panel_draw_all ( vw->viking_vlp );
307   vik_viewport_draw_scale ( vw->viking_vvp );
308 }
309
310 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
311 {
312   PangoFontDescription *pfd;
313   PangoLayout *pl;
314   gchar str[128];
315   GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
316   GdkGC *thickgc = gdk_gc_new(d);
317   
318   gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
319   gdouble dx = (x2-x1)/len*10; 
320   gdouble dy = (y2-y1)/len*10;
321   gdouble c = cos(15.0 * M_PI/180.0);
322   gdouble s = sin(15.0 * M_PI/180.0);
323   gdouble angle;
324   gdouble baseangle = 0;
325   gint i;
326
327   /* draw line with arrow ends */
328   {
329     gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
330     a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
331     gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
332   }
333
334   a_viewport_clip_line(&x1, &y1, &x2, &y2);
335   gdk_draw_line(d, gc, x1, y1, x2, y2);
336
337   gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
338   gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
339   gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
340   gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
341   gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
342   gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
343
344   /* draw compass */
345 #define CR 80
346 #define CW 4
347   angle = atan2(dy, dx) + M_PI_2;
348
349   if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
350     VikCoord test;
351     struct LatLon ll;
352     struct UTM u;
353     gint tx, ty;
354
355     vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
356     vik_coord_to_latlon ( &test, &ll );
357     ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
358     a_coords_latlon_to_utm ( &ll, &u );
359     vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
360     vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
361
362     baseangle = M_PI - atan2(tx-x1, ty-y1);
363     angle -= baseangle;
364   }
365
366   if (angle<0) 
367     angle+=2*M_PI;
368   if (angle>2*M_PI)
369     angle-=2*M_PI;
370
371   {
372     GdkColor color;
373     gdk_gc_copy(thickgc, gc);
374     gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
375     gdk_color_parse("#2255cc", &color);
376     gdk_gc_set_rgb_fg_color(thickgc, &color);
377   }
378   gdk_draw_arc (d, thickgc, FALSE, x1-CR+CW/2, y1-CR+CW/2, 2*CR-CW, 2*CR-CW, (90 - baseangle*180/M_PI)*64, -angle*180/M_PI*64);
379
380
381   gdk_gc_copy(thickgc, gc);
382   gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
383   for (i=0; i<180; i++) {
384     c = cos(i*M_PI/90.0 + baseangle);
385     s = sin(i*M_PI/90.0 + baseangle);
386
387     if (i%5) {
388       gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
389     } else {
390       gdouble ticksize = 2*CW;
391       gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
392     }
393   }
394
395   gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
396   gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
397   gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
398   c = (CR+CW*2)*cos(baseangle);
399   s = (CR+CW*2)*sin(baseangle);
400   gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
401   gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
402
403   /* draw labels */
404 #define LABEL(x, y, w, h) { \
405     gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
406     gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
407     gdk_draw_layout(d, gc, (x), (y), pl); } 
408   {
409     gint wd, hd, xd, yd;
410     gint wb, hb, xb, yb;
411
412     pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
413
414     pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
415     pango_layout_set_font_description (pl, pfd);
416     pango_font_description_free (pfd);
417
418     pango_layout_set_text(pl, "N", -1);
419     gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
420
421     /* draw label with distance */
422     if (distance >= 1000 && distance < 100000) {
423       g_sprintf(str, "%3.2f km", distance/1000.0);
424     } else if (distance < 1000) {
425       g_sprintf(str, "%d m", (int)distance);
426     } else {
427       g_sprintf(str, "%d km", (int)distance/1000);
428     }
429     pango_layout_set_text(pl, str, -1);
430
431     pango_layout_get_pixel_size ( pl, &wd, &hd );
432     if (dy>0) {
433       xd = (x1+x2)/2 + dy;
434       yd = (y1+y2)/2 - hd/2 - dx;
435     } else {
436       xd = (x1+x2)/2 - dy;
437       yd = (y1+y2)/2 - hd/2 + dx;
438     }
439
440     if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
441       xd = x2 + 10;
442       yd = y2 - 5;
443     }
444
445     LABEL(xd, yd, wd, hd);
446
447     /* draw label with bearing */
448     g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
449     pango_layout_set_text(pl, str, -1);
450     pango_layout_get_pixel_size ( pl, &wb, &hb );
451     xb = x1 + CR*cos(angle-M_PI_2);
452     yb = y1 + CR*sin(angle-M_PI_2);
453
454     if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
455       xb = x2 + 10;
456       yb = y2 + 10;
457     }
458
459     {
460       GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
461       if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
462         xb = xd + wd + 5;
463       }
464     }
465     LABEL(xb, yb, wb, hb);
466   }
467 #undef LABEL
468
469   g_object_unref ( G_OBJECT ( pl ) );
470   g_object_unref ( G_OBJECT ( labgc ) );
471   g_object_unref ( G_OBJECT ( thickgc ) );
472 }
473
474 gboolean draw_buf_done = TRUE;
475
476 static gboolean draw_buf(gpointer data)
477 {
478   gpointer *pass_along = data;
479   gdk_threads_enter();
480   gdk_draw_drawable (pass_along[0], pass_along[1],
481                      pass_along[2], 0, 0, 0, 0, -1, -1);
482   gdk_threads_leave();
483   draw_buf_done = TRUE;
484   return FALSE;
485 }
486
487 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
488 {
489   VikViewport *vvp = vw->viking_vvp;
490   static VikCoord coord;
491   static struct UTM utm;
492   static struct LatLon ll;
493   static char pointer_buf[36];
494
495   vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
496   vik_coord_to_utm ( &coord, &utm );
497   a_coords_utm_to_latlon ( &utm, &ll );
498
499   if ( vw->current_tool == TOOL_RULER )
500   {
501     struct LatLon ll;
502     VikCoord coord;
503     gchar *temp;
504
505     if ( vw->has_oldcoord ) {
506       int oldx, oldy, w1, h1, w2, h2;
507       static GdkPixmap *buf = NULL;
508       w1 = vik_viewport_get_width(vvp); 
509       h1 = vik_viewport_get_height(vvp);
510       if (!buf) {
511         buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
512       }
513       gdk_drawable_get_size(buf, &w2, &h2);
514       if (w1 != w2 || h1 != h2) {
515         g_object_unref ( G_OBJECT ( buf ) );
516         buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
517       }
518
519       vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
520       vik_coord_to_latlon ( &coord, &ll );
521       vik_viewport_coord_to_screen ( vvp, &vw->oldcoord, &oldx, &oldy );
522
523       gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc, 
524                          vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
525       draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(vw->oldcoord)) );
526       if (draw_buf_done) {
527         static gpointer pass_along[3];
528         pass_along[0] = GTK_WIDGET(vvp)->window;
529         pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
530         pass_along[2] = buf;
531         g_idle_add (draw_buf, pass_along);
532         draw_buf_done = FALSE;
533       }
534
535       temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
536       vik_statusbar_set_message ( vw->viking_vs, 3, temp );
537       g_free ( temp );
538     }
539   }
540
541   g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
542
543   if ( vw->pan_x != -1 ) {
544     vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
545   }
546
547   vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
548 }
549
550 static void draw_scroll (VikWindow *vw, GdkEvent *event)
551 {
552   if ( event->scroll.direction == GDK_SCROLL_UP )
553     vik_viewport_zoom_in (vw->viking_vvp);
554   else
555     vik_viewport_zoom_out(vw->viking_vvp);
556   draw_update(vw);
557 }
558
559 static void draw_release ( VikWindow *vw, GdkEventButton *event )
560 {
561   if ( event->button == 2 ) { /* move / pan */
562     if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
563         vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
564       else
565          vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
566                                          vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
567       draw_update ( vw );
568       vw->pan_x = vw->pan_y = -1;
569   }
570   if ( vw->current_tool == TOOL_LAYER && ( event->button == 1 || event->button == 3 ) &&
571       vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release )
572   {
573     vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
574         vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release,
575         event, vw->viking_vvp );
576   }
577 }
578
579 static void draw_click (VikWindow *vw, GdkEventButton *event)
580 {
581   if ( event->button == 2) {
582     vw->pan_x = (gint) event->x;
583     vw->pan_y = (gint) event->y;
584   } else if ( vw->current_tool == TOOL_ZOOM )
585   {
586     vw->modified = TRUE;
587     vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
588     if ( event->button == 1 )
589       vik_viewport_zoom_in (vw->viking_vvp);
590     else if ( event->button == 3 )
591       vik_viewport_zoom_out (vw->viking_vvp);
592     draw_update ( vw );
593   }
594   else if ( vw->current_tool == TOOL_RULER )
595   {
596     struct LatLon ll;
597     VikCoord coord;
598     gchar *temp;
599     if ( event->button == 1 || event->button == 3 )
600     {
601       vik_viewport_screen_to_coord ( vw->viking_vvp, (gint) event->x, (gint) event->y, &coord );
602       vik_coord_to_latlon ( &coord, &ll );
603       if ( vw->has_oldcoord ) {
604          temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
605          vw->has_oldcoord = FALSE;
606       }
607       else {
608         temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
609         vw->has_oldcoord = TRUE;
610       }
611
612       vik_statusbar_set_message ( vw->viking_vs, 3, temp );
613       g_free ( temp );
614
615       vw->oldcoord = coord;
616       /* we don't use anything else so far */
617     }
618     else
619     {
620       vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
621       draw_update ( vw );
622     }
623   }
624   else if ( vw->current_tool == TOOL_LAYER )
625   {
626     vw->modified = TRUE;
627     if ( ! vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
628         vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback,
629         event, vw->viking_vvp ) )
630       a_dialog_info_msg_extra ( GTK_WINDOW(vw), "A %s Layer must exist and be either selected or visible before you can use this function.", vik_layer_get_interface ( vw->tool_layer_id )->name );
631   }
632 }
633
634 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
635 {
636   guint what = 128;
637
638   if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
639     what = -3;
640   } 
641   else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
642     what = -4;
643   }
644   else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
645     what = -2;
646   }
647   else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
648     what = -1;
649   }
650   else {
651     gchar *s = (gchar *)gtk_action_get_name(a);
652     what = atoi(s+4);
653   }
654
655   switch (what)
656   {
657     case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
658     case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
659     case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
660     case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
661     default: vik_viewport_set_zoom ( vw->viking_vvp, what );
662   }
663   draw_update ( vw );
664 }
665
666 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
667 {
668   VikCoord new_center;
669
670   if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
671     struct LatLon ll, llold;
672     vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
673     if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
674       vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
675     else
676       return;
677   }
678   else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
679     struct UTM utm, utmold;
680     vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
681     if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
682       vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
683     else
684      return;
685   }
686   else {
687     fprintf(stderr, "Houston we have a problem\n");
688     return;
689   }
690
691   vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
692   draw_update ( vw );
693 }
694
695 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
696 {
697   gint type;
698   for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
699     if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
700       if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
701         draw_update ( vw );
702         vw->modified = TRUE;
703       }
704     }
705   }
706 }
707
708 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
709 {
710   a_clipboard_copy ( vw->viking_vlp );
711 }
712
713 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
714 {
715   a_clipboard_copy ( vw->viking_vlp );
716   menu_delete_layer_cb ( a, vw );
717 }
718
719 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
720 {
721   if ( a_clipboard_paste ( vw->viking_vlp ) )
722   {
723     draw_update ( vw );
724     vw->modified = TRUE;
725   }
726 }
727
728 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
729 {
730   if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
731     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
732 }
733
734 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
735 {
736   if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
737   {
738     vik_layers_panel_delete_selected ( vw->viking_vlp );
739     vw->modified = TRUE;
740   }
741   else
742     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
743 }
744
745 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
746 {
747   /* White Magic, my friends ... White Magic... */
748   int i, j;
749
750   if (!strcmp(gtk_action_get_name(a), "Zoom")) {
751     vw->current_tool = TOOL_ZOOM;
752   } 
753   else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
754     vw->current_tool = TOOL_RULER;
755   }
756   else {
757     for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
758       for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
759         if (!strcmp(vik_layer_get_interface(i)->tools[j].name, gtk_action_get_name(a))) {
760           vw->current_tool = TOOL_LAYER;
761           vw->tool_layer_id = i;
762           vw->tool_tool_id = j;
763         }
764       }
765     }
766   }
767   draw_status ( vw );
768 }
769
770 static void window_set_filename ( VikWindow *vw, const gchar *filename )
771 {
772   gchar *title;
773   if ( vw->filename )
774     g_free ( vw->filename );
775   if ( filename == NULL )
776   {
777     vw->filename = NULL;
778     gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
779   }
780   else
781   {
782     vw->filename = g_strdup(filename);
783     title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
784     gtk_window_set_title ( GTK_WINDOW(vw), title );
785     g_free ( title );
786   }
787 }
788
789 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
790 {
791   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
792   {
793     case 0:
794       a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
795       break;
796     case 1:
797     {
798       GtkWidget *mode_button;
799       gchar *buttonname;
800       if ( change_filename )
801         window_set_filename ( vw, filename );
802       switch ( vik_viewport_get_drawmode ( vw->viking_vvp ) ) {
803         case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
804         case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
805         case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
806         default: buttonname = "/ui/MainMenu/View/ModeKH";
807       }
808       mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
809       g_assert ( mode_button );
810       vw->only_updating_coord_mode_ui = TRUE; /* if we don't set this, it will change the coord to UTM if we click Lat/Lon. I don't know why. */
811       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
812       vw->only_updating_coord_mode_ui = FALSE;
813
814       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
815     }
816     default: draw_update ( vw );
817   }
818 }
819
820 static void load_file ( GtkAction *a, VikWindow *vw )
821 {
822   gboolean newwindow;
823   if (!strcmp(gtk_action_get_name(a), "Open")) {
824     newwindow = TRUE;
825   } 
826   else if (!strcmp(gtk_action_get_name(a), "Append")) {
827     newwindow = FALSE;
828   } 
829   else {
830     fprintf(stderr, "Houston we got a problem\n");
831     return;
832   }
833     
834   if ( ! vw->open_dia )
835   {
836     vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
837     gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
838     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
839     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
840   }
841   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
842   {
843     gtk_widget_hide ( vw->open_dia );
844     if ( (vw->modified || vw->filename) && newwindow )
845       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
846     else {
847       gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
848       gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
849       while ( *files ) {
850         vik_window_open_file ( vw, *files, change_fn );
851         files++;
852       }
853     }
854   }
855   else
856     gtk_widget_hide ( vw->open_dia );
857 }
858
859 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
860 {
861   gboolean rv = FALSE;
862   const gchar *fn;
863   if ( ! vw->save_dia )
864   {
865     vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
866     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
867     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
868   }
869
870   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
871   {
872     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
873     if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(vw->save_dia), "The file \"%s\" exists, do you wish to overwrite it?", a_file_basename ( fn ) ) )
874     {
875       window_set_filename ( vw, fn );
876       rv = window_save ( vw );
877       vw->modified = FALSE;
878       break;
879     }
880   }
881   gtk_widget_hide ( vw->save_dia );
882   return rv;
883 }
884
885 static gboolean window_save ( VikWindow *vw )
886 {
887   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
888     return TRUE;
889   else
890   {
891     a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
892     return FALSE;
893   }
894 }
895
896 static gboolean save_file ( GtkAction *a, VikWindow *vw )
897 {
898   if ( ! vw->filename )
899     return save_file_as ( NULL, vw );
900   else
901   {
902     vw->modified = FALSE;
903     return window_save ( vw );
904   }
905 }
906
907 static void clear_cb ( GtkAction *a, VikWindow *vw )
908 {
909   vik_layers_panel_clear ( vw->viking_vlp );
910   window_set_filename ( vw, NULL );
911   draw_update ( vw );
912 }
913
914 static void window_close ( GtkAction *a, VikWindow *vw )
915 {
916   if ( ! delete_event ( vw ) )
917     gtk_widget_destroy ( GTK_WIDGET(vw) );
918 }
919
920 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
921 {
922   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
923   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
924   {
925     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
926     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
927     draw_update ( vw );
928   }
929 }
930
931 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
932 {
933   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
934   GdkPixbuf *pixbuf_to_save;
935   gdouble old_xmpp, old_ympp;
936   GError *error = NULL;
937
938   /* backup old zoom & set new */
939   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
940   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
941   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
942
943   /* reset width and height: */
944   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
945
946   /* draw all layers */
947   draw_redraw ( vw );
948
949   /* save buffer as file. */
950   pixbuf_to_save = gdk_pixbuf_get_from_drawable ( NULL, GDK_DRAWABLE(vik_viewport_get_pixmap ( vw->viking_vvp )), NULL, 0, 0, 0, 0, w, h);
951   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
952   if (error)
953   {
954     fprintf(stderr, "Unable to write to file %s: %s", fn, error->message );
955     g_error_free (error);
956   }
957   g_object_unref ( G_OBJECT(pixbuf_to_save) );
958
959   /* pretend like nothing happened ;) */
960   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
961   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
962   vik_viewport_configure ( vw->viking_vvp );
963   draw_update ( vw );
964 }
965
966 static void save_image_dir ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png, guint tiles_w, guint tiles_h )
967 {
968   gulong size = sizeof(gchar) * (strlen(fn) + 15);
969   gchar *name_of_file = g_malloc ( size );
970   guint x = 1, y = 1;
971   struct UTM utm_orig, utm;
972
973   /* *** copied from above *** */
974   GdkPixbuf *pixbuf_to_save;
975   gdouble old_xmpp, old_ympp;
976   GError *error = NULL;
977
978   /* backup old zoom & set new */
979   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
980   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
981   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
982
983   /* reset width and height: do this only once for all images (same size) */
984   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
985   /* *** end copy from above *** */
986
987   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
988
989   make_dir(fn);
990
991   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
992
993   for ( y = 1; y <= tiles_h; y++ )
994   {
995     for ( x = 1; x <= tiles_w; x++ )
996     {
997       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, VIKING_FILE_SEP, y, x, save_as_png ? "png" : "jpg" );
998       utm = utm_orig;
999       if ( tiles_w & 0x1 )
1000         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1001       else
1002         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1003       if ( tiles_h & 0x1 ) /* odd */
1004         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1005       else /* even */
1006         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1007
1008       /* move to correct place. */
1009       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1010
1011       draw_redraw ( vw );
1012
1013       /* save buffer as file. */
1014       pixbuf_to_save = gdk_pixbuf_get_from_drawable ( NULL, GDK_DRAWABLE(vik_viewport_get_pixmap ( vw->viking_vvp )), NULL, 0, 0, 0, 0, w, h);
1015       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1016       if (error)
1017       {
1018         fprintf(stderr, "Unable to write to file %s: %s", name_of_file, error->message );
1019         g_error_free (error);
1020       }
1021
1022       g_object_unref ( G_OBJECT(pixbuf_to_save) );
1023     }
1024   }
1025
1026   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1027   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1028   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1029   vik_viewport_configure ( vw->viking_vvp );
1030   draw_update ( vw );
1031
1032   g_free ( name_of_file );
1033 }
1034
1035 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1036 {
1037   VikWindow *vw = VIK_WINDOW(pass_along[0]);
1038   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1039   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1040   gdouble width_min, width_max, height_min, height_max;
1041   gint width, height;
1042
1043   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1044   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1045
1046   /* TODO: support for xzoom and yzoom values */
1047   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1048   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1049
1050   if ( width > width_max || width < width_min || height > height_max || height < height_min )
1051     a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
1052
1053   gtk_spin_button_set_value ( width_spin, width );
1054   gtk_spin_button_set_value ( height_spin, height );
1055 }
1056
1057 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1058 {
1059   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1060   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1061   gchar *label_text;
1062   gdouble w, h;
1063   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1064   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1065   if (pass_along[4]) /* save many images; find TOTAL area covered */
1066   {
1067     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1068     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1069   }
1070   label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
1071   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1072   g_free ( label_text );
1073 }
1074
1075 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1076 {
1077   /* todo: default for answers inside VikWindow or static (thruout instance) */
1078   GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
1079                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1080                                                   GTK_STOCK_CANCEL,
1081                                                   GTK_RESPONSE_REJECT,
1082                                                   GTK_STOCK_OK,
1083                                                   GTK_RESPONSE_ACCEPT,
1084                                                   0 );
1085   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1086   GtkWidget *png_radio, *jpeg_radio;
1087   GtkWidget *current_window_button;
1088   gpointer current_window_pass_along[7];
1089   GtkWidget *zoom_label, *zoom_spin;
1090   GtkWidget *total_size_label;
1091
1092   /* only used if (!one_image_only) */
1093   GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1094
1095
1096   width_label = gtk_label_new ( "Width (pixels):" );
1097   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1098   height_label = gtk_label_new ( "Height (pixels):" );
1099   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1100
1101   zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
1102   /* TODO: separate xzoom and yzoom factors */
1103   zoom_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vik_viewport_get_xmpp(vw->viking_vvp), VIK_VIEWPORT_MIN_ZOOM, VIK_VIEWPORT_MAX_ZOOM/2.0, 1, 100, 3 )), 16, 3);
1104
1105   total_size_label = gtk_label_new ( NULL );
1106
1107   current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
1108   current_window_pass_along [0] = vw;
1109   current_window_pass_along [1] = width_spin;
1110   current_window_pass_along [2] = height_spin;
1111   current_window_pass_along [3] = zoom_spin;
1112   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1113   current_window_pass_along [5] = NULL;
1114   current_window_pass_along [6] = total_size_label;
1115   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1116
1117   png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
1118   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
1119
1120   if ( ! vw->draw_image_save_as_png )
1121     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1122
1123   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1124   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1125   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1126   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1127   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1128   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1129   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1130   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1131   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1132
1133   if ( ! one_image_only )
1134   {
1135     GtkWidget *tiles_width_label, *tiles_height_label;
1136
1137
1138     tiles_width_label = gtk_label_new ( "East-west image tiles:" );
1139     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1140     tiles_height_label = gtk_label_new ( "North-south image tiles:" );
1141     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1142     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1143     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1144     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1145     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1146
1147     current_window_pass_along [4] = tiles_width_spin;
1148     current_window_pass_along [5] = tiles_height_spin;
1149     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1150     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1151   }
1152   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1153   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1154   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1155   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1156
1157   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1158
1159   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1160
1161   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1162   {
1163     gtk_widget_hide ( GTK_WIDGET(dialog) );
1164     if ( one_image_only )
1165       save_image_file ( vw, fn, 
1166                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1167                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1168                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1169                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1170     else {
1171       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1172         save_image_dir ( vw, fn, 
1173                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1174                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1175                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1176                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1177                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1178                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1179       else
1180         a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
1181     }
1182   }
1183   gtk_widget_destroy ( GTK_WIDGET(dialog) );
1184 }
1185
1186
1187 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1188 {
1189   GtkWidget *file_selector;
1190   const gchar *fn;
1191   file_selector = gtk_file_selection_new ("Save Image");
1192
1193   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
1194   {
1195     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
1196     if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(file_selector), "The file \"%s\" exists, do you wish to overwrite it?", a_file_basename ( fn ) ) )
1197     {
1198       gtk_widget_hide ( file_selector );
1199       draw_to_image_file ( vw, fn, TRUE );
1200       break;
1201     }
1202   }
1203   gtk_widget_destroy ( file_selector );
1204 }
1205
1206 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1207 {
1208   GtkWidget *file_selector;
1209   const gchar *fn;
1210   file_selector = gtk_file_selection_new ("Choose a name for a new directory to hold images");
1211
1212   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
1213   {
1214     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
1215     if ( access ( fn, F_OK ) == 0 )
1216       a_dialog_info_msg_extra ( GTK_WINDOW(file_selector), "The file %s exists. Please choose a name for a new directory to hold images in that does not exist.", a_file_basename(fn) );
1217     else
1218     {
1219       gtk_widget_hide ( file_selector );
1220       draw_to_image_file ( vw, fn, FALSE );
1221       break;
1222     }
1223   }
1224   gtk_widget_destroy ( file_selector );
1225 }
1226
1227 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1228 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1229 {
1230   VikViewportDrawMode drawmode;
1231   if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1232     drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1233   }
1234   else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1235     drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1236   }
1237   else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1238     drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1239   }
1240   else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1241     drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1242   }
1243   else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1244     drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1245   }
1246   else {
1247     fprintf(stderr, "Houston, we got a problem!\n");
1248     return;
1249   }
1250
1251   if ( !vw->only_updating_coord_mode_ui )
1252   {
1253     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1254     if ( olddrawmode != drawmode )
1255     {
1256       /* this takes care of coord mode too */
1257       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1258       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1259         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1260       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1261         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1262       }
1263       draw_update ( vw );
1264     }
1265   }
1266 }
1267
1268 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1269 {
1270   vik_viewport_set_draw_scale ( vw->viking_vvp, !vik_viewport_get_draw_scale(vw->viking_vvp) );
1271   draw_update ( vw );
1272 }
1273
1274 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1275 {
1276   GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1277   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1278   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1279   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1280   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1281   {
1282     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1283     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1284     draw_update ( vw );
1285   }
1286   g_free ( color );
1287   gtk_widget_destroy ( colorsd );
1288 }
1289
1290 static GtkActionEntry entries[] = {
1291   { "File", NULL, "_File", 0, 0, 0 },
1292   { "View", NULL, "_View", 0, 0, 0 },
1293   { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1294   { "Layers", NULL, "_Layers", 0, 0, 0 },
1295   { "Tools", NULL, "_Tools", 0, 0, 0 },
1296
1297   { "New",       GTK_STOCK_NEW,          "_New",                          "<control>N", "New file",                                     (GCallback)newwindow_cb          },
1298   { "Open",      GTK_STOCK_OPEN,         "_Open",                         "<control>O", "Open a file",                                  (GCallback)load_file             },
1299   { "Append",    GTK_STOCK_ADD,          "A_ppend File",                  NULL,         "Append data from a different file",            (GCallback)load_file             },
1300   { "Save",      GTK_STOCK_SAVE,         "_Save",                         "<control>S", "Save the file",                                (GCallback)save_file             },
1301   { "SaveAs",    GTK_STOCK_SAVE_AS,      "Save _As",                      NULL,         "Save the file under different name",           (GCallback)save_file_as          },
1302   { "GenImg",    GTK_STOCK_CLEAR,        "_Generate Image File",          NULL,         "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1303   { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL,         "FIXME:IMGDIR",                                 (GCallback)draw_to_image_dir_cb  },
1304   { "Exit",      GTK_STOCK_QUIT,         "E_xit",                         "<control>W", "Exit the program",                             (GCallback)window_close          },
1305
1306   { "GotoLL",    GTK_STOCK_QUIT,         "_Go to Lat\\/Lon...",           NULL,         "Go to arbitrary lat\\/lon coordinate",         (GCallback)draw_goto_cb          },
1307   { "GotoUTM",   GTK_STOCK_QUIT,         "Go to UTM...",                  NULL,         "Go to arbitrary UTM coordinate",               (GCallback)draw_goto_cb          },
1308   { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...",       NULL,         NULL,                                           (GCallback)set_bg_color          },
1309   { "ZoomIn",    GTK_STOCK_ZOOM_IN,      "Zoom _In",                   "<control>plus", NULL,                                           (GCallback)draw_zoom_cb          },
1310   { "ZoomOut",   GTK_STOCK_ZOOM_OUT,     "Zoom _Out",                 "<control>minus", NULL,                                           (GCallback)draw_zoom_cb          },
1311   { "ZoomTo",    GTK_STOCK_ZOOM_FIT,     "Zoom _To",               "<control><shift>Z", NULL,                                           (GCallback)zoom_to_cb            },
1312   { "Zoom0.25",  NULL,                   "0.25",                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1313   { "Zoom0.5",   NULL,                   "0.5",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1314   { "Zoom1",     NULL,                   "1",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1315   { "Zoom2",     NULL,                   "2",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1316   { "Zoom4",     NULL,                   "4",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1317   { "Zoom8",     NULL,                   "8",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1318   { "Zoom16",    NULL,                   "16",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1319   { "Zoom32",    NULL,                   "32",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1320   { "Zoom64",    NULL,                   "64",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1321   { "Zoom128",   NULL,                   "128",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1322   { "BGJobs",    GTK_STOCK_EXECUTE,      "Background _Jobs",              NULL,         NULL,                                           (GCallback)a_background_show_window },
1323
1324   { "Cut",       GTK_STOCK_CUT,          "Cu_t",                          NULL,         NULL,                                           (GCallback)menu_cut_layer_cb     },
1325   { "Copy",      GTK_STOCK_COPY,         "_Copy",                         NULL,         NULL,                                           (GCallback)menu_copy_layer_cb    },
1326   { "Paste",     GTK_STOCK_PASTE,        "_Paste",                        NULL,         NULL,                                           (GCallback)menu_paste_layer_cb   },
1327   { "Delete",    GTK_STOCK_DELETE,       "_Delete",                       NULL,         NULL,                                           (GCallback)menu_delete_layer_cb  },
1328   { "DeleteAll", NULL,                   "Delete All",                    NULL,         NULL,                                           (GCallback)clear_cb              },
1329   { "Properties",GTK_STOCK_PROPERTIES,   "_Properties",                   NULL,         NULL,                                           (GCallback)menu_properties_cb    },
1330 };
1331
1332 /* Radio items */
1333 static GtkRadioActionEntry mode_entries[] = {
1334   { "ModeUTM",         NULL,         "_UTM Mode",               "<control>u", NULL, 0 },
1335   { "ModeExpedia",     NULL,         "_Expedia Mode",           "<control>e", NULL, 1 },
1336   { "ModeGoogle",      NULL,         "_Google Mode",            "<control>g", NULL, 2 },
1337   { "ModeKH",          NULL,         "_KH\\/Flat LatLon Mode",  "<control>k", NULL, 3 },
1338   { "ModeMercator",    NULL,         "_Mercator (New Google)",  "<control>m", NULL, 4 }
1339 };
1340
1341 static GtkRadioActionEntry tool_entries[] = {
1342   { "Zoom",      "vik-icon-zoom",        "_Zoom",                         "<control><shift>Z", "Zoom Tool",  0 },
1343   { "Ruler",     "vik-icon-ruler",       "_Ruler",                        "<control><shift>R", "Ruler Tool", 1 }
1344 };
1345
1346 static GtkToggleActionEntry toggle_entries[] = {
1347   { "ShowScale", NULL,                   "Show Scale",                    NULL,         NULL,                                           (GCallback)set_draw_scale, TRUE   },
1348 };
1349
1350 #include "menu.xml.h"
1351 static void window_create_ui( VikWindow *window )
1352 {
1353   GtkUIManager *uim;
1354   GtkActionGroup *action_group;
1355   GtkAccelGroup *accel_group;
1356   GError *error;
1357   guint i, j, mid;
1358   GtkIconFactory *icon_factory;
1359   GtkIconSet *icon_set; 
1360   GtkRadioActionEntry *tools = NULL, *radio;
1361   guint ntools;
1362   
1363   uim = gtk_ui_manager_new ();
1364   window->uim = uim;
1365
1366   
1367   error = NULL;
1368   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1369     g_error_free (error);
1370     exit (1);
1371   }
1372
1373   action_group = gtk_action_group_new ("MenuActions");
1374   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1375   gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1376   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 0, (GCallback)window_change_coord_mode_cb, window);
1377
1378   icon_factory = gtk_icon_factory_new ();
1379
1380   register_vik_icons(icon_factory);
1381
1382   ntools = 0;
1383   for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1384       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1385       radio = &tools[ntools];
1386       ntools++;
1387       *radio = tool_entries[i];
1388       radio->value = ntools;
1389   }  
1390
1391   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1392     GtkActionEntry action;
1393     gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Layers/", 
1394                           vik_layer_get_interface(i)->name,
1395                           vik_layer_get_interface(i)->name,
1396                           GTK_UI_MANAGER_MENUITEM, FALSE);
1397
1398     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1399     gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1400     gtk_icon_set_unref (icon_set);
1401
1402     action.name = vik_layer_get_interface(i)->name;
1403     action.stock_id = vik_layer_get_interface(i)->name;
1404     action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1405     action.accelerator = NULL;
1406     action.tooltip = NULL;
1407     action.callback = (GCallback)menu_addlayer_cb;
1408     gtk_action_group_add_actions(action_group, &action, 1, window);
1409
1410     if ( vik_layer_get_interface(i)->tools_count ) {
1411       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1412       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1413     }
1414
1415     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1416       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1417       radio = &tools[ntools];
1418       ntools++;
1419       
1420       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
1421                             vik_layer_get_interface(i)->tools[j].name,
1422                             vik_layer_get_interface(i)->tools[j].name,
1423                             GTK_UI_MANAGER_MENUITEM, FALSE);
1424       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems", 
1425                             vik_layer_get_interface(i)->tools[j].name,
1426                             vik_layer_get_interface(i)->tools[j].name,
1427                             GTK_UI_MANAGER_TOOLITEM, FALSE);
1428
1429       radio->name = vik_layer_get_interface(i)->tools[j].name;
1430       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1431       radio->label = vik_layer_get_interface(i)->tools[j].name;
1432       radio->accelerator = NULL;
1433       radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1434       radio->value = ntools;
1435     }
1436   }
1437
1438   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1439   g_free(tools);
1440
1441   gtk_ui_manager_insert_action_group (uim, action_group, 0);
1442
1443   accel_group = gtk_ui_manager_get_accel_group (uim);
1444   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1445   gtk_ui_manager_ensure_update (uim);
1446   gtk_icon_factory_add_default (icon_factory); 
1447   g_object_unref (icon_factory);
1448 }
1449
1450
1451 #include "icons/icons.h"
1452 static struct { 
1453   const GdkPixdata *data;
1454   gchar *stock_id;
1455 } stock_icons[] = {
1456   { &addtr_18,          "Create Track"      },
1457   { &edtr_18,           "Edit Trackpoint"   },
1458   { &addwp_18,          "Create Waypoint"   },
1459   { &edwp_18,           "Edit Waypoint"     },
1460   { &zoom_18,           "vik-icon-zoom"     },
1461   { &ruler_18,          "vik-icon-ruler"    },
1462   { &geozoom_18,        "Georef Zoom Tool"  },
1463   { &geomove_18,        "Georef Move Map"   },
1464   { &mapdl_18,          "Maps Download"     },
1465   { &showpic_18,        "Show Picture"      },
1466 };
1467  
1468
1469 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1470
1471 static void
1472 register_vik_icons (GtkIconFactory *icon_factory)
1473 {
1474   GtkIconSet *icon_set; 
1475   gint i;
1476
1477   for (i = 0; i < n_stock_icons; i++) {
1478     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
1479                    stock_icons[i].data, FALSE, NULL ));
1480     gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1481     gtk_icon_set_unref (icon_set);
1482   }
1483 }