Parent Directory | Revision Log
Bearing fix and demo.gpx extended, version to 0.8.1
1 | harbaum | 33 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. | ||
3 | * | ||
4 | * This file is part of GPXView. | ||
5 | * | ||
6 | * GPXView 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 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * GPXView 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 GPXView. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include "gpxview.h" | ||
21 | harbaum | 34 | #include <math.h> // for isnan |
22 | harbaum | 33 | |
23 | #ifdef ENABLE_OSM_GPS_MAP | ||
24 | #include "osm-gps-map.h" | ||
25 | #endif | ||
26 | |||
27 | harbaum | 42 | /* equatorial radius in meters */ |
28 | #define EQ_RADIUS (6378137.0) | ||
29 | |||
30 | #define RAD2DEG(a) (((a)*180.0)/M_PI) | ||
31 | #define DEG2RAD(a) (((a)*M_PI)/180.0) | ||
32 | |||
33 | harbaum | 33 | typedef struct { |
34 | harbaum | 34 | appdata_t *appdata; |
35 | harbaum | 33 | GtkWidget *widget; |
36 | GtkWidget *zoomin, *zoomout, *gps; | ||
37 | gint handler_id; | ||
38 | harbaum | 42 | cache_t *press_on; |
39 | harbaum | 40 | #if MAEMO_VERSION_MAJOR == 5 |
40 | GtkWidget *old_view; | ||
41 | #endif | ||
42 | harbaum | 33 | } map_context_t; |
43 | |||
44 | harbaum | 34 | #define PROXY_KEY "/system/http_proxy/" |
45 | |||
46 | static const char *get_proxy_uri(appdata_t *appdata) { | ||
47 | static char proxy_buffer[64] = ""; | ||
48 | harbaum | 33 | |
49 | /* use environment settings if preset */ | ||
50 | const char *proxy = g_getenv("http_proxy"); | ||
51 | if(proxy) { | ||
52 | printf("http_proxy: %s\n", proxy); | ||
53 | return proxy; | ||
54 | } | ||
55 | |||
56 | harbaum | 34 | /* ------------- get proxy settings -------------------- */ |
57 | if(gconf_client_get_bool(appdata->gconf_client, | ||
58 | PROXY_KEY "use_http_proxy", NULL)) { | ||
59 | harbaum | 33 | |
60 | harbaum | 34 | /* we can savely ignore things like "ignore_hosts" since we */ |
61 | /* are pretty sure not inside the net of one of our map renderers */ | ||
62 | /* (unless the user works at google :-) */ | ||
63 | |||
64 | /* get basic settings */ | ||
65 | char *host = | ||
66 | gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL); | ||
67 | if(host) { | ||
68 | int port = | ||
69 | gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL); | ||
70 | harbaum | 33 | |
71 | harbaum | 34 | snprintf(proxy_buffer, sizeof(proxy_buffer), |
72 | "http://%s:%u", host, port); | ||
73 | harbaum | 33 | |
74 | harbaum | 34 | g_free(host); |
75 | } | ||
76 | harbaum | 35 | return proxy_buffer; |
77 | harbaum | 34 | } |
78 | |||
79 | harbaum | 35 | return NULL; |
80 | harbaum | 33 | } |
81 | |||
82 | static void map_zoom(map_context_t *context, int step) { | ||
83 | int zoom; | ||
84 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); | ||
85 | g_object_get(map, "zoom", &zoom, NULL); | ||
86 | zoom = osm_gps_map_set_zoom(map, zoom+step); | ||
87 | |||
88 | /* enable/disable zoom buttons as required */ | ||
89 | gtk_widget_set_sensitive(context->zoomin, zoom<17); | ||
90 | gtk_widget_set_sensitive(context->zoomout, zoom>1); | ||
91 | } | ||
92 | |||
93 | static gboolean | ||
94 | cb_map_zoomin(GtkButton *button, map_context_t *context) { | ||
95 | map_zoom(context, +1); | ||
96 | return FALSE; | ||
97 | } | ||
98 | |||
99 | static gboolean | ||
100 | cb_map_zoomout(GtkButton *button, map_context_t *context) { | ||
101 | map_zoom(context, -1); | ||
102 | return FALSE; | ||
103 | } | ||
104 | |||
105 | static gboolean | ||
106 | cb_map_gps(GtkButton *button, map_context_t *context) { | ||
107 | harbaum | 34 | pos_t *refpos = get_pos(context->appdata); |
108 | if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { | ||
109 | harbaum | 35 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
110 | refpos->lat, refpos->lon, 14); | ||
111 | } else { | ||
112 | /* no coordinates given: display the entire world */ | ||
113 | osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), | ||
114 | 0.0, 0.0, 1); | ||
115 | harbaum | 34 | } |
116 | harbaum | 33 | |
117 | return FALSE; | ||
118 | } | ||
119 | |||
120 | static GtkWidget | ||
121 | *map_add_button(const gchar *icon, GCallback cb, gpointer data, | ||
122 | char *tooltip) { | ||
123 | GtkWidget *button = gtk_button_new(); | ||
124 | gtk_button_set_image(GTK_BUTTON(button), | ||
125 | harbaum | 45 | gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON)); |
126 | harbaum | 33 | g_signal_connect(button, "clicked", cb, data); |
127 | #ifndef USE_MAEMO | ||
128 | gtk_widget_set_tooltip_text(button, tooltip); | ||
129 | #endif | ||
130 | return button; | ||
131 | } | ||
132 | |||
133 | static gboolean map_gps_update(gpointer data) { | ||
134 | map_context_t *context = (map_context_t*)data; | ||
135 | |||
136 | harbaum | 34 | pos_t *refpos = get_pos(context->appdata); |
137 | gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); | ||
138 | harbaum | 33 | |
139 | harbaum | 34 | /* get reference position and go there */ |
140 | gtk_widget_set_sensitive(context->gps, ok); | ||
141 | |||
142 | harbaum | 33 | return TRUE; |
143 | } | ||
144 | |||
145 | harbaum | 35 | static gboolean on_map_configure(GtkWidget *widget, |
146 | GdkEventConfigure *event, | ||
147 | map_context_t *context) { | ||
148 | harbaum | 33 | |
149 | harbaum | 35 | cb_map_gps(NULL, context); |
150 | |||
151 | return FALSE; | ||
152 | } | ||
153 | |||
154 | harbaum | 38 | static void map_draw_cachelist(GtkWidget *map, cache_t *cache) { |
155 | while(cache) { | ||
156 | GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); | ||
157 | |||
158 | osm_gps_map_add_image(OSM_GPS_MAP(map), | ||
159 | cache->pos.lat, cache->pos.lon, icon); | ||
160 | |||
161 | cache = cache->next; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | harbaum | 41 | /* draw a nice popup */ |
166 | typedef struct { | ||
167 | appdata_t *appdata; | ||
168 | GtkWidget *window; | ||
169 | GMainLoop *loop; | ||
170 | } popup_context_t; | ||
171 | |||
172 | harbaum | 47 | /* draw shape */ |
173 | #define ARROW_BORDER 20 | ||
174 | #define CORNER_RADIUS 10 | ||
175 | harbaum | 41 | |
176 | harbaum | 47 | #ifndef USE_MAEMO |
177 | harbaum | 41 | #define POPUP_WIDTH 300 |
178 | #define POPUP_HEIGHT 100 | ||
179 | #else | ||
180 | harbaum | 47 | #define POPUP_WIDTH 350 |
181 | #define POPUP_HEIGHT 120 | ||
182 | harbaum | 41 | #endif |
183 | |||
184 | static gboolean | ||
185 | pointer_in_window(GtkWidget *widget, gint x_root, gint y_root) { | ||
186 | if(GTK_WIDGET_MAPPED(gtk_widget_get_toplevel(widget))) { | ||
187 | gint window_x, window_y; | ||
188 | |||
189 | gdk_window_get_position(gtk_widget_get_toplevel(widget)->window, | ||
190 | &window_x, &window_y); | ||
191 | |||
192 | if(x_root >= window_x && x_root < window_x + widget->allocation.width && | ||
193 | y_root >= window_y && y_root < window_y + widget->allocation.height) | ||
194 | return TRUE; | ||
195 | } | ||
196 | |||
197 | return FALSE; | ||
198 | } | ||
199 | |||
200 | static gboolean | ||
201 | on_button_press_event(GtkWidget *widget, | ||
202 | GdkEventButton *event, popup_context_t *context) { | ||
203 | gboolean in = pointer_in_window(widget, event->x_root, event->y_root); | ||
204 | |||
205 | harbaum | 45 | printf("overlay button press (in = %d)\n", in); |
206 | harbaum | 41 | return !in; |
207 | } | ||
208 | |||
209 | static gboolean | ||
210 | on_button_release_event(GtkWidget *widget, | ||
211 | GdkEventButton *event, popup_context_t *context) { | ||
212 | gboolean in = pointer_in_window(widget, event->x_root, event->y_root); | ||
213 | |||
214 | harbaum | 45 | printf("overlay button release (in = %d)\n", in); |
215 | harbaum | 41 | |
216 | if(!in) { | ||
217 | printf("destroying popup\n"); | ||
218 | gtk_widget_destroy(gtk_widget_get_toplevel(widget)); | ||
219 | } | ||
220 | |||
221 | return !in; | ||
222 | } | ||
223 | |||
224 | static void | ||
225 | shutdown_loop(popup_context_t *context) { | ||
226 | if(g_main_loop_is_running(context->loop)) | ||
227 | g_main_loop_quit(context->loop); | ||
228 | } | ||
229 | |||
230 | static gint | ||
231 | run_delete_handler(GtkWindow *window, GdkEventAny *event, | ||
232 | popup_context_t *context) { | ||
233 | shutdown_loop(context); | ||
234 | return TRUE; /* Do not destroy */ | ||
235 | } | ||
236 | |||
237 | static void | ||
238 | run_destroy_handler(GtkWindow *window, popup_context_t *context) { | ||
239 | /* shutdown_loop will be called by run_unmap_handler */ | ||
240 | printf("popup destroyed\n"); | ||
241 | } | ||
242 | |||
243 | static void | ||
244 | run_unmap_handler(GtkWindow *window, popup_context_t *context) { | ||
245 | shutdown_loop(context); | ||
246 | } | ||
247 | |||
248 | harbaum | 46 | static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) { |
249 | GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1); | ||
250 | |||
251 | GdkGC *gc = gdk_gc_new(mask); | ||
252 | GdkColormap *colormap; | ||
253 | GdkColor black; | ||
254 | GdkColor white; | ||
255 | |||
256 | /* get black/white color values */ | ||
257 | colormap = gdk_colormap_get_system(); | ||
258 | gdk_color_black(colormap, &black); | ||
259 | gdk_color_white(colormap, &white); | ||
260 | |||
261 | /* erase */ | ||
262 | gdk_gc_set_foreground(gc, &black); | ||
263 | gdk_gc_set_background(gc, &white); | ||
264 | |||
265 | /* erase background */ | ||
266 | gdk_draw_rectangle(mask, gc, TRUE, 0, 0, POPUP_WIDTH, POPUP_HEIGHT); | ||
267 | |||
268 | gdk_gc_set_foreground(gc, &white); | ||
269 | gdk_gc_set_background(gc, &black); | ||
270 | |||
271 | harbaum | 47 | /* the tip is always above or below the "bubble" but never at its side */ |
272 | guint tip_offset = (tip_y == 0)?ARROW_BORDER:0; | ||
273 | |||
274 | harbaum | 46 | gdk_draw_rectangle(mask, gc, TRUE, |
275 | harbaum | 47 | 0, tip_offset + CORNER_RADIUS, |
276 | harbaum | 46 | POPUP_WIDTH, |
277 | harbaum | 47 | POPUP_HEIGHT - 2*CORNER_RADIUS - ARROW_BORDER); |
278 | harbaum | 46 | |
279 | gdk_draw_rectangle(mask, gc, TRUE, | ||
280 | harbaum | 47 | CORNER_RADIUS, tip_offset, |
281 | harbaum | 46 | POPUP_WIDTH - 2*CORNER_RADIUS, |
282 | harbaum | 47 | POPUP_HEIGHT - ARROW_BORDER); |
283 | harbaum | 46 | |
284 | int off[][2] = { | ||
285 | harbaum | 47 | { CORNER_RADIUS, tip_offset + CORNER_RADIUS }, |
286 | { POPUP_WIDTH - CORNER_RADIUS, tip_offset + CORNER_RADIUS }, | ||
287 | harbaum | 46 | { POPUP_WIDTH - CORNER_RADIUS, |
288 | harbaum | 47 | POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset}, |
289 | harbaum | 46 | { CORNER_RADIUS, |
290 | harbaum | 47 | POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER + tip_offset}}; |
291 | harbaum | 46 | |
292 | int i; | ||
293 | for(i=0;i<4;i++) { | ||
294 | gdk_draw_arc(mask, gc, TRUE, | ||
295 | off[i][0]-CORNER_RADIUS, off[i][1]-CORNER_RADIUS, | ||
296 | 2*CORNER_RADIUS, 2*CORNER_RADIUS, | ||
297 | 0, 360*64); | ||
298 | } | ||
299 | |||
300 | GdkPoint points[3] = { {POPUP_WIDTH*1/3, POPUP_HEIGHT/2}, | ||
301 | {POPUP_WIDTH*2/3, POPUP_HEIGHT/2}, | ||
302 | {tip_x,tip_y} }; | ||
303 | gdk_draw_polygon(mask, gc, TRUE, points, 3); | ||
304 | |||
305 | |||
306 | gdk_window_shape_combine_mask(window->window, mask, 0, 0); | ||
307 | } | ||
308 | |||
309 | harbaum | 47 | /* create a left aligned label (normal ones are centered) */ |
310 | static GtkWidget *gtk_label_left_new(char *str) { | ||
311 | GtkWidget *label = gtk_label_new(str); | ||
312 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); | ||
313 | return label; | ||
314 | } | ||
315 | |||
316 | /* the small labels are actually only on maemo small */ | ||
317 | #ifdef USE_MAEMO | ||
318 | #define MARKUP_SMALL "<span size='small'>%s</span>" | ||
319 | GtkWidget *gtk_label_small_left_new(char *str) { | ||
320 | GtkWidget *label = gtk_label_new(""); | ||
321 | char *markup = g_markup_printf_escaped(MARKUP_SMALL, str); | ||
322 | gtk_label_set_markup(GTK_LABEL(label), markup); | ||
323 | g_free(markup); | ||
324 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); | ||
325 | return label; | ||
326 | } | ||
327 | #define gtk_label_big_left_new(a) gtk_label_left_new(a) | ||
328 | #else | ||
329 | #define gtk_label_small_left_new(a) gtk_label_left_new(a) | ||
330 | #define MARKUP_BIG "<span size='x-large'>%s</span>" | ||
331 | GtkWidget *gtk_label_big_left_new(char *str) { | ||
332 | GtkWidget *label = gtk_label_new(""); | ||
333 | char *markup = g_markup_printf_escaped(MARKUP_BIG, str); | ||
334 | gtk_label_set_markup(GTK_LABEL(label), markup); | ||
335 | g_free(markup); | ||
336 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, .5f); | ||
337 | return label; | ||
338 | } | ||
339 | #endif | ||
340 | |||
341 | harbaum | 42 | void cache_popup(map_context_t *mcontext, cache_t *cache) { |
342 | popup_context_t pcontext; | ||
343 | pcontext.appdata = mcontext->appdata; | ||
344 | harbaum | 41 | |
345 | harbaum | 42 | pcontext.window = gtk_window_new(GTK_WINDOW_POPUP); |
346 | gtk_widget_realize(pcontext.window); | ||
347 | gtk_window_set_default_size(GTK_WINDOW(pcontext.window), | ||
348 | harbaum | 41 | POPUP_WIDTH, POPUP_HEIGHT); |
349 | harbaum | 45 | gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT); |
350 | harbaum | 42 | // gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE); |
351 | gtk_window_set_transient_for(GTK_WINDOW(pcontext.window), | ||
352 | GTK_WINDOW(mcontext->appdata->window)); | ||
353 | gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE); | ||
354 | gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE); | ||
355 | gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC); | ||
356 | gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE); | ||
357 | harbaum | 41 | |
358 | /* connect events */ | ||
359 | harbaum | 42 | g_signal_connect(G_OBJECT(pcontext.window), "button-press-event", |
360 | G_CALLBACK(on_button_press_event), &pcontext); | ||
361 | g_signal_connect(G_OBJECT(pcontext.window), "button-release-event", | ||
362 | G_CALLBACK(on_button_release_event), &pcontext); | ||
363 | g_signal_connect(G_OBJECT(pcontext.window), "delete-event", | ||
364 | G_CALLBACK(run_delete_handler), &pcontext); | ||
365 | g_signal_connect(G_OBJECT(pcontext.window), "destroy", | ||
366 | G_CALLBACK(run_destroy_handler), &pcontext); | ||
367 | g_signal_connect(G_OBJECT(pcontext.window), "unmap", | ||
368 | G_CALLBACK(run_unmap_handler), &pcontext); | ||
369 | harbaum | 41 | |
370 | harbaum | 42 | gdk_pointer_grab(pcontext.window->window, TRUE, |
371 | harbaum | 41 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
372 | NULL, NULL, GDK_CURRENT_TIME); | ||
373 | harbaum | 42 | gtk_grab_add(pcontext.window); |
374 | harbaum | 41 | |
375 | harbaum | 42 | /* check whether cache is in upper or lower half of window */ |
376 | harbaum | 45 | gint x, y, sx, sy; |
377 | harbaum | 42 | osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget), |
378 | cache->pos.lat, cache->pos.lon, | ||
379 | harbaum | 45 | &sx, &sy); |
380 | harbaum | 42 | |
381 | gdk_window_get_origin(mcontext->widget->window, &x, &y); | ||
382 | |||
383 | harbaum | 45 | gint ax = 0, ay = 0; |
384 | if(sx > mcontext->widget->allocation.width/2) | ||
385 | ax = POPUP_WIDTH; | ||
386 | harbaum | 41 | |
387 | harbaum | 45 | if(sy > mcontext->widget->allocation.height/2) |
388 | ay = POPUP_HEIGHT; | ||
389 | harbaum | 41 | |
390 | harbaum | 47 | #if !defined(USE_MAEMO) || (MAEMO_VERSION_MAJOR < 5) |
391 | harbaum | 46 | GdkColor color; |
392 | gdk_color_parse("darkgray", &color); | ||
393 | gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color); | ||
394 | #endif | ||
395 | |||
396 | harbaum | 42 | gtk_window_move(GTK_WINDOW(pcontext.window), |
397 | harbaum | 45 | x + mcontext->widget->allocation.x + sx - ax, |
398 | y + mcontext->widget->allocation.y + sy - ay); | ||
399 | harbaum | 41 | |
400 | harbaum | 46 | |
401 | harbaum | 47 | GtkWidget *alignment = gtk_alignment_new(0.5, 0.5, 1.0, 1.0); |
402 | gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), | ||
403 | CORNER_RADIUS/2 + (ay?0:ARROW_BORDER), | ||
404 | CORNER_RADIUS/2 + (ay?ARROW_BORDER:0), | ||
405 | CORNER_RADIUS, CORNER_RADIUS); | ||
406 | harbaum | 41 | |
407 | harbaum | 47 | /* --- actual content ---- */ |
408 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); | ||
409 | |||
410 | if(cache->id) { | ||
411 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); | ||
412 | |||
413 | gtk_box_pack_start(GTK_BOX(ihbox), | ||
414 | icon_get_widget(ICON_CACHE_TYPE, cache->type), | ||
415 | FALSE, FALSE, 5); | ||
416 | |||
417 | gtk_box_pack_start_defaults(GTK_BOX(ihbox), | ||
418 | gtk_label_big_left_new(cache->id)); | ||
419 | |||
420 | gtk_box_pack_start_defaults(GTK_BOX(vbox), ihbox); | ||
421 | } | ||
422 | |||
423 | if(cache->name) { | ||
424 | GtkWidget *label = gtk_label_small_left_new(cache->name); | ||
425 | gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); | ||
426 | gtk_box_pack_start_defaults(GTK_BOX(vbox), label); | ||
427 | } | ||
428 | |||
429 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); | ||
430 | if(cache->terrain) { | ||
431 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); | ||
432 | gtk_box_pack_start(GTK_BOX(ihbox), | ||
433 | gtk_label_small_left_new(_("Terrain:")), FALSE, FALSE, 0); | ||
434 | gtk_box_pack_start(GTK_BOX(ihbox), | ||
435 | icon_get_widget(ICON_STARS, (int)(cache->terrain*2-2)), | ||
436 | FALSE, FALSE, 5); | ||
437 | gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox); | ||
438 | } | ||
439 | |||
440 | if(cache->difficulty) { | ||
441 | GtkWidget *ihbox = gtk_hbox_new(FALSE, 0); | ||
442 | gtk_box_pack_start(GTK_BOX(ihbox), | ||
443 | gtk_label_small_left_new(_("Difficulty:")), FALSE, FALSE, 0); | ||
444 | gtk_box_pack_start(GTK_BOX(ihbox), | ||
445 | icon_get_widget(ICON_STARS, (int)(cache->difficulty*2-2)), | ||
446 | FALSE, FALSE, 5); | ||
447 | gtk_box_pack_start_defaults(GTK_BOX(hbox), ihbox); | ||
448 | } | ||
449 | |||
450 | gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); | ||
451 | |||
452 | gtk_container_add(GTK_CONTAINER(alignment), vbox); | ||
453 | /* ----------------------- */ | ||
454 | |||
455 | |||
456 | gtk_container_add(GTK_CONTAINER(pcontext.window), alignment); | ||
457 | harbaum | 41 | |
458 | harbaum | 47 | /* give window its shape */ |
459 | harbaum | 46 | popup_window_shape(pcontext.window, ax, ay); |
460 | |||
461 | harbaum | 42 | gtk_widget_show_all(pcontext.window); |
462 | harbaum | 41 | |
463 | /* handle this popup until it's gone */ | ||
464 | |||
465 | harbaum | 42 | pcontext.loop = g_main_loop_new(NULL, FALSE); |
466 | harbaum | 41 | |
467 | GDK_THREADS_LEAVE(); | ||
468 | harbaum | 42 | g_main_loop_run(pcontext.loop); |
469 | harbaum | 41 | GDK_THREADS_ENTER(); |
470 | |||
471 | harbaum | 42 | g_main_loop_unref(pcontext.loop); |
472 | harbaum | 41 | |
473 | printf("cache popup removed\n"); | ||
474 | } | ||
475 | |||
476 | static void | ||
477 | map_cachelist_nearest(cache_t *cache, pos_t *pos, | ||
478 | cache_t **result, float *distance) { | ||
479 | while(cache) { | ||
480 | float dist = | ||
481 | pow(cache->pos.lat - pos->lat, 2) + | ||
482 | pow(cache->pos.lon - pos->lon, 2); | ||
483 | |||
484 | if(!(dist > *distance)) { | ||
485 | *result = cache; | ||
486 | *distance = dist; | ||
487 | } | ||
488 | |||
489 | cache = cache->next; | ||
490 | } | ||
491 | } | ||
492 | |||
493 | static cache_t *map_closest(map_context_t *context, pos_t *pos) { | ||
494 | cache_t *result = NULL; | ||
495 | float distance = NAN; | ||
496 | |||
497 | #ifdef USE_MAEMO | ||
498 | if(!context->appdata->cur_gpx) { | ||
499 | #endif | ||
500 | /* search all geocaches */ | ||
501 | gpx_t *gpx = context->appdata->gpx; | ||
502 | while(gpx) { | ||
503 | map_cachelist_nearest(gpx->cache, pos, &result, &distance); | ||
504 | gpx = gpx->next; | ||
505 | } | ||
506 | #ifdef USE_MAEMO | ||
507 | } else { | ||
508 | map_cachelist_nearest(context->appdata->cur_gpx->cache, | ||
509 | pos, &result, &distance); | ||
510 | } | ||
511 | #endif | ||
512 | |||
513 | return result; | ||
514 | } | ||
515 | |||
516 | /* translate between osm-gps-map positions and gpxview ones */ | ||
517 | pos_t coord2pos(coord_t coo) { | ||
518 | pos_t pos; | ||
519 | pos.lat = RAD2DEG(coo.rlat); | ||
520 | pos.lon = RAD2DEG(coo.rlon); | ||
521 | return pos; | ||
522 | } | ||
523 | |||
524 | harbaum | 42 | static int dist2pixel(map_context_t *context, float km, float lat) { |
525 | int zoom; | ||
526 | g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); | ||
527 | |||
528 | /* world at zoom 1 == 512 pixels */ | ||
529 | float m_per_pix = | ||
530 | cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom)); | ||
531 | |||
532 | return 1000.0*km/m_per_pix; | ||
533 | } | ||
534 | |||
535 | harbaum | 47 | #define CLICK_FUZZ (24) |
536 | harbaum | 42 | |
537 | harbaum | 41 | static gboolean |
538 | on_map_button_press_event(GtkWidget *widget, | ||
539 | GdkEventButton *event, map_context_t *context) { | ||
540 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); | ||
541 | |||
542 | harbaum | 44 | /* got a press event without release event? eat it! */ |
543 | if(context->press_on != NULL) { | ||
544 | printf("PRESS: already\n"); | ||
545 | return TRUE; | ||
546 | } | ||
547 | |||
548 | harbaum | 41 | pos_t pos = |
549 | harbaum | 42 | coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
550 | harbaum | 41 | |
551 | harbaum | 42 | cache_t *nearest = map_closest(context, &pos); |
552 | if(nearest) { | ||
553 | float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); | ||
554 | harbaum | 44 | if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
555 | harbaum | 42 | context->press_on = nearest; |
556 | } | ||
557 | harbaum | 44 | |
558 | harbaum | 41 | return FALSE; |
559 | } | ||
560 | |||
561 | static gboolean | ||
562 | on_map_button_release_event(GtkWidget *widget, | ||
563 | GdkEventButton *event, map_context_t *context) { | ||
564 | harbaum | 42 | if(context->press_on) { |
565 | OsmGpsMap *map = OSM_GPS_MAP(context->widget); | ||
566 | harbaum | 41 | |
567 | harbaum | 42 | pos_t pos = |
568 | coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); | ||
569 | harbaum | 41 | |
570 | harbaum | 42 | cache_t *nearest = map_closest(context, &pos); |
571 | if(nearest && nearest == context->press_on) { | ||
572 | float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); | ||
573 | harbaum | 44 | if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
574 | harbaum | 42 | cache_popup(context, nearest); |
575 | } | ||
576 | harbaum | 44 | context->press_on = NULL; |
577 | harbaum | 41 | } |
578 | |||
579 | return FALSE; | ||
580 | } | ||
581 | |||
582 | harbaum | 44 | |
583 | harbaum | 40 | #if MAEMO_VERSION_MAJOR == 5 |
584 | static void on_window_destroy(GtkWidget *widget, map_context_t *context) { | ||
585 | printf("destroy map view\n"); | ||
586 | |||
587 | /* restore cur_view */ | ||
588 | context->appdata->cur_view = context->old_view; | ||
589 | |||
590 | gtk_timeout_remove(context->handler_id); | ||
591 | g_free(context); | ||
592 | } | ||
593 | #endif | ||
594 | |||
595 | harbaum | 33 | void map(appdata_t *appdata) { |
596 | harbaum | 40 | map_context_t *context = g_new0(map_context_t, 1); |
597 | context->appdata = appdata; | ||
598 | harbaum | 33 | |
599 | harbaum | 41 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
600 | |||
601 | char *path = g_strdup_printf("%s/map/", appdata->image_path); | ||
602 | const char *proxy = get_proxy_uri(appdata); | ||
603 | |||
604 | context->widget = g_object_new(OSM_TYPE_GPS_MAP, | ||
605 | "repo-uri", MAP_SOURCE_OPENSTREETMAP, | ||
606 | "tile-cache", path, | ||
607 | proxy?"proxy-uri":NULL, proxy, | ||
608 | NULL); | ||
609 | |||
610 | g_free(path); | ||
611 | |||
612 | char *name = NULL; | ||
613 | #ifdef USE_MAEMO | ||
614 | if(!appdata->cur_gpx) { | ||
615 | #endif | ||
616 | /* draw all geocaches */ | ||
617 | gpx_t *gpx = appdata->gpx; | ||
618 | while(gpx) { | ||
619 | map_draw_cachelist(context->widget, gpx->cache); | ||
620 | gpx = gpx->next; | ||
621 | } | ||
622 | name = g_strdup(_("all geocaches")); | ||
623 | #ifdef USE_MAEMO | ||
624 | } else { | ||
625 | map_draw_cachelist(context->widget, appdata->cur_gpx->cache); | ||
626 | harbaum | 44 | name = g_strdup(appdata->cur_gpx->name); |
627 | harbaum | 41 | } |
628 | #endif | ||
629 | |||
630 | char *title = g_strdup_printf(_("Map - %s"), name); | ||
631 | g_free(name); | ||
632 | |||
633 | harbaum | 40 | #if MAEMO_VERSION_MAJOR == 5 |
634 | GtkWidget *window = hildon_stackable_window_new(); | ||
635 | harbaum | 41 | gtk_window_set_title(GTK_WINDOW(window), title); |
636 | harbaum | 40 | #else |
637 | harbaum | 41 | GtkWidget *dialog = gtk_dialog_new_with_buttons(title, |
638 | harbaum | 33 | GTK_WINDOW(appdata->window), |
639 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | ||
640 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, | ||
641 | NULL); | ||
642 | |||
643 | #ifndef USE_MAEMO | ||
644 | harbaum | 45 | gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480); |
645 | harbaum | 33 | #else |
646 | gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480); | ||
647 | #endif | ||
648 | harbaum | 40 | #endif |
649 | harbaum | 33 | |
650 | harbaum | 41 | g_free(title); |
651 | harbaum | 33 | |
652 | harbaum | 41 | g_signal_connect(G_OBJECT(context->widget), "configure-event", |
653 | G_CALLBACK(on_map_configure), context); | ||
654 | harbaum | 33 | |
655 | harbaum | 41 | g_signal_connect(G_OBJECT(context->widget), "button-press-event", |
656 | G_CALLBACK(on_map_button_press_event), context); | ||
657 | harbaum | 33 | |
658 | harbaum | 40 | g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
659 | G_CALLBACK(on_map_button_release_event), context); | ||
660 | harbaum | 33 | |
661 | harbaum | 40 | gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget); |
662 | harbaum | 33 | /* zoom button box */ |
663 | GtkWidget *vbox = gtk_vbox_new(FALSE,0); | ||
664 | |||
665 | harbaum | 40 | context->zoomin = |
666 | harbaum | 33 | map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin), |
667 | harbaum | 40 | context, _("Zoom in")); |
668 | gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0); | ||
669 | harbaum | 33 | |
670 | harbaum | 40 | context->zoomout = |
671 | harbaum | 33 | map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout), |
672 | harbaum | 40 | context, _("Zoom out")); |
673 | gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0); | ||
674 | harbaum | 33 | |
675 | harbaum | 40 | context->gps = |
676 | harbaum | 33 | map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps), |
677 | harbaum | 40 | context, _("Jump to GPS position")); |
678 | gtk_widget_set_sensitive(context->gps, FALSE); | ||
679 | harbaum | 33 | /* install handler for timed updates of the gps button */ |
680 | harbaum | 40 | context->handler_id = gtk_timeout_add(1000, map_gps_update, context); |
681 | gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0); | ||
682 | harbaum | 33 | |
683 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | ||
684 | |||
685 | harbaum | 40 | #if MAEMO_VERSION_MAJOR == 5 |
686 | /* prevent some of the main screen things */ | ||
687 | context->old_view = appdata->cur_view; | ||
688 | appdata->cur_view = NULL; | ||
689 | |||
690 | g_signal_connect(G_OBJECT(window), "destroy", | ||
691 | G_CALLBACK(on_window_destroy), context); | ||
692 | |||
693 | gtk_container_add(GTK_CONTAINER(window), hbox); | ||
694 | gtk_widget_show_all(GTK_WIDGET(window)); | ||
695 | |||
696 | #else | ||
697 | harbaum | 33 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
698 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); | ||
699 | gtk_widget_show_all(dialog); | ||
700 | gtk_dialog_run(GTK_DIALOG(dialog)); | ||
701 | harbaum | 40 | gtk_timeout_remove(context->handler_id); |
702 | harbaum | 33 | gtk_widget_destroy(dialog); |
703 | harbaum | 40 | g_free(context); |
704 | #endif | ||
705 | harbaum | 33 | } |