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