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