2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
22 #include "background.h"
24 #define VIKING_TITLE " - Viking " VIKING_VERSION " " VIKING_VERSION_NAME " " VIKING_URL
32 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
33 #define make_dir(dir) mkdir(dir)
35 #include <sys/types.h>
37 #define make_dir(dir) mkdir(dir,0777)
40 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
41 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
42 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
44 static void window_finalize ( GObject *gob );
45 static GObjectClass *parent_class;
47 static void window_init ( VikWindow *vw );
48 static void window_class_init ( VikWindowClass *klass );
50 static void draw_update ( VikWindow *vw );
52 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
56 static gboolean delete_event( VikWindow *vw );
58 static void draw_sync ( VikWindow *vw );
59 static void draw_redraw ( VikWindow *vw );
60 static void draw_scroll ( VikWindow *vw, GdkEvent *event );
61 static void draw_click ( VikWindow *vw, GdkEventButton *event );
62 static void draw_release ( VikWindow *vw, GdkEventButton *event );
63 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
64 static void draw_set_current_tool_cb ( GtkAction *a, VikWindow *vw );
65 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
66 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
68 static void draw_status ();
70 /* End Drawing Functions */
72 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
73 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
74 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
75 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
77 static void window_create_ui( VikWindow *window );
79 static void load_file ( GtkAction *a, VikWindow *vw );
80 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
81 static gboolean save_file ( GtkAction *a, VikWindow *vw );
82 static gboolean window_save ( VikWindow *vw );
83 static void register_vik_icons (GtkIconFactory *icon_factory);
88 VikViewport *viking_vvp;
89 VikLayersPanel *viking_vlp;
90 VikStatusbar *viking_vs;
94 GtkItemFactory *item_factory;
97 gboolean has_oldcoord;
100 guint16 tool_layer_id;
101 guint16 tool_tool_id;
105 guint draw_image_width, draw_image_height;
106 gboolean draw_image_save_as_png;
111 GtkWidget *open_dia, *save_dia;
113 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
126 VW_OPENWINDOW_SIGNAL,
130 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
132 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
134 GType vik_window_get_type (void)
136 static GType vw_type = 0;
140 static const GTypeInfo vw_info =
142 sizeof (VikWindowClass),
143 NULL, /* base_init */
144 NULL, /* base_finalize */
145 (GClassInitFunc) window_class_init, /* class_init */
146 NULL, /* class_finalize */
147 NULL, /* class_data */
150 (GInstanceInitFunc) window_init,
152 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
158 static void window_finalize ( GObject *gob )
160 VikWindow *vw = VIK_WINDOW(gob);
161 g_return_if_fail ( vw != NULL );
163 a_background_remove_status ( vw->viking_vs );
165 G_OBJECT_CLASS(parent_class)->finalize(gob);
168 static void window_class_init ( VikWindowClass *klass )
171 GObjectClass *object_class;
173 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);
174 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);
176 object_class = G_OBJECT_CLASS (klass);
178 object_class->finalize = window_finalize;
180 parent_class = g_type_class_peek_parent (klass);
184 static void window_init ( VikWindow *vw )
186 GtkWidget *main_vbox;
189 window_create_ui(vw);
190 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
192 vw->viking_vvp = vik_viewport_new();
193 vw->viking_vlp = vik_layers_panel_new();
194 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
195 vw->viking_vs = vik_statusbar_new();
198 vw->item_factory = NULL;
200 vw->has_oldcoord = FALSE;
201 vw->modified = FALSE;
202 vw->only_updating_coord_mode_ui = FALSE;
204 vw->pan_x = vw->pan_y = -1;
205 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
206 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
207 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
209 main_vbox = gtk_vbox_new(FALSE, 1);
210 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
212 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
213 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
214 gtk_toolbar_set_icon_size(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), GTK_ICON_SIZE_SMALL_TOOLBAR);
215 gtk_toolbar_set_style (gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), GTK_TOOLBAR_ICONS);
217 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
219 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
220 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
221 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
222 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
223 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
224 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
225 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
226 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
228 gtk_window_set_default_size ( GTK_WINDOW(vw), 1000, 800);
230 hpaned = gtk_hpaned_new ();
231 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
232 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
234 /* This packs the button into the window (a gtk container). */
235 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
237 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
239 a_background_add_status(vw->viking_vs);
245 VikWindow *vik_window_new ()
247 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
250 static gboolean delete_event( VikWindow *vw )
255 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
256 "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.",
257 vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
258 gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
259 switch ( gtk_dialog_run ( dia ) )
261 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
262 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
263 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
270 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
272 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
275 static void draw_update ( VikWindow *vw )
281 static void draw_sync ( VikWindow *vw )
283 vik_viewport_sync(vw->viking_vvp);
285 /* other things may be necc here later. */
288 static void draw_status ( VikWindow *vw )
290 static gchar zoom_level[22];
291 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" );
292 if ( vw->current_tool == TOOL_LAYER )
293 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
295 vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
297 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
300 static void draw_redraw ( VikWindow *vw )
302 vik_viewport_clear ( vw->viking_vvp);
304 vik_layers_panel_draw_all ( vw->viking_vlp );
307 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
309 static VikCoord coord;
310 static struct UTM utm;
311 static struct LatLon ll;
312 static char pointer_buf[36];
314 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
315 vik_coord_to_utm ( &coord, &utm );
316 a_coords_utm_to_latlon ( &utm, &ll );
318 g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
320 if ( vw->pan_x != -1 )
321 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
323 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
326 static void draw_scroll (VikWindow *vw, GdkEvent *event)
328 if ( event->scroll.direction == GDK_SCROLL_UP )
329 vik_viewport_zoom_in (vw->viking_vvp);
331 vik_viewport_zoom_out(vw->viking_vvp);
335 static void draw_release ( VikWindow *vw, GdkEventButton *event )
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 );
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 );
344 vw->pan_x = vw->pan_y = -1;
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 )
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 );
355 static void draw_click (VikWindow *vw, GdkEventButton *event)
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 )
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);
370 else if ( vw->current_tool == TOOL_RULER )
375 if ( event->button == 1 || event->button == 3 )
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) ) );
382 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
384 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
387 vw->oldcoord = coord;
388 /* we don't use anything else so far */
389 vw->has_oldcoord = TRUE;
393 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
397 else if ( vw->current_tool == TOOL_LAYER )
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 );
407 static void draw_set_current_tool_cb ( GtkAction *a, VikWindow *vw )
409 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
410 vw->current_tool = TOOL_ZOOM;
412 if (!strcmp(gtk_action_get_name(a), "Ruler")) {
413 vw->current_tool = TOOL_RULER;
418 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
422 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
425 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
428 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
431 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
435 gchar *s = (gchar *)gtk_action_get_name(a);
441 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
442 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
443 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
444 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
445 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
450 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
454 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
455 struct LatLon ll, llold;
456 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
457 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
458 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
462 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
463 struct UTM utm, utmold;
464 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
465 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
466 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
471 fprintf(stderr, "Houston we have a problem\n");
475 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
479 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
482 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
483 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
484 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
492 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
494 a_clipboard_copy ( vw->viking_vlp );
497 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
499 a_clipboard_copy ( vw->viking_vlp );
500 menu_delete_layer_cb ( a, vw );
503 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
505 if ( a_clipboard_paste ( vw->viking_vlp ) )
512 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
514 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
515 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
518 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
520 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
522 vik_layers_panel_delete_selected ( vw->viking_vlp );
526 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
529 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
531 /* White Magic, my friends ... White Magic... */
534 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
535 vw->current_tool = TOOL_ZOOM;
537 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
538 vw->current_tool = TOOL_RULER;
541 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
542 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
543 if (!strcmp(vik_layer_get_interface(i)->tools[j].name, gtk_action_get_name(a))) {
544 vw->current_tool = TOOL_LAYER;
545 vw->tool_layer_id = i;
546 vw->tool_tool_id = j;
554 static void window_set_filename ( VikWindow *vw, const gchar *filename )
558 g_free ( vw->filename );
559 if ( filename == NULL )
562 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
566 vw->filename = g_strdup(filename);
567 title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
568 gtk_window_set_title ( GTK_WINDOW(vw), title );
573 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
575 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
578 a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
582 GtkWidget *mode_button;
584 if ( change_filename )
585 window_set_filename ( vw, filename );
586 switch ( vik_viewport_get_drawmode ( vw->viking_vvp ) ) {
587 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
588 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
589 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
590 default: buttonname = "/ui/MainMenu/View/ModeKH";
592 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
593 g_assert ( mode_button );
594 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. */
595 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
596 vw->only_updating_coord_mode_ui = FALSE;
598 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
600 default: draw_update ( vw );
604 static void load_file ( GtkAction *a, VikWindow *vw )
607 if (!strcmp(gtk_action_get_name(a), "Open")) {
610 else if (!strcmp(gtk_action_get_name(a), "Append")) {
614 fprintf(stderr, "Houston we got a problem\n");
618 if ( ! vw->open_dia )
620 vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
621 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
622 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
623 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
625 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
627 gtk_widget_hide ( vw->open_dia );
628 if ( (vw->modified || vw->filename) && newwindow )
629 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
631 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
632 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
634 vik_window_open_file ( vw, *files, change_fn );
640 gtk_widget_hide ( vw->open_dia );
643 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
647 if ( ! vw->save_dia )
649 vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
650 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
651 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
654 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
656 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
657 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 ) ) )
659 window_set_filename ( vw, fn );
660 rv = window_save ( vw );
661 vw->modified = FALSE;
665 gtk_widget_hide ( vw->save_dia );
669 static gboolean window_save ( VikWindow *vw )
671 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
675 a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
680 static gboolean save_file ( GtkAction *a, VikWindow *vw )
682 if ( ! vw->filename )
683 return save_file_as ( NULL, vw );
686 vw->modified = FALSE;
687 return window_save ( vw );
691 static void clear_cb ( GtkAction *a, VikWindow *vw )
693 vik_layers_panel_clear ( vw->viking_vlp );
694 window_set_filename ( vw, NULL );
698 static void window_close ( GtkAction *a, VikWindow *vw )
700 if ( ! delete_event ( vw ) )
701 gtk_widget_destroy ( GTK_WIDGET(vw) );
704 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
706 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
707 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
709 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
710 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
715 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
717 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
718 GdkPixbuf *pixbuf_to_save;
719 gdouble old_xmpp, old_ympp;
720 GError *error = NULL;
722 /* backup old zoom & set new */
723 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
724 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
725 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
727 /* reset width and height: */
728 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
730 /* draw all layers */
733 /* save buffer as file. */
734 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);
735 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
738 fprintf(stderr, "Unable to write to file %s: %s", fn, error->message );
739 g_error_free (error);
741 g_object_unref ( G_OBJECT(pixbuf_to_save) );
743 /* pretend like nothing happened ;) */
744 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
745 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
746 vik_viewport_configure ( vw->viking_vvp );
750 static 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 )
752 gulong size = sizeof(gchar) * (strlen(fn) + 15);
753 gchar *name_of_file = g_malloc ( size );
755 struct UTM utm_orig, utm;
757 /* *** copied from above *** */
758 GdkPixbuf *pixbuf_to_save;
759 gdouble old_xmpp, old_ympp;
760 GError *error = NULL;
762 /* backup old zoom & set new */
763 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
764 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
765 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
767 /* reset width and height: do this only once for all images (same size) */
768 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
769 /* *** end copy from above *** */
771 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
775 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
777 for ( y = 1; y <= tiles_h; y++ )
779 for ( x = 1; x <= tiles_w; x++ )
781 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, VIKING_FILE_SEP, y, x, save_as_png ? "png" : "jpg" );
784 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
786 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
787 if ( tiles_h & 0x1 ) /* odd */
788 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
790 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
792 /* move to correct place. */
793 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
797 /* save buffer as file. */
798 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);
799 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
802 fprintf(stderr, "Unable to write to file %s: %s", name_of_file, error->message );
803 g_error_free (error);
806 g_object_unref ( G_OBJECT(pixbuf_to_save) );
810 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
811 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
812 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
813 vik_viewport_configure ( vw->viking_vvp );
816 g_free ( name_of_file );
819 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
821 VikWindow *vw = VIK_WINDOW(pass_along[0]);
822 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
823 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
824 gdouble width_min, width_max, height_min, height_max;
827 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
828 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
830 /* TODO: support for xzoom and yzoom values */
831 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
832 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
834 if ( width > width_max || width < width_min || height > height_max || height < height_min )
835 a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
837 gtk_spin_button_set_value ( width_spin, width );
838 gtk_spin_button_set_value ( height_spin, height );
841 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
843 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
844 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
847 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
848 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
849 if (pass_along[4]) /* save many images; find TOTAL area covered */
851 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
852 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
854 label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
855 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
856 g_free ( label_text );
859 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
861 /* todo: default for answers inside VikWindow or static (thruout instance) */
862 GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
863 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
869 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
870 GtkWidget *png_radio, *jpeg_radio;
871 GtkWidget *current_window_button;
872 gpointer current_window_pass_along[7];
873 GtkWidget *zoom_label, *zoom_spin;
874 GtkWidget *total_size_label;
876 /* only used if (!one_image_only) */
877 GtkWidget *tiles_width_spin, *tiles_height_spin;
880 width_label = gtk_label_new ( "Width (pixels):" );
881 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
882 height_label = gtk_label_new ( "Height (pixels):" );
883 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
885 zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
886 /* TODO: separate xzoom and yzoom factors */
887 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);
889 total_size_label = gtk_label_new ( NULL );
891 current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
892 current_window_pass_along [0] = vw;
893 current_window_pass_along [1] = width_spin;
894 current_window_pass_along [2] = height_spin;
895 current_window_pass_along [3] = zoom_spin;
896 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
897 current_window_pass_along [5] = NULL;
898 current_window_pass_along [6] = total_size_label;
899 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
901 png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
902 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
904 if ( ! vw->draw_image_save_as_png )
905 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
907 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
908 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
909 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
910 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
911 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
912 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
913 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
914 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
915 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
917 if ( ! one_image_only )
919 GtkWidget *tiles_width_label, *tiles_height_label;
922 tiles_width_label = gtk_label_new ( "East-west image tiles:" );
923 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
924 tiles_height_label = gtk_label_new ( "North-south image tiles:" );
925 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
926 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
927 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
928 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
929 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
931 current_window_pass_along [4] = tiles_width_spin;
932 current_window_pass_along [5] = tiles_height_spin;
933 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
934 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
936 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
937 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
938 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
939 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
941 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
943 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
945 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
947 gtk_widget_hide ( GTK_WIDGET(dialog) );
948 if ( one_image_only )
949 save_image_file ( vw, fn,
950 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
951 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
952 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
953 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
955 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
956 save_image_dir ( vw, fn,
957 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
958 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
959 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
960 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
961 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
962 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
964 a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
967 gtk_widget_destroy ( GTK_WIDGET(dialog) );
971 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
973 GtkWidget *file_selector;
975 file_selector = gtk_file_selection_new ("Save Image");
977 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
979 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
980 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 ) ) )
982 gtk_widget_hide ( file_selector );
983 draw_to_image_file ( vw, fn, TRUE );
987 gtk_widget_destroy ( file_selector );
990 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
992 GtkWidget *file_selector;
994 file_selector = gtk_file_selection_new ("Choose a name for a new directory to hold images");
996 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
998 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
999 if ( access ( fn, F_OK ) == 0 )
1000 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) );
1003 gtk_widget_hide ( file_selector );
1004 draw_to_image_file ( vw, fn, FALSE );
1008 gtk_widget_destroy ( file_selector );
1011 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1012 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1014 VikViewportDrawMode drawmode;
1015 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1016 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1018 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1019 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1021 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1022 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1024 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1025 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1027 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1028 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1031 fprintf(stderr, "Houston, we got a problem!\n");
1035 if ( !vw->only_updating_coord_mode_ui )
1037 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1038 if ( olddrawmode != drawmode )
1040 /* this takes care of coord mode too */
1041 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1042 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1043 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1044 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1045 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1052 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1054 GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1055 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1056 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1057 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1058 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1060 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1061 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1065 gtk_widget_destroy ( colorsd );
1068 static GtkActionEntry entries[] = {
1069 { "File", NULL, "_File", 0, 0, 0 },
1070 { "View", NULL, "_View", 0, 0, 0 },
1071 { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1072 { "Layers", NULL, "_Layers", 0, 0, 0 },
1073 { "Tools", NULL, "_Tools", 0, 0, 0 },
1075 { "New", GTK_STOCK_NEW, "_New", "<control>N", "New file", (GCallback)newwindow_cb },
1076 { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", (GCallback)load_file },
1077 { "Append", GTK_STOCK_ADD, "A_ppend File", NULL, "Append data from a different file", (GCallback)load_file },
1078 { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the file", (GCallback)save_file },
1079 { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As", NULL, "Save the file under different name", (GCallback)save_file_as },
1080 { "GenImg", GTK_STOCK_CLEAR, "_Generate Image File", NULL, "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1081 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL, "FIXME:IMGDIR", (GCallback)draw_to_image_dir_cb },
1082 { "Exit", GTK_STOCK_QUIT, "E_xit", "<control>W", "Exit the program", (GCallback)window_close },
1084 { "GotoLL", GTK_STOCK_QUIT, "_Go to Lat\\/Lon...", NULL, "Go to arbitrary lat\\/lon coordinate", (GCallback)draw_goto_cb },
1085 { "GotoUTM", GTK_STOCK_QUIT, "Go to UTM...", NULL, "Go to arbitrary UTM coordinate", (GCallback)draw_goto_cb },
1086 { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...", NULL, NULL, (GCallback)set_bg_color },
1087 { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>plus", NULL, (GCallback)draw_zoom_cb },
1088 { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control>minus", NULL, (GCallback)draw_zoom_cb },
1089 { "ZoomTo", GTK_STOCK_ZOOM_FIT, "Zoom _To", "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1090 { "Zoom0.25", NULL, "0.25", NULL, NULL, (GCallback)draw_zoom_cb },
1091 { "Zoom0.5", NULL, "0.5", NULL, NULL, (GCallback)draw_zoom_cb },
1092 { "Zoom1", NULL, "1", NULL, NULL, (GCallback)draw_zoom_cb },
1093 { "Zoom2", NULL, "2", NULL, NULL, (GCallback)draw_zoom_cb },
1094 { "Zoom4", NULL, "4", NULL, NULL, (GCallback)draw_zoom_cb },
1095 { "Zoom8", NULL, "8", NULL, NULL, (GCallback)draw_zoom_cb },
1096 { "Zoom16", NULL, "16", NULL, NULL, (GCallback)draw_zoom_cb },
1097 { "Zoom32", NULL, "32", NULL, NULL, (GCallback)draw_zoom_cb },
1098 { "Zoom64", NULL, "64", NULL, NULL, (GCallback)draw_zoom_cb },
1099 { "Zoom128", NULL, "128", NULL, NULL, (GCallback)draw_zoom_cb },
1100 { "BGJobs", GTK_STOCK_EXECUTE, "Background _Jobs", NULL, NULL, (GCallback)a_background_show_window },
1102 { "Cut", GTK_STOCK_CUT, "Cu_t", NULL, NULL, (GCallback)menu_cut_layer_cb },
1103 { "Copy", GTK_STOCK_COPY, "_Copy", NULL, NULL, (GCallback)menu_copy_layer_cb },
1104 { "Paste", GTK_STOCK_PASTE, "_Paste", NULL, NULL, (GCallback)menu_paste_layer_cb },
1105 { "Delete", GTK_STOCK_DELETE, "_Delete", NULL, NULL, (GCallback)menu_delete_layer_cb },
1106 { "DeleteAll", NULL, "Delete All", NULL, NULL, (GCallback)clear_cb },
1107 { "Properties",GTK_STOCK_PROPERTIES, "_Properties", NULL, NULL, (GCallback)menu_properties_cb },
1111 static GtkRadioActionEntry mode_entries[] = {
1112 { "ModeUTM", NULL, "_UTM Mode", "<control>u", NULL, 0 },
1113 { "ModeExpedia", NULL, "_Expedia Mode", "<control>e", NULL, 1 },
1114 { "ModeGoogle", NULL, "_Google Mode", "<control>g", NULL, 2 },
1115 { "ModeKH", NULL, "_KH\\/Flat LatLon Mode", "<control>k", NULL, 3 },
1116 { "ModeMercator", NULL, "_Mercator (New Google)", "<control>m", NULL, 4 }
1119 static GtkRadioActionEntry tool_entries[] = {
1120 { "Zoom", "vik-icon-zoom", "_Zoom", "<control><shift>Z", "Zoom Tool", 0 },
1121 { "Ruler", "vik-icon-ruler", "_Ruler", "<control><shift>R", "Ruler Tool", 1 }
1124 #include "menu.xml.h"
1125 static void window_create_ui( VikWindow *window )
1128 GtkActionGroup *action_group;
1129 GtkAccelGroup *accel_group;
1131 GtkActionEntry add_layer_item;
1133 GtkIconFactory *icon_factory;
1134 GtkIconSet *icon_set;
1135 GtkIconSource *icon_source;
1136 GtkRadioActionEntry *tools = NULL, *radio;
1139 uim = gtk_ui_manager_new ();
1144 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1145 g_error_free (error);
1149 action_group = gtk_action_group_new ("MenuActions");
1150 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1151 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 0, (GCallback)window_change_coord_mode_cb, window);
1153 icon_factory = gtk_icon_factory_new ();
1155 register_vik_icons(icon_factory);
1158 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1159 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1160 radio = &tools[ntools];
1162 *radio = tool_entries[i];
1163 radio->value = ntools;
1166 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1167 GtkActionEntry action;
1168 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1169 vik_layer_get_interface(i)->name,
1170 vik_layer_get_interface(i)->name,
1171 GTK_UI_MANAGER_MENUITEM, FALSE);
1173 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1174 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1175 gtk_icon_set_unref (icon_set);
1177 action.name = vik_layer_get_interface(i)->name;
1178 action.stock_id = vik_layer_get_interface(i)->name;
1179 action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1180 action.accelerator = NULL;
1181 action.tooltip = NULL;
1182 action.callback = (GCallback)menu_addlayer_cb;
1183 gtk_action_group_add_actions(action_group, &action, 1, window);
1185 if ( vik_layer_get_interface(i)->tools_count ) {
1186 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1187 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1190 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1191 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1192 radio = &tools[ntools];
1195 fprintf(stderr, " %s\n", vik_layer_get_interface(i)->tools[j].name);
1196 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
1197 vik_layer_get_interface(i)->tools[j].name,
1198 vik_layer_get_interface(i)->tools[j].name,
1199 GTK_UI_MANAGER_MENUITEM, FALSE);
1200 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
1201 vik_layer_get_interface(i)->tools[j].name,
1202 vik_layer_get_interface(i)->tools[j].name,
1203 GTK_UI_MANAGER_TOOLITEM, FALSE);
1205 radio->name = vik_layer_get_interface(i)->tools[j].name;
1206 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1207 radio->label = vik_layer_get_interface(i)->tools[j].name;
1208 radio->accelerator = NULL;
1209 radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1210 radio->value = ntools;
1214 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1217 gtk_ui_manager_insert_action_group (uim, action_group, 0);
1219 accel_group = gtk_ui_manager_get_accel_group (uim);
1220 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1221 gtk_ui_manager_ensure_update (uim);
1222 gtk_icon_factory_add_default (icon_factory);
1223 g_object_unref (icon_factory);
1231 { { NULL, "addtr-18.png", NULL }, "Create Track" },
1232 { { NULL, "edtr-18.png", NULL }, "Edit Trackpoint" },
1233 { { NULL, "addwp-18.png", NULL }, "Create Waypoint" },
1234 { { NULL, "edwp-18.png", NULL }, "Edit Waypoint" },
1235 { { NULL, "zoom-18.png", NULL }, "vik-icon-zoom" },
1236 { { NULL, "ruler-18.png", NULL }, "vik-icon-ruler" },
1237 { { NULL, "geozoom-18.png", NULL }, "Georef Zoom Tool" },
1238 { { NULL, "geomove-18.png", NULL }, "Georef Move Map" },
1239 { { NULL, "mapdl-18.png", NULL }, "Maps Download" },
1240 { { NULL, "showpic-18.png", NULL }, "Show Picture" },
1244 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1247 register_vik_icons (GtkIconFactory *icon_factory)
1249 GtkIconSet *icon_set;
1250 GtkIconSource *icon_source;
1253 getcwd(cwd, sizeof(cwd));
1255 for (i = 0; i < n_stock_icons; i++) {
1259 icon_set = gtk_icon_set_new ();
1261 for (j=0; j<3; j++) {
1262 if (stock_icons[i].fn[j]) {
1263 icon_source = gtk_icon_source_new ();
1264 sprintf(fn, "%s/%s", cwd, stock_icons[i].fn[j]);
1265 gtk_icon_source_set_filename (icon_source, fn);
1266 gtk_icon_set_add_source (icon_set, icon_source);
1267 gtk_icon_source_free (icon_source);
1271 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1272 gtk_icon_set_unref (icon_set);