]> git.street.me.uk Git - andy/viking.git/blame - src/vikwindow.c
Reduce GObject cast
[andy/viking.git] / src / vikwindow.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
a482007a 5 * Copyright (C) 2005-2006, Alex Foobarian <foobarian@gmail.com>
50a14534
EB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
4c77d5e0
GB
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
50a14534
EB
27#include "viking.h"
28#include "background.h"
1d1bc3c1 29#include "acquire.h"
7b3479e3 30#include "datasources.h"
34e71b99 31#include "vikgoto.h"
071da616 32#include "dems.h"
7c259702 33#include "mapcache.h"
42f34743 34#include "print.h"
17a1f8f9 35#include "preferences.h"
f2f2f7bf 36#include "icons/icons.h"
92806042 37#include "vikexttools.h"
9be0449f 38#include "garminsymbols.h"
50a14534 39
8c00358d 40#ifdef HAVE_STDLIB_H
e4afc73a 41#include <stdlib.h>
8c00358d
GB
42#endif
43#ifdef HAVE_MATH_H
50a14534 44#include <math.h>
8c00358d
GB
45#endif
46#ifdef HAVE_STRING_H
50a14534 47#include <string.h>
8c00358d 48#endif
50a14534 49#include <ctype.h>
f83131b9
MA
50#include <glib.h>
51#include <glib/gstdio.h>
1d1bc3c1 52#include <glib/gprintf.h>
4c77d5e0 53#include <glib/gi18n.h>
314084b8 54#include <gio/gio.h>
7622022f 55#include <gdk/gdkkeysyms.h>
50a14534 56
3570ad57
QT
57#define VIKING_WINDOW_WIDTH 1000
58#define VIKING_WINDOW_HEIGHT 800
50a14534
EB
59#define DRAW_IMAGE_DEFAULT_WIDTH 1280
60#define DRAW_IMAGE_DEFAULT_HEIGHT 1024
61#define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
62
63static void window_finalize ( GObject *gob );
64static GObjectClass *parent_class;
65
66static void window_init ( VikWindow *vw );
67static void window_class_init ( VikWindowClass *klass );
4c77d5e0 68static void window_set_filename ( VikWindow *vw, const gchar *filename );
50a14534
EB
69
70static void draw_update ( VikWindow *vw );
71
e4afc73a 72static void newwindow_cb ( GtkAction *a, VikWindow *vw );
50a14534
EB
73
74/* Drawing & stuff */
75
76static gboolean delete_event( VikWindow *vw );
77
777e2d4d
EB
78static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
79
bce3a7b0 80static void window_configure_event ( VikWindow *vw );
50a14534
EB
81static void draw_sync ( VikWindow *vw );
82static void draw_redraw ( VikWindow *vw );
941aa6e9 83static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
50a14534
EB
84static void draw_click ( VikWindow *vw, GdkEventButton *event );
85static void draw_release ( VikWindow *vw, GdkEventButton *event );
86static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
e4afc73a
EB
87static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
88static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
50a14534
EB
89
90static void draw_status ();
91
92/* End Drawing Functions */
93
e4afc73a
EB
94static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
95static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
96static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
941aa6e9
AF
97
98/* tool management */
99typedef struct {
100 VikToolInterface ti;
101 gpointer state;
9593a4c9 102 gint layer_type;
941aa6e9 103} toolbox_tool_t;
9593a4c9 104#define TOOL_LAYER_TYPE_NONE -1
941aa6e9
AF
105
106typedef struct {
107 int active_tool;
108 int n_tools;
109 toolbox_tool_t *tools;
110 VikWindow *vw;
111} toolbox_tools_t;
112
e4afc73a 113static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
941aa6e9 114static toolbox_tools_t* toolbox_create(VikWindow *vw);
9593a4c9 115static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
941aa6e9
AF
116static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
117static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
f2f2f7bf 118static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name);
941aa6e9 119static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
dc2c040e 120static void toolbox_move (toolbox_tools_t *vt, GdkEventMotion *event);
941aa6e9
AF
121static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
122
50a14534 123
941aa6e9 124/* ui creation */
e4afc73a 125static void window_create_ui( VikWindow *window );
941aa6e9 126static void register_vik_icons (GtkIconFactory *icon_factory);
50a14534 127
941aa6e9 128/* i/o */
e4afc73a
EB
129static void load_file ( GtkAction *a, VikWindow *vw );
130static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
131static gboolean save_file ( GtkAction *a, VikWindow *vw );
2bf7cadd 132static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
50a14534
EB
133static gboolean window_save ( VikWindow *vw );
134
135struct _VikWindow {
136 GtkWindow gtkwindow;
137 VikViewport *viking_vvp;
138 VikLayersPanel *viking_vlp;
139 VikStatusbar *viking_vs;
140
e4afc73a
EB
141 GtkToolbar *toolbar;
142
50a14534
EB
143 GtkItemFactory *item_factory;
144
941aa6e9 145 /* tool management state */
50a14534 146 guint current_tool;
941aa6e9 147 toolbox_tools_t *vt;
50a14534
EB
148 guint16 tool_layer_id;
149 guint16 tool_tool_id;
150
79845167
QT
151 GtkActionGroup *action_group;
152
b71eff77 153 gboolean pan_move;
50a14534
EB
154 gint pan_x, pan_y;
155
156 guint draw_image_width, draw_image_height;
157 gboolean draw_image_save_as_png;
158
159 gchar *filename;
160 gboolean modified;
161
162 GtkWidget *open_dia, *save_dia;
f2a1ca71 163 GtkWidget *save_img_dia, *save_img_dir_dia;
50a14534
EB
164
165 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
e4afc73a 166 GtkUIManager *uim;
c9177aae
QT
167
168 /* half-drawn update */
169 VikLayer *trigger;
170 VikCoord trigger_center;
50a14534
EB
171};
172
173enum {
576cbd17
GB
174 TOOL_PAN = 0,
175 TOOL_ZOOM,
50a14534
EB
176 TOOL_RULER,
177 TOOL_LAYER,
178 NUMBER_OF_TOOLS
179};
180
181enum {
182 VW_NEWWINDOW_SIGNAL,
183 VW_OPENWINDOW_SIGNAL,
184 VW_LAST_SIGNAL
185};
186
187static guint window_signals[VW_LAST_SIGNAL] = { 0 };
188
576cbd17 189static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Pan"), N_("Zoom"), N_("Ruler") };
50a14534
EB
190
191GType vik_window_get_type (void)
192{
193 static GType vw_type = 0;
194
195 if (!vw_type)
196 {
197 static const GTypeInfo vw_info =
198 {
199 sizeof (VikWindowClass),
200 NULL, /* base_init */
201 NULL, /* base_finalize */
202 (GClassInitFunc) window_class_init, /* class_init */
203 NULL, /* class_finalize */
204 NULL, /* class_data */
205 sizeof (VikWindow),
206 0,
207 (GInstanceInitFunc) window_init,
208 };
209 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
210 }
211
212 return vw_type;
213}
214
014128f6
QT
215VikViewport * vik_window_viewport(VikWindow *vw)
216{
217 return(vw->viking_vvp);
218}
219
79845167
QT
220void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
221{
222 int i, j, tool_count;
223 VikLayerInterface *layer_interface;
224
225 if (!vw->action_group) return;
226
227 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
228 GtkAction *action;
229 layer_interface = vik_layer_get_interface(i);
230 tool_count = layer_interface->tools_count;
231
232 for (j = 0; j < tool_count; j++) {
233 action = gtk_action_group_get_action(vw->action_group,
234 layer_interface->tools[j].name);
235 g_object_set(action, "sensitive", i == vl->type, NULL);
236 }
237 }
238}
239
50a14534
EB
240static void window_finalize ( GObject *gob )
241{
242 VikWindow *vw = VIK_WINDOW(gob);
243 g_return_if_fail ( vw != NULL );
244
245 a_background_remove_status ( vw->viking_vs );
246
247 G_OBJECT_CLASS(parent_class)->finalize(gob);
248}
249
bce3a7b0 250
50a14534
EB
251static void window_class_init ( VikWindowClass *klass )
252{
253 /* destructor */
254 GObjectClass *object_class;
255
256 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);
8c4f1350 257 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);
50a14534
EB
258
259 object_class = G_OBJECT_CLASS (klass);
260
261 object_class->finalize = window_finalize;
262
263 parent_class = g_type_class_peek_parent (klass);
264
265}
266
267static void window_init ( VikWindow *vw )
268{
269 GtkWidget *main_vbox;
270 GtkWidget *hpaned;
271
79845167 272 vw->action_group = NULL;
50a14534 273
24277274 274 vw->viking_vvp = vik_viewport_new();
50a14534
EB
275 vw->viking_vlp = vik_layers_panel_new();
276 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
277 vw->viking_vs = vik_statusbar_new();
278
941aa6e9
AF
279 vw->vt = toolbox_create(vw);
280 window_create_ui(vw);
4c77d5e0 281 window_set_filename (vw, NULL);
64e3d6c9
RN
282 vw->toolbar = GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"));
283
576cbd17 284 toolbox_activate(vw->vt, "Pan");
941aa6e9 285
50a14534 286 vw->filename = NULL;
e4afc73a 287 vw->item_factory = NULL;
50a14534 288
50a14534
EB
289 vw->modified = FALSE;
290 vw->only_updating_coord_mode_ui = FALSE;
b71eff77
JJ
291
292 vw->pan_move = FALSE;
50a14534
EB
293 vw->pan_x = vw->pan_y = -1;
294 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
295 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
296 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
297
298 main_vbox = gtk_vbox_new(FALSE, 1);
299 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
300
e4afc73a 301 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
64e3d6c9
RN
302 gtk_box_pack_start (GTK_BOX(main_vbox), GTK_WIDGET(vw->toolbar), FALSE, TRUE, 0);
303 gtk_toolbar_set_icon_size (vw->toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
304 gtk_toolbar_set_style (vw->toolbar, GTK_TOOLBAR_ICONS);
50a14534 305
92806042 306 vik_ext_tools_add_menu_items ( vw, vw->uim );
777e2d4d 307
50a14534
EB
308 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
309
310 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
bce3a7b0 311 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
fe27d6d2 312 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK );
50a14534
EB
313 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
314 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
315 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
316 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
317 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
318
165d30aa 319 g_signal_connect_swapped (G_OBJECT (vw->viking_vvp), "key_press_event", G_CALLBACK (key_press_event), vw);
777e2d4d 320
9a3cdf12 321 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
50a14534
EB
322
323 hpaned = gtk_hpaned_new ();
181f5d0c
MA
324 gtk_paned_pack1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp), FALSE, FALSE );
325 gtk_paned_pack2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp), TRUE, TRUE );
50a14534
EB
326
327 /* This packs the button into the window (a gtk container). */
328 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
329
330 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
331
332 a_background_add_status(vw->viking_vs);
333
334 vw->open_dia = NULL;
335 vw->save_dia = NULL;
f2a1ca71
QT
336 vw->save_img_dia = NULL;
337 vw->save_img_dir_dia = NULL;
50a14534
EB
338}
339
340VikWindow *vik_window_new ()
341{
342 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
343}
344
777e2d4d
EB
345static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data )
346{
347 VikLayer *vl = vik_layers_panel_get_selected ( vw->viking_vlp );
348 if (vl && vw->vt->active_tool != -1 && vw->vt->tools[vw->vt->active_tool].ti.key_press ) {
349 gint ltype = vw->vt->tools[vw->vt->active_tool].layer_type;
350 if ( vl && ltype == vl->type )
351 return vw->vt->tools[vw->vt->active_tool].ti.key_press(vl, event, vw->vt->tools[vw->vt->active_tool].state);
352 }
7622022f
RN
353
354 /* Restore Main Menu via Escape key if the user has hidden it */
355 /* This key is more likely to be used as they may not remember the function key */
356 if ( event->keyval == GDK_Escape ) {
48df6aa3 357 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewMainMenu" );
7622022f
RN
358 if ( check_box ) {
359 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
360 if ( !state ) {
361 gtk_widget_show ( gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu" ) );
362 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(check_box), TRUE );
363 return TRUE; /* handled keypress */
364 }
365 }
366 }
367
777e2d4d
EB
368 return FALSE; /* don't handle the keypress */
369}
370
50a14534
EB
371static gboolean delete_event( VikWindow *vw )
372{
a5fd2196 373#ifdef VIKING_PROMPT_IF_MODIFIED
50a14534 374 if ( vw->modified )
a5fd2196
QT
375#else
376 if (0)
377#endif
50a14534
EB
378 {
379 GtkDialog *dia;
380 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
4c77d5e0
GB
381 _("Do you want to save the changes you made to the document \"%s\"?\n"
382 "\n"
383 "Your changes will be lost if you don't save them."),
384 vw->filename ? a_file_basename ( vw->filename ) : _("Untitled") ) );
385 gtk_dialog_add_buttons ( dia, _("Don't Save"), GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
50a14534
EB
386 switch ( gtk_dialog_run ( dia ) )
387 {
388 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
389 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
e4afc73a 390 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
50a14534
EB
391 }
392 }
393 return FALSE;
394}
395
396/* Drawing stuff */
e4afc73a 397static void newwindow_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
398{
399 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
400}
401
402static void draw_update ( VikWindow *vw )
403{
404 draw_redraw (vw);
405 draw_sync (vw);
406}
407
408static void draw_sync ( VikWindow *vw )
409{
410 vik_viewport_sync(vw->viking_vvp);
411 draw_status ( vw );
412 /* other things may be necc here later. */
413}
414
415static void draw_status ( VikWindow *vw )
416{
417 static gchar zoom_level[22];
82940cf6
GB
418 gdouble xmpp = vik_viewport_get_xmpp (vw->viking_vvp);
419 gdouble ympp = vik_viewport_get_ympp(vw->viking_vvp);
420 gchar *unit = vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM ? _("mpp") : _("pixelfact");
421 if (xmpp != ympp)
422 g_snprintf ( zoom_level, 22, "%.3f/%.3f %s", xmpp, ympp, unit );
423 else
fb5e99bb
RN
424 if ( (int)xmpp - xmpp < 0.0 )
425 g_snprintf ( zoom_level, 22, "%.3f %s", xmpp, unit );
426 else
427 /* xmpp should be a whole number so don't show useless .000 bit */
428 g_snprintf ( zoom_level, 22, "%d %s", (int)xmpp, unit );
50a14534
EB
429 if ( vw->current_tool == TOOL_LAYER )
430 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
431 else
4c77d5e0 432 vik_statusbar_set_message ( vw->viking_vs, 0, _(tool_names[vw->current_tool]) );
50a14534
EB
433
434 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
435}
436
c9177aae
QT
437void vik_window_set_redraw_trigger(VikLayer *vl)
438{
730a38c1 439 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
be3b5803
GB
440 if (NULL != vw)
441 vw->trigger = vl;
c9177aae
QT
442}
443
bce3a7b0
EB
444static void window_configure_event ( VikWindow *vw )
445{
f2f2f7bf 446 static int first = 1;
bce3a7b0 447 draw_redraw ( vw );
372132a6
GB
448 if (first) {
449 // This is a hack to set the cursor corresponding to the first tool
450 // FIXME find the correct way to initialize both tool and its cursor
f2f2f7bf 451 const GdkCursor *cursor = NULL;
372132a6 452 first = 0;
f2f2f7bf
GB
453 cursor = toolbox_get_cursor(vw->vt, "Pan");
454 /* We set cursor, even if it is NULL: it resets to default */
9b5dcb38 455 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, (GdkCursor *)cursor );
372132a6 456 }
bce3a7b0
EB
457}
458
50a14534
EB
459static void draw_redraw ( VikWindow *vw )
460{
c9177aae
QT
461 VikCoord old_center = vw->trigger_center;
462 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
463 VikLayer *new_trigger = vw->trigger;
464 vw->trigger = NULL;
0df66d57
EB
465 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
466
467 if ( ! new_trigger )
468 ; /* do nothing -- have to redraw everything. */
07c9d0bf 469 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) || (new_trigger->type == VIK_LAYER_AGGREGATE) )
0df66d57
EB
470 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
471 else
472 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
473
474 /* actually draw */
50a14534 475 vik_viewport_clear ( vw->viking_vvp);
50a14534 476 vik_layers_panel_draw_all ( vw->viking_vlp );
acaf7113 477 vik_viewport_draw_scale ( vw->viking_vvp );
82aa018d 478 vik_viewport_draw_copyright ( vw->viking_vvp );
c933487f 479 vik_viewport_draw_centermark ( vw->viking_vvp );
0df66d57
EB
480
481 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
50a14534
EB
482}
483
941aa6e9
AF
484gboolean draw_buf_done = TRUE;
485
486static gboolean draw_buf(gpointer data)
487{
488 gpointer *pass_along = data;
489 gdk_threads_enter();
490 gdk_draw_drawable (pass_along[0], pass_along[1],
491 pass_along[2], 0, 0, 0, 0, -1, -1);
492 draw_buf_done = TRUE;
493 gdk_threads_leave();
494 return FALSE;
495}
496
497
498/* Mouse event handlers ************************************************************************/
499
b57126a3
GB
500static void vik_window_pan_click (VikWindow *vw, GdkEventButton *event)
501{
502 /* set panning origin */
b71eff77 503 vw->pan_move = FALSE;
b57126a3
GB
504 vw->pan_x = (gint) event->x;
505 vw->pan_y = (gint) event->y;
506}
507
941aa6e9
AF
508static void draw_click (VikWindow *vw, GdkEventButton *event)
509{
165d30aa 510 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
941aa6e9
AF
511
512 /* middle button pressed. we reserve all middle button and scroll events
513 * for panning and zooming; tools only get left/right/movement
514 */
515 if ( event->button == 2) {
b57126a3 516 vik_window_pan_click ( vw, event );
941aa6e9
AF
517 }
518 else {
519 toolbox_click(vw->vt, event);
520 }
521}
522
b57126a3
GB
523static void vik_window_pan_move (VikWindow *vw, GdkEventMotion *event)
524{
525 if ( vw->pan_x != -1 ) {
b71eff77
JJ
526 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
527 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
b71eff77
JJ
528 vw->pan_move = TRUE;
529 vw->pan_x = event->x;
530 vw->pan_y = event->y;
a7abaae5 531 draw_update ( vw );
b57126a3
GB
532 }
533}
534
941aa6e9
AF
535static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
536{
537 static VikCoord coord;
538 static struct UTM utm;
539 static struct LatLon ll;
a58aaed4
GB
540 #define BUFFER_SIZE 50
541 static char pointer_buf[BUFFER_SIZE];
542 gchar *lat = NULL, *lon = NULL;
071da616 543 gint16 alt;
228213c5
QT
544 gdouble zoom;
545 VikDemInterpol interpol_method;
941aa6e9 546
fe27d6d2
SB
547 /* This is a hack, but work far the best, at least for single pointer systems.
548 * See http://bugzilla.gnome.org/show_bug.cgi?id=587714 for more. */
549 gint x, y;
550 gdk_window_get_pointer (event->window, &x, &y, NULL);
551 event->x = x;
552 event->y = y;
553
576cbd17 554 toolbox_move(vw->vt, event);
941aa6e9
AF
555
556 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
557 vik_coord_to_utm ( &coord, &utm );
558 a_coords_utm_to_latlon ( &utm, &ll );
a58aaed4 559 a_coords_latlon_to_string ( &ll, &lat, &lon );
228213c5
QT
560 /* Change interpolate method according to scale */
561 zoom = vik_viewport_get_zoom(vw->viking_vvp);
562 if (zoom > 2.0)
563 interpol_method = VIK_DEM_INTERPOL_NONE;
564 else if (zoom >= 1.0)
565 interpol_method = VIK_DEM_INTERPOL_SIMPLE;
566 else
567 interpol_method = VIK_DEM_INTERPOL_BEST;
8c5f013a
RN
568 if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION) {
569 if ( a_vik_get_units_height () == VIK_UNITS_HEIGHT_METRES )
570 g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s %dm"), lat, lon, alt );
571 else
6c20e59a 572 g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s %dft"), lat, lon, (int)VIK_METERS_TO_FEET(alt) );
8c5f013a 573 }
071da616 574 else
a58aaed4
GB
575 g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s"), lat, lon );
576 g_free (lat);
577 lat = NULL;
578 g_free (lon);
579 lon = NULL;
941aa6e9
AF
580 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
581
b57126a3 582 vik_window_pan_move ( vw, event );
fe27d6d2
SB
583
584 /* This is recommended by the GTK+ documentation, but does not work properly.
585 * Use deprecated way until GTK+ gets a solution for correct motion hint handling:
586 * http://bugzilla.gnome.org/show_bug.cgi?id=587714
587 */
588 /* gdk_event_request_motions ( event ); */
b57126a3
GB
589}
590
591static void vik_window_pan_release ( VikWindow *vw, GdkEventButton *event )
592{
b71eff77 593 if ( vw->pan_move == FALSE )
b57126a3
GB
594 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
595 else
596 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
597 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
a7abaae5 598 vw->pan_move = FALSE;
b57126a3 599 vw->pan_x = vw->pan_y = -1;
a7abaae5 600 draw_update ( vw );
941aa6e9
AF
601}
602
603static void draw_release ( VikWindow *vw, GdkEventButton *event )
604{
165d30aa
EB
605 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
606
941aa6e9 607 if ( event->button == 2 ) { /* move / pan */
b57126a3 608 vik_window_pan_release(vw, event);
941aa6e9
AF
609 }
610 else {
611 toolbox_release(vw->vt, event);
612 }
613}
614
615static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
616{
d1a45556
JN
617 guint modifiers = event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK);
618 if ( modifiers == GDK_CONTROL_MASK ) {
8c721f83
EB
619 /* control == pan up & down */
620 if ( event->direction == GDK_SCROLL_UP )
621 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
622 else
623 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 );
d1a45556
JN
624 } else if ( modifiers == GDK_SHIFT_MASK ) {
625 /* shift == pan left & right */
8c721f83
EB
626 if ( event->direction == GDK_SCROLL_UP )
627 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
628 else
629 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 );
d1a45556 630 } else if ( modifiers == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
3c575a4a
JN
631 /* control+shift == make sure mouse is still over the same point on the map when we zoom */
632 VikCoord coord;
633 gint x, y;
634 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
635 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
636 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
637 if ( event->direction == GDK_SCROLL_UP )
8c721f83 638 vik_viewport_zoom_in (vw->viking_vvp);
3c575a4a 639 else
8c721f83 640 vik_viewport_zoom_out(vw->viking_vvp);
3c575a4a
JN
641 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
642 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
643 center_y + (y - event->y) );
e086b16d
EB
644 } else {
645 if ( event->direction == GDK_SCROLL_UP )
646 vik_viewport_zoom_in (vw->viking_vvp);
647 else
648 vik_viewport_zoom_out (vw->viking_vvp);
8c721f83
EB
649 }
650
941aa6e9
AF
651 draw_update(vw);
652}
653
654
655
656/********************************************************************************
657 ** Ruler tool code
658 ********************************************************************************/
e4847ce9
AF
659static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
660{
661 PangoFontDescription *pfd;
662 PangoLayout *pl;
663 gchar str[128];
0dff88ea
AF
664 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
665 GdkGC *thickgc = gdk_gc_new(d);
666
e4847ce9
AF
667 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
668 gdouble dx = (x2-x1)/len*10;
669 gdouble dy = (y2-y1)/len*10;
670 gdouble c = cos(15.0 * M_PI/180.0);
671 gdouble s = sin(15.0 * M_PI/180.0);
0dff88ea 672 gdouble angle;
15614495 673 gdouble baseangle = 0;
0dff88ea 674 gint i;
e4847ce9 675
0dff88ea 676 /* draw line with arrow ends */
d9ffd267
EB
677 {
678 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
679 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
680 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
681 }
682
15614495
AF
683 a_viewport_clip_line(&x1, &y1, &x2, &y2);
684 gdk_draw_line(d, gc, x1, y1, x2, y2);
d9ffd267 685
e4847ce9
AF
686 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
687 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
688 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
689 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
690 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
691 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
692
0dff88ea
AF
693 /* draw compass */
694#define CR 80
695#define CW 4
15614495
AF
696 angle = atan2(dy, dx) + M_PI_2;
697
698 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
699 VikCoord test;
700 struct LatLon ll;
701 struct UTM u;
702 gint tx, ty;
703
704 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
705 vik_coord_to_latlon ( &test, &ll );
706 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
707 a_coords_latlon_to_utm ( &ll, &u );
708 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
709 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
710
711 baseangle = M_PI - atan2(tx-x1, ty-y1);
712 angle -= baseangle;
713 }
714
0dff88ea
AF
715 if (angle<0)
716 angle+=2*M_PI;
717 if (angle>2*M_PI)
718 angle-=2*M_PI;
719
720 {
721 GdkColor color;
722 gdk_gc_copy(thickgc, gc);
723 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
724 gdk_color_parse("#2255cc", &color);
725 gdk_gc_set_rgb_fg_color(thickgc, &color);
726 }
15614495 727 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);
0dff88ea 728
e4847ce9 729
0dff88ea
AF
730 gdk_gc_copy(thickgc, gc);
731 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
732 for (i=0; i<180; i++) {
15614495
AF
733 c = cos(i*M_PI/90.0 + baseangle);
734 s = sin(i*M_PI/90.0 + baseangle);
e4847ce9 735
0dff88ea
AF
736 if (i%5) {
737 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
738 } else {
739 gdouble ticksize = 2*CW;
740 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
741 }
e4847ce9 742 }
0dff88ea
AF
743
744 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
745 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
746 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
15614495
AF
747 c = (CR+CW*2)*cos(baseangle);
748 s = (CR+CW*2)*sin(baseangle);
749 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
750 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
0dff88ea
AF
751
752 /* draw labels */
753#define LABEL(x, y, w, h) { \
754 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
755 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
756 gdk_draw_layout(d, gc, (x), (y), pl); }
757 {
758 gint wd, hd, xd, yd;
759 gint wb, hb, xb, yb;
760
761 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
762
763 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
764 pango_layout_set_font_description (pl, pfd);
765 pango_font_description_free (pfd);
766
767 pango_layout_set_text(pl, "N", -1);
768 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
769
770 /* draw label with distance */
6f9336aa
RN
771 vik_units_distance_t dist_units = a_vik_get_units_distance ();
772 switch (dist_units) {
773 case VIK_UNITS_DISTANCE_KILOMETRES:
774 if (distance >= 1000 && distance < 100000) {
775 g_sprintf(str, "%3.2f km", distance/1000.0);
776 } else if (distance < 1000) {
777 g_sprintf(str, "%d m", (int)distance);
778 } else {
779 g_sprintf(str, "%d km", (int)distance/1000);
780 }
781 break;
782 case VIK_UNITS_DISTANCE_MILES:
433b3f7f
RN
783 if (distance >= VIK_MILES_TO_METERS(1) && distance < VIK_MILES_TO_METERS(100)) {
784 g_sprintf(str, "%3.2f miles", VIK_METERS_TO_MILES(distance));
785 } else if (distance < VIK_MILES_TO_METERS(1)) {
6f9336aa
RN
786 g_sprintf(str, "%d yards", (int)(distance*1.0936133));
787 } else {
433b3f7f 788 g_sprintf(str, "%d miles", (int)VIK_METERS_TO_MILES(distance));
6f9336aa
RN
789 }
790 break;
791 default:
792 g_critical("Houston, we've had a problem. distance=%d", dist_units);
0dff88ea 793 }
6f9336aa 794
0dff88ea
AF
795 pango_layout_set_text(pl, str, -1);
796
797 pango_layout_get_pixel_size ( pl, &wd, &hd );
798 if (dy>0) {
799 xd = (x1+x2)/2 + dy;
800 yd = (y1+y2)/2 - hd/2 - dx;
801 } else {
802 xd = (x1+x2)/2 - dy;
803 yd = (y1+y2)/2 - hd/2 + dx;
804 }
024f32c1
EB
805
806 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
807 xd = x2 + 10;
808 yd = y2 - 5;
809 }
810
0dff88ea
AF
811 LABEL(xd, yd, wd, hd);
812
813 /* draw label with bearing */
15614495 814 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
0dff88ea
AF
815 pango_layout_set_text(pl, str, -1);
816 pango_layout_get_pixel_size ( pl, &wb, &hb );
817 xb = x1 + CR*cos(angle-M_PI_2);
818 yb = y1 + CR*sin(angle-M_PI_2);
819
024f32c1
EB
820 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
821 xb = x2 + 10;
822 yb = y2 + 10;
823 }
824
0dff88ea
AF
825 {
826 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
827 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
828 xb = xd + wd + 5;
829 }
830 }
831 LABEL(xb, yb, wb, hb);
e4847ce9 832 }
03d62e57 833#undef LABEL
e4847ce9 834
024f32c1 835 g_object_unref ( G_OBJECT ( pl ) );
0dff88ea
AF
836 g_object_unref ( G_OBJECT ( labgc ) );
837 g_object_unref ( G_OBJECT ( thickgc ) );
e4847ce9
AF
838}
839
941aa6e9
AF
840typedef struct {
841 VikWindow *vw;
842 VikViewport *vvp;
843 gboolean has_oldcoord;
844 VikCoord oldcoord;
845} ruler_tool_state_t;
03d62e57 846
941aa6e9 847static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
03d62e57 848{
941aa6e9
AF
849 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
850 s->vw = vw;
851 s->vvp = vvp;
852 s->has_oldcoord = FALSE;
853 return s;
03d62e57
AF
854}
855
941aa6e9 856static void ruler_destroy (ruler_tool_state_t *s)
50a14534 857{
941aa6e9
AF
858 g_free(s);
859}
50a14534 860
941aa6e9
AF
861static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
862{
863 struct LatLon ll;
864 VikCoord coord;
865 gchar *temp;
866 if ( event->button == 1 ) {
a58aaed4 867 gchar *lat=NULL, *lon=NULL;
941aa6e9
AF
868 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
869 vik_coord_to_latlon ( &coord, &ll );
a58aaed4 870 a_coords_latlon_to_string ( &ll, &lat, &lon );
941aa6e9 871 if ( s->has_oldcoord ) {
6f9336aa
RN
872 vik_units_distance_t dist_units = a_vik_get_units_distance ();
873 switch (dist_units) {
874 case VIK_UNITS_DISTANCE_KILOMETRES:
875 temp = g_strdup_printf ( "%s %s DIFF %f meters", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
876 break;
877 case VIK_UNITS_DISTANCE_MILES:
433b3f7f 878 temp = g_strdup_printf ( "%s %s DIFF %f miles", lat, lon, VIK_METERS_TO_MILES(vik_coord_diff( &coord, &(s->oldcoord) )) );
6f9336aa
RN
879 break;
880 default:
881 temp = g_strdup_printf ("Just to keep the compiler happy");
882 g_critical("Houston, we've had a problem. distance=%d", dist_units);
883 }
884
941aa6e9
AF
885 s->has_oldcoord = FALSE;
886 }
887 else {
a58aaed4 888 temp = g_strdup_printf ( "%s %s", lat, lon );
941aa6e9
AF
889 s->has_oldcoord = TRUE;
890 }
50a14534 891
941aa6e9
AF
892 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
893 g_free ( temp );
e4847ce9 894
941aa6e9
AF
895 s->oldcoord = coord;
896 }
897 else {
898 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
899 draw_update ( s->vw );
900 }
901 return VIK_LAYER_TOOL_ACK;
902}
e4847ce9 903
dc2c040e 904static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventMotion *event, ruler_tool_state_t *s)
941aa6e9
AF
905{
906 VikViewport *vvp = s->vvp;
907 VikWindow *vw = s->vw;
908
909 struct LatLon ll;
910 VikCoord coord;
911 gchar *temp;
912
913 if ( s->has_oldcoord ) {
914 int oldx, oldy, w1, h1, w2, h2;
915 static GdkPixmap *buf = NULL;
a58aaed4 916 gchar *lat=NULL, *lon=NULL;
941aa6e9
AF
917 w1 = vik_viewport_get_width(vvp);
918 h1 = vik_viewport_get_height(vvp);
919 if (!buf) {
920 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
921 }
922 gdk_drawable_get_size(buf, &w2, &h2);
923 if (w1 != w2 || h1 != h2) {
924 g_object_unref ( G_OBJECT ( buf ) );
925 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
e4847ce9 926 }
e4847ce9 927
941aa6e9
AF
928 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
929 vik_coord_to_latlon ( &coord, &ll );
930 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
931
932 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
933 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
934 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
935 if (draw_buf_done) {
936 static gpointer pass_along[3];
937 pass_along[0] = GTK_WIDGET(vvp)->window;
938 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
939 pass_along[2] = buf;
940 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
941 draw_buf_done = FALSE;
942 }
a58aaed4 943 a_coords_latlon_to_string(&ll, &lat, &lon);
6f9336aa
RN
944 vik_units_distance_t dist_units = a_vik_get_units_distance ();
945 switch (dist_units) {
946 case VIK_UNITS_DISTANCE_KILOMETRES:
947 temp = g_strdup_printf ( "%s %s DIFF %f meters", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
948 break;
949 case VIK_UNITS_DISTANCE_MILES:
433b3f7f 950 temp = g_strdup_printf ( "%s %s DIFF %f miles", lat, lon, VIK_METERS_TO_MILES (vik_coord_diff( &coord, &(s->oldcoord) )) );
6f9336aa
RN
951 break;
952 default:
953 temp = g_strdup_printf ("Just to keep the compiler happy");
954 g_critical("Houston, we've had a problem. distance=%d", dist_units);
955 }
941aa6e9
AF
956 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
957 g_free ( temp );
acaf7113 958 }
941aa6e9
AF
959 return VIK_LAYER_TOOL_ACK;
960}
50a14534 961
941aa6e9
AF
962static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
963{
964 return VIK_LAYER_TOOL_ACK;
50a14534
EB
965}
966
941aa6e9 967static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
50a14534 968{
941aa6e9 969 draw_update ( s->vw );
50a14534
EB
970}
971
941aa6e9
AF
972static VikToolInterface ruler_tool =
973 { "Ruler",
974 (VikToolConstructorFunc) ruler_create,
975 (VikToolDestructorFunc) ruler_destroy,
976 (VikToolActivationFunc) NULL,
977 (VikToolActivationFunc) ruler_deactivate,
978 (VikToolMouseFunc) ruler_click,
dc2c040e 979 (VikToolMouseMoveFunc) ruler_move,
f2f2f7bf
GB
980 (VikToolMouseFunc) ruler_release,
981 NULL,
982 GDK_CURSOR_IS_PIXMAP,
5bfafde9 983 &cursor_ruler_pixbuf };
941aa6e9
AF
984/*** end ruler code ********************************************************/
985
986
987
988/********************************************************************************
989 ** Zoom tool code
990 ********************************************************************************/
991static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
50a14534 992{
941aa6e9 993 return vw;
50a14534
EB
994}
995
941aa6e9 996static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
50a14534 997{
941aa6e9
AF
998 vw->modified = TRUE;
999 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
1000 if ( event->button == 1 )
1001 vik_viewport_zoom_in (vw->viking_vvp);
1002 else if ( event->button == 3 )
1003 vik_viewport_zoom_out (vw->viking_vvp);
1004 draw_update ( vw );
1005 return VIK_LAYER_TOOL_ACK;
1006}
50a14534 1007
dc2c040e 1008static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventMotion *event, VikViewport *vvp)
941aa6e9
AF
1009{
1010 return VIK_LAYER_TOOL_ACK;
1011}
50a14534 1012
941aa6e9
AF
1013static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
1014{
1015 return VIK_LAYER_TOOL_ACK;
50a14534
EB
1016}
1017
941aa6e9
AF
1018static VikToolInterface zoom_tool =
1019 { "Zoom",
1020 (VikToolConstructorFunc) zoomtool_create,
1021 (VikToolDestructorFunc) NULL,
1022 (VikToolActivationFunc) NULL,
1023 (VikToolActivationFunc) NULL,
1024 (VikToolMouseFunc) zoomtool_click,
dc2c040e 1025 (VikToolMouseMoveFunc) zoomtool_move,
f2f2f7bf
GB
1026 (VikToolMouseFunc) zoomtool_release,
1027 NULL,
1028 GDK_CURSOR_IS_PIXMAP,
5bfafde9 1029 &cursor_zoom_pixbuf };
463f9d07 1030/*** end zoom code ********************************************************/
941aa6e9 1031
576cbd17
GB
1032/********************************************************************************
1033 ** Pan tool code
1034 ********************************************************************************/
1035static gpointer pantool_create (VikWindow *vw, VikViewport *vvp)
1036{
1037 return vw;
1038}
1039
1040static VikLayerToolFuncStatus pantool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
1041{
1042 vw->modified = TRUE;
1043 if ( event->button == 1 )
1044 vik_window_pan_click ( vw, event );
1045 draw_update ( vw );
1046 return VIK_LAYER_TOOL_ACK;
1047}
1048
dc2c040e 1049static VikLayerToolFuncStatus pantool_move (VikLayer *vl, GdkEventMotion *event, VikWindow *vw)
576cbd17
GB
1050{
1051 vik_window_pan_move ( vw, event );
1052 return VIK_LAYER_TOOL_ACK;
1053}
1054
1055static VikLayerToolFuncStatus pantool_release (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
1056{
1057 if ( event->button == 1 )
1058 vik_window_pan_release ( vw, event );
1059 return VIK_LAYER_TOOL_ACK;
1060}
1061
1062static VikToolInterface pan_tool =
1063 { "Pan",
1064 (VikToolConstructorFunc) pantool_create,
1065 (VikToolDestructorFunc) NULL,
1066 (VikToolActivationFunc) NULL,
1067 (VikToolActivationFunc) NULL,
1068 (VikToolMouseFunc) pantool_click,
dc2c040e 1069 (VikToolMouseMoveFunc) pantool_move,
f2f2f7bf
GB
1070 (VikToolMouseFunc) pantool_release,
1071 NULL,
1072 GDK_FLEUR };
576cbd17
GB
1073/*** end pan code ********************************************************/
1074
8c721f83
EB
1075static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
1076{
1077 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
1078 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
1079 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
1080 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
1081 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
1082 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
1083 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
1084 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
1085 }
1086 draw_update ( vw );
1087}
941aa6e9 1088
7de42638
EB
1089static void full_screen_cb ( GtkAction *a, VikWindow *vw )
1090{
1091 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
1092 g_assert(check_box);
1093 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1094 if ( state )
1095 gtk_window_fullscreen ( GTK_WINDOW(vw) );
1096 else
1097 gtk_window_unfullscreen ( GTK_WINDOW(vw) );
1098}
941aa6e9 1099
e4afc73a 1100static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
50a14534 1101{
e4afc73a
EB
1102 guint what = 128;
1103
1104 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
1105 what = -3;
1106 }
1107 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
1108 what = -4;
1109 }
1110 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
1111 what = -2;
1112 }
1113 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
1114 what = -1;
1115 }
1116 else {
1117 gchar *s = (gchar *)gtk_action_get_name(a);
1118 what = atoi(s+4);
1119 }
1120
50a14534
EB
1121 switch (what)
1122 {
1123 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
1124 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
1125 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
1126 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
1127 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
1128 }
1129 draw_update ( vw );
1130}
1131
e4afc73a 1132void draw_goto_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1133{
1134 VikCoord new_center;
e4afc73a
EB
1135
1136 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
50a14534
EB
1137 struct LatLon ll, llold;
1138 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
1139 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
1140 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
1141 else
1142 return;
1143 }
e4afc73a 1144 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
50a14534
EB
1145 struct UTM utm, utmold;
1146 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
1147 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
1148 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
1149 else
1150 return;
1151 }
e4afc73a 1152 else {
8dc8da82 1153 g_critical("Houston, we've had a problem.");
e4afc73a
EB
1154 return;
1155 }
50a14534
EB
1156
1157 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
1158 draw_update ( vw );
1159}
1160
e4afc73a 1161static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
50a14534 1162{
e4afc73a
EB
1163 gint type;
1164 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
1165 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
1166 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
1167 draw_update ( vw );
1168 vw->modified = TRUE;
1169 }
1170 }
50a14534
EB
1171 }
1172}
1173
e4afc73a 1174static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534 1175{
2cebc318 1176 a_clipboard_copy_selected ( vw->viking_vlp );
50a14534
EB
1177}
1178
e4afc73a 1179static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534 1180{
2cebc318 1181 a_clipboard_copy_selected ( vw->viking_vlp );
e4afc73a 1182 menu_delete_layer_cb ( a, vw );
50a14534
EB
1183}
1184
e4afc73a 1185static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1186{
1187 if ( a_clipboard_paste ( vw->viking_vlp ) )
1188 {
1189 draw_update ( vw );
1190 vw->modified = TRUE;
1191 }
1192}
1193
e4afc73a 1194static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1195{
1196 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
4c77d5e0 1197 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
50a14534
EB
1198}
1199
5ff75d1e
GB
1200static void help_help_cb ( GtkAction *a, VikWindow *vw )
1201{
1202#if GTK_CHECK_VERSION (2, 14, 0)
1203 gchar *uri;
1204 uri = g_strdup_printf("ghelp:%s", PACKAGE);
1205 gtk_show_uri(NULL, uri, GDK_CURRENT_TIME, NULL);
1206 g_free(uri);
1207#endif
1208}
1209
d0a5f320
AF
1210static void help_about_cb ( GtkAction *a, VikWindow *vw )
1211{
1212 a_dialog_about(GTK_WINDOW(vw));
1213}
1214
e4afc73a 1215static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1216{
1217 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1218 {
1219 vik_layers_panel_delete_selected ( vw->viking_vlp );
1220 vw->modified = TRUE;
1221 }
1222 else
4c77d5e0 1223 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
50a14534
EB
1224}
1225
181f5d0c
MA
1226static void view_side_panel_cb ( GtkAction *a, VikWindow *vw )
1227{
48df6aa3 1228 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewSidePanel" );
181f5d0c
MA
1229 g_assert(check_box);
1230 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1231 if ( state )
1232 gtk_widget_show(GTK_WIDGET(vw->viking_vlp));
1233 else
1234 gtk_widget_hide(GTK_WIDGET(vw->viking_vlp));
1235}
1236
a459ee10
RN
1237static void view_statusbar_cb ( GtkAction *a, VikWindow *vw )
1238{
48df6aa3 1239 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewStatusBar" );
a459ee10
RN
1240 if ( !check_box )
1241 return;
1242 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
1243 if ( state )
1244 gtk_widget_show ( GTK_WIDGET(vw->viking_vs) );
1245 else
1246 gtk_widget_hide ( GTK_WIDGET(vw->viking_vs) );
1247}
1248
e7591765
RN
1249static void view_toolbar_cb ( GtkAction *a, VikWindow *vw )
1250{
48df6aa3 1251 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewToolbar" );
e7591765
RN
1252 if ( !check_box )
1253 return;
1254 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
1255 if ( state )
1256 gtk_widget_show ( GTK_WIDGET(vw->toolbar) );
1257 else
1258 gtk_widget_hide ( GTK_WIDGET(vw->toolbar) );
1259}
1260
7622022f
RN
1261static void view_main_menu_cb ( GtkAction *a, VikWindow *vw )
1262{
48df6aa3 1263 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewMainMenu" );
7622022f
RN
1264 if ( !check_box )
1265 return;
1266 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
1267 if ( !state )
1268 gtk_widget_hide ( gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu" ) );
1269 else
1270 gtk_widget_show ( gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu" ) );
1271}
1272
941aa6e9
AF
1273/***************************************
1274 ** tool management routines
1275 **
1276 ***************************************/
1277
1278static toolbox_tools_t* toolbox_create(VikWindow *vw)
1279{
1280 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1281 vt->tools = NULL;
1282 vt->n_tools = 0;
1283 vt->active_tool = -1;
1284 vt->vw = vw;
1285 if (!vw->viking_vvp) {
7742da66 1286 g_critical("no viewport found.");
941aa6e9
AF
1287 exit(1);
1288 }
1289 return vt;
1290}
1291
9593a4c9 1292static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
941aa6e9
AF
1293{
1294 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1295 vt->tools[vt->n_tools].ti = *vti;
9593a4c9 1296 vt->tools[vt->n_tools].layer_type = layer_type;
941aa6e9
AF
1297 if (vti->create) {
1298 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1299 }
1300 else {
1301 vt->tools[vt->n_tools].state = NULL;
1302 }
1303 vt->n_tools++;
1304}
1305
1306static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1307{
1308 int i;
1309 for (i=0; i<vt->n_tools; i++) {
1310 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1311 break;
1312 }
1313 }
1314 return i;
1315}
1316
1317static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1318{
1319 int tool = toolbox_get_tool(vt, tool_name);
1320 toolbox_tool_t *t = &vt->tools[tool];
1321 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1322
1323 if (tool == vt->n_tools) {
7742da66 1324 g_critical("trying to activate a non-existent tool...");
941aa6e9
AF
1325 exit(1);
1326 }
1327 /* is the tool already active? */
1328 if (vt->active_tool == tool) {
1329 return;
1330 }
1331
1332 if (vt->active_tool != -1) {
1333 if (vt->tools[vt->active_tool].ti.deactivate) {
1334 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1335 }
1336 }
1337 if (t->ti.activate) {
1338 t->ti.activate(vl, t->state);
1339 }
1340 vt->active_tool = tool;
1341}
1342
f2f2f7bf
GB
1343static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
1344{
1345 int tool = toolbox_get_tool(vt, tool_name);
1346 toolbox_tool_t *t = &vt->tools[tool];
1347 if (t->ti.cursor == NULL) {
1348 if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
1349 GError *cursor_load_err = NULL;
1350 GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
1351 /* TODO: settable offeset */
1352 t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
1353 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1354 } else {
1355 t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
1356 }
1357 }
1358 return t->ti.cursor;
1359}
1360
941aa6e9
AF
1361static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1362{
1363 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1364 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
9593a4c9
EB
1365 gint ltype = vt->tools[vt->active_tool].layer_type;
1366 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1367 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
941aa6e9
AF
1368 }
1369}
1370
dc2c040e 1371static void toolbox_move (toolbox_tools_t *vt, GdkEventMotion *event)
941aa6e9
AF
1372{
1373 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1374 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
9593a4c9
EB
1375 gint ltype = vt->tools[vt->active_tool].layer_type;
1376 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
165d30aa
EB
1377 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1378 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
941aa6e9
AF
1379 }
1380}
1381
1382static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1383{
1384 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
9593a4c9
EB
1385 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1386 gint ltype = vt->tools[vt->active_tool].layer_type;
1387 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1388 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
941aa6e9
AF
1389 }
1390}
1391/** End tool management ************************************/
1392
8fb71d6c
EB
1393void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1394{
1395 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1396}
941aa6e9
AF
1397
1398/* this function gets called whenever a toolbar tool is clicked */
e4afc73a 1399static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
50a14534
EB
1400{
1401 /* White Magic, my friends ... White Magic... */
8fb71d6c 1402 int layer_id, tool_id;
f2f2f7bf 1403 const GdkCursor *cursor = NULL;
e4afc73a 1404
941aa6e9
AF
1405 toolbox_activate(vw->vt, gtk_action_get_name(a));
1406
f2f2f7bf
GB
1407 cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
1408 /* We set cursor, even if it is NULL: it resets to default */
9b5dcb38 1409 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, (GdkCursor *)cursor );
f2f2f7bf 1410
576cbd17
GB
1411 if (!strcmp(gtk_action_get_name(a), "Pan")) {
1412 vw->current_tool = TOOL_PAN;
576cbd17
GB
1413 }
1414 else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
e4afc73a
EB
1415 vw->current_tool = TOOL_ZOOM;
1416 }
1417 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1418 vw->current_tool = TOOL_RULER;
1419 }
1420 else {
79845167 1421 /* TODO: only enable tools from active layer */
8fb71d6c
EB
1422 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1423 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1424 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1425 vw->current_tool = TOOL_LAYER;
1426 vw->tool_layer_id = layer_id;
1427 vw->tool_tool_id = tool_id;
e4afc73a
EB
1428 }
1429 }
1430 }
1431 }
50a14534
EB
1432 draw_status ( vw );
1433}
1434
1435static void window_set_filename ( VikWindow *vw, const gchar *filename )
1436{
1437 gchar *title;
4c77d5e0 1438 const gchar *file;
50a14534
EB
1439 if ( vw->filename )
1440 g_free ( vw->filename );
1441 if ( filename == NULL )
1442 {
1443 vw->filename = NULL;
4c77d5e0 1444 file = _("Untitled");
50a14534
EB
1445 }
1446 else
1447 {
1448 vw->filename = g_strdup(filename);
4c77d5e0 1449 file = a_file_basename ( filename );
50a14534 1450 }
4c77d5e0
GB
1451 title = g_strdup_printf( "%s - Viking", file );
1452 gtk_window_set_title ( GTK_WINDOW(vw), title );
1453 g_free ( title );
50a14534
EB
1454}
1455
7bc965c0
GB
1456GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1457{
1458 GtkWidget *mode_button;
1459 gchar *buttonname;
1460 switch ( mode ) {
794c8f18 1461#ifdef VIK_CONFIG_EXPEDIA
7bc965c0 1462 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
794c8f18 1463#endif
7bc965c0 1464 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
d587678a 1465 case VIK_VIEWPORT_DRAWMODE_LATLON: buttonname = "/ui/MainMenu/View/ModeLatLon"; break;
794c8f18 1466 default: buttonname = "/ui/MainMenu/View/ModeUTM";
7bc965c0
GB
1467 }
1468 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1469 g_assert ( mode_button );
1470 return mode_button;
1471}
1472
01da6b4d
GB
1473/**
1474 * vik_window_get_pan_move:
1475 * @vw: some VikWindow
1476 *
1477 * Retrieves @vw's pan_move.
1478 *
1479 * Should be removed as soon as possible.
1480 *
1481 * Returns: @vw's pan_move
1482 *
1483 * Since: 0.9.96
1484 **/
1c6a6010
GB
1485gboolean vik_window_get_pan_move ( VikWindow *vw )
1486{
1487 return vw->pan_move;
1488}
1489
13505702
GB
1490static void on_activate_recent_item (GtkRecentChooser *chooser,
1491 VikWindow *self)
1492{
1493 gchar *filename;
1494
1495 filename = gtk_recent_chooser_get_current_uri (chooser);
1496 if (filename != NULL)
1497 {
1498 GFile *file = g_file_new_for_uri ( filename );
1499 gchar *path = g_file_get_path ( file );
1500 g_object_unref ( file );
1501 if ( self->filename )
1502 {
1503 gchar *filenames[] = { path, NULL };
1504 g_signal_emit ( G_OBJECT(self), window_signals[VW_OPENWINDOW_SIGNAL], 0, filenames );
1505 }
1506 else
756d53f5 1507 vik_window_open_file ( self, path, TRUE );
13505702
GB
1508 g_free ( path );
1509 }
1510
1511 g_free (filename);
1512}
1513
1514static void setup_recent_files (VikWindow *self)
1515{
1516 GtkRecentManager *manager;
1517 GtkRecentFilter *filter;
1518 GtkWidget *menu, *menu_item;
1519
1520 filter = gtk_recent_filter_new ();
1521 /* gtk_recent_filter_add_application (filter, g_get_application_name()); */
1522 gtk_recent_filter_add_group(filter, "viking");
1523
1524 manager = gtk_recent_manager_get_default ();
1525 menu = gtk_recent_chooser_menu_new_for_manager (manager);
1526 gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu), GTK_RECENT_SORT_MRU);
1527 gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu), filter);
1528
1529 menu_item = gtk_ui_manager_get_widget (self->uim, "/ui/MainMenu/File/OpenRecentFile");
1530 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
1531
1532 g_signal_connect (G_OBJECT (menu), "item-activated",
1533 G_CALLBACK (on_activate_recent_item), (gpointer) self);
1534}
1535
1536static void update_recently_used_document(const gchar *filename)
1537{
1538 /* Update Recently Used Document framework */
1539 GtkRecentManager *manager = gtk_recent_manager_get_default();
1540 GtkRecentData *recent_data = g_slice_new (GtkRecentData);
1541 gchar *groups[] = {"viking", NULL};
1542 GFile *file = g_file_new_for_commandline_arg(filename);
1543 gchar *uri = g_file_get_uri(file);
1544 gchar *basename = g_path_get_basename(filename);
1545 g_object_unref(file);
1546 file = NULL;
1547
1548 recent_data->display_name = basename;
1549 recent_data->description = NULL;
1550 recent_data->mime_type = "text/x-gps-data";
1551 recent_data->app_name = (gchar *) g_get_application_name ();
1552 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%f", NULL);
1553 recent_data->groups = groups;
1554 recent_data->is_private = FALSE;
1555 if (!gtk_recent_manager_add_full (manager, uri, recent_data))
1556 {
1557 g_warning (_("Unable to add '%s' to the list of recently used documents"), uri);
1558 }
1559
1560 g_free (uri);
1561 g_free (basename);
1562 g_free (recent_data->app_exec);
1563 g_slice_free (GtkRecentData, recent_data);
1564}
1565
50a14534
EB
1566void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1567{
1568 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1569 {
1570 case 0:
4c77d5e0 1571 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
50a14534
EB
1572 break;
1573 case 1:
1574 {
1575 GtkWidget *mode_button;
13505702 1576 /* Update UI */
50a14534
EB
1577 if ( change_filename )
1578 window_set_filename ( vw, filename );
7bc965c0 1579 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
50a14534
EB
1580 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. */
1581 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1582 vw->only_updating_coord_mode_ui = FALSE;
1583
1584 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1657065a 1585
48df6aa3 1586 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowScale" );
1657065a
QT
1587 g_assert ( mode_button );
1588 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1589
48df6aa3 1590 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowCenterMark" );
1657065a
QT
1591 g_assert ( mode_button );
1592 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
50a14534 1593 }
13505702
GB
1594 default:
1595 update_recently_used_document(filename);
1596 draw_update ( vw );
50a14534
EB
1597 }
1598}
e4afc73a 1599static void load_file ( GtkAction *a, VikWindow *vw )
50a14534 1600{
6e4a49aa
MA
1601 GSList *files = NULL;
1602 GSList *cur_file = NULL;
e4afc73a
EB
1603 gboolean newwindow;
1604 if (!strcmp(gtk_action_get_name(a), "Open")) {
1605 newwindow = TRUE;
1606 }
1607 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1608 newwindow = FALSE;
1609 }
1610 else {
8dc8da82 1611 g_critical("Houston, we've had a problem.");
e4afc73a
EB
1612 return;
1613 }
1614
50a14534
EB
1615 if ( ! vw->open_dia )
1616 {
6e4a49aa
MA
1617 vw->open_dia = gtk_file_chooser_dialog_new (_("Please select a GPS data file to open. "),
1618 GTK_WINDOW(vw),
1619 GTK_FILE_CHOOSER_ACTION_OPEN,
1620 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1621 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1622 NULL);
1623 gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(vw->open_dia), TRUE );
50a14534
EB
1624 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1625 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1626 }
6e4a49aa 1627 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534
EB
1628 {
1629 gtk_widget_hide ( vw->open_dia );
a5fd2196 1630#ifdef VIKING_PROMPT_IF_MODIFIED
50a14534 1631 if ( (vw->modified || vw->filename) && newwindow )
a5fd2196
QT
1632#else
1633 if ( vw->filename && newwindow )
1634#endif
6e4a49aa 1635 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) ) );
50a14534 1636 else {
6e4a49aa
MA
1637 files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) );
1638 gboolean change_fn = newwindow && (g_slist_length(files)==1); /* only change fn if one file */
1639
1640 cur_file = files;
1641 while ( cur_file ) {
1642 gchar *file_name = cur_file->data;
1643 vik_window_open_file ( vw, file_name, change_fn );
1644 g_free (file_name);
1645 cur_file = g_slist_next (cur_file);
50a14534 1646 }
6e4a49aa 1647 g_slist_free (files);
50a14534
EB
1648 }
1649 }
1650 else
1651 gtk_widget_hide ( vw->open_dia );
1652}
1653
e4afc73a 1654static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
50a14534
EB
1655{
1656 gboolean rv = FALSE;
1657 const gchar *fn;
1658 if ( ! vw->save_dia )
1659 {
6e4a49aa
MA
1660 vw->save_dia = gtk_file_chooser_dialog_new (_("Save as Viking File."),
1661 GTK_WINDOW(vw),
1662 GTK_FILE_CHOOSER_ACTION_SAVE,
1663 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1664 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1665 NULL);
50a14534
EB
1666 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1667 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1668 }
1669
6e4a49aa 1670 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534 1671 {
6e4a49aa 1672 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
d91e5f2b 1673 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_yes_or_no ( GTK_WINDOW(vw->save_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
50a14534
EB
1674 {
1675 window_set_filename ( vw, fn );
1676 rv = window_save ( vw );
1677 vw->modified = FALSE;
1678 break;
1679 }
1680 }
1681 gtk_widget_hide ( vw->save_dia );
1682 return rv;
1683}
1684
1685static gboolean window_save ( VikWindow *vw )
1686{
1687 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
13505702
GB
1688 {
1689 update_recently_used_document ( vw->filename );
50a14534 1690 return TRUE;
13505702 1691 }
50a14534
EB
1692 else
1693 {
4c77d5e0 1694 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
50a14534
EB
1695 return FALSE;
1696 }
1697}
1698
e4afc73a 1699static gboolean save_file ( GtkAction *a, VikWindow *vw )
50a14534
EB
1700{
1701 if ( ! vw->filename )
e4afc73a 1702 return save_file_as ( NULL, vw );
50a14534
EB
1703 else
1704 {
1705 vw->modified = FALSE;
1706 return window_save ( vw );
1707 }
1708}
1709
1d1bc3c1
EB
1710static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1711{
7b3479e3
EB
1712 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1713}
1714
1715static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1716{
1717 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1d1bc3c1
EB
1718}
1719
1ef9e637 1720#ifdef VIK_CONFIG_GEOCACHES
3333c069
EB
1721static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1722{
1723 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1724}
1ef9e637 1725#endif
3333c069 1726
5210c3d3
GB
1727static void goto_default_location( GtkAction *a, VikWindow *vw)
1728{
1729 struct LatLon ll;
1730 ll.lat = a_vik_get_default_lat();
1731 ll.lon = a_vik_get_default_long();
1732 vik_viewport_set_center_latlon(vw->viking_vvp, &ll);
1733 vik_layers_panel_emit_update(vw->viking_vlp);
1734}
1735
1736
369126f3
QT
1737static void goto_address( GtkAction *a, VikWindow *vw)
1738{
34e71b99 1739 a_vik_goto(vw, vw->viking_vlp, vw->viking_vvp);
369126f3
QT
1740}
1741
7c259702
JJ
1742static void mapcache_flush_cb ( GtkAction *a, VikWindow *vw )
1743{
1744 a_mapcache_flush();
1745}
1746
17a1f8f9
EB
1747static void preferences_cb ( GtkAction *a, VikWindow *vw )
1748{
9be0449f
RN
1749 gboolean wp_icon_size = a_vik_get_use_large_waypoint_icons();
1750
17a1f8f9 1751 a_preferences_show_window ( GTK_WINDOW(vw) );
9be0449f
RN
1752
1753 // Delete icon indexing 'cache' and so automatically regenerates with the new setting when changed
1754 if (wp_icon_size != a_vik_get_use_large_waypoint_icons())
1755 clear_garmin_icon_syms ();
1756
6f9336aa 1757 draw_update ( vw );
17a1f8f9
EB
1758}
1759
5210c3d3
GB
1760static void default_location_cb ( GtkAction *a, VikWindow *vw )
1761{
1762 /* Simplistic repeat of preference setting
1763 Only the name & type are important for setting the preference via this 'external' way */
1764 VikLayerParam pref_lat[] = {
1765 { VIKING_PREFERENCES_NAMESPACE "default_latitude",
1766 VIK_LAYER_PARAM_DOUBLE,
1767 VIK_LOCATION_LAT,
1768 NULL,
1769 VIK_LAYER_WIDGET_SPINBUTTON,
1770 NULL,
1771 NULL },
1772 };
1773 VikLayerParam pref_lon[] = {
1774 { VIKING_PREFERENCES_NAMESPACE "default_longitude",
1775 VIK_LAYER_PARAM_DOUBLE,
1776 VIK_LOCATION_LONG,
1777 NULL,
1778 VIK_LAYER_WIDGET_SPINBUTTON,
1779 NULL,
1780 NULL },
1781 };
1782
1783 /* Get current center */
1784 struct LatLon ll;
1785 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &ll );
1786
1787 /* Apply to preferences */
1788 VikLayerParamData vlp_data;
1789 vlp_data.d = ll.lat;
1790 a_preferences_run_setparam (vlp_data, pref_lat);
1791 vlp_data.d = ll.lon;
1792 a_preferences_run_setparam (vlp_data, pref_lon);
1793 /* Remember to save */
1794 a_preferences_save_to_file();
1795}
1796
e4afc73a 1797static void clear_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1798{
1799 vik_layers_panel_clear ( vw->viking_vlp );
1800 window_set_filename ( vw, NULL );
1801 draw_update ( vw );
1802}
1803
e4afc73a 1804static void window_close ( GtkAction *a, VikWindow *vw )
50a14534
EB
1805{
1806 if ( ! delete_event ( vw ) )
1807 gtk_widget_destroy ( GTK_WIDGET(vw) );
1808}
1809
2bf7cadd
QT
1810static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1811{
7bb60307 1812 if (save_file( NULL, vw)) {
2bf7cadd 1813 window_close( NULL, vw);
7bb60307
QT
1814 return(TRUE);
1815 }
2bf7cadd
QT
1816 else
1817 return(FALSE);
1818}
1819
e4afc73a 1820static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1821{
1822 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1823 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1824 {
1825 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1826 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1827 draw_update ( vw );
1828 }
1829}
1830
1831static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1832{
1833 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1834 GdkPixbuf *pixbuf_to_save;
1835 gdouble old_xmpp, old_ympp;
1836 GError *error = NULL;
1837
1838 /* backup old zoom & set new */
1839 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1840 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1841 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1842
1843 /* reset width and height: */
1844 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1845
1846 /* draw all layers */
1847 draw_redraw ( vw );
1848
1849 /* save buffer as file. */
1850 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);
1851 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1852 if (error)
1853 {
7742da66 1854 g_warning("Unable to write to file %s: %s", fn, error->message );
50a14534
EB
1855 g_error_free (error);
1856 }
1857 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1858
1859 /* pretend like nothing happened ;) */
1860 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1861 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1862 vik_viewport_configure ( vw->viking_vvp );
1863 draw_update ( vw );
1864}
1865
1866static 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 )
1867{
1868 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1869 gchar *name_of_file = g_malloc ( size );
1870 guint x = 1, y = 1;
1871 struct UTM utm_orig, utm;
1872
1873 /* *** copied from above *** */
1874 GdkPixbuf *pixbuf_to_save;
1875 gdouble old_xmpp, old_ympp;
1876 GError *error = NULL;
1877
1878 /* backup old zoom & set new */
1879 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1880 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1881 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1882
1883 /* reset width and height: do this only once for all images (same size) */
1884 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1885 /* *** end copy from above *** */
1886
1887 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1888
f83131b9 1889 g_mkdir(fn,0777);
50a14534
EB
1890
1891 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1892
1893 for ( y = 1; y <= tiles_h; y++ )
1894 {
1895 for ( x = 1; x <= tiles_w; x++ )
1896 {
3d9454e6 1897 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
50a14534
EB
1898 utm = utm_orig;
1899 if ( tiles_w & 0x1 )
1900 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1901 else
1902 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1903 if ( tiles_h & 0x1 ) /* odd */
1904 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1905 else /* even */
1906 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1907
1908 /* move to correct place. */
1909 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1910
1911 draw_redraw ( vw );
1912
1913 /* save buffer as file. */
1914 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);
1915 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1916 if (error)
1917 {
7742da66 1918 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
50a14534
EB
1919 g_error_free (error);
1920 }
1921
1922 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1923 }
1924 }
1925
1926 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1927 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1928 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1929 vik_viewport_configure ( vw->viking_vvp );
1930 draw_update ( vw );
1931
1932 g_free ( name_of_file );
1933}
1934
1935static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1936{
1937 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1938 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1939 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1940 gdouble width_min, width_max, height_min, height_max;
1941 gint width, height;
1942
1943 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1944 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1945
1946 /* TODO: support for xzoom and yzoom values */
1947 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1948 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1949
1950 if ( width > width_max || width < width_min || height > height_max || height < height_min )
4c77d5e0 1951 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
50a14534
EB
1952
1953 gtk_spin_button_set_value ( width_spin, width );
1954 gtk_spin_button_set_value ( height_spin, height );
1955}
1956
1957static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1958{
1959 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1960 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1961 gchar *label_text;
1962 gdouble w, h;
1963 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1964 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1965 if (pass_along[4]) /* save many images; find TOTAL area covered */
1966 {
1967 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1968 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1969 }
6f9336aa
RN
1970 vik_units_distance_t dist_units = a_vik_get_units_distance ();
1971 switch (dist_units) {
1972 case VIK_UNITS_DISTANCE_KILOMETRES:
1973 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1974 break;
1975 case VIK_UNITS_DISTANCE_MILES:
1976 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. miles)"), (glong)w, (glong)h, (w*h/2589988.11));
1977 break;
1978 default:
1979 label_text = g_strdup_printf ("Just to keep the compiler happy");
1980 g_critical("Houston, we've had a problem. distance=%d", dist_units);
1981 }
1982
50a14534
EB
1983 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1984 g_free ( label_text );
1985}
1986
1987static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1988{
1989 /* todo: default for answers inside VikWindow or static (thruout instance) */
4c77d5e0 1990 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
50a14534
EB
1991 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1992 GTK_STOCK_CANCEL,
1993 GTK_RESPONSE_REJECT,
1994 GTK_STOCK_OK,
1995 GTK_RESPONSE_ACCEPT,
10888930 1996 NULL );
50a14534
EB
1997 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1998 GtkWidget *png_radio, *jpeg_radio;
1999 GtkWidget *current_window_button;
2000 gpointer current_window_pass_along[7];
2001 GtkWidget *zoom_label, *zoom_spin;
2002 GtkWidget *total_size_label;
2003
2004 /* only used if (!one_image_only) */
886031df 2005 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
50a14534
EB
2006
2007
4c77d5e0 2008 width_label = gtk_label_new ( _("Width (pixels):") );
50a14534 2009 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
4c77d5e0 2010 height_label = gtk_label_new ( _("Height (pixels):") );
50a14534
EB
2011 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
2012
4c77d5e0 2013 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
50a14534 2014 /* TODO: separate xzoom and yzoom factors */
ac33062d 2015 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, 0 )), 16, 0);
50a14534
EB
2016
2017 total_size_label = gtk_label_new ( NULL );
2018
4c77d5e0 2019 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
50a14534
EB
2020 current_window_pass_along [0] = vw;
2021 current_window_pass_along [1] = width_spin;
2022 current_window_pass_along [2] = height_spin;
2023 current_window_pass_along [3] = zoom_spin;
2024 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
2025 current_window_pass_along [5] = NULL;
2026 current_window_pass_along [6] = total_size_label;
2027 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
2028
4c77d5e0
GB
2029 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
2030 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
50a14534
EB
2031
2032 if ( ! vw->draw_image_save_as_png )
2033 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
2034
2035 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
2036 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
2037 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
2038 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
2039 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
2040 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
2041 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
2042 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
2043 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
2044
2045 if ( ! one_image_only )
2046 {
2047 GtkWidget *tiles_width_label, *tiles_height_label;
2048
2049
4c77d5e0 2050 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
50a14534 2051 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
4c77d5e0 2052 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
50a14534
EB
2053 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
2054 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
2055 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
2056 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
2057 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
2058
2059 current_window_pass_along [4] = tiles_width_spin;
2060 current_window_pass_along [5] = tiles_height_spin;
2061 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2062 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2063 }
2064 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
2065 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2066 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2067 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2068
2069 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
2070
1fb999d3
RN
2071 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
2072
50a14534
EB
2073 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
2074
2075 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
2076 {
2077 gtk_widget_hide ( GTK_WIDGET(dialog) );
2078 if ( one_image_only )
2079 save_image_file ( vw, fn,
2080 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
2081 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
2082 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
2083 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
2084 else {
2085 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
2086 save_image_dir ( vw, fn,
2087 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
2088 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
2089 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
2090 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
2091 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
2092 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
2093 else
4c77d5e0 2094 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
50a14534
EB
2095 }
2096 }
2097 gtk_widget_destroy ( GTK_WIDGET(dialog) );
2098}
2099
2100
e4afc73a 2101static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
50a14534 2102{
ca9e25af 2103 gchar *fn;
f2a1ca71 2104 if (!vw->save_img_dia) {
6e4a49aa
MA
2105 vw->save_img_dia = gtk_file_chooser_dialog_new (_("Save Image"),
2106 GTK_WINDOW(vw),
2107 GTK_FILE_CHOOSER_ACTION_SAVE,
2108 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2109 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
2110 NULL);
f2a1ca71
QT
2111 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
2112 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
2113 }
50a14534 2114
6e4a49aa 2115 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534 2116 {
6e4a49aa 2117 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
d91e5f2b 2118 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_yes_or_no ( GTK_WINDOW(vw->save_img_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
50a14534 2119 {
50a14534
EB
2120 draw_to_image_file ( vw, fn, TRUE );
2121 break;
2122 }
fc759827
GB
2123 g_free(fn);
2124 fn = NULL;
50a14534 2125 }
f2a1ca71 2126 gtk_widget_hide ( vw->save_img_dia );
50a14534
EB
2127}
2128
e4afc73a 2129static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
50a14534 2130{
6e4a49aa
MA
2131 gchar *fn = NULL;
2132
f2a1ca71 2133 if (!vw->save_img_dir_dia) {
6e4a49aa
MA
2134 vw->save_img_dir_dia = gtk_file_chooser_dialog_new (_("Choose a directory to hold images"),
2135 GTK_WINDOW(vw),
2136 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
2137 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2138 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2139 NULL);
f2a1ca71
QT
2140 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
2141 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
2142 }
6e4a49aa
MA
2143
2144 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534 2145 {
6e4a49aa
MA
2146 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dir_dia) );
2147 if ( fn )
50a14534 2148 {
50a14534 2149 draw_to_image_file ( vw, fn, FALSE );
6e4a49aa
MA
2150 g_free(fn);
2151 fn = NULL;
50a14534
EB
2152 break;
2153 }
2154 }
f2a1ca71 2155 gtk_widget_hide ( vw->save_img_dir_dia );
50a14534
EB
2156}
2157
9a995996 2158#if GTK_CHECK_VERSION(2,10,0)
42f34743
QT
2159static void print_cb ( GtkAction *a, VikWindow *vw )
2160{
2161 a_print(vw, vw->viking_vvp);
2162}
9a995996 2163#endif
42f34743 2164
50a14534 2165/* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
e4afc73a 2166static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
50a14534 2167{
e4afc73a
EB
2168 VikViewportDrawMode drawmode;
2169 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
2170 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
2171 }
d587678a
GB
2172 else if (!strcmp(gtk_action_get_name(a), "ModeLatLon")) {
2173 drawmode = VIK_VIEWPORT_DRAWMODE_LATLON;
2174 }
e4afc73a
EB
2175 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
2176 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
2177 }
e4afc73a
EB
2178 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
2179 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
2180 }
2181 else {
8dc8da82 2182 g_critical("Houston, we've had a problem.");
e4afc73a
EB
2183 return;
2184 }
2185
50a14534
EB
2186 if ( !vw->only_updating_coord_mode_ui )
2187 {
2188 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
2189 if ( olddrawmode != drawmode )
2190 {
2191 /* this takes care of coord mode too */
2192 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
2193 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
2194 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
2195 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
2196 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
2197 }
2198 draw_update ( vw );
2199 }
2200 }
2201}
2202
35c7c0ba
EB
2203static void set_draw_scale ( GtkAction *a, VikWindow *vw )
2204{
48df6aa3 2205 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowScale" );
1657065a
QT
2206 g_assert(check_box);
2207 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2208 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
35c7c0ba
EB
2209 draw_update ( vw );
2210}
2211
c933487f
QT
2212static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
2213{
48df6aa3 2214 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowCenterMark" );
1657065a
QT
2215 g_assert(check_box);
2216 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2217 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
c933487f
QT
2218 draw_update ( vw );
2219}
2220
e4afc73a 2221static void set_bg_color ( GtkAction *a, VikWindow *vw )
50a14534 2222{
4c77d5e0 2223 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
50a14534
EB
2224 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
2225 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2226 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2227 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
2228 {
2229 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2230 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
2231 draw_update ( vw );
2232 }
2233 g_free ( color );
2234 gtk_widget_destroy ( colorsd );
2235}
2236
941aa6e9
AF
2237
2238
2239/***********************************************************************************************
2240 ** GUI Creation
2241 ***********************************************************************************************/
2242
e4afc73a 2243static GtkActionEntry entries[] = {
5515e2d3
JJ
2244 { "File", NULL, N_("_File"), 0, 0, 0 },
2245 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
2246 { "View", NULL, N_("_View"), 0, 0, 0 },
48df6aa3 2247 { "SetShow", NULL, N_("_Show"), 0, 0, 0 },
5515e2d3
JJ
2248 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
2249 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
2250 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
2251 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
92806042 2252 { "Exttools", NULL, N_("_Webtools"), 0, 0, 0 },
5515e2d3
JJ
2253 { "Help", NULL, N_("_Help"), 0, 0, 0 },
2254
2255 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
06526f88
RN
2256 { "Open", GTK_STOCK_OPEN, N_("_Open..."), "<control>O", N_("Open a file"), (GCallback)load_file },
2257 { "OpenRecentFile", NULL, N_("Open _Recent File"), NULL, NULL, (GCallback)NULL },
2258 { "Append", GTK_STOCK_ADD, N_("Append _File..."), NULL, N_("Append data from a different file"), (GCallback)load_file },
5515e2d3 2259 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
06526f88
RN
2260 { "AcquireGPS", NULL, N_("From _GPS..."), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
2261 { "AcquireGoogle", NULL, N_("Google _Directions..."), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1ef9e637 2262#ifdef VIK_CONFIG_GEOCACHES
06526f88 2263 { "AcquireGC", NULL, N_("Geo_caches..."), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1ef9e637 2264#endif
5515e2d3 2265 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
06526f88
RN
2266 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As..."), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
2267 { "GenImg", GTK_STOCK_CLEAR, N_("_Generate Image File..."), NULL, N_("Save a snapshot of the workspace into a file"), (GCallback)draw_to_image_file_cb },
2268 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images..."), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
9a995996
EB
2269
2270#if GTK_CHECK_VERSION(2,10,0)
5515e2d3 2271 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
9a995996
EB
2272#endif
2273
5515e2d3
JJ
2274 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
2275 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
2276
5210c3d3 2277 { "GotoDefaultLocation", GTK_STOCK_HOME, N_("Go to the _Default Location"), NULL, N_("Go to the default location"), (GCallback)goto_default_location },
cbd293bb 2278 { "GotoSearch", GTK_STOCK_JUMP_TO, N_("Go to _Location..."), NULL, N_("Go to address/place using text search"), (GCallback)goto_address },
402fee6c 2279 { "GotoLL", GTK_STOCK_JUMP_TO, N_("_Go to Lat/Lon..."), NULL, N_("Go to arbitrary lat/lon coordinate"), (GCallback)draw_goto_cb },
150618fe 2280 { "GotoUTM", GTK_STOCK_JUMP_TO, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
cbd293bb 2281 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Bac_kground Color..."), NULL, NULL, (GCallback)set_bg_color },
5515e2d3
JJ
2282 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
2283 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
22c15481 2284 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To..."), "<control>Z", NULL, (GCallback)zoom_to_cb },
5515e2d3
JJ
2285 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
2286 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
2287 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
2288 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
2289 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
2290 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
2291 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
2292 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
2293 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
2294 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
4344546e
RN
2295 { "Zoom256", NULL, N_("256"), NULL, NULL, (GCallback)draw_zoom_cb },
2296 { "Zoom512", NULL, N_("512"), NULL, NULL, (GCallback)draw_zoom_cb },
2297 { "Zoom1024", NULL, N_("1024"), NULL, NULL, (GCallback)draw_zoom_cb },
2298 { "Zoom2048", NULL, N_("2048"), NULL, NULL, (GCallback)draw_zoom_cb },
2299 { "Zoom4096", NULL, N_("4096"), NULL, NULL, (GCallback)draw_zoom_cb },
2300 { "Zoom8192", NULL, N_("8192"), NULL, NULL, (GCallback)draw_zoom_cb },
2301 { "Zoom16384", NULL, N_("16384"), NULL, NULL, (GCallback)draw_zoom_cb },
2302 { "Zoom32768", NULL, N_("32768"), NULL, NULL, (GCallback)draw_zoom_cb },
cbd293bb
RN
2303 { "PanNorth", NULL, N_("Pan _North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
2304 { "PanEast", NULL, N_("Pan _East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
2305 { "PanSouth", NULL, N_("Pan _South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
2306 { "PanWest", NULL, N_("Pan _West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
5515e2d3
JJ
2307 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
2308
2309 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
2310 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
2311 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
2312 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
2313 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
06526f88 2314 { "MapCacheFlush",NULL, N_("_Flush Map Cache"), NULL, NULL, (GCallback)mapcache_flush_cb },
5210c3d3 2315 { "SetDefaultLocation", GTK_STOCK_GO_FORWARD, N_("_Set the Default Location"), NULL, N_("Set the Default Location to the current position"),(GCallback)default_location_cb },
06526f88 2316 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences"), NULL, NULL, (GCallback)preferences_cb },
5515e2d3
JJ
2317 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
2318
5ff75d1e 2319 { "HelpEntry", GTK_STOCK_HELP, N_("_Help"), "F1", NULL, (GCallback)help_help_cb },
5515e2d3 2320 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
e4afc73a
EB
2321};
2322
2323/* Radio items */
d587678a 2324/* FIXME use VIEWPORT_DRAWMODE values */
e4afc73a 2325static GtkRadioActionEntry mode_entries[] = {
5515e2d3
JJ
2326 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
2327 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
ac16c140 2328 { "ModeMercator", NULL, N_("_Mercator Mode"), "<control>m", NULL, 4 },
bd5e571a 2329 { "ModeLatLon", NULL, N_("Lat_/Lon Mode"), "<control>l", NULL, 5 },
50a14534
EB
2330};
2331
e4afc73a 2332static GtkRadioActionEntry tool_entries[] = {
576cbd17
GB
2333 { "Pan", "vik-icon-pan", N_("_Pan"), "<control><shift>P", N_("Pan Tool"), 0 },
2334 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 1 },
2335 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 2 }
e4afc73a 2336};
50a14534 2337
35c7c0ba 2338static GtkToggleActionEntry toggle_entries[] = {
48df6aa3 2339 { "ShowScale", NULL, N_("Show _Scale"), "F5", N_("Show Scale"), (GCallback)set_draw_scale, TRUE },
cbd293bb
RN
2340 { "ShowCenterMark", NULL, N_("Show _Center Mark"), "F6", N_("Show Center Mark"), (GCallback)set_draw_centermark, TRUE },
2341 { "FullScreen", GTK_STOCK_FULLSCREEN, N_("_Full Screen"), "F11", N_("Activate full screen mode"), (GCallback)full_screen_cb, FALSE },
48df6aa3 2342 { "ViewSidePanel", GTK_STOCK_INDEX, N_("Show Side _Panel"), "F9", N_("Show Side Panel"), (GCallback)view_side_panel_cb, TRUE },
a459ee10 2343 { "ViewStatusBar", NULL, N_("Show Status_bar"), "F12", N_("Show Statusbar"), (GCallback)view_statusbar_cb, TRUE },
48df6aa3
RN
2344 { "ViewToolbar", NULL, N_("Show _Toolbar"), "F3", N_("Show Toolbar"), (GCallback)view_toolbar_cb, TRUE },
2345 { "ViewMainMenu", NULL, N_("Show _Menu"), "F4", N_("Show Menu"), (GCallback)view_main_menu_cb, TRUE },
35c7c0ba
EB
2346};
2347
a527cfff 2348#include "menu.xml.h"
e4afc73a 2349static void window_create_ui( VikWindow *window )
50a14534 2350{
e4afc73a
EB
2351 GtkUIManager *uim;
2352 GtkActionGroup *action_group;
50a14534 2353 GtkAccelGroup *accel_group;
e4afc73a 2354 GError *error;
e4afc73a
EB
2355 guint i, j, mid;
2356 GtkIconFactory *icon_factory;
2357 GtkIconSet *icon_set;
e4afc73a
EB
2358 GtkRadioActionEntry *tools = NULL, *radio;
2359 guint ntools;
2360
2361 uim = gtk_ui_manager_new ();
2362 window->uim = uim;
2363
9593a4c9
EB
2364 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
2365 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
576cbd17 2366 toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
941aa6e9 2367
e4afc73a 2368 error = NULL;
a527cfff 2369 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
e4afc73a
EB
2370 g_error_free (error);
2371 exit (1);
2372 }
2373
2374 action_group = gtk_action_group_new ("MenuActions");
5515e2d3 2375 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
e4afc73a 2376 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
35c7c0ba 2377 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
6a9ff0ee 2378 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
e4afc73a
EB
2379
2380 icon_factory = gtk_icon_factory_new ();
ee36ac4f 2381 gtk_icon_factory_add_default (icon_factory);
e4afc73a
EB
2382
2383 register_vik_icons(icon_factory);
2384
2385 ntools = 0;
2386 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2387 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2388 radio = &tools[ntools];
2389 ntools++;
2390 *radio = tool_entries[i];
2391 radio->value = ntools;
2392 }
2393
2394 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2395 GtkActionEntry action;
2396 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
2397 vik_layer_get_interface(i)->name,
2398 vik_layer_get_interface(i)->name,
2399 GTK_UI_MANAGER_MENUITEM, FALSE);
2400
2401 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2402 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2403 gtk_icon_set_unref (icon_set);
2404
2405 action.name = vik_layer_get_interface(i)->name;
2406 action.stock_id = vik_layer_get_interface(i)->name;
4c77d5e0 2407 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
e4afc73a
EB
2408 action.accelerator = NULL;
2409 action.tooltip = NULL;
2410 action.callback = (GCallback)menu_addlayer_cb;
2411 gtk_action_group_add_actions(action_group, &action, 1, window);
2412
2413 if ( vik_layer_get_interface(i)->tools_count ) {
2414 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2415 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2416 }
2417
2418 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2419 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2420 radio = &tools[ntools];
2421 ntools++;
2422
e4afc73a 2423 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
4c77d5e0 2424 _(vik_layer_get_interface(i)->tools[j].name),
e4afc73a
EB
2425 vik_layer_get_interface(i)->tools[j].name,
2426 GTK_UI_MANAGER_MENUITEM, FALSE);
2427 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
4c77d5e0 2428 _(vik_layer_get_interface(i)->tools[j].name),
e4afc73a
EB
2429 vik_layer_get_interface(i)->tools[j].name,
2430 GTK_UI_MANAGER_TOOLITEM, FALSE);
2431
9593a4c9 2432 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
941aa6e9 2433
e4afc73a
EB
2434 radio->name = vik_layer_get_interface(i)->tools[j].name;
2435 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
4c77d5e0 2436 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
e4afc73a 2437 radio->accelerator = NULL;
4c77d5e0 2438 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
e4afc73a
EB
2439 radio->value = ntools;
2440 }
2441 }
ee36ac4f 2442 g_object_unref (icon_factory);
50a14534 2443
e4afc73a
EB
2444 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2445 g_free(tools);
50a14534 2446
e4afc73a 2447 gtk_ui_manager_insert_action_group (uim, action_group, 0);
50a14534 2448
79845167
QT
2449 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2450 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2451 GtkAction *action = gtk_action_group_get_action(action_group,
2452 vik_layer_get_interface(i)->tools[j].name);
2453 g_object_set(action, "sensitive", FALSE, NULL);
2454 }
2455 }
2456 window->action_group = action_group;
2457
e4afc73a
EB
2458 accel_group = gtk_ui_manager_get_accel_group (uim);
2459 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2460 gtk_ui_manager_ensure_update (uim);
13505702
GB
2461
2462 setup_recent_files(window);
e4afc73a 2463}
50a14534 2464
50a14534 2465
941aa6e9 2466
e4afc73a 2467static struct {
4bd45256 2468 const GdkPixdata *data;
e4afc73a
EB
2469 gchar *stock_id;
2470} stock_icons[] = {
5bfafde9
GB
2471 { &begintr_18_pixbuf, "Begin Track" },
2472 { &iscissors_18_pixbuf, "Magic Scissors" },
2473 { &mover_22_pixbuf, "vik-icon-pan" },
2474 { &demdl_18_pixbuf, "DEM Download/Import" },
2475 { &showpic_18_pixbuf, "Show Picture" },
2476 { &addtr_18_pixbuf, "Create Track" },
2477 { &edtr_18_pixbuf, "Edit Trackpoint" },
2478 { &addwp_18_pixbuf, "Create Waypoint" },
2479 { &edwp_18_pixbuf, "Edit Waypoint" },
2480 { &zoom_18_pixbuf, "vik-icon-zoom" },
2481 { &ruler_18_pixbuf, "vik-icon-ruler" },
2482 { &geozoom_18_pixbuf, "Georef Zoom Tool" },
2483 { &geomove_18_pixbuf, "Georef Move Map" },
2484 { &mapdl_18_pixbuf, "Maps Download" },
2485 { &showpic_18_pixbuf, "Show Picture" },
e4afc73a
EB
2486};
2487
e4afc73a
EB
2488static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2489
2490static void
2491register_vik_icons (GtkIconFactory *icon_factory)
2492{
2493 GtkIconSet *icon_set;
e4afc73a 2494 gint i;
e4afc73a
EB
2495
2496 for (i = 0; i < n_stock_icons; i++) {
4bd45256
EB
2497 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2498 stock_icons[i].data, FALSE, NULL ));
e4afc73a
EB
2499 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2500 gtk_icon_set_unref (icon_set);
2501 }
50a14534 2502}
bce3a7b0 2503