]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
Remove dependencies to gob2
[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     case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1322     default: buttonname = "/ui/MainMenu/View/ModeUTM";
1323   }
1324   mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1325   g_assert ( mode_button );
1326   return mode_button;
1327 }
1328
1329 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1330 {
1331   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1332   {
1333     case 0:
1334       a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1335       break;
1336     case 1:
1337     {
1338       GtkWidget *mode_button;
1339       if ( change_filename )
1340         window_set_filename ( vw, filename );
1341       mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1342       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. */
1343       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1344       vw->only_updating_coord_mode_ui = FALSE;
1345
1346       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1347
1348       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1349       g_assert ( mode_button );
1350       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1351
1352       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1353       g_assert ( mode_button );
1354       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1355     }
1356     default: draw_update ( vw );
1357   }
1358 }
1359 static void load_file ( GtkAction *a, VikWindow *vw )
1360 {
1361   GSList *files = NULL;
1362   GSList *cur_file = NULL;
1363   gboolean newwindow;
1364   if (!strcmp(gtk_action_get_name(a), "Open")) {
1365     newwindow = TRUE;
1366   } 
1367   else if (!strcmp(gtk_action_get_name(a), "Append")) {
1368     newwindow = FALSE;
1369   } 
1370   else {
1371     g_critical("Houston, we've had a problem.");
1372     return;
1373   }
1374     
1375   if ( ! vw->open_dia )
1376   {
1377     vw->open_dia = gtk_file_chooser_dialog_new (_("Please select a GPS data file to open. "),
1378                                       GTK_WINDOW(vw),
1379                                       GTK_FILE_CHOOSER_ACTION_OPEN,
1380                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1381                                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1382                                       NULL);
1383     gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(vw->open_dia), TRUE );
1384     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1385     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1386   }
1387   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_ACCEPT )
1388   {
1389     gtk_widget_hide ( vw->open_dia );
1390 #ifdef VIKING_PROMPT_IF_MODIFIED
1391     if ( (vw->modified || vw->filename) && newwindow )
1392 #else
1393     if ( vw->filename && newwindow )
1394 #endif
1395       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) ) );
1396     else {
1397       files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) );
1398       gboolean change_fn = newwindow && (g_slist_length(files)==1); /* only change fn if one file */
1399       
1400       cur_file = files;
1401       while ( cur_file ) {
1402         gchar *file_name = cur_file->data;
1403         vik_window_open_file ( vw, file_name, change_fn );
1404         g_free (file_name);
1405         cur_file = g_slist_next (cur_file);
1406       }
1407       g_slist_free (files);
1408     }
1409   }
1410   else
1411     gtk_widget_hide ( vw->open_dia );
1412 }
1413
1414 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1415 {
1416   gboolean rv = FALSE;
1417   const gchar *fn;
1418   if ( ! vw->save_dia )
1419   {
1420     vw->save_dia = gtk_file_chooser_dialog_new (_("Save as Viking File."),
1421                                       GTK_WINDOW(vw),
1422                                       GTK_FILE_CHOOSER_ACTION_SAVE,
1423                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1424                                       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1425                                       NULL);
1426     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1427     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1428   }
1429
1430   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
1431   {
1432     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
1433     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 ) ) )
1434     {
1435       window_set_filename ( vw, fn );
1436       rv = window_save ( vw );
1437       vw->modified = FALSE;
1438       break;
1439     }
1440   }
1441   gtk_widget_hide ( vw->save_dia );
1442   return rv;
1443 }
1444
1445 static gboolean window_save ( VikWindow *vw )
1446 {
1447   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1448     return TRUE;
1449   else
1450   {
1451     a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1452     return FALSE;
1453   }
1454 }
1455
1456 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1457 {
1458   if ( ! vw->filename )
1459     return save_file_as ( NULL, vw );
1460   else
1461   {
1462     vw->modified = FALSE;
1463     return window_save ( vw );
1464   }
1465 }
1466
1467 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1468 {
1469   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1470 }
1471
1472 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1473 {
1474   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1475 }
1476
1477 #ifdef VIK_CONFIG_GEOCACHES
1478 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1479 {
1480   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1481 }
1482 #endif
1483
1484 static void goto_address( GtkAction *a, VikWindow *vw)
1485 {
1486   a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1487 }
1488
1489 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1490 {
1491   a_preferences_show_window ( GTK_WINDOW(vw) );
1492 }
1493
1494 static void clear_cb ( GtkAction *a, VikWindow *vw )
1495 {
1496   vik_layers_panel_clear ( vw->viking_vlp );
1497   window_set_filename ( vw, NULL );
1498   draw_update ( vw );
1499 }
1500
1501 static void window_close ( GtkAction *a, VikWindow *vw )
1502 {
1503   if ( ! delete_event ( vw ) )
1504     gtk_widget_destroy ( GTK_WIDGET(vw) );
1505 }
1506
1507 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1508 {
1509   if (save_file( NULL, vw)) {
1510     window_close( NULL, vw);
1511     return(TRUE);
1512   }
1513   else
1514     return(FALSE);
1515 }
1516
1517 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1518 {
1519   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1520   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1521   {
1522     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1523     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1524     draw_update ( vw );
1525   }
1526 }
1527
1528 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1529 {
1530   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1531   GdkPixbuf *pixbuf_to_save;
1532   gdouble old_xmpp, old_ympp;
1533   GError *error = NULL;
1534
1535   /* backup old zoom & set new */
1536   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1537   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1538   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1539
1540   /* reset width and height: */
1541   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1542
1543   /* draw all layers */
1544   draw_redraw ( vw );
1545
1546   /* save buffer as file. */
1547   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);
1548   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1549   if (error)
1550   {
1551     g_warning("Unable to write to file %s: %s", fn, error->message );
1552     g_error_free (error);
1553   }
1554   g_object_unref ( G_OBJECT(pixbuf_to_save) );
1555
1556   /* pretend like nothing happened ;) */
1557   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1558   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1559   vik_viewport_configure ( vw->viking_vvp );
1560   draw_update ( vw );
1561 }
1562
1563 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 )
1564 {
1565   gulong size = sizeof(gchar) * (strlen(fn) + 15);
1566   gchar *name_of_file = g_malloc ( size );
1567   guint x = 1, y = 1;
1568   struct UTM utm_orig, utm;
1569
1570   /* *** copied from above *** */
1571   GdkPixbuf *pixbuf_to_save;
1572   gdouble old_xmpp, old_ympp;
1573   GError *error = NULL;
1574
1575   /* backup old zoom & set new */
1576   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1577   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1578   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1579
1580   /* reset width and height: do this only once for all images (same size) */
1581   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1582   /* *** end copy from above *** */
1583
1584   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1585
1586   g_mkdir(fn,0777);
1587
1588   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1589
1590   for ( y = 1; y <= tiles_h; y++ )
1591   {
1592     for ( x = 1; x <= tiles_w; x++ )
1593     {
1594       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1595       utm = utm_orig;
1596       if ( tiles_w & 0x1 )
1597         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1598       else
1599         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1600       if ( tiles_h & 0x1 ) /* odd */
1601         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1602       else /* even */
1603         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1604
1605       /* move to correct place. */
1606       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1607
1608       draw_redraw ( vw );
1609
1610       /* save buffer as file. */
1611       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);
1612       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1613       if (error)
1614       {
1615         g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1616         g_error_free (error);
1617       }
1618
1619       g_object_unref ( G_OBJECT(pixbuf_to_save) );
1620     }
1621   }
1622
1623   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1624   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1625   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1626   vik_viewport_configure ( vw->viking_vvp );
1627   draw_update ( vw );
1628
1629   g_free ( name_of_file );
1630 }
1631
1632 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1633 {
1634   VikWindow *vw = VIK_WINDOW(pass_along[0]);
1635   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1636   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1637   gdouble width_min, width_max, height_min, height_max;
1638   gint width, height;
1639
1640   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1641   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1642
1643   /* TODO: support for xzoom and yzoom values */
1644   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1645   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1646
1647   if ( width > width_max || width < width_min || height > height_max || height < height_min )
1648     a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1649
1650   gtk_spin_button_set_value ( width_spin, width );
1651   gtk_spin_button_set_value ( height_spin, height );
1652 }
1653
1654 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1655 {
1656   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1657   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1658   gchar *label_text;
1659   gdouble w, h;
1660   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1661   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1662   if (pass_along[4]) /* save many images; find TOTAL area covered */
1663   {
1664     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1665     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1666   }
1667   label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1668   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1669   g_free ( label_text );
1670 }
1671
1672 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1673 {
1674   /* todo: default for answers inside VikWindow or static (thruout instance) */
1675   GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1676                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1677                                                   GTK_STOCK_CANCEL,
1678                                                   GTK_RESPONSE_REJECT,
1679                                                   GTK_STOCK_OK,
1680                                                   GTK_RESPONSE_ACCEPT,
1681                                                   NULL );
1682   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1683   GtkWidget *png_radio, *jpeg_radio;
1684   GtkWidget *current_window_button;
1685   gpointer current_window_pass_along[7];
1686   GtkWidget *zoom_label, *zoom_spin;
1687   GtkWidget *total_size_label;
1688
1689   /* only used if (!one_image_only) */
1690   GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1691
1692
1693   width_label = gtk_label_new ( _("Width (pixels):") );
1694   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1695   height_label = gtk_label_new ( _("Height (pixels):") );
1696   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1697
1698   zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1699   /* TODO: separate xzoom and yzoom factors */
1700   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);
1701
1702   total_size_label = gtk_label_new ( NULL );
1703
1704   current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1705   current_window_pass_along [0] = vw;
1706   current_window_pass_along [1] = width_spin;
1707   current_window_pass_along [2] = height_spin;
1708   current_window_pass_along [3] = zoom_spin;
1709   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1710   current_window_pass_along [5] = NULL;
1711   current_window_pass_along [6] = total_size_label;
1712   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1713
1714   png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1715   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1716
1717   if ( ! vw->draw_image_save_as_png )
1718     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1719
1720   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1721   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1722   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1723   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1724   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1725   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1726   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1727   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1728   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1729
1730   if ( ! one_image_only )
1731   {
1732     GtkWidget *tiles_width_label, *tiles_height_label;
1733
1734
1735     tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1736     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1737     tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1738     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1739     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1740     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1741     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1742     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1743
1744     current_window_pass_along [4] = tiles_width_spin;
1745     current_window_pass_along [5] = tiles_height_spin;
1746     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1747     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1748   }
1749   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1750   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1751   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1752   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1753
1754   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1755
1756   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1757
1758   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1759   {
1760     gtk_widget_hide ( GTK_WIDGET(dialog) );
1761     if ( one_image_only )
1762       save_image_file ( vw, fn, 
1763                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1764                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1765                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1766                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1767     else {
1768       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1769         save_image_dir ( vw, fn, 
1770                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1771                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1772                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1773                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1774                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1775                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1776       else
1777         a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1778     }
1779   }
1780   gtk_widget_destroy ( GTK_WIDGET(dialog) );
1781 }
1782
1783
1784 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1785 {
1786   const gchar *fn;
1787   if (!vw->save_img_dia) {
1788     vw->save_img_dia = gtk_file_chooser_dialog_new (_("Save Image"),
1789                                       GTK_WINDOW(vw),
1790                                       GTK_FILE_CHOOSER_ACTION_SAVE,
1791                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1792                                       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1793                                       NULL);
1794     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1795     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1796   }
1797
1798   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
1799   {
1800     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
1801     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 ) ) )
1802     {
1803       draw_to_image_file ( vw, fn, TRUE );
1804       break;
1805     }
1806   }
1807   gtk_widget_hide ( vw->save_img_dia );
1808 }
1809
1810 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1811 {
1812   gchar *fn = NULL;
1813   
1814   if (!vw->save_img_dir_dia) {
1815     vw->save_img_dir_dia = gtk_file_chooser_dialog_new (_("Choose a directory to hold images"),
1816                                       GTK_WINDOW(vw),
1817                                       GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1818                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1819                                       GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1820                                       NULL);
1821     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1822     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1823   }
1824   
1825   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_ACCEPT )
1826   {
1827     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dir_dia) );
1828     if ( fn )
1829     {
1830       draw_to_image_file ( vw, fn, FALSE );
1831       g_free(fn);
1832       fn = NULL;
1833       break;
1834     }
1835   }
1836   gtk_widget_hide ( vw->save_img_dir_dia );
1837 }
1838
1839 #if GTK_CHECK_VERSION(2,10,0)
1840 static void print_cb ( GtkAction *a, VikWindow *vw )
1841 {
1842   a_print(vw, vw->viking_vvp);
1843 }
1844 #endif
1845
1846 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1847 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1848 {
1849   VikViewportDrawMode drawmode;
1850   if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1851     drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1852   }
1853   else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1854     drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1855   }
1856   else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1857     drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1858   }
1859   else {
1860     g_critical("Houston, we've had a problem.");
1861     return;
1862   }
1863
1864   if ( !vw->only_updating_coord_mode_ui )
1865   {
1866     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1867     if ( olddrawmode != drawmode )
1868     {
1869       /* this takes care of coord mode too */
1870       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1871       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1872         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1873       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1874         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1875       }
1876       draw_update ( vw );
1877     }
1878   }
1879 }
1880
1881 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1882 {
1883   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1884   g_assert(check_box);
1885   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1886   vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1887   draw_update ( vw );
1888 }
1889
1890 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1891 {
1892   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1893   g_assert(check_box);
1894   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1895   vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1896   draw_update ( vw );
1897 }
1898
1899 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1900 {
1901   GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1902   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1903   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1904   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1905   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1906   {
1907     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1908     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1909     draw_update ( vw );
1910   }
1911   g_free ( color );
1912   gtk_widget_destroy ( colorsd );
1913 }
1914
1915
1916
1917 /***********************************************************************************************
1918  ** GUI Creation
1919  ***********************************************************************************************/
1920
1921 static GtkActionEntry entries[] = {
1922   { "File", NULL, N_("_File"), 0, 0, 0 },
1923   { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
1924   { "View", NULL, N_("_View"), 0, 0, 0 },
1925   { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
1926   { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
1927   { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
1928   { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
1929   { "Help", NULL, N_("_Help"), 0, 0, 0 },
1930
1931   { "New",       GTK_STOCK_NEW,          N_("_New"),                          "<control>N", N_("New file"),                                     (GCallback)newwindow_cb          },
1932   { "Open",      GTK_STOCK_OPEN,         N_("_Open"),                         "<control>O", N_("Open a file"),                                  (GCallback)load_file             },
1933   { "Append",    GTK_STOCK_ADD,          N_("A_ppend File"),                  NULL,         N_("Append data from a different file"),            (GCallback)load_file             },
1934   { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
1935   { "AcquireGPS",   NULL,                N_("From _GPS"),                 NULL,         N_("Transfer data from a GPS device"),              (GCallback)acquire_from_gps      },
1936   { "AcquireGoogle",   NULL,             N_("Google _Directions"),        NULL,         N_("Get driving directions from Google"),           (GCallback)acquire_from_google   },
1937 #ifdef VIK_CONFIG_GEOCACHES
1938   { "AcquireGC",   NULL,                 N_("Geo_caches"),                NULL,         N_("Get Geocaches from geocaching.com"),            (GCallback)acquire_from_gc       },
1939 #endif
1940   { "Save",      GTK_STOCK_SAVE,         N_("_Save"),                         "<control>S", N_("Save the file"),                                (GCallback)save_file             },
1941   { "SaveAs",    GTK_STOCK_SAVE_AS,      N_("Save _As"),                      NULL,         N_("Save the file under different name"),           (GCallback)save_file_as          },
1942   { "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 },
1943   { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images"), NULL,         N_("FIXME:IMGDIR"),                                 (GCallback)draw_to_image_dir_cb  },
1944
1945 #if GTK_CHECK_VERSION(2,10,0)
1946   { "Print",    GTK_STOCK_PRINT,        N_("_Print..."),          NULL,         N_("Print maps"), (GCallback)print_cb },
1947 #endif
1948
1949   { "Exit",      GTK_STOCK_QUIT,         N_("E_xit"),                         "<control>W", N_("Exit the program"),                             (GCallback)window_close          },
1950   { "SaveExit",  GTK_STOCK_QUIT,         N_("Save and Exit"),                 NULL, N_("Save and Exit the program"),                             (GCallback)save_file_and_exit          },
1951
1952   { "GoogleMapsSearch",   GTK_STOCK_JUMP_TO,                 N_("Go To Google Maps location"),                    NULL,         N_("Go to address/place using Google Maps search"),            (GCallback)goto_address       },
1953   { "GotoLL",    GTK_STOCK_QUIT,         N_("_Go to Lat\\/Lon..."),           NULL,         N_("Go to arbitrary lat\\/lon coordinate"),         (GCallback)draw_goto_cb          },
1954   { "GotoUTM",   GTK_STOCK_QUIT,         N_("Go to UTM..."),                  NULL,         N_("Go to arbitrary UTM coordinate"),               (GCallback)draw_goto_cb          },
1955   { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Background Color..."),       NULL,         NULL,                                           (GCallback)set_bg_color          },
1956   { "ZoomIn",    GTK_STOCK_ZOOM_IN,      N_("Zoom _In"),                   "<control>plus", NULL,                                           (GCallback)draw_zoom_cb          },
1957   { "ZoomOut",   GTK_STOCK_ZOOM_OUT,     N_("Zoom _Out"),                 "<control>minus", NULL,                                           (GCallback)draw_zoom_cb          },
1958   { "ZoomTo",    GTK_STOCK_ZOOM_FIT,     N_("Zoom _To"),               "<control><shift>Z", NULL,                                           (GCallback)zoom_to_cb            },
1959   { "Zoom0.25",  NULL,                   N_("0.25"),                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1960   { "Zoom0.5",   NULL,                   N_("0.5"),                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1961   { "Zoom1",     NULL,                   N_("1"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1962   { "Zoom2",     NULL,                   N_("2"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1963   { "Zoom4",     NULL,                   N_("4"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1964   { "Zoom8",     NULL,                   N_("8"),                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1965   { "Zoom16",    NULL,                   N_("16"),                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1966   { "Zoom32",    NULL,                   N_("32"),                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1967   { "Zoom64",    NULL,                   N_("64"),                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1968   { "Zoom128",   NULL,                   N_("128"),                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1969   { "PanNorth",  NULL,                   N_("Pan North"),                  "<control>Up", NULL,                                           (GCallback)draw_pan_cb },
1970   { "PanEast",  NULL,                    N_("Pan East"),                   "<control>Right", NULL,                                           (GCallback)draw_pan_cb },
1971   { "PanSouth",  NULL,                   N_("Pan South"),                  "<control>Down", NULL,                                           (GCallback)draw_pan_cb },
1972   { "PanWest",  NULL,                    N_("Pan West"),                   "<control>Left", NULL,                                           (GCallback)draw_pan_cb },
1973   { "BGJobs",    GTK_STOCK_EXECUTE,      N_("Background _Jobs"),              NULL,         NULL,                                           (GCallback)a_background_show_window },
1974
1975   { "Cut",       GTK_STOCK_CUT,          N_("Cu_t"),                          NULL,         NULL,                                           (GCallback)menu_cut_layer_cb     },
1976   { "Copy",      GTK_STOCK_COPY,         N_("_Copy"),                         NULL,         NULL,                                           (GCallback)menu_copy_layer_cb    },
1977   { "Paste",     GTK_STOCK_PASTE,        N_("_Paste"),                        NULL,         NULL,                                           (GCallback)menu_paste_layer_cb   },
1978   { "Delete",    GTK_STOCK_DELETE,       N_("_Delete"),                       NULL,         NULL,                                           (GCallback)menu_delete_layer_cb  },
1979   { "DeleteAll", NULL,                   N_("Delete All"),                    NULL,         NULL,                                           (GCallback)clear_cb              },
1980   { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences..."),                    NULL,         NULL,                                           (GCallback)preferences_cb              },
1981   { "Properties",GTK_STOCK_PROPERTIES,   N_("_Properties"),                   NULL,         NULL,                                           (GCallback)menu_properties_cb    },
1982
1983   { "About",     GTK_STOCK_ABOUT,        N_("_About"),                        NULL,         NULL,                                           (GCallback)help_about_cb    },
1984 };
1985
1986 /* Radio items */
1987 static GtkRadioActionEntry mode_entries[] = {
1988   { "ModeUTM",         NULL,         N_("_UTM Mode"),               "<control>u", NULL, 0 },
1989   { "ModeExpedia",     NULL,         N_("_Expedia Mode"),           "<control>e", NULL, 1 },
1990   { "ModeMercator",    NULL,         N_("_Mercator Mode"),            "<control>g", NULL, 4 }
1991 };
1992
1993 static GtkRadioActionEntry tool_entries[] = {
1994   { "Pan",      "vik-icon-pan",        N_("_Pan"),                         "<control><shift>P", N_("Pan Tool"),  0 },
1995   { "Zoom",      "vik-icon-zoom",        N_("_Zoom"),                         "<control><shift>Z", N_("Zoom Tool"),  1 },
1996   { "Ruler",     "vik-icon-ruler",       N_("_Ruler"),                        "<control><shift>R", N_("Ruler Tool"), 2 }
1997 };
1998
1999 static GtkToggleActionEntry toggle_entries[] = {
2000   { "ShowScale", NULL,                   N_("Show Scale"),                    NULL,         N_("Show Scale"),                                           (GCallback)set_draw_scale, TRUE   },
2001   { "ShowCenterMark", NULL,                   N_("Show Center Mark"),                    NULL,         N_("Show Center Mark"),                                           (GCallback)set_draw_centermark, TRUE   },
2002   { "FullScreen",    GTK_STOCK_FULLSCREEN,      N_("Full Screen"),                   "F11", N_("Activate full screen mode"),                                           (GCallback)full_screen_cb, FALSE },
2003   { "ViewSidePanel", GTK_STOCK_INDEX,   N_("Show Side Panel"),                   "F9",         N_("Show Side Panel"),                                           (GCallback)view_side_panel_cb, TRUE    },
2004 };
2005
2006 #include "menu.xml.h"
2007 static void window_create_ui( VikWindow *window )
2008 {
2009   GtkUIManager *uim;
2010   GtkActionGroup *action_group;
2011   GtkAccelGroup *accel_group;
2012   GError *error;
2013   guint i, j, mid;
2014   GtkIconFactory *icon_factory;
2015   GtkIconSet *icon_set; 
2016   GtkRadioActionEntry *tools = NULL, *radio;
2017   guint ntools;
2018   
2019   uim = gtk_ui_manager_new ();
2020   window->uim = uim;
2021
2022   toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
2023   toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
2024   toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
2025
2026   error = NULL;
2027   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
2028     g_error_free (error);
2029     exit (1);
2030   }
2031
2032   action_group = gtk_action_group_new ("MenuActions");
2033   gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
2034   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
2035   gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
2036   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
2037
2038   icon_factory = gtk_icon_factory_new ();
2039   gtk_icon_factory_add_default (icon_factory); 
2040
2041   register_vik_icons(icon_factory);
2042
2043   ntools = 0;
2044   for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2045       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2046       radio = &tools[ntools];
2047       ntools++;
2048       *radio = tool_entries[i];
2049       radio->value = ntools;
2050   }  
2051
2052   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2053     GtkActionEntry action;
2054     gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Layers/", 
2055                           vik_layer_get_interface(i)->name,
2056                           vik_layer_get_interface(i)->name,
2057                           GTK_UI_MANAGER_MENUITEM, FALSE);
2058
2059     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2060     gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2061     gtk_icon_set_unref (icon_set);
2062
2063     action.name = vik_layer_get_interface(i)->name;
2064     action.stock_id = vik_layer_get_interface(i)->name;
2065     action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
2066     action.accelerator = NULL;
2067     action.tooltip = NULL;
2068     action.callback = (GCallback)menu_addlayer_cb;
2069     gtk_action_group_add_actions(action_group, &action, 1, window);
2070
2071     if ( vik_layer_get_interface(i)->tools_count ) {
2072       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2073       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2074     }
2075
2076     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2077       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2078       radio = &tools[ntools];
2079       ntools++;
2080       
2081       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
2082                             _(vik_layer_get_interface(i)->tools[j].name),
2083                             vik_layer_get_interface(i)->tools[j].name,
2084                             GTK_UI_MANAGER_MENUITEM, FALSE);
2085       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems", 
2086                             _(vik_layer_get_interface(i)->tools[j].name),
2087                             vik_layer_get_interface(i)->tools[j].name,
2088                             GTK_UI_MANAGER_TOOLITEM, FALSE);
2089
2090       toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
2091
2092       radio->name = vik_layer_get_interface(i)->tools[j].name;
2093       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
2094       radio->label = _(vik_layer_get_interface(i)->tools[j].name);
2095       radio->accelerator = NULL;
2096       radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
2097       radio->value = ntools;
2098     }
2099   }
2100   g_object_unref (icon_factory);
2101
2102   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2103   g_free(tools);
2104
2105   gtk_ui_manager_insert_action_group (uim, action_group, 0);
2106
2107   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2108     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2109       GtkAction *action = gtk_action_group_get_action(action_group,
2110                             vik_layer_get_interface(i)->tools[j].name);
2111       g_object_set(action, "sensitive", FALSE, NULL);
2112     }
2113   }
2114   window->action_group = action_group;
2115
2116   accel_group = gtk_ui_manager_get_accel_group (uim);
2117   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2118   gtk_ui_manager_ensure_update (uim);
2119 }
2120
2121
2122
2123 static struct { 
2124   const GdkPixdata *data;
2125   gchar *stock_id;
2126 } stock_icons[] = {
2127   { &begintr_18_pixbuf,         "Begin Track"      },
2128   { &iscissors_18_pixbuf,       "Magic Scissors"   },
2129   { &mover_22_pixbuf,           "vik-icon-pan"     },
2130   { &demdl_18_pixbuf,           "DEM Download/Import"     },
2131   { &showpic_18_pixbuf,         "Show Picture"      },
2132   { &addtr_18_pixbuf,           "Create Track"      },
2133   { &edtr_18_pixbuf,            "Edit Trackpoint"   },
2134   { &addwp_18_pixbuf,           "Create Waypoint"   },
2135   { &edwp_18_pixbuf,            "Edit Waypoint"     },
2136   { &zoom_18_pixbuf,            "vik-icon-zoom"     },
2137   { &ruler_18_pixbuf,           "vik-icon-ruler"    },
2138   { &geozoom_18_pixbuf,         "Georef Zoom Tool"  },
2139   { &geomove_18_pixbuf,         "Georef Move Map"   },
2140   { &mapdl_18_pixbuf,           "Maps Download"     },
2141   { &showpic_18_pixbuf,         "Show Picture"      },
2142 };
2143  
2144 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2145
2146 static void
2147 register_vik_icons (GtkIconFactory *icon_factory)
2148 {
2149   GtkIconSet *icon_set; 
2150   gint i;
2151
2152   for (i = 0; i < n_stock_icons; i++) {
2153     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2154                    stock_icons[i].data, FALSE, NULL ));
2155     gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2156     gtk_icon_set_unref (icon_set);
2157   }
2158 }
2159