1 |
/* |
/* |
2 |
* Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
* Copyright (C) 2008-2009 Till Harbaum <till@harbaum.org>. |
3 |
* |
* |
4 |
* This file is part of GPXView. |
* This file is part of GPXView. |
5 |
* |
* |
17 |
* along with GPXView. If not, see <http://www.gnu.org/licenses/>. |
* along with GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 |
*/ |
*/ |
19 |
|
|
20 |
|
/* |
21 |
|
* http://topo.geofabrik.de/relief/${z}/${x}/${y}.png 8-15 |
22 |
|
* http://topo.geofabrik.de/trail/${z}/${x}/${y}.png 8-15 |
23 |
|
*/ |
24 |
|
|
25 |
#include "gpxview.h" |
#include "gpxview.h" |
26 |
|
#include "converter.h" |
27 |
#include <math.h> // for isnan |
#include <math.h> // for isnan |
28 |
|
|
29 |
#ifdef ENABLE_OSM_GPS_MAP |
#ifdef ENABLE_OSM_GPS_MAP |
30 |
#include "osm-gps-map.h" |
#include "osm-gps-map.h" |
31 |
|
#include "osm-gps-map-osd-classic.h" |
32 |
#endif |
#endif |
33 |
|
|
34 |
/* equatorial radius in meters */ |
#if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) |
35 |
#define EQ_RADIUS (6378137.0) |
#include <gdk/gdkx.h> |
36 |
|
#include <X11/Xatom.h> |
|
#define RAD2DEG(a) (((a)*180.0)/M_PI) |
|
|
#define DEG2RAD(a) (((a)*M_PI)/180.0) |
|
|
|
|
|
typedef struct { |
|
|
appdata_t *appdata; |
|
|
GtkWidget *widget; |
|
|
GtkWidget *zoomin, *zoomout, *gps; |
|
|
gint handler_id; |
|
|
cache_t *press_on; |
|
|
#if MAEMO_VERSION_MAJOR == 5 |
|
|
GtkWidget *old_view; |
|
37 |
#endif |
#endif |
38 |
} map_context_t; |
|
39 |
|
/* default values */ |
40 |
|
#define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENCYCLEMAP |
41 |
|
#define GPS_DEFAULT_ZOOM 13 |
42 |
|
|
43 |
#define PROXY_KEY "/system/http_proxy/" |
#define PROXY_KEY "/system/http_proxy/" |
44 |
|
|
78 |
return NULL; |
return NULL; |
79 |
} |
} |
80 |
|
|
81 |
static void map_zoom(map_context_t *context, int step) { |
static void |
82 |
int zoom; |
cb_map_gps(osd_button_t but, map_context_t *context) { |
|
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
|
|
g_object_get(map, "zoom", &zoom, NULL); |
|
|
zoom = osm_gps_map_set_zoom(map, zoom+step); |
|
|
|
|
|
/* enable/disable zoom buttons as required */ |
|
|
gtk_widget_set_sensitive(context->zoomin, zoom<17); |
|
|
gtk_widget_set_sensitive(context->zoomout, zoom>1); |
|
|
} |
|
|
|
|
|
static gboolean |
|
|
cb_map_zoomin(GtkButton *button, map_context_t *context) { |
|
|
map_zoom(context, +1); |
|
|
return FALSE; |
|
|
} |
|
|
|
|
|
static gboolean |
|
|
cb_map_zoomout(GtkButton *button, map_context_t *context) { |
|
|
map_zoom(context, -1); |
|
|
return FALSE; |
|
|
} |
|
83 |
|
|
84 |
static gboolean |
if(but == OSD_GPS) { |
85 |
cb_map_gps(GtkButton *button, map_context_t *context) { |
pos_t *refpos = get_pos(context->appdata); |
86 |
pos_t *refpos = get_pos(context->appdata); |
if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { |
87 |
if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { |
gint zoom; |
88 |
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); |
89 |
refpos->lat, refpos->lon, 14); |
if(zoom < 10) |
90 |
} else { |
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
91 |
/* no coordinates given: display the entire world */ |
refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM); |
92 |
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
else |
93 |
0.0, 0.0, 1); |
osm_gps_map_set_center(OSM_GPS_MAP(context->widget), |
94 |
|
refpos->lat, refpos->lon); |
95 |
|
|
96 |
|
/* re-enable centering */ |
97 |
|
g_object_set(context->widget, "auto-center", TRUE, NULL); |
98 |
|
} else { |
99 |
|
/* no coordinates given: display the entire world */ |
100 |
|
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
101 |
|
0.0, 0.0, 1); |
102 |
|
} |
103 |
} |
} |
|
|
|
|
return FALSE; |
|
104 |
} |
} |
105 |
|
|
106 |
static GtkWidget |
static int dist2pixel(map_context_t *context, float km, float lat) { |
107 |
*map_add_button(const gchar *icon, GCallback cb, gpointer data, |
return 1000.0*km/osm_gps_map_get_scale(OSM_GPS_MAP(context->widget)); |
|
char *tooltip) { |
|
|
GtkWidget *button = gtk_button_new(); |
|
|
gtk_button_set_image(GTK_BUTTON(button), |
|
|
gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON)); |
|
|
g_signal_connect(button, "clicked", cb, data); |
|
|
#ifndef USE_MAEMO |
|
|
gtk_widget_set_tooltip_text(button, tooltip); |
|
|
#endif |
|
|
return button; |
|
108 |
} |
} |
109 |
|
|
110 |
static gboolean map_gps_update(gpointer data) { |
static gboolean map_gps_update(gpointer data) { |
111 |
map_context_t *context = (map_context_t*)data; |
map_context_t *context = (map_context_t*)data; |
112 |
|
static gboolean goto_is_enabled = FALSE; |
113 |
|
|
114 |
|
/* get reference position ... */ |
115 |
pos_t *refpos = get_pos(context->appdata); |
pos_t *refpos = get_pos(context->appdata); |
116 |
gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); |
gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon); |
117 |
|
|
118 |
/* get reference position and go there */ |
/* ... and enable "goto" button if it's valid */ |
119 |
gtk_widget_set_sensitive(context->gps, ok); |
if(ok != goto_is_enabled) { |
120 |
|
osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget), |
121 |
|
OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context); |
122 |
|
goto_is_enabled = ok; |
123 |
|
} |
124 |
|
|
125 |
|
if(ok) { |
126 |
|
float heading = NAN; |
127 |
|
int radius = 0; |
128 |
|
|
129 |
|
if(context->appdata->use_gps) { |
130 |
|
heading = gps_get_heading(context->appdata); |
131 |
|
|
132 |
|
/* get error */ |
133 |
|
float eph = gps_get_eph(context->appdata); |
134 |
|
if(!isnan(eph)) |
135 |
|
radius = dist2pixel(context, eph/1000, refpos->lat); |
136 |
|
} |
137 |
|
|
138 |
|
/* TODO: in order to save energy: only draw if state actually changed */ |
139 |
|
|
140 |
|
g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL); |
141 |
|
osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget), |
142 |
|
refpos->lat, refpos->lon, heading); |
143 |
|
} else |
144 |
|
osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget)); |
145 |
|
|
146 |
return TRUE; |
return TRUE; |
147 |
} |
} |
148 |
|
|
149 |
static gboolean on_map_configure(GtkWidget *widget, |
static void map_draw_cache(GtkWidget *map, cache_t *cache) { |
150 |
GdkEventConfigure *event, |
GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); |
|
map_context_t *context) { |
|
151 |
|
|
152 |
cb_map_gps(NULL, context); |
/* check if there's also an overwritten coordinate */ |
153 |
|
if(cache->notes && cache->notes->override) { |
154 |
|
GdkPixbuf *over = icon_get(ICON_MISC, 1); |
155 |
|
|
156 |
return FALSE; |
osm_gps_map_add_image(OSM_GPS_MAP(map), |
157 |
|
cache->notes->pos.lat, cache->notes->pos.lon, icon); |
158 |
|
|
159 |
|
osm_gps_map_add_image(OSM_GPS_MAP(map), |
160 |
|
cache->notes->pos.lat, cache->notes->pos.lon, over); |
161 |
|
} else { |
162 |
|
if(!isnan(cache->pos.lat) && !isnan(cache->pos.lon)) |
163 |
|
osm_gps_map_add_image(OSM_GPS_MAP(map), |
164 |
|
cache->pos.lat, cache->pos.lon, icon); |
165 |
|
} |
166 |
} |
} |
167 |
|
|
168 |
static void map_draw_cachelist(GtkWidget *map, cache_t *cache) { |
static void map_draw_gpx(appdata_t *appdata, GtkWidget *map, gpx_t *gpx) { |
169 |
while(cache) { |
if(!gpx->notes_loaded) { |
170 |
GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); |
notes_load_all(appdata, gpx); |
171 |
|
gpx->notes_loaded = TRUE; |
172 |
osm_gps_map_add_image(OSM_GPS_MAP(map), |
} |
|
cache->pos.lat, cache->pos.lon, icon); |
|
173 |
|
|
174 |
|
cache_t *cache = gpx->cache; |
175 |
|
while(cache) { |
176 |
|
map_draw_cache(map, cache); |
177 |
cache = cache->next; |
cache = cache->next; |
178 |
} |
} |
179 |
} |
} |
180 |
|
|
181 |
/* draw a nice popup */ |
/* draw geocaches and set window title */ |
182 |
typedef struct { |
static void map_setup(map_context_t *context) { |
183 |
appdata_t *appdata; |
char *name = NULL; |
|
GtkWidget *window; |
|
|
GMainLoop *loop; |
|
|
} popup_context_t; |
|
184 |
|
|
185 |
|
if(!context->appdata->cur_gpx && !context->appdata->cur_cache) { |
186 |
|
if(context->state != MAP_ALL) { |
187 |
|
printf("map_setup(ALL)\n"); |
188 |
|
|
189 |
|
#ifdef OSD_NAV |
190 |
|
/* no navigation in this mode */ |
191 |
|
osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget)); |
192 |
|
#endif |
193 |
|
|
194 |
|
/* clear all existing ccahe images */ |
195 |
|
osm_gps_map_clear_images (OSM_GPS_MAP(context->widget)); |
196 |
|
|
197 |
|
/* draw all geocaches */ |
198 |
|
gpx_t *gpx = context->appdata->gpx; |
199 |
|
while(gpx) { |
200 |
|
map_draw_gpx(context->appdata, context->widget, gpx); |
201 |
|
gpx = gpx->next; |
202 |
|
} |
203 |
|
name = g_strdup(_("all")); |
204 |
|
context->state = MAP_ALL; |
205 |
|
} |
206 |
|
} else if(!context->appdata->cur_cache) { |
207 |
|
if(context->state != MAP_GPX) { |
208 |
|
printf("map_setup(GPX)\n"); |
209 |
|
|
210 |
|
#ifdef OSD_NAV |
211 |
|
/* no navigation in this mode */ |
212 |
|
osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget)); |
213 |
|
#endif |
214 |
|
|
215 |
|
/* clear all existing ccahe images */ |
216 |
|
osm_gps_map_clear_images (OSM_GPS_MAP(context->widget)); |
217 |
|
|
218 |
|
map_draw_gpx(context->appdata, context->widget, |
219 |
|
context->appdata->cur_gpx); |
220 |
|
name = g_strdup(context->appdata->cur_gpx->name); |
221 |
|
context->state = MAP_GPX; |
222 |
|
} |
223 |
|
} else { |
224 |
|
cache_t *cache = context->appdata->cur_cache; |
225 |
|
|
226 |
|
printf("map_setp(CACHE)\n"); |
227 |
|
|
228 |
|
/* no balloons in this mode */ |
229 |
|
context->balloon = NULL; |
230 |
|
osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(context->widget)); |
231 |
|
|
232 |
|
/* clear all existing ccahe images */ |
233 |
|
osm_gps_map_clear_images (OSM_GPS_MAP(context->widget)); |
234 |
|
|
235 |
|
map_draw_cache(context->widget, cache); |
236 |
|
name = g_strdup(cache->name); |
237 |
|
context->state = MAP_CACHE; |
238 |
|
|
239 |
|
/* navigation in this mode! */ |
240 |
|
pos_t cpos = gpx_cache_pos(cache); |
241 |
|
|
242 |
#ifndef USE_HILDON |
#ifdef OSD_NAV |
243 |
#define POPUP_WIDTH 300 |
osm_gps_map_osd_draw_nav (OSM_GPS_MAP(context->widget), |
244 |
#define POPUP_HEIGHT 100 |
context->appdata->imperial, |
245 |
|
cpos.lat, cpos.lon, cache->name); |
246 |
#else |
#else |
247 |
#define POPUP_WIDTH 600 |
#warning OSD_NAV not defined! |
|
#define POPUP_HEIGHT 200 |
|
248 |
#endif |
#endif |
|
|
|
|
static gboolean |
|
|
pointer_in_window(GtkWidget *widget, gint x_root, gint y_root) { |
|
|
if(GTK_WIDGET_MAPPED(gtk_widget_get_toplevel(widget))) { |
|
|
gint window_x, window_y; |
|
|
|
|
|
gdk_window_get_position(gtk_widget_get_toplevel(widget)->window, |
|
|
&window_x, &window_y); |
|
|
|
|
|
if(x_root >= window_x && x_root < window_x + widget->allocation.width && |
|
|
y_root >= window_y && y_root < window_y + widget->allocation.height) |
|
|
return TRUE; |
|
249 |
} |
} |
|
|
|
|
return FALSE; |
|
|
} |
|
250 |
|
|
251 |
static gboolean |
if(name) { |
252 |
on_button_press_event(GtkWidget *widget, |
char *title = g_strdup_printf(_("Map - %s"), name); |
253 |
GdkEventButton *event, popup_context_t *context) { |
g_free(name); |
|
gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
|
254 |
|
|
255 |
printf("overlay button press (in = %d)\n", in); |
gtk_window_set_title(GTK_WINDOW(context->window), title); |
256 |
return !in; |
|
257 |
|
g_free(title); |
258 |
|
} else |
259 |
|
printf("map_setup(keep)\n"); |
260 |
} |
} |
261 |
|
|
262 |
static gboolean |
static gboolean on_map_configure(GtkWidget *widget, |
263 |
on_button_release_event(GtkWidget *widget, |
GdkEventConfigure *event, |
264 |
GdkEventButton *event, popup_context_t *context) { |
map_context_t *context) { |
|
gboolean in = pointer_in_window(widget, event->x_root, event->y_root); |
|
265 |
|
|
266 |
printf("overlay button release (in = %d)\n", in); |
/* for some reason there's a configure event with 1/1 */ |
267 |
|
/* on diablo. We just ignore this! */ |
268 |
|
|
269 |
if(!in) { |
printf("on_map_configure %d %d\n", |
270 |
printf("destroying popup\n"); |
widget->allocation.width, |
271 |
gtk_widget_destroy(gtk_widget_get_toplevel(widget)); |
widget->allocation.height); |
272 |
|
|
273 |
|
if(!context->map_complete && |
274 |
|
(widget->allocation.width > 100) && |
275 |
|
(widget->allocation.height > 100)) { |
276 |
|
|
277 |
|
/* setup cache state */ |
278 |
|
map_setup(context); |
279 |
|
|
280 |
|
/* set default values if they are invalid */ |
281 |
|
if(!context->appdata->map.zoom || |
282 |
|
isnan(context->appdata->map.pos.lat) || |
283 |
|
isnan(context->appdata->map.pos.lon)) { |
284 |
|
printf("no valid map position found\n"); |
285 |
|
|
286 |
|
pos_t *refpos = get_pos(context->appdata); |
287 |
|
if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) { |
288 |
|
printf("use refpos\n"); |
289 |
|
|
290 |
|
/* use gps position if present */ |
291 |
|
context->appdata->map.pos = *refpos; |
292 |
|
context->appdata->map.zoom = GPS_DEFAULT_ZOOM; |
293 |
|
} else { |
294 |
|
printf("use zero pos\n"); |
295 |
|
|
296 |
|
/* use world map otherwise */ |
297 |
|
context->appdata->map.pos.lat = 0.0; |
298 |
|
context->appdata->map.pos.lon = 0.0; |
299 |
|
context->appdata->map.zoom = 1; |
300 |
|
} |
301 |
|
} |
302 |
|
|
303 |
|
/* jump to initial position */ |
304 |
|
printf("osm_gps_map_set_mapcenter(%f,%f,%d)\n", |
305 |
|
context->appdata->map.pos.lat, |
306 |
|
context->appdata->map.pos.lon, |
307 |
|
context->appdata->map.zoom); |
308 |
|
|
309 |
|
osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget), |
310 |
|
context->appdata->map.pos.lat, |
311 |
|
context->appdata->map.pos.lon, |
312 |
|
context->appdata->map.zoom); |
313 |
|
context->map_complete = TRUE; |
314 |
} |
} |
315 |
|
|
316 |
return !in; |
return FALSE; |
|
} |
|
|
|
|
|
static void |
|
|
shutdown_loop(popup_context_t *context) { |
|
|
if(g_main_loop_is_running(context->loop)) |
|
|
g_main_loop_quit(context->loop); |
|
|
} |
|
|
|
|
|
static gint |
|
|
run_delete_handler(GtkWindow *window, GdkEventAny *event, |
|
|
popup_context_t *context) { |
|
|
shutdown_loop(context); |
|
|
return TRUE; /* Do not destroy */ |
|
|
} |
|
|
|
|
|
static void |
|
|
run_destroy_handler(GtkWindow *window, popup_context_t *context) { |
|
|
/* shutdown_loop will be called by run_unmap_handler */ |
|
|
printf("popup destroyed\n"); |
|
|
} |
|
|
|
|
|
static void |
|
|
run_unmap_handler(GtkWindow *window, popup_context_t *context) { |
|
|
shutdown_loop(context); |
|
|
} |
|
|
|
|
|
/* draw shape */ |
|
|
#define ARROW_BORDER 20 |
|
|
#define CORNER_RADIUS 10 |
|
|
|
|
|
static void popup_window_shape(GtkWidget *window, int tip_x, int tip_y) { |
|
|
GdkBitmap *mask = gdk_pixmap_new(NULL, POPUP_WIDTH, POPUP_HEIGHT, 1); |
|
|
|
|
|
GdkGC *gc = gdk_gc_new(mask); |
|
|
GdkColormap *colormap; |
|
|
GdkColor black; |
|
|
GdkColor white; |
|
|
|
|
|
/* get black/white color values */ |
|
|
colormap = gdk_colormap_get_system(); |
|
|
gdk_color_black(colormap, &black); |
|
|
gdk_color_white(colormap, &white); |
|
|
|
|
|
/* erase */ |
|
|
gdk_gc_set_foreground(gc, &black); |
|
|
gdk_gc_set_background(gc, &white); |
|
|
|
|
|
/* erase background */ |
|
|
gdk_draw_rectangle(mask, gc, TRUE, 0, 0, POPUP_WIDTH, POPUP_HEIGHT); |
|
|
|
|
|
gdk_gc_set_foreground(gc, &white); |
|
|
gdk_gc_set_background(gc, &black); |
|
|
|
|
|
gdk_draw_rectangle(mask, gc, TRUE, |
|
|
0, ARROW_BORDER + CORNER_RADIUS, |
|
|
POPUP_WIDTH, |
|
|
POPUP_HEIGHT - 2*CORNER_RADIUS - 2*ARROW_BORDER); |
|
|
|
|
|
gdk_draw_rectangle(mask, gc, TRUE, |
|
|
CORNER_RADIUS, ARROW_BORDER, |
|
|
POPUP_WIDTH - 2*CORNER_RADIUS, |
|
|
POPUP_HEIGHT - 2*ARROW_BORDER); |
|
|
|
|
|
int off[][2] = { |
|
|
{ CORNER_RADIUS, ARROW_BORDER + CORNER_RADIUS }, |
|
|
{ POPUP_WIDTH - CORNER_RADIUS, |
|
|
ARROW_BORDER + CORNER_RADIUS }, |
|
|
{ POPUP_WIDTH - CORNER_RADIUS, |
|
|
POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER }, |
|
|
{ CORNER_RADIUS, |
|
|
POPUP_HEIGHT - CORNER_RADIUS - ARROW_BORDER}}; |
|
|
|
|
|
int i; |
|
|
for(i=0;i<4;i++) { |
|
|
gdk_draw_arc(mask, gc, TRUE, |
|
|
off[i][0]-CORNER_RADIUS, off[i][1]-CORNER_RADIUS, |
|
|
2*CORNER_RADIUS, 2*CORNER_RADIUS, |
|
|
0, 360*64); |
|
|
} |
|
|
|
|
|
GdkPoint points[3] = { {POPUP_WIDTH*1/3, POPUP_HEIGHT/2}, |
|
|
{POPUP_WIDTH*2/3, POPUP_HEIGHT/2}, |
|
|
{tip_x,tip_y} }; |
|
|
gdk_draw_polygon(mask, gc, TRUE, points, 3); |
|
|
|
|
|
|
|
|
gdk_window_shape_combine_mask(window->window, mask, 0, 0); |
|
|
} |
|
|
|
|
|
void cache_popup(map_context_t *mcontext, cache_t *cache) { |
|
|
popup_context_t pcontext; |
|
|
pcontext.appdata = mcontext->appdata; |
|
|
|
|
|
pcontext.window = gtk_window_new(GTK_WINDOW_POPUP); |
|
|
gtk_widget_realize(pcontext.window); |
|
|
gtk_window_set_default_size(GTK_WINDOW(pcontext.window), |
|
|
POPUP_WIDTH, POPUP_HEIGHT); |
|
|
gtk_window_resize(GTK_WINDOW(pcontext.window), POPUP_WIDTH, POPUP_HEIGHT); |
|
|
// gtk_window_set_resizable(GTK_WINDOW(pcontext.window), FALSE); |
|
|
gtk_window_set_transient_for(GTK_WINDOW(pcontext.window), |
|
|
GTK_WINDOW(mcontext->appdata->window)); |
|
|
gtk_window_set_keep_above(GTK_WINDOW(pcontext.window), TRUE); |
|
|
gtk_window_set_destroy_with_parent(GTK_WINDOW(pcontext.window), TRUE); |
|
|
gtk_window_set_gravity(GTK_WINDOW(pcontext.window), GDK_GRAVITY_STATIC); |
|
|
gtk_window_set_modal(GTK_WINDOW(pcontext.window), TRUE); |
|
|
|
|
|
/* connect events */ |
|
|
g_signal_connect(G_OBJECT(pcontext.window), "button-press-event", |
|
|
G_CALLBACK(on_button_press_event), &pcontext); |
|
|
g_signal_connect(G_OBJECT(pcontext.window), "button-release-event", |
|
|
G_CALLBACK(on_button_release_event), &pcontext); |
|
|
g_signal_connect(G_OBJECT(pcontext.window), "delete-event", |
|
|
G_CALLBACK(run_delete_handler), &pcontext); |
|
|
g_signal_connect(G_OBJECT(pcontext.window), "destroy", |
|
|
G_CALLBACK(run_destroy_handler), &pcontext); |
|
|
g_signal_connect(G_OBJECT(pcontext.window), "unmap", |
|
|
G_CALLBACK(run_unmap_handler), &pcontext); |
|
|
|
|
|
gdk_pointer_grab(pcontext.window->window, TRUE, |
|
|
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, |
|
|
NULL, NULL, GDK_CURRENT_TIME); |
|
|
gtk_grab_add(pcontext.window); |
|
|
|
|
|
/* check whether cache is in upper or lower half of window */ |
|
|
gint x, y, sx, sy; |
|
|
osm_gps_map_geographic_to_screen(OSM_GPS_MAP(mcontext->widget), |
|
|
cache->pos.lat, cache->pos.lon, |
|
|
&sx, &sy); |
|
|
|
|
|
printf("screen pos %d/%d\n", sx, sy); |
|
|
|
|
|
gdk_window_get_origin(mcontext->widget->window, &x, &y); |
|
|
printf("window = %d/%d +%d+%d %d*%d\n", x, y, |
|
|
mcontext->widget->allocation.x, |
|
|
mcontext->widget->allocation.y, |
|
|
mcontext->widget->allocation.width, |
|
|
mcontext->widget->allocation.height |
|
|
); |
|
|
|
|
|
gint ax = 0, ay = 0; |
|
|
if(sx > mcontext->widget->allocation.width/2) |
|
|
ax = POPUP_WIDTH; |
|
|
|
|
|
if(sy > mcontext->widget->allocation.height/2) |
|
|
ay = POPUP_HEIGHT; |
|
|
|
|
|
#if !defined(USE_HILDON) || (MAEMO_VERSION_MAJOR < 5) |
|
|
GdkColor color; |
|
|
gdk_color_parse("darkgray", &color); |
|
|
gtk_widget_modify_bg(GTK_WIDGET(pcontext.window), GTK_STATE_NORMAL, &color); |
|
|
#endif |
|
|
|
|
|
gtk_window_move(GTK_WINDOW(pcontext.window), |
|
|
x + mcontext->widget->allocation.x + sx - ax, |
|
|
y + mcontext->widget->allocation.y + sy - ay); |
|
|
|
|
|
|
|
|
GtkWidget *frame = gtk_frame_new(NULL); |
|
|
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE); |
|
|
gtk_container_set_border_width(GTK_CONTAINER(frame), |
|
|
ARROW_BORDER+CORNER_RADIUS); |
|
|
gtk_container_add(GTK_CONTAINER(frame), |
|
|
gtk_button_new_with_label(cache->name)); |
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(pcontext.window), frame); |
|
|
|
|
|
/* --------- give the window its shape ----------- */ |
|
|
popup_window_shape(pcontext.window, ax, ay); |
|
|
|
|
|
gtk_widget_show_all(pcontext.window); |
|
|
|
|
|
/* handle this popup until it's gone */ |
|
|
|
|
|
pcontext.loop = g_main_loop_new(NULL, FALSE); |
|
|
|
|
|
GDK_THREADS_LEAVE(); |
|
|
g_main_loop_run(pcontext.loop); |
|
|
GDK_THREADS_ENTER(); |
|
|
|
|
|
g_main_loop_unref(pcontext.loop); |
|
|
|
|
|
printf("cache popup removed\n"); |
|
317 |
} |
} |
318 |
|
|
319 |
static void |
static void |
320 |
map_cachelist_nearest(cache_t *cache, pos_t *pos, |
map_cachelist_nearest(cache_t *cache, pos_t *pos, |
321 |
cache_t **result, float *distance) { |
cache_t **result, float *distance) { |
322 |
|
|
323 |
while(cache) { |
while(cache) { |
324 |
|
pos_t cpos = gpx_cache_pos(cache); |
325 |
|
|
326 |
float dist = |
float dist = |
327 |
pow(cache->pos.lat - pos->lat, 2) + |
pow(cpos.lat - pos->lat, 2) + |
328 |
pow(cache->pos.lon - pos->lon, 2); |
pow(cpos.lon - pos->lon, 2); |
329 |
|
|
330 |
if(!(dist > *distance)) { |
if(!(dist > *distance)) { |
331 |
*result = cache; |
*result = cache; |
340 |
cache_t *result = NULL; |
cache_t *result = NULL; |
341 |
float distance = NAN; |
float distance = NAN; |
342 |
|
|
343 |
#ifdef USE_MAEMO |
if(!context->appdata->cur_gpx && !context->appdata->cur_cache) { |
|
if(!context->appdata->cur_gpx) { |
|
|
#endif |
|
344 |
/* search all geocaches */ |
/* search all geocaches */ |
345 |
gpx_t *gpx = context->appdata->gpx; |
gpx_t *gpx = context->appdata->gpx; |
346 |
while(gpx) { |
while(gpx) { |
347 |
map_cachelist_nearest(gpx->cache, pos, &result, &distance); |
map_cachelist_nearest(gpx->cache, pos, &result, &distance); |
348 |
gpx = gpx->next; |
gpx = gpx->next; |
349 |
} |
} |
350 |
#ifdef USE_MAEMO |
} else if(context->appdata->cur_gpx) { |
|
} else { |
|
351 |
map_cachelist_nearest(context->appdata->cur_gpx->cache, |
map_cachelist_nearest(context->appdata->cur_gpx->cache, |
352 |
pos, &result, &distance); |
pos, &result, &distance); |
353 |
} |
} else |
354 |
#endif |
result = context->appdata->cur_gpx->cache; |
355 |
|
|
356 |
return result; |
return result; |
357 |
} |
} |
359 |
/* translate between osm-gps-map positions and gpxview ones */ |
/* translate between osm-gps-map positions and gpxview ones */ |
360 |
pos_t coord2pos(coord_t coo) { |
pos_t coord2pos(coord_t coo) { |
361 |
pos_t pos; |
pos_t pos; |
362 |
pos.lat = RAD2DEG(coo.rlat); |
pos.lat = rad2deg(coo.rlat); |
363 |
pos.lon = RAD2DEG(coo.rlon); |
pos.lon = rad2deg(coo.rlon); |
364 |
return pos; |
return pos; |
365 |
} |
} |
366 |
|
|
367 |
static int dist2pixel(map_context_t *context, float km, float lat) { |
#define CLICK_FUZZ (24) |
|
int zoom; |
|
|
g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL); |
|
|
|
|
|
/* world at zoom 1 == 512 pixels */ |
|
|
float m_per_pix = |
|
|
cos(DEG2RAD(lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom)); |
|
|
|
|
|
return 1000.0*km/m_per_pix; |
|
|
} |
|
|
|
|
|
#define CLICK_FUZZ (16) |
|
368 |
|
|
369 |
static gboolean |
static gboolean |
370 |
on_map_button_press_event(GtkWidget *widget, |
on_map_button_press_event(GtkWidget *widget, |
371 |
GdkEventButton *event, map_context_t *context) { |
GdkEventButton *event, map_context_t *context) { |
372 |
|
|
373 |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
374 |
|
|
375 |
|
/* check if we actually clicked parts of the OSD */ |
376 |
|
if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE) |
377 |
|
return FALSE; |
378 |
|
|
379 |
/* got a press event without release event? eat it! */ |
/* got a press event without release event? eat it! */ |
380 |
if(context->press_on != NULL) { |
if(context->press_on != NULL) { |
381 |
printf("PRESS: already\n"); |
printf("PRESS: already\n"); |
382 |
return TRUE; |
return FALSE; |
383 |
} |
} |
384 |
|
|
385 |
pos_t pos = |
pos_t pos = |
387 |
|
|
388 |
cache_t *nearest = map_closest(context, &pos); |
cache_t *nearest = map_closest(context, &pos); |
389 |
if(nearest) { |
if(nearest) { |
390 |
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
pos_t cpos = gpx_cache_pos(nearest); |
391 |
if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
|
392 |
|
float dist = gpx_pos_get_distance(pos, cpos, FALSE); |
393 |
|
if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ) |
394 |
context->press_on = nearest; |
context->press_on = nearest; |
395 |
} |
} |
396 |
|
|
397 |
return FALSE; |
return FALSE; |
398 |
} |
} |
399 |
|
|
400 |
|
static void |
401 |
|
cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) { |
402 |
|
/* convert the pixbuf into something cairo can handle */ |
403 |
|
|
404 |
|
// Create a new ImageSurface |
405 |
|
cairo_surface_t *image_surface = |
406 |
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
407 |
|
gdk_pixbuf_get_width(buf), |
408 |
|
gdk_pixbuf_get_height(buf)); |
409 |
|
|
410 |
|
// Create the new Context for the ImageSurface |
411 |
|
cairo_t *context = cairo_create(image_surface); |
412 |
|
|
413 |
|
// Draw the image on the new Context |
414 |
|
gdk_cairo_set_source_pixbuf(context, buf, 0.0, 0.0); |
415 |
|
cairo_paint(context); |
416 |
|
|
417 |
|
// now draw this onto the original context |
418 |
|
cairo_set_source_surface(cr, image_surface, x, y); |
419 |
|
|
420 |
|
cairo_paint(cr); |
421 |
|
} |
422 |
|
|
423 |
|
#ifndef BIG_BALLOONS |
424 |
|
#define LINE_SKIP 7 |
425 |
|
#else |
426 |
|
#define LINE_SKIP 12 |
427 |
|
#endif |
428 |
|
|
429 |
|
static void |
430 |
|
balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) { |
431 |
|
printf("balloon event: "); |
432 |
|
|
433 |
|
map_context_t *context = (map_context_t*)data; |
434 |
|
cache_t *cache = context->balloon; |
435 |
|
|
436 |
|
if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW) { |
437 |
|
printf("draw\n"); |
438 |
|
|
439 |
|
#if 0 |
440 |
|
/* draw pink background to check clipping */ |
441 |
|
cairo_rectangle (event->data.draw.cr, |
442 |
|
event->data.draw.rect->x-20, event->data.draw.rect->y-20, |
443 |
|
event->data.draw.rect->w+40, event->data.draw.rect->h+40); |
444 |
|
cairo_set_source_rgba (event->data.draw.cr, 1, 0, 0, 0.3); |
445 |
|
cairo_fill_preserve (event->data.draw.cr); |
446 |
|
cairo_set_line_width (event->data.draw.cr, 0); |
447 |
|
cairo_stroke (event->data.draw.cr); |
448 |
|
#endif |
449 |
|
|
450 |
|
/* leave a little border top and left */ |
451 |
|
gint x = event->data.draw.rect->x, y = event->data.draw.rect->y; |
452 |
|
|
453 |
|
/* draw the cache type icon ... */ |
454 |
|
GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type); |
455 |
|
cairo_draw_pixbuf(event->data.draw.cr, icon, x, y); |
456 |
|
|
457 |
|
if(cache->notes && cache->notes->override) { |
458 |
|
GdkPixbuf *over = icon_get(ICON_MISC, 1); |
459 |
|
cairo_draw_pixbuf(event->data.draw.cr, over, x, y); |
460 |
|
} |
461 |
|
|
462 |
|
/* ... and right of it the waypoint id */ |
463 |
|
cairo_text_extents_t extents; |
464 |
|
|
465 |
|
if(cache->id) { |
466 |
|
cairo_select_font_face (event->data.draw.cr, "Sans", |
467 |
|
CAIRO_FONT_SLANT_NORMAL, |
468 |
|
CAIRO_FONT_WEIGHT_BOLD); |
469 |
|
|
470 |
|
#ifndef BIG_BALLOONS |
471 |
|
cairo_set_font_size (event->data.draw.cr, 20.0); |
472 |
|
#else |
473 |
|
cairo_set_font_size (event->data.draw.cr, 36.0); |
474 |
|
#endif |
475 |
|
|
476 |
|
cairo_text_extents (event->data.draw.cr, cache->id, &extents); |
477 |
|
|
478 |
|
/* display id right of icon vertically centered */ |
479 |
|
x += gdk_pixbuf_get_width(icon) + 5; |
480 |
|
y += (gdk_pixbuf_get_height(icon) + extents.height)/2; |
481 |
|
cairo_move_to (event->data.draw.cr, x, y); |
482 |
|
cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); |
483 |
|
cairo_show_text (event->data.draw.cr, cache->id); |
484 |
|
cairo_stroke (event->data.draw.cr); |
485 |
|
|
486 |
|
y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP; |
487 |
|
} else |
488 |
|
y += gdk_pixbuf_get_height(icon); |
489 |
|
|
490 |
|
/* return to the left border and below icon/text */ |
491 |
|
x = event->data.draw.rect->x; |
492 |
|
|
493 |
|
/* everything from here uses the same font */ |
494 |
|
cairo_select_font_face (event->data.draw.cr, "Sans", CAIRO_FONT_SLANT_NORMAL, |
495 |
|
CAIRO_FONT_WEIGHT_NORMAL); |
496 |
|
#ifndef BIG_BALLOONS |
497 |
|
cairo_set_font_size (event->data.draw.cr, 14.0); |
498 |
|
#else |
499 |
|
cairo_set_font_size (event->data.draw.cr, 22.0); |
500 |
|
#endif |
501 |
|
|
502 |
|
if(cache->name) { |
503 |
|
/* draw cache name */ |
504 |
|
cairo_text_extents (event->data.draw.cr, cache->name, &extents); |
505 |
|
y += extents.height; |
506 |
|
cairo_move_to (event->data.draw.cr, x, y); |
507 |
|
cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); |
508 |
|
cairo_show_text (event->data.draw.cr, cache->name); |
509 |
|
cairo_stroke (event->data.draw.cr); |
510 |
|
|
511 |
|
/* return to the left border and below text */ |
512 |
|
y += LINE_SKIP; |
513 |
|
x = event->data.draw.rect->x; |
514 |
|
} |
515 |
|
|
516 |
|
if(cache->terrain) { |
517 |
|
/* draw cache rating */ |
518 |
|
const char *terrain = "Terrain:"; |
519 |
|
icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2)); |
520 |
|
cairo_text_extents (event->data.draw.cr, _(terrain), &extents); |
521 |
|
y += (gdk_pixbuf_get_height(icon) + extents.height)/2; |
522 |
|
|
523 |
|
/* draw "Terrain:" string */ |
524 |
|
cairo_move_to (event->data.draw.cr, x, y); |
525 |
|
cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); |
526 |
|
cairo_show_text (event->data.draw.cr, _(terrain)); |
527 |
|
cairo_stroke (event->data.draw.cr); |
528 |
|
x += extents.width + 2; |
529 |
|
|
530 |
|
/* draw terrain stars */ |
531 |
|
cairo_draw_pixbuf(event->data.draw.cr, icon, x, y - |
532 |
|
(gdk_pixbuf_get_height(icon) + extents.height)/2); |
533 |
|
|
534 |
|
x += gdk_pixbuf_get_width(icon) + LINE_SKIP; |
535 |
|
y -= (gdk_pixbuf_get_height(icon) + extents.height)/2; |
536 |
|
} |
537 |
|
|
538 |
|
if(cache->difficulty) { |
539 |
|
const char *difficulty = "Difficulty:"; |
540 |
|
cairo_text_extents (event->data.draw.cr, _(difficulty), &extents); |
541 |
|
y += (gdk_pixbuf_get_height(icon) + extents.height)/2; |
542 |
|
|
543 |
|
/* draw "Difficulty:" string */ |
544 |
|
cairo_move_to (event->data.draw.cr, x, y); |
545 |
|
cairo_set_source_rgba (event->data.draw.cr, 0, 0, 0, 1); |
546 |
|
cairo_show_text (event->data.draw.cr, _(difficulty)); |
547 |
|
cairo_stroke (event->data.draw.cr); |
548 |
|
x += extents.width + 2; |
549 |
|
|
550 |
|
icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2)); |
551 |
|
cairo_draw_pixbuf(event->data.draw.cr, icon, x, y - |
552 |
|
(gdk_pixbuf_get_height(icon) + extents.height)/2); |
553 |
|
} |
554 |
|
} else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK) { |
555 |
|
printf("click %s event at %d %d\n", |
556 |
|
event->data.click.down?"down":"up", |
557 |
|
event->data.click.x, event->data.click.y); |
558 |
|
|
559 |
|
/* make the main screen jump to that cache */ |
560 |
|
if(!event->data.click.down) { |
561 |
|
if(context->appdata->cur_cache) { |
562 |
|
printf("ERROR: no current cache should be visible!\n"); |
563 |
|
} else { |
564 |
|
gpx_t *is_in = NULL; |
565 |
|
|
566 |
|
if(!context->appdata->cur_gpx) { |
567 |
|
printf("click while in \"all\" view\n"); |
568 |
|
|
569 |
|
/* we first need to figure out which gpx file this cache */ |
570 |
|
/* is in so we can open it first */ |
571 |
|
gpx_t *gpx = context->appdata->gpx; |
572 |
|
while(gpx && !is_in) { |
573 |
|
cache_t *cur = gpx->cache; |
574 |
|
while(cur && !is_in) { |
575 |
|
if(cur == cache) |
576 |
|
is_in = gpx; |
577 |
|
cur = cur->next; |
578 |
|
} |
579 |
|
gpx = gpx->next; |
580 |
|
} |
581 |
|
|
582 |
|
if(is_in) |
583 |
|
gpxlist_goto_cachelist(context->appdata, is_in); |
584 |
|
|
585 |
|
} else |
586 |
|
/* the simple case: there already is an open gpx file and */ |
587 |
|
/* we just jump into the "cache" view */ |
588 |
|
is_in = context->appdata->cur_gpx; |
589 |
|
|
590 |
|
if(is_in) { |
591 |
|
printf("selecting %s in %s\n", |
592 |
|
cache->id, |
593 |
|
context->appdata->cur_gpx->name); |
594 |
|
|
595 |
|
cachelist_goto_cache(context->appdata, cache); |
596 |
|
|
597 |
|
/* give focus to main screen (important for maemo) */ |
598 |
|
printf("raising main window\n"); |
599 |
|
gtk_window_present(GTK_WINDOW(context->appdata->window)); |
600 |
|
} |
601 |
|
} |
602 |
|
} |
603 |
|
} else if(event->type == OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED) { |
604 |
|
printf("removed\n"); |
605 |
|
context->balloon = NULL; |
606 |
|
} |
607 |
|
} |
608 |
|
|
609 |
static gboolean |
static gboolean |
610 |
on_map_button_release_event(GtkWidget *widget, |
on_map_button_release_event(GtkWidget *widget, |
611 |
GdkEventButton *event, map_context_t *context) { |
GdkEventButton *event, map_context_t *context) { |
612 |
if(context->press_on) { |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
613 |
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
|
614 |
|
/* in "MAP_CACHE" state only one cache is visible */ |
615 |
|
/* and the map is in navigation mode. the balloon is */ |
616 |
|
/* pretty useless there */ |
617 |
|
if(context->press_on && (context->state != MAP_CACHE)) { |
618 |
|
|
619 |
|
coord_t coo; |
620 |
|
coo = osm_gps_map_get_co_ordinates(map, event->x, event->y); |
621 |
|
|
622 |
pos_t pos = |
pos_t pos = |
623 |
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y)); |
624 |
|
|
625 |
cache_t *nearest = map_closest(context, &pos); |
cache_t *nearest = map_closest(context, &pos); |
626 |
if(nearest && nearest == context->press_on) { |
if(nearest && nearest == context->press_on) { |
627 |
float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE); |
pos_t cpos = gpx_cache_pos(nearest); |
628 |
if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) |
|
629 |
cache_popup(context, nearest); |
float dist = gpx_pos_get_distance(pos, cpos, FALSE); |
630 |
|
if(dist2pixel(context, dist, cpos.lat) < CLICK_FUZZ) { |
631 |
|
|
632 |
|
context->balloon = nearest; |
633 |
|
osm_gps_map_osd_draw_balloon(map, cpos.lat, cpos.lon, |
634 |
|
balloon_cb, context); |
635 |
|
} |
636 |
} |
} |
637 |
context->press_on = NULL; |
context->press_on = NULL; |
638 |
|
} else { |
639 |
|
/* save new map position */ |
640 |
|
gfloat lat, lon; |
641 |
|
g_object_get(map, "latitude", &lat, "longitude", &lon, NULL); |
642 |
|
context->appdata->map.pos.lat = lat; |
643 |
|
context->appdata->map.pos.lon = lon; |
644 |
} |
} |
645 |
|
|
646 |
return FALSE; |
return FALSE; |
647 |
} |
} |
648 |
|
|
|
|
|
|
#if MAEMO_VERSION_MAJOR == 5 |
|
649 |
static void on_window_destroy(GtkWidget *widget, map_context_t *context) { |
static void on_window_destroy(GtkWidget *widget, map_context_t *context) { |
650 |
printf("destroy map view\n"); |
appdata_t *appdata = context->appdata; |
651 |
|
|
652 |
|
/* save map parameters */ |
653 |
|
OsmGpsMap *map = OSM_GPS_MAP(context->widget); |
654 |
|
gint zoom; |
655 |
|
g_object_get(map, "zoom", &zoom, NULL); |
656 |
|
context->appdata->map.zoom = zoom; |
657 |
|
|
658 |
|
gfloat lat, lon; |
659 |
|
g_object_get(map, "latitude", &lat, "longitude", &lon, NULL); |
660 |
|
context->appdata->map.pos.lat = lat; |
661 |
|
context->appdata->map.pos.lon = lon; |
662 |
|
|
663 |
|
gint source; |
664 |
|
g_object_get(map, "map-source", &source, NULL); |
665 |
|
context->appdata->map.source = source; |
666 |
|
|
667 |
|
#if MAEMO_VERSION_MAJOR == 5 |
668 |
/* restore cur_view */ |
/* restore cur_view */ |
669 |
context->appdata->cur_view = context->old_view; |
context->appdata->cur_view = context->old_view; |
670 |
|
#endif |
671 |
|
|
672 |
gtk_timeout_remove(context->handler_id); |
gtk_timeout_remove(context->handler_id); |
673 |
|
gps_unregister_callback(appdata, context->cb_id); |
674 |
|
|
675 |
g_free(context); |
g_free(context); |
676 |
|
appdata->map.context = NULL; |
677 |
|
} |
678 |
|
|
679 |
|
#if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__) |
680 |
|
/* get access to zoom buttons */ |
681 |
|
static void |
682 |
|
on_window_realize(GtkWidget *widget, gpointer data) { |
683 |
|
if (widget->window) { |
684 |
|
unsigned char value = 1; |
685 |
|
Atom hildon_zoom_key_atom = |
686 |
|
gdk_x11_get_xatom_by_name("_HILDON_ZOOM_KEY_ATOM"), |
687 |
|
integer_atom = gdk_x11_get_xatom_by_name("INTEGER"); |
688 |
|
Display *dpy = |
689 |
|
GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(widget->window)); |
690 |
|
Window w = GDK_WINDOW_XID(widget->window); |
691 |
|
|
692 |
|
XChangeProperty(dpy, w, hildon_zoom_key_atom, |
693 |
|
integer_atom, 8, PropModeReplace, &value, 1); |
694 |
|
} |
695 |
} |
} |
696 |
#endif |
#endif |
697 |
|
|
698 |
|
/* on maemo a window is either on top or completely invisible. this */ |
699 |
|
/* means that we only need to update the map window if its raised. */ |
700 |
|
/* on ordinary desktops this is different and we always update */ |
701 |
|
|
702 |
|
static gboolean on_focus_in(GtkWidget *widget, GdkEventFocus *event, |
703 |
|
gpointer data) { |
704 |
|
printf("map got focus\n"); |
705 |
|
map_setup((map_context_t*)data); |
706 |
|
return FALSE; |
707 |
|
} |
708 |
|
|
709 |
|
void map_update(appdata_t *appdata) { |
710 |
|
printf("map_update\n"); |
711 |
|
#ifndef USE_MAEMO |
712 |
|
if(appdata->map.context) |
713 |
|
map_setup(appdata->map.context); |
714 |
|
#endif |
715 |
|
} |
716 |
|
|
717 |
|
/* callback called by the gps layer whenever gps state changes */ |
718 |
|
static void |
719 |
|
gps_callback(struct gps_state *state, gpointer data) { |
720 |
|
printf("map: gps callback\n"); |
721 |
|
} |
722 |
|
|
723 |
void map(appdata_t *appdata) { |
void map(appdata_t *appdata) { |
724 |
map_context_t *context = g_new0(map_context_t, 1); |
map_context_t *context = NULL; |
725 |
|
|
726 |
|
/* if the map window already exists, just raise it */ |
727 |
|
if(appdata->map.context) { |
728 |
|
printf("using existing map!\n"); |
729 |
|
gtk_window_present(GTK_WINDOW(appdata->map.context->window)); |
730 |
|
map_setup(appdata->map.context); |
731 |
|
return; |
732 |
|
} |
733 |
|
|
734 |
|
context = appdata->map.context = g_new0(map_context_t, 1); |
735 |
context->appdata = appdata; |
context->appdata = appdata; |
736 |
|
context->map_complete = FALSE; |
737 |
|
context->state = MAP_NONE; |
738 |
|
|
739 |
GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
/* cleanup old (pre 0.8.7) path if it exists */ |
740 |
|
char *old_path = g_strdup_printf("%s/map/", appdata->image_path); |
741 |
|
if(g_file_test(old_path, G_FILE_TEST_IS_DIR)) { |
742 |
|
printf("old file path %s exists\n", old_path); |
743 |
|
rmdir_recursive(old_path); |
744 |
|
} |
745 |
|
|
746 |
|
/* It is recommanded that all applications share these same */ |
747 |
|
/* map path, so data is only cached once. The path should be: */ |
748 |
|
/* ~/.osm-gps-map on standard PC (users home) */ |
749 |
|
/* /home/user/.osm-gps-map on Maemo5 (ext3 on internal card) */ |
750 |
|
/* /media/mmc2/osm-gps-map on Maemo4 (vfat on internal card) */ |
751 |
|
#if !defined(USE_MAEMO) |
752 |
|
char *p = getenv("HOME"); |
753 |
|
if(!p) p = "/tmp"; |
754 |
|
char *path = g_strdup_printf("%s/.osm-gps-map", p); |
755 |
|
#else |
756 |
|
#if MAEMO_VERSION_MAJOR == 5 |
757 |
|
char *path = g_strdup("/home/user/.osm-gps-map"); |
758 |
|
#else |
759 |
|
char *path = g_strdup("/media/mmc2/osm-gps-map"); |
760 |
|
#endif |
761 |
|
#endif |
762 |
|
|
|
char *path = g_strdup_printf("%s/map/", appdata->image_path); |
|
763 |
const char *proxy = get_proxy_uri(appdata); |
const char *proxy = get_proxy_uri(appdata); |
764 |
|
|
765 |
|
gint source = context->appdata->map.source; |
766 |
|
if(!source) source = MAP_SOURCE; |
767 |
|
|
768 |
context->widget = g_object_new(OSM_TYPE_GPS_MAP, |
context->widget = g_object_new(OSM_TYPE_GPS_MAP, |
769 |
"repo-uri", MAP_SOURCE_OPENSTREETMAP, |
"map-source", source, |
770 |
"tile-cache", path, |
"tile-cache", path, |
771 |
proxy?"proxy-uri":NULL, proxy, |
"auto-center", FALSE, |
772 |
|
"record-trip-history", FALSE, |
773 |
|
"show-trip-history", FALSE, |
774 |
|
proxy?"proxy-uri":NULL, proxy, |
775 |
NULL); |
NULL); |
776 |
|
|
777 |
g_free(path); |
g_free(path); |
778 |
|
|
779 |
char *name = NULL; |
osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget)); |
|
#ifdef USE_MAEMO |
|
|
if(!appdata->cur_gpx) { |
|
|
#endif |
|
|
/* draw all geocaches */ |
|
|
gpx_t *gpx = appdata->gpx; |
|
|
while(gpx) { |
|
|
map_draw_cachelist(context->widget, gpx->cache); |
|
|
gpx = gpx->next; |
|
|
} |
|
|
name = g_strdup(_("all geocaches")); |
|
|
#ifdef USE_MAEMO |
|
|
} else { |
|
|
map_draw_cachelist(context->widget, appdata->cur_gpx->cache); |
|
|
name = g_strdup(appdata->cur_gpx->name); |
|
|
} |
|
|
#endif |
|
|
|
|
|
char *title = g_strdup_printf(_("Map - %s"), name); |
|
|
g_free(name); |
|
780 |
|
|
781 |
#if MAEMO_VERSION_MAJOR == 5 |
#ifdef USE_MAEMO |
782 |
GtkWidget *window = hildon_stackable_window_new(); |
/* we don't use a stackable window here on fremantle, since */ |
783 |
gtk_window_set_title(GTK_WINDOW(window), title); |
/* this leaves the main window independent from the map and */ |
784 |
|
/* the user can e.g. still navigate the main menu */ |
785 |
|
context->window = hildon_window_new(); |
786 |
|
|
787 |
|
#if (MAEMO_VERSION_MAJOR == 5) && !defined(__i386__) |
788 |
|
g_signal_connect(G_OBJECT(context->window), "realize", |
789 |
|
G_CALLBACK(on_window_realize), NULL); |
790 |
|
#endif // MAEMO_VERSION |
791 |
#else |
#else |
792 |
GtkWidget *dialog = gtk_dialog_new_with_buttons(title, |
context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
793 |
GTK_WINDOW(appdata->window), |
#endif |
|
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
|
|
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
|
|
NULL); |
|
794 |
|
|
795 |
#ifndef USE_MAEMO |
#ifndef USE_MAEMO |
796 |
gtk_window_set_default_size(GTK_WINDOW(dialog), 640, 480); |
gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480); |
|
#else |
|
|
gtk_window_set_default_size(GTK_WINDOW(dialog), 800, 480); |
|
|
#endif |
|
797 |
#endif |
#endif |
798 |
|
|
799 |
g_free(title); |
g_signal_connect(G_OBJECT(context->widget), "focus-in-event", |
800 |
|
G_CALLBACK(on_focus_in), context); |
801 |
|
|
802 |
g_signal_connect(G_OBJECT(context->widget), "configure-event", |
g_signal_connect(G_OBJECT(context->widget), "configure-event", |
803 |
G_CALLBACK(on_map_configure), context); |
G_CALLBACK(on_map_configure), context); |
808 |
g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
g_signal_connect(G_OBJECT(context->widget), "button-release-event", |
809 |
G_CALLBACK(on_map_button_release_event), context); |
G_CALLBACK(on_map_button_release_event), context); |
810 |
|
|
|
gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget); |
|
|
/* zoom button box */ |
|
|
GtkWidget *vbox = gtk_vbox_new(FALSE,0); |
|
|
|
|
|
context->zoomin = |
|
|
map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin), |
|
|
context, _("Zoom in")); |
|
|
gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0); |
|
|
|
|
|
context->zoomout = |
|
|
map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout), |
|
|
context, _("Zoom out")); |
|
|
gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0); |
|
|
|
|
|
context->gps = |
|
|
map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps), |
|
|
context, _("Jump to GPS position")); |
|
|
gtk_widget_set_sensitive(context->gps, FALSE); |
|
811 |
/* install handler for timed updates of the gps button */ |
/* install handler for timed updates of the gps button */ |
812 |
context->handler_id = gtk_timeout_add(1000, map_gps_update, context); |
context->handler_id = gtk_timeout_add(1000, map_gps_update, context); |
813 |
gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0); |
context->cb_id = gps_register_callback(appdata, gps_callback, context); |
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); |
|
814 |
|
|
815 |
#if MAEMO_VERSION_MAJOR == 5 |
#if MAEMO_VERSION_MAJOR == 5 |
816 |
/* prevent some of the main screen things */ |
/* prevent some of the main screen things */ |
817 |
context->old_view = appdata->cur_view; |
context->old_view = appdata->cur_view; |
818 |
appdata->cur_view = NULL; |
appdata->cur_view = NULL; |
819 |
|
#endif |
820 |
|
|
821 |
g_signal_connect(G_OBJECT(window), "destroy", |
g_signal_connect(G_OBJECT(context->window), "destroy", |
822 |
G_CALLBACK(on_window_destroy), context); |
G_CALLBACK(on_window_destroy), context); |
823 |
|
|
824 |
gtk_container_add(GTK_CONTAINER(window), hbox); |
gtk_container_add(GTK_CONTAINER(context->window), context->widget); |
825 |
gtk_widget_show_all(GTK_WIDGET(window)); |
gtk_widget_show_all(GTK_WIDGET(context->window)); |
|
|
|
|
#else |
|
|
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox); |
|
|
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); |
|
|
gtk_widget_show_all(dialog); |
|
|
gtk_dialog_run(GTK_DIALOG(dialog)); |
|
|
gtk_timeout_remove(context->handler_id); |
|
|
gtk_widget_destroy(dialog); |
|
|
g_free(context); |
|
|
#endif |
|
826 |
} |
} |