]> git.street.me.uk Git - andy/viking.git/blob - src/vikwindow.c
Alex Foobarian's track profile patch
[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 #include "viking.h"
23 #include "background.h"
24
25 #define VIKING_TITLE " - Viking " VIKING_VERSION " " VIKING_VERSION_NAME " " VIKING_URL
26
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 ( 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 ( VikWindow *vw, guint mode );
65 static void draw_zoom ( VikWindow *vw, gint what );
66 static void draw_goto ( VikWindow *vw, gint mode );
67
68 static void draw_status ();
69
70 /* End Drawing Functions */
71
72 static void menu_addlayer_cb ( VikWindow *vw, gint type );
73 static void menu_properties_cb ( VikWindow *vw );
74 static void menu_delete_layer_cb ( VikWindow *vw );
75 static void menu_dynamic_tool_cb ( VikWindow *vw, guint id );
76
77 static GtkWidget *window_create_menubar( VikWindow *window );
78
79 static void load_file ( VikWindow *vw, gboolean newwindow );
80 static gboolean save_file_as ( VikWindow *vw );
81 static gboolean save_file ( VikWindow *vw );
82 static gboolean window_save ( VikWindow *vw );
83
84 struct _VikWindow {
85   GtkWindow gtkwindow;
86   VikViewport *viking_vvp;
87   VikLayersPanel *viking_vlp;
88   VikStatusbar *viking_vs;
89
90   GtkItemFactory *item_factory;
91
92   VikCoord oldcoord;
93   gboolean has_oldcoord;
94   guint current_tool;
95
96   guint16 tool_layer_id;
97   guint16 tool_tool_id;
98
99   gint pan_x, pan_y;
100
101   guint draw_image_width, draw_image_height;
102   gboolean draw_image_save_as_png;
103
104   gchar *filename;
105   gboolean modified;
106
107   GtkWidget *open_dia, *save_dia;
108
109   gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
110 };
111
112 enum {
113  TOOL_ZOOM = 0,
114  TOOL_RULER,
115  TOOL_LAYER,
116  NUMBER_OF_TOOLS
117 };
118
119 enum {
120   VW_NEWWINDOW_SIGNAL,
121   VW_OPENWINDOW_SIGNAL,
122   VW_LAST_SIGNAL
123 };
124
125 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
126
127 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
128
129 GType vik_window_get_type (void)
130 {
131   static GType vw_type = 0;
132
133   if (!vw_type)
134   {
135     static const GTypeInfo vw_info = 
136     {
137       sizeof (VikWindowClass),
138       NULL, /* base_init */
139       NULL, /* base_finalize */
140       (GClassInitFunc) window_class_init, /* class_init */
141       NULL, /* class_finalize */
142       NULL, /* class_data */
143       sizeof (VikWindow),
144       0,
145       (GInstanceInitFunc) window_init,
146     };
147     vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
148   }
149
150   return vw_type;
151 }
152
153 static void window_finalize ( GObject *gob )
154 {
155   VikWindow *vw = VIK_WINDOW(gob);
156   g_return_if_fail ( vw != NULL );
157
158   a_background_remove_status ( vw->viking_vs );
159
160   G_OBJECT_CLASS(parent_class)->finalize(gob);
161 }
162
163 static void window_class_init ( VikWindowClass *klass )
164 {
165   /* destructor */
166   GObjectClass *object_class;
167
168   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);
169   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__STRING, G_TYPE_NONE, 1, G_TYPE_POINTER);
170
171   object_class = G_OBJECT_CLASS (klass);
172
173   object_class->finalize = window_finalize;
174
175   parent_class = g_type_class_peek_parent (klass);
176
177 }
178
179 static void window_init ( VikWindow *vw )
180 {
181   GtkWidget *main_vbox;
182   GtkWidget *hpaned;
183
184   gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
185
186   vw->viking_vvp = vik_viewport_new();
187   vw->viking_vlp = vik_layers_panel_new();
188   vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
189   vw->viking_vs = vik_statusbar_new();
190
191   vw->filename = NULL;
192
193   vw->has_oldcoord = FALSE;
194   vw->modified = FALSE;
195   vw->only_updating_coord_mode_ui = FALSE;
196
197   vw->pan_x = vw->pan_y = -1;
198   vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
199   vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
200   vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
201
202   main_vbox = gtk_vbox_new(FALSE, 1);
203   gtk_container_add (GTK_CONTAINER (vw), main_vbox);
204
205   gtk_box_pack_start (GTK_BOX(main_vbox), window_create_menubar(vw), FALSE, TRUE, 0);
206
207   g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
208
209   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
210   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
211   gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
212   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
213   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
214   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
215   g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
216   g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
217
218   gtk_window_set_default_size ( GTK_WINDOW(vw), 1000, 800);
219
220   hpaned = gtk_hpaned_new ();
221   gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
222   gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
223
224   /* This packs the button into the window (a gtk container). */
225   gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
226
227   gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
228
229   a_background_add_status(vw->viking_vs);
230
231   vw->open_dia = NULL;
232   vw->save_dia = NULL;
233 }
234
235 VikWindow *vik_window_new ()
236 {
237   return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
238 }
239
240 static gboolean delete_event( VikWindow *vw )
241 {
242   if ( vw->modified )
243   {
244     GtkDialog *dia;
245     dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
246       "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.",
247       vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
248     gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
249     switch ( gtk_dialog_run ( dia ) )
250     {
251       case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
252       case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
253       default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(vw);
254     }
255   }
256   return FALSE;
257 }
258
259 /* Drawing stuff */
260 static void newwindow_cb ( VikWindow *vw )
261 {
262   g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
263 }
264
265 static void draw_update ( VikWindow *vw )
266 {
267   draw_redraw (vw);
268   draw_sync (vw);
269 }
270
271 static void draw_sync ( VikWindow *vw )
272 {
273   vik_viewport_sync(vw->viking_vvp);
274   draw_status ( vw );
275   /* other things may be necc here later. */
276 }
277
278 static void draw_status ( VikWindow *vw )
279 {
280   static gchar zoom_level[22];
281   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" );
282   if ( vw->current_tool == TOOL_LAYER )
283     vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
284   else
285     vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
286
287   vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
288 }
289
290 static void draw_redraw ( VikWindow *vw )
291 {
292   vik_viewport_clear ( vw->viking_vvp);
293
294   vik_layers_panel_draw_all ( vw->viking_vlp );
295 }
296
297 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
298 {
299   static VikCoord coord;
300   static struct UTM utm;
301   static struct LatLon ll;
302   static char pointer_buf[36];
303
304   vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
305   vik_coord_to_utm ( &coord, &utm );
306   a_coords_utm_to_latlon ( &utm, &ll );
307
308   g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
309
310   if ( vw->pan_x != -1 )
311     vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
312
313   vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
314 }
315
316 static void draw_scroll (VikWindow *vw, GdkEvent *event)
317 {
318   if ( event->scroll.direction == GDK_SCROLL_UP )
319     vik_viewport_zoom_in (vw->viking_vvp);
320   else
321     vik_viewport_zoom_out(vw->viking_vvp);
322   draw_update(vw);
323 }
324
325 static void draw_release ( VikWindow *vw, GdkEventButton *event )
326 {
327   if ( event->button == 2 ) { /* move / pan */
328     if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
329         vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
330       else
331          vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
332                                          vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
333       draw_update ( vw );
334       vw->pan_x = vw->pan_y = -1;
335   }
336   if ( vw->current_tool == TOOL_LAYER && ( event->button == 1 || event->button == 3 ) &&
337       vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release )
338   {
339     vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
340         vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release,
341         event, vw->viking_vvp );
342   }
343 }
344
345 static void draw_click (VikWindow *vw, GdkEventButton *event)
346 {
347   if ( event->button == 2) {
348     vw->pan_x = (gint) event->x;
349     vw->pan_y = (gint) event->y;
350   } else if ( vw->current_tool == TOOL_ZOOM )
351   {
352     vw->modified = TRUE;
353     vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
354     if ( event->button == 1 )
355       vik_viewport_zoom_in (vw->viking_vvp);
356     else if ( event->button == 3 )
357       vik_viewport_zoom_out (vw->viking_vvp);
358     draw_update ( vw );
359   }
360   else if ( vw->current_tool == TOOL_RULER )
361   {
362     struct LatLon ll;
363     VikCoord coord;
364     gchar *temp;
365     if ( event->button == 1 || event->button == 3 )
366     {
367       vik_viewport_screen_to_coord ( vw->viking_vvp, (gint) event->x, (gint) event->y, &coord );
368       vik_coord_to_latlon ( &coord, &ll );
369       if ( vw->has_oldcoord )
370          temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
371       else
372         temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
373
374       vik_statusbar_set_message ( vw->viking_vs, 3, temp );
375       g_free ( temp );
376
377       vw->oldcoord = coord;
378       /* we don't use anything else so far */
379       vw->has_oldcoord = TRUE;
380     }
381     else
382     {
383       vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
384       draw_update ( vw );
385     }
386   }
387   else if ( vw->current_tool == TOOL_LAYER )
388   {
389     vw->modified = TRUE;
390     if ( ! vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
391         vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback,
392         event, vw->viking_vvp ) )
393       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 );
394   }
395 }
396
397 static void draw_set_current_tool ( VikWindow *vw, guint mode )
398 {
399   vw->current_tool = mode;
400   draw_status ( vw );
401 }
402
403 static void draw_zoom ( VikWindow *vw, gint what )
404 {
405   switch (what)
406   {
407     case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
408     case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
409     case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
410     case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
411     default: vik_viewport_set_zoom ( vw->viking_vvp, what );
412   }
413   draw_update ( vw );
414 }
415
416 void draw_goto ( VikWindow *vw, gint mode )
417 {
418   VikCoord new_center;
419   if ( mode == 1)
420   {
421     struct LatLon ll, llold;
422     vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
423     if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
424       vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
425     else
426       return;
427   }
428   else
429   {
430     struct UTM utm, utmold;
431     vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
432     if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
433       vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
434     else
435      return;
436   }
437
438   vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
439   draw_update ( vw );
440 }
441
442 static void menu_addlayer_cb ( VikWindow *vw, gint type )
443 {
444   if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) )
445   {
446     draw_update ( vw );
447     vw->modified = TRUE;
448   }
449 }
450
451 static void menu_copy_layer_cb ( VikWindow *vw )
452 {
453   a_clipboard_copy ( vw->viking_vlp );
454 }
455
456 static void menu_cut_layer_cb ( VikWindow *vw )
457 {
458   a_clipboard_copy ( vw->viking_vlp );
459   menu_delete_layer_cb ( vw );
460 }
461
462 static void menu_paste_layer_cb ( VikWindow *vw )
463 {
464   if ( a_clipboard_paste ( vw->viking_vlp ) )
465   {
466     draw_update ( vw );
467     vw->modified = TRUE;
468   }
469 }
470
471 static void menu_properties_cb ( VikWindow *vw )
472 {
473   if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
474     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
475 }
476
477 static void menu_delete_layer_cb ( VikWindow *vw )
478 {
479   if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
480   {
481     vik_layers_panel_delete_selected ( vw->viking_vlp );
482     vw->modified = TRUE;
483   }
484   else
485     a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
486 }
487
488 static void menu_dynamic_tool_cb ( VikWindow *vw, guint id )
489 {
490   /* White Magic, my friends ... White Magic... */
491   vw->current_tool = TOOL_LAYER;
492   vw->tool_layer_id = id >> 16;
493   vw->tool_tool_id = id & 0xff;
494   draw_status ( vw );
495 }
496
497 static void window_set_filename ( VikWindow *vw, const gchar *filename )
498 {
499   gchar *title;
500   if ( vw->filename )
501     g_free ( vw->filename );
502   if ( filename == NULL )
503   {
504     vw->filename = NULL;
505     gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
506   }
507   else
508   {
509     vw->filename = g_strdup(filename);
510     title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
511     gtk_window_set_title ( GTK_WINDOW(vw), title );
512     g_free ( title );
513   }
514 }
515
516 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
517 {
518   switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
519   {
520     case 0:
521       a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
522       break;
523     case 1:
524     {
525       GtkWidget *mode_button;
526       gchar *buttonname;
527       if ( change_filename )
528         window_set_filename ( vw, filename );
529       switch ( vik_viewport_get_drawmode ( vw->viking_vvp ) ) {
530         case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/View/UTM Mode"; break;
531         case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/View/Expedia Mode"; break;
532         case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/View/Google Mode"; break;
533         default: buttonname = "/View/KH\\/Flat LatLon Mode";
534       }
535       mode_button = gtk_item_factory_get_item ( vw->item_factory, buttonname );
536       g_assert ( mode_button );
537       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. */
538       gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
539       vw->only_updating_coord_mode_ui = FALSE;
540
541       vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
542     }
543     default: draw_update ( vw );
544   }
545 }
546
547 static void load_file ( VikWindow *vw, gboolean newwindow )
548 {
549   if ( ! vw->open_dia )
550   {
551     vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
552     gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
553     gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
554     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
555   }
556   if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
557   {
558     gtk_widget_hide ( vw->open_dia );
559     if ( (vw->modified || vw->filename) && newwindow )
560       g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
561     else {
562       gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
563       gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
564       while ( *files ) {
565         vik_window_open_file ( vw, *files, change_fn );
566         files++;
567       }
568     }
569   }
570   else
571     gtk_widget_hide ( vw->open_dia );
572 }
573
574 static gboolean save_file_as ( VikWindow *vw )
575 {
576   gboolean rv = FALSE;
577   const gchar *fn;
578   if ( ! vw->save_dia )
579   {
580     vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
581     gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
582     gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
583   }
584
585   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
586   {
587     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
588     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 ) ) )
589     {
590       window_set_filename ( vw, fn );
591       rv = window_save ( vw );
592       vw->modified = FALSE;
593       break;
594     }
595   }
596   gtk_widget_hide ( vw->save_dia );
597   return rv;
598 }
599
600 static gboolean window_save ( VikWindow *vw )
601 {
602   if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
603     return TRUE;
604   else
605   {
606     a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
607     return FALSE;
608   }
609 }
610
611 static gboolean save_file ( VikWindow *vw )
612 {
613   if ( ! vw->filename )
614     return save_file_as ( vw );
615   else
616   {
617     vw->modified = FALSE;
618     return window_save ( vw );
619   }
620 }
621
622 static void clear_cb ( VikWindow *vw )
623 {
624   vik_layers_panel_clear ( vw->viking_vlp );
625   window_set_filename ( vw, NULL );
626   draw_update ( vw );
627 }
628
629 static void window_close ( VikWindow *vw )
630 {
631   if ( ! delete_event ( vw ) )
632     gtk_widget_destroy ( GTK_WIDGET(vw) );
633 }
634
635 static void zoom_to ( VikWindow *vw )
636 {
637   gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
638   if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
639   {
640     vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
641     vik_viewport_set_ympp ( vw->viking_vvp, ympp );
642     draw_update ( vw );
643   }
644 }
645
646 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
647 {
648   /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
649   GdkPixbuf *pixbuf_to_save;
650   gdouble old_xmpp, old_ympp;
651   GError *error = NULL;
652
653   /* backup old zoom & set new */
654   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
655   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
656   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
657
658   /* reset width and height: */
659   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
660
661   /* draw all layers */
662   draw_redraw ( vw );
663
664   /* save buffer as file. */
665   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);
666   gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
667   if (error)
668   {
669     fprintf(stderr, "Unable to write to file %s: %s", fn, error->message );
670     g_error_free (error);
671   }
672   g_object_unref ( G_OBJECT(pixbuf_to_save) );
673
674   /* pretend like nothing happened ;) */
675   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
676   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
677   vik_viewport_configure ( vw->viking_vvp );
678   draw_update ( vw );
679 }
680
681 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 )
682 {
683   gulong size = sizeof(gchar) * (strlen(fn) + 15);
684   gchar *name_of_file = g_malloc ( size );
685   guint x = 1, y = 1;
686   struct UTM utm_orig, utm;
687
688   /* *** copied from above *** */
689   GdkPixbuf *pixbuf_to_save;
690   gdouble old_xmpp, old_ympp;
691   GError *error = NULL;
692
693   /* backup old zoom & set new */
694   old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
695   old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
696   vik_viewport_set_zoom ( vw->viking_vvp, zoom );
697
698   /* reset width and height: do this only once for all images (same size) */
699   vik_viewport_configure_manually ( vw->viking_vvp, w, h );
700   /* *** end copy from above *** */
701
702   g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
703
704   make_dir(fn);
705
706   utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
707
708   for ( y = 1; y <= tiles_h; y++ )
709   {
710     for ( x = 1; x <= tiles_w; x++ )
711     {
712       g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, VIKING_FILE_SEP, y, x, save_as_png ? "png" : "jpg" );
713       utm = utm_orig;
714       if ( tiles_w & 0x1 )
715         utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
716       else
717         utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
718       if ( tiles_h & 0x1 ) /* odd */
719         utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
720       else /* even */
721         utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
722
723       /* move to correct place. */
724       vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
725
726       draw_redraw ( vw );
727
728       /* save buffer as file. */
729       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);
730       gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
731       if (error)
732       {
733         fprintf(stderr, "Unable to write to file %s: %s", name_of_file, error->message );
734         g_error_free (error);
735       }
736
737       g_object_unref ( G_OBJECT(pixbuf_to_save) );
738     }
739   }
740
741   vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
742   vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
743   vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
744   vik_viewport_configure ( vw->viking_vvp );
745   draw_update ( vw );
746
747   g_free ( name_of_file );
748 }
749
750 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
751 {
752   VikWindow *vw = VIK_WINDOW(pass_along[0]);
753   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
754   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
755   gdouble width_min, width_max, height_min, height_max;
756   gint width, height;
757
758   gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
759   gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
760
761   /* TODO: support for xzoom and yzoom values */
762   width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
763   height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
764
765   if ( width > width_max || width < width_min || height > height_max || height < height_min )
766     a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
767
768   gtk_spin_button_set_value ( width_spin, width );
769   gtk_spin_button_set_value ( height_spin, height );
770 }
771
772 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
773 {
774   GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
775   GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
776   gchar *label_text;
777   gdouble w, h;
778   w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
779   h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
780   if (pass_along[4]) /* save many images; find TOTAL area covered */
781   {
782     w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
783     h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
784   }
785   label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
786   gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
787   g_free ( label_text );
788 }
789
790 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
791 {
792   /* todo: default for answers inside VikWindow or static (thruout instance) */
793   GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
794                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
795                                                   GTK_STOCK_CANCEL,
796                                                   GTK_RESPONSE_REJECT,
797                                                   GTK_STOCK_OK,
798                                                   GTK_RESPONSE_ACCEPT,
799                                                   0 );
800   GtkWidget *width_label, *width_spin, *height_label, *height_spin;
801   GtkWidget *png_radio, *jpeg_radio;
802   GtkWidget *current_window_button;
803   gpointer current_window_pass_along[7];
804   GtkWidget *zoom_label, *zoom_spin;
805   GtkWidget *total_size_label;
806
807   /* only used if (!one_image_only) */
808   GtkWidget *tiles_width_spin, *tiles_height_spin;
809
810
811   width_label = gtk_label_new ( "Width (pixels):" );
812   width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
813   height_label = gtk_label_new ( "Height (pixels):" );
814   height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
815
816   zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
817   /* TODO: separate xzoom and yzoom factors */
818   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);
819
820   total_size_label = gtk_label_new ( NULL );
821
822   current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
823   current_window_pass_along [0] = vw;
824   current_window_pass_along [1] = width_spin;
825   current_window_pass_along [2] = height_spin;
826   current_window_pass_along [3] = zoom_spin;
827   current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
828   current_window_pass_along [5] = NULL;
829   current_window_pass_along [6] = total_size_label;
830   g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
831
832   png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
833   jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
834
835   if ( ! vw->draw_image_save_as_png )
836     gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
837
838   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
839   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
840   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
841   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
842   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
843   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
844   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
845   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
846   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
847
848   if ( ! one_image_only )
849   {
850     GtkWidget *tiles_width_label, *tiles_height_label;
851
852
853     tiles_width_label = gtk_label_new ( "East-west image tiles:" );
854     tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
855     tiles_height_label = gtk_label_new ( "North-south image tiles:" );
856     tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
857     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
858     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
859     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
860     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
861
862     current_window_pass_along [4] = tiles_width_spin;
863     current_window_pass_along [5] = tiles_height_spin;
864     g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
865     g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
866   }
867   gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
868   g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
869   g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
870   g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
871
872   draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
873
874   gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
875
876   if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
877   {
878     gtk_widget_hide ( GTK_WIDGET(dialog) );
879     if ( one_image_only )
880       save_image_file ( vw, fn, 
881                       vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
882                       vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
883                       gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
884                       vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
885     else {
886       if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
887         save_image_dir ( vw, fn, 
888                        vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
889                        vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
890                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
891                        vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
892                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
893                        gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
894       else
895         a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
896     }
897   }
898   gtk_widget_destroy ( GTK_WIDGET(dialog) );
899 }
900
901
902 static void draw_to_image_file_cb ( VikWindow *vw )
903 {
904   GtkWidget *file_selector;
905   const gchar *fn;
906   file_selector = gtk_file_selection_new ("Save Image");
907
908   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
909   {
910     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
911     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 ) ) )
912     {
913       gtk_widget_hide ( file_selector );
914       draw_to_image_file ( vw, fn, TRUE );
915       break;
916     }
917   }
918   gtk_widget_destroy ( file_selector );
919 }
920
921 static void draw_to_image_dir_cb ( VikWindow *vw )
922 {
923   GtkWidget *file_selector;
924   const gchar *fn;
925   file_selector = gtk_file_selection_new ("Choose a name for a new directory to hold images");
926
927   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
928   {
929     fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
930     if ( access ( fn, F_OK ) == 0 )
931       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) );
932     else
933     {
934       gtk_widget_hide ( file_selector );
935       draw_to_image_file ( vw, fn, FALSE );
936       break;
937     }
938   }
939   gtk_widget_destroy ( file_selector );
940 }
941
942 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
943 static void window_change_coord_mode ( VikWindow *vw, VikViewportDrawMode drawmode )
944 {
945   if ( !vw->only_updating_coord_mode_ui )
946   {
947     VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
948     if ( olddrawmode != drawmode )
949     {
950       /* this takes care of coord mode too */
951       vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
952       if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
953         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
954       } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
955         vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
956       }
957       draw_update ( vw );
958     }
959   }
960 }
961
962 static void set_bg_color ( VikWindow *vw )
963 {
964   GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
965   GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
966   gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
967   gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
968   if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
969   {
970     gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
971     vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
972     draw_update ( vw );
973   }
974   g_free ( color );
975   gtk_widget_destroy ( colorsd );
976 }
977
978 static GtkItemFactoryEntry menu_items[] = {
979   { "/_File", NULL, NULL, 0, "<Branch>" },
980   { "/File/_New", "<control>N", newwindow_cb, 0, "<StockItem>", GTK_STOCK_NEW }, 
981   { "/File/_Open", "<control>O", load_file, TRUE, "<StockItem>", GTK_STOCK_OPEN },
982   { "/File/A_ppend File", NULL, load_file, FALSE, "<Item>" },
983   { "/File/_Save", "<control>S", (GtkItemFactoryCallback) save_file, 0, "<StockItem>", GTK_STOCK_SAVE },
984   { "/File/Save _As", NULL, (GtkItemFactoryCallback) save_file_as, 0, "<StockItem>", GTK_STOCK_SAVE_AS },
985   { "/File/sep1", NULL, NULL, 0, "<Separator>" },
986   { "/File/_Generate Image File", NULL, draw_to_image_file_cb, 0, "<Item>" },
987   { "/File/Generate Directory of Images", NULL, draw_to_image_dir_cb, 0, "<Item>" },
988   { "/File/sep1", NULL, NULL, 0, "<Separator>" },
989   { "/File/_Close", "<CTRL>W", window_close, 0, "<StockItem>", GTK_STOCK_QUIT },
990   { "/_View", NULL, NULL, 0, "<Branch>" },
991   { "/View/_UTM Mode", "<ctrl>u", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_UTM, "<RadioItem>" },
992   { "/View/_Expedia Mode", "<ctrl>e", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_EXPEDIA, "/View/UTM Mode" },
993   { "/View/_Google Mode", "<ctrl>g", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_GOOGLE, "/View/UTM Mode" },
994   { "/View/_KH\\/Flat LatLon Mode", "<ctrl>k", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_KH, "/View/UTM Mode" },
995   { "/View/_Mercator (New Google)", "<ctrl>m", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_MERCATOR, "/View/UTM Mode" },
996   { "/View/sep1", NULL, NULL, 0, "<Separator>" },
997   { "/View/_Go to Lat\\/Lon...", NULL, draw_goto, 1, "<Item>" },
998   { "/View/Go to UTM...", NULL, draw_goto, 2, "<Item>" },
999   { "/View/sep1", NULL, NULL, 0, "<Separator>" },
1000   { "/View/Set Background Color...", NULL, set_bg_color, 0, "<StockItem>", GTK_STOCK_SELECT_COLOR },
1001   { "/View/sep1", NULL, NULL, 0, "<Separator>" },
1002   { "/View/Zoom _In", "<ctrl>plus", draw_zoom, -3, "<StockItem>", GTK_STOCK_ZOOM_IN },
1003   { "/View/Zoom _Out", "<ctrl>minus", draw_zoom, -4, "<StockItem>", GTK_STOCK_ZOOM_OUT },
1004   { "/View/Zoom To...", "<ctrl><shift>Z", zoom_to, 0, "<Item>" },
1005   { "/View/_Zoom", NULL, NULL, 0, "<Branch>" },
1006   { "/View/Zoom/0.25", "<ctrl>1", draw_zoom, -2, "<Item>" },
1007   { "/View/Zoom/0.5", "<ctrl>2", draw_zoom, -1, "<Item>" },
1008   { "/View/Zoom/1", "<ctrl>3", draw_zoom, 1, "<Item>" },
1009   { "/View/Zoom/2", "<ctrl>4", draw_zoom, 2, "<Item>" },
1010   { "/View/Zoom/4", "<ctrl>5", draw_zoom, 4, "<Item>" },
1011   { "/View/Zoom/8", "<ctrl>6", draw_zoom, 8, "<Item>" },
1012   { "/View/Zoom/16", "<ctrl>7", draw_zoom, 16, "<Item>" },
1013   { "/View/Zoom/32", "<ctrl>8", draw_zoom, 32, "<Item>" },
1014   { "/View/Zoom/64", "<ctrl>9", draw_zoom, 64, "<Item>" },
1015   { "/View/Zoom/128", "<ctrl>0", draw_zoom, 64, "<Item>" },
1016   { "/View/sep1", NULL, NULL, 0, "<Separator>" },
1017   { "/View/Background _Jobs...", "<ctrl>j", (GtkItemFactoryCallback) a_background_show_window, 0, "<Item>" },
1018
1019   { "/_Layers", NULL, NULL, 0, "<Branch>" },
1020   { "/Layers/Cu_t", NULL, menu_cut_layer_cb, -1, "<StockItem>", GTK_STOCK_CUT },
1021   { "/Layers/_Copy", NULL, menu_copy_layer_cb, -1, "<StockItem>", GTK_STOCK_COPY },
1022   { "/Layers/_Paste", NULL, menu_paste_layer_cb, -1, "<StockItem>", GTK_STOCK_PASTE },
1023   { "/Layers/sep1", NULL, NULL, 0, "<Separator>" },
1024   { "/Layers/_Properties", NULL, menu_properties_cb, -1, "<StockItem>", GTK_STOCK_PROPERTIES },
1025   { "/Layers/_Delete", NULL, menu_delete_layer_cb, -1, "<StockItem>", GTK_STOCK_DELETE },
1026   { "/Layers/Delete All", NULL, clear_cb, 0, "<StockItem>", GTK_STOCK_CLEAR },
1027   { "/Layers/sep1", NULL, NULL, 0, "<Separator>" },
1028   /* Plus Dynamic */
1029
1030   { "/_Tools", NULL, NULL, 0, "<Branch>" },
1031   { "/Tools/sep1", NULL, NULL, 0, "<Tearoff>" },
1032   { "/Tools/_Zoom", "<ctrl><shift>Z", draw_set_current_tool, TOOL_ZOOM, "<Item>" },
1033   { "/Tools/_Ruler", "<ctrl><shift>R", draw_set_current_tool, TOOL_RULER, "<Item>" },
1034   /* Plus Dynamic */
1035 };
1036
1037 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
1038 static GtkItemFactoryEntry tool_sep = { "/_Tools/sep1", NULL, NULL, 0, "<Separator>" };
1039
1040 static GtkWidget *window_create_menubar( VikWindow *window )
1041 {
1042   GtkItemFactory *item_factory;
1043   GtkAccelGroup *accel_group;
1044   GtkItemFactoryEntry add_layer_item;
1045   guint i, j, tmp;
1046
1047   g_assert ( sizeof(guint16)*2 <= sizeof(guint) ); /* FIXME: should be a compiler warning */
1048
1049   accel_group = gtk_accel_group_new ();
1050   item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
1051                                        accel_group);
1052   gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, window);
1053   gtk_window_add_accel_group ( GTK_WINDOW(window), accel_group);
1054
1055   add_layer_item.accelerator = NULL;
1056
1057   for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
1058   {
1059     /* TODO: FIXME: if name has a '/' in it it will get all messed up. why not have an itemfactory field with
1060                     name, serialized icon, shortcut, etc.? */
1061     add_layer_item.path = g_strdup_printf("/_Layers/New %s Layer", vik_layer_get_interface(i)->name );
1062     add_layer_item.callback = menu_addlayer_cb;
1063     add_layer_item.callback_action = i;
1064     if ( vik_layer_get_interface(i)->icon )
1065     {
1066       add_layer_item.item_type = "<ImageItem>";
1067       add_layer_item.extra_data = gdk_pixdata_serialize ( vik_layer_get_interface(i)->icon, &tmp );
1068     }
1069     else
1070       add_layer_item.item_type = "<Item>";
1071     gtk_item_factory_create_item ( item_factory, &add_layer_item, window, 1 );
1072     g_free ( add_layer_item.path );
1073     g_free ( (gpointer) add_layer_item.extra_data );
1074
1075     if ( vik_layer_get_interface(i)->tools_count )
1076       gtk_item_factory_create_item ( item_factory, &tool_sep, window, 1 );
1077
1078     for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ )
1079     {
1080       add_layer_item.path = g_strdup_printf("/_Tools/%s", vik_layer_get_interface(i)->tools[j].name);
1081       add_layer_item.callback = menu_dynamic_tool_cb;
1082       add_layer_item.callback_action = ( i << 16 ) | j;
1083       add_layer_item.item_type = "<Item>";
1084       gtk_item_factory_create_item ( item_factory, &add_layer_item, window, 1 );
1085       g_free ( add_layer_item.path );
1086     }
1087   }
1088
1089   window->item_factory = item_factory;
1090   return gtk_item_factory_get_widget (item_factory, "<main>");
1091 }