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, GdkEventMotion *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_pack1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp), FALSE, FALSE );
309 gtk_paned_pack2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp), TRUE, TRUE );
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));
403 static void window_configure_event ( VikWindow *vw )
405 static int first = 1;
408 // This is a hack to set the cursor corresponding to the first tool
409 // FIXME find the correct way to initialize both tool and its cursor
410 const GdkCursor *cursor = NULL;
412 cursor = toolbox_get_cursor(vw->vt, "Pan");
413 /* We set cursor, even if it is NULL: it resets to default */
414 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
418 static void draw_redraw ( VikWindow *vw )
420 VikCoord old_center = vw->trigger_center;
421 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
422 VikLayer *new_trigger = vw->trigger;
424 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
427 ; /* do nothing -- have to redraw everything. */
428 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
429 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
431 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
434 vik_viewport_clear ( vw->viking_vvp);
435 vik_layers_panel_draw_all ( vw->viking_vlp );
436 vik_viewport_draw_scale ( vw->viking_vvp );
437 vik_viewport_draw_centermark ( vw->viking_vvp );
439 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
442 gboolean draw_buf_done = TRUE;
444 static gboolean draw_buf(gpointer data)
446 gpointer *pass_along = data;
448 gdk_draw_drawable (pass_along[0], pass_along[1],
449 pass_along[2], 0, 0, 0, 0, -1, -1);
450 draw_buf_done = TRUE;
456 /* Mouse event handlers ************************************************************************/
458 static void vik_window_pan_click (VikWindow *vw, GdkEventButton *event)
460 /* set panning origin */
461 vw->pan_x = (gint) event->x;
462 vw->pan_y = (gint) event->y;
465 static void draw_click (VikWindow *vw, GdkEventButton *event)
467 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
469 /* middle button pressed. we reserve all middle button and scroll events
470 * for panning and zooming; tools only get left/right/movement
472 if ( event->button == 2) {
473 vik_window_pan_click ( vw, event );
476 toolbox_click(vw->vt, event);
480 static void vik_window_pan_move (VikWindow *vw, GdkEventMotion *event)
482 if ( vw->pan_x != -1 ) {
483 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
487 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
489 static VikCoord coord;
490 static struct UTM utm;
491 static struct LatLon ll;
492 #define BUFFER_SIZE 50
493 static char pointer_buf[BUFFER_SIZE];
494 gchar *lat = NULL, *lon = NULL;
497 VikDemInterpol interpol_method;
499 toolbox_move(vw->vt, event);
501 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
502 vik_coord_to_utm ( &coord, &utm );
503 a_coords_utm_to_latlon ( &utm, &ll );
504 a_coords_latlon_to_string ( &ll, &lat, &lon );
505 /* Change interpolate method according to scale */
506 zoom = vik_viewport_get_zoom(vw->viking_vvp);
508 interpol_method = VIK_DEM_INTERPOL_NONE;
509 else if (zoom >= 1.0)
510 interpol_method = VIK_DEM_INTERPOL_SIMPLE;
512 interpol_method = VIK_DEM_INTERPOL_BEST;
513 if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION)
514 g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s %dm"), lat, lon, alt );
516 g_snprintf ( pointer_buf, BUFFER_SIZE, _("%s %s"), lat, lon );
521 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
523 vik_window_pan_move ( vw, event );
526 static void vik_window_pan_release ( VikWindow *vw, GdkEventButton *event )
528 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
529 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
531 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
532 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
534 vw->pan_x = vw->pan_y = -1;
537 static void draw_release ( VikWindow *vw, GdkEventButton *event )
539 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
541 if ( event->button == 2 ) { /* move / pan */
542 vik_window_pan_release(vw, event);
545 toolbox_release(vw->vt, event);
549 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
551 if ( event->state == GDK_CONTROL_MASK ) {
552 /* control == pan up & down */
553 if ( event->direction == GDK_SCROLL_UP )
554 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
556 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 );
557 } else if ( event->state == GDK_SHIFT_MASK ) {
558 /* control-shift == pan left & right */
559 if ( event->direction == GDK_SCROLL_UP )
560 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
562 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 );
563 } else if ( event->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
564 if ( event->direction == GDK_SCROLL_UP ) {
565 /* make sure mouse is still over the same point on the map when we zoom */
568 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
569 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
570 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
571 vik_viewport_zoom_in (vw->viking_vvp);
572 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
573 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
574 center_y + (y - event->y) );
579 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
580 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
581 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
582 vik_viewport_zoom_out(vw->viking_vvp);
583 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
584 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
585 center_y + (y - event->y) );
588 if ( event->direction == GDK_SCROLL_UP )
589 vik_viewport_zoom_in (vw->viking_vvp);
591 vik_viewport_zoom_out (vw->viking_vvp);
599 /********************************************************************************
601 ********************************************************************************/
602 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
604 PangoFontDescription *pfd;
607 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
608 GdkGC *thickgc = gdk_gc_new(d);
610 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
611 gdouble dx = (x2-x1)/len*10;
612 gdouble dy = (y2-y1)/len*10;
613 gdouble c = cos(15.0 * M_PI/180.0);
614 gdouble s = sin(15.0 * M_PI/180.0);
616 gdouble baseangle = 0;
619 /* draw line with arrow ends */
621 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
622 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
623 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
626 a_viewport_clip_line(&x1, &y1, &x2, &y2);
627 gdk_draw_line(d, gc, x1, y1, x2, y2);
629 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
630 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
631 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
632 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
633 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
634 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
639 angle = atan2(dy, dx) + M_PI_2;
641 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
647 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
648 vik_coord_to_latlon ( &test, &ll );
649 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
650 a_coords_latlon_to_utm ( &ll, &u );
651 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
652 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
654 baseangle = M_PI - atan2(tx-x1, ty-y1);
665 gdk_gc_copy(thickgc, gc);
666 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
667 gdk_color_parse("#2255cc", &color);
668 gdk_gc_set_rgb_fg_color(thickgc, &color);
670 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);
673 gdk_gc_copy(thickgc, gc);
674 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
675 for (i=0; i<180; i++) {
676 c = cos(i*M_PI/90.0 + baseangle);
677 s = sin(i*M_PI/90.0 + baseangle);
680 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
682 gdouble ticksize = 2*CW;
683 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
687 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
688 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
689 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
690 c = (CR+CW*2)*cos(baseangle);
691 s = (CR+CW*2)*sin(baseangle);
692 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
693 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
696 #define LABEL(x, y, w, h) { \
697 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
698 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
699 gdk_draw_layout(d, gc, (x), (y), pl); }
704 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
706 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
707 pango_layout_set_font_description (pl, pfd);
708 pango_font_description_free (pfd);
710 pango_layout_set_text(pl, "N", -1);
711 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
713 /* draw label with distance */
714 if (distance >= 1000 && distance < 100000) {
715 g_sprintf(str, "%3.2f km", distance/1000.0);
716 } else if (distance < 1000) {
717 g_sprintf(str, "%d m", (int)distance);
719 g_sprintf(str, "%d km", (int)distance/1000);
721 pango_layout_set_text(pl, str, -1);
723 pango_layout_get_pixel_size ( pl, &wd, &hd );
726 yd = (y1+y2)/2 - hd/2 - dx;
729 yd = (y1+y2)/2 - hd/2 + dx;
732 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
737 LABEL(xd, yd, wd, hd);
739 /* draw label with bearing */
740 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
741 pango_layout_set_text(pl, str, -1);
742 pango_layout_get_pixel_size ( pl, &wb, &hb );
743 xb = x1 + CR*cos(angle-M_PI_2);
744 yb = y1 + CR*sin(angle-M_PI_2);
746 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
752 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
753 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
757 LABEL(xb, yb, wb, hb);
761 g_object_unref ( G_OBJECT ( pl ) );
762 g_object_unref ( G_OBJECT ( labgc ) );
763 g_object_unref ( G_OBJECT ( thickgc ) );
769 gboolean has_oldcoord;
771 } ruler_tool_state_t;
773 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
775 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
778 s->has_oldcoord = FALSE;
782 static void ruler_destroy (ruler_tool_state_t *s)
787 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
792 if ( event->button == 1 ) {
793 gchar *lat=NULL, *lon=NULL;
794 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
795 vik_coord_to_latlon ( &coord, &ll );
796 a_coords_latlon_to_string ( &ll, &lat, &lon );
797 if ( s->has_oldcoord ) {
798 temp = g_strdup_printf ( "%s %s DIFF %f meters", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
799 s->has_oldcoord = FALSE;
802 temp = g_strdup_printf ( "%s %s", lat, lon );
803 s->has_oldcoord = TRUE;
806 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
812 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
813 draw_update ( s->vw );
815 return VIK_LAYER_TOOL_ACK;
818 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventMotion *event, ruler_tool_state_t *s)
820 VikViewport *vvp = s->vvp;
821 VikWindow *vw = s->vw;
827 if ( s->has_oldcoord ) {
828 int oldx, oldy, w1, h1, w2, h2;
829 static GdkPixmap *buf = NULL;
830 gchar *lat=NULL, *lon=NULL;
831 w1 = vik_viewport_get_width(vvp);
832 h1 = vik_viewport_get_height(vvp);
834 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
836 gdk_drawable_get_size(buf, &w2, &h2);
837 if (w1 != w2 || h1 != h2) {
838 g_object_unref ( G_OBJECT ( buf ) );
839 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
842 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
843 vik_coord_to_latlon ( &coord, &ll );
844 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
846 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
847 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
848 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
850 static gpointer pass_along[3];
851 pass_along[0] = GTK_WIDGET(vvp)->window;
852 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
854 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
855 draw_buf_done = FALSE;
857 a_coords_latlon_to_string(&ll, &lat, &lon);
858 temp = g_strdup_printf ( "%s %s DIFF %f meters", lat, lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
859 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
862 return VIK_LAYER_TOOL_ACK;
865 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
867 return VIK_LAYER_TOOL_ACK;
870 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
872 draw_update ( s->vw );
875 static VikToolInterface ruler_tool =
877 (VikToolConstructorFunc) ruler_create,
878 (VikToolDestructorFunc) ruler_destroy,
879 (VikToolActivationFunc) NULL,
880 (VikToolActivationFunc) ruler_deactivate,
881 (VikToolMouseFunc) ruler_click,
882 (VikToolMouseMoveFunc) ruler_move,
883 (VikToolMouseFunc) ruler_release,
885 GDK_CURSOR_IS_PIXMAP,
886 &cursor_ruler_pixbuf };
887 /*** end ruler code ********************************************************/
891 /********************************************************************************
893 ********************************************************************************/
894 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
899 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
902 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
903 if ( event->button == 1 )
904 vik_viewport_zoom_in (vw->viking_vvp);
905 else if ( event->button == 3 )
906 vik_viewport_zoom_out (vw->viking_vvp);
908 return VIK_LAYER_TOOL_ACK;
911 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventMotion *event, VikViewport *vvp)
913 return VIK_LAYER_TOOL_ACK;
916 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
918 return VIK_LAYER_TOOL_ACK;
921 static VikToolInterface zoom_tool =
923 (VikToolConstructorFunc) zoomtool_create,
924 (VikToolDestructorFunc) NULL,
925 (VikToolActivationFunc) NULL,
926 (VikToolActivationFunc) NULL,
927 (VikToolMouseFunc) zoomtool_click,
928 (VikToolMouseMoveFunc) zoomtool_move,
929 (VikToolMouseFunc) zoomtool_release,
931 GDK_CURSOR_IS_PIXMAP,
932 &cursor_zoom_pixbuf };
933 /*** end zoom code ********************************************************/
935 /********************************************************************************
937 ********************************************************************************/
938 static gpointer pantool_create (VikWindow *vw, VikViewport *vvp)
943 static VikLayerToolFuncStatus pantool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
946 if ( event->button == 1 )
947 vik_window_pan_click ( vw, event );
949 return VIK_LAYER_TOOL_ACK;
952 static VikLayerToolFuncStatus pantool_move (VikLayer *vl, GdkEventMotion *event, VikWindow *vw)
954 vik_window_pan_move ( vw, event );
955 return VIK_LAYER_TOOL_ACK;
958 static VikLayerToolFuncStatus pantool_release (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
960 if ( event->button == 1 )
961 vik_window_pan_release ( vw, event );
962 return VIK_LAYER_TOOL_ACK;
965 static VikToolInterface pan_tool =
967 (VikToolConstructorFunc) pantool_create,
968 (VikToolDestructorFunc) NULL,
969 (VikToolActivationFunc) NULL,
970 (VikToolActivationFunc) NULL,
971 (VikToolMouseFunc) pantool_click,
972 (VikToolMouseMoveFunc) pantool_move,
973 (VikToolMouseFunc) pantool_release,
976 /*** end pan code ********************************************************/
978 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
980 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
981 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
982 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
983 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
984 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
985 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
986 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
987 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
992 static void full_screen_cb ( GtkAction *a, VikWindow *vw )
994 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
996 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
998 gtk_window_fullscreen ( GTK_WINDOW(vw) );
1000 gtk_window_unfullscreen ( GTK_WINDOW(vw) );
1003 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
1007 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
1010 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
1013 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
1016 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
1020 gchar *s = (gchar *)gtk_action_get_name(a);
1026 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
1027 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
1028 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
1029 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
1030 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
1035 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
1037 VikCoord new_center;
1039 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
1040 struct LatLon ll, llold;
1041 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
1042 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
1043 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
1047 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
1048 struct UTM utm, utmold;
1049 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
1050 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
1051 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
1056 g_critical("Houston, we've had a problem.");
1060 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
1064 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
1067 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
1068 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
1069 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
1071 vw->modified = TRUE;
1077 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
1079 a_clipboard_copy_selected ( vw->viking_vlp );
1082 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
1084 a_clipboard_copy_selected ( vw->viking_vlp );
1085 menu_delete_layer_cb ( a, vw );
1088 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1090 if ( a_clipboard_paste ( vw->viking_vlp ) )
1093 vw->modified = TRUE;
1097 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1099 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1100 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1103 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1105 a_dialog_about(GTK_WINDOW(vw));
1108 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1110 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1112 vik_layers_panel_delete_selected ( vw->viking_vlp );
1113 vw->modified = TRUE;
1116 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1119 static void view_side_panel_cb ( GtkAction *a, VikWindow *vw )
1121 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ViewSidePanel" );
1122 g_assert(check_box);
1123 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1125 gtk_widget_show(GTK_WIDGET(vw->viking_vlp));
1127 gtk_widget_hide(GTK_WIDGET(vw->viking_vlp));
1130 /***************************************
1131 ** tool management routines
1133 ***************************************/
1135 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1137 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1140 vt->active_tool = -1;
1142 if (!vw->viking_vvp) {
1143 g_critical("no viewport found.");
1149 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1151 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1152 vt->tools[vt->n_tools].ti = *vti;
1153 vt->tools[vt->n_tools].layer_type = layer_type;
1155 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1158 vt->tools[vt->n_tools].state = NULL;
1163 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1166 for (i=0; i<vt->n_tools; i++) {
1167 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1174 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1176 int tool = toolbox_get_tool(vt, tool_name);
1177 toolbox_tool_t *t = &vt->tools[tool];
1178 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1180 if (tool == vt->n_tools) {
1181 g_critical("trying to activate a non-existent tool...");
1184 /* is the tool already active? */
1185 if (vt->active_tool == tool) {
1189 if (vt->active_tool != -1) {
1190 if (vt->tools[vt->active_tool].ti.deactivate) {
1191 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1194 if (t->ti.activate) {
1195 t->ti.activate(vl, t->state);
1197 vt->active_tool = tool;
1200 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
1202 int tool = toolbox_get_tool(vt, tool_name);
1203 toolbox_tool_t *t = &vt->tools[tool];
1204 if (t->ti.cursor == NULL) {
1205 if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
1206 GError *cursor_load_err = NULL;
1207 GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
1208 /* TODO: settable offeset */
1209 t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
1210 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1212 t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
1215 return t->ti.cursor;
1218 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1220 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1221 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1222 gint ltype = vt->tools[vt->active_tool].layer_type;
1223 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1224 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1228 static void toolbox_move (toolbox_tools_t *vt, GdkEventMotion *event)
1230 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1231 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1232 gint ltype = vt->tools[vt->active_tool].layer_type;
1233 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1234 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1235 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1239 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1241 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1242 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1243 gint ltype = vt->tools[vt->active_tool].layer_type;
1244 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1245 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1248 /** End tool management ************************************/
1250 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1252 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1255 /* this function gets called whenever a toolbar tool is clicked */
1256 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1258 /* White Magic, my friends ... White Magic... */
1259 int layer_id, tool_id;
1260 const GdkCursor *cursor = NULL;
1262 toolbox_activate(vw->vt, gtk_action_get_name(a));
1264 cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
1265 /* We set cursor, even if it is NULL: it resets to default */
1266 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
1268 if (!strcmp(gtk_action_get_name(a), "Pan")) {
1269 vw->current_tool = TOOL_PAN;
1271 else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1272 vw->current_tool = TOOL_ZOOM;
1274 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1275 vw->current_tool = TOOL_RULER;
1278 /* TODO: only enable tools from active layer */
1279 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1280 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1281 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1282 vw->current_tool = TOOL_LAYER;
1283 vw->tool_layer_id = layer_id;
1284 vw->tool_tool_id = tool_id;
1292 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1297 g_free ( vw->filename );
1298 if ( filename == NULL )
1300 vw->filename = NULL;
1301 file = _("Untitled");
1305 vw->filename = g_strdup(filename);
1306 file = a_file_basename ( filename );
1308 title = g_strdup_printf( "%s - Viking", file );
1309 gtk_window_set_title ( GTK_WINDOW(vw), title );
1313 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1315 GtkWidget *mode_button;
1318 #ifdef VIK_CONFIG_EXPEDIA
1319 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1321 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1322 default: buttonname = "/ui/MainMenu/View/ModeUTM";
1324 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1325 g_assert ( mode_button );
1329 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1331 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1334 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1338 GtkWidget *mode_button;
1339 if ( change_filename )
1340 window_set_filename ( vw, filename );
1341 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1342 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. */
1343 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1344 vw->only_updating_coord_mode_ui = FALSE;
1346 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1348 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1349 g_assert ( mode_button );
1350 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1352 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1353 g_assert ( mode_button );
1354 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1356 default: draw_update ( vw );
1359 static void load_file ( GtkAction *a, VikWindow *vw )
1361 GSList *files = NULL;
1362 GSList *cur_file = NULL;
1364 if (!strcmp(gtk_action_get_name(a), "Open")) {
1367 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1371 g_critical("Houston, we've had a problem.");
1375 if ( ! vw->open_dia )
1377 vw->open_dia = gtk_file_chooser_dialog_new (_("Please select a GPS data file to open. "),
1379 GTK_FILE_CHOOSER_ACTION_OPEN,
1380 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1381 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1383 gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(vw->open_dia), TRUE );
1384 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1385 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1387 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_ACCEPT )
1389 gtk_widget_hide ( vw->open_dia );
1390 #ifdef VIKING_PROMPT_IF_MODIFIED
1391 if ( (vw->modified || vw->filename) && newwindow )
1393 if ( vw->filename && newwindow )
1395 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) ) );
1397 files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER(vw->open_dia) );
1398 gboolean change_fn = newwindow && (g_slist_length(files)==1); /* only change fn if one file */
1401 while ( cur_file ) {
1402 gchar *file_name = cur_file->data;
1403 vik_window_open_file ( vw, file_name, change_fn );
1405 cur_file = g_slist_next (cur_file);
1407 g_slist_free (files);
1411 gtk_widget_hide ( vw->open_dia );
1414 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1416 gboolean rv = FALSE;
1418 if ( ! vw->save_dia )
1420 vw->save_dia = gtk_file_chooser_dialog_new (_("Save as Viking File."),
1422 GTK_FILE_CHOOSER_ACTION_SAVE,
1423 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1424 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1426 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1427 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1430 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
1432 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
1433 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 ) ) )
1435 window_set_filename ( vw, fn );
1436 rv = window_save ( vw );
1437 vw->modified = FALSE;
1441 gtk_widget_hide ( vw->save_dia );
1445 static gboolean window_save ( VikWindow *vw )
1447 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1451 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1456 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1458 if ( ! vw->filename )
1459 return save_file_as ( NULL, vw );
1462 vw->modified = FALSE;
1463 return window_save ( vw );
1467 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1469 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1472 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1474 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1477 #ifdef VIK_CONFIG_GEOCACHES
1478 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1480 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1484 static void goto_address( GtkAction *a, VikWindow *vw)
1486 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1489 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1491 a_preferences_show_window ( GTK_WINDOW(vw) );
1494 static void clear_cb ( GtkAction *a, VikWindow *vw )
1496 vik_layers_panel_clear ( vw->viking_vlp );
1497 window_set_filename ( vw, NULL );
1501 static void window_close ( GtkAction *a, VikWindow *vw )
1503 if ( ! delete_event ( vw ) )
1504 gtk_widget_destroy ( GTK_WIDGET(vw) );
1507 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1509 if (save_file( NULL, vw)) {
1510 window_close( NULL, vw);
1517 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1519 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1520 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1522 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1523 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1528 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1530 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1531 GdkPixbuf *pixbuf_to_save;
1532 gdouble old_xmpp, old_ympp;
1533 GError *error = NULL;
1535 /* backup old zoom & set new */
1536 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1537 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1538 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1540 /* reset width and height: */
1541 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1543 /* draw all layers */
1546 /* save buffer as file. */
1547 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);
1548 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1551 g_warning("Unable to write to file %s: %s", fn, error->message );
1552 g_error_free (error);
1554 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1556 /* pretend like nothing happened ;) */
1557 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1558 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1559 vik_viewport_configure ( vw->viking_vvp );
1563 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 )
1565 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1566 gchar *name_of_file = g_malloc ( size );
1568 struct UTM utm_orig, utm;
1570 /* *** copied from above *** */
1571 GdkPixbuf *pixbuf_to_save;
1572 gdouble old_xmpp, old_ympp;
1573 GError *error = NULL;
1575 /* backup old zoom & set new */
1576 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1577 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1578 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1580 /* reset width and height: do this only once for all images (same size) */
1581 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1582 /* *** end copy from above *** */
1584 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1588 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1590 for ( y = 1; y <= tiles_h; y++ )
1592 for ( x = 1; x <= tiles_w; x++ )
1594 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1596 if ( tiles_w & 0x1 )
1597 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1599 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1600 if ( tiles_h & 0x1 ) /* odd */
1601 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1603 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1605 /* move to correct place. */
1606 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1610 /* save buffer as file. */
1611 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);
1612 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1615 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1616 g_error_free (error);
1619 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1623 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1624 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1625 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1626 vik_viewport_configure ( vw->viking_vvp );
1629 g_free ( name_of_file );
1632 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1634 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1635 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1636 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1637 gdouble width_min, width_max, height_min, height_max;
1640 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1641 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1643 /* TODO: support for xzoom and yzoom values */
1644 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1645 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1647 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1648 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1650 gtk_spin_button_set_value ( width_spin, width );
1651 gtk_spin_button_set_value ( height_spin, height );
1654 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1656 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1657 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1660 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1661 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1662 if (pass_along[4]) /* save many images; find TOTAL area covered */
1664 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1665 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1667 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1668 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1669 g_free ( label_text );
1672 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1674 /* todo: default for answers inside VikWindow or static (thruout instance) */
1675 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1676 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1678 GTK_RESPONSE_REJECT,
1680 GTK_RESPONSE_ACCEPT,
1682 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1683 GtkWidget *png_radio, *jpeg_radio;
1684 GtkWidget *current_window_button;
1685 gpointer current_window_pass_along[7];
1686 GtkWidget *zoom_label, *zoom_spin;
1687 GtkWidget *total_size_label;
1689 /* only used if (!one_image_only) */
1690 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1693 width_label = gtk_label_new ( _("Width (pixels):") );
1694 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1695 height_label = gtk_label_new ( _("Height (pixels):") );
1696 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1698 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1699 /* TODO: separate xzoom and yzoom factors */
1700 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);
1702 total_size_label = gtk_label_new ( NULL );
1704 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1705 current_window_pass_along [0] = vw;
1706 current_window_pass_along [1] = width_spin;
1707 current_window_pass_along [2] = height_spin;
1708 current_window_pass_along [3] = zoom_spin;
1709 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1710 current_window_pass_along [5] = NULL;
1711 current_window_pass_along [6] = total_size_label;
1712 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1714 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1715 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1717 if ( ! vw->draw_image_save_as_png )
1718 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1720 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1721 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1722 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1723 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1724 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1725 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1726 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1727 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1728 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1730 if ( ! one_image_only )
1732 GtkWidget *tiles_width_label, *tiles_height_label;
1735 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1736 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1737 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1738 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1739 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1740 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1741 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1742 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1744 current_window_pass_along [4] = tiles_width_spin;
1745 current_window_pass_along [5] = tiles_height_spin;
1746 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1747 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1749 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1750 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1751 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1752 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1754 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1756 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1758 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1760 gtk_widget_hide ( GTK_WIDGET(dialog) );
1761 if ( one_image_only )
1762 save_image_file ( vw, fn,
1763 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1764 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1765 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1766 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1768 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1769 save_image_dir ( vw, fn,
1770 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1771 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1772 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1773 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1774 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1775 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1777 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1780 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1784 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1787 if (!vw->save_img_dia) {
1788 vw->save_img_dia = gtk_file_chooser_dialog_new (_("Save Image"),
1790 GTK_FILE_CHOOSER_ACTION_SAVE,
1791 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1792 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1794 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1795 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1798 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
1800 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
1801 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 ) ) )
1803 draw_to_image_file ( vw, fn, TRUE );
1807 gtk_widget_hide ( vw->save_img_dia );
1810 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1814 if (!vw->save_img_dir_dia) {
1815 vw->save_img_dir_dia = gtk_file_chooser_dialog_new (_("Choose a directory to hold images"),
1817 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1818 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1819 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1821 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1822 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1825 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_ACCEPT )
1827 fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dir_dia) );
1830 draw_to_image_file ( vw, fn, FALSE );
1836 gtk_widget_hide ( vw->save_img_dir_dia );
1839 #if GTK_CHECK_VERSION(2,10,0)
1840 static void print_cb ( GtkAction *a, VikWindow *vw )
1842 a_print(vw, vw->viking_vvp);
1846 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1847 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1849 VikViewportDrawMode drawmode;
1850 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1851 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1853 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1854 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1856 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1857 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1860 g_critical("Houston, we've had a problem.");
1864 if ( !vw->only_updating_coord_mode_ui )
1866 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1867 if ( olddrawmode != drawmode )
1869 /* this takes care of coord mode too */
1870 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1871 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1872 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1873 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1874 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1881 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1883 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1884 g_assert(check_box);
1885 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1886 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1890 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1892 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1893 g_assert(check_box);
1894 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1895 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1899 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1901 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1902 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1903 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1904 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1905 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1907 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1908 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1912 gtk_widget_destroy ( colorsd );
1917 /***********************************************************************************************
1919 ***********************************************************************************************/
1921 static GtkActionEntry entries[] = {
1922 { "File", NULL, N_("_File"), 0, 0, 0 },
1923 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
1924 { "View", NULL, N_("_View"), 0, 0, 0 },
1925 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
1926 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
1927 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
1928 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
1929 { "Help", NULL, N_("_Help"), 0, 0, 0 },
1931 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
1932 { "Open", GTK_STOCK_OPEN, N_("_Open"), "<control>O", N_("Open a file"), (GCallback)load_file },
1933 { "Append", GTK_STOCK_ADD, N_("A_ppend File"), NULL, N_("Append data from a different file"), (GCallback)load_file },
1934 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
1935 { "AcquireGPS", NULL, N_("From _GPS"), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
1936 { "AcquireGoogle", NULL, N_("Google _Directions"), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1937 #ifdef VIK_CONFIG_GEOCACHES
1938 { "AcquireGC", NULL, N_("Geo_caches"), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1940 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
1941 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As"), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
1942 { "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 },
1943 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images"), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
1945 #if GTK_CHECK_VERSION(2,10,0)
1946 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
1949 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
1950 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
1952 { "GoogleMapsSearch", GTK_STOCK_JUMP_TO, N_("Go To Google Maps location"), NULL, N_("Go to address/place using Google Maps search"), (GCallback)goto_address },
1953 { "GotoLL", GTK_STOCK_QUIT, N_("_Go to Lat\\/Lon..."), NULL, N_("Go to arbitrary lat\\/lon coordinate"), (GCallback)draw_goto_cb },
1954 { "GotoUTM", GTK_STOCK_QUIT, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
1955 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Background Color..."), NULL, NULL, (GCallback)set_bg_color },
1956 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
1957 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
1958 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To"), "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1959 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
1960 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
1961 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
1962 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
1963 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
1964 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
1965 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
1966 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
1967 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
1968 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
1969 { "PanNorth", NULL, N_("Pan North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
1970 { "PanEast", NULL, N_("Pan East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
1971 { "PanSouth", NULL, N_("Pan South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
1972 { "PanWest", NULL, N_("Pan West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
1973 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
1975 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
1976 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
1977 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
1978 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
1979 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
1980 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences..."), NULL, NULL, (GCallback)preferences_cb },
1981 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
1983 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
1987 static GtkRadioActionEntry mode_entries[] = {
1988 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
1989 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
1990 { "ModeMercator", NULL, N_("_Mercator Mode"), "<control>g", NULL, 4 }
1993 static GtkRadioActionEntry tool_entries[] = {
1994 { "Pan", "vik-icon-pan", N_("_Pan"), "<control><shift>P", N_("Pan Tool"), 0 },
1995 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 1 },
1996 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 2 }
1999 static GtkToggleActionEntry toggle_entries[] = {
2000 { "ShowScale", NULL, N_("Show Scale"), NULL, N_("Show Scale"), (GCallback)set_draw_scale, TRUE },
2001 { "ShowCenterMark", NULL, N_("Show Center Mark"), NULL, N_("Show Center Mark"), (GCallback)set_draw_centermark, TRUE },
2002 { "FullScreen", GTK_STOCK_FULLSCREEN, N_("Full Screen"), "F11", N_("Activate full screen mode"), (GCallback)full_screen_cb, FALSE },
2003 { "ViewSidePanel", GTK_STOCK_INDEX, N_("Show Side Panel"), "F9", N_("Show Side Panel"), (GCallback)view_side_panel_cb, TRUE },
2006 #include "menu.xml.h"
2007 static void window_create_ui( VikWindow *window )
2010 GtkActionGroup *action_group;
2011 GtkAccelGroup *accel_group;
2014 GtkIconFactory *icon_factory;
2015 GtkIconSet *icon_set;
2016 GtkRadioActionEntry *tools = NULL, *radio;
2019 uim = gtk_ui_manager_new ();
2022 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
2023 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
2024 toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
2027 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
2028 g_error_free (error);
2032 action_group = gtk_action_group_new ("MenuActions");
2033 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
2034 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
2035 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
2036 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
2038 icon_factory = gtk_icon_factory_new ();
2039 gtk_icon_factory_add_default (icon_factory);
2041 register_vik_icons(icon_factory);
2044 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2045 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2046 radio = &tools[ntools];
2048 *radio = tool_entries[i];
2049 radio->value = ntools;
2052 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2053 GtkActionEntry action;
2054 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
2055 vik_layer_get_interface(i)->name,
2056 vik_layer_get_interface(i)->name,
2057 GTK_UI_MANAGER_MENUITEM, FALSE);
2059 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2060 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2061 gtk_icon_set_unref (icon_set);
2063 action.name = vik_layer_get_interface(i)->name;
2064 action.stock_id = vik_layer_get_interface(i)->name;
2065 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
2066 action.accelerator = NULL;
2067 action.tooltip = NULL;
2068 action.callback = (GCallback)menu_addlayer_cb;
2069 gtk_action_group_add_actions(action_group, &action, 1, window);
2071 if ( vik_layer_get_interface(i)->tools_count ) {
2072 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2073 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2076 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2077 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2078 radio = &tools[ntools];
2081 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
2082 _(vik_layer_get_interface(i)->tools[j].name),
2083 vik_layer_get_interface(i)->tools[j].name,
2084 GTK_UI_MANAGER_MENUITEM, FALSE);
2085 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
2086 _(vik_layer_get_interface(i)->tools[j].name),
2087 vik_layer_get_interface(i)->tools[j].name,
2088 GTK_UI_MANAGER_TOOLITEM, FALSE);
2090 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
2092 radio->name = vik_layer_get_interface(i)->tools[j].name;
2093 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
2094 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
2095 radio->accelerator = NULL;
2096 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
2097 radio->value = ntools;
2100 g_object_unref (icon_factory);
2102 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2105 gtk_ui_manager_insert_action_group (uim, action_group, 0);
2107 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2108 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2109 GtkAction *action = gtk_action_group_get_action(action_group,
2110 vik_layer_get_interface(i)->tools[j].name);
2111 g_object_set(action, "sensitive", FALSE, NULL);
2114 window->action_group = action_group;
2116 accel_group = gtk_ui_manager_get_accel_group (uim);
2117 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2118 gtk_ui_manager_ensure_update (uim);
2124 const GdkPixdata *data;
2127 { &begintr_18_pixbuf, "Begin Track" },
2128 { &iscissors_18_pixbuf, "Magic Scissors" },
2129 { &mover_22_pixbuf, "vik-icon-pan" },
2130 { &demdl_18_pixbuf, "DEM Download/Import" },
2131 { &showpic_18_pixbuf, "Show Picture" },
2132 { &addtr_18_pixbuf, "Create Track" },
2133 { &edtr_18_pixbuf, "Edit Trackpoint" },
2134 { &addwp_18_pixbuf, "Create Waypoint" },
2135 { &edwp_18_pixbuf, "Edit Waypoint" },
2136 { &zoom_18_pixbuf, "vik-icon-zoom" },
2137 { &ruler_18_pixbuf, "vik-icon-ruler" },
2138 { &geozoom_18_pixbuf, "Georef Zoom Tool" },
2139 { &geomove_18_pixbuf, "Georef Move Map" },
2140 { &mapdl_18_pixbuf, "Maps Download" },
2141 { &showpic_18_pixbuf, "Show Picture" },
2144 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2147 register_vik_icons (GtkIconFactory *icon_factory)
2149 GtkIconSet *icon_set;
2152 for (i = 0; i < n_stock_icons; i++) {
2153 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2154 stock_icons[i].data, FALSE, NULL ));
2155 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2156 gtk_icon_set_unref (icon_set);