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