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

Parent Directory Parent Directory | Revision Log Revision Log


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