Experimental "begin track" tool
[viking/gosmore.git] / src / vikwindow.c
bloba247b006080eb10b4d5e001b60b1cf3800f5da2a
1 /*
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
21 #include "viking.h"
22 #include "background.h"
23 #include "acquire.h"
24 #include "datasources.h"
25 #include "googlesearch.h"
26 #include "dems.h"
28 #define VIKING_TITLE " - Viking"
30 #include <glib/gprintf.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <glib/gprintf.h>
36 #ifdef WINDOWS
37 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
38 #define make_dir(dir) mkdir(dir)
39 #else
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #define make_dir(dir) mkdir(dir,0777)
43 #endif
45 #define VIKING_WINDOW_WIDTH 1000
46 #define VIKING_WINDOW_HEIGHT 800
47 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
48 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
49 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
51 static void window_finalize ( GObject *gob );
52 static GObjectClass *parent_class;
54 static void window_init ( VikWindow *vw );
55 static void window_class_init ( VikWindowClass *klass );
57 static void draw_update ( VikWindow *vw );
59 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
61 /* Drawing & stuff */
63 static gboolean delete_event( VikWindow *vw );
65 static void window_configure_event ( VikWindow *vw );
66 static void draw_sync ( VikWindow *vw );
67 static void draw_redraw ( VikWindow *vw );
68 static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
69 static void draw_click ( VikWindow *vw, GdkEventButton *event );
70 static void draw_release ( VikWindow *vw, GdkEventButton *event );
71 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
72 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
73 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
75 static void draw_status ();
77 /* End Drawing Functions */
79 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
80 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
81 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
83 /* tool management */
84 typedef struct {
85 VikToolInterface ti;
86 gpointer state;
87 gint layer_type;
88 } toolbox_tool_t;
89 #define TOOL_LAYER_TYPE_NONE -1
91 typedef struct {
92 int active_tool;
93 int n_tools;
94 toolbox_tool_t *tools;
95 VikWindow *vw;
96 } toolbox_tools_t;
98 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
99 static toolbox_tools_t* toolbox_create(VikWindow *vw);
100 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
101 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
102 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
103 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
104 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
105 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
108 /* ui creation */
109 static void window_create_ui( VikWindow *window );
110 static void register_vik_icons (GtkIconFactory *icon_factory);
112 /* i/o */
113 static void load_file ( GtkAction *a, VikWindow *vw );
114 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
115 static gboolean save_file ( GtkAction *a, VikWindow *vw );
116 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
117 static gboolean window_save ( VikWindow *vw );
119 struct _VikWindow {
120 GtkWindow gtkwindow;
121 VikViewport *viking_vvp;
122 VikLayersPanel *viking_vlp;
123 VikStatusbar *viking_vs;
125 GtkToolbar *toolbar;
127 GtkItemFactory *item_factory;
129 /* tool management state */
130 guint current_tool;
131 toolbox_tools_t *vt;
132 guint16 tool_layer_id;
133 guint16 tool_tool_id;
135 GtkActionGroup *action_group;
137 gint pan_x, pan_y;
139 guint draw_image_width, draw_image_height;
140 gboolean draw_image_save_as_png;
142 gchar *filename;
143 gboolean modified;
145 GtkWidget *open_dia, *save_dia;
146 GtkWidget *save_img_dia, *save_img_dir_dia;
148 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
149 GtkUIManager *uim;
151 /* half-drawn update */
152 VikLayer *trigger;
153 VikCoord trigger_center;
156 enum {
157 TOOL_ZOOM = 0,
158 TOOL_RULER,
159 TOOL_LAYER,
160 NUMBER_OF_TOOLS
163 enum {
164 VW_NEWWINDOW_SIGNAL,
165 VW_OPENWINDOW_SIGNAL,
166 VW_LAST_SIGNAL
169 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
171 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
173 GdkCursor *vw_cursor_zoom = NULL;
174 GdkCursor *vw_cursor_ruler = NULL;
176 GType vik_window_get_type (void)
178 static GType vw_type = 0;
180 if (!vw_type)
182 static const GTypeInfo vw_info =
184 sizeof (VikWindowClass),
185 NULL, /* base_init */
186 NULL, /* base_finalize */
187 (GClassInitFunc) window_class_init, /* class_init */
188 NULL, /* class_finalize */
189 NULL, /* class_data */
190 sizeof (VikWindow),
192 (GInstanceInitFunc) window_init,
194 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
197 return vw_type;
200 VikViewport * vik_window_viewport(VikWindow *vw)
202 return(vw->viking_vvp);
205 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
207 int i, j, tool_count;
208 VikLayerInterface *layer_interface;
210 if (!vw->action_group) return;
212 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
213 GtkAction *action;
214 layer_interface = vik_layer_get_interface(i);
215 tool_count = layer_interface->tools_count;
217 for (j = 0; j < tool_count; j++) {
218 action = gtk_action_group_get_action(vw->action_group,
219 layer_interface->tools[j].name);
220 g_object_set(action, "sensitive", i == vl->type, NULL);
225 static void window_finalize ( GObject *gob )
227 VikWindow *vw = VIK_WINDOW(gob);
228 g_return_if_fail ( vw != NULL );
230 a_background_remove_status ( vw->viking_vs );
232 G_OBJECT_CLASS(parent_class)->finalize(gob);
236 static void window_class_init ( VikWindowClass *klass )
238 /* destructor */
239 GObjectClass *object_class;
241 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);
242 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);
244 object_class = G_OBJECT_CLASS (klass);
246 object_class->finalize = window_finalize;
248 parent_class = g_type_class_peek_parent (klass);
252 static void window_init ( VikWindow *vw )
254 GtkWidget *main_vbox;
255 GtkWidget *hpaned;
257 vw->action_group = NULL;
259 vw->viking_vvp = vik_viewport_new();
260 vw->viking_vlp = vik_layers_panel_new();
261 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
262 vw->viking_vs = vik_statusbar_new();
264 vw->vt = toolbox_create(vw);
265 window_create_ui(vw);
266 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
268 toolbox_activate(vw->vt, "Zoom");
270 vw->filename = NULL;
271 vw->item_factory = NULL;
273 vw->modified = FALSE;
274 vw->only_updating_coord_mode_ui = FALSE;
277 vw->pan_x = vw->pan_y = -1;
278 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
279 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
280 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
282 main_vbox = gtk_vbox_new(FALSE, 1);
283 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
285 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
286 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
287 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
288 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
290 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
292 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
293 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
294 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
295 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
296 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
297 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
298 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
299 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
301 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
303 hpaned = gtk_hpaned_new ();
304 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
305 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
307 /* This packs the button into the window (a gtk container). */
308 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
310 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
312 a_background_add_status(vw->viking_vs);
314 vw->open_dia = NULL;
315 vw->save_dia = NULL;
316 vw->save_img_dia = NULL;
317 vw->save_img_dir_dia = NULL;
320 VikWindow *vik_window_new ()
322 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
325 static gboolean delete_event( VikWindow *vw )
327 #ifdef VIKING_PROMPT_IF_MODIFIED
328 if ( vw->modified )
329 #else
330 if (0)
331 #endif
333 GtkDialog *dia;
334 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
335 "Do you want to save the changes you made to the document \"%s\"?\n\nYour changes will be lost if you don't save them.",
336 vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
337 gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
338 switch ( gtk_dialog_run ( dia ) )
340 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
341 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
342 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
345 return FALSE;
348 /* Drawing stuff */
349 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
351 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
354 static void draw_update ( VikWindow *vw )
356 draw_redraw (vw);
357 draw_sync (vw);
360 static void draw_sync ( VikWindow *vw )
362 vik_viewport_sync(vw->viking_vvp);
363 draw_status ( vw );
364 /* other things may be necc here later. */
367 static void draw_status ( VikWindow *vw )
369 static gchar zoom_level[22];
370 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" );
371 if ( vw->current_tool == TOOL_LAYER )
372 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
373 else
374 vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
376 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
379 void vik_window_set_redraw_trigger(VikLayer *vl)
381 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
382 vw->trigger = vl;
385 static void window_configure_event ( VikWindow *vw )
387 draw_redraw ( vw );
388 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
391 static void draw_redraw ( VikWindow *vw )
393 VikCoord old_center = vw->trigger_center;
394 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
395 VikLayer *new_trigger = vw->trigger;
396 vw->trigger = NULL;
397 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
399 if ( ! new_trigger )
400 ; /* do nothing -- have to redraw everything. */
401 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
402 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
403 else
404 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
406 /* actually draw */
407 vik_viewport_clear ( vw->viking_vvp);
408 vik_layers_panel_draw_all ( vw->viking_vlp );
409 vik_viewport_draw_scale ( vw->viking_vvp );
410 vik_viewport_draw_centermark ( vw->viking_vvp );
412 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
415 gboolean draw_buf_done = TRUE;
417 static gboolean draw_buf(gpointer data)
419 gpointer *pass_along = data;
420 gdk_threads_enter();
421 gdk_draw_drawable (pass_along[0], pass_along[1],
422 pass_along[2], 0, 0, 0, 0, -1, -1);
423 draw_buf_done = TRUE;
424 gdk_threads_leave();
425 return FALSE;
429 /* Mouse event handlers ************************************************************************/
431 static void draw_click (VikWindow *vw, GdkEventButton *event)
434 /* middle button pressed. we reserve all middle button and scroll events
435 * for panning and zooming; tools only get left/right/movement
437 if ( event->button == 2) {
438 /* set panning origin */
439 vw->pan_x = (gint) event->x;
440 vw->pan_y = (gint) event->y;
442 else {
443 toolbox_click(vw->vt, event);
447 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
449 static VikCoord coord;
450 static struct UTM utm;
451 static struct LatLon ll;
452 static char pointer_buf[36];
453 gint16 alt;
455 toolbox_move(vw->vt, (GdkEventButton *)event);
457 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
458 vik_coord_to_utm ( &coord, &utm );
459 a_coords_utm_to_latlon ( &utm, &ll );
460 if ((alt = a_dems_get_elev_by_coord(&coord)) != VIK_DEM_INVALID_ELEVATION)
461 g_snprintf ( pointer_buf, 36, "Cursor: %f %f %dm", ll.lat, ll.lon, alt );
462 else
463 g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
464 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
466 if ( vw->pan_x != -1 ) {
467 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
471 static void draw_release ( VikWindow *vw, GdkEventButton *event )
473 if ( event->button == 2 ) { /* move / pan */
474 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
475 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
476 else
477 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
478 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
479 draw_update ( vw );
480 vw->pan_x = vw->pan_y = -1;
482 else {
483 toolbox_release(vw->vt, event);
487 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
489 if ( event->state == GDK_CONTROL_MASK ) {
490 /* control == pan up & down */
491 if ( event->direction == GDK_SCROLL_UP )
492 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
493 else
494 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 );
495 } else if ( event->state & GDK_SHIFT_MASK ) {
496 /* control-shift == pan left & right */
497 if ( event->direction == GDK_SCROLL_UP )
498 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
499 else
500 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 );
501 } else {
502 if ( event->direction == GDK_SCROLL_UP ) {
503 /* make sure mouse is still over the same point on the map when we zoom */
504 VikCoord coord;
505 gint x, y;
506 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
507 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
508 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
509 vik_viewport_zoom_in (vw->viking_vvp);
510 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
511 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
512 center_y + (y - event->y) );
514 else {
515 VikCoord coord;
516 gint x, y;
517 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
518 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
519 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
520 vik_viewport_zoom_out(vw->viking_vvp);
521 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
522 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
523 center_y + (y - event->y) );
527 draw_update(vw);
532 /********************************************************************************
533 ** Ruler tool code
534 ********************************************************************************/
535 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
537 PangoFontDescription *pfd;
538 PangoLayout *pl;
539 gchar str[128];
540 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
541 GdkGC *thickgc = gdk_gc_new(d);
543 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
544 gdouble dx = (x2-x1)/len*10;
545 gdouble dy = (y2-y1)/len*10;
546 gdouble c = cos(15.0 * M_PI/180.0);
547 gdouble s = sin(15.0 * M_PI/180.0);
548 gdouble angle;
549 gdouble baseangle = 0;
550 gint i;
552 /* draw line with arrow ends */
554 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
555 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
556 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
559 a_viewport_clip_line(&x1, &y1, &x2, &y2);
560 gdk_draw_line(d, gc, x1, y1, x2, y2);
562 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
563 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
564 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
565 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
566 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
567 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
569 /* draw compass */
570 #define CR 80
571 #define CW 4
572 angle = atan2(dy, dx) + M_PI_2;
574 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
575 VikCoord test;
576 struct LatLon ll;
577 struct UTM u;
578 gint tx, ty;
580 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
581 vik_coord_to_latlon ( &test, &ll );
582 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
583 a_coords_latlon_to_utm ( &ll, &u );
584 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
585 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
587 baseangle = M_PI - atan2(tx-x1, ty-y1);
588 angle -= baseangle;
591 if (angle<0)
592 angle+=2*M_PI;
593 if (angle>2*M_PI)
594 angle-=2*M_PI;
597 GdkColor color;
598 gdk_gc_copy(thickgc, gc);
599 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
600 gdk_color_parse("#2255cc", &color);
601 gdk_gc_set_rgb_fg_color(thickgc, &color);
603 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);
606 gdk_gc_copy(thickgc, gc);
607 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
608 for (i=0; i<180; i++) {
609 c = cos(i*M_PI/90.0 + baseangle);
610 s = sin(i*M_PI/90.0 + baseangle);
612 if (i%5) {
613 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
614 } else {
615 gdouble ticksize = 2*CW;
616 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
620 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
621 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
622 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
623 c = (CR+CW*2)*cos(baseangle);
624 s = (CR+CW*2)*sin(baseangle);
625 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
626 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
628 /* draw labels */
629 #define LABEL(x, y, w, h) { \
630 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
631 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
632 gdk_draw_layout(d, gc, (x), (y), pl); }
634 gint wd, hd, xd, yd;
635 gint wb, hb, xb, yb;
637 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
639 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
640 pango_layout_set_font_description (pl, pfd);
641 pango_font_description_free (pfd);
643 pango_layout_set_text(pl, "N", -1);
644 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
646 /* draw label with distance */
647 if (distance >= 1000 && distance < 100000) {
648 g_sprintf(str, "%3.2f km", distance/1000.0);
649 } else if (distance < 1000) {
650 g_sprintf(str, "%d m", (int)distance);
651 } else {
652 g_sprintf(str, "%d km", (int)distance/1000);
654 pango_layout_set_text(pl, str, -1);
656 pango_layout_get_pixel_size ( pl, &wd, &hd );
657 if (dy>0) {
658 xd = (x1+x2)/2 + dy;
659 yd = (y1+y2)/2 - hd/2 - dx;
660 } else {
661 xd = (x1+x2)/2 - dy;
662 yd = (y1+y2)/2 - hd/2 + dx;
665 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
666 xd = x2 + 10;
667 yd = y2 - 5;
670 LABEL(xd, yd, wd, hd);
672 /* draw label with bearing */
673 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
674 pango_layout_set_text(pl, str, -1);
675 pango_layout_get_pixel_size ( pl, &wb, &hb );
676 xb = x1 + CR*cos(angle-M_PI_2);
677 yb = y1 + CR*sin(angle-M_PI_2);
679 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
680 xb = x2 + 10;
681 yb = y2 + 10;
685 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
686 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
687 xb = xd + wd + 5;
690 LABEL(xb, yb, wb, hb);
692 #undef LABEL
694 g_object_unref ( G_OBJECT ( pl ) );
695 g_object_unref ( G_OBJECT ( labgc ) );
696 g_object_unref ( G_OBJECT ( thickgc ) );
699 typedef struct {
700 VikWindow *vw;
701 VikViewport *vvp;
702 gboolean has_oldcoord;
703 VikCoord oldcoord;
704 } ruler_tool_state_t;
706 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
708 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
709 s->vw = vw;
710 s->vvp = vvp;
711 s->has_oldcoord = FALSE;
712 return s;
715 static void ruler_destroy (ruler_tool_state_t *s)
717 g_free(s);
720 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
722 struct LatLon ll;
723 VikCoord coord;
724 gchar *temp;
725 if ( event->button == 1 ) {
726 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
727 vik_coord_to_latlon ( &coord, &ll );
728 if ( s->has_oldcoord ) {
729 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
730 s->has_oldcoord = FALSE;
732 else {
733 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
734 s->has_oldcoord = TRUE;
737 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
738 g_free ( temp );
740 s->oldcoord = coord;
742 else {
743 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
744 draw_update ( s->vw );
746 return VIK_LAYER_TOOL_ACK;
749 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
751 VikViewport *vvp = s->vvp;
752 VikWindow *vw = s->vw;
754 struct LatLon ll;
755 VikCoord coord;
756 gchar *temp;
758 if ( s->has_oldcoord ) {
759 int oldx, oldy, w1, h1, w2, h2;
760 static GdkPixmap *buf = NULL;
761 w1 = vik_viewport_get_width(vvp);
762 h1 = vik_viewport_get_height(vvp);
763 if (!buf) {
764 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
766 gdk_drawable_get_size(buf, &w2, &h2);
767 if (w1 != w2 || h1 != h2) {
768 g_object_unref ( G_OBJECT ( buf ) );
769 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
772 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
773 vik_coord_to_latlon ( &coord, &ll );
774 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
776 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
777 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
778 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
779 if (draw_buf_done) {
780 static gpointer pass_along[3];
781 pass_along[0] = GTK_WIDGET(vvp)->window;
782 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
783 pass_along[2] = buf;
784 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
785 draw_buf_done = FALSE;
788 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
789 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
790 g_free ( temp );
792 return VIK_LAYER_TOOL_ACK;
795 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
797 return VIK_LAYER_TOOL_ACK;
800 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
802 draw_update ( s->vw );
805 static VikToolInterface ruler_tool =
806 { "Ruler",
807 (VikToolConstructorFunc) ruler_create,
808 (VikToolDestructorFunc) ruler_destroy,
809 (VikToolActivationFunc) NULL,
810 (VikToolActivationFunc) ruler_deactivate,
811 (VikToolMouseFunc) ruler_click,
812 (VikToolMouseFunc) ruler_move,
813 (VikToolMouseFunc) ruler_release };
814 /*** end ruler code ********************************************************/
818 /********************************************************************************
819 ** Zoom tool code
820 ********************************************************************************/
821 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
823 return vw;
826 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
828 vw->modified = TRUE;
829 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
830 if ( event->button == 1 )
831 vik_viewport_zoom_in (vw->viking_vvp);
832 else if ( event->button == 3 )
833 vik_viewport_zoom_out (vw->viking_vvp);
834 draw_update ( vw );
835 return VIK_LAYER_TOOL_ACK;
838 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
840 return VIK_LAYER_TOOL_ACK;
843 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
845 return VIK_LAYER_TOOL_ACK;
848 static VikToolInterface zoom_tool =
849 { "Zoom",
850 (VikToolConstructorFunc) zoomtool_create,
851 (VikToolDestructorFunc) NULL,
852 (VikToolActivationFunc) NULL,
853 (VikToolActivationFunc) NULL,
854 (VikToolMouseFunc) zoomtool_click,
855 (VikToolMouseFunc) zoomtool_move,
856 (VikToolMouseFunc) zoomtool_release };
857 /*** end ruler code ********************************************************/
859 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
861 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
862 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
863 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
864 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
865 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
866 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
867 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
868 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
870 draw_update ( vw );
874 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
876 guint what = 128;
878 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
879 what = -3;
881 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
882 what = -4;
884 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
885 what = -2;
887 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
888 what = -1;
890 else {
891 gchar *s = (gchar *)gtk_action_get_name(a);
892 what = atoi(s+4);
895 switch (what)
897 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
898 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
899 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
900 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
901 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
903 draw_update ( vw );
906 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
908 VikCoord new_center;
910 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
911 struct LatLon ll, llold;
912 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
913 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
914 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
915 else
916 return;
918 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
919 struct UTM utm, utmold;
920 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
921 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
922 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
923 else
924 return;
926 else {
927 g_critical("Houston we have a problem\n");
928 return;
931 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
932 draw_update ( vw );
935 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
937 gint type;
938 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
939 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
940 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
941 draw_update ( vw );
942 vw->modified = TRUE;
948 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
950 a_clipboard_copy_selected ( vw->viking_vlp );
953 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
955 a_clipboard_copy_selected ( vw->viking_vlp );
956 menu_delete_layer_cb ( a, vw );
959 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
961 if ( a_clipboard_paste ( vw->viking_vlp ) )
963 draw_update ( vw );
964 vw->modified = TRUE;
968 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
970 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
971 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
974 static void help_about_cb ( GtkAction *a, VikWindow *vw )
976 a_dialog_about(GTK_WINDOW(vw));
979 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
981 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
983 vik_layers_panel_delete_selected ( vw->viking_vlp );
984 vw->modified = TRUE;
986 else
987 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
990 /***************************************
991 ** tool management routines
993 ***************************************/
995 static toolbox_tools_t* toolbox_create(VikWindow *vw)
997 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
998 vt->tools = NULL;
999 vt->n_tools = 0;
1000 vt->active_tool = -1;
1001 vt->vw = vw;
1002 if (!vw->viking_vvp) {
1003 g_critical("no viewport found.");
1004 exit(1);
1006 return vt;
1009 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1011 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1012 vt->tools[vt->n_tools].ti = *vti;
1013 vt->tools[vt->n_tools].layer_type = layer_type;
1014 if (vti->create) {
1015 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1017 else {
1018 vt->tools[vt->n_tools].state = NULL;
1020 vt->n_tools++;
1023 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1025 int i;
1026 for (i=0; i<vt->n_tools; i++) {
1027 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1028 break;
1031 return i;
1034 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1036 int tool = toolbox_get_tool(vt, tool_name);
1037 toolbox_tool_t *t = &vt->tools[tool];
1038 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1040 if (tool == vt->n_tools) {
1041 g_critical("trying to activate a non-existent tool...");
1042 exit(1);
1044 /* is the tool already active? */
1045 if (vt->active_tool == tool) {
1046 return;
1049 if (vt->active_tool != -1) {
1050 if (vt->tools[vt->active_tool].ti.deactivate) {
1051 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1054 if (t->ti.activate) {
1055 t->ti.activate(vl, t->state);
1057 vt->active_tool = tool;
1060 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1062 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1063 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1064 gint ltype = vt->tools[vt->active_tool].layer_type;
1065 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1066 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1070 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1072 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1073 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1074 gint ltype = vt->tools[vt->active_tool].layer_type;
1075 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1076 vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state);
1080 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1082 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1083 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1084 gint ltype = vt->tools[vt->active_tool].layer_type;
1085 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1086 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1089 /** End tool management ************************************/
1091 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1093 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1096 /* this function gets called whenever a toolbar tool is clicked */
1097 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1099 /* White Magic, my friends ... White Magic... */
1100 int layer_id, tool_id;
1102 toolbox_activate(vw->vt, gtk_action_get_name(a));
1104 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1105 vw->current_tool = TOOL_ZOOM;
1106 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
1108 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1109 vw->current_tool = TOOL_RULER;
1110 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
1112 else {
1113 /* TODO: only enable tools from active layer */
1114 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1115 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1116 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1117 vw->current_tool = TOOL_LAYER;
1118 vw->tool_layer_id = layer_id;
1119 vw->tool_tool_id = tool_id;
1120 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
1125 draw_status ( vw );
1128 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1130 gchar *title;
1131 if ( vw->filename )
1132 g_free ( vw->filename );
1133 if ( filename == NULL )
1135 vw->filename = NULL;
1136 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
1138 else
1140 vw->filename = g_strdup(filename);
1141 title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
1142 gtk_window_set_title ( GTK_WINDOW(vw), title );
1143 g_free ( title );
1147 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1149 GtkWidget *mode_button;
1150 gchar *buttonname;
1151 switch ( mode ) {
1152 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1153 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1154 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1155 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1156 default: buttonname = "/ui/MainMenu/View/ModeKH";
1158 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1159 g_assert ( mode_button );
1160 return mode_button;
1163 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1165 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1167 case 0:
1168 a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
1169 break;
1170 case 1:
1172 GtkWidget *mode_button;
1173 if ( change_filename )
1174 window_set_filename ( vw, filename );
1175 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1176 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. */
1177 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1178 vw->only_updating_coord_mode_ui = FALSE;
1180 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1182 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1183 g_assert ( mode_button );
1184 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1186 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1187 g_assert ( mode_button );
1188 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1190 default: draw_update ( vw );
1193 static void load_file ( GtkAction *a, VikWindow *vw )
1195 gboolean newwindow;
1196 if (!strcmp(gtk_action_get_name(a), "Open")) {
1197 newwindow = TRUE;
1199 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1200 newwindow = FALSE;
1202 else {
1203 g_critical("Houston we got a problem\n");
1204 return;
1207 if ( ! vw->open_dia )
1209 vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
1210 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1211 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1212 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1214 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1216 gtk_widget_hide ( vw->open_dia );
1217 #ifdef VIKING_PROMPT_IF_MODIFIED
1218 if ( (vw->modified || vw->filename) && newwindow )
1219 #else
1220 if ( vw->filename && newwindow )
1221 #endif
1222 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1223 else {
1224 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1225 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1226 while ( *files ) {
1227 vik_window_open_file ( vw, *files, change_fn );
1228 files++;
1232 else
1233 gtk_widget_hide ( vw->open_dia );
1236 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1238 gboolean rv = FALSE;
1239 const gchar *fn;
1240 if ( ! vw->save_dia )
1242 vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
1243 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1244 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1247 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1249 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1250 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 ) ) )
1252 window_set_filename ( vw, fn );
1253 rv = window_save ( vw );
1254 vw->modified = FALSE;
1255 break;
1258 gtk_widget_hide ( vw->save_dia );
1259 return rv;
1262 static gboolean window_save ( VikWindow *vw )
1264 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1265 return TRUE;
1266 else
1268 a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
1269 return FALSE;
1273 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1275 if ( ! vw->filename )
1276 return save_file_as ( NULL, vw );
1277 else
1279 vw->modified = FALSE;
1280 return window_save ( vw );
1284 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1286 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1289 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1291 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1294 #ifdef VIK_CONFIG_GEOCACHES
1295 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1297 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1299 #endif
1301 static void goto_address( GtkAction *a, VikWindow *vw)
1303 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1306 static void clear_cb ( GtkAction *a, VikWindow *vw )
1308 vik_layers_panel_clear ( vw->viking_vlp );
1309 window_set_filename ( vw, NULL );
1310 draw_update ( vw );
1313 static void window_close ( GtkAction *a, VikWindow *vw )
1315 if ( ! delete_event ( vw ) )
1316 gtk_widget_destroy ( GTK_WIDGET(vw) );
1319 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1321 if (save_file( NULL, vw)) {
1322 window_close( NULL, vw);
1323 return(TRUE);
1325 else
1326 return(FALSE);
1329 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1331 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1332 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1334 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1335 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1336 draw_update ( vw );
1340 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1342 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1343 GdkPixbuf *pixbuf_to_save;
1344 gdouble old_xmpp, old_ympp;
1345 GError *error = NULL;
1347 /* backup old zoom & set new */
1348 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1349 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1350 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1352 /* reset width and height: */
1353 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1355 /* draw all layers */
1356 draw_redraw ( vw );
1358 /* save buffer as file. */
1359 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);
1360 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1361 if (error)
1363 g_warning("Unable to write to file %s: %s", fn, error->message );
1364 g_error_free (error);
1366 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1368 /* pretend like nothing happened ;) */
1369 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1370 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1371 vik_viewport_configure ( vw->viking_vvp );
1372 draw_update ( vw );
1375 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 )
1377 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1378 gchar *name_of_file = g_malloc ( size );
1379 guint x = 1, y = 1;
1380 struct UTM utm_orig, utm;
1382 /* *** copied from above *** */
1383 GdkPixbuf *pixbuf_to_save;
1384 gdouble old_xmpp, old_ympp;
1385 GError *error = NULL;
1387 /* backup old zoom & set new */
1388 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1389 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1390 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1392 /* reset width and height: do this only once for all images (same size) */
1393 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1394 /* *** end copy from above *** */
1396 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1398 make_dir(fn);
1400 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1402 for ( y = 1; y <= tiles_h; y++ )
1404 for ( x = 1; x <= tiles_w; x++ )
1406 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1407 utm = utm_orig;
1408 if ( tiles_w & 0x1 )
1409 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1410 else
1411 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1412 if ( tiles_h & 0x1 ) /* odd */
1413 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1414 else /* even */
1415 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1417 /* move to correct place. */
1418 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1420 draw_redraw ( vw );
1422 /* save buffer as file. */
1423 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);
1424 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1425 if (error)
1427 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1428 g_error_free (error);
1431 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1435 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1436 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1437 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1438 vik_viewport_configure ( vw->viking_vvp );
1439 draw_update ( vw );
1441 g_free ( name_of_file );
1444 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1446 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1447 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1448 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1449 gdouble width_min, width_max, height_min, height_max;
1450 gint width, height;
1452 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1453 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1455 /* TODO: support for xzoom and yzoom values */
1456 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1457 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1459 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1460 a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
1462 gtk_spin_button_set_value ( width_spin, width );
1463 gtk_spin_button_set_value ( height_spin, height );
1466 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1468 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1469 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1470 gchar *label_text;
1471 gdouble w, h;
1472 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1473 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1474 if (pass_along[4]) /* save many images; find TOTAL area covered */
1476 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1477 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1479 label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
1480 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1481 g_free ( label_text );
1484 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1486 /* todo: default for answers inside VikWindow or static (thruout instance) */
1487 GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
1488 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1489 GTK_STOCK_CANCEL,
1490 GTK_RESPONSE_REJECT,
1491 GTK_STOCK_OK,
1492 GTK_RESPONSE_ACCEPT,
1493 NULL );
1494 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1495 GtkWidget *png_radio, *jpeg_radio;
1496 GtkWidget *current_window_button;
1497 gpointer current_window_pass_along[7];
1498 GtkWidget *zoom_label, *zoom_spin;
1499 GtkWidget *total_size_label;
1501 /* only used if (!one_image_only) */
1502 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1505 width_label = gtk_label_new ( "Width (pixels):" );
1506 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1507 height_label = gtk_label_new ( "Height (pixels):" );
1508 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1510 zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
1511 /* TODO: separate xzoom and yzoom factors */
1512 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);
1514 total_size_label = gtk_label_new ( NULL );
1516 current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
1517 current_window_pass_along [0] = vw;
1518 current_window_pass_along [1] = width_spin;
1519 current_window_pass_along [2] = height_spin;
1520 current_window_pass_along [3] = zoom_spin;
1521 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1522 current_window_pass_along [5] = NULL;
1523 current_window_pass_along [6] = total_size_label;
1524 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1526 png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
1527 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
1529 if ( ! vw->draw_image_save_as_png )
1530 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1532 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1533 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1534 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1535 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1536 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1537 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1538 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1539 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1540 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1542 if ( ! one_image_only )
1544 GtkWidget *tiles_width_label, *tiles_height_label;
1547 tiles_width_label = gtk_label_new ( "East-west image tiles:" );
1548 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1549 tiles_height_label = gtk_label_new ( "North-south image tiles:" );
1550 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1551 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1552 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1553 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1554 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1556 current_window_pass_along [4] = tiles_width_spin;
1557 current_window_pass_along [5] = tiles_height_spin;
1558 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1559 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1561 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1562 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1563 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1564 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1566 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1568 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1570 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1572 gtk_widget_hide ( GTK_WIDGET(dialog) );
1573 if ( one_image_only )
1574 save_image_file ( vw, fn,
1575 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1576 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1577 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1578 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1579 else {
1580 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1581 save_image_dir ( vw, fn,
1582 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1583 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1584 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1585 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1586 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1587 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1588 else
1589 a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
1592 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1596 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1598 const gchar *fn;
1599 if (!vw->save_img_dia) {
1600 vw->save_img_dia = gtk_file_selection_new ("Save Image");
1601 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1602 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1605 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1607 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1608 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 ) ) )
1610 draw_to_image_file ( vw, fn, TRUE );
1611 break;
1614 gtk_widget_hide ( vw->save_img_dia );
1617 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1619 const gchar *fn;
1620 if (!vw->save_img_dir_dia) {
1621 vw->save_img_dir_dia = gtk_file_selection_new ("Choose a name for a new directory to hold images");
1622 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1623 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1626 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1628 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1629 if ( access ( fn, F_OK ) == 0 )
1630 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) );
1631 else
1633 draw_to_image_file ( vw, fn, FALSE );
1634 break;
1637 gtk_widget_hide ( vw->save_img_dir_dia );
1640 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1641 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1643 VikViewportDrawMode drawmode;
1644 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1645 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1647 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1648 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1650 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1651 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1653 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1654 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1656 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1657 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1659 else {
1660 g_critical("Houston, we got a problem!\n");
1661 return;
1664 if ( !vw->only_updating_coord_mode_ui )
1666 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1667 if ( olddrawmode != drawmode )
1669 /* this takes care of coord mode too */
1670 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1671 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1672 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1673 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1674 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1676 draw_update ( vw );
1681 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1683 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1684 g_assert(check_box);
1685 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1686 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1687 draw_update ( vw );
1690 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1692 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1693 g_assert(check_box);
1694 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1695 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1696 draw_update ( vw );
1699 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1701 GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1702 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1703 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1704 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1705 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1707 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1708 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1709 draw_update ( vw );
1711 g_free ( color );
1712 gtk_widget_destroy ( colorsd );
1717 /***********************************************************************************************
1718 ** GUI Creation
1719 ***********************************************************************************************/
1721 static GtkActionEntry entries[] = {
1722 { "File", NULL, "_File", 0, 0, 0 },
1723 { "Edit", NULL, "_Edit", 0, 0, 0 },
1724 { "View", NULL, "_View", 0, 0, 0 },
1725 { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1726 { "SetPan", NULL, "_Pan", 0, 0, 0 },
1727 { "Layers", NULL, "_Layers", 0, 0, 0 },
1728 { "Tools", NULL, "_Tools", 0, 0, 0 },
1729 { "Help", NULL, "_Help", 0, 0, 0 },
1731 { "New", GTK_STOCK_NEW, "_New", "<control>N", "New file", (GCallback)newwindow_cb },
1732 { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", (GCallback)load_file },
1733 { "Append", GTK_STOCK_ADD, "A_ppend File", NULL, "Append data from a different file", (GCallback)load_file },
1734 { "Acquire", NULL, "A_cquire", 0, 0, 0 },
1735 { "AcquireGPS", NULL, "From _GPS", NULL, "Transfer data from a GPS device", (GCallback)acquire_from_gps },
1736 { "AcquireGoogle", NULL, "Google _Directions", NULL, "Get driving directions from Google", (GCallback)acquire_from_google },
1737 #ifdef VIK_CONFIG_GEOCACHES
1738 { "AcquireGC", NULL, "Geo_caches", NULL, "Get Geocaches from geocaching.com", (GCallback)acquire_from_gc },
1739 #endif
1740 { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the file", (GCallback)save_file },
1741 { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As", NULL, "Save the file under different name", (GCallback)save_file_as },
1742 { "GenImg", GTK_STOCK_CLEAR, "_Generate Image File", NULL, "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1743 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL, "FIXME:IMGDIR", (GCallback)draw_to_image_dir_cb },
1744 { "Exit", GTK_STOCK_QUIT, "E_xit", "<control>W", "Exit the program", (GCallback)window_close },
1745 { "SaveExit", GTK_STOCK_QUIT, "Save and Exit", NULL, "Save and Exit the program", (GCallback)save_file_and_exit },
1747 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, "Go To Google Maps location", NULL, "Go to address/place using Google Maps search", (GCallback)goto_address },
1748 { "GotoLL", GTK_STOCK_QUIT, "_Go to Lat\\/Lon...", NULL, "Go to arbitrary lat\\/lon coordinate", (GCallback)draw_goto_cb },
1749 { "GotoUTM", GTK_STOCK_QUIT, "Go to UTM...", NULL, "Go to arbitrary UTM coordinate", (GCallback)draw_goto_cb },
1750 { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...", NULL, NULL, (GCallback)set_bg_color },
1751 { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>plus", NULL, (GCallback)draw_zoom_cb },
1752 { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control>minus", NULL, (GCallback)draw_zoom_cb },
1753 { "ZoomTo", GTK_STOCK_ZOOM_FIT, "Zoom _To", "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1754 { "Zoom0.25", NULL, "0.25", NULL, NULL, (GCallback)draw_zoom_cb },
1755 { "Zoom0.5", NULL, "0.5", NULL, NULL, (GCallback)draw_zoom_cb },
1756 { "Zoom1", NULL, "1", NULL, NULL, (GCallback)draw_zoom_cb },
1757 { "Zoom2", NULL, "2", NULL, NULL, (GCallback)draw_zoom_cb },
1758 { "Zoom4", NULL, "4", NULL, NULL, (GCallback)draw_zoom_cb },
1759 { "Zoom8", NULL, "8", NULL, NULL, (GCallback)draw_zoom_cb },
1760 { "Zoom16", NULL, "16", NULL, NULL, (GCallback)draw_zoom_cb },
1761 { "Zoom32", NULL, "32", NULL, NULL, (GCallback)draw_zoom_cb },
1762 { "Zoom64", NULL, "64", NULL, NULL, (GCallback)draw_zoom_cb },
1763 { "Zoom128", NULL, "128", NULL, NULL, (GCallback)draw_zoom_cb },
1764 { "PanNorth", NULL, "Pan North", "<control>Up", NULL, (GCallback)draw_pan_cb },
1765 { "PanEast", NULL, "Pan East", "<control>Right", NULL, (GCallback)draw_pan_cb },
1766 { "PanSouth", NULL, "Pan South", "<control>Down", NULL, (GCallback)draw_pan_cb },
1767 { "PanWest", NULL, "Pan West", "<control>Left", NULL, (GCallback)draw_pan_cb },
1768 { "BGJobs", GTK_STOCK_EXECUTE, "Background _Jobs", NULL, NULL, (GCallback)a_background_show_window },
1770 { "Cut", GTK_STOCK_CUT, "Cu_t", NULL, NULL, (GCallback)menu_cut_layer_cb },
1771 { "Copy", GTK_STOCK_COPY, "_Copy", NULL, NULL, (GCallback)menu_copy_layer_cb },
1772 { "Paste", GTK_STOCK_PASTE, "_Paste", NULL, NULL, (GCallback)menu_paste_layer_cb },
1773 { "Delete", GTK_STOCK_DELETE, "_Delete", NULL, NULL, (GCallback)menu_delete_layer_cb },
1774 { "DeleteAll", NULL, "Delete All", NULL, NULL, (GCallback)clear_cb },
1775 { "Properties",GTK_STOCK_PROPERTIES, "_Properties", NULL, NULL, (GCallback)menu_properties_cb },
1777 { "About", GTK_STOCK_ABOUT, "_About", NULL, NULL, (GCallback)help_about_cb },
1780 /* Radio items */
1781 static GtkRadioActionEntry mode_entries[] = {
1782 { "ModeUTM", NULL, "_UTM Mode", "<control>u", NULL, 0 },
1783 { "ModeExpedia", NULL, "_Expedia Mode", "<control>e", NULL, 1 },
1784 { "ModeGoogle", NULL, "_Old Google Mode", "<control>o", NULL, 2 },
1785 { "ModeKH", NULL, "Old _KH Mode", "<control>k", NULL, 3 },
1786 { "ModeMercator", NULL, "_Google Mode", "<control>g", NULL, 4 }
1789 static GtkRadioActionEntry tool_entries[] = {
1790 { "Zoom", "vik-icon-zoom", "_Zoom", "<control><shift>Z", "Zoom Tool", 0 },
1791 { "Ruler", "vik-icon-ruler", "_Ruler", "<control><shift>R", "Ruler Tool", 1 }
1794 static GtkToggleActionEntry toggle_entries[] = {
1795 { "ShowScale", NULL, "Show Scale", NULL, NULL, (GCallback)set_draw_scale, TRUE },
1796 { "ShowCenterMark", NULL, "Show Center Mark", NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1799 #include "menu.xml.h"
1800 static void window_create_ui( VikWindow *window )
1802 GtkUIManager *uim;
1803 GtkActionGroup *action_group;
1804 GtkAccelGroup *accel_group;
1805 GError *error;
1806 guint i, j, mid;
1807 GtkIconFactory *icon_factory;
1808 GtkIconSet *icon_set;
1809 GtkRadioActionEntry *tools = NULL, *radio;
1810 guint ntools;
1812 uim = gtk_ui_manager_new ();
1813 window->uim = uim;
1815 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1816 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1818 error = NULL;
1819 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1820 g_error_free (error);
1821 exit (1);
1824 action_group = gtk_action_group_new ("MenuActions");
1825 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1826 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1827 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1829 icon_factory = gtk_icon_factory_new ();
1830 gtk_icon_factory_add_default (icon_factory);
1832 register_vik_icons(icon_factory);
1834 ntools = 0;
1835 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1836 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1837 radio = &tools[ntools];
1838 ntools++;
1839 *radio = tool_entries[i];
1840 radio->value = ntools;
1843 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1844 GtkActionEntry action;
1845 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1846 vik_layer_get_interface(i)->name,
1847 vik_layer_get_interface(i)->name,
1848 GTK_UI_MANAGER_MENUITEM, FALSE);
1850 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1851 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1852 gtk_icon_set_unref (icon_set);
1854 action.name = vik_layer_get_interface(i)->name;
1855 action.stock_id = vik_layer_get_interface(i)->name;
1856 action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1857 action.accelerator = NULL;
1858 action.tooltip = NULL;
1859 action.callback = (GCallback)menu_addlayer_cb;
1860 gtk_action_group_add_actions(action_group, &action, 1, window);
1862 if ( vik_layer_get_interface(i)->tools_count ) {
1863 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1864 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1867 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1868 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1869 radio = &tools[ntools];
1870 ntools++;
1872 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
1873 vik_layer_get_interface(i)->tools[j].name,
1874 vik_layer_get_interface(i)->tools[j].name,
1875 GTK_UI_MANAGER_MENUITEM, FALSE);
1876 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
1877 vik_layer_get_interface(i)->tools[j].name,
1878 vik_layer_get_interface(i)->tools[j].name,
1879 GTK_UI_MANAGER_TOOLITEM, FALSE);
1881 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
1883 radio->name = vik_layer_get_interface(i)->tools[j].name;
1884 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1885 radio->label = vik_layer_get_interface(i)->tools[j].name;
1886 radio->accelerator = NULL;
1887 radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1888 radio->value = ntools;
1891 g_object_unref (icon_factory);
1893 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1894 g_free(tools);
1896 gtk_ui_manager_insert_action_group (uim, action_group, 0);
1898 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1899 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1900 GtkAction *action = gtk_action_group_get_action(action_group,
1901 vik_layer_get_interface(i)->tools[j].name);
1902 g_object_set(action, "sensitive", FALSE, NULL);
1905 window->action_group = action_group;
1907 accel_group = gtk_ui_manager_get_accel_group (uim);
1908 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1909 gtk_ui_manager_ensure_update (uim);
1914 #include "icons/icons.h"
1915 static struct {
1916 const GdkPixdata *data;
1917 gchar *stock_id;
1918 } stock_icons[] = {
1919 { &addtr_18, "Create Track" },
1920 { &begintr_18, "Begin Track" },
1921 { &edtr_18, "Edit Trackpoint" },
1922 { &addwp_18, "Create Waypoint" },
1923 { &edwp_18, "Edit Waypoint" },
1924 { &iscissors_18, "Magic Scissors" },
1925 { &zoom_18, "vik-icon-zoom" },
1926 { &ruler_18, "vik-icon-ruler" },
1927 { &geozoom_18, "Georef Zoom Tool" },
1928 { &geomove_18, "Georef Move Map" },
1929 { &mapdl_18, "Maps Download" },
1930 { &demdl_18, "DEM Download/Import" },
1931 { &showpic_18, "Show Picture" },
1934 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1936 static void
1937 register_vik_icons (GtkIconFactory *icon_factory)
1939 GtkIconSet *icon_set;
1940 gint i;
1942 for (i = 0; i < n_stock_icons; i++) {
1943 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
1944 stock_icons[i].data, FALSE, NULL ));
1945 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1946 gtk_icon_set_unref (icon_set);
1950 void vik_window_cursors_init()
1952 GdkPixbuf *cursor_pixbuf;
1953 GError *cursor_load_err;
1955 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
1956 vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
1958 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1960 cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
1961 vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
1963 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1966 void vik_window_cursors_uninit()
1968 gdk_cursor_unref ( vw_cursor_zoom );
1969 gdk_cursor_unref ( vw_cursor_ruler );