]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
b995b553a088b637424807010168ae545883a781
[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 #include "viking.h"
22 #include "background.h"
23
24 #define VIKING_TITLE " - Viking " VIKING_VERSION " " VIKING_VERSION_NAME " " VIKING_URL
25
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #ifdef WINDOWS
32 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
33 #define make_dir(dir) mkdir(dir)
34 #else
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #define make_dir(dir) mkdir(dir,0777)
38 #endif
39
40 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
41 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
42 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
43
44 static void window_finalize ( GObject *gob );
45 static GObjectClass *parent_class;
46
47 static void window_init ( VikWindow *vw );
48 static void window_class_init ( VikWindowClass *klass );
49
50 static void draw_update ( VikWindow *vw );
51
52 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
53
54 /* Drawing & stuff */
55
56 static gboolean delete_event( VikWindow *vw );
57
58 static void draw_sync ( VikWindow *vw );
59 static void draw_redraw ( VikWindow *vw );
60 static void draw_scroll  ( VikWindow *vw, GdkEvent *event );
61 static void draw_click  ( VikWindow *vw, GdkEventButton *event );
62 static void draw_release ( VikWindow *vw, GdkEventButton *event );
63 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
64 static void draw_set_current_tool_cb ( GtkAction *a, VikWindow *vw );
65 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
66 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
67
68 static void draw_status ();
69
70 /* End Drawing Functions */
71
72 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
73 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
74 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
75 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
76
77 static void window_create_ui( VikWindow *window );
78
79 static void load_file ( GtkAction *a, VikWindow *vw );
80 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
81 static gboolean save_file ( GtkAction *a, VikWindow *vw );
82 static gboolean window_save ( VikWindow *vw );
83 static void register_vik_icons (GtkIconFactory *icon_factory);
84
85
86 struct _VikWindow {
87   GtkWindow gtkwindow;
88   VikViewport *viking_vvp;
89   VikLayersPanel *viking_vlp;
90   VikStatusbar *viking_vs;
91
92   GtkToolbar *toolbar;
93
94   GtkItemFactory *item_factory;
95
96   VikCoord oldcoord;
97   gboolean has_oldcoord;
98   guint current_tool;
99
100   guint16 tool_layer_id;
101   guint16 tool_tool_id;
102
103   gint pan_x, pan_y;
104
105   guint draw_image_width, draw_image_height;
106   gboolean draw_image_save_as_png;
107
108   gchar *filename;
109   gboolean modified;
110
111   GtkWidget *open_dia, *save_dia;
112
113   gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
114   GtkUIManager *uim;
115 };
116
117 enum {
118  TOOL_ZOOM = 0,
119  TOOL_RULER,
120  TOOL_LAYER,
121  NUMBER_OF_TOOLS
122 };
123
124 enum {
125   VW_NEWWINDOW_SIGNAL,
126   VW_OPENWINDOW_SIGNAL,
127   VW_LAST_SIGNAL
128 };
129
130 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
131
132 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
133
134 GType vik_window_get_type (void)
135 {
136   static GType vw_type = 0;
137
138   if (!vw_type)
139   {
140     static const GTypeInfo vw_info = 
141     {
142       sizeof (VikWindowClass),
143       NULL, /* base_init */
144       NULL, /* base_finalize */
145       (GClassInitFunc) window_class_init, /* class_init */
146       NULL, /* class_finalize */
147       NULL, /* class_data */
148       sizeof (VikWindow),
149       0,
150       (GInstanceInitFunc) window_init,
151     };
152     vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
153   }
154
155   return vw_type;
156 }
157
158 static void window_finalize ( GObject *gob )
159 {
160   VikWindow *vw = VIK_WINDOW(gob);
161   g_return_if_fail ( vw != NULL );
162
163   a_background_remove_status ( vw->viking_vs );
164
165   G_OBJECT_CLASS(parent_class)->finalize(gob);
166 }
167
168 static void window_class_init ( VikWindowClass *klass )
169 {
170   /* destructor */
171   GObjectClass *object_class;
172
173   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);
174   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);
175
176   object_class = G_OBJECT_CLASS (klass);
177
178   object_class->finalize = window_finalize;
179
180   parent_class = g_type_class_peek_parent (klass);
181
182 }
183
184 static void window_init ( VikWindow *vw )
185 {
186   GtkWidget *main_vbox;
187   GtkWidget *hpaned;
188
189   window_create_ui(vw);
190   gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
191
192   vw->viking_vvp = vik_viewport_new();
193   vw->viking_vlp = vik_layers_panel_new();
194   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
195   vw->viking_vs = vik_statusbar_new();
196
197   vw->filename = NULL;
198   vw->item_factory = NULL;
199
200   vw->has_oldcoord = FALSE;
201   vw->modified = FALSE;
202   vw->only_updating_coord_mode_ui = FALSE;
203
204   vw->pan_x = vw->pan_y = -1;
205   vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
206   vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
207   vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
208
209   main_vbox = gtk_vbox_new(FALSE, 1);
210   gtk_container_add (GTK_CONTAINER (vw), main_vbox);
211
212   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
213   gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
214   gtk_toolbar_set_icon_size(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), GTK_ICON_SIZE_SMALL_TOOLBAR);
215   gtk_toolbar_set_style (gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), GTK_TOOLBAR_ICONS);
216
217   g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
218
219   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
220   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
221   gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
222   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
223   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
224   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
225   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
226   g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
227
228   gtk_window_set_default_size ( GTK_WINDOW(vw), 1000, 800);
229
230   hpaned = gtk_hpaned_new ();
231   gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
232   gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
233
234   /* This packs the button into the window (a gtk container). */
235   gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
236
237   gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
238
239   a_background_add_status(vw->viking_vs);
240
241   vw->open_dia = NULL;
242   vw->save_dia = NULL;
243 }
244
245 VikWindow *vik_window_new ()
246 {
247   return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
248 }
249
250 static gboolean delete_event( VikWindow *vw )
251 {
252   if ( vw->modified )
253   {
254     GtkDialog *dia;
255     dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
256       "Do you want to save the changes you made to the document \"%s\"?\n\nYour changes will be lost if you don't save them.",
257       vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
258     gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
259     switch ( gtk_dialog_run ( dia ) )
260     {
261       case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
262       case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
263       default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
264     }
265   }
266   return FALSE;
267 }
268
269 /* Drawing stuff */
270 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
271 {
272   g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
273 }
274
275 static void draw_update ( VikWindow *vw )
276 {
277   draw_redraw (vw);
278   draw_sync (vw);
279 }
280
281 static void draw_sync ( VikWindow *vw )
282 {
283   vik_viewport_sync(vw->viking_vvp);
284   draw_status ( vw );
285   /* other things may be necc here later. */
286 }
287
288 static void draw_status ( VikWindow *vw )
289 {
290   static gchar zoom_level[22];
291   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" );
292   if ( vw->current_tool == TOOL_LAYER )
293     vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
294   else
295     vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
296
297   vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
298 }
299
300 static void draw_redraw ( VikWindow *vw )
301 {
302   vik_viewport_clear ( vw->viking_vvp);
303
304   vik_layers_panel_draw_all ( vw->viking_vlp );
305 }
306
307 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
308 {
309   static VikCoord coord;
310   static struct UTM utm;
311   static struct LatLon ll;
312   static char pointer_buf[36];
313
314   vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
315   vik_coord_to_utm ( &coord, &utm );
316   a_coords_utm_to_latlon ( &utm, &ll );
317
318   g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
319
320   if ( vw->pan_x != -1 )
321     vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
322
323   vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
324 }
325
326 static void draw_scroll (VikWindow *vw, GdkEvent *event)
327 {
328   if ( event->scroll.direction == GDK_SCROLL_UP )
329     vik_viewport_zoom_in (vw->viking_vvp);
330   else
331     vik_viewport_zoom_out(vw->viking_vvp);
332   draw_update(vw);
333 }
334
335 static void draw_release ( VikWindow *vw, GdkEventButton *event )
336 {
337   if ( event->button == 2 ) { /* move / pan */
338     if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
339         vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
340       else
341          vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
342                                          vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
343       draw_update ( vw );
344       vw->pan_x = vw->pan_y = -1;
345   }
346   if ( vw->current_tool == TOOL_LAYER && ( event->button == 1 || event->button == 3 ) &&
347       vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release )
348   {
349     vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
350         vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release,
351         event, vw->viking_vvp );
352   }
353 }
354
355 static void draw_click (VikWindow *vw, GdkEventButton *event)
356 {
357   if ( event->button == 2) {
358     vw->pan_x = (gint) event->x;
359     vw->pan_y = (gint) event->y;
360   } else if ( vw->current_tool == TOOL_ZOOM )
361   {
362     vw->modified = TRUE;
363     vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
364     if ( event->button == 1 )
365       vik_viewport_zoom_in (vw->viking_vvp);
366     else if ( event->button == 3 )
367       vik_viewport_zoom_out (vw->viking_vvp);
368     draw_update ( vw );
369   }
370   else if ( vw->current_tool == TOOL_RULER )
371   {
372     struct LatLon ll;
373     VikCoord coord;
374     gchar *temp;
375     if ( event->button == 1 || event->button == 3 )
376     {
377       vik_viewport_screen_to_coord ( vw->viking_vvp, (gint) event->x, (gint) event->y, &coord );
378       vik_coord_to_latlon ( &coord, &ll );
379       if ( vw->has_oldcoord )
380          temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
381       else
382         temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
383
384       vik_statusbar_set_message ( vw->viking_vs, 3, temp );
385       g_free ( temp );
386
387       vw->oldcoord = coord;
388       /* we don't use anything else so far */
389       vw->has_oldcoord = TRUE;
390     }
391     else
392     {
393       vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
394       draw_update ( vw );
395     }
396   }
397   else if ( vw->current_tool == TOOL_LAYER )
398   {
399     vw->modified = TRUE;
400     if ( ! vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
401         vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback,
402         event, vw->viking_vvp ) )
403       a_dialog_info_msg_extra ( GTK_WINDOW(vw), "A %s Layer must exist and be either selected or visible before you can use this function.", vik_layer_get_interface ( vw->tool_layer_id )->name );
404   }
405 }
406
407 static void draw_set_current_tool_cb ( GtkAction *a, VikWindow *vw )
408 {
409   if (!strcmp(gtk_action_get_name(a), "Zoom")) {
410     vw->current_tool = TOOL_ZOOM;
411   }
412   if (!strcmp(gtk_action_get_name(a), "Ruler")) {
413     vw->current_tool = TOOL_RULER;
414   }
415   draw_status ( vw );
416 }
417
418 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
419 {
420   guint what = 128;
421
422   if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
423     what = -3;
424   } 
425   else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
426     what = -4;
427   }
428   else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
429     what = -2;
430   }
431   else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
432     what = -1;
433   }
434   else {
435     gchar *s = (gchar *)gtk_action_get_name(a);
436     what = atoi(s+4);
437   }
438
439   switch (what)
440   {
441     case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
442     case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
443     case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
444     case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
445     default: vik_viewport_set_zoom ( vw->viking_vvp, what );
446   }
447   draw_update ( vw );
448 }
449
450 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
451 {
452   VikCoord new_center;
453
454   if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
455     struct LatLon ll, llold;
456     vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
457     if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
458       vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
459     else
460       return;
461   }
462   else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
463     struct UTM utm, utmold;
464     vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
465     if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
466       vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
467     else
468      return;
469   }
470   else {
471     fprintf(stderr, "Houston we have a problem\n");
472     return;
473   }
474
475   vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
476   draw_update ( vw );
477 }
478
479 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
480 {
481   gint type;
482   for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
483     if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
484       if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
485         draw_update ( vw );
486         vw->modified = TRUE;
487       }
488     }
489   }
490 }
491
492 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
493 {
494   a_clipboard_copy ( vw->viking_vlp );
495 }
496
497 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
498 {
499   a_clipboard_copy ( vw->viking_vlp );
500   menu_delete_layer_cb ( a, vw );
501 }
502
503 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
504 {
505   if ( a_clipboard_paste ( vw->viking_vlp ) )
506   {
507     draw_update ( vw );
508     vw->modified = TRUE;
509   }
510 }
511
512 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
513 {
514   if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
515     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
516 }
517
518 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
519 {
520   if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
521   {
522     vik_layers_panel_delete_selected ( vw->viking_vlp );
523     vw->modified = TRUE;
524   }
525   else
526     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
527 }
528
529 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
530 {
531   /* White Magic, my friends ... White Magic... */
532   int i, j;
533
534   if (!strcmp(gtk_action_get_name(a), "Zoom")) {
535     vw->current_tool = TOOL_ZOOM;
536   } 
537   else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
538     vw->current_tool = TOOL_RULER;
539   }
540   else {
541     for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
542       for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
543         if (!strcmp(vik_layer_get_interface(i)->tools[j].name, gtk_action_get_name(a))) {
544           vw->current_tool = TOOL_LAYER;
545           vw->tool_layer_id = i;
546           vw->tool_tool_id = j;
547         }
548       }
549     }
550   }
551   draw_status ( vw );
552 }
553
554 static void window_set_filename ( VikWindow *vw, const gchar *filename )
555 {
556   gchar *title;
557   if ( vw->filename )
558     g_free ( vw->filename );
559   if ( filename == NULL )
560   {
561     vw->filename = NULL;
562     gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
563   }
564   else
565   {
566     vw->filename = g_strdup(filename);
567     title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
568     gtk_window_set_title ( GTK_WINDOW(vw), title );
569     g_free ( title );
570   }
571 }
572
573 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
574 {
575   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
576   {
577     case 0:
578       a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
579       break;
580     case 1:
581     {
582       GtkWidget *mode_button;
583       gchar *buttonname;
584       if ( change_filename )
585         window_set_filename ( vw, filename );
586       switch ( vik_viewport_get_drawmode ( vw->viking_vvp ) ) {
587         case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
588         case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
589         case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
590         default: buttonname = "/ui/MainMenu/View/ModeKH";
591       }
592       mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
593       g_assert ( mode_button );
594       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. */
595       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
596       vw->only_updating_coord_mode_ui = FALSE;
597
598       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
599     }
600     default: draw_update ( vw );
601   }
602 }
603
604 static void load_file ( GtkAction *a, VikWindow *vw )
605 {
606   gboolean newwindow;
607   if (!strcmp(gtk_action_get_name(a), "Open")) {
608     newwindow = TRUE;
609   } 
610   else if (!strcmp(gtk_action_get_name(a), "Append")) {
611     newwindow = FALSE;
612   } 
613   else {
614     fprintf(stderr, "Houston we got a problem\n");
615     return;
616   }
617     
618   if ( ! vw->open_dia )
619   {
620     vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
621     gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
622     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
623     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
624   }
625   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
626   {
627     gtk_widget_hide ( vw->open_dia );
628     if ( (vw->modified || vw->filename) && newwindow )
629       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
630     else {
631       gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
632       gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
633       while ( *files ) {
634         vik_window_open_file ( vw, *files, change_fn );
635         files++;
636       }
637     }
638   }
639   else
640     gtk_widget_hide ( vw->open_dia );
641 }
642
643 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
644 {
645   gboolean rv = FALSE;
646   const gchar *fn;
647   if ( ! vw->save_dia )
648   {
649     vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
650     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
651     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
652   }
653
654   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
655   {
656     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
657     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 ) ) )
658     {
659       window_set_filename ( vw, fn );
660       rv = window_save ( vw );
661       vw->modified = FALSE;
662       break;
663     }
664   }
665   gtk_widget_hide ( vw->save_dia );
666   return rv;
667 }
668
669 static gboolean window_save ( VikWindow *vw )
670 {
671   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
672     return TRUE;
673   else
674   {
675     a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
676     return FALSE;
677   }
678 }
679
680 static gboolean save_file ( GtkAction *a, VikWindow *vw )
681 {
682   if ( ! vw->filename )
683     return save_file_as ( NULL, vw );
684   else
685   {
686     vw->modified = FALSE;
687     return window_save ( vw );
688   }
689 }
690
691 static void clear_cb ( GtkAction *a, VikWindow *vw )
692 {
693   vik_layers_panel_clear ( vw->viking_vlp );
694   window_set_filename ( vw, NULL );
695   draw_update ( vw );
696 }
697
698 static void window_close ( GtkAction *a, VikWindow *vw )
699 {
700   if ( ! delete_event ( vw ) )
701     gtk_widget_destroy ( GTK_WIDGET(vw) );
702 }
703
704 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
705 {
706   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
707   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
708   {
709     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
710     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
711     draw_update ( vw );
712   }
713 }
714
715 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
716 {
717   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
718   GdkPixbuf *pixbuf_to_save;
719   gdouble old_xmpp, old_ympp;
720   GError *error = NULL;
721
722   /* backup old zoom & set new */
723   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
724   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
725   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
726
727   /* reset width and height: */
728   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
729
730   /* draw all layers */
731   draw_redraw ( vw );
732
733   /* save buffer as file. */
734   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);
735   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
736   if (error)
737   {
738     fprintf(stderr, "Unable to write to file %s: %s", fn, error->message );
739     g_error_free (error);
740   }
741   g_object_unref ( G_OBJECT(pixbuf_to_save) );
742
743   /* pretend like nothing happened ;) */
744   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
745   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
746   vik_viewport_configure ( vw->viking_vvp );
747   draw_update ( vw );
748 }
749
750 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 )
751 {
752   gulong size = sizeof(gchar) * (strlen(fn) + 15);
753   gchar *name_of_file = g_malloc ( size );
754   guint x = 1, y = 1;
755   struct UTM utm_orig, utm;
756
757   /* *** copied from above *** */
758   GdkPixbuf *pixbuf_to_save;
759   gdouble old_xmpp, old_ympp;
760   GError *error = NULL;
761
762   /* backup old zoom & set new */
763   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
764   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
765   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
766
767   /* reset width and height: do this only once for all images (same size) */
768   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
769   /* *** end copy from above *** */
770
771   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
772
773   make_dir(fn);
774
775   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
776
777   for ( y = 1; y <= tiles_h; y++ )
778   {
779     for ( x = 1; x <= tiles_w; x++ )
780     {
781       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, VIKING_FILE_SEP, y, x, save_as_png ? "png" : "jpg" );
782       utm = utm_orig;
783       if ( tiles_w & 0x1 )
784         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
785       else
786         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
787       if ( tiles_h & 0x1 ) /* odd */
788         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
789       else /* even */
790         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
791
792       /* move to correct place. */
793       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
794
795       draw_redraw ( vw );
796
797       /* save buffer as file. */
798       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);
799       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
800       if (error)
801       {
802         fprintf(stderr, "Unable to write to file %s: %s", name_of_file, error->message );
803         g_error_free (error);
804       }
805
806       g_object_unref ( G_OBJECT(pixbuf_to_save) );
807     }
808   }
809
810   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
811   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
812   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
813   vik_viewport_configure ( vw->viking_vvp );
814   draw_update ( vw );
815
816   g_free ( name_of_file );
817 }
818
819 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
820 {
821   VikWindow *vw = VIK_WINDOW(pass_along[0]);
822   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
823   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
824   gdouble width_min, width_max, height_min, height_max;
825   gint width, height;
826
827   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
828   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
829
830   /* TODO: support for xzoom and yzoom values */
831   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
832   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
833
834   if ( width > width_max || width < width_min || height > height_max || height < height_min )
835     a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
836
837   gtk_spin_button_set_value ( width_spin, width );
838   gtk_spin_button_set_value ( height_spin, height );
839 }
840
841 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
842 {
843   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
844   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
845   gchar *label_text;
846   gdouble w, h;
847   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
848   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
849   if (pass_along[4]) /* save many images; find TOTAL area covered */
850   {
851     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
852     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
853   }
854   label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
855   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
856   g_free ( label_text );
857 }
858
859 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
860 {
861   /* todo: default for answers inside VikWindow or static (thruout instance) */
862   GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
863                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
864                                                   GTK_STOCK_CANCEL,
865                                                   GTK_RESPONSE_REJECT,
866                                                   GTK_STOCK_OK,
867                                                   GTK_RESPONSE_ACCEPT,
868                                                   0 );
869   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
870   GtkWidget *png_radio, *jpeg_radio;
871   GtkWidget *current_window_button;
872   gpointer current_window_pass_along[7];
873   GtkWidget *zoom_label, *zoom_spin;
874   GtkWidget *total_size_label;
875
876   /* only used if (!one_image_only) */
877   GtkWidget *tiles_width_spin, *tiles_height_spin;
878
879
880   width_label = gtk_label_new ( "Width (pixels):" );
881   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
882   height_label = gtk_label_new ( "Height (pixels):" );
883   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
884
885   zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
886   /* TODO: separate xzoom and yzoom factors */
887   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);
888
889   total_size_label = gtk_label_new ( NULL );
890
891   current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
892   current_window_pass_along [0] = vw;
893   current_window_pass_along [1] = width_spin;
894   current_window_pass_along [2] = height_spin;
895   current_window_pass_along [3] = zoom_spin;
896   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
897   current_window_pass_along [5] = NULL;
898   current_window_pass_along [6] = total_size_label;
899   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
900
901   png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
902   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
903
904   if ( ! vw->draw_image_save_as_png )
905     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
906
907   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
908   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
909   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
910   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
911   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
912   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
913   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
914   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
915   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
916
917   if ( ! one_image_only )
918   {
919     GtkWidget *tiles_width_label, *tiles_height_label;
920
921
922     tiles_width_label = gtk_label_new ( "East-west image tiles:" );
923     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
924     tiles_height_label = gtk_label_new ( "North-south image tiles:" );
925     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
926     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
927     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
928     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
929     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
930
931     current_window_pass_along [4] = tiles_width_spin;
932     current_window_pass_along [5] = tiles_height_spin;
933     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
934     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
935   }
936   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
937   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
938   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
939   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
940
941   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
942
943   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
944
945   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
946   {
947     gtk_widget_hide ( GTK_WIDGET(dialog) );
948     if ( one_image_only )
949       save_image_file ( vw, fn, 
950                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
951                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
952                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
953                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
954     else {
955       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
956         save_image_dir ( vw, fn, 
957                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
958                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
959                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
960                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
961                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
962                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
963       else
964         a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
965     }
966   }
967   gtk_widget_destroy ( GTK_WIDGET(dialog) );
968 }
969
970
971 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
972 {
973   GtkWidget *file_selector;
974   const gchar *fn;
975   file_selector = gtk_file_selection_new ("Save Image");
976
977   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
978   {
979     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
980     if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(file_selector), "The file \"%s\" exists, do you wish to overwrite it?", a_file_basename ( fn ) ) )
981     {
982       gtk_widget_hide ( file_selector );
983       draw_to_image_file ( vw, fn, TRUE );
984       break;
985     }
986   }
987   gtk_widget_destroy ( file_selector );
988 }
989
990 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
991 {
992   GtkWidget *file_selector;
993   const gchar *fn;
994   file_selector = gtk_file_selection_new ("Choose a name for a new directory to hold images");
995
996   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
997   {
998     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
999     if ( access ( fn, F_OK ) == 0 )
1000       a_dialog_info_msg_extra ( GTK_WINDOW(file_selector), "The file %s exists. Please choose a name for a new directory to hold images in that does not exist.", a_file_basename(fn) );
1001     else
1002     {
1003       gtk_widget_hide ( file_selector );
1004       draw_to_image_file ( vw, fn, FALSE );
1005       break;
1006     }
1007   }
1008   gtk_widget_destroy ( file_selector );
1009 }
1010
1011 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1012 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1013 {
1014   VikViewportDrawMode drawmode;
1015   if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1016     drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1017   }
1018   else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1019     drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1020   }
1021   else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1022     drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1023   }
1024   else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1025     drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1026   }
1027   else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1028     drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1029   }
1030   else {
1031     fprintf(stderr, "Houston, we got a problem!\n");
1032     return;
1033   }
1034
1035   if ( !vw->only_updating_coord_mode_ui )
1036   {
1037     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1038     if ( olddrawmode != drawmode )
1039     {
1040       /* this takes care of coord mode too */
1041       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1042       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1043         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1044       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1045         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1046       }
1047       draw_update ( vw );
1048     }
1049   }
1050 }
1051
1052 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1053 {
1054   GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1055   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1056   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1057   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1058   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1059   {
1060     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1061     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1062     draw_update ( vw );
1063   }
1064   g_free ( color );
1065   gtk_widget_destroy ( colorsd );
1066 }
1067
1068 static GtkActionEntry entries[] = {
1069   { "File", NULL, "_File", 0, 0, 0 },
1070   { "View", NULL, "_View", 0, 0, 0 },
1071   { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1072   { "Layers", NULL, "_Layers", 0, 0, 0 },
1073   { "Tools", NULL, "_Tools", 0, 0, 0 },
1074
1075   { "New",       GTK_STOCK_NEW,          "_New",                          "<control>N", "New file",                                     (GCallback)newwindow_cb          },
1076   { "Open",      GTK_STOCK_OPEN,         "_Open",                         "<control>O", "Open a file",                                  (GCallback)load_file             },
1077   { "Append",    GTK_STOCK_ADD,          "A_ppend File",                  NULL,         "Append data from a different file",            (GCallback)load_file             },
1078   { "Save",      GTK_STOCK_SAVE,         "_Save",                         "<control>S", "Save the file",                                (GCallback)save_file             },
1079   { "SaveAs",    GTK_STOCK_SAVE_AS,      "Save _As",                      NULL,         "Save the file under different name",           (GCallback)save_file_as          },
1080   { "GenImg",    GTK_STOCK_CLEAR,        "_Generate Image File",          NULL,         "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1081   { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL,         "FIXME:IMGDIR",                                 (GCallback)draw_to_image_dir_cb  },
1082   { "Exit",      GTK_STOCK_QUIT,         "E_xit",                         "<control>W", "Exit the program",                             (GCallback)window_close          },
1083
1084   { "GotoLL",    GTK_STOCK_QUIT,         "_Go to Lat\\/Lon...",           NULL,         "Go to arbitrary lat\\/lon coordinate",         (GCallback)draw_goto_cb          },
1085   { "GotoUTM",   GTK_STOCK_QUIT,         "Go to UTM...",                  NULL,         "Go to arbitrary UTM coordinate",               (GCallback)draw_goto_cb          },
1086   { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...",       NULL,         NULL,                                           (GCallback)set_bg_color          },
1087   { "ZoomIn",    GTK_STOCK_ZOOM_IN,      "Zoom _In",                   "<control>plus", NULL,                                           (GCallback)draw_zoom_cb          },
1088   { "ZoomOut",   GTK_STOCK_ZOOM_OUT,     "Zoom _Out",                 "<control>minus", NULL,                                           (GCallback)draw_zoom_cb          },
1089   { "ZoomTo",    GTK_STOCK_ZOOM_FIT,     "Zoom _To",               "<control><shift>Z", NULL,                                           (GCallback)zoom_to_cb            },
1090   { "Zoom0.25",  NULL,                   "0.25",                          NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1091   { "Zoom0.5",   NULL,                   "0.5",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1092   { "Zoom1",     NULL,                   "1",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1093   { "Zoom2",     NULL,                   "2",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1094   { "Zoom4",     NULL,                   "4",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1095   { "Zoom8",     NULL,                   "8",                             NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1096   { "Zoom16",    NULL,                   "16",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1097   { "Zoom32",    NULL,                   "32",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1098   { "Zoom64",    NULL,                   "64",                            NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1099   { "Zoom128",   NULL,                   "128",                           NULL,         NULL,                                           (GCallback)draw_zoom_cb          },
1100   { "BGJobs",    GTK_STOCK_EXECUTE,      "Background _Jobs",              NULL,         NULL,                                           (GCallback)a_background_show_window },
1101
1102   { "Cut",       GTK_STOCK_CUT,          "Cu_t",                          NULL,         NULL,                                           (GCallback)menu_cut_layer_cb     },
1103   { "Copy",      GTK_STOCK_COPY,         "_Copy",                         NULL,         NULL,                                           (GCallback)menu_copy_layer_cb    },
1104   { "Paste",     GTK_STOCK_PASTE,        "_Paste",                        NULL,         NULL,                                           (GCallback)menu_paste_layer_cb   },
1105   { "Delete",    GTK_STOCK_DELETE,       "_Delete",                       NULL,         NULL,                                           (GCallback)menu_delete_layer_cb  },
1106   { "DeleteAll", NULL,                   "Delete All",                    NULL,         NULL,                                           (GCallback)clear_cb              },
1107   { "Properties",GTK_STOCK_PROPERTIES,   "_Properties",                   NULL,         NULL,                                           (GCallback)menu_properties_cb    },
1108 };
1109
1110 /* Radio items */
1111 static GtkRadioActionEntry mode_entries[] = {
1112   { "ModeUTM",         NULL,         "_UTM Mode",               "<control>u", NULL, 0 },
1113   { "ModeExpedia",     NULL,         "_Expedia Mode",           "<control>e", NULL, 1 },
1114   { "ModeGoogle",      NULL,         "_Google Mode",            "<control>g", NULL, 2 },
1115   { "ModeKH",          NULL,         "_KH\\/Flat LatLon Mode",  "<control>k", NULL, 3 },
1116   { "ModeMercator",    NULL,         "_Mercator (New Google)",  "<control>m", NULL, 4 }
1117 };
1118
1119 static GtkRadioActionEntry tool_entries[] = {
1120   { "Zoom",      "vik-icon-zoom",        "_Zoom",                         "<control><shift>Z", "Zoom Tool",  0 },
1121   { "Ruler",     "vik-icon-ruler",       "_Ruler",                        "<control><shift>R", "Ruler Tool", 1 }
1122 };
1123
1124 #include "menu.xml.h"
1125 static void window_create_ui( VikWindow *window )
1126 {
1127   GtkUIManager *uim;
1128   GtkActionGroup *action_group;
1129   GtkAccelGroup *accel_group;
1130   GError *error;
1131   GtkActionEntry add_layer_item;
1132   guint i, j, mid;
1133   GtkIconFactory *icon_factory;
1134   GtkIconSet *icon_set; 
1135   GtkIconSource *icon_source;
1136   GtkRadioActionEntry *tools = NULL, *radio;
1137   guint ntools;
1138   
1139   uim = gtk_ui_manager_new ();
1140   window->uim = uim;
1141
1142   
1143   error = NULL;
1144   if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1145     g_error_free (error);
1146     exit (1);
1147   }
1148
1149   action_group = gtk_action_group_new ("MenuActions");
1150   gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1151   gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 0, (GCallback)window_change_coord_mode_cb, window);
1152
1153   icon_factory = gtk_icon_factory_new ();
1154
1155   register_vik_icons(icon_factory);
1156
1157   ntools = 0;
1158   for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1159       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1160       radio = &tools[ntools];
1161       ntools++;
1162       *radio = tool_entries[i];
1163       radio->value = ntools;
1164   }  
1165
1166   for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1167     GtkActionEntry action;
1168     gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Layers/", 
1169                           vik_layer_get_interface(i)->name,
1170                           vik_layer_get_interface(i)->name,
1171                           GTK_UI_MANAGER_MENUITEM, FALSE);
1172
1173     icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1174     gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1175     gtk_icon_set_unref (icon_set);
1176
1177     action.name = vik_layer_get_interface(i)->name;
1178     action.stock_id = vik_layer_get_interface(i)->name;
1179     action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1180     action.accelerator = NULL;
1181     action.tooltip = NULL;
1182     action.callback = (GCallback)menu_addlayer_cb;
1183     gtk_action_group_add_actions(action_group, &action, 1, window);
1184
1185     if ( vik_layer_get_interface(i)->tools_count ) {
1186       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1187       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1188     }
1189
1190     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1191       tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1192       radio = &tools[ntools];
1193       ntools++;
1194       
1195       fprintf(stderr, "    %s\n",                           vik_layer_get_interface(i)->tools[j].name);
1196       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainMenu/Tools", 
1197                             vik_layer_get_interface(i)->tools[j].name,
1198                             vik_layer_get_interface(i)->tools[j].name,
1199                             GTK_UI_MANAGER_MENUITEM, FALSE);
1200       gtk_ui_manager_add_ui(uim, mid,  "/ui/MainToolbar/ToolItems", 
1201                             vik_layer_get_interface(i)->tools[j].name,
1202                             vik_layer_get_interface(i)->tools[j].name,
1203                             GTK_UI_MANAGER_TOOLITEM, FALSE);
1204
1205       radio->name = vik_layer_get_interface(i)->tools[j].name;
1206       radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1207       radio->label = vik_layer_get_interface(i)->tools[j].name;
1208       radio->accelerator = NULL;
1209       radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1210       radio->value = ntools;
1211     }
1212   }
1213
1214   gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1215   g_free(tools);
1216
1217   gtk_ui_manager_insert_action_group (uim, action_group, 0);
1218
1219   accel_group = gtk_ui_manager_get_accel_group (uim);
1220   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1221   gtk_ui_manager_ensure_update (uim);
1222   gtk_icon_factory_add_default (icon_factory); 
1223   g_object_unref (icon_factory);
1224 }
1225
1226
1227 static struct { 
1228   gchar *fn[3];
1229   gchar *stock_id;
1230 } stock_icons[] = {
1231   { { NULL, "addtr-18.png",   NULL },            "Create Track"      },
1232   { { NULL, "edtr-18.png",    NULL },            "Edit Trackpoint"   },
1233   { { NULL, "addwp-18.png",   NULL },            "Create Waypoint"   },
1234   { { NULL, "edwp-18.png",    NULL },            "Edit Waypoint"     },
1235   { { NULL, "zoom-18.png",    NULL },            "vik-icon-zoom"     },
1236   { { NULL, "ruler-18.png",   NULL },            "vik-icon-ruler"    },
1237   { { NULL, "geozoom-18.png", NULL },            "Georef Zoom Tool"  },
1238   { { NULL, "geomove-18.png", NULL },            "Georef Move Map"   },
1239   { { NULL, "mapdl-18.png",   NULL },            "Maps Download"     },
1240   { { NULL, "showpic-18.png", NULL },            "Show Picture"      },
1241 };
1242  
1243
1244 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1245
1246 static void
1247 register_vik_icons (GtkIconFactory *icon_factory)
1248 {
1249   GtkIconSet *icon_set; 
1250   GtkIconSource *icon_source;
1251   gint i;
1252   gchar cwd[128];
1253   getcwd(cwd, sizeof(cwd));
1254
1255   for (i = 0; i < n_stock_icons; i++) {
1256     gchar fn[128];
1257     int j;
1258
1259     icon_set = gtk_icon_set_new ();
1260
1261     for (j=0; j<3; j++) {
1262       if (stock_icons[i].fn[j]) {
1263         icon_source = gtk_icon_source_new ();
1264         sprintf(fn, "%s/%s", cwd, stock_icons[i].fn[j]);
1265         gtk_icon_source_set_filename (icon_source, fn);
1266         gtk_icon_set_add_source (icon_set, icon_source);
1267         gtk_icon_source_free (icon_source);
1268       }
1269     }
1270
1271     gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1272     gtk_icon_set_unref (icon_set);
1273   }
1274 }