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