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
27 #include "background.h"
29 #include "datasources.h"
30 #include "googlesearch.h"
33 #include "preferences.h"
34 #include "icons/icons.h"
41 #include <glib/gstdio.h>
42 #include <glib/gprintf.h>
43 #include <glib/gi18n.h>
45 #define VIKING_WINDOW_WIDTH 1000
46 #define VIKING_WINDOW_HEIGHT 800
47 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
48 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
49 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
51 static void window_finalize ( GObject *gob );
52 static GObjectClass *parent_class;
54 static void window_init ( VikWindow *vw );
55 static void window_class_init ( VikWindowClass *klass );
56 static void window_set_filename ( VikWindow *vw, const gchar *filename );
58 static void draw_update ( VikWindow *vw );
60 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
64 static gboolean delete_event( VikWindow *vw );
66 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
68 static void window_configure_event ( VikWindow *vw );
69 static void draw_sync ( VikWindow *vw );
70 static void draw_redraw ( VikWindow *vw );
71 static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
72 static void draw_click ( VikWindow *vw, GdkEventButton *event );
73 static void draw_release ( VikWindow *vw, GdkEventButton *event );
74 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
75 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
76 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
78 static void draw_status ();
80 /* End Drawing Functions */
82 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
83 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
84 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
92 #define TOOL_LAYER_TYPE_NONE -1
97 toolbox_tool_t *tools;
101 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
102 static toolbox_tools_t* toolbox_create(VikWindow *vw);
103 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
104 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
105 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
106 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name);
107 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
108 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
109 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
113 static void window_create_ui( VikWindow *window );
114 static void register_vik_icons (GtkIconFactory *icon_factory);
117 static void load_file ( GtkAction *a, VikWindow *vw );
118 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
119 static gboolean save_file ( GtkAction *a, VikWindow *vw );
120 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
121 static gboolean window_save ( VikWindow *vw );
125 VikViewport *viking_vvp;
126 VikLayersPanel *viking_vlp;
127 VikStatusbar *viking_vs;
131 GtkItemFactory *item_factory;
133 /* tool management state */
136 guint16 tool_layer_id;
137 guint16 tool_tool_id;
139 GtkActionGroup *action_group;
143 guint draw_image_width, draw_image_height;
144 gboolean draw_image_save_as_png;
149 GtkWidget *open_dia, *save_dia;
150 GtkWidget *save_img_dia, *save_img_dir_dia;
152 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
155 /* half-drawn update */
157 VikCoord trigger_center;
170 VW_OPENWINDOW_SIGNAL,
174 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
176 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Pan"), N_("Zoom"), N_("Ruler") };
178 GType vik_window_get_type (void)
180 static GType vw_type = 0;
184 static const GTypeInfo vw_info =
186 sizeof (VikWindowClass),
187 NULL, /* base_init */
188 NULL, /* base_finalize */
189 (GClassInitFunc) window_class_init, /* class_init */
190 NULL, /* class_finalize */
191 NULL, /* class_data */
194 (GInstanceInitFunc) window_init,
196 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
202 VikViewport * vik_window_viewport(VikWindow *vw)
204 return(vw->viking_vvp);
207 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
209 int i, j, tool_count;
210 VikLayerInterface *layer_interface;
212 if (!vw->action_group) return;
214 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
216 layer_interface = vik_layer_get_interface(i);
217 tool_count = layer_interface->tools_count;
219 for (j = 0; j < tool_count; j++) {
220 action = gtk_action_group_get_action(vw->action_group,
221 layer_interface->tools[j].name);
222 g_object_set(action, "sensitive", i == vl->type, NULL);
227 static void window_finalize ( GObject *gob )
229 VikWindow *vw = VIK_WINDOW(gob);
230 g_return_if_fail ( vw != NULL );
232 a_background_remove_status ( vw->viking_vs );
234 G_OBJECT_CLASS(parent_class)->finalize(gob);
238 static void window_class_init ( VikWindowClass *klass )
241 GObjectClass *object_class;
243 window_signals[VW_NEWWINDOW_SIGNAL] = g_signal_new ( "newwindow", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikWindowClass, newwindow), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
244 window_signals[VW_OPENWINDOW_SIGNAL] = g_signal_new ( "openwindow", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikWindowClass, openwindow), NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
246 object_class = G_OBJECT_CLASS (klass);
248 object_class->finalize = window_finalize;
250 parent_class = g_type_class_peek_parent (klass);
254 static void window_init ( VikWindow *vw )
256 GtkWidget *main_vbox;
259 vw->action_group = NULL;
261 vw->viking_vvp = vik_viewport_new();
262 vw->viking_vlp = vik_layers_panel_new();
263 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
264 vw->viking_vs = vik_statusbar_new();
266 vw->vt = toolbox_create(vw);
267 window_create_ui(vw);
268 window_set_filename (vw, NULL);
270 toolbox_activate(vw->vt, "Pan");
273 vw->item_factory = NULL;
275 vw->modified = FALSE;
276 vw->only_updating_coord_mode_ui = FALSE;
278 vw->pan_x = vw->pan_y = -1;
279 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
280 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
281 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
283 main_vbox = gtk_vbox_new(FALSE, 1);
284 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
286 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
287 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
288 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
289 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
292 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
294 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
295 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
296 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK );
297 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
298 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
299 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
300 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
301 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
303 g_signal_connect_swapped (G_OBJECT (vw->viking_vvp), "key_press_event", G_CALLBACK (key_press_event), vw);
305 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
307 hpaned = gtk_hpaned_new ();
308 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
309 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
311 /* This packs the button into the window (a gtk container). */
312 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
314 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
316 a_background_add_status(vw->viking_vs);
320 vw->save_img_dia = NULL;
321 vw->save_img_dir_dia = NULL;
324 VikWindow *vik_window_new ()
326 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
329 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data )
331 VikLayer *vl = vik_layers_panel_get_selected ( vw->viking_vlp );
332 if (vl && vw->vt->active_tool != -1 && vw->vt->tools[vw->vt->active_tool].ti.key_press ) {
333 gint ltype = vw->vt->tools[vw->vt->active_tool].layer_type;
334 if ( vl && ltype == vl->type )
335 return vw->vt->tools[vw->vt->active_tool].ti.key_press(vl, event, vw->vt->tools[vw->vt->active_tool].state);
337 return FALSE; /* don't handle the keypress */
340 static gboolean delete_event( VikWindow *vw )
342 #ifdef VIKING_PROMPT_IF_MODIFIED
349 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
350 _("Do you want to save the changes you made to the document \"%s\"?\n"
352 "Your changes will be lost if you don't save them."),
353 vw->filename ? a_file_basename ( vw->filename ) : _("Untitled") ) );
354 gtk_dialog_add_buttons ( dia, _("Don't Save"), GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
355 switch ( gtk_dialog_run ( dia ) )
357 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
358 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
359 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
366 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
368 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
371 static void draw_update ( VikWindow *vw )
377 static void draw_sync ( VikWindow *vw )
379 vik_viewport_sync(vw->viking_vvp);
381 /* other things may be necc here later. */
384 static void draw_status ( VikWindow *vw )
386 static gchar zoom_level[22];
387 g_snprintf ( zoom_level, 22, "%.3f/%.3f %s", vik_viewport_get_xmpp (vw->viking_vvp), vik_viewport_get_ympp(vw->viking_vvp), vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM ? _("mpp") : _("pixelfact") );
388 if ( vw->current_tool == TOOL_LAYER )
389 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
391 vik_statusbar_set_message ( vw->viking_vs, 0, _(tool_names[vw->current_tool]) );
393 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
396 void vik_window_set_redraw_trigger(VikLayer *vl)
398 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
402 static void window_configure_event ( VikWindow *vw )
404 static int first = 1;
407 // This is a hack to set the cursor corresponding to the first tool
408 // FIXME find the correct way to initialize both tool and its cursor
409 const GdkCursor *cursor = NULL;
411 cursor = toolbox_get_cursor(vw->vt, "Pan");
412 /* We set cursor, even if it is NULL: it resets to default */
413 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
417 static void draw_redraw ( VikWindow *vw )
419 VikCoord old_center = vw->trigger_center;
420 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
421 VikLayer *new_trigger = vw->trigger;
423 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
426 ; /* do nothing -- have to redraw everything. */
427 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
428 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
430 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
433 vik_viewport_clear ( vw->viking_vvp);
434 vik_layers_panel_draw_all ( vw->viking_vlp );
435 vik_viewport_draw_scale ( vw->viking_vvp );
436 vik_viewport_draw_centermark ( vw->viking_vvp );
438 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
441 gboolean draw_buf_done = TRUE;
443 static gboolean draw_buf(gpointer data)
445 gpointer *pass_along = data;
447 gdk_draw_drawable (pass_along[0], pass_along[1],
448 pass_along[2], 0, 0, 0, 0, -1, -1);
449 draw_buf_done = TRUE;
455 /* Mouse event handlers ************************************************************************/
457 static void vik_window_pan_click (VikWindow *vw, GdkEventButton *event)
459 /* set panning origin */
460 vw->pan_x = (gint) event->x;
461 vw->pan_y = (gint) event->y;
464 static void draw_click (VikWindow *vw, GdkEventButton *event)
466 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
468 /* middle button pressed. we reserve all middle button and scroll events
469 * for panning and zooming; tools only get left/right/movement
471 if ( event->button == 2) {
472 vik_window_pan_click ( vw, event );
475 toolbox_click(vw->vt, event);
479 static void vik_window_pan_move (VikWindow *vw, GdkEventMotion *event)
481 if ( vw->pan_x != -1 ) {
482 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
486 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
488 static VikCoord coord;
489 static struct UTM utm;
490 static struct LatLon ll;
491 static char pointer_buf[36];
494 VikDemInterpol interpol_method;
496 toolbox_move(vw->vt, event);
498 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
499 vik_coord_to_utm ( &coord, &utm );
500 a_coords_utm_to_latlon ( &utm, &ll );
501 /* Change interpolate method according to scale */
502 zoom = vik_viewport_get_zoom(vw->viking_vvp);
504 interpol_method = VIK_DEM_INTERPOL_NONE;
505 else if (zoom >= 1.0)
506 interpol_method = VIK_DEM_INTERPOL_SIMPLE;
508 interpol_method = VIK_DEM_INTERPOL_BEST;
509 if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION)
510 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f %dm"), ll.lat, ll.lon, alt );
512 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f"), ll.lat, ll.lon );
513 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
515 vik_window_pan_move ( vw, event );
518 static void vik_window_pan_release ( VikWindow *vw, GdkEventButton *event )
520 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
521 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
523 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
524 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
526 vw->pan_x = vw->pan_y = -1;
529 static void draw_release ( VikWindow *vw, GdkEventButton *event )
531 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
533 if ( event->button == 2 ) { /* move / pan */
534 vik_window_pan_release(vw, event);
537 toolbox_release(vw->vt, event);
541 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
543 if ( event->state == GDK_CONTROL_MASK ) {
544 /* control == pan up & down */
545 if ( event->direction == GDK_SCROLL_UP )
546 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
548 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)*2/3 );
549 } else if ( event->state == GDK_SHIFT_MASK ) {
550 /* control-shift == pan left & right */
551 if ( event->direction == GDK_SCROLL_UP )
552 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
554 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)*2/3, vik_viewport_get_height(vw->viking_vvp)/2 );
555 } else if ( event->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
556 if ( event->direction == GDK_SCROLL_UP ) {
557 /* make sure mouse is still over the same point on the map when we zoom */
560 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
561 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
562 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
563 vik_viewport_zoom_in (vw->viking_vvp);
564 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
565 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
566 center_y + (y - event->y) );
571 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
572 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
573 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
574 vik_viewport_zoom_out(vw->viking_vvp);
575 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
576 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
577 center_y + (y - event->y) );
580 if ( event->direction == GDK_SCROLL_UP )
581 vik_viewport_zoom_in (vw->viking_vvp);
583 vik_viewport_zoom_out (vw->viking_vvp);
591 /********************************************************************************
593 ********************************************************************************/
594 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
596 PangoFontDescription *pfd;
599 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
600 GdkGC *thickgc = gdk_gc_new(d);
602 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
603 gdouble dx = (x2-x1)/len*10;
604 gdouble dy = (y2-y1)/len*10;
605 gdouble c = cos(15.0 * M_PI/180.0);
606 gdouble s = sin(15.0 * M_PI/180.0);
608 gdouble baseangle = 0;
611 /* draw line with arrow ends */
613 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
614 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
615 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
618 a_viewport_clip_line(&x1, &y1, &x2, &y2);
619 gdk_draw_line(d, gc, x1, y1, x2, y2);
621 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
622 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
623 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
624 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
625 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
626 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
631 angle = atan2(dy, dx) + M_PI_2;
633 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
639 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
640 vik_coord_to_latlon ( &test, &ll );
641 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
642 a_coords_latlon_to_utm ( &ll, &u );
643 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
644 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
646 baseangle = M_PI - atan2(tx-x1, ty-y1);
657 gdk_gc_copy(thickgc, gc);
658 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
659 gdk_color_parse("#2255cc", &color);
660 gdk_gc_set_rgb_fg_color(thickgc, &color);
662 gdk_draw_arc (d, thickgc, FALSE, x1-CR+CW/2, y1-CR+CW/2, 2*CR-CW, 2*CR-CW, (90 - baseangle*180/M_PI)*64, -angle*180/M_PI*64);
665 gdk_gc_copy(thickgc, gc);
666 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
667 for (i=0; i<180; i++) {
668 c = cos(i*M_PI/90.0 + baseangle);
669 s = sin(i*M_PI/90.0 + baseangle);
672 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
674 gdouble ticksize = 2*CW;
675 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
679 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
680 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
681 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
682 c = (CR+CW*2)*cos(baseangle);
683 s = (CR+CW*2)*sin(baseangle);
684 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
685 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
688 #define LABEL(x, y, w, h) { \
689 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
690 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
691 gdk_draw_layout(d, gc, (x), (y), pl); }
696 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
698 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
699 pango_layout_set_font_description (pl, pfd);
700 pango_font_description_free (pfd);
702 pango_layout_set_text(pl, "N", -1);
703 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
705 /* draw label with distance */
706 if (distance >= 1000 && distance < 100000) {
707 g_sprintf(str, "%3.2f km", distance/1000.0);
708 } else if (distance < 1000) {
709 g_sprintf(str, "%d m", (int)distance);
711 g_sprintf(str, "%d km", (int)distance/1000);
713 pango_layout_set_text(pl, str, -1);
715 pango_layout_get_pixel_size ( pl, &wd, &hd );
718 yd = (y1+y2)/2 - hd/2 - dx;
721 yd = (y1+y2)/2 - hd/2 + dx;
724 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
729 LABEL(xd, yd, wd, hd);
731 /* draw label with bearing */
732 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
733 pango_layout_set_text(pl, str, -1);
734 pango_layout_get_pixel_size ( pl, &wb, &hb );
735 xb = x1 + CR*cos(angle-M_PI_2);
736 yb = y1 + CR*sin(angle-M_PI_2);
738 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
744 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
745 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
749 LABEL(xb, yb, wb, hb);
753 g_object_unref ( G_OBJECT ( pl ) );
754 g_object_unref ( G_OBJECT ( labgc ) );
755 g_object_unref ( G_OBJECT ( thickgc ) );
761 gboolean has_oldcoord;
763 } ruler_tool_state_t;
765 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
767 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
770 s->has_oldcoord = FALSE;
774 static void ruler_destroy (ruler_tool_state_t *s)
779 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
784 if ( event->button == 1 ) {
785 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
786 vik_coord_to_latlon ( &coord, &ll );
787 if ( s->has_oldcoord ) {
788 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
789 s->has_oldcoord = FALSE;
792 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
793 s->has_oldcoord = TRUE;
796 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
802 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
803 draw_update ( s->vw );
805 return VIK_LAYER_TOOL_ACK;
808 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
810 VikViewport *vvp = s->vvp;
811 VikWindow *vw = s->vw;
817 if ( s->has_oldcoord ) {
818 int oldx, oldy, w1, h1, w2, h2;
819 static GdkPixmap *buf = NULL;
820 w1 = vik_viewport_get_width(vvp);
821 h1 = vik_viewport_get_height(vvp);
823 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
825 gdk_drawable_get_size(buf, &w2, &h2);
826 if (w1 != w2 || h1 != h2) {
827 g_object_unref ( G_OBJECT ( buf ) );
828 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
831 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
832 vik_coord_to_latlon ( &coord, &ll );
833 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
835 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
836 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
837 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
839 static gpointer pass_along[3];
840 pass_along[0] = GTK_WIDGET(vvp)->window;
841 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
843 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
844 draw_buf_done = FALSE;
847 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
848 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
851 return VIK_LAYER_TOOL_ACK;
854 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
856 return VIK_LAYER_TOOL_ACK;
859 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
861 draw_update ( s->vw );
864 static VikToolInterface ruler_tool =
866 (VikToolConstructorFunc) ruler_create,
867 (VikToolDestructorFunc) ruler_destroy,
868 (VikToolActivationFunc) NULL,
869 (VikToolActivationFunc) ruler_deactivate,
870 (VikToolMouseFunc) ruler_click,
871 (VikToolMouseFunc) ruler_move,
872 (VikToolMouseFunc) ruler_release,
874 GDK_CURSOR_IS_PIXMAP,
876 /*** end ruler code ********************************************************/
880 /********************************************************************************
882 ********************************************************************************/
883 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
888 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
891 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
892 if ( event->button == 1 )
893 vik_viewport_zoom_in (vw->viking_vvp);
894 else if ( event->button == 3 )
895 vik_viewport_zoom_out (vw->viking_vvp);
897 return VIK_LAYER_TOOL_ACK;
900 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
902 return VIK_LAYER_TOOL_ACK;
905 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
907 return VIK_LAYER_TOOL_ACK;
910 static VikToolInterface zoom_tool =
912 (VikToolConstructorFunc) zoomtool_create,
913 (VikToolDestructorFunc) NULL,
914 (VikToolActivationFunc) NULL,
915 (VikToolActivationFunc) NULL,
916 (VikToolMouseFunc) zoomtool_click,
917 (VikToolMouseFunc) zoomtool_move,
918 (VikToolMouseFunc) zoomtool_release,
920 GDK_CURSOR_IS_PIXMAP,
922 /*** end zoom code ********************************************************/
924 /********************************************************************************
926 ********************************************************************************/
927 static gpointer pantool_create (VikWindow *vw, VikViewport *vvp)
932 static VikLayerToolFuncStatus pantool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
935 if ( event->button == 1 )
936 vik_window_pan_click ( vw, event );
938 return VIK_LAYER_TOOL_ACK;
941 static VikLayerToolFuncStatus pantool_move (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
943 vik_window_pan_move ( vw, event );
944 return VIK_LAYER_TOOL_ACK;
947 static VikLayerToolFuncStatus pantool_release (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
949 if ( event->button == 1 )
950 vik_window_pan_release ( vw, event );
951 return VIK_LAYER_TOOL_ACK;
954 static VikToolInterface pan_tool =
956 (VikToolConstructorFunc) pantool_create,
957 (VikToolDestructorFunc) NULL,
958 (VikToolActivationFunc) NULL,
959 (VikToolActivationFunc) NULL,
960 (VikToolMouseFunc) pantool_click,
961 (VikToolMouseFunc) pantool_move,
962 (VikToolMouseFunc) pantool_release,
965 /*** end pan code ********************************************************/
967 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
969 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
970 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
971 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
972 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
973 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
974 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
975 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
976 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
981 static void full_screen_cb ( GtkAction *a, VikWindow *vw )
983 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
985 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
987 gtk_window_fullscreen ( GTK_WINDOW(vw) );
989 gtk_window_unfullscreen ( GTK_WINDOW(vw) );
992 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
996 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
999 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
1002 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
1005 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
1009 gchar *s = (gchar *)gtk_action_get_name(a);
1015 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
1016 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
1017 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
1018 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
1019 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
1024 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
1026 VikCoord new_center;
1028 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
1029 struct LatLon ll, llold;
1030 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
1031 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
1032 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
1036 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
1037 struct UTM utm, utmold;
1038 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
1039 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
1040 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
1045 g_critical("Houston, we've had a problem.");
1049 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
1053 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
1056 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
1057 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
1058 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
1060 vw->modified = TRUE;
1066 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
1068 a_clipboard_copy_selected ( vw->viking_vlp );
1071 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
1073 a_clipboard_copy_selected ( vw->viking_vlp );
1074 menu_delete_layer_cb ( a, vw );
1077 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1079 if ( a_clipboard_paste ( vw->viking_vlp ) )
1082 vw->modified = TRUE;
1086 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1088 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1089 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1092 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1094 a_dialog_about(GTK_WINDOW(vw));
1097 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1099 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1101 vik_layers_panel_delete_selected ( vw->viking_vlp );
1102 vw->modified = TRUE;
1105 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1108 /***************************************
1109 ** tool management routines
1111 ***************************************/
1113 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1115 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1118 vt->active_tool = -1;
1120 if (!vw->viking_vvp) {
1121 g_critical("no viewport found.");
1127 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1129 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1130 vt->tools[vt->n_tools].ti = *vti;
1131 vt->tools[vt->n_tools].layer_type = layer_type;
1133 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1136 vt->tools[vt->n_tools].state = NULL;
1141 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1144 for (i=0; i<vt->n_tools; i++) {
1145 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1152 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1154 int tool = toolbox_get_tool(vt, tool_name);
1155 toolbox_tool_t *t = &vt->tools[tool];
1156 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1158 if (tool == vt->n_tools) {
1159 g_critical("trying to activate a non-existent tool...");
1162 /* is the tool already active? */
1163 if (vt->active_tool == tool) {
1167 if (vt->active_tool != -1) {
1168 if (vt->tools[vt->active_tool].ti.deactivate) {
1169 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1172 if (t->ti.activate) {
1173 t->ti.activate(vl, t->state);
1175 vt->active_tool = tool;
1178 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
1180 int tool = toolbox_get_tool(vt, tool_name);
1181 toolbox_tool_t *t = &vt->tools[tool];
1182 if (t->ti.cursor == NULL) {
1183 if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
1184 GError *cursor_load_err = NULL;
1185 GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
1186 /* TODO: settable offeset */
1187 t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
1188 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1190 t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
1193 return t->ti.cursor;
1196 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1198 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1199 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1200 gint ltype = vt->tools[vt->active_tool].layer_type;
1201 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1202 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1206 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1208 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1209 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1210 gint ltype = vt->tools[vt->active_tool].layer_type;
1211 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1212 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1213 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1217 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1219 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1220 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1221 gint ltype = vt->tools[vt->active_tool].layer_type;
1222 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1223 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1226 /** End tool management ************************************/
1228 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1230 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1233 /* this function gets called whenever a toolbar tool is clicked */
1234 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1236 /* White Magic, my friends ... White Magic... */
1237 int layer_id, tool_id;
1238 const GdkCursor *cursor = NULL;
1240 toolbox_activate(vw->vt, gtk_action_get_name(a));
1242 cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
1243 /* We set cursor, even if it is NULL: it resets to default */
1244 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
1246 if (!strcmp(gtk_action_get_name(a), "Pan")) {
1247 vw->current_tool = TOOL_PAN;
1249 else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1250 vw->current_tool = TOOL_ZOOM;
1252 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1253 vw->current_tool = TOOL_RULER;
1256 /* TODO: only enable tools from active layer */
1257 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1258 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1259 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1260 vw->current_tool = TOOL_LAYER;
1261 vw->tool_layer_id = layer_id;
1262 vw->tool_tool_id = tool_id;
1270 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1275 g_free ( vw->filename );
1276 if ( filename == NULL )
1278 vw->filename = NULL;
1279 file = _("Untitled");
1283 vw->filename = g_strdup(filename);
1284 file = a_file_basename ( filename );
1286 title = g_strdup_printf( "%s - Viking", file );
1287 gtk_window_set_title ( GTK_WINDOW(vw), title );
1291 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1293 GtkWidget *mode_button;
1296 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1297 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1298 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1299 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1300 default: buttonname = "/ui/MainMenu/View/ModeKH";
1302 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1303 g_assert ( mode_button );
1307 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1309 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1312 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1316 GtkWidget *mode_button;
1317 if ( change_filename )
1318 window_set_filename ( vw, filename );
1319 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1320 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. */
1321 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1322 vw->only_updating_coord_mode_ui = FALSE;
1324 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1326 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1327 g_assert ( mode_button );
1328 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1330 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1331 g_assert ( mode_button );
1332 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1334 default: draw_update ( vw );
1337 static void load_file ( GtkAction *a, VikWindow *vw )
1339 GSList *files = NULL;
1340 GSList *cur_file = NULL;
1342 if (!strcmp(gtk_action_get_name(a), "Open")) {
1345 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1349 g_critical("Houston, we've had a problem.");
1353 if ( ! vw->open_dia )
1355 vw->open_dia = gtk_file_chooser_dialog_new (_("Please select a GPS data file to open. "),
1357 GTK_FILE_CHOOSER_ACTION_OPEN,
1358 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1359 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1361 gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(vw->open_dia), TRUE );
1362 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1363 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1365 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_ACCEPT )
1367 gtk_widget_hide ( vw->open_dia );
1368 #ifdef VIKING_PROMPT_IF_MODIFIED
1369 if ( (vw->modified || vw->filename) && newwindow )
1371 if ( vw->filename && newwindow )
1373 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) ) );
1375 files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) );
1376 gboolean change_fn = newwindow && (g_slist_length(files)==1); /* only change fn if one file */
1379 while ( cur_file ) {
1380 gchar *file_name = cur_file->data;
1381 vik_window_open_file ( vw, file_name, change_fn );
1383 cur_file = g_slist_next (cur_file);
1385 g_slist_free (files);
1389 gtk_widget_hide ( vw->open_dia );
1392 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1394 gboolean rv = FALSE;
1396 if ( ! vw->save_dia )
1398 vw->save_dia = gtk_file_chooser_dialog_new (_("Save as Viking File."),
1400 GTK_FILE_CHOOSER_ACTION_SAVE,
1401 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1402 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1404 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1405 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1408 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
1410 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
1411 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_overwrite ( GTK_WINDOW(vw->save_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
1413 window_set_filename ( vw, fn );
1414 rv = window_save ( vw );
1415 vw->modified = FALSE;
1419 gtk_widget_hide ( vw->save_dia );
1423 static gboolean window_save ( VikWindow *vw )
1425 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1429 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1434 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1436 if ( ! vw->filename )
1437 return save_file_as ( NULL, vw );
1440 vw->modified = FALSE;
1441 return window_save ( vw );
1445 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1447 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1450 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1452 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1455 #ifdef VIK_CONFIG_GEOCACHES
1456 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1458 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1462 static void goto_address( GtkAction *a, VikWindow *vw)
1464 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1467 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1469 a_preferences_show_window ( GTK_WINDOW(vw) );
1472 static void clear_cb ( GtkAction *a, VikWindow *vw )
1474 vik_layers_panel_clear ( vw->viking_vlp );
1475 window_set_filename ( vw, NULL );
1479 static void window_close ( GtkAction *a, VikWindow *vw )
1481 if ( ! delete_event ( vw ) )
1482 gtk_widget_destroy ( GTK_WIDGET(vw) );
1485 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1487 if (save_file( NULL, vw)) {
1488 window_close( NULL, vw);
1495 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1497 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1498 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1500 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1501 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1506 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1508 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1509 GdkPixbuf *pixbuf_to_save;
1510 gdouble old_xmpp, old_ympp;
1511 GError *error = NULL;
1513 /* backup old zoom & set new */
1514 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1515 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1516 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1518 /* reset width and height: */
1519 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1521 /* draw all layers */
1524 /* save buffer as file. */
1525 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);
1526 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1529 g_warning("Unable to write to file %s: %s", fn, error->message );
1530 g_error_free (error);
1532 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1534 /* pretend like nothing happened ;) */
1535 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1536 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1537 vik_viewport_configure ( vw->viking_vvp );
1541 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 )
1543 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1544 gchar *name_of_file = g_malloc ( size );
1546 struct UTM utm_orig, utm;
1548 /* *** copied from above *** */
1549 GdkPixbuf *pixbuf_to_save;
1550 gdouble old_xmpp, old_ympp;
1551 GError *error = NULL;
1553 /* backup old zoom & set new */
1554 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1555 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1556 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1558 /* reset width and height: do this only once for all images (same size) */
1559 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1560 /* *** end copy from above *** */
1562 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1566 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1568 for ( y = 1; y <= tiles_h; y++ )
1570 for ( x = 1; x <= tiles_w; x++ )
1572 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1574 if ( tiles_w & 0x1 )
1575 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1577 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1578 if ( tiles_h & 0x1 ) /* odd */
1579 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1581 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1583 /* move to correct place. */
1584 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1588 /* save buffer as file. */
1589 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);
1590 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1593 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1594 g_error_free (error);
1597 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1601 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1602 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1603 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1604 vik_viewport_configure ( vw->viking_vvp );
1607 g_free ( name_of_file );
1610 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1612 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1613 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1614 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1615 gdouble width_min, width_max, height_min, height_max;
1618 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1619 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1621 /* TODO: support for xzoom and yzoom values */
1622 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1623 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1625 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1626 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1628 gtk_spin_button_set_value ( width_spin, width );
1629 gtk_spin_button_set_value ( height_spin, height );
1632 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1634 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1635 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1638 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1639 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1640 if (pass_along[4]) /* save many images; find TOTAL area covered */
1642 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1643 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1645 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1646 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1647 g_free ( label_text );
1650 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1652 /* todo: default for answers inside VikWindow or static (thruout instance) */
1653 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1654 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1656 GTK_RESPONSE_REJECT,
1658 GTK_RESPONSE_ACCEPT,
1660 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1661 GtkWidget *png_radio, *jpeg_radio;
1662 GtkWidget *current_window_button;
1663 gpointer current_window_pass_along[7];
1664 GtkWidget *zoom_label, *zoom_spin;
1665 GtkWidget *total_size_label;
1667 /* only used if (!one_image_only) */
1668 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1671 width_label = gtk_label_new ( _("Width (pixels):") );
1672 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1673 height_label = gtk_label_new ( _("Height (pixels):") );
1674 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1676 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1677 /* TODO: separate xzoom and yzoom factors */
1678 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);
1680 total_size_label = gtk_label_new ( NULL );
1682 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1683 current_window_pass_along [0] = vw;
1684 current_window_pass_along [1] = width_spin;
1685 current_window_pass_along [2] = height_spin;
1686 current_window_pass_along [3] = zoom_spin;
1687 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1688 current_window_pass_along [5] = NULL;
1689 current_window_pass_along [6] = total_size_label;
1690 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1692 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1693 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1695 if ( ! vw->draw_image_save_as_png )
1696 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1698 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1699 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1700 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1701 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1702 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1703 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1704 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1705 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1706 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1708 if ( ! one_image_only )
1710 GtkWidget *tiles_width_label, *tiles_height_label;
1713 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1714 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1715 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1716 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1717 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1718 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1719 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1720 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1722 current_window_pass_along [4] = tiles_width_spin;
1723 current_window_pass_along [5] = tiles_height_spin;
1724 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1725 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1727 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1728 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1729 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1730 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1732 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1734 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1736 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1738 gtk_widget_hide ( GTK_WIDGET(dialog) );
1739 if ( one_image_only )
1740 save_image_file ( vw, fn,
1741 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1742 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1743 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1744 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1746 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1747 save_image_dir ( vw, fn,
1748 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1749 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1750 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1751 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1752 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1753 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1755 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1758 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1762 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1765 if (!vw->save_img_dia) {
1766 vw->save_img_dia = gtk_file_chooser_dialog_new (_("Save Image"),
1768 GTK_FILE_CHOOSER_ACTION_SAVE,
1769 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1770 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1772 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1773 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1776 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
1778 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
1779 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_overwrite ( GTK_WINDOW(vw->save_img_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
1781 draw_to_image_file ( vw, fn, TRUE );
1785 gtk_widget_hide ( vw->save_img_dia );
1788 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1792 if (!vw->save_img_dir_dia) {
1793 vw->save_img_dir_dia = gtk_file_chooser_dialog_new (_("Choose a directory to hold images"),
1795 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1796 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1797 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1799 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1800 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1803 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_ACCEPT )
1805 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dir_dia) );
1808 draw_to_image_file ( vw, fn, FALSE );
1814 gtk_widget_hide ( vw->save_img_dir_dia );
1817 #if GTK_CHECK_VERSION(2,10,0)
1818 static void print_cb ( GtkAction *a, VikWindow *vw )
1820 a_print(vw, vw->viking_vvp);
1824 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1825 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1827 VikViewportDrawMode drawmode;
1828 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1829 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1831 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1832 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1834 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1835 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1837 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1838 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1840 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1841 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1844 g_critical("Houston, we've had a problem.");
1848 if ( !vw->only_updating_coord_mode_ui )
1850 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1851 if ( olddrawmode != drawmode )
1853 /* this takes care of coord mode too */
1854 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1855 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1856 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1857 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1858 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1865 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1867 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1868 g_assert(check_box);
1869 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1870 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1874 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1876 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1877 g_assert(check_box);
1878 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1879 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1883 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1885 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1886 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1887 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1888 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1889 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1891 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1892 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1896 gtk_widget_destroy ( colorsd );
1901 /***********************************************************************************************
1903 ***********************************************************************************************/
1905 static GtkActionEntry entries[] = {
1906 { "File", NULL, N_("_File"), 0, 0, 0 },
1907 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
1908 { "View", NULL, N_("_View"), 0, 0, 0 },
1909 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
1910 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
1911 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
1912 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
1913 { "Help", NULL, N_("_Help"), 0, 0, 0 },
1915 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
1916 { "Open", GTK_STOCK_OPEN, N_("_Open"), "<control>O", N_("Open a file"), (GCallback)load_file },
1917 { "Append", GTK_STOCK_ADD, N_("A_ppend File"), NULL, N_("Append data from a different file"), (GCallback)load_file },
1918 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
1919 { "AcquireGPS", NULL, N_("From _GPS"), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
1920 { "AcquireGoogle", NULL, N_("Google _Directions"), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1921 #ifdef VIK_CONFIG_GEOCACHES
1922 { "AcquireGC", NULL, N_("Geo_caches"), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1924 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
1925 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As"), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
1926 { "GenImg", GTK_STOCK_CLEAR, N_("_Generate Image File"), NULL, N_("Save a snapshot of the workspace into a file"), (GCallback)draw_to_image_file_cb },
1927 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images"), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
1929 #if GTK_CHECK_VERSION(2,10,0)
1930 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
1933 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
1934 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
1936 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, N_("Go To Google Maps location"), NULL, N_("Go to address/place using Google Maps search"), (GCallback)goto_address },
1937 { "GotoLL", GTK_STOCK_QUIT, N_("_Go to Lat\\/Lon..."), NULL, N_("Go to arbitrary lat\\/lon coordinate"), (GCallback)draw_goto_cb },
1938 { "GotoUTM", GTK_STOCK_QUIT, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
1939 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Background Color..."), NULL, NULL, (GCallback)set_bg_color },
1940 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
1941 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
1942 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To"), "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1943 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
1944 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
1945 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
1946 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
1947 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
1948 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
1949 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
1950 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
1951 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
1952 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
1953 { "PanNorth", NULL, N_("Pan North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
1954 { "PanEast", NULL, N_("Pan East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
1955 { "PanSouth", NULL, N_("Pan South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
1956 { "PanWest", NULL, N_("Pan West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
1957 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
1959 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
1960 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
1961 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
1962 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
1963 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
1964 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences..."), NULL, NULL, (GCallback)preferences_cb },
1965 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
1967 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
1971 static GtkRadioActionEntry mode_entries[] = {
1972 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
1973 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
1974 { "ModeGoogle", NULL, N_("_Old Google Mode"), "<control>o", NULL, 2 },
1975 { "ModeKH", NULL, N_("Old _KH Mode"), "<control>k", NULL, 3 },
1976 { "ModeMercator", NULL, N_("_Google Mode"), "<control>g", NULL, 4 }
1979 static GtkRadioActionEntry tool_entries[] = {
1980 { "Pan", "vik-icon-pan", N_("_Pan"), "<control><shift>P", N_("Pan Tool"), 0 },
1981 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 1 },
1982 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 2 }
1985 static GtkToggleActionEntry toggle_entries[] = {
1986 { "ShowScale", NULL, N_("Show Scale"), NULL, NULL, (GCallback)set_draw_scale, TRUE },
1987 { "ShowCenterMark", NULL, N_("Show Center Mark"), NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1988 { "FullScreen", NULL, N_("Full Screen"), "F11", NULL, (GCallback)full_screen_cb, FALSE },
1991 #include "menu.xml.h"
1992 static void window_create_ui( VikWindow *window )
1995 GtkActionGroup *action_group;
1996 GtkAccelGroup *accel_group;
1999 GtkIconFactory *icon_factory;
2000 GtkIconSet *icon_set;
2001 GtkRadioActionEntry *tools = NULL, *radio;
2004 uim = gtk_ui_manager_new ();
2007 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
2008 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
2009 toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
2012 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
2013 g_error_free (error);
2017 action_group = gtk_action_group_new ("MenuActions");
2018 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
2019 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
2020 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
2021 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
2023 icon_factory = gtk_icon_factory_new ();
2024 gtk_icon_factory_add_default (icon_factory);
2026 register_vik_icons(icon_factory);
2029 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2030 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2031 radio = &tools[ntools];
2033 *radio = tool_entries[i];
2034 radio->value = ntools;
2037 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2038 GtkActionEntry action;
2039 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
2040 vik_layer_get_interface(i)->name,
2041 vik_layer_get_interface(i)->name,
2042 GTK_UI_MANAGER_MENUITEM, FALSE);
2044 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2045 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2046 gtk_icon_set_unref (icon_set);
2048 action.name = vik_layer_get_interface(i)->name;
2049 action.stock_id = vik_layer_get_interface(i)->name;
2050 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
2051 action.accelerator = NULL;
2052 action.tooltip = NULL;
2053 action.callback = (GCallback)menu_addlayer_cb;
2054 gtk_action_group_add_actions(action_group, &action, 1, window);
2056 if ( vik_layer_get_interface(i)->tools_count ) {
2057 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2058 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2061 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2062 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2063 radio = &tools[ntools];
2066 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
2067 _(vik_layer_get_interface(i)->tools[j].name),
2068 vik_layer_get_interface(i)->tools[j].name,
2069 GTK_UI_MANAGER_MENUITEM, FALSE);
2070 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
2071 _(vik_layer_get_interface(i)->tools[j].name),
2072 vik_layer_get_interface(i)->tools[j].name,
2073 GTK_UI_MANAGER_TOOLITEM, FALSE);
2075 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
2077 radio->name = vik_layer_get_interface(i)->tools[j].name;
2078 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
2079 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
2080 radio->accelerator = NULL;
2081 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
2082 radio->value = ntools;
2085 g_object_unref (icon_factory);
2087 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2090 gtk_ui_manager_insert_action_group (uim, action_group, 0);
2092 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2093 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2094 GtkAction *action = gtk_action_group_get_action(action_group,
2095 vik_layer_get_interface(i)->tools[j].name);
2096 g_object_set(action, "sensitive", FALSE, NULL);
2099 window->action_group = action_group;
2101 accel_group = gtk_ui_manager_get_accel_group (uim);
2102 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2103 gtk_ui_manager_ensure_update (uim);
2109 const GdkPixdata *data;
2112 { &addtr_18, "Create Track" },
2113 { &begintr_18, "Begin Track" },
2114 { &edtr_18, "Edit Trackpoint" },
2115 { &addwp_18, "Create Waypoint" },
2116 { &edwp_18, "Edit Waypoint" },
2117 { &iscissors_18, "Magic Scissors" },
2118 { &mover_22, "vik-icon-pan" },
2119 { &zoom_18, "vik-icon-zoom" },
2120 { &ruler_18, "vik-icon-ruler" },
2121 { &geozoom_18, "Georef Zoom Tool" },
2122 { &geomove_18, "Georef Move Map" },
2123 { &mapdl_18, "Maps Download" },
2124 { &demdl_18, "DEM Download/Import" },
2125 { &showpic_18, "Show Picture" },
2128 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2131 register_vik_icons (GtkIconFactory *icon_factory)
2133 GtkIconSet *icon_set;
2136 for (i = 0; i < n_stock_icons; i++) {
2137 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2138 stock_icons[i].data, FALSE, NULL ));
2139 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2140 gtk_icon_set_unref (icon_set);