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 |
242 |
shutdown_loop(context); |
shutdown_loop(context); |
243 |
} |
} |
244 |
|
|
245 |
void cache_popup(appdata_t *appdata, cache_t *cache) { |
void cache_popup(map_context_t *mcontext, cache_t *cache) { |
246 |
popup_context_t context; |
popup_context_t pcontext; |
247 |
context.appdata = appdata; |
pcontext.appdata = mcontext->appdata; |
248 |
|
|
249 |
context.window = gtk_window_new(GTK_WINDOW_POPUP); |
pcontext.window = gtk_window_new(GTK_WINDOW_POPUP); |
250 |
gtk_widget_realize(context.window); |
gtk_widget_realize(pcontext.window); |
251 |
gtk_window_set_default_size(GTK_WINDOW(context.window), |
gtk_window_set_default_size(GTK_WINDOW(pcontext.window), |
252 |
POPUP_WIDTH, POPUP_HEIGHT); |
POPUP_WIDTH, POPUP_HEIGHT); |
253 |
gtk_window_resize(GTK_WINDOW(context.window), |
gtk_window_resize(GTK_WINDOW(pcontext.window), |
254 |
POPUP_WIDTH, POPUP_HEIGHT); |
POPUP_WIDTH, POPUP_HEIGHT); |
255 |
// gtk_window_set_resizable(GTK_WINDOW(context.window), FALSE); |
// gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE); |
256 |
gtk_window_set_transient_for(GTK_WINDOW(context.window), |
gtk_window_set_transient_for(GTK_WINDOW(pcontext.window), |
257 |
GTK_WINDOW(appdata->window)); |
GTK_WINDOW(mcontext->appdata->window)); |
258 |
gtk_window_set_keep_above(GTK_WINDOW(context.window), TRUE); |
gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE); |
259 |
gtk_window_set_destroy_with_parent(GTK_WINDOW(context.window), TRUE); |
gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE); |
260 |
gtk_window_set_gravity(GTK_WINDOW(context.window), GDK_GRAVITY_STATIC); |
gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC); |
261 |
gtk_window_set_modal(GTK_WINDOW(context.window), TRUE); |
gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE); |
262 |
|
|
263 |
/* connect events */ |
/* connect events */ |
264 |
g_signal_connect(G_OBJECT(context.window), "button-press-event", |
g_signal_connect(G_OBJECT(pcontext.window), "button-press-event", |
265 |
G_CALLBACK(on_button_press_event), &context); |
G_CALLBACK(on_button_press_event), &pcontext); |
266 |
g_signal_connect(G_OBJECT(context.window), "button-release-event", |
g_signal_connect(G_OBJECT(pcontext.window), "button-release-event", |
267 |
G_CALLBACK(on_button_release_event), &context); |
G_CALLBACK(on_button_release_event), &pcontext); |
268 |
g_signal_connect(G_OBJECT(context.window), "delete-event", |
g_signal_connect(G_OBJECT(pcontext.window), "delete-event", |
269 |
G_CALLBACK(run_delete_handler), &context); |
G_CALLBACK(run_delete_handler), &pcontext); |
270 |
g_signal_connect(G_OBJECT(context.window), "destroy", |
g_signal_connect(G_OBJECT(pcontext.window), "destroy", |
271 |
G_CALLBACK(run_destroy_handler), &context); |
G_CALLBACK(run_destroy_handler), &pcontext); |
272 |
g_signal_connect(G_OBJECT(context.window), "unmap", |
g_signal_connect(G_OBJECT(pcontext.window), "unmap", |
273 |
G_CALLBACK(run_unmap_handler), &context); |
G_CALLBACK(run_unmap_handler), &pcontext); |
274 |
|
|
275 |
gdk_pointer_grab(context.window->window, TRUE, |
gdk_pointer_grab(pcontext.window->window, TRUE, |
276 |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
277 |
NULL, NULL, GDK_CURRENT_TIME); |
NULL, NULL, GDK_CURRENT_TIME); |
278 |
gtk_grab_add(context.window); |
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 |
// gint x, y; |
// gtk_window_move(GTK_WINDOW(pcontext.window), |
|
// gdk_window_get_origin(button->window, &x, &y); |
|
|
|
|
|
// gtk_window_move(GTK_WINDOW(context.window), |
|
294 |
// x + button->allocation.x, |
// x + button->allocation.x, |
295 |
// y + button->allocation.y - HEIGHT); |
// y + button->allocation.y - HEIGHT); |
296 |
|
|
297 |
|
|
298 |
gtk_window_move(GTK_WINDOW(context.window), |
gtk_window_move(GTK_WINDOW(pcontext.window), |
299 |
100, |
100, |
300 |
100); |
100); |
301 |
|
|
304 |
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); |
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); |
305 |
|
|
306 |
gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name)); |
gtk_container_add(GTK_CONTAINER(frame), gtk_label_new(cache->name)); |
307 |
gtk_container_add(GTK_CONTAINER(context.window), frame); |
gtk_container_add(GTK_CONTAINER(pcontext.window), frame); |
308 |
|
|
309 |
gtk_widget_show_all(context.window); |
gtk_widget_show_all(pcontext.window); |
310 |
|
|
311 |
/* handle this popup until it's gone */ |
/* handle this popup until it's gone */ |
312 |
|
|
313 |
context.loop = g_main_loop_new(NULL, FALSE); |
pcontext.loop = g_main_loop_new(NULL, FALSE); |
314 |
|
|
315 |
GDK_THREADS_LEAVE(); |
GDK_THREADS_LEAVE(); |
316 |
g_main_loop_run(context.loop); |
g_main_loop_run(pcontext.loop); |
317 |
GDK_THREADS_ENTER(); |
GDK_THREADS_ENTER(); |
318 |
|
|
319 |
g_main_loop_unref(context.loop); |
g_main_loop_unref(pcontext.loop); |
320 |
|
|
321 |
printf("cache popup removed\n"); |
printf("cache popup removed\n"); |
322 |
} |
} |
323 |
|
|
|
#define RAD2DEG(a) (((a)*180.0)/M_PI) |
|
|
|
|
324 |
static void |
static void |
325 |
map_cachelist_nearest(cache_t *cache, pos_t *pos, |
map_cachelist_nearest(cache_t *cache, pos_t *pos, |
326 |
cache_t **result, float *distance) { |
cache_t **result, float *distance) { |
369 |
return pos; |
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 (10) |
384 |
|
|
385 |
static gboolean |
static gboolean |
386 |
on_map_button_press_event(GtkWidget *widget, |
on_map_button_press_event(GtkWidget *widget, |
387 |
GdkEventButton *event, map_context_t *context) { |
GdkEventButton *event, map_context_t *context) { |
388 |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
389 |
|
|
390 |
pos_t pos = |
pos_t pos = |
391 |
coord2pos(osm_gps_map_get_co_ordinates(map, (int)event->x, (int)event->y)); |
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
392 |
|
|
393 |
printf("clicked at %f/%f\n", pos.lat, pos.lon); |
cache_t *nearest = map_closest(context, &pos); |
394 |
|
if(nearest) { |
395 |
|
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
396 |
|
if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) { |
397 |
|
context->press_on = nearest; |
398 |
|
return TRUE; |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
|
context->press_on = NULL; |
403 |
return FALSE; |
return FALSE; |
404 |
} |
} |
405 |
|
|
406 |
static gboolean |
static gboolean |
407 |
on_map_button_release_event(GtkWidget *widget, |
on_map_button_release_event(GtkWidget *widget, |
408 |
GdkEventButton *event, map_context_t *context) { |
GdkEventButton *event, map_context_t *context) { |
409 |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
if(context->press_on) { |
410 |
|
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
411 |
|
|
412 |
pos_t pos = |
pos_t pos = |
413 |
coord2pos(osm_gps_map_get_co_ordinates(map, (int)event->x, (int)event->y)); |
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
|
|
|
|
printf("released at %f/%f\n", pos.lat, pos.lon); |
|
414 |
|
|
415 |
/* return true if we clicked a cache */ |
cache_t *nearest = map_closest(context, &pos); |
416 |
/* ... */ |
if(nearest && nearest == context->press_on) { |
417 |
|
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
418 |
cache_t *nearest = map_closest(context, &pos); |
if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) { |
419 |
|
|
420 |
if(nearest) { |
cache_popup(context, nearest); |
421 |
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
|
422 |
|
context->press_on = NULL; |
423 |
printf("nearest = %s, distance = %fkm\n", nearest->name, dist); |
return TRUE; |
424 |
|
} |
425 |
cache_popup(context->appdata, nearest); |
} |
|
|
|
|
return TRUE; |
|
426 |
} |
} |
427 |
|
|
428 |
|
context->press_on = NULL; |
429 |
return FALSE; |
return FALSE; |
430 |
} |
} |
431 |
|
|