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