24 |
#include "osm-gps-map.h" |
#include "osm-gps-map.h" |
25 |
#endif |
#endif |
26 |
|
|
27 |
|
/* 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 |
typedef struct { |
typedef struct { |
34 |
appdata_t *appdata; |
appdata_t *appdata; |
35 |
GtkWidget *widget; |
GtkWidget *widget; |
36 |
GtkWidget *zoomin, *zoomout, *gps; |
GtkWidget *zoomin, *zoomout, *gps; |
37 |
gint handler_id; |
gint handler_id; |
38 |
|
cache_t *press_on; |
39 |
#if MAEMO_VERSION_MAJOR == 5 |
#if MAEMO_VERSION_MAJOR == 5 |
40 |
GtkWidget *old_view; |
GtkWidget *old_view; |
41 |
#endif |
#endif |
162 |
} |
} |
163 |
} |
} |
164 |
|
|
165 |
|
/* draw a nice popup */ |
166 |
|
typedef struct { |
167 |
|
appdata_t *appdata; |
168 |
|
GtkWidget *window; |
169 |
|
GMainLoop *loop; |
170 |
|
} popup_context_t; |
171 |
|
|
172 |
|
|
173 |
|
#ifndef USE_HILDON |
174 |
|
#define POPUP_WIDTH 300 |
175 |
|
#define POPUP_HEIGHT 100 |
176 |
|
#else |
177 |
|
#define POPUP_WIDTH 600 |
178 |
|
#define POPUP_HEIGHT 200 |
179 |
|
#endif |
180 |
|
|
181 |
|
static gboolean |
182 |
|
pointer_in_window(GtkWidget *widget, gint x_root, gint y_root) { |
183 |
|
if(GTK_WIDGET_MAPPED(gtk_widget_get_toplevel(widget))) { |
184 |
|
gint window_x, window_y; |
185 |
|
|
186 |
|
gdk_window_get_position(gtk_widget_get_toplevel(widget)->window, |
187 |
|
&window_x, &window_y); |
188 |
|
|
189 |
|
if(x_root >= window_x && x_root < window_x + widget->allocation.width && |
190 |
|
y_root >= window_y && y_root < window_y + widget->allocation.height) |
191 |
|
return TRUE; |
192 |
|
} |
193 |
|
|
194 |
|
return FALSE; |
195 |
|
} |
196 |
|
|
197 |
|
static gboolean |
198 |
|
on_button_press_event(GtkWidget *widget, |
199 |
|
GdkEventButton *event, popup_context_t *context) { |
200 |
|
gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
201 |
|
|
202 |
|
printf("overlay button press(in = %d)\n", in); |
203 |
|
return !in; |
204 |
|
} |
205 |
|
|
206 |
|
static gboolean |
207 |
|
on_button_release_event(GtkWidget *widget, |
208 |
|
GdkEventButton *event, popup_context_t *context) { |
209 |
|
gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
210 |
|
|
211 |
|
printf("overlay button release(in = %d)\n", in); |
212 |
|
|
213 |
|
if(!in) { |
214 |
|
printf("destroying popup\n"); |
215 |
|
gtk_widget_destroy(gtk_widget_get_toplevel(widget)); |
216 |
|
} |
217 |
|
|
218 |
|
return !in; |
219 |
|
} |
220 |
|
|
221 |
|
static void |
222 |
|
shutdown_loop(popup_context_t *context) { |
223 |
|
if(g_main_loop_is_running(context->loop)) |
224 |
|
g_main_loop_quit(context->loop); |
225 |
|
} |
226 |
|
|
227 |
|
static gint |
228 |
|
run_delete_handler(GtkWindow *window, GdkEventAny *event, |
229 |
|
popup_context_t *context) { |
230 |
|
shutdown_loop(context); |
231 |
|
return TRUE; /* Do not destroy */ |
232 |
|
} |
233 |
|
|
234 |
|
static void |
235 |
|
run_destroy_handler(GtkWindow *window, popup_context_t *context) { |
236 |
|
/* shutdown_loop will be called by run_unmap_handler */ |
237 |
|
printf("popup destroyed\n"); |
238 |
|
} |
239 |
|
|
240 |
|
static void |
241 |
|
run_unmap_handler(GtkWindow *window, popup_context_t *context) { |
242 |
|
shutdown_loop(context); |
243 |
|
} |
244 |
|
|
245 |
|
void cache_popup(map_context_t *mcontext, cache_t *cache) { |
246 |
|
popup_context_t pcontext; |
247 |
|
pcontext.appdata = mcontext->appdata; |
248 |
|
|
249 |
|
pcontext.window = gtk_window_new(GTK_WINDOW_POPUP); |
250 |
|
gtk_widget_realize(pcontext.window); |
251 |
|
gtk_window_set_default_size(GTK_WINDOW(pcontext.window), |
252 |
|
POPUP_WIDTH, POPUP_HEIGHT); |
253 |
|
gtk_window_resize(GTK_WINDOW(pcontext.window), |
254 |
|
POPUP_WIDTH, POPUP_HEIGHT); |
255 |
|
// gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE); |
256 |
|
gtk_window_set_transient_for(GTK_WINDOW(pcontext.window), |
257 |
|
GTK_WINDOW(mcontext->appdata->window)); |
258 |
|
gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE); |
259 |
|
gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE); |
260 |
|
gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC); |
261 |
|
gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE); |
262 |
|
|
263 |
|
/* connect events */ |
264 |
|
g_signal_connect(G_OBJECT(pcontext.window), "button-press-event", |
265 |
|
G_CALLBACK(on_button_press_event), &pcontext); |
266 |
|
g_signal_connect(G_OBJECT(pcontext.window), "button-release-event", |
267 |
|
G_CALLBACK(on_button_release_event), &pcontext); |
268 |
|
g_signal_connect(G_OBJECT(pcontext.window), "delete-event", |
269 |
|
G_CALLBACK(run_delete_handler), &pcontext); |
270 |
|
g_signal_connect(G_OBJECT(pcontext.window), "destroy", |
271 |
|
G_CALLBACK(run_destroy_handler), &pcontext); |
272 |
|
g_signal_connect(G_OBJECT(pcontext.window), "unmap", |
273 |
|
G_CALLBACK(run_unmap_handler), &pcontext); |
274 |
|
|
275 |
|
gdk_pointer_grab(pcontext.window->window, TRUE, |
276 |
|
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
277 |
|
NULL, NULL, GDK_CURRENT_TIME); |
278 |
|
gtk_grab_add(pcontext.window); |
279 |
|
|
280 |
|
/* check whether cache is in upper or lower half of window */ |
281 |
|
gint x, y; |
282 |
|
osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget), |
283 |
|
cache->pos.lat, cache->pos.lon, |
284 |
|
&x, &y); |
285 |
|
|
286 |
|
printf("screen pos %d/%d\n", x, y); |
287 |
|
|
288 |
|
gdk_window_get_origin(mcontext->widget->window, &x, &y); |
289 |
|
printf("window = %d/%d %d/%d\n", x, y, |
290 |
|
mcontext->widget->allocation.x, |
291 |
|
mcontext->widget->allocation.y); |
292 |
|
|
293 |
|
// gtk_window_move(GTK_WINDOW(pcontext.window), |
294 |
|
// x + button->allocation.x, |
295 |
|
// y + button->allocation.y - HEIGHT); |
296 |
|
|
297 |
|
|
298 |
|
gtk_window_move(GTK_WINDOW(pcontext.window), |
299 |
|
100, |
300 |
|
100); |
301 |
|
|
302 |
|
/* a frame with a vscale inside */ |
303 |
|
GtkWidget *frame = gtk_frame_new(NULL); |
304 |
|
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); |
305 |
|
|
306 |
|
gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name)); |
307 |
|
gtk_container_add(GTK_CONTAINER(pcontext.window), frame); |
308 |
|
|
309 |
|
gtk_widget_show_all(pcontext.window); |
310 |
|
|
311 |
|
/* handle this popup until it's gone */ |
312 |
|
|
313 |
|
pcontext.loop = g_main_loop_new(NULL, FALSE); |
314 |
|
|
315 |
|
GDK_THREADS_LEAVE(); |
316 |
|
g_main_loop_run(pcontext.loop); |
317 |
|
GDK_THREADS_ENTER(); |
318 |
|
|
319 |
|
g_main_loop_unref(pcontext.loop); |
320 |
|
|
321 |
|
printf("cache popup removed\n"); |
322 |
|
} |
323 |
|
|
324 |
|
static void |
325 |
|
map_cachelist_nearest(cache_t *cache, pos_t *pos, |
326 |
|
cache_t **result, float *distance) { |
327 |
|
while(cache) { |
328 |
|
float dist = |
329 |
|
pow(cache->pos.lat - pos->lat, 2) + |
330 |
|
pow(cache->pos.lon - pos->lon, 2); |
331 |
|
|
332 |
|
if(!(dist > *distance)) { |
333 |
|
*result = cache; |
334 |
|
*distance = dist; |
335 |
|
} |
336 |
|
|
337 |
|
cache = cache->next; |
338 |
|
} |
339 |
|
} |
340 |
|
|
341 |
|
static cache_t *map_closest(map_context_t *context, pos_t *pos) { |
342 |
|
cache_t *result = NULL; |
343 |
|
float distance = NAN; |
344 |
|
|
345 |
|
#ifdef USE_MAEMO |
346 |
|
if(!context->appdata->cur_gpx) { |
347 |
|
#endif |
348 |
|
/* search all geocaches */ |
349 |
|
gpx_t *gpx = context->appdata->gpx; |
350 |
|
while(gpx) { |
351 |
|
map_cachelist_nearest(gpx->cache, pos, &result, &distance); |
352 |
|
gpx = gpx->next; |
353 |
|
} |
354 |
|
#ifdef USE_MAEMO |
355 |
|
} else { |
356 |
|
map_cachelist_nearest(context->appdata->cur_gpx->cache, |
357 |
|
pos, &result, &distance); |
358 |
|
} |
359 |
|
#endif |
360 |
|
|
361 |
|
return result; |
362 |
|
} |
363 |
|
|
364 |
|
/* translate between osm-gps-map positions and gpxview ones */ |
365 |
|
pos_t coord2pos(coord_t coo) { |
366 |
|
pos_t pos; |
367 |
|
pos.lat = RAD2DEG(coo.rlat); |
368 |
|
pos.lon = RAD2DEG(coo.rlon); |
369 |
|
return pos; |
370 |
|
} |
371 |
|
|
372 |
|
static int dist2pixel(map_context_t *context, float km, float lat) { |
373 |
|
int zoom; |
374 |
|
g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); |
375 |
|
|
376 |
|
/* world at zoom 1 == 512 pixels */ |
377 |
|
float m_per_pix = |
378 |
|
cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom)); |
379 |
|
|
380 |
|
return 1000.0*km/m_per_pix; |
381 |
|
} |
382 |
|
|
383 |
|
#define CLICK_FUZZ (16) |
384 |
|
|
385 |
|
static gboolean |
386 |
|
on_map_button_press_event(GtkWidget *widget, |
387 |
|
GdkEventButton *event, map_context_t *context) { |
388 |
|
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
389 |
|
|
390 |
|
/* got a press event without release event? eat it! */ |
391 |
|
if(context->press_on != NULL) { |
392 |
|
printf("PRESS: already\n"); |
393 |
|
return TRUE; |
394 |
|
} |
395 |
|
|
396 |
|
pos_t pos = |
397 |
|
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
398 |
|
|
399 |
|
cache_t *nearest = map_closest(context, &pos); |
400 |
|
if(nearest) { |
401 |
|
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
402 |
|
if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
403 |
|
context->press_on = nearest; |
404 |
|
} |
405 |
|
|
406 |
|
return FALSE; |
407 |
|
} |
408 |
|
|
409 |
|
static gboolean |
410 |
|
on_map_button_release_event(GtkWidget *widget, |
411 |
|
GdkEventButton *event, map_context_t *context) { |
412 |
|
if(context->press_on) { |
413 |
|
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
414 |
|
|
415 |
|
pos_t pos = |
416 |
|
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
417 |
|
|
418 |
|
cache_t *nearest = map_closest(context, &pos); |
419 |
|
if(nearest && nearest == context->press_on) { |
420 |
|
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
421 |
|
if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
422 |
|
cache_popup(context, nearest); |
423 |
|
} |
424 |
|
context->press_on = NULL; |
425 |
|
} |
426 |
|
|
427 |
|
return FALSE; |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
#if MAEMO_VERSION_MAJOR == 5 |
#if MAEMO_VERSION_MAJOR == 5 |
432 |
static void on_window_destroy(GtkWidget *widget, map_context_t *context) { |
static void on_window_destroy(GtkWidget *widget, map_context_t *context) { |
433 |
printf("destroy map view\n"); |
printf("destroy map view\n"); |
444 |
map_context_t *context = g_new0(map_context_t, 1); |
map_context_t *context = g_new0(map_context_t, 1); |
445 |
context->appdata = appdata; |
context->appdata = appdata; |
446 |
|
|
447 |
|
GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
448 |
|
|
449 |
|
char *path = g_strdup_printf("%s/map/", appdata->image_path); |
450 |
|
const char *proxy = get_proxy_uri(appdata); |
451 |
|
|
452 |
|
context->widget = g_object_new(OSM_TYPE_GPS_MAP, |
453 |
|
"repo-uri", MAP_SOURCE_OPENSTREETMAP, |
454 |
|
"tile-cache", path, |
455 |
|
proxy?"proxy-uri":NULL, proxy, |
456 |
|
NULL); |
457 |
|
|
458 |
|
g_free(path); |
459 |
|
|
460 |
|
char *name = NULL; |
461 |
|
#ifdef USE_MAEMO |
462 |
|
if(!appdata->cur_gpx) { |
463 |
|
#endif |
464 |
|
/* draw all geocaches */ |
465 |
|
gpx_t *gpx = appdata->gpx; |
466 |
|
while(gpx) { |
467 |
|
map_draw_cachelist(context->widget, gpx->cache); |
468 |
|
gpx = gpx->next; |
469 |
|
} |
470 |
|
name = g_strdup(_("all geocaches")); |
471 |
|
#ifdef USE_MAEMO |
472 |
|
} else { |
473 |
|
map_draw_cachelist(context->widget, appdata->cur_gpx->cache); |
474 |
|
name = g_strdup(appdata->cur_gpx->name); |
475 |
|
} |
476 |
|
#endif |
477 |
|
|
478 |
|
char *title = g_strdup_printf(_("Map - %s"), name); |
479 |
|
g_free(name); |
480 |
|
|
481 |
#if MAEMO_VERSION_MAJOR == 5 |
#if MAEMO_VERSION_MAJOR == 5 |
482 |
GtkWidget *window = hildon_stackable_window_new(); |
GtkWidget *window = hildon_stackable_window_new(); |
483 |
gtk_window_set_title(GTK_WINDOW(window), _("Map")); |
gtk_window_set_title(GTK_WINDOW(window), title); |
484 |
#else |
#else |
485 |
GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Map"), |
GtkWidget *dialog = gtk_dialog_new_with_buttons(title, |
486 |
GTK_WINDOW(appdata->window), |
GTK_WINDOW(appdata->window), |
487 |
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
488 |
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
495 |
#endif |
#endif |
496 |
#endif |
#endif |
497 |
|
|
498 |
GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
g_free(title); |
|
|
|
|
char *path = g_strdup_printf("%s/map/", appdata->image_path); |
|
|
const char *proxy = get_proxy_uri(appdata); |
|
|
|
|
|
context->widget = g_object_new(OSM_TYPE_GPS_MAP, |
|
|
"repo-uri", MAP_SOURCE_OPENSTREETMAP, |
|
|
"tile-cache", path, |
|
|
proxy?"proxy-uri":NULL, proxy, |
|
|
NULL); |
|
|
|
|
|
g_free(path); |
|
|
|
|
|
/* draw all geocaches */ |
|
|
gpx_t *gpx = appdata->gpx; |
|
|
while(gpx) { |
|
|
map_draw_cachelist(context->widget, gpx->cache); |
|
|
gpx = gpx->next; |
|
|
} |
|
499 |
|
|
500 |
g_signal_connect(G_OBJECT(context->widget), "configure-event", |
g_signal_connect(G_OBJECT(context->widget), "configure-event", |
501 |
G_CALLBACK(on_map_configure), context); |
G_CALLBACK(on_map_configure), context); |
502 |
#if 0 |
|
503 |
|
g_signal_connect(G_OBJECT(context->widget), "button-press-event", |
504 |
|
G_CALLBACK(on_map_button_press_event), context); |
505 |
|
|
506 |
g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
507 |
G_CALLBACK(on_map_button_release_event), context); |
G_CALLBACK(on_map_button_release_event), context); |
|
#endif |
|
508 |
|
|
509 |
gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget); |
gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget); |
510 |
/* zoom button box */ |
/* zoom button box */ |