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