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;
173 VW_OPENWINDOW_SIGNAL,
177 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
179 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Zoom"), N_("Ruler"), N_("Pan") };
181 GdkCursor *vw_cursor_zoom = NULL;
182 GdkCursor *vw_cursor_ruler = NULL;
184 GType vik_window_get_type (void)
186 static GType vw_type = 0;
190 static const GTypeInfo vw_info =
192 sizeof (VikWindowClass),
193 NULL, /* base_init */
194 NULL, /* base_finalize */
195 (GClassInitFunc) window_class_init, /* class_init */
196 NULL, /* class_finalize */
197 NULL, /* class_data */
200 (GInstanceInitFunc) window_init,
202 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
208 VikViewport * vik_window_viewport(VikWindow *vw)
210 return(vw->viking_vvp);
213 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
215 int i, j, tool_count;
216 VikLayerInterface *layer_interface;
218 if (!vw->action_group) return;
220 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
222 layer_interface = vik_layer_get_interface(i);
223 tool_count = layer_interface->tools_count;
225 for (j = 0; j < tool_count; j++) {
226 action = gtk_action_group_get_action(vw->action_group,
227 layer_interface->tools[j].name);
228 g_object_set(action, "sensitive", i == vl->type, NULL);
233 static void window_finalize ( GObject *gob )
235 VikWindow *vw = VIK_WINDOW(gob);
236 g_return_if_fail ( vw != NULL );
238 a_background_remove_status ( vw->viking_vs );
240 G_OBJECT_CLASS(parent_class)->finalize(gob);
244 static void window_class_init ( VikWindowClass *klass )
247 GObjectClass *object_class;
249 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);
250 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);
252 object_class = G_OBJECT_CLASS (klass);
254 object_class->finalize = window_finalize;
256 parent_class = g_type_class_peek_parent (klass);
260 static void window_init ( VikWindow *vw )
262 GtkWidget *main_vbox;
265 vw->action_group = NULL;
267 vw->viking_vvp = vik_viewport_new();
268 vw->viking_vlp = vik_layers_panel_new();
269 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
270 vw->viking_vs = vik_statusbar_new();
272 vw->vt = toolbox_create(vw);
273 window_create_ui(vw);
274 window_set_filename (vw, NULL);
276 toolbox_activate(vw->vt, "Zoom");
279 vw->item_factory = NULL;
281 vw->modified = FALSE;
282 vw->only_updating_coord_mode_ui = FALSE;
284 vw->pan_x = vw->pan_y = -1;
285 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
286 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
287 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
289 main_vbox = gtk_vbox_new(FALSE, 1);
290 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
292 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
293 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
294 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
295 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
298 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
300 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
301 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
302 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 );
303 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
304 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
305 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
306 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
307 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
309 g_signal_connect_swapped (G_OBJECT (vw->viking_vvp), "key_press_event", G_CALLBACK (key_press_event), vw);
311 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
313 hpaned = gtk_hpaned_new ();
314 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
315 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
317 /* This packs the button into the window (a gtk container). */
318 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
320 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
322 a_background_add_status(vw->viking_vs);
326 vw->save_img_dia = NULL;
327 vw->save_img_dir_dia = NULL;
330 VikWindow *vik_window_new ()
332 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
335 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data )
337 VikLayer *vl = vik_layers_panel_get_selected ( vw->viking_vlp );
338 if (vl && vw->vt->active_tool != -1 && vw->vt->tools[vw->vt->active_tool].ti.key_press ) {
339 gint ltype = vw->vt->tools[vw->vt->active_tool].layer_type;
340 if ( vl && ltype == vl->type )
341 return vw->vt->tools[vw->vt->active_tool].ti.key_press(vl, event, vw->vt->tools[vw->vt->active_tool].state);
343 return FALSE; /* don't handle the keypress */
346 static gboolean delete_event( VikWindow *vw )
348 #ifdef VIKING_PROMPT_IF_MODIFIED
355 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
356 _("Do you want to save the changes you made to the document \"%s\"?\n"
358 "Your changes will be lost if you don't save them."),
359 vw->filename ? a_file_basename ( vw->filename ) : _("Untitled") ) );
360 gtk_dialog_add_buttons ( dia, _("Don't Save"), GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
361 switch ( gtk_dialog_run ( dia ) )
363 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
364 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
365 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
372 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
374 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
377 static void draw_update ( VikWindow *vw )
383 static void draw_sync ( VikWindow *vw )
385 vik_viewport_sync(vw->viking_vvp);
387 /* other things may be necc here later. */
390 static void draw_status ( VikWindow *vw )
392 static gchar zoom_level[22];
393 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") );
394 if ( vw->current_tool == TOOL_LAYER )
395 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
397 vik_statusbar_set_message ( vw->viking_vs, 0, _(tool_names[vw->current_tool]) );
399 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
402 void vik_window_set_redraw_trigger(VikLayer *vl)
404 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
408 static void window_configure_event ( VikWindow *vw )
411 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
414 static void draw_redraw ( VikWindow *vw )
416 VikCoord old_center = vw->trigger_center;
417 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
418 VikLayer *new_trigger = vw->trigger;
420 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
423 ; /* do nothing -- have to redraw everything. */
424 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
425 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
427 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
430 vik_viewport_clear ( vw->viking_vvp);
431 vik_layers_panel_draw_all ( vw->viking_vlp );
432 vik_viewport_draw_scale ( vw->viking_vvp );
433 vik_viewport_draw_centermark ( vw->viking_vvp );
435 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
438 gboolean draw_buf_done = TRUE;
440 static gboolean draw_buf(gpointer data)
442 gpointer *pass_along = data;
444 gdk_draw_drawable (pass_along[0], pass_along[1],
445 pass_along[2], 0, 0, 0, 0, -1, -1);
446 draw_buf_done = TRUE;
452 /* Mouse event handlers ************************************************************************/
454 static void draw_click (VikWindow *vw, GdkEventButton *event)
456 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
458 /* middle button pressed. we reserve all middle button and scroll events
459 * for panning and zooming; tools only get left/right/movement
461 if ( event->button == 2) {
462 /* set panning origin */
463 vw->pan_x = (gint) event->x;
464 vw->pan_y = (gint) event->y;
467 toolbox_click(vw->vt, event);
471 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
473 static VikCoord coord;
474 static struct UTM utm;
475 static struct LatLon ll;
476 static char pointer_buf[36];
479 VikDemInterpol interpol_method;
481 toolbox_move(vw->vt, (GdkEventButton *)event);
483 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
484 vik_coord_to_utm ( &coord, &utm );
485 a_coords_utm_to_latlon ( &utm, &ll );
486 /* Change interpolate method according to scale */
487 zoom = vik_viewport_get_zoom(vw->viking_vvp);
489 interpol_method = VIK_DEM_INTERPOL_NONE;
490 else if (zoom >= 1.0)
491 interpol_method = VIK_DEM_INTERPOL_SIMPLE;
493 interpol_method = VIK_DEM_INTERPOL_BEST;
494 if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION)
495 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f %dm"), ll.lat, ll.lon, alt );
497 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f"), ll.lat, ll.lon );
498 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
500 if ( vw->pan_x != -1 ) {
501 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
505 static void draw_release ( VikWindow *vw, GdkEventButton *event )
507 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
509 if ( event->button == 2 ) { /* move / pan */
510 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
511 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
513 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
514 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
516 vw->pan_x = vw->pan_y = -1;
519 toolbox_release(vw->vt, event);
523 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
525 if ( event->state == GDK_CONTROL_MASK ) {
526 /* control == pan up & down */
527 if ( event->direction == GDK_SCROLL_UP )
528 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
530 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 );
531 } else if ( event->state == GDK_SHIFT_MASK ) {
532 /* control-shift == pan left & right */
533 if ( event->direction == GDK_SCROLL_UP )
534 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
536 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 );
537 } else if ( event->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
538 if ( event->direction == GDK_SCROLL_UP ) {
539 /* make sure mouse is still over the same point on the map when we zoom */
542 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
543 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
544 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
545 vik_viewport_zoom_in (vw->viking_vvp);
546 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
547 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
548 center_y + (y - event->y) );
553 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
554 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
555 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
556 vik_viewport_zoom_out(vw->viking_vvp);
557 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
558 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
559 center_y + (y - event->y) );
562 if ( event->direction == GDK_SCROLL_UP )
563 vik_viewport_zoom_in (vw->viking_vvp);
565 vik_viewport_zoom_out (vw->viking_vvp);
573 /********************************************************************************
575 ********************************************************************************/
576 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
578 PangoFontDescription *pfd;
581 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
582 GdkGC *thickgc = gdk_gc_new(d);
584 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
585 gdouble dx = (x2-x1)/len*10;
586 gdouble dy = (y2-y1)/len*10;
587 gdouble c = cos(15.0 * M_PI/180.0);
588 gdouble s = sin(15.0 * M_PI/180.0);
590 gdouble baseangle = 0;
593 /* draw line with arrow ends */
595 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
596 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
597 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
600 a_viewport_clip_line(&x1, &y1, &x2, &y2);
601 gdk_draw_line(d, gc, x1, y1, x2, y2);
603 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
604 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
605 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
606 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
607 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
608 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
613 angle = atan2(dy, dx) + M_PI_2;
615 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
621 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
622 vik_coord_to_latlon ( &test, &ll );
623 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
624 a_coords_latlon_to_utm ( &ll, &u );
625 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
626 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
628 baseangle = M_PI - atan2(tx-x1, ty-y1);
639 gdk_gc_copy(thickgc, gc);
640 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
641 gdk_color_parse("#2255cc", &color);
642 gdk_gc_set_rgb_fg_color(thickgc, &color);
644 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);
647 gdk_gc_copy(thickgc, gc);
648 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
649 for (i=0; i<180; i++) {
650 c = cos(i*M_PI/90.0 + baseangle);
651 s = sin(i*M_PI/90.0 + baseangle);
654 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
656 gdouble ticksize = 2*CW;
657 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
661 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
662 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
663 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
664 c = (CR+CW*2)*cos(baseangle);
665 s = (CR+CW*2)*sin(baseangle);
666 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
667 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
670 #define LABEL(x, y, w, h) { \
671 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
672 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
673 gdk_draw_layout(d, gc, (x), (y), pl); }
678 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
680 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
681 pango_layout_set_font_description (pl, pfd);
682 pango_font_description_free (pfd);
684 pango_layout_set_text(pl, "N", -1);
685 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
687 /* draw label with distance */
688 if (distance >= 1000 && distance < 100000) {
689 g_sprintf(str, "%3.2f km", distance/1000.0);
690 } else if (distance < 1000) {
691 g_sprintf(str, "%d m", (int)distance);
693 g_sprintf(str, "%d km", (int)distance/1000);
695 pango_layout_set_text(pl, str, -1);
697 pango_layout_get_pixel_size ( pl, &wd, &hd );
700 yd = (y1+y2)/2 - hd/2 - dx;
703 yd = (y1+y2)/2 - hd/2 + dx;
706 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
711 LABEL(xd, yd, wd, hd);
713 /* draw label with bearing */
714 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
715 pango_layout_set_text(pl, str, -1);
716 pango_layout_get_pixel_size ( pl, &wb, &hb );
717 xb = x1 + CR*cos(angle-M_PI_2);
718 yb = y1 + CR*sin(angle-M_PI_2);
720 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
726 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
727 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
731 LABEL(xb, yb, wb, hb);
735 g_object_unref ( G_OBJECT ( pl ) );
736 g_object_unref ( G_OBJECT ( labgc ) );
737 g_object_unref ( G_OBJECT ( thickgc ) );
743 gboolean has_oldcoord;
745 } ruler_tool_state_t;
747 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
749 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
752 s->has_oldcoord = FALSE;
756 static void ruler_destroy (ruler_tool_state_t *s)
761 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
766 if ( event->button == 1 ) {
767 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
768 vik_coord_to_latlon ( &coord, &ll );
769 if ( s->has_oldcoord ) {
770 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
771 s->has_oldcoord = FALSE;
774 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
775 s->has_oldcoord = TRUE;
778 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
784 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
785 draw_update ( s->vw );
787 return VIK_LAYER_TOOL_ACK;
790 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
792 VikViewport *vvp = s->vvp;
793 VikWindow *vw = s->vw;
799 if ( s->has_oldcoord ) {
800 int oldx, oldy, w1, h1, w2, h2;
801 static GdkPixmap *buf = NULL;
802 w1 = vik_viewport_get_width(vvp);
803 h1 = vik_viewport_get_height(vvp);
805 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
807 gdk_drawable_get_size(buf, &w2, &h2);
808 if (w1 != w2 || h1 != h2) {
809 g_object_unref ( G_OBJECT ( buf ) );
810 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
813 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
814 vik_coord_to_latlon ( &coord, &ll );
815 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
817 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
818 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
819 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
821 static gpointer pass_along[3];
822 pass_along[0] = GTK_WIDGET(vvp)->window;
823 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
825 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
826 draw_buf_done = FALSE;
829 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
830 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
833 return VIK_LAYER_TOOL_ACK;
836 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
838 return VIK_LAYER_TOOL_ACK;
841 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
843 draw_update ( s->vw );
846 static VikToolInterface ruler_tool =
848 (VikToolConstructorFunc) ruler_create,
849 (VikToolDestructorFunc) ruler_destroy,
850 (VikToolActivationFunc) NULL,
851 (VikToolActivationFunc) ruler_deactivate,
852 (VikToolMouseFunc) ruler_click,
853 (VikToolMouseFunc) ruler_move,
854 (VikToolMouseFunc) ruler_release };
855 /*** end ruler code ********************************************************/
859 /********************************************************************************
861 ********************************************************************************/
862 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
867 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
870 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
871 if ( event->button == 1 )
872 vik_viewport_zoom_in (vw->viking_vvp);
873 else if ( event->button == 3 )
874 vik_viewport_zoom_out (vw->viking_vvp);
876 return VIK_LAYER_TOOL_ACK;
879 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
881 return VIK_LAYER_TOOL_ACK;
884 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
886 return VIK_LAYER_TOOL_ACK;
889 static VikToolInterface zoom_tool =
891 (VikToolConstructorFunc) zoomtool_create,
892 (VikToolDestructorFunc) NULL,
893 (VikToolActivationFunc) NULL,
894 (VikToolActivationFunc) NULL,
895 (VikToolMouseFunc) zoomtool_click,
896 (VikToolMouseFunc) zoomtool_move,
897 (VikToolMouseFunc) zoomtool_release };
898 /*** end ruler code ********************************************************/
900 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
902 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
903 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
904 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
905 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
906 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
907 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
908 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
909 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
914 static void full_screen_cb ( GtkAction *a, VikWindow *vw )
916 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
918 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
920 gtk_window_fullscreen ( GTK_WINDOW(vw) );
922 gtk_window_unfullscreen ( GTK_WINDOW(vw) );
925 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
929 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
932 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
935 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
938 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
942 gchar *s = (gchar *)gtk_action_get_name(a);
948 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
949 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
950 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
951 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
952 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
957 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
961 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
962 struct LatLon ll, llold;
963 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
964 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
965 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
969 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
970 struct UTM utm, utmold;
971 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
972 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
973 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
978 g_critical("Houston we have a problem\n");
982 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
986 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
989 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
990 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
991 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
999 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
1001 a_clipboard_copy_selected ( vw->viking_vlp );
1004 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
1006 a_clipboard_copy_selected ( vw->viking_vlp );
1007 menu_delete_layer_cb ( a, vw );
1010 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1012 if ( a_clipboard_paste ( vw->viking_vlp ) )
1015 vw->modified = TRUE;
1019 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1021 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1022 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1025 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1027 a_dialog_about(GTK_WINDOW(vw));
1030 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1032 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1034 vik_layers_panel_delete_selected ( vw->viking_vlp );
1035 vw->modified = TRUE;
1038 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1041 /***************************************
1042 ** tool management routines
1044 ***************************************/
1046 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1048 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1051 vt->active_tool = -1;
1053 if (!vw->viking_vvp) {
1054 g_critical("no viewport found.");
1060 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1062 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1063 vt->tools[vt->n_tools].ti = *vti;
1064 vt->tools[vt->n_tools].layer_type = layer_type;
1066 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1069 vt->tools[vt->n_tools].state = NULL;
1074 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1077 for (i=0; i<vt->n_tools; i++) {
1078 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1085 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1087 int tool = toolbox_get_tool(vt, tool_name);
1088 toolbox_tool_t *t = &vt->tools[tool];
1089 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1091 if (tool == vt->n_tools) {
1092 g_critical("trying to activate a non-existent tool...");
1095 /* is the tool already active? */
1096 if (vt->active_tool == tool) {
1100 if (vt->active_tool != -1) {
1101 if (vt->tools[vt->active_tool].ti.deactivate) {
1102 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1105 if (t->ti.activate) {
1106 t->ti.activate(vl, t->state);
1108 vt->active_tool = tool;
1111 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1113 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1114 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1115 gint ltype = vt->tools[vt->active_tool].layer_type;
1116 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1117 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1121 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1123 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1124 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1125 gint ltype = vt->tools[vt->active_tool].layer_type;
1126 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1127 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1128 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1132 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1134 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1135 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1136 gint ltype = vt->tools[vt->active_tool].layer_type;
1137 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1138 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1141 /** End tool management ************************************/
1143 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1145 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1148 /* this function gets called whenever a toolbar tool is clicked */
1149 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1151 /* White Magic, my friends ... White Magic... */
1152 int layer_id, tool_id;
1154 toolbox_activate(vw->vt, gtk_action_get_name(a));
1156 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1157 vw->current_tool = TOOL_ZOOM;
1158 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
1160 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1161 vw->current_tool = TOOL_RULER;
1162 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
1165 /* TODO: only enable tools from active layer */
1166 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1167 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1168 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1169 vw->current_tool = TOOL_LAYER;
1170 vw->tool_layer_id = layer_id;
1171 vw->tool_tool_id = tool_id;
1172 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
1180 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1185 g_free ( vw->filename );
1186 if ( filename == NULL )
1188 vw->filename = NULL;
1189 file = _("Untitled");
1193 vw->filename = g_strdup(filename);
1194 file = a_file_basename ( filename );
1196 title = g_strdup_printf( "%s - Viking", file );
1197 gtk_window_set_title ( GTK_WINDOW(vw), title );
1201 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1203 GtkWidget *mode_button;
1206 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1207 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1208 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1209 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1210 default: buttonname = "/ui/MainMenu/View/ModeKH";
1212 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1213 g_assert ( mode_button );
1217 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1219 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1222 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1226 GtkWidget *mode_button;
1227 if ( change_filename )
1228 window_set_filename ( vw, filename );
1229 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1230 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. */
1231 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1232 vw->only_updating_coord_mode_ui = FALSE;
1234 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1236 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1237 g_assert ( mode_button );
1238 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1240 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1241 g_assert ( mode_button );
1242 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1244 default: draw_update ( vw );
1247 static void load_file ( GtkAction *a, VikWindow *vw )
1250 if (!strcmp(gtk_action_get_name(a), "Open")) {
1253 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1257 g_critical("Houston we got a problem\n");
1261 if ( ! vw->open_dia )
1263 vw->open_dia = gtk_file_selection_new ( _("Please select a GPS data file to open. ") );
1264 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1265 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1266 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1268 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1270 gtk_widget_hide ( vw->open_dia );
1271 #ifdef VIKING_PROMPT_IF_MODIFIED
1272 if ( (vw->modified || vw->filename) && newwindow )
1274 if ( vw->filename && newwindow )
1276 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1278 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1279 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1281 vik_window_open_file ( vw, *files, change_fn );
1287 gtk_widget_hide ( vw->open_dia );
1290 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1292 gboolean rv = FALSE;
1294 if ( ! vw->save_dia )
1296 vw->save_dia = gtk_file_selection_new ( _("Save as Viking File.") );
1297 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1298 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1301 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1303 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1304 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 ) ) )
1306 window_set_filename ( vw, fn );
1307 rv = window_save ( vw );
1308 vw->modified = FALSE;
1312 gtk_widget_hide ( vw->save_dia );
1316 static gboolean window_save ( VikWindow *vw )
1318 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1322 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1327 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1329 if ( ! vw->filename )
1330 return save_file_as ( NULL, vw );
1333 vw->modified = FALSE;
1334 return window_save ( vw );
1338 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1340 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1343 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1345 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1348 #ifdef VIK_CONFIG_GEOCACHES
1349 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1351 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1355 static void goto_address( GtkAction *a, VikWindow *vw)
1357 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1360 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1362 a_preferences_show_window ( GTK_WINDOW(vw) );
1365 static void clear_cb ( GtkAction *a, VikWindow *vw )
1367 vik_layers_panel_clear ( vw->viking_vlp );
1368 window_set_filename ( vw, NULL );
1372 static void window_close ( GtkAction *a, VikWindow *vw )
1374 if ( ! delete_event ( vw ) )
1375 gtk_widget_destroy ( GTK_WIDGET(vw) );
1378 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1380 if (save_file( NULL, vw)) {
1381 window_close( NULL, vw);
1388 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1390 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1391 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1393 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1394 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1399 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1401 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1402 GdkPixbuf *pixbuf_to_save;
1403 gdouble old_xmpp, old_ympp;
1404 GError *error = NULL;
1406 /* backup old zoom & set new */
1407 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1408 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1409 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1411 /* reset width and height: */
1412 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1414 /* draw all layers */
1417 /* save buffer as file. */
1418 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);
1419 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1422 g_warning("Unable to write to file %s: %s", fn, error->message );
1423 g_error_free (error);
1425 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1427 /* pretend like nothing happened ;) */
1428 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1429 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1430 vik_viewport_configure ( vw->viking_vvp );
1434 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 )
1436 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1437 gchar *name_of_file = g_malloc ( size );
1439 struct UTM utm_orig, utm;
1441 /* *** copied from above *** */
1442 GdkPixbuf *pixbuf_to_save;
1443 gdouble old_xmpp, old_ympp;
1444 GError *error = NULL;
1446 /* backup old zoom & set new */
1447 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1448 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1449 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1451 /* reset width and height: do this only once for all images (same size) */
1452 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1453 /* *** end copy from above *** */
1455 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1459 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1461 for ( y = 1; y <= tiles_h; y++ )
1463 for ( x = 1; x <= tiles_w; x++ )
1465 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1467 if ( tiles_w & 0x1 )
1468 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1470 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1471 if ( tiles_h & 0x1 ) /* odd */
1472 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1474 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1476 /* move to correct place. */
1477 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1481 /* save buffer as file. */
1482 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);
1483 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1486 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1487 g_error_free (error);
1490 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1494 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1495 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1496 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1497 vik_viewport_configure ( vw->viking_vvp );
1500 g_free ( name_of_file );
1503 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1505 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1506 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1507 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1508 gdouble width_min, width_max, height_min, height_max;
1511 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1512 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1514 /* TODO: support for xzoom and yzoom values */
1515 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1516 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1518 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1519 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1521 gtk_spin_button_set_value ( width_spin, width );
1522 gtk_spin_button_set_value ( height_spin, height );
1525 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1527 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1528 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1531 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1532 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1533 if (pass_along[4]) /* save many images; find TOTAL area covered */
1535 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1536 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1538 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1539 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1540 g_free ( label_text );
1543 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1545 /* todo: default for answers inside VikWindow or static (thruout instance) */
1546 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1547 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1549 GTK_RESPONSE_REJECT,
1551 GTK_RESPONSE_ACCEPT,
1553 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1554 GtkWidget *png_radio, *jpeg_radio;
1555 GtkWidget *current_window_button;
1556 gpointer current_window_pass_along[7];
1557 GtkWidget *zoom_label, *zoom_spin;
1558 GtkWidget *total_size_label;
1560 /* only used if (!one_image_only) */
1561 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1564 width_label = gtk_label_new ( _("Width (pixels):") );
1565 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1566 height_label = gtk_label_new ( _("Height (pixels):") );
1567 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1569 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1570 /* TODO: separate xzoom and yzoom factors */
1571 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);
1573 total_size_label = gtk_label_new ( NULL );
1575 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1576 current_window_pass_along [0] = vw;
1577 current_window_pass_along [1] = width_spin;
1578 current_window_pass_along [2] = height_spin;
1579 current_window_pass_along [3] = zoom_spin;
1580 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1581 current_window_pass_along [5] = NULL;
1582 current_window_pass_along [6] = total_size_label;
1583 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1585 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1586 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1588 if ( ! vw->draw_image_save_as_png )
1589 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1591 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1592 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1593 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1594 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1595 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1596 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1597 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1598 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1599 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1601 if ( ! one_image_only )
1603 GtkWidget *tiles_width_label, *tiles_height_label;
1606 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1607 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1608 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1609 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1610 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1611 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1612 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1613 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1615 current_window_pass_along [4] = tiles_width_spin;
1616 current_window_pass_along [5] = tiles_height_spin;
1617 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1618 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1620 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1621 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1622 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1623 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1625 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1627 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1629 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1631 gtk_widget_hide ( GTK_WIDGET(dialog) );
1632 if ( one_image_only )
1633 save_image_file ( vw, fn,
1634 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1635 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1636 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1637 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1639 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1640 save_image_dir ( vw, fn,
1641 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1642 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1643 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1644 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1645 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1646 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1648 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1651 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1655 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1658 if (!vw->save_img_dia) {
1659 vw->save_img_dia = gtk_file_selection_new ( _("Save Image") );
1660 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1661 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1664 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1666 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1667 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 ) ) )
1669 draw_to_image_file ( vw, fn, TRUE );
1673 gtk_widget_hide ( vw->save_img_dia );
1676 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1679 if (!vw->save_img_dir_dia) {
1680 vw->save_img_dir_dia = gtk_file_selection_new ( _("Choose a name for a new directory to hold images"));
1681 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1682 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1685 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1687 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1688 if ( access ( fn, F_OK ) == 0 )
1689 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) );
1692 draw_to_image_file ( vw, fn, FALSE );
1696 gtk_widget_hide ( vw->save_img_dir_dia );
1699 #if GTK_CHECK_VERSION(2,10,0)
1700 static void print_cb ( GtkAction *a, VikWindow *vw )
1702 a_print(vw, vw->viking_vvp);
1706 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1707 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1709 VikViewportDrawMode drawmode;
1710 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1711 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1713 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1714 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1716 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1717 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1719 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1720 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1722 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1723 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1726 g_critical("Houston, we got a problem!\n");
1730 if ( !vw->only_updating_coord_mode_ui )
1732 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1733 if ( olddrawmode != drawmode )
1735 /* this takes care of coord mode too */
1736 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1737 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1738 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1739 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1740 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1747 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1749 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1750 g_assert(check_box);
1751 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1752 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1756 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1758 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1759 g_assert(check_box);
1760 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1761 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1765 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1767 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1768 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1769 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1770 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1771 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1773 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1774 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1778 gtk_widget_destroy ( colorsd );
1783 /***********************************************************************************************
1785 ***********************************************************************************************/
1787 static GtkActionEntry entries[] = {
1788 { "File", NULL, N_("_File"), 0, 0, 0 },
1789 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
1790 { "View", NULL, N_("_View"), 0, 0, 0 },
1791 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
1792 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
1793 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
1794 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
1795 { "Help", NULL, N_("_Help"), 0, 0, 0 },
1797 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
1798 { "Open", GTK_STOCK_OPEN, N_("_Open"), "<control>O", N_("Open a file"), (GCallback)load_file },
1799 { "Append", GTK_STOCK_ADD, N_("A_ppend File"), NULL, N_("Append data from a different file"), (GCallback)load_file },
1800 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
1801 { "AcquireGPS", NULL, N_("From _GPS"), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
1802 { "AcquireGoogle", NULL, N_("Google _Directions"), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1803 #ifdef VIK_CONFIG_GEOCACHES
1804 { "AcquireGC", NULL, N_("Geo_caches"), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1806 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
1807 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As"), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
1808 { "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 },
1809 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images"), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
1811 #if GTK_CHECK_VERSION(2,10,0)
1812 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
1815 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
1816 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
1818 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, N_("Go To Google Maps location"), NULL, N_("Go to address/place using Google Maps search"), (GCallback)goto_address },
1819 { "GotoLL", GTK_STOCK_QUIT, N_("_Go to Lat\\/Lon..."), NULL, N_("Go to arbitrary lat\\/lon coordinate"), (GCallback)draw_goto_cb },
1820 { "GotoUTM", GTK_STOCK_QUIT, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
1821 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Background Color..."), NULL, NULL, (GCallback)set_bg_color },
1822 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
1823 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
1824 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To"), "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1825 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
1826 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
1827 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
1828 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
1829 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
1830 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
1831 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
1832 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
1833 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
1834 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
1835 { "PanNorth", NULL, N_("Pan North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
1836 { "PanEast", NULL, N_("Pan East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
1837 { "PanSouth", NULL, N_("Pan South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
1838 { "PanWest", NULL, N_("Pan West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
1839 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
1841 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
1842 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
1843 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
1844 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
1845 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
1846 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences..."), NULL, NULL, (GCallback)preferences_cb },
1847 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
1849 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
1853 static GtkRadioActionEntry mode_entries[] = {
1854 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
1855 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
1856 { "ModeGoogle", NULL, N_("_Old Google Mode"), "<control>o", NULL, 2 },
1857 { "ModeKH", NULL, N_("Old _KH Mode"), "<control>k", NULL, 3 },
1858 { "ModeMercator", NULL, N_("_Google Mode"), "<control>g", NULL, 4 }
1861 static GtkRadioActionEntry tool_entries[] = {
1862 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 0 },
1863 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 1 }
1866 static GtkToggleActionEntry toggle_entries[] = {
1867 { "ShowScale", NULL, N_("Show Scale"), NULL, NULL, (GCallback)set_draw_scale, TRUE },
1868 { "ShowCenterMark", NULL, N_("Show Center Mark"), NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1869 { "FullScreen", NULL, N_("Full Screen"), "F11", NULL, (GCallback)full_screen_cb, FALSE },
1872 #include "menu.xml.h"
1873 static void window_create_ui( VikWindow *window )
1876 GtkActionGroup *action_group;
1877 GtkAccelGroup *accel_group;
1880 GtkIconFactory *icon_factory;
1881 GtkIconSet *icon_set;
1882 GtkRadioActionEntry *tools = NULL, *radio;
1885 uim = gtk_ui_manager_new ();
1888 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1889 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1892 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1893 g_error_free (error);
1897 action_group = gtk_action_group_new ("MenuActions");
1898 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
1899 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1900 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1901 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1903 icon_factory = gtk_icon_factory_new ();
1904 gtk_icon_factory_add_default (icon_factory);
1906 register_vik_icons(icon_factory);
1909 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1910 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1911 radio = &tools[ntools];
1913 *radio = tool_entries[i];
1914 radio->value = ntools;
1917 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1918 GtkActionEntry action;
1919 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1920 vik_layer_get_interface(i)->name,
1921 vik_layer_get_interface(i)->name,
1922 GTK_UI_MANAGER_MENUITEM, FALSE);
1924 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1925 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1926 gtk_icon_set_unref (icon_set);
1928 action.name = vik_layer_get_interface(i)->name;
1929 action.stock_id = vik_layer_get_interface(i)->name;
1930 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
1931 action.accelerator = NULL;
1932 action.tooltip = NULL;
1933 action.callback = (GCallback)menu_addlayer_cb;
1934 gtk_action_group_add_actions(action_group, &action, 1, window);
1936 if ( vik_layer_get_interface(i)->tools_count ) {
1937 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1938 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1941 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1942 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1943 radio = &tools[ntools];
1946 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
1947 _(vik_layer_get_interface(i)->tools[j].name),
1948 vik_layer_get_interface(i)->tools[j].name,
1949 GTK_UI_MANAGER_MENUITEM, FALSE);
1950 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
1951 _(vik_layer_get_interface(i)->tools[j].name),
1952 vik_layer_get_interface(i)->tools[j].name,
1953 GTK_UI_MANAGER_TOOLITEM, FALSE);
1955 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
1957 radio->name = vik_layer_get_interface(i)->tools[j].name;
1958 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1959 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
1960 radio->accelerator = NULL;
1961 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
1962 radio->value = ntools;
1965 g_object_unref (icon_factory);
1967 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1970 gtk_ui_manager_insert_action_group (uim, action_group, 0);
1972 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1973 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1974 GtkAction *action = gtk_action_group_get_action(action_group,
1975 vik_layer_get_interface(i)->tools[j].name);
1976 g_object_set(action, "sensitive", FALSE, NULL);
1979 window->action_group = action_group;
1981 accel_group = gtk_ui_manager_get_accel_group (uim);
1982 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1983 gtk_ui_manager_ensure_update (uim);
1988 #include "icons/icons.h"
1990 const GdkPixdata *data;
1993 { &addtr_18, "Create Track" },
1994 { &begintr_18, "Begin Track" },
1995 { &edtr_18, "Edit Trackpoint" },
1996 { &addwp_18, "Create Waypoint" },
1997 { &edwp_18, "Edit Waypoint" },
1998 { &iscissors_18, "Magic Scissors" },
1999 { &zoom_18, "vik-icon-zoom" },
2000 { &ruler_18, "vik-icon-ruler" },
2001 { &geozoom_18, "Georef Zoom Tool" },
2002 { &geomove_18, "Georef Move Map" },
2003 { &mapdl_18, "Maps Download" },
2004 { &demdl_18, "DEM Download/Import" },
2005 { &showpic_18, "Show Picture" },
2008 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2011 register_vik_icons (GtkIconFactory *icon_factory)
2013 GtkIconSet *icon_set;
2016 for (i = 0; i < n_stock_icons; i++) {
2017 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2018 stock_icons[i].data, FALSE, NULL ));
2019 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2020 gtk_icon_set_unref (icon_set);
2024 void vik_window_cursors_init()
2026 GdkPixbuf *cursor_pixbuf;
2027 GError *cursor_load_err;
2029 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
2030 vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2032 g_object_unref ( G_OBJECT(cursor_pixbuf) );
2034 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
2035 vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2037 g_object_unref ( G_OBJECT(cursor_pixbuf) );
2040 void vik_window_cursors_uninit()
2042 gdk_cursor_unref ( vw_cursor_zoom );
2043 gdk_cursor_unref ( vw_cursor_ruler );