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

Parent Directory Parent Directory | Revision Log Revision Log


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