]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
Marking translatable strings.
[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
34 #include <stdlib.h>
35 #include <math.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <glib/gprintf.h>
39 #include <glib/gi18n.h>
40 #ifdef WINDOWS
41 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
42 #define make_dir(dir) mkdir(dir)
43 #else
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #define make_dir(dir) mkdir(dir,0777)
47 #endif
48
49 #define VIKING_WINDOW_WIDTH      1000
50 #define VIKING_WINDOW_HEIGHT     800
51 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
52 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
53 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
54
55 static void window_finalize ( GObject *gob );
56 static GObjectClass *parent_class;
57
58 static void window_init ( VikWindow *vw );
59 static void window_class_init ( VikWindowClass *klass );
60 static void window_set_filename ( VikWindow *vw, const gchar *filename );
61
62 static void draw_update ( VikWindow *vw );
63
64 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
65
66 /* Drawing & stuff */
67
68 static gboolean delete_event( VikWindow *vw );
69
70 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
71
72 static void window_configure_event ( VikWindow *vw );
73 static void draw_sync ( VikWindow *vw );
74 static void draw_redraw ( VikWindow *vw );
75 static void draw_scroll  ( VikWindow *vw, GdkEventScroll *event );
76 static void draw_click  ( VikWindow *vw, GdkEventButton *event );
77 static void draw_release ( VikWindow *vw, GdkEventButton *event );
78 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
79 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
80 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
81
82 static void draw_status ();
83
84 /* End Drawing Functions */
85
86 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
87 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
88 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
89
90 /* tool management */
91 typedef struct {
92   VikToolInterface ti;
93   gpointer state;
94   gint layer_type;
95 } toolbox_tool_t;
96 #define TOOL_LAYER_TYPE_NONE -1
97
98 typedef struct {
99   int                   active_tool;
100   int                   n_tools;
101   toolbox_tool_t        *tools;
102   VikWindow *vw;
103 } toolbox_tools_t;
104
105 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
106 static toolbox_tools_t* toolbox_create(VikWindow *vw);
107 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
108 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
109 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
110 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
111 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
112 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
113
114
115 /* ui creation */
116 static void window_create_ui( VikWindow *window );
117 static void register_vik_icons (GtkIconFactory *icon_factory);
118
119 /* i/o */
120 static void load_file ( GtkAction *a, VikWindow *vw );
121 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
122 static gboolean save_file ( GtkAction *a, VikWindow *vw );
123 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
124 static gboolean window_save ( VikWindow *vw );
125
126 struct _VikWindow {
127   GtkWindow gtkwindow;
128   VikViewport *viking_vvp;
129   VikLayersPanel *viking_vlp;
130   VikStatusbar *viking_vs;
131
132   GtkToolbar *toolbar;
133
134   GtkItemFactory *item_factory;
135
136   /* tool management state */
137   guint current_tool;
138   toolbox_tools_t *vt;
139   guint16 tool_layer_id;
140   guint16 tool_tool_id;
141
142   GtkActionGroup *action_group;
143
144   gint pan_x, pan_y;
145
146   guint draw_image_width, draw_image_height;
147   gboolean draw_image_save_as_png;
148
149   gchar *filename;
150   gboolean modified;
151
152   GtkWidget *open_dia, *save_dia;
153   GtkWidget *save_img_dia, *save_img_dir_dia;
154
155   gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
156   GtkUIManager *uim;
157
158   /* half-drawn update */
159   VikLayer *trigger;
160   VikCoord trigger_center;
161 };
162
163 enum {
164  TOOL_ZOOM = 0,
165  TOOL_RULER,
166  TOOL_LAYER,
167  NUMBER_OF_TOOLS
168 };
169
170 enum {
171   VW_NEWWINDOW_SIGNAL,
172   VW_OPENWINDOW_SIGNAL,
173   VW_LAST_SIGNAL
174 };
175
176 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
177
178 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Zoom"), N_("Ruler"), N_("Pan") };
179
180 GdkCursor *vw_cursor_zoom = NULL;
181 GdkCursor *vw_cursor_ruler = NULL;
182
183 GType vik_window_get_type (void)
184 {
185   static GType vw_type = 0;
186
187   if (!vw_type)
188   {
189     static const GTypeInfo vw_info = 
190     {
191       sizeof (VikWindowClass),
192       NULL, /* base_init */
193       NULL, /* base_finalize */
194       (GClassInitFunc) window_class_init, /* class_init */
195       NULL, /* class_finalize */
196       NULL, /* class_data */
197       sizeof (VikWindow),
198       0,
199       (GInstanceInitFunc) window_init,
200     };
201     vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
202   }
203
204   return vw_type;
205 }
206
207 VikViewport * vik_window_viewport(VikWindow *vw)
208 {
209   return(vw->viking_vvp);
210 }
211
212 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
213 {
214   int i, j, tool_count;
215   VikLayerInterface *layer_interface;
216
217   if (!vw->action_group) return;
218
219   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
220     GtkAction *action;
221     layer_interface = vik_layer_get_interface(i);
222     tool_count = layer_interface->tools_count;
223
224     for (j = 0; j < tool_count; j++) {
225       action = gtk_action_group_get_action(vw->action_group,
226             layer_interface->tools[j].name);
227       g_object_set(action, "sensitive", i == vl->type, NULL);
228     }
229   }
230 }
231
232 static void window_finalize ( GObject *gob )
233 {
234   VikWindow *vw = VIK_WINDOW(gob);
235   g_return_if_fail ( vw != NULL );
236
237   a_background_remove_status ( vw->viking_vs );
238
239   G_OBJECT_CLASS(parent_class)->finalize(gob);
240 }
241
242
243 static void window_class_init ( VikWindowClass *klass )
244 {
245   /* destructor */
246   GObjectClass *object_class;
247
248   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);
249   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);
250
251   object_class = G_OBJECT_CLASS (klass);
252
253   object_class->finalize = window_finalize;
254
255   parent_class = g_type_class_peek_parent (klass);
256
257 }
258
259 static void window_init ( VikWindow *vw )
260 {
261   GtkWidget *main_vbox;
262   GtkWidget *hpaned;
263
264   vw->action_group = NULL;
265
266   vw->viking_vvp = vik_viewport_new();
267   vw->viking_vlp = vik_layers_panel_new();
268   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
269   vw->viking_vs = vik_statusbar_new();
270
271   vw->vt = toolbox_create(vw);
272   window_create_ui(vw);
273   window_set_filename (vw, NULL);
274   
275   toolbox_activate(vw->vt, "Zoom");
276
277   vw->filename = NULL;
278   vw->item_factory = NULL;
279
280   vw->modified = FALSE;
281   vw->only_updating_coord_mode_ui = FALSE;
282   
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 ruler 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
915 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
916 {
917   guint what = 128;
918
919   if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
920     what = -3;
921   } 
922   else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
923     what = -4;
924   }
925   else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
926     what = -2;
927   }
928   else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
929     what = -1;
930   }
931   else {
932     gchar *s = (gchar *)gtk_action_get_name(a);
933     what = atoi(s+4);
934   }
935
936   switch (what)
937   {
938     case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
939     case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
940     case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
941     case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
942     default: vik_viewport_set_zoom ( vw->viking_vvp, what );
943   }
944   draw_update ( vw );
945 }
946
947 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
948 {
949   VikCoord new_center;
950
951   if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
952     struct LatLon ll, llold;
953     vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
954     if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
955       vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
956     else
957       return;
958   }
959   else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
960     struct UTM utm, utmold;
961     vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
962     if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
963       vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
964     else
965      return;
966   }
967   else {
968     g_critical("Houston we have a problem\n");
969     return;
970   }
971
972   vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
973   draw_update ( vw );
974 }
975
976 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
977 {
978   gint type;
979   for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
980     if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
981       if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
982         draw_update ( vw );
983         vw->modified = TRUE;
984       }
985     }
986   }
987 }
988
989 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
990 {
991   a_clipboard_copy_selected ( vw->viking_vlp );
992 }
993
994 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
995 {
996   a_clipboard_copy_selected ( vw->viking_vlp );
997   menu_delete_layer_cb ( a, vw );
998 }
999
1000 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1001 {
1002   if ( a_clipboard_paste ( vw->viking_vlp ) )
1003   {
1004     draw_update ( vw );
1005     vw->modified = TRUE;
1006   }
1007 }
1008
1009 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1010 {
1011   if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1012     a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1013 }
1014
1015 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1016 {
1017   a_dialog_about(GTK_WINDOW(vw));
1018 }
1019
1020 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1021 {
1022   if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1023   {
1024     vik_layers_panel_delete_selected ( vw->viking_vlp );
1025     vw->modified = TRUE;
1026   }
1027   else
1028     a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1029 }
1030
1031 /***************************************
1032  ** tool management routines
1033  **
1034  ***************************************/
1035
1036 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1037 {
1038   toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1039   vt->tools = NULL;
1040   vt->n_tools = 0;
1041   vt->active_tool = -1;
1042   vt->vw = vw;
1043   if (!vw->viking_vvp) {
1044     g_critical("no viewport found.");
1045     exit(1);
1046   }
1047   return vt;
1048 }
1049
1050 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1051 {
1052   vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1053   vt->tools[vt->n_tools].ti = *vti;
1054   vt->tools[vt->n_tools].layer_type = layer_type;
1055   if (vti->create) {
1056     vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1057   } 
1058   else {
1059     vt->tools[vt->n_tools].state = NULL;
1060   }
1061   vt->n_tools++;
1062 }
1063
1064 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1065 {
1066   int i;
1067   for (i=0; i<vt->n_tools; i++) {
1068     if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1069       break;
1070     }
1071   }
1072   return i;
1073 }
1074
1075 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1076 {
1077   int tool = toolbox_get_tool(vt, tool_name);
1078   toolbox_tool_t *t = &vt->tools[tool];
1079   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1080
1081   if (tool == vt->n_tools) {
1082     g_critical("trying to activate a non-existent tool...");
1083     exit(1);
1084   }
1085   /* is the tool already active? */
1086   if (vt->active_tool == tool) {
1087     return;
1088   }
1089
1090   if (vt->active_tool != -1) {
1091     if (vt->tools[vt->active_tool].ti.deactivate) {
1092       vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1093     }
1094   }
1095   if (t->ti.activate) {
1096     t->ti.activate(vl, t->state);
1097   }
1098   vt->active_tool = tool;
1099 }
1100
1101 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1102 {
1103   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1104   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1105     gint ltype = vt->tools[vt->active_tool].layer_type;
1106     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1107       vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1108   }
1109 }
1110
1111 static void toolbox_move (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.move) {
1115     gint ltype = vt->tools[vt->active_tool].layer_type;
1116     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1117       if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1118         gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1119   }
1120 }
1121
1122 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1123 {
1124   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1125   if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1126     gint ltype = vt->tools[vt->active_tool].layer_type;
1127     if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1128       vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1129   }
1130 }
1131 /** End tool management ************************************/
1132
1133 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1134 {
1135   gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1136 }
1137
1138 /* this function gets called whenever a toolbar tool is clicked */
1139 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1140 {
1141   /* White Magic, my friends ... White Magic... */
1142   int layer_id, tool_id;
1143
1144   toolbox_activate(vw->vt, gtk_action_get_name(a));
1145
1146   if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1147     vw->current_tool = TOOL_ZOOM;
1148     gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
1149   } 
1150   else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1151     vw->current_tool = TOOL_RULER;
1152     gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
1153   }
1154   else {
1155     /* TODO: only enable tools from active layer */
1156     for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1157       for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1158         if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1159            vw->current_tool = TOOL_LAYER;
1160            vw->tool_layer_id = layer_id;
1161            vw->tool_tool_id = tool_id;
1162            gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
1163         }
1164       }
1165     }
1166   }
1167   draw_status ( vw );
1168 }
1169
1170 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1171 {
1172   gchar *title;
1173   const gchar *file;
1174   if ( vw->filename )
1175     g_free ( vw->filename );
1176   if ( filename == NULL )
1177   {
1178     vw->filename = NULL;
1179     file = _("Untitled");
1180   }
1181   else
1182   {
1183     vw->filename = g_strdup(filename);
1184     file = a_file_basename ( filename );
1185   }
1186   title = g_strdup_printf( "%s - Viking", file );
1187   gtk_window_set_title ( GTK_WINDOW(vw), title );
1188   g_free ( title );
1189 }
1190
1191 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1192 {
1193   GtkWidget *mode_button;
1194   gchar *buttonname;
1195   switch ( mode ) {
1196     case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1197     case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1198     case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1199     case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1200     default: buttonname = "/ui/MainMenu/View/ModeKH";
1201   }
1202   mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1203   g_assert ( mode_button );
1204   return mode_button;
1205 }
1206
1207 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1208 {
1209   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1210   {
1211     case 0:
1212       a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1213       break;
1214     case 1:
1215     {
1216       GtkWidget *mode_button;
1217       if ( change_filename )
1218         window_set_filename ( vw, filename );
1219       mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1220       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. */
1221       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1222       vw->only_updating_coord_mode_ui = FALSE;
1223
1224       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1225
1226       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1227       g_assert ( mode_button );
1228       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1229
1230       mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1231       g_assert ( mode_button );
1232       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1233     }
1234     default: draw_update ( vw );
1235   }
1236 }
1237 static void load_file ( GtkAction *a, VikWindow *vw )
1238 {
1239   gboolean newwindow;
1240   if (!strcmp(gtk_action_get_name(a), "Open")) {
1241     newwindow = TRUE;
1242   } 
1243   else if (!strcmp(gtk_action_get_name(a), "Append")) {
1244     newwindow = FALSE;
1245   } 
1246   else {
1247     g_critical("Houston we got a problem\n");
1248     return;
1249   }
1250     
1251   if ( ! vw->open_dia )
1252   {
1253     vw->open_dia = gtk_file_selection_new ( _("Please select a GPS data file to open. ") );
1254     gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1255     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1256     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1257   }
1258   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1259   {
1260     gtk_widget_hide ( vw->open_dia );
1261 #ifdef VIKING_PROMPT_IF_MODIFIED
1262     if ( (vw->modified || vw->filename) && newwindow )
1263 #else
1264     if ( vw->filename && newwindow )
1265 #endif
1266       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1267     else {
1268       gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1269       gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1270       while ( *files ) {
1271         vik_window_open_file ( vw, *files, change_fn );
1272         files++;
1273       }
1274     }
1275   }
1276   else
1277     gtk_widget_hide ( vw->open_dia );
1278 }
1279
1280 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1281 {
1282   gboolean rv = FALSE;
1283   const gchar *fn;
1284   if ( ! vw->save_dia )
1285   {
1286     vw->save_dia = gtk_file_selection_new ( _("Save as Viking File.") );
1287     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1288     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1289   }
1290
1291   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1292   {
1293     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1294     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 ) ) )
1295     {
1296       window_set_filename ( vw, fn );
1297       rv = window_save ( vw );
1298       vw->modified = FALSE;
1299       break;
1300     }
1301   }
1302   gtk_widget_hide ( vw->save_dia );
1303   return rv;
1304 }
1305
1306 static gboolean window_save ( VikWindow *vw )
1307 {
1308   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1309     return TRUE;
1310   else
1311   {
1312     a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1313     return FALSE;
1314   }
1315 }
1316
1317 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1318 {
1319   if ( ! vw->filename )
1320     return save_file_as ( NULL, vw );
1321   else
1322   {
1323     vw->modified = FALSE;
1324     return window_save ( vw );
1325   }
1326 }
1327
1328 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1329 {
1330   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1331 }
1332
1333 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1334 {
1335   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1336 }
1337
1338 #ifdef VIK_CONFIG_GEOCACHES
1339 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1340 {
1341   a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1342 }
1343 #endif
1344
1345 static void goto_address( GtkAction *a, VikWindow *vw)
1346 {
1347   a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1348 }
1349
1350 static void clear_cb ( GtkAction *a, VikWindow *vw )
1351 {
1352   vik_layers_panel_clear ( vw->viking_vlp );
1353   window_set_filename ( vw, NULL );
1354   draw_update ( vw );
1355 }
1356
1357 static void window_close ( GtkAction *a, VikWindow *vw )
1358 {
1359   if ( ! delete_event ( vw ) )
1360     gtk_widget_destroy ( GTK_WIDGET(vw) );
1361 }
1362
1363 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1364 {
1365   if (save_file( NULL, vw)) {
1366     window_close( NULL, vw);
1367     return(TRUE);
1368   }
1369   else
1370     return(FALSE);
1371 }
1372
1373 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1374 {
1375   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1376   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1377   {
1378     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1379     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1380     draw_update ( vw );
1381   }
1382 }
1383
1384 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1385 {
1386   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1387   GdkPixbuf *pixbuf_to_save;
1388   gdouble old_xmpp, old_ympp;
1389   GError *error = NULL;
1390
1391   /* backup old zoom & set new */
1392   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1393   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1394   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1395
1396   /* reset width and height: */
1397   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1398
1399   /* draw all layers */
1400   draw_redraw ( vw );
1401
1402   /* save buffer as file. */
1403   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);
1404   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1405   if (error)
1406   {
1407     g_warning("Unable to write to file %s: %s", fn, error->message );
1408     g_error_free (error);
1409   }
1410   g_object_unref ( G_OBJECT(pixbuf_to_save) );
1411
1412   /* pretend like nothing happened ;) */
1413   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1414   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1415   vik_viewport_configure ( vw->viking_vvp );
1416   draw_update ( vw );
1417 }
1418
1419 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 )
1420 {
1421   gulong size = sizeof(gchar) * (strlen(fn) + 15);
1422   gchar *name_of_file = g_malloc ( size );
1423   guint x = 1, y = 1;
1424   struct UTM utm_orig, utm;
1425
1426   /* *** copied from above *** */
1427   GdkPixbuf *pixbuf_to_save;
1428   gdouble old_xmpp, old_ympp;
1429   GError *error = NULL;
1430
1431   /* backup old zoom & set new */
1432   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1433   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1434   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1435
1436   /* reset width and height: do this only once for all images (same size) */
1437   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1438   /* *** end copy from above *** */
1439
1440   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1441
1442   make_dir(fn);
1443
1444   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1445
1446   for ( y = 1; y <= tiles_h; y++ )
1447   {
1448     for ( x = 1; x <= tiles_w; x++ )
1449     {
1450       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1451       utm = utm_orig;
1452       if ( tiles_w & 0x1 )
1453         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1454       else
1455         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1456       if ( tiles_h & 0x1 ) /* odd */
1457         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1458       else /* even */
1459         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1460
1461       /* move to correct place. */
1462       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1463
1464       draw_redraw ( vw );
1465
1466       /* save buffer as file. */
1467       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);
1468       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1469       if (error)
1470       {
1471         g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1472         g_error_free (error);
1473       }
1474
1475       g_object_unref ( G_OBJECT(pixbuf_to_save) );
1476     }
1477   }
1478
1479   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1480   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1481   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1482   vik_viewport_configure ( vw->viking_vvp );
1483   draw_update ( vw );
1484
1485   g_free ( name_of_file );
1486 }
1487
1488 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1489 {
1490   VikWindow *vw = VIK_WINDOW(pass_along[0]);
1491   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1492   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1493   gdouble width_min, width_max, height_min, height_max;
1494   gint width, height;
1495
1496   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1497   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1498
1499   /* TODO: support for xzoom and yzoom values */
1500   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1501   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1502
1503   if ( width > width_max || width < width_min || height > height_max || height < height_min )
1504     a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1505
1506   gtk_spin_button_set_value ( width_spin, width );
1507   gtk_spin_button_set_value ( height_spin, height );
1508 }
1509
1510 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1511 {
1512   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1513   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1514   gchar *label_text;
1515   gdouble w, h;
1516   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1517   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1518   if (pass_along[4]) /* save many images; find TOTAL area covered */
1519   {
1520     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1521     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1522   }
1523   label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1524   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1525   g_free ( label_text );
1526 }
1527
1528 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1529 {
1530   /* todo: default for answers inside VikWindow or static (thruout instance) */
1531   GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1532                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1533                                                   GTK_STOCK_CANCEL,
1534                                                   GTK_RESPONSE_REJECT,
1535                                                   GTK_STOCK_OK,
1536                                                   GTK_RESPONSE_ACCEPT,
1537                                                   NULL );
1538   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1539   GtkWidget *png_radio, *jpeg_radio;
1540   GtkWidget *current_window_button;
1541   gpointer current_window_pass_along[7];
1542   GtkWidget *zoom_label, *zoom_spin;
1543   GtkWidget *total_size_label;
1544
1545   /* only used if (!one_image_only) */
1546   GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1547
1548
1549   width_label = gtk_label_new ( _("Width (pixels):") );
1550   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1551   height_label = gtk_label_new ( _("Height (pixels):") );
1552   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1553
1554   zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1555   /* TODO: separate xzoom and yzoom factors */
1556   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);
1557
1558   total_size_label = gtk_label_new ( NULL );
1559
1560   current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1561   current_window_pass_along [0] = vw;
1562   current_window_pass_along [1] = width_spin;
1563   current_window_pass_along [2] = height_spin;
1564   current_window_pass_along [3] = zoom_spin;
1565   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1566   current_window_pass_along [5] = NULL;
1567   current_window_pass_along [6] = total_size_label;
1568   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1569
1570   png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1571   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1572
1573   if ( ! vw->draw_image_save_as_png )
1574     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1575
1576   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1577   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1578   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1579   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1580   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1581   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1582   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1583   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1584   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1585
1586   if ( ! one_image_only )
1587   {
1588     GtkWidget *tiles_width_label, *tiles_height_label;
1589
1590
1591     tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1592     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1593     tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1594     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1595     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1596     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1597     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1598     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1599
1600     current_window_pass_along [4] = tiles_width_spin;
1601     current_window_pass_along [5] = tiles_height_spin;
1602     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1603     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1604   }
1605   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1606   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1607   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1608   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1609
1610   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1611
1612   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1613
1614   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1615   {
1616     gtk_widget_hide ( GTK_WIDGET(dialog) );
1617     if ( one_image_only )
1618       save_image_file ( vw, fn, 
1619                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1620                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1621                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1622                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1623     else {
1624       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1625         save_image_dir ( vw, fn, 
1626                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1627                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1628                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1629                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1630                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1631                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1632       else
1633         a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1634     }
1635   }
1636   gtk_widget_destroy ( GTK_WIDGET(dialog) );
1637 }
1638
1639
1640 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1641 {
1642   const gchar *fn;
1643   if (!vw->save_img_dia) {
1644     vw->save_img_dia = gtk_file_selection_new ( _("Save Image") );
1645     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1646     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1647   }
1648
1649   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1650   {
1651     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1652     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 ) ) )
1653     {
1654       draw_to_image_file ( vw, fn, TRUE );
1655       break;
1656     }
1657   }
1658   gtk_widget_hide ( vw->save_img_dia );
1659 }
1660
1661 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1662 {
1663   const gchar *fn;
1664   if (!vw->save_img_dir_dia) {
1665     vw->save_img_dir_dia = gtk_file_selection_new ( _("Choose a name for a new directory to hold images"));
1666     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1667     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1668   }
1669
1670   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1671   {
1672     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1673     if ( access ( fn, F_OK ) == 0 )
1674       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) );
1675     else
1676     {
1677       draw_to_image_file ( vw, fn, FALSE );
1678       break;
1679     }
1680   }
1681   gtk_widget_hide ( vw->save_img_dir_dia );
1682 }
1683
1684 #if GTK_CHECK_VERSION(2,10,0)
1685 static void print_cb ( GtkAction *a, VikWindow *vw )
1686 {
1687   a_print(vw, vw->viking_vvp);
1688 }
1689 #endif
1690
1691 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1692 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1693 {
1694   VikViewportDrawMode drawmode;
1695   if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1696     drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1697   }
1698   else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1699     drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1700   }
1701   else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1702     drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1703   }
1704   else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1705     drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1706   }
1707   else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1708     drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1709   }
1710   else {
1711     g_critical("Houston, we got a problem!\n");
1712     return;
1713   }
1714
1715   if ( !vw->only_updating_coord_mode_ui )
1716   {
1717     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1718     if ( olddrawmode != drawmode )
1719     {
1720       /* this takes care of coord mode too */
1721       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1722       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1723         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1724       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1725         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1726       }
1727       draw_update ( vw );
1728     }
1729   }
1730 }
1731
1732 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1733 {
1734   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1735   g_assert(check_box);
1736   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1737   vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1738   draw_update ( vw );
1739 }
1740
1741 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1742 {
1743   GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1744   g_assert(check_box);
1745   gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1746   vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1747   draw_update ( vw );
1748 }
1749
1750 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1751 {
1752   GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1753   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1754   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1755   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1756   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1757   {
1758     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1759     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1760     draw_update ( vw );
1761   }
1762   g_free ( color );
1763   gtk_widget_destroy ( colorsd );
1764 }
1765
1766
1767
1768 /***********************************************************************************************
1769  ** GUI Creation
1770  ***********************************************************************************************/
1771
1772 static GtkActionEntry entries[] = {
1773   { "File", NULL, "_File", 0, 0, 0 },
1774   { "Edit", NULL, "_Edit", 0, 0, 0 },
1775   { "View", NULL, "_View", 0, 0, 0 },
1776   { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1777   { "SetPan", NULL, "_Pan", 0, 0, 0 },
1778   { "Layers", NULL, "_Layers", 0, 0, 0 },
1779   { "Tools", NULL, "_Tools", 0, 0, 0 },
1780   { "Help", NULL, "_Help", 0, 0, 0 },
1781
1782   { "New",       GTK_STOCK_NEW,          "_New",                          "<control>N", "New file",                                     (GCallback)newwindow_cb          },
1783   { "Open",      GTK_STOCK_OPEN,         "_Open",                         "<control>O", "Open a file",                                  (GCallback)load_file             },
1784   { "Append",    GTK_STOCK_ADD,          "A_ppend File",                  NULL,         "Append data from a different file",            (GCallback)load_file             },
1785   { "Acquire", NULL, "A_cquire", 0, 0, 0 },
1786   { "AcquireGPS",   NULL,                "From _GPS",                     NULL,         "Transfer data from a GPS device",              (GCallback)acquire_from_gps      },
1787   { "AcquireGoogle",   NULL,             "Google _Directions",            NULL,         "Get driving directions from Google",           (GCallback)acquire_from_google   },
1788 #ifdef VIK_CONFIG_GEOCACHES
1789   { "AcquireGC",   NULL,                 "Geo_caches",                    NULL,         "Get Geocaches from geocaching.com",            (GCallback)acquire_from_gc       },
1790 #endif
1791   { "Save",      GTK_STOCK_SAVE,         "_Save",                         "<control>S", "Save the file",                                (GCallback)save_file             },
1792   { "SaveAs",    GTK_STOCK_SAVE_AS,      "Save _As",                      NULL,         "Save the file under different name",           (GCallback)save_file_as          },
1793   { "GenImg",    GTK_STOCK_CLEAR,        "_Generate Image File",          NULL,         "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1794   { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL,         "FIXME:IMGDIR",                                 (GCallback)draw_to_image_dir_cb  },
1795
1796 #if GTK_CHECK_VERSION(2,10,0)
1797   { "Print",    GTK_STOCK_PRINT,        "_Print...",          NULL,         "Print maps", (GCallback)print_cb },
1798 #endif
1799
1800   { "Exit",      GTK_STOCK_QUIT,         "E_xit",                         "<control>W", "Exit the program",                             (GCallback)window_close          },
1801   { "SaveExit",  GTK_STOCK_QUIT,         "Save and Exit",                 NULL, "Save and Exit the program",                             (GCallback)save_file_and_exit          },
1802
1803   { "GoogleMapsSearch",   GTK_STOCK_GO_FORWARD,                 "Go To Google Maps location",                     NULL,         "Go to address/place using Google Maps search",            (GCallback)goto_address       },
1804   { "GotoLL",    GTK_STOCK_QUIT,         "_Go to Lat\\/Lon...",           NULL,         "Go to arbitrary lat\\/lon coordinate",         (GCallback)draw_goto_cb          },
1805   { "GotoUTM",   GTK_STOCK_QUIT,         "Go to UTM...",                  NULL,         "Go to arbitrary UTM coordinate",               (GCallback)draw_goto_cb          },
1806   { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...",       NULL,         NULL,                                           (GCallback)set_bg_color          },
1807   { "ZoomIn",    GTK_STOCK_ZOOM_IN,      "Zoom _In",                   "<control>plus", NULL,                                           (GCallback)draw_zoom_cb          },
1808   { "ZoomOut",   GTK_STOCK_ZOOM_OUT,     "Zoom _Out",                 "<control>minus", NULL,                                           (GCallback)draw_zoom_cb          },
1809   { "ZoomTo",    GTK_STOCK_ZOOM_FIT,     "Zoom _To",               "<control><shift>Z", NULL,                                           (GCallback)zoom_to_cb            },
1810   { "Zoom0.25",  NULL,                   "0.25",                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1811   { "Zoom0.5",   NULL,                   "0.5",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1812   { "Zoom1",     NULL,                   "1",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1813   { "Zoom2",     NULL,                   "2",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1814   { "Zoom4",     NULL,                   "4",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1815   { "Zoom8",     NULL,                   "8",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1816   { "Zoom16",    NULL,                   "16",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1817   { "Zoom32",    NULL,                   "32",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1818   { "Zoom64",    NULL,                   "64",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1819   { "Zoom128",   NULL,                   "128",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1820   { "PanNorth",  NULL,                   "Pan North",                  "<control>Up", NULL,                                           (GCallback)draw_pan_cb },
1821   { "PanEast",  NULL,                    "Pan East",                   "<control>Right", NULL,                                           (GCallback)draw_pan_cb },
1822   { "PanSouth",  NULL,                   "Pan South",                  "<control>Down", NULL,                                           (GCallback)draw_pan_cb },
1823   { "PanWest",  NULL,                    "Pan West",                   "<control>Left", NULL,                                           (GCallback)draw_pan_cb },
1824   { "BGJobs",    GTK_STOCK_EXECUTE,      "Background _Jobs",              NULL,         NULL,                                           (GCallback)a_background_show_window },
1825
1826   { "Cut",       GTK_STOCK_CUT,          "Cu_t",                          NULL,         NULL,                                           (GCallback)menu_cut_layer_cb     },
1827   { "Copy",      GTK_STOCK_COPY,         "_Copy",                         NULL,         NULL,                                           (GCallback)menu_copy_layer_cb    },
1828   { "Paste",     GTK_STOCK_PASTE,        "_Paste",                        NULL,         NULL,                                           (GCallback)menu_paste_layer_cb   },
1829   { "Delete",    GTK_STOCK_DELETE,       "_Delete",                       NULL,         NULL,                                           (GCallback)menu_delete_layer_cb  },
1830   { "DeleteAll", NULL,                   "Delete All",                    NULL,         NULL,                                           (GCallback)clear_cb              },
1831   { "Properties",GTK_STOCK_PROPERTIES,   "_Properties",                   NULL,         NULL,                                           (GCallback)menu_properties_cb    },
1832
1833   { "About",     GTK_STOCK_ABOUT,        "_About",                        NULL,         NULL,                                           (GCallback)help_about_cb    },
1834 };
1835
1836 /* Radio items */
1837 static GtkRadioActionEntry mode_entries[] = {
1838   { "ModeUTM",         NULL,         "_UTM Mode",               "<control>u", NULL, 0 },
1839   { "ModeExpedia",     NULL,         "_Expedia Mode",           "<control>e", NULL, 1 },
1840   { "ModeGoogle",      NULL,         "_Old Google Mode",        "<control>o", NULL, 2 },
1841   { "ModeKH",          NULL,         "Old _KH Mode",            "<control>k", NULL, 3 },
1842   { "ModeMercator",    NULL,         "_Google Mode",            "<control>g", NULL, 4 }
1843 };
1844
1845 static GtkRadioActionEntry tool_entries[] = {
1846   { "Zoom",      "vik-icon-zoom",        "_Zoom",                         "<control><shift>Z", "Zoom Tool",  0 },
1847   { "Ruler",     "vik-icon-ruler",       "_Ruler",                        "<control><shift>R", "Ruler Tool", 1 }
1848 };
1849
1850 static GtkToggleActionEntry toggle_entries[] = {
1851   { "ShowScale", NULL,                   "Show Scale",                    NULL,         NULL,                                           (GCallback)set_draw_scale, TRUE   },
1852   { "ShowCenterMark", NULL,                   "Show Center Mark",                    NULL,         NULL,                                           (GCallback)set_draw_centermark, TRUE   },
1853 };
1854
1855 #include "menu.xml.h"
1856 static void window_create_ui( VikWindow *window )
1857 {
1858   GtkUIManager *uim;
1859   GtkActionGroup *action_group;
1860   GtkAccelGroup *accel_group;
1861   GError *error;
1862   guint i, j, mid;
1863   GtkIconFactory *icon_factory;
1864   GtkIconSet *icon_set; 
1865   GtkRadioActionEntry *tools = NULL, *radio;
1866   guint ntools;
1867   
1868   uim = gtk_ui_manager_new ();
1869   window->uim = uim;
1870
1871   toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1872   toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1873
1874   error = NULL;
1875   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1876     g_error_free (error);
1877     exit (1);
1878   }
1879
1880   action_group = gtk_action_group_new ("MenuActions");
1881   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1882   gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1883   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1884
1885   icon_factory = gtk_icon_factory_new ();
1886   gtk_icon_factory_add_default (icon_factory); 
1887
1888   register_vik_icons(icon_factory);
1889
1890   ntools = 0;
1891   for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1892       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1893       radio = &tools[ntools];
1894       ntools++;
1895       *radio = tool_entries[i];
1896       radio->value = ntools;
1897   }  
1898
1899   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1900     GtkActionEntry action;
1901     gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Layers/", 
1902                           vik_layer_get_interface(i)->name,
1903                           vik_layer_get_interface(i)->name,
1904                           GTK_UI_MANAGER_MENUITEM, FALSE);
1905
1906     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1907     gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1908     gtk_icon_set_unref (icon_set);
1909
1910     action.name = vik_layer_get_interface(i)->name;
1911     action.stock_id = vik_layer_get_interface(i)->name;
1912     action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
1913     action.accelerator = NULL;
1914     action.tooltip = NULL;
1915     action.callback = (GCallback)menu_addlayer_cb;
1916     gtk_action_group_add_actions(action_group, &action, 1, window);
1917
1918     if ( vik_layer_get_interface(i)->tools_count ) {
1919       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1920       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1921     }
1922
1923     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1924       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1925       radio = &tools[ntools];
1926       ntools++;
1927       
1928       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
1929                             _(vik_layer_get_interface(i)->tools[j].name),
1930                             vik_layer_get_interface(i)->tools[j].name,
1931                             GTK_UI_MANAGER_MENUITEM, FALSE);
1932       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems", 
1933                             _(vik_layer_get_interface(i)->tools[j].name),
1934                             vik_layer_get_interface(i)->tools[j].name,
1935                             GTK_UI_MANAGER_TOOLITEM, FALSE);
1936
1937       toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
1938
1939       radio->name = vik_layer_get_interface(i)->tools[j].name;
1940       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1941       radio->label = _(vik_layer_get_interface(i)->tools[j].name);
1942       radio->accelerator = NULL;
1943       radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
1944       radio->value = ntools;
1945     }
1946   }
1947   g_object_unref (icon_factory);
1948
1949   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1950   g_free(tools);
1951
1952   gtk_ui_manager_insert_action_group (uim, action_group, 0);
1953
1954   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1955     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1956       GtkAction *action = gtk_action_group_get_action(action_group,
1957                             vik_layer_get_interface(i)->tools[j].name);
1958       g_object_set(action, "sensitive", FALSE, NULL);
1959     }
1960   }
1961   window->action_group = action_group;
1962
1963   accel_group = gtk_ui_manager_get_accel_group (uim);
1964   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1965   gtk_ui_manager_ensure_update (uim);
1966 }
1967
1968
1969
1970 #include "icons/icons.h"
1971 static struct { 
1972   const GdkPixdata *data;
1973   gchar *stock_id;
1974 } stock_icons[] = {
1975   { &addtr_18,          "Create Track"      },
1976   { &begintr_18,        "Begin Track"      },
1977   { &edtr_18,           "Edit Trackpoint"   },
1978   { &addwp_18,          "Create Waypoint"   },
1979   { &edwp_18,           "Edit Waypoint"     },
1980   { &iscissors_18,      "Magic Scissors"   },
1981   { &zoom_18,           "vik-icon-zoom"     },
1982   { &ruler_18,          "vik-icon-ruler"    },
1983   { &geozoom_18,        "Georef Zoom Tool"  },
1984   { &geomove_18,        "Georef Move Map"   },
1985   { &mapdl_18,          "Maps Download"     },
1986   { &demdl_18,          "DEM Download/Import"     },
1987   { &showpic_18,        "Show Picture"      },
1988 };
1989  
1990 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1991
1992 static void
1993 register_vik_icons (GtkIconFactory *icon_factory)
1994 {
1995   GtkIconSet *icon_set; 
1996   gint i;
1997
1998   for (i = 0; i < n_stock_icons; i++) {
1999     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2000                    stock_icons[i].data, FALSE, NULL ));
2001     gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2002     gtk_icon_set_unref (icon_set);
2003   }
2004 }
2005
2006 void vik_window_cursors_init()
2007 {
2008   GdkPixbuf *cursor_pixbuf;
2009   GError *cursor_load_err;
2010
2011   cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
2012   vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2013
2014   g_object_unref ( G_OBJECT(cursor_pixbuf) );
2015
2016   cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
2017   vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2018
2019   g_object_unref ( G_OBJECT(cursor_pixbuf) );
2020 }
2021
2022 void vik_window_cursors_uninit()
2023 {
2024   gdk_cursor_unref ( vw_cursor_zoom );
2025   gdk_cursor_unref ( vw_cursor_ruler );
2026 }