]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
2ed82ec79e7ea588eaa57e24f447330b4106d684
[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 #include "datasources.h"
25 #include "googlesearch.h"
26 #include "dems.h"
27
28 #define VIKING_TITLE " - Viking"
29
30 #include <glib/gprintf.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <glib/gprintf.h>
36 #ifdef WINDOWS
37 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
38 #define make_dir(dir) mkdir(dir)
39 #else
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #define make_dir(dir) mkdir(dir,0777)
43 #endif
44
45 #define VIKING_WINDOW_WIDTH      1000
46 #define VIKING_WINDOW_HEIGHT     800
47 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
48 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
49 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
50
51 static void window_finalize ( GObject *gob );
52 static GObjectClass *parent_class;
53
54 static void window_init ( VikWindow *vw );
55 static void window_class_init ( VikWindowClass *klass );
56
57 static void draw_update ( VikWindow *vw );
58
59 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
60
61 /* Drawing & stuff */
62
63 static gboolean delete_event( VikWindow *vw );
64
65 static void window_configure_event ( VikWindow *vw );
66 static void draw_sync ( VikWindow *vw );
67 static void draw_redraw ( VikWindow *vw );
68 static void draw_scroll  ( VikWindow *vw, GdkEventScroll *event );
69 static void draw_click  ( VikWindow *vw, GdkEventButton *event );
70 static void draw_release ( VikWindow *vw, GdkEventButton *event );
71 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
72 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
73 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
74
75 static void draw_status ();
76
77 /* End Drawing Functions */
78
79 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
80 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
81 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
82
83 /* tool management */
84 typedef struct {
85   VikToolInterface ti;
86   gpointer state;
87   gint layer_type;
88 } toolbox_tool_t;
89 #define TOOL_LAYER_TYPE_NONE -1
90
91 typedef struct {
92   int                   active_tool;
93   int                   n_tools;
94   toolbox_tool_t        *tools;
95   VikWindow *vw;
96 } toolbox_tools_t;
97
98 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
99 static toolbox_tools_t* toolbox_create(VikWindow *vw);
100 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
101 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
102 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
103 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
104 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
105 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
106
107
108 /* ui creation */
109 static void window_create_ui( VikWindow *window );
110 static void register_vik_icons (GtkIconFactory *icon_factory);
111
112 /* i/o */
113 static void load_file ( GtkAction *a, VikWindow *vw );
114 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
115 static gboolean save_file ( GtkAction *a, VikWindow *vw );
116 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
117 static gboolean window_save ( VikWindow *vw );
118
119 struct _VikWindow {
120   GtkWindow gtkwindow;
121   VikViewport *viking_vvp;
122   VikLayersPanel *viking_vlp;
123   VikStatusbar *viking_vs;
124
125   GtkToolbar *toolbar;
126
127   GtkItemFactory *item_factory;
128
129   /* tool management state */
130   guint current_tool;
131   toolbox_tools_t *vt;
132   guint16 tool_layer_id;
133   guint16 tool_tool_id;
134
135   GtkActionGroup *action_group;
136
137   gint pan_x, pan_y;
138
139   guint draw_image_width, draw_image_height;
140   gboolean draw_image_save_as_png;
141
142   gchar *filename;
143   gboolean modified;
144
145   GtkWidget *open_dia, *save_dia;
146   GtkWidget *save_img_dia, *save_img_dir_dia;
147
148   gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
149   GtkUIManager *uim;
150
151   /* half-drawn update */
152   VikLayer *trigger;
153   VikCoord trigger_center;
154 };
155
156 enum {
157  TOOL_ZOOM = 0,
158  TOOL_RULER,
159  TOOL_LAYER,
160  NUMBER_OF_TOOLS
161 };
162
163 enum {
164   VW_NEWWINDOW_SIGNAL,
165   VW_OPENWINDOW_SIGNAL,
166   VW_LAST_SIGNAL
167 };
168
169 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
170
171 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
172
173 GdkCursor *vw_cursor_zoom = NULL;
174 GdkCursor *vw_cursor_ruler = NULL;
175
176 GType vik_window_get_type (void)
177 {
178   static GType vw_type = 0;
179
180   if (!vw_type)
181   {
182     static const GTypeInfo vw_info = 
183     {
184       sizeof (VikWindowClass),
185       NULL, /* base_init */
186       NULL, /* base_finalize */
187       (GClassInitFunc) window_class_init, /* class_init */
188       NULL, /* class_finalize */
189       NULL, /* class_data */
190       sizeof (VikWindow),
191       0,
192       (GInstanceInitFunc) window_init,
193     };
194     vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
195   }
196
197   return vw_type;
198 }
199
200 VikViewport * vik_window_viewport(VikWindow *vw)
201 {
202   return(vw->viking_vvp);
203 }
204
205 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
206 {
207   int i, j, tool_count;
208   VikLayerInterface *layer_interface;
209
210   if (!vw->action_group) return;
211
212   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
213     GtkAction *action;
214     layer_interface = vik_layer_get_interface(i);
215     tool_count = layer_interface->tools_count;
216
217     for (j = 0; j < tool_count; j++) {
218       action = gtk_action_group_get_action(vw->action_group,
219             layer_interface->tools[j].name);
220       g_object_set(action, "sensitive", i == vl->type, NULL);
221     }
222   }
223 }
224
225 static void window_finalize ( GObject *gob )
226 {
227   VikWindow *vw = VIK_WINDOW(gob);
228   g_return_if_fail ( vw != NULL );
229
230   a_background_remove_status ( vw->viking_vs );
231
232   G_OBJECT_CLASS(parent_class)->finalize(gob);
233 }
234
235
236 static void window_class_init ( VikWindowClass *klass )
237 {
238   /* destructor */
239   GObjectClass *object_class;
240
241   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);
242   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);
243
244   object_class = G_OBJECT_CLASS (klass);
245
246   object_class->finalize = window_finalize;
247
248   parent_class = g_type_class_peek_parent (klass);
249
250 }
251
252 static void window_init ( VikWindow *vw )
253 {
254   GtkWidget *main_vbox;
255   GtkWidget *hpaned;
256
257   vw->action_group = NULL;
258
259   vw->viking_vvp = vik_viewport_new();
260   vw->viking_vlp = vik_layers_panel_new();
261   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
262   vw->viking_vs = vik_statusbar_new();
263
264   vw->vt = toolbox_create(vw);
265   window_create_ui(vw);
266   gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
267   
268   toolbox_activate(vw->vt, "Zoom");
269
270   vw->filename = NULL;
271   vw->item_factory = NULL;
272
273   vw->modified = FALSE;
274   vw->only_updating_coord_mode_ui = FALSE;
275   
276
277   vw->pan_x = vw->pan_y = -1;
278   vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
279   vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
280   vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
281
282   main_vbox = gtk_vbox_new(FALSE, 1);
283   gtk_container_add (GTK_CONTAINER (vw), main_vbox);
284
285   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
286   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
287   gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
288   gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
289
290   g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
291
292   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
293   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
294   gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
295   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
296   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
297   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
298   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
299   g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
300
301   gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
302
303   hpaned = gtk_hpaned_new ();
304   gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
305   gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
306
307   /* This packs the button into the window (a gtk container). */
308   gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
309
310   gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
311
312   a_background_add_status(vw->viking_vs);
313
314   vw->open_dia = NULL;
315   vw->save_dia = NULL;
316   vw->save_img_dia = NULL;
317   vw->save_img_dir_dia = NULL;
318 }
319
320 VikWindow *vik_window_new ()
321 {
322   return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
323 }
324
325 static gboolean delete_event( VikWindow *vw )
326 {
327 #ifdef VIKING_PROMPT_IF_MODIFIED
328   if ( vw->modified )
329 #else
330   if (0)
331 #endif
332   {
333     GtkDialog *dia;
334     dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
335       "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.",
336       vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
337     gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
338     switch ( gtk_dialog_run ( dia ) )
339     {
340       case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
341       case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
342       default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
343     }
344   }
345   return FALSE;
346 }
347
348 /* Drawing stuff */
349 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
350 {
351   g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
352 }
353
354 static void draw_update ( VikWindow *vw )
355 {
356   draw_redraw (vw);
357   draw_sync (vw);
358 }
359
360 static void draw_sync ( VikWindow *vw )
361 {
362   vik_viewport_sync(vw->viking_vvp);
363   draw_status ( vw );
364   /* other things may be necc here later. */
365 }
366
367 static void draw_status ( VikWindow *vw )
368 {
369   static gchar zoom_level[22];
370   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" );
371   if ( vw->current_tool == TOOL_LAYER )
372     vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
373   else
374     vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
375
376   vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
377 }
378
379 void vik_window_set_redraw_trigger(VikLayer *vl)
380 {
381   VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
382   vw->trigger = vl;
383 }
384
385 static void window_configure_event ( VikWindow *vw )
386 {
387   draw_redraw ( vw );
388   gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
389 }
390
391 static void draw_redraw ( VikWindow *vw )
392 {
393   VikCoord old_center = vw->trigger_center;
394   vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
395   VikLayer *new_trigger = vw->trigger;
396   vw->trigger = NULL;
397   VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
398
399   if ( ! new_trigger )
400     ; /* do nothing -- have to redraw everything. */
401   else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
402     vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
403   else
404     vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
405
406   /* actually draw */
407   vik_viewport_clear ( vw->viking_vvp);
408   vik_layers_panel_draw_all ( vw->viking_vlp );
409   vik_viewport_draw_scale ( vw->viking_vvp );
410   vik_viewport_draw_centermark ( vw->viking_vvp );
411
412   vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
413 }
414
415 gboolean draw_buf_done = TRUE;
416
417 static gboolean draw_buf(gpointer data)
418 {
419   gpointer *pass_along = data;
420   gdk_threads_enter();
421   gdk_draw_drawable (pass_along[0], pass_along[1],
422                      pass_along[2], 0, 0, 0, 0, -1, -1);
423   draw_buf_done = TRUE;
424   gdk_threads_leave();
425   return FALSE;
426 }
427
428
429 /* Mouse event handlers ************************************************************************/
430
431 static void draw_click (VikWindow *vw, GdkEventButton *event)
432 {
433
434   /* middle button pressed.  we reserve all middle button and scroll events
435    * for panning and zooming; tools only get left/right/movement 
436    */
437   if ( event->button == 2) {
438     /* set panning origin */
439     vw->pan_x = (gint) event->x;
440     vw->pan_y = (gint) event->y;
441   } 
442   else {
443     toolbox_click(vw->vt, event);
444   }
445 }
446
447 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
448 {
449   static VikCoord coord;
450   static struct UTM utm;
451   static struct LatLon ll;
452   static char pointer_buf[36];
453   gint16 alt;
454
455   toolbox_move(vw->vt, (GdkEventButton *)event);
456
457   vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
458   vik_coord_to_utm ( &coord, &utm );
459   a_coords_utm_to_latlon ( &utm, &ll );
460   if ((alt = a_dems_get_elev_by_coord(&coord)) != VIK_DEM_INVALID_ELEVATION)
461     g_snprintf ( pointer_buf, 36, "Cursor: %f %f %dm", ll.lat, ll.lon, alt );
462   else
463     g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
464   vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
465
466   if ( vw->pan_x != -1 ) {
467     vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
468   }
469 }
470
471 static void draw_release ( VikWindow *vw, GdkEventButton *event )
472 {
473   if ( event->button == 2 ) {  /* move / pan */
474     if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
475         vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
476       else
477          vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
478                                          vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
479       draw_update ( vw );
480       vw->pan_x = vw->pan_y = -1;
481   }
482   else {
483     toolbox_release(vw->vt, event);
484   }
485 }
486
487 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
488 {
489   if ( event->state == GDK_CONTROL_MASK ) {
490     /* control == pan up & down */
491     if ( event->direction == GDK_SCROLL_UP )
492       vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
493     else
494       vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)*2/3 );
495   } else if ( event->state == GDK_SHIFT_MASK ) {
496     /* control-shift == pan left & right */
497     if ( event->direction == GDK_SCROLL_UP )
498       vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
499     else
500       vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)*2/3, vik_viewport_get_height(vw->viking_vvp)/2 );
501   } else if ( event->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
502     if ( event->direction == GDK_SCROLL_UP ) {
503       /* make sure mouse is still over the same point on the map when we zoom */
504       VikCoord coord;
505       gint x, y;
506       gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
507       gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
508       vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
509       vik_viewport_zoom_in (vw->viking_vvp);
510       vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
511       vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
512                                 center_y + (y - event->y) );
513     }
514     else {
515       VikCoord coord;
516       gint x, y;
517       gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
518       gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
519       vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
520       vik_viewport_zoom_out(vw->viking_vvp);
521       vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
522       vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
523                                 center_y + (y - event->y) );
524     }
525   } else {
526     if ( event->direction == GDK_SCROLL_UP )
527       vik_viewport_zoom_in (vw->viking_vvp);
528     else
529       vik_viewport_zoom_out (vw->viking_vvp);
530   }
531
532   draw_update(vw);
533 }
534
535
536
537 /********************************************************************************
538  ** Ruler tool code
539  ********************************************************************************/
540 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
541 {
542   PangoFontDescription *pfd;
543   PangoLayout *pl;
544   gchar str[128];
545   GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
546   GdkGC *thickgc = gdk_gc_new(d);
547   
548   gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
549   gdouble dx = (x2-x1)/len*10; 
550   gdouble dy = (y2-y1)/len*10;
551   gdouble c = cos(15.0 * M_PI/180.0);
552   gdouble s = sin(15.0 * M_PI/180.0);
553   gdouble angle;
554   gdouble baseangle = 0;
555   gint i;
556
557   /* draw line with arrow ends */
558   {
559     gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
560     a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
561     gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
562   }
563
564   a_viewport_clip_line(&x1, &y1, &x2, &y2);
565   gdk_draw_line(d, gc, x1, y1, x2, y2);
566
567   gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
568   gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
569   gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
570   gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
571   gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
572   gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
573
574   /* draw compass */
575 #define CR 80
576 #define CW 4
577   angle = atan2(dy, dx) + M_PI_2;
578
579   if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
580     VikCoord test;
581     struct LatLon ll;
582     struct UTM u;
583     gint tx, ty;
584
585     vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
586     vik_coord_to_latlon ( &test, &ll );
587     ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
588     a_coords_latlon_to_utm ( &ll, &u );
589     vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
590     vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
591
592     baseangle = M_PI - atan2(tx-x1, ty-y1);
593     angle -= baseangle;
594   }
595
596   if (angle<0) 
597     angle+=2*M_PI;
598   if (angle>2*M_PI)
599     angle-=2*M_PI;
600
601   {
602     GdkColor color;
603     gdk_gc_copy(thickgc, gc);
604     gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
605     gdk_color_parse("#2255cc", &color);
606     gdk_gc_set_rgb_fg_color(thickgc, &color);
607   }
608   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);
609
610
611   gdk_gc_copy(thickgc, gc);
612   gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
613   for (i=0; i<180; i++) {
614     c = cos(i*M_PI/90.0 + baseangle);
615     s = sin(i*M_PI/90.0 + baseangle);
616
617     if (i%5) {
618       gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
619     } else {
620       gdouble ticksize = 2*CW;
621       gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
622     }
623   }
624
625   gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
626   gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
627   gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
628   c = (CR+CW*2)*cos(baseangle);
629   s = (CR+CW*2)*sin(baseangle);
630   gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
631   gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
632
633   /* draw labels */
634 #define LABEL(x, y, w, h) { \
635     gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
636     gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
637     gdk_draw_layout(d, gc, (x), (y), pl); } 
638   {
639     gint wd, hd, xd, yd;
640     gint wb, hb, xb, yb;
641
642     pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
643
644     pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
645     pango_layout_set_font_description (pl, pfd);
646     pango_font_description_free (pfd);
647
648     pango_layout_set_text(pl, "N", -1);
649     gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
650
651     /* draw label with distance */
652     if (distance >= 1000 && distance < 100000) {
653       g_sprintf(str, "%3.2f km", distance/1000.0);
654     } else if (distance < 1000) {
655       g_sprintf(str, "%d m", (int)distance);
656     } else {
657       g_sprintf(str, "%d km", (int)distance/1000);
658     }
659     pango_layout_set_text(pl, str, -1);
660
661     pango_layout_get_pixel_size ( pl, &wd, &hd );
662     if (dy>0) {
663       xd = (x1+x2)/2 + dy;
664       yd = (y1+y2)/2 - hd/2 - dx;
665     } else {
666       xd = (x1+x2)/2 - dy;
667       yd = (y1+y2)/2 - hd/2 + dx;
668     }
669
670     if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
671       xd = x2 + 10;
672       yd = y2 - 5;
673     }
674
675     LABEL(xd, yd, wd, hd);
676
677     /* draw label with bearing */
678     g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
679     pango_layout_set_text(pl, str, -1);
680     pango_layout_get_pixel_size ( pl, &wb, &hb );
681     xb = x1 + CR*cos(angle-M_PI_2);
682     yb = y1 + CR*sin(angle-M_PI_2);
683
684     if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
685       xb = x2 + 10;
686       yb = y2 + 10;
687     }
688
689     {
690       GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
691       if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
692         xb = xd + wd + 5;
693       }
694     }
695     LABEL(xb, yb, wb, hb);
696   }
697 #undef LABEL
698
699   g_object_unref ( G_OBJECT ( pl ) );
700   g_object_unref ( G_OBJECT ( labgc ) );
701   g_object_unref ( G_OBJECT ( thickgc ) );
702 }
703
704 typedef struct {
705   VikWindow *vw;
706   VikViewport *vvp;
707   gboolean has_oldcoord;
708   VikCoord oldcoord;
709 } ruler_tool_state_t;
710
711 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp) 
712 {
713   ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
714   s->vw = vw;
715   s->vvp = vvp;
716   s->has_oldcoord = FALSE;
717   return s;
718 }
719
720 static void ruler_destroy (ruler_tool_state_t *s)
721 {
722   g_free(s);
723 }
724
725 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
726 {
727   struct LatLon ll;
728   VikCoord coord;
729   gchar *temp;
730   if ( event->button == 1 ) {
731     vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
732     vik_coord_to_latlon ( &coord, &ll );
733     if ( s->has_oldcoord ) {
734       temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
735       s->has_oldcoord = FALSE;
736     }
737     else {
738       temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
739       s->has_oldcoord = TRUE;
740     }
741
742     vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
743     g_free ( temp );
744
745     s->oldcoord = coord;
746   }
747   else {
748     vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
749     draw_update ( s->vw );
750   }
751   return VIK_LAYER_TOOL_ACK;
752 }
753
754 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
755 {
756   VikViewport *vvp = s->vvp;
757   VikWindow *vw = s->vw;
758
759   struct LatLon ll;
760   VikCoord coord;
761   gchar *temp;
762
763   if ( s->has_oldcoord ) {
764     int oldx, oldy, w1, h1, w2, h2;
765     static GdkPixmap *buf = NULL;
766     w1 = vik_viewport_get_width(vvp); 
767     h1 = vik_viewport_get_height(vvp);
768     if (!buf) {
769       buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
770     }
771     gdk_drawable_get_size(buf, &w2, &h2);
772     if (w1 != w2 || h1 != h2) {
773       g_object_unref ( G_OBJECT ( buf ) );
774       buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
775     }
776
777     vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
778     vik_coord_to_latlon ( &coord, &ll );
779     vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
780
781     gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc, 
782                        vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
783     draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
784     if (draw_buf_done) {
785       static gpointer pass_along[3];
786       pass_along[0] = GTK_WIDGET(vvp)->window;
787       pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
788       pass_along[2] = buf;
789       g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
790       draw_buf_done = FALSE;
791     }
792
793     temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
794     vik_statusbar_set_message ( vw->viking_vs, 3, temp );
795     g_free ( temp );
796   }
797   return VIK_LAYER_TOOL_ACK;
798 }
799
800 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
801 {
802   return VIK_LAYER_TOOL_ACK;
803 }
804
805 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
806 {
807   draw_update ( s->vw );
808 }
809
810 static VikToolInterface ruler_tool = 
811   { "Ruler", 
812     (VikToolConstructorFunc) ruler_create,
813     (VikToolDestructorFunc) ruler_destroy,
814     (VikToolActivationFunc) NULL,
815     (VikToolActivationFunc) ruler_deactivate, 
816     (VikToolMouseFunc) ruler_click, 
817     (VikToolMouseFunc) ruler_move, 
818     (VikToolMouseFunc) ruler_release };
819 /*** end ruler code ********************************************************/
820
821
822
823 /********************************************************************************
824  ** Zoom tool code
825  ********************************************************************************/
826 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
827 {
828   return vw;
829 }
830
831 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
832 {
833   vw->modified = TRUE;
834   vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
835   if ( event->button == 1 )
836     vik_viewport_zoom_in (vw->viking_vvp);
837   else if ( event->button == 3 )
838     vik_viewport_zoom_out (vw->viking_vvp);
839   draw_update ( vw );
840   return VIK_LAYER_TOOL_ACK;
841 }
842
843 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
844 {
845   return VIK_LAYER_TOOL_ACK;
846 }
847
848 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
849 {
850   return VIK_LAYER_TOOL_ACK;
851 }
852
853 static VikToolInterface zoom_tool = 
854   { "Zoom", 
855     (VikToolConstructorFunc) zoomtool_create,
856     (VikToolDestructorFunc) NULL,
857     (VikToolActivationFunc) NULL,
858     (VikToolActivationFunc) NULL,
859     (VikToolMouseFunc) zoomtool_click, 
860     (VikToolMouseFunc) zoomtool_move,
861     (VikToolMouseFunc) zoomtool_release };
862 /*** end ruler code ********************************************************/
863
864 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
865 {
866   if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
867     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
868   } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
869     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
870   } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
871     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
872   } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
873     vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
874   }
875   draw_update ( vw );
876 }
877
878
879 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
880 {
881   guint what = 128;
882
883   if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
884     what = -3;
885   } 
886   else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
887     what = -4;
888   }
889   else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
890     what = -2;
891   }
892   else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
893     what = -1;
894   }
895   else {
896     gchar *s = (gchar *)gtk_action_get_name(a);
897     what = atoi(s+4);
898   }
899
900   switch (what)
901   {
902     case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
903     case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
904     case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
905     case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
906     default: vik_viewport_set_zoom ( vw->viking_vvp, what );
907   }
908   draw_update ( vw );
909 }
910
911 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
912 {
913   VikCoord new_center;
914
915   if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
916     struct LatLon ll, llold;
917     vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
918     if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
919       vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
920     else
921       return;
922   }
923   else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
924     struct UTM utm, utmold;
925     vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
926     if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
927       vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
928     else
929      return;
930   }
931   else {
932     g_critical("Houston we have a problem\n");
933     return;
934   }
935
936   vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
937   draw_update ( vw );
938 }
939
940 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
941 {
942   gint type;
943   for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
944     if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
945       if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
946         draw_update ( vw );
947         vw->modified = TRUE;
948       }
949     }
950   }
951 }
952
953 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
954 {
955   a_clipboard_copy_selected ( vw->viking_vlp );
956 }
957
958 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
959 {
960   a_clipboard_copy_selected ( vw->viking_vlp );
961   menu_delete_layer_cb ( a, vw );
962 }
963
964 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
965 {
966   if ( a_clipboard_paste ( vw->viking_vlp ) )
967   {
968     draw_update ( vw );
969     vw->modified = TRUE;
970   }
971 }
972
973 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
974 {
975   if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
976     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
977 }
978
979 static void help_about_cb ( GtkAction *a, VikWindow *vw )
980 {
981   a_dialog_about(GTK_WINDOW(vw));
982 }
983
984 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
985 {
986   if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
987   {
988     vik_layers_panel_delete_selected ( vw->viking_vlp );
989     vw->modified = TRUE;
990   }
991   else
992     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
993 }
994
995 /***************************************
996  ** tool management routines
997  **
998  ***************************************/
999
1000 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1001 {
1002   toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1003   vt->tools = NULL;
1004   vt->n_tools = 0;
1005   vt->active_tool = -1;
1006   vt->vw = vw;
1007   if (!vw->viking_vvp) {
1008     g_critical("no viewport found.");
1009     exit(1);
1010   }
1011   return vt;
1012 }
1013
1014 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1015 {
1016   vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1017   vt->tools[vt->n_tools].ti = *vti;
1018   vt->tools[vt->n_tools].layer_type = layer_type;
1019   if (vti->create) {
1020     vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1021   } 
1022   else {
1023     vt->tools[vt->n_tools].state = NULL;
1024   }
1025   vt->n_tools++;
1026 }
1027
1028 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1029 {
1030   int i;
1031   for (i=0; i<vt->n_tools; i++) {
1032     if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1033       break;
1034     }
1035   }
1036   return i;
1037 }
1038
1039 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1040 {
1041   int tool = toolbox_get_tool(vt, tool_name);
1042   toolbox_tool_t *t = &vt->tools[tool];
1043   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1044
1045   if (tool == vt->n_tools) {
1046     g_critical("trying to activate a non-existent tool...");
1047     exit(1);
1048   }
1049   /* is the tool already active? */
1050   if (vt->active_tool == tool) {
1051     return;
1052   }
1053
1054   if (vt->active_tool != -1) {
1055     if (vt->tools[vt->active_tool].ti.deactivate) {
1056       vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1057     }
1058   }
1059   if (t->ti.activate) {
1060     t->ti.activate(vl, t->state);
1061   }
1062   vt->active_tool = tool;
1063 }
1064
1065 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1066 {
1067   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1068   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1069     gint ltype = vt->tools[vt->active_tool].layer_type;
1070     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1071       vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1072   }
1073 }
1074
1075 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1076 {
1077   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1078   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1079     gint ltype = vt->tools[vt->active_tool].layer_type;
1080     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1081       vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state);
1082   }
1083 }
1084
1085 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1086 {
1087   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1088   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1089     gint ltype = vt->tools[vt->active_tool].layer_type;
1090     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1091       vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1092   }
1093 }
1094 /** End tool management ************************************/
1095
1096 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1097 {
1098   gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1099 }
1100
1101 /* this function gets called whenever a toolbar tool is clicked */
1102 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1103 {
1104   /* White Magic, my friends ... White Magic... */
1105   int layer_id, tool_id;
1106
1107   toolbox_activate(vw->vt, gtk_action_get_name(a));
1108
1109   if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1110     vw->current_tool = TOOL_ZOOM;
1111     gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
1112   } 
1113   else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1114     vw->current_tool = TOOL_RULER;
1115     gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
1116   }
1117   else {
1118     /* TODO: only enable tools from active layer */
1119     for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1120       for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1121         if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1122            vw->current_tool = TOOL_LAYER;
1123            vw->tool_layer_id = layer_id;
1124            vw->tool_tool_id = tool_id;
1125            gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
1126         }
1127       }
1128     }
1129   }
1130   draw_status ( vw );
1131 }
1132
1133 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1134 {
1135   gchar *title;
1136   if ( vw->filename )
1137     g_free ( vw->filename );
1138   if ( filename == NULL )
1139   {
1140     vw->filename = NULL;
1141     gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
1142   }
1143   else
1144   {
1145     vw->filename = g_strdup(filename);
1146     title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
1147     gtk_window_set_title ( GTK_WINDOW(vw), title );
1148     g_free ( title );
1149   }
1150 }
1151
1152 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1153 {
1154   GtkWidget *mode_button;
1155   gchar *buttonname;
1156   switch ( mode ) {
1157     case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1158     case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1159     case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1160     case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1161     default: buttonname = "/ui/MainMenu/View/ModeKH";
1162   }
1163   mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1164   g_assert ( mode_button );
1165   return mode_button;
1166 }
1167
1168 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1169 {
1170   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1171   {
1172     case 0:
1173       a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
1174       break;
1175     case 1:
1176     {
1177       GtkWidget *mode_button;
1178       if ( change_filename )
1179         window_set_filename ( vw, filename );
1180       mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1181       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. */
1182       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1183       vw->only_updating_coord_mode_ui = FALSE;
1184
1185       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1186
1187       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1188       g_assert ( mode_button );
1189       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1190
1191       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1192       g_assert ( mode_button );
1193       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1194     }
1195     default: draw_update ( vw );
1196   }
1197 }
1198 static void load_file ( GtkAction *a, VikWindow *vw )
1199 {
1200   gboolean newwindow;
1201   if (!strcmp(gtk_action_get_name(a), "Open")) {
1202     newwindow = TRUE;
1203   } 
1204   else if (!strcmp(gtk_action_get_name(a), "Append")) {
1205     newwindow = FALSE;
1206   } 
1207   else {
1208     g_critical("Houston we got a problem\n");
1209     return;
1210   }
1211     
1212   if ( ! vw->open_dia )
1213   {
1214     vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
1215     gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1216     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1217     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1218   }
1219   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1220   {
1221     gtk_widget_hide ( vw->open_dia );
1222 #ifdef VIKING_PROMPT_IF_MODIFIED
1223     if ( (vw->modified || vw->filename) && newwindow )
1224 #else
1225     if ( vw->filename && newwindow )
1226 #endif
1227       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1228     else {
1229       gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1230       gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1231       while ( *files ) {
1232         vik_window_open_file ( vw, *files, change_fn );
1233         files++;
1234       }
1235     }
1236   }
1237   else
1238     gtk_widget_hide ( vw->open_dia );
1239 }
1240
1241 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1242 {
1243   gboolean rv = FALSE;
1244   const gchar *fn;
1245   if ( ! vw->save_dia )
1246   {
1247     vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
1248     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1249     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1250   }
1251
1252   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1253   {
1254     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1255     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 ) ) )
1256     {
1257       window_set_filename ( vw, fn );
1258       rv = window_save ( vw );
1259       vw->modified = FALSE;
1260       break;
1261     }
1262   }
1263   gtk_widget_hide ( vw->save_dia );
1264   return rv;
1265 }
1266
1267 static gboolean window_save ( VikWindow *vw )
1268 {
1269   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1270     return TRUE;
1271   else
1272   {
1273     a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
1274     return FALSE;
1275   }
1276 }
1277
1278 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1279 {
1280   if ( ! vw->filename )
1281     return save_file_as ( NULL, vw );
1282   else
1283   {
1284     vw->modified = FALSE;
1285     return window_save ( vw );
1286   }
1287 }
1288
1289 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1290 {
1291   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1292 }
1293
1294 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1295 {
1296   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1297 }
1298
1299 #ifdef VIK_CONFIG_GEOCACHES
1300 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1301 {
1302   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1303 }
1304 #endif
1305
1306 static void goto_address( GtkAction *a, VikWindow *vw)
1307 {
1308   a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1309 }
1310
1311 static void clear_cb ( GtkAction *a, VikWindow *vw )
1312 {
1313   vik_layers_panel_clear ( vw->viking_vlp );
1314   window_set_filename ( vw, NULL );
1315   draw_update ( vw );
1316 }
1317
1318 static void window_close ( GtkAction *a, VikWindow *vw )
1319 {
1320   if ( ! delete_event ( vw ) )
1321     gtk_widget_destroy ( GTK_WIDGET(vw) );
1322 }
1323
1324 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1325 {
1326   if (save_file( NULL, vw)) {
1327     window_close( NULL, vw);
1328     return(TRUE);
1329   }
1330   else
1331     return(FALSE);
1332 }
1333
1334 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1335 {
1336   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1337   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1338   {
1339     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1340     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1341     draw_update ( vw );
1342   }
1343 }
1344
1345 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1346 {
1347   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1348   GdkPixbuf *pixbuf_to_save;
1349   gdouble old_xmpp, old_ympp;
1350   GError *error = NULL;
1351
1352   /* backup old zoom & set new */
1353   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1354   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1355   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1356
1357   /* reset width and height: */
1358   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1359
1360   /* draw all layers */
1361   draw_redraw ( vw );
1362
1363   /* save buffer as file. */
1364   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);
1365   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1366   if (error)
1367   {
1368     g_warning("Unable to write to file %s: %s", fn, error->message );
1369     g_error_free (error);
1370   }
1371   g_object_unref ( G_OBJECT(pixbuf_to_save) );
1372
1373   /* pretend like nothing happened ;) */
1374   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1375   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1376   vik_viewport_configure ( vw->viking_vvp );
1377   draw_update ( vw );
1378 }
1379
1380 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 )
1381 {
1382   gulong size = sizeof(gchar) * (strlen(fn) + 15);
1383   gchar *name_of_file = g_malloc ( size );
1384   guint x = 1, y = 1;
1385   struct UTM utm_orig, utm;
1386
1387   /* *** copied from above *** */
1388   GdkPixbuf *pixbuf_to_save;
1389   gdouble old_xmpp, old_ympp;
1390   GError *error = NULL;
1391
1392   /* backup old zoom & set new */
1393   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1394   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1395   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1396
1397   /* reset width and height: do this only once for all images (same size) */
1398   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1399   /* *** end copy from above *** */
1400
1401   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1402
1403   make_dir(fn);
1404
1405   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1406
1407   for ( y = 1; y <= tiles_h; y++ )
1408   {
1409     for ( x = 1; x <= tiles_w; x++ )
1410     {
1411       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1412       utm = utm_orig;
1413       if ( tiles_w & 0x1 )
1414         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1415       else
1416         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1417       if ( tiles_h & 0x1 ) /* odd */
1418         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1419       else /* even */
1420         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1421
1422       /* move to correct place. */
1423       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1424
1425       draw_redraw ( vw );
1426
1427       /* save buffer as file. */
1428       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);
1429       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1430       if (error)
1431       {
1432         g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1433         g_error_free (error);
1434       }
1435
1436       g_object_unref ( G_OBJECT(pixbuf_to_save) );
1437     }
1438   }
1439
1440   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1441   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1442   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1443   vik_viewport_configure ( vw->viking_vvp );
1444   draw_update ( vw );
1445
1446   g_free ( name_of_file );
1447 }
1448
1449 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1450 {
1451   VikWindow *vw = VIK_WINDOW(pass_along[0]);
1452   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1453   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1454   gdouble width_min, width_max, height_min, height_max;
1455   gint width, height;
1456
1457   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1458   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1459
1460   /* TODO: support for xzoom and yzoom values */
1461   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1462   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1463
1464   if ( width > width_max || width < width_min || height > height_max || height < height_min )
1465     a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
1466
1467   gtk_spin_button_set_value ( width_spin, width );
1468   gtk_spin_button_set_value ( height_spin, height );
1469 }
1470
1471 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1472 {
1473   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1474   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1475   gchar *label_text;
1476   gdouble w, h;
1477   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1478   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1479   if (pass_along[4]) /* save many images; find TOTAL area covered */
1480   {
1481     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1482     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1483   }
1484   label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
1485   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1486   g_free ( label_text );
1487 }
1488
1489 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1490 {
1491   /* todo: default for answers inside VikWindow or static (thruout instance) */
1492   GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
1493                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1494                                                   GTK_STOCK_CANCEL,
1495                                                   GTK_RESPONSE_REJECT,
1496                                                   GTK_STOCK_OK,
1497                                                   GTK_RESPONSE_ACCEPT,
1498                                                   NULL );
1499   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1500   GtkWidget *png_radio, *jpeg_radio;
1501   GtkWidget *current_window_button;
1502   gpointer current_window_pass_along[7];
1503   GtkWidget *zoom_label, *zoom_spin;
1504   GtkWidget *total_size_label;
1505
1506   /* only used if (!one_image_only) */
1507   GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1508
1509
1510   width_label = gtk_label_new ( "Width (pixels):" );
1511   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1512   height_label = gtk_label_new ( "Height (pixels):" );
1513   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1514
1515   zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
1516   /* TODO: separate xzoom and yzoom factors */
1517   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);
1518
1519   total_size_label = gtk_label_new ( NULL );
1520
1521   current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
1522   current_window_pass_along [0] = vw;
1523   current_window_pass_along [1] = width_spin;
1524   current_window_pass_along [2] = height_spin;
1525   current_window_pass_along [3] = zoom_spin;
1526   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1527   current_window_pass_along [5] = NULL;
1528   current_window_pass_along [6] = total_size_label;
1529   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1530
1531   png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
1532   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
1533
1534   if ( ! vw->draw_image_save_as_png )
1535     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1536
1537   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1538   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1539   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1540   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1541   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1542   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1543   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1544   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1545   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1546
1547   if ( ! one_image_only )
1548   {
1549     GtkWidget *tiles_width_label, *tiles_height_label;
1550
1551
1552     tiles_width_label = gtk_label_new ( "East-west image tiles:" );
1553     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1554     tiles_height_label = gtk_label_new ( "North-south image tiles:" );
1555     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1556     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1557     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1558     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1559     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1560
1561     current_window_pass_along [4] = tiles_width_spin;
1562     current_window_pass_along [5] = tiles_height_spin;
1563     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1564     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1565   }
1566   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1567   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1568   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1569   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1570
1571   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1572
1573   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1574
1575   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1576   {
1577     gtk_widget_hide ( GTK_WIDGET(dialog) );
1578     if ( one_image_only )
1579       save_image_file ( vw, fn, 
1580                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1581                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1582                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1583                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1584     else {
1585       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1586         save_image_dir ( vw, fn, 
1587                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1588                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1589                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1590                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1591                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1592                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1593       else
1594         a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
1595     }
1596   }
1597   gtk_widget_destroy ( GTK_WIDGET(dialog) );
1598 }
1599
1600
1601 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1602 {
1603   const gchar *fn;
1604   if (!vw->save_img_dia) {
1605     vw->save_img_dia = gtk_file_selection_new ("Save Image");
1606     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1607     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1608   }
1609
1610   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1611   {
1612     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1613     if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(vw->save_img_dia), "The file \"%s\" exists, do you wish to overwrite it?", a_file_basename ( fn ) ) )
1614     {
1615       draw_to_image_file ( vw, fn, TRUE );
1616       break;
1617     }
1618   }
1619   gtk_widget_hide ( vw->save_img_dia );
1620 }
1621
1622 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1623 {
1624   const gchar *fn;
1625   if (!vw->save_img_dir_dia) {
1626     vw->save_img_dir_dia = gtk_file_selection_new ("Choose a name for a new directory to hold images");
1627     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1628     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1629   }
1630
1631   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1632   {
1633     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1634     if ( access ( fn, F_OK ) == 0 )
1635       a_dialog_info_msg_extra ( GTK_WINDOW(vw->save_img_dir_dia), "The file %s exists. Please choose a name for a new directory to hold images in that does not exist.", a_file_basename(fn) );
1636     else
1637     {
1638       draw_to_image_file ( vw, fn, FALSE );
1639       break;
1640     }
1641   }
1642   gtk_widget_hide ( vw->save_img_dir_dia );
1643 }
1644
1645 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1646 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1647 {
1648   VikViewportDrawMode drawmode;
1649   if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1650     drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1651   }
1652   else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1653     drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1654   }
1655   else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1656     drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1657   }
1658   else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1659     drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1660   }
1661   else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1662     drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1663   }
1664   else {
1665     g_critical("Houston, we got a problem!\n");
1666     return;
1667   }
1668
1669   if ( !vw->only_updating_coord_mode_ui )
1670   {
1671     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1672     if ( olddrawmode != drawmode )
1673     {
1674       /* this takes care of coord mode too */
1675       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1676       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1677         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1678       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1679         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1680       }
1681       draw_update ( vw );
1682     }
1683   }
1684 }
1685
1686 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1687 {
1688   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1689   g_assert(check_box);
1690   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1691   vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1692   draw_update ( vw );
1693 }
1694
1695 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1696 {
1697   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1698   g_assert(check_box);
1699   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1700   vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1701   draw_update ( vw );
1702 }
1703
1704 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1705 {
1706   GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1707   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1708   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1709   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1710   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1711   {
1712     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1713     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1714     draw_update ( vw );
1715   }
1716   g_free ( color );
1717   gtk_widget_destroy ( colorsd );
1718 }
1719
1720
1721
1722 /***********************************************************************************************
1723  ** GUI Creation
1724  ***********************************************************************************************/
1725
1726 static GtkActionEntry entries[] = {
1727   { "File", NULL, "_File", 0, 0, 0 },
1728   { "Edit", NULL, "_Edit", 0, 0, 0 },
1729   { "View", NULL, "_View", 0, 0, 0 },
1730   { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1731   { "SetPan", NULL, "_Pan", 0, 0, 0 },
1732   { "Layers", NULL, "_Layers", 0, 0, 0 },
1733   { "Tools", NULL, "_Tools", 0, 0, 0 },
1734   { "Help", NULL, "_Help", 0, 0, 0 },
1735
1736   { "New",       GTK_STOCK_NEW,          "_New",                          "<control>N", "New file",                                     (GCallback)newwindow_cb          },
1737   { "Open",      GTK_STOCK_OPEN,         "_Open",                         "<control>O", "Open a file",                                  (GCallback)load_file             },
1738   { "Append",    GTK_STOCK_ADD,          "A_ppend File",                  NULL,         "Append data from a different file",            (GCallback)load_file             },
1739   { "Acquire", NULL, "A_cquire", 0, 0, 0 },
1740   { "AcquireGPS",   NULL,                "From _GPS",                     NULL,         "Transfer data from a GPS device",              (GCallback)acquire_from_gps      },
1741   { "AcquireGoogle",   NULL,             "Google _Directions",            NULL,         "Get driving directions from Google",           (GCallback)acquire_from_google   },
1742 #ifdef VIK_CONFIG_GEOCACHES
1743   { "AcquireGC",   NULL,                 "Geo_caches",                    NULL,         "Get Geocaches from geocaching.com",            (GCallback)acquire_from_gc       },
1744 #endif
1745   { "Save",      GTK_STOCK_SAVE,         "_Save",                         "<control>S", "Save the file",                                (GCallback)save_file             },
1746   { "SaveAs",    GTK_STOCK_SAVE_AS,      "Save _As",                      NULL,         "Save the file under different name",           (GCallback)save_file_as          },
1747   { "GenImg",    GTK_STOCK_CLEAR,        "_Generate Image File",          NULL,         "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1748   { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL,         "FIXME:IMGDIR",                                 (GCallback)draw_to_image_dir_cb  },
1749   { "Exit",      GTK_STOCK_QUIT,         "E_xit",                         "<control>W", "Exit the program",                             (GCallback)window_close          },
1750   { "SaveExit",  GTK_STOCK_QUIT,         "Save and Exit",                 NULL, "Save and Exit the program",                             (GCallback)save_file_and_exit          },
1751
1752   { "GoogleMapsSearch",   GTK_STOCK_GO_FORWARD,                 "Go To Google Maps location",                     NULL,         "Go to address/place using Google Maps search",            (GCallback)goto_address       },
1753   { "GotoLL",    GTK_STOCK_QUIT,         "_Go to Lat\\/Lon...",           NULL,         "Go to arbitrary lat\\/lon coordinate",         (GCallback)draw_goto_cb          },
1754   { "GotoUTM",   GTK_STOCK_QUIT,         "Go to UTM...",                  NULL,         "Go to arbitrary UTM coordinate",               (GCallback)draw_goto_cb          },
1755   { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...",       NULL,         NULL,                                           (GCallback)set_bg_color          },
1756   { "ZoomIn",    GTK_STOCK_ZOOM_IN,      "Zoom _In",                   "<control>plus", NULL,                                           (GCallback)draw_zoom_cb          },
1757   { "ZoomOut",   GTK_STOCK_ZOOM_OUT,     "Zoom _Out",                 "<control>minus", NULL,                                           (GCallback)draw_zoom_cb          },
1758   { "ZoomTo",    GTK_STOCK_ZOOM_FIT,     "Zoom _To",               "<control><shift>Z", NULL,                                           (GCallback)zoom_to_cb            },
1759   { "Zoom0.25",  NULL,                   "0.25",                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1760   { "Zoom0.5",   NULL,                   "0.5",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1761   { "Zoom1",     NULL,                   "1",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1762   { "Zoom2",     NULL,                   "2",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1763   { "Zoom4",     NULL,                   "4",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1764   { "Zoom8",     NULL,                   "8",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1765   { "Zoom16",    NULL,                   "16",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1766   { "Zoom32",    NULL,                   "32",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1767   { "Zoom64",    NULL,                   "64",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1768   { "Zoom128",   NULL,                   "128",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1769   { "PanNorth",  NULL,                   "Pan North",                  "<control>Up", NULL,                                           (GCallback)draw_pan_cb },
1770   { "PanEast",  NULL,                    "Pan East",                   "<control>Right", NULL,                                           (GCallback)draw_pan_cb },
1771   { "PanSouth",  NULL,                   "Pan South",                  "<control>Down", NULL,                                           (GCallback)draw_pan_cb },
1772   { "PanWest",  NULL,                    "Pan West",                   "<control>Left", NULL,                                           (GCallback)draw_pan_cb },
1773   { "BGJobs",    GTK_STOCK_EXECUTE,      "Background _Jobs",              NULL,         NULL,                                           (GCallback)a_background_show_window },
1774
1775   { "Cut",       GTK_STOCK_CUT,          "Cu_t",                          NULL,         NULL,                                           (GCallback)menu_cut_layer_cb     },
1776   { "Copy",      GTK_STOCK_COPY,         "_Copy",                         NULL,         NULL,                                           (GCallback)menu_copy_layer_cb    },
1777   { "Paste",     GTK_STOCK_PASTE,        "_Paste",                        NULL,         NULL,                                           (GCallback)menu_paste_layer_cb   },
1778   { "Delete",    GTK_STOCK_DELETE,       "_Delete",                       NULL,         NULL,                                           (GCallback)menu_delete_layer_cb  },
1779   { "DeleteAll", NULL,                   "Delete All",                    NULL,         NULL,                                           (GCallback)clear_cb              },
1780   { "Properties",GTK_STOCK_PROPERTIES,   "_Properties",                   NULL,         NULL,                                           (GCallback)menu_properties_cb    },
1781
1782   { "About",     GTK_STOCK_ABOUT,        "_About",                        NULL,         NULL,                                           (GCallback)help_about_cb    },
1783 };
1784
1785 /* Radio items */
1786 static GtkRadioActionEntry mode_entries[] = {
1787   { "ModeUTM",         NULL,         "_UTM Mode",               "<control>u", NULL, 0 },
1788   { "ModeExpedia",     NULL,         "_Expedia Mode",           "<control>e", NULL, 1 },
1789   { "ModeGoogle",      NULL,         "_Old Google Mode",        "<control>o", NULL, 2 },
1790   { "ModeKH",          NULL,         "Old _KH Mode",            "<control>k", NULL, 3 },
1791   { "ModeMercator",    NULL,         "_Google Mode",            "<control>g", NULL, 4 }
1792 };
1793
1794 static GtkRadioActionEntry tool_entries[] = {
1795   { "Zoom",      "vik-icon-zoom",        "_Zoom",                         "<control><shift>Z", "Zoom Tool",  0 },
1796   { "Ruler",     "vik-icon-ruler",       "_Ruler",                        "<control><shift>R", "Ruler Tool", 1 }
1797 };
1798
1799 static GtkToggleActionEntry toggle_entries[] = {
1800   { "ShowScale", NULL,                   "Show Scale",                    NULL,         NULL,                                           (GCallback)set_draw_scale, TRUE   },
1801   { "ShowCenterMark", NULL,                   "Show Center Mark",                    NULL,         NULL,                                           (GCallback)set_draw_centermark, TRUE   },
1802 };
1803
1804 #include "menu.xml.h"
1805 static void window_create_ui( VikWindow *window )
1806 {
1807   GtkUIManager *uim;
1808   GtkActionGroup *action_group;
1809   GtkAccelGroup *accel_group;
1810   GError *error;
1811   guint i, j, mid;
1812   GtkIconFactory *icon_factory;
1813   GtkIconSet *icon_set; 
1814   GtkRadioActionEntry *tools = NULL, *radio;
1815   guint ntools;
1816   
1817   uim = gtk_ui_manager_new ();
1818   window->uim = uim;
1819
1820   toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1821   toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1822
1823   error = NULL;
1824   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1825     g_error_free (error);
1826     exit (1);
1827   }
1828
1829   action_group = gtk_action_group_new ("MenuActions");
1830   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1831   gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1832   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1833
1834   icon_factory = gtk_icon_factory_new ();
1835   gtk_icon_factory_add_default (icon_factory); 
1836
1837   register_vik_icons(icon_factory);
1838
1839   ntools = 0;
1840   for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1841       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1842       radio = &tools[ntools];
1843       ntools++;
1844       *radio = tool_entries[i];
1845       radio->value = ntools;
1846   }  
1847
1848   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1849     GtkActionEntry action;
1850     gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Layers/", 
1851                           vik_layer_get_interface(i)->name,
1852                           vik_layer_get_interface(i)->name,
1853                           GTK_UI_MANAGER_MENUITEM, FALSE);
1854
1855     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1856     gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1857     gtk_icon_set_unref (icon_set);
1858
1859     action.name = vik_layer_get_interface(i)->name;
1860     action.stock_id = vik_layer_get_interface(i)->name;
1861     action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1862     action.accelerator = NULL;
1863     action.tooltip = NULL;
1864     action.callback = (GCallback)menu_addlayer_cb;
1865     gtk_action_group_add_actions(action_group, &action, 1, window);
1866
1867     if ( vik_layer_get_interface(i)->tools_count ) {
1868       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1869       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1870     }
1871
1872     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1873       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1874       radio = &tools[ntools];
1875       ntools++;
1876       
1877       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
1878                             vik_layer_get_interface(i)->tools[j].name,
1879                             vik_layer_get_interface(i)->tools[j].name,
1880                             GTK_UI_MANAGER_MENUITEM, FALSE);
1881       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems", 
1882                             vik_layer_get_interface(i)->tools[j].name,
1883                             vik_layer_get_interface(i)->tools[j].name,
1884                             GTK_UI_MANAGER_TOOLITEM, FALSE);
1885
1886       toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
1887
1888       radio->name = vik_layer_get_interface(i)->tools[j].name;
1889       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1890       radio->label = vik_layer_get_interface(i)->tools[j].name;
1891       radio->accelerator = NULL;
1892       radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1893       radio->value = ntools;
1894     }
1895   }
1896   g_object_unref (icon_factory);
1897
1898   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1899   g_free(tools);
1900
1901   gtk_ui_manager_insert_action_group (uim, action_group, 0);
1902
1903   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1904     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1905       GtkAction *action = gtk_action_group_get_action(action_group,
1906                             vik_layer_get_interface(i)->tools[j].name);
1907       g_object_set(action, "sensitive", FALSE, NULL);
1908     }
1909   }
1910   window->action_group = action_group;
1911
1912   accel_group = gtk_ui_manager_get_accel_group (uim);
1913   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1914   gtk_ui_manager_ensure_update (uim);
1915 }
1916
1917
1918
1919 #include "icons/icons.h"
1920 static struct { 
1921   const GdkPixdata *data;
1922   gchar *stock_id;
1923 } stock_icons[] = {
1924   { &addtr_18,          "Create Track"      },
1925   { &begintr_18,        "Begin Track"      },
1926   { &edtr_18,           "Edit Trackpoint"   },
1927   { &addwp_18,          "Create Waypoint"   },
1928   { &edwp_18,           "Edit Waypoint"     },
1929   { &iscissors_18,      "Magic Scissors"   },
1930   { &zoom_18,           "vik-icon-zoom"     },
1931   { &ruler_18,          "vik-icon-ruler"    },
1932   { &geozoom_18,        "Georef Zoom Tool"  },
1933   { &geomove_18,        "Georef Move Map"   },
1934   { &mapdl_18,          "Maps Download"     },
1935   { &demdl_18,          "DEM Download/Import"     },
1936   { &showpic_18,        "Show Picture"      },
1937 };
1938  
1939 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1940
1941 static void
1942 register_vik_icons (GtkIconFactory *icon_factory)
1943 {
1944   GtkIconSet *icon_set; 
1945   gint i;
1946
1947   for (i = 0; i < n_stock_icons; i++) {
1948     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
1949                    stock_icons[i].data, FALSE, NULL ));
1950     gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1951     gtk_icon_set_unref (icon_set);
1952   }
1953 }
1954
1955 void vik_window_cursors_init()
1956 {
1957   GdkPixbuf *cursor_pixbuf;
1958   GError *cursor_load_err;
1959
1960   cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
1961   vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
1962
1963   g_object_unref ( G_OBJECT(cursor_pixbuf) );
1964
1965   cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
1966   vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
1967
1968   g_object_unref ( G_OBJECT(cursor_pixbuf) );
1969 }
1970
1971 void vik_window_cursors_uninit()
1972 {
1973   gdk_cursor_unref ( vw_cursor_zoom );
1974   gdk_cursor_unref ( vw_cursor_ruler );
1975 }