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