]> git.street.me.uk Git - andy/viking.git/blame - src/vikwindow.c
Improve the associated icon, remove the scissor effort and use something to try to...
[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 */
aa7ed888 1139 GList* gl = vik_layers_panel_get_all_layers_of_type ( t->vw->viking_vlp, VIK_LAYER_TRW, FALSE ); // Don't get invisible layers
a47bfefa
RN
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{
169acf64
RN
1322 vik_layers_panel_cut_selected ( vw->viking_vlp );
1323 draw_update ( vw );
1324 vw->modified = TRUE;
50a14534
EB
1325}
1326
e4afc73a 1327static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1328{
1329 if ( a_clipboard_paste ( vw->viking_vlp ) )
1330 {
1331 draw_update ( vw );
1332 vw->modified = TRUE;
1333 }
1334}
1335
e4afc73a 1336static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1337{
1338 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
4c77d5e0 1339 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
50a14534
EB
1340}
1341
5ff75d1e
GB
1342static void help_help_cb ( GtkAction *a, VikWindow *vw )
1343{
6ace3182
MA
1344#ifdef WINDOWS
1345 ShellExecute(NULL, "open", ""PACKAGE".pdf", NULL, NULL, SW_SHOWNORMAL);
1346#else /* WINDOWS */
5ff75d1e
GB
1347#if GTK_CHECK_VERSION (2, 14, 0)
1348 gchar *uri;
1349 uri = g_strdup_printf("ghelp:%s", PACKAGE);
1350 gtk_show_uri(NULL, uri, GDK_CURRENT_TIME, NULL);
1351 g_free(uri);
1352#endif
6ace3182 1353#endif /* WINDOWS */
5ff75d1e
GB
1354}
1355
d0a5f320
AF
1356static void help_about_cb ( GtkAction *a, VikWindow *vw )
1357{
1358 a_dialog_about(GTK_WINDOW(vw));
1359}
1360
e4afc73a 1361static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1362{
1363 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1364 {
1365 vik_layers_panel_delete_selected ( vw->viking_vlp );
1366 vw->modified = TRUE;
1367 }
1368 else
4c77d5e0 1369 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
50a14534
EB
1370}
1371
181f5d0c
MA
1372static void view_side_panel_cb ( GtkAction *a, VikWindow *vw )
1373{
48df6aa3 1374 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewSidePanel" );
181f5d0c
MA
1375 g_assert(check_box);
1376 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1377 if ( state )
1378 gtk_widget_show(GTK_WIDGET(vw->viking_vlp));
1379 else
1380 gtk_widget_hide(GTK_WIDGET(vw->viking_vlp));
1381}
1382
a459ee10
RN
1383static void view_statusbar_cb ( GtkAction *a, VikWindow *vw )
1384{
48df6aa3 1385 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewStatusBar" );
a459ee10
RN
1386 if ( !check_box )
1387 return;
1388 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
1389 if ( state )
1390 gtk_widget_show ( GTK_WIDGET(vw->viking_vs) );
1391 else
1392 gtk_widget_hide ( GTK_WIDGET(vw->viking_vs) );
1393}
1394
e7591765
RN
1395static void view_toolbar_cb ( GtkAction *a, VikWindow *vw )
1396{
48df6aa3 1397 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewToolbar" );
e7591765
RN
1398 if ( !check_box )
1399 return;
1400 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
1401 if ( state )
1402 gtk_widget_show ( GTK_WIDGET(vw->toolbar) );
1403 else
1404 gtk_widget_hide ( GTK_WIDGET(vw->toolbar) );
1405}
1406
7622022f
RN
1407static void view_main_menu_cb ( GtkAction *a, VikWindow *vw )
1408{
48df6aa3 1409 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ViewMainMenu" );
7622022f
RN
1410 if ( !check_box )
1411 return;
1412 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box) );
1413 if ( !state )
1414 gtk_widget_hide ( gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu" ) );
1415 else
1416 gtk_widget_show ( gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu" ) );
1417}
1418
941aa6e9
AF
1419/***************************************
1420 ** tool management routines
1421 **
1422 ***************************************/
1423
1424static toolbox_tools_t* toolbox_create(VikWindow *vw)
1425{
1426 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1427 vt->tools = NULL;
1428 vt->n_tools = 0;
1429 vt->active_tool = -1;
1430 vt->vw = vw;
1431 if (!vw->viking_vvp) {
7742da66 1432 g_critical("no viewport found.");
941aa6e9
AF
1433 exit(1);
1434 }
1435 return vt;
1436}
1437
9593a4c9 1438static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
941aa6e9
AF
1439{
1440 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1441 vt->tools[vt->n_tools].ti = *vti;
9593a4c9 1442 vt->tools[vt->n_tools].layer_type = layer_type;
941aa6e9
AF
1443 if (vti->create) {
1444 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1445 }
1446 else {
1447 vt->tools[vt->n_tools].state = NULL;
1448 }
1449 vt->n_tools++;
1450}
1451
1452static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1453{
1454 int i;
1455 for (i=0; i<vt->n_tools; i++) {
1456 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1457 break;
1458 }
1459 }
1460 return i;
1461}
1462
1463static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1464{
1465 int tool = toolbox_get_tool(vt, tool_name);
1466 toolbox_tool_t *t = &vt->tools[tool];
1467 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1468
1469 if (tool == vt->n_tools) {
7742da66 1470 g_critical("trying to activate a non-existent tool...");
941aa6e9
AF
1471 exit(1);
1472 }
1473 /* is the tool already active? */
1474 if (vt->active_tool == tool) {
1475 return;
1476 }
1477
1478 if (vt->active_tool != -1) {
1479 if (vt->tools[vt->active_tool].ti.deactivate) {
1480 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1481 }
1482 }
1483 if (t->ti.activate) {
1484 t->ti.activate(vl, t->state);
1485 }
1486 vt->active_tool = tool;
1487}
1488
f2f2f7bf
GB
1489static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
1490{
1491 int tool = toolbox_get_tool(vt, tool_name);
1492 toolbox_tool_t *t = &vt->tools[tool];
1493 if (t->ti.cursor == NULL) {
1494 if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
1495 GError *cursor_load_err = NULL;
1496 GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
1497 /* TODO: settable offeset */
1498 t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
1499 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1500 } else {
1501 t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
1502 }
1503 }
1504 return t->ti.cursor;
1505}
1506
941aa6e9
AF
1507static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1508{
1509 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1510 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
9593a4c9
EB
1511 gint ltype = vt->tools[vt->active_tool].layer_type;
1512 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1513 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
941aa6e9
AF
1514 }
1515}
1516
dc2c040e 1517static void toolbox_move (toolbox_tools_t *vt, GdkEventMotion *event)
941aa6e9
AF
1518{
1519 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1520 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
9593a4c9
EB
1521 gint ltype = vt->tools[vt->active_tool].layer_type;
1522 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
165d30aa
EB
1523 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1524 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
941aa6e9
AF
1525 }
1526}
1527
1528static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1529{
1530 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
9593a4c9
EB
1531 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1532 gint ltype = vt->tools[vt->active_tool].layer_type;
1533 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1534 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
941aa6e9
AF
1535 }
1536}
1537/** End tool management ************************************/
1538
8fb71d6c
EB
1539void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1540{
1541 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1542}
941aa6e9
AF
1543
1544/* this function gets called whenever a toolbar tool is clicked */
e4afc73a 1545static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
50a14534
EB
1546{
1547 /* White Magic, my friends ... White Magic... */
8fb71d6c 1548 int layer_id, tool_id;
f2f2f7bf 1549 const GdkCursor *cursor = NULL;
e4afc73a 1550
941aa6e9
AF
1551 toolbox_activate(vw->vt, gtk_action_get_name(a));
1552
f2f2f7bf
GB
1553 cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
1554 /* We set cursor, even if it is NULL: it resets to default */
9b5dcb38 1555 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, (GdkCursor *)cursor );
f2f2f7bf 1556
576cbd17
GB
1557 if (!strcmp(gtk_action_get_name(a), "Pan")) {
1558 vw->current_tool = TOOL_PAN;
576cbd17
GB
1559 }
1560 else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
e4afc73a
EB
1561 vw->current_tool = TOOL_ZOOM;
1562 }
1563 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1564 vw->current_tool = TOOL_RULER;
1565 }
a47bfefa
RN
1566 else if (!strcmp(gtk_action_get_name(a), "Select")) {
1567 vw->current_tool = TOOL_SELECT;
1568 }
e4afc73a 1569 else {
79845167 1570 /* TODO: only enable tools from active layer */
8fb71d6c
EB
1571 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1572 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1573 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1574 vw->current_tool = TOOL_LAYER;
1575 vw->tool_layer_id = layer_id;
1576 vw->tool_tool_id = tool_id;
e4afc73a
EB
1577 }
1578 }
1579 }
1580 }
50a14534
EB
1581 draw_status ( vw );
1582}
1583
1584static void window_set_filename ( VikWindow *vw, const gchar *filename )
1585{
1586 gchar *title;
4c77d5e0 1587 const gchar *file;
50a14534
EB
1588 if ( vw->filename )
1589 g_free ( vw->filename );
1590 if ( filename == NULL )
1591 {
1592 vw->filename = NULL;
4c77d5e0 1593 file = _("Untitled");
50a14534
EB
1594 }
1595 else
1596 {
1597 vw->filename = g_strdup(filename);
4c77d5e0 1598 file = a_file_basename ( filename );
50a14534 1599 }
4c77d5e0
GB
1600 title = g_strdup_printf( "%s - Viking", file );
1601 gtk_window_set_title ( GTK_WINDOW(vw), title );
1602 g_free ( title );
50a14534
EB
1603}
1604
7bc965c0
GB
1605GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1606{
1607 GtkWidget *mode_button;
1608 gchar *buttonname;
1609 switch ( mode ) {
794c8f18 1610#ifdef VIK_CONFIG_EXPEDIA
7bc965c0 1611 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
794c8f18 1612#endif
7bc965c0 1613 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
d587678a 1614 case VIK_VIEWPORT_DRAWMODE_LATLON: buttonname = "/ui/MainMenu/View/ModeLatLon"; break;
794c8f18 1615 default: buttonname = "/ui/MainMenu/View/ModeUTM";
7bc965c0
GB
1616 }
1617 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1618 g_assert ( mode_button );
1619 return mode_button;
1620}
1621
01da6b4d
GB
1622/**
1623 * vik_window_get_pan_move:
1624 * @vw: some VikWindow
1625 *
1626 * Retrieves @vw's pan_move.
1627 *
1628 * Should be removed as soon as possible.
1629 *
1630 * Returns: @vw's pan_move
1631 *
1632 * Since: 0.9.96
1633 **/
1c6a6010
GB
1634gboolean vik_window_get_pan_move ( VikWindow *vw )
1635{
1636 return vw->pan_move;
1637}
1638
13505702
GB
1639static void on_activate_recent_item (GtkRecentChooser *chooser,
1640 VikWindow *self)
1641{
1642 gchar *filename;
1643
1644 filename = gtk_recent_chooser_get_current_uri (chooser);
1645 if (filename != NULL)
1646 {
1647 GFile *file = g_file_new_for_uri ( filename );
1648 gchar *path = g_file_get_path ( file );
1649 g_object_unref ( file );
1650 if ( self->filename )
1651 {
1652 gchar *filenames[] = { path, NULL };
1653 g_signal_emit ( G_OBJECT(self), window_signals[VW_OPENWINDOW_SIGNAL], 0, filenames );
1654 }
1655 else
756d53f5 1656 vik_window_open_file ( self, path, TRUE );
13505702
GB
1657 g_free ( path );
1658 }
1659
1660 g_free (filename);
1661}
1662
1663static void setup_recent_files (VikWindow *self)
1664{
1665 GtkRecentManager *manager;
1666 GtkRecentFilter *filter;
1667 GtkWidget *menu, *menu_item;
1668
1669 filter = gtk_recent_filter_new ();
1670 /* gtk_recent_filter_add_application (filter, g_get_application_name()); */
1671 gtk_recent_filter_add_group(filter, "viking");
1672
1673 manager = gtk_recent_manager_get_default ();
1674 menu = gtk_recent_chooser_menu_new_for_manager (manager);
1675 gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu), GTK_RECENT_SORT_MRU);
1676 gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu), filter);
1677
1678 menu_item = gtk_ui_manager_get_widget (self->uim, "/ui/MainMenu/File/OpenRecentFile");
1679 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
1680
1681 g_signal_connect (G_OBJECT (menu), "item-activated",
1682 G_CALLBACK (on_activate_recent_item), (gpointer) self);
1683}
1684
1685static void update_recently_used_document(const gchar *filename)
1686{
1687 /* Update Recently Used Document framework */
1688 GtkRecentManager *manager = gtk_recent_manager_get_default();
1689 GtkRecentData *recent_data = g_slice_new (GtkRecentData);
1690 gchar *groups[] = {"viking", NULL};
1691 GFile *file = g_file_new_for_commandline_arg(filename);
1692 gchar *uri = g_file_get_uri(file);
1693 gchar *basename = g_path_get_basename(filename);
1694 g_object_unref(file);
1695 file = NULL;
1696
1697 recent_data->display_name = basename;
1698 recent_data->description = NULL;
1699 recent_data->mime_type = "text/x-gps-data";
1700 recent_data->app_name = (gchar *) g_get_application_name ();
1701 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%f", NULL);
1702 recent_data->groups = groups;
1703 recent_data->is_private = FALSE;
1704 if (!gtk_recent_manager_add_full (manager, uri, recent_data))
1705 {
1706 g_warning (_("Unable to add '%s' to the list of recently used documents"), uri);
1707 }
1708
1709 g_free (uri);
1710 g_free (basename);
1711 g_free (recent_data->app_exec);
1712 g_slice_free (GtkRecentData, recent_data);
1713}
1714
50a14534
EB
1715void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1716{
1717 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1718 {
ba9d0a00 1719 case LOAD_TYPE_READ_FAILURE:
4c77d5e0 1720 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
50a14534 1721 break;
ba9d0a00
RN
1722 case LOAD_TYPE_GPSBABEL_FAILURE:
1723 a_dialog_error_msg ( GTK_WINDOW(vw), _("GPSBabel is required to load files of this type or GPSBabel encountered problems.") );
1724 break;
1725 case LOAD_TYPE_VIK_SUCCESS:
50a14534
EB
1726 {
1727 GtkWidget *mode_button;
13505702 1728 /* Update UI */
50a14534
EB
1729 if ( change_filename )
1730 window_set_filename ( vw, filename );
7bc965c0 1731 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
50a14534
EB
1732 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. */
1733 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1734 vw->only_updating_coord_mode_ui = FALSE;
1735
1736 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
2afcef36 1737
48df6aa3 1738 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowScale" );
1657065a
QT
1739 g_assert ( mode_button );
1740 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1741
48df6aa3 1742 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowCenterMark" );
1657065a
QT
1743 g_assert ( mode_button );
1744 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
2afcef36
RN
1745
1746 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowHighlight" );
1747 g_assert ( mode_button );
1748 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_highlight (vw->viking_vvp) );
50a14534 1749 }
ba9d0a00 1750 //case LOAD_TYPE_OTHER_SUCCESS:
13505702
GB
1751 default:
1752 update_recently_used_document(filename);
1753 draw_update ( vw );
ba9d0a00 1754 break;
50a14534
EB
1755 }
1756}
e4afc73a 1757static void load_file ( GtkAction *a, VikWindow *vw )
50a14534 1758{
6e4a49aa
MA
1759 GSList *files = NULL;
1760 GSList *cur_file = NULL;
e4afc73a
EB
1761 gboolean newwindow;
1762 if (!strcmp(gtk_action_get_name(a), "Open")) {
1763 newwindow = TRUE;
1764 }
1765 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1766 newwindow = FALSE;
1767 }
1768 else {
8dc8da82 1769 g_critical("Houston, we've had a problem.");
e4afc73a
EB
1770 return;
1771 }
1772
50a14534
EB
1773 if ( ! vw->open_dia )
1774 {
6e4a49aa
MA
1775 vw->open_dia = gtk_file_chooser_dialog_new (_("Please select a GPS data file to open. "),
1776 GTK_WINDOW(vw),
1777 GTK_FILE_CHOOSER_ACTION_OPEN,
1778 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1779 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1780 NULL);
1781 gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(vw->open_dia), TRUE );
50a14534
EB
1782 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1783 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1784 }
6e4a49aa 1785 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534
EB
1786 {
1787 gtk_widget_hide ( vw->open_dia );
a5fd2196 1788#ifdef VIKING_PROMPT_IF_MODIFIED
50a14534 1789 if ( (vw->modified || vw->filename) && newwindow )
a5fd2196
QT
1790#else
1791 if ( vw->filename && newwindow )
1792#endif
6e4a49aa 1793 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) ) );
50a14534 1794 else {
6e4a49aa
MA
1795 files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) );
1796 gboolean change_fn = newwindow && (g_slist_length(files)==1); /* only change fn if one file */
1797
1798 cur_file = files;
1799 while ( cur_file ) {
1800 gchar *file_name = cur_file->data;
1801 vik_window_open_file ( vw, file_name, change_fn );
1802 g_free (file_name);
1803 cur_file = g_slist_next (cur_file);
50a14534 1804 }
6e4a49aa 1805 g_slist_free (files);
50a14534
EB
1806 }
1807 }
1808 else
1809 gtk_widget_hide ( vw->open_dia );
1810}
1811
e4afc73a 1812static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
50a14534
EB
1813{
1814 gboolean rv = FALSE;
1815 const gchar *fn;
1816 if ( ! vw->save_dia )
1817 {
6e4a49aa
MA
1818 vw->save_dia = gtk_file_chooser_dialog_new (_("Save as Viking File."),
1819 GTK_WINDOW(vw),
1820 GTK_FILE_CHOOSER_ACTION_SAVE,
1821 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1822 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1823 NULL);
50a14534
EB
1824 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1825 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1826 }
1827
6e4a49aa 1828 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534 1829 {
6e4a49aa 1830 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
d91e5f2b 1831 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
1832 {
1833 window_set_filename ( vw, fn );
1834 rv = window_save ( vw );
1835 vw->modified = FALSE;
1836 break;
1837 }
1838 }
1839 gtk_widget_hide ( vw->save_dia );
1840 return rv;
1841}
1842
1843static gboolean window_save ( VikWindow *vw )
1844{
1845 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
13505702
GB
1846 {
1847 update_recently_used_document ( vw->filename );
50a14534 1848 return TRUE;
13505702 1849 }
50a14534
EB
1850 else
1851 {
4c77d5e0 1852 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
50a14534
EB
1853 return FALSE;
1854 }
1855}
1856
e4afc73a 1857static gboolean save_file ( GtkAction *a, VikWindow *vw )
50a14534
EB
1858{
1859 if ( ! vw->filename )
e4afc73a 1860 return save_file_as ( NULL, vw );
50a14534
EB
1861 else
1862 {
1863 vw->modified = FALSE;
1864 return window_save ( vw );
1865 }
1866}
1867
1d1bc3c1
EB
1868static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1869{
7b3479e3
EB
1870 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1871}
1872
1873static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1874{
1875 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1d1bc3c1
EB
1876}
1877
1ef9e637 1878#ifdef VIK_CONFIG_GEOCACHES
3333c069
EB
1879static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1880{
1881 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1882}
1ef9e637 1883#endif
3333c069 1884
5210c3d3
GB
1885static void goto_default_location( GtkAction *a, VikWindow *vw)
1886{
1887 struct LatLon ll;
1888 ll.lat = a_vik_get_default_lat();
1889 ll.lon = a_vik_get_default_long();
1890 vik_viewport_set_center_latlon(vw->viking_vvp, &ll);
1891 vik_layers_panel_emit_update(vw->viking_vlp);
1892}
1893
1894
369126f3
QT
1895static void goto_address( GtkAction *a, VikWindow *vw)
1896{
34e71b99 1897 a_vik_goto(vw, vw->viking_vlp, vw->viking_vvp);
369126f3
QT
1898}
1899
7c259702
JJ
1900static void mapcache_flush_cb ( GtkAction *a, VikWindow *vw )
1901{
1902 a_mapcache_flush();
1903}
1904
17a1f8f9
EB
1905static void preferences_cb ( GtkAction *a, VikWindow *vw )
1906{
9be0449f
RN
1907 gboolean wp_icon_size = a_vik_get_use_large_waypoint_icons();
1908
17a1f8f9 1909 a_preferences_show_window ( GTK_WINDOW(vw) );
9be0449f
RN
1910
1911 // Delete icon indexing 'cache' and so automatically regenerates with the new setting when changed
1912 if (wp_icon_size != a_vik_get_use_large_waypoint_icons())
1913 clear_garmin_icon_syms ();
1914
6f9336aa 1915 draw_update ( vw );
17a1f8f9
EB
1916}
1917
5210c3d3
GB
1918static void default_location_cb ( GtkAction *a, VikWindow *vw )
1919{
1920 /* Simplistic repeat of preference setting
1921 Only the name & type are important for setting the preference via this 'external' way */
1922 VikLayerParam pref_lat[] = {
1923 { VIKING_PREFERENCES_NAMESPACE "default_latitude",
1924 VIK_LAYER_PARAM_DOUBLE,
1925 VIK_LOCATION_LAT,
1926 NULL,
1927 VIK_LAYER_WIDGET_SPINBUTTON,
1928 NULL,
1929 NULL },
1930 };
1931 VikLayerParam pref_lon[] = {
1932 { VIKING_PREFERENCES_NAMESPACE "default_longitude",
1933 VIK_LAYER_PARAM_DOUBLE,
1934 VIK_LOCATION_LONG,
1935 NULL,
1936 VIK_LAYER_WIDGET_SPINBUTTON,
1937 NULL,
1938 NULL },
1939 };
1940
1941 /* Get current center */
1942 struct LatLon ll;
1943 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &ll );
1944
1945 /* Apply to preferences */
1946 VikLayerParamData vlp_data;
1947 vlp_data.d = ll.lat;
1948 a_preferences_run_setparam (vlp_data, pref_lat);
1949 vlp_data.d = ll.lon;
1950 a_preferences_run_setparam (vlp_data, pref_lon);
1951 /* Remember to save */
1952 a_preferences_save_to_file();
1953}
1954
e4afc73a 1955static void clear_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1956{
1957 vik_layers_panel_clear ( vw->viking_vlp );
1958 window_set_filename ( vw, NULL );
1959 draw_update ( vw );
1960}
1961
e4afc73a 1962static void window_close ( GtkAction *a, VikWindow *vw )
50a14534
EB
1963{
1964 if ( ! delete_event ( vw ) )
1965 gtk_widget_destroy ( GTK_WIDGET(vw) );
1966}
1967
2bf7cadd
QT
1968static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1969{
7bb60307 1970 if (save_file( NULL, vw)) {
2bf7cadd 1971 window_close( NULL, vw);
7bb60307
QT
1972 return(TRUE);
1973 }
2bf7cadd
QT
1974 else
1975 return(FALSE);
1976}
1977
e4afc73a 1978static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
1979{
1980 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1981 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1982 {
1983 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1984 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1985 draw_update ( vw );
1986 }
1987}
1988
1989static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1990{
1991 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1992 GdkPixbuf *pixbuf_to_save;
1993 gdouble old_xmpp, old_ympp;
1994 GError *error = NULL;
1995
1996 /* backup old zoom & set new */
1997 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1998 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1999 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
2000
2001 /* reset width and height: */
2002 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
2003
2004 /* draw all layers */
2005 draw_redraw ( vw );
2006
2007 /* save buffer as file. */
2008 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);
2009 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
2010 if (error)
2011 {
7742da66 2012 g_warning("Unable to write to file %s: %s", fn, error->message );
50a14534
EB
2013 g_error_free (error);
2014 }
2015 g_object_unref ( G_OBJECT(pixbuf_to_save) );
2016
2017 /* pretend like nothing happened ;) */
2018 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
2019 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
2020 vik_viewport_configure ( vw->viking_vvp );
2021 draw_update ( vw );
2022}
2023
2024static 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 )
2025{
2026 gulong size = sizeof(gchar) * (strlen(fn) + 15);
2027 gchar *name_of_file = g_malloc ( size );
2028 guint x = 1, y = 1;
2029 struct UTM utm_orig, utm;
2030
2031 /* *** copied from above *** */
2032 GdkPixbuf *pixbuf_to_save;
2033 gdouble old_xmpp, old_ympp;
2034 GError *error = NULL;
2035
2036 /* backup old zoom & set new */
2037 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
2038 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
2039 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
2040
2041 /* reset width and height: do this only once for all images (same size) */
2042 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
2043 /* *** end copy from above *** */
2044
2045 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
2046
f83131b9 2047 g_mkdir(fn,0777);
50a14534
EB
2048
2049 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
2050
2051 for ( y = 1; y <= tiles_h; y++ )
2052 {
2053 for ( x = 1; x <= tiles_w; x++ )
2054 {
3d9454e6 2055 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
2056 utm = utm_orig;
2057 if ( tiles_w & 0x1 )
2058 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
2059 else
2060 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
2061 if ( tiles_h & 0x1 ) /* odd */
2062 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
2063 else /* even */
2064 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
2065
2066 /* move to correct place. */
2067 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
2068
2069 draw_redraw ( vw );
2070
2071 /* save buffer as file. */
2072 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);
2073 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
2074 if (error)
2075 {
7742da66 2076 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
50a14534
EB
2077 g_error_free (error);
2078 }
2079
2080 g_object_unref ( G_OBJECT(pixbuf_to_save) );
2081 }
2082 }
2083
2084 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
2085 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
2086 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
2087 vik_viewport_configure ( vw->viking_vvp );
2088 draw_update ( vw );
2089
2090 g_free ( name_of_file );
2091}
2092
2093static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
2094{
2095 VikWindow *vw = VIK_WINDOW(pass_along[0]);
2096 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
2097 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
2098 gdouble width_min, width_max, height_min, height_max;
2099 gint width, height;
2100
2101 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
2102 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
2103
2104 /* TODO: support for xzoom and yzoom values */
2105 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
2106 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
2107
2108 if ( width > width_max || width < width_min || height > height_max || height < height_min )
4c77d5e0 2109 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
50a14534
EB
2110
2111 gtk_spin_button_set_value ( width_spin, width );
2112 gtk_spin_button_set_value ( height_spin, height );
2113}
2114
2115static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
2116{
2117 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
2118 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
2119 gchar *label_text;
2120 gdouble w, h;
2121 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
2122 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
2123 if (pass_along[4]) /* save many images; find TOTAL area covered */
2124 {
2125 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
2126 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
2127 }
6f9336aa
RN
2128 vik_units_distance_t dist_units = a_vik_get_units_distance ();
2129 switch (dist_units) {
2130 case VIK_UNITS_DISTANCE_KILOMETRES:
2131 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
2132 break;
2133 case VIK_UNITS_DISTANCE_MILES:
2134 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. miles)"), (glong)w, (glong)h, (w*h/2589988.11));
2135 break;
2136 default:
2137 label_text = g_strdup_printf ("Just to keep the compiler happy");
2138 g_critical("Houston, we've had a problem. distance=%d", dist_units);
2139 }
2140
50a14534
EB
2141 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
2142 g_free ( label_text );
2143}
2144
2145static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
2146{
2147 /* todo: default for answers inside VikWindow or static (thruout instance) */
4c77d5e0 2148 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
50a14534
EB
2149 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
2150 GTK_STOCK_CANCEL,
2151 GTK_RESPONSE_REJECT,
2152 GTK_STOCK_OK,
2153 GTK_RESPONSE_ACCEPT,
10888930 2154 NULL );
50a14534
EB
2155 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
2156 GtkWidget *png_radio, *jpeg_radio;
2157 GtkWidget *current_window_button;
2158 gpointer current_window_pass_along[7];
2159 GtkWidget *zoom_label, *zoom_spin;
2160 GtkWidget *total_size_label;
2161
2162 /* only used if (!one_image_only) */
886031df 2163 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
50a14534
EB
2164
2165
4c77d5e0 2166 width_label = gtk_label_new ( _("Width (pixels):") );
50a14534 2167 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
4c77d5e0 2168 height_label = gtk_label_new ( _("Height (pixels):") );
50a14534
EB
2169 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
2170
4c77d5e0 2171 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
50a14534 2172 /* TODO: separate xzoom and yzoom factors */
ac33062d 2173 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
2174
2175 total_size_label = gtk_label_new ( NULL );
2176
4c77d5e0 2177 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
50a14534
EB
2178 current_window_pass_along [0] = vw;
2179 current_window_pass_along [1] = width_spin;
2180 current_window_pass_along [2] = height_spin;
2181 current_window_pass_along [3] = zoom_spin;
2182 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
2183 current_window_pass_along [5] = NULL;
2184 current_window_pass_along [6] = total_size_label;
2185 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
2186
4c77d5e0
GB
2187 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
2188 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
50a14534
EB
2189
2190 if ( ! vw->draw_image_save_as_png )
2191 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
2192
2193 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
2194 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
2195 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
2196 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
2197 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
2198 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
2199 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
2200 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
2201 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
2202
2203 if ( ! one_image_only )
2204 {
2205 GtkWidget *tiles_width_label, *tiles_height_label;
2206
2207
4c77d5e0 2208 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
50a14534 2209 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
4c77d5e0 2210 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
50a14534
EB
2211 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
2212 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
2213 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
2214 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
2215 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
2216
2217 current_window_pass_along [4] = tiles_width_spin;
2218 current_window_pass_along [5] = tiles_height_spin;
2219 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2220 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2221 }
2222 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
2223 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2224 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2225 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
2226
2227 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
2228
1fb999d3
RN
2229 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
2230
50a14534
EB
2231 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
2232
2233 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
2234 {
2235 gtk_widget_hide ( GTK_WIDGET(dialog) );
2236 if ( one_image_only )
2237 save_image_file ( vw, fn,
2238 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
2239 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
2240 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
2241 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
2242 else {
2243 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
2244 save_image_dir ( vw, fn,
2245 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
2246 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
2247 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
2248 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
2249 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
2250 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
2251 else
4c77d5e0 2252 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
50a14534
EB
2253 }
2254 }
2255 gtk_widget_destroy ( GTK_WIDGET(dialog) );
2256}
2257
2258
e4afc73a 2259static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
50a14534 2260{
ca9e25af 2261 gchar *fn;
f2a1ca71 2262 if (!vw->save_img_dia) {
6e4a49aa
MA
2263 vw->save_img_dia = gtk_file_chooser_dialog_new (_("Save Image"),
2264 GTK_WINDOW(vw),
2265 GTK_FILE_CHOOSER_ACTION_SAVE,
2266 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2267 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
2268 NULL);
f2a1ca71
QT
2269 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
2270 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
2271 }
50a14534 2272
6e4a49aa 2273 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534 2274 {
6e4a49aa 2275 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
d91e5f2b 2276 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 2277 {
50a14534
EB
2278 draw_to_image_file ( vw, fn, TRUE );
2279 break;
2280 }
fc759827
GB
2281 g_free(fn);
2282 fn = NULL;
50a14534 2283 }
f2a1ca71 2284 gtk_widget_hide ( vw->save_img_dia );
50a14534
EB
2285}
2286
e4afc73a 2287static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
50a14534 2288{
6e4a49aa
MA
2289 gchar *fn = NULL;
2290
f2a1ca71 2291 if (!vw->save_img_dir_dia) {
6e4a49aa
MA
2292 vw->save_img_dir_dia = gtk_file_chooser_dialog_new (_("Choose a directory to hold images"),
2293 GTK_WINDOW(vw),
2294 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
2295 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2296 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
2297 NULL);
f2a1ca71
QT
2298 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
2299 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
2300 }
6e4a49aa
MA
2301
2302 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_ACCEPT )
50a14534 2303 {
6e4a49aa
MA
2304 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dir_dia) );
2305 if ( fn )
50a14534 2306 {
50a14534 2307 draw_to_image_file ( vw, fn, FALSE );
6e4a49aa
MA
2308 g_free(fn);
2309 fn = NULL;
50a14534
EB
2310 break;
2311 }
2312 }
f2a1ca71 2313 gtk_widget_hide ( vw->save_img_dir_dia );
50a14534
EB
2314}
2315
9a995996 2316#if GTK_CHECK_VERSION(2,10,0)
42f34743
QT
2317static void print_cb ( GtkAction *a, VikWindow *vw )
2318{
2319 a_print(vw, vw->viking_vvp);
2320}
9a995996 2321#endif
42f34743 2322
50a14534 2323/* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
e4afc73a 2324static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
50a14534 2325{
e4afc73a
EB
2326 VikViewportDrawMode drawmode;
2327 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
2328 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
2329 }
d587678a
GB
2330 else if (!strcmp(gtk_action_get_name(a), "ModeLatLon")) {
2331 drawmode = VIK_VIEWPORT_DRAWMODE_LATLON;
2332 }
e4afc73a
EB
2333 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
2334 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
2335 }
e4afc73a
EB
2336 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
2337 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
2338 }
2339 else {
8dc8da82 2340 g_critical("Houston, we've had a problem.");
e4afc73a
EB
2341 return;
2342 }
2343
50a14534
EB
2344 if ( !vw->only_updating_coord_mode_ui )
2345 {
2346 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
2347 if ( olddrawmode != drawmode )
2348 {
2349 /* this takes care of coord mode too */
2350 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
2351 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
2352 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
2353 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
2354 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
2355 }
2356 draw_update ( vw );
2357 }
2358 }
2359}
2360
35c7c0ba
EB
2361static void set_draw_scale ( GtkAction *a, VikWindow *vw )
2362{
48df6aa3 2363 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowScale" );
1657065a
QT
2364 g_assert(check_box);
2365 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2366 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
35c7c0ba
EB
2367 draw_update ( vw );
2368}
2369
c933487f
QT
2370static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
2371{
48df6aa3 2372 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowCenterMark" );
1657065a
QT
2373 g_assert(check_box);
2374 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2375 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
c933487f
QT
2376 draw_update ( vw );
2377}
2378
2afcef36
RN
2379static void set_draw_highlight ( GtkAction *a, VikWindow *vw )
2380{
2381 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/SetShow/ShowHighlight" );
2382 g_assert(check_box);
2383 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
2384 vik_viewport_set_draw_highlight ( vw->viking_vvp, state );
2385 draw_update ( vw );
2386}
2387
e4afc73a 2388static void set_bg_color ( GtkAction *a, VikWindow *vw )
50a14534 2389{
4c77d5e0 2390 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
50a14534
EB
2391 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
2392 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2393 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2394 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
2395 {
2396 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2397 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
2398 draw_update ( vw );
2399 }
2400 g_free ( color );
2401 gtk_widget_destroy ( colorsd );
2402}
2403
480fb7e1
RN
2404static void set_highlight_color ( GtkAction *a, VikWindow *vw )
2405{
2406 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a track highlight color") );
2407 GdkColor *color = vik_viewport_get_highlight_gdkcolor ( vw->viking_vvp );
2408 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2409 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2410 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
2411 {
2412 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
2413 vik_viewport_set_highlight_gdkcolor ( vw->viking_vvp, color );
2414 draw_update ( vw );
2415 }
2416 g_free ( color );
2417 gtk_widget_destroy ( colorsd );
2418}
2419
941aa6e9
AF
2420
2421
2422/***********************************************************************************************
2423 ** GUI Creation
2424 ***********************************************************************************************/
2425
e4afc73a 2426static GtkActionEntry entries[] = {
5515e2d3
JJ
2427 { "File", NULL, N_("_File"), 0, 0, 0 },
2428 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
2429 { "View", NULL, N_("_View"), 0, 0, 0 },
48df6aa3 2430 { "SetShow", NULL, N_("_Show"), 0, 0, 0 },
5515e2d3
JJ
2431 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
2432 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
2433 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
2434 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
92806042 2435 { "Exttools", NULL, N_("_Webtools"), 0, 0, 0 },
5515e2d3
JJ
2436 { "Help", NULL, N_("_Help"), 0, 0, 0 },
2437
2438 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
06526f88
RN
2439 { "Open", GTK_STOCK_OPEN, N_("_Open..."), "<control>O", N_("Open a file"), (GCallback)load_file },
2440 { "OpenRecentFile", NULL, N_("Open _Recent File"), NULL, NULL, (GCallback)NULL },
2441 { "Append", GTK_STOCK_ADD, N_("Append _File..."), NULL, N_("Append data from a different file"), (GCallback)load_file },
5515e2d3 2442 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
06526f88
RN
2443 { "AcquireGPS", NULL, N_("From _GPS..."), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
2444 { "AcquireGoogle", NULL, N_("Google _Directions..."), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1ef9e637 2445#ifdef VIK_CONFIG_GEOCACHES
06526f88 2446 { "AcquireGC", NULL, N_("Geo_caches..."), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1ef9e637 2447#endif
5515e2d3 2448 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
06526f88
RN
2449 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As..."), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
2450 { "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 },
2451 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images..."), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
9a995996
EB
2452
2453#if GTK_CHECK_VERSION(2,10,0)
5515e2d3 2454 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
9a995996
EB
2455#endif
2456
5515e2d3
JJ
2457 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
2458 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
2459
5210c3d3 2460 { "GotoDefaultLocation", GTK_STOCK_HOME, N_("Go to the _Default Location"), NULL, N_("Go to the default location"), (GCallback)goto_default_location },
cbd293bb 2461 { "GotoSearch", GTK_STOCK_JUMP_TO, N_("Go to _Location..."), NULL, N_("Go to address/place using text search"), (GCallback)goto_address },
402fee6c 2462 { "GotoLL", GTK_STOCK_JUMP_TO, N_("_Go to Lat/Lon..."), NULL, N_("Go to arbitrary lat/lon coordinate"), (GCallback)draw_goto_cb },
150618fe 2463 { "GotoUTM", GTK_STOCK_JUMP_TO, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
480fb7e1 2464 { "SetHLColor",GTK_STOCK_SELECT_COLOR, N_("Set _Highlight Color..."), NULL, NULL, (GCallback)set_highlight_color },
cbd293bb 2465 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Bac_kground Color..."), NULL, NULL, (GCallback)set_bg_color },
5515e2d3
JJ
2466 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
2467 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
22c15481 2468 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To..."), "<control>Z", NULL, (GCallback)zoom_to_cb },
5515e2d3
JJ
2469 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
2470 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
2471 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
2472 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
2473 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
2474 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
2475 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
2476 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
2477 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
2478 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
4344546e
RN
2479 { "Zoom256", NULL, N_("256"), NULL, NULL, (GCallback)draw_zoom_cb },
2480 { "Zoom512", NULL, N_("512"), NULL, NULL, (GCallback)draw_zoom_cb },
2481 { "Zoom1024", NULL, N_("1024"), NULL, NULL, (GCallback)draw_zoom_cb },
2482 { "Zoom2048", NULL, N_("2048"), NULL, NULL, (GCallback)draw_zoom_cb },
2483 { "Zoom4096", NULL, N_("4096"), NULL, NULL, (GCallback)draw_zoom_cb },
2484 { "Zoom8192", NULL, N_("8192"), NULL, NULL, (GCallback)draw_zoom_cb },
2485 { "Zoom16384", NULL, N_("16384"), NULL, NULL, (GCallback)draw_zoom_cb },
2486 { "Zoom32768", NULL, N_("32768"), NULL, NULL, (GCallback)draw_zoom_cb },
cbd293bb
RN
2487 { "PanNorth", NULL, N_("Pan _North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
2488 { "PanEast", NULL, N_("Pan _East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
2489 { "PanSouth", NULL, N_("Pan _South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
2490 { "PanWest", NULL, N_("Pan _West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
5515e2d3
JJ
2491 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
2492
2493 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
2494 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
2495 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
2496 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
2497 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
06526f88 2498 { "MapCacheFlush",NULL, N_("_Flush Map Cache"), NULL, NULL, (GCallback)mapcache_flush_cb },
5210c3d3 2499 { "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 2500 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences"), NULL, NULL, (GCallback)preferences_cb },
5515e2d3
JJ
2501 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
2502
5ff75d1e 2503 { "HelpEntry", GTK_STOCK_HELP, N_("_Help"), "F1", NULL, (GCallback)help_help_cb },
5515e2d3 2504 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
e4afc73a
EB
2505};
2506
2507/* Radio items */
d587678a 2508/* FIXME use VIEWPORT_DRAWMODE values */
e4afc73a 2509static GtkRadioActionEntry mode_entries[] = {
5515e2d3
JJ
2510 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
2511 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
ac16c140 2512 { "ModeMercator", NULL, N_("_Mercator Mode"), "<control>m", NULL, 4 },
bd5e571a 2513 { "ModeLatLon", NULL, N_("Lat_/Lon Mode"), "<control>l", NULL, 5 },
50a14534
EB
2514};
2515
e4afc73a 2516static GtkRadioActionEntry tool_entries[] = {
576cbd17
GB
2517 { "Pan", "vik-icon-pan", N_("_Pan"), "<control><shift>P", N_("Pan Tool"), 0 },
2518 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 1 },
a47bfefa
RN
2519 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 2 },
2520 { "Select", "vik-icon-select", N_("_Select"), "<control><shift>S", N_("Select Tool"), 3 }
e4afc73a 2521};
50a14534 2522
35c7c0ba 2523static GtkToggleActionEntry toggle_entries[] = {
48df6aa3 2524 { "ShowScale", NULL, N_("Show _Scale"), "F5", N_("Show Scale"), (GCallback)set_draw_scale, TRUE },
cbd293bb 2525 { "ShowCenterMark", NULL, N_("Show _Center Mark"), "F6", N_("Show Center Mark"), (GCallback)set_draw_centermark, TRUE },
2afcef36 2526 { "ShowHighlight", GTK_STOCK_UNDERLINE, N_("Show _Highlight"), "F7", N_("Show Highlight"), (GCallback)set_draw_highlight, TRUE },
cbd293bb 2527 { "FullScreen", GTK_STOCK_FULLSCREEN, N_("_Full Screen"), "F11", N_("Activate full screen mode"), (GCallback)full_screen_cb, FALSE },
48df6aa3 2528 { "ViewSidePanel", GTK_STOCK_INDEX, N_("Show Side _Panel"), "F9", N_("Show Side Panel"), (GCallback)view_side_panel_cb, TRUE },
a459ee10 2529 { "ViewStatusBar", NULL, N_("Show Status_bar"), "F12", N_("Show Statusbar"), (GCallback)view_statusbar_cb, TRUE },
48df6aa3
RN
2530 { "ViewToolbar", NULL, N_("Show _Toolbar"), "F3", N_("Show Toolbar"), (GCallback)view_toolbar_cb, TRUE },
2531 { "ViewMainMenu", NULL, N_("Show _Menu"), "F4", N_("Show Menu"), (GCallback)view_main_menu_cb, TRUE },
35c7c0ba
EB
2532};
2533
a527cfff 2534#include "menu.xml.h"
e4afc73a 2535static void window_create_ui( VikWindow *window )
50a14534 2536{
e4afc73a
EB
2537 GtkUIManager *uim;
2538 GtkActionGroup *action_group;
50a14534 2539 GtkAccelGroup *accel_group;
e4afc73a 2540 GError *error;
e4afc73a
EB
2541 guint i, j, mid;
2542 GtkIconFactory *icon_factory;
2543 GtkIconSet *icon_set;
e4afc73a
EB
2544 GtkRadioActionEntry *tools = NULL, *radio;
2545 guint ntools;
2546
2547 uim = gtk_ui_manager_new ();
2548 window->uim = uim;
2549
9593a4c9
EB
2550 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
2551 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
576cbd17 2552 toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
a47bfefa 2553 toolbox_add_tool(window->vt, &select_tool, TOOL_LAYER_TYPE_NONE);
941aa6e9 2554
e4afc73a 2555 error = NULL;
a527cfff 2556 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
e4afc73a
EB
2557 g_error_free (error);
2558 exit (1);
2559 }
2560
2561 action_group = gtk_action_group_new ("MenuActions");
5515e2d3 2562 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
e4afc73a 2563 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
35c7c0ba 2564 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
6a9ff0ee 2565 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
2566
2567 icon_factory = gtk_icon_factory_new ();
ee36ac4f 2568 gtk_icon_factory_add_default (icon_factory);
e4afc73a
EB
2569
2570 register_vik_icons(icon_factory);
2571
2572 ntools = 0;
2573 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2574 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2575 radio = &tools[ntools];
2576 ntools++;
2577 *radio = tool_entries[i];
2578 radio->value = ntools;
2579 }
2580
2581 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2582 GtkActionEntry action;
2583 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
2584 vik_layer_get_interface(i)->name,
2585 vik_layer_get_interface(i)->name,
2586 GTK_UI_MANAGER_MENUITEM, FALSE);
2587
2588 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2589 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2590 gtk_icon_set_unref (icon_set);
2591
2592 action.name = vik_layer_get_interface(i)->name;
2593 action.stock_id = vik_layer_get_interface(i)->name;
4c77d5e0 2594 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
e4afc73a
EB
2595 action.accelerator = NULL;
2596 action.tooltip = NULL;
2597 action.callback = (GCallback)menu_addlayer_cb;
2598 gtk_action_group_add_actions(action_group, &action, 1, window);
2599
2600 if ( vik_layer_get_interface(i)->tools_count ) {
2601 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2602 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2603 }
2604
2605 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2606 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2607 radio = &tools[ntools];
2608 ntools++;
2609
e4afc73a 2610 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
4c77d5e0 2611 _(vik_layer_get_interface(i)->tools[j].name),
e4afc73a
EB
2612 vik_layer_get_interface(i)->tools[j].name,
2613 GTK_UI_MANAGER_MENUITEM, FALSE);
2614 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
4c77d5e0 2615 _(vik_layer_get_interface(i)->tools[j].name),
e4afc73a
EB
2616 vik_layer_get_interface(i)->tools[j].name,
2617 GTK_UI_MANAGER_TOOLITEM, FALSE);
2618
9593a4c9 2619 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
941aa6e9 2620
e4afc73a
EB
2621 radio->name = vik_layer_get_interface(i)->tools[j].name;
2622 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
4c77d5e0 2623 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
e4afc73a 2624 radio->accelerator = NULL;
4c77d5e0 2625 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
e4afc73a
EB
2626 radio->value = ntools;
2627 }
2628 }
ee36ac4f 2629 g_object_unref (icon_factory);
50a14534 2630
e4afc73a
EB
2631 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2632 g_free(tools);
50a14534 2633
e4afc73a 2634 gtk_ui_manager_insert_action_group (uim, action_group, 0);
50a14534 2635
79845167
QT
2636 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2637 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2638 GtkAction *action = gtk_action_group_get_action(action_group,
2639 vik_layer_get_interface(i)->tools[j].name);
2640 g_object_set(action, "sensitive", FALSE, NULL);
2641 }
2642 }
2643 window->action_group = action_group;
2644
e4afc73a
EB
2645 accel_group = gtk_ui_manager_get_accel_group (uim);
2646 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2647 gtk_ui_manager_ensure_update (uim);
13505702
GB
2648
2649 setup_recent_files(window);
e4afc73a 2650}
50a14534 2651
50a14534 2652
941aa6e9 2653
e4afc73a 2654static struct {
4bd45256 2655 const GdkPixdata *data;
e4afc73a
EB
2656 gchar *stock_id;
2657} stock_icons[] = {
5bfafde9 2658 { &begintr_18_pixbuf, "Begin Track" },
c3092f0f 2659 { &route_finder_18_pixbuf, "Route Finder" },
5bfafde9
GB
2660 { &mover_22_pixbuf, "vik-icon-pan" },
2661 { &demdl_18_pixbuf, "DEM Download/Import" },
2662 { &showpic_18_pixbuf, "Show Picture" },
2663 { &addtr_18_pixbuf, "Create Track" },
2664 { &edtr_18_pixbuf, "Edit Trackpoint" },
2665 { &addwp_18_pixbuf, "Create Waypoint" },
2666 { &edwp_18_pixbuf, "Edit Waypoint" },
2667 { &zoom_18_pixbuf, "vik-icon-zoom" },
2668 { &ruler_18_pixbuf, "vik-icon-ruler" },
a47bfefa 2669 { &select_18_pixbuf, "vik-icon-select" },
5bfafde9
GB
2670 { &geozoom_18_pixbuf, "Georef Zoom Tool" },
2671 { &geomove_18_pixbuf, "Georef Move Map" },
2672 { &mapdl_18_pixbuf, "Maps Download" },
e4afc73a
EB
2673};
2674
e4afc73a
EB
2675static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2676
2677static void
2678register_vik_icons (GtkIconFactory *icon_factory)
2679{
2680 GtkIconSet *icon_set;
e4afc73a 2681 gint i;
e4afc73a
EB
2682
2683 for (i = 0; i < n_stock_icons; i++) {
4bd45256
EB
2684 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2685 stock_icons[i].data, FALSE, NULL ));
e4afc73a
EB
2686 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2687 gtk_icon_set_unref (icon_set);
2688 }
50a14534 2689}
bce3a7b0 2690
9d7c24ed
RN
2691gpointer vik_window_get_selected_trw_layer ( VikWindow *vw )
2692{
2693 return vw->selected_vtl;
2694}
2695
2696void vik_window_set_selected_trw_layer ( VikWindow *vw, gpointer vtl )
2697{
113c74f6
RN
2698 vw->selected_vtl = vtl;
2699 vw->containing_vtl = vtl;
9d7c24ed
RN
2700 /* Clear others */
2701 vw->selected_track = NULL;
2702 vw->selected_tracks = NULL;
2703 vw->selected_waypoint = NULL;
2704 vw->selected_waypoints = NULL;
43f2e1da 2705 vw->selected_name = NULL;
9d7c24ed
RN
2706}
2707
2708gpointer vik_window_get_selected_tracks ( VikWindow *vw )
2709{
2710 return vw->selected_tracks;
2711}
2712
113c74f6 2713void vik_window_set_selected_tracks ( VikWindow *vw, gpointer gl, gpointer vtl )
9d7c24ed
RN
2714{
2715 vw->selected_tracks = gl;
113c74f6 2716 vw->containing_vtl = vtl;
9d7c24ed
RN
2717 /* Clear others */
2718 vw->selected_vtl = NULL;
2719 vw->selected_track = NULL;
2720 vw->selected_waypoint = NULL;
2721 vw->selected_waypoints = NULL;
43f2e1da 2722 vw->selected_name = NULL;
9d7c24ed
RN
2723}
2724
2725gpointer vik_window_get_selected_track ( VikWindow *vw )
2726{
2727 return vw->selected_track;
2728}
2729
43f2e1da 2730void vik_window_set_selected_track ( VikWindow *vw, gpointer *vt, gpointer vtl, gpointer name )
9d7c24ed
RN
2731{
2732 vw->selected_track = vt;
113c74f6 2733 vw->containing_vtl = vtl;
43f2e1da 2734 vw->selected_name = name;
9d7c24ed
RN
2735 /* Clear others */
2736 vw->selected_vtl = NULL;
2737 vw->selected_tracks = NULL;
2738 vw->selected_waypoint = NULL;
2739 vw->selected_waypoints = NULL;
2740}
2741gpointer vik_window_get_selected_waypoints ( VikWindow *vw )
2742{
2743 return vw->selected_waypoints;
2744}
2745
113c74f6 2746void vik_window_set_selected_waypoints ( VikWindow *vw, gpointer gl, gpointer vtl )
9d7c24ed
RN
2747{
2748 vw->selected_waypoints = gl;
113c74f6 2749 vw->containing_vtl = vtl;
9d7c24ed
RN
2750 /* Clear others */
2751 vw->selected_vtl = NULL;
2752 vw->selected_track = NULL;
2753 vw->selected_tracks = NULL;
2754 vw->selected_waypoint = NULL;
43f2e1da 2755 vw->selected_name = NULL;
9d7c24ed
RN
2756}
2757
2758gpointer vik_window_get_selected_waypoint ( VikWindow *vw )
2759{
2760 return vw->selected_waypoint;
2761}
2762
43f2e1da 2763void vik_window_set_selected_waypoint ( VikWindow *vw, gpointer *vwp, gpointer vtl, gpointer name )
9d7c24ed
RN
2764{
2765 vw->selected_waypoint = vwp;
113c74f6 2766 vw->containing_vtl = vtl;
43f2e1da 2767 vw->selected_name = name;
9d7c24ed
RN
2768 /* Clear others */
2769 vw->selected_vtl = NULL;
2770 vw->selected_track = NULL;
2771 vw->selected_tracks = NULL;
2772 vw->selected_waypoints = NULL;
2773}
2774
43f2e1da
RN
2775gpointer vik_window_get_selected_name ( VikWindow *vw )
2776{
2777 return vw->selected_name;
2778}
2779
9d7c24ed
RN
2780gboolean vik_window_clear_highlight ( VikWindow *vw )
2781{
2782 gboolean need_redraw = FALSE;
2783 if ( vw->selected_vtl != NULL ) {
2784 vw->selected_vtl = NULL;
2785 need_redraw = TRUE;
2786 }
2787 if ( vw->selected_track != NULL ) {
2788 vw->selected_track = NULL;
2789 need_redraw = TRUE;
2790 }
2791 if ( vw->selected_tracks != NULL ) {
2792 vw->selected_tracks = NULL;
2793 need_redraw = TRUE;
2794 }
2795 if ( vw->selected_waypoint != NULL ) {
2796 vw->selected_waypoint = NULL;
2797 need_redraw = TRUE;
2798 }
2799 if ( vw->selected_waypoints != NULL ) {
2800 vw->selected_waypoints = NULL;
2801 need_redraw = TRUE;
2802 }
2803 return need_redraw;
2804}