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