]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
Add copyright to srtm_continent.c
[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
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "viking.h"
27 #include "background.h"
28 #include "acquire.h"
29 #include "datasources.h"
30 #include "vikgoto.h"
31 #include "dems.h"
32 #include "mapcache.h"
33 #include "print.h"
34 #include "preferences.h"
35 #include "icons/icons.h"
36 #include "vikexttools.h"
37 #include "garminsymbols.h"
38
39 #ifdef HAVE_STDLIB_H
40 #include <stdlib.h>
41 #endif
42 #ifdef HAVE_MATH_H
43 #include <math.h>
44 #endif
45 #ifdef HAVE_STRING_H
46 #include <string.h>
47 #endif
48 #include <ctype.h>
49 #include <glib.h>
50 #include <glib/gstdio.h>
51 #include <glib/gprintf.h>
52 #include <glib/gi18n.h>
53 #include <gio/gio.h>
54
55 #define VIKING_WINDOW_WIDTH      1000
56 #define VIKING_WINDOW_HEIGHT     800
57 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
58 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
59 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
60
61 static void window_finalize ( GObject *gob );
62 static GObjectClass *parent_class;
63
64 static void window_init ( VikWindow *vw );
65 static void window_class_init ( VikWindowClass *klass );
66 static void window_set_filename ( VikWindow *vw, const gchar *filename );
67
68 static void draw_update ( VikWindow *vw );
69
70 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
71
72 /* Drawing & stuff */
73
74 static gboolean delete_event( VikWindow *vw );
75
76 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
77
78 static void window_configure_event ( VikWindow *vw );
79 static void draw_sync ( VikWindow *vw );
80 static void draw_redraw ( VikWindow *vw );
81 static void draw_scroll  ( VikWindow *vw, GdkEventScroll *event );
82 static void draw_click  ( VikWindow *vw, GdkEventButton *event );
83 static void draw_release ( VikWindow *vw, GdkEventButton *event );
84 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
85 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
86 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
87
88 static void draw_status ();
89
90 /* End Drawing Functions */
91
92 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
93 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
94 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
95
96 /* tool management */
97 typedef struct {
98   VikToolInterface ti;
99   gpointer state;
100   gint layer_type;
101 } toolbox_tool_t;
102 #define TOOL_LAYER_TYPE_NONE -1
103
104 typedef struct {
105   int                   active_tool;
106   int                   n_tools;
107   toolbox_tool_t        *tools;
108   VikWindow *vw;
109 } toolbox_tools_t;
110
111 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
112 static toolbox_tools_t* toolbox_create(VikWindow *vw);
113 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
114 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
115 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
116 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name);
117 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
118 static void toolbox_move (toolbox_tools_t *vt, GdkEventMotion *event);
119 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
120
121
122 /* ui creation */
123 static void window_create_ui( VikWindow *window );
124 static void register_vik_icons (GtkIconFactory *icon_factory);
125
126 /* i/o */
127 static void load_file ( GtkAction *a, VikWindow *vw );
128 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
129 static gboolean save_file ( GtkAction *a, VikWindow *vw );
130 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
131 static gboolean window_save ( VikWindow *vw );
132
133 struct _VikWindow {
134   GtkWindow gtkwindow;
135   VikViewport *viking_vvp;
136   VikLayersPanel *viking_vlp;
137   VikStatusbar *viking_vs;
138
139   GtkToolbar *toolbar;
140
141   GtkItemFactory *item_factory;
142
143   /* tool management state */
144   guint current_tool;
145   toolbox_tools_t *vt;
146   guint16 tool_layer_id;
147   guint16 tool_tool_id;
148
149   GtkActionGroup *action_group;
150
151   gboolean pan_move;
152   gint pan_x, pan_y;
153
154   guint draw_image_width, draw_image_height;
155   gboolean draw_image_save_as_png;
156
157   gchar *filename;
158   gboolean modified;
159
160   GtkWidget *open_dia, *save_dia;
161   GtkWidget *save_img_dia, *save_img_dir_dia;
162
163   gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
164   GtkUIManager *uim;
165
166   /* half-drawn update */
167   VikLayer *trigger;
168   VikCoord trigger_center;
169 };
170
171 enum {
172  TOOL_PAN = 0,
173  TOOL_ZOOM,
174  TOOL_RULER,
175  TOOL_LAYER,
176  NUMBER_OF_TOOLS
177 };
178
179 enum {
180   VW_NEWWINDOW_SIGNAL,
181   VW_OPENWINDOW_SIGNAL,
182   VW_LAST_SIGNAL
183 };
184
185 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
186
187 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Pan"), N_("Zoom"), N_("Ruler") };
188
189 GType vik_window_get_type (void)
190 {
191   static GType vw_type = 0;
192
193   if (!vw_type)
194   {
195     static const GTypeInfo vw_info = 
196     {
197       sizeof (VikWindowClass),
198       NULL, /* base_init */
199       NULL, /* base_finalize */
200       (GClassInitFunc) window_class_init, /* class_init */
201       NULL, /* class_finalize */
202       NULL, /* class_data */
203       sizeof (VikWindow),
204       0,
205       (GInstanceInitFunc) window_init,
206     };
207     vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
208   }
209
210   return vw_type;
211 }
212
213 VikViewport * vik_window_viewport(VikWindow *vw)
214 {
215   return(vw->viking_vvp);
216 }
217
218 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
219 {
220   int i, j, tool_count;
221   VikLayerInterface *layer_interface;
222
223   if (!vw->action_group) return;
224
225   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
226     GtkAction *action;
227     layer_interface = vik_layer_get_interface(i);
228     tool_count = layer_interface->tools_count;
229
230     for (j = 0; j < tool_count; j++) {
231       action = gtk_action_group_get_action(vw->action_group,
232             layer_interface->tools[j].name);
233       g_object_set(action, "sensitive", i == vl->type, NULL);
234     }
235   }
236 }
237
238 static void window_finalize ( GObject *gob )
239 {
240   VikWindow *vw = VIK_WINDOW(gob);
241   g_return_if_fail ( vw != NULL );
242
243   a_background_remove_status ( vw->viking_vs );
244
245   G_OBJECT_CLASS(parent_class)->finalize(gob);
246 }
247
248
249 static void window_class_init ( VikWindowClass *klass )
250 {
251   /* destructor */
252   GObjectClass *object_class;
253
254   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);
255   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);
256
257   object_class = G_OBJECT_CLASS (klass);
258
259   object_class->finalize = window_finalize;
260
261   parent_class = g_type_class_peek_parent (klass);
262
263 }
264
265 static void window_init ( VikWindow *vw )
266 {
267   GtkWidget *main_vbox;
268   GtkWidget *hpaned;
269
270   vw->action_group = NULL;
271
272   vw->viking_vvp = vik_viewport_new();
273   vw->viking_vlp = vik_layers_panel_new();
274   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
275   vw->viking_vs = vik_statusbar_new();
276
277   vw->vt = toolbox_create(vw);
278   window_create_ui(vw);
279   window_set_filename (vw, NULL);
280   
281   toolbox_activate(vw->vt, "Pan");
282
283   vw->filename = NULL;
284   vw->item_factory = NULL;
285
286   vw->modified = FALSE;
287   vw->only_updating_coord_mode_ui = FALSE;
288  
289   vw->pan_move = FALSE; 
290   vw->pan_x = vw->pan_y = -1;
291   vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
292   vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
293   vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
294
295   main_vbox = gtk_vbox_new(FALSE, 1);
296   gtk_container_add (GTK_CONTAINER (vw), main_vbox);
297
298   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
299   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
300   gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
301   gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
302
303   vik_ext_tools_add_menu_items ( vw, vw->uim );
304
305   g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
306
307   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
308   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
309   gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK );
310   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
311   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
312   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
313   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
314   g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
315
316   g_signal_connect_swapped (G_OBJECT (vw->viking_vvp), "key_press_event", G_CALLBACK (key_press_event), vw);
317
318   gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
319
320   hpaned = gtk_hpaned_new ();
321   gtk_paned_pack1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp), FALSE, FALSE );
322   gtk_paned_pack2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp), TRUE, TRUE );
323
324   /* This packs the button into the window (a gtk container). */
325   gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
326
327   gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
328
329   a_background_add_status(vw->viking_vs);
330
331   vw->open_dia = NULL;
332   vw->save_dia = NULL;
333   vw->save_img_dia = NULL;
334   vw->save_img_dir_dia = NULL;
335 }
336
337 VikWindow *vik_window_new ()
338 {
339   return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
340 }
341
342 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data )
343 {
344   VikLayer *vl = vik_layers_panel_get_selected ( vw->viking_vlp );
345   if (vl && vw->vt->active_tool != -1 && vw->vt->tools[vw->vt->active_tool].ti.key_press ) {
346     gint ltype = vw->vt->tools[vw->vt->active_tool].layer_type;
347     if ( vl && ltype == vl->type )
348       return vw->vt->tools[vw->vt->active_tool].ti.key_press(vl, event, vw->vt->tools[vw->vt->active_tool].state);
349   }
350   return FALSE; /* don't handle the keypress */
351 }
352
353 static gboolean delete_event( VikWindow *vw )
354 {
355 #ifdef VIKING_PROMPT_IF_MODIFIED
356   if ( vw->modified )
357 #else
358   if (0)
359 #endif
360   {
361     GtkDialog *dia;
362     dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
363       _("Do you want to save the changes you made to the document \"%s\"?\n"
364         "\n"
365         "Your changes will be lost if you don't save them."),
366       vw->filename ? a_file_basename ( vw->filename ) : _("Untitled") ) );
367     gtk_dialog_add_buttons ( dia, _("Don't Save"), GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
368     switch ( gtk_dialog_run ( dia ) )
369     {
370       case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
371       case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
372       default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
373     }
374   }
375   return FALSE;
376 }
377
378 /* Drawing stuff */
379 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
380 {
381   g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
382 }
383
384 static void draw_update ( VikWindow *vw )
385 {
386   draw_redraw (vw);
387   draw_sync (vw);
388 }
389
390 static void draw_sync ( VikWindow *vw )
391 {
392   vik_viewport_sync(vw->viking_vvp);
393   draw_status ( vw );
394   /* other things may be necc here later. */
395 }
396
397 static void draw_status ( VikWindow *vw )
398 {
399   static gchar zoom_level[22];
400   gdouble xmpp = vik_viewport_get_xmpp (vw->viking_vvp);
401   gdouble ympp = vik_viewport_get_ympp(vw->viking_vvp);
402   gchar *unit = vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM ? _("mpp") : _("pixelfact");
403   if (xmpp != ympp)
404     g_snprintf ( zoom_level, 22, "%.3f/%.3f %s", xmpp, ympp, unit );
405   else
406     if ( (int)xmpp - xmpp < 0.0 )
407       g_snprintf ( zoom_level, 22, "%.3f %s", xmpp, unit );
408     else
409       /* xmpp should be a whole number so don't show useless .000 bit */
410       g_snprintf ( zoom_level, 22, "%d %s", (int)xmpp, unit );
411   if ( vw->current_tool == TOOL_LAYER )
412     vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
413   else
414     vik_statusbar_set_message ( vw->viking_vs, 0, _(tool_names[vw->current_tool]) );
415
416   vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
417 }
418
419 void vik_window_set_redraw_trigger(VikLayer *vl)
420 {
421   VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
422   if (NULL != vw)
423     vw->trigger = vl;
424 }
425
426 static void window_configure_event ( VikWindow *vw )
427 {
428   static int first = 1;
429   draw_redraw ( vw );
430   if (first) {
431     // This is a hack to set the cursor corresponding to the first tool
432     // FIXME find the correct way to initialize both tool and its cursor
433     const GdkCursor *cursor = NULL;
434     first = 0;
435     cursor = toolbox_get_cursor(vw->vt, "Pan");
436     /* We set cursor, even if it is NULL: it resets to default */
437     gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, (GdkCursor *)cursor );
438   }
439 }
440
441 static void draw_redraw ( VikWindow *vw )
442 {
443   VikCoord old_center = vw->trigger_center;
444   vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
445   VikLayer *new_trigger = vw->trigger;
446   vw->trigger = NULL;
447   VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
448
449   if ( ! new_trigger )
450     ; /* do nothing -- have to redraw everything. */
451   else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) || (new_trigger->type == VIK_LAYER_AGGREGATE) )
452     vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
453   else
454     vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
455
456   /* actually draw */
457   vik_viewport_clear ( vw->viking_vvp);
458   vik_layers_panel_draw_all ( vw->viking_vlp );
459   vik_viewport_draw_scale ( vw->viking_vvp );
460   vik_viewport_draw_centermark ( vw->viking_vvp );
461
462   vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
463 }
464
465 gboolean draw_buf_done = TRUE;
466
467 static gboolean draw_buf(gpointer data)
468 {
469   gpointer *pass_along = data;
470   gdk_threads_enter();
471   gdk_draw_drawable (pass_along[0], pass_along[1],
472                      pass_along[2], 0, 0, 0, 0, -1, -1);
473   draw_buf_done = TRUE;
474   gdk_threads_leave();
475   return FALSE;
476 }
477
478
479 /* Mouse event handlers ************************************************************************/
480
481 static void vik_window_pan_click (VikWindow *vw, GdkEventButton *event)
482 {
483   /* set panning origin */
484   vw->pan_move = FALSE;
485   vw->pan_x = (gint) event->x;
486   vw->pan_y = (gint) event->y;
487 }
488
489 static void draw_click (VikWindow *vw, GdkEventButton *event)
490 {
491   gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
492
493   /* middle button pressed.  we reserve all middle button and scroll events
494    * for panning and zooming; tools only get left/right/movement 
495    */
496   if ( event->button == 2) {
497     vik_window_pan_click ( vw, event );
498   } 
499   else {
500     toolbox_click(vw->vt, event);
501   }
502 }
503
504 static void vik_window_pan_move (VikWindow *vw, GdkEventMotion *event)
505 {
506   if ( vw->pan_x != -1 ) {
507     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
508                                      vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
509     vw->pan_move = TRUE;
510     vw->pan_x = event->x;
511     vw->pan_y = event->y;
512     draw_update ( vw );
513   }
514 }
515
516 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
517 {
518   static VikCoord coord;
519   static struct UTM utm;
520   static struct LatLon ll;
521   #define BUFFER_SIZE 50
522   static char pointer_buf[BUFFER_SIZE];
523   gchar *lat = NULL, *lon = NULL;
524   gint16 alt;
525   gdouble zoom;
526   VikDemInterpol interpol_method;
527
528   /* This is a hack, but work far the best, at least for single pointer systems.
529    * See http://bugzilla.gnome.org/show_bug.cgi?id=587714 for more. */
530   gint x, y;
531   gdk_window_get_pointer (event->window, &x, &y, NULL);
532   event->x = x;
533   event->y = y;
534
535   toolbox_move(vw->vt, event);
536
537   vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
538   vik_coord_to_utm ( &coord, &utm );
539   a_coords_utm_to_latlon ( &utm, &ll );
540   a_coords_latlon_to_string ( &ll, &lat, &lon );
541   /* Change interpolate method according to scale */
542   zoom = vik_viewport_get_zoom(vw->viking_vvp);
543   if (zoom > 2.0)
544     interpol_method = VIK_DEM_INTERPOL_NONE;
545   else if (zoom >= 1.0)
546     interpol_method = VIK_DEM_INTERPOL_SIMPLE;
547   else
548     interpol_method = VIK_DEM_INTERPOL_BEST;
549   if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION) {
550     if ( a_vik_get_units_height () == VIK_UNITS_HEIGHT_METRES )
551       g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s %dm"), lat, lon, alt );
552     else
553       g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s %dft"), lat, lon, (int)VIK_METERS_TO_FEET(alt) );
554   }
555   else
556     g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s"), lat, lon );
557   g_free (lat);
558   lat = NULL;
559   g_free (lon);
560   lon = NULL;
561   vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
562
563   vik_window_pan_move ( vw, event );
564
565   /* This is recommended by the GTK+ documentation, but does not work properly.
566    * Use deprecated way until GTK+ gets a solution for correct motion hint handling:
567    * http://bugzilla.gnome.org/show_bug.cgi?id=587714
568   */
569   /* gdk_event_request_motions ( event ); */
570 }
571
572 static void vik_window_pan_release ( VikWindow *vw, GdkEventButton *event )
573 {
574   if ( vw->pan_move == FALSE )
575     vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
576   else
577      vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
578                                       vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
579   vw->pan_move = FALSE;
580   vw->pan_x = vw->pan_y = -1;
581   draw_update ( vw );
582 }
583
584 static void draw_release ( VikWindow *vw, GdkEventButton *event )
585 {
586   gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
587
588   if ( event->button == 2 ) {  /* move / pan */
589     vik_window_pan_release(vw, event);
590   }
591   else {
592     toolbox_release(vw->vt, event);
593   }
594 }
595
596 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
597 {
598   guint modifiers = event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK);
599   if ( modifiers == GDK_CONTROL_MASK ) {
600     /* control == pan up & down */
601     if ( event->direction == GDK_SCROLL_UP )
602       vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
603     else
604       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 );
605   } else if ( modifiers == GDK_SHIFT_MASK ) {
606     /* shift == pan left & right */
607     if ( event->direction == GDK_SCROLL_UP )
608       vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
609     else
610       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 );
611   } else if ( modifiers == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
612     /* control+shift == make sure mouse is still over the same point on the map when we zoom */
613     VikCoord coord;
614     gint x, y;
615     gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
616     gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
617     vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
618     if ( event->direction == GDK_SCROLL_UP )
619       vik_viewport_zoom_in (vw->viking_vvp);
620     else
621       vik_viewport_zoom_out(vw->viking_vvp);
622     vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
623     vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
624                                      center_y + (y - event->y) );
625   } else {
626     if ( event->direction == GDK_SCROLL_UP )
627       vik_viewport_zoom_in (vw->viking_vvp);
628     else
629       vik_viewport_zoom_out (vw->viking_vvp);
630   }
631
632   draw_update(vw);
633 }
634
635
636
637 /********************************************************************************
638  ** Ruler tool code
639  ********************************************************************************/
640 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
641 {
642   PangoFontDescription *pfd;
643   PangoLayout *pl;
644   gchar str[128];
645   GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
646   GdkGC *thickgc = gdk_gc_new(d);
647   
648   gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
649   gdouble dx = (x2-x1)/len*10; 
650   gdouble dy = (y2-y1)/len*10;
651   gdouble c = cos(15.0 * M_PI/180.0);
652   gdouble s = sin(15.0 * M_PI/180.0);
653   gdouble angle;
654   gdouble baseangle = 0;
655   gint i;
656
657   /* draw line with arrow ends */
658   {
659     gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
660     a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
661     gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
662   }
663
664   a_viewport_clip_line(&x1, &y1, &x2, &y2);
665   gdk_draw_line(d, gc, x1, y1, x2, y2);
666
667   gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
668   gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
669   gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
670   gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
671   gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
672   gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
673
674   /* draw compass */
675 #define CR 80
676 #define CW 4
677   angle = atan2(dy, dx) + M_PI_2;
678
679   if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
680     VikCoord test;
681     struct LatLon ll;
682     struct UTM u;
683     gint tx, ty;
684
685     vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
686     vik_coord_to_latlon ( &test, &ll );
687     ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
688     a_coords_latlon_to_utm ( &ll, &u );
689     vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
690     vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
691
692     baseangle = M_PI - atan2(tx-x1, ty-y1);
693     angle -= baseangle;
694   }
695
696   if (angle<0) 
697     angle+=2*M_PI;
698   if (angle>2*M_PI)
699     angle-=2*M_PI;
700
701   {
702     GdkColor color;
703     gdk_gc_copy(thickgc, gc);
704     gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
705     gdk_color_parse("#2255cc", &color);
706     gdk_gc_set_rgb_fg_color(thickgc, &color);
707   }
708   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);
709
710
711   gdk_gc_copy(thickgc, gc);
712   gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
713   for (i=0; i<180; i++) {
714     c = cos(i*M_PI/90.0 + baseangle);
715     s = sin(i*M_PI/90.0 + baseangle);
716
717     if (i%5) {
718       gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
719     } else {
720       gdouble ticksize = 2*CW;
721       gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
722     }
723   }
724
725   gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
726   gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
727   gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
728   c = (CR+CW*2)*cos(baseangle);
729   s = (CR+CW*2)*sin(baseangle);
730   gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
731   gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
732
733   /* draw labels */
734 #define LABEL(x, y, w, h) { \
735     gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
736     gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
737     gdk_draw_layout(d, gc, (x), (y), pl); } 
738   {
739     gint wd, hd, xd, yd;
740     gint wb, hb, xb, yb;
741
742     pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
743
744     pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
745     pango_layout_set_font_description (pl, pfd);
746     pango_font_description_free (pfd);
747
748     pango_layout_set_text(pl, "N", -1);
749     gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
750
751     /* draw label with distance */
752     vik_units_distance_t dist_units = a_vik_get_units_distance ();
753     switch (dist_units) {
754     case VIK_UNITS_DISTANCE_KILOMETRES:
755       if (distance >= 1000 && distance < 100000) {
756         g_sprintf(str, "%3.2f km", distance/1000.0);
757       } else if (distance < 1000) {
758         g_sprintf(str, "%d m", (int)distance);
759       } else {
760         g_sprintf(str, "%d km", (int)distance/1000);
761       }
762       break;
763     case VIK_UNITS_DISTANCE_MILES:
764       if (distance >= 1600 && distance < 160000) {
765         g_sprintf(str, "%3.2f miles", distance/1600.0);
766       } else if (distance < 1600) {
767         g_sprintf(str, "%d yards", (int)(distance*1.0936133));
768       } else {
769         g_sprintf(str, "%d miles", (int)distance/1600);
770       }
771       break;
772     default:
773       g_critical("Houston, we've had a problem. distance=%d", dist_units);
774     }
775
776     pango_layout_set_text(pl, str, -1);
777
778     pango_layout_get_pixel_size ( pl, &wd, &hd );
779     if (dy>0) {
780       xd = (x1+x2)/2 + dy;
781       yd = (y1+y2)/2 - hd/2 - dx;
782     } else {
783       xd = (x1+x2)/2 - dy;
784       yd = (y1+y2)/2 - hd/2 + dx;
785     }
786
787     if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
788       xd = x2 + 10;
789       yd = y2 - 5;
790     }
791
792     LABEL(xd, yd, wd, hd);
793
794     /* draw label with bearing */
795     g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
796     pango_layout_set_text(pl, str, -1);
797     pango_layout_get_pixel_size ( pl, &wb, &hb );
798     xb = x1 + CR*cos(angle-M_PI_2);
799     yb = y1 + CR*sin(angle-M_PI_2);
800
801     if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
802       xb = x2 + 10;
803       yb = y2 + 10;
804     }
805
806     {
807       GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
808       if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
809         xb = xd + wd + 5;
810       }
811     }
812     LABEL(xb, yb, wb, hb);
813   }
814 #undef LABEL
815
816   g_object_unref ( G_OBJECT ( pl ) );
817   g_object_unref ( G_OBJECT ( labgc ) );
818   g_object_unref ( G_OBJECT ( thickgc ) );
819 }
820
821 typedef struct {
822   VikWindow *vw;
823   VikViewport *vvp;
824   gboolean has_oldcoord;
825   VikCoord oldcoord;
826 } ruler_tool_state_t;
827
828 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp) 
829 {
830   ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
831   s->vw = vw;
832   s->vvp = vvp;
833   s->has_oldcoord = FALSE;
834   return s;
835 }
836
837 static void ruler_destroy (ruler_tool_state_t *s)
838 {
839   g_free(s);
840 }
841
842 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
843 {
844   struct LatLon ll;
845   VikCoord coord;
846   gchar *temp;
847   if ( event->button == 1 ) {
848     gchar *lat=NULL, *lon=NULL;
849     vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
850     vik_coord_to_latlon ( &coord, &ll );
851     a_coords_latlon_to_string ( &ll, &lat, &lon );
852     if ( s->has_oldcoord ) {
853       vik_units_distance_t dist_units = a_vik_get_units_distance ();
854       switch (dist_units) {
855       case VIK_UNITS_DISTANCE_KILOMETRES:
856         temp = g_strdup_printf ( "%s %s DIFF %f meters", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
857         break;
858       case VIK_UNITS_DISTANCE_MILES:
859         temp = g_strdup_printf ( "%s %s DIFF %f miles", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) )* 0.000621371192);
860         break;
861       default:
862         temp = g_strdup_printf ("Just to keep the compiler happy");
863         g_critical("Houston, we've had a problem. distance=%d", dist_units);
864       }
865
866       s->has_oldcoord = FALSE;
867     }
868     else {
869       temp = g_strdup_printf ( "%s %s", lat, lon );
870       s->has_oldcoord = TRUE;
871     }
872
873     vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
874     g_free ( temp );
875
876     s->oldcoord = coord;
877   }
878   else {
879     vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
880     draw_update ( s->vw );
881   }
882   return VIK_LAYER_TOOL_ACK;
883 }
884
885 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventMotion *event, ruler_tool_state_t *s)
886 {
887   VikViewport *vvp = s->vvp;
888   VikWindow *vw = s->vw;
889
890   struct LatLon ll;
891   VikCoord coord;
892   gchar *temp;
893
894   if ( s->has_oldcoord ) {
895     int oldx, oldy, w1, h1, w2, h2;
896     static GdkPixmap *buf = NULL;
897     gchar *lat=NULL, *lon=NULL;
898     w1 = vik_viewport_get_width(vvp); 
899     h1 = vik_viewport_get_height(vvp);
900     if (!buf) {
901       buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
902     }
903     gdk_drawable_get_size(buf, &w2, &h2);
904     if (w1 != w2 || h1 != h2) {
905       g_object_unref ( G_OBJECT ( buf ) );
906       buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
907     }
908
909     vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
910     vik_coord_to_latlon ( &coord, &ll );
911     vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
912
913     gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc, 
914                        vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
915     draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
916     if (draw_buf_done) {
917       static gpointer pass_along[3];
918       pass_along[0] = GTK_WIDGET(vvp)->window;
919       pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
920       pass_along[2] = buf;
921       g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
922       draw_buf_done = FALSE;
923     }
924     a_coords_latlon_to_string(&ll, &lat, &lon);
925     vik_units_distance_t dist_units = a_vik_get_units_distance ();
926     switch (dist_units) {
927     case VIK_UNITS_DISTANCE_KILOMETRES:
928       temp = g_strdup_printf ( "%s %s DIFF %f meters", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
929       break;
930     case VIK_UNITS_DISTANCE_MILES:
931       temp = g_strdup_printf ( "%s %s DIFF %f miles", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) )* 0.000621371192);
932       break;
933     default:
934       temp = g_strdup_printf ("Just to keep the compiler happy");
935       g_critical("Houston, we've had a problem. distance=%d", dist_units);
936     }
937     vik_statusbar_set_message ( vw->viking_vs, 3, temp );
938     g_free ( temp );
939   }
940   return VIK_LAYER_TOOL_ACK;
941 }
942
943 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
944 {
945   return VIK_LAYER_TOOL_ACK;
946 }
947
948 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
949 {
950   draw_update ( s->vw );
951 }
952
953 static VikToolInterface ruler_tool = 
954   { "Ruler", 
955     (VikToolConstructorFunc) ruler_create,
956     (VikToolDestructorFunc) ruler_destroy,
957     (VikToolActivationFunc) NULL,
958     (VikToolActivationFunc) ruler_deactivate, 
959     (VikToolMouseFunc) ruler_click, 
960     (VikToolMouseMoveFunc) ruler_move, 
961     (VikToolMouseFunc) ruler_release,
962     NULL,
963     GDK_CURSOR_IS_PIXMAP,
964     &cursor_ruler_pixbuf };
965 /*** end ruler code ********************************************************/
966
967
968
969 /********************************************************************************
970  ** Zoom tool code
971  ********************************************************************************/
972 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
973 {
974   return vw;
975 }
976
977 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
978 {
979   vw->modified = TRUE;
980   vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
981   if ( event->button == 1 )
982     vik_viewport_zoom_in (vw->viking_vvp);
983   else if ( event->button == 3 )
984     vik_viewport_zoom_out (vw->viking_vvp);
985   draw_update ( vw );
986   return VIK_LAYER_TOOL_ACK;
987 }
988
989 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventMotion *event, VikViewport *vvp)
990 {
991   return VIK_LAYER_TOOL_ACK;
992 }
993
994 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
995 {
996   return VIK_LAYER_TOOL_ACK;
997 }
998
999 static VikToolInterface zoom_tool = 
1000   { "Zoom", 
1001     (VikToolConstructorFunc) zoomtool_create,
1002     (VikToolDestructorFunc) NULL,
1003     (VikToolActivationFunc) NULL,
1004     (VikToolActivationFunc) NULL,
1005     (VikToolMouseFunc) zoomtool_click, 
1006     (VikToolMouseMoveFunc) zoomtool_move,
1007     (VikToolMouseFunc) zoomtool_release,
1008     NULL,
1009     GDK_CURSOR_IS_PIXMAP,
1010     &cursor_zoom_pixbuf };
1011 /*** end zoom code ********************************************************/
1012
1013 /********************************************************************************
1014  ** Pan tool code
1015  ********************************************************************************/
1016 static gpointer pantool_create (VikWindow *vw, VikViewport *vvp)
1017 {
1018   return vw;
1019 }
1020
1021 static VikLayerToolFuncStatus pantool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
1022 {
1023   vw->modified = TRUE;
1024   if ( event->button == 1 )
1025     vik_window_pan_click ( vw, event );
1026   draw_update ( vw );
1027   return VIK_LAYER_TOOL_ACK;
1028 }
1029
1030 static VikLayerToolFuncStatus pantool_move (VikLayer *vl, GdkEventMotion *event, VikWindow *vw)
1031 {
1032   vik_window_pan_move ( vw, event );
1033   return VIK_LAYER_TOOL_ACK;
1034 }
1035
1036 static VikLayerToolFuncStatus pantool_release (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
1037 {
1038   if ( event->button == 1 )
1039     vik_window_pan_release ( vw, event );
1040   return VIK_LAYER_TOOL_ACK;
1041 }
1042
1043 static VikToolInterface pan_tool = 
1044   { "Pan", 
1045     (VikToolConstructorFunc) pantool_create,
1046     (VikToolDestructorFunc) NULL,
1047     (VikToolActivationFunc) NULL,
1048     (VikToolActivationFunc) NULL,
1049     (VikToolMouseFunc) pantool_click, 
1050     (VikToolMouseMoveFunc) pantool_move,
1051     (VikToolMouseFunc) pantool_release,
1052     NULL,
1053     GDK_FLEUR };
1054 /*** end pan code ********************************************************/
1055
1056 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
1057 {
1058   if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
1059     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
1060   } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
1061     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
1062   } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
1063     vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
1064   } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
1065     vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
1066   }
1067   draw_update ( vw );
1068 }
1069
1070 static void full_screen_cb ( GtkAction *a, VikWindow *vw )
1071 {
1072   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
1073   g_assert(check_box);
1074   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1075   if ( state )
1076     gtk_window_fullscreen ( GTK_WINDOW(vw) );
1077   else
1078     gtk_window_unfullscreen ( GTK_WINDOW(vw) );
1079 }
1080
1081 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
1082 {
1083   guint what = 128;
1084
1085   if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
1086     what = -3;
1087   } 
1088   else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
1089     what = -4;
1090   }
1091   else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
1092     what = -2;
1093   }
1094   else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
1095     what = -1;
1096   }
1097   else {
1098     gchar *s = (gchar *)gtk_action_get_name(a);
1099     what = atoi(s+4);
1100   }
1101
1102   switch (what)
1103   {
1104     case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
1105     case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
1106     case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
1107     case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
1108     default: vik_viewport_set_zoom ( vw->viking_vvp, what );
1109   }
1110   draw_update ( vw );
1111 }
1112
1113 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
1114 {
1115   VikCoord new_center;
1116
1117   if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
1118     struct LatLon ll, llold;
1119     vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
1120     if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
1121       vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
1122     else
1123       return;
1124   }
1125   else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
1126     struct UTM utm, utmold;
1127     vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
1128     if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
1129       vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
1130     else
1131      return;
1132   }
1133   else {
1134     g_critical("Houston, we've had a problem.");
1135     return;
1136   }
1137
1138   vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
1139   draw_update ( vw );
1140 }
1141
1142 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
1143 {
1144   gint type;
1145   for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
1146     if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
1147       if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
1148         draw_update ( vw );
1149         vw->modified = TRUE;
1150       }
1151     }
1152   }
1153 }
1154
1155 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
1156 {
1157   a_clipboard_copy_selected ( vw->viking_vlp );
1158 }
1159
1160 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
1161 {
1162   a_clipboard_copy_selected ( vw->viking_vlp );
1163   menu_delete_layer_cb ( a, vw );
1164 }
1165
1166 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1167 {
1168   if ( a_clipboard_paste ( vw->viking_vlp ) )
1169   {
1170     draw_update ( vw );
1171     vw->modified = TRUE;
1172   }
1173 }
1174
1175 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1176 {
1177   if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1178     a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1179 }
1180
1181 static void help_help_cb ( GtkAction *a, VikWindow *vw )
1182 {
1183 #if GTK_CHECK_VERSION (2, 14, 0)
1184   gchar *uri;
1185   uri = g_strdup_printf("ghelp:%s", PACKAGE);
1186   gtk_show_uri(NULL, uri, GDK_CURRENT_TIME, NULL);
1187   g_free(uri);
1188 #endif
1189 }
1190
1191 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1192 {
1193   a_dialog_about(GTK_WINDOW(vw));
1194 }
1195
1196 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1197 {
1198   if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1199   {
1200     vik_layers_panel_delete_selected ( vw->viking_vlp );
1201     vw->modified = TRUE;
1202   }
1203   else
1204     a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1205 }
1206
1207 static void view_side_panel_cb ( GtkAction *a, VikWindow *vw )
1208 {
1209   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ViewSidePanel" );
1210   g_assert(check_box);
1211   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1212   if ( state )
1213     gtk_widget_show(GTK_WIDGET(vw->viking_vlp));
1214   else
1215     gtk_widget_hide(GTK_WIDGET(vw->viking_vlp));
1216 }
1217
1218 /***************************************
1219  ** tool management routines
1220  **
1221  ***************************************/
1222
1223 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1224 {
1225   toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1226   vt->tools = NULL;
1227   vt->n_tools = 0;
1228   vt->active_tool = -1;
1229   vt->vw = vw;
1230   if (!vw->viking_vvp) {
1231     g_critical("no viewport found.");
1232     exit(1);
1233   }
1234   return vt;
1235 }
1236
1237 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1238 {
1239   vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1240   vt->tools[vt->n_tools].ti = *vti;
1241   vt->tools[vt->n_tools].layer_type = layer_type;
1242   if (vti->create) {
1243     vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1244   } 
1245   else {
1246     vt->tools[vt->n_tools].state = NULL;
1247   }
1248   vt->n_tools++;
1249 }
1250
1251 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1252 {
1253   int i;
1254   for (i=0; i<vt->n_tools; i++) {
1255     if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1256       break;
1257     }
1258   }
1259   return i;
1260 }
1261
1262 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1263 {
1264   int tool = toolbox_get_tool(vt, tool_name);
1265   toolbox_tool_t *t = &vt->tools[tool];
1266   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1267
1268   if (tool == vt->n_tools) {
1269     g_critical("trying to activate a non-existent tool...");
1270     exit(1);
1271   }
1272   /* is the tool already active? */
1273   if (vt->active_tool == tool) {
1274     return;
1275   }
1276
1277   if (vt->active_tool != -1) {
1278     if (vt->tools[vt->active_tool].ti.deactivate) {
1279       vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1280     }
1281   }
1282   if (t->ti.activate) {
1283     t->ti.activate(vl, t->state);
1284   }
1285   vt->active_tool = tool;
1286 }
1287
1288 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
1289 {
1290   int tool = toolbox_get_tool(vt, tool_name);
1291   toolbox_tool_t *t = &vt->tools[tool];
1292   if (t->ti.cursor == NULL) {
1293     if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
1294       GError *cursor_load_err = NULL;
1295       GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
1296       /* TODO: settable offeset */
1297       t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
1298       g_object_unref ( G_OBJECT(cursor_pixbuf) );
1299     } else {
1300       t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
1301     }
1302   }
1303   return t->ti.cursor;
1304 }
1305
1306 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1307 {
1308   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1309   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1310     gint ltype = vt->tools[vt->active_tool].layer_type;
1311     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1312       vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1313   }
1314 }
1315
1316 static void toolbox_move (toolbox_tools_t *vt, GdkEventMotion *event)
1317 {
1318   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1319   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1320     gint ltype = vt->tools[vt->active_tool].layer_type;
1321     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1322       if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1323         gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1324   }
1325 }
1326
1327 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1328 {
1329   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1330   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1331     gint ltype = vt->tools[vt->active_tool].layer_type;
1332     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1333       vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1334   }
1335 }
1336 /** End tool management ************************************/
1337
1338 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1339 {
1340   gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1341 }
1342
1343 /* this function gets called whenever a toolbar tool is clicked */
1344 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1345 {
1346   /* White Magic, my friends ... White Magic... */
1347   int layer_id, tool_id;
1348   const GdkCursor *cursor = NULL;
1349
1350   toolbox_activate(vw->vt, gtk_action_get_name(a));
1351
1352   cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
1353   /* We set cursor, even if it is NULL: it resets to default */
1354   gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, (GdkCursor *)cursor );
1355
1356   if (!strcmp(gtk_action_get_name(a), "Pan")) {
1357     vw->current_tool = TOOL_PAN;
1358   } 
1359   else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1360     vw->current_tool = TOOL_ZOOM;
1361   } 
1362   else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1363     vw->current_tool = TOOL_RULER;
1364   }
1365   else {
1366     /* TODO: only enable tools from active layer */
1367     for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1368       for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1369         if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1370            vw->current_tool = TOOL_LAYER;
1371            vw->tool_layer_id = layer_id;
1372            vw->tool_tool_id = tool_id;
1373         }
1374       }
1375     }
1376   }
1377   draw_status ( vw );
1378 }
1379
1380 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1381 {
1382   gchar *title;
1383   const gchar *file;
1384   if ( vw->filename )
1385     g_free ( vw->filename );
1386   if ( filename == NULL )
1387   {
1388     vw->filename = NULL;
1389     file = _("Untitled");
1390   }
1391   else
1392   {
1393     vw->filename = g_strdup(filename);
1394     file = a_file_basename ( filename );
1395   }
1396   title = g_strdup_printf( "%s - Viking", file );
1397   gtk_window_set_title ( GTK_WINDOW(vw), title );
1398   g_free ( title );
1399 }
1400
1401 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1402 {
1403   GtkWidget *mode_button;
1404   gchar *buttonname;
1405   switch ( mode ) {
1406 #ifdef VIK_CONFIG_EXPEDIA
1407     case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1408 #endif
1409     case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1410     default: buttonname = "/ui/MainMenu/View/ModeUTM";
1411   }
1412   mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1413   g_assert ( mode_button );
1414   return mode_button;
1415 }
1416
1417 /**
1418  * vik_window_get_pan_move:
1419  * @vw: some VikWindow
1420  *
1421  * Retrieves @vw's pan_move.
1422  *
1423  * Should be removed as soon as possible.
1424  *
1425  * Returns: @vw's pan_move
1426  *
1427  * Since: 0.9.96
1428  **/
1429 gboolean vik_window_get_pan_move ( VikWindow *vw )
1430 {
1431   return vw->pan_move;
1432 }
1433
1434 static void on_activate_recent_item (GtkRecentChooser *chooser,
1435                                      VikWindow *self)
1436 {
1437   gchar *filename;
1438
1439   filename = gtk_recent_chooser_get_current_uri (chooser);
1440   if (filename != NULL)
1441   {
1442     GFile *file = g_file_new_for_uri ( filename );
1443     gchar *path = g_file_get_path ( file );
1444     g_object_unref ( file );
1445     if ( self->filename )
1446     {
1447       gchar *filenames[] = { path, NULL };
1448       g_signal_emit ( G_OBJECT(self), window_signals[VW_OPENWINDOW_SIGNAL], 0, filenames );
1449     }
1450     else
1451       vik_window_open_file ( self, path, TRUE );
1452     g_free ( path );
1453   }
1454
1455   g_free (filename);
1456 }
1457
1458 static void setup_recent_files (VikWindow *self)
1459 {
1460   GtkRecentManager *manager;
1461   GtkRecentFilter *filter;
1462   GtkWidget *menu, *menu_item;
1463
1464   filter = gtk_recent_filter_new ();
1465   /* gtk_recent_filter_add_application (filter, g_get_application_name()); */
1466   gtk_recent_filter_add_group(filter, "viking");
1467
1468   manager = gtk_recent_manager_get_default ();
1469   menu = gtk_recent_chooser_menu_new_for_manager (manager);
1470   gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu), GTK_RECENT_SORT_MRU);
1471   gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu), filter);
1472
1473   menu_item = gtk_ui_manager_get_widget (self->uim, "/ui/MainMenu/File/OpenRecentFile");
1474   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
1475
1476   g_signal_connect (G_OBJECT (menu), "item-activated",
1477                     G_CALLBACK (on_activate_recent_item), (gpointer) self);
1478 }
1479
1480 static void update_recently_used_document(const gchar *filename)
1481 {
1482   /* Update Recently Used Document framework */
1483   GtkRecentManager *manager = gtk_recent_manager_get_default();
1484   GtkRecentData *recent_data = g_slice_new (GtkRecentData);
1485   gchar *groups[] = {"viking", NULL};
1486   GFile *file = g_file_new_for_commandline_arg(filename);
1487   gchar *uri = g_file_get_uri(file);
1488   gchar *basename = g_path_get_basename(filename);
1489   g_object_unref(file);
1490   file = NULL;
1491
1492   recent_data->display_name   = basename;
1493   recent_data->description    = NULL;
1494   recent_data->mime_type      = "text/x-gps-data";
1495   recent_data->app_name       = (gchar *) g_get_application_name ();
1496   recent_data->app_exec       = g_strjoin (" ", g_get_prgname (), "%f", NULL);
1497   recent_data->groups         = groups;
1498   recent_data->is_private     = FALSE;
1499   if (!gtk_recent_manager_add_full (manager, uri, recent_data))
1500   {
1501     g_warning (_("Unable to add '%s' to the list of recently used documents"), uri);
1502   }
1503
1504   g_free (uri);
1505   g_free (basename);
1506   g_free (recent_data->app_exec);
1507   g_slice_free (GtkRecentData, recent_data);
1508 }
1509
1510 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1511 {
1512   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1513   {
1514     case 0:
1515       a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1516       break;
1517     case 1:
1518     {
1519       GtkWidget *mode_button;
1520       /* Update UI */
1521       if ( change_filename )
1522         window_set_filename ( vw, filename );
1523       mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1524       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. */
1525       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1526       vw->only_updating_coord_mode_ui = FALSE;
1527
1528       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1529
1530       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1531       g_assert ( mode_button );
1532       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1533
1534       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1535       g_assert ( mode_button );
1536       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1537     }
1538     default:
1539       update_recently_used_document(filename);
1540       draw_update ( vw );
1541   }
1542 }
1543 static void load_file ( GtkAction *a, VikWindow *vw )
1544 {
1545   GSList *files = NULL;
1546   GSList *cur_file = NULL;
1547   gboolean newwindow;
1548   if (!strcmp(gtk_action_get_name(a), "Open")) {
1549     newwindow = TRUE;
1550   } 
1551   else if (!strcmp(gtk_action_get_name(a), "Append")) {
1552     newwindow = FALSE;
1553   } 
1554   else {
1555     g_critical("Houston, we've had a problem.");
1556     return;
1557   }
1558     
1559   if ( ! vw->open_dia )
1560   {
1561     vw->open_dia = gtk_file_chooser_dialog_new (_("Please select a GPS data file to open. "),
1562                                       GTK_WINDOW(vw),
1563                                       GTK_FILE_CHOOSER_ACTION_OPEN,
1564                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1565                                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1566                                       NULL);
1567     gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(vw->open_dia), TRUE );
1568     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1569     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1570   }
1571   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_ACCEPT )
1572   {
1573     gtk_widget_hide ( vw->open_dia );
1574 #ifdef VIKING_PROMPT_IF_MODIFIED
1575     if ( (vw->modified || vw->filename) && newwindow )
1576 #else
1577     if ( vw->filename && newwindow )
1578 #endif
1579       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) ) );
1580     else {
1581       files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) );
1582       gboolean change_fn = newwindow && (g_slist_length(files)==1); /* only change fn if one file */
1583       
1584       cur_file = files;
1585       while ( cur_file ) {
1586         gchar *file_name = cur_file->data;
1587         vik_window_open_file ( vw, file_name, change_fn );
1588         g_free (file_name);
1589         cur_file = g_slist_next (cur_file);
1590       }
1591       g_slist_free (files);
1592     }
1593   }
1594   else
1595     gtk_widget_hide ( vw->open_dia );
1596 }
1597
1598 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1599 {
1600   gboolean rv = FALSE;
1601   const gchar *fn;
1602   if ( ! vw->save_dia )
1603   {
1604     vw->save_dia = gtk_file_chooser_dialog_new (_("Save as Viking File."),
1605                                       GTK_WINDOW(vw),
1606                                       GTK_FILE_CHOOSER_ACTION_SAVE,
1607                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1608                                       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1609                                       NULL);
1610     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1611     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1612   }
1613
1614   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
1615   {
1616     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
1617     if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_overwrite ( GTK_WINDOW(vw->save_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
1618     {
1619       window_set_filename ( vw, fn );
1620       rv = window_save ( vw );
1621       vw->modified = FALSE;
1622       break;
1623     }
1624   }
1625   gtk_widget_hide ( vw->save_dia );
1626   return rv;
1627 }
1628
1629 static gboolean window_save ( VikWindow *vw )
1630 {
1631   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1632   {
1633     update_recently_used_document ( vw->filename );
1634     return TRUE;
1635   }
1636   else
1637   {
1638     a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1639     return FALSE;
1640   }
1641 }
1642
1643 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1644 {
1645   if ( ! vw->filename )
1646     return save_file_as ( NULL, vw );
1647   else
1648   {
1649     vw->modified = FALSE;
1650     return window_save ( vw );
1651   }
1652 }
1653
1654 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1655 {
1656   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1657 }
1658
1659 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1660 {
1661   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1662 }
1663
1664 #ifdef VIK_CONFIG_GEOCACHES
1665 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1666 {
1667   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1668 }
1669 #endif
1670
1671 static void goto_default_location( GtkAction *a, VikWindow *vw)
1672 {
1673   struct LatLon ll;
1674   ll.lat = a_vik_get_default_lat();
1675   ll.lon = a_vik_get_default_long();
1676   vik_viewport_set_center_latlon(vw->viking_vvp, &ll);
1677   vik_layers_panel_emit_update(vw->viking_vlp);
1678 }
1679
1680
1681 static void goto_address( GtkAction *a, VikWindow *vw)
1682 {
1683   a_vik_goto(vw, vw->viking_vlp, vw->viking_vvp);
1684 }
1685
1686 static void mapcache_flush_cb ( GtkAction *a, VikWindow *vw )
1687 {
1688   a_mapcache_flush();
1689 }
1690
1691 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1692 {
1693   gboolean wp_icon_size = a_vik_get_use_large_waypoint_icons();
1694
1695   a_preferences_show_window ( GTK_WINDOW(vw) );
1696
1697   // Delete icon indexing 'cache' and so automatically regenerates with the new setting when changed
1698   if (wp_icon_size != a_vik_get_use_large_waypoint_icons())
1699     clear_garmin_icon_syms ();
1700
1701   draw_update ( vw );
1702 }
1703
1704 static void default_location_cb ( GtkAction *a, VikWindow *vw )
1705 {
1706   /* Simplistic repeat of preference setting
1707      Only the name & type are important for setting the preference via this 'external' way */
1708   VikLayerParam pref_lat[] = {
1709     { VIKING_PREFERENCES_NAMESPACE "default_latitude",
1710       VIK_LAYER_PARAM_DOUBLE,
1711       VIK_LOCATION_LAT,
1712       NULL,
1713       VIK_LAYER_WIDGET_SPINBUTTON,
1714       NULL,
1715       NULL },
1716   };
1717   VikLayerParam pref_lon[] = {
1718     { VIKING_PREFERENCES_NAMESPACE "default_longitude",
1719       VIK_LAYER_PARAM_DOUBLE,
1720       VIK_LOCATION_LONG,
1721       NULL,
1722       VIK_LAYER_WIDGET_SPINBUTTON,
1723       NULL,
1724       NULL },
1725   };
1726
1727   /* Get current center */
1728   struct LatLon ll;
1729   vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &ll );
1730
1731   /* Apply to preferences */
1732   VikLayerParamData vlp_data;
1733   vlp_data.d = ll.lat;
1734   a_preferences_run_setparam (vlp_data, pref_lat);
1735   vlp_data.d = ll.lon;
1736   a_preferences_run_setparam (vlp_data, pref_lon);
1737   /* Remember to save */
1738   a_preferences_save_to_file();
1739 }
1740
1741 static void clear_cb ( GtkAction *a, VikWindow *vw )
1742 {
1743   vik_layers_panel_clear ( vw->viking_vlp );
1744   window_set_filename ( vw, NULL );
1745   draw_update ( vw );
1746 }
1747
1748 static void window_close ( GtkAction *a, VikWindow *vw )
1749 {
1750   if ( ! delete_event ( vw ) )
1751     gtk_widget_destroy ( GTK_WIDGET(vw) );
1752 }
1753
1754 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1755 {
1756   if (save_file( NULL, vw)) {
1757     window_close( NULL, vw);
1758     return(TRUE);
1759   }
1760   else
1761     return(FALSE);
1762 }
1763
1764 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1765 {
1766   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1767   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1768   {
1769     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1770     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1771     draw_update ( vw );
1772   }
1773 }
1774
1775 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1776 {
1777   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1778   GdkPixbuf *pixbuf_to_save;
1779   gdouble old_xmpp, old_ympp;
1780   GError *error = NULL;
1781
1782   /* backup old zoom & set new */
1783   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1784   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1785   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1786
1787   /* reset width and height: */
1788   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1789
1790   /* draw all layers */
1791   draw_redraw ( vw );
1792
1793   /* save buffer as file. */
1794   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);
1795   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1796   if (error)
1797   {
1798     g_warning("Unable to write to file %s: %s", fn, error->message );
1799     g_error_free (error);
1800   }
1801   g_object_unref ( G_OBJECT(pixbuf_to_save) );
1802
1803   /* pretend like nothing happened ;) */
1804   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1805   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1806   vik_viewport_configure ( vw->viking_vvp );
1807   draw_update ( vw );
1808 }
1809
1810 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 )
1811 {
1812   gulong size = sizeof(gchar) * (strlen(fn) + 15);
1813   gchar *name_of_file = g_malloc ( size );
1814   guint x = 1, y = 1;
1815   struct UTM utm_orig, utm;
1816
1817   /* *** copied from above *** */
1818   GdkPixbuf *pixbuf_to_save;
1819   gdouble old_xmpp, old_ympp;
1820   GError *error = NULL;
1821
1822   /* backup old zoom & set new */
1823   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1824   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1825   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1826
1827   /* reset width and height: do this only once for all images (same size) */
1828   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1829   /* *** end copy from above *** */
1830
1831   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1832
1833   g_mkdir(fn,0777);
1834
1835   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1836
1837   for ( y = 1; y <= tiles_h; y++ )
1838   {
1839     for ( x = 1; x <= tiles_w; x++ )
1840     {
1841       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1842       utm = utm_orig;
1843       if ( tiles_w & 0x1 )
1844         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1845       else
1846         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1847       if ( tiles_h & 0x1 ) /* odd */
1848         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1849       else /* even */
1850         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1851
1852       /* move to correct place. */
1853       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1854
1855       draw_redraw ( vw );
1856
1857       /* save buffer as file. */
1858       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);
1859       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1860       if (error)
1861       {
1862         g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1863         g_error_free (error);
1864       }
1865
1866       g_object_unref ( G_OBJECT(pixbuf_to_save) );
1867     }
1868   }
1869
1870   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1871   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1872   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1873   vik_viewport_configure ( vw->viking_vvp );
1874   draw_update ( vw );
1875
1876   g_free ( name_of_file );
1877 }
1878
1879 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1880 {
1881   VikWindow *vw = VIK_WINDOW(pass_along[0]);
1882   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1883   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1884   gdouble width_min, width_max, height_min, height_max;
1885   gint width, height;
1886
1887   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1888   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1889
1890   /* TODO: support for xzoom and yzoom values */
1891   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1892   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1893
1894   if ( width > width_max || width < width_min || height > height_max || height < height_min )
1895     a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1896
1897   gtk_spin_button_set_value ( width_spin, width );
1898   gtk_spin_button_set_value ( height_spin, height );
1899 }
1900
1901 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1902 {
1903   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1904   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1905   gchar *label_text;
1906   gdouble w, h;
1907   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1908   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1909   if (pass_along[4]) /* save many images; find TOTAL area covered */
1910   {
1911     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1912     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1913   }
1914   vik_units_distance_t dist_units = a_vik_get_units_distance ();
1915   switch (dist_units) {
1916   case VIK_UNITS_DISTANCE_KILOMETRES:
1917     label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1918     break;
1919   case VIK_UNITS_DISTANCE_MILES:
1920     label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. miles)"), (glong)w, (glong)h, (w*h/2589988.11));
1921     break;
1922   default:
1923     label_text = g_strdup_printf ("Just to keep the compiler happy");
1924     g_critical("Houston, we've had a problem. distance=%d", dist_units);
1925   }
1926
1927   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1928   g_free ( label_text );
1929 }
1930
1931 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1932 {
1933   /* todo: default for answers inside VikWindow or static (thruout instance) */
1934   GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1935                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1936                                                   GTK_STOCK_CANCEL,
1937                                                   GTK_RESPONSE_REJECT,
1938                                                   GTK_STOCK_OK,
1939                                                   GTK_RESPONSE_ACCEPT,
1940                                                   NULL );
1941   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1942   GtkWidget *png_radio, *jpeg_radio;
1943   GtkWidget *current_window_button;
1944   gpointer current_window_pass_along[7];
1945   GtkWidget *zoom_label, *zoom_spin;
1946   GtkWidget *total_size_label;
1947
1948   /* only used if (!one_image_only) */
1949   GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1950
1951
1952   width_label = gtk_label_new ( _("Width (pixels):") );
1953   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1954   height_label = gtk_label_new ( _("Height (pixels):") );
1955   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1956
1957   zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1958   /* TODO: separate xzoom and yzoom factors */
1959   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, 0 )), 16, 0);
1960
1961   total_size_label = gtk_label_new ( NULL );
1962
1963   current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1964   current_window_pass_along [0] = vw;
1965   current_window_pass_along [1] = width_spin;
1966   current_window_pass_along [2] = height_spin;
1967   current_window_pass_along [3] = zoom_spin;
1968   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1969   current_window_pass_along [5] = NULL;
1970   current_window_pass_along [6] = total_size_label;
1971   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1972
1973   png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1974   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1975
1976   if ( ! vw->draw_image_save_as_png )
1977     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1978
1979   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1980   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1981   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1982   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1983   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1984   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1985   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1986   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1987   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1988
1989   if ( ! one_image_only )
1990   {
1991     GtkWidget *tiles_width_label, *tiles_height_label;
1992
1993
1994     tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1995     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1996     tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1997     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1998     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1999     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
2000     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
2001     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
2002
2003     current_window_pass_along [4] = tiles_width_spin;
2004     current_window_pass_along [5] = tiles_height_spin;
2005     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2006     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2007   }
2008   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
2009   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2010   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2011   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2012
2013   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
2014
2015   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
2016
2017   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
2018   {
2019     gtk_widget_hide ( GTK_WIDGET(dialog) );
2020     if ( one_image_only )
2021       save_image_file ( vw, fn, 
2022                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
2023                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
2024                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
2025                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
2026     else {
2027       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
2028         save_image_dir ( vw, fn, 
2029                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
2030                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
2031                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
2032                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
2033                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
2034                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
2035       else
2036         a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
2037     }
2038   }
2039   gtk_widget_destroy ( GTK_WIDGET(dialog) );
2040 }
2041
2042
2043 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
2044 {
2045   const gchar *fn;
2046   if (!vw->save_img_dia) {
2047     vw->save_img_dia = gtk_file_chooser_dialog_new (_("Save Image"),
2048                                       GTK_WINDOW(vw),
2049                                       GTK_FILE_CHOOSER_ACTION_SAVE,
2050                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2051                                       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
2052                                       NULL);
2053     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
2054     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
2055   }
2056
2057   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
2058   {
2059     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
2060     if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_overwrite ( GTK_WINDOW(vw->save_img_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
2061     {
2062       draw_to_image_file ( vw, fn, TRUE );
2063       break;
2064     }
2065   }
2066   gtk_widget_hide ( vw->save_img_dia );
2067 }
2068
2069 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
2070 {
2071   gchar *fn = NULL;
2072   
2073   if (!vw->save_img_dir_dia) {
2074     vw->save_img_dir_dia = gtk_file_chooser_dialog_new (_("Choose a directory to hold images"),
2075                                       GTK_WINDOW(vw),
2076                                       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
2077                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2078                                       GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2079                                       NULL);
2080     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
2081     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
2082   }
2083   
2084   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_ACCEPT )
2085   {
2086     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dir_dia) );
2087     if ( fn )
2088     {
2089       draw_to_image_file ( vw, fn, FALSE );
2090       g_free(fn);
2091       fn = NULL;
2092       break;
2093     }
2094   }
2095   gtk_widget_hide ( vw->save_img_dir_dia );
2096 }
2097
2098 #if GTK_CHECK_VERSION(2,10,0)
2099 static void print_cb ( GtkAction *a, VikWindow *vw )
2100 {
2101   a_print(vw, vw->viking_vvp);
2102 }
2103 #endif
2104
2105 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
2106 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
2107 {
2108   VikViewportDrawMode drawmode;
2109   if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
2110     drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
2111   }
2112   else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
2113     drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
2114   }
2115   else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
2116     drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
2117   }
2118   else {
2119     g_critical("Houston, we've had a problem.");
2120     return;
2121   }
2122
2123   if ( !vw->only_updating_coord_mode_ui )
2124   {
2125     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
2126     if ( olddrawmode != drawmode )
2127     {
2128       /* this takes care of coord mode too */
2129       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
2130       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
2131         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
2132       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
2133         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
2134       }
2135       draw_update ( vw );
2136     }
2137   }
2138 }
2139
2140 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
2141 {
2142   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
2143   g_assert(check_box);
2144   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2145   vik_viewport_set_draw_scale ( vw->viking_vvp, state );
2146   draw_update ( vw );
2147 }
2148
2149 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
2150 {
2151   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
2152   g_assert(check_box);
2153   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2154   vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
2155   draw_update ( vw );
2156 }
2157
2158 static void set_bg_color ( GtkAction *a, VikWindow *vw )
2159 {
2160   GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
2161   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
2162   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2163   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2164   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
2165   {
2166     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2167     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
2168     draw_update ( vw );
2169   }
2170   g_free ( color );
2171   gtk_widget_destroy ( colorsd );
2172 }
2173
2174
2175
2176 /***********************************************************************************************
2177  ** GUI Creation
2178  ***********************************************************************************************/
2179
2180 static GtkActionEntry entries[] = {
2181   { "File", NULL, N_("_File"), 0, 0, 0 },
2182   { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
2183   { "View", NULL, N_("_View"), 0, 0, 0 },
2184   { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
2185   { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
2186   { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
2187   { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
2188   { "Exttools", NULL, N_("_Webtools"), 0, 0, 0 },
2189   { "Help", NULL, N_("_Help"), 0, 0, 0 },
2190
2191   { "New",       GTK_STOCK_NEW,          N_("_New"),                          "<control>N", N_("New file"),                                     (GCallback)newwindow_cb          },
2192   { "Open",      GTK_STOCK_OPEN,         N_("_Open..."),                         "<control>O", N_("Open a file"),                                  (GCallback)load_file             },
2193   { "OpenRecentFile", NULL,              N_("Open _Recent File"),         NULL,         NULL,                                               (GCallback)NULL },
2194   { "Append",    GTK_STOCK_ADD,          N_("Append _File..."),           NULL,         N_("Append data from a different file"),            (GCallback)load_file             },
2195   { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
2196   { "AcquireGPS",   NULL,                N_("From _GPS..."),              NULL,         N_("Transfer data from a GPS device"),              (GCallback)acquire_from_gps      },
2197   { "AcquireGoogle",   NULL,             N_("Google _Directions..."),     NULL,         N_("Get driving directions from Google"),           (GCallback)acquire_from_google   },
2198 #ifdef VIK_CONFIG_GEOCACHES
2199   { "AcquireGC",   NULL,                 N_("Geo_caches..."),             NULL,         N_("Get Geocaches from geocaching.com"),            (GCallback)acquire_from_gc       },
2200 #endif
2201   { "Save",      GTK_STOCK_SAVE,         N_("_Save"),                         "<control>S", N_("Save the file"),                                (GCallback)save_file             },
2202   { "SaveAs",    GTK_STOCK_SAVE_AS,      N_("Save _As..."),                      NULL,  N_("Save the file under different name"),           (GCallback)save_file_as          },
2203   { "GenImg",    GTK_STOCK_CLEAR,        N_("_Generate Image File..."),          NULL,  N_("Save a snapshot of the workspace into a file"), (GCallback)draw_to_image_file_cb },
2204   { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images..."), NULL,  N_("FIXME:IMGDIR"),                                 (GCallback)draw_to_image_dir_cb  },
2205
2206 #if GTK_CHECK_VERSION(2,10,0)
2207   { "Print",    GTK_STOCK_PRINT,        N_("_Print..."),          NULL,         N_("Print maps"), (GCallback)print_cb },
2208 #endif
2209
2210   { "Exit",      GTK_STOCK_QUIT,         N_("E_xit"),                         "<control>W", N_("Exit the program"),                             (GCallback)window_close          },
2211   { "SaveExit",  GTK_STOCK_QUIT,         N_("Save and Exit"),                 NULL, N_("Save and Exit the program"),                             (GCallback)save_file_and_exit          },
2212
2213   { "GotoDefaultLocation", GTK_STOCK_HOME, N_("Go to the _Default Location"),  NULL,         N_("Go to the default location"),                     (GCallback)goto_default_location },
2214   { "GotoSearch", GTK_STOCK_JUMP_TO,     N_("Go to _Location..."),            NULL,         N_("Go to address/place using text search"),        (GCallback)goto_address       },
2215   { "GotoLL",    GTK_STOCK_JUMP_TO,      N_("_Go to Lat/Lon..."),           NULL,         N_("Go to arbitrary lat/lon coordinate"),         (GCallback)draw_goto_cb          },
2216   { "GotoUTM",   GTK_STOCK_JUMP_TO,      N_("Go to UTM..."),                  NULL,         N_("Go to arbitrary UTM coordinate"),               (GCallback)draw_goto_cb          },
2217   { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Bac_kground Color..."),      NULL,         NULL,                                           (GCallback)set_bg_color          },
2218   { "ZoomIn",    GTK_STOCK_ZOOM_IN,      N_("Zoom _In"),                   "<control>plus", NULL,                                           (GCallback)draw_zoom_cb          },
2219   { "ZoomOut",   GTK_STOCK_ZOOM_OUT,     N_("Zoom _Out"),                 "<control>minus", NULL,                                           (GCallback)draw_zoom_cb          },
2220   { "ZoomTo",    GTK_STOCK_ZOOM_FIT,     N_("Zoom _To..."),               "<control>Z", NULL,                                           (GCallback)zoom_to_cb            },
2221   { "Zoom0.25",  NULL,                   N_("0.25"),                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2222   { "Zoom0.5",   NULL,                   N_("0.5"),                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2223   { "Zoom1",     NULL,                   N_("1"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2224   { "Zoom2",     NULL,                   N_("2"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2225   { "Zoom4",     NULL,                   N_("4"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2226   { "Zoom8",     NULL,                   N_("8"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2227   { "Zoom16",    NULL,                   N_("16"),                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2228   { "Zoom32",    NULL,                   N_("32"),                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2229   { "Zoom64",    NULL,                   N_("64"),                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2230   { "Zoom128",   NULL,                   N_("128"),                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2231   { "Zoom256",   NULL,                   N_("256"),                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2232   { "Zoom512",   NULL,                   N_("512"),                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2233   { "Zoom1024",  NULL,                   N_("1024"),                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2234   { "Zoom2048",  NULL,                   N_("2048"),                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2235   { "Zoom4096",  NULL,                   N_("4096"),                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2236   { "Zoom8192",  NULL,                   N_("8192"),                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2237   { "Zoom16384", NULL,                   N_("16384"),                         NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2238   { "Zoom32768", NULL,                   N_("32768"),                         NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
2239   { "PanNorth",  NULL,                   N_("Pan _North"),                "<control>Up",    NULL,                                           (GCallback)draw_pan_cb },
2240   { "PanEast",   NULL,                   N_("Pan _East"),                 "<control>Right", NULL,                                           (GCallback)draw_pan_cb },
2241   { "PanSouth",  NULL,                   N_("Pan _South"),                "<control>Down",  NULL,                                           (GCallback)draw_pan_cb },
2242   { "PanWest",   NULL,                   N_("Pan _West"),                 "<control>Left",  NULL,                                           (GCallback)draw_pan_cb },
2243   { "BGJobs",    GTK_STOCK_EXECUTE,      N_("Background _Jobs"),              NULL,         NULL,                                           (GCallback)a_background_show_window },
2244
2245   { "Cut",       GTK_STOCK_CUT,          N_("Cu_t"),                          NULL,         NULL,                                           (GCallback)menu_cut_layer_cb     },
2246   { "Copy",      GTK_STOCK_COPY,         N_("_Copy"),                         NULL,         NULL,                                           (GCallback)menu_copy_layer_cb    },
2247   { "Paste",     GTK_STOCK_PASTE,        N_("_Paste"),                        NULL,         NULL,                                           (GCallback)menu_paste_layer_cb   },
2248   { "Delete",    GTK_STOCK_DELETE,       N_("_Delete"),                       NULL,         NULL,                                           (GCallback)menu_delete_layer_cb  },
2249   { "DeleteAll", NULL,                   N_("Delete All"),                    NULL,         NULL,                                           (GCallback)clear_cb              },
2250   { "MapCacheFlush",NULL,                N_("_Flush Map Cache"),              NULL,         NULL,                                           (GCallback)mapcache_flush_cb     },
2251   { "SetDefaultLocation", GTK_STOCK_GO_FORWARD, N_("_Set the Default Location"), NULL, N_("Set the Default Location to the current position"),(GCallback)default_location_cb },
2252   { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences"),                  NULL,         NULL,                                           (GCallback)preferences_cb              },
2253   { "Properties",GTK_STOCK_PROPERTIES,   N_("_Properties"),                   NULL,         NULL,                                           (GCallback)menu_properties_cb    },
2254
2255   { "HelpEntry", GTK_STOCK_HELP,         N_("_Help"),                         "F1",         NULL,                                           (GCallback)help_help_cb     },
2256   { "About",     GTK_STOCK_ABOUT,        N_("_About"),                        NULL,         NULL,                                           (GCallback)help_about_cb    },
2257 };
2258
2259 /* Radio items */
2260 static GtkRadioActionEntry mode_entries[] = {
2261   { "ModeUTM",         NULL,         N_("_UTM Mode"),               "<control>u", NULL, 0 },
2262   { "ModeExpedia",     NULL,         N_("_Expedia Mode"),           "<control>e", NULL, 1 },
2263   { "ModeMercator",    NULL,         N_("_Mercator Mode"),            "<control>g", NULL, 4 }
2264 };
2265
2266 static GtkRadioActionEntry tool_entries[] = {
2267   { "Pan",      "vik-icon-pan",        N_("_Pan"),                         "<control><shift>P", N_("Pan Tool"),  0 },
2268   { "Zoom",      "vik-icon-zoom",        N_("_Zoom"),                         "<control><shift>Z", N_("Zoom Tool"),  1 },
2269   { "Ruler",     "vik-icon-ruler",       N_("_Ruler"),                        "<control><shift>R", N_("Ruler Tool"), 2 }
2270 };
2271
2272 static GtkToggleActionEntry toggle_entries[] = {
2273   { "ShowScale",      NULL,                 N_("_Show Scale"),               "F5",         N_("Show Scale"),                              (GCallback)set_draw_scale, TRUE },
2274   { "ShowCenterMark", NULL,                 N_("Show _Center Mark"),         "F6",         N_("Show Center Mark"),                        (GCallback)set_draw_centermark, TRUE },
2275   { "FullScreen",     GTK_STOCK_FULLSCREEN, N_("_Full Screen"),              "F11",        N_("Activate full screen mode"),               (GCallback)full_screen_cb, FALSE },
2276   { "ViewSidePanel",  GTK_STOCK_INDEX,      N_("Show Side Pa_nel"),          "F9",         N_("Show Side Panel"),                         (GCallback)view_side_panel_cb, TRUE },
2277 };
2278
2279 #include "menu.xml.h"
2280 static void window_create_ui( VikWindow *window )
2281 {
2282   GtkUIManager *uim;
2283   GtkActionGroup *action_group;
2284   GtkAccelGroup *accel_group;
2285   GError *error;
2286   guint i, j, mid;
2287   GtkIconFactory *icon_factory;
2288   GtkIconSet *icon_set; 
2289   GtkRadioActionEntry *tools = NULL, *radio;
2290   guint ntools;
2291   
2292   uim = gtk_ui_manager_new ();
2293   window->uim = uim;
2294
2295   toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
2296   toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
2297   toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
2298
2299   error = NULL;
2300   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
2301     g_error_free (error);
2302     exit (1);
2303   }
2304
2305   action_group = gtk_action_group_new ("MenuActions");
2306   gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
2307   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
2308   gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
2309   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
2310
2311   icon_factory = gtk_icon_factory_new ();
2312   gtk_icon_factory_add_default (icon_factory); 
2313
2314   register_vik_icons(icon_factory);
2315
2316   ntools = 0;
2317   for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2318       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2319       radio = &tools[ntools];
2320       ntools++;
2321       *radio = tool_entries[i];
2322       radio->value = ntools;
2323   }  
2324
2325   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2326     GtkActionEntry action;
2327     gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Layers/", 
2328                           vik_layer_get_interface(i)->name,
2329                           vik_layer_get_interface(i)->name,
2330                           GTK_UI_MANAGER_MENUITEM, FALSE);
2331
2332     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2333     gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2334     gtk_icon_set_unref (icon_set);
2335
2336     action.name = vik_layer_get_interface(i)->name;
2337     action.stock_id = vik_layer_get_interface(i)->name;
2338     action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
2339     action.accelerator = NULL;
2340     action.tooltip = NULL;
2341     action.callback = (GCallback)menu_addlayer_cb;
2342     gtk_action_group_add_actions(action_group, &action, 1, window);
2343
2344     if ( vik_layer_get_interface(i)->tools_count ) {
2345       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2346       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2347     }
2348
2349     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2350       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2351       radio = &tools[ntools];
2352       ntools++;
2353       
2354       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
2355                             _(vik_layer_get_interface(i)->tools[j].name),
2356                             vik_layer_get_interface(i)->tools[j].name,
2357                             GTK_UI_MANAGER_MENUITEM, FALSE);
2358       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems", 
2359                             _(vik_layer_get_interface(i)->tools[j].name),
2360                             vik_layer_get_interface(i)->tools[j].name,
2361                             GTK_UI_MANAGER_TOOLITEM, FALSE);
2362
2363       toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
2364
2365       radio->name = vik_layer_get_interface(i)->tools[j].name;
2366       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
2367       radio->label = _(vik_layer_get_interface(i)->tools[j].name);
2368       radio->accelerator = NULL;
2369       radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
2370       radio->value = ntools;
2371     }
2372   }
2373   g_object_unref (icon_factory);
2374
2375   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2376   g_free(tools);
2377
2378   gtk_ui_manager_insert_action_group (uim, action_group, 0);
2379
2380   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2381     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2382       GtkAction *action = gtk_action_group_get_action(action_group,
2383                             vik_layer_get_interface(i)->tools[j].name);
2384       g_object_set(action, "sensitive", FALSE, NULL);
2385     }
2386   }
2387   window->action_group = action_group;
2388
2389   accel_group = gtk_ui_manager_get_accel_group (uim);
2390   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2391   gtk_ui_manager_ensure_update (uim);
2392   
2393   setup_recent_files(window);
2394 }
2395
2396
2397
2398 static struct { 
2399   const GdkPixdata *data;
2400   gchar *stock_id;
2401 } stock_icons[] = {
2402   { &begintr_18_pixbuf,         "Begin Track"      },
2403   { &iscissors_18_pixbuf,       "Magic Scissors"   },
2404   { &mover_22_pixbuf,           "vik-icon-pan"     },
2405   { &demdl_18_pixbuf,           "DEM Download/Import"     },
2406   { &showpic_18_pixbuf,         "Show Picture"      },
2407   { &addtr_18_pixbuf,           "Create Track"      },
2408   { &edtr_18_pixbuf,            "Edit Trackpoint"   },
2409   { &addwp_18_pixbuf,           "Create Waypoint"   },
2410   { &edwp_18_pixbuf,            "Edit Waypoint"     },
2411   { &zoom_18_pixbuf,            "vik-icon-zoom"     },
2412   { &ruler_18_pixbuf,           "vik-icon-ruler"    },
2413   { &geozoom_18_pixbuf,         "Georef Zoom Tool"  },
2414   { &geomove_18_pixbuf,         "Georef Move Map"   },
2415   { &mapdl_18_pixbuf,           "Maps Download"     },
2416   { &showpic_18_pixbuf,         "Show Picture"      },
2417 };
2418  
2419 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2420
2421 static void
2422 register_vik_icons (GtkIconFactory *icon_factory)
2423 {
2424   GtkIconSet *icon_set; 
2425   gint i;
2426
2427   for (i = 0; i < n_stock_icons; i++) {
2428     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2429                    stock_icons[i].data, FALSE, NULL ));
2430     gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2431     gtk_icon_set_unref (icon_set);
2432   }
2433 }
2434