Fix crashes on using wrong tools
[viking/gosmore.git] / src / vikwindow.c
blob03a061bdf0983bdcbdd130ee6e7a92583ae314b8
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 draw_sync ( VikWindow *vw );
66 static void draw_redraw ( VikWindow *vw );
67 static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
68 static void draw_click ( VikWindow *vw, GdkEventButton *event );
69 static void draw_release ( VikWindow *vw, GdkEventButton *event );
70 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
71 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
72 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
74 static void draw_status ();
76 /* End Drawing Functions */
78 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
79 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
80 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
82 /* tool management */
83 typedef struct {
84 VikToolInterface ti;
85 gpointer state;
86 gint layer_type;
87 } toolbox_tool_t;
88 #define TOOL_LAYER_TYPE_NONE -1
90 typedef struct {
91 int active_tool;
92 int n_tools;
93 toolbox_tool_t *tools;
94 VikWindow *vw;
95 } toolbox_tools_t;
97 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
98 static toolbox_tools_t* toolbox_create(VikWindow *vw);
99 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
100 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
101 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
102 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
103 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
104 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
107 /* ui creation */
108 static void window_create_ui( VikWindow *window );
109 static void register_vik_icons (GtkIconFactory *icon_factory);
111 /* i/o */
112 static void load_file ( GtkAction *a, VikWindow *vw );
113 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
114 static gboolean save_file ( GtkAction *a, VikWindow *vw );
115 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
116 static gboolean window_save ( VikWindow *vw );
118 struct _VikWindow {
119 GtkWindow gtkwindow;
120 VikViewport *viking_vvp;
121 VikLayersPanel *viking_vlp;
122 VikStatusbar *viking_vs;
124 GtkToolbar *toolbar;
126 GtkItemFactory *item_factory;
128 /* tool management state */
129 guint current_tool;
130 toolbox_tools_t *vt;
131 guint16 tool_layer_id;
132 guint16 tool_tool_id;
134 GtkActionGroup *action_group;
136 gint pan_x, pan_y;
138 guint draw_image_width, draw_image_height;
139 gboolean draw_image_save_as_png;
141 gchar *filename;
142 gboolean modified;
144 GtkWidget *open_dia, *save_dia;
145 GtkWidget *save_img_dia, *save_img_dir_dia;
147 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
148 GtkUIManager *uim;
151 enum {
152 TOOL_ZOOM = 0,
153 TOOL_RULER,
154 TOOL_LAYER,
155 NUMBER_OF_TOOLS
158 enum {
159 VW_NEWWINDOW_SIGNAL,
160 VW_OPENWINDOW_SIGNAL,
161 VW_LAST_SIGNAL
164 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
166 static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" };
168 GType vik_window_get_type (void)
170 static GType vw_type = 0;
172 if (!vw_type)
174 static const GTypeInfo vw_info =
176 sizeof (VikWindowClass),
177 NULL, /* base_init */
178 NULL, /* base_finalize */
179 (GClassInitFunc) window_class_init, /* class_init */
180 NULL, /* class_finalize */
181 NULL, /* class_data */
182 sizeof (VikWindow),
184 (GInstanceInitFunc) window_init,
186 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
189 return vw_type;
192 VikViewport * vik_window_viewport(VikWindow *vw)
194 return(vw->viking_vvp);
197 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
199 int i, j, tool_count;
200 VikLayerInterface *layer_interface;
202 if (!vw->action_group) return;
204 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
205 GtkAction *action;
206 layer_interface = vik_layer_get_interface(i);
207 tool_count = layer_interface->tools_count;
209 for (j = 0; j < tool_count; j++) {
210 action = gtk_action_group_get_action(vw->action_group,
211 layer_interface->tools[j].name);
212 g_object_set(action, "sensitive", i == vl->type, NULL);
217 static void window_finalize ( GObject *gob )
219 VikWindow *vw = VIK_WINDOW(gob);
220 g_return_if_fail ( vw != NULL );
222 a_background_remove_status ( vw->viking_vs );
224 G_OBJECT_CLASS(parent_class)->finalize(gob);
227 static void window_class_init ( VikWindowClass *klass )
229 /* destructor */
230 GObjectClass *object_class;
232 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);
233 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);
235 object_class = G_OBJECT_CLASS (klass);
237 object_class->finalize = window_finalize;
239 parent_class = g_type_class_peek_parent (klass);
243 static void window_init ( VikWindow *vw )
245 GtkWidget *main_vbox;
246 GtkWidget *hpaned;
248 vw->action_group = NULL;
250 vw->viking_vvp = vik_viewport_new();
251 vw->viking_vlp = vik_layers_panel_new();
252 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
253 vw->viking_vs = vik_statusbar_new();
255 vw->vt = toolbox_create(vw);
256 window_create_ui(vw);
257 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
259 toolbox_activate(vw->vt, "Zoom");
261 vw->filename = NULL;
262 vw->item_factory = NULL;
264 vw->modified = FALSE;
265 vw->only_updating_coord_mode_ui = FALSE;
268 vw->pan_x = vw->pan_y = -1;
269 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
270 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
271 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
273 main_vbox = gtk_vbox_new(FALSE, 1);
274 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
276 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
277 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
278 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
279 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
281 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
283 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
284 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw);
285 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
286 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
287 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
288 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
289 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
290 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
292 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
294 hpaned = gtk_hpaned_new ();
295 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
296 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
298 /* This packs the button into the window (a gtk container). */
299 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
301 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
303 a_background_add_status(vw->viking_vs);
305 vw->open_dia = NULL;
306 vw->save_dia = NULL;
307 vw->save_img_dia = NULL;
308 vw->save_img_dir_dia = NULL;
311 VikWindow *vik_window_new ()
313 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
316 static gboolean delete_event( VikWindow *vw )
318 #ifdef VIKING_PROMPT_IF_MODIFIED
319 if ( vw->modified )
320 #else
321 if (0)
322 #endif
324 GtkDialog *dia;
325 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
326 "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.",
327 vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) );
328 gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
329 switch ( gtk_dialog_run ( dia ) )
331 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
332 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
333 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
336 return FALSE;
339 /* Drawing stuff */
340 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
342 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
345 static void draw_update ( VikWindow *vw )
347 draw_redraw (vw);
348 draw_sync (vw);
351 static void draw_sync ( VikWindow *vw )
353 vik_viewport_sync(vw->viking_vvp);
354 draw_status ( vw );
355 /* other things may be necc here later. */
358 static void draw_status ( VikWindow *vw )
360 static gchar zoom_level[22];
361 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" );
362 if ( vw->current_tool == TOOL_LAYER )
363 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
364 else
365 vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] );
367 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
370 static void draw_redraw ( VikWindow *vw )
372 vik_viewport_clear ( vw->viking_vvp);
373 vik_layers_panel_draw_all ( vw->viking_vlp );
374 vik_viewport_draw_scale ( vw->viking_vvp );
375 vik_viewport_draw_centermark ( vw->viking_vvp );
378 gboolean draw_buf_done = TRUE;
380 static gboolean draw_buf(gpointer data)
382 gpointer *pass_along = data;
383 gdk_threads_enter();
384 gdk_draw_drawable (pass_along[0], pass_along[1],
385 pass_along[2], 0, 0, 0, 0, -1, -1);
386 draw_buf_done = TRUE;
387 gdk_threads_leave();
388 return FALSE;
392 /* Mouse event handlers ************************************************************************/
394 static void draw_click (VikWindow *vw, GdkEventButton *event)
397 /* middle button pressed. we reserve all middle button and scroll events
398 * for panning and zooming; tools only get left/right/movement
400 if ( event->button == 2) {
401 /* set panning origin */
402 vw->pan_x = (gint) event->x;
403 vw->pan_y = (gint) event->y;
405 else {
406 toolbox_click(vw->vt, event);
410 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
412 static VikCoord coord;
413 static struct UTM utm;
414 static struct LatLon ll;
415 static char pointer_buf[36];
416 gint16 alt;
418 toolbox_move(vw->vt, (GdkEventButton *)event);
420 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
421 vik_coord_to_utm ( &coord, &utm );
422 a_coords_utm_to_latlon ( &utm, &ll );
423 if ((alt = a_dems_get_elev_by_coord(&coord)) != VIK_DEM_INVALID_ELEVATION)
424 g_snprintf ( pointer_buf, 36, "Cursor: %f %f %dm", ll.lat, ll.lon, alt );
425 else
426 g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon );
427 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
429 if ( vw->pan_x != -1 ) {
430 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
434 static void draw_release ( VikWindow *vw, GdkEventButton *event )
436 if ( event->button == 2 ) { /* move / pan */
437 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
438 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
439 else
440 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
441 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
442 draw_update ( vw );
443 vw->pan_x = vw->pan_y = -1;
445 else {
446 toolbox_release(vw->vt, event);
450 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
452 if ( event->state == 4 ) {
453 /* control == pan up & down */
454 if ( event->direction == GDK_SCROLL_UP )
455 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
456 else
457 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 );
458 } else if ( event->state == 5 ) {
459 /* control-shift == pan left & right */
460 if ( event->direction == GDK_SCROLL_UP )
461 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
462 else
463 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 );
464 } else {
465 if ( event->direction == GDK_SCROLL_UP )
466 vik_viewport_zoom_in (vw->viking_vvp);
467 else
468 vik_viewport_zoom_out(vw->viking_vvp);
471 draw_update(vw);
476 /********************************************************************************
477 ** Ruler tool code
478 ********************************************************************************/
479 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
481 PangoFontDescription *pfd;
482 PangoLayout *pl;
483 gchar str[128];
484 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
485 GdkGC *thickgc = gdk_gc_new(d);
487 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
488 gdouble dx = (x2-x1)/len*10;
489 gdouble dy = (y2-y1)/len*10;
490 gdouble c = cos(15.0 * M_PI/180.0);
491 gdouble s = sin(15.0 * M_PI/180.0);
492 gdouble angle;
493 gdouble baseangle = 0;
494 gint i;
496 /* draw line with arrow ends */
498 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
499 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
500 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
503 a_viewport_clip_line(&x1, &y1, &x2, &y2);
504 gdk_draw_line(d, gc, x1, y1, x2, y2);
506 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
507 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
508 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
509 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
510 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
511 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
513 /* draw compass */
514 #define CR 80
515 #define CW 4
516 angle = atan2(dy, dx) + M_PI_2;
518 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
519 VikCoord test;
520 struct LatLon ll;
521 struct UTM u;
522 gint tx, ty;
524 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
525 vik_coord_to_latlon ( &test, &ll );
526 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
527 a_coords_latlon_to_utm ( &ll, &u );
528 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
529 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
531 baseangle = M_PI - atan2(tx-x1, ty-y1);
532 angle -= baseangle;
535 if (angle<0)
536 angle+=2*M_PI;
537 if (angle>2*M_PI)
538 angle-=2*M_PI;
541 GdkColor color;
542 gdk_gc_copy(thickgc, gc);
543 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
544 gdk_color_parse("#2255cc", &color);
545 gdk_gc_set_rgb_fg_color(thickgc, &color);
547 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);
550 gdk_gc_copy(thickgc, gc);
551 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
552 for (i=0; i<180; i++) {
553 c = cos(i*M_PI/90.0 + baseangle);
554 s = sin(i*M_PI/90.0 + baseangle);
556 if (i%5) {
557 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
558 } else {
559 gdouble ticksize = 2*CW;
560 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
564 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
565 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
566 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
567 c = (CR+CW*2)*cos(baseangle);
568 s = (CR+CW*2)*sin(baseangle);
569 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
570 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
572 /* draw labels */
573 #define LABEL(x, y, w, h) { \
574 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
575 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
576 gdk_draw_layout(d, gc, (x), (y), pl); }
578 gint wd, hd, xd, yd;
579 gint wb, hb, xb, yb;
581 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
583 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
584 pango_layout_set_font_description (pl, pfd);
585 pango_font_description_free (pfd);
587 pango_layout_set_text(pl, "N", -1);
588 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
590 /* draw label with distance */
591 if (distance >= 1000 && distance < 100000) {
592 g_sprintf(str, "%3.2f km", distance/1000.0);
593 } else if (distance < 1000) {
594 g_sprintf(str, "%d m", (int)distance);
595 } else {
596 g_sprintf(str, "%d km", (int)distance/1000);
598 pango_layout_set_text(pl, str, -1);
600 pango_layout_get_pixel_size ( pl, &wd, &hd );
601 if (dy>0) {
602 xd = (x1+x2)/2 + dy;
603 yd = (y1+y2)/2 - hd/2 - dx;
604 } else {
605 xd = (x1+x2)/2 - dy;
606 yd = (y1+y2)/2 - hd/2 + dx;
609 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
610 xd = x2 + 10;
611 yd = y2 - 5;
614 LABEL(xd, yd, wd, hd);
616 /* draw label with bearing */
617 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
618 pango_layout_set_text(pl, str, -1);
619 pango_layout_get_pixel_size ( pl, &wb, &hb );
620 xb = x1 + CR*cos(angle-M_PI_2);
621 yb = y1 + CR*sin(angle-M_PI_2);
623 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
624 xb = x2 + 10;
625 yb = y2 + 10;
629 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
630 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
631 xb = xd + wd + 5;
634 LABEL(xb, yb, wb, hb);
636 #undef LABEL
638 g_object_unref ( G_OBJECT ( pl ) );
639 g_object_unref ( G_OBJECT ( labgc ) );
640 g_object_unref ( G_OBJECT ( thickgc ) );
643 typedef struct {
644 VikWindow *vw;
645 VikViewport *vvp;
646 gboolean has_oldcoord;
647 VikCoord oldcoord;
648 } ruler_tool_state_t;
650 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
652 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
653 s->vw = vw;
654 s->vvp = vvp;
655 s->has_oldcoord = FALSE;
656 return s;
659 static void ruler_destroy (ruler_tool_state_t *s)
661 g_free(s);
664 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
666 struct LatLon ll;
667 VikCoord coord;
668 gchar *temp;
669 if ( event->button == 1 ) {
670 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
671 vik_coord_to_latlon ( &coord, &ll );
672 if ( s->has_oldcoord ) {
673 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
674 s->has_oldcoord = FALSE;
676 else {
677 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
678 s->has_oldcoord = TRUE;
681 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
682 g_free ( temp );
684 s->oldcoord = coord;
686 else {
687 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
688 draw_update ( s->vw );
690 return VIK_LAYER_TOOL_ACK;
693 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
695 VikViewport *vvp = s->vvp;
696 VikWindow *vw = s->vw;
698 struct LatLon ll;
699 VikCoord coord;
700 gchar *temp;
702 if ( s->has_oldcoord ) {
703 int oldx, oldy, w1, h1, w2, h2;
704 static GdkPixmap *buf = NULL;
705 w1 = vik_viewport_get_width(vvp);
706 h1 = vik_viewport_get_height(vvp);
707 if (!buf) {
708 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
710 gdk_drawable_get_size(buf, &w2, &h2);
711 if (w1 != w2 || h1 != h2) {
712 g_object_unref ( G_OBJECT ( buf ) );
713 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
716 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
717 vik_coord_to_latlon ( &coord, &ll );
718 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
720 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
721 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
722 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
723 if (draw_buf_done) {
724 static gpointer pass_along[3];
725 pass_along[0] = GTK_WIDGET(vvp)->window;
726 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
727 pass_along[2] = buf;
728 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
729 draw_buf_done = FALSE;
732 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
733 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
734 g_free ( temp );
736 return VIK_LAYER_TOOL_ACK;
739 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
741 return VIK_LAYER_TOOL_ACK;
744 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
746 draw_update ( s->vw );
749 static VikToolInterface ruler_tool =
750 { "Ruler",
751 (VikToolConstructorFunc) ruler_create,
752 (VikToolDestructorFunc) ruler_destroy,
753 (VikToolActivationFunc) NULL,
754 (VikToolActivationFunc) ruler_deactivate,
755 (VikToolMouseFunc) ruler_click,
756 (VikToolMouseFunc) ruler_move,
757 (VikToolMouseFunc) ruler_release };
758 /*** end ruler code ********************************************************/
762 /********************************************************************************
763 ** Zoom tool code
764 ********************************************************************************/
765 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
767 return vw;
770 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
772 vw->modified = TRUE;
773 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
774 if ( event->button == 1 )
775 vik_viewport_zoom_in (vw->viking_vvp);
776 else if ( event->button == 3 )
777 vik_viewport_zoom_out (vw->viking_vvp);
778 draw_update ( vw );
779 return VIK_LAYER_TOOL_ACK;
782 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
784 return VIK_LAYER_TOOL_ACK;
787 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
789 return VIK_LAYER_TOOL_ACK;
792 static VikToolInterface zoom_tool =
793 { "Zoom",
794 (VikToolConstructorFunc) zoomtool_create,
795 (VikToolDestructorFunc) NULL,
796 (VikToolActivationFunc) NULL,
797 (VikToolActivationFunc) NULL,
798 (VikToolMouseFunc) zoomtool_click,
799 (VikToolMouseFunc) zoomtool_move,
800 (VikToolMouseFunc) zoomtool_release };
801 /*** end ruler code ********************************************************/
803 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
805 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
806 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
807 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
808 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
809 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
810 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
811 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
812 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
814 draw_update ( vw );
818 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
820 guint what = 128;
822 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
823 what = -3;
825 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
826 what = -4;
828 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
829 what = -2;
831 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
832 what = -1;
834 else {
835 gchar *s = (gchar *)gtk_action_get_name(a);
836 what = atoi(s+4);
839 switch (what)
841 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
842 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
843 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
844 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
845 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
847 draw_update ( vw );
850 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
852 VikCoord new_center;
854 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
855 struct LatLon ll, llold;
856 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
857 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
858 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
859 else
860 return;
862 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
863 struct UTM utm, utmold;
864 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
865 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
866 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
867 else
868 return;
870 else {
871 g_critical("Houston we have a problem\n");
872 return;
875 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
876 draw_update ( vw );
879 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
881 gint type;
882 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
883 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
884 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
885 draw_update ( vw );
886 vw->modified = TRUE;
892 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
894 a_clipboard_copy_selected ( vw->viking_vlp );
897 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
899 a_clipboard_copy_selected ( vw->viking_vlp );
900 menu_delete_layer_cb ( a, vw );
903 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
905 if ( a_clipboard_paste ( vw->viking_vlp ) )
907 draw_update ( vw );
908 vw->modified = TRUE;
912 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
914 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
915 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." );
918 static void help_about_cb ( GtkAction *a, VikWindow *vw )
920 a_dialog_about(GTK_WINDOW(vw));
923 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
925 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
927 vik_layers_panel_delete_selected ( vw->viking_vlp );
928 vw->modified = TRUE;
930 else
931 a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." );
934 /***************************************
935 ** tool management routines
937 ***************************************/
939 static toolbox_tools_t* toolbox_create(VikWindow *vw)
941 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
942 vt->tools = NULL;
943 vt->n_tools = 0;
944 vt->active_tool = -1;
945 vt->vw = vw;
946 if (!vw->viking_vvp) {
947 g_critical("no viewport found.");
948 exit(1);
950 return vt;
953 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
955 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
956 vt->tools[vt->n_tools].ti = *vti;
957 vt->tools[vt->n_tools].layer_type = layer_type;
958 if (vti->create) {
959 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
961 else {
962 vt->tools[vt->n_tools].state = NULL;
964 vt->n_tools++;
967 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
969 int i;
970 for (i=0; i<vt->n_tools; i++) {
971 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
972 break;
975 return i;
978 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
980 int tool = toolbox_get_tool(vt, tool_name);
981 toolbox_tool_t *t = &vt->tools[tool];
982 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
984 if (tool == vt->n_tools) {
985 g_critical("trying to activate a non-existent tool...");
986 exit(1);
988 /* is the tool already active? */
989 if (vt->active_tool == tool) {
990 return;
993 if (vt->active_tool != -1) {
994 if (vt->tools[vt->active_tool].ti.deactivate) {
995 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
998 if (t->ti.activate) {
999 t->ti.activate(vl, t->state);
1001 vt->active_tool = tool;
1004 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1006 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1007 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1008 gint ltype = vt->tools[vt->active_tool].layer_type;
1009 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1010 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1014 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1016 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1017 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1018 gint ltype = vt->tools[vt->active_tool].layer_type;
1019 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1020 vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state);
1024 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1026 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1027 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1028 gint ltype = vt->tools[vt->active_tool].layer_type;
1029 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1030 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1033 /** End tool management ************************************/
1037 /* this function gets called whenever a toolbar tool is clicked */
1038 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1040 /* White Magic, my friends ... White Magic... */
1041 int i, j;
1043 toolbox_activate(vw->vt, gtk_action_get_name(a));
1045 if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1046 vw->current_tool = TOOL_ZOOM;
1048 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1049 vw->current_tool = TOOL_RULER;
1051 else {
1052 /* TODO: only enable tools from active layer */
1053 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1054 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1055 if (!strcmp(vik_layer_get_interface(i)->tools[j].name, gtk_action_get_name(a))) {
1056 vw->current_tool = TOOL_LAYER;
1057 vw->tool_layer_id = i;
1058 vw->tool_tool_id = j;
1063 draw_status ( vw );
1066 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1068 gchar *title;
1069 if ( vw->filename )
1070 g_free ( vw->filename );
1071 if ( filename == NULL )
1073 vw->filename = NULL;
1074 gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE );
1076 else
1078 vw->filename = g_strdup(filename);
1079 title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL );
1080 gtk_window_set_title ( GTK_WINDOW(vw), title );
1081 g_free ( title );
1085 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1087 GtkWidget *mode_button;
1088 gchar *buttonname;
1089 switch ( mode ) {
1090 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1091 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1092 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1093 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1094 default: buttonname = "/ui/MainMenu/View/ModeKH";
1096 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1097 g_assert ( mode_button );
1098 return mode_button;
1101 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1103 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1105 case 0:
1106 a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." );
1107 break;
1108 case 1:
1110 GtkWidget *mode_button;
1111 if ( change_filename )
1112 window_set_filename ( vw, filename );
1113 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1114 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. */
1115 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1116 vw->only_updating_coord_mode_ui = FALSE;
1118 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1120 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1121 g_assert ( mode_button );
1122 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1124 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1125 g_assert ( mode_button );
1126 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1128 default: draw_update ( vw );
1131 static void load_file ( GtkAction *a, VikWindow *vw )
1133 gboolean newwindow;
1134 if (!strcmp(gtk_action_get_name(a), "Open")) {
1135 newwindow = TRUE;
1137 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1138 newwindow = FALSE;
1140 else {
1141 g_critical("Houston we got a problem\n");
1142 return;
1145 if ( ! vw->open_dia )
1147 vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " );
1148 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1149 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1150 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1152 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1154 gtk_widget_hide ( vw->open_dia );
1155 #ifdef VIKING_PROMPT_IF_MODIFIED
1156 if ( (vw->modified || vw->filename) && newwindow )
1157 #else
1158 if ( vw->filename && newwindow )
1159 #endif
1160 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1161 else {
1162 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1163 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1164 while ( *files ) {
1165 vik_window_open_file ( vw, *files, change_fn );
1166 files++;
1170 else
1171 gtk_widget_hide ( vw->open_dia );
1174 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1176 gboolean rv = FALSE;
1177 const gchar *fn;
1178 if ( ! vw->save_dia )
1180 vw->save_dia = gtk_file_selection_new ("Save as Viking File. " );
1181 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1182 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1185 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1187 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1188 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 ) ) )
1190 window_set_filename ( vw, fn );
1191 rv = window_save ( vw );
1192 vw->modified = FALSE;
1193 break;
1196 gtk_widget_hide ( vw->save_dia );
1197 return rv;
1200 static gboolean window_save ( VikWindow *vw )
1202 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1203 return TRUE;
1204 else
1206 a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." );
1207 return FALSE;
1211 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1213 if ( ! vw->filename )
1214 return save_file_as ( NULL, vw );
1215 else
1217 vw->modified = FALSE;
1218 return window_save ( vw );
1222 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1224 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1227 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1229 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1232 #ifdef VIK_CONFIG_GEOCACHES
1233 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1235 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1237 #endif
1239 static void goto_address( GtkAction *a, VikWindow *vw)
1241 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1244 static void clear_cb ( GtkAction *a, VikWindow *vw )
1246 vik_layers_panel_clear ( vw->viking_vlp );
1247 window_set_filename ( vw, NULL );
1248 draw_update ( vw );
1251 static void window_close ( GtkAction *a, VikWindow *vw )
1253 if ( ! delete_event ( vw ) )
1254 gtk_widget_destroy ( GTK_WIDGET(vw) );
1257 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1259 if (save_file( NULL, vw)) {
1260 window_close( NULL, vw);
1261 return(TRUE);
1263 else
1264 return(FALSE);
1267 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1269 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1270 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1272 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1273 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1274 draw_update ( vw );
1278 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1280 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1281 GdkPixbuf *pixbuf_to_save;
1282 gdouble old_xmpp, old_ympp;
1283 GError *error = NULL;
1285 /* backup old zoom & set new */
1286 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1287 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1288 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1290 /* reset width and height: */
1291 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1293 /* draw all layers */
1294 draw_redraw ( vw );
1296 /* save buffer as file. */
1297 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);
1298 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1299 if (error)
1301 g_warning("Unable to write to file %s: %s", fn, error->message );
1302 g_error_free (error);
1304 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1306 /* pretend like nothing happened ;) */
1307 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1308 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1309 vik_viewport_configure ( vw->viking_vvp );
1310 draw_update ( vw );
1313 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 )
1315 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1316 gchar *name_of_file = g_malloc ( size );
1317 guint x = 1, y = 1;
1318 struct UTM utm_orig, utm;
1320 /* *** copied from above *** */
1321 GdkPixbuf *pixbuf_to_save;
1322 gdouble old_xmpp, old_ympp;
1323 GError *error = NULL;
1325 /* backup old zoom & set new */
1326 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1327 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1328 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1330 /* reset width and height: do this only once for all images (same size) */
1331 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1332 /* *** end copy from above *** */
1334 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1336 make_dir(fn);
1338 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1340 for ( y = 1; y <= tiles_h; y++ )
1342 for ( x = 1; x <= tiles_w; x++ )
1344 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1345 utm = utm_orig;
1346 if ( tiles_w & 0x1 )
1347 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1348 else
1349 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1350 if ( tiles_h & 0x1 ) /* odd */
1351 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1352 else /* even */
1353 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1355 /* move to correct place. */
1356 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1358 draw_redraw ( vw );
1360 /* save buffer as file. */
1361 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);
1362 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1363 if (error)
1365 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1366 g_error_free (error);
1369 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1373 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1374 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1375 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1376 vik_viewport_configure ( vw->viking_vvp );
1377 draw_update ( vw );
1379 g_free ( name_of_file );
1382 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1384 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1385 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1386 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1387 gdouble width_min, width_max, height_min, height_max;
1388 gint width, height;
1390 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1391 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1393 /* TODO: support for xzoom and yzoom values */
1394 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1395 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1397 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1398 a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." );
1400 gtk_spin_button_set_value ( width_spin, width );
1401 gtk_spin_button_set_value ( height_spin, height );
1404 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1406 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1407 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1408 gchar *label_text;
1409 gdouble w, h;
1410 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1411 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1412 if (pass_along[4]) /* save many images; find TOTAL area covered */
1414 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1415 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1417 label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000));
1418 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1419 g_free ( label_text );
1422 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1424 /* todo: default for answers inside VikWindow or static (thruout instance) */
1425 GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw),
1426 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1427 GTK_STOCK_CANCEL,
1428 GTK_RESPONSE_REJECT,
1429 GTK_STOCK_OK,
1430 GTK_RESPONSE_ACCEPT,
1431 NULL );
1432 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1433 GtkWidget *png_radio, *jpeg_radio;
1434 GtkWidget *current_window_button;
1435 gpointer current_window_pass_along[7];
1436 GtkWidget *zoom_label, *zoom_spin;
1437 GtkWidget *total_size_label;
1439 /* only used if (!one_image_only) */
1440 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1443 width_label = gtk_label_new ( "Width (pixels):" );
1444 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1445 height_label = gtk_label_new ( "Height (pixels):" );
1446 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1448 zoom_label = gtk_label_new ( "Zoom (meters per pixel):" );
1449 /* TODO: separate xzoom and yzoom factors */
1450 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);
1452 total_size_label = gtk_label_new ( NULL );
1454 current_window_button = gtk_button_new_with_label ( "Area in current viewable window" );
1455 current_window_pass_along [0] = vw;
1456 current_window_pass_along [1] = width_spin;
1457 current_window_pass_along [2] = height_spin;
1458 current_window_pass_along [3] = zoom_spin;
1459 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1460 current_window_pass_along [5] = NULL;
1461 current_window_pass_along [6] = total_size_label;
1462 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1464 png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" );
1465 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" );
1467 if ( ! vw->draw_image_save_as_png )
1468 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1470 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1471 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1472 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1473 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1474 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1475 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1476 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1477 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1478 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1480 if ( ! one_image_only )
1482 GtkWidget *tiles_width_label, *tiles_height_label;
1485 tiles_width_label = gtk_label_new ( "East-west image tiles:" );
1486 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1487 tiles_height_label = gtk_label_new ( "North-south image tiles:" );
1488 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1489 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1490 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1491 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1492 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1494 current_window_pass_along [4] = tiles_width_spin;
1495 current_window_pass_along [5] = tiles_height_spin;
1496 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1497 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1499 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1500 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1501 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1502 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1504 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1506 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1508 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1510 gtk_widget_hide ( GTK_WIDGET(dialog) );
1511 if ( one_image_only )
1512 save_image_file ( vw, fn,
1513 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1514 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1515 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1516 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1517 else {
1518 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1519 save_image_dir ( vw, fn,
1520 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1521 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1522 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1523 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1524 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1525 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1526 else
1527 a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" );
1530 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1534 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1536 const gchar *fn;
1537 if (!vw->save_img_dia) {
1538 vw->save_img_dia = gtk_file_selection_new ("Save Image");
1539 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1540 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1543 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1545 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1546 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 ) ) )
1548 draw_to_image_file ( vw, fn, TRUE );
1549 break;
1552 gtk_widget_hide ( vw->save_img_dia );
1555 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1557 const gchar *fn;
1558 if (!vw->save_img_dir_dia) {
1559 vw->save_img_dir_dia = gtk_file_selection_new ("Choose a name for a new directory to hold images");
1560 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1561 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1564 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1566 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1567 if ( access ( fn, F_OK ) == 0 )
1568 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) );
1569 else
1571 draw_to_image_file ( vw, fn, FALSE );
1572 break;
1575 gtk_widget_hide ( vw->save_img_dir_dia );
1578 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1579 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1581 VikViewportDrawMode drawmode;
1582 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1583 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1585 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1586 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1588 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1589 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1591 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1592 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1594 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1595 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1597 else {
1598 g_critical("Houston, we got a problem!\n");
1599 return;
1602 if ( !vw->only_updating_coord_mode_ui )
1604 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1605 if ( olddrawmode != drawmode )
1607 /* this takes care of coord mode too */
1608 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1609 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1610 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1611 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1612 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1614 draw_update ( vw );
1619 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1621 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1622 g_assert(check_box);
1623 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1624 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1625 draw_update ( vw );
1628 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1630 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1631 g_assert(check_box);
1632 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1633 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1634 draw_update ( vw );
1637 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1639 GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color");
1640 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1641 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1642 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1643 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1645 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1646 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1647 draw_update ( vw );
1649 g_free ( color );
1650 gtk_widget_destroy ( colorsd );
1655 /***********************************************************************************************
1656 ** GUI Creation
1657 ***********************************************************************************************/
1659 static GtkActionEntry entries[] = {
1660 { "File", NULL, "_File", 0, 0, 0 },
1661 { "Edit", NULL, "_Edit", 0, 0, 0 },
1662 { "View", NULL, "_View", 0, 0, 0 },
1663 { "SetZoom", NULL, "_Zoom", 0, 0, 0 },
1664 { "SetPan", NULL, "_Pan", 0, 0, 0 },
1665 { "Layers", NULL, "_Layers", 0, 0, 0 },
1666 { "Tools", NULL, "_Tools", 0, 0, 0 },
1667 { "Help", NULL, "_Help", 0, 0, 0 },
1669 { "New", GTK_STOCK_NEW, "_New", "<control>N", "New file", (GCallback)newwindow_cb },
1670 { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", (GCallback)load_file },
1671 { "Append", GTK_STOCK_ADD, "A_ppend File", NULL, "Append data from a different file", (GCallback)load_file },
1672 { "Acquire", NULL, "A_cquire", 0, 0, 0 },
1673 { "AcquireGPS", NULL, "From _GPS", NULL, "Transfer data from a GPS device", (GCallback)acquire_from_gps },
1674 { "AcquireGoogle", NULL, "Google _Directions", NULL, "Get driving directions from Google", (GCallback)acquire_from_google },
1675 #ifdef VIK_CONFIG_GEOCACHES
1676 { "AcquireGC", NULL, "Geo_caches", NULL, "Get Geocaches from geocaching.com", (GCallback)acquire_from_gc },
1677 #endif
1678 { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the file", (GCallback)save_file },
1679 { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As", NULL, "Save the file under different name", (GCallback)save_file_as },
1680 { "GenImg", GTK_STOCK_CLEAR, "_Generate Image File", NULL, "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb },
1681 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL, "FIXME:IMGDIR", (GCallback)draw_to_image_dir_cb },
1682 { "Exit", GTK_STOCK_QUIT, "E_xit", "<control>W", "Exit the program", (GCallback)window_close },
1683 { "SaveExit", GTK_STOCK_QUIT, "Save and Exit", NULL, "Save and Exit the program", (GCallback)save_file_and_exit },
1685 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, "Go To Google Maps location", NULL, "Go to address/place using Google Maps search", (GCallback)goto_address },
1686 { "GotoLL", GTK_STOCK_QUIT, "_Go to Lat\\/Lon...", NULL, "Go to arbitrary lat\\/lon coordinate", (GCallback)draw_goto_cb },
1687 { "GotoUTM", GTK_STOCK_QUIT, "Go to UTM...", NULL, "Go to arbitrary UTM coordinate", (GCallback)draw_goto_cb },
1688 { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...", NULL, NULL, (GCallback)set_bg_color },
1689 { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>plus", NULL, (GCallback)draw_zoom_cb },
1690 { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control>minus", NULL, (GCallback)draw_zoom_cb },
1691 { "ZoomTo", GTK_STOCK_ZOOM_FIT, "Zoom _To", "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1692 { "Zoom0.25", NULL, "0.25", NULL, NULL, (GCallback)draw_zoom_cb },
1693 { "Zoom0.5", NULL, "0.5", NULL, NULL, (GCallback)draw_zoom_cb },
1694 { "Zoom1", NULL, "1", NULL, NULL, (GCallback)draw_zoom_cb },
1695 { "Zoom2", NULL, "2", NULL, NULL, (GCallback)draw_zoom_cb },
1696 { "Zoom4", NULL, "4", NULL, NULL, (GCallback)draw_zoom_cb },
1697 { "Zoom8", NULL, "8", NULL, NULL, (GCallback)draw_zoom_cb },
1698 { "Zoom16", NULL, "16", NULL, NULL, (GCallback)draw_zoom_cb },
1699 { "Zoom32", NULL, "32", NULL, NULL, (GCallback)draw_zoom_cb },
1700 { "Zoom64", NULL, "64", NULL, NULL, (GCallback)draw_zoom_cb },
1701 { "Zoom128", NULL, "128", NULL, NULL, (GCallback)draw_zoom_cb },
1702 { "PanNorth", NULL, "Pan North", "<control>Up", NULL, (GCallback)draw_pan_cb },
1703 { "PanEast", NULL, "Pan East", "<control>Right", NULL, (GCallback)draw_pan_cb },
1704 { "PanSouth", NULL, "Pan South", "<control>Down", NULL, (GCallback)draw_pan_cb },
1705 { "PanWest", NULL, "Pan West", "<control>Left", NULL, (GCallback)draw_pan_cb },
1706 { "BGJobs", GTK_STOCK_EXECUTE, "Background _Jobs", NULL, NULL, (GCallback)a_background_show_window },
1708 { "Cut", GTK_STOCK_CUT, "Cu_t", NULL, NULL, (GCallback)menu_cut_layer_cb },
1709 { "Copy", GTK_STOCK_COPY, "_Copy", NULL, NULL, (GCallback)menu_copy_layer_cb },
1710 { "Paste", GTK_STOCK_PASTE, "_Paste", NULL, NULL, (GCallback)menu_paste_layer_cb },
1711 { "Delete", GTK_STOCK_DELETE, "_Delete", NULL, NULL, (GCallback)menu_delete_layer_cb },
1712 { "DeleteAll", NULL, "Delete All", NULL, NULL, (GCallback)clear_cb },
1713 { "Properties",GTK_STOCK_PROPERTIES, "_Properties", NULL, NULL, (GCallback)menu_properties_cb },
1715 { "About", GTK_STOCK_ABOUT, "_About", NULL, NULL, (GCallback)help_about_cb },
1718 /* Radio items */
1719 static GtkRadioActionEntry mode_entries[] = {
1720 { "ModeUTM", NULL, "_UTM Mode", "<control>u", NULL, 0 },
1721 { "ModeExpedia", NULL, "_Expedia Mode", "<control>e", NULL, 1 },
1722 { "ModeGoogle", NULL, "_Old Google Mode", "<control>o", NULL, 2 },
1723 { "ModeKH", NULL, "Old _KH Mode", "<control>k", NULL, 3 },
1724 { "ModeMercator", NULL, "_Google Mode", "<control>g", NULL, 4 }
1727 static GtkRadioActionEntry tool_entries[] = {
1728 { "Zoom", "vik-icon-zoom", "_Zoom", "<control><shift>Z", "Zoom Tool", 0 },
1729 { "Ruler", "vik-icon-ruler", "_Ruler", "<control><shift>R", "Ruler Tool", 1 }
1732 static GtkToggleActionEntry toggle_entries[] = {
1733 { "ShowScale", NULL, "Show Scale", NULL, NULL, (GCallback)set_draw_scale, TRUE },
1734 { "ShowCenterMark", NULL, "Show Center Mark", NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1737 #include "menu.xml.h"
1738 static void window_create_ui( VikWindow *window )
1740 GtkUIManager *uim;
1741 GtkActionGroup *action_group;
1742 GtkAccelGroup *accel_group;
1743 GError *error;
1744 guint i, j, mid;
1745 GtkIconFactory *icon_factory;
1746 GtkIconSet *icon_set;
1747 GtkRadioActionEntry *tools = NULL, *radio;
1748 guint ntools;
1750 uim = gtk_ui_manager_new ();
1751 window->uim = uim;
1753 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1754 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1756 error = NULL;
1757 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1758 g_error_free (error);
1759 exit (1);
1762 action_group = gtk_action_group_new ("MenuActions");
1763 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1764 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1765 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
1767 icon_factory = gtk_icon_factory_new ();
1768 gtk_icon_factory_add_default (icon_factory);
1770 register_vik_icons(icon_factory);
1772 ntools = 0;
1773 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
1774 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1775 radio = &tools[ntools];
1776 ntools++;
1777 *radio = tool_entries[i];
1778 radio->value = ntools;
1781 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1782 GtkActionEntry action;
1783 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
1784 vik_layer_get_interface(i)->name,
1785 vik_layer_get_interface(i)->name,
1786 GTK_UI_MANAGER_MENUITEM, FALSE);
1788 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
1789 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
1790 gtk_icon_set_unref (icon_set);
1792 action.name = vik_layer_get_interface(i)->name;
1793 action.stock_id = vik_layer_get_interface(i)->name;
1794 action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name);
1795 action.accelerator = NULL;
1796 action.tooltip = NULL;
1797 action.callback = (GCallback)menu_addlayer_cb;
1798 gtk_action_group_add_actions(action_group, &action, 1, window);
1800 if ( vik_layer_get_interface(i)->tools_count ) {
1801 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1802 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
1805 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1806 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
1807 radio = &tools[ntools];
1808 ntools++;
1810 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
1811 vik_layer_get_interface(i)->tools[j].name,
1812 vik_layer_get_interface(i)->tools[j].name,
1813 GTK_UI_MANAGER_MENUITEM, FALSE);
1814 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
1815 vik_layer_get_interface(i)->tools[j].name,
1816 vik_layer_get_interface(i)->tools[j].name,
1817 GTK_UI_MANAGER_TOOLITEM, FALSE);
1819 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
1821 radio->name = vik_layer_get_interface(i)->tools[j].name;
1822 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
1823 radio->label = vik_layer_get_interface(i)->tools[j].name;
1824 radio->accelerator = NULL;
1825 radio->tooltip = vik_layer_get_interface(i)->tools[j].name;
1826 radio->value = ntools;
1829 g_object_unref (icon_factory);
1831 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
1832 g_free(tools);
1834 gtk_ui_manager_insert_action_group (uim, action_group, 0);
1836 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
1837 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
1838 GtkAction *action = gtk_action_group_get_action(action_group,
1839 vik_layer_get_interface(i)->tools[j].name);
1840 g_object_set(action, "sensitive", FALSE, NULL);
1843 window->action_group = action_group;
1845 accel_group = gtk_ui_manager_get_accel_group (uim);
1846 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
1847 gtk_ui_manager_ensure_update (uim);
1852 #include "icons/icons.h"
1853 static struct {
1854 const GdkPixdata *data;
1855 gchar *stock_id;
1856 } stock_icons[] = {
1857 { &addtr_18, "Create Track" },
1858 { &edtr_18, "Edit Trackpoint" },
1859 { &addwp_18, "Create Waypoint" },
1860 { &edwp_18, "Edit Waypoint" },
1861 { &zoom_18, "vik-icon-zoom" },
1862 { &ruler_18, "vik-icon-ruler" },
1863 { &geozoom_18, "Georef Zoom Tool" },
1864 { &geomove_18, "Georef Move Map" },
1865 { &mapdl_18, "Maps Download" },
1866 { &demdl_18, "DEM Download/Import" },
1867 { &showpic_18, "Show Picture" },
1870 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
1872 static void
1873 register_vik_icons (GtkIconFactory *icon_factory)
1875 GtkIconSet *icon_set;
1876 gint i;
1878 for (i = 0; i < n_stock_icons; i++) {
1879 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
1880 stock_icons[i].data, FALSE, NULL ));
1881 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
1882 gtk_icon_set_unref (icon_set);