Parent Directory | Revision Log
osm-gps-map navigator
1 | /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* vim:set et sw=4 ts=4 cino=t0,(0: */ |
3 | /* |
4 | * osm-gps-map.c |
5 | * Copyright (C) Marcus Bauer 2008 <marcus.bauer@gmail.com> |
6 | * Copyright (C) John Stowers 2009 <john.stowers@gmail.com> |
7 | * |
8 | * Contributions by |
9 | * Everaldo Canuto 2009 <everaldo.canuto@gmail.com> |
10 | * |
11 | * osm-gps-map.c is free software: you can redistribute it and/or modify it |
12 | * under the terms of the GNU General Public License as published by the |
13 | * Free Software Foundation, either version 3 of the License, or |
14 | * (at your option) any later version. |
15 | * |
16 | * osm-gps-map.c is distributed in the hope that it will be useful, but |
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | * See the GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU General Public License along |
22 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
23 | */ |
24 | |
25 | #include "config.h" |
26 | |
27 | #include <fcntl.h> |
28 | #include <math.h> |
29 | #include <unistd.h> |
30 | #include <stdio.h> |
31 | #include <stdlib.h> |
32 | #include <string.h> |
33 | |
34 | #include <gdk/gdk.h> |
35 | #include <glib.h> |
36 | #include <glib/gstdio.h> |
37 | #include <glib/gprintf.h> |
38 | #include <libsoup/soup.h> |
39 | |
40 | #include "converter.h" |
41 | #include "osm-gps-map-types.h" |
42 | #include "osm-gps-map.h" |
43 | |
44 | #ifdef USE_CAIRO |
45 | #include <cairo.h> |
46 | #endif |
47 | |
48 | #define ENABLE_DEBUG 0 |
49 | |
50 | #define EXTRA_BORDER (TILESIZE / 2) |
51 | |
52 | struct _OsmGpsMapPrivate |
53 | { |
54 | GHashTable *tile_queue; |
55 | GHashTable *missing_tiles; |
56 | GHashTable *tile_cache; |
57 | |
58 | int map_zoom; |
59 | int max_zoom; |
60 | int min_zoom; |
61 | gboolean map_auto_center; |
62 | gboolean map_auto_download; |
63 | int map_x; |
64 | int map_y; |
65 | |
66 | /* Latitude and longitude of the center of the map, in radians */ |
67 | gfloat center_rlat; |
68 | gfloat center_rlon; |
69 | |
70 | guint max_tile_cache_size; |
71 | /* Incremented at each redraw */ |
72 | guint redraw_cycle; |
73 | /* ID of the idle redraw operation */ |
74 | guint idle_map_redraw; |
75 | |
76 | //how we download tiles |
77 | SoupSession *soup_session; |
78 | char *proxy_uri; |
79 | |
80 | //where downloaded tiles are cached |
81 | char *cache_dir; |
82 | gboolean cache_dir_is_full_path; |
83 | |
84 | //contains flags indicating the various special characters |
85 | //the uri string contains, that will be replaced when calculating |
86 | //the uri to download. |
87 | OsmGpsMapSource_t map_source; |
88 | char *repo_uri; |
89 | char *image_format; |
90 | int uri_format; |
91 | //flag indicating if the map source is located on the google |
92 | gboolean the_google; |
93 | |
94 | //gps tracking state |
95 | gboolean record_trip_history; |
96 | gboolean show_trip_history; |
97 | GSList *trip_history; |
98 | coord_t *gps; |
99 | gboolean gps_valid; |
100 | |
101 | //a balloon with additional info |
102 | struct { |
103 | coord_t *coo; |
104 | gboolean valid; |
105 | OsmGpsMapRect_t rect; |
106 | OsmGpsMapBalloonCallback cb; |
107 | gpointer data; |
108 | } balloon; |
109 | |
110 | #ifdef ENABLE_OSD |
111 | //the osd controls |
112 | struct { |
113 | GdkPixmap *backup; |
114 | gint backup_x, backup_y; |
115 | |
116 | |
117 | // GdkPixbuf *pixbuf; |
118 | } osd; |
119 | #endif |
120 | |
121 | //additional images or tracks added to the map |
122 | GSList *tracks; |
123 | GSList *images; |
124 | |
125 | //Used for storing the joined tiles |
126 | GdkPixmap *pixmap; |
127 | GdkGC *gc_map; |
128 | |
129 | //The tile painted when one cannot be found |
130 | GdkPixbuf *null_tile; |
131 | |
132 | //For tracking click and drag |
133 | int drag_counter; |
134 | int drag_mouse_dx; |
135 | int drag_mouse_dy; |
136 | int drag_start_mouse_x; |
137 | int drag_start_mouse_y; |
138 | int drag_start_map_x; |
139 | int drag_start_map_y; |
140 | |
141 | //for customizing the redering of the gps track |
142 | int ui_gps_track_width; |
143 | int ui_gps_point_inner_radius; |
144 | int ui_gps_point_outer_radius; |
145 | |
146 | guint is_disposed : 1; |
147 | guint dragging : 1; |
148 | guint center_coord_set : 1; |
149 | }; |
150 | |
151 | #define OSM_GPS_MAP_PRIVATE(o) (OSM_GPS_MAP (o)->priv) |
152 | |
153 | typedef struct |
154 | { |
155 | GdkPixbuf *pixbuf; |
156 | /* We keep track of the number of the redraw cycle this tile was last used, |
157 | * so that osm_gps_map_purge_cache() can remove the older ones */ |
158 | guint redraw_cycle; |
159 | } OsmCachedTile; |
160 | |
161 | enum |
162 | { |
163 | PROP_0, |
164 | |
165 | PROP_AUTO_CENTER, |
166 | PROP_RECORD_TRIP_HISTORY, |
167 | PROP_SHOW_TRIP_HISTORY, |
168 | PROP_AUTO_DOWNLOAD, |
169 | PROP_REPO_URI, |
170 | PROP_PROXY_URI, |
171 | PROP_TILE_CACHE_DIR, |
172 | PROP_TILE_CACHE_DIR_IS_FULL_PATH, |
173 | PROP_ZOOM, |
174 | PROP_MAX_ZOOM, |
175 | PROP_MIN_ZOOM, |
176 | PROP_LATITUDE, |
177 | PROP_LONGITUDE, |
178 | PROP_MAP_X, |
179 | PROP_MAP_Y, |
180 | PROP_TILES_QUEUED, |
181 | PROP_GPS_TRACK_WIDTH, |
182 | PROP_GPS_POINT_R1, |
183 | PROP_GPS_POINT_R2, |
184 | PROP_MAP_SOURCE, |
185 | PROP_IMAGE_FORMAT |
186 | }; |
187 | |
188 | G_DEFINE_TYPE (OsmGpsMap, osm_gps_map, GTK_TYPE_DRAWING_AREA); |
189 | |
190 | /* |
191 | * Drawing function forward defintions |
192 | */ |
193 | static gchar *replace_string(const gchar *src, const gchar *from, const gchar *to); |
194 | static void inspect_map_uri(OsmGpsMap *map); |
195 | static gchar *replace_map_uri(OsmGpsMap *map, const gchar *uri, int zoom, int x, int y); |
196 | static void osm_gps_map_print_images (OsmGpsMap *map); |
197 | static void osm_gps_map_draw_gps_point (OsmGpsMap *map); |
198 | static void osm_gps_map_blit_tile(OsmGpsMap *map, GdkPixbuf *pixbuf, int offset_x, int offset_y); |
199 | #ifdef LIBSOUP22 |
200 | static void osm_gps_map_tile_download_complete (SoupMessage *msg, gpointer user_data); |
201 | #else |
202 | static void osm_gps_map_tile_download_complete (SoupSession *session, SoupMessage *msg, gpointer user_data); |
203 | #endif |
204 | static void osm_gps_map_download_tile (OsmGpsMap *map, int zoom, int x, int y, gboolean redraw); |
205 | static void osm_gps_map_load_tile (OsmGpsMap *map, int zoom, int x, int y, int offset_x, int offset_y); |
206 | static void osm_gps_map_fill_tiles_pixel (OsmGpsMap *map); |
207 | static gboolean osm_gps_map_map_redraw (OsmGpsMap *map); |
208 | static void osm_gps_map_map_redraw_idle (OsmGpsMap *map); |
209 | |
210 | static void |
211 | cached_tile_free (OsmCachedTile *tile) |
212 | { |
213 | g_object_unref (tile->pixbuf); |
214 | g_slice_free (OsmCachedTile, tile); |
215 | } |
216 | |
217 | /* |
218 | * Description: |
219 | * Find and replace text within a string. |
220 | * |
221 | * Parameters: |
222 | * src (in) - pointer to source string |
223 | * from (in) - pointer to search text |
224 | * to (in) - pointer to replacement text |
225 | * |
226 | * Returns: |
227 | * Returns a pointer to dynamically-allocated memory containing string |
228 | * with occurences of the text pointed to by 'from' replaced by with the |
229 | * text pointed to by 'to'. |
230 | */ |
231 | static gchar * |
232 | replace_string(const gchar *src, const gchar *from, const gchar *to) |
233 | { |
234 | size_t size = strlen(src) + 1; |
235 | size_t fromlen = strlen(from); |
236 | size_t tolen = strlen(to); |
237 | |
238 | /* Allocate the first chunk with enough for the original string. */ |
239 | gchar *value = g_malloc(size); |
240 | |
241 | |
242 | /* We need to return 'value', so let's make a copy to mess around with. */ |
243 | gchar *dst = value; |
244 | |
245 | if ( value != NULL ) |
246 | { |
247 | for ( ;; ) |
248 | { |
249 | /* Try to find the search text. */ |
250 | const gchar *match = g_strstr_len(src, size, from); |
251 | if ( match != NULL ) |
252 | { |
253 | gchar *temp; |
254 | /* Find out how many characters to copy up to the 'match'. */ |
255 | size_t count = match - src; |
256 | |
257 | |
258 | /* Calculate the total size the string will be after the |
259 | * replacement is performed. */ |
260 | size += tolen - fromlen; |
261 | |
262 | temp = g_realloc(value, size); |
263 | if ( temp == NULL ) |
264 | { |
265 | g_free(value); |
266 | return NULL; |
267 | } |
268 | |
269 | /* we'll want to return 'value' eventually, so let's point it |
270 | * to the memory that we are now working with. |
271 | * And let's not forget to point to the right location in |
272 | * the destination as well. */ |
273 | dst = temp + (dst - value); |
274 | value = temp; |
275 | |
276 | /* |
277 | * Copy from the source to the point where we matched. Then |
278 | * move the source pointer ahead by the amount we copied. And |
279 | * move the destination pointer ahead by the same amount. |
280 | */ |
281 | g_memmove(dst, src, count); |
282 | src += count; |
283 | dst += count; |
284 | |
285 | /* Now copy in the replacement text 'to' at the position of |
286 | * the match. Adjust the source pointer by the text we replaced. |
287 | * Adjust the destination pointer by the amount of replacement |
288 | * text. */ |
289 | g_memmove(dst, to, tolen); |
290 | src += fromlen; |
291 | dst += tolen; |
292 | } |
293 | else |
294 | { |
295 | /* |
296 | * Copy any remaining part of the string. This includes the null |
297 | * termination character. |
298 | */ |
299 | strcpy(dst, src); |
300 | break; |
301 | } |
302 | } |
303 | } |
304 | return value; |
305 | } |
306 | |
307 | static void |
308 | map_convert_coords_to_quadtree_string(OsmGpsMap *map, gint x, gint y, gint zoomlevel, |
309 | gchar *buffer, const gchar initial, |
310 | const gchar *const quadrant) |
311 | { |
312 | gchar *ptr = buffer; |
313 | gint n; |
314 | |
315 | if (initial) |
316 | *ptr++ = initial; |
317 | |
318 | for(n = zoomlevel-1; n >= 0; n--) |
319 | { |
320 | gint xbit = (x >> n) & 1; |
321 | gint ybit = (y >> n) & 1; |
322 | *ptr++ = quadrant[xbit + 2 * ybit]; |
323 | } |
324 | |
325 | *ptr++ = '\0'; |
326 | } |
327 | |
328 | |
329 | static void |
330 | inspect_map_uri(OsmGpsMap *map) |
331 | { |
332 | OsmGpsMapPrivate *priv = map->priv; |
333 | |
334 | if (g_strrstr(priv->repo_uri, URI_MARKER_X)) |
335 | priv->uri_format |= URI_HAS_X; |
336 | |
337 | if (g_strrstr(priv->repo_uri, URI_MARKER_Y)) |
338 | priv->uri_format |= URI_HAS_Y; |
339 | |
340 | if (g_strrstr(priv->repo_uri, URI_MARKER_Z)) |
341 | priv->uri_format |= URI_HAS_Z; |
342 | |
343 | if (g_strrstr(priv->repo_uri, URI_MARKER_S)) |
344 | priv->uri_format |= URI_HAS_S; |
345 | |
346 | if (g_strrstr(priv->repo_uri, URI_MARKER_Q)) |
347 | priv->uri_format |= URI_HAS_Q; |
348 | |
349 | if (g_strrstr(priv->repo_uri, URI_MARKER_Q0)) |
350 | priv->uri_format |= URI_HAS_Q0; |
351 | |
352 | if (g_strrstr(priv->repo_uri, URI_MARKER_YS)) |
353 | priv->uri_format |= URI_HAS_YS; |
354 | |
355 | if (g_strrstr(priv->repo_uri, URI_MARKER_R)) |
356 | priv->uri_format |= URI_HAS_R; |
357 | |
358 | if (g_strrstr(priv->repo_uri, "google.com")) |
359 | priv->the_google = TRUE; |
360 | |
361 | g_debug("URI Format: 0x%X (google: %X)", priv->uri_format, priv->the_google); |
362 | |
363 | } |
364 | |
365 | static gchar * |
366 | replace_map_uri(OsmGpsMap *map, const gchar *uri, int zoom, int x, int y) |
367 | { |
368 | OsmGpsMapPrivate *priv = map->priv; |
369 | char *url; |
370 | unsigned int i; |
371 | char location[22]; |
372 | |
373 | i = 1; |
374 | url = g_strdup(uri); |
375 | while (i < URI_FLAG_END) |
376 | { |
377 | char *s = NULL; |
378 | char *old; |
379 | |
380 | old = url; |
381 | switch(i & priv->uri_format) |
382 | { |
383 | case URI_HAS_X: |
384 | s = g_strdup_printf("%d", x); |
385 | url = replace_string(url, URI_MARKER_X, s); |
386 | //g_debug("FOUND " URI_MARKER_X); |
387 | break; |
388 | case URI_HAS_Y: |
389 | s = g_strdup_printf("%d", y); |
390 | url = replace_string(url, URI_MARKER_Y, s); |
391 | //g_debug("FOUND " URI_MARKER_Y); |
392 | break; |
393 | case URI_HAS_Z: |
394 | s = g_strdup_printf("%d", zoom); |
395 | url = replace_string(url, URI_MARKER_Z, s); |
396 | //g_debug("FOUND " URI_MARKER_Z); |
397 | break; |
398 | case URI_HAS_S: |
399 | s = g_strdup_printf("%d", priv->max_zoom-zoom); |
400 | url = replace_string(url, URI_MARKER_S, s); |
401 | //g_debug("FOUND " URI_MARKER_S); |
402 | break; |
403 | case URI_HAS_Q: |
404 | map_convert_coords_to_quadtree_string(map,x,y,zoom,location,'t',"qrts"); |
405 | s = g_strdup_printf("%s", location); |
406 | url = replace_string(url, URI_MARKER_Q, s); |
407 | //g_debug("FOUND " URI_MARKER_Q); |
408 | break; |
409 | case URI_HAS_Q0: |
410 | map_convert_coords_to_quadtree_string(map,x,y,zoom,location,'\0', "0123"); |
411 | s = g_strdup_printf("%s", location); |
412 | url = replace_string(url, URI_MARKER_Q0, s); |
413 | //g_debug("FOUND " URI_MARKER_Q0); |
414 | break; |
415 | case URI_HAS_YS: |
416 | // s = g_strdup_printf("%d", y); |
417 | // url = replace_string(url, URI_MARKER_YS, s); |
418 | g_warning("FOUND " URI_MARKER_YS " NOT IMPLEMENTED"); |
419 | // retval = g_strdup_printf(repo->url, |
420 | // tilex, |
421 | // (1 << (MAX_ZOOM - zoom)) - tiley - 1, |
422 | // zoom - (MAX_ZOOM - 17)); |
423 | break; |
424 | case URI_HAS_R: |
425 | s = g_strdup_printf("%d", g_random_int_range(0,4)); |
426 | url = replace_string(url, URI_MARKER_R, s); |
427 | //g_debug("FOUND " URI_MARKER_R); |
428 | break; |
429 | default: |
430 | s = NULL; |
431 | break; |
432 | } |
433 | |
434 | if (s) { |
435 | g_free(s); |
436 | g_free(old); |
437 | } |
438 | |
439 | i = (i << 1); |
440 | |
441 | } |
442 | |
443 | return url; |
444 | } |
445 | |
446 | static void |
447 | my_log_handler (const gchar * log_domain, GLogLevelFlags log_level, const gchar * message, gpointer user_data) |
448 | { |
449 | if (!(log_level & G_LOG_LEVEL_DEBUG) || ENABLE_DEBUG) |
450 | g_log_default_handler (log_domain, log_level, message, user_data); |
451 | } |
452 | |
453 | static float |
454 | osm_gps_map_get_scale_at_point(int zoom, float rlat, float rlon) |
455 | { |
456 | /* world at zoom 1 == 512 pixels */ |
457 | return cos(rlat) * M_PI * OSM_EQ_RADIUS / (1<<(7+zoom)); |
458 | } |
459 | |
460 | /* clears the trip list and all resources */ |
461 | static void |
462 | osm_gps_map_free_trip (OsmGpsMap *map) |
463 | { |
464 | OsmGpsMapPrivate *priv = map->priv; |
465 | if (priv->trip_history) { |
466 | g_slist_foreach(priv->trip_history, (GFunc) g_free, NULL); |
467 | g_slist_free(priv->trip_history); |
468 | priv->trip_history = NULL; |
469 | } |
470 | } |
471 | |
472 | /* clears the tracks and all resources */ |
473 | static void |
474 | osm_gps_map_free_tracks (OsmGpsMap *map) |
475 | { |
476 | OsmGpsMapPrivate *priv = map->priv; |
477 | if (priv->tracks) |
478 | { |
479 | GSList* tmp = priv->tracks; |
480 | while (tmp != NULL) |
481 | { |
482 | g_slist_foreach(tmp->data, (GFunc) g_free, NULL); |
483 | g_slist_free(tmp->data); |
484 | tmp = g_slist_next(tmp); |
485 | } |
486 | g_slist_free(priv->tracks); |
487 | priv->tracks = NULL; |
488 | } |
489 | } |
490 | |
491 | /* free the poi image lists */ |
492 | static void |
493 | osm_gps_map_free_images (OsmGpsMap *map) |
494 | { |
495 | OsmGpsMapPrivate *priv = map->priv; |
496 | if (priv->images) { |
497 | GSList *list; |
498 | for(list = priv->images; list != NULL; list = list->next) |
499 | { |
500 | image_t *im = list->data; |
501 | g_object_unref(im->image); |
502 | g_free(im); |
503 | } |
504 | g_slist_free(priv->images); |
505 | priv->images = NULL; |
506 | } |
507 | } |
508 | |
509 | static void |
510 | osm_gps_map_print_images (OsmGpsMap *map) |
511 | { |
512 | GSList *list; |
513 | int x,y,pixel_x,pixel_y; |
514 | int min_x = 0,min_y = 0,max_x = 0,max_y = 0; |
515 | int map_x0, map_y0; |
516 | OsmGpsMapPrivate *priv = map->priv; |
517 | |
518 | map_x0 = priv->map_x - EXTRA_BORDER; |
519 | map_y0 = priv->map_y - EXTRA_BORDER; |
520 | for(list = priv->images; list != NULL; list = list->next) |
521 | { |
522 | image_t *im = list->data; |
523 | |
524 | // pixel_x,y, offsets |
525 | pixel_x = lon2pixel(priv->map_zoom, im->pt.rlon); |
526 | pixel_y = lat2pixel(priv->map_zoom, im->pt.rlat); |
527 | |
528 | g_debug("Image %dx%d @: %f,%f (%d,%d)", |
529 | im->w, im->h, |
530 | im->pt.rlat, im->pt.rlon, |
531 | pixel_x, pixel_y); |
532 | |
533 | x = pixel_x - map_x0; |
534 | y = pixel_y - map_y0; |
535 | |
536 | gdk_draw_pixbuf ( |
537 | priv->pixmap, |
538 | priv->gc_map, |
539 | im->image, |
540 | 0,0, |
541 | x-(im->w/2),y-(im->h/2), |
542 | im->w,im->h, |
543 | GDK_RGB_DITHER_NONE, 0, 0); |
544 | |
545 | max_x = MAX(x+im->w,max_x); |
546 | min_x = MIN(x-im->w,min_x); |
547 | max_y = MAX(y+im->h,max_y); |
548 | min_y = MIN(y-im->h,min_y); |
549 | } |
550 | |
551 | gtk_widget_queue_draw_area ( |
552 | GTK_WIDGET(map), |
553 | min_x + EXTRA_BORDER, min_y + EXTRA_BORDER, |
554 | max_x + EXTRA_BORDER, max_y + EXTRA_BORDER); |
555 | |
556 | } |
557 | |
558 | static void |
559 | osm_gps_map_draw_gps_point (OsmGpsMap *map) |
560 | { |
561 | OsmGpsMapPrivate *priv = map->priv; |
562 | |
563 | //incase we get called before we have got a gps point |
564 | if (priv->gps_valid) { |
565 | int map_x0, map_y0; |
566 | int x, y; |
567 | int r = priv->ui_gps_point_inner_radius; |
568 | int r2 = priv->ui_gps_point_outer_radius; |
569 | // int lw = priv->ui_gps_track_width; |
570 | int mr = MAX(r,r2); |
571 | |
572 | map_x0 = priv->map_x - EXTRA_BORDER; |
573 | map_y0 = priv->map_y - EXTRA_BORDER; |
574 | x = lon2pixel(priv->map_zoom, priv->gps->rlon) - map_x0; |
575 | y = lat2pixel(priv->map_zoom, priv->gps->rlat) - map_y0; |
576 | #ifdef USE_CAIRO |
577 | cairo_t *cr; |
578 | cairo_pattern_t *pat; |
579 | #else |
580 | GdkColor color; |
581 | GdkGC *marker; |
582 | #endif |
583 | |
584 | #ifdef USE_CAIRO |
585 | cr = gdk_cairo_create(priv->pixmap); |
586 | |
587 | // draw transparent area |
588 | if (r2 > 0) { |
589 | cairo_set_line_width (cr, 1.5); |
590 | cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 0.4); |
591 | cairo_arc (cr, x, y, r2, 0, 2 * M_PI); |
592 | cairo_fill (cr); |
593 | // draw transparent area border |
594 | cairo_set_source_rgba (cr, 0.55, 0.55, 0.55, 0.4); |
595 | cairo_arc (cr, x, y, r2, 0, 2 * M_PI); |
596 | cairo_stroke(cr); |
597 | } |
598 | |
599 | // draw ball gradient |
600 | if (r > 0) { |
601 | pat = cairo_pattern_create_radial (x-(r/5), y-(r/5), (r/5), x, y, r); |
602 | cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1.0); |
603 | cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 1, 1.0); |
604 | cairo_set_source (cr, pat); |
605 | cairo_arc (cr, x, y, r, 0, 2 * M_PI); |
606 | cairo_fill (cr); |
607 | cairo_pattern_destroy (pat); |
608 | // draw ball border |
609 | cairo_set_line_width (cr, 1.0); |
610 | cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0); |
611 | cairo_arc (cr, x, y, r, 0, 2 * M_PI); |
612 | cairo_stroke(cr); |
613 | } |
614 | |
615 | cairo_destroy(cr); |
616 | gtk_widget_queue_draw_area (GTK_WIDGET(map), |
617 | x-mr, |
618 | y-mr, |
619 | mr*2, |
620 | mr*2); |
621 | #else |
622 | marker = gdk_gc_new(priv->pixmap); |
623 | color.red = 5000; |
624 | color.green = 5000; |
625 | color.blue = 55000; |
626 | gdk_gc_set_rgb_fg_color(marker, &color); |
627 | gdk_gc_set_line_attributes(marker, lw, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); |
628 | |
629 | if (r2 > 0) { |
630 | gdk_draw_arc (priv->pixmap, |
631 | marker, |
632 | FALSE, //filled |
633 | x-r2, y-r2, // x,y |
634 | r2*2,r2*2, // width, height |
635 | 0, 360*64); // start-end angle 64th, from 3h, anti clockwise |
636 | } |
637 | if (r > 0) { |
638 | gdk_draw_arc (priv->pixmap, |
639 | marker, |
640 | TRUE, //filled |
641 | x-r, y-r, // x,y |
642 | r*2,r*2, // width, height |
643 | 0, 360*64); // start-end angle 64th, from 3h, anti clockwise |
644 | } |
645 | |
646 | g_object_unref(marker); |
647 | gtk_widget_queue_draw_area (GTK_WIDGET(map), |
648 | x-(mr+lw), |
649 | y-(mr+lw), |
650 | (mr*2)+lw+lw, |
651 | (mr*2)+lw+lw); |
652 | #endif |
653 | } |
654 | } |
655 | |
656 | /* most visual effects are hardcoded by now, but may be made */ |
657 | /* available via properties later */ |
658 | #define BALLOON_AREA_WIDTH 290 |
659 | #define BALLOON_AREA_HEIGHT 75 |
660 | |
661 | #define BALLOON_CORNER_RADIUS 20 |
662 | #define BALLOON_BORDER (BALLOON_CORNER_RADIUS/4) |
663 | #define BALLOON_WIDTH (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER) |
664 | #define BALLOON_HEIGHT (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER) |
665 | #define BALLOON_TRANSPARENCY 0.8 |
666 | #define POINTER_HEIGHT 20 |
667 | #define POINTER_FOOT_WIDTH 20 |
668 | #define POINTER_OFFSET (BALLOON_CORNER_RADIUS*3/4) |
669 | #define BALLOON_SHADOW 5 |
670 | #define BALLOON_SHADOW_TRANSPARENCY 0.2 |
671 | |
672 | #define CLOSE_BUTTON_RADIUS (BALLOON_CORNER_RADIUS/3) |
673 | |
674 | |
675 | /* draw the bubble shape. this is used twice, once for the shape and once */ |
676 | /* for the shadow */ |
677 | static void |
678 | osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1, |
679 | gboolean bottom, int px, int py, int px0, int px1) { |
680 | |
681 | cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS); |
682 | cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + BALLOON_CORNER_RADIUS, y0); |
683 | if(!bottom) { |
684 | /* insert top pointer */ |
685 | cairo_line_to (cr, px1, y0); |
686 | cairo_line_to (cr, px, py); |
687 | cairo_line_to (cr, px0, y0); |
688 | } |
689 | |
690 | cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0); |
691 | cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + BALLOON_CORNER_RADIUS); |
692 | cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS); |
693 | cairo_curve_to (cr, x1, y1, x1, y1, x1 - BALLOON_CORNER_RADIUS, y1); |
694 | if(bottom) { |
695 | /* insert bottom pointer */ |
696 | cairo_line_to (cr, px0, y1); |
697 | cairo_line_to (cr, px, py); |
698 | cairo_line_to (cr, px1, y1); |
699 | } |
700 | |
701 | cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1); |
702 | cairo_curve_to (cr, x0, y1, x0, y1, x0, y1 - BALLOON_CORNER_RADIUS); |
703 | |
704 | cairo_close_path (cr); |
705 | } |
706 | |
707 | static void |
708 | osm_gps_map_draw_balloon_int (OsmGpsMap *map) |
709 | { |
710 | OsmGpsMapPrivate *priv = map->priv; |
711 | |
712 | if (priv->balloon.valid) { |
713 | |
714 | /* ------- convert given coordinate into screen position --------- */ |
715 | int x0 = lon2pixel(priv->map_zoom, priv->balloon.coo->rlon) - |
716 | priv->map_x + EXTRA_BORDER; |
717 | int y0 = lat2pixel(priv->map_zoom, priv->balloon.coo->rlat) - |
718 | priv->map_y + EXTRA_BORDER; |
719 | |
720 | /* check position of this relative to screen center to determine */ |
721 | /* pointer direction ... */ |
722 | int pointer_x = x0, pointer_x0, pointer_x1; |
723 | int pointer_y = y0; |
724 | |
725 | /* ... and calculate position */ |
726 | if((x0 - EXTRA_BORDER) > GTK_WIDGET(map)->allocation.width/2) { |
727 | x0 -= BALLOON_WIDTH - POINTER_OFFSET; |
728 | pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET); |
729 | pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH; |
730 | } else { |
731 | x0 -= POINTER_OFFSET; |
732 | pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET); |
733 | pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH; |
734 | } |
735 | |
736 | gboolean bottom = FALSE; |
737 | if((y0 - EXTRA_BORDER) > GTK_WIDGET(map)->allocation.height/2) { |
738 | bottom = TRUE; |
739 | y0 -= BALLOON_HEIGHT + POINTER_HEIGHT; |
740 | } else |
741 | y0 += POINTER_HEIGHT; |
742 | |
743 | /* calculate bottom/right of box */ |
744 | int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT; |
745 | |
746 | /* save balloon screen coordinates for later use */ |
747 | priv->balloon.rect.x = x0 + BALLOON_BORDER; |
748 | priv->balloon.rect.y = y0 + BALLOON_BORDER; |
749 | priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER; |
750 | priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER; |
751 | |
752 | #ifdef USE_CAIRO |
753 | cairo_t *cr = gdk_cairo_create(priv->pixmap); |
754 | |
755 | /* --------- draw shadow --------------- */ |
756 | osm_gps_map_draw_balloon_shape (cr, |
757 | x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW, |
758 | x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW, |
759 | bottom, pointer_x, pointer_y, |
760 | pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW); |
761 | |
762 | cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY); |
763 | cairo_fill_preserve (cr); |
764 | cairo_set_source_rgba (cr, 1, 0, 0, 1.0); |
765 | cairo_set_line_width (cr, 0); |
766 | cairo_stroke (cr); |
767 | |
768 | /* --------- draw main shape ----------- */ |
769 | osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1, |
770 | bottom, pointer_x, pointer_y, pointer_x0, pointer_x1); |
771 | |
772 | cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY); |
773 | cairo_fill_preserve (cr); |
774 | cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY); |
775 | cairo_set_line_width (cr, 1); |
776 | cairo_stroke (cr); |
777 | |
778 | |
779 | /* ---------- draw close button --------- */ |
780 | |
781 | int cx = x1 - BALLOON_BORDER - CLOSE_BUTTON_RADIUS; |
782 | int cy = y0 + BALLOON_BORDER + CLOSE_BUTTON_RADIUS; |
783 | int crad = CLOSE_BUTTON_RADIUS; |
784 | |
785 | cairo_arc (cr, cx, cy, crad, 0, 2 * M_PI); |
786 | cairo_set_source_rgba (cr, 0.8, 0, 0, 1.0); |
787 | cairo_fill_preserve (cr); |
788 | cairo_set_source_rgba (cr, 0.3, 0, 0, 1.0); |
789 | cairo_set_line_width (cr, 2); |
790 | cairo_stroke(cr); |
791 | |
792 | cairo_set_source_rgba (cr, 1, 1, 1, 1.0); |
793 | cairo_set_line_width (cr, 3); |
794 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
795 | cairo_move_to (cr, cx - crad/2, cy - crad/2); |
796 | cairo_line_to (cr, cx + crad/2, cy + crad/2); |
797 | cairo_stroke (cr); |
798 | cairo_move_to (cr, cx + crad/2, cy - crad/2); |
799 | cairo_line_to (cr, cx - crad/2, cy + crad/2); |
800 | cairo_stroke (cr); |
801 | |
802 | if (priv->balloon.cb) { |
803 | /* clip in case application tries to draw in */ |
804 | /* exceed of the balloon */ |
805 | cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y, |
806 | priv->balloon.rect.w, priv->balloon.rect.h); |
807 | cairo_clip (cr); |
808 | cairo_new_path (cr); /* current path is not |
809 | consumed by cairo_clip() */ |
810 | |
811 | priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data); |
812 | } |
813 | |
814 | cairo_destroy(cr); |
815 | |
816 | gtk_widget_queue_draw_area (GTK_WIDGET(map), |
817 | x0, y0, BALLOON_WIDTH, |
818 | BALLOON_HEIGHT + POINTER_HEIGHT); |
819 | #else |
820 | #warning "Balloon display lacks a non-cairo implementation!" |
821 | #endif |
822 | } |
823 | } |
824 | |
825 | /* the user clicked into the balloons main area. handle this */ |
826 | static void |
827 | osm_gps_map_handle_balloon_click(OsmGpsMap *map, gint x, gint y) |
828 | { |
829 | OsmGpsMapPrivate *priv = map->priv; |
830 | |
831 | if (!priv->balloon.valid) |
832 | return; |
833 | |
834 | /* check if the close button was clicked */ |
835 | if ((x > priv->balloon.rect.w - 2*CLOSE_BUTTON_RADIUS) && |
836 | (x < priv->balloon.rect.w) && |
837 | (y > 0) && (y < 2*CLOSE_BUTTON_RADIUS)) { |
838 | |
839 | priv->balloon.valid = FALSE; |
840 | osm_gps_map_map_redraw_idle(map); |
841 | } |
842 | } |
843 | |
844 | /* return true if balloon is being displayed and if */ |
845 | /* the given coordinate is within this balloon */ |
846 | static gboolean |
847 | osm_gps_map_in_balloon(OsmGpsMapPrivate *priv, gint x, gint y) |
848 | { |
849 | return (priv->balloon.valid && |
850 | (x > priv->balloon.rect.x) && |
851 | (x < priv->balloon.rect.x + priv->balloon.rect.w) && |
852 | (y > priv->balloon.rect.y) && |
853 | (y < priv->balloon.rect.y + priv->balloon.rect.h)); |
854 | } |
855 | |
856 | static void |
857 | osm_gps_map_blit_tile(OsmGpsMap *map, GdkPixbuf *pixbuf, int offset_x, int offset_y) |
858 | { |
859 | OsmGpsMapPrivate *priv = map->priv; |
860 | |
861 | g_debug("Queing redraw @ %d,%d (w:%d h:%d)", offset_x,offset_y, TILESIZE,TILESIZE); |
862 | |
863 | /* draw pixbuf onto pixmap */ |
864 | gdk_draw_pixbuf (priv->pixmap, |
865 | priv->gc_map, |
866 | pixbuf, |
867 | 0,0, |
868 | offset_x,offset_y, |
869 | TILESIZE,TILESIZE, |
870 | GDK_RGB_DITHER_NONE, 0, 0); |
871 | } |
872 | |
873 | /* libsoup-2.2 and libsoup-2.4 use different ways to store the body data */ |
874 | #ifdef LIBSOUP22 |
875 | #define soup_message_headers_append(a,b,c) soup_message_add_header(a,b,c) |
876 | #define MSG_RESPONSE_BODY(a) ((a)->response.body) |
877 | #define MSG_RESPONSE_LEN(a) ((a)->response.length) |
878 | #define MSG_RESPONSE_LEN_FORMAT "%u" |
879 | #else |
880 | #define MSG_RESPONSE_BODY(a) ((a)->response_body->data) |
881 | #define MSG_RESPONSE_LEN(a) ((a)->response_body->length) |
882 | #define MSG_RESPONSE_LEN_FORMAT "%lld" |
883 | #endif |
884 | |
885 | #ifdef LIBSOUP22 |
886 | static void |
887 | osm_gps_map_tile_download_complete (SoupMessage *msg, gpointer user_data) |
888 | #else |
889 | static void |
890 | osm_gps_map_tile_download_complete (SoupSession *session, SoupMessage *msg, gpointer user_data) |
891 | #endif |
892 | { |
893 | FILE *file; |
894 | tile_download_t *dl = (tile_download_t *)user_data; |
895 | OsmGpsMap *map = OSM_GPS_MAP(dl->map); |
896 | OsmGpsMapPrivate *priv = map->priv; |
897 | gboolean file_saved = FALSE; |
898 | |
899 | if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) |
900 | { |
901 | /* save tile into cachedir if one has been specified */ |
902 | if (priv->cache_dir) |
903 | { |
904 | if (g_mkdir_with_parents(dl->folder,0700) == 0) |
905 | { |
906 | file = g_fopen(dl->filename, "wb"); |
907 | if (file != NULL) |
908 | { |
909 | fwrite (MSG_RESPONSE_BODY(msg), 1, MSG_RESPONSE_LEN(msg), file); |
910 | file_saved = TRUE; |
911 | g_debug("Wrote "MSG_RESPONSE_LEN_FORMAT" bytes to %s", MSG_RESPONSE_LEN(msg), dl->filename); |
912 | fclose (file); |
913 | |
914 | } |
915 | } |
916 | else |
917 | { |
918 | g_warning("Error creating tile download directory: %s", dl->folder); |
919 | } |
920 | } |
921 | |
922 | if (dl->redraw) |
923 | { |
924 | GdkPixbuf *pixbuf = NULL; |
925 | |
926 | /* if the file was actually stored on disk, we can simply */ |
927 | /* load and decode it from that file */ |
928 | if (priv->cache_dir) |
929 | { |
930 | if (file_saved) |
931 | { |
932 | pixbuf = gdk_pixbuf_new_from_file (dl->filename, NULL); |
933 | } |
934 | } |
935 | else |
936 | { |
937 | GdkPixbufLoader *loader; |
938 | char *extension = strrchr (dl->filename, '.'); |
939 | |
940 | /* parse file directly from memory */ |
941 | if (extension) |
942 | { |
943 | loader = gdk_pixbuf_loader_new_with_type (extension+1, NULL); |
944 | if (!gdk_pixbuf_loader_write (loader, (unsigned char*)MSG_RESPONSE_BODY(msg), MSG_RESPONSE_LEN(msg), NULL)) |
945 | { |
946 | g_warning("Error: Decoding of image failed"); |
947 | } |
948 | gdk_pixbuf_loader_close(loader, NULL); |
949 | |
950 | pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); |
951 | |
952 | /* give up loader but keep the pixbuf */ |
953 | g_object_ref(pixbuf); |
954 | g_object_unref(loader); |
955 | } |
956 | else |
957 | { |
958 | g_warning("Error: Unable to determine image file format"); |
959 | } |
960 | } |
961 | |
962 | /* Store the tile into the cache */ |
963 | if (G_LIKELY (pixbuf)) |
964 | { |
965 | OsmCachedTile *tile = g_slice_new (OsmCachedTile); |
966 | tile->pixbuf = pixbuf; |
967 | tile->redraw_cycle = priv->redraw_cycle; |
968 | /* if the tile is already in the cache (it could be one |
969 | * rendered from another zoom level), it will be |
970 | * overwritten */ |
971 | g_hash_table_insert (priv->tile_cache, dl->filename, tile); |
972 | /* NULL-ify dl->filename so that it won't be freed, as |
973 | * we are using it as a key in the hash table */ |
974 | dl->filename = NULL; |
975 | } |
976 | osm_gps_map_map_redraw_idle (map); |
977 | } |
978 | g_hash_table_remove(priv->tile_queue, dl->uri); |
979 | |
980 | g_free(dl->uri); |
981 | g_free(dl->folder); |
982 | g_free(dl->filename); |
983 | g_free(dl); |
984 | } |
985 | else |
986 | { |
987 | g_warning("Error downloading tile: %d - %s", msg->status_code, msg->reason_phrase); |
988 | if (msg->status_code == SOUP_STATUS_NOT_FOUND) |
989 | { |
990 | g_hash_table_insert(priv->missing_tiles, dl->uri, NULL); |
991 | g_hash_table_remove(priv->tile_queue, dl->uri); |
992 | } |
993 | else if (msg->status_code == SOUP_STATUS_CANCELLED) |
994 | { |
995 | ;//application exiting |
996 | } |
997 | else |
998 | { |
999 | #ifdef LIBSOUP22 |
1000 | soup_session_requeue_message(dl->session, msg); |
1001 | #else |
1002 | soup_session_requeue_message(session, msg); |
1003 | #endif |
1004 | return; |
1005 | } |
1006 | } |
1007 | |
1008 | |
1009 | } |
1010 | |
1011 | static void |
1012 | osm_gps_map_download_tile (OsmGpsMap *map, int zoom, int x, int y, gboolean redraw) |
1013 | { |
1014 | SoupMessage *msg; |
1015 | OsmGpsMapPrivate *priv = map->priv; |
1016 | tile_download_t *dl = g_new0(tile_download_t,1); |
1017 | |
1018 | //calculate the uri to download |
1019 | dl->uri = replace_map_uri(map, priv->repo_uri, zoom, x, y); |
1020 | |
1021 | #ifdef LIBSOUP22 |
1022 | dl->session = priv->soup_session; |
1023 | #endif |
1024 | |
1025 | //check the tile has not already been queued for download, |
1026 | //or has been attempted, and its missing |
1027 | if (g_hash_table_lookup_extended(priv->tile_queue, dl->uri, NULL, NULL) || |
1028 | g_hash_table_lookup_extended(priv->missing_tiles, dl->uri, NULL, NULL) ) |
1029 | { |
1030 | g_debug("Tile already downloading (or missing)"); |
1031 | g_free(dl->uri); |
1032 | g_free(dl); |
1033 | } else { |
1034 | dl->folder = g_strdup_printf("%s%c%d%c%d%c", |
1035 | priv->cache_dir, G_DIR_SEPARATOR, |
1036 | zoom, G_DIR_SEPARATOR, |
1037 | x, G_DIR_SEPARATOR); |
1038 | dl->filename = g_strdup_printf("%s%c%d%c%d%c%d.%s", |
1039 | priv->cache_dir, G_DIR_SEPARATOR, |
1040 | zoom, G_DIR_SEPARATOR, |
1041 | x, G_DIR_SEPARATOR, |
1042 | y, |
1043 | priv->image_format); |
1044 | dl->map = map; |
1045 | dl->redraw = redraw; |
1046 | |
1047 | g_debug("Download tile: %d,%d z:%d\n\t%s --> %s", x, y, zoom, dl->uri, dl->filename); |
1048 | |
1049 | msg = soup_message_new (SOUP_METHOD_GET, dl->uri); |
1050 | if (msg) { |
1051 | if (priv->the_google) { |
1052 | //Set maps.google.com as the referrer |
1053 | g_debug("Setting Google Referrer"); |
1054 | soup_message_headers_append(msg->request_headers, "Referer", "http://maps.google.com/"); |
1055 | //For google satelite also set the appropriate cookie value |
1056 | if (priv->uri_format & URI_HAS_Q) { |
1057 | const char *cookie = g_getenv("GOOGLE_COOKIE"); |
1058 | if (cookie) { |
1059 | g_debug("Adding Google Cookie"); |
1060 | soup_message_headers_append(msg->request_headers, "Cookie", cookie); |
1061 | } |
1062 | } |
1063 | } |
1064 | |
1065 | g_hash_table_insert (priv->tile_queue, dl->uri, msg); |
1066 | soup_session_queue_message (priv->soup_session, msg, osm_gps_map_tile_download_complete, dl); |
1067 | } else { |
1068 | g_warning("Could not create soup message"); |
1069 | g_free(dl->uri); |
1070 | g_free(dl->folder); |
1071 | g_free(dl->filename); |
1072 | g_free(dl); |
1073 | } |
1074 | } |
1075 | } |
1076 | |
1077 | static GdkPixbuf * |
1078 | osm_gps_map_load_cached_tile (OsmGpsMap *map, int zoom, int x, int y) |
1079 | { |
1080 | OsmGpsMapPrivate *priv = map->priv; |
1081 | gchar *filename; |
1082 | GdkPixbuf *pixbuf = NULL; |
1083 | OsmCachedTile *tile; |
1084 | |
1085 | filename = g_strdup_printf("%s%c%d%c%d%c%d.%s", |
1086 | priv->cache_dir, G_DIR_SEPARATOR, |
1087 | zoom, G_DIR_SEPARATOR, |
1088 | x, G_DIR_SEPARATOR, |
1089 | y, |
1090 | priv->image_format); |
1091 | |
1092 | tile = g_hash_table_lookup (priv->tile_cache, filename); |
1093 | if (tile) |
1094 | { |
1095 | g_free (filename); |
1096 | } |
1097 | else |
1098 | { |
1099 | pixbuf = gdk_pixbuf_new_from_file (filename, NULL); |
1100 | if (pixbuf) |
1101 | { |
1102 | tile = g_slice_new (OsmCachedTile); |
1103 | tile->pixbuf = pixbuf; |
1104 | g_hash_table_insert (priv->tile_cache, filename, tile); |
1105 | } |
1106 | } |
1107 | |
1108 | /* set/update the redraw_cycle timestamp on the tile */ |
1109 | if (tile) |
1110 | { |
1111 | tile->redraw_cycle = priv->redraw_cycle; |
1112 | pixbuf = g_object_ref (tile->pixbuf); |
1113 | } |
1114 | |
1115 | return pixbuf; |
1116 | } |
1117 | |
1118 | static GdkPixbuf * |
1119 | osm_gps_map_find_bigger_tile (OsmGpsMap *map, int zoom, int x, int y, |
1120 | int *zoom_found) |
1121 | { |
1122 | GdkPixbuf *pixbuf; |
1123 | int next_zoom, next_x, next_y; |
1124 | |
1125 | if (zoom == 0) return NULL; |
1126 | next_zoom = zoom - 1; |
1127 | next_x = x / 2; |
1128 | next_y = y / 2; |
1129 | pixbuf = osm_gps_map_load_cached_tile (map, next_zoom, next_x, next_y); |
1130 | if (pixbuf) |
1131 | *zoom_found = next_zoom; |
1132 | else |
1133 | pixbuf = osm_gps_map_find_bigger_tile (map, next_zoom, next_x, next_y, |
1134 | zoom_found); |
1135 | return pixbuf; |
1136 | } |
1137 | |
1138 | static GdkPixbuf * |
1139 | osm_gps_map_render_missing_tile_upscaled (OsmGpsMap *map, int zoom, |
1140 | int x, int y) |
1141 | { |
1142 | GdkPixbuf *pixbuf, *big, *area; |
1143 | int zoom_big, zoom_diff, area_size, area_x, area_y; |
1144 | int modulo; |
1145 | |
1146 | big = osm_gps_map_find_bigger_tile (map, zoom, x, y, &zoom_big); |
1147 | if (!big) return NULL; |
1148 | |
1149 | g_debug ("Found bigger tile (zoom = %d, wanted = %d)", zoom_big, zoom); |
1150 | |
1151 | /* get a Pixbuf for the area to magnify */ |
1152 | zoom_diff = zoom - zoom_big; |
1153 | area_size = TILESIZE >> zoom_diff; |
1154 | modulo = 1 << zoom_diff; |
1155 | area_x = (x % modulo) * area_size; |
1156 | area_y = (y % modulo) * area_size; |
1157 | area = gdk_pixbuf_new_subpixbuf (big, area_x, area_y, |
1158 | area_size, area_size); |
1159 | g_object_unref (big); |
1160 | pixbuf = gdk_pixbuf_scale_simple (area, TILESIZE, TILESIZE, |
1161 | GDK_INTERP_NEAREST); |
1162 | g_object_unref (area); |
1163 | return pixbuf; |
1164 | } |
1165 | |
1166 | static GdkPixbuf * |
1167 | osm_gps_map_render_missing_tile (OsmGpsMap *map, int zoom, int x, int y) |
1168 | { |
1169 | /* maybe TODO: render from downscaled tiles, if the following fails */ |
1170 | return osm_gps_map_render_missing_tile_upscaled (map, zoom, x, y); |
1171 | } |
1172 | |
1173 | static void |
1174 | osm_gps_map_load_tile (OsmGpsMap *map, int zoom, int x, int y, int offset_x, int offset_y) |
1175 | { |
1176 | OsmGpsMapPrivate *priv = map->priv; |
1177 | gchar *filename; |
1178 | GdkPixbuf *pixbuf; |
1179 | |
1180 | g_debug("Load tile %d,%d (%d,%d) z:%d", x, y, offset_x, offset_y, zoom); |
1181 | |
1182 | if (priv->map_source == OSM_GPS_MAP_SOURCE_NULL) { |
1183 | osm_gps_map_blit_tile(map, priv->null_tile, offset_x,offset_y); |
1184 | return; |
1185 | } |
1186 | |
1187 | filename = g_strdup_printf("%s%c%d%c%d%c%d.%s", |
1188 | priv->cache_dir, G_DIR_SEPARATOR, |
1189 | zoom, G_DIR_SEPARATOR, |
1190 | x, G_DIR_SEPARATOR, |
1191 | y, |
1192 | priv->image_format); |
1193 | |
1194 | /* try to get file from internal cache first */ |
1195 | if(!(pixbuf = osm_gps_map_load_cached_tile(map, zoom, x, y))) |
1196 | pixbuf = gdk_pixbuf_new_from_file (filename, NULL); |
1197 | |
1198 | if(pixbuf) |
1199 | { |
1200 | g_debug("Found tile %s", filename); |
1201 | osm_gps_map_blit_tile(map, pixbuf, offset_x,offset_y); |
1202 | g_object_unref (pixbuf); |
1203 | } |
1204 | else |
1205 | { |
1206 | if (priv->map_auto_download) |
1207 | osm_gps_map_download_tile(map, zoom, x, y, TRUE); |
1208 | |
1209 | /* try to render the tile by scaling cached tiles from other zoom |
1210 | * levels */ |
1211 | pixbuf = osm_gps_map_render_missing_tile (map, zoom, x, y); |
1212 | if (pixbuf) |
1213 | { |
1214 | gdk_draw_pixbuf (priv->pixmap, |
1215 | priv->gc_map, |
1216 | pixbuf, |
1217 | 0,0, |
1218 | offset_x,offset_y, |
1219 | TILESIZE,TILESIZE, |
1220 | GDK_RGB_DITHER_NONE, 0, 0); |
1221 | g_object_unref (pixbuf); |
1222 | } |
1223 | else |
1224 | { |
1225 | //prevent some artifacts when drawing not yet loaded areas. |
1226 | gdk_draw_rectangle (priv->pixmap, |
1227 | GTK_WIDGET(map)->style->white_gc, |
1228 | TRUE, offset_x, offset_y, TILESIZE, TILESIZE); |
1229 | } |
1230 | } |
1231 | g_free(filename); |
1232 | } |
1233 | |
1234 | static void |
1235 | osm_gps_map_fill_tiles_pixel (OsmGpsMap *map) |
1236 | { |
1237 | OsmGpsMapPrivate *priv = map->priv; |
1238 | int i,j, width, height, tile_x0, tile_y0, tiles_nx, tiles_ny; |
1239 | int offset_xn = 0; |
1240 | int offset_yn = 0; |
1241 | int offset_x; |
1242 | int offset_y; |
1243 | |
1244 | g_debug("Fill tiles: %d,%d z:%d", priv->map_x, priv->map_y, priv->map_zoom); |
1245 | |
1246 | offset_x = - priv->map_x % TILESIZE; |
1247 | offset_y = - priv->map_y % TILESIZE; |
1248 | if (offset_x > 0) offset_x -= TILESIZE; |
1249 | if (offset_y > 0) offset_y -= TILESIZE; |
1250 | |
1251 | offset_xn = offset_x + EXTRA_BORDER; |
1252 | offset_yn = offset_y + EXTRA_BORDER; |
1253 | |
1254 | width = GTK_WIDGET(map)->allocation.width; |
1255 | height = GTK_WIDGET(map)->allocation.height; |
1256 | |
1257 | tiles_nx = (width - offset_x) / TILESIZE + 1; |
1258 | tiles_ny = (height - offset_y) / TILESIZE + 1; |
1259 | |
1260 | tile_x0 = floor((float)priv->map_x / (float)TILESIZE); |
1261 | tile_y0 = floor((float)priv->map_y / (float)TILESIZE); |
1262 | |
1263 | //TODO: implement wrap around |
1264 | for (i=tile_x0; i<(tile_x0+tiles_nx);i++) |
1265 | { |
1266 | for (j=tile_y0; j<(tile_y0+tiles_ny); j++) |
1267 | { |
1268 | if( j<0 || i<0 || i>=exp(priv->map_zoom * M_LN2) || j>=exp(priv->map_zoom * M_LN2)) |
1269 | { |
1270 | gdk_draw_rectangle (priv->pixmap, |
1271 | GTK_WIDGET(map)->style->white_gc, |
1272 | TRUE, |
1273 | offset_xn, offset_yn, |
1274 | TILESIZE,TILESIZE); |
1275 | } |
1276 | else |
1277 | { |
1278 | osm_gps_map_load_tile(map, |
1279 | priv->map_zoom, |
1280 | i,j, |
1281 | offset_xn,offset_yn); |
1282 | } |
1283 | offset_yn += TILESIZE; |
1284 | } |
1285 | offset_xn += TILESIZE; |
1286 | offset_yn = offset_y + EXTRA_BORDER; |
1287 | } |
1288 | } |
1289 | |
1290 | static void |
1291 | osm_gps_map_print_track (OsmGpsMap *map, GSList *trackpoint_list) |
1292 | { |
1293 | OsmGpsMapPrivate *priv = map->priv; |
1294 | |
1295 | GSList *list; |
1296 | int x,y; |
1297 | int min_x = 0,min_y = 0,max_x = 0,max_y = 0; |
1298 | int lw = priv->ui_gps_track_width; |
1299 | int map_x0, map_y0; |
1300 | #ifdef USE_CAIRO |
1301 | cairo_t *cr; |
1302 | #else |
1303 | int last_x = 0, last_y = 0; |
1304 | GdkColor color; |
1305 | GdkGC *gc; |
1306 | #endif |
1307 | |
1308 | #ifdef USE_CAIRO |
1309 | cr = gdk_cairo_create(priv->pixmap); |
1310 | cairo_set_line_width (cr, lw); |
1311 | cairo_set_source_rgba (cr, 60000.0/65535.0, 0.0, 0.0, 0.6); |
1312 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
1313 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
1314 | #else |
1315 | gc = gdk_gc_new(priv->pixmap); |
1316 | color.green = 0; |
1317 | color.blue = 0; |
1318 | color.red = 60000; |
1319 | gdk_gc_set_rgb_fg_color(gc, &color); |
1320 | gdk_gc_set_line_attributes(gc, lw, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND); |
1321 | #endif |
1322 | |
1323 | map_x0 = priv->map_x - EXTRA_BORDER; |
1324 | map_y0 = priv->map_y - EXTRA_BORDER; |
1325 | for(list = trackpoint_list; list != NULL; list = list->next) |
1326 | { |
1327 | coord_t *tp = list->data; |
1328 | |
1329 | x = lon2pixel(priv->map_zoom, tp->rlon) - map_x0; |
1330 | y = lat2pixel(priv->map_zoom, tp->rlat) - map_y0; |
1331 | |
1332 | // first time through loop |
1333 | if (list == trackpoint_list) { |
1334 | #ifdef USE_CAIRO |
1335 | cairo_move_to(cr, x, y); |
1336 | #else |
1337 | last_x = x; |
1338 | last_y = y; |
1339 | #endif |
1340 | } |
1341 | |
1342 | #ifdef USE_CAIRO |
1343 | cairo_line_to(cr, x, y); |
1344 | #else |
1345 | gdk_draw_line (priv->pixmap, gc, x, y, last_x, last_y); |
1346 | last_x = x; |
1347 | last_y = y; |
1348 | #endif |
1349 | |
1350 | max_x = MAX(x,max_x); |
1351 | min_x = MIN(x,min_x); |
1352 | max_y = MAX(y,max_y); |
1353 | min_y = MIN(y,min_y); |
1354 | } |
1355 | |
1356 | gtk_widget_queue_draw_area ( |
1357 | GTK_WIDGET(map), |
1358 | min_x - lw, |
1359 | min_y - lw, |
1360 | max_x + (lw * 2), |
1361 | max_y + (lw * 2)); |
1362 | |
1363 | #ifdef USE_CAIRO |
1364 | cairo_stroke(cr); |
1365 | cairo_destroy(cr); |
1366 | #else |
1367 | g_object_unref(gc); |
1368 | #endif |
1369 | } |
1370 | |
1371 | /* Prints the gps trip history, and any other tracks */ |
1372 | static void |
1373 | osm_gps_map_print_tracks (OsmGpsMap *map) |
1374 | { |
1375 | OsmGpsMapPrivate *priv = map->priv; |
1376 | |
1377 | if (priv->show_trip_history) |
1378 | osm_gps_map_print_track (map, priv->trip_history); |
1379 | |
1380 | if (priv->tracks) |
1381 | { |
1382 | GSList* tmp = priv->tracks; |
1383 | while (tmp != NULL) |
1384 | { |
1385 | osm_gps_map_print_track (map, tmp->data); |
1386 | tmp = g_slist_next(tmp); |
1387 | } |
1388 | } |
1389 | } |
1390 | |
1391 | static gboolean |
1392 | osm_gps_map_purge_cache_check(gpointer key, gpointer value, gpointer user) |
1393 | { |
1394 | return (((OsmCachedTile*)value)->redraw_cycle != ((OsmGpsMapPrivate*)user)->redraw_cycle); |
1395 | } |
1396 | |
1397 | static void |
1398 | osm_gps_map_purge_cache (OsmGpsMap *map) |
1399 | { |
1400 | OsmGpsMapPrivate *priv = map->priv; |
1401 | |
1402 | if (g_hash_table_size (priv->tile_cache) < priv->max_tile_cache_size) |
1403 | return; |
1404 | |
1405 | /* run through the cache, and remove the tiles which have not been used |
1406 | * during the last redraw operation */ |
1407 | g_hash_table_foreach_remove(priv->tile_cache, osm_gps_map_purge_cache_check, priv); |
1408 | } |
1409 | |
1410 | #ifdef ENABLE_OSD |
1411 | /* position and extent of bounding box */ |
1412 | #define OSD_X (10) |
1413 | #define OSD_Y (10) |
1414 | #define OSD_W (80+5) |
1415 | #define OSD_H (120+5) |
1416 | |
1417 | static void |
1418 | osm_gps_map_draw_osd_controls (OsmGpsMap *map, gint xoffset, gint yoffset) |
1419 | { |
1420 | /* xyz */ |
1421 | OsmGpsMapPrivate *priv = map->priv; |
1422 | |
1423 | /* backup previous contents */ |
1424 | if(!priv->osd.backup) |
1425 | priv->osd.backup = gdk_pixmap_new(priv->pixmap, OSD_W, OSD_H, -1); |
1426 | |
1427 | gint x = OSD_X + EXTRA_BORDER + xoffset; |
1428 | gint y = OSD_Y + EXTRA_BORDER + yoffset; |
1429 | |
1430 | /* create backup of background */ |
1431 | gdk_draw_drawable(priv->osd.backup, |
1432 | GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))], |
1433 | priv->pixmap, x, y, 0, 0, OSD_W, OSD_H); |
1434 | priv->osd.backup_x = x; |
1435 | priv->osd.backup_y = y; |
1436 | |
1437 | #if 0 |
1438 | /* create pixbuf for osd */ |
1439 | if(!priv->osd.pixbuf) |
1440 | priv->osd.pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
1441 | TRUE, 8, OSD_W, OSD_H); |
1442 | cairo_surface_t *surface = |
1443 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W, OSD_H); |
1444 | |
1445 | /* fill with transparency */ |
1446 | { |
1447 | cairo_t *cr = cairo_create(surface); |
1448 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
1449 | cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0); |
1450 | cairo_paint(cr); |
1451 | cairo_destroy(cr); |
1452 | } |
1453 | #endif |
1454 | |
1455 | |
1456 | #ifdef USE_CAIRO |
1457 | // cairo_t *cr = cairo_create(surface); |
1458 | cairo_t *cr = gdk_cairo_create(priv->pixmap); |
1459 | |
1460 | #define RAD 40 |
1461 | #define TIP 35 |
1462 | #define LEN 15 |
1463 | #define WID 15 |
1464 | |
1465 | /* --------- the direction "pad" shape and shadow ----------- */ |
1466 | cairo_arc (cr, x+RAD+5, y+RAD+5, RAD, 0, 2 * M_PI); |
1467 | cairo_set_source_rgba (cr, 0, 0, 0, 0.2); |
1468 | cairo_fill (cr); |
1469 | cairo_stroke (cr); |
1470 | |
1471 | cairo_arc (cr, x+RAD, y+RAD, RAD, 0, 2 * M_PI); |
1472 | cairo_set_source_rgb (cr, 1, 1, 1); |
1473 | cairo_fill_preserve (cr); |
1474 | cairo_set_source_rgb (cr, 0.6, 0.6, 1); |
1475 | cairo_set_line_width (cr, 1); |
1476 | cairo_stroke (cr); |
1477 | |
1478 | /* ---------- the zoom pad shape and shadow -------------- */ |
1479 | cairo_move_to (cr, x, y+2*RAD); |
1480 | cairo_line_to (cr, x+2*RAD-10, y+2*RAD); |
1481 | cairo_curve_to (cr, x+2*RAD, y+2*RAD, |
1482 | x+2*RAD, y+3*RAD, |
1483 | x+2*RAD-10, y+3*RAD); |
1484 | cairo_close_path (cr); |
1485 | |
1486 | |
1487 | cairo_set_source_rgba (cr, 1, 1, 1, 1.0); |
1488 | cairo_fill (cr); |
1489 | cairo_stroke (cr); |
1490 | |
1491 | |
1492 | /* left arrow/triangle */ |
1493 | cairo_move_to (cr, x+RAD-TIP, y+RAD); |
1494 | cairo_rel_line_to (cr, +LEN, -WID/2); |
1495 | cairo_rel_line_to (cr, 0, +WID); |
1496 | cairo_rel_line_to (cr, -LEN, -WID/2); |
1497 | cairo_close_path (cr); |
1498 | |
1499 | /* right arrow/triangle */ |
1500 | cairo_move_to (cr, x+RAD+TIP, y+RAD); |
1501 | cairo_rel_line_to (cr, -LEN, -WID/2); |
1502 | cairo_rel_line_to (cr, 0, +WID); |
1503 | cairo_rel_line_to (cr, +LEN, -WID/2); |
1504 | cairo_close_path (cr); |
1505 | |
1506 | /* top arrow/triangle */ |
1507 | cairo_move_to (cr, x+RAD, y+RAD-TIP); |
1508 | cairo_rel_line_to (cr, -WID/2, +LEN); |
1509 | cairo_rel_line_to (cr, +WID, 0); |
1510 | cairo_rel_line_to (cr, -WID/2, -LEN); |
1511 | cairo_close_path (cr); |
1512 | |
1513 | /* bottom arrow/triangle */ |
1514 | cairo_move_to (cr, x+RAD, y+RAD+TIP); |
1515 | cairo_rel_line_to (cr, -WID/2, -LEN); |
1516 | cairo_rel_line_to (cr, +WID, 0); |
1517 | cairo_rel_line_to (cr, -WID/2, +LEN); |
1518 | cairo_close_path (cr); |
1519 | |
1520 | cairo_set_source_rgb (cr, 0.6, 0.6, 1); |
1521 | cairo_fill_preserve (cr); |
1522 | cairo_set_line_width (cr, 0); |
1523 | cairo_set_source_rgba (cr, 0, 0, 0, 1); |
1524 | cairo_stroke (cr); |
1525 | |
1526 | |
1527 | cairo_destroy(cr); |
1528 | |
1529 | #else |
1530 | #warning "OSD control display lacks a non-cairo implementation!" |
1531 | #endif |
1532 | } |
1533 | |
1534 | static void |
1535 | osm_gps_map_osd_restore (OsmGpsMap *map) |
1536 | { |
1537 | OsmGpsMapPrivate *priv = map->priv; |
1538 | |
1539 | /* restore backup of previous contents */ |
1540 | if(priv->osd.backup) { |
1541 | /* create backup of background */ |
1542 | gdk_draw_drawable(priv->pixmap, |
1543 | GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))], |
1544 | priv->osd.backup, 0, 0, |
1545 | priv->osd.backup_x, priv->osd.backup_y, OSD_W, OSD_H); |
1546 | } |
1547 | } |
1548 | |
1549 | #endif |
1550 | |
1551 | static gboolean |
1552 | osm_gps_map_map_redraw (OsmGpsMap *map) |
1553 | { |
1554 | OsmGpsMapPrivate *priv = map->priv; |
1555 | |
1556 | priv->idle_map_redraw = 0; |
1557 | |
1558 | /* the motion_notify handler uses priv->pixmap to redraw the area; if we |
1559 | * change it while we are dragging, we will end up showing it in the wrong |
1560 | * place. This could be fixed by carefully recompute the coordinates, but |
1561 | * for now it's easier just to disable redrawing the map while dragging */ |
1562 | if (priv->dragging) |
1563 | return FALSE; |
1564 | |
1565 | priv->redraw_cycle++; |
1566 | |
1567 | /* draw white background to initialise pixmap */ |
1568 | gdk_draw_rectangle ( |
1569 | priv->pixmap, |
1570 | GTK_WIDGET(map)->style->white_gc, |
1571 | TRUE, |
1572 | 0, 0, |
1573 | GTK_WIDGET(map)->allocation.width + EXTRA_BORDER * 2, |
1574 | GTK_WIDGET(map)->allocation.height + EXTRA_BORDER * 2); |
1575 | |
1576 | osm_gps_map_fill_tiles_pixel(map); |
1577 | |
1578 | osm_gps_map_print_tracks(map); |
1579 | osm_gps_map_draw_gps_point(map); |
1580 | osm_gps_map_print_images(map); |
1581 | osm_gps_map_draw_balloon_int(map); |
1582 | #ifdef ENABLE_OSD |
1583 | osm_gps_map_draw_osd_controls(map, 0, 0); |
1584 | #endif |
1585 | |
1586 | //osm_gps_map_osd_speed(map, 1.5); |
1587 | osm_gps_map_purge_cache(map); |
1588 | gtk_widget_queue_draw (GTK_WIDGET (map)); |
1589 | |
1590 | return FALSE; |
1591 | } |
1592 | |
1593 | static void |
1594 | osm_gps_map_map_redraw_idle (OsmGpsMap *map) |
1595 | { |
1596 | OsmGpsMapPrivate *priv = map->priv; |
1597 | |
1598 | if (priv->idle_map_redraw == 0) |
1599 | priv->idle_map_redraw = g_idle_add ((GSourceFunc)osm_gps_map_map_redraw, map); |
1600 | } |
1601 | |
1602 | static void |
1603 | osm_gps_map_init (OsmGpsMap *object) |
1604 | { |
1605 | OsmGpsMapPrivate *priv; |
1606 | |
1607 | priv = G_TYPE_INSTANCE_GET_PRIVATE (object, OSM_TYPE_GPS_MAP, OsmGpsMapPrivate); |
1608 | object->priv = priv; |
1609 | |
1610 | priv->pixmap = NULL; |
1611 | |
1612 | priv->trip_history = NULL; |
1613 | priv->gps = g_new0(coord_t, 1); |
1614 | priv->gps_valid = FALSE; |
1615 | |
1616 | priv->balloon.coo = g_new0(coord_t, 1); |
1617 | priv->balloon.valid = FALSE; |
1618 | priv->balloon.cb = NULL; |
1619 | |
1620 | #ifdef ENABLE_OSD |
1621 | priv->osd.backup = NULL; |
1622 | #endif |
1623 | |
1624 | priv->tracks = NULL; |
1625 | priv->images = NULL; |
1626 | |
1627 | priv->drag_counter = 0; |
1628 | priv->drag_mouse_dx = 0; |
1629 | priv->drag_mouse_dy = 0; |
1630 | priv->drag_start_mouse_x = 0; |
1631 | priv->drag_start_mouse_y = 0; |
1632 | |
1633 | priv->uri_format = 0; |
1634 | priv->the_google = FALSE; |
1635 | |
1636 | priv->map_source = -1; |
1637 | |
1638 | #ifndef LIBSOUP22 |
1639 | //Change naumber of concurrent connections option? |
1640 | priv->soup_session = soup_session_async_new_with_options( |
1641 | SOUP_SESSION_USER_AGENT, |
1642 | "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11", |
1643 | NULL); |
1644 | #else |
1645 | /* libsoup-2.2 seems not to be able to set the user agent */ |
1646 | priv->soup_session = soup_session_async_new(); |
1647 | #endif |
1648 | |
1649 | //Hash table which maps tile d/l URIs to SoupMessage requests |
1650 | priv->tile_queue = g_hash_table_new (g_str_hash, g_str_equal); |
1651 | |
1652 | //Some mapping providers (Google) have varying degrees of tiles at multiple |
1653 | //zoom levels |
1654 | priv->missing_tiles = g_hash_table_new (g_str_hash, g_str_equal); |
1655 | |
1656 | /* memory cache for most recently used tiles */ |
1657 | priv->tile_cache = g_hash_table_new_full (g_str_hash, g_str_equal, |
1658 | g_free, (GDestroyNotify)cached_tile_free); |
1659 | priv->max_tile_cache_size = 20; |
1660 | |
1661 | gtk_widget_add_events (GTK_WIDGET (object), |
1662 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | |
1663 | GDK_POINTER_MOTION_MASK | |
1664 | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK); |
1665 | GTK_WIDGET_SET_FLAGS (object, GTK_CAN_FOCUS); |
1666 | |
1667 | g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, my_log_handler, NULL); |
1668 | } |
1669 | |
1670 | #ifndef G_CHECKSUM_MD5 |
1671 | /* simple hash algorithm hack if md5 is not present */ |
1672 | static char *simple_hash(char *str) { |
1673 | union { |
1674 | char str[4]; |
1675 | gulong val; |
1676 | } hash = { .val = 0x55555555 }; |
1677 | |
1678 | while(*str) { |
1679 | hash.str[(int)str & 3] ^= *str; |
1680 | str++; |
1681 | } |
1682 | return g_strdup_printf("%08lX", hash.val); |
1683 | } |
1684 | #endif |
1685 | |
1686 | static GObject * |
1687 | osm_gps_map_constructor (GType gtype, guint n_properties, GObjectConstructParam *properties) |
1688 | { |
1689 | GObject *object; |
1690 | OsmGpsMapPrivate *priv; |
1691 | OsmGpsMap *map; |
1692 | const char *uri; |
1693 | |
1694 | //Always chain up to the parent constructor |
1695 | object = G_OBJECT_CLASS(osm_gps_map_parent_class)->constructor(gtype, n_properties, properties); |
1696 | map = OSM_GPS_MAP(object); |
1697 | priv = OSM_GPS_MAP_PRIVATE(object); |
1698 | |
1699 | //user can specify a map source ID, or a repo URI as the map source |
1700 | uri = osm_gps_map_source_get_repo_uri(OSM_GPS_MAP_SOURCE_NULL); |
1701 | if ( (priv->map_source == 0) || (strcmp(priv->repo_uri, uri) == 0) ) { |
1702 | g_debug("Using null source"); |
1703 | priv->map_source = OSM_GPS_MAP_SOURCE_NULL; |
1704 | |
1705 | priv->null_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 256, 256); |
1706 | gdk_pixbuf_fill(priv->null_tile, 0xcccccc00); |
1707 | } |
1708 | else if (priv->map_source >= 0) { |
1709 | //check if the source given is valid |
1710 | uri = osm_gps_map_source_get_repo_uri(priv->map_source); |
1711 | if (uri) { |
1712 | g_debug("Setting map source from ID"); |
1713 | g_free(priv->repo_uri); |
1714 | |
1715 | priv->repo_uri = g_strdup(uri); |
1716 | priv->image_format = g_strdup( |
1717 | osm_gps_map_source_get_image_format(priv->map_source)); |
1718 | priv->max_zoom = osm_gps_map_source_get_max_zoom(priv->map_source); |
1719 | priv->min_zoom = osm_gps_map_source_get_min_zoom(priv->map_source); |
1720 | } |
1721 | } |
1722 | |
1723 | if (!priv->cache_dir_is_full_path) { |
1724 | #ifdef G_CHECKSUM_MD5 |
1725 | char *md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, priv->repo_uri, -1); |
1726 | #else |
1727 | char *md5 = simple_hash(priv->repo_uri); |
1728 | #endif |
1729 | |
1730 | if (priv->cache_dir) { |
1731 | char *old = priv->cache_dir; |
1732 | //the new cachedir is the given cache dir + the md5 of the repo_uri |
1733 | priv->cache_dir = g_strdup_printf("%s%c%s", old, G_DIR_SEPARATOR, md5); |
1734 | g_debug("Adjusting cache dir %s -> %s", old, priv->cache_dir); |
1735 | g_free(old); |
1736 | } else { |
1737 | //the new cachedir is the current dir + the md5 of the repo_uri |
1738 | priv->cache_dir = g_strdup(md5); |
1739 | } |
1740 | |
1741 | g_free(md5); |
1742 | } |
1743 | |
1744 | inspect_map_uri(map); |
1745 | |
1746 | return object; |
1747 | } |
1748 | |
1749 | static void |
1750 | osm_gps_map_dispose (GObject *object) |
1751 | { |
1752 | OsmGpsMap *map = OSM_GPS_MAP(object); |
1753 | OsmGpsMapPrivate *priv = map->priv; |
1754 | |
1755 | if (priv->is_disposed) |
1756 | return; |
1757 | |
1758 | priv->is_disposed = TRUE; |
1759 | |
1760 | soup_session_abort(priv->soup_session); |
1761 | g_object_unref(priv->soup_session); |
1762 | |
1763 | g_hash_table_destroy(priv->tile_queue); |
1764 | g_hash_table_destroy(priv->missing_tiles); |
1765 | g_hash_table_destroy(priv->tile_cache); |
1766 | |
1767 | osm_gps_map_free_images(map); |
1768 | |
1769 | if(priv->pixmap) |
1770 | g_object_unref (priv->pixmap); |
1771 | |
1772 | if (priv->null_tile) |
1773 | g_object_unref (priv->null_tile); |
1774 | |
1775 | if(priv->gc_map) |
1776 | g_object_unref(priv->gc_map); |
1777 | |
1778 | if (priv->idle_map_redraw != 0) |
1779 | g_source_remove (priv->idle_map_redraw); |
1780 | |
1781 | g_free(priv->gps); |
1782 | g_free(priv->balloon.coo); |
1783 | |
1784 | #ifdef ENABLE_OSD |
1785 | if (priv->osd.backup) |
1786 | g_object_unref(priv->osd.backup); |
1787 | #endif |
1788 | |
1789 | G_OBJECT_CLASS (osm_gps_map_parent_class)->dispose (object); |
1790 | } |
1791 | |
1792 | static void |
1793 | osm_gps_map_finalize (GObject *object) |
1794 | { |
1795 | OsmGpsMap *map = OSM_GPS_MAP(object); |
1796 | OsmGpsMapPrivate *priv = map->priv; |
1797 | |
1798 | g_free(priv->cache_dir); |
1799 | g_free(priv->repo_uri); |
1800 | g_free(priv->image_format); |
1801 | |
1802 | osm_gps_map_free_trip(map); |
1803 | osm_gps_map_free_tracks(map); |
1804 | |
1805 | G_OBJECT_CLASS (osm_gps_map_parent_class)->finalize (object); |
1806 | } |
1807 | |
1808 | static void |
1809 | osm_gps_map_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) |
1810 | { |
1811 | g_return_if_fail (OSM_IS_GPS_MAP (object)); |
1812 | OsmGpsMap *map = OSM_GPS_MAP(object); |
1813 | OsmGpsMapPrivate *priv = map->priv; |
1814 | |
1815 | switch (prop_id) |
1816 | { |
1817 | case PROP_AUTO_CENTER: |
1818 | priv->map_auto_center = g_value_get_boolean (value); |
1819 | break; |
1820 | case PROP_RECORD_TRIP_HISTORY: |
1821 | priv->record_trip_history = g_value_get_boolean (value); |
1822 | break; |
1823 | case PROP_SHOW_TRIP_HISTORY: |
1824 | priv->show_trip_history = g_value_get_boolean (value); |
1825 | break; |
1826 | case PROP_AUTO_DOWNLOAD: |
1827 | priv->map_auto_download = g_value_get_boolean (value); |
1828 | break; |
1829 | case PROP_REPO_URI: |
1830 | priv->repo_uri = g_value_dup_string (value); |
1831 | break; |
1832 | case PROP_PROXY_URI: |
1833 | if ( g_value_get_string(value) ) { |
1834 | priv->proxy_uri = g_value_dup_string (value); |
1835 | g_debug("Setting proxy server: %s", priv->proxy_uri); |
1836 | |
1837 | #ifndef LIBSOUP22 |
1838 | GValue val = {0}; |
1839 | |
1840 | SoupURI* uri = soup_uri_new(priv->proxy_uri); |
1841 | g_value_init(&val, SOUP_TYPE_URI); |
1842 | g_value_take_boxed(&val, uri); |
1843 | |
1844 | g_object_set_property(G_OBJECT(priv->soup_session),SOUP_SESSION_PROXY_URI,&val); |
1845 | #else |
1846 | SoupUri* uri = soup_uri_new(priv->proxy_uri); |
1847 | g_object_set(G_OBJECT(priv->soup_session), SOUP_SESSION_PROXY_URI, uri, NULL); |
1848 | #endif |
1849 | } else |
1850 | priv->proxy_uri = NULL; |
1851 | |
1852 | break; |
1853 | case PROP_TILE_CACHE_DIR: |
1854 | if ( g_value_get_string(value) ) |
1855 | priv->cache_dir = g_value_dup_string (value); |
1856 | break; |
1857 | case PROP_TILE_CACHE_DIR_IS_FULL_PATH: |
1858 | priv->cache_dir_is_full_path = g_value_get_boolean (value); |
1859 | break; |
1860 | case PROP_ZOOM: |
1861 | priv->map_zoom = g_value_get_int (value); |
1862 | break; |
1863 | case PROP_MAX_ZOOM: |
1864 | priv->max_zoom = g_value_get_int (value); |
1865 | break; |
1866 | case PROP_MIN_ZOOM: |
1867 | priv->min_zoom = g_value_get_int (value); |
1868 | break; |
1869 | case PROP_MAP_X: |
1870 | priv->map_x = g_value_get_int (value); |
1871 | priv->center_coord_set = FALSE; |
1872 | break; |
1873 | case PROP_MAP_Y: |
1874 | priv->map_y = g_value_get_int (value); |
1875 | priv->center_coord_set = FALSE; |
1876 | break; |
1877 | case PROP_GPS_TRACK_WIDTH: |
1878 | priv->ui_gps_track_width = g_value_get_int (value); |
1879 | break; |
1880 | case PROP_GPS_POINT_R1: |
1881 | priv->ui_gps_point_inner_radius = g_value_get_int (value); |
1882 | break; |
1883 | case PROP_GPS_POINT_R2: |
1884 | priv->ui_gps_point_outer_radius = g_value_get_int (value); |
1885 | break; |
1886 | case PROP_MAP_SOURCE: |
1887 | priv->map_source = g_value_get_int (value); |
1888 | break; |
1889 | case PROP_IMAGE_FORMAT: |
1890 | priv->image_format = g_value_dup_string (value); |
1891 | break; |
1892 | default: |
1893 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
1894 | break; |
1895 | } |
1896 | } |
1897 | |
1898 | static void |
1899 | osm_gps_map_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) |
1900 | { |
1901 | g_return_if_fail (OSM_IS_GPS_MAP (object)); |
1902 | float lat,lon; |
1903 | OsmGpsMap *map = OSM_GPS_MAP(object); |
1904 | OsmGpsMapPrivate *priv = map->priv; |
1905 | |
1906 | switch (prop_id) |
1907 | { |
1908 | case PROP_AUTO_CENTER: |
1909 | g_value_set_boolean(value, priv->map_auto_center); |
1910 | break; |
1911 | case PROP_RECORD_TRIP_HISTORY: |
1912 | g_value_set_boolean(value, priv->record_trip_history); |
1913 | break; |
1914 | case PROP_SHOW_TRIP_HISTORY: |
1915 | g_value_set_boolean(value, priv->show_trip_history); |
1916 | break; |
1917 | case PROP_AUTO_DOWNLOAD: |
1918 | g_value_set_boolean(value, priv->map_auto_download); |
1919 | break; |
1920 | case PROP_REPO_URI: |
1921 | g_value_set_string(value, priv->repo_uri); |
1922 | break; |
1923 | case PROP_PROXY_URI: |
1924 | g_value_set_string(value, priv->proxy_uri); |
1925 | break; |
1926 | case PROP_TILE_CACHE_DIR: |
1927 | g_value_set_string(value, priv->cache_dir); |
1928 | break; |
1929 | case PROP_TILE_CACHE_DIR_IS_FULL_PATH: |
1930 | g_value_set_boolean(value, priv->cache_dir_is_full_path); |
1931 | break; |
1932 | case PROP_ZOOM: |
1933 | g_value_set_int(value, priv->map_zoom); |
1934 | break; |
1935 | case PROP_MAX_ZOOM: |
1936 | g_value_set_int(value, priv->max_zoom); |
1937 | break; |
1938 | case PROP_MIN_ZOOM: |
1939 | g_value_set_int(value, priv->min_zoom); |
1940 | break; |
1941 | case PROP_LATITUDE: |
1942 | lat = pixel2lat(priv->map_zoom, |
1943 | priv->map_y + (GTK_WIDGET(map)->allocation.height / 2)); |
1944 | g_value_set_float(value, rad2deg(lat)); |
1945 | break; |
1946 | case PROP_LONGITUDE: |
1947 | lon = pixel2lon(priv->map_zoom, |
1948 | priv->map_x + (GTK_WIDGET(map)->allocation.width / 2)); |
1949 | g_value_set_float(value, rad2deg(lon)); |
1950 | break; |
1951 | case PROP_MAP_X: |
1952 | g_value_set_int(value, priv->map_x); |
1953 | break; |
1954 | case PROP_MAP_Y: |
1955 | g_value_set_int(value, priv->map_y); |
1956 | break; |
1957 | case PROP_TILES_QUEUED: |
1958 | g_value_set_int(value, g_hash_table_size(priv->tile_queue)); |
1959 | break; |
1960 | case PROP_GPS_TRACK_WIDTH: |
1961 | g_value_set_int(value, priv->ui_gps_track_width); |
1962 | break; |
1963 | case PROP_GPS_POINT_R1: |
1964 | g_value_set_int(value, priv->ui_gps_point_inner_radius); |
1965 | break; |
1966 | case PROP_GPS_POINT_R2: |
1967 | g_value_set_int(value, priv->ui_gps_point_outer_radius); |
1968 | break; |
1969 | case PROP_MAP_SOURCE: |
1970 | g_value_set_int(value, priv->map_source); |
1971 | break; |
1972 | case PROP_IMAGE_FORMAT: |
1973 | g_value_set_string(value, priv->image_format); |
1974 | break; |
1975 | default: |
1976 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
1977 | break; |
1978 | } |
1979 | } |
1980 | |
1981 | static gboolean |
1982 | osm_gps_map_scroll_event (GtkWidget *widget, GdkEventScroll *event) |
1983 | { |
1984 | OsmGpsMap *map = OSM_GPS_MAP(widget); |
1985 | OsmGpsMapPrivate *priv = map->priv; |
1986 | |
1987 | if (event->direction == GDK_SCROLL_UP) |
1988 | { |
1989 | osm_gps_map_set_zoom(map, priv->map_zoom+1); |
1990 | } |
1991 | else |
1992 | { |
1993 | osm_gps_map_set_zoom(map, priv->map_zoom-1); |
1994 | } |
1995 | |
1996 | return FALSE; |
1997 | } |
1998 | |
1999 | static gboolean |
2000 | osm_gps_map_button_press (GtkWidget *widget, GdkEventButton *event) |
2001 | { |
2002 | OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget); |
2003 | |
2004 | /* don't drag if the user clicked within the balloon */ |
2005 | if (osm_gps_map_in_balloon(priv, |
2006 | event->x + EXTRA_BORDER, |
2007 | event->y + EXTRA_BORDER)) |
2008 | { |
2009 | priv->drag_counter = -1; |
2010 | return FALSE; |
2011 | } |
2012 | |
2013 | priv->drag_counter = 0; |
2014 | priv->drag_start_mouse_x = (int) event->x; |
2015 | priv->drag_start_mouse_y = (int) event->y; |
2016 | priv->drag_start_map_x = priv->map_x; |
2017 | priv->drag_start_map_y = priv->map_y; |
2018 | |
2019 | return FALSE; |
2020 | } |
2021 | |
2022 | static gboolean |
2023 | osm_gps_map_button_release (GtkWidget *widget, GdkEventButton *event) |
2024 | { |
2025 | OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget); |
2026 | |
2027 | /* released inside the balloon? */ |
2028 | if (osm_gps_map_in_balloon(priv, |
2029 | event->x + EXTRA_BORDER, |
2030 | event->y + EXTRA_BORDER)) |
2031 | { |
2032 | osm_gps_map_handle_balloon_click(OSM_GPS_MAP(widget), |
2033 | event->x - priv->balloon.rect.x + EXTRA_BORDER, |
2034 | event->y - priv->balloon.rect.y + EXTRA_BORDER); |
2035 | return FALSE; |
2036 | } |
2037 | |
2038 | if (priv->drag_counter < 0) |
2039 | return FALSE; |
2040 | |
2041 | if (priv->dragging) |
2042 | { |
2043 | priv->dragging = FALSE; |
2044 | |
2045 | priv->map_x = priv->drag_start_map_x; |
2046 | priv->map_y = priv->drag_start_map_y; |
2047 | |
2048 | priv->map_x += (priv->drag_start_mouse_x - (int) event->x); |
2049 | priv->map_y += (priv->drag_start_mouse_y - (int) event->y); |
2050 | |
2051 | priv->center_coord_set = FALSE; |
2052 | |
2053 | osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget)); |
2054 | } |
2055 | |
2056 | priv->drag_mouse_dx = 0; |
2057 | priv->drag_mouse_dy = 0; |
2058 | priv->drag_counter = -1; |
2059 | |
2060 | return FALSE; |
2061 | } |
2062 | |
2063 | static gboolean |
2064 | osm_gps_map_motion_notify (GtkWidget *widget, GdkEventMotion *event) |
2065 | { |
2066 | int x, y; |
2067 | GdkModifierType state; |
2068 | OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget); |
2069 | |
2070 | if (event->is_hint) |
2071 | gdk_window_get_pointer (event->window, &x, &y, &state); |
2072 | else |
2073 | { |
2074 | x = event->x; |
2075 | y = event->y; |
2076 | state = event->state; |
2077 | } |
2078 | |
2079 | // are we being dragged |
2080 | if (!(state & GDK_BUTTON1_MASK)) |
2081 | return FALSE; |
2082 | |
2083 | if (priv->drag_counter < 0) |
2084 | return FALSE; |
2085 | |
2086 | priv->drag_counter++; |
2087 | |
2088 | // we havent dragged more than 6 pixels |
2089 | if (priv->drag_counter < 6) |
2090 | return FALSE; |
2091 | |
2092 | priv->dragging = TRUE; |
2093 | |
2094 | if (priv->map_auto_center) |
2095 | g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL); |
2096 | |
2097 | priv->drag_mouse_dx = x - priv->drag_start_mouse_x; |
2098 | priv->drag_mouse_dy = y - priv->drag_start_mouse_y; |
2099 | |
2100 | #ifdef ENABLE_OSD |
2101 | /* undo OSD */ |
2102 | osm_gps_map_osd_restore (OSM_GPS_MAP(widget)); |
2103 | |
2104 | /* draw new OSD */ |
2105 | osm_gps_map_draw_osd_controls (OSM_GPS_MAP(widget), |
2106 | -priv->drag_mouse_dx, |
2107 | -priv->drag_mouse_dy); |
2108 | #endif |
2109 | |
2110 | gdk_draw_drawable ( |
2111 | widget->window, |
2112 | widget->style->fg_gc[GTK_WIDGET_STATE (widget)], |
2113 | priv->pixmap, |
2114 | 0,0, |
2115 | priv->drag_mouse_dx - EXTRA_BORDER, priv->drag_mouse_dy - EXTRA_BORDER, |
2116 | -1,-1); |
2117 | |
2118 | //Paint white outside of the map if dragging. Its less |
2119 | //ugly than painting the corrupted map |
2120 | if(priv->drag_mouse_dx>EXTRA_BORDER) { |
2121 | gdk_draw_rectangle ( |
2122 | widget->window, |
2123 | widget->style->white_gc, |
2124 | TRUE, |
2125 | 0, 0, |
2126 | priv->drag_mouse_dx - EXTRA_BORDER, |
2127 | widget->allocation.height); |
2128 | } |
2129 | else if (-priv->drag_mouse_dx > EXTRA_BORDER) |
2130 | { |
2131 | gdk_draw_rectangle ( |
2132 | widget->window, |
2133 | widget->style->white_gc, |
2134 | TRUE, |
2135 | priv->drag_mouse_dx + widget->allocation.width + EXTRA_BORDER, 0, |
2136 | -priv->drag_mouse_dx - EXTRA_BORDER, |
2137 | widget->allocation.height); |
2138 | } |
2139 | |
2140 | if (priv->drag_mouse_dy>EXTRA_BORDER) { |
2141 | gdk_draw_rectangle ( |
2142 | widget->window, |
2143 | widget->style->white_gc, |
2144 | TRUE, |
2145 | 0, 0, |
2146 | widget->allocation.width, |
2147 | priv->drag_mouse_dy - EXTRA_BORDER); |
2148 | } |
2149 | else if (-priv->drag_mouse_dy > EXTRA_BORDER) |
2150 | { |
2151 | gdk_draw_rectangle ( |
2152 | widget->window, |
2153 | widget->style->white_gc, |
2154 | TRUE, |
2155 | 0, priv->drag_mouse_dy + widget->allocation.height + EXTRA_BORDER, |
2156 | widget->allocation.width, |
2157 | -priv->drag_mouse_dy - EXTRA_BORDER); |
2158 | } |
2159 | |
2160 | return FALSE; |
2161 | } |
2162 | |
2163 | static gboolean |
2164 | osm_gps_map_configure (GtkWidget *widget, GdkEventConfigure *event) |
2165 | { |
2166 | OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget); |
2167 | |
2168 | /* create pixmap */ |
2169 | if (priv->pixmap) |
2170 | g_object_unref (priv->pixmap); |
2171 | |
2172 | priv->pixmap = gdk_pixmap_new ( |
2173 | widget->window, |
2174 | widget->allocation.width + EXTRA_BORDER * 2, |
2175 | widget->allocation.height + EXTRA_BORDER * 2, |
2176 | -1); |
2177 | |
2178 | /* and gc, used for clipping (I think......) */ |
2179 | if(priv->gc_map) |
2180 | g_object_unref(priv->gc_map); |
2181 | |
2182 | priv->gc_map = gdk_gc_new(priv->pixmap); |
2183 | |
2184 | osm_gps_map_map_redraw(OSM_GPS_MAP(widget)); |
2185 | |
2186 | return FALSE; |
2187 | } |
2188 | |
2189 | static gboolean |
2190 | osm_gps_map_expose (GtkWidget *widget, GdkEventExpose *event) |
2191 | { |
2192 | OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget); |
2193 | |
2194 | gdk_draw_drawable ( |
2195 | widget->window, |
2196 | widget->style->fg_gc[GTK_WIDGET_STATE (widget)], |
2197 | priv->pixmap, |
2198 | event->area.x + EXTRA_BORDER, |
2199 | event->area.y + EXTRA_BORDER, |
2200 | event->area.x, event->area.y, |
2201 | event->area.width, event->area.height); |
2202 | |
2203 | #ifdef ENABLE_OSD_OVL |
2204 | /* TODO: intersect with area */ |
2205 | if (priv->osd.pixbuf) |
2206 | { |
2207 | // gdk_draw_drawable (widget->window, |
2208 | // widget->style->fg_gc[GTK_WIDGET_STATE (widget)], |
2209 | // priv->osd.pixbuf, 0, 0, |
2210 | // OSD_X, OSD_Y, OSD_W, OSD_H); |
2211 | } |
2212 | #endif |
2213 | |
2214 | return FALSE; |
2215 | } |
2216 | |
2217 | static void |
2218 | osm_gps_map_class_init (OsmGpsMapClass *klass) |
2219 | { |
2220 | GObjectClass* object_class = G_OBJECT_CLASS (klass); |
2221 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); |
2222 | |
2223 | g_type_class_add_private (klass, sizeof (OsmGpsMapPrivate)); |
2224 | |
2225 | object_class->dispose = osm_gps_map_dispose; |
2226 | object_class->finalize = osm_gps_map_finalize; |
2227 | object_class->constructor = osm_gps_map_constructor; |
2228 | object_class->set_property = osm_gps_map_set_property; |
2229 | object_class->get_property = osm_gps_map_get_property; |
2230 | |
2231 | widget_class->expose_event = osm_gps_map_expose; |
2232 | widget_class->configure_event = osm_gps_map_configure; |
2233 | widget_class->button_press_event = osm_gps_map_button_press; |
2234 | widget_class->button_release_event = osm_gps_map_button_release; |
2235 | widget_class->motion_notify_event = osm_gps_map_motion_notify; |
2236 | widget_class->scroll_event = osm_gps_map_scroll_event; |
2237 | |
2238 | g_object_class_install_property (object_class, |
2239 | PROP_AUTO_CENTER, |
2240 | g_param_spec_boolean ("auto-center", |
2241 | "auto center", |
2242 | "map auto center", |
2243 | TRUE, |
2244 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2245 | |
2246 | g_object_class_install_property (object_class, |
2247 | PROP_RECORD_TRIP_HISTORY, |
2248 | g_param_spec_boolean ("record-trip-history", |
2249 | "record trip history", |
2250 | "should all gps points be recorded in a trip history", |
2251 | TRUE, |
2252 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2253 | |
2254 | g_object_class_install_property (object_class, |
2255 | PROP_SHOW_TRIP_HISTORY, |
2256 | g_param_spec_boolean ("show-trip-history", |
2257 | "show trip history", |
2258 | "should the recorded trip history be shown on the map", |
2259 | TRUE, |
2260 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2261 | |
2262 | g_object_class_install_property (object_class, |
2263 | PROP_AUTO_DOWNLOAD, |
2264 | g_param_spec_boolean ("auto-download", |
2265 | "auto download", |
2266 | "map auto download", |
2267 | TRUE, |
2268 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2269 | |
2270 | g_object_class_install_property (object_class, |
2271 | PROP_REPO_URI, |
2272 | g_param_spec_string ("repo-uri", |
2273 | "repo uri", |
2274 | "map source tile repository uri", |
2275 | OSM_REPO_URI, |
2276 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2277 | |
2278 | g_object_class_install_property (object_class, |
2279 | PROP_PROXY_URI, |
2280 | g_param_spec_string ("proxy-uri", |
2281 | "proxy uri", |
2282 | "http proxy uri on NULL", |
2283 | NULL, |
2284 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2285 | |
2286 | g_object_class_install_property (object_class, |
2287 | PROP_TILE_CACHE_DIR, |
2288 | g_param_spec_string ("tile-cache", |
2289 | "tile cache", |
2290 | "osm local tile cache dir", |
2291 | NULL, |
2292 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2293 | |
2294 | g_object_class_install_property (object_class, |
2295 | PROP_TILE_CACHE_DIR_IS_FULL_PATH, |
2296 | g_param_spec_boolean ("tile-cache-is-full-path", |
2297 | "tile cache is full path", |
2298 | "if true, the path passed to tile-cache is interpreted as the full cache path", |
2299 | FALSE, |
2300 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2301 | |
2302 | g_object_class_install_property (object_class, |
2303 | PROP_ZOOM, |
2304 | g_param_spec_int ("zoom", |
2305 | "zoom", |
2306 | "zoom level", |
2307 | MIN_ZOOM, /* minimum property value */ |
2308 | MAX_ZOOM, /* maximum property value */ |
2309 | 3, |
2310 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2311 | |
2312 | g_object_class_install_property (object_class, |
2313 | PROP_MAX_ZOOM, |
2314 | g_param_spec_int ("max-zoom", |
2315 | "max zoom", |
2316 | "maximum zoom level", |
2317 | MIN_ZOOM, /* minimum property value */ |
2318 | MAX_ZOOM, /* maximum property value */ |
2319 | OSM_MAX_ZOOM, |
2320 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2321 | |
2322 | g_object_class_install_property (object_class, |
2323 | PROP_MIN_ZOOM, |
2324 | g_param_spec_int ("min-zoom", |
2325 | "min zoom", |
2326 | "minimum zoom level", |
2327 | MIN_ZOOM, /* minimum property value */ |
2328 | MAX_ZOOM, /* maximum property value */ |
2329 | OSM_MIN_ZOOM, |
2330 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2331 | |
2332 | g_object_class_install_property (object_class, |
2333 | PROP_LATITUDE, |
2334 | g_param_spec_float ("latitude", |
2335 | "latitude", |
2336 | "latitude in degrees", |
2337 | -90.0, /* minimum property value */ |
2338 | 90.0, /* maximum property value */ |
2339 | 0, |
2340 | G_PARAM_READABLE)); |
2341 | |
2342 | g_object_class_install_property (object_class, |
2343 | PROP_LONGITUDE, |
2344 | g_param_spec_float ("longitude", |
2345 | "longitude", |
2346 | "longitude in degrees", |
2347 | -180.0, /* minimum property value */ |
2348 | 180.0, /* maximum property value */ |
2349 | 0, |
2350 | G_PARAM_READABLE)); |
2351 | |
2352 | g_object_class_install_property (object_class, |
2353 | PROP_MAP_X, |
2354 | g_param_spec_int ("map-x", |
2355 | "map-x", |
2356 | "initial map x location", |
2357 | G_MININT, /* minimum property value */ |
2358 | G_MAXINT, /* maximum property value */ |
2359 | 890, |
2360 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2361 | |
2362 | g_object_class_install_property (object_class, |
2363 | PROP_MAP_Y, |
2364 | g_param_spec_int ("map-y", |
2365 | "map-y", |
2366 | "initial map y location", |
2367 | G_MININT, /* minimum property value */ |
2368 | G_MAXINT, /* maximum property value */ |
2369 | 515, |
2370 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2371 | |
2372 | g_object_class_install_property (object_class, |
2373 | PROP_TILES_QUEUED, |
2374 | g_param_spec_int ("tiles-queued", |
2375 | "tiles-queued", |
2376 | "number of tiles currently waiting to download", |
2377 | G_MININT, /* minimum property value */ |
2378 | G_MAXINT, /* maximum property value */ |
2379 | 0, |
2380 | G_PARAM_READABLE)); |
2381 | |
2382 | g_object_class_install_property (object_class, |
2383 | PROP_GPS_TRACK_WIDTH, |
2384 | g_param_spec_int ("gps-track-width", |
2385 | "gps-track-width", |
2386 | "width of the lines drawn for the gps track", |
2387 | 1, /* minimum property value */ |
2388 | G_MAXINT, /* maximum property value */ |
2389 | 4, |
2390 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2391 | |
2392 | g_object_class_install_property (object_class, |
2393 | PROP_GPS_POINT_R1, |
2394 | g_param_spec_int ("gps-track-point-radius", |
2395 | "gps-track-point-radius", |
2396 | "radius of the gps point inner circle", |
2397 | 0, /* minimum property value */ |
2398 | G_MAXINT, /* maximum property value */ |
2399 | 5, |
2400 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2401 | |
2402 | g_object_class_install_property (object_class, |
2403 | PROP_GPS_POINT_R2, |
2404 | g_param_spec_int ("gps-track-highlight-radius", |
2405 | "gps-track-highlight-radius", |
2406 | "radius of the gps point highlight circle", |
2407 | 0, /* minimum property value */ |
2408 | G_MAXINT, /* maximum property value */ |
2409 | 20, |
2410 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT)); |
2411 | |
2412 | g_object_class_install_property (object_class, |
2413 | PROP_MAP_SOURCE, |
2414 | g_param_spec_int ("map-source", |
2415 | "map source", |
2416 | "map source ID", |
2417 | -1, /* minimum property value */ |
2418 | G_MAXINT, /* maximum property value */ |
2419 | -1, |
2420 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2421 | |
2422 | g_object_class_install_property (object_class, |
2423 | PROP_IMAGE_FORMAT, |
2424 | g_param_spec_string ("image-format", |
2425 | "image format", |
2426 | "map source tile repository image format (jpg, png)", |
2427 | OSM_IMAGE_FORMAT, |
2428 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); |
2429 | } |
2430 | |
2431 | const char* |
2432 | osm_gps_map_source_get_friendly_name(OsmGpsMapSource_t source) |
2433 | { |
2434 | switch(source) |
2435 | { |
2436 | case OSM_GPS_MAP_SOURCE_NULL: |
2437 | return "None"; |
2438 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP: |
2439 | return "OpenStreetMap"; |
2440 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER: |
2441 | return "OpenStreetMap Renderer"; |
2442 | case OSM_GPS_MAP_SOURCE_OPENAERIALMAP: |
2443 | return "OpenAerialMap"; |
2444 | case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE: |
2445 | return "Maps-For-Free"; |
2446 | case OSM_GPS_MAP_SOURCE_GOOGLE_STREET: |
2447 | return "Google Maps"; |
2448 | case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE: |
2449 | return "Google Satellite"; |
2450 | case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID: |
2451 | return "Google Hybrid"; |
2452 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET: |
2453 | return "Virtual Earth"; |
2454 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE: |
2455 | return "Virtual Earth Satellite"; |
2456 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID: |
2457 | return "Virtual Earth Hybrid"; |
2458 | case OSM_GPS_MAP_SOURCE_YAHOO_STREET: |
2459 | return "Yahoo Maps"; |
2460 | case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE: |
2461 | return "Yahoo Satellite"; |
2462 | case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID: |
2463 | return "Yahoo Hybrid"; |
2464 | default: |
2465 | return NULL; |
2466 | } |
2467 | return NULL; |
2468 | } |
2469 | |
2470 | //http://www.internettablettalk.com/forums/showthread.php?t=5209 |
2471 | //https://garage.maemo.org/plugins/scmsvn/viewcvs.php/trunk/src/maps.c?root=maemo-mapper&view=markup |
2472 | //http://www.ponies.me.uk/maps/GoogleTileUtils.java |
2473 | //http://www.mgmaps.com/cache/MapTileCacher.perl |
2474 | const char* |
2475 | osm_gps_map_source_get_repo_uri(OsmGpsMapSource_t source) |
2476 | { |
2477 | switch(source) |
2478 | { |
2479 | case OSM_GPS_MAP_SOURCE_NULL: |
2480 | return "none://"; |
2481 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP: |
2482 | return OSM_REPO_URI; |
2483 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER: |
2484 | return "http://tah.openstreetmap.org/Tiles/tile/#Z/#X/#Y.png"; |
2485 | case OSM_GPS_MAP_SOURCE_OPENAERIALMAP: |
2486 | return "http://tile.openaerialmap.org/tiles/1.0.0/openaerialmap-900913/#Z/#X/#Y.jpg"; |
2487 | case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE: |
2488 | return "http://maps-for-free.com/layer/relief/z#Z/row#Y/#Z_#X-#Y.jpg"; |
2489 | case OSM_GPS_MAP_SOURCE_GOOGLE_STREET: |
2490 | return "http://mt#R.google.com/vt/v=w2.97&x=#X&y=#Y&z=#Z"; |
2491 | case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE: |
2492 | return "http://khm#R.google.com/kh?n=404&v=3&t=#Q"; |
2493 | case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID: |
2494 | return NULL; /* No longer working "http://mt#R.google.com/mt?n=404&v=w2t.99&x=#X&y=#Y&zoom=#S" */ |
2495 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET: |
2496 | return "http://a#R.ortho.tiles.virtualearth.net/tiles/r#W.jpeg?g=50"; |
2497 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE: |
2498 | return "http://a#R.ortho.tiles.virtualearth.net/tiles/a#W.jpeg?g=50"; |
2499 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID: |
2500 | return "http://a#R.ortho.tiles.virtualearth.net/tiles/h#W.jpeg?g=50"; |
2501 | case OSM_GPS_MAP_SOURCE_YAHOO_STREET: |
2502 | case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE: |
2503 | case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID: |
2504 | /* TODO: Implement signed Y, aka U |
2505 | * http://us.maps3.yimg.com/aerial.maps.yimg.com/ximg?v=1.7&t=a&s=256&x=%d&y=%-d&z=%d |
2506 | * x = tilex, |
2507 | * y = (1 << (MAX_ZOOM - zoom)) - tiley - 1, |
2508 | * z = zoom - (MAX_ZOOM - 17)); |
2509 | */ |
2510 | return NULL; |
2511 | default: |
2512 | return NULL; |
2513 | } |
2514 | return NULL; |
2515 | } |
2516 | |
2517 | const char * |
2518 | osm_gps_map_source_get_image_format(OsmGpsMapSource_t source) |
2519 | { |
2520 | switch(source) { |
2521 | case OSM_GPS_MAP_SOURCE_NULL: |
2522 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP: |
2523 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER: |
2524 | return "png"; |
2525 | case OSM_GPS_MAP_SOURCE_OPENAERIALMAP: |
2526 | case OSM_GPS_MAP_SOURCE_GOOGLE_STREET: |
2527 | case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID: |
2528 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET: |
2529 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE: |
2530 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID: |
2531 | case OSM_GPS_MAP_SOURCE_YAHOO_STREET: |
2532 | case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE: |
2533 | case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID: |
2534 | case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE: |
2535 | case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE: |
2536 | return "jpg"; |
2537 | default: |
2538 | return "bin"; |
2539 | } |
2540 | return "bin"; |
2541 | } |
2542 | |
2543 | |
2544 | int |
2545 | osm_gps_map_source_get_min_zoom(OsmGpsMapSource_t source) |
2546 | { |
2547 | return 1; |
2548 | } |
2549 | |
2550 | int |
2551 | osm_gps_map_source_get_max_zoom(OsmGpsMapSource_t source) |
2552 | { |
2553 | switch(source) { |
2554 | case OSM_GPS_MAP_SOURCE_NULL: |
2555 | return 18; |
2556 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP: |
2557 | return OSM_MAX_ZOOM; |
2558 | case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER: |
2559 | case OSM_GPS_MAP_SOURCE_OPENAERIALMAP: |
2560 | case OSM_GPS_MAP_SOURCE_GOOGLE_STREET: |
2561 | case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID: |
2562 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET: |
2563 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE: |
2564 | case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID: |
2565 | case OSM_GPS_MAP_SOURCE_YAHOO_STREET: |
2566 | case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE: |
2567 | case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID: |
2568 | return 17; |
2569 | case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE: |
2570 | return 11; |
2571 | case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE: |
2572 | return 18; |
2573 | default: |
2574 | return 17; |
2575 | } |
2576 | return 17; |
2577 | } |
2578 | |
2579 | void |
2580 | osm_gps_map_download_maps (OsmGpsMap *map, coord_t *pt1, coord_t *pt2, int zoom_start, int zoom_end) |
2581 | { |
2582 | int i,j,zoom,num_tiles; |
2583 | OsmGpsMapPrivate *priv = map->priv; |
2584 | |
2585 | if (pt1 && pt2) |
2586 | { |
2587 | gchar *filename; |
2588 | num_tiles = 0; |
2589 | zoom_end = CLAMP(zoom_end, priv->min_zoom, priv->max_zoom); |
2590 | g_debug("Download maps: z:%d->%d",zoom_start, zoom_end); |
2591 | |
2592 | for(zoom=zoom_start; zoom<=zoom_end; zoom++) |
2593 | { |
2594 | int x1,y1,x2,y2; |
2595 | |
2596 | x1 = (int)floor((float)lon2pixel(zoom, pt1->rlon) / (float)TILESIZE); |
2597 | y1 = (int)floor((float)lat2pixel(zoom, pt1->rlat) / (float)TILESIZE); |
2598 | |
2599 | x2 = (int)floor((float)lon2pixel(zoom, pt2->rlon) / (float)TILESIZE); |
2600 | y2 = (int)floor((float)lat2pixel(zoom, pt2->rlat) / (float)TILESIZE); |
2601 | |
2602 | // loop x1-x2 |
2603 | for(i=x1; i<=x2; i++) |
2604 | { |
2605 | // loop y1 - y2 |
2606 | for(j=y1; j<=y2; j++) |
2607 | { |
2608 | // x = i, y = j |
2609 | filename = g_strdup_printf("%s%c%d%c%d%c%d.%s", |
2610 | priv->cache_dir, G_DIR_SEPARATOR, |
2611 | zoom, G_DIR_SEPARATOR, |
2612 | i, G_DIR_SEPARATOR, |
2613 | j, |
2614 | priv->image_format); |
2615 | if (!g_file_test(filename, G_FILE_TEST_EXISTS)) |
2616 | { |
2617 | osm_gps_map_download_tile(map, zoom, i, j, FALSE); |
2618 | num_tiles++; |
2619 | } |
2620 | g_free(filename); |
2621 | } |
2622 | } |
2623 | g_debug("DL @Z:%d = %d tiles",zoom,num_tiles); |
2624 | } |
2625 | } |
2626 | } |
2627 | |
2628 | void |
2629 | osm_gps_map_get_bbox (OsmGpsMap *map, coord_t *pt1, coord_t *pt2) |
2630 | { |
2631 | OsmGpsMapPrivate *priv = map->priv; |
2632 | |
2633 | if (pt1 && pt2) { |
2634 | pt1->rlat = pixel2lat(priv->map_zoom, priv->map_y); |
2635 | pt1->rlon = pixel2lon(priv->map_zoom, priv->map_x); |
2636 | pt2->rlat = pixel2lat(priv->map_zoom, priv->map_y + GTK_WIDGET(map)->allocation.height); |
2637 | pt2->rlon = pixel2lon(priv->map_zoom, priv->map_x + GTK_WIDGET(map)->allocation.width); |
2638 | |
2639 | g_debug("BBOX: %f %f %f %f", pt1->rlat, pt1->rlon, pt2->rlat, pt2->rlon); |
2640 | } |
2641 | } |
2642 | |
2643 | void |
2644 | osm_gps_map_set_mapcenter (OsmGpsMap *map, float latitude, float longitude, int zoom) |
2645 | { |
2646 | osm_gps_map_set_center (map, latitude, longitude); |
2647 | osm_gps_map_set_zoom (map, zoom); |
2648 | } |
2649 | |
2650 | void |
2651 | osm_gps_map_set_center (OsmGpsMap *map, float latitude, float longitude) |
2652 | { |
2653 | int pixel_x, pixel_y; |
2654 | OsmGpsMapPrivate *priv; |
2655 | |
2656 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2657 | priv = map->priv; |
2658 | |
2659 | priv->center_rlat = deg2rad(latitude); |
2660 | priv->center_rlon = deg2rad(longitude); |
2661 | priv->center_coord_set = TRUE; |
2662 | |
2663 | // pixel_x,y, offsets |
2664 | pixel_x = lon2pixel(priv->map_zoom, priv->center_rlon); |
2665 | pixel_y = lat2pixel(priv->map_zoom, priv->center_rlat); |
2666 | |
2667 | priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2; |
2668 | priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2; |
2669 | |
2670 | osm_gps_map_map_redraw_idle(map); |
2671 | } |
2672 | |
2673 | int |
2674 | osm_gps_map_set_zoom (OsmGpsMap *map, int zoom) |
2675 | { |
2676 | int zoom_old; |
2677 | double factor = 0.0; |
2678 | int width_center, height_center; |
2679 | OsmGpsMapPrivate *priv; |
2680 | |
2681 | g_return_val_if_fail (OSM_IS_GPS_MAP (map), 0); |
2682 | priv = map->priv; |
2683 | |
2684 | if (zoom != priv->map_zoom) |
2685 | { |
2686 | width_center = GTK_WIDGET(map)->allocation.width / 2; |
2687 | height_center = GTK_WIDGET(map)->allocation.height / 2; |
2688 | |
2689 | zoom_old = priv->map_zoom; |
2690 | //constrain zoom min_zoom -> max_zoom |
2691 | priv->map_zoom = CLAMP(zoom, priv->min_zoom, priv->max_zoom); |
2692 | |
2693 | if (priv->center_coord_set) |
2694 | { |
2695 | priv->map_x = lon2pixel(priv->map_zoom, priv->center_rlon) - width_center; |
2696 | priv->map_y = lat2pixel(priv->map_zoom, priv->center_rlat) - height_center; |
2697 | } |
2698 | else |
2699 | { |
2700 | factor = exp(priv->map_zoom * M_LN2)/exp(zoom_old * M_LN2); |
2701 | priv->map_x = ((priv->map_x + width_center) * factor) - width_center; |
2702 | priv->map_y = ((priv->map_y + height_center) * factor) - height_center; |
2703 | } |
2704 | |
2705 | g_debug("Zoom changed from %d to %d factor:%f x:%d", |
2706 | zoom_old, priv->map_zoom, factor, priv->map_x); |
2707 | |
2708 | osm_gps_map_map_redraw_idle(map); |
2709 | } |
2710 | return priv->map_zoom; |
2711 | } |
2712 | |
2713 | void |
2714 | osm_gps_map_add_track (OsmGpsMap *map, GSList *track) |
2715 | { |
2716 | OsmGpsMapPrivate *priv; |
2717 | |
2718 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2719 | priv = map->priv; |
2720 | |
2721 | if (track) { |
2722 | priv->tracks = g_slist_append(priv->tracks, track); |
2723 | osm_gps_map_map_redraw_idle(map); |
2724 | } |
2725 | } |
2726 | |
2727 | void |
2728 | osm_gps_map_clear_tracks (OsmGpsMap *map) |
2729 | { |
2730 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2731 | |
2732 | osm_gps_map_free_tracks(map); |
2733 | osm_gps_map_map_redraw_idle(map); |
2734 | } |
2735 | |
2736 | void |
2737 | osm_gps_map_add_image (OsmGpsMap *map, float latitude, float longitude, GdkPixbuf *image) |
2738 | { |
2739 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2740 | |
2741 | if (image) { |
2742 | OsmGpsMapPrivate *priv = map->priv; |
2743 | image_t *im; |
2744 | |
2745 | //cache w/h for speed, and add image to list |
2746 | im = g_new0(image_t,1); |
2747 | im->w = gdk_pixbuf_get_width(image); |
2748 | im->h = gdk_pixbuf_get_height(image); |
2749 | im->pt.rlat = deg2rad(latitude); |
2750 | im->pt.rlon = deg2rad(longitude); |
2751 | |
2752 | g_object_ref(image); |
2753 | im->image = image; |
2754 | |
2755 | priv->images = g_slist_append(priv->images, im); |
2756 | |
2757 | osm_gps_map_map_redraw_idle(map); |
2758 | } |
2759 | } |
2760 | |
2761 | gboolean |
2762 | osm_gps_map_remove_image (OsmGpsMap *map, GdkPixbuf *image) |
2763 | { |
2764 | OsmGpsMapPrivate *priv = map->priv; |
2765 | if (priv->images) { |
2766 | GSList *list; |
2767 | for(list = priv->images; list != NULL; list = list->next) |
2768 | { |
2769 | image_t *im = list->data; |
2770 | if (im->image == image) |
2771 | { |
2772 | priv->images = g_slist_remove_link(priv->images, list); |
2773 | g_object_unref(im->image); |
2774 | g_free(im); |
2775 | osm_gps_map_map_redraw_idle(map); |
2776 | return TRUE; |
2777 | } |
2778 | } |
2779 | } |
2780 | return FALSE; |
2781 | } |
2782 | |
2783 | void |
2784 | osm_gps_map_clear_images (OsmGpsMap *map) |
2785 | { |
2786 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2787 | |
2788 | osm_gps_map_free_images(map); |
2789 | osm_gps_map_map_redraw_idle(map); |
2790 | } |
2791 | |
2792 | void |
2793 | osm_gps_map_osd_speed (OsmGpsMap *map, float speed) |
2794 | { |
2795 | OsmGpsMapPrivate *priv; |
2796 | |
2797 | PangoContext *context = NULL; |
2798 | PangoLayout *layout = NULL; |
2799 | PangoFontDescription *desc = NULL; |
2800 | |
2801 | GdkColor color; |
2802 | GdkGC *gc; |
2803 | |
2804 | gchar *buffer; |
2805 | //static int x = 10, y = 10; |
2806 | static int width = 0, height = 0; |
2807 | |
2808 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2809 | priv = map->priv; |
2810 | |
2811 | buffer = g_strdup_printf("%.0f", speed); |
2812 | |
2813 | /* pango initialisation */ |
2814 | context = gtk_widget_get_pango_context (GTK_WIDGET(map)); |
2815 | layout = pango_layout_new (context); |
2816 | desc = pango_font_description_new(); |
2817 | |
2818 | pango_font_description_set_size (desc, 40 * PANGO_SCALE); |
2819 | pango_layout_set_font_description (layout, desc); |
2820 | pango_layout_set_text (layout, buffer, strlen(buffer)); |
2821 | |
2822 | gc = gdk_gc_new (GTK_WIDGET(map)->window); |
2823 | |
2824 | color.red = (0 > 50) ? 0xffff : 0; |
2825 | color.green = 0; |
2826 | color.blue = 0; |
2827 | |
2828 | gdk_gc_set_rgb_fg_color (gc, &color); |
2829 | |
2830 | /* faster / less flicker alternative:*/ |
2831 | gdk_draw_drawable ( |
2832 | GTK_WIDGET(map)->window, |
2833 | GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(map)], |
2834 | priv->pixmap, |
2835 | 0,0, |
2836 | 0,0, |
2837 | width+10,width+10); |
2838 | |
2839 | gdk_draw_layout(GTK_WIDGET(map)->window, |
2840 | gc, |
2841 | 0, 0, |
2842 | layout); |
2843 | |
2844 | /* set width and height */ |
2845 | pango_layout_get_pixel_size(layout, &width, &height); |
2846 | |
2847 | g_free(buffer); |
2848 | pango_font_description_free (desc); |
2849 | g_object_unref (layout); |
2850 | g_object_unref (gc); |
2851 | } |
2852 | |
2853 | void |
2854 | osm_gps_map_draw_gps (OsmGpsMap *map, float latitude, float longitude, float heading) |
2855 | { |
2856 | int pixel_x, pixel_y; |
2857 | OsmGpsMapPrivate *priv; |
2858 | |
2859 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2860 | priv = map->priv; |
2861 | |
2862 | priv->gps->rlat = deg2rad(latitude); |
2863 | priv->gps->rlon = deg2rad(longitude); |
2864 | priv->gps_valid = TRUE; |
2865 | |
2866 | // pixel_x,y, offsets |
2867 | pixel_x = lon2pixel(priv->map_zoom, priv->gps->rlon); |
2868 | pixel_y = lat2pixel(priv->map_zoom, priv->gps->rlat); |
2869 | |
2870 | //If trip marker add to list of gps points. |
2871 | if (priv->record_trip_history) { |
2872 | coord_t *tp = g_new0(coord_t,1); |
2873 | tp->rlat = priv->gps->rlat; |
2874 | tp->rlon = priv->gps->rlon; |
2875 | priv->trip_history = g_slist_append(priv->trip_history, tp); |
2876 | } |
2877 | |
2878 | // dont draw anything if we are dragging |
2879 | if (priv->dragging) { |
2880 | g_debug("Dragging"); |
2881 | return; |
2882 | } |
2883 | |
2884 | //Automatically center the map if the track approaches the edge |
2885 | if(priv->map_auto_center) { |
2886 | int x = pixel_x - priv->map_x; |
2887 | int y = pixel_y - priv->map_y; |
2888 | int width = GTK_WIDGET(map)->allocation.width; |
2889 | int height = GTK_WIDGET(map)->allocation.height; |
2890 | if( x < (width/2 - width/8) || x > (width/2 + width/8) || |
2891 | y < (height/2 - height/8) || y > (height/2 + height/8)) { |
2892 | |
2893 | priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2; |
2894 | priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2; |
2895 | priv->center_coord_set = FALSE; |
2896 | } |
2897 | } |
2898 | |
2899 | // this redraws the map (including the gps track, and adjusts the |
2900 | // map center if it was changed |
2901 | osm_gps_map_map_redraw_idle(map); |
2902 | } |
2903 | |
2904 | void |
2905 | osm_gps_map_clear_gps (OsmGpsMap *map) |
2906 | { |
2907 | osm_gps_map_free_trip(map); |
2908 | osm_gps_map_map_redraw_idle(map); |
2909 | } |
2910 | |
2911 | coord_t |
2912 | osm_gps_map_get_co_ordinates (OsmGpsMap *map, int pixel_x, int pixel_y) |
2913 | { |
2914 | coord_t coord; |
2915 | OsmGpsMapPrivate *priv = map->priv; |
2916 | |
2917 | coord.rlat = pixel2lat(priv->map_zoom, priv->map_y + pixel_y); |
2918 | coord.rlon = pixel2lon(priv->map_zoom, priv->map_x + pixel_x); |
2919 | return coord; |
2920 | } |
2921 | |
2922 | GtkWidget * |
2923 | osm_gps_map_new (void) |
2924 | { |
2925 | return g_object_new (OSM_TYPE_GPS_MAP, NULL); |
2926 | } |
2927 | |
2928 | void |
2929 | osm_gps_map_screen_to_geographic (OsmGpsMap *map, gint pixel_x, gint pixel_y, |
2930 | gfloat *latitude, gfloat *longitude) |
2931 | { |
2932 | OsmGpsMapPrivate *priv; |
2933 | |
2934 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2935 | priv = map->priv; |
2936 | |
2937 | if (latitude) |
2938 | *latitude = rad2deg(pixel2lat(priv->map_zoom, priv->map_y + pixel_y)); |
2939 | if (longitude) |
2940 | *longitude = rad2deg(pixel2lon(priv->map_zoom, priv->map_x + pixel_x)); |
2941 | } |
2942 | |
2943 | void |
2944 | osm_gps_map_geographic_to_screen (OsmGpsMap *map, |
2945 | gfloat latitude, gfloat longitude, |
2946 | gint *pixel_x, gint *pixel_y) |
2947 | { |
2948 | OsmGpsMapPrivate *priv; |
2949 | |
2950 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2951 | priv = map->priv; |
2952 | |
2953 | if (pixel_x) |
2954 | *pixel_x = lon2pixel(priv->map_zoom, deg2rad(longitude)) - priv->map_x; |
2955 | if (pixel_y) |
2956 | *pixel_y = lat2pixel(priv->map_zoom, deg2rad(latitude)) - priv->map_y; |
2957 | } |
2958 | |
2959 | void |
2960 | osm_gps_map_scroll (OsmGpsMap *map, gint dx, gint dy) |
2961 | { |
2962 | OsmGpsMapPrivate *priv; |
2963 | |
2964 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2965 | priv = map->priv; |
2966 | |
2967 | priv->center_coord_set = FALSE; |
2968 | priv->map_x += dx; |
2969 | priv->map_y += dy; |
2970 | |
2971 | osm_gps_map_map_redraw_idle (map); |
2972 | } |
2973 | |
2974 | float |
2975 | osm_gps_map_get_scale(OsmGpsMap *map) |
2976 | { |
2977 | OsmGpsMapPrivate *priv; |
2978 | |
2979 | g_return_val_if_fail (OSM_IS_GPS_MAP (map), OSM_NAN); |
2980 | priv = map->priv; |
2981 | |
2982 | return osm_gps_map_get_scale_at_point(priv->map_zoom, priv->center_rlat, priv->center_rlon); |
2983 | } |
2984 | |
2985 | void |
2986 | osm_gps_map_draw_balloon (OsmGpsMap *map, float latitude, float longitude, |
2987 | OsmGpsMapBalloonCallback cb, gpointer data) |
2988 | { |
2989 | OsmGpsMapPrivate *priv; |
2990 | |
2991 | /* remove and previously installed balloon */ |
2992 | osm_gps_map_clear_balloon (map); |
2993 | |
2994 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
2995 | priv = map->priv; |
2996 | |
2997 | priv->balloon.coo->rlat = deg2rad(latitude); |
2998 | priv->balloon.coo->rlon = deg2rad(longitude); |
2999 | priv->balloon.valid = TRUE; |
3000 | |
3001 | priv->balloon.cb = cb; |
3002 | priv->balloon.data = data; |
3003 | |
3004 | // this redraws the map |
3005 | osm_gps_map_map_redraw_idle(map); |
3006 | } |
3007 | |
3008 | void |
3009 | osm_gps_map_clear_balloon (OsmGpsMap *map) |
3010 | { |
3011 | OsmGpsMapPrivate *priv; |
3012 | |
3013 | g_return_if_fail (OSM_IS_GPS_MAP (map)); |
3014 | priv = map->priv; |
3015 | |
3016 | priv->balloon.valid = FALSE; |
3017 | |
3018 | osm_gps_map_map_redraw_idle(map); |
3019 | } |