Contents of /trunk/src/osm-gps-map.c

Parent Directory Parent Directory | Revision Log Revision Log


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