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"
39 #include <glib/gprintf.h>
40 #include <glib/gi18n.h>
42 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
43 #define make_dir(dir) mkdir(dir)
45 #include <sys/types.h>
47 #define make_dir(dir) mkdir(dir,0777)
50 #define VIKING_WINDOW_WIDTH 1000
51 #define VIKING_WINDOW_HEIGHT 800
52 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
53 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
54 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
56 static void window_finalize ( GObject *gob );
57 static GObjectClass *parent_class;
59 static void window_init ( VikWindow *vw );
60 static void window_class_init ( VikWindowClass *klass );
61 static void window_set_filename ( VikWindow *vw, const gchar *filename );
63 static void draw_update ( VikWindow *vw );
65 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
69 static gboolean delete_event( VikWindow *vw );
71 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
73 static void window_configure_event ( VikWindow *vw );
74 static void draw_sync ( VikWindow *vw );
75 static void draw_redraw ( VikWindow *vw );
76 static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
77 static void draw_click ( VikWindow *vw, GdkEventButton *event );
78 static void draw_release ( VikWindow *vw, GdkEventButton *event );
79 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
80 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
81 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
83 static void draw_status ();
85 /* End Drawing Functions */
87 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
88 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
89 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
97 #define TOOL_LAYER_TYPE_NONE -1
102 toolbox_tool_t *tools;
106 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
107 static toolbox_tools_t* toolbox_create(VikWindow *vw);
108 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
109 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
110 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
111 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
112 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
113 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
117 static void window_create_ui( VikWindow *window );
118 static void register_vik_icons (GtkIconFactory *icon_factory);
121 static void load_file ( GtkAction *a, VikWindow *vw );
122 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
123 static gboolean save_file ( GtkAction *a, VikWindow *vw );
124 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
125 static gboolean window_save ( VikWindow *vw );
129 VikViewport *viking_vvp;
130 VikLayersPanel *viking_vlp;
131 VikStatusbar *viking_vs;
135 GtkItemFactory *item_factory;
137 /* tool management state */
140 guint16 tool_layer_id;
141 guint16 tool_tool_id;
143 GtkActionGroup *action_group;
147 guint draw_image_width, draw_image_height;
148 gboolean draw_image_save_as_png;
153 GtkWidget *open_dia, *save_dia;
154 GtkWidget *save_img_dia, *save_img_dir_dia;
156 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
159 /* half-drawn update */
161 VikCoord trigger_center;
174 VW_OPENWINDOW_SIGNAL,
178 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
180 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Pan"), N_("Zoom"), N_("Ruler") };
182 GdkCursor *vw_cursor_pan = NULL;
183 GdkCursor *vw_cursor_zoom = NULL;
184 GdkCursor *vw_cursor_ruler = NULL;
186 GType vik_window_get_type (void)
188 static GType vw_type = 0;
192 static const GTypeInfo vw_info =
194 sizeof (VikWindowClass),
195 NULL, /* base_init */
196 NULL, /* base_finalize */
197 (GClassInitFunc) window_class_init, /* class_init */
198 NULL, /* class_finalize */
199 NULL, /* class_data */
202 (GInstanceInitFunc) window_init,
204 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
210 VikViewport * vik_window_viewport(VikWindow *vw)
212 return(vw->viking_vvp);
215 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
217 int i, j, tool_count;
218 VikLayerInterface *layer_interface;
220 if (!vw->action_group) return;
222 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
224 layer_interface = vik_layer_get_interface(i);
225 tool_count = layer_interface->tools_count;
227 for (j = 0; j < tool_count; j++) {
228 action = gtk_action_group_get_action(vw->action_group,
229 layer_interface->tools[j].name);
230 g_object_set(action, "sensitive", i == vl->type, NULL);
235 static void window_finalize ( GObject *gob )
237 VikWindow *vw = VIK_WINDOW(gob);
238 g_return_if_fail ( vw != NULL );
240 a_background_remove_status ( vw->viking_vs );
242 G_OBJECT_CLASS(parent_class)->finalize(gob);
246 static void window_class_init ( VikWindowClass *klass )
249 GObjectClass *object_class;
251 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);
252 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);
254 object_class = G_OBJECT_CLASS (klass);
256 object_class->finalize = window_finalize;
258 parent_class = g_type_class_peek_parent (klass);
262 static void window_init ( VikWindow *vw )
264 GtkWidget *main_vbox;
267 vw->action_group = NULL;
269 vw->viking_vvp = vik_viewport_new();
270 vw->viking_vlp = vik_layers_panel_new();
271 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
272 vw->viking_vs = vik_statusbar_new();
274 vw->vt = toolbox_create(vw);
275 window_create_ui(vw);
276 window_set_filename (vw, NULL);
278 toolbox_activate(vw->vt, "Pan");
281 vw->item_factory = NULL;
283 vw->modified = FALSE;
284 vw->only_updating_coord_mode_ui = FALSE;
286 vw->pan_x = vw->pan_y = -1;
287 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
288 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
289 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
291 main_vbox = gtk_vbox_new(FALSE, 1);
292 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
294 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
295 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
296 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
297 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
300 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
302 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
303 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
304 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 );
305 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
306 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
307 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
308 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
309 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
311 g_signal_connect_swapped (G_OBJECT (vw->viking_vvp), "key_press_event", G_CALLBACK (key_press_event), vw);
313 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
315 hpaned = gtk_hpaned_new ();
316 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
317 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
319 /* This packs the button into the window (a gtk container). */
320 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
322 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
324 a_background_add_status(vw->viking_vs);
328 vw->save_img_dia = NULL;
329 vw->save_img_dir_dia = NULL;
332 VikWindow *vik_window_new ()
334 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
337 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data )
339 VikLayer *vl = vik_layers_panel_get_selected ( vw->viking_vlp );
340 if (vl && vw->vt->active_tool != -1 && vw->vt->tools[vw->vt->active_tool].ti.key_press ) {
341 gint ltype = vw->vt->tools[vw->vt->active_tool].layer_type;
342 if ( vl && ltype == vl->type )
343 return vw->vt->tools[vw->vt->active_tool].ti.key_press(vl, event, vw->vt->tools[vw->vt->active_tool].state);
345 return FALSE; /* don't handle the keypress */
348 static gboolean delete_event( VikWindow *vw )
350 #ifdef VIKING_PROMPT_IF_MODIFIED
357 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
358 _("Do you want to save the changes you made to the document \"%s\"?\n"
360 "Your changes will be lost if you don't save them."),
361 vw->filename ? a_file_basename ( vw->filename ) : _("Untitled") ) );
362 gtk_dialog_add_buttons ( dia, _("Don't Save"), GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
363 switch ( gtk_dialog_run ( dia ) )
365 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
366 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
367 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
374 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
376 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
379 static void draw_update ( VikWindow *vw )
385 static void draw_sync ( VikWindow *vw )
387 vik_viewport_sync(vw->viking_vvp);
389 /* other things may be necc here later. */
392 static void draw_status ( VikWindow *vw )
394 static gchar zoom_level[22];
395 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") );
396 if ( vw->current_tool == TOOL_LAYER )
397 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
399 vik_statusbar_set_message ( vw->viking_vs, 0, _(tool_names[vw->current_tool]) );
401 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
404 void vik_window_set_redraw_trigger(VikLayer *vl)
406 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
410 static void window_configure_event ( VikWindow *vw )
415 // This is a hack to set the cursor corresponding to the first tool
416 // FIXME find the correct way to initialize both tool and its cursor
418 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_pan );
422 static void draw_redraw ( VikWindow *vw )
424 VikCoord old_center = vw->trigger_center;
425 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
426 VikLayer *new_trigger = vw->trigger;
428 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
431 ; /* do nothing -- have to redraw everything. */
432 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
433 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
435 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
438 vik_viewport_clear ( vw->viking_vvp);
439 vik_layers_panel_draw_all ( vw->viking_vlp );
440 vik_viewport_draw_scale ( vw->viking_vvp );
441 vik_viewport_draw_centermark ( vw->viking_vvp );
443 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
446 gboolean draw_buf_done = TRUE;
448 static gboolean draw_buf(gpointer data)
450 gpointer *pass_along = data;
452 gdk_draw_drawable (pass_along[0], pass_along[1],
453 pass_along[2], 0, 0, 0, 0, -1, -1);
454 draw_buf_done = TRUE;
460 /* Mouse event handlers ************************************************************************/
462 static void vik_window_pan_click (VikWindow *vw, GdkEventButton *event)
464 /* set panning origin */
465 vw->pan_x = (gint) event->x;
466 vw->pan_y = (gint) event->y;
469 static void draw_click (VikWindow *vw, GdkEventButton *event)
471 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
473 /* middle button pressed. we reserve all middle button and scroll events
474 * for panning and zooming; tools only get left/right/movement
476 if ( event->button == 2) {
477 vik_window_pan_click ( vw, event );
480 toolbox_click(vw->vt, event);
484 static void vik_window_pan_move (VikWindow *vw, GdkEventMotion *event)
486 if ( vw->pan_x != -1 ) {
487 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
491 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
493 static VikCoord coord;
494 static struct UTM utm;
495 static struct LatLon ll;
496 static char pointer_buf[36];
499 VikDemInterpol interpol_method;
501 toolbox_move(vw->vt, event);
503 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
504 vik_coord_to_utm ( &coord, &utm );
505 a_coords_utm_to_latlon ( &utm, &ll );
506 /* Change interpolate method according to scale */
507 zoom = vik_viewport_get_zoom(vw->viking_vvp);
509 interpol_method = VIK_DEM_INTERPOL_NONE;
510 else if (zoom >= 1.0)
511 interpol_method = VIK_DEM_INTERPOL_SIMPLE;
513 interpol_method = VIK_DEM_INTERPOL_BEST;
514 if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION)
515 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f %dm"), ll.lat, ll.lon, alt );
517 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f"), ll.lat, ll.lon );
518 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
520 vik_window_pan_move ( vw, event );
523 static void vik_window_pan_release ( VikWindow *vw, GdkEventButton *event )
525 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
526 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
528 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
529 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
531 vw->pan_x = vw->pan_y = -1;
534 static void draw_release ( VikWindow *vw, GdkEventButton *event )
536 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
538 if ( event->button == 2 ) { /* move / pan */
539 vik_window_pan_release(vw, event);
542 toolbox_release(vw->vt, event);
546 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
548 if ( event->state == GDK_CONTROL_MASK ) {
549 /* control == pan up & down */
550 if ( event->direction == GDK_SCROLL_UP )
551 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
553 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 );
554 } else if ( event->state == GDK_SHIFT_MASK ) {
555 /* control-shift == pan left & right */
556 if ( event->direction == GDK_SCROLL_UP )
557 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
559 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 );
560 } else if ( event->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
561 if ( event->direction == GDK_SCROLL_UP ) {
562 /* make sure mouse is still over the same point on the map when we zoom */
565 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
566 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
567 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
568 vik_viewport_zoom_in (vw->viking_vvp);
569 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
570 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
571 center_y + (y - event->y) );
576 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
577 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
578 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
579 vik_viewport_zoom_out(vw->viking_vvp);
580 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
581 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
582 center_y + (y - event->y) );
585 if ( event->direction == GDK_SCROLL_UP )
586 vik_viewport_zoom_in (vw->viking_vvp);
588 vik_viewport_zoom_out (vw->viking_vvp);
596 /********************************************************************************
598 ********************************************************************************/
599 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
601 PangoFontDescription *pfd;
604 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
605 GdkGC *thickgc = gdk_gc_new(d);
607 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
608 gdouble dx = (x2-x1)/len*10;
609 gdouble dy = (y2-y1)/len*10;
610 gdouble c = cos(15.0 * M_PI/180.0);
611 gdouble s = sin(15.0 * M_PI/180.0);
613 gdouble baseangle = 0;
616 /* draw line with arrow ends */
618 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
619 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
620 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
623 a_viewport_clip_line(&x1, &y1, &x2, &y2);
624 gdk_draw_line(d, gc, x1, y1, x2, y2);
626 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
627 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
628 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
629 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
630 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
631 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
636 angle = atan2(dy, dx) + M_PI_2;
638 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
644 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
645 vik_coord_to_latlon ( &test, &ll );
646 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
647 a_coords_latlon_to_utm ( &ll, &u );
648 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
649 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
651 baseangle = M_PI - atan2(tx-x1, ty-y1);
662 gdk_gc_copy(thickgc, gc);
663 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
664 gdk_color_parse("#2255cc", &color);
665 gdk_gc_set_rgb_fg_color(thickgc, &color);
667 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);
670 gdk_gc_copy(thickgc, gc);
671 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
672 for (i=0; i<180; i++) {
673 c = cos(i*M_PI/90.0 + baseangle);
674 s = sin(i*M_PI/90.0 + baseangle);
677 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
679 gdouble ticksize = 2*CW;
680 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
684 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
685 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
686 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
687 c = (CR+CW*2)*cos(baseangle);
688 s = (CR+CW*2)*sin(baseangle);
689 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
690 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
693 #define LABEL(x, y, w, h) { \
694 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
695 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
696 gdk_draw_layout(d, gc, (x), (y), pl); }
701 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
703 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
704 pango_layout_set_font_description (pl, pfd);
705 pango_font_description_free (pfd);
707 pango_layout_set_text(pl, "N", -1);
708 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
710 /* draw label with distance */
711 if (distance >= 1000 && distance < 100000) {
712 g_sprintf(str, "%3.2f km", distance/1000.0);
713 } else if (distance < 1000) {
714 g_sprintf(str, "%d m", (int)distance);
716 g_sprintf(str, "%d km", (int)distance/1000);
718 pango_layout_set_text(pl, str, -1);
720 pango_layout_get_pixel_size ( pl, &wd, &hd );
723 yd = (y1+y2)/2 - hd/2 - dx;
726 yd = (y1+y2)/2 - hd/2 + dx;
729 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
734 LABEL(xd, yd, wd, hd);
736 /* draw label with bearing */
737 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
738 pango_layout_set_text(pl, str, -1);
739 pango_layout_get_pixel_size ( pl, &wb, &hb );
740 xb = x1 + CR*cos(angle-M_PI_2);
741 yb = y1 + CR*sin(angle-M_PI_2);
743 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
749 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
750 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
754 LABEL(xb, yb, wb, hb);
758 g_object_unref ( G_OBJECT ( pl ) );
759 g_object_unref ( G_OBJECT ( labgc ) );
760 g_object_unref ( G_OBJECT ( thickgc ) );
766 gboolean has_oldcoord;
768 } ruler_tool_state_t;
770 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
772 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
775 s->has_oldcoord = FALSE;
779 static void ruler_destroy (ruler_tool_state_t *s)
784 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
789 if ( event->button == 1 ) {
790 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
791 vik_coord_to_latlon ( &coord, &ll );
792 if ( s->has_oldcoord ) {
793 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
794 s->has_oldcoord = FALSE;
797 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
798 s->has_oldcoord = TRUE;
801 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
807 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
808 draw_update ( s->vw );
810 return VIK_LAYER_TOOL_ACK;
813 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
815 VikViewport *vvp = s->vvp;
816 VikWindow *vw = s->vw;
822 if ( s->has_oldcoord ) {
823 int oldx, oldy, w1, h1, w2, h2;
824 static GdkPixmap *buf = NULL;
825 w1 = vik_viewport_get_width(vvp);
826 h1 = vik_viewport_get_height(vvp);
828 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
830 gdk_drawable_get_size(buf, &w2, &h2);
831 if (w1 != w2 || h1 != h2) {
832 g_object_unref ( G_OBJECT ( buf ) );
833 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
836 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
837 vik_coord_to_latlon ( &coord, &ll );
838 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
840 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
841 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
842 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
844 static gpointer pass_along[3];
845 pass_along[0] = GTK_WIDGET(vvp)->window;
846 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
848 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
849 draw_buf_done = FALSE;
852 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
853 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
856 return VIK_LAYER_TOOL_ACK;
859 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
861 return VIK_LAYER_TOOL_ACK;
864 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
866 draw_update ( s->vw );
869 static VikToolInterface ruler_tool =
871 (VikToolConstructorFunc) ruler_create,
872 (VikToolDestructorFunc) ruler_destroy,
873 (VikToolActivationFunc) NULL,
874 (VikToolActivationFunc) ruler_deactivate,
875 (VikToolMouseFunc) ruler_click,
876 (VikToolMouseFunc) ruler_move,
877 (VikToolMouseFunc) ruler_release };
878 /*** end ruler code ********************************************************/
882 /********************************************************************************
884 ********************************************************************************/
885 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
890 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
893 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
894 if ( event->button == 1 )
895 vik_viewport_zoom_in (vw->viking_vvp);
896 else if ( event->button == 3 )
897 vik_viewport_zoom_out (vw->viking_vvp);
899 return VIK_LAYER_TOOL_ACK;
902 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
904 return VIK_LAYER_TOOL_ACK;
907 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
909 return VIK_LAYER_TOOL_ACK;
912 static VikToolInterface zoom_tool =
914 (VikToolConstructorFunc) zoomtool_create,
915 (VikToolDestructorFunc) NULL,
916 (VikToolActivationFunc) NULL,
917 (VikToolActivationFunc) NULL,
918 (VikToolMouseFunc) zoomtool_click,
919 (VikToolMouseFunc) zoomtool_move,
920 (VikToolMouseFunc) zoomtool_release };
921 /*** end zoom code ********************************************************/
923 /********************************************************************************
925 ********************************************************************************/
926 static gpointer pantool_create (VikWindow *vw, VikViewport *vvp)
931 static VikLayerToolFuncStatus pantool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
934 if ( event->button == 1 )
935 vik_window_pan_click ( vw, event );
937 return VIK_LAYER_TOOL_ACK;
940 static VikLayerToolFuncStatus pantool_move (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
942 vik_window_pan_move ( vw, event );
943 return VIK_LAYER_TOOL_ACK;
946 static VikLayerToolFuncStatus pantool_release (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
948 if ( event->button == 1 )
949 vik_window_pan_release ( vw, event );
950 return VIK_LAYER_TOOL_ACK;
953 static VikToolInterface pan_tool =
955 (VikToolConstructorFunc) pantool_create,
956 (VikToolDestructorFunc) NULL,
957 (VikToolActivationFunc) NULL,
958 (VikToolActivationFunc) NULL,
959 (VikToolMouseFunc) pantool_click,
960 (VikToolMouseFunc) pantool_move,
961 (VikToolMouseFunc) pantool_release };
962 /*** end pan code ********************************************************/
964 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
966 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
967 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
968 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
969 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
970 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
971 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
972 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
973 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
978 static void full_screen_cb ( GtkAction *a, VikWindow *vw )
980 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
982 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
984 gtk_window_fullscreen ( GTK_WINDOW(vw) );
986 gtk_window_unfullscreen ( GTK_WINDOW(vw) );
989 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
993 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
996 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
999 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
1002 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
1006 gchar *s = (gchar *)gtk_action_get_name(a);
1012 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
1013 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
1014 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
1015 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
1016 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
1021 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
1023 VikCoord new_center;
1025 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
1026 struct LatLon ll, llold;
1027 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
1028 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
1029 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
1033 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
1034 struct UTM utm, utmold;
1035 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
1036 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
1037 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
1042 g_critical("Houston, we've had a problem.");
1046 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
1050 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
1053 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
1054 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
1055 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
1057 vw->modified = TRUE;
1063 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
1065 a_clipboard_copy_selected ( vw->viking_vlp );
1068 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
1070 a_clipboard_copy_selected ( vw->viking_vlp );
1071 menu_delete_layer_cb ( a, vw );
1074 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1076 if ( a_clipboard_paste ( vw->viking_vlp ) )
1079 vw->modified = TRUE;
1083 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1085 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1086 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1089 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1091 a_dialog_about(GTK_WINDOW(vw));
1094 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1096 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1098 vik_layers_panel_delete_selected ( vw->viking_vlp );
1099 vw->modified = TRUE;
1102 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1105 /***************************************
1106 ** tool management routines
1108 ***************************************/
1110 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1112 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1115 vt->active_tool = -1;
1117 if (!vw->viking_vvp) {
1118 g_critical("no viewport found.");
1124 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1126 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1127 vt->tools[vt->n_tools].ti = *vti;
1128 vt->tools[vt->n_tools].layer_type = layer_type;
1130 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1133 vt->tools[vt->n_tools].state = NULL;
1138 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1141 for (i=0; i<vt->n_tools; i++) {
1142 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1149 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1151 int tool = toolbox_get_tool(vt, tool_name);
1152 toolbox_tool_t *t = &vt->tools[tool];
1153 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1155 if (tool == vt->n_tools) {
1156 g_critical("trying to activate a non-existent tool...");
1159 /* is the tool already active? */
1160 if (vt->active_tool == tool) {
1164 if (vt->active_tool != -1) {
1165 if (vt->tools[vt->active_tool].ti.deactivate) {
1166 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1169 if (t->ti.activate) {
1170 t->ti.activate(vl, t->state);
1172 vt->active_tool = tool;
1175 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1177 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1178 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1179 gint ltype = vt->tools[vt->active_tool].layer_type;
1180 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1181 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1185 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1187 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1188 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1189 gint ltype = vt->tools[vt->active_tool].layer_type;
1190 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1191 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1192 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1196 static void toolbox_release (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.release ) {
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.release(vl, event, vt->tools[vt->active_tool].state);
1205 /** End tool management ************************************/
1207 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1209 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1212 /* this function gets called whenever a toolbar tool is clicked */
1213 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1215 /* White Magic, my friends ... White Magic... */
1216 int layer_id, tool_id;
1218 toolbox_activate(vw->vt, gtk_action_get_name(a));
1220 if (!strcmp(gtk_action_get_name(a), "Pan")) {
1221 vw->current_tool = TOOL_PAN;
1222 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_pan );
1224 else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1225 vw->current_tool = TOOL_ZOOM;
1226 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
1228 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1229 vw->current_tool = TOOL_RULER;
1230 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
1233 /* TODO: only enable tools from active layer */
1234 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1235 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1236 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1237 vw->current_tool = TOOL_LAYER;
1238 vw->tool_layer_id = layer_id;
1239 vw->tool_tool_id = tool_id;
1240 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
1248 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1253 g_free ( vw->filename );
1254 if ( filename == NULL )
1256 vw->filename = NULL;
1257 file = _("Untitled");
1261 vw->filename = g_strdup(filename);
1262 file = a_file_basename ( filename );
1264 title = g_strdup_printf( "%s - Viking", file );
1265 gtk_window_set_title ( GTK_WINDOW(vw), title );
1269 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1271 GtkWidget *mode_button;
1274 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1275 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1276 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1277 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1278 default: buttonname = "/ui/MainMenu/View/ModeKH";
1280 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1281 g_assert ( mode_button );
1285 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1287 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1290 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1294 GtkWidget *mode_button;
1295 if ( change_filename )
1296 window_set_filename ( vw, filename );
1297 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1298 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. */
1299 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1300 vw->only_updating_coord_mode_ui = FALSE;
1302 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1304 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1305 g_assert ( mode_button );
1306 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1308 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1309 g_assert ( mode_button );
1310 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1312 default: draw_update ( vw );
1315 static void load_file ( GtkAction *a, VikWindow *vw )
1318 if (!strcmp(gtk_action_get_name(a), "Open")) {
1321 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1325 g_critical("Houston, we've had a problem.");
1329 if ( ! vw->open_dia )
1331 vw->open_dia = gtk_file_selection_new ( _("Please select a GPS data file to open. ") );
1332 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1333 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1334 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1336 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1338 gtk_widget_hide ( vw->open_dia );
1339 #ifdef VIKING_PROMPT_IF_MODIFIED
1340 if ( (vw->modified || vw->filename) && newwindow )
1342 if ( vw->filename && newwindow )
1344 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1346 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1347 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1349 vik_window_open_file ( vw, *files, change_fn );
1355 gtk_widget_hide ( vw->open_dia );
1358 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1360 gboolean rv = FALSE;
1362 if ( ! vw->save_dia )
1364 vw->save_dia = gtk_file_selection_new ( _("Save as Viking File.") );
1365 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1366 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1369 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1371 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1372 if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(vw->save_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
1374 window_set_filename ( vw, fn );
1375 rv = window_save ( vw );
1376 vw->modified = FALSE;
1380 gtk_widget_hide ( vw->save_dia );
1384 static gboolean window_save ( VikWindow *vw )
1386 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1390 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1395 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1397 if ( ! vw->filename )
1398 return save_file_as ( NULL, vw );
1401 vw->modified = FALSE;
1402 return window_save ( vw );
1406 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1408 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1411 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1413 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1416 #ifdef VIK_CONFIG_GEOCACHES
1417 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1419 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1423 static void goto_address( GtkAction *a, VikWindow *vw)
1425 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1428 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1430 a_preferences_show_window ( GTK_WINDOW(vw) );
1433 static void clear_cb ( GtkAction *a, VikWindow *vw )
1435 vik_layers_panel_clear ( vw->viking_vlp );
1436 window_set_filename ( vw, NULL );
1440 static void window_close ( GtkAction *a, VikWindow *vw )
1442 if ( ! delete_event ( vw ) )
1443 gtk_widget_destroy ( GTK_WIDGET(vw) );
1446 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1448 if (save_file( NULL, vw)) {
1449 window_close( NULL, vw);
1456 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1458 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1459 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1461 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1462 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1467 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1469 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1470 GdkPixbuf *pixbuf_to_save;
1471 gdouble old_xmpp, old_ympp;
1472 GError *error = NULL;
1474 /* backup old zoom & set new */
1475 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1476 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1477 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1479 /* reset width and height: */
1480 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1482 /* draw all layers */
1485 /* save buffer as file. */
1486 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);
1487 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1490 g_warning("Unable to write to file %s: %s", fn, error->message );
1491 g_error_free (error);
1493 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1495 /* pretend like nothing happened ;) */
1496 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1497 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1498 vik_viewport_configure ( vw->viking_vvp );
1502 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 )
1504 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1505 gchar *name_of_file = g_malloc ( size );
1507 struct UTM utm_orig, utm;
1509 /* *** copied from above *** */
1510 GdkPixbuf *pixbuf_to_save;
1511 gdouble old_xmpp, old_ympp;
1512 GError *error = NULL;
1514 /* backup old zoom & set new */
1515 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1516 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1517 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1519 /* reset width and height: do this only once for all images (same size) */
1520 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1521 /* *** end copy from above *** */
1523 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1527 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1529 for ( y = 1; y <= tiles_h; y++ )
1531 for ( x = 1; x <= tiles_w; x++ )
1533 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1535 if ( tiles_w & 0x1 )
1536 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1538 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1539 if ( tiles_h & 0x1 ) /* odd */
1540 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1542 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1544 /* move to correct place. */
1545 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1549 /* save buffer as file. */
1550 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);
1551 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1554 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1555 g_error_free (error);
1558 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1562 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1563 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1564 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1565 vik_viewport_configure ( vw->viking_vvp );
1568 g_free ( name_of_file );
1571 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1573 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1574 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1575 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1576 gdouble width_min, width_max, height_min, height_max;
1579 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1580 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1582 /* TODO: support for xzoom and yzoom values */
1583 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1584 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1586 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1587 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1589 gtk_spin_button_set_value ( width_spin, width );
1590 gtk_spin_button_set_value ( height_spin, height );
1593 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1595 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1596 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1599 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1600 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1601 if (pass_along[4]) /* save many images; find TOTAL area covered */
1603 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1604 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1606 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1607 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1608 g_free ( label_text );
1611 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1613 /* todo: default for answers inside VikWindow or static (thruout instance) */
1614 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1615 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1617 GTK_RESPONSE_REJECT,
1619 GTK_RESPONSE_ACCEPT,
1621 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1622 GtkWidget *png_radio, *jpeg_radio;
1623 GtkWidget *current_window_button;
1624 gpointer current_window_pass_along[7];
1625 GtkWidget *zoom_label, *zoom_spin;
1626 GtkWidget *total_size_label;
1628 /* only used if (!one_image_only) */
1629 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1632 width_label = gtk_label_new ( _("Width (pixels):") );
1633 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1634 height_label = gtk_label_new ( _("Height (pixels):") );
1635 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1637 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1638 /* TODO: separate xzoom and yzoom factors */
1639 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);
1641 total_size_label = gtk_label_new ( NULL );
1643 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1644 current_window_pass_along [0] = vw;
1645 current_window_pass_along [1] = width_spin;
1646 current_window_pass_along [2] = height_spin;
1647 current_window_pass_along [3] = zoom_spin;
1648 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1649 current_window_pass_along [5] = NULL;
1650 current_window_pass_along [6] = total_size_label;
1651 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1653 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1654 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1656 if ( ! vw->draw_image_save_as_png )
1657 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1659 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1660 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1661 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1662 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1663 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1664 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1665 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1666 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1667 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1669 if ( ! one_image_only )
1671 GtkWidget *tiles_width_label, *tiles_height_label;
1674 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1675 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1676 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1677 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1678 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1679 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1680 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1681 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1683 current_window_pass_along [4] = tiles_width_spin;
1684 current_window_pass_along [5] = tiles_height_spin;
1685 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1686 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1688 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1689 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1690 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1691 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1693 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1695 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1697 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1699 gtk_widget_hide ( GTK_WIDGET(dialog) );
1700 if ( one_image_only )
1701 save_image_file ( vw, fn,
1702 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1703 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1704 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1705 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1707 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1708 save_image_dir ( vw, fn,
1709 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1710 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1711 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1712 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1713 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1714 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1716 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1719 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1723 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1726 if (!vw->save_img_dia) {
1727 vw->save_img_dia = gtk_file_selection_new ( _("Save Image") );
1728 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1729 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1732 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1734 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1735 if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(vw->save_img_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
1737 draw_to_image_file ( vw, fn, TRUE );
1741 gtk_widget_hide ( vw->save_img_dia );
1744 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1747 if (!vw->save_img_dir_dia) {
1748 vw->save_img_dir_dia = gtk_file_selection_new ( _("Choose a name for a new directory to hold images"));
1749 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1750 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1753 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1755 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1756 if ( access ( fn, F_OK ) == 0 )
1757 a_dialog_info_msg_extra ( GTK_WINDOW(vw->save_img_dir_dia), _("The file %s exists. Please choose a name for a new directory to hold images in that does not exist."), a_file_basename(fn) );
1760 draw_to_image_file ( vw, fn, FALSE );
1764 gtk_widget_hide ( vw->save_img_dir_dia );
1767 #if GTK_CHECK_VERSION(2,10,0)
1768 static void print_cb ( GtkAction *a, VikWindow *vw )
1770 a_print(vw, vw->viking_vvp);
1774 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1775 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1777 VikViewportDrawMode drawmode;
1778 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1779 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1781 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1782 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1784 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1785 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1787 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1788 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1790 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1791 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1794 g_critical("Houston, we've had a problem.");
1798 if ( !vw->only_updating_coord_mode_ui )
1800 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1801 if ( olddrawmode != drawmode )
1803 /* this takes care of coord mode too */
1804 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1805 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1806 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1807 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1808 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1815 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1817 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1818 g_assert(check_box);
1819 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1820 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1824 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1826 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1827 g_assert(check_box);
1828 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1829 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1833 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1835 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1836 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1837 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1838 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1839 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1841 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1842 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1846 gtk_widget_destroy ( colorsd );
1851 /***********************************************************************************************
1853 ***********************************************************************************************/
1855 static GtkActionEntry entries[] = {
1856 { "File", NULL, N_("_File"), 0, 0, 0 },
1857 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
1858 { "View", NULL, N_("_View"), 0, 0, 0 },
1859 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
1860 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
1861 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
1862 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
1863 { "Help", NULL, N_("_Help"), 0, 0, 0 },
1865 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
1866 { "Open", GTK_STOCK_OPEN, N_("_Open"), "<control>O", N_("Open a file"), (GCallback)load_file },
1867 { "Append", GTK_STOCK_ADD, N_("A_ppend File"), NULL, N_("Append data from a different file"), (GCallback)load_file },
1868 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
1869 { "AcquireGPS", NULL, N_("From _GPS"), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
1870 { "AcquireGoogle", NULL, N_("Google _Directions"), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1871 #ifdef VIK_CONFIG_GEOCACHES
1872 { "AcquireGC", NULL, N_("Geo_caches"), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1874 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
1875 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As"), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
1876 { "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 },
1877 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images"), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
1879 #if GTK_CHECK_VERSION(2,10,0)
1880 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
1883 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
1884 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
1886 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, N_("Go To Google Maps location"), NULL, N_("Go to address/place using Google Maps search"), (GCallback)goto_address },
1887 { "GotoLL", GTK_STOCK_QUIT, N_("_Go to Lat\\/Lon..."), NULL, N_("Go to arbitrary lat\\/lon coordinate"), (GCallback)draw_goto_cb },
1888 { "GotoUTM", GTK_STOCK_QUIT, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
1889 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Background Color..."), NULL, NULL, (GCallback)set_bg_color },
1890 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
1891 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
1892 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To"), "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1893 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
1894 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
1895 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
1896 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
1897 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
1898 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
1899 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
1900 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
1901 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
1902 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
1903 { "PanNorth", NULL, N_("Pan North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
1904 { "PanEast", NULL, N_("Pan East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
1905 { "PanSouth", NULL, N_("Pan South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
1906 { "PanWest", NULL, N_("Pan West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
1907 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
1909 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
1910 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
1911 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
1912 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
1913 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
1914 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences..."), NULL, NULL, (GCallback)preferences_cb },
1915 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
1917 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
1921 static GtkRadioActionEntry mode_entries[] = {
1922 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
1923 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
1924 { "ModeGoogle", NULL, N_("_Old Google Mode"), "<control>o", NULL, 2 },
1925 { "ModeKH", NULL, N_("Old _KH Mode"), "<control>k", NULL, 3 },
1926 { "ModeMercator", NULL, N_("_Google Mode"), "<control>g", NULL, 4 }
1929 static GtkRadioActionEntry tool_entries[] = {
1930 { "Pan", "vik-icon-pan", N_("_Pan"), "<control><shift>P", N_("Pan Tool"), 0 },
1931 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 1 },
1932 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 2 }
1935 static GtkToggleActionEntry toggle_entries[] = {
1936 { "ShowScale", NULL, N_("Show Scale"), NULL, NULL, (GCallback)set_draw_scale, TRUE },
1937 { "ShowCenterMark", NULL, N_("Show Center Mark"), NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1938 { "FullScreen", NULL, N_("Full Screen"), "F11", NULL, (GCallback)full_screen_cb, FALSE },
1941 #include "menu.xml.h"
1942 static void window_create_ui( VikWindow *window )
1945 GtkActionGroup *action_group;
1946 GtkAccelGroup *accel_group;
1949 GtkIconFactory *icon_factory;
1950 GtkIconSet *icon_set;
1951 GtkRadioActionEntry *tools = NULL, *radio;
1954 uim = gtk_ui_manager_new ();
1957 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1958 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1959 toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
1962 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1963 g_error_free (error);
1967 action_group = gtk_action_group_new ("MenuActions");
1968 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
1969 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1970 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1971 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1973 icon_factory = gtk_icon_factory_new ();
1974 gtk_icon_factory_add_default (icon_factory);
1976 register_vik_icons(icon_factory);
1979 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1980 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1981 radio = &tools[ntools];
1983 *radio = tool_entries[i];
1984 radio->value = ntools;
1987 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1988 GtkActionEntry action;
1989 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1990 vik_layer_get_interface(i)->name,
1991 vik_layer_get_interface(i)->name,
1992 GTK_UI_MANAGER_MENUITEM, FALSE);
1994 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1995 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1996 gtk_icon_set_unref (icon_set);
1998 action.name = vik_layer_get_interface(i)->name;
1999 action.stock_id = vik_layer_get_interface(i)->name;
2000 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
2001 action.accelerator = NULL;
2002 action.tooltip = NULL;
2003 action.callback = (GCallback)menu_addlayer_cb;
2004 gtk_action_group_add_actions(action_group, &action, 1, window);
2006 if ( vik_layer_get_interface(i)->tools_count ) {
2007 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2008 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2011 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2012 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2013 radio = &tools[ntools];
2016 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
2017 _(vik_layer_get_interface(i)->tools[j].name),
2018 vik_layer_get_interface(i)->tools[j].name,
2019 GTK_UI_MANAGER_MENUITEM, FALSE);
2020 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
2021 _(vik_layer_get_interface(i)->tools[j].name),
2022 vik_layer_get_interface(i)->tools[j].name,
2023 GTK_UI_MANAGER_TOOLITEM, FALSE);
2025 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
2027 radio->name = vik_layer_get_interface(i)->tools[j].name;
2028 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
2029 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
2030 radio->accelerator = NULL;
2031 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
2032 radio->value = ntools;
2035 g_object_unref (icon_factory);
2037 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2040 gtk_ui_manager_insert_action_group (uim, action_group, 0);
2042 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2043 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2044 GtkAction *action = gtk_action_group_get_action(action_group,
2045 vik_layer_get_interface(i)->tools[j].name);
2046 g_object_set(action, "sensitive", FALSE, NULL);
2049 window->action_group = action_group;
2051 accel_group = gtk_ui_manager_get_accel_group (uim);
2052 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2053 gtk_ui_manager_ensure_update (uim);
2058 #include "icons/icons.h"
2060 const GdkPixdata *data;
2063 { &addtr_18, "Create Track" },
2064 { &begintr_18, "Begin Track" },
2065 { &edtr_18, "Edit Trackpoint" },
2066 { &addwp_18, "Create Waypoint" },
2067 { &edwp_18, "Edit Waypoint" },
2068 { &iscissors_18, "Magic Scissors" },
2069 { &mover_22, "vik-icon-pan" },
2070 { &zoom_18, "vik-icon-zoom" },
2071 { &ruler_18, "vik-icon-ruler" },
2072 { &geozoom_18, "Georef Zoom Tool" },
2073 { &geomove_18, "Georef Move Map" },
2074 { &mapdl_18, "Maps Download" },
2075 { &demdl_18, "DEM Download/Import" },
2076 { &showpic_18, "Show Picture" },
2079 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2082 register_vik_icons (GtkIconFactory *icon_factory)
2084 GtkIconSet *icon_set;
2087 for (i = 0; i < n_stock_icons; i++) {
2088 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2089 stock_icons[i].data, FALSE, NULL ));
2090 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2091 gtk_icon_set_unref (icon_set);
2095 void vik_window_cursors_init()
2097 GdkPixbuf *cursor_pixbuf;
2098 GError *cursor_load_err;
2100 vw_cursor_pan = gdk_cursor_new(GDK_FLEUR);
2102 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
2103 vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2105 g_object_unref ( G_OBJECT(cursor_pixbuf) );
2107 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
2108 vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2110 g_object_unref ( G_OBJECT(cursor_pixbuf) );
2113 void vik_window_cursors_uninit()
2115 gdk_cursor_unref ( vw_cursor_pan );
2116 gdk_cursor_unref ( vw_cursor_zoom );
2117 gdk_cursor_unref ( vw_cursor_ruler );