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"
38 #include <glib/gprintf.h>
39 #include <glib/gi18n.h>
41 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
42 #define make_dir(dir) mkdir(dir)
44 #include <sys/types.h>
46 #define make_dir(dir) mkdir(dir,0777)
49 #define VIKING_WINDOW_WIDTH 1000
50 #define VIKING_WINDOW_HEIGHT 800
51 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
52 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
53 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
55 static void window_finalize ( GObject *gob );
56 static GObjectClass *parent_class;
58 static void window_init ( VikWindow *vw );
59 static void window_class_init ( VikWindowClass *klass );
60 static void window_set_filename ( VikWindow *vw, const gchar *filename );
62 static void draw_update ( VikWindow *vw );
64 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
68 static gboolean delete_event( VikWindow *vw );
70 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
72 static void window_configure_event ( VikWindow *vw );
73 static void draw_sync ( VikWindow *vw );
74 static void draw_redraw ( VikWindow *vw );
75 static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
76 static void draw_click ( VikWindow *vw, GdkEventButton *event );
77 static void draw_release ( VikWindow *vw, GdkEventButton *event );
78 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
79 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
80 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
82 static void draw_status ();
84 /* End Drawing Functions */
86 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
87 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
88 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
96 #define TOOL_LAYER_TYPE_NONE -1
101 toolbox_tool_t *tools;
105 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
106 static toolbox_tools_t* toolbox_create(VikWindow *vw);
107 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
108 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
109 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
110 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
111 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
112 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
116 static void window_create_ui( VikWindow *window );
117 static void register_vik_icons (GtkIconFactory *icon_factory);
120 static void load_file ( GtkAction *a, VikWindow *vw );
121 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
122 static gboolean save_file ( GtkAction *a, VikWindow *vw );
123 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
124 static gboolean window_save ( VikWindow *vw );
128 VikViewport *viking_vvp;
129 VikLayersPanel *viking_vlp;
130 VikStatusbar *viking_vs;
134 GtkItemFactory *item_factory;
136 /* tool management state */
139 guint16 tool_layer_id;
140 guint16 tool_tool_id;
142 GtkActionGroup *action_group;
146 guint draw_image_width, draw_image_height;
147 gboolean draw_image_save_as_png;
152 GtkWidget *open_dia, *save_dia;
153 GtkWidget *save_img_dia, *save_img_dir_dia;
155 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
158 /* half-drawn update */
160 VikCoord trigger_center;
172 VW_OPENWINDOW_SIGNAL,
176 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
178 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Zoom"), N_("Ruler"), N_("Pan") };
180 GdkCursor *vw_cursor_zoom = NULL;
181 GdkCursor *vw_cursor_ruler = NULL;
183 GType vik_window_get_type (void)
185 static GType vw_type = 0;
189 static const GTypeInfo vw_info =
191 sizeof (VikWindowClass),
192 NULL, /* base_init */
193 NULL, /* base_finalize */
194 (GClassInitFunc) window_class_init, /* class_init */
195 NULL, /* class_finalize */
196 NULL, /* class_data */
199 (GInstanceInitFunc) window_init,
201 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
207 VikViewport * vik_window_viewport(VikWindow *vw)
209 return(vw->viking_vvp);
212 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
214 int i, j, tool_count;
215 VikLayerInterface *layer_interface;
217 if (!vw->action_group) return;
219 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
221 layer_interface = vik_layer_get_interface(i);
222 tool_count = layer_interface->tools_count;
224 for (j = 0; j < tool_count; j++) {
225 action = gtk_action_group_get_action(vw->action_group,
226 layer_interface->tools[j].name);
227 g_object_set(action, "sensitive", i == vl->type, NULL);
232 static void window_finalize ( GObject *gob )
234 VikWindow *vw = VIK_WINDOW(gob);
235 g_return_if_fail ( vw != NULL );
237 a_background_remove_status ( vw->viking_vs );
239 G_OBJECT_CLASS(parent_class)->finalize(gob);
243 static void window_class_init ( VikWindowClass *klass )
246 GObjectClass *object_class;
248 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);
249 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);
251 object_class = G_OBJECT_CLASS (klass);
253 object_class->finalize = window_finalize;
255 parent_class = g_type_class_peek_parent (klass);
259 static void window_init ( VikWindow *vw )
261 GtkWidget *main_vbox;
264 vw->action_group = NULL;
266 vw->viking_vvp = vik_viewport_new();
267 vw->viking_vlp = vik_layers_panel_new();
268 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
269 vw->viking_vs = vik_statusbar_new();
271 vw->vt = toolbox_create(vw);
272 window_create_ui(vw);
273 window_set_filename (vw, NULL);
275 toolbox_activate(vw->vt, "Zoom");
278 vw->item_factory = NULL;
280 vw->modified = FALSE;
281 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 );
915 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
919 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
922 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
925 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
928 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
932 gchar *s = (gchar *)gtk_action_get_name(a);
938 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
939 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
940 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
941 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
942 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
947 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
951 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
952 struct LatLon ll, llold;
953 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
954 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
955 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
959 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
960 struct UTM utm, utmold;
961 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
962 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
963 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
968 g_critical("Houston we have a problem\n");
972 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
976 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
979 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
980 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
981 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
989 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
991 a_clipboard_copy_selected ( vw->viking_vlp );
994 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
996 a_clipboard_copy_selected ( vw->viking_vlp );
997 menu_delete_layer_cb ( a, vw );
1000 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1002 if ( a_clipboard_paste ( vw->viking_vlp ) )
1005 vw->modified = TRUE;
1009 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1011 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1012 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1015 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1017 a_dialog_about(GTK_WINDOW(vw));
1020 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1022 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1024 vik_layers_panel_delete_selected ( vw->viking_vlp );
1025 vw->modified = TRUE;
1028 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1031 /***************************************
1032 ** tool management routines
1034 ***************************************/
1036 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1038 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1041 vt->active_tool = -1;
1043 if (!vw->viking_vvp) {
1044 g_critical("no viewport found.");
1050 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1052 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1053 vt->tools[vt->n_tools].ti = *vti;
1054 vt->tools[vt->n_tools].layer_type = layer_type;
1056 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1059 vt->tools[vt->n_tools].state = NULL;
1064 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1067 for (i=0; i<vt->n_tools; i++) {
1068 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1075 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1077 int tool = toolbox_get_tool(vt, tool_name);
1078 toolbox_tool_t *t = &vt->tools[tool];
1079 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1081 if (tool == vt->n_tools) {
1082 g_critical("trying to activate a non-existent tool...");
1085 /* is the tool already active? */
1086 if (vt->active_tool == tool) {
1090 if (vt->active_tool != -1) {
1091 if (vt->tools[vt->active_tool].ti.deactivate) {
1092 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1095 if (t->ti.activate) {
1096 t->ti.activate(vl, t->state);
1098 vt->active_tool = tool;
1101 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1103 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1104 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1105 gint ltype = vt->tools[vt->active_tool].layer_type;
1106 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1107 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1111 static void toolbox_move (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.move) {
1115 gint ltype = vt->tools[vt->active_tool].layer_type;
1116 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1117 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1118 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1122 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1124 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1125 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1126 gint ltype = vt->tools[vt->active_tool].layer_type;
1127 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1128 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1131 /** End tool management ************************************/
1133 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1135 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1138 /* this function gets called whenever a toolbar tool is clicked */
1139 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1141 /* White Magic, my friends ... White Magic... */
1142 int layer_id, tool_id;
1144 toolbox_activate(vw->vt, gtk_action_get_name(a));
1146 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1147 vw->current_tool = TOOL_ZOOM;
1148 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
1150 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1151 vw->current_tool = TOOL_RULER;
1152 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
1155 /* TODO: only enable tools from active layer */
1156 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1157 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1158 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1159 vw->current_tool = TOOL_LAYER;
1160 vw->tool_layer_id = layer_id;
1161 vw->tool_tool_id = tool_id;
1162 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
1170 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1175 g_free ( vw->filename );
1176 if ( filename == NULL )
1178 vw->filename = NULL;
1179 file = _("Untitled");
1183 vw->filename = g_strdup(filename);
1184 file = a_file_basename ( filename );
1186 title = g_strdup_printf( "%s - Viking", file );
1187 gtk_window_set_title ( GTK_WINDOW(vw), title );
1191 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1193 GtkWidget *mode_button;
1196 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1197 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1198 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1199 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1200 default: buttonname = "/ui/MainMenu/View/ModeKH";
1202 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1203 g_assert ( mode_button );
1207 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1209 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1212 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1216 GtkWidget *mode_button;
1217 if ( change_filename )
1218 window_set_filename ( vw, filename );
1219 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1220 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. */
1221 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1222 vw->only_updating_coord_mode_ui = FALSE;
1224 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1226 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1227 g_assert ( mode_button );
1228 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1230 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1231 g_assert ( mode_button );
1232 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1234 default: draw_update ( vw );
1237 static void load_file ( GtkAction *a, VikWindow *vw )
1240 if (!strcmp(gtk_action_get_name(a), "Open")) {
1243 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1247 g_critical("Houston we got a problem\n");
1251 if ( ! vw->open_dia )
1253 vw->open_dia = gtk_file_selection_new ( _("Please select a GPS data file to open. ") );
1254 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1255 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1256 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1258 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1260 gtk_widget_hide ( vw->open_dia );
1261 #ifdef VIKING_PROMPT_IF_MODIFIED
1262 if ( (vw->modified || vw->filename) && newwindow )
1264 if ( vw->filename && newwindow )
1266 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1268 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1269 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1271 vik_window_open_file ( vw, *files, change_fn );
1277 gtk_widget_hide ( vw->open_dia );
1280 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1282 gboolean rv = FALSE;
1284 if ( ! vw->save_dia )
1286 vw->save_dia = gtk_file_selection_new ( _("Save as Viking File.") );
1287 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1288 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1291 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1293 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1294 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 ) ) )
1296 window_set_filename ( vw, fn );
1297 rv = window_save ( vw );
1298 vw->modified = FALSE;
1302 gtk_widget_hide ( vw->save_dia );
1306 static gboolean window_save ( VikWindow *vw )
1308 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1312 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1317 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1319 if ( ! vw->filename )
1320 return save_file_as ( NULL, vw );
1323 vw->modified = FALSE;
1324 return window_save ( vw );
1328 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1330 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1333 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1335 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1338 #ifdef VIK_CONFIG_GEOCACHES
1339 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1341 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1345 static void goto_address( GtkAction *a, VikWindow *vw)
1347 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1350 static void clear_cb ( GtkAction *a, VikWindow *vw )
1352 vik_layers_panel_clear ( vw->viking_vlp );
1353 window_set_filename ( vw, NULL );
1357 static void window_close ( GtkAction *a, VikWindow *vw )
1359 if ( ! delete_event ( vw ) )
1360 gtk_widget_destroy ( GTK_WIDGET(vw) );
1363 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1365 if (save_file( NULL, vw)) {
1366 window_close( NULL, vw);
1373 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1375 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1376 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1378 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1379 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1384 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1386 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1387 GdkPixbuf *pixbuf_to_save;
1388 gdouble old_xmpp, old_ympp;
1389 GError *error = NULL;
1391 /* backup old zoom & set new */
1392 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1393 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1394 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1396 /* reset width and height: */
1397 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1399 /* draw all layers */
1402 /* save buffer as file. */
1403 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);
1404 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1407 g_warning("Unable to write to file %s: %s", fn, error->message );
1408 g_error_free (error);
1410 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1412 /* pretend like nothing happened ;) */
1413 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1414 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1415 vik_viewport_configure ( vw->viking_vvp );
1419 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 )
1421 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1422 gchar *name_of_file = g_malloc ( size );
1424 struct UTM utm_orig, utm;
1426 /* *** copied from above *** */
1427 GdkPixbuf *pixbuf_to_save;
1428 gdouble old_xmpp, old_ympp;
1429 GError *error = NULL;
1431 /* backup old zoom & set new */
1432 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1433 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1434 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1436 /* reset width and height: do this only once for all images (same size) */
1437 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1438 /* *** end copy from above *** */
1440 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1444 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1446 for ( y = 1; y <= tiles_h; y++ )
1448 for ( x = 1; x <= tiles_w; x++ )
1450 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1452 if ( tiles_w & 0x1 )
1453 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1455 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1456 if ( tiles_h & 0x1 ) /* odd */
1457 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1459 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1461 /* move to correct place. */
1462 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1466 /* save buffer as file. */
1467 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);
1468 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1471 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1472 g_error_free (error);
1475 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1479 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1480 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1481 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1482 vik_viewport_configure ( vw->viking_vvp );
1485 g_free ( name_of_file );
1488 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1490 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1491 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1492 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1493 gdouble width_min, width_max, height_min, height_max;
1496 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1497 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1499 /* TODO: support for xzoom and yzoom values */
1500 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1501 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1503 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1504 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1506 gtk_spin_button_set_value ( width_spin, width );
1507 gtk_spin_button_set_value ( height_spin, height );
1510 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1512 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1513 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1516 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1517 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1518 if (pass_along[4]) /* save many images; find TOTAL area covered */
1520 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1521 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1523 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1524 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1525 g_free ( label_text );
1528 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1530 /* todo: default for answers inside VikWindow or static (thruout instance) */
1531 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1532 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1534 GTK_RESPONSE_REJECT,
1536 GTK_RESPONSE_ACCEPT,
1538 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1539 GtkWidget *png_radio, *jpeg_radio;
1540 GtkWidget *current_window_button;
1541 gpointer current_window_pass_along[7];
1542 GtkWidget *zoom_label, *zoom_spin;
1543 GtkWidget *total_size_label;
1545 /* only used if (!one_image_only) */
1546 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1549 width_label = gtk_label_new ( _("Width (pixels):") );
1550 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1551 height_label = gtk_label_new ( _("Height (pixels):") );
1552 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1554 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1555 /* TODO: separate xzoom and yzoom factors */
1556 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);
1558 total_size_label = gtk_label_new ( NULL );
1560 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1561 current_window_pass_along [0] = vw;
1562 current_window_pass_along [1] = width_spin;
1563 current_window_pass_along [2] = height_spin;
1564 current_window_pass_along [3] = zoom_spin;
1565 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1566 current_window_pass_along [5] = NULL;
1567 current_window_pass_along [6] = total_size_label;
1568 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1570 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1571 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1573 if ( ! vw->draw_image_save_as_png )
1574 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1576 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1577 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1578 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1579 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1580 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1581 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1582 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1583 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1584 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1586 if ( ! one_image_only )
1588 GtkWidget *tiles_width_label, *tiles_height_label;
1591 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1592 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1593 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1594 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1595 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1596 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1597 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1598 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1600 current_window_pass_along [4] = tiles_width_spin;
1601 current_window_pass_along [5] = tiles_height_spin;
1602 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1603 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1605 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1606 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1607 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1608 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1610 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1612 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1614 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1616 gtk_widget_hide ( GTK_WIDGET(dialog) );
1617 if ( one_image_only )
1618 save_image_file ( vw, fn,
1619 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1620 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1621 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1622 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1624 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1625 save_image_dir ( vw, fn,
1626 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1627 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1628 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1629 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1630 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1631 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1633 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1636 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1640 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1643 if (!vw->save_img_dia) {
1644 vw->save_img_dia = gtk_file_selection_new ( _("Save Image") );
1645 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1646 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1649 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1651 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1652 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 ) ) )
1654 draw_to_image_file ( vw, fn, TRUE );
1658 gtk_widget_hide ( vw->save_img_dia );
1661 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1664 if (!vw->save_img_dir_dia) {
1665 vw->save_img_dir_dia = gtk_file_selection_new ( _("Choose a name for a new directory to hold images"));
1666 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1667 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1670 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1672 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1673 if ( access ( fn, F_OK ) == 0 )
1674 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) );
1677 draw_to_image_file ( vw, fn, FALSE );
1681 gtk_widget_hide ( vw->save_img_dir_dia );
1684 #if GTK_CHECK_VERSION(2,10,0)
1685 static void print_cb ( GtkAction *a, VikWindow *vw )
1687 a_print(vw, vw->viking_vvp);
1691 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1692 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1694 VikViewportDrawMode drawmode;
1695 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1696 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1698 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1699 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1701 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1702 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1704 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1705 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1707 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1708 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1711 g_critical("Houston, we got a problem!\n");
1715 if ( !vw->only_updating_coord_mode_ui )
1717 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1718 if ( olddrawmode != drawmode )
1720 /* this takes care of coord mode too */
1721 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1722 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1723 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1724 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1725 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1732 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1734 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1735 g_assert(check_box);
1736 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1737 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1741 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1743 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1744 g_assert(check_box);
1745 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1746 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1750 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1752 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1753 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1754 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1755 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1756 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1758 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1759 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1763 gtk_widget_destroy ( colorsd );
1768 /***********************************************************************************************
1770 ***********************************************************************************************/
1772 static GtkActionEntry entries[] = {
1773 { "File", NULL, "_File", 0, 0, 0 },
1774 { "Edit", NULL, "_Edit", 0, 0, 0 },
1775 { "View", NULL, "_View", 0, 0, 0 },
1776 { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1777 { "SetPan", NULL, "_Pan", 0, 0, 0 },
1778 { "Layers", NULL, "_Layers", 0, 0, 0 },
1779 { "Tools", NULL, "_Tools", 0, 0, 0 },
1780 { "Help", NULL, "_Help", 0, 0, 0 },
1782 { "New", GTK_STOCK_NEW, "_New", "<control>N", "New file", (GCallback)newwindow_cb },
1783 { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", (GCallback)load_file },
1784 { "Append", GTK_STOCK_ADD, "A_ppend File", NULL, "Append data from a different file", (GCallback)load_file },
1785 { "Acquire", NULL, "A_cquire", 0, 0, 0 },
1786 { "AcquireGPS", NULL, "From _GPS", NULL, "Transfer data from a GPS device", (GCallback)acquire_from_gps },
1787 { "AcquireGoogle", NULL, "Google _Directions", NULL, "Get driving directions from Google", (GCallback)acquire_from_google },
1788 #ifdef VIK_CONFIG_GEOCACHES
1789 { "AcquireGC", NULL, "Geo_caches", NULL, "Get Geocaches from geocaching.com", (GCallback)acquire_from_gc },
1791 { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the file", (GCallback)save_file },
1792 { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As", NULL, "Save the file under different name", (GCallback)save_file_as },
1793 { "GenImg", GTK_STOCK_CLEAR, "_Generate Image File", NULL, "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1794 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL, "FIXME:IMGDIR", (GCallback)draw_to_image_dir_cb },
1796 #if GTK_CHECK_VERSION(2,10,0)
1797 { "Print", GTK_STOCK_PRINT, "_Print...", NULL, "Print maps", (GCallback)print_cb },
1800 { "Exit", GTK_STOCK_QUIT, "E_xit", "<control>W", "Exit the program", (GCallback)window_close },
1801 { "SaveExit", GTK_STOCK_QUIT, "Save and Exit", NULL, "Save and Exit the program", (GCallback)save_file_and_exit },
1803 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, "Go To Google Maps location", NULL, "Go to address/place using Google Maps search", (GCallback)goto_address },
1804 { "GotoLL", GTK_STOCK_QUIT, "_Go to Lat\\/Lon...", NULL, "Go to arbitrary lat\\/lon coordinate", (GCallback)draw_goto_cb },
1805 { "GotoUTM", GTK_STOCK_QUIT, "Go to UTM...", NULL, "Go to arbitrary UTM coordinate", (GCallback)draw_goto_cb },
1806 { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...", NULL, NULL, (GCallback)set_bg_color },
1807 { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>plus", NULL, (GCallback)draw_zoom_cb },
1808 { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control>minus", NULL, (GCallback)draw_zoom_cb },
1809 { "ZoomTo", GTK_STOCK_ZOOM_FIT, "Zoom _To", "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1810 { "Zoom0.25", NULL, "0.25", NULL, NULL, (GCallback)draw_zoom_cb },
1811 { "Zoom0.5", NULL, "0.5", NULL, NULL, (GCallback)draw_zoom_cb },
1812 { "Zoom1", NULL, "1", NULL, NULL, (GCallback)draw_zoom_cb },
1813 { "Zoom2", NULL, "2", NULL, NULL, (GCallback)draw_zoom_cb },
1814 { "Zoom4", NULL, "4", NULL, NULL, (GCallback)draw_zoom_cb },
1815 { "Zoom8", NULL, "8", NULL, NULL, (GCallback)draw_zoom_cb },
1816 { "Zoom16", NULL, "16", NULL, NULL, (GCallback)draw_zoom_cb },
1817 { "Zoom32", NULL, "32", NULL, NULL, (GCallback)draw_zoom_cb },
1818 { "Zoom64", NULL, "64", NULL, NULL, (GCallback)draw_zoom_cb },
1819 { "Zoom128", NULL, "128", NULL, NULL, (GCallback)draw_zoom_cb },
1820 { "PanNorth", NULL, "Pan North", "<control>Up", NULL, (GCallback)draw_pan_cb },
1821 { "PanEast", NULL, "Pan East", "<control>Right", NULL, (GCallback)draw_pan_cb },
1822 { "PanSouth", NULL, "Pan South", "<control>Down", NULL, (GCallback)draw_pan_cb },
1823 { "PanWest", NULL, "Pan West", "<control>Left", NULL, (GCallback)draw_pan_cb },
1824 { "BGJobs", GTK_STOCK_EXECUTE, "Background _Jobs", NULL, NULL, (GCallback)a_background_show_window },
1826 { "Cut", GTK_STOCK_CUT, "Cu_t", NULL, NULL, (GCallback)menu_cut_layer_cb },
1827 { "Copy", GTK_STOCK_COPY, "_Copy", NULL, NULL, (GCallback)menu_copy_layer_cb },
1828 { "Paste", GTK_STOCK_PASTE, "_Paste", NULL, NULL, (GCallback)menu_paste_layer_cb },
1829 { "Delete", GTK_STOCK_DELETE, "_Delete", NULL, NULL, (GCallback)menu_delete_layer_cb },
1830 { "DeleteAll", NULL, "Delete All", NULL, NULL, (GCallback)clear_cb },
1831 { "Properties",GTK_STOCK_PROPERTIES, "_Properties", NULL, NULL, (GCallback)menu_properties_cb },
1833 { "About", GTK_STOCK_ABOUT, "_About", NULL, NULL, (GCallback)help_about_cb },
1837 static GtkRadioActionEntry mode_entries[] = {
1838 { "ModeUTM", NULL, "_UTM Mode", "<control>u", NULL, 0 },
1839 { "ModeExpedia", NULL, "_Expedia Mode", "<control>e", NULL, 1 },
1840 { "ModeGoogle", NULL, "_Old Google Mode", "<control>o", NULL, 2 },
1841 { "ModeKH", NULL, "Old _KH Mode", "<control>k", NULL, 3 },
1842 { "ModeMercator", NULL, "_Google Mode", "<control>g", NULL, 4 }
1845 static GtkRadioActionEntry tool_entries[] = {
1846 { "Zoom", "vik-icon-zoom", "_Zoom", "<control><shift>Z", "Zoom Tool", 0 },
1847 { "Ruler", "vik-icon-ruler", "_Ruler", "<control><shift>R", "Ruler Tool", 1 }
1850 static GtkToggleActionEntry toggle_entries[] = {
1851 { "ShowScale", NULL, "Show Scale", NULL, NULL, (GCallback)set_draw_scale, TRUE },
1852 { "ShowCenterMark", NULL, "Show Center Mark", NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1855 #include "menu.xml.h"
1856 static void window_create_ui( VikWindow *window )
1859 GtkActionGroup *action_group;
1860 GtkAccelGroup *accel_group;
1863 GtkIconFactory *icon_factory;
1864 GtkIconSet *icon_set;
1865 GtkRadioActionEntry *tools = NULL, *radio;
1868 uim = gtk_ui_manager_new ();
1871 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1872 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1875 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1876 g_error_free (error);
1880 action_group = gtk_action_group_new ("MenuActions");
1881 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1882 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1883 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1885 icon_factory = gtk_icon_factory_new ();
1886 gtk_icon_factory_add_default (icon_factory);
1888 register_vik_icons(icon_factory);
1891 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1892 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1893 radio = &tools[ntools];
1895 *radio = tool_entries[i];
1896 radio->value = ntools;
1899 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1900 GtkActionEntry action;
1901 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1902 vik_layer_get_interface(i)->name,
1903 vik_layer_get_interface(i)->name,
1904 GTK_UI_MANAGER_MENUITEM, FALSE);
1906 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1907 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1908 gtk_icon_set_unref (icon_set);
1910 action.name = vik_layer_get_interface(i)->name;
1911 action.stock_id = vik_layer_get_interface(i)->name;
1912 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
1913 action.accelerator = NULL;
1914 action.tooltip = NULL;
1915 action.callback = (GCallback)menu_addlayer_cb;
1916 gtk_action_group_add_actions(action_group, &action, 1, window);
1918 if ( vik_layer_get_interface(i)->tools_count ) {
1919 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1920 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1923 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1924 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1925 radio = &tools[ntools];
1928 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
1929 _(vik_layer_get_interface(i)->tools[j].name),
1930 vik_layer_get_interface(i)->tools[j].name,
1931 GTK_UI_MANAGER_MENUITEM, FALSE);
1932 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
1933 _(vik_layer_get_interface(i)->tools[j].name),
1934 vik_layer_get_interface(i)->tools[j].name,
1935 GTK_UI_MANAGER_TOOLITEM, FALSE);
1937 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
1939 radio->name = vik_layer_get_interface(i)->tools[j].name;
1940 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1941 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
1942 radio->accelerator = NULL;
1943 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
1944 radio->value = ntools;
1947 g_object_unref (icon_factory);
1949 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1952 gtk_ui_manager_insert_action_group (uim, action_group, 0);
1954 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1955 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1956 GtkAction *action = gtk_action_group_get_action(action_group,
1957 vik_layer_get_interface(i)->tools[j].name);
1958 g_object_set(action, "sensitive", FALSE, NULL);
1961 window->action_group = action_group;
1963 accel_group = gtk_ui_manager_get_accel_group (uim);
1964 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1965 gtk_ui_manager_ensure_update (uim);
1970 #include "icons/icons.h"
1972 const GdkPixdata *data;
1975 { &addtr_18, "Create Track" },
1976 { &begintr_18, "Begin Track" },
1977 { &edtr_18, "Edit Trackpoint" },
1978 { &addwp_18, "Create Waypoint" },
1979 { &edwp_18, "Edit Waypoint" },
1980 { &iscissors_18, "Magic Scissors" },
1981 { &zoom_18, "vik-icon-zoom" },
1982 { &ruler_18, "vik-icon-ruler" },
1983 { &geozoom_18, "Georef Zoom Tool" },
1984 { &geomove_18, "Georef Move Map" },
1985 { &mapdl_18, "Maps Download" },
1986 { &demdl_18, "DEM Download/Import" },
1987 { &showpic_18, "Show Picture" },
1990 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1993 register_vik_icons (GtkIconFactory *icon_factory)
1995 GtkIconSet *icon_set;
1998 for (i = 0; i < n_stock_icons; i++) {
1999 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2000 stock_icons[i].data, FALSE, NULL ));
2001 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2002 gtk_icon_set_unref (icon_set);
2006 void vik_window_cursors_init()
2008 GdkPixbuf *cursor_pixbuf;
2009 GError *cursor_load_err;
2011 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
2012 vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2014 g_object_unref ( G_OBJECT(cursor_pixbuf) );
2016 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
2017 vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
2019 g_object_unref ( G_OBJECT(cursor_pixbuf) );
2022 void vik_window_cursors_uninit()
2024 gdk_cursor_unref ( vw_cursor_zoom );
2025 gdk_cursor_unref ( vw_cursor_ruler );