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