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

Parent Directory Parent Directory | Revision Log Revision Log


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