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