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

Parent Directory Parent Directory | Revision Log Revision Log


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