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