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