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
23 #include "background.h"
25 #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 ( 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 ( VikWindow *vw, guint mode );
65 static void draw_zoom ( VikWindow *vw, gint what );
66 static void draw_goto ( VikWindow *vw, gint mode );
68 static void draw_status ();
70 /* End Drawing Functions */
72 static void menu_addlayer_cb ( VikWindow *vw, gint type );
73 static void menu_properties_cb ( VikWindow *vw );
74 static void menu_delete_layer_cb ( VikWindow *vw );
75 static void menu_dynamic_tool_cb ( VikWindow *vw, guint id );
77 static GtkWidget *window_create_menubar( VikWindow *window );
79 static void load_file ( VikWindow *vw, gboolean newwindow );
80 static gboolean save_file_as ( VikWindow *vw );
81 static gboolean save_file ( VikWindow *vw );
82 static gboolean window_save ( VikWindow *vw );
86 VikViewport *viking_vvp;
87 VikLayersPanel *viking_vlp;
88 VikStatusbar *viking_vs;
90 GtkItemFactory *item_factory;
93 gboolean has_oldcoord;
96 guint16 tool_layer_id;
101 guint draw_image_width, draw_image_height;
102 gboolean draw_image_save_as_png;
107 GtkWidget *open_dia, *save_dia;
109 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
121 VW_OPENWINDOW_SIGNAL,
125 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
127 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
129 GType vik_window_get_type (void)
131 static GType vw_type = 0;
135 static const GTypeInfo vw_info =
137 sizeof (VikWindowClass),
138 NULL, /* base_init */
139 NULL, /* base_finalize */
140 (GClassInitFunc) window_class_init, /* class_init */
141 NULL, /* class_finalize */
142 NULL, /* class_data */
145 (GInstanceInitFunc) window_init,
147 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
153 static void window_finalize ( GObject *gob )
155 VikWindow *vw = VIK_WINDOW(gob);
156 g_return_if_fail ( vw != NULL );
158 a_background_remove_status ( vw->viking_vs );
160 G_OBJECT_CLASS(parent_class)->finalize(gob);
163 static void window_class_init ( VikWindowClass *klass )
166 GObjectClass *object_class;
168 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);
169 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__STRING, G_TYPE_NONE, 1, G_TYPE_POINTER);
171 object_class = G_OBJECT_CLASS (klass);
173 object_class->finalize = window_finalize;
175 parent_class = g_type_class_peek_parent (klass);
179 static void window_init ( VikWindow *vw )
181 GtkWidget *main_vbox;
184 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
186 vw->viking_vvp = vik_viewport_new();
187 vw->viking_vlp = vik_layers_panel_new();
188 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
189 vw->viking_vs = vik_statusbar_new();
193 vw->has_oldcoord = FALSE;
194 vw->modified = FALSE;
195 vw->only_updating_coord_mode_ui = FALSE;
197 vw->pan_x = vw->pan_y = -1;
198 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
199 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
200 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
202 main_vbox = gtk_vbox_new(FALSE, 1);
203 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
205 gtk_box_pack_start (GTK_BOX(main_vbox), window_create_menubar(vw), FALSE, TRUE, 0);
207 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
209 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
210 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
211 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
212 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
213 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
214 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
215 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
216 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
218 gtk_window_set_default_size ( GTK_WINDOW(vw), 1000, 800);
220 hpaned = gtk_hpaned_new ();
221 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
222 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
224 /* This packs the button into the window (a gtk container). */
225 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
227 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
229 a_background_add_status(vw->viking_vs);
235 VikWindow *vik_window_new ()
237 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
240 static gboolean delete_event( VikWindow *vw )
245 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
246 "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.",
247 vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
248 gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
249 switch ( gtk_dialog_run ( dia ) )
251 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
252 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
253 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(vw);
260 static void newwindow_cb ( VikWindow *vw )
262 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
265 static void draw_update ( VikWindow *vw )
271 static void draw_sync ( VikWindow *vw )
273 vik_viewport_sync(vw->viking_vvp);
275 /* other things may be necc here later. */
278 static void draw_status ( VikWindow *vw )
280 static gchar zoom_level[22];
281 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" );
282 if ( vw->current_tool == TOOL_LAYER )
283 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
285 vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
287 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
290 static void draw_redraw ( VikWindow *vw )
292 vik_viewport_clear ( vw->viking_vvp);
294 vik_layers_panel_draw_all ( vw->viking_vlp );
297 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
299 static VikCoord coord;
300 static struct UTM utm;
301 static struct LatLon ll;
302 static char pointer_buf[36];
304 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
305 vik_coord_to_utm ( &coord, &utm );
306 a_coords_utm_to_latlon ( &utm, &ll );
308 g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
310 if ( vw->pan_x != -1 )
311 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
313 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
316 static void draw_scroll (VikWindow *vw, GdkEvent *event)
318 if ( event->scroll.direction == GDK_SCROLL_UP )
319 vik_viewport_zoom_in (vw->viking_vvp);
321 vik_viewport_zoom_out(vw->viking_vvp);
325 static void draw_release ( VikWindow *vw, GdkEventButton *event )
327 if ( event->button == 2 ) { /* move / pan */
328 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
329 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
331 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
332 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
334 vw->pan_x = vw->pan_y = -1;
336 if ( vw->current_tool == TOOL_LAYER && ( event->button == 1 || event->button == 3 ) &&
337 vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release )
339 vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
340 vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback_release,
341 event, vw->viking_vvp );
345 static void draw_click (VikWindow *vw, GdkEventButton *event)
347 if ( event->button == 2) {
348 vw->pan_x = (gint) event->x;
349 vw->pan_y = (gint) event->y;
350 } else if ( vw->current_tool == TOOL_ZOOM )
353 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
354 if ( event->button == 1 )
355 vik_viewport_zoom_in (vw->viking_vvp);
356 else if ( event->button == 3 )
357 vik_viewport_zoom_out (vw->viking_vvp);
360 else if ( vw->current_tool == TOOL_RULER )
365 if ( event->button == 1 || event->button == 3 )
367 vik_viewport_screen_to_coord ( vw->viking_vvp, (gint) event->x, (gint) event->y, &coord );
368 vik_coord_to_latlon ( &coord, &ll );
369 if ( vw->has_oldcoord )
370 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(vw->oldcoord) ) );
372 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
374 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
377 vw->oldcoord = coord;
378 /* we don't use anything else so far */
379 vw->has_oldcoord = TRUE;
383 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
387 else if ( vw->current_tool == TOOL_LAYER )
390 if ( ! vik_layers_panel_tool ( vw->viking_vlp, vw->tool_layer_id,
391 vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].callback,
392 event, vw->viking_vvp ) )
393 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 );
397 static void draw_set_current_tool ( VikWindow *vw, guint mode )
399 vw->current_tool = mode;
403 static void draw_zoom ( VikWindow *vw, gint what )
407 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
408 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
409 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
410 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
411 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
416 void draw_goto ( VikWindow *vw, gint mode )
421 struct LatLon ll, llold;
422 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
423 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
424 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
430 struct UTM utm, utmold;
431 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
432 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
433 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
438 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
442 static void menu_addlayer_cb ( VikWindow *vw, gint type )
444 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) )
451 static void menu_copy_layer_cb ( VikWindow *vw )
453 a_clipboard_copy ( vw->viking_vlp );
456 static void menu_cut_layer_cb ( VikWindow *vw )
458 a_clipboard_copy ( vw->viking_vlp );
459 menu_delete_layer_cb ( vw );
462 static void menu_paste_layer_cb ( VikWindow *vw )
464 if ( a_clipboard_paste ( vw->viking_vlp ) )
471 static void menu_properties_cb ( VikWindow *vw )
473 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
474 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
477 static void menu_delete_layer_cb ( VikWindow *vw )
479 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
481 vik_layers_panel_delete_selected ( vw->viking_vlp );
485 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
488 static void menu_dynamic_tool_cb ( VikWindow *vw, guint id )
490 /* White Magic, my friends ... White Magic... */
491 vw->current_tool = TOOL_LAYER;
492 vw->tool_layer_id = id >> 16;
493 vw->tool_tool_id = id & 0xff;
497 static void window_set_filename ( VikWindow *vw, const gchar *filename )
501 g_free ( vw->filename );
502 if ( filename == NULL )
505 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
509 vw->filename = g_strdup(filename);
510 title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
511 gtk_window_set_title ( GTK_WINDOW(vw), title );
516 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
518 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
521 a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
525 GtkWidget *mode_button;
527 if ( change_filename )
528 window_set_filename ( vw, filename );
529 switch ( vik_viewport_get_drawmode ( vw->viking_vvp ) ) {
530 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/View/UTM Mode"; break;
531 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/View/Expedia Mode"; break;
532 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/View/Google Mode"; break;
533 default: buttonname = "/View/KH\\/Flat LatLon Mode";
535 mode_button = gtk_item_factory_get_item ( vw->item_factory, buttonname );
536 g_assert ( mode_button );
537 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. */
538 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
539 vw->only_updating_coord_mode_ui = FALSE;
541 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
543 default: draw_update ( vw );
547 static void load_file ( VikWindow *vw, gboolean newwindow )
549 if ( ! vw->open_dia )
551 vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
552 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
553 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
554 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
556 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
558 gtk_widget_hide ( vw->open_dia );
559 if ( (vw->modified || vw->filename) && newwindow )
560 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
562 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
563 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
565 vik_window_open_file ( vw, *files, change_fn );
571 gtk_widget_hide ( vw->open_dia );
574 static gboolean save_file_as ( VikWindow *vw )
578 if ( ! vw->save_dia )
580 vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
581 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
582 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
585 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
587 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
588 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 ) ) )
590 window_set_filename ( vw, fn );
591 rv = window_save ( vw );
592 vw->modified = FALSE;
596 gtk_widget_hide ( vw->save_dia );
600 static gboolean window_save ( VikWindow *vw )
602 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
606 a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
611 static gboolean save_file ( VikWindow *vw )
613 if ( ! vw->filename )
614 return save_file_as ( vw );
617 vw->modified = FALSE;
618 return window_save ( vw );
622 static void clear_cb ( VikWindow *vw )
624 vik_layers_panel_clear ( vw->viking_vlp );
625 window_set_filename ( vw, NULL );
629 static void window_close ( VikWindow *vw )
631 if ( ! delete_event ( vw ) )
632 gtk_widget_destroy ( GTK_WIDGET(vw) );
635 static void zoom_to ( VikWindow *vw )
637 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
638 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
640 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
641 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
646 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
648 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
649 GdkPixbuf *pixbuf_to_save;
650 gdouble old_xmpp, old_ympp;
651 GError *error = NULL;
653 /* backup old zoom & set new */
654 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
655 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
656 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
658 /* reset width and height: */
659 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
661 /* draw all layers */
664 /* save buffer as file. */
665 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);
666 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
669 fprintf(stderr, "Unable to write to file %s: %s", fn, error->message );
670 g_error_free (error);
672 g_object_unref ( G_OBJECT(pixbuf_to_save) );
674 /* pretend like nothing happened ;) */
675 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
676 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
677 vik_viewport_configure ( vw->viking_vvp );
681 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 )
683 gulong size = sizeof(gchar) * (strlen(fn) + 15);
684 gchar *name_of_file = g_malloc ( size );
686 struct UTM utm_orig, utm;
688 /* *** copied from above *** */
689 GdkPixbuf *pixbuf_to_save;
690 gdouble old_xmpp, old_ympp;
691 GError *error = NULL;
693 /* backup old zoom & set new */
694 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
695 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
696 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
698 /* reset width and height: do this only once for all images (same size) */
699 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
700 /* *** end copy from above *** */
702 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
706 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
708 for ( y = 1; y <= tiles_h; y++ )
710 for ( x = 1; x <= tiles_w; x++ )
712 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, VIKING_FILE_SEP, y, x, save_as_png ? "png" : "jpg" );
715 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
717 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
718 if ( tiles_h & 0x1 ) /* odd */
719 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
721 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
723 /* move to correct place. */
724 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
728 /* save buffer as file. */
729 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);
730 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
733 fprintf(stderr, "Unable to write to file %s: %s", name_of_file, error->message );
734 g_error_free (error);
737 g_object_unref ( G_OBJECT(pixbuf_to_save) );
741 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
742 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
743 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
744 vik_viewport_configure ( vw->viking_vvp );
747 g_free ( name_of_file );
750 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
752 VikWindow *vw = VIK_WINDOW(pass_along[0]);
753 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
754 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
755 gdouble width_min, width_max, height_min, height_max;
758 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
759 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
761 /* TODO: support for xzoom and yzoom values */
762 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
763 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
765 if ( width > width_max || width < width_min || height > height_max || height < height_min )
766 a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
768 gtk_spin_button_set_value ( width_spin, width );
769 gtk_spin_button_set_value ( height_spin, height );
772 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
774 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
775 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
778 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
779 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
780 if (pass_along[4]) /* save many images; find TOTAL area covered */
782 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
783 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
785 label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
786 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
787 g_free ( label_text );
790 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
792 /* todo: default for answers inside VikWindow or static (thruout instance) */
793 GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
794 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
800 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
801 GtkWidget *png_radio, *jpeg_radio;
802 GtkWidget *current_window_button;
803 gpointer current_window_pass_along[7];
804 GtkWidget *zoom_label, *zoom_spin;
805 GtkWidget *total_size_label;
807 /* only used if (!one_image_only) */
808 GtkWidget *tiles_width_spin, *tiles_height_spin;
811 width_label = gtk_label_new ( "Width (pixels):" );
812 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
813 height_label = gtk_label_new ( "Height (pixels):" );
814 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
816 zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
817 /* TODO: separate xzoom and yzoom factors */
818 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);
820 total_size_label = gtk_label_new ( NULL );
822 current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
823 current_window_pass_along [0] = vw;
824 current_window_pass_along [1] = width_spin;
825 current_window_pass_along [2] = height_spin;
826 current_window_pass_along [3] = zoom_spin;
827 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
828 current_window_pass_along [5] = NULL;
829 current_window_pass_along [6] = total_size_label;
830 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
832 png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
833 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
835 if ( ! vw->draw_image_save_as_png )
836 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
838 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
839 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
840 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
841 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
842 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
843 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
844 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
845 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
846 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
848 if ( ! one_image_only )
850 GtkWidget *tiles_width_label, *tiles_height_label;
853 tiles_width_label = gtk_label_new ( "East-west image tiles:" );
854 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
855 tiles_height_label = gtk_label_new ( "North-south image tiles:" );
856 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
857 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
858 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
859 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
860 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
862 current_window_pass_along [4] = tiles_width_spin;
863 current_window_pass_along [5] = tiles_height_spin;
864 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
865 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
867 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
868 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
869 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
870 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
872 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
874 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
876 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
878 gtk_widget_hide ( GTK_WIDGET(dialog) );
879 if ( one_image_only )
880 save_image_file ( vw, fn,
881 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
882 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
883 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
884 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
886 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
887 save_image_dir ( vw, fn,
888 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
889 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
890 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
891 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
892 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
893 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
895 a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
898 gtk_widget_destroy ( GTK_WIDGET(dialog) );
902 static void draw_to_image_file_cb ( VikWindow *vw )
904 GtkWidget *file_selector;
906 file_selector = gtk_file_selection_new ("Save Image");
908 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
910 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
911 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 ) ) )
913 gtk_widget_hide ( file_selector );
914 draw_to_image_file ( vw, fn, TRUE );
918 gtk_widget_destroy ( file_selector );
921 static void draw_to_image_dir_cb ( VikWindow *vw )
923 GtkWidget *file_selector;
925 file_selector = gtk_file_selection_new ("Choose a name for a new directory to hold images");
927 while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_OK )
929 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector) );
930 if ( access ( fn, F_OK ) == 0 )
931 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) );
934 gtk_widget_hide ( file_selector );
935 draw_to_image_file ( vw, fn, FALSE );
939 gtk_widget_destroy ( file_selector );
942 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
943 static void window_change_coord_mode ( VikWindow *vw, VikViewportDrawMode drawmode )
945 if ( !vw->only_updating_coord_mode_ui )
947 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
948 if ( olddrawmode != drawmode )
950 /* this takes care of coord mode too */
951 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
952 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
953 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
954 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
955 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
962 static void set_bg_color ( VikWindow *vw )
964 GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
965 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
966 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
967 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
968 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
970 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
971 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
975 gtk_widget_destroy ( colorsd );
978 static GtkItemFactoryEntry menu_items[] = {
979 { "/_File", NULL, NULL, 0, "<Branch>" },
980 { "/File/_New", "<control>N", newwindow_cb, 0, "<StockItem>", GTK_STOCK_NEW },
981 { "/File/_Open", "<control>O", load_file, TRUE, "<StockItem>", GTK_STOCK_OPEN },
982 { "/File/A_ppend File", NULL, load_file, FALSE, "<Item>" },
983 { "/File/_Save", "<control>S", (GtkItemFactoryCallback) save_file, 0, "<StockItem>", GTK_STOCK_SAVE },
984 { "/File/Save _As", NULL, (GtkItemFactoryCallback) save_file_as, 0, "<StockItem>", GTK_STOCK_SAVE_AS },
985 { "/File/sep1", NULL, NULL, 0, "<Separator>" },
986 { "/File/_Generate Image File", NULL, draw_to_image_file_cb, 0, "<Item>" },
987 { "/File/Generate Directory of Images", NULL, draw_to_image_dir_cb, 0, "<Item>" },
988 { "/File/sep1", NULL, NULL, 0, "<Separator>" },
989 { "/File/_Close", "<CTRL>W", window_close, 0, "<StockItem>", GTK_STOCK_QUIT },
990 { "/_View", NULL, NULL, 0, "<Branch>" },
991 { "/View/_UTM Mode", "<ctrl>u", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_UTM, "<RadioItem>" },
992 { "/View/_Expedia Mode", "<ctrl>e", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_EXPEDIA, "/View/UTM Mode" },
993 { "/View/_Google Mode", "<ctrl>g", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_GOOGLE, "/View/UTM Mode" },
994 { "/View/_KH\\/Flat LatLon Mode", "<ctrl>k", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_KH, "/View/UTM Mode" },
995 { "/View/_Mercator (New Google)", "<ctrl>m", (GtkItemFactoryCallback) window_change_coord_mode, VIK_VIEWPORT_DRAWMODE_MERCATOR, "/View/UTM Mode" },
996 { "/View/sep1", NULL, NULL, 0, "<Separator>" },
997 { "/View/_Go to Lat\\/Lon...", NULL, draw_goto, 1, "<Item>" },
998 { "/View/Go to UTM...", NULL, draw_goto, 2, "<Item>" },
999 { "/View/sep1", NULL, NULL, 0, "<Separator>" },
1000 { "/View/Set Background Color...", NULL, set_bg_color, 0, "<StockItem>", GTK_STOCK_SELECT_COLOR },
1001 { "/View/sep1", NULL, NULL, 0, "<Separator>" },
1002 { "/View/Zoom _In", "<ctrl>plus", draw_zoom, -3, "<StockItem>", GTK_STOCK_ZOOM_IN },
1003 { "/View/Zoom _Out", "<ctrl>minus", draw_zoom, -4, "<StockItem>", GTK_STOCK_ZOOM_OUT },
1004 { "/View/Zoom To...", "<ctrl><shift>Z", zoom_to, 0, "<Item>" },
1005 { "/View/_Zoom", NULL, NULL, 0, "<Branch>" },
1006 { "/View/Zoom/0.25", "<ctrl>1", draw_zoom, -2, "<Item>" },
1007 { "/View/Zoom/0.5", "<ctrl>2", draw_zoom, -1, "<Item>" },
1008 { "/View/Zoom/1", "<ctrl>3", draw_zoom, 1, "<Item>" },
1009 { "/View/Zoom/2", "<ctrl>4", draw_zoom, 2, "<Item>" },
1010 { "/View/Zoom/4", "<ctrl>5", draw_zoom, 4, "<Item>" },
1011 { "/View/Zoom/8", "<ctrl>6", draw_zoom, 8, "<Item>" },
1012 { "/View/Zoom/16", "<ctrl>7", draw_zoom, 16, "<Item>" },
1013 { "/View/Zoom/32", "<ctrl>8", draw_zoom, 32, "<Item>" },
1014 { "/View/Zoom/64", "<ctrl>9", draw_zoom, 64, "<Item>" },
1015 { "/View/Zoom/128", "<ctrl>0", draw_zoom, 64, "<Item>" },
1016 { "/View/sep1", NULL, NULL, 0, "<Separator>" },
1017 { "/View/Background _Jobs...", "<ctrl>j", (GtkItemFactoryCallback) a_background_show_window, 0, "<Item>" },
1019 { "/_Layers", NULL, NULL, 0, "<Branch>" },
1020 { "/Layers/Cu_t", NULL, menu_cut_layer_cb, -1, "<StockItem>", GTK_STOCK_CUT },
1021 { "/Layers/_Copy", NULL, menu_copy_layer_cb, -1, "<StockItem>", GTK_STOCK_COPY },
1022 { "/Layers/_Paste", NULL, menu_paste_layer_cb, -1, "<StockItem>", GTK_STOCK_PASTE },
1023 { "/Layers/sep1", NULL, NULL, 0, "<Separator>" },
1024 { "/Layers/_Properties", NULL, menu_properties_cb, -1, "<StockItem>", GTK_STOCK_PROPERTIES },
1025 { "/Layers/_Delete", NULL, menu_delete_layer_cb, -1, "<StockItem>", GTK_STOCK_DELETE },
1026 { "/Layers/Delete All", NULL, clear_cb, 0, "<StockItem>", GTK_STOCK_CLEAR },
1027 { "/Layers/sep1", NULL, NULL, 0, "<Separator>" },
1030 { "/_Tools", NULL, NULL, 0, "<Branch>" },
1031 { "/Tools/sep1", NULL, NULL, 0, "<Tearoff>" },
1032 { "/Tools/_Zoom", "<ctrl><shift>Z", draw_set_current_tool, TOOL_ZOOM, "<Item>" },
1033 { "/Tools/_Ruler", "<ctrl><shift>R", draw_set_current_tool, TOOL_RULER, "<Item>" },
1037 static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
1038 static GtkItemFactoryEntry tool_sep = { "/_Tools/sep1", NULL, NULL, 0, "<Separator>" };
1040 static GtkWidget *window_create_menubar( VikWindow *window )
1042 GtkItemFactory *item_factory;
1043 GtkAccelGroup *accel_group;
1044 GtkItemFactoryEntry add_layer_item;
1047 g_assert ( sizeof(guint16)*2 <= sizeof(guint) ); /* FIXME: should be a compiler warning */
1049 accel_group = gtk_accel_group_new ();
1050 item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
1052 gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, window);
1053 gtk_window_add_accel_group ( GTK_WINDOW(window), accel_group);
1055 add_layer_item.accelerator = NULL;
1057 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
1059 /* TODO: FIXME: if name has a '/' in it it will get all messed up. why not have an itemfactory field with
1060 name, serialized icon, shortcut, etc.? */
1061 add_layer_item.path = g_strdup_printf("/_Layers/New %s Layer", vik_layer_get_interface(i)->name );
1062 add_layer_item.callback = menu_addlayer_cb;
1063 add_layer_item.callback_action = i;
1064 if ( vik_layer_get_interface(i)->icon )
1066 add_layer_item.item_type = "<ImageItem>";
1067 add_layer_item.extra_data = gdk_pixdata_serialize ( vik_layer_get_interface(i)->icon, &tmp );
1070 add_layer_item.item_type = "<Item>";
1071 gtk_item_factory_create_item ( item_factory, &add_layer_item, window, 1 );
1072 g_free ( add_layer_item.path );
1073 g_free ( (gpointer) add_layer_item.extra_data );
1075 if ( vik_layer_get_interface(i)->tools_count )
1076 gtk_item_factory_create_item ( item_factory, &tool_sep, window, 1 );
1078 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ )
1080 add_layer_item.path = g_strdup_printf("/_Tools/%s", vik_layer_get_interface(i)->tools[j].name);
1081 add_layer_item.callback = menu_dynamic_tool_cb;
1082 add_layer_item.callback_action = ( i << 16 ) | j;
1083 add_layer_item.item_type = "<Item>";
1084 gtk_item_factory_create_item ( item_factory, &add_layer_item, window, 1 );
1085 g_free ( add_layer_item.path );
1089 window->item_factory = item_factory;
1090 return gtk_item_factory_get_widget (item_factory, "<main>");