Store cursor info directly into tool struct
[viking.git] / src / vikwindow.c
blobfdf78ce9d4413efadafc6bcf6182b23f48d1c9d5
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
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "viking.h"
27 #include "background.h"
28 #include "acquire.h"
29 #include "datasources.h"
30 #include "googlesearch.h"
31 #include "dems.h"
32 #include "print.h"
33 #include "preferences.h"
34 #include "icons/icons.h"
36 #include <stdlib.h>
37 #include <math.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <glib/gprintf.h>
41 #include <glib/gi18n.h>
42 #ifdef WINDOWS
43 /* TODO IMPORTANT: mkdir for windows header? is it called 'mkdir' */
44 #define make_dir(dir) mkdir(dir)
45 #else
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #define make_dir(dir) mkdir(dir,0777)
49 #endif
51 #define VIKING_WINDOW_WIDTH 1000
52 #define VIKING_WINDOW_HEIGHT 800
53 #define DRAW_IMAGE_DEFAULT_WIDTH 1280
54 #define DRAW_IMAGE_DEFAULT_HEIGHT 1024
55 #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE
57 static void window_finalize ( GObject *gob );
58 static GObjectClass *parent_class;
60 static void window_init ( VikWindow *vw );
61 static void window_class_init ( VikWindowClass *klass );
62 static void window_set_filename ( VikWindow *vw, const gchar *filename );
64 static void draw_update ( VikWindow *vw );
66 static void newwindow_cb ( GtkAction *a, VikWindow *vw );
68 /* Drawing & stuff */
70 static gboolean delete_event( VikWindow *vw );
72 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data );
74 static void window_configure_event ( VikWindow *vw );
75 static void draw_sync ( VikWindow *vw );
76 static void draw_redraw ( VikWindow *vw );
77 static void draw_scroll ( VikWindow *vw, GdkEventScroll *event );
78 static void draw_click ( VikWindow *vw, GdkEventButton *event );
79 static void draw_release ( VikWindow *vw, GdkEventButton *event );
80 static void draw_mouse_motion ( VikWindow *vw, GdkEventMotion *event );
81 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw );
82 static void draw_goto_cb ( GtkAction *a, VikWindow *vw );
84 static void draw_status ();
86 /* End Drawing Functions */
88 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw );
89 static void menu_properties_cb ( GtkAction *a, VikWindow *vw );
90 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw );
92 /* tool management */
93 typedef struct {
94 VikToolInterface ti;
95 gpointer state;
96 gint layer_type;
97 } toolbox_tool_t;
98 #define TOOL_LAYER_TYPE_NONE -1
100 typedef struct {
101 int active_tool;
102 int n_tools;
103 toolbox_tool_t *tools;
104 VikWindow *vw;
105 } toolbox_tools_t;
107 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw );
108 static toolbox_tools_t* toolbox_create(VikWindow *vw);
109 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
110 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
111 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
112 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name);
113 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
114 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
115 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
118 /* ui creation */
119 static void window_create_ui( VikWindow *window );
120 static void register_vik_icons (GtkIconFactory *icon_factory);
122 /* i/o */
123 static void load_file ( GtkAction *a, VikWindow *vw );
124 static gboolean save_file_as ( GtkAction *a, VikWindow *vw );
125 static gboolean save_file ( GtkAction *a, VikWindow *vw );
126 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw );
127 static gboolean window_save ( VikWindow *vw );
129 struct _VikWindow {
130 GtkWindow gtkwindow;
131 VikViewport *viking_vvp;
132 VikLayersPanel *viking_vlp;
133 VikStatusbar *viking_vs;
135 GtkToolbar *toolbar;
137 GtkItemFactory *item_factory;
139 /* tool management state */
140 guint current_tool;
141 toolbox_tools_t *vt;
142 guint16 tool_layer_id;
143 guint16 tool_tool_id;
145 GtkActionGroup *action_group;
147 gint pan_x, pan_y;
149 guint draw_image_width, draw_image_height;
150 gboolean draw_image_save_as_png;
152 gchar *filename;
153 gboolean modified;
155 GtkWidget *open_dia, *save_dia;
156 GtkWidget *save_img_dia, *save_img_dir_dia;
158 gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */
159 GtkUIManager *uim;
161 /* half-drawn update */
162 VikLayer *trigger;
163 VikCoord trigger_center;
166 enum {
167 TOOL_PAN = 0,
168 TOOL_ZOOM,
169 TOOL_RULER,
170 TOOL_LAYER,
171 NUMBER_OF_TOOLS
174 enum {
175 VW_NEWWINDOW_SIGNAL,
176 VW_OPENWINDOW_SIGNAL,
177 VW_LAST_SIGNAL
180 static guint window_signals[VW_LAST_SIGNAL] = { 0 };
182 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Pan"), N_("Zoom"), N_("Ruler") };
184 GType vik_window_get_type (void)
186 static GType vw_type = 0;
188 if (!vw_type)
190 static const GTypeInfo vw_info =
192 sizeof (VikWindowClass),
193 NULL, /* base_init */
194 NULL, /* base_finalize */
195 (GClassInitFunc) window_class_init, /* class_init */
196 NULL, /* class_finalize */
197 NULL, /* class_data */
198 sizeof (VikWindow),
200 (GInstanceInitFunc) window_init,
202 vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 );
205 return vw_type;
208 VikViewport * vik_window_viewport(VikWindow *vw)
210 return(vw->viking_vvp);
213 void vik_window_selected_layer(VikWindow *vw, VikLayer *vl)
215 int i, j, tool_count;
216 VikLayerInterface *layer_interface;
218 if (!vw->action_group) return;
220 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
221 GtkAction *action;
222 layer_interface = vik_layer_get_interface(i);
223 tool_count = layer_interface->tools_count;
225 for (j = 0; j < tool_count; j++) {
226 action = gtk_action_group_get_action(vw->action_group,
227 layer_interface->tools[j].name);
228 g_object_set(action, "sensitive", i == vl->type, NULL);
233 static void window_finalize ( GObject *gob )
235 VikWindow *vw = VIK_WINDOW(gob);
236 g_return_if_fail ( vw != NULL );
238 a_background_remove_status ( vw->viking_vs );
240 G_OBJECT_CLASS(parent_class)->finalize(gob);
244 static void window_class_init ( VikWindowClass *klass )
246 /* destructor */
247 GObjectClass *object_class;
249 window_signals[VW_NEWWINDOW_SIGNAL] = g_signal_new ( "newwindow", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikWindowClass, newwindow), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
250 window_signals[VW_OPENWINDOW_SIGNAL] = g_signal_new ( "openwindow", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (VikWindowClass, openwindow), NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
252 object_class = G_OBJECT_CLASS (klass);
254 object_class->finalize = window_finalize;
256 parent_class = g_type_class_peek_parent (klass);
260 static void window_init ( VikWindow *vw )
262 GtkWidget *main_vbox;
263 GtkWidget *hpaned;
265 vw->action_group = NULL;
267 vw->viking_vvp = vik_viewport_new();
268 vw->viking_vlp = vik_layers_panel_new();
269 vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp );
270 vw->viking_vs = vik_statusbar_new();
272 vw->vt = toolbox_create(vw);
273 window_create_ui(vw);
274 window_set_filename (vw, NULL);
276 toolbox_activate(vw->vt, "Pan");
278 vw->filename = NULL;
279 vw->item_factory = NULL;
281 vw->modified = FALSE;
282 vw->only_updating_coord_mode_ui = FALSE;
284 vw->pan_x = vw->pan_y = -1;
285 vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH;
286 vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT;
287 vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG;
289 main_vbox = gtk_vbox_new(FALSE, 1);
290 gtk_container_add (GTK_CONTAINER (vw), main_vbox);
292 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0);
293 gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0);
294 gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR);
295 gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS);
298 g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL);
300 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw);
301 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(window_configure_event), vw);
302 gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK );
303 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw);
304 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw);
305 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw);
306 g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw);
307 g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw);
309 g_signal_connect_swapped (G_OBJECT (vw->viking_vvp), "key_press_event", G_CALLBACK (key_press_event), vw);
311 gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT);
313 hpaned = gtk_hpaned_new ();
314 gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) );
315 gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) );
317 /* This packs the button into the window (a gtk container). */
318 gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0);
320 gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0);
322 a_background_add_status(vw->viking_vs);
324 vw->open_dia = NULL;
325 vw->save_dia = NULL;
326 vw->save_img_dia = NULL;
327 vw->save_img_dir_dia = NULL;
330 VikWindow *vik_window_new ()
332 return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) );
335 static gboolean key_press_event( VikWindow *vw, GdkEventKey *event, gpointer data )
337 VikLayer *vl = vik_layers_panel_get_selected ( vw->viking_vlp );
338 if (vl && vw->vt->active_tool != -1 && vw->vt->tools[vw->vt->active_tool].ti.key_press ) {
339 gint ltype = vw->vt->tools[vw->vt->active_tool].layer_type;
340 if ( vl && ltype == vl->type )
341 return vw->vt->tools[vw->vt->active_tool].ti.key_press(vl, event, vw->vt->tools[vw->vt->active_tool].state);
343 return FALSE; /* don't handle the keypress */
346 static gboolean delete_event( VikWindow *vw )
348 #ifdef VIKING_PROMPT_IF_MODIFIED
349 if ( vw->modified )
350 #else
351 if (0)
352 #endif
354 GtkDialog *dia;
355 dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
356 _("Do you want to save the changes you made to the document \"%s\"?\n"
357 "\n"
358 "Your changes will be lost if you don't save them."),
359 vw->filename ? a_file_basename ( vw->filename ) : _("Untitled") ) );
360 gtk_dialog_add_buttons ( dia, _("Don't Save"), GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL );
361 switch ( gtk_dialog_run ( dia ) )
363 case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE;
364 case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE;
365 default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw);
368 return FALSE;
371 /* Drawing stuff */
372 static void newwindow_cb ( GtkAction *a, VikWindow *vw )
374 g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 );
377 static void draw_update ( VikWindow *vw )
379 draw_redraw (vw);
380 draw_sync (vw);
383 static void draw_sync ( VikWindow *vw )
385 vik_viewport_sync(vw->viking_vvp);
386 draw_status ( vw );
387 /* other things may be necc here later. */
390 static void draw_status ( VikWindow *vw )
392 static gchar zoom_level[22];
393 g_snprintf ( zoom_level, 22, "%.3f/%.3f %s", vik_viewport_get_xmpp (vw->viking_vvp), vik_viewport_get_ympp(vw->viking_vvp), vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM ? _("mpp") : _("pixelfact") );
394 if ( vw->current_tool == TOOL_LAYER )
395 vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name );
396 else
397 vik_statusbar_set_message ( vw->viking_vs, 0, _(tool_names[vw->current_tool]) );
399 vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level );
402 void vik_window_set_redraw_trigger(VikLayer *vl)
404 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl));
405 vw->trigger = vl;
408 static void window_configure_event ( VikWindow *vw )
410 static int first = 1;
411 draw_redraw ( vw );
412 if (first) {
413 // This is a hack to set the cursor corresponding to the first tool
414 // FIXME find the correct way to initialize both tool and its cursor
415 const GdkCursor *cursor = NULL;
416 first = 0;
417 cursor = toolbox_get_cursor(vw->vt, "Pan");
418 /* We set cursor, even if it is NULL: it resets to default */
419 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
423 static void draw_redraw ( VikWindow *vw )
425 VikCoord old_center = vw->trigger_center;
426 vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp));
427 VikLayer *new_trigger = vw->trigger;
428 vw->trigger = NULL;
429 VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp));
431 if ( ! new_trigger )
432 ; /* do nothing -- have to redraw everything. */
433 else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) )
434 vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */
435 else
436 vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE );
438 /* actually draw */
439 vik_viewport_clear ( vw->viking_vvp);
440 vik_layers_panel_draw_all ( vw->viking_vlp );
441 vik_viewport_draw_scale ( vw->viking_vvp );
442 vik_viewport_draw_centermark ( vw->viking_vvp );
444 vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */
447 gboolean draw_buf_done = TRUE;
449 static gboolean draw_buf(gpointer data)
451 gpointer *pass_along = data;
452 gdk_threads_enter();
453 gdk_draw_drawable (pass_along[0], pass_along[1],
454 pass_along[2], 0, 0, 0, 0, -1, -1);
455 draw_buf_done = TRUE;
456 gdk_threads_leave();
457 return FALSE;
461 /* Mouse event handlers ************************************************************************/
463 static void vik_window_pan_click (VikWindow *vw, GdkEventButton *event)
465 /* set panning origin */
466 vw->pan_x = (gint) event->x;
467 vw->pan_y = (gint) event->y;
470 static void draw_click (VikWindow *vw, GdkEventButton *event)
472 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
474 /* middle button pressed. we reserve all middle button and scroll events
475 * for panning and zooming; tools only get left/right/movement
477 if ( event->button == 2) {
478 vik_window_pan_click ( vw, event );
480 else {
481 toolbox_click(vw->vt, event);
485 static void vik_window_pan_move (VikWindow *vw, GdkEventMotion *event)
487 if ( vw->pan_x != -1 ) {
488 vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y );
492 static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event)
494 static VikCoord coord;
495 static struct UTM utm;
496 static struct LatLon ll;
497 static char pointer_buf[36];
498 gint16 alt;
499 gdouble zoom;
500 VikDemInterpol interpol_method;
502 toolbox_move(vw->vt, event);
504 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
505 vik_coord_to_utm ( &coord, &utm );
506 a_coords_utm_to_latlon ( &utm, &ll );
507 /* Change interpolate method according to scale */
508 zoom = vik_viewport_get_zoom(vw->viking_vvp);
509 if (zoom > 2.0)
510 interpol_method = VIK_DEM_INTERPOL_NONE;
511 else if (zoom >= 1.0)
512 interpol_method = VIK_DEM_INTERPOL_SIMPLE;
513 else
514 interpol_method = VIK_DEM_INTERPOL_BEST;
515 if ((alt = a_dems_get_elev_by_coord(&coord, interpol_method)) != VIK_DEM_INVALID_ELEVATION)
516 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f %dm"), ll.lat, ll.lon, alt );
517 else
518 g_snprintf ( pointer_buf, 36, _("Cursor: %f %f"), ll.lat, ll.lon );
519 vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf );
521 vik_window_pan_move ( vw, event );
524 static void vik_window_pan_release ( VikWindow *vw, GdkEventButton *event )
526 if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 )
527 vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y );
528 else
529 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x,
530 vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y );
531 draw_update ( vw );
532 vw->pan_x = vw->pan_y = -1;
535 static void draw_release ( VikWindow *vw, GdkEventButton *event )
537 gtk_widget_grab_focus ( GTK_WIDGET(vw->viking_vvp) );
539 if ( event->button == 2 ) { /* move / pan */
540 vik_window_pan_release(vw, event);
542 else {
543 toolbox_release(vw->vt, event);
547 static void draw_scroll (VikWindow *vw, GdkEventScroll *event)
549 if ( event->state == GDK_CONTROL_MASK ) {
550 /* control == pan up & down */
551 if ( event->direction == GDK_SCROLL_UP )
552 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 );
553 else
554 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 );
555 } else if ( event->state == GDK_SHIFT_MASK ) {
556 /* control-shift == pan left & right */
557 if ( event->direction == GDK_SCROLL_UP )
558 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 );
559 else
560 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 );
561 } else if ( event->state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) ) {
562 if ( event->direction == GDK_SCROLL_UP ) {
563 /* make sure mouse is still over the same point on the map when we zoom */
564 VikCoord coord;
565 gint x, y;
566 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
567 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
568 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
569 vik_viewport_zoom_in (vw->viking_vvp);
570 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
571 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
572 center_y + (y - event->y) );
574 else {
575 VikCoord coord;
576 gint x, y;
577 gint center_x = vik_viewport_get_width ( vw->viking_vvp ) / 2;
578 gint center_y = vik_viewport_get_height ( vw->viking_vvp ) / 2;
579 vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord );
580 vik_viewport_zoom_out(vw->viking_vvp);
581 vik_viewport_coord_to_screen ( vw->viking_vvp, &coord, &x, &y );
582 vik_viewport_set_center_screen ( vw->viking_vvp, center_x + (x - event->x),
583 center_y + (y - event->y) );
585 } else {
586 if ( event->direction == GDK_SCROLL_UP )
587 vik_viewport_zoom_in (vw->viking_vvp);
588 else
589 vik_viewport_zoom_out (vw->viking_vvp);
592 draw_update(vw);
597 /********************************************************************************
598 ** Ruler tool code
599 ********************************************************************************/
600 static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance)
602 PangoFontDescription *pfd;
603 PangoLayout *pl;
604 gchar str[128];
605 GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1);
606 GdkGC *thickgc = gdk_gc_new(d);
608 gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
609 gdouble dx = (x2-x1)/len*10;
610 gdouble dy = (y2-y1)/len*10;
611 gdouble c = cos(15.0 * M_PI/180.0);
612 gdouble s = sin(15.0 * M_PI/180.0);
613 gdouble angle;
614 gdouble baseangle = 0;
615 gint i;
617 /* draw line with arrow ends */
619 gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2;
620 a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2);
621 gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2);
624 a_viewport_clip_line(&x1, &y1, &x2, &y2);
625 gdk_draw_line(d, gc, x1, y1, x2, y2);
627 gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx);
628 gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx);
629 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s));
630 gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s));
631 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s));
632 gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s));
634 /* draw compass */
635 #define CR 80
636 #define CW 4
637 angle = atan2(dy, dx) + M_PI_2;
639 if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) {
640 VikCoord test;
641 struct LatLon ll;
642 struct UTM u;
643 gint tx, ty;
645 vik_viewport_screen_to_coord ( vvp, x1, y1, &test );
646 vik_coord_to_latlon ( &test, &ll );
647 ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude
648 a_coords_latlon_to_utm ( &ll, &u );
649 vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u );
650 vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty );
652 baseangle = M_PI - atan2(tx-x1, ty-y1);
653 angle -= baseangle;
656 if (angle<0)
657 angle+=2*M_PI;
658 if (angle>2*M_PI)
659 angle-=2*M_PI;
662 GdkColor color;
663 gdk_gc_copy(thickgc, gc);
664 gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
665 gdk_color_parse("#2255cc", &color);
666 gdk_gc_set_rgb_fg_color(thickgc, &color);
668 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);
671 gdk_gc_copy(thickgc, gc);
672 gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
673 for (i=0; i<180; i++) {
674 c = cos(i*M_PI/90.0 + baseangle);
675 s = sin(i*M_PI/90.0 + baseangle);
677 if (i%5) {
678 gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s);
679 } else {
680 gdouble ticksize = 2*CW;
681 gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s);
685 gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360);
686 gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360);
687 gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360);
688 c = (CR+CW*2)*cos(baseangle);
689 s = (CR+CW*2)*sin(baseangle);
690 gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s);
691 gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c);
693 /* draw labels */
694 #define LABEL(x, y, w, h) { \
695 gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \
696 gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \
697 gdk_draw_layout(d, gc, (x), (y), pl); }
699 gint wd, hd, xd, yd;
700 gint wb, hb, xb, yb;
702 pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL);
704 pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
705 pango_layout_set_font_description (pl, pfd);
706 pango_font_description_free (pfd);
708 pango_layout_set_text(pl, "N", -1);
709 gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl);
711 /* draw label with distance */
712 if (distance >= 1000 && distance < 100000) {
713 g_sprintf(str, "%3.2f km", distance/1000.0);
714 } else if (distance < 1000) {
715 g_sprintf(str, "%d m", (int)distance);
716 } else {
717 g_sprintf(str, "%d km", (int)distance/1000);
719 pango_layout_set_text(pl, str, -1);
721 pango_layout_get_pixel_size ( pl, &wd, &hd );
722 if (dy>0) {
723 xd = (x1+x2)/2 + dy;
724 yd = (y1+y2)/2 - hd/2 - dx;
725 } else {
726 xd = (x1+x2)/2 - dy;
727 yd = (y1+y2)/2 - hd/2 + dx;
730 if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) {
731 xd = x2 + 10;
732 yd = y2 - 5;
735 LABEL(xd, yd, wd, hd);
737 /* draw label with bearing */
738 g_sprintf(str, "%3.1f°", angle*180.0/M_PI);
739 pango_layout_set_text(pl, str, -1);
740 pango_layout_get_pixel_size ( pl, &wb, &hb );
741 xb = x1 + CR*cos(angle-M_PI_2);
742 yb = y1 + CR*sin(angle-M_PI_2);
744 if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) {
745 xb = x2 + 10;
746 yb = y2 + 10;
750 GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1};
751 if (gdk_rectangle_intersect(&r1, &r2, &r2)) {
752 xb = xd + wd + 5;
755 LABEL(xb, yb, wb, hb);
757 #undef LABEL
759 g_object_unref ( G_OBJECT ( pl ) );
760 g_object_unref ( G_OBJECT ( labgc ) );
761 g_object_unref ( G_OBJECT ( thickgc ) );
764 typedef struct {
765 VikWindow *vw;
766 VikViewport *vvp;
767 gboolean has_oldcoord;
768 VikCoord oldcoord;
769 } ruler_tool_state_t;
771 static gpointer ruler_create (VikWindow *vw, VikViewport *vvp)
773 ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1);
774 s->vw = vw;
775 s->vvp = vvp;
776 s->has_oldcoord = FALSE;
777 return s;
780 static void ruler_destroy (ruler_tool_state_t *s)
782 g_free(s);
785 static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
787 struct LatLon ll;
788 VikCoord coord;
789 gchar *temp;
790 if ( event->button == 1 ) {
791 vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord );
792 vik_coord_to_latlon ( &coord, &ll );
793 if ( s->has_oldcoord ) {
794 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
795 s->has_oldcoord = FALSE;
797 else {
798 temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon );
799 s->has_oldcoord = TRUE;
802 vik_statusbar_set_message ( s->vw->viking_vs, 3, temp );
803 g_free ( temp );
805 s->oldcoord = coord;
807 else {
808 vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y );
809 draw_update ( s->vw );
811 return VIK_LAYER_TOOL_ACK;
814 static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
816 VikViewport *vvp = s->vvp;
817 VikWindow *vw = s->vw;
819 struct LatLon ll;
820 VikCoord coord;
821 gchar *temp;
823 if ( s->has_oldcoord ) {
824 int oldx, oldy, w1, h1, w2, h2;
825 static GdkPixmap *buf = NULL;
826 w1 = vik_viewport_get_width(vvp);
827 h1 = vik_viewport_get_height(vvp);
828 if (!buf) {
829 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
831 gdk_drawable_get_size(buf, &w2, &h2);
832 if (w1 != w2 || h1 != h2) {
833 g_object_unref ( G_OBJECT ( buf ) );
834 buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 );
837 vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord );
838 vik_coord_to_latlon ( &coord, &ll );
839 vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy );
841 gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc,
842 vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1);
843 draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) );
844 if (draw_buf_done) {
845 static gpointer pass_along[3];
846 pass_along[0] = GTK_WIDGET(vvp)->window;
847 pass_along[1] = GTK_WIDGET(vvp)->style->black_gc;
848 pass_along[2] = buf;
849 g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL);
850 draw_buf_done = FALSE;
853 temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) );
854 vik_statusbar_set_message ( vw->viking_vs, 3, temp );
855 g_free ( temp );
857 return VIK_LAYER_TOOL_ACK;
860 static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s)
862 return VIK_LAYER_TOOL_ACK;
865 static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s)
867 draw_update ( s->vw );
870 static VikToolInterface ruler_tool =
871 { "Ruler",
872 (VikToolConstructorFunc) ruler_create,
873 (VikToolDestructorFunc) ruler_destroy,
874 (VikToolActivationFunc) NULL,
875 (VikToolActivationFunc) ruler_deactivate,
876 (VikToolMouseFunc) ruler_click,
877 (VikToolMouseFunc) ruler_move,
878 (VikToolMouseFunc) ruler_release,
879 NULL,
880 GDK_CURSOR_IS_PIXMAP,
881 &cursor_ruler };
882 /*** end ruler code ********************************************************/
886 /********************************************************************************
887 ** Zoom tool code
888 ********************************************************************************/
889 static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp)
891 return vw;
894 static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
896 vw->modified = TRUE;
897 vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y );
898 if ( event->button == 1 )
899 vik_viewport_zoom_in (vw->viking_vvp);
900 else if ( event->button == 3 )
901 vik_viewport_zoom_out (vw->viking_vvp);
902 draw_update ( vw );
903 return VIK_LAYER_TOOL_ACK;
906 static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
908 return VIK_LAYER_TOOL_ACK;
911 static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp)
913 return VIK_LAYER_TOOL_ACK;
916 static VikToolInterface zoom_tool =
917 { "Zoom",
918 (VikToolConstructorFunc) zoomtool_create,
919 (VikToolDestructorFunc) NULL,
920 (VikToolActivationFunc) NULL,
921 (VikToolActivationFunc) NULL,
922 (VikToolMouseFunc) zoomtool_click,
923 (VikToolMouseFunc) zoomtool_move,
924 (VikToolMouseFunc) zoomtool_release,
925 NULL,
926 GDK_CURSOR_IS_PIXMAP,
927 &cursor_zoom };
928 /*** end zoom code ********************************************************/
930 /********************************************************************************
931 ** Pan tool code
932 ********************************************************************************/
933 static gpointer pantool_create (VikWindow *vw, VikViewport *vvp)
935 return vw;
938 static VikLayerToolFuncStatus pantool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
940 vw->modified = TRUE;
941 if ( event->button == 1 )
942 vik_window_pan_click ( vw, event );
943 draw_update ( vw );
944 return VIK_LAYER_TOOL_ACK;
947 static VikLayerToolFuncStatus pantool_move (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
949 vik_window_pan_move ( vw, event );
950 return VIK_LAYER_TOOL_ACK;
953 static VikLayerToolFuncStatus pantool_release (VikLayer *vl, GdkEventButton *event, VikWindow *vw)
955 if ( event->button == 1 )
956 vik_window_pan_release ( vw, event );
957 return VIK_LAYER_TOOL_ACK;
960 static VikToolInterface pan_tool =
961 { "Pan",
962 (VikToolConstructorFunc) pantool_create,
963 (VikToolDestructorFunc) NULL,
964 (VikToolActivationFunc) NULL,
965 (VikToolActivationFunc) NULL,
966 (VikToolMouseFunc) pantool_click,
967 (VikToolMouseFunc) pantool_move,
968 (VikToolMouseFunc) pantool_release,
969 NULL,
970 GDK_FLEUR };
971 /*** end pan code ********************************************************/
973 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
975 if (!strcmp(gtk_action_get_name(a), "PanNorth")) {
976 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 );
977 } else if (!strcmp(gtk_action_get_name(a), "PanEast")) {
978 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 );
979 } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) {
980 vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) );
981 } else if (!strcmp(gtk_action_get_name(a), "PanWest")) {
982 vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 );
984 draw_update ( vw );
987 static void full_screen_cb ( GtkAction *a, VikWindow *vw )
989 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/FullScreen" );
990 g_assert(check_box);
991 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
992 if ( state )
993 gtk_window_fullscreen ( GTK_WINDOW(vw) );
994 else
995 gtk_window_unfullscreen ( GTK_WINDOW(vw) );
998 static void draw_zoom_cb ( GtkAction *a, VikWindow *vw )
1000 guint what = 128;
1002 if (!strcmp(gtk_action_get_name(a), "ZoomIn")) {
1003 what = -3;
1005 else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) {
1006 what = -4;
1008 else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) {
1009 what = -2;
1011 else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) {
1012 what = -1;
1014 else {
1015 gchar *s = (gchar *)gtk_action_get_name(a);
1016 what = atoi(s+4);
1019 switch (what)
1021 case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break;
1022 case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break;
1023 case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break;
1024 case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break;
1025 default: vik_viewport_set_zoom ( vw->viking_vvp, what );
1027 draw_update ( vw );
1030 void draw_goto_cb ( GtkAction *a, VikWindow *vw )
1032 VikCoord new_center;
1034 if (!strcmp(gtk_action_get_name(a), "GotoLL")) {
1035 struct LatLon ll, llold;
1036 vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold );
1037 if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) )
1038 vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll );
1039 else
1040 return;
1042 else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) {
1043 struct UTM utm, utmold;
1044 vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold );
1045 if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) )
1046 vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm );
1047 else
1048 return;
1050 else {
1051 g_critical("Houston, we've had a problem.");
1052 return;
1055 vik_viewport_set_center_coord ( vw->viking_vvp, &new_center );
1056 draw_update ( vw );
1059 static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw )
1061 gint type;
1062 for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) {
1063 if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) {
1064 if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) {
1065 draw_update ( vw );
1066 vw->modified = TRUE;
1072 static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw )
1074 a_clipboard_copy_selected ( vw->viking_vlp );
1077 static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw )
1079 a_clipboard_copy_selected ( vw->viking_vlp );
1080 menu_delete_layer_cb ( a, vw );
1083 static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw )
1085 if ( a_clipboard_paste ( vw->viking_vlp ) )
1087 draw_update ( vw );
1088 vw->modified = TRUE;
1092 static void menu_properties_cb ( GtkAction *a, VikWindow *vw )
1094 if ( ! vik_layers_panel_properties ( vw->viking_vlp ) )
1095 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to show its properties.") );
1098 static void help_about_cb ( GtkAction *a, VikWindow *vw )
1100 a_dialog_about(GTK_WINDOW(vw));
1103 static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw )
1105 if ( vik_layers_panel_get_selected ( vw->viking_vlp ) )
1107 vik_layers_panel_delete_selected ( vw->viking_vlp );
1108 vw->modified = TRUE;
1110 else
1111 a_dialog_info_msg ( GTK_WINDOW(vw), _("You must select a layer to delete.") );
1114 /***************************************
1115 ** tool management routines
1117 ***************************************/
1119 static toolbox_tools_t* toolbox_create(VikWindow *vw)
1121 toolbox_tools_t *vt = g_new(toolbox_tools_t, 1);
1122 vt->tools = NULL;
1123 vt->n_tools = 0;
1124 vt->active_tool = -1;
1125 vt->vw = vw;
1126 if (!vw->viking_vvp) {
1127 g_critical("no viewport found.");
1128 exit(1);
1130 return vt;
1133 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type )
1135 vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1);
1136 vt->tools[vt->n_tools].ti = *vti;
1137 vt->tools[vt->n_tools].layer_type = layer_type;
1138 if (vti->create) {
1139 vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp);
1141 else {
1142 vt->tools[vt->n_tools].state = NULL;
1144 vt->n_tools++;
1147 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name)
1149 int i;
1150 for (i=0; i<vt->n_tools; i++) {
1151 if (!strcmp(tool_name, vt->tools[i].ti.name)) {
1152 break;
1155 return i;
1158 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
1160 int tool = toolbox_get_tool(vt, tool_name);
1161 toolbox_tool_t *t = &vt->tools[tool];
1162 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1164 if (tool == vt->n_tools) {
1165 g_critical("trying to activate a non-existent tool...");
1166 exit(1);
1168 /* is the tool already active? */
1169 if (vt->active_tool == tool) {
1170 return;
1173 if (vt->active_tool != -1) {
1174 if (vt->tools[vt->active_tool].ti.deactivate) {
1175 vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state);
1178 if (t->ti.activate) {
1179 t->ti.activate(vl, t->state);
1181 vt->active_tool = tool;
1184 static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
1186 int tool = toolbox_get_tool(vt, tool_name);
1187 toolbox_tool_t *t = &vt->tools[tool];
1188 if (t->ti.cursor == NULL) {
1189 if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
1190 GError *cursor_load_err = NULL;
1191 GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
1192 /* TODO: settable offeset */
1193 t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
1194 g_object_unref ( G_OBJECT(cursor_pixbuf) );
1195 } else {
1196 t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
1199 return t->ti.cursor;
1202 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
1204 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1205 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) {
1206 gint ltype = vt->tools[vt->active_tool].layer_type;
1207 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1208 vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state);
1212 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event)
1214 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1215 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) {
1216 gint ltype = vt->tools[vt->active_tool].layer_type;
1217 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1218 if ( VIK_LAYER_TOOL_ACK_GRAB_FOCUS == vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state) )
1219 gtk_widget_grab_focus ( GTK_WIDGET(vt->vw->viking_vvp) );
1223 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event)
1225 VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
1226 if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) {
1227 gint ltype = vt->tools[vt->active_tool].layer_type;
1228 if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) )
1229 vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state);
1232 /** End tool management ************************************/
1234 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id )
1236 gtk_action_activate ( gtk_action_group_get_action ( vw->action_group, vik_layer_get_interface(layer_id)->tools[tool_id].name ) );
1239 /* this function gets called whenever a toolbar tool is clicked */
1240 static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
1242 /* White Magic, my friends ... White Magic... */
1243 int layer_id, tool_id;
1244 const GdkCursor *cursor = NULL;
1246 toolbox_activate(vw->vt, gtk_action_get_name(a));
1248 cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
1249 /* We set cursor, even if it is NULL: it resets to default */
1250 gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
1252 if (!strcmp(gtk_action_get_name(a), "Pan")) {
1253 vw->current_tool = TOOL_PAN;
1255 else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
1256 vw->current_tool = TOOL_ZOOM;
1258 else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
1259 vw->current_tool = TOOL_RULER;
1261 else {
1262 /* TODO: only enable tools from active layer */
1263 for (layer_id=0; layer_id<VIK_LAYER_NUM_TYPES; layer_id++) {
1264 for ( tool_id = 0; tool_id < vik_layer_get_interface(layer_id)->tools_count; tool_id++ ) {
1265 if (!strcmp(vik_layer_get_interface(layer_id)->tools[tool_id].name, gtk_action_get_name(a))) {
1266 vw->current_tool = TOOL_LAYER;
1267 vw->tool_layer_id = layer_id;
1268 vw->tool_tool_id = tool_id;
1273 draw_status ( vw );
1276 static void window_set_filename ( VikWindow *vw, const gchar *filename )
1278 gchar *title;
1279 const gchar *file;
1280 if ( vw->filename )
1281 g_free ( vw->filename );
1282 if ( filename == NULL )
1284 vw->filename = NULL;
1285 file = _("Untitled");
1287 else
1289 vw->filename = g_strdup(filename);
1290 file = a_file_basename ( filename );
1292 title = g_strdup_printf( "%s - Viking", file );
1293 gtk_window_set_title ( GTK_WINDOW(vw), title );
1294 g_free ( title );
1297 GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode )
1299 GtkWidget *mode_button;
1300 gchar *buttonname;
1301 switch ( mode ) {
1302 case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break;
1303 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break;
1304 case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break;
1305 case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break;
1306 default: buttonname = "/ui/MainMenu/View/ModeKH";
1308 mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname );
1309 g_assert ( mode_button );
1310 return mode_button;
1313 void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename )
1315 switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) )
1317 case 0:
1318 a_dialog_error_msg ( GTK_WINDOW(vw), _("The file you requested could not be opened.") );
1319 break;
1320 case 1:
1322 GtkWidget *mode_button;
1323 if ( change_filename )
1324 window_set_filename ( vw, filename );
1325 mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) );
1326 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. */
1327 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE );
1328 vw->only_updating_coord_mode_ui = FALSE;
1330 vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) );
1332 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1333 g_assert ( mode_button );
1334 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) );
1336 mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1337 g_assert ( mode_button );
1338 gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) );
1340 default: draw_update ( vw );
1343 static void load_file ( GtkAction *a, VikWindow *vw )
1345 gboolean newwindow;
1346 if (!strcmp(gtk_action_get_name(a), "Open")) {
1347 newwindow = TRUE;
1349 else if (!strcmp(gtk_action_get_name(a), "Append")) {
1350 newwindow = FALSE;
1352 else {
1353 g_critical("Houston, we've had a problem.");
1354 return;
1357 if ( ! vw->open_dia )
1359 vw->open_dia = gtk_file_selection_new ( _("Please select a GPS data file to open. ") );
1360 gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE );
1361 gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) );
1362 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE );
1364 if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK )
1366 gtk_widget_hide ( vw->open_dia );
1367 #ifdef VIKING_PROMPT_IF_MODIFIED
1368 if ( (vw->modified || vw->filename) && newwindow )
1369 #else
1370 if ( vw->filename && newwindow )
1371 #endif
1372 g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) );
1373 else {
1374 gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) );
1375 gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */
1376 while ( *files ) {
1377 vik_window_open_file ( vw, *files, change_fn );
1378 files++;
1382 else
1383 gtk_widget_hide ( vw->open_dia );
1386 static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
1388 gboolean rv = FALSE;
1389 const gchar *fn;
1390 if ( ! vw->save_dia )
1392 vw->save_dia = gtk_file_selection_new ( _("Save as Viking File.") );
1393 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) );
1394 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE );
1397 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK )
1399 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) );
1400 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 ) ) )
1402 window_set_filename ( vw, fn );
1403 rv = window_save ( vw );
1404 vw->modified = FALSE;
1405 break;
1408 gtk_widget_hide ( vw->save_dia );
1409 return rv;
1412 static gboolean window_save ( VikWindow *vw )
1414 if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) )
1415 return TRUE;
1416 else
1418 a_dialog_error_msg ( GTK_WINDOW(vw), _("The filename you requested could not be opened for writing.") );
1419 return FALSE;
1423 static gboolean save_file ( GtkAction *a, VikWindow *vw )
1425 if ( ! vw->filename )
1426 return save_file_as ( NULL, vw );
1427 else
1429 vw->modified = FALSE;
1430 return window_save ( vw );
1434 static void acquire_from_gps ( GtkAction *a, VikWindow *vw )
1436 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface );
1439 static void acquire_from_google ( GtkAction *a, VikWindow *vw )
1441 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface );
1444 #ifdef VIK_CONFIG_GEOCACHES
1445 static void acquire_from_gc ( GtkAction *a, VikWindow *vw )
1447 a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface );
1449 #endif
1451 static void goto_address( GtkAction *a, VikWindow *vw)
1453 a_google_search(vw, vw->viking_vlp, vw->viking_vvp);
1456 static void preferences_cb ( GtkAction *a, VikWindow *vw )
1458 a_preferences_show_window ( GTK_WINDOW(vw) );
1461 static void clear_cb ( GtkAction *a, VikWindow *vw )
1463 vik_layers_panel_clear ( vw->viking_vlp );
1464 window_set_filename ( vw, NULL );
1465 draw_update ( vw );
1468 static void window_close ( GtkAction *a, VikWindow *vw )
1470 if ( ! delete_event ( vw ) )
1471 gtk_widget_destroy ( GTK_WIDGET(vw) );
1474 static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw )
1476 if (save_file( NULL, vw)) {
1477 window_close( NULL, vw);
1478 return(TRUE);
1480 else
1481 return(FALSE);
1484 static void zoom_to_cb ( GtkAction *a, VikWindow *vw )
1486 gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1487 if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) )
1489 vik_viewport_set_xmpp ( vw->viking_vvp, xmpp );
1490 vik_viewport_set_ympp ( vw->viking_vvp, ympp );
1491 draw_update ( vw );
1495 static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png )
1497 /* more efficient way: stuff draws directly to pixbuf (fork viewport) */
1498 GdkPixbuf *pixbuf_to_save;
1499 gdouble old_xmpp, old_ympp;
1500 GError *error = NULL;
1502 /* backup old zoom & set new */
1503 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1504 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1505 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1507 /* reset width and height: */
1508 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1510 /* draw all layers */
1511 draw_redraw ( vw );
1513 /* save buffer as file. */
1514 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);
1515 gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL );
1516 if (error)
1518 g_warning("Unable to write to file %s: %s", fn, error->message );
1519 g_error_free (error);
1521 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1523 /* pretend like nothing happened ;) */
1524 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1525 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1526 vik_viewport_configure ( vw->viking_vvp );
1527 draw_update ( vw );
1530 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 )
1532 gulong size = sizeof(gchar) * (strlen(fn) + 15);
1533 gchar *name_of_file = g_malloc ( size );
1534 guint x = 1, y = 1;
1535 struct UTM utm_orig, utm;
1537 /* *** copied from above *** */
1538 GdkPixbuf *pixbuf_to_save;
1539 gdouble old_xmpp, old_ympp;
1540 GError *error = NULL;
1542 /* backup old zoom & set new */
1543 old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp );
1544 old_ympp = vik_viewport_get_ympp ( vw->viking_vvp );
1545 vik_viewport_set_zoom ( vw->viking_vvp, zoom );
1547 /* reset width and height: do this only once for all images (same size) */
1548 vik_viewport_configure_manually ( vw->viking_vvp, w, h );
1549 /* *** end copy from above *** */
1551 g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM );
1553 make_dir(fn);
1555 utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp ));
1557 for ( y = 1; y <= tiles_h; y++ )
1559 for ( x = 1; x <= tiles_w; x++ )
1561 g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" );
1562 utm = utm_orig;
1563 if ( tiles_w & 0x1 )
1564 utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom);
1565 else
1566 utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom);
1567 if ( tiles_h & 0x1 ) /* odd */
1568 utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom);
1569 else /* even */
1570 utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom);
1572 /* move to correct place. */
1573 vik_viewport_set_center_utm ( vw->viking_vvp, &utm );
1575 draw_redraw ( vw );
1577 /* save buffer as file. */
1578 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);
1579 gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL );
1580 if (error)
1582 g_warning("Unable to write to file %s: %s", name_of_file, error->message );
1583 g_error_free (error);
1586 g_object_unref ( G_OBJECT(pixbuf_to_save) );
1590 vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig );
1591 vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp );
1592 vik_viewport_set_ympp ( vw->viking_vvp, old_ympp );
1593 vik_viewport_configure ( vw->viking_vvp );
1594 draw_update ( vw );
1596 g_free ( name_of_file );
1599 static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along)
1601 VikWindow *vw = VIK_WINDOW(pass_along[0]);
1602 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1603 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1604 gdouble width_min, width_max, height_min, height_max;
1605 gint width, height;
1607 gtk_spin_button_get_range ( width_spin, &width_min, &width_max );
1608 gtk_spin_button_get_range ( height_spin, &height_min, &height_max );
1610 /* TODO: support for xzoom and yzoom values */
1611 width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1612 height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin );
1614 if ( width > width_max || width < width_min || height > height_max || height < height_min )
1615 a_dialog_info_msg ( GTK_WINDOW(vw), _("Viewable region outside allowable pixel size bounds for image. Clipping width/height values.") );
1617 gtk_spin_button_set_value ( width_spin, width );
1618 gtk_spin_button_set_value ( height_spin, height );
1621 static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
1623 GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]);
1624 GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]);
1625 gchar *label_text;
1626 gdouble w, h;
1627 w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin);
1628 h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin);
1629 if (pass_along[4]) /* save many images; find TOTAL area covered */
1631 w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4]));
1632 h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5]));
1634 label_text = g_strdup_printf ( _("Total area: %ldm x %ldm (%.3f sq. km)"), (glong)w, (glong)h, (w*h/1000000));
1635 gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text);
1636 g_free ( label_text );
1639 static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only )
1641 /* todo: default for answers inside VikWindow or static (thruout instance) */
1642 GtkWidget *dialog = gtk_dialog_new_with_buttons ( _("Save to Image File"), GTK_WINDOW(vw),
1643 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1644 GTK_STOCK_CANCEL,
1645 GTK_RESPONSE_REJECT,
1646 GTK_STOCK_OK,
1647 GTK_RESPONSE_ACCEPT,
1648 NULL );
1649 GtkWidget *width_label, *width_spin, *height_label, *height_spin;
1650 GtkWidget *png_radio, *jpeg_radio;
1651 GtkWidget *current_window_button;
1652 gpointer current_window_pass_along[7];
1653 GtkWidget *zoom_label, *zoom_spin;
1654 GtkWidget *total_size_label;
1656 /* only used if (!one_image_only) */
1657 GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL;
1660 width_label = gtk_label_new ( _("Width (pixels):") );
1661 width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 );
1662 height_label = gtk_label_new ( _("Height (pixels):") );
1663 height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 );
1665 zoom_label = gtk_label_new ( _("Zoom (meters per pixel):") );
1666 /* TODO: separate xzoom and yzoom factors */
1667 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);
1669 total_size_label = gtk_label_new ( NULL );
1671 current_window_button = gtk_button_new_with_label ( _("Area in current viewable window") );
1672 current_window_pass_along [0] = vw;
1673 current_window_pass_along [1] = width_spin;
1674 current_window_pass_along [2] = height_spin;
1675 current_window_pass_along [3] = zoom_spin;
1676 current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */
1677 current_window_pass_along [5] = NULL;
1678 current_window_pass_along [6] = total_size_label;
1679 g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along );
1681 png_radio = gtk_radio_button_new_with_label ( NULL, _("Save as PNG") );
1682 jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), _("Save as JPEG") );
1684 if ( ! vw->draw_image_save_as_png )
1685 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE );
1687 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0);
1688 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0);
1689 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0);
1690 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0);
1691 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0);
1692 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0);
1693 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0);
1694 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0);
1695 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0);
1697 if ( ! one_image_only )
1699 GtkWidget *tiles_width_label, *tiles_height_label;
1702 tiles_width_label = gtk_label_new ( _("East-west image tiles:") );
1703 tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1704 tiles_height_label = gtk_label_new ( _("North-south image tiles:") );
1705 tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 );
1706 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0);
1707 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0);
1708 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0);
1709 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0);
1711 current_window_pass_along [4] = tiles_width_spin;
1712 current_window_pass_along [5] = tiles_height_spin;
1713 g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1714 g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1716 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0);
1717 g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1718 g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1719 g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along );
1721 draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */
1723 gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox );
1725 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT )
1727 gtk_widget_hide ( GTK_WIDGET(dialog) );
1728 if ( one_image_only )
1729 save_image_file ( vw, fn,
1730 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1731 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1732 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1733 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) );
1734 else {
1735 if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM )
1736 save_image_dir ( vw, fn,
1737 vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ),
1738 vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ),
1739 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */
1740 vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ),
1741 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ),
1742 gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) );
1743 else
1744 a_dialog_error_msg ( GTK_WINDOW(vw), _("You must be in UTM mode to use this feature") );
1747 gtk_widget_destroy ( GTK_WIDGET(dialog) );
1751 static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
1753 const gchar *fn;
1754 if (!vw->save_img_dia) {
1755 vw->save_img_dia = gtk_file_selection_new ( _("Save Image") );
1756 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) );
1757 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE );
1760 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK )
1762 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) );
1763 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 ) ) )
1765 draw_to_image_file ( vw, fn, TRUE );
1766 break;
1769 gtk_widget_hide ( vw->save_img_dia );
1772 static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw )
1774 const gchar *fn;
1775 if (!vw->save_img_dir_dia) {
1776 vw->save_img_dir_dia = gtk_file_selection_new ( _("Choose a name for a new directory to hold images"));
1777 gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) );
1778 gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE );
1781 while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK )
1783 fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) );
1784 if ( access ( fn, F_OK ) == 0 )
1785 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) );
1786 else
1788 draw_to_image_file ( vw, fn, FALSE );
1789 break;
1792 gtk_widget_hide ( vw->save_img_dir_dia );
1795 #if GTK_CHECK_VERSION(2,10,0)
1796 static void print_cb ( GtkAction *a, VikWindow *vw )
1798 a_print(vw, vw->viking_vvp);
1800 #endif
1802 /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */
1803 static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw )
1805 VikViewportDrawMode drawmode;
1806 if (!strcmp(gtk_action_get_name(a), "ModeUTM")) {
1807 drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
1809 else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) {
1810 drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA;
1812 else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) {
1813 drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE;
1815 else if (!strcmp(gtk_action_get_name(a), "ModeKH")) {
1816 drawmode = VIK_VIEWPORT_DRAWMODE_KH;
1818 else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) {
1819 drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
1821 else {
1822 g_critical("Houston, we've had a problem.");
1823 return;
1826 if ( !vw->only_updating_coord_mode_ui )
1828 VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp );
1829 if ( olddrawmode != drawmode )
1831 /* this takes care of coord mode too */
1832 vik_viewport_set_drawmode ( vw->viking_vvp, drawmode );
1833 if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1834 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM );
1835 } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) {
1836 vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON );
1838 draw_update ( vw );
1843 static void set_draw_scale ( GtkAction *a, VikWindow *vw )
1845 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" );
1846 g_assert(check_box);
1847 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1848 vik_viewport_set_draw_scale ( vw->viking_vvp, state );
1849 draw_update ( vw );
1852 static void set_draw_centermark ( GtkAction *a, VikWindow *vw )
1854 GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" );
1855 g_assert(check_box);
1856 gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box));
1857 vik_viewport_set_draw_centermark ( vw->viking_vvp, state );
1858 draw_update ( vw );
1861 static void set_bg_color ( GtkAction *a, VikWindow *vw )
1863 GtkWidget *colorsd = gtk_color_selection_dialog_new ( _("Choose a background color") );
1864 GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp );
1865 gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1866 gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1867 if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK )
1869 gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color );
1870 vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color );
1871 draw_update ( vw );
1873 g_free ( color );
1874 gtk_widget_destroy ( colorsd );
1879 /***********************************************************************************************
1880 ** GUI Creation
1881 ***********************************************************************************************/
1883 static GtkActionEntry entries[] = {
1884 { "File", NULL, N_("_File"), 0, 0, 0 },
1885 { "Edit", NULL, N_("_Edit"), 0, 0, 0 },
1886 { "View", NULL, N_("_View"), 0, 0, 0 },
1887 { "SetZoom", NULL, N_("_Zoom"), 0, 0, 0 },
1888 { "SetPan", NULL, N_("_Pan"), 0, 0, 0 },
1889 { "Layers", NULL, N_("_Layers"), 0, 0, 0 },
1890 { "Tools", NULL, N_("_Tools"), 0, 0, 0 },
1891 { "Help", NULL, N_("_Help"), 0, 0, 0 },
1893 { "New", GTK_STOCK_NEW, N_("_New"), "<control>N", N_("New file"), (GCallback)newwindow_cb },
1894 { "Open", GTK_STOCK_OPEN, N_("_Open"), "<control>O", N_("Open a file"), (GCallback)load_file },
1895 { "Append", GTK_STOCK_ADD, N_("A_ppend File"), NULL, N_("Append data from a different file"), (GCallback)load_file },
1896 { "Acquire", NULL, N_("A_cquire"), 0, 0, 0 },
1897 { "AcquireGPS", NULL, N_("From _GPS"), NULL, N_("Transfer data from a GPS device"), (GCallback)acquire_from_gps },
1898 { "AcquireGoogle", NULL, N_("Google _Directions"), NULL, N_("Get driving directions from Google"), (GCallback)acquire_from_google },
1899 #ifdef VIK_CONFIG_GEOCACHES
1900 { "AcquireGC", NULL, N_("Geo_caches"), NULL, N_("Get Geocaches from geocaching.com"), (GCallback)acquire_from_gc },
1901 #endif
1902 { "Save", GTK_STOCK_SAVE, N_("_Save"), "<control>S", N_("Save the file"), (GCallback)save_file },
1903 { "SaveAs", GTK_STOCK_SAVE_AS, N_("Save _As"), NULL, N_("Save the file under different name"), (GCallback)save_file_as },
1904 { "GenImg", GTK_STOCK_CLEAR, N_("_Generate Image File"), NULL, N_("Save a snapshot of the workspace into a file"), (GCallback)draw_to_image_file_cb },
1905 { "GenImgDir", GTK_STOCK_DND_MULTIPLE, N_("Generate _Directory of Images"), NULL, N_("FIXME:IMGDIR"), (GCallback)draw_to_image_dir_cb },
1907 #if GTK_CHECK_VERSION(2,10,0)
1908 { "Print", GTK_STOCK_PRINT, N_("_Print..."), NULL, N_("Print maps"), (GCallback)print_cb },
1909 #endif
1911 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>W", N_("Exit the program"), (GCallback)window_close },
1912 { "SaveExit", GTK_STOCK_QUIT, N_("Save and Exit"), NULL, N_("Save and Exit the program"), (GCallback)save_file_and_exit },
1914 { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, N_("Go To Google Maps location"), NULL, N_("Go to address/place using Google Maps search"), (GCallback)goto_address },
1915 { "GotoLL", GTK_STOCK_QUIT, N_("_Go to Lat\\/Lon..."), NULL, N_("Go to arbitrary lat\\/lon coordinate"), (GCallback)draw_goto_cb },
1916 { "GotoUTM", GTK_STOCK_QUIT, N_("Go to UTM..."), NULL, N_("Go to arbitrary UTM coordinate"), (GCallback)draw_goto_cb },
1917 { "SetBGColor",GTK_STOCK_SELECT_COLOR, N_("Set Background Color..."), NULL, NULL, (GCallback)set_bg_color },
1918 { "ZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom _In"), "<control>plus", NULL, (GCallback)draw_zoom_cb },
1919 { "ZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), "<control>minus", NULL, (GCallback)draw_zoom_cb },
1920 { "ZoomTo", GTK_STOCK_ZOOM_FIT, N_("Zoom _To"), "<control><shift>Z", NULL, (GCallback)zoom_to_cb },
1921 { "Zoom0.25", NULL, N_("0.25"), NULL, NULL, (GCallback)draw_zoom_cb },
1922 { "Zoom0.5", NULL, N_("0.5"), NULL, NULL, (GCallback)draw_zoom_cb },
1923 { "Zoom1", NULL, N_("1"), NULL, NULL, (GCallback)draw_zoom_cb },
1924 { "Zoom2", NULL, N_("2"), NULL, NULL, (GCallback)draw_zoom_cb },
1925 { "Zoom4", NULL, N_("4"), NULL, NULL, (GCallback)draw_zoom_cb },
1926 { "Zoom8", NULL, N_("8"), NULL, NULL, (GCallback)draw_zoom_cb },
1927 { "Zoom16", NULL, N_("16"), NULL, NULL, (GCallback)draw_zoom_cb },
1928 { "Zoom32", NULL, N_("32"), NULL, NULL, (GCallback)draw_zoom_cb },
1929 { "Zoom64", NULL, N_("64"), NULL, NULL, (GCallback)draw_zoom_cb },
1930 { "Zoom128", NULL, N_("128"), NULL, NULL, (GCallback)draw_zoom_cb },
1931 { "PanNorth", NULL, N_("Pan North"), "<control>Up", NULL, (GCallback)draw_pan_cb },
1932 { "PanEast", NULL, N_("Pan East"), "<control>Right", NULL, (GCallback)draw_pan_cb },
1933 { "PanSouth", NULL, N_("Pan South"), "<control>Down", NULL, (GCallback)draw_pan_cb },
1934 { "PanWest", NULL, N_("Pan West"), "<control>Left", NULL, (GCallback)draw_pan_cb },
1935 { "BGJobs", GTK_STOCK_EXECUTE, N_("Background _Jobs"), NULL, NULL, (GCallback)a_background_show_window },
1937 { "Cut", GTK_STOCK_CUT, N_("Cu_t"), NULL, NULL, (GCallback)menu_cut_layer_cb },
1938 { "Copy", GTK_STOCK_COPY, N_("_Copy"), NULL, NULL, (GCallback)menu_copy_layer_cb },
1939 { "Paste", GTK_STOCK_PASTE, N_("_Paste"), NULL, NULL, (GCallback)menu_paste_layer_cb },
1940 { "Delete", GTK_STOCK_DELETE, N_("_Delete"), NULL, NULL, (GCallback)menu_delete_layer_cb },
1941 { "DeleteAll", NULL, N_("Delete All"), NULL, NULL, (GCallback)clear_cb },
1942 { "Preferences",GTK_STOCK_PREFERENCES, N_("_Preferences..."), NULL, NULL, (GCallback)preferences_cb },
1943 { "Properties",GTK_STOCK_PROPERTIES, N_("_Properties"), NULL, NULL, (GCallback)menu_properties_cb },
1945 { "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, (GCallback)help_about_cb },
1948 /* Radio items */
1949 static GtkRadioActionEntry mode_entries[] = {
1950 { "ModeUTM", NULL, N_("_UTM Mode"), "<control>u", NULL, 0 },
1951 { "ModeExpedia", NULL, N_("_Expedia Mode"), "<control>e", NULL, 1 },
1952 { "ModeGoogle", NULL, N_("_Old Google Mode"), "<control>o", NULL, 2 },
1953 { "ModeKH", NULL, N_("Old _KH Mode"), "<control>k", NULL, 3 },
1954 { "ModeMercator", NULL, N_("_Google Mode"), "<control>g", NULL, 4 }
1957 static GtkRadioActionEntry tool_entries[] = {
1958 { "Pan", "vik-icon-pan", N_("_Pan"), "<control><shift>P", N_("Pan Tool"), 0 },
1959 { "Zoom", "vik-icon-zoom", N_("_Zoom"), "<control><shift>Z", N_("Zoom Tool"), 1 },
1960 { "Ruler", "vik-icon-ruler", N_("_Ruler"), "<control><shift>R", N_("Ruler Tool"), 2 }
1963 static GtkToggleActionEntry toggle_entries[] = {
1964 { "ShowScale", NULL, N_("Show Scale"), NULL, NULL, (GCallback)set_draw_scale, TRUE },
1965 { "ShowCenterMark", NULL, N_("Show Center Mark"), NULL, NULL, (GCallback)set_draw_centermark, TRUE },
1966 { "FullScreen", NULL, N_("Full Screen"), "F11", NULL, (GCallback)full_screen_cb, FALSE },
1969 #include "menu.xml.h"
1970 static void window_create_ui( VikWindow *window )
1972 GtkUIManager *uim;
1973 GtkActionGroup *action_group;
1974 GtkAccelGroup *accel_group;
1975 GError *error;
1976 guint i, j, mid;
1977 GtkIconFactory *icon_factory;
1978 GtkIconSet *icon_set;
1979 GtkRadioActionEntry *tools = NULL, *radio;
1980 guint ntools;
1982 uim = gtk_ui_manager_new ();
1983 window->uim = uim;
1985 toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE);
1986 toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE);
1987 toolbox_add_tool(window->vt, &pan_tool, TOOL_LAYER_TYPE_NONE);
1989 error = NULL;
1990 if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) {
1991 g_error_free (error);
1992 exit (1);
1995 action_group = gtk_action_group_new ("MenuActions");
1996 gtk_action_group_set_translation_domain(action_group, PACKAGE_NAME);
1997 gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window);
1998 gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window);
1999 gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window);
2001 icon_factory = gtk_icon_factory_new ();
2002 gtk_icon_factory_add_default (icon_factory);
2004 register_vik_icons(icon_factory);
2006 ntools = 0;
2007 for (i=0; i<G_N_ELEMENTS(tool_entries); i++) {
2008 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2009 radio = &tools[ntools];
2010 ntools++;
2011 *radio = tool_entries[i];
2012 radio->value = ntools;
2015 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2016 GtkActionEntry action;
2017 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/",
2018 vik_layer_get_interface(i)->name,
2019 vik_layer_get_interface(i)->name,
2020 GTK_UI_MANAGER_MENUITEM, FALSE);
2022 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL ));
2023 gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set);
2024 gtk_icon_set_unref (icon_set);
2026 action.name = vik_layer_get_interface(i)->name;
2027 action.stock_id = vik_layer_get_interface(i)->name;
2028 action.label = g_strdup_printf( _("New %s Layer"), vik_layer_get_interface(i)->name);
2029 action.accelerator = NULL;
2030 action.tooltip = NULL;
2031 action.callback = (GCallback)menu_addlayer_cb;
2032 gtk_action_group_add_actions(action_group, &action, 1, window);
2034 if ( vik_layer_get_interface(i)->tools_count ) {
2035 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2036 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE);
2039 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2040 tools = g_renew(GtkRadioActionEntry, tools, ntools+1);
2041 radio = &tools[ntools];
2042 ntools++;
2044 gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools",
2045 _(vik_layer_get_interface(i)->tools[j].name),
2046 vik_layer_get_interface(i)->tools[j].name,
2047 GTK_UI_MANAGER_MENUITEM, FALSE);
2048 gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems",
2049 _(vik_layer_get_interface(i)->tools[j].name),
2050 vik_layer_get_interface(i)->tools[j].name,
2051 GTK_UI_MANAGER_TOOLITEM, FALSE);
2053 toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i);
2055 radio->name = vik_layer_get_interface(i)->tools[j].name;
2056 radio->stock_id = vik_layer_get_interface(i)->tools[j].name,
2057 radio->label = _(vik_layer_get_interface(i)->tools[j].name);
2058 radio->accelerator = NULL;
2059 radio->tooltip = _(vik_layer_get_interface(i)->tools[j].name);
2060 radio->value = ntools;
2063 g_object_unref (icon_factory);
2065 gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window);
2066 g_free(tools);
2068 gtk_ui_manager_insert_action_group (uim, action_group, 0);
2070 for (i=0; i<VIK_LAYER_NUM_TYPES; i++) {
2071 for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) {
2072 GtkAction *action = gtk_action_group_get_action(action_group,
2073 vik_layer_get_interface(i)->tools[j].name);
2074 g_object_set(action, "sensitive", FALSE, NULL);
2077 window->action_group = action_group;
2079 accel_group = gtk_ui_manager_get_accel_group (uim);
2080 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
2081 gtk_ui_manager_ensure_update (uim);
2086 static struct {
2087 const GdkPixdata *data;
2088 gchar *stock_id;
2089 } stock_icons[] = {
2090 { &addtr_18, "Create Track" },
2091 { &begintr_18, "Begin Track" },
2092 { &edtr_18, "Edit Trackpoint" },
2093 { &addwp_18, "Create Waypoint" },
2094 { &edwp_18, "Edit Waypoint" },
2095 { &iscissors_18, "Magic Scissors" },
2096 { &mover_22, "vik-icon-pan" },
2097 { &zoom_18, "vik-icon-zoom" },
2098 { &ruler_18, "vik-icon-ruler" },
2099 { &geozoom_18, "Georef Zoom Tool" },
2100 { &geomove_18, "Georef Move Map" },
2101 { &mapdl_18, "Maps Download" },
2102 { &demdl_18, "DEM Download/Import" },
2103 { &showpic_18, "Show Picture" },
2106 static gint n_stock_icons = G_N_ELEMENTS (stock_icons);
2108 static void
2109 register_vik_icons (GtkIconFactory *icon_factory)
2111 GtkIconSet *icon_set;
2112 gint i;
2114 for (i = 0; i < n_stock_icons; i++) {
2115 icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (
2116 stock_icons[i].data, FALSE, NULL ));
2117 gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set);
2118 gtk_icon_set_unref (icon_set);