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