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

Parent Directory Parent Directory | Revision Log Revision Log


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