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