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

Parent Directory Parent Directory | Revision Log Revision Log


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