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