]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | * | |
20 | */ | |
50a14534 EB |
21 | #include "viking.h" |
22 | #include "background.h" | |
1d1bc3c1 | 23 | #include "acquire.h" |
7b3479e3 | 24 | #include "datasources.h" |
369126f3 | 25 | #include "googlesearch.h" |
071da616 | 26 | #include "dems.h" |
50a14534 | 27 | |
8c3faf1b | 28 | #define VIKING_TITLE " - Viking" |
50a14534 | 29 | |
9b4c77ad | 30 | #include <glib/gprintf.h> |
e4afc73a | 31 | #include <stdlib.h> |
50a14534 EB |
32 | #include <math.h> |
33 | #include <string.h> | |
34 | #include <ctype.h> | |
1d1bc3c1 | 35 | #include <glib/gprintf.h> |
50a14534 EB |
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 | |
44 | ||
3570ad57 QT |
45 | #define VIKING_WINDOW_WIDTH 1000 |
46 | #define VIKING_WINDOW_HEIGHT 800 | |
50a14534 EB |
47 | #define DRAW_IMAGE_DEFAULT_WIDTH 1280 |
48 | #define DRAW_IMAGE_DEFAULT_HEIGHT 1024 | |
49 | #define DRAW_IMAGE_DEFAULT_SAVE_AS_PNG TRUE | |
50 | ||
51 | static void window_finalize ( GObject *gob ); | |
52 | static GObjectClass *parent_class; | |
53 | ||
54 | static void window_init ( VikWindow *vw ); | |
55 | static void window_class_init ( VikWindowClass *klass ); | |
56 | ||
57 | static void draw_update ( VikWindow *vw ); | |
58 | ||
e4afc73a | 59 | static void newwindow_cb ( GtkAction *a, VikWindow *vw ); |
50a14534 EB |
60 | |
61 | /* Drawing & stuff */ | |
62 | ||
63 | static gboolean delete_event( VikWindow *vw ); | |
64 | ||
65 | static void draw_sync ( VikWindow *vw ); | |
66 | static void draw_redraw ( VikWindow *vw ); | |
941aa6e9 | 67 | static void draw_scroll ( VikWindow *vw, GdkEventScroll *event ); |
50a14534 EB |
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 ); | |
e4afc73a EB |
71 | static void draw_zoom_cb ( GtkAction *a, VikWindow *vw ); |
72 | static void draw_goto_cb ( GtkAction *a, VikWindow *vw ); | |
50a14534 EB |
73 | |
74 | static void draw_status (); | |
75 | ||
76 | /* End Drawing Functions */ | |
77 | ||
e4afc73a EB |
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 ); | |
941aa6e9 AF |
81 | |
82 | /* tool management */ | |
83 | typedef struct { | |
84 | VikToolInterface ti; | |
85 | gpointer state; | |
9593a4c9 | 86 | gint layer_type; |
941aa6e9 | 87 | } toolbox_tool_t; |
9593a4c9 | 88 | #define TOOL_LAYER_TYPE_NONE -1 |
941aa6e9 AF |
89 | |
90 | typedef struct { | |
91 | int active_tool; | |
92 | int n_tools; | |
93 | toolbox_tool_t *tools; | |
94 | VikWindow *vw; | |
95 | } toolbox_tools_t; | |
96 | ||
e4afc73a | 97 | static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw ); |
941aa6e9 | 98 | static toolbox_tools_t* toolbox_create(VikWindow *vw); |
9593a4c9 | 99 | static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type ); |
941aa6e9 AF |
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); | |
105 | ||
50a14534 | 106 | |
941aa6e9 | 107 | /* ui creation */ |
e4afc73a | 108 | static void window_create_ui( VikWindow *window ); |
941aa6e9 | 109 | static void register_vik_icons (GtkIconFactory *icon_factory); |
50a14534 | 110 | |
941aa6e9 | 111 | /* i/o */ |
e4afc73a EB |
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 ); | |
2bf7cadd | 115 | static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw ); |
50a14534 EB |
116 | static gboolean window_save ( VikWindow *vw ); |
117 | ||
118 | struct _VikWindow { | |
119 | GtkWindow gtkwindow; | |
120 | VikViewport *viking_vvp; | |
121 | VikLayersPanel *viking_vlp; | |
122 | VikStatusbar *viking_vs; | |
123 | ||
e4afc73a EB |
124 | GtkToolbar *toolbar; |
125 | ||
50a14534 EB |
126 | GtkItemFactory *item_factory; |
127 | ||
941aa6e9 | 128 | /* tool management state */ |
50a14534 | 129 | guint current_tool; |
941aa6e9 | 130 | toolbox_tools_t *vt; |
50a14534 EB |
131 | guint16 tool_layer_id; |
132 | guint16 tool_tool_id; | |
133 | ||
79845167 QT |
134 | GtkActionGroup *action_group; |
135 | ||
50a14534 EB |
136 | gint pan_x, pan_y; |
137 | ||
138 | guint draw_image_width, draw_image_height; | |
139 | gboolean draw_image_save_as_png; | |
140 | ||
141 | gchar *filename; | |
142 | gboolean modified; | |
143 | ||
144 | GtkWidget *open_dia, *save_dia; | |
f2a1ca71 | 145 | GtkWidget *save_img_dia, *save_img_dir_dia; |
50a14534 EB |
146 | |
147 | gboolean only_updating_coord_mode_ui; /* hack for a bug in GTK */ | |
e4afc73a | 148 | GtkUIManager *uim; |
c9177aae QT |
149 | |
150 | /* half-drawn update */ | |
151 | VikLayer *trigger; | |
152 | VikCoord trigger_center; | |
50a14534 EB |
153 | }; |
154 | ||
155 | enum { | |
156 | TOOL_ZOOM = 0, | |
157 | TOOL_RULER, | |
158 | TOOL_LAYER, | |
159 | NUMBER_OF_TOOLS | |
160 | }; | |
161 | ||
162 | enum { | |
163 | VW_NEWWINDOW_SIGNAL, | |
164 | VW_OPENWINDOW_SIGNAL, | |
165 | VW_LAST_SIGNAL | |
166 | }; | |
167 | ||
168 | static guint window_signals[VW_LAST_SIGNAL] = { 0 }; | |
169 | ||
170 | static gchar *tool_names[NUMBER_OF_TOOLS] = { "Zoom", "Ruler", "Pan" }; | |
171 | ||
172 | GType vik_window_get_type (void) | |
173 | { | |
174 | static GType vw_type = 0; | |
175 | ||
176 | if (!vw_type) | |
177 | { | |
178 | static const GTypeInfo vw_info = | |
179 | { | |
180 | sizeof (VikWindowClass), | |
181 | NULL, /* base_init */ | |
182 | NULL, /* base_finalize */ | |
183 | (GClassInitFunc) window_class_init, /* class_init */ | |
184 | NULL, /* class_finalize */ | |
185 | NULL, /* class_data */ | |
186 | sizeof (VikWindow), | |
187 | 0, | |
188 | (GInstanceInitFunc) window_init, | |
189 | }; | |
190 | vw_type = g_type_register_static ( GTK_TYPE_WINDOW, "VikWindow", &vw_info, 0 ); | |
191 | } | |
192 | ||
193 | return vw_type; | |
194 | } | |
195 | ||
014128f6 QT |
196 | VikViewport * vik_window_viewport(VikWindow *vw) |
197 | { | |
198 | return(vw->viking_vvp); | |
199 | } | |
200 | ||
79845167 QT |
201 | void vik_window_selected_layer(VikWindow *vw, VikLayer *vl) |
202 | { | |
203 | int i, j, tool_count; | |
204 | VikLayerInterface *layer_interface; | |
205 | ||
206 | if (!vw->action_group) return; | |
207 | ||
208 | for (i=0; i<VIK_LAYER_NUM_TYPES; i++) { | |
209 | GtkAction *action; | |
210 | layer_interface = vik_layer_get_interface(i); | |
211 | tool_count = layer_interface->tools_count; | |
212 | ||
213 | for (j = 0; j < tool_count; j++) { | |
214 | action = gtk_action_group_get_action(vw->action_group, | |
215 | layer_interface->tools[j].name); | |
216 | g_object_set(action, "sensitive", i == vl->type, NULL); | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
50a14534 EB |
221 | static void window_finalize ( GObject *gob ) |
222 | { | |
223 | VikWindow *vw = VIK_WINDOW(gob); | |
224 | g_return_if_fail ( vw != NULL ); | |
225 | ||
226 | a_background_remove_status ( vw->viking_vs ); | |
227 | ||
228 | G_OBJECT_CLASS(parent_class)->finalize(gob); | |
229 | } | |
230 | ||
231 | static void window_class_init ( VikWindowClass *klass ) | |
232 | { | |
233 | /* destructor */ | |
234 | GObjectClass *object_class; | |
235 | ||
236 | 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); | |
8c4f1350 | 237 | 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); |
50a14534 EB |
238 | |
239 | object_class = G_OBJECT_CLASS (klass); | |
240 | ||
241 | object_class->finalize = window_finalize; | |
242 | ||
243 | parent_class = g_type_class_peek_parent (klass); | |
244 | ||
245 | } | |
246 | ||
247 | static void window_init ( VikWindow *vw ) | |
248 | { | |
249 | GtkWidget *main_vbox; | |
250 | GtkWidget *hpaned; | |
251 | ||
79845167 | 252 | vw->action_group = NULL; |
50a14534 | 253 | |
24277274 | 254 | vw->viking_vvp = vik_viewport_new(); |
50a14534 EB |
255 | vw->viking_vlp = vik_layers_panel_new(); |
256 | vik_layers_panel_set_viewport ( vw->viking_vlp, vw->viking_vvp ); | |
257 | vw->viking_vs = vik_statusbar_new(); | |
258 | ||
941aa6e9 AF |
259 | vw->vt = toolbox_create(vw); |
260 | window_create_ui(vw); | |
261 | gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE ); | |
262 | ||
263 | toolbox_activate(vw->vt, "Zoom"); | |
264 | ||
50a14534 | 265 | vw->filename = NULL; |
e4afc73a | 266 | vw->item_factory = NULL; |
50a14534 | 267 | |
50a14534 EB |
268 | vw->modified = FALSE; |
269 | vw->only_updating_coord_mode_ui = FALSE; | |
941aa6e9 | 270 | |
50a14534 EB |
271 | |
272 | vw->pan_x = vw->pan_y = -1; | |
273 | vw->draw_image_width = DRAW_IMAGE_DEFAULT_WIDTH; | |
274 | vw->draw_image_height = DRAW_IMAGE_DEFAULT_HEIGHT; | |
275 | vw->draw_image_save_as_png = DRAW_IMAGE_DEFAULT_SAVE_AS_PNG; | |
276 | ||
277 | main_vbox = gtk_vbox_new(FALSE, 1); | |
278 | gtk_container_add (GTK_CONTAINER (vw), main_vbox); | |
279 | ||
e4afc73a EB |
280 | gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainMenu"), FALSE, TRUE, 0); |
281 | gtk_box_pack_start (GTK_BOX(main_vbox), gtk_ui_manager_get_widget (vw->uim, "/MainToolbar"), FALSE, TRUE, 0); | |
886031df AF |
282 | gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR); |
283 | gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS); | |
50a14534 EB |
284 | |
285 | g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL); | |
286 | ||
287 | g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "expose_event", G_CALLBACK(draw_sync), vw); | |
288 | g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "configure_event", G_CALLBACK(draw_redraw), vw); | |
289 | gtk_widget_add_events ( GTK_WIDGET(vw->viking_vvp), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK ); | |
290 | g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "scroll_event", G_CALLBACK(draw_scroll), vw); | |
291 | g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_press_event", G_CALLBACK(draw_click), vw); | |
292 | g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "button_release_event", G_CALLBACK(draw_release), vw); | |
293 | g_signal_connect_swapped (G_OBJECT(vw->viking_vvp), "motion_notify_event", G_CALLBACK(draw_mouse_motion), vw); | |
294 | g_signal_connect_swapped (G_OBJECT(vw->viking_vlp), "update", G_CALLBACK(draw_update), vw); | |
295 | ||
9a3cdf12 | 296 | gtk_window_set_default_size ( GTK_WINDOW(vw), VIKING_WINDOW_WIDTH, VIKING_WINDOW_HEIGHT); |
50a14534 EB |
297 | |
298 | hpaned = gtk_hpaned_new (); | |
299 | gtk_paned_add1 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vlp) ); | |
300 | gtk_paned_add2 ( GTK_PANED(hpaned), GTK_WIDGET (vw->viking_vvp) ); | |
301 | ||
302 | /* This packs the button into the window (a gtk container). */ | |
303 | gtk_box_pack_start (GTK_BOX(main_vbox), hpaned, TRUE, TRUE, 0); | |
304 | ||
305 | gtk_box_pack_end (GTK_BOX(main_vbox), GTK_WIDGET(vw->viking_vs), FALSE, TRUE, 0); | |
306 | ||
307 | a_background_add_status(vw->viking_vs); | |
308 | ||
309 | vw->open_dia = NULL; | |
310 | vw->save_dia = NULL; | |
f2a1ca71 QT |
311 | vw->save_img_dia = NULL; |
312 | vw->save_img_dir_dia = NULL; | |
50a14534 EB |
313 | } |
314 | ||
315 | VikWindow *vik_window_new () | |
316 | { | |
317 | return VIK_WINDOW ( g_object_new ( VIK_WINDOW_TYPE, NULL ) ); | |
318 | } | |
319 | ||
320 | static gboolean delete_event( VikWindow *vw ) | |
321 | { | |
a5fd2196 | 322 | #ifdef VIKING_PROMPT_IF_MODIFIED |
50a14534 | 323 | if ( vw->modified ) |
a5fd2196 QT |
324 | #else |
325 | if (0) | |
326 | #endif | |
50a14534 EB |
327 | { |
328 | GtkDialog *dia; | |
329 | dia = GTK_DIALOG ( gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, | |
330 | "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.", | |
331 | vw->filename ? a_file_basename ( vw->filename ) : "Untitled" ) ); | |
332 | gtk_dialog_add_buttons ( dia, "Don't Save", GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL ); | |
333 | switch ( gtk_dialog_run ( dia ) ) | |
334 | { | |
335 | case GTK_RESPONSE_NO: gtk_widget_destroy ( GTK_WIDGET(dia) ); return FALSE; | |
336 | case GTK_RESPONSE_CANCEL: gtk_widget_destroy ( GTK_WIDGET(dia) ); return TRUE; | |
e4afc73a | 337 | default: gtk_widget_destroy ( GTK_WIDGET(dia) ); return ! save_file(NULL, vw); |
50a14534 EB |
338 | } |
339 | } | |
340 | return FALSE; | |
341 | } | |
342 | ||
343 | /* Drawing stuff */ | |
e4afc73a | 344 | static void newwindow_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
345 | { |
346 | g_signal_emit ( G_OBJECT(vw), window_signals[VW_NEWWINDOW_SIGNAL], 0 ); | |
347 | } | |
348 | ||
349 | static void draw_update ( VikWindow *vw ) | |
350 | { | |
351 | draw_redraw (vw); | |
352 | draw_sync (vw); | |
353 | } | |
354 | ||
355 | static void draw_sync ( VikWindow *vw ) | |
356 | { | |
357 | vik_viewport_sync(vw->viking_vvp); | |
358 | draw_status ( vw ); | |
359 | /* other things may be necc here later. */ | |
360 | } | |
361 | ||
362 | static void draw_status ( VikWindow *vw ) | |
363 | { | |
364 | static gchar zoom_level[22]; | |
365 | 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" ); | |
366 | if ( vw->current_tool == TOOL_LAYER ) | |
367 | vik_statusbar_set_message ( vw->viking_vs, 0, vik_layer_get_interface(vw->tool_layer_id)->tools[vw->tool_tool_id].name ); | |
368 | else | |
369 | vik_statusbar_set_message ( vw->viking_vs, 0, tool_names[vw->current_tool] ); | |
370 | ||
371 | vik_statusbar_set_message ( vw->viking_vs, 2, zoom_level ); | |
372 | } | |
373 | ||
c9177aae QT |
374 | void vik_window_set_redraw_trigger(VikLayer *vl) |
375 | { | |
376 | VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vl)); | |
377 | vw->trigger = vl; | |
378 | } | |
379 | ||
50a14534 EB |
380 | static void draw_redraw ( VikWindow *vw ) |
381 | { | |
c9177aae QT |
382 | VikCoord old_center = vw->trigger_center; |
383 | vw->trigger_center = *(vik_viewport_get_center(vw->viking_vvp)); | |
384 | VikLayer *new_trigger = vw->trigger; | |
385 | vw->trigger = NULL; | |
0df66d57 EB |
386 | VikLayer *old_trigger = VIK_LAYER(vik_viewport_get_trigger(vw->viking_vvp)); |
387 | ||
388 | if ( ! new_trigger ) | |
389 | ; /* do nothing -- have to redraw everything. */ | |
c9177aae | 390 | else if ( (old_trigger != new_trigger) || !vik_coord_equals(&old_center, &vw->trigger_center) ) |
0df66d57 EB |
391 | vik_viewport_set_trigger ( vw->viking_vvp, new_trigger ); /* todo: set to half_drawn mode if new trigger is above old */ |
392 | else | |
393 | vik_viewport_set_half_drawn ( vw->viking_vvp, TRUE ); | |
394 | ||
395 | /* actually draw */ | |
50a14534 | 396 | vik_viewport_clear ( vw->viking_vvp); |
50a14534 | 397 | vik_layers_panel_draw_all ( vw->viking_vlp ); |
acaf7113 | 398 | vik_viewport_draw_scale ( vw->viking_vvp ); |
c933487f | 399 | vik_viewport_draw_centermark ( vw->viking_vvp ); |
0df66d57 EB |
400 | |
401 | vik_viewport_set_half_drawn ( vw->viking_vvp, FALSE ); /* just in case. */ | |
50a14534 EB |
402 | } |
403 | ||
941aa6e9 AF |
404 | gboolean draw_buf_done = TRUE; |
405 | ||
406 | static gboolean draw_buf(gpointer data) | |
407 | { | |
408 | gpointer *pass_along = data; | |
409 | gdk_threads_enter(); | |
410 | gdk_draw_drawable (pass_along[0], pass_along[1], | |
411 | pass_along[2], 0, 0, 0, 0, -1, -1); | |
412 | draw_buf_done = TRUE; | |
413 | gdk_threads_leave(); | |
414 | return FALSE; | |
415 | } | |
416 | ||
417 | ||
418 | /* Mouse event handlers ************************************************************************/ | |
419 | ||
420 | static void draw_click (VikWindow *vw, GdkEventButton *event) | |
421 | { | |
422 | ||
423 | /* middle button pressed. we reserve all middle button and scroll events | |
424 | * for panning and zooming; tools only get left/right/movement | |
425 | */ | |
426 | if ( event->button == 2) { | |
427 | /* set panning origin */ | |
428 | vw->pan_x = (gint) event->x; | |
429 | vw->pan_y = (gint) event->y; | |
430 | } | |
431 | else { | |
432 | toolbox_click(vw->vt, event); | |
433 | } | |
434 | } | |
435 | ||
436 | static void draw_mouse_motion (VikWindow *vw, GdkEventMotion *event) | |
437 | { | |
438 | static VikCoord coord; | |
439 | static struct UTM utm; | |
440 | static struct LatLon ll; | |
441 | static char pointer_buf[36]; | |
071da616 | 442 | gint16 alt; |
941aa6e9 AF |
443 | |
444 | toolbox_move(vw->vt, (GdkEventButton *)event); | |
445 | ||
446 | vik_viewport_screen_to_coord ( vw->viking_vvp, event->x, event->y, &coord ); | |
447 | vik_coord_to_utm ( &coord, &utm ); | |
448 | a_coords_utm_to_latlon ( &utm, &ll ); | |
071da616 QT |
449 | if ((alt = a_dems_get_elev_by_coord(&coord)) != VIK_DEM_INVALID_ELEVATION) |
450 | g_snprintf ( pointer_buf, 36, "Cursor: %f %f %dm", ll.lat, ll.lon, alt ); | |
451 | else | |
452 | g_snprintf ( pointer_buf, 36, "Cursor: %f %f", ll.lat, ll.lon ); | |
941aa6e9 AF |
453 | vik_statusbar_set_message ( vw->viking_vs, 4, pointer_buf ); |
454 | ||
455 | if ( vw->pan_x != -1 ) { | |
456 | vik_viewport_pan_sync ( vw->viking_vvp, event->x - vw->pan_x, event->y - vw->pan_y ); | |
457 | } | |
458 | } | |
459 | ||
460 | static void draw_release ( VikWindow *vw, GdkEventButton *event ) | |
461 | { | |
462 | if ( event->button == 2 ) { /* move / pan */ | |
463 | if ( ABS(vw->pan_x - event->x) <= 1 && ABS(vw->pan_y - event->y) <= 1 ) | |
464 | vik_viewport_set_center_screen ( vw->viking_vvp, vw->pan_x, vw->pan_y ); | |
465 | else | |
466 | vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2 - event->x + vw->pan_x, | |
467 | vik_viewport_get_height(vw->viking_vvp)/2 - event->y + vw->pan_y ); | |
468 | draw_update ( vw ); | |
469 | vw->pan_x = vw->pan_y = -1; | |
470 | } | |
471 | else { | |
472 | toolbox_release(vw->vt, event); | |
473 | } | |
474 | } | |
475 | ||
476 | static void draw_scroll (VikWindow *vw, GdkEventScroll *event) | |
477 | { | |
bddd2056 | 478 | if ( event->state == GDK_CONTROL_MASK ) { |
8c721f83 EB |
479 | /* control == pan up & down */ |
480 | if ( event->direction == GDK_SCROLL_UP ) | |
481 | vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp)/3 ); | |
482 | else | |
483 | 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 ); | |
bddd2056 | 484 | } else if ( event->state & GDK_SHIFT_MASK ) { |
8c721f83 EB |
485 | /* control-shift == pan left & right */ |
486 | if ( event->direction == GDK_SCROLL_UP ) | |
487 | vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/3, vik_viewport_get_height(vw->viking_vvp)/2 ); | |
488 | else | |
489 | 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 ); | |
490 | } else { | |
491 | if ( event->direction == GDK_SCROLL_UP ) | |
492 | vik_viewport_zoom_in (vw->viking_vvp); | |
493 | else | |
494 | vik_viewport_zoom_out(vw->viking_vvp); | |
495 | } | |
496 | ||
941aa6e9 AF |
497 | draw_update(vw); |
498 | } | |
499 | ||
500 | ||
501 | ||
502 | /******************************************************************************** | |
503 | ** Ruler tool code | |
504 | ********************************************************************************/ | |
e4847ce9 AF |
505 | static void draw_ruler(VikViewport *vvp, GdkDrawable *d, GdkGC *gc, gint x1, gint y1, gint x2, gint y2, gdouble distance) |
506 | { | |
507 | PangoFontDescription *pfd; | |
508 | PangoLayout *pl; | |
509 | gchar str[128]; | |
0dff88ea AF |
510 | GdkGC *labgc = vik_viewport_new_gc ( vvp, "#cccccc", 1); |
511 | GdkGC *thickgc = gdk_gc_new(d); | |
512 | ||
e4847ce9 AF |
513 | gdouble len = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); |
514 | gdouble dx = (x2-x1)/len*10; | |
515 | gdouble dy = (y2-y1)/len*10; | |
516 | gdouble c = cos(15.0 * M_PI/180.0); | |
517 | gdouble s = sin(15.0 * M_PI/180.0); | |
0dff88ea | 518 | gdouble angle; |
15614495 | 519 | gdouble baseangle = 0; |
0dff88ea | 520 | gint i; |
e4847ce9 | 521 | |
0dff88ea | 522 | /* draw line with arrow ends */ |
d9ffd267 EB |
523 | { |
524 | gint tmp_x1=x1, tmp_y1=y1, tmp_x2=x2, tmp_y2=y2; | |
525 | a_viewport_clip_line(&tmp_x1, &tmp_y1, &tmp_x2, &tmp_y2); | |
526 | gdk_draw_line(d, gc, tmp_x1, tmp_y1, tmp_x2, tmp_y2); | |
527 | } | |
528 | ||
15614495 AF |
529 | a_viewport_clip_line(&x1, &y1, &x2, &y2); |
530 | gdk_draw_line(d, gc, x1, y1, x2, y2); | |
d9ffd267 | 531 | |
e4847ce9 AF |
532 | gdk_draw_line(d, gc, x1 - dy, y1 + dx, x1 + dy, y1 - dx); |
533 | gdk_draw_line(d, gc, x2 - dy, y2 + dx, x2 + dy, y2 - dx); | |
534 | gdk_draw_line(d, gc, x2, y2, x2 - (dx * c + dy * s), y2 - (dy * c - dx * s)); | |
535 | gdk_draw_line(d, gc, x2, y2, x2 - (dx * c - dy * s), y2 - (dy * c + dx * s)); | |
536 | gdk_draw_line(d, gc, x1, y1, x1 + (dx * c + dy * s), y1 + (dy * c - dx * s)); | |
537 | gdk_draw_line(d, gc, x1, y1, x1 + (dx * c - dy * s), y1 + (dy * c + dx * s)); | |
538 | ||
0dff88ea AF |
539 | /* draw compass */ |
540 | #define CR 80 | |
541 | #define CW 4 | |
15614495 AF |
542 | angle = atan2(dy, dx) + M_PI_2; |
543 | ||
544 | if ( vik_viewport_get_drawmode ( vvp ) == VIK_VIEWPORT_DRAWMODE_UTM) { | |
545 | VikCoord test; | |
546 | struct LatLon ll; | |
547 | struct UTM u; | |
548 | gint tx, ty; | |
549 | ||
550 | vik_viewport_screen_to_coord ( vvp, x1, y1, &test ); | |
551 | vik_coord_to_latlon ( &test, &ll ); | |
552 | ll.lat += vik_viewport_get_ympp ( vvp ) * vik_viewport_get_height ( vvp ) / 11000.0; // about 11km per degree latitude | |
553 | a_coords_latlon_to_utm ( &ll, &u ); | |
554 | vik_coord_load_from_utm ( &test, VIK_VIEWPORT_DRAWMODE_UTM, &u ); | |
555 | vik_viewport_coord_to_screen ( vvp, &test, &tx, &ty ); | |
556 | ||
557 | baseangle = M_PI - atan2(tx-x1, ty-y1); | |
558 | angle -= baseangle; | |
559 | } | |
560 | ||
0dff88ea AF |
561 | if (angle<0) |
562 | angle+=2*M_PI; | |
563 | if (angle>2*M_PI) | |
564 | angle-=2*M_PI; | |
565 | ||
566 | { | |
567 | GdkColor color; | |
568 | gdk_gc_copy(thickgc, gc); | |
569 | gdk_gc_set_line_attributes(thickgc, CW, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER); | |
570 | gdk_color_parse("#2255cc", &color); | |
571 | gdk_gc_set_rgb_fg_color(thickgc, &color); | |
572 | } | |
15614495 | 573 | 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); |
0dff88ea | 574 | |
e4847ce9 | 575 | |
0dff88ea AF |
576 | gdk_gc_copy(thickgc, gc); |
577 | gdk_gc_set_line_attributes(thickgc, 2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER); | |
578 | for (i=0; i<180; i++) { | |
15614495 AF |
579 | c = cos(i*M_PI/90.0 + baseangle); |
580 | s = sin(i*M_PI/90.0 + baseangle); | |
e4847ce9 | 581 | |
0dff88ea AF |
582 | if (i%5) { |
583 | gdk_draw_line (d, gc, x1 + CR*c, y1 + CR*s, x1 + (CR+CW)*c, y1 + (CR+CW)*s); | |
584 | } else { | |
585 | gdouble ticksize = 2*CW; | |
586 | gdk_draw_line (d, thickgc, x1 + (CR-CW)*c, y1 + (CR-CW)*s, x1 + (CR+ticksize)*c, y1 + (CR+ticksize)*s); | |
587 | } | |
e4847ce9 | 588 | } |
0dff88ea AF |
589 | |
590 | gdk_draw_arc (d, gc, FALSE, x1-CR, y1-CR, 2*CR, 2*CR, 0, 64*360); | |
591 | gdk_draw_arc (d, gc, FALSE, x1-CR-CW, y1-CR-CW, 2*(CR+CW), 2*(CR+CW), 0, 64*360); | |
592 | gdk_draw_arc (d, gc, FALSE, x1-CR+CW, y1-CR+CW, 2*(CR-CW), 2*(CR-CW), 0, 64*360); | |
15614495 AF |
593 | c = (CR+CW*2)*cos(baseangle); |
594 | s = (CR+CW*2)*sin(baseangle); | |
595 | gdk_draw_line (d, gc, x1-c, y1-s, x1+c, y1+s); | |
596 | gdk_draw_line (d, gc, x1+s, y1-c, x1-s, y1+c); | |
0dff88ea AF |
597 | |
598 | /* draw labels */ | |
599 | #define LABEL(x, y, w, h) { \ | |
600 | gdk_draw_rectangle(d, labgc, TRUE, (x)-2, (y)-1, (w)+4, (h)+1); \ | |
601 | gdk_draw_rectangle(d, gc, FALSE, (x)-2, (y)-1, (w)+4, (h)+1); \ | |
602 | gdk_draw_layout(d, gc, (x), (y), pl); } | |
603 | { | |
604 | gint wd, hd, xd, yd; | |
605 | gint wb, hb, xb, yb; | |
606 | ||
607 | pl = gtk_widget_create_pango_layout (GTK_WIDGET(vvp), NULL); | |
608 | ||
609 | pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable? | |
610 | pango_layout_set_font_description (pl, pfd); | |
611 | pango_font_description_free (pfd); | |
612 | ||
613 | pango_layout_set_text(pl, "N", -1); | |
614 | gdk_draw_layout(d, gc, x1-5, y1-CR-3*CW-8, pl); | |
615 | ||
616 | /* draw label with distance */ | |
617 | if (distance >= 1000 && distance < 100000) { | |
15614495 | 618 | g_sprintf(str, "%3.2f km", distance/1000.0); |
0dff88ea | 619 | } else if (distance < 1000) { |
15614495 | 620 | g_sprintf(str, "%d m", (int)distance); |
0dff88ea | 621 | } else { |
15614495 | 622 | g_sprintf(str, "%d km", (int)distance/1000); |
0dff88ea AF |
623 | } |
624 | pango_layout_set_text(pl, str, -1); | |
625 | ||
626 | pango_layout_get_pixel_size ( pl, &wd, &hd ); | |
627 | if (dy>0) { | |
628 | xd = (x1+x2)/2 + dy; | |
629 | yd = (y1+y2)/2 - hd/2 - dx; | |
630 | } else { | |
631 | xd = (x1+x2)/2 - dy; | |
632 | yd = (y1+y2)/2 - hd/2 + dx; | |
633 | } | |
024f32c1 EB |
634 | |
635 | if ( xd < -5 || yd < -5 || xd > vik_viewport_get_width(vvp)+5 || yd > vik_viewport_get_height(vvp)+5 ) { | |
636 | xd = x2 + 10; | |
637 | yd = y2 - 5; | |
638 | } | |
639 | ||
0dff88ea AF |
640 | LABEL(xd, yd, wd, hd); |
641 | ||
642 | /* draw label with bearing */ | |
15614495 | 643 | g_sprintf(str, "%3.1f°", angle*180.0/M_PI); |
0dff88ea AF |
644 | pango_layout_set_text(pl, str, -1); |
645 | pango_layout_get_pixel_size ( pl, &wb, &hb ); | |
646 | xb = x1 + CR*cos(angle-M_PI_2); | |
647 | yb = y1 + CR*sin(angle-M_PI_2); | |
648 | ||
024f32c1 EB |
649 | if ( xb < -5 || yb < -5 || xb > vik_viewport_get_width(vvp)+5 || yb > vik_viewport_get_height(vvp)+5 ) { |
650 | xb = x2 + 10; | |
651 | yb = y2 + 10; | |
652 | } | |
653 | ||
0dff88ea AF |
654 | { |
655 | GdkRectangle r1 = {xd-2, yd-1, wd+4, hd+1}, r2 = {xb-2, yb-1, wb+4, hb+1}; | |
656 | if (gdk_rectangle_intersect(&r1, &r2, &r2)) { | |
657 | xb = xd + wd + 5; | |
658 | } | |
659 | } | |
660 | LABEL(xb, yb, wb, hb); | |
e4847ce9 | 661 | } |
03d62e57 | 662 | #undef LABEL |
e4847ce9 | 663 | |
024f32c1 | 664 | g_object_unref ( G_OBJECT ( pl ) ); |
0dff88ea AF |
665 | g_object_unref ( G_OBJECT ( labgc ) ); |
666 | g_object_unref ( G_OBJECT ( thickgc ) ); | |
e4847ce9 AF |
667 | } |
668 | ||
941aa6e9 AF |
669 | typedef struct { |
670 | VikWindow *vw; | |
671 | VikViewport *vvp; | |
672 | gboolean has_oldcoord; | |
673 | VikCoord oldcoord; | |
674 | } ruler_tool_state_t; | |
03d62e57 | 675 | |
941aa6e9 | 676 | static gpointer ruler_create (VikWindow *vw, VikViewport *vvp) |
03d62e57 | 677 | { |
941aa6e9 AF |
678 | ruler_tool_state_t *s = g_new(ruler_tool_state_t, 1); |
679 | s->vw = vw; | |
680 | s->vvp = vvp; | |
681 | s->has_oldcoord = FALSE; | |
682 | return s; | |
03d62e57 AF |
683 | } |
684 | ||
941aa6e9 | 685 | static void ruler_destroy (ruler_tool_state_t *s) |
50a14534 | 686 | { |
941aa6e9 AF |
687 | g_free(s); |
688 | } | |
50a14534 | 689 | |
941aa6e9 AF |
690 | static VikLayerToolFuncStatus ruler_click (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s) |
691 | { | |
692 | struct LatLon ll; | |
693 | VikCoord coord; | |
694 | gchar *temp; | |
695 | if ( event->button == 1 ) { | |
696 | vik_viewport_screen_to_coord ( s->vvp, (gint) event->x, (gint) event->y, &coord ); | |
697 | vik_coord_to_latlon ( &coord, &ll ); | |
698 | if ( s->has_oldcoord ) { | |
699 | temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) ); | |
700 | s->has_oldcoord = FALSE; | |
701 | } | |
702 | else { | |
703 | temp = g_strdup_printf ( "%f %f", ll.lat, ll.lon ); | |
704 | s->has_oldcoord = TRUE; | |
705 | } | |
50a14534 | 706 | |
941aa6e9 AF |
707 | vik_statusbar_set_message ( s->vw->viking_vs, 3, temp ); |
708 | g_free ( temp ); | |
e4847ce9 | 709 | |
941aa6e9 AF |
710 | s->oldcoord = coord; |
711 | } | |
712 | else { | |
713 | vik_viewport_set_center_screen ( s->vvp, (gint) event->x, (gint) event->y ); | |
714 | draw_update ( s->vw ); | |
715 | } | |
716 | return VIK_LAYER_TOOL_ACK; | |
717 | } | |
e4847ce9 | 718 | |
941aa6e9 AF |
719 | static VikLayerToolFuncStatus ruler_move (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s) |
720 | { | |
721 | VikViewport *vvp = s->vvp; | |
722 | VikWindow *vw = s->vw; | |
723 | ||
724 | struct LatLon ll; | |
725 | VikCoord coord; | |
726 | gchar *temp; | |
727 | ||
728 | if ( s->has_oldcoord ) { | |
729 | int oldx, oldy, w1, h1, w2, h2; | |
730 | static GdkPixmap *buf = NULL; | |
731 | w1 = vik_viewport_get_width(vvp); | |
732 | h1 = vik_viewport_get_height(vvp); | |
733 | if (!buf) { | |
734 | buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 ); | |
735 | } | |
736 | gdk_drawable_get_size(buf, &w2, &h2); | |
737 | if (w1 != w2 || h1 != h2) { | |
738 | g_object_unref ( G_OBJECT ( buf ) ); | |
739 | buf = gdk_pixmap_new ( GTK_WIDGET(vvp)->window, w1, h1, -1 ); | |
e4847ce9 | 740 | } |
e4847ce9 | 741 | |
941aa6e9 AF |
742 | vik_viewport_screen_to_coord ( vvp, (gint) event->x, (gint) event->y, &coord ); |
743 | vik_coord_to_latlon ( &coord, &ll ); | |
744 | vik_viewport_coord_to_screen ( vvp, &s->oldcoord, &oldx, &oldy ); | |
745 | ||
746 | gdk_draw_drawable (buf, GTK_WIDGET(vvp)->style->black_gc, | |
747 | vik_viewport_get_pixmap(vvp), 0, 0, 0, 0, -1, -1); | |
748 | draw_ruler(vvp, buf, GTK_WIDGET(vvp)->style->black_gc, oldx, oldy, event->x, event->y, vik_coord_diff( &coord, &(s->oldcoord)) ); | |
749 | if (draw_buf_done) { | |
750 | static gpointer pass_along[3]; | |
751 | pass_along[0] = GTK_WIDGET(vvp)->window; | |
752 | pass_along[1] = GTK_WIDGET(vvp)->style->black_gc; | |
753 | pass_along[2] = buf; | |
754 | g_idle_add_full (G_PRIORITY_HIGH_IDLE + 10, draw_buf, pass_along, NULL); | |
755 | draw_buf_done = FALSE; | |
756 | } | |
50a14534 | 757 | |
941aa6e9 AF |
758 | temp = g_strdup_printf ( "%f %f DIFF %f meters", ll.lat, ll.lon, vik_coord_diff( &coord, &(s->oldcoord) ) ); |
759 | vik_statusbar_set_message ( vw->viking_vs, 3, temp ); | |
760 | g_free ( temp ); | |
acaf7113 | 761 | } |
941aa6e9 AF |
762 | return VIK_LAYER_TOOL_ACK; |
763 | } | |
50a14534 | 764 | |
941aa6e9 AF |
765 | static VikLayerToolFuncStatus ruler_release (VikLayer *vl, GdkEventButton *event, ruler_tool_state_t *s) |
766 | { | |
767 | return VIK_LAYER_TOOL_ACK; | |
50a14534 EB |
768 | } |
769 | ||
941aa6e9 | 770 | static void ruler_deactivate (VikLayer *vl, ruler_tool_state_t *s) |
50a14534 | 771 | { |
941aa6e9 | 772 | draw_update ( s->vw ); |
50a14534 EB |
773 | } |
774 | ||
941aa6e9 AF |
775 | static VikToolInterface ruler_tool = |
776 | { "Ruler", | |
777 | (VikToolConstructorFunc) ruler_create, | |
778 | (VikToolDestructorFunc) ruler_destroy, | |
779 | (VikToolActivationFunc) NULL, | |
780 | (VikToolActivationFunc) ruler_deactivate, | |
781 | (VikToolMouseFunc) ruler_click, | |
782 | (VikToolMouseFunc) ruler_move, | |
783 | (VikToolMouseFunc) ruler_release }; | |
784 | /*** end ruler code ********************************************************/ | |
785 | ||
786 | ||
787 | ||
788 | /******************************************************************************** | |
789 | ** Zoom tool code | |
790 | ********************************************************************************/ | |
791 | static gpointer zoomtool_create (VikWindow *vw, VikViewport *vvp) | |
50a14534 | 792 | { |
941aa6e9 | 793 | return vw; |
50a14534 EB |
794 | } |
795 | ||
941aa6e9 | 796 | static VikLayerToolFuncStatus zoomtool_click (VikLayer *vl, GdkEventButton *event, VikWindow *vw) |
50a14534 | 797 | { |
941aa6e9 AF |
798 | vw->modified = TRUE; |
799 | vik_viewport_set_center_screen ( vw->viking_vvp, (gint) event->x, (gint) event->y ); | |
800 | if ( event->button == 1 ) | |
801 | vik_viewport_zoom_in (vw->viking_vvp); | |
802 | else if ( event->button == 3 ) | |
803 | vik_viewport_zoom_out (vw->viking_vvp); | |
804 | draw_update ( vw ); | |
805 | return VIK_LAYER_TOOL_ACK; | |
806 | } | |
50a14534 | 807 | |
941aa6e9 AF |
808 | static VikLayerToolFuncStatus zoomtool_move (VikLayer *vl, GdkEventButton *event, VikViewport *vvp) |
809 | { | |
810 | return VIK_LAYER_TOOL_ACK; | |
811 | } | |
50a14534 | 812 | |
941aa6e9 AF |
813 | static VikLayerToolFuncStatus zoomtool_release (VikLayer *vl, GdkEventButton *event, VikViewport *vvp) |
814 | { | |
815 | return VIK_LAYER_TOOL_ACK; | |
50a14534 EB |
816 | } |
817 | ||
941aa6e9 AF |
818 | static VikToolInterface zoom_tool = |
819 | { "Zoom", | |
820 | (VikToolConstructorFunc) zoomtool_create, | |
821 | (VikToolDestructorFunc) NULL, | |
822 | (VikToolActivationFunc) NULL, | |
823 | (VikToolActivationFunc) NULL, | |
824 | (VikToolMouseFunc) zoomtool_click, | |
825 | (VikToolMouseFunc) zoomtool_move, | |
826 | (VikToolMouseFunc) zoomtool_release }; | |
827 | /*** end ruler code ********************************************************/ | |
828 | ||
8c721f83 EB |
829 | static void draw_pan_cb ( GtkAction *a, VikWindow *vw ) |
830 | { | |
831 | if (!strcmp(gtk_action_get_name(a), "PanNorth")) { | |
832 | vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, 0 ); | |
833 | } else if (!strcmp(gtk_action_get_name(a), "PanEast")) { | |
834 | vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp), vik_viewport_get_height(vw->viking_vvp)/2 ); | |
835 | } else if (!strcmp(gtk_action_get_name(a), "PanSouth")) { | |
836 | vik_viewport_set_center_screen ( vw->viking_vvp, vik_viewport_get_width(vw->viking_vvp)/2, vik_viewport_get_height(vw->viking_vvp) ); | |
837 | } else if (!strcmp(gtk_action_get_name(a), "PanWest")) { | |
838 | vik_viewport_set_center_screen ( vw->viking_vvp, 0, vik_viewport_get_height(vw->viking_vvp)/2 ); | |
839 | } | |
840 | draw_update ( vw ); | |
841 | } | |
941aa6e9 AF |
842 | |
843 | ||
e4afc73a | 844 | static void draw_zoom_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 | 845 | { |
e4afc73a EB |
846 | guint what = 128; |
847 | ||
848 | if (!strcmp(gtk_action_get_name(a), "ZoomIn")) { | |
849 | what = -3; | |
850 | } | |
851 | else if (!strcmp(gtk_action_get_name(a), "ZoomOut")) { | |
852 | what = -4; | |
853 | } | |
854 | else if (!strcmp(gtk_action_get_name(a), "Zoom0.25")) { | |
855 | what = -2; | |
856 | } | |
857 | else if (!strcmp(gtk_action_get_name(a), "Zoom0.5")) { | |
858 | what = -1; | |
859 | } | |
860 | else { | |
861 | gchar *s = (gchar *)gtk_action_get_name(a); | |
862 | what = atoi(s+4); | |
863 | } | |
864 | ||
50a14534 EB |
865 | switch (what) |
866 | { | |
867 | case -3: vik_viewport_zoom_in ( vw->viking_vvp ); break; | |
868 | case -4: vik_viewport_zoom_out ( vw->viking_vvp ); break; | |
869 | case -1: vik_viewport_set_zoom ( vw->viking_vvp, 0.5 ); break; | |
870 | case -2: vik_viewport_set_zoom ( vw->viking_vvp, 0.25 ); break; | |
871 | default: vik_viewport_set_zoom ( vw->viking_vvp, what ); | |
872 | } | |
873 | draw_update ( vw ); | |
874 | } | |
875 | ||
e4afc73a | 876 | void draw_goto_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
877 | { |
878 | VikCoord new_center; | |
e4afc73a EB |
879 | |
880 | if (!strcmp(gtk_action_get_name(a), "GotoLL")) { | |
50a14534 EB |
881 | struct LatLon ll, llold; |
882 | vik_coord_to_latlon ( vik_viewport_get_center ( vw->viking_vvp ), &llold ); | |
883 | if ( a_dialog_goto_latlon ( GTK_WINDOW(vw), &ll, &llold ) ) | |
884 | vik_coord_load_from_latlon ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &ll ); | |
885 | else | |
886 | return; | |
887 | } | |
e4afc73a | 888 | else if (!strcmp(gtk_action_get_name(a), "GotoUTM")) { |
50a14534 EB |
889 | struct UTM utm, utmold; |
890 | vik_coord_to_utm ( vik_viewport_get_center ( vw->viking_vvp ), &utmold ); | |
891 | if ( a_dialog_goto_utm ( GTK_WINDOW(vw), &utm, &utmold ) ) | |
892 | vik_coord_load_from_utm ( &new_center, vik_viewport_get_coord_mode(vw->viking_vvp), &utm ); | |
893 | else | |
894 | return; | |
895 | } | |
e4afc73a | 896 | else { |
7742da66 | 897 | g_critical("Houston we have a problem\n"); |
e4afc73a EB |
898 | return; |
899 | } | |
50a14534 EB |
900 | |
901 | vik_viewport_set_center_coord ( vw->viking_vvp, &new_center ); | |
902 | draw_update ( vw ); | |
903 | } | |
904 | ||
e4afc73a | 905 | static void menu_addlayer_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 | 906 | { |
e4afc73a EB |
907 | gint type; |
908 | for ( type = 0; type < VIK_LAYER_NUM_TYPES; type++ ) { | |
909 | if (!strcmp(vik_layer_get_interface(type)->name, gtk_action_get_name(a))) { | |
910 | if ( vik_layers_panel_new_layer ( vw->viking_vlp, type ) ) { | |
911 | draw_update ( vw ); | |
912 | vw->modified = TRUE; | |
913 | } | |
914 | } | |
50a14534 EB |
915 | } |
916 | } | |
917 | ||
e4afc73a | 918 | static void menu_copy_layer_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 | 919 | { |
2cebc318 | 920 | a_clipboard_copy_selected ( vw->viking_vlp ); |
50a14534 EB |
921 | } |
922 | ||
e4afc73a | 923 | static void menu_cut_layer_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 | 924 | { |
2cebc318 | 925 | a_clipboard_copy_selected ( vw->viking_vlp ); |
e4afc73a | 926 | menu_delete_layer_cb ( a, vw ); |
50a14534 EB |
927 | } |
928 | ||
e4afc73a | 929 | static void menu_paste_layer_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
930 | { |
931 | if ( a_clipboard_paste ( vw->viking_vlp ) ) | |
932 | { | |
933 | draw_update ( vw ); | |
934 | vw->modified = TRUE; | |
935 | } | |
936 | } | |
937 | ||
e4afc73a | 938 | static void menu_properties_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
939 | { |
940 | if ( ! vik_layers_panel_properties ( vw->viking_vlp ) ) | |
941 | a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to show its properties." ); | |
942 | } | |
943 | ||
d0a5f320 AF |
944 | static void help_about_cb ( GtkAction *a, VikWindow *vw ) |
945 | { | |
946 | a_dialog_about(GTK_WINDOW(vw)); | |
947 | } | |
948 | ||
e4afc73a | 949 | static void menu_delete_layer_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
950 | { |
951 | if ( vik_layers_panel_get_selected ( vw->viking_vlp ) ) | |
952 | { | |
953 | vik_layers_panel_delete_selected ( vw->viking_vlp ); | |
954 | vw->modified = TRUE; | |
955 | } | |
956 | else | |
957 | a_dialog_info_msg ( GTK_WINDOW(vw), "You must select a layer to delete." ); | |
958 | } | |
959 | ||
941aa6e9 AF |
960 | /*************************************** |
961 | ** tool management routines | |
962 | ** | |
963 | ***************************************/ | |
964 | ||
965 | static toolbox_tools_t* toolbox_create(VikWindow *vw) | |
966 | { | |
967 | toolbox_tools_t *vt = g_new(toolbox_tools_t, 1); | |
968 | vt->tools = NULL; | |
969 | vt->n_tools = 0; | |
970 | vt->active_tool = -1; | |
971 | vt->vw = vw; | |
972 | if (!vw->viking_vvp) { | |
7742da66 | 973 | g_critical("no viewport found."); |
941aa6e9 AF |
974 | exit(1); |
975 | } | |
976 | return vt; | |
977 | } | |
978 | ||
9593a4c9 | 979 | static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type ) |
941aa6e9 AF |
980 | { |
981 | vt->tools = g_renew(toolbox_tool_t, vt->tools, vt->n_tools+1); | |
982 | vt->tools[vt->n_tools].ti = *vti; | |
9593a4c9 | 983 | vt->tools[vt->n_tools].layer_type = layer_type; |
941aa6e9 AF |
984 | if (vti->create) { |
985 | vt->tools[vt->n_tools].state = vti->create(vt->vw, vt->vw->viking_vvp); | |
986 | } | |
987 | else { | |
988 | vt->tools[vt->n_tools].state = NULL; | |
989 | } | |
990 | vt->n_tools++; | |
991 | } | |
992 | ||
993 | static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name) | |
994 | { | |
995 | int i; | |
996 | for (i=0; i<vt->n_tools; i++) { | |
997 | if (!strcmp(tool_name, vt->tools[i].ti.name)) { | |
998 | break; | |
999 | } | |
1000 | } | |
1001 | return i; | |
1002 | } | |
1003 | ||
1004 | static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name) | |
1005 | { | |
1006 | int tool = toolbox_get_tool(vt, tool_name); | |
1007 | toolbox_tool_t *t = &vt->tools[tool]; | |
1008 | VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp ); | |
1009 | ||
1010 | if (tool == vt->n_tools) { | |
7742da66 | 1011 | g_critical("trying to activate a non-existent tool..."); |
941aa6e9 AF |
1012 | exit(1); |
1013 | } | |
1014 | /* is the tool already active? */ | |
1015 | if (vt->active_tool == tool) { | |
1016 | return; | |
1017 | } | |
1018 | ||
1019 | if (vt->active_tool != -1) { | |
1020 | if (vt->tools[vt->active_tool].ti.deactivate) { | |
1021 | vt->tools[vt->active_tool].ti.deactivate(NULL, vt->tools[vt->active_tool].state); | |
1022 | } | |
1023 | } | |
1024 | if (t->ti.activate) { | |
1025 | t->ti.activate(vl, t->state); | |
1026 | } | |
1027 | vt->active_tool = tool; | |
1028 | } | |
1029 | ||
1030 | static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event) | |
1031 | { | |
1032 | VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp ); | |
1033 | if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.click) { | |
9593a4c9 EB |
1034 | gint ltype = vt->tools[vt->active_tool].layer_type; |
1035 | if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) ) | |
1036 | vt->tools[vt->active_tool].ti.click(vl, event, vt->tools[vt->active_tool].state); | |
941aa6e9 AF |
1037 | } |
1038 | } | |
1039 | ||
1040 | static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event) | |
1041 | { | |
1042 | VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp ); | |
1043 | if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.move) { | |
9593a4c9 EB |
1044 | gint ltype = vt->tools[vt->active_tool].layer_type; |
1045 | if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) ) | |
1046 | vt->tools[vt->active_tool].ti.move(vl, event, vt->tools[vt->active_tool].state); | |
941aa6e9 AF |
1047 | } |
1048 | } | |
1049 | ||
1050 | static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event) | |
1051 | { | |
1052 | VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp ); | |
9593a4c9 EB |
1053 | if (vt->active_tool != -1 && vt->tools[vt->active_tool].ti.release ) { |
1054 | gint ltype = vt->tools[vt->active_tool].layer_type; | |
1055 | if ( ltype == TOOL_LAYER_TYPE_NONE || (vl && ltype == vl->type) ) | |
1056 | vt->tools[vt->active_tool].ti.release(vl, event, vt->tools[vt->active_tool].state); | |
941aa6e9 AF |
1057 | } |
1058 | } | |
1059 | /** End tool management ************************************/ | |
1060 | ||
1061 | ||
1062 | ||
1063 | /* this function gets called whenever a toolbar tool is clicked */ | |
e4afc73a | 1064 | static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1065 | { |
1066 | /* White Magic, my friends ... White Magic... */ | |
e4afc73a EB |
1067 | int i, j; |
1068 | ||
941aa6e9 AF |
1069 | toolbox_activate(vw->vt, gtk_action_get_name(a)); |
1070 | ||
e4afc73a EB |
1071 | if (!strcmp(gtk_action_get_name(a), "Zoom")) { |
1072 | vw->current_tool = TOOL_ZOOM; | |
1073 | } | |
1074 | else if (!strcmp(gtk_action_get_name(a), "Ruler")) { | |
1075 | vw->current_tool = TOOL_RULER; | |
1076 | } | |
1077 | else { | |
79845167 | 1078 | /* TODO: only enable tools from active layer */ |
e4afc73a EB |
1079 | for (i=0; i<VIK_LAYER_NUM_TYPES; i++) { |
1080 | for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) { | |
1081 | if (!strcmp(vik_layer_get_interface(i)->tools[j].name, gtk_action_get_name(a))) { | |
1082 | vw->current_tool = TOOL_LAYER; | |
1083 | vw->tool_layer_id = i; | |
1084 | vw->tool_tool_id = j; | |
1085 | } | |
1086 | } | |
1087 | } | |
1088 | } | |
50a14534 EB |
1089 | draw_status ( vw ); |
1090 | } | |
1091 | ||
1092 | static void window_set_filename ( VikWindow *vw, const gchar *filename ) | |
1093 | { | |
1094 | gchar *title; | |
1095 | if ( vw->filename ) | |
1096 | g_free ( vw->filename ); | |
1097 | if ( filename == NULL ) | |
1098 | { | |
1099 | vw->filename = NULL; | |
1100 | gtk_window_set_title ( GTK_WINDOW(vw), "Untitled" VIKING_TITLE ); | |
1101 | } | |
1102 | else | |
1103 | { | |
1104 | vw->filename = g_strdup(filename); | |
1105 | title = g_strconcat ( a_file_basename ( filename ), VIKING_TITLE, NULL ); | |
1106 | gtk_window_set_title ( GTK_WINDOW(vw), title ); | |
1107 | g_free ( title ); | |
1108 | } | |
1109 | } | |
1110 | ||
7bc965c0 GB |
1111 | GtkWidget *vik_window_get_drawmode_button ( VikWindow *vw, VikViewportDrawMode mode ) |
1112 | { | |
1113 | GtkWidget *mode_button; | |
1114 | gchar *buttonname; | |
1115 | switch ( mode ) { | |
1116 | case VIK_VIEWPORT_DRAWMODE_UTM: buttonname = "/ui/MainMenu/View/ModeUTM"; break; | |
1117 | case VIK_VIEWPORT_DRAWMODE_EXPEDIA: buttonname = "/ui/MainMenu/View/ModeExpedia"; break; | |
1118 | case VIK_VIEWPORT_DRAWMODE_GOOGLE: buttonname = "/ui/MainMenu/View/ModeGoogle"; break; | |
1119 | case VIK_VIEWPORT_DRAWMODE_MERCATOR: buttonname = "/ui/MainMenu/View/ModeMercator"; break; | |
1120 | default: buttonname = "/ui/MainMenu/View/ModeKH"; | |
1121 | } | |
1122 | mode_button = gtk_ui_manager_get_widget ( vw->uim, buttonname ); | |
1123 | g_assert ( mode_button ); | |
1124 | return mode_button; | |
1125 | } | |
1126 | ||
50a14534 EB |
1127 | void vik_window_open_file ( VikWindow *vw, const gchar *filename, gboolean change_filename ) |
1128 | { | |
1129 | switch ( a_file_load ( vik_layers_panel_get_top_layer(vw->viking_vlp), vw->viking_vvp, filename ) ) | |
1130 | { | |
1131 | case 0: | |
1132 | a_dialog_error_msg ( GTK_WINDOW(vw), "The file you requested could not be opened." ); | |
1133 | break; | |
1134 | case 1: | |
1135 | { | |
1136 | GtkWidget *mode_button; | |
50a14534 EB |
1137 | if ( change_filename ) |
1138 | window_set_filename ( vw, filename ); | |
7bc965c0 | 1139 | mode_button = vik_window_get_drawmode_button ( vw, vik_viewport_get_drawmode ( vw->viking_vvp ) ); |
50a14534 EB |
1140 | 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. */ |
1141 | gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button), TRUE ); | |
1142 | vw->only_updating_coord_mode_ui = FALSE; | |
1143 | ||
1144 | vik_layers_panel_change_coord_mode ( vw->viking_vlp, vik_viewport_get_coord_mode ( vw->viking_vvp ) ); | |
1657065a QT |
1145 | |
1146 | mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" ); | |
1147 | g_assert ( mode_button ); | |
1148 | gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_scale(vw->viking_vvp) ); | |
1149 | ||
1150 | mode_button = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" ); | |
1151 | g_assert ( mode_button ); | |
1152 | gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM(mode_button),vik_viewport_get_draw_centermark(vw->viking_vvp) ); | |
50a14534 EB |
1153 | } |
1154 | default: draw_update ( vw ); | |
1155 | } | |
1156 | } | |
e4afc73a | 1157 | static void load_file ( GtkAction *a, VikWindow *vw ) |
50a14534 | 1158 | { |
e4afc73a EB |
1159 | gboolean newwindow; |
1160 | if (!strcmp(gtk_action_get_name(a), "Open")) { | |
1161 | newwindow = TRUE; | |
1162 | } | |
1163 | else if (!strcmp(gtk_action_get_name(a), "Append")) { | |
1164 | newwindow = FALSE; | |
1165 | } | |
1166 | else { | |
7742da66 | 1167 | g_critical("Houston we got a problem\n"); |
e4afc73a EB |
1168 | return; |
1169 | } | |
1170 | ||
50a14534 EB |
1171 | if ( ! vw->open_dia ) |
1172 | { | |
1173 | vw->open_dia = gtk_file_selection_new ("Please select a GPS data file to open. " ); | |
1174 | gtk_file_selection_set_select_multiple ( GTK_FILE_SELECTION(vw->open_dia), TRUE ); | |
1175 | gtk_window_set_transient_for ( GTK_WINDOW(vw->open_dia), GTK_WINDOW(vw) ); | |
1176 | gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->open_dia), TRUE ); | |
1177 | } | |
1178 | if ( gtk_dialog_run ( GTK_DIALOG(vw->open_dia) ) == GTK_RESPONSE_OK ) | |
1179 | { | |
1180 | gtk_widget_hide ( vw->open_dia ); | |
a5fd2196 | 1181 | #ifdef VIKING_PROMPT_IF_MODIFIED |
50a14534 | 1182 | if ( (vw->modified || vw->filename) && newwindow ) |
a5fd2196 QT |
1183 | #else |
1184 | if ( vw->filename && newwindow ) | |
1185 | #endif | |
50a14534 EB |
1186 | g_signal_emit ( G_OBJECT(vw), window_signals[VW_OPENWINDOW_SIGNAL], 0, gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ) ); |
1187 | else { | |
1188 | gchar **files = gtk_file_selection_get_selections (GTK_FILE_SELECTION(vw->open_dia) ); | |
1189 | gboolean change_fn = newwindow && (!files[1]); /* only change fn if one file */ | |
1190 | while ( *files ) { | |
1191 | vik_window_open_file ( vw, *files, change_fn ); | |
1192 | files++; | |
1193 | } | |
1194 | } | |
1195 | } | |
1196 | else | |
1197 | gtk_widget_hide ( vw->open_dia ); | |
1198 | } | |
1199 | ||
e4afc73a | 1200 | static gboolean save_file_as ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1201 | { |
1202 | gboolean rv = FALSE; | |
1203 | const gchar *fn; | |
1204 | if ( ! vw->save_dia ) | |
1205 | { | |
1206 | vw->save_dia = gtk_file_selection_new ("Save as Viking File. " ); | |
1207 | gtk_window_set_transient_for ( GTK_WINDOW(vw->save_dia), GTK_WINDOW(vw) ); | |
1208 | gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_dia), TRUE ); | |
1209 | } | |
1210 | ||
1211 | while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_OK ) | |
1212 | { | |
1213 | fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_dia) ); | |
1214 | 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 ) ) ) | |
1215 | { | |
1216 | window_set_filename ( vw, fn ); | |
1217 | rv = window_save ( vw ); | |
1218 | vw->modified = FALSE; | |
1219 | break; | |
1220 | } | |
1221 | } | |
1222 | gtk_widget_hide ( vw->save_dia ); | |
1223 | return rv; | |
1224 | } | |
1225 | ||
1226 | static gboolean window_save ( VikWindow *vw ) | |
1227 | { | |
1228 | if ( a_file_save ( vik_layers_panel_get_top_layer ( vw->viking_vlp ), vw->viking_vvp, vw->filename ) ) | |
1229 | return TRUE; | |
1230 | else | |
1231 | { | |
1232 | a_dialog_error_msg ( GTK_WINDOW(vw), "The filename you requested could not be opened for writing." ); | |
1233 | return FALSE; | |
1234 | } | |
1235 | } | |
1236 | ||
e4afc73a | 1237 | static gboolean save_file ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1238 | { |
1239 | if ( ! vw->filename ) | |
e4afc73a | 1240 | return save_file_as ( NULL, vw ); |
50a14534 EB |
1241 | else |
1242 | { | |
1243 | vw->modified = FALSE; | |
1244 | return window_save ( vw ); | |
1245 | } | |
1246 | } | |
1247 | ||
1d1bc3c1 EB |
1248 | static void acquire_from_gps ( GtkAction *a, VikWindow *vw ) |
1249 | { | |
7b3479e3 EB |
1250 | a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gps_interface ); |
1251 | } | |
1252 | ||
1253 | static void acquire_from_google ( GtkAction *a, VikWindow *vw ) | |
1254 | { | |
1255 | a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_google_interface ); | |
1d1bc3c1 EB |
1256 | } |
1257 | ||
1ef9e637 | 1258 | #ifdef VIK_CONFIG_GEOCACHES |
3333c069 EB |
1259 | static void acquire_from_gc ( GtkAction *a, VikWindow *vw ) |
1260 | { | |
1261 | a_acquire(vw, vw->viking_vlp, vw->viking_vvp, &vik_datasource_gc_interface ); | |
1262 | } | |
1ef9e637 | 1263 | #endif |
3333c069 | 1264 | |
369126f3 QT |
1265 | static void goto_address( GtkAction *a, VikWindow *vw) |
1266 | { | |
1267 | a_google_search(vw, vw->viking_vlp, vw->viking_vvp); | |
1268 | } | |
1269 | ||
e4afc73a | 1270 | static void clear_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1271 | { |
1272 | vik_layers_panel_clear ( vw->viking_vlp ); | |
1273 | window_set_filename ( vw, NULL ); | |
1274 | draw_update ( vw ); | |
1275 | } | |
1276 | ||
e4afc73a | 1277 | static void window_close ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1278 | { |
1279 | if ( ! delete_event ( vw ) ) | |
1280 | gtk_widget_destroy ( GTK_WIDGET(vw) ); | |
1281 | } | |
1282 | ||
2bf7cadd QT |
1283 | static gboolean save_file_and_exit ( GtkAction *a, VikWindow *vw ) |
1284 | { | |
7bb60307 | 1285 | if (save_file( NULL, vw)) { |
2bf7cadd | 1286 | window_close( NULL, vw); |
7bb60307 QT |
1287 | return(TRUE); |
1288 | } | |
2bf7cadd QT |
1289 | else |
1290 | return(FALSE); | |
1291 | } | |
1292 | ||
e4afc73a | 1293 | static void zoom_to_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1294 | { |
1295 | gdouble xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ), ympp = vik_viewport_get_ympp ( vw->viking_vvp ); | |
1296 | if ( a_dialog_custom_zoom ( GTK_WINDOW(vw), &xmpp, &ympp ) ) | |
1297 | { | |
1298 | vik_viewport_set_xmpp ( vw->viking_vvp, xmpp ); | |
1299 | vik_viewport_set_ympp ( vw->viking_vvp, ympp ); | |
1300 | draw_update ( vw ); | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | static void save_image_file ( VikWindow *vw, const gchar *fn, guint w, guint h, gdouble zoom, gboolean save_as_png ) | |
1305 | { | |
1306 | /* more efficient way: stuff draws directly to pixbuf (fork viewport) */ | |
1307 | GdkPixbuf *pixbuf_to_save; | |
1308 | gdouble old_xmpp, old_ympp; | |
1309 | GError *error = NULL; | |
1310 | ||
1311 | /* backup old zoom & set new */ | |
1312 | old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ); | |
1313 | old_ympp = vik_viewport_get_ympp ( vw->viking_vvp ); | |
1314 | vik_viewport_set_zoom ( vw->viking_vvp, zoom ); | |
1315 | ||
1316 | /* reset width and height: */ | |
1317 | vik_viewport_configure_manually ( vw->viking_vvp, w, h ); | |
1318 | ||
1319 | /* draw all layers */ | |
1320 | draw_redraw ( vw ); | |
1321 | ||
1322 | /* save buffer as file. */ | |
1323 | 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); | |
1324 | gdk_pixbuf_save ( pixbuf_to_save, fn, save_as_png ? "png" : "jpeg", &error, NULL ); | |
1325 | if (error) | |
1326 | { | |
7742da66 | 1327 | g_warning("Unable to write to file %s: %s", fn, error->message ); |
50a14534 EB |
1328 | g_error_free (error); |
1329 | } | |
1330 | g_object_unref ( G_OBJECT(pixbuf_to_save) ); | |
1331 | ||
1332 | /* pretend like nothing happened ;) */ | |
1333 | vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp ); | |
1334 | vik_viewport_set_ympp ( vw->viking_vvp, old_ympp ); | |
1335 | vik_viewport_configure ( vw->viking_vvp ); | |
1336 | draw_update ( vw ); | |
1337 | } | |
1338 | ||
1339 | 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 ) | |
1340 | { | |
1341 | gulong size = sizeof(gchar) * (strlen(fn) + 15); | |
1342 | gchar *name_of_file = g_malloc ( size ); | |
1343 | guint x = 1, y = 1; | |
1344 | struct UTM utm_orig, utm; | |
1345 | ||
1346 | /* *** copied from above *** */ | |
1347 | GdkPixbuf *pixbuf_to_save; | |
1348 | gdouble old_xmpp, old_ympp; | |
1349 | GError *error = NULL; | |
1350 | ||
1351 | /* backup old zoom & set new */ | |
1352 | old_xmpp = vik_viewport_get_xmpp ( vw->viking_vvp ); | |
1353 | old_ympp = vik_viewport_get_ympp ( vw->viking_vvp ); | |
1354 | vik_viewport_set_zoom ( vw->viking_vvp, zoom ); | |
1355 | ||
1356 | /* reset width and height: do this only once for all images (same size) */ | |
1357 | vik_viewport_configure_manually ( vw->viking_vvp, w, h ); | |
1358 | /* *** end copy from above *** */ | |
1359 | ||
1360 | g_assert ( vik_viewport_get_coord_mode ( vw->viking_vvp ) == VIK_COORD_UTM ); | |
1361 | ||
1362 | make_dir(fn); | |
1363 | ||
1364 | utm_orig = *((const struct UTM *)vik_viewport_get_center ( vw->viking_vvp )); | |
1365 | ||
1366 | for ( y = 1; y <= tiles_h; y++ ) | |
1367 | { | |
1368 | for ( x = 1; x <= tiles_w; x++ ) | |
1369 | { | |
3d9454e6 | 1370 | g_snprintf ( name_of_file, size, "%s%cy%d-x%d.%s", fn, G_DIR_SEPARATOR, y, x, save_as_png ? "png" : "jpg" ); |
50a14534 EB |
1371 | utm = utm_orig; |
1372 | if ( tiles_w & 0x1 ) | |
1373 | utm.easting += ((gdouble)x - ceil(((gdouble)tiles_w)/2)) * (w*zoom); | |
1374 | else | |
1375 | utm.easting += ((gdouble)x - (((gdouble)tiles_w)+1)/2) * (w*zoom); | |
1376 | if ( tiles_h & 0x1 ) /* odd */ | |
1377 | utm.northing -= ((gdouble)y - ceil(((gdouble)tiles_h)/2)) * (h*zoom); | |
1378 | else /* even */ | |
1379 | utm.northing -= ((gdouble)y - (((gdouble)tiles_h)+1)/2) * (h*zoom); | |
1380 | ||
1381 | /* move to correct place. */ | |
1382 | vik_viewport_set_center_utm ( vw->viking_vvp, &utm ); | |
1383 | ||
1384 | draw_redraw ( vw ); | |
1385 | ||
1386 | /* save buffer as file. */ | |
1387 | 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); | |
1388 | gdk_pixbuf_save ( pixbuf_to_save, name_of_file, save_as_png ? "png" : "jpeg", &error, NULL ); | |
1389 | if (error) | |
1390 | { | |
7742da66 | 1391 | g_warning("Unable to write to file %s: %s", name_of_file, error->message ); |
50a14534 EB |
1392 | g_error_free (error); |
1393 | } | |
1394 | ||
1395 | g_object_unref ( G_OBJECT(pixbuf_to_save) ); | |
1396 | } | |
1397 | } | |
1398 | ||
1399 | vik_viewport_set_center_utm ( vw->viking_vvp, &utm_orig ); | |
1400 | vik_viewport_set_xmpp ( vw->viking_vvp, old_xmpp ); | |
1401 | vik_viewport_set_ympp ( vw->viking_vvp, old_ympp ); | |
1402 | vik_viewport_configure ( vw->viking_vvp ); | |
1403 | draw_update ( vw ); | |
1404 | ||
1405 | g_free ( name_of_file ); | |
1406 | } | |
1407 | ||
1408 | static void draw_to_image_file_current_window_cb(GtkWidget* widget,GdkEventButton *event,gpointer *pass_along) | |
1409 | { | |
1410 | VikWindow *vw = VIK_WINDOW(pass_along[0]); | |
1411 | GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]); | |
1412 | GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]); | |
1413 | gdouble width_min, width_max, height_min, height_max; | |
1414 | gint width, height; | |
1415 | ||
1416 | gtk_spin_button_get_range ( width_spin, &width_min, &width_max ); | |
1417 | gtk_spin_button_get_range ( height_spin, &height_min, &height_max ); | |
1418 | ||
1419 | /* TODO: support for xzoom and yzoom values */ | |
1420 | width = vik_viewport_get_width ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin ); | |
1421 | height = vik_viewport_get_height ( vw->viking_vvp ) * vik_viewport_get_xmpp ( vw->viking_vvp ) / gtk_spin_button_get_value ( zoom_spin ); | |
1422 | ||
1423 | if ( width > width_max || width < width_min || height > height_max || height < height_min ) | |
1424 | a_dialog_info_msg ( GTK_WINDOW(vw), "Viewable region outside allowable pixel size bounds for image. Clipping width/height values." ); | |
1425 | ||
1426 | gtk_spin_button_set_value ( width_spin, width ); | |
1427 | gtk_spin_button_set_value ( height_spin, height ); | |
1428 | } | |
1429 | ||
1430 | static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along) | |
1431 | { | |
1432 | GtkSpinButton *width_spin = GTK_SPIN_BUTTON(pass_along[1]), *height_spin = GTK_SPIN_BUTTON(pass_along[2]); | |
1433 | GtkSpinButton *zoom_spin = GTK_SPIN_BUTTON(pass_along[3]); | |
1434 | gchar *label_text; | |
1435 | gdouble w, h; | |
1436 | w = gtk_spin_button_get_value(width_spin) * gtk_spin_button_get_value(zoom_spin); | |
1437 | h = gtk_spin_button_get_value(height_spin) * gtk_spin_button_get_value(zoom_spin); | |
1438 | if (pass_along[4]) /* save many images; find TOTAL area covered */ | |
1439 | { | |
1440 | w *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[4])); | |
1441 | h *= gtk_spin_button_get_value(GTK_SPIN_BUTTON(pass_along[5])); | |
1442 | } | |
1443 | label_text = g_strdup_printf ( "Total area: %ldm x %ldm (%.3f sq. km)", (glong)w, (glong)h, (w*h/1000000)); | |
1444 | gtk_label_set_text(GTK_LABEL(pass_along[6]), label_text); | |
1445 | g_free ( label_text ); | |
1446 | } | |
1447 | ||
1448 | static void draw_to_image_file ( VikWindow *vw, const gchar *fn, gboolean one_image_only ) | |
1449 | { | |
1450 | /* todo: default for answers inside VikWindow or static (thruout instance) */ | |
1451 | GtkWidget *dialog = gtk_dialog_new_with_buttons ( "Save to Image File", GTK_WINDOW(vw), | |
1452 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | |
1453 | GTK_STOCK_CANCEL, | |
1454 | GTK_RESPONSE_REJECT, | |
1455 | GTK_STOCK_OK, | |
1456 | GTK_RESPONSE_ACCEPT, | |
10888930 | 1457 | NULL ); |
50a14534 EB |
1458 | GtkWidget *width_label, *width_spin, *height_label, *height_spin; |
1459 | GtkWidget *png_radio, *jpeg_radio; | |
1460 | GtkWidget *current_window_button; | |
1461 | gpointer current_window_pass_along[7]; | |
1462 | GtkWidget *zoom_label, *zoom_spin; | |
1463 | GtkWidget *total_size_label; | |
1464 | ||
1465 | /* only used if (!one_image_only) */ | |
886031df | 1466 | GtkWidget *tiles_width_spin = NULL, *tiles_height_spin = NULL; |
50a14534 EB |
1467 | |
1468 | ||
1469 | width_label = gtk_label_new ( "Width (pixels):" ); | |
1470 | width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_width, 10, 5000, 10, 100, 0 )), 10, 0 ); | |
1471 | height_label = gtk_label_new ( "Height (pixels):" ); | |
1472 | height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( vw->draw_image_height, 10, 5000, 10, 100, 0 )), 10, 0 ); | |
1473 | ||
1474 | zoom_label = gtk_label_new ( "Zoom (meters per pixel):" ); | |
1475 | /* TODO: separate xzoom and yzoom factors */ | |
1476 | 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); | |
1477 | ||
1478 | total_size_label = gtk_label_new ( NULL ); | |
1479 | ||
1480 | current_window_button = gtk_button_new_with_label ( "Area in current viewable window" ); | |
1481 | current_window_pass_along [0] = vw; | |
1482 | current_window_pass_along [1] = width_spin; | |
1483 | current_window_pass_along [2] = height_spin; | |
1484 | current_window_pass_along [3] = zoom_spin; | |
1485 | current_window_pass_along [4] = NULL; /* used for one_image_only != 1 */ | |
1486 | current_window_pass_along [5] = NULL; | |
1487 | current_window_pass_along [6] = total_size_label; | |
1488 | g_signal_connect ( G_OBJECT(current_window_button), "button_press_event", G_CALLBACK(draw_to_image_file_current_window_cb), current_window_pass_along ); | |
1489 | ||
1490 | png_radio = gtk_radio_button_new_with_label ( NULL, "Save as PNG" ); | |
1491 | jpeg_radio = gtk_radio_button_new_with_label_from_widget ( GTK_RADIO_BUTTON(png_radio), "Save as JPEG" ); | |
1492 | ||
1493 | if ( ! vw->draw_image_save_as_png ) | |
1494 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(jpeg_radio), TRUE ); | |
1495 | ||
1496 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_label, FALSE, FALSE, 0); | |
1497 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), width_spin, FALSE, FALSE, 0); | |
1498 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_label, FALSE, FALSE, 0); | |
1499 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), height_spin, FALSE, FALSE, 0); | |
1500 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), current_window_button, FALSE, FALSE, 0); | |
1501 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), png_radio, FALSE, FALSE, 0); | |
1502 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), jpeg_radio, FALSE, FALSE, 0); | |
1503 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_label, FALSE, FALSE, 0); | |
1504 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), zoom_spin, FALSE, FALSE, 0); | |
1505 | ||
1506 | if ( ! one_image_only ) | |
1507 | { | |
1508 | GtkWidget *tiles_width_label, *tiles_height_label; | |
1509 | ||
1510 | ||
1511 | tiles_width_label = gtk_label_new ( "East-west image tiles:" ); | |
1512 | tiles_width_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 ); | |
1513 | tiles_height_label = gtk_label_new ( "North-south image tiles:" ); | |
1514 | tiles_height_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 5, 1, 10, 1, 100, 0 )), 1, 0 ); | |
1515 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_label, FALSE, FALSE, 0); | |
1516 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_width_spin, FALSE, FALSE, 0); | |
1517 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_label, FALSE, FALSE, 0); | |
1518 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), tiles_height_spin, FALSE, FALSE, 0); | |
1519 | ||
1520 | current_window_pass_along [4] = tiles_width_spin; | |
1521 | current_window_pass_along [5] = tiles_height_spin; | |
1522 | g_signal_connect ( G_OBJECT(tiles_width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along ); | |
1523 | g_signal_connect ( G_OBJECT(tiles_height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along ); | |
1524 | } | |
1525 | gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), total_size_label, FALSE, FALSE, 0); | |
1526 | g_signal_connect ( G_OBJECT(width_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along ); | |
1527 | g_signal_connect ( G_OBJECT(height_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along ); | |
1528 | g_signal_connect ( G_OBJECT(zoom_spin), "value-changed", G_CALLBACK(draw_to_image_file_total_area_cb), current_window_pass_along ); | |
1529 | ||
1530 | draw_to_image_file_total_area_cb ( NULL, current_window_pass_along ); /* set correct size info now */ | |
1531 | ||
1532 | gtk_widget_show_all ( GTK_DIALOG(dialog)->vbox ); | |
1533 | ||
1534 | if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) | |
1535 | { | |
1536 | gtk_widget_hide ( GTK_WIDGET(dialog) ); | |
1537 | if ( one_image_only ) | |
1538 | save_image_file ( vw, fn, | |
1539 | vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ), | |
1540 | vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ), | |
1541 | gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */ | |
1542 | vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ) ); | |
1543 | else { | |
1544 | if ( vik_viewport_get_coord_mode(vw->viking_vvp) == VIK_COORD_UTM ) | |
1545 | save_image_dir ( vw, fn, | |
1546 | vw->draw_image_width = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(width_spin) ), | |
1547 | vw->draw_image_height = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(height_spin) ), | |
1548 | gtk_spin_button_get_value ( GTK_SPIN_BUTTON(zoom_spin) ), /* do not save this value, default is current zoom */ | |
1549 | vw->draw_image_save_as_png = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(png_radio) ), | |
1550 | gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_width_spin) ), | |
1551 | gtk_spin_button_get_value ( GTK_SPIN_BUTTON(tiles_height_spin) ) ); | |
1552 | else | |
1553 | a_dialog_error_msg ( GTK_WINDOW(vw), "You must be in UTM mode to use this feature" ); | |
1554 | } | |
1555 | } | |
1556 | gtk_widget_destroy ( GTK_WIDGET(dialog) ); | |
1557 | } | |
1558 | ||
1559 | ||
e4afc73a | 1560 | static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 | 1561 | { |
50a14534 | 1562 | const gchar *fn; |
f2a1ca71 QT |
1563 | if (!vw->save_img_dia) { |
1564 | vw->save_img_dia = gtk_file_selection_new ("Save Image"); | |
1565 | gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dia), GTK_WINDOW(vw) ); | |
1566 | gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dia), TRUE ); | |
1567 | } | |
50a14534 | 1568 | |
f2a1ca71 | 1569 | while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_OK ) |
50a14534 | 1570 | { |
f2a1ca71 QT |
1571 | fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dia) ); |
1572 | 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 ) ) ) | |
50a14534 | 1573 | { |
50a14534 EB |
1574 | draw_to_image_file ( vw, fn, TRUE ); |
1575 | break; | |
1576 | } | |
1577 | } | |
f2a1ca71 | 1578 | gtk_widget_hide ( vw->save_img_dia ); |
50a14534 EB |
1579 | } |
1580 | ||
e4afc73a | 1581 | static void draw_to_image_dir_cb ( GtkAction *a, VikWindow *vw ) |
50a14534 | 1582 | { |
50a14534 | 1583 | const gchar *fn; |
f2a1ca71 QT |
1584 | if (!vw->save_img_dir_dia) { |
1585 | vw->save_img_dir_dia = gtk_file_selection_new ("Choose a name for a new directory to hold images"); | |
1586 | gtk_window_set_transient_for ( GTK_WINDOW(vw->save_img_dir_dia), GTK_WINDOW(vw) ); | |
1587 | gtk_window_set_destroy_with_parent ( GTK_WINDOW(vw->save_img_dir_dia), TRUE ); | |
1588 | } | |
50a14534 | 1589 | |
f2a1ca71 | 1590 | while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dir_dia) ) == GTK_RESPONSE_OK ) |
50a14534 | 1591 | { |
f2a1ca71 | 1592 | fn = gtk_file_selection_get_filename (GTK_FILE_SELECTION(vw->save_img_dir_dia) ); |
50a14534 | 1593 | if ( access ( fn, F_OK ) == 0 ) |
f2a1ca71 | 1594 | 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) ); |
50a14534 EB |
1595 | else |
1596 | { | |
50a14534 EB |
1597 | draw_to_image_file ( vw, fn, FALSE ); |
1598 | break; | |
1599 | } | |
1600 | } | |
f2a1ca71 | 1601 | gtk_widget_hide ( vw->save_img_dir_dia ); |
50a14534 EB |
1602 | } |
1603 | ||
1604 | /* really a misnomer: changes coord mode (actual coordinates) AND/OR draw mode (viewport only) */ | |
e4afc73a | 1605 | static void window_change_coord_mode_cb ( GtkAction *old_a, GtkAction *a, VikWindow *vw ) |
50a14534 | 1606 | { |
e4afc73a EB |
1607 | VikViewportDrawMode drawmode; |
1608 | if (!strcmp(gtk_action_get_name(a), "ModeUTM")) { | |
1609 | drawmode = VIK_VIEWPORT_DRAWMODE_UTM; | |
1610 | } | |
1611 | else if (!strcmp(gtk_action_get_name(a), "ModeExpedia")) { | |
1612 | drawmode = VIK_VIEWPORT_DRAWMODE_EXPEDIA; | |
1613 | } | |
1614 | else if (!strcmp(gtk_action_get_name(a), "ModeGoogle")) { | |
1615 | drawmode = VIK_VIEWPORT_DRAWMODE_GOOGLE; | |
1616 | } | |
1617 | else if (!strcmp(gtk_action_get_name(a), "ModeKH")) { | |
1618 | drawmode = VIK_VIEWPORT_DRAWMODE_KH; | |
1619 | } | |
1620 | else if (!strcmp(gtk_action_get_name(a), "ModeMercator")) { | |
1621 | drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR; | |
1622 | } | |
1623 | else { | |
7742da66 | 1624 | g_critical("Houston, we got a problem!\n"); |
e4afc73a EB |
1625 | return; |
1626 | } | |
1627 | ||
50a14534 EB |
1628 | if ( !vw->only_updating_coord_mode_ui ) |
1629 | { | |
1630 | VikViewportDrawMode olddrawmode = vik_viewport_get_drawmode ( vw->viking_vvp ); | |
1631 | if ( olddrawmode != drawmode ) | |
1632 | { | |
1633 | /* this takes care of coord mode too */ | |
1634 | vik_viewport_set_drawmode ( vw->viking_vvp, drawmode ); | |
1635 | if ( drawmode == VIK_VIEWPORT_DRAWMODE_UTM ) { | |
1636 | vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_UTM ); | |
1637 | } else if ( olddrawmode == VIK_VIEWPORT_DRAWMODE_UTM ) { | |
1638 | vik_layers_panel_change_coord_mode ( vw->viking_vlp, VIK_COORD_LATLON ); | |
1639 | } | |
1640 | draw_update ( vw ); | |
1641 | } | |
1642 | } | |
1643 | } | |
1644 | ||
35c7c0ba EB |
1645 | static void set_draw_scale ( GtkAction *a, VikWindow *vw ) |
1646 | { | |
1657065a QT |
1647 | GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowScale" ); |
1648 | g_assert(check_box); | |
1649 | gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box)); | |
1650 | vik_viewport_set_draw_scale ( vw->viking_vvp, state ); | |
35c7c0ba EB |
1651 | draw_update ( vw ); |
1652 | } | |
1653 | ||
c933487f QT |
1654 | static void set_draw_centermark ( GtkAction *a, VikWindow *vw ) |
1655 | { | |
1657065a QT |
1656 | GtkWidget *check_box = gtk_ui_manager_get_widget ( vw->uim, "/ui/MainMenu/View/ShowCenterMark" ); |
1657 | g_assert(check_box); | |
1658 | gboolean state = gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM(check_box)); | |
1659 | vik_viewport_set_draw_centermark ( vw->viking_vvp, state ); | |
c933487f QT |
1660 | draw_update ( vw ); |
1661 | } | |
1662 | ||
e4afc73a | 1663 | static void set_bg_color ( GtkAction *a, VikWindow *vw ) |
50a14534 EB |
1664 | { |
1665 | GtkWidget *colorsd = gtk_color_selection_dialog_new ("Choose a background color"); | |
1666 | GdkColor *color = vik_viewport_get_background_gdkcolor ( vw->viking_vvp ); | |
1667 | gtk_color_selection_set_previous_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color ); | |
1668 | gtk_color_selection_set_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color ); | |
1669 | if ( gtk_dialog_run ( GTK_DIALOG(colorsd) ) == GTK_RESPONSE_OK ) | |
1670 | { | |
1671 | gtk_color_selection_get_current_color ( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorsd)->colorsel), color ); | |
1672 | vik_viewport_set_background_gdkcolor ( vw->viking_vvp, color ); | |
1673 | draw_update ( vw ); | |
1674 | } | |
1675 | g_free ( color ); | |
1676 | gtk_widget_destroy ( colorsd ); | |
1677 | } | |
1678 | ||
941aa6e9 AF |
1679 | |
1680 | ||
1681 | /*********************************************************************************************** | |
1682 | ** GUI Creation | |
1683 | ***********************************************************************************************/ | |
1684 | ||
e4afc73a EB |
1685 | static GtkActionEntry entries[] = { |
1686 | { "File", NULL, "_File", 0, 0, 0 }, | |
33534cd8 | 1687 | { "Edit", NULL, "_Edit", 0, 0, 0 }, |
e4afc73a EB |
1688 | { "View", NULL, "_View", 0, 0, 0 }, |
1689 | { "SetZoom", NULL, "_Zoom", 0, 0, 0 }, | |
8c721f83 | 1690 | { "SetPan", NULL, "_Pan", 0, 0, 0 }, |
e4afc73a EB |
1691 | { "Layers", NULL, "_Layers", 0, 0, 0 }, |
1692 | { "Tools", NULL, "_Tools", 0, 0, 0 }, | |
d0a5f320 | 1693 | { "Help", NULL, "_Help", 0, 0, 0 }, |
e4afc73a EB |
1694 | |
1695 | { "New", GTK_STOCK_NEW, "_New", "<control>N", "New file", (GCallback)newwindow_cb }, | |
1696 | { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", (GCallback)load_file }, | |
1697 | { "Append", GTK_STOCK_ADD, "A_ppend File", NULL, "Append data from a different file", (GCallback)load_file }, | |
7b3479e3 EB |
1698 | { "Acquire", NULL, "A_cquire", 0, 0, 0 }, |
1699 | { "AcquireGPS", NULL, "From _GPS", NULL, "Transfer data from a GPS device", (GCallback)acquire_from_gps }, | |
3333c069 | 1700 | { "AcquireGoogle", NULL, "Google _Directions", NULL, "Get driving directions from Google", (GCallback)acquire_from_google }, |
1ef9e637 | 1701 | #ifdef VIK_CONFIG_GEOCACHES |
3333c069 | 1702 | { "AcquireGC", NULL, "Geo_caches", NULL, "Get Geocaches from geocaching.com", (GCallback)acquire_from_gc }, |
1ef9e637 | 1703 | #endif |
e4afc73a EB |
1704 | { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the file", (GCallback)save_file }, |
1705 | { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As", NULL, "Save the file under different name", (GCallback)save_file_as }, | |
1706 | { "GenImg", GTK_STOCK_CLEAR, "_Generate Image File", NULL, "Save a snapshot of the workspace into a file", (GCallback)draw_to_image_file_cb }, | |
1707 | { "GenImgDir", GTK_STOCK_DND_MULTIPLE, "Generate _Directory of Images", NULL, "FIXME:IMGDIR", (GCallback)draw_to_image_dir_cb }, | |
1708 | { "Exit", GTK_STOCK_QUIT, "E_xit", "<control>W", "Exit the program", (GCallback)window_close }, | |
2bf7cadd | 1709 | { "SaveExit", GTK_STOCK_QUIT, "Save and Exit", NULL, "Save and Exit the program", (GCallback)save_file_and_exit }, |
e4afc73a | 1710 | |
c341ddd8 | 1711 | { "GoogleMapsSearch", GTK_STOCK_GO_FORWARD, "Go To Google Maps location", NULL, "Go to address/place using Google Maps search", (GCallback)goto_address }, |
e4afc73a EB |
1712 | { "GotoLL", GTK_STOCK_QUIT, "_Go to Lat\\/Lon...", NULL, "Go to arbitrary lat\\/lon coordinate", (GCallback)draw_goto_cb }, |
1713 | { "GotoUTM", GTK_STOCK_QUIT, "Go to UTM...", NULL, "Go to arbitrary UTM coordinate", (GCallback)draw_goto_cb }, | |
1714 | { "SetBGColor",GTK_STOCK_SELECT_COLOR, "Set Background Color...", NULL, NULL, (GCallback)set_bg_color }, | |
1715 | { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>plus", NULL, (GCallback)draw_zoom_cb }, | |
1716 | { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control>minus", NULL, (GCallback)draw_zoom_cb }, | |
1717 | { "ZoomTo", GTK_STOCK_ZOOM_FIT, "Zoom _To", "<control><shift>Z", NULL, (GCallback)zoom_to_cb }, | |
1718 | { "Zoom0.25", NULL, "0.25", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1719 | { "Zoom0.5", NULL, "0.5", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1720 | { "Zoom1", NULL, "1", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1721 | { "Zoom2", NULL, "2", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1722 | { "Zoom4", NULL, "4", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1723 | { "Zoom8", NULL, "8", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1724 | { "Zoom16", NULL, "16", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1725 | { "Zoom32", NULL, "32", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1726 | { "Zoom64", NULL, "64", NULL, NULL, (GCallback)draw_zoom_cb }, | |
1727 | { "Zoom128", NULL, "128", NULL, NULL, (GCallback)draw_zoom_cb }, | |
8c721f83 EB |
1728 | { "PanNorth", NULL, "Pan North", "<control>Up", NULL, (GCallback)draw_pan_cb }, |
1729 | { "PanEast", NULL, "Pan East", "<control>Right", NULL, (GCallback)draw_pan_cb }, | |
1730 | { "PanSouth", NULL, "Pan South", "<control>Down", NULL, (GCallback)draw_pan_cb }, | |
1731 | { "PanWest", NULL, "Pan West", "<control>Left", NULL, (GCallback)draw_pan_cb }, | |
e4afc73a EB |
1732 | { "BGJobs", GTK_STOCK_EXECUTE, "Background _Jobs", NULL, NULL, (GCallback)a_background_show_window }, |
1733 | ||
1734 | { "Cut", GTK_STOCK_CUT, "Cu_t", NULL, NULL, (GCallback)menu_cut_layer_cb }, | |
1735 | { "Copy", GTK_STOCK_COPY, "_Copy", NULL, NULL, (GCallback)menu_copy_layer_cb }, | |
1736 | { "Paste", GTK_STOCK_PASTE, "_Paste", NULL, NULL, (GCallback)menu_paste_layer_cb }, | |
1737 | { "Delete", GTK_STOCK_DELETE, "_Delete", NULL, NULL, (GCallback)menu_delete_layer_cb }, | |
1738 | { "DeleteAll", NULL, "Delete All", NULL, NULL, (GCallback)clear_cb }, | |
1739 | { "Properties",GTK_STOCK_PROPERTIES, "_Properties", NULL, NULL, (GCallback)menu_properties_cb }, | |
d0a5f320 AF |
1740 | |
1741 | { "About", GTK_STOCK_ABOUT, "_About", NULL, NULL, (GCallback)help_about_cb }, | |
e4afc73a EB |
1742 | }; |
1743 | ||
1744 | /* Radio items */ | |
1745 | static GtkRadioActionEntry mode_entries[] = { | |
1746 | { "ModeUTM", NULL, "_UTM Mode", "<control>u", NULL, 0 }, | |
1747 | { "ModeExpedia", NULL, "_Expedia Mode", "<control>e", NULL, 1 }, | |
f9db6bd6 AF |
1748 | { "ModeGoogle", NULL, "_Old Google Mode", "<control>o", NULL, 2 }, |
1749 | { "ModeKH", NULL, "Old _KH Mode", "<control>k", NULL, 3 }, | |
1750 | { "ModeMercator", NULL, "_Google Mode", "<control>g", NULL, 4 } | |
50a14534 EB |
1751 | }; |
1752 | ||
e4afc73a EB |
1753 | static GtkRadioActionEntry tool_entries[] = { |
1754 | { "Zoom", "vik-icon-zoom", "_Zoom", "<control><shift>Z", "Zoom Tool", 0 }, | |
1755 | { "Ruler", "vik-icon-ruler", "_Ruler", "<control><shift>R", "Ruler Tool", 1 } | |
1756 | }; | |
50a14534 | 1757 | |
35c7c0ba EB |
1758 | static GtkToggleActionEntry toggle_entries[] = { |
1759 | { "ShowScale", NULL, "Show Scale", NULL, NULL, (GCallback)set_draw_scale, TRUE }, | |
c933487f | 1760 | { "ShowCenterMark", NULL, "Show Center Mark", NULL, NULL, (GCallback)set_draw_centermark, TRUE }, |
35c7c0ba EB |
1761 | }; |
1762 | ||
a527cfff | 1763 | #include "menu.xml.h" |
e4afc73a | 1764 | static void window_create_ui( VikWindow *window ) |
50a14534 | 1765 | { |
e4afc73a EB |
1766 | GtkUIManager *uim; |
1767 | GtkActionGroup *action_group; | |
50a14534 | 1768 | GtkAccelGroup *accel_group; |
e4afc73a | 1769 | GError *error; |
e4afc73a EB |
1770 | guint i, j, mid; |
1771 | GtkIconFactory *icon_factory; | |
1772 | GtkIconSet *icon_set; | |
e4afc73a EB |
1773 | GtkRadioActionEntry *tools = NULL, *radio; |
1774 | guint ntools; | |
1775 | ||
1776 | uim = gtk_ui_manager_new (); | |
1777 | window->uim = uim; | |
1778 | ||
9593a4c9 EB |
1779 | toolbox_add_tool(window->vt, &ruler_tool, TOOL_LAYER_TYPE_NONE); |
1780 | toolbox_add_tool(window->vt, &zoom_tool, TOOL_LAYER_TYPE_NONE); | |
941aa6e9 | 1781 | |
e4afc73a | 1782 | error = NULL; |
a527cfff | 1783 | if (!(mid = gtk_ui_manager_add_ui_from_string (uim, menu_xml, -1, &error))) { |
e4afc73a EB |
1784 | g_error_free (error); |
1785 | exit (1); | |
1786 | } | |
1787 | ||
1788 | action_group = gtk_action_group_new ("MenuActions"); | |
1789 | gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), window); | |
35c7c0ba | 1790 | gtk_action_group_add_toggle_actions (action_group, toggle_entries, G_N_ELEMENTS (toggle_entries), window); |
6a9ff0ee | 1791 | gtk_action_group_add_radio_actions (action_group, mode_entries, G_N_ELEMENTS (mode_entries), 4, (GCallback)window_change_coord_mode_cb, window); |
e4afc73a EB |
1792 | |
1793 | icon_factory = gtk_icon_factory_new (); | |
ee36ac4f | 1794 | gtk_icon_factory_add_default (icon_factory); |
e4afc73a EB |
1795 | |
1796 | register_vik_icons(icon_factory); | |
1797 | ||
1798 | ntools = 0; | |
1799 | for (i=0; i<G_N_ELEMENTS(tool_entries); i++) { | |
1800 | tools = g_renew(GtkRadioActionEntry, tools, ntools+1); | |
1801 | radio = &tools[ntools]; | |
1802 | ntools++; | |
1803 | *radio = tool_entries[i]; | |
1804 | radio->value = ntools; | |
1805 | } | |
1806 | ||
1807 | for (i=0; i<VIK_LAYER_NUM_TYPES; i++) { | |
1808 | GtkActionEntry action; | |
1809 | gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Layers/", | |
1810 | vik_layer_get_interface(i)->name, | |
1811 | vik_layer_get_interface(i)->name, | |
1812 | GTK_UI_MANAGER_MENUITEM, FALSE); | |
1813 | ||
1814 | icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata (vik_layer_get_interface(i)->icon, FALSE, NULL )); | |
1815 | gtk_icon_factory_add (icon_factory, vik_layer_get_interface(i)->name, icon_set); | |
1816 | gtk_icon_set_unref (icon_set); | |
1817 | ||
1818 | action.name = vik_layer_get_interface(i)->name; | |
1819 | action.stock_id = vik_layer_get_interface(i)->name; | |
1820 | action.label = g_strdup_printf("New %s Layer", vik_layer_get_interface(i)->name); | |
1821 | action.accelerator = NULL; | |
1822 | action.tooltip = NULL; | |
1823 | action.callback = (GCallback)menu_addlayer_cb; | |
1824 | gtk_action_group_add_actions(action_group, &action, 1, window); | |
1825 | ||
1826 | if ( vik_layer_get_interface(i)->tools_count ) { | |
1827 | gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE); | |
1828 | gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems/", vik_layer_get_interface(i)->name, NULL, GTK_UI_MANAGER_SEPARATOR, FALSE); | |
1829 | } | |
1830 | ||
1831 | for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) { | |
1832 | tools = g_renew(GtkRadioActionEntry, tools, ntools+1); | |
1833 | radio = &tools[ntools]; | |
1834 | ntools++; | |
1835 | ||
e4afc73a EB |
1836 | gtk_ui_manager_add_ui(uim, mid, "/ui/MainMenu/Tools", |
1837 | vik_layer_get_interface(i)->tools[j].name, | |
1838 | vik_layer_get_interface(i)->tools[j].name, | |
1839 | GTK_UI_MANAGER_MENUITEM, FALSE); | |
1840 | gtk_ui_manager_add_ui(uim, mid, "/ui/MainToolbar/ToolItems", | |
1841 | vik_layer_get_interface(i)->tools[j].name, | |
1842 | vik_layer_get_interface(i)->tools[j].name, | |
1843 | GTK_UI_MANAGER_TOOLITEM, FALSE); | |
1844 | ||
9593a4c9 | 1845 | toolbox_add_tool(window->vt, &(vik_layer_get_interface(i)->tools[j]), i); |
941aa6e9 | 1846 | |
e4afc73a EB |
1847 | radio->name = vik_layer_get_interface(i)->tools[j].name; |
1848 | radio->stock_id = vik_layer_get_interface(i)->tools[j].name, | |
1849 | radio->label = vik_layer_get_interface(i)->tools[j].name; | |
1850 | radio->accelerator = NULL; | |
1851 | radio->tooltip = vik_layer_get_interface(i)->tools[j].name; | |
1852 | radio->value = ntools; | |
1853 | } | |
1854 | } | |
ee36ac4f | 1855 | g_object_unref (icon_factory); |
50a14534 | 1856 | |
e4afc73a EB |
1857 | gtk_action_group_add_radio_actions(action_group, tools, ntools, 0, (GCallback)menu_tool_cb, window); |
1858 | g_free(tools); | |
50a14534 | 1859 | |
e4afc73a | 1860 | gtk_ui_manager_insert_action_group (uim, action_group, 0); |
50a14534 | 1861 | |
79845167 QT |
1862 | for (i=0; i<VIK_LAYER_NUM_TYPES; i++) { |
1863 | for ( j = 0; j < vik_layer_get_interface(i)->tools_count; j++ ) { | |
1864 | GtkAction *action = gtk_action_group_get_action(action_group, | |
1865 | vik_layer_get_interface(i)->tools[j].name); | |
1866 | g_object_set(action, "sensitive", FALSE, NULL); | |
1867 | } | |
1868 | } | |
1869 | window->action_group = action_group; | |
1870 | ||
e4afc73a EB |
1871 | accel_group = gtk_ui_manager_get_accel_group (uim); |
1872 | gtk_window_add_accel_group (GTK_WINDOW (window), accel_group); | |
1873 | gtk_ui_manager_ensure_update (uim); | |
e4afc73a | 1874 | } |
50a14534 | 1875 | |
50a14534 | 1876 | |
941aa6e9 | 1877 | |
80dc8501 | 1878 | #include "icons/icons.h" |
e4afc73a | 1879 | static struct { |
4bd45256 | 1880 | const GdkPixdata *data; |
e4afc73a EB |
1881 | gchar *stock_id; |
1882 | } stock_icons[] = { | |
4bd45256 EB |
1883 | { &addtr_18, "Create Track" }, |
1884 | { &edtr_18, "Edit Trackpoint" }, | |
1885 | { &addwp_18, "Create Waypoint" }, | |
1886 | { &edwp_18, "Edit Waypoint" }, | |
ee6ef0b1 | 1887 | { &iscissors_18, "Magic Scissors" }, |
4bd45256 EB |
1888 | { &zoom_18, "vik-icon-zoom" }, |
1889 | { &ruler_18, "vik-icon-ruler" }, | |
1890 | { &geozoom_18, "Georef Zoom Tool" }, | |
1891 | { &geomove_18, "Georef Move Map" }, | |
1892 | { &mapdl_18, "Maps Download" }, | |
0c1044e9 | 1893 | { &demdl_18, "DEM Download/Import" }, |
4bd45256 | 1894 | { &showpic_18, "Show Picture" }, |
e4afc73a EB |
1895 | }; |
1896 | ||
e4afc73a EB |
1897 | static gint n_stock_icons = G_N_ELEMENTS (stock_icons); |
1898 | ||
1899 | static void | |
1900 | register_vik_icons (GtkIconFactory *icon_factory) | |
1901 | { | |
1902 | GtkIconSet *icon_set; | |
e4afc73a | 1903 | gint i; |
e4afc73a EB |
1904 | |
1905 | for (i = 0; i < n_stock_icons; i++) { | |
4bd45256 EB |
1906 | icon_set = gtk_icon_set_new_from_pixbuf (gdk_pixbuf_from_pixdata ( |
1907 | stock_icons[i].data, FALSE, NULL )); | |
e4afc73a EB |
1908 | gtk_icon_factory_add (icon_factory, stock_icons[i].stock_id, icon_set); |
1909 | gtk_icon_set_unref (icon_set); | |
1910 | } | |
50a14534 | 1911 | } |