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