]> git.street.me.uk Git - andy/viking.git/blame - src/vikwindow.c
Updated DND to insert layers at the point of the drop, instead of front of list
[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"
23
24#define VIKING_TITLE " - Viking " VIKING_VERSION " " VIKING_VERSION_NAME " " VIKING_URL
25
e4afc73a 26#include <stdlib.h>
50a14534
EB
27#include <math.h>
28#include <string.h>
29#include <ctype.h>
30
31#ifdef WINDOWS
32/* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
33#define make_dir(dir) mkdir(dir)
34#else
35#include <sys/types.h>
36#include <sys/stat.h>
37#define make_dir(dir) mkdir(dir,0777)
38#endif
39
40#define DRAW_IMAGE_DEFAULT_WIDTH 1280
41#define DRAW_IMAGE_DEFAULT_HEIGHT 1024
42#define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
43
44static void window_finalize ( GObject *gob );
45static GObjectClass *parent_class;
46
47static void window_init ( VikWindow *vw );
48static void window_class_init ( VikWindowClass *klass );
49
50static void draw_update ( VikWindow *vw );
51
e4afc73a 52static void newwindow_cb ( GtkAction *a, VikWindow *vw );
50a14534
EB
53
54/* Drawing & stuff */
55
56static gboolean delete_event( VikWindow *vw );
57
58static void draw_sync ( VikWindow *vw );
59static void draw_redraw ( VikWindow *vw );
60static void draw_scroll ( VikWindow *vw, GdkEvent *event );
61static void draw_click ( VikWindow *vw, GdkEventButton *event );
62static void draw_release ( VikWindow *vw, GdkEventButton *event );
63static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
e4afc73a
EB
64static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
65static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
50a14534
EB
66
67static void draw_status ();
68
69/* End Drawing Functions */
70
e4afc73a
EB
71static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
72static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
73static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
74static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
50a14534 75
e4afc73a 76static void window_create_ui( VikWindow *window );
50a14534 77
e4afc73a
EB
78static void load_file ( GtkAction *a, VikWindow *vw );
79static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
80static gboolean save_file ( GtkAction *a, VikWindow *vw );
50a14534 81static gboolean window_save ( VikWindow *vw );
e4afc73a
EB
82static void register_vik_icons (GtkIconFactory *icon_factory);
83
50a14534
EB
84
85struct _VikWindow {
86 GtkWindow gtkwindow;
87 VikViewport *viking_vvp;
88 VikLayersPanel *viking_vlp;
89 VikStatusbar *viking_vs;
90
e4afc73a
EB
91 GtkToolbar *toolbar;
92
50a14534
EB
93 GtkItemFactory *item_factory;
94
95 VikCoord oldcoord;
96 gboolean has_oldcoord;
97 guint current_tool;
98
99 guint16 tool_layer_id;
100 guint16 tool_tool_id;
101
102 gint pan_x, pan_y;
103
104 guint draw_image_width, draw_image_height;
105 gboolean draw_image_save_as_png;
106
107 gchar *filename;
108 gboolean modified;
109
110 GtkWidget *open_dia, *save_dia;
111
112 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
e4afc73a 113 GtkUIManager *uim;
50a14534
EB
114};
115
116enum {
117 TOOL_ZOOM = 0,
118 TOOL_RULER,
119 TOOL_LAYER,
120 NUMBER_OF_TOOLS
121};
122
123enum {
124 VW_NEWWINDOW_SIGNAL,
125 VW_OPENWINDOW_SIGNAL,
126 VW_LAST_SIGNAL
127};
128
129static guint window_signals[VW_LAST_SIGNAL] = { 0 };
130
131static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
132
133GType vik_window_get_type (void)
134{
135 static GType vw_type = 0;
136
137 if (!vw_type)
138 {
139 static const GTypeInfo vw_info =
140 {
141 sizeof (VikWindowClass),
142 NULL, /* base_init */
143 NULL, /* base_finalize */
144 (GClassInitFunc) window_class_init, /* class_init */
145 NULL, /* class_finalize */
146 NULL, /* class_data */
147 sizeof (VikWindow),
148 0,
149 (GInstanceInitFunc) window_init,
150 };
151 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
152 }
153
154 return vw_type;
155}
156
157static void window_finalize ( GObject *gob )
158{
159 VikWindow *vw = VIK_WINDOW(gob);
160 g_return_if_fail ( vw != NULL );
161
162 a_background_remove_status ( vw->viking_vs );
163
164 G_OBJECT_CLASS(parent_class)->finalize(gob);
165}
166
167static void window_class_init ( VikWindowClass *klass )
168{
169 /* destructor */
170 GObjectClass *object_class;
171
172 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 173 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
174
175 object_class = G_OBJECT_CLASS (klass);
176
177 object_class->finalize = window_finalize;
178
179 parent_class = g_type_class_peek_parent (klass);
180
181}
182
183static void window_init ( VikWindow *vw )
184{
185 GtkWidget *main_vbox;
186 GtkWidget *hpaned;
187
e4afc73a 188 window_create_ui(vw);
50a14534
EB
189 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
190
191 vw->viking_vvp = vik_viewport_new();
192 vw->viking_vlp = vik_layers_panel_new();
193 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
194 vw->viking_vs = vik_statusbar_new();
195
196 vw->filename = NULL;
e4afc73a 197 vw->item_factory = NULL;
50a14534
EB
198
199 vw->has_oldcoord = FALSE;
200 vw->modified = FALSE;
201 vw->only_updating_coord_mode_ui = FALSE;
202
203 vw->pan_x = vw->pan_y = -1;
204 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
205 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
206 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
207
208 main_vbox = gtk_vbox_new(FALSE, 1);
209 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
210
e4afc73a
EB
211 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
212 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
886031df
AF
213 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
214 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
50a14534
EB
215
216 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
217
218 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
219 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
220 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
221 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
222 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
223 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
224 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
225 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
226
227 gtk_window_set_default_size ( GTK_WINDOW(vw), 1000, 800);
228
229 hpaned = gtk_hpaned_new ();
230 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
231 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
232
233 /* This packs the button into the window (a gtk container). */
234 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
235
236 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
237
238 a_background_add_status(vw->viking_vs);
239
240 vw->open_dia = NULL;
241 vw->save_dia = NULL;
242}
243
244VikWindow *vik_window_new ()
245{
246 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
247}
248
249static gboolean delete_event( VikWindow *vw )
250{
251 if ( vw->modified )
252 {
253 GtkDialog *dia;
254 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
255 "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.",
256 vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
257 gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
258 switch ( gtk_dialog_run ( dia ) )
259 {
260 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
261 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
e4afc73a 262 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
50a14534
EB
263 }
264 }
265 return FALSE;
266}
267
268/* Drawing stuff */
e4afc73a 269static void newwindow_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
270{
271 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
272}
273
274static void draw_update ( VikWindow *vw )
275{
276 draw_redraw (vw);
277 draw_sync (vw);
278}
279
280static void draw_sync ( VikWindow *vw )
281{
282 vik_viewport_sync(vw->viking_vvp);
283 draw_status ( vw );
284 /* other things may be necc here later. */
285}
286
287static void draw_status ( VikWindow *vw )
288{
289 static gchar zoom_level[22];
290 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" );
291 if ( vw->current_tool == TOOL_LAYER )
292 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
293 else
294 vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
295
296 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
297}
298
299static void draw_redraw ( VikWindow *vw )
300{
301 vik_viewport_clear ( vw->viking_vvp);
50a14534 302 vik_layers_panel_draw_all ( vw->viking_vlp );
acaf7113 303 vik_viewport_draw_scale ( vw->viking_vvp );
50a14534
EB
304}
305
306static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
307{
308 static VikCoord coord;
309 static struct UTM utm;
310 static struct LatLon ll;
311 static char pointer_buf[36];
312
313 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
314 vik_coord_to_utm ( &coord, &utm );
315 a_coords_utm_to_latlon ( &utm, &ll );
316
317 g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
318
acaf7113 319 if ( vw->pan_x != -1 ) {
50a14534 320 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
acaf7113 321 }
50a14534
EB
322
323 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
324}
325
326static void draw_scroll (VikWindow *vw, GdkEvent *event)
327{
328 if ( event->scroll.direction == GDK_SCROLL_UP )
329 vik_viewport_zoom_in (vw->viking_vvp);
330 else
331 vik_viewport_zoom_out(vw->viking_vvp);
332 draw_update(vw);
333}
334
335static void draw_release ( VikWindow *vw, GdkEventButton *event )
336{
337 if ( event->button == 2 ) { /* move / pan */
338 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
339 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
340 else
341 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
342 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
343 draw_update ( vw );
344 vw->pan_x = vw->pan_y = -1;
345 }
346 if ( vw->current_tool == TOOL_LAYER && ( event->button == 1 || event->button == 3 ) &&
347 vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release )
348 {
349 vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
350 vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release,
351 event, vw->viking_vvp );
352 }
353}
354
355static void draw_click (VikWindow *vw, GdkEventButton *event)
356{
357 if ( event->button == 2) {
358 vw->pan_x = (gint) event->x;
359 vw->pan_y = (gint) event->y;
360 } else if ( vw->current_tool == TOOL_ZOOM )
361 {
362 vw->modified = TRUE;
363 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
364 if ( event->button == 1 )
365 vik_viewport_zoom_in (vw->viking_vvp);
366 else if ( event->button == 3 )
367 vik_viewport_zoom_out (vw->viking_vvp);
368 draw_update ( vw );
369 }
370 else if ( vw->current_tool == TOOL_RULER )
371 {
372 struct LatLon ll;
373 VikCoord coord;
374 gchar *temp;
375 if ( event->button == 1 || event->button == 3 )
376 {
377 vik_viewport_screen_to_coord ( vw->viking_vvp, (gint) event->x, (gint) event->y, &coord );
378 vik_coord_to_latlon ( &coord, &ll );
379 if ( vw->has_oldcoord )
380 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
381 else
382 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
383
384 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
385 g_free ( temp );
386
387 vw->oldcoord = coord;
388 /* we don't use anything else so far */
389 vw->has_oldcoord = TRUE;
390 }
391 else
392 {
393 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
394 draw_update ( vw );
395 }
396 }
397 else if ( vw->current_tool == TOOL_LAYER )
398 {
399 vw->modified = TRUE;
400 if ( ! vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
401 vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback,
402 event, vw->viking_vvp ) )
403 a_dialog_info_msg_extra ( GTK_WINDOW(vw), "A %s Layer must exist and be either selected or visible before you can use this function.", vik_layer_get_interface ( vw->tool_layer_id )->name );
404 }
405}
406
e4afc73a 407static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
50a14534 408{
e4afc73a
EB
409 guint what = 128;
410
411 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
412 what = -3;
413 }
414 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
415 what = -4;
416 }
417 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
418 what = -2;
419 }
420 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
421 what = -1;
422 }
423 else {
424 gchar *s = (gchar *)gtk_action_get_name(a);
425 what = atoi(s+4);
426 }
427
50a14534
EB
428 switch (what)
429 {
430 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
431 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
432 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
433 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
434 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
435 }
436 draw_update ( vw );
437}
438
e4afc73a 439void draw_goto_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
440{
441 VikCoord new_center;
e4afc73a
EB
442
443 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
50a14534
EB
444 struct LatLon ll, llold;
445 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
446 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
447 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
448 else
449 return;
450 }
e4afc73a 451 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
50a14534
EB
452 struct UTM utm, utmold;
453 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
454 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
455 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
456 else
457 return;
458 }
e4afc73a
EB
459 else {
460 fprintf(stderr, "Houston we have a problem\n");
461 return;
462 }
50a14534
EB
463
464 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
465 draw_update ( vw );
466}
467
e4afc73a 468static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
50a14534 469{
e4afc73a
EB
470 gint type;
471 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
472 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
473 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
474 draw_update ( vw );
475 vw->modified = TRUE;
476 }
477 }
50a14534
EB
478 }
479}
480
e4afc73a 481static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
482{
483 a_clipboard_copy ( vw->viking_vlp );
484}
485
e4afc73a 486static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
487{
488 a_clipboard_copy ( vw->viking_vlp );
e4afc73a 489 menu_delete_layer_cb ( a, vw );
50a14534
EB
490}
491
e4afc73a 492static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
493{
494 if ( a_clipboard_paste ( vw->viking_vlp ) )
495 {
496 draw_update ( vw );
497 vw->modified = TRUE;
498 }
499}
500
e4afc73a 501static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
502{
503 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
504 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
505}
506
e4afc73a 507static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
508{
509 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
510 {
511 vik_layers_panel_delete_selected ( vw->viking_vlp );
512 vw->modified = TRUE;
513 }
514 else
515 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
516}
517
e4afc73a 518static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
50a14534
EB
519{
520 /* White Magic, my friends ... White Magic... */
e4afc73a
EB
521 int i, j;
522
523 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
524 vw->current_tool = TOOL_ZOOM;
525 }
526 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
527 vw->current_tool = TOOL_RULER;
528 }
529 else {
530 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
531 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
532 if (!strcmp(vik_layer_get_interface(i)->tools[j].name, gtk_action_get_name(a))) {
533 vw->current_tool = TOOL_LAYER;
534 vw->tool_layer_id = i;
535 vw->tool_tool_id = j;
536 }
537 }
538 }
539 }
50a14534
EB
540 draw_status ( vw );
541}
542
543static void window_set_filename ( VikWindow *vw, const gchar *filename )
544{
545 gchar *title;
546 if ( vw->filename )
547 g_free ( vw->filename );
548 if ( filename == NULL )
549 {
550 vw->filename = NULL;
551 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
552 }
553 else
554 {
555 vw->filename = g_strdup(filename);
556 title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
557 gtk_window_set_title ( GTK_WINDOW(vw), title );
558 g_free ( title );
559 }
560}
561
562void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
563{
564 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
565 {
566 case 0:
567 a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
568 break;
569 case 1:
570 {
571 GtkWidget *mode_button;
572 gchar *buttonname;
573 if ( change_filename )
574 window_set_filename ( vw, filename );
575 switch ( vik_viewport_get_drawmode ( vw->viking_vvp ) ) {
e4afc73a
EB
576 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
577 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
578 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
579 default: buttonname = "/ui/MainMenu/View/ModeKH";
50a14534 580 }
e4afc73a 581 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
50a14534
EB
582 g_assert ( mode_button );
583 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. */
584 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
585 vw->only_updating_coord_mode_ui = FALSE;
586
587 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
588 }
589 default: draw_update ( vw );
590 }
591}
592
e4afc73a 593static void load_file ( GtkAction *a, VikWindow *vw )
50a14534 594{
e4afc73a
EB
595 gboolean newwindow;
596 if (!strcmp(gtk_action_get_name(a), "Open")) {
597 newwindow = TRUE;
598 }
599 else if (!strcmp(gtk_action_get_name(a), "Append")) {
600 newwindow = FALSE;
601 }
602 else {
603 fprintf(stderr, "Houston we got a problem\n");
604 return;
605 }
606
50a14534
EB
607 if ( ! vw->open_dia )
608 {
609 vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
610 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
611 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
612 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
613 }
614 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
615 {
616 gtk_widget_hide ( vw->open_dia );
617 if ( (vw->modified || vw->filename) && newwindow )
618 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
619 else {
620 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
621 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
622 while ( *files ) {
623 vik_window_open_file ( vw, *files, change_fn );
624 files++;
625 }
626 }
627 }
628 else
629 gtk_widget_hide ( vw->open_dia );
630}
631
e4afc73a 632static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
50a14534
EB
633{
634 gboolean rv = FALSE;
635 const gchar *fn;
636 if ( ! vw->save_dia )
637 {
638 vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
639 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
640 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
641 }
642
643 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
644 {
645 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
646 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 ) ) )
647 {
648 window_set_filename ( vw, fn );
649 rv = window_save ( vw );
650 vw->modified = FALSE;
651 break;
652 }
653 }
654 gtk_widget_hide ( vw->save_dia );
655 return rv;
656}
657
658static gboolean window_save ( VikWindow *vw )
659{
660 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
661 return TRUE;
662 else
663 {
664 a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
665 return FALSE;
666 }
667}
668
e4afc73a 669static gboolean save_file ( GtkAction *a, VikWindow *vw )
50a14534
EB
670{
671 if ( ! vw->filename )
e4afc73a 672 return save_file_as ( NULL, vw );
50a14534
EB
673 else
674 {
675 vw->modified = FALSE;
676 return window_save ( vw );
677 }
678}
679
e4afc73a 680static void clear_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
681{
682 vik_layers_panel_clear ( vw->viking_vlp );
683 window_set_filename ( vw, NULL );
684 draw_update ( vw );
685}
686
e4afc73a 687static void window_close ( GtkAction *a, VikWindow *vw )
50a14534
EB
688{
689 if ( ! delete_event ( vw ) )
690 gtk_widget_destroy ( GTK_WIDGET(vw) );
691}
692
e4afc73a 693static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
694{
695 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
696 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
697 {
698 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
699 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
700 draw_update ( vw );
701 }
702}
703
704static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
705{
706 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
707 GdkPixbuf *pixbuf_to_save;
708 gdouble old_xmpp, old_ympp;
709 GError *error = NULL;
710
711 /* backup old zoom & set new */
712 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
713 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
714 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
715
716 /* reset width and height: */
717 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
718
719 /* draw all layers */
720 draw_redraw ( vw );
721
722 /* save buffer as file. */
723 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);
724 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
725 if (error)
726 {
727 fprintf(stderr, "Unable to write to file %s: %s", fn, error->message );
728 g_error_free (error);
729 }
730 g_object_unref ( G_OBJECT(pixbuf_to_save) );
731
732 /* pretend like nothing happened ;) */
733 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
734 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
735 vik_viewport_configure ( vw->viking_vvp );
736 draw_update ( vw );
737}
738
739static 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 )
740{
741 gulong size = sizeof(gchar) * (strlen(fn) + 15);
742 gchar *name_of_file = g_malloc ( size );
743 guint x = 1, y = 1;
744 struct UTM utm_orig, utm;
745
746 /* *** copied from above *** */
747 GdkPixbuf *pixbuf_to_save;
748 gdouble old_xmpp, old_ympp;
749 GError *error = NULL;
750
751 /* backup old zoom & set new */
752 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
753 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
754 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
755
756 /* reset width and height: do this only once for all images (same size) */
757 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
758 /* *** end copy from above *** */
759
760 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
761
762 make_dir(fn);
763
764 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
765
766 for ( y = 1; y <= tiles_h; y++ )
767 {
768 for ( x = 1; x <= tiles_w; x++ )
769 {
770 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, VIKING_FILE_SEP, y, x, save_as_png ? "png" : "jpg" );
771 utm = utm_orig;
772 if ( tiles_w & 0x1 )
773 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
774 else
775 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
776 if ( tiles_h & 0x1 ) /* odd */
777 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
778 else /* even */
779 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
780
781 /* move to correct place. */
782 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
783
784 draw_redraw ( vw );
785
786 /* save buffer as file. */
787 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);
788 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
789 if (error)
790 {
791 fprintf(stderr, "Unable to write to file %s: %s", name_of_file, error->message );
792 g_error_free (error);
793 }
794
795 g_object_unref ( G_OBJECT(pixbuf_to_save) );
796 }
797 }
798
799 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
800 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
801 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
802 vik_viewport_configure ( vw->viking_vvp );
803 draw_update ( vw );
804
805 g_free ( name_of_file );
806}
807
808static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
809{
810 VikWindow *vw = VIK_WINDOW(pass_along[0]);
811 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
812 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
813 gdouble width_min, width_max, height_min, height_max;
814 gint width, height;
815
816 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
817 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
818
819 /* TODO: support for xzoom and yzoom values */
820 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
821 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
822
823 if ( width > width_max || width < width_min || height > height_max || height < height_min )
824 a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
825
826 gtk_spin_button_set_value ( width_spin, width );
827 gtk_spin_button_set_value ( height_spin, height );
828}
829
830static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
831{
832 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
833 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
834 gchar *label_text;
835 gdouble w, h;
836 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
837 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
838 if (pass_along[4]) /* save many images; find TOTAL area covered */
839 {
840 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
841 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
842 }
843 label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
844 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
845 g_free ( label_text );
846}
847
848static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
849{
850 /* todo: default for answers inside VikWindow or static (thruout instance) */
851 GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
852 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
853 GTK_STOCK_CANCEL,
854 GTK_RESPONSE_REJECT,
855 GTK_STOCK_OK,
856 GTK_RESPONSE_ACCEPT,
857 0 );
858 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
859 GtkWidget *png_radio, *jpeg_radio;
860 GtkWidget *current_window_button;
861 gpointer current_window_pass_along[7];
862 GtkWidget *zoom_label, *zoom_spin;
863 GtkWidget *total_size_label;
864
865 /* only used if (!one_image_only) */
886031df 866 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
50a14534
EB
867
868
869 width_label = gtk_label_new ( "Width (pixels):" );
870 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
871 height_label = gtk_label_new ( "Height (pixels):" );
872 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
873
874 zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
875 /* TODO: separate xzoom and yzoom factors */
876 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);
877
878 total_size_label = gtk_label_new ( NULL );
879
880 current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
881 current_window_pass_along [0] = vw;
882 current_window_pass_along [1] = width_spin;
883 current_window_pass_along [2] = height_spin;
884 current_window_pass_along [3] = zoom_spin;
885 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
886 current_window_pass_along [5] = NULL;
887 current_window_pass_along [6] = total_size_label;
888 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
889
890 png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
891 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
892
893 if ( ! vw->draw_image_save_as_png )
894 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
895
896 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
897 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
898 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
899 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
900 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
901 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
902 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
903 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
904 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
905
906 if ( ! one_image_only )
907 {
908 GtkWidget *tiles_width_label, *tiles_height_label;
909
910
911 tiles_width_label = gtk_label_new ( "East-west image tiles:" );
912 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
913 tiles_height_label = gtk_label_new ( "North-south image tiles:" );
914 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
915 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
916 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
917 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
918 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
919
920 current_window_pass_along [4] = tiles_width_spin;
921 current_window_pass_along [5] = tiles_height_spin;
922 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
923 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
924 }
925 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
926 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
927 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
928 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
929
930 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
931
932 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
933
934 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
935 {
936 gtk_widget_hide ( GTK_WIDGET(dialog) );
937 if ( one_image_only )
938 save_image_file ( vw, fn,
939 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
940 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
941 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
942 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
943 else {
944 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
945 save_image_dir ( vw, fn,
946 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
947 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
948 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
949 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
950 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
951 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
952 else
953 a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
954 }
955 }
956 gtk_widget_destroy ( GTK_WIDGET(dialog) );
957}
958
959
e4afc73a 960static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
961{
962 GtkWidget *file_selector;
963 const gchar *fn;
964 file_selector = gtk_file_selection_new ("Save Image");
965
966 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
967 {
968 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
969 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 ) ) )
970 {
971 gtk_widget_hide ( file_selector );
972 draw_to_image_file ( vw, fn, TRUE );
973 break;
974 }
975 }
976 gtk_widget_destroy ( file_selector );
977}
978
e4afc73a 979static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
50a14534
EB
980{
981 GtkWidget *file_selector;
982 const gchar *fn;
983 file_selector = gtk_file_selection_new ("Choose a name for a new directory to hold images");
984
985 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
986 {
987 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
988 if ( access ( fn, F_OK ) == 0 )
989 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) );
990 else
991 {
992 gtk_widget_hide ( file_selector );
993 draw_to_image_file ( vw, fn, FALSE );
994 break;
995 }
996 }
997 gtk_widget_destroy ( file_selector );
998}
999
1000/* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
e4afc73a 1001static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
50a14534 1002{
e4afc73a
EB
1003 VikViewportDrawMode drawmode;
1004 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1005 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1006 }
1007 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1008 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1009 }
1010 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1011 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1012 }
1013 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1014 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1015 }
1016 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1017 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1018 }
1019 else {
1020 fprintf(stderr, "Houston, we got a problem!\n");
1021 return;
1022 }
1023
50a14534
EB
1024 if ( !vw->only_updating_coord_mode_ui )
1025 {
1026 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1027 if ( olddrawmode != drawmode )
1028 {
1029 /* this takes care of coord mode too */
1030 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1031 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1032 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1033 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1034 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1035 }
1036 draw_update ( vw );
1037 }
1038 }
1039}
1040
e4afc73a 1041static void set_bg_color ( GtkAction *a, VikWindow *vw )
50a14534
EB
1042{
1043 GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1044 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1045 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1046 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1047 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1048 {
1049 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1050 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1051 draw_update ( vw );
1052 }
1053 g_free ( color );
1054 gtk_widget_destroy ( colorsd );
1055}
1056
e4afc73a
EB
1057static GtkActionEntry entries[] = {
1058 { "File", NULL, "_File", 0, 0, 0 },
1059 { "View", NULL, "_View", 0, 0, 0 },
1060 { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1061 { "Layers", NULL, "_Layers", 0, 0, 0 },
1062 { "Tools", NULL, "_Tools", 0, 0, 0 },
1063
1064 { "New", GTK_STOCK_NEW, "_New", "<control>N", "New file", (GCallback)newwindow_cb },
1065 { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", (GCallback)load_file },
1066 { "Append", GTK_STOCK_ADD, "A_ppend File", NULL, "Append data from a different file", (GCallback)load_file },
1067 { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the file", (GCallback)save_file },
1068 { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As", NULL, "Save the file under different name", (GCallback)save_file_as },
1069 { "GenImg", GTK_STOCK_CLEAR, "_Generate Image File", NULL, "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1070 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL, "FIXME:IMGDIR", (GCallback)draw_to_image_dir_cb },
1071 { "Exit", GTK_STOCK_QUIT, "E_xit", "<control>W", "Exit the program", (GCallback)window_close },
1072
1073 { "GotoLL", GTK_STOCK_QUIT, "_Go to Lat\\/Lon...", NULL, "Go to arbitrary lat\\/lon coordinate", (GCallback)draw_goto_cb },
1074 { "GotoUTM", GTK_STOCK_QUIT, "Go to UTM...", NULL, "Go to arbitrary UTM coordinate", (GCallback)draw_goto_cb },
1075 { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...", NULL, NULL, (GCallback)set_bg_color },
1076 { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>plus", NULL, (GCallback)draw_zoom_cb },
1077 { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control>minus", NULL, (GCallback)draw_zoom_cb },
1078 { "ZoomTo", GTK_STOCK_ZOOM_FIT, "Zoom _To", "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1079 { "Zoom0.25", NULL, "0.25", NULL, NULL, (GCallback)draw_zoom_cb },
1080 { "Zoom0.5", NULL, "0.5", NULL, NULL, (GCallback)draw_zoom_cb },
1081 { "Zoom1", NULL, "1", NULL, NULL, (GCallback)draw_zoom_cb },
1082 { "Zoom2", NULL, "2", NULL, NULL, (GCallback)draw_zoom_cb },
1083 { "Zoom4", NULL, "4", NULL, NULL, (GCallback)draw_zoom_cb },
1084 { "Zoom8", NULL, "8", NULL, NULL, (GCallback)draw_zoom_cb },
1085 { "Zoom16", NULL, "16", NULL, NULL, (GCallback)draw_zoom_cb },
1086 { "Zoom32", NULL, "32", NULL, NULL, (GCallback)draw_zoom_cb },
1087 { "Zoom64", NULL, "64", NULL, NULL, (GCallback)draw_zoom_cb },
1088 { "Zoom128", NULL, "128", NULL, NULL, (GCallback)draw_zoom_cb },
1089 { "BGJobs", GTK_STOCK_EXECUTE, "Background _Jobs", NULL, NULL, (GCallback)a_background_show_window },
1090
1091 { "Cut", GTK_STOCK_CUT, "Cu_t", NULL, NULL, (GCallback)menu_cut_layer_cb },
1092 { "Copy", GTK_STOCK_COPY, "_Copy", NULL, NULL, (GCallback)menu_copy_layer_cb },
1093 { "Paste", GTK_STOCK_PASTE, "_Paste", NULL, NULL, (GCallback)menu_paste_layer_cb },
1094 { "Delete", GTK_STOCK_DELETE, "_Delete", NULL, NULL, (GCallback)menu_delete_layer_cb },
1095 { "DeleteAll", NULL, "Delete All", NULL, NULL, (GCallback)clear_cb },
1096 { "Properties",GTK_STOCK_PROPERTIES, "_Properties", NULL, NULL, (GCallback)menu_properties_cb },
1097};
1098
1099/* Radio items */
1100static GtkRadioActionEntry mode_entries[] = {
1101 { "ModeUTM", NULL, "_UTM Mode", "<control>u", NULL, 0 },
1102 { "ModeExpedia", NULL, "_Expedia Mode", "<control>e", NULL, 1 },
1103 { "ModeGoogle", NULL, "_Google Mode", "<control>g", NULL, 2 },
1104 { "ModeKH", NULL, "_KH\\/Flat LatLon Mode", "<control>k", NULL, 3 },
1105 { "ModeMercator", NULL, "_Mercator (New Google)", "<control>m", NULL, 4 }
50a14534
EB
1106};
1107
e4afc73a
EB
1108static GtkRadioActionEntry tool_entries[] = {
1109 { "Zoom", "vik-icon-zoom", "_Zoom", "<control><shift>Z", "Zoom Tool", 0 },
1110 { "Ruler", "vik-icon-ruler", "_Ruler", "<control><shift>R", "Ruler Tool", 1 }
1111};
50a14534 1112
a527cfff 1113#include "menu.xml.h"
e4afc73a 1114static void window_create_ui( VikWindow *window )
50a14534 1115{
e4afc73a
EB
1116 GtkUIManager *uim;
1117 GtkActionGroup *action_group;
50a14534 1118 GtkAccelGroup *accel_group;
e4afc73a 1119 GError *error;
e4afc73a
EB
1120 guint i, j, mid;
1121 GtkIconFactory *icon_factory;
1122 GtkIconSet *icon_set;
e4afc73a
EB
1123 GtkRadioActionEntry *tools = NULL, *radio;
1124 guint ntools;
1125
1126 uim = gtk_ui_manager_new ();
1127 window->uim = uim;
1128
1129
1130 error = NULL;
a527cfff 1131 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
e4afc73a
EB
1132 g_error_free (error);
1133 exit (1);
1134 }
1135
1136 action_group = gtk_action_group_new ("MenuActions");
1137 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1138 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 0, (GCallback)window_change_coord_mode_cb, window);
1139
1140 icon_factory = gtk_icon_factory_new ();
1141
1142 register_vik_icons(icon_factory);
1143
1144 ntools = 0;
1145 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1146 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1147 radio = &tools[ntools];
1148 ntools++;
1149 *radio = tool_entries[i];
1150 radio->value = ntools;
1151 }
1152
1153 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1154 GtkActionEntry action;
1155 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1156 vik_layer_get_interface(i)->name,
1157 vik_layer_get_interface(i)->name,
1158 GTK_UI_MANAGER_MENUITEM, FALSE);
1159
1160 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1161 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1162 gtk_icon_set_unref (icon_set);
1163
1164 action.name = vik_layer_get_interface(i)->name;
1165 action.stock_id = vik_layer_get_interface(i)->name;
1166 action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1167 action.accelerator = NULL;
1168 action.tooltip = NULL;
1169 action.callback = (GCallback)menu_addlayer_cb;
1170 gtk_action_group_add_actions(action_group, &action, 1, window);
1171
1172 if ( vik_layer_get_interface(i)->tools_count ) {
1173 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1174 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1175 }
1176
1177 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1178 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1179 radio = &tools[ntools];
1180 ntools++;
1181
e4afc73a
EB
1182 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
1183 vik_layer_get_interface(i)->tools[j].name,
1184 vik_layer_get_interface(i)->tools[j].name,
1185 GTK_UI_MANAGER_MENUITEM, FALSE);
1186 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
1187 vik_layer_get_interface(i)->tools[j].name,
1188 vik_layer_get_interface(i)->tools[j].name,
1189 GTK_UI_MANAGER_TOOLITEM, FALSE);
1190
1191 radio->name = vik_layer_get_interface(i)->tools[j].name;
1192 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1193 radio->label = vik_layer_get_interface(i)->tools[j].name;
1194 radio->accelerator = NULL;
1195 radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1196 radio->value = ntools;
1197 }
1198 }
50a14534 1199
e4afc73a
EB
1200 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1201 g_free(tools);
50a14534 1202
e4afc73a 1203 gtk_ui_manager_insert_action_group (uim, action_group, 0);
50a14534 1204
e4afc73a
EB
1205 accel_group = gtk_ui_manager_get_accel_group (uim);
1206 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1207 gtk_ui_manager_ensure_update (uim);
1208 gtk_icon_factory_add_default (icon_factory);
1209 g_object_unref (icon_factory);
1210}
50a14534 1211
50a14534 1212
4bd45256 1213#include "icons.h"
e4afc73a 1214static struct {
4bd45256 1215 const GdkPixdata *data;
e4afc73a
EB
1216 gchar *stock_id;
1217} stock_icons[] = {
4bd45256
EB
1218 { &addtr_18, "Create Track" },
1219 { &edtr_18, "Edit Trackpoint" },
1220 { &addwp_18, "Create Waypoint" },
1221 { &edwp_18, "Edit Waypoint" },
1222 { &zoom_18, "vik-icon-zoom" },
1223 { &ruler_18, "vik-icon-ruler" },
1224 { &geozoom_18, "Georef Zoom Tool" },
1225 { &geomove_18, "Georef Move Map" },
1226 { &mapdl_18, "Maps Download" },
1227 { &showpic_18, "Show Picture" },
e4afc73a
EB
1228};
1229
50a14534 1230
e4afc73a
EB
1231static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1232
1233static void
1234register_vik_icons (GtkIconFactory *icon_factory)
1235{
1236 GtkIconSet *icon_set;
e4afc73a 1237 gint i;
e4afc73a
EB
1238
1239 for (i = 0; i < n_stock_icons; i++) {
4bd45256
EB
1240 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
1241 stock_icons[i].data, FALSE, NULL ));
e4afc73a
EB
1242 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1243 gtk_icon_set_unref (icon_set);
1244 }
50a14534 1245}