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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 132 - (show annotations)
Thu Oct 1 08:09:00 2009 UTC (14 years, 7 months ago) by harbaum
File MIME type: text/plain
File size: 95194 byte(s)
Nav OSD done
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 priv->idle_map_redraw = 0;
1252
1253 /* don't redraw the entire map while the OSD is doing */
1254 /* some animation or the like. This is to keep the animation */
1255 /* fluid */
1256 if (priv->osd->busy(priv->osd))
1257 return FALSE;
1258
1259 #ifdef DRAG_DEBUG
1260 printf("trying redraw\n");
1261 #endif
1262
1263 /* the motion_notify handler uses priv->pixmap to redraw the area; if we
1264 * change it while we are dragging, we will end up showing it in the wrong
1265 * place. This could be fixed by carefully recompute the coordinates, but
1266 * for now it's easier just to disable redrawing the map while dragging */
1267 if (priv->dragging)
1268 return FALSE;
1269
1270 /* undo all offsets that may have happened when dragging */
1271 priv->drag_mouse_dx = 0;
1272 priv->drag_mouse_dy = 0;
1273
1274 priv->redraw_cycle++;
1275
1276 /* draw white background to initialise pixmap */
1277 gdk_draw_rectangle (
1278 priv->pixmap,
1279 GTK_WIDGET(map)->style->white_gc,
1280 TRUE,
1281 0, 0,
1282 GTK_WIDGET(map)->allocation.width + EXTRA_BORDER * 2,
1283 GTK_WIDGET(map)->allocation.height + EXTRA_BORDER * 2);
1284
1285 osm_gps_map_fill_tiles_pixel(map);
1286
1287 osm_gps_map_print_tracks(map);
1288 osm_gps_map_draw_gps_point(map);
1289 osm_gps_map_print_images(map);
1290
1291 #ifdef ENABLE_OSD
1292 /* OSD may contain a coordinate/scale, so we may have to re-render it */
1293 if(priv->osd && OSM_IS_GPS_MAP (priv->osd->widget))
1294 priv->osd->render (priv->osd);
1295 #endif
1296
1297 osm_gps_map_purge_cache(map);
1298 gtk_widget_queue_draw (GTK_WIDGET (map));
1299
1300 return FALSE;
1301 }
1302
1303 static void
1304 osm_gps_map_map_redraw_idle (OsmGpsMap *map)
1305 {
1306 OsmGpsMapPrivate *priv = map->priv;
1307
1308 if (priv->idle_map_redraw == 0)
1309 priv->idle_map_redraw = g_idle_add ((GSourceFunc)osm_gps_map_map_redraw, map);
1310 }
1311
1312 #ifdef OSM_GPS_MAP_KEYS
1313 static gboolean
1314 on_window_key_press(GtkWidget *widget,
1315 GdkEventKey *event, OsmGpsMapPrivate *priv) {
1316 gboolean handled = FALSE;
1317 int step = GTK_WIDGET(widget)->allocation.width/OSM_GPS_MAP_SCROLL_STEP;
1318
1319 // the map handles some keys on its own ...
1320 switch(event->keyval) {
1321 #ifdef OSM_GPS_MAP_KEY_FULLSCREEN
1322 case OSM_GPS_MAP_KEY_FULLSCREEN: {
1323 GtkWidget *toplevel = gtk_widget_get_toplevel(GTK_WIDGET(widget));
1324 if(!priv->fullscreen)
1325 gtk_window_fullscreen(GTK_WINDOW(toplevel));
1326 else
1327 gtk_window_unfullscreen(GTK_WINDOW(toplevel));
1328
1329 priv->fullscreen = !priv->fullscreen;
1330 handled = TRUE;
1331 } break;
1332 #endif
1333
1334 #ifdef OSM_GPS_MAP_KEY_ZOOMIN
1335 case OSM_GPS_MAP_KEY_ZOOMIN:
1336 osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom+1);
1337 handled = TRUE;
1338 break;
1339 #endif
1340
1341 #ifdef OSM_GPS_MAP_KEY_ZOOMOUT
1342 case OSM_GPS_MAP_KEY_ZOOMOUT:
1343 osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom-1);
1344 handled = TRUE;
1345 break;
1346 #endif
1347
1348 #ifdef OSM_GPS_MAP_KEY_UP
1349 case OSM_GPS_MAP_KEY_UP:
1350 priv->map_y -= step;
1351 priv->center_coord_set = FALSE;
1352 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1353 handled = TRUE;
1354 break;
1355 #endif
1356
1357 #ifdef OSM_GPS_MAP_KEY_DOWN
1358 case OSM_GPS_MAP_KEY_DOWN:
1359 priv->map_y += step;
1360 priv->center_coord_set = FALSE;
1361 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1362 handled = TRUE;
1363 break;
1364 #endif
1365
1366 #ifdef OSM_GPS_MAP_KEY_LEFT
1367 case OSM_GPS_MAP_KEY_LEFT:
1368 priv->map_x -= step;
1369 priv->center_coord_set = FALSE;
1370 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1371 handled = TRUE;
1372 break;
1373 #endif
1374
1375 #ifdef OSM_GPS_MAP_KEY_RIGHT
1376 case OSM_GPS_MAP_KEY_RIGHT:
1377 priv->map_x += step;
1378 priv->center_coord_set = FALSE;
1379 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1380 handled = TRUE;
1381 break;
1382 #endif
1383
1384 default:
1385 break;
1386 }
1387
1388 return handled;
1389 }
1390 #endif
1391
1392 static void
1393 osm_gps_map_init (OsmGpsMap *object)
1394 {
1395 OsmGpsMapPrivate *priv;
1396
1397 priv = G_TYPE_INSTANCE_GET_PRIVATE (object, OSM_TYPE_GPS_MAP, OsmGpsMapPrivate);
1398 object->priv = priv;
1399
1400 priv->pixmap = NULL;
1401
1402 priv->trip_history = NULL;
1403 priv->gps = g_new0(coord_t, 1);
1404 priv->gps_valid = FALSE;
1405 priv->gps_heading = OSM_GPS_MAP_INVALID;
1406
1407 #ifdef ENABLE_OSD
1408 priv->osd = NULL;
1409 #endif
1410
1411 #ifdef OSM_GPS_MAP_BUTTON_FULLSCREEN
1412 priv->fullscreen = FALSE;
1413 #endif
1414
1415 priv->tracks = NULL;
1416 priv->images = NULL;
1417
1418 priv->drag_counter = 0;
1419 priv->drag_mouse_dx = 0;
1420 priv->drag_mouse_dy = 0;
1421 priv->drag_start_mouse_x = 0;
1422 priv->drag_start_mouse_y = 0;
1423
1424 priv->uri_format = 0;
1425 priv->the_google = FALSE;
1426
1427 priv->map_source = -1;
1428
1429 #ifndef LIBSOUP22
1430 //Change naumber of concurrent connections option?
1431 priv->soup_session =
1432 soup_session_async_new_with_options(SOUP_SESSION_USER_AGENT,
1433 USER_AGENT, NULL);
1434 #else
1435 /* libsoup-2.2 has no special way to set the user agent, so we */
1436 /* set it seperately as an extra header field for each reuest */
1437 priv->soup_session = soup_session_async_new();
1438 #endif
1439
1440 //Hash table which maps tile d/l URIs to SoupMessage requests
1441 priv->tile_queue = g_hash_table_new (g_str_hash, g_str_equal);
1442
1443 //Some mapping providers (Google) have varying degrees of tiles at multiple
1444 //zoom levels
1445 priv->missing_tiles = g_hash_table_new (g_str_hash, g_str_equal);
1446
1447 /* memory cache for most recently used tiles */
1448 priv->tile_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1449 g_free, (GDestroyNotify)cached_tile_free);
1450 priv->max_tile_cache_size = 20;
1451
1452 gtk_widget_add_events (GTK_WIDGET (object),
1453 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1454 GDK_POINTER_MOTION_MASK |
1455 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
1456 GTK_WIDGET_SET_FLAGS (object, GTK_CAN_FOCUS);
1457
1458 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, my_log_handler, NULL);
1459
1460 #ifdef OSM_GPS_MAP_KEYS
1461 g_signal_connect(G_OBJECT(object), "key_press_event",
1462 G_CALLBACK(on_window_key_press), priv);
1463 #endif
1464 }
1465
1466 static void
1467 osm_gps_map_setup(OsmGpsMapPrivate *priv) {
1468 const char *uri;
1469
1470 //user can specify a map source ID, or a repo URI as the map source
1471 uri = osm_gps_map_source_get_repo_uri(OSM_GPS_MAP_SOURCE_NULL);
1472 if ( (priv->map_source == 0) || (strcmp(priv->repo_uri, uri) == 0) ) {
1473 g_debug("Using null source");
1474 priv->map_source = OSM_GPS_MAP_SOURCE_NULL;
1475
1476 priv->null_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 256, 256);
1477 gdk_pixbuf_fill(priv->null_tile, 0xcccccc00);
1478 }
1479 else if (priv->map_source >= 0) {
1480 //check if the source given is valid
1481 uri = osm_gps_map_source_get_repo_uri(priv->map_source);
1482 if (uri) {
1483 g_debug("Setting map source from ID");
1484 g_free(priv->repo_uri);
1485
1486 priv->repo_uri = g_strdup(uri);
1487 priv->image_format = g_strdup(
1488 osm_gps_map_source_get_image_format(priv->map_source));
1489 priv->max_zoom = osm_gps_map_source_get_max_zoom(priv->map_source);
1490 priv->min_zoom = osm_gps_map_source_get_min_zoom(priv->map_source);
1491 }
1492 }
1493
1494 const char *fname = osm_gps_map_source_get_friendly_name(priv->map_source);
1495 if(!fname) fname = "_unknown_";
1496
1497 if (priv->tile_dir) {
1498 //the new cachedir is the given cache dir + the friendly name of the repo_uri
1499 priv->cache_dir = g_strdup_printf("%s%c%s", priv->tile_dir, G_DIR_SEPARATOR, fname);
1500 g_debug("Adjusting cache dir %s -> %s", priv->tile_dir, priv->cache_dir);
1501 }
1502 }
1503
1504 static GObject *
1505 osm_gps_map_constructor (GType gtype, guint n_properties, GObjectConstructParam *properties)
1506 {
1507 //Always chain up to the parent constructor
1508 GObject *object =
1509 G_OBJECT_CLASS(osm_gps_map_parent_class)->constructor(gtype, n_properties, properties);
1510
1511 osm_gps_map_setup(OSM_GPS_MAP_PRIVATE(object));
1512
1513 inspect_map_uri(OSM_GPS_MAP(object));
1514
1515 return object;
1516 }
1517
1518 static void
1519 osm_gps_map_dispose (GObject *object)
1520 {
1521 OsmGpsMap *map = OSM_GPS_MAP(object);
1522 OsmGpsMapPrivate *priv = map->priv;
1523
1524 if (priv->is_disposed)
1525 return;
1526
1527 priv->is_disposed = TRUE;
1528
1529 soup_session_abort(priv->soup_session);
1530 g_object_unref(priv->soup_session);
1531
1532 g_hash_table_destroy(priv->tile_queue);
1533 g_hash_table_destroy(priv->missing_tiles);
1534 g_hash_table_destroy(priv->tile_cache);
1535
1536 osm_gps_map_free_images(map);
1537
1538 if(priv->pixmap)
1539 g_object_unref (priv->pixmap);
1540
1541 if (priv->null_tile)
1542 g_object_unref (priv->null_tile);
1543
1544 if(priv->gc_map)
1545 g_object_unref(priv->gc_map);
1546
1547 if (priv->idle_map_redraw != 0)
1548 g_source_remove (priv->idle_map_redraw);
1549
1550 if (priv->drag_expose != 0)
1551 g_source_remove (priv->drag_expose);
1552
1553 g_free(priv->gps);
1554
1555 #ifdef ENABLE_OSD
1556 if(priv->osd)
1557 priv->osd->free(priv->osd);
1558
1559 #ifdef OSD_DOUBLE_BUFFER
1560 if(priv->dbuf_pixmap)
1561 g_object_unref (priv->dbuf_pixmap);
1562 #endif
1563 #endif
1564
1565 G_OBJECT_CLASS (osm_gps_map_parent_class)->dispose (object);
1566 }
1567
1568 static void
1569 osm_gps_map_finalize (GObject *object)
1570 {
1571 OsmGpsMap *map = OSM_GPS_MAP(object);
1572 OsmGpsMapPrivate *priv = map->priv;
1573
1574 if(priv->tile_dir)
1575 g_free(priv->tile_dir);
1576
1577 if(priv->cache_dir)
1578 g_free(priv->cache_dir);
1579
1580 g_free(priv->repo_uri);
1581 g_free(priv->image_format);
1582
1583 osm_gps_map_free_trip(map);
1584 osm_gps_map_free_tracks(map);
1585
1586 G_OBJECT_CLASS (osm_gps_map_parent_class)->finalize (object);
1587 }
1588
1589 static void
1590 osm_gps_map_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
1591 {
1592 g_return_if_fail (OSM_IS_GPS_MAP (object));
1593 OsmGpsMap *map = OSM_GPS_MAP(object);
1594 OsmGpsMapPrivate *priv = map->priv;
1595
1596 switch (prop_id)
1597 {
1598 case PROP_AUTO_CENTER:
1599 priv->map_auto_center = g_value_get_boolean (value);
1600 break;
1601 case PROP_RECORD_TRIP_HISTORY:
1602 priv->record_trip_history = g_value_get_boolean (value);
1603 break;
1604 case PROP_SHOW_TRIP_HISTORY:
1605 priv->show_trip_history = g_value_get_boolean (value);
1606 break;
1607 case PROP_AUTO_DOWNLOAD:
1608 priv->map_auto_download = g_value_get_boolean (value);
1609 break;
1610 case PROP_REPO_URI:
1611 priv->repo_uri = g_value_dup_string (value);
1612 break;
1613 case PROP_PROXY_URI:
1614 if ( g_value_get_string(value) ) {
1615 priv->proxy_uri = g_value_dup_string (value);
1616 g_debug("Setting proxy server: %s", priv->proxy_uri);
1617
1618 #ifndef LIBSOUP22
1619 GValue val = {0};
1620
1621 SoupURI* uri = soup_uri_new(priv->proxy_uri);
1622 g_value_init(&val, SOUP_TYPE_URI);
1623 g_value_take_boxed(&val, uri);
1624
1625 g_object_set_property(G_OBJECT(priv->soup_session),SOUP_SESSION_PROXY_URI,&val);
1626 #else
1627 SoupUri* uri = soup_uri_new(priv->proxy_uri);
1628 g_object_set(G_OBJECT(priv->soup_session), SOUP_SESSION_PROXY_URI, uri, NULL);
1629 #endif
1630 } else
1631 priv->proxy_uri = NULL;
1632
1633 break;
1634 case PROP_TILE_CACHE_DIR:
1635 if ( g_value_get_string(value) )
1636 priv->tile_dir = g_value_dup_string (value);
1637 break;
1638 case PROP_ZOOM:
1639 priv->map_zoom = g_value_get_int (value);
1640 break;
1641 case PROP_MAX_ZOOM:
1642 priv->max_zoom = g_value_get_int (value);
1643 break;
1644 case PROP_MIN_ZOOM:
1645 priv->min_zoom = g_value_get_int (value);
1646 break;
1647 case PROP_MAP_X:
1648 priv->map_x = g_value_get_int (value);
1649 priv->center_coord_set = FALSE;
1650 break;
1651 case PROP_MAP_Y:
1652 priv->map_y = g_value_get_int (value);
1653 priv->center_coord_set = FALSE;
1654 break;
1655 case PROP_GPS_TRACK_WIDTH:
1656 priv->ui_gps_track_width = g_value_get_int (value);
1657 break;
1658 case PROP_GPS_POINT_R1:
1659 priv->ui_gps_point_inner_radius = g_value_get_int (value);
1660 break;
1661 case PROP_GPS_POINT_R2:
1662 priv->ui_gps_point_outer_radius = g_value_get_int (value);
1663 break;
1664 case PROP_MAP_SOURCE: {
1665 gint old = priv->map_source;
1666 priv->map_source = g_value_get_int (value);
1667 if(old >= OSM_GPS_MAP_SOURCE_NULL &&
1668 priv->map_source != old &&
1669 priv->map_source >= OSM_GPS_MAP_SOURCE_NULL &&
1670 priv->map_source <= OSM_GPS_MAP_SOURCE_LAST) {
1671
1672 /* we now have to switch the entire map */
1673
1674 /* flush the ram cache */
1675 g_hash_table_remove_all(priv->tile_cache);
1676
1677 osm_gps_map_setup(priv);
1678
1679 inspect_map_uri(map);
1680
1681 /* adjust zoom if necessary */
1682 if(priv->map_zoom > priv->max_zoom)
1683 osm_gps_map_set_zoom(map, priv->max_zoom);
1684
1685 if(priv->map_zoom < priv->min_zoom)
1686 osm_gps_map_set_zoom(map, priv->min_zoom);
1687
1688 } } break;
1689 case PROP_IMAGE_FORMAT:
1690 priv->image_format = g_value_dup_string (value);
1691 break;
1692 default:
1693 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1694 break;
1695 }
1696 }
1697
1698 static void
1699 osm_gps_map_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
1700 {
1701 g_return_if_fail (OSM_IS_GPS_MAP (object));
1702 float lat,lon;
1703 OsmGpsMap *map = OSM_GPS_MAP(object);
1704 OsmGpsMapPrivate *priv = map->priv;
1705
1706 switch (prop_id)
1707 {
1708 case PROP_AUTO_CENTER:
1709 g_value_set_boolean(value, priv->map_auto_center);
1710 break;
1711 case PROP_RECORD_TRIP_HISTORY:
1712 g_value_set_boolean(value, priv->record_trip_history);
1713 break;
1714 case PROP_SHOW_TRIP_HISTORY:
1715 g_value_set_boolean(value, priv->show_trip_history);
1716 break;
1717 case PROP_AUTO_DOWNLOAD:
1718 g_value_set_boolean(value, priv->map_auto_download);
1719 break;
1720 case PROP_REPO_URI:
1721 g_value_set_string(value, priv->repo_uri);
1722 break;
1723 case PROP_PROXY_URI:
1724 g_value_set_string(value, priv->proxy_uri);
1725 break;
1726 case PROP_TILE_CACHE_DIR:
1727 g_value_set_string(value, priv->cache_dir);
1728 break;
1729 case PROP_ZOOM:
1730 g_value_set_int(value, priv->map_zoom);
1731 break;
1732 case PROP_MAX_ZOOM:
1733 g_value_set_int(value, priv->max_zoom);
1734 break;
1735 case PROP_MIN_ZOOM:
1736 g_value_set_int(value, priv->min_zoom);
1737 break;
1738 case PROP_LATITUDE:
1739 lat = pixel2lat(priv->map_zoom,
1740 priv->map_y + (GTK_WIDGET(map)->allocation.height / 2));
1741 g_value_set_float(value, rad2deg(lat));
1742 break;
1743 case PROP_LONGITUDE:
1744 lon = pixel2lon(priv->map_zoom,
1745 priv->map_x + (GTK_WIDGET(map)->allocation.width / 2));
1746 g_value_set_float(value, rad2deg(lon));
1747 break;
1748 case PROP_MAP_X:
1749 g_value_set_int(value, priv->map_x);
1750 break;
1751 case PROP_MAP_Y:
1752 g_value_set_int(value, priv->map_y);
1753 break;
1754 case PROP_TILES_QUEUED:
1755 g_value_set_int(value, g_hash_table_size(priv->tile_queue));
1756 break;
1757 case PROP_GPS_TRACK_WIDTH:
1758 g_value_set_int(value, priv->ui_gps_track_width);
1759 break;
1760 case PROP_GPS_POINT_R1:
1761 g_value_set_int(value, priv->ui_gps_point_inner_radius);
1762 break;
1763 case PROP_GPS_POINT_R2:
1764 g_value_set_int(value, priv->ui_gps_point_outer_radius);
1765 break;
1766 case PROP_MAP_SOURCE:
1767 g_value_set_int(value, priv->map_source);
1768 break;
1769 case PROP_IMAGE_FORMAT:
1770 g_value_set_string(value, priv->image_format);
1771 break;
1772 default:
1773 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1774 break;
1775 }
1776 }
1777
1778 static gboolean
1779 osm_gps_map_scroll_event (GtkWidget *widget, GdkEventScroll *event)
1780 {
1781 OsmGpsMap *map = OSM_GPS_MAP(widget);
1782 OsmGpsMapPrivate *priv = map->priv;
1783
1784 if (event->direction == GDK_SCROLL_UP)
1785 {
1786 osm_gps_map_set_zoom(map, priv->map_zoom+1);
1787 }
1788 else
1789 {
1790 osm_gps_map_set_zoom(map, priv->map_zoom-1);
1791 }
1792
1793 return FALSE;
1794 }
1795
1796 static gboolean
1797 osm_gps_map_button_press (GtkWidget *widget, GdkEventButton *event)
1798 {
1799 OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1800
1801 #ifdef ENABLE_OSD
1802 /* pressed inside OSD control? */
1803 if(priv->osd) {
1804 osd_button_t but =
1805 priv->osd->check(priv->osd, TRUE, event->x, event->y);
1806
1807 if(but != OSD_NONE)
1808 {
1809 int step =
1810 GTK_WIDGET(widget)->allocation.width/OSM_GPS_MAP_SCROLL_STEP;
1811 priv->drag_counter = -1;
1812
1813 switch(but) {
1814 case OSD_UP:
1815 priv->map_y -= step;
1816 priv->center_coord_set = FALSE;
1817 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1818 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1819 break;
1820
1821 case OSD_DOWN:
1822 priv->map_y += step;
1823 priv->center_coord_set = FALSE;
1824 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1825 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1826 break;
1827
1828 case OSD_LEFT:
1829 priv->map_x -= step;
1830 priv->center_coord_set = FALSE;
1831 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1832 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1833 break;
1834
1835 case OSD_RIGHT:
1836 priv->map_x += step;
1837 priv->center_coord_set = FALSE;
1838 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1839 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1840 break;
1841
1842 case OSD_IN:
1843 osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom+1);
1844 break;
1845
1846 case OSD_OUT:
1847 osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom-1);
1848 break;
1849
1850 default:
1851 /* all custom buttons are forwarded to the application */
1852 if(priv->osd->cb)
1853 priv->osd->cb(but, priv->osd->data);
1854 break;
1855 }
1856
1857 return FALSE;
1858 }
1859 }
1860 #endif
1861
1862 priv->drag_counter = 0;
1863 priv->drag_start_mouse_x = (int) event->x;
1864 priv->drag_start_mouse_y = (int) event->y;
1865 priv->drag_start_map_x = priv->map_x;
1866 priv->drag_start_map_y = priv->map_y;
1867
1868 return FALSE;
1869 }
1870
1871 static gboolean
1872 osm_gps_map_button_release (GtkWidget *widget, GdkEventButton *event)
1873 {
1874 OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1875
1876 if (priv->dragging)
1877 {
1878 priv->dragging = FALSE;
1879
1880 priv->map_x = priv->drag_start_map_x;
1881 priv->map_y = priv->drag_start_map_y;
1882
1883 priv->map_x += (priv->drag_start_mouse_x - (int) event->x);
1884 priv->map_y += (priv->drag_start_mouse_y - (int) event->y);
1885
1886 priv->center_coord_set = FALSE;
1887
1888 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1889 }
1890 #ifdef ENABLE_OSD
1891 /* pressed inside OSD control? */
1892 else if(priv->osd)
1893 priv->osd->check(priv->osd, FALSE, event->x, event->y);
1894 #endif
1895
1896 #ifdef DRAG_DEBUG
1897 printf("dragging done\n");
1898 #endif
1899
1900 priv->drag_counter = -1;
1901
1902 return FALSE;
1903 }
1904
1905 static gboolean
1906 osm_gps_map_expose (GtkWidget *widget, GdkEventExpose *event);
1907
1908 static gboolean
1909 osm_gps_map_map_expose (GtkWidget *widget)
1910 {
1911 OsmGpsMapPrivate *priv = OSM_GPS_MAP(widget)->priv;
1912
1913 priv->drag_expose = 0;
1914 osm_gps_map_expose (widget, NULL);
1915 return FALSE;
1916 }
1917
1918 static gboolean
1919 osm_gps_map_motion_notify (GtkWidget *widget, GdkEventMotion *event)
1920 {
1921 int x, y;
1922 GdkModifierType state;
1923 OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1924
1925 if (event->is_hint)
1926 gdk_window_get_pointer (event->window, &x, &y, &state);
1927 else
1928 {
1929 x = event->x;
1930 y = event->y;
1931 state = event->state;
1932 }
1933
1934 // are we being dragged
1935 if (!(state & GDK_BUTTON1_MASK))
1936 return FALSE;
1937
1938 if (priv->drag_counter < 0)
1939 return FALSE;
1940
1941 /* not yet dragged far enough? */
1942 if(!priv->drag_counter &&
1943 ( (x - priv->drag_start_mouse_x) * (x - priv->drag_start_mouse_x) +
1944 (y - priv->drag_start_mouse_y) * (y - priv->drag_start_mouse_y) <
1945 10*10))
1946 return FALSE;
1947
1948 priv->drag_counter++;
1949
1950 priv->dragging = TRUE;
1951
1952 if (priv->map_auto_center)
1953 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1954
1955 priv->drag_mouse_dx = x - priv->drag_start_mouse_x;
1956 priv->drag_mouse_dy = y - priv->drag_start_mouse_y;
1957
1958 /* instead of redrawing directly just add an idle function */
1959 if (!priv->drag_expose)
1960 priv->drag_expose =
1961 g_idle_add ((GSourceFunc)osm_gps_map_map_expose, widget);
1962
1963 return FALSE;
1964 }
1965
1966 static gboolean
1967 osm_gps_map_configure (GtkWidget *widget, GdkEventConfigure *event)
1968 {
1969 OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1970
1971 /* create pixmap */
1972 if (priv->pixmap)
1973 g_object_unref (priv->pixmap);
1974
1975 priv->pixmap = gdk_pixmap_new (
1976 widget->window,
1977 widget->allocation.width + EXTRA_BORDER * 2,
1978 widget->allocation.height + EXTRA_BORDER * 2,
1979 -1);
1980
1981 #ifdef ENABLE_OSD
1982
1983 #ifdef OSD_DOUBLE_BUFFER
1984 if (priv->dbuf_pixmap)
1985 g_object_unref (priv->dbuf_pixmap);
1986
1987 priv->dbuf_pixmap = gdk_pixmap_new (
1988 widget->window,
1989 widget->allocation.width,
1990 widget->allocation.height,
1991 -1);
1992 #endif
1993
1994 /* the osd needs some references to map internal objects */
1995 if(priv->osd)
1996 priv->osd->widget = widget;
1997 #endif
1998
1999 /* and gc, used for clipping (I think......) */
2000 if(priv->gc_map)
2001 g_object_unref(priv->gc_map);
2002
2003 priv->gc_map = gdk_gc_new(priv->pixmap);
2004
2005 osm_gps_map_map_redraw(OSM_GPS_MAP(widget));
2006
2007 return FALSE;
2008 }
2009
2010 static gboolean
2011 osm_gps_map_expose (GtkWidget *widget, GdkEventExpose *event)
2012 {
2013 OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
2014
2015 #if defined(ENABLE_OSD) && defined(OSD_DOUBLE_BUFFER)
2016 GdkDrawable *drawable = priv->dbuf_pixmap;
2017 #else
2018 GdkDrawable *drawable = widget->window;
2019 #endif
2020
2021 #ifdef DRAG_DEBUG
2022 printf("expose, map %d/%d\n", priv->map_x, priv->map_y);
2023 #endif
2024
2025 if (!priv->drag_mouse_dx && !priv->drag_mouse_dy && event)
2026 {
2027 #ifdef DRAG_DEBUG
2028 printf(" dragging = %d, event = %p\n", priv->dragging, event);
2029 #endif
2030
2031 gdk_draw_drawable (drawable,
2032 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
2033 priv->pixmap,
2034 event->area.x + EXTRA_BORDER, event->area.y + EXTRA_BORDER,
2035 event->area.x, event->area.y,
2036 event->area.width, event->area.height);
2037 }
2038 else
2039 {
2040 #ifdef DRAG_DEBUG
2041 printf(" drag_mouse %d/%d\n",
2042 priv->drag_mouse_dx - EXTRA_BORDER,
2043 priv->drag_mouse_dy - EXTRA_BORDER);
2044 #endif
2045
2046 gdk_draw_drawable (drawable,
2047 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
2048 priv->pixmap,
2049 0,0,
2050 priv->drag_mouse_dx - EXTRA_BORDER,
2051 priv->drag_mouse_dy - EXTRA_BORDER,
2052 -1,-1);
2053
2054 //Paint white outside of the map if dragging. Its less
2055 //ugly than painting the corrupted map
2056 if(priv->drag_mouse_dx>EXTRA_BORDER) {
2057 gdk_draw_rectangle (drawable,
2058 widget->style->white_gc,
2059 TRUE,
2060 0, 0,
2061 priv->drag_mouse_dx - EXTRA_BORDER,
2062 widget->allocation.height);
2063 }
2064 else if (-priv->drag_mouse_dx > EXTRA_BORDER)
2065 {
2066 gdk_draw_rectangle (drawable,
2067 widget->style->white_gc,
2068 TRUE,
2069 priv->drag_mouse_dx + widget->allocation.width + EXTRA_BORDER, 0,
2070 -priv->drag_mouse_dx - EXTRA_BORDER,
2071 widget->allocation.height);
2072 }
2073
2074 if (priv->drag_mouse_dy>EXTRA_BORDER) {
2075 gdk_draw_rectangle (drawable,
2076 widget->style->white_gc,
2077 TRUE,
2078 0, 0,
2079 widget->allocation.width,
2080 priv->drag_mouse_dy - EXTRA_BORDER);
2081 }
2082 else if (-priv->drag_mouse_dy > EXTRA_BORDER)
2083 {
2084 gdk_draw_rectangle (drawable,
2085 widget->style->white_gc,
2086 TRUE,
2087 0, priv->drag_mouse_dy + widget->allocation.height + EXTRA_BORDER,
2088 widget->allocation.width,
2089 -priv->drag_mouse_dy - EXTRA_BORDER);
2090 }
2091 }
2092
2093 #ifdef ENABLE_OSD
2094 /* draw new OSD */
2095 if(priv->osd)
2096 priv->osd->draw (priv->osd, drawable);
2097
2098 #ifdef OSD_DOUBLE_BUFFER
2099 gdk_draw_drawable (widget->window,
2100 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
2101 priv->dbuf_pixmap,
2102 0,0,0,0,-1,-1);
2103 #endif
2104
2105 #endif
2106
2107 return FALSE;
2108 }
2109
2110 static void
2111 osm_gps_map_class_init (OsmGpsMapClass *klass)
2112 {
2113 GObjectClass* object_class = G_OBJECT_CLASS (klass);
2114 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2115
2116 g_type_class_add_private (klass, sizeof (OsmGpsMapPrivate));
2117
2118 object_class->dispose = osm_gps_map_dispose;
2119 object_class->finalize = osm_gps_map_finalize;
2120 object_class->constructor = osm_gps_map_constructor;
2121 object_class->set_property = osm_gps_map_set_property;
2122 object_class->get_property = osm_gps_map_get_property;
2123
2124 widget_class->expose_event = osm_gps_map_expose;
2125 widget_class->configure_event = osm_gps_map_configure;
2126 widget_class->button_press_event = osm_gps_map_button_press;
2127 widget_class->button_release_event = osm_gps_map_button_release;
2128 widget_class->motion_notify_event = osm_gps_map_motion_notify;
2129 widget_class->scroll_event = osm_gps_map_scroll_event;
2130
2131 g_object_class_install_property (object_class,
2132 PROP_AUTO_CENTER,
2133 g_param_spec_boolean ("auto-center",
2134 "auto center",
2135 "map auto center",
2136 TRUE,
2137 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2138
2139 g_object_class_install_property (object_class,
2140 PROP_RECORD_TRIP_HISTORY,
2141 g_param_spec_boolean ("record-trip-history",
2142 "record trip history",
2143 "should all gps points be recorded in a trip history",
2144 TRUE,
2145 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2146
2147 g_object_class_install_property (object_class,
2148 PROP_SHOW_TRIP_HISTORY,
2149 g_param_spec_boolean ("show-trip-history",
2150 "show trip history",
2151 "should the recorded trip history be shown on the map",
2152 TRUE,
2153 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2154
2155 g_object_class_install_property (object_class,
2156 PROP_AUTO_DOWNLOAD,
2157 g_param_spec_boolean ("auto-download",
2158 "auto download",
2159 "map auto download",
2160 TRUE,
2161 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2162
2163 g_object_class_install_property (object_class,
2164 PROP_REPO_URI,
2165 g_param_spec_string ("repo-uri",
2166 "repo uri",
2167 "map source tile repository uri",
2168 OSM_REPO_URI,
2169 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2170
2171 g_object_class_install_property (object_class,
2172 PROP_PROXY_URI,
2173 g_param_spec_string ("proxy-uri",
2174 "proxy uri",
2175 "http proxy uri on NULL",
2176 NULL,
2177 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2178
2179 g_object_class_install_property (object_class,
2180 PROP_TILE_CACHE_DIR,
2181 g_param_spec_string ("tile-cache",
2182 "tile cache",
2183 "osm local tile cache dir",
2184 NULL,
2185 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2186
2187 g_object_class_install_property (object_class,
2188 PROP_ZOOM,
2189 g_param_spec_int ("zoom",
2190 "zoom",
2191 "zoom level",
2192 MIN_ZOOM, /* minimum property value */
2193 MAX_ZOOM, /* maximum property value */
2194 3,
2195 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2196
2197 g_object_class_install_property (object_class,
2198 PROP_MAX_ZOOM,
2199 g_param_spec_int ("max-zoom",
2200 "max zoom",
2201 "maximum zoom level",
2202 MIN_ZOOM, /* minimum property value */
2203 MAX_ZOOM, /* maximum property value */
2204 OSM_MAX_ZOOM,
2205 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2206
2207 g_object_class_install_property (object_class,
2208 PROP_MIN_ZOOM,
2209 g_param_spec_int ("min-zoom",
2210 "min zoom",
2211 "minimum zoom level",
2212 MIN_ZOOM, /* minimum property value */
2213 MAX_ZOOM, /* maximum property value */
2214 OSM_MIN_ZOOM,
2215 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2216
2217 g_object_class_install_property (object_class,
2218 PROP_LATITUDE,
2219 g_param_spec_float ("latitude",
2220 "latitude",
2221 "latitude in degrees",
2222 -90.0, /* minimum property value */
2223 90.0, /* maximum property value */
2224 0,
2225 G_PARAM_READABLE));
2226
2227 g_object_class_install_property (object_class,
2228 PROP_LONGITUDE,
2229 g_param_spec_float ("longitude",
2230 "longitude",
2231 "longitude in degrees",
2232 -180.0, /* minimum property value */
2233 180.0, /* maximum property value */
2234 0,
2235 G_PARAM_READABLE));
2236
2237 g_object_class_install_property (object_class,
2238 PROP_MAP_X,
2239 g_param_spec_int ("map-x",
2240 "map-x",
2241 "initial map x location",
2242 G_MININT, /* minimum property value */
2243 G_MAXINT, /* maximum property value */
2244 890,
2245 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2246
2247 g_object_class_install_property (object_class,
2248 PROP_MAP_Y,
2249 g_param_spec_int ("map-y",
2250 "map-y",
2251 "initial map y location",
2252 G_MININT, /* minimum property value */
2253 G_MAXINT, /* maximum property value */
2254 515,
2255 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2256
2257 g_object_class_install_property (object_class,
2258 PROP_TILES_QUEUED,
2259 g_param_spec_int ("tiles-queued",
2260 "tiles-queued",
2261 "number of tiles currently waiting to download",
2262 G_MININT, /* minimum property value */
2263 G_MAXINT, /* maximum property value */
2264 0,
2265 G_PARAM_READABLE));
2266
2267 g_object_class_install_property (object_class,
2268 PROP_GPS_TRACK_WIDTH,
2269 g_param_spec_int ("gps-track-width",
2270 "gps-track-width",
2271 "width of the lines drawn for the gps track",
2272 1, /* minimum property value */
2273 G_MAXINT, /* maximum property value */
2274 4,
2275 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2276
2277 g_object_class_install_property (object_class,
2278 PROP_GPS_POINT_R1,
2279 g_param_spec_int ("gps-track-point-radius",
2280 "gps-track-point-radius",
2281 "radius of the gps point inner circle",
2282 0, /* minimum property value */
2283 G_MAXINT, /* maximum property value */
2284 10,
2285 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2286
2287 g_object_class_install_property (object_class,
2288 PROP_GPS_POINT_R2,
2289 g_param_spec_int ("gps-track-highlight-radius",
2290 "gps-track-highlight-radius",
2291 "radius of the gps point highlight circle",
2292 0, /* minimum property value */
2293 G_MAXINT, /* maximum property value */
2294 20,
2295 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2296
2297 g_object_class_install_property (object_class,
2298 PROP_MAP_SOURCE,
2299 g_param_spec_int ("map-source",
2300 "map source",
2301 "map source ID",
2302 -1, /* minimum property value */
2303 G_MAXINT, /* maximum property value */
2304 -1,
2305 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2306
2307 g_object_class_install_property (object_class,
2308 PROP_IMAGE_FORMAT,
2309 g_param_spec_string ("image-format",
2310 "image format",
2311 "map source tile repository image format (jpg, png)",
2312 OSM_IMAGE_FORMAT,
2313 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2314 }
2315
2316 const char*
2317 osm_gps_map_source_get_friendly_name(OsmGpsMapSource_t source)
2318 {
2319 switch(source)
2320 {
2321 case OSM_GPS_MAP_SOURCE_NULL:
2322 return "None";
2323 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2324 return "OpenStreetMap I";
2325 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2326 return "OpenStreetMap II";
2327 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2328 return "OpenCycleMap";
2329 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2330 return "OSMC Trails";
2331 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2332 return "Maps-For-Free";
2333 case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2334 return "Google Maps";
2335 case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2336 return "Google Satellite";
2337 case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2338 return "Google Hybrid";
2339 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2340 return "Virtual Earth";
2341 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2342 return "Virtual Earth Satellite";
2343 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2344 return "Virtual Earth Hybrid";
2345 case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2346 return "Yahoo Maps";
2347 case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2348 return "Yahoo Satellite";
2349 case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2350 return "Yahoo Hybrid";
2351 default:
2352 return NULL;
2353 }
2354 return NULL;
2355 }
2356
2357 //http://www.internettablettalk.com/forums/showthread.php?t=5209
2358 //https://garage.maemo.org/plugins/scmsvn/viewcvs.php/trunk/src/maps.c?root=maemo-mapper&view=markup
2359 //http://www.ponies.me.uk/maps/GoogleTileUtils.java
2360 //http://www.mgmaps.com/cache/MapTileCacher.perl
2361 const char*
2362 osm_gps_map_source_get_repo_uri(OsmGpsMapSource_t source)
2363 {
2364 switch(source)
2365 {
2366 case OSM_GPS_MAP_SOURCE_NULL:
2367 return "none://";
2368 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2369 return OSM_REPO_URI;
2370 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2371 return "http://tah.openstreetmap.org/Tiles/tile/#Z/#X/#Y.png";
2372 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2373 return "http://c.andy.sandbox.cloudmade.com/tiles/cycle/#Z/#X/#Y.png";
2374 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2375 return "http://topo.geofabrik.de/trails/#Z/#X/#Y.png";
2376 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2377 return "http://maps-for-free.com/layer/relief/z#Z/row#Y/#Z_#X-#Y.jpg";
2378 case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2379 return "http://mt#R.google.com/vt/v=w2.97&x=#X&y=#Y&z=#Z";
2380 case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2381 return "http://khm#R.google.com/kh?n=404&v=3&t=#Q";
2382 case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2383 return NULL; /* No longer working "http://mt#R.google.com/mt?n=404&v=w2t.99&x=#X&y=#Y&zoom=#S" */
2384 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2385 return "http://a#R.ortho.tiles.virtualearth.net/tiles/r#W.jpeg?g=50";
2386 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2387 return "http://a#R.ortho.tiles.virtualearth.net/tiles/a#W.jpeg?g=50";
2388 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2389 return "http://a#R.ortho.tiles.virtualearth.net/tiles/h#W.jpeg?g=50";
2390 case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2391 case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2392 case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2393 /* TODO: Implement signed Y, aka U
2394 * http://us.maps3.yimg.com/aerial.maps.yimg.com/ximg?v=1.7&t=a&s=256&x=%d&y=%-d&z=%d
2395 * x = tilex,
2396 * y = (1 << (MAX_ZOOM - zoom)) - tiley - 1,
2397 * z = zoom - (MAX_ZOOM - 17));
2398 */
2399 return NULL;
2400 default:
2401 return NULL;
2402 }
2403 return NULL;
2404 }
2405
2406 const char *
2407 osm_gps_map_source_get_image_format(OsmGpsMapSource_t source)
2408 {
2409 switch(source) {
2410 case OSM_GPS_MAP_SOURCE_NULL:
2411 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2412 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2413 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2414 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2415 return "png";
2416 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2417 case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2418 case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2419 case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2420 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2421 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2422 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2423 case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2424 case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2425 case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2426 return "jpg";
2427 default:
2428 return "bin";
2429 }
2430 return "bin";
2431 }
2432
2433
2434 int
2435 osm_gps_map_source_get_min_zoom(OsmGpsMapSource_t source)
2436 {
2437 return 1;
2438 }
2439
2440 int
2441 osm_gps_map_source_get_max_zoom(OsmGpsMapSource_t source)
2442 {
2443 switch(source) {
2444 case OSM_GPS_MAP_SOURCE_NULL:
2445 return 18;
2446 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2447 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2448 return OSM_MAX_ZOOM;
2449 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2450 case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2451 case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2452 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2453 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2454 case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2455 case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2456 case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2457 case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2458 return 17;
2459 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2460 return 15;
2461 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2462 return 11;
2463 case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2464 return 18;
2465 default:
2466 return 17;
2467 }
2468 return 17;
2469 }
2470
2471 void
2472 osm_gps_map_download_maps (OsmGpsMap *map, coord_t *pt1, coord_t *pt2, int zoom_start, int zoom_end)
2473 {
2474 int i,j,zoom,num_tiles;
2475 OsmGpsMapPrivate *priv = map->priv;
2476
2477 if (pt1 && pt2)
2478 {
2479 gchar *filename;
2480 num_tiles = 0;
2481 zoom_end = CLAMP(zoom_end, priv->min_zoom, priv->max_zoom);
2482 g_debug("Download maps: z:%d->%d",zoom_start, zoom_end);
2483
2484 for(zoom=zoom_start; zoom<=zoom_end; zoom++)
2485 {
2486 int x1,y1,x2,y2;
2487
2488 x1 = (int)floor((float)lon2pixel(zoom, pt1->rlon) / (float)TILESIZE);
2489 y1 = (int)floor((float)lat2pixel(zoom, pt1->rlat) / (float)TILESIZE);
2490
2491 x2 = (int)floor((float)lon2pixel(zoom, pt2->rlon) / (float)TILESIZE);
2492 y2 = (int)floor((float)lat2pixel(zoom, pt2->rlat) / (float)TILESIZE);
2493
2494 // loop x1-x2
2495 for(i=x1; i<=x2; i++)
2496 {
2497 // loop y1 - y2
2498 for(j=y1; j<=y2; j++)
2499 {
2500 // x = i, y = j
2501 filename = g_strdup_printf("%s%c%d%c%d%c%d.%s",
2502 priv->cache_dir, G_DIR_SEPARATOR,
2503 zoom, G_DIR_SEPARATOR,
2504 i, G_DIR_SEPARATOR,
2505 j,
2506 priv->image_format);
2507 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
2508 {
2509 osm_gps_map_download_tile(map, zoom, i, j, FALSE);
2510 num_tiles++;
2511 }
2512 g_free(filename);
2513 }
2514 }
2515 g_debug("DL @Z:%d = %d tiles",zoom,num_tiles);
2516 }
2517 }
2518 }
2519
2520 void
2521 osm_gps_map_get_bbox (OsmGpsMap *map, coord_t *pt1, coord_t *pt2)
2522 {
2523 OsmGpsMapPrivate *priv = map->priv;
2524
2525 if (pt1 && pt2) {
2526 pt1->rlat = pixel2lat(priv->map_zoom, priv->map_y);
2527 pt1->rlon = pixel2lon(priv->map_zoom, priv->map_x);
2528 pt2->rlat = pixel2lat(priv->map_zoom, priv->map_y + GTK_WIDGET(map)->allocation.height);
2529 pt2->rlon = pixel2lon(priv->map_zoom, priv->map_x + GTK_WIDGET(map)->allocation.width);
2530
2531 g_debug("BBOX: %f %f %f %f", pt1->rlat, pt1->rlon, pt2->rlat, pt2->rlon);
2532 }
2533 }
2534
2535 void
2536 osm_gps_map_set_mapcenter (OsmGpsMap *map, float latitude, float longitude, int zoom)
2537 {
2538 osm_gps_map_set_center (map, latitude, longitude);
2539 osm_gps_map_set_zoom (map, zoom);
2540 }
2541
2542 void
2543 osm_gps_map_set_center (OsmGpsMap *map, float latitude, float longitude)
2544 {
2545 int pixel_x, pixel_y;
2546 OsmGpsMapPrivate *priv;
2547
2548 g_return_if_fail (OSM_IS_GPS_MAP (map));
2549 priv = map->priv;
2550
2551 priv->center_rlat = deg2rad(latitude);
2552 priv->center_rlon = deg2rad(longitude);
2553 priv->center_coord_set = TRUE;
2554
2555 // pixel_x,y, offsets
2556 pixel_x = lon2pixel(priv->map_zoom, priv->center_rlon);
2557 pixel_y = lat2pixel(priv->map_zoom, priv->center_rlat);
2558
2559 priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;
2560 priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;
2561
2562 osm_gps_map_map_redraw_idle(map);
2563 }
2564
2565 int
2566 osm_gps_map_set_zoom (OsmGpsMap *map, int zoom)
2567 {
2568 int zoom_old;
2569 double factor = 0.0;
2570 int width_center, height_center;
2571 OsmGpsMapPrivate *priv;
2572
2573 g_return_val_if_fail (OSM_IS_GPS_MAP (map), 0);
2574 priv = map->priv;
2575
2576 if (zoom != priv->map_zoom)
2577 {
2578 width_center = GTK_WIDGET(map)->allocation.width / 2;
2579 height_center = GTK_WIDGET(map)->allocation.height / 2;
2580
2581 zoom_old = priv->map_zoom;
2582 //constrain zoom min_zoom -> max_zoom
2583 priv->map_zoom = CLAMP(zoom, priv->min_zoom, priv->max_zoom);
2584
2585 if (priv->center_coord_set)
2586 {
2587 priv->map_x = lon2pixel(priv->map_zoom, priv->center_rlon) - width_center;
2588 priv->map_y = lat2pixel(priv->map_zoom, priv->center_rlat) - height_center;
2589 }
2590 else
2591 {
2592 factor = exp(priv->map_zoom * M_LN2)/exp(zoom_old * M_LN2);
2593 priv->map_x = ((priv->map_x + width_center) * factor) - width_center;
2594 priv->map_y = ((priv->map_y + height_center) * factor) - height_center;
2595 }
2596
2597 g_debug("Zoom changed from %d to %d factor:%f x:%d",
2598 zoom_old, priv->map_zoom, factor, priv->map_x);
2599
2600 #ifdef ENABLE_OSD
2601 /* OSD may contain a scale, so we may have to re-render it */
2602 if(priv->osd && OSM_IS_GPS_MAP (priv->osd->widget))
2603 priv->osd->render (priv->osd);
2604 #endif
2605
2606 osm_gps_map_map_redraw_idle(map);
2607 }
2608 return priv->map_zoom;
2609 }
2610
2611 void
2612 osm_gps_map_add_track (OsmGpsMap *map, GSList *track)
2613 {
2614 OsmGpsMapPrivate *priv;
2615
2616 g_return_if_fail (OSM_IS_GPS_MAP (map));
2617 priv = map->priv;
2618
2619 if (track) {
2620 priv->tracks = g_slist_append(priv->tracks, track);
2621 osm_gps_map_map_redraw_idle(map);
2622 }
2623 }
2624
2625 void
2626 osm_gps_map_clear_tracks (OsmGpsMap *map)
2627 {
2628 g_return_if_fail (OSM_IS_GPS_MAP (map));
2629
2630 osm_gps_map_free_tracks(map);
2631 osm_gps_map_map_redraw_idle(map);
2632 }
2633
2634 void
2635 osm_gps_map_add_image (OsmGpsMap *map, float latitude, float longitude, GdkPixbuf *image)
2636 {
2637 g_return_if_fail (OSM_IS_GPS_MAP (map));
2638
2639 if (image) {
2640 OsmGpsMapPrivate *priv = map->priv;
2641 image_t *im;
2642
2643 //cache w/h for speed, and add image to list
2644 im = g_new0(image_t,1);
2645 im->w = gdk_pixbuf_get_width(image);
2646 im->h = gdk_pixbuf_get_height(image);
2647 im->pt.rlat = deg2rad(latitude);
2648 im->pt.rlon = deg2rad(longitude);
2649
2650 g_object_ref(image);
2651 im->image = image;
2652
2653 priv->images = g_slist_append(priv->images, im);
2654
2655 osm_gps_map_map_redraw_idle(map);
2656 }
2657 }
2658
2659 gboolean
2660 osm_gps_map_remove_image (OsmGpsMap *map, GdkPixbuf *image)
2661 {
2662 OsmGpsMapPrivate *priv = map->priv;
2663 if (priv->images) {
2664 GSList *list;
2665 for(list = priv->images; list != NULL; list = list->next)
2666 {
2667 image_t *im = list->data;
2668 if (im->image == image)
2669 {
2670 priv->images = g_slist_remove_link(priv->images, list);
2671 g_object_unref(im->image);
2672 g_free(im);
2673 osm_gps_map_map_redraw_idle(map);
2674 return TRUE;
2675 }
2676 }
2677 }
2678 return FALSE;
2679 }
2680
2681 void
2682 osm_gps_map_clear_images (OsmGpsMap *map)
2683 {
2684 g_return_if_fail (OSM_IS_GPS_MAP (map));
2685
2686 osm_gps_map_free_images(map);
2687 osm_gps_map_map_redraw_idle(map);
2688 }
2689
2690 void
2691 osm_gps_map_draw_gps (OsmGpsMap *map, float latitude, float longitude, float heading)
2692 {
2693 int pixel_x, pixel_y;
2694 OsmGpsMapPrivate *priv;
2695
2696 g_return_if_fail (OSM_IS_GPS_MAP (map));
2697 priv = map->priv;
2698
2699 priv->gps->rlat = deg2rad(latitude);
2700 priv->gps->rlon = deg2rad(longitude);
2701 priv->gps_valid = TRUE;
2702 priv->gps_heading = deg2rad(heading);
2703
2704 // pixel_x,y, offsets
2705 pixel_x = lon2pixel(priv->map_zoom, priv->gps->rlon);
2706 pixel_y = lat2pixel(priv->map_zoom, priv->gps->rlat);
2707
2708 //If trip marker add to list of gps points.
2709 if (priv->record_trip_history) {
2710 coord_t *tp = g_new0(coord_t,1);
2711 tp->rlat = priv->gps->rlat;
2712 tp->rlon = priv->gps->rlon;
2713 priv->trip_history = g_slist_append(priv->trip_history, tp);
2714 }
2715
2716 // dont draw anything if we are dragging
2717 if (priv->dragging) {
2718 g_debug("Dragging");
2719 return;
2720 }
2721
2722 //Automatically center the map if the track approaches the edge
2723 if(priv->map_auto_center) {
2724 int x = pixel_x - priv->map_x;
2725 int y = pixel_y - priv->map_y;
2726 int width = GTK_WIDGET(map)->allocation.width;
2727 int height = GTK_WIDGET(map)->allocation.height;
2728 if( x < (width/2 - width/8) || x > (width/2 + width/8) ||
2729 y < (height/2 - height/8) || y > (height/2 + height/8)) {
2730
2731 priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;
2732 priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;
2733 priv->center_coord_set = FALSE;
2734 }
2735 }
2736
2737 // this redraws the map (including the gps track, and adjusts the
2738 // map center if it was changed
2739 osm_gps_map_map_redraw_idle(map);
2740 }
2741
2742 void
2743 osm_gps_map_clear_gps (OsmGpsMap *map)
2744 {
2745 osm_gps_map_free_trip(map);
2746 osm_gps_map_map_redraw_idle(map);
2747 }
2748
2749 coord_t
2750 osm_gps_map_get_co_ordinates (OsmGpsMap *map, int pixel_x, int pixel_y)
2751 {
2752 coord_t coord;
2753 OsmGpsMapPrivate *priv = map->priv;
2754
2755 coord.rlat = pixel2lat(priv->map_zoom, priv->map_y + pixel_y);
2756 coord.rlon = pixel2lon(priv->map_zoom, priv->map_x + pixel_x);
2757 return coord;
2758 }
2759
2760 GtkWidget *
2761 osm_gps_map_new (void)
2762 {
2763 return g_object_new (OSM_TYPE_GPS_MAP, NULL);
2764 }
2765
2766 void
2767 osm_gps_map_screen_to_geographic (OsmGpsMap *map, gint pixel_x, gint pixel_y,
2768 gfloat *latitude, gfloat *longitude)
2769 {
2770 OsmGpsMapPrivate *priv;
2771
2772 g_return_if_fail (OSM_IS_GPS_MAP (map));
2773 priv = map->priv;
2774
2775 if (latitude)
2776 *latitude = rad2deg(pixel2lat(priv->map_zoom, priv->map_y + pixel_y));
2777 if (longitude)
2778 *longitude = rad2deg(pixel2lon(priv->map_zoom, priv->map_x + pixel_x));
2779 }
2780
2781 void
2782 osm_gps_map_geographic_to_screen (OsmGpsMap *map,
2783 gfloat latitude, gfloat longitude,
2784 gint *pixel_x, gint *pixel_y)
2785 {
2786 OsmGpsMapPrivate *priv;
2787
2788 g_return_if_fail (OSM_IS_GPS_MAP (map));
2789 priv = map->priv;
2790
2791 if (pixel_x)
2792 *pixel_x = lon2pixel(priv->map_zoom, deg2rad(longitude)) -
2793 priv->map_x + priv->drag_mouse_dx;
2794 if (pixel_y)
2795 *pixel_y = lat2pixel(priv->map_zoom, deg2rad(latitude)) -
2796 priv->map_y + priv->drag_mouse_dy;
2797 }
2798
2799 void
2800 osm_gps_map_scroll (OsmGpsMap *map, gint dx, gint dy)
2801 {
2802 OsmGpsMapPrivate *priv;
2803
2804 g_return_if_fail (OSM_IS_GPS_MAP (map));
2805 priv = map->priv;
2806
2807 priv->center_coord_set = FALSE;
2808 priv->map_x += dx;
2809 priv->map_y += dy;
2810
2811 #ifdef ENABLE_OSD
2812 /* OSD may contain a coordinate, so we may have to re-render it */
2813 if(priv->osd && OSM_IS_GPS_MAP (priv->osd->widget))
2814 priv->osd->render (priv->osd);
2815 #endif
2816
2817 osm_gps_map_map_redraw_idle (map);
2818 }
2819
2820 float
2821 osm_gps_map_get_scale(OsmGpsMap *map)
2822 {
2823 OsmGpsMapPrivate *priv;
2824
2825 g_return_val_if_fail (OSM_IS_GPS_MAP (map), OSM_GPS_MAP_INVALID);
2826 priv = map->priv;
2827
2828 return osm_gps_map_get_scale_at_point(priv->map_zoom, priv->center_rlat, priv->center_rlon);
2829 }
2830
2831 #ifdef ENABLE_OSD
2832
2833 void
2834 osm_gps_map_redraw (OsmGpsMap *map)
2835 {
2836 osm_gps_map_map_redraw_idle(map);
2837 }
2838
2839 osm_gps_map_osd_t *
2840 osm_gps_map_osd_get(OsmGpsMap *map)
2841 {
2842 g_return_val_if_fail (OSM_IS_GPS_MAP (map), NULL);
2843 return map->priv->osd;
2844 }
2845
2846 void
2847 osm_gps_map_register_osd(OsmGpsMap *map, osm_gps_map_osd_t *osd)
2848 {
2849 OsmGpsMapPrivate *priv;
2850
2851 g_return_if_fail (OSM_IS_GPS_MAP (map));
2852
2853 priv = map->priv;
2854 g_return_if_fail (!priv->osd);
2855
2856 priv->osd = osd;
2857 }
2858
2859 void
2860 osm_gps_map_repaint (OsmGpsMap *map)
2861 {
2862 osm_gps_map_expose (GTK_WIDGET(map), NULL);
2863 }
2864
2865 coord_t *
2866 osm_gps_map_get_gps (OsmGpsMap *map)
2867 {
2868 g_return_val_if_fail (OSM_IS_GPS_MAP (map), NULL);
2869
2870 if(!map->priv->gps_valid)
2871 return NULL;
2872
2873 return map->priv->gps;
2874 }
2875
2876 #endif