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