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