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