Diff of /trunk/src/osm-gps-map-osd-classic.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 70 by harbaum, Thu Aug 20 19:17:23 2009 UTC revision 132 by harbaum, Thu Oct 1 08:09:00 2009 UTC
# Line 1  Line 1 
1    /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */
2    /* vim:set et sw=4 ts=4 cino=t0,(0: */
3  /*  /*
4   * Copyright (C) Till Harbaum 2009 <till@harbaum.org>   * Copyright (C) Till Harbaum 2009 <till@harbaum.org>
5   *   *
# Line 15  Line 17 
17   * with this program.  If not, see <http://www.gnu.org/licenses/>.   * with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */   */
19    
20  #include "config.h"  #include "config.h"
21    #include <stdlib.h>  // abs
22    #include <string.h>
23    #include <math.h>    // M_PI/cos()
24    
25    /* parameters that can be overwritten from the config file: */
26    /* OSD_DIAMETER */
27    /* OSD_X, OSD_Y */
28    
29  #ifdef USE_CAIRO  #ifndef USE_CAIRO
30  #include <cairo.h>  #error "OSD control display lacks a non-cairo implementation!"
31  #endif  #endif
32    
33    #include <cairo.h>
34    
35    #include "osm-gps-map.h"
36    #include "converter.h"
37    #include "osm-gps-map-osd-classic.h"
38    
39  //the osd controls  //the osd controls
40  typedef struct {  typedef struct {
41      GdkPixmap *backup;      /* the offscreen representation of the OSD */
42      cairo_surface_t *overlay;      struct {
43      gint backup_x, backup_y;          cairo_surface_t *surface;
44      OsmGpsMapOsdGpsCallback cb;          gboolean rendered;
45      gpointer data;  #ifdef OSD_GPS_BUTTON
46            gboolean gps_enabled;
47    #endif
48        } controls;
49    
50    #ifdef OSD_BALLOON
51        //a balloon with additional info
52        struct {
53            cairo_surface_t *surface;
54            int orientation, offset_x, offset_y;
55    
56            gboolean just_created;
57            float lat, lon;
58            OsmGpsMapRect_t rect;
59    
60            /* function called to have the user app draw the contents */
61            OsmGpsMapBalloonCallback cb;
62            gpointer data;
63        } balloon;
64    #endif
65    
66    #ifdef OSD_SCALE
67        struct {
68            cairo_surface_t *surface;
69            int zoom;
70        } scale;
71    #endif
72    
73    #ifdef OSD_CROSSHAIR
74        struct {
75            cairo_surface_t *surface;
76            gboolean rendered;
77        } crosshair;
78    #endif
79    
80    #ifdef OSD_NAV
81        struct {
82            cairo_surface_t *surface;
83            float lat, lon;
84            char *name;
85        } nav;
86    #endif
87    
88    #ifdef OSD_COORDINATES
89        struct {
90            cairo_surface_t *surface;
91            float lat, lon;
92        } coordinates;
93    #endif
94    
95    #ifdef OSD_SOURCE_SEL
96        struct {
97            /* values to handle the "source" menu */
98            cairo_surface_t *surface;
99            gboolean expanded;
100            gint shift, dir, count;
101            gint handler_id;
102            gint width, height;
103            gboolean rendered;
104        } source_sel;
105    #endif
106    
107  } osd_priv_t;  } osd_priv_t;
108    
109  typedef struct {  #ifdef OSD_BALLOON
110    /* most visual effects are hardcoded by now, but may be made */
111    /* available via properties later */
112    #ifndef BALLOON_AREA_WIDTH
113    #define BALLOON_AREA_WIDTH           290
114    #endif
115    #ifndef BALLOON_AREA_HEIGHT
116    #define BALLOON_AREA_HEIGHT           75
117    #endif
118    #ifndef BALLOON_CORNER_RADIUS
119    #define BALLOON_CORNER_RADIUS         10
120    #endif
121    
122    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
123    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
124    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
125    #define BALLOON_TRANSPARENCY         0.8
126    #define POINTER_HEIGHT                20
127    #define POINTER_FOOT_WIDTH            20
128    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
129    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
130    #define BALLOON_SHADOW_TRANSPARENCY  0.2
131    
132    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
133    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
134    
135    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
136    
137    
138    /* draw the bubble shape. this is used twice, once for the shape and once */
139    /* for the shadow */
140    static void
141    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
142           gboolean bottom, int px, int py, int px0, int px1) {
143    
144        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
145        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
146                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
147        if(!bottom) {
148            /* insert top pointer */
149            cairo_line_to (cr, px1, y0);
150            cairo_line_to (cr, px, py);
151            cairo_line_to (cr, px0, y0);
152        }
153    
154        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
155        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
156                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
157        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
158        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
159                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
160        if(bottom) {
161            /* insert bottom pointer */
162            cairo_line_to (cr, px0, y1);
163            cairo_line_to (cr, px, py);
164            cairo_line_to (cr, px1, y1);
165        }
166    
167        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
168        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
169                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
170    
171        cairo_close_path (cr);
172    }
173    
174    static void
175    osd_render_balloon(osm_gps_map_osd_t *osd) {
176        osd_priv_t *priv = (osd_priv_t*)osd->priv;
177    
178        /* get zoom */
179        gint zoom;
180        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
181    
182        /* ------- convert given coordinate into screen position --------- */
183        gint xs, ys;
184        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
185                                          priv->balloon.lat, priv->balloon.lon,
186                                          &xs, &ys);
187    
188        gint x0 = 1, y0 = 1;
189    
190        /* check position of this relative to screen center to determine */
191        /* pointer direction ... */
192        int pointer_x, pointer_x0, pointer_x1;
193        int pointer_y;
194    
195        /* ... and calculate position */
196        int orientation = 0;
197        if(xs > osd->widget->allocation.width/2) {
198            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
199            pointer_x = x0 - priv->balloon.offset_x;
200            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
201            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
202            orientation |= 1;
203        } else {
204            priv->balloon.offset_x = -POINTER_OFFSET;
205            pointer_x = x0 - priv->balloon.offset_x;
206            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
207            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
208        }
209    
210        gboolean bottom = FALSE;
211        if(ys > osd->widget->allocation.height/2) {
212            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
213            pointer_y = y0 - priv->balloon.offset_y;
214            bottom = TRUE;
215            orientation |= 2;
216        } else {
217            priv->balloon.offset_y = 0;
218            pointer_y = y0 - priv->balloon.offset_y;
219            y0 += POINTER_HEIGHT;
220        }
221    
222        /* if required orientation equals current one, then don't render */
223        /* anything */
224        if(orientation == priv->balloon.orientation)
225            return;
226    
227        priv->balloon.orientation = orientation;
228    
229        /* calculate bottom/right of box */
230        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
231    
232        /* save balloon screen coordinates for later use */
233        priv->balloon.rect.x = x0 + BALLOON_BORDER;
234        priv->balloon.rect.y = y0 + BALLOON_BORDER;
235        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
236        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
237    
238        cairo_t *cr = cairo_create(priv->balloon.surface);
239        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
240        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
241        cairo_paint(cr);
242        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
243    
244        /* --------- draw shadow --------------- */
245        osm_gps_map_draw_balloon_shape (cr,
246                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
247                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
248                     bottom, pointer_x, pointer_y,
249                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
250    
251        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
252        cairo_fill_preserve (cr);
253        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
254        cairo_set_line_width (cr, 0);
255        cairo_stroke (cr);
256    
257        /* --------- draw main shape ----------- */
258        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
259                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
260    
261        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
262        cairo_fill_preserve (cr);
263        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
264        cairo_set_line_width (cr, 1);
265        cairo_stroke (cr);
266    
267        if (priv->balloon.cb) {
268            /* clip in case application tries to draw in */
269                /* exceed of the balloon */
270            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
271                             priv->balloon.rect.w, priv->balloon.rect.h);
272            cairo_clip (cr);
273            cairo_new_path (cr);  /* current path is not
274                                     consumed by cairo_clip() */
275    
276            priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
277        }
278    
279        cairo_destroy(cr);
280    }
281    
282    /* return true if balloon is being displayed and if */
283    /* the given coordinate is within this balloon */
284    static gboolean
285    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y)
286    {
287        osd_priv_t *priv = (osd_priv_t*)osd->priv;
288    
289        if(!priv->balloon.surface)
290            return FALSE;
291    
292        gint xs, ys;
293        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
294                                          priv->balloon.lat, priv->balloon.lon,
295                                          &xs, &ys);
296    
297        xs += priv->balloon.rect.x + priv->balloon.offset_x;
298        ys += priv->balloon.rect.y + priv->balloon.offset_y;
299    
300        gboolean is_in =
301            (x > xs) && (x < xs + priv->balloon.rect.w) &&
302            (y > ys) && (y < ys + priv->balloon.rect.h);
303    
304        /* handle the fact that the balloon may have been created by the */
305        /* button down event */
306        if(!is_in && !down && !priv->balloon.just_created) {
307            /* the user actually clicked outside the balloon */
308    
309            /* close the balloon! */
310            osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
311        }
312    
313        return is_in;
314    }
315    
316    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
317        g_return_if_fail (OSM_IS_GPS_MAP (map));
318    
319      gpointer priv;      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
320  } osm_gps_map_osd_t;      g_return_if_fail (osd);
321    
322        osd_priv_t *priv = (osd_priv_t*)osd->priv;
323        g_return_if_fail (priv);
324    
325        if(priv->balloon.surface) {
326            cairo_surface_destroy(priv->balloon.surface);
327            priv->balloon.surface = NULL;
328            priv->balloon.lat = OSM_GPS_MAP_INVALID;
329            priv->balloon.lon = OSM_GPS_MAP_INVALID;
330        }
331        osm_gps_map_redraw(map);
332    }
333    
334    void
335    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
336                                  OsmGpsMapBalloonCallback cb, gpointer data) {
337        g_return_if_fail (OSM_IS_GPS_MAP (map));
338    
339        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
340        g_return_if_fail (osd);
341    
342        osd_priv_t *priv = (osd_priv_t*)osd->priv;
343        g_return_if_fail (priv);
344    
345        osm_gps_map_osd_clear_balloon (map);
346    
347        /* allocate balloon surface */
348        priv->balloon.surface =
349            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
350                                       BALLOON_W+2, BALLOON_H+2);
351    
352        priv->balloon.lat = latitude;
353        priv->balloon.lon = longitude;
354        priv->balloon.cb = cb;
355        priv->balloon.data = data;
356        priv->balloon.just_created = TRUE;
357    
358        priv->balloon.orientation = -1;
359    
360        osd_render_balloon(osd);
361    
362        osm_gps_map_redraw(map);
363    }
364    
365    #endif // OSD_BALLOON
366    
367  /* position and extent of bounding box */  /* position and extent of bounding box */
368    #ifndef OSD_X
369  #define OSD_X      (10)  #define OSD_X      (10)
370  #define OSD_Y      (10)  #endif
371    
372  #define OSD_COLOR            0.5, 0.5, 1  #ifndef OSD_Y
373  #define OSD_COLOR_DISABLED   0.8, 0.8, 0.8  #define OSD_Y      (10)
374    #endif
375    
376  /* parameters of the direction shape */  /* parameters of the direction shape */
377  #ifndef OSM_GPS_MAP_OSD_DIAMETER  #ifndef OSD_DIAMETER
378  #define D_RAD  (30)         // diameter of dpad  #define D_RAD  (30)         // diameter of dpad
379  #else  #else
380  #define D_RAD  (OSM_GPS_MAP_OSD_DIAMETER)  #define D_RAD  (OSD_DIAMETER)
381  #endif  #endif
382  #define D_TIP  (4*D_RAD/5)  // distance of arrow tip from dpad center  #define D_TIP  (4*D_RAD/5)  // distance of arrow tip from dpad center
383  #define D_LEN  (D_RAD/4)    // length of arrow  #define D_LEN  (D_RAD/4)    // length of arrow
# Line 59  typedef struct { Line 387  typedef struct {
387  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom
388  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar
389    
390    #ifdef OSD_SHADOW_ENABLE
391  /* shadow also depends on control size */  /* shadow also depends on control size */
392  #define OSD_SHADOW (D_RAD/6)  #define OSD_SHADOW (D_RAD/6)
393    #else
394    #define OSD_SHADOW (0)
395    #endif
396    
397    /* normally the GPS button is in the center of the dpad. if there's */
398    /* no dpad it will go into the zoom area */
399    #if defined(OSD_GPS_BUTTON) && defined(OSD_NO_DPAD)
400    #define Z_GPS  1
401    #else
402    #define Z_GPS  0
403    #endif
404    
405  /* total width and height of controls incl. shadow */  /* total width and height of controls incl. shadow */
406  #define OSD_W    (2*D_RAD + OSD_SHADOW)  #define OSD_W    (2*D_RAD + OSD_SHADOW + Z_GPS * 2 * Z_RAD)
407    #if !Z_GPS
408  #define OSD_H    (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)  #define OSD_H    (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)
409    #else
410    #define OSD_H    (2*Z_RAD + OSD_SHADOW)
411    #endif
412    
413    #ifdef OSD_SHADOW_ENABLE
414  #define OSD_LBL_SHADOW (OSD_SHADOW/2)  #define OSD_LBL_SHADOW (OSD_SHADOW/2)
415    #endif
416    
417    #define Z_TOP    ((1-Z_GPS) * (2 * D_RAD + Z_STEP))
418    
 #define Z_TOP    (2 * D_RAD + Z_STEP)  
419  #define Z_MID    (Z_TOP + Z_RAD)  #define Z_MID    (Z_TOP + Z_RAD)
420  #define Z_BOT    (Z_MID + Z_RAD)  #define Z_BOT    (Z_MID + Z_RAD)
421  #define Z_LEFT   (Z_RAD)  #define Z_LEFT   (Z_RAD)
422  #define Z_RIGHT  (2 * D_RAD - Z_RAD)  #define Z_RIGHT  (2 * D_RAD - Z_RAD + Z_GPS * 2 * Z_RAD)
423    #define Z_CENTER ((Z_RIGHT + Z_LEFT)/2)
424    
425  /* create the cairo shape used for the zoom buttons */  /* create the cairo shape used for the zoom buttons */
426  static void  static void
427  osm_gps_map_osd_zoom_shape(cairo_t *cr, gint x, gint y) {  osd_zoom_shape(cairo_t *cr, gint x, gint y)
428    {
429      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);
430      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);
431      cairo_arc     (cr, x+Z_RIGHT,   y+Z_MID, Z_RAD, -M_PI/2,  M_PI/2);      cairo_arc     (cr, x+Z_RIGHT,   y+Z_MID, Z_RAD, -M_PI/2,  M_PI/2);
# Line 84  osm_gps_map_osd_zoom_shape(cairo_t *cr, Line 433  osm_gps_map_osd_zoom_shape(cairo_t *cr,
433      cairo_arc     (cr, x+Z_LEFT,    y+Z_MID, Z_RAD,  M_PI/2, -M_PI/2);      cairo_arc     (cr, x+Z_LEFT,    y+Z_MID, Z_RAD,  M_PI/2, -M_PI/2);
434  }  }
435    
436    /* ------------------- color/shadow functions ----------------- */
437    
438    #ifndef OSD_COLOR
439    /* if no color has been specified we just use the gdks default colors */
440    static void
441    osd_labels(cairo_t *cr, gint width, gboolean enabled,
442                           GdkColor *fg, GdkColor *disabled) {
443        if(enabled)  gdk_cairo_set_source_color(cr, fg);
444        else         gdk_cairo_set_source_color(cr, disabled);
445        cairo_set_line_width (cr, width);
446    }
447    #else
448    static void
449    osd_labels(cairo_t *cr, gint width, gboolean enabled) {
450        if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);
451        else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
452        cairo_set_line_width (cr, width);
453    }
454    #endif
455    
456    #ifdef OSD_SHADOW_ENABLE
457    static void
458    osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
459        cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
460        cairo_set_line_width (cr, width);
461    }
462    #endif
463    
464    #ifndef OSD_NO_DPAD
465  /* create the cairo shape used for the dpad */  /* create the cairo shape used for the dpad */
466  static void  static void
467  osm_gps_map_osd_dpad_shape(cairo_t *cr, gint x, gint y) {  osd_dpad_shape(cairo_t *cr, gint x, gint y)
468    {
469      cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);      cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);
470  }  }
471    #endif
472    
473    #ifdef OSD_SHADOW_ENABLE
474    static void
475    osd_shape_shadow(cairo_t *cr) {
476        cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
477        cairo_fill (cr);
478        cairo_stroke (cr);
479    }
480    #endif
481    
482    #ifndef OSD_COLOR
483    /* if no color has been specified we just use the gdks default colors */
484    static void
485    osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
486        gdk_cairo_set_source_color(cr, bg);
487        cairo_fill_preserve (cr);
488        gdk_cairo_set_source_color(cr, fg);
489        cairo_set_line_width (cr, 1);
490        cairo_stroke (cr);
491    }
492    #else
493    static void
494    osd_shape(cairo_t *cr) {
495        cairo_set_source_rgb (cr, OSD_COLOR_BG);
496        cairo_fill_preserve (cr);
497        cairo_set_source_rgb (cr, OSD_COLOR);
498        cairo_set_line_width (cr, 1);
499        cairo_stroke (cr);
500    }
501    #endif
502    
 typedef enum {  
     OSD_NONE = 0,  
     OSD_BG,  
     OSD_UP,  
     OSD_DOWN,  
     OSD_LEFT,  
     OSD_RIGHT,  
     OSD_IN,  
     OSD_OUT,  
     OSD_GPS  
 } osd_button_t;  
503    
504  static gboolean  static gboolean
505  osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)  osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)
# Line 108  osm_gps_map_in_circle(gint x, gint y, gi Line 507  osm_gps_map_in_circle(gint x, gint y, gi
507      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
508  }  }
509    
510    #ifndef OSD_NO_DPAD
511  /* check whether x/y is within the dpad */  /* check whether x/y is within the dpad */
512  static osd_button_t  static osd_button_t
513  osm_gps_map_osd_check_dpad(gint x, gint y)  osd_check_dpad(gint x, gint y)
514  {  {
515      /* within entire dpad circle */      /* within entire dpad circle */
516      if( osm_gps_map_in_circle(x, y, OSD_X + D_RAD, OSD_Y + D_RAD, D_RAD))      if( osm_gps_map_in_circle(x, y, D_RAD, D_RAD, D_RAD))
517      {      {
518          /* convert into position relative to dpads centre */          /* convert into position relative to dpads centre */
519          x -= (OSD_X + D_RAD);          x -= D_RAD;
520          y -= (OSD_Y + D_RAD);          y -= D_RAD;
521    
522    #ifdef OSD_GPS_BUTTON
523          /* check for dpad center goes here! */          /* check for dpad center goes here! */
524          if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))          if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))
525              return OSD_GPS;              return OSD_GPS;
526    #endif
527    
528          if( y < 0 && abs(x) < abs(y))          if( y < 0 && abs(x) < abs(y))
529              return OSD_UP;              return OSD_UP;
# Line 139  osm_gps_map_osd_check_dpad(gint x, gint Line 541  osm_gps_map_osd_check_dpad(gint x, gint
541      }      }
542      return OSD_NONE;      return OSD_NONE;
543  }  }
544    #endif
545    
546  /* check whether x/y is within the zoom pads */  /* check whether x/y is within the zoom pads */
547  static osd_button_t  static osd_button_t
548  osm_gps_map_osd_check_zoom(gint x, gint y) {  osd_check_zoom(gint x, gint y) {
549      if( x > OSD_X && x < (OSD_X + OSD_W) && y > Z_TOP && y < (OSD_Y+Z_BOT)) {      if( x > 0 && x < OSD_W && y > Z_TOP && y < Z_BOT) {
550    
551          /* within circle around (-) label */          /* within circle around (-) label */
552          if( osm_gps_map_in_circle(x, y, OSD_X + Z_LEFT, OSD_Y + Z_MID, Z_RAD))          if( osm_gps_map_in_circle(x, y, Z_LEFT, Z_MID, Z_RAD))
             return OSD_OUT;  
   
         /* between center of (-) button and center of entire zoom control area */  
         if(x > OSD_LEFT && x < OSD_X + D_RAD)  
553              return OSD_OUT;              return OSD_OUT;
554    
555          /* within circle around (+) label */          /* within circle around (+) label */
556          if( osm_gps_map_in_circle(x, y, OSD_X + Z_RIGHT, OSD_Y + Z_MID, Z_RAD))          if( osm_gps_map_in_circle(x, y, Z_RIGHT, Z_MID, Z_RAD))
557              return OSD_IN;              return OSD_IN;
558    
559    #if Z_GPS == 1
560            /* within square around center */
561            if( x > Z_CENTER - Z_RAD && x < Z_CENTER + Z_RAD)
562                return OSD_GPS;
563    #endif
564    
565            /* between center of (-) button and center of entire zoom control area */
566            if(x > OSD_LEFT && x < D_RAD)
567                return OSD_OUT;
568    
569          /* between center of (+) button and center of entire zoom control area */          /* between center of (+) button and center of entire zoom control area */
570          if(x < OSD_RIGHT && x > OSD_X + D_RAD)          if(x < OSD_RIGHT && x > D_RAD)
571              return OSD_IN;              return OSD_IN;
572      }      }
573    
574      return OSD_NONE;      return OSD_NONE;
575  }  }
576    
577  static osd_button_t  #ifdef OSD_SOURCE_SEL
 osm_gps_map_osd_check(gint x, gint y) {  
     osd_button_t but = OSD_NONE;  
578    
579      /* first do a rough test for the OSD area. */  /* place source selection at right border */
580      /* this is just to avoid an unnecessary detailed test */  #define OSD_S_RAD (Z_RAD)
581      if(x > OSD_X && x < OSD_X + OSD_W &&  #define OSD_S_X   (-OSD_X)
582         y > OSD_Y && y < OSD_Y + OSD_H) {  #define OSD_S_Y   (OSD_Y)
583          but = osm_gps_map_osd_check_dpad(x, y);  #define OSD_S_PW  (2 * Z_RAD)
584    #define OSD_S_W   (OSD_S_PW)
585    #define OSD_S_PH  (2 * Z_RAD)
586    #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
587    
588    /* size of usable area when expanded */
589    #define OSD_S_AREA_W (priv->source_sel.width)
590    #define OSD_S_AREA_H (priv->source_sel.height)
591    #define OSD_S_EXP_W  (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
592    #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
593    
594    /* internal value to draw the arrow on the "puller" */
595    #define OSD_S_D0  (OSD_S_RAD/2)
596    #ifndef OSD_FONT_SIZE
597    #define OSD_FONT_SIZE 16.0
598    #endif
599    #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)
600    #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)
601    
602          if(but == OSD_NONE)  /* draw the shape of the source selection OSD, either only the puller (not expanded) */
603              but = osm_gps_map_osd_check_zoom(x, y);  /* or the entire menu incl. the puller (expanded) */
604    static void
605    osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
606        if(!priv->source_sel.expanded) {
607            /* just draw the puller */
608            cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
609            cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
610            cairo_line_to (cr, x + OSD_S_PW, y);
611        } else {
612            /* draw the puller and the area itself */
613            cairo_move_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y + OSD_S_AREA_H);
614            cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H);
615            if(OSD_S_Y > 0) {
616                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_PH);
617                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
618            } else {
619                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_AREA_H-OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
620                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H - OSD_S_PH);
621                cairo_line_to (cr, x + OSD_S_PW, y);
622            }
623            cairo_line_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y);
624            cairo_close_path (cr);
625      }      }
626    }
627    
628      return but;  static void
629    osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
630        osd_priv_t *priv = (osd_priv_t*)osd->priv;
631    
632        int py = offset + OSD_S_RAD - OSD_S_D0;
633    
634        if(!priv->source_sel.expanded) {
635            /* draw the "puller" open (<) arrow */
636            cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
637            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
638            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
639        } else {
640            if(OSD_S_Y < 0)
641                py += OSD_S_AREA_H - OSD_S_PH;
642    
643            /* draw the "puller" close (>) arrow */
644            cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
645            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
646            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
647            cairo_stroke(cr);
648    
649            /* don't draw a shadow for the text content */
650            if(offset == 1) {
651                gint source;
652                g_object_get(osd->widget, "map-source", &source, NULL);
653    
654                cairo_select_font_face (cr, "Sans",
655                                        CAIRO_FONT_SLANT_NORMAL,
656                                        CAIRO_FONT_WEIGHT_BOLD);
657                cairo_set_font_size (cr, OSD_FONT_SIZE);
658    
659                int i, step = (priv->source_sel.height - 2*OSD_TEXT_BORDER) /
660                    OSM_GPS_MAP_SOURCE_LAST;
661                for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
662                    cairo_text_extents_t extents;
663                    const char *src = osm_gps_map_source_get_friendly_name(i);
664                    cairo_text_extents (cr, src, &extents);
665    
666                    int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
667                    int y = offset + step * (i-1) + OSD_TEXT_BORDER;
668    
669                    /* draw filled rectangle if selected */
670                    if(source == i) {
671                        cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
672                                        y - OSD_TEXT_SKIP,
673                                        priv->source_sel.width - OSD_TEXT_BORDER,
674                                        step + OSD_TEXT_SKIP);
675                        cairo_fill(cr);
676    
677                        /* temprarily draw with background color */
678    #ifndef OSD_COLOR
679                        GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
680                        gdk_cairo_set_source_color(cr, &bg);
681    #else
682                        cairo_set_source_rgb (cr, OSD_COLOR_BG);
683    #endif
684                    }
685    
686                    cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
687                    cairo_show_text (cr, src);
688    
689                    /* restore color */
690                    if(source == i) {
691    #ifndef OSD_COLOR
692                        GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
693                        gdk_cairo_set_source_color(cr, &fg);
694    #else
695                        cairo_set_source_rgb (cr, OSD_COLOR);
696    #endif
697                    }
698                }
699            }
700        }
701  }  }
702    
703  static void  static void
704  osm_gps_map_osd_shape_shadow(cairo_t *cr) {  osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
705      cairo_set_source_rgba (cr, 0, 0, 0, 0.2);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
706      cairo_fill (cr);  
707        if(priv->source_sel.rendered && !force_rerender)
708            return;
709    
710        priv->source_sel.rendered = TRUE;
711    
712    #ifndef OSD_COLOR
713        GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
714        GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
715        GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
716    #endif
717    
718        /* draw source selector */
719        cairo_t *cr = cairo_create(priv->source_sel.surface);
720        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
721        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
722        cairo_paint(cr);
723        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
724    
725    #ifdef OSD_SHADOW_ENABLE
726        osd_source_shape(priv, cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
727        osd_shape_shadow(cr);
728    #endif
729    
730        osd_source_shape(priv, cr, 1, 1);
731    #ifndef OSD_COLOR
732        osd_shape(cr, &bg, &fg);
733    #else
734        osd_shape(cr);
735    #endif
736    
737    #ifdef OSD_SHADOW_ENABLE
738        osd_labels_shadow(cr, Z_RAD/3, TRUE);
739        osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
740      cairo_stroke (cr);      cairo_stroke (cr);
741    #endif
742    #ifndef OSD_COLOR
743        osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
744    #else
745        osd_labels(cr, Z_RAD/3, TRUE);
746    #endif
747        osd_source_content(osd, cr, 1);
748        cairo_stroke (cr);
749    
750        cairo_destroy(cr);
751  }  }
752    
753  static void  /* re-allocate the buffer used to draw the menu. This is used */
754  osm_gps_map_osd_shape(cairo_t *cr) {  /* to collapse/expand the buffer */
755      cairo_set_source_rgb (cr, 1, 1, 1);  static void
756      cairo_fill_preserve (cr);  osd_source_reallocate(osm_gps_map_osd_t *osd) {
757      cairo_set_source_rgb (cr, OSD_COLOR);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
758      cairo_set_line_width (cr, 1);  
759      cairo_stroke (cr);      /* re-allocate offscreen bitmap */
760        g_assert (priv->source_sel.surface);
761    
762        int w = OSD_S_W, h = OSD_S_H;
763        if(priv->source_sel.expanded) {
764            cairo_text_extents_t extents;
765    
766            /* determine content size */
767            cairo_t *cr = cairo_create(priv->source_sel.surface);
768            cairo_select_font_face (cr, "Sans",
769                                    CAIRO_FONT_SLANT_NORMAL,
770                                    CAIRO_FONT_WEIGHT_BOLD);
771            cairo_set_font_size (cr, OSD_FONT_SIZE);
772    
773            /* calculate menu size */
774            int i, max_h = 0, max_w = 0;
775            for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
776                const char *src = osm_gps_map_source_get_friendly_name(i);
777                cairo_text_extents (cr, src, &extents);
778    
779                if(extents.width > max_w) max_w = extents.width;
780                if(extents.height > max_h) max_h = extents.height;
781            }
782            cairo_destroy(cr);
783    
784            priv->source_sel.width  = max_w + 2*OSD_TEXT_BORDER;
785            priv->source_sel.height = OSM_GPS_MAP_SOURCE_LAST *
786                (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
787    
788            w = OSD_S_EXP_W;
789            h = OSD_S_EXP_H;
790        }
791    
792        cairo_surface_destroy(priv->source_sel.surface);
793        priv->source_sel.surface =
794            cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
795    
796        osd_render_source_sel(osd, TRUE);
797    }
798    
799    #define OSD_HZ      15
800    #define OSD_TIME    500
801    
802    static gboolean osd_source_animate(gpointer data) {
803        osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
804        osd_priv_t *priv = (osd_priv_t*)osd->priv;
805        int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
806        gboolean done = FALSE;
807        priv->source_sel.count += priv->source_sel.dir;
808    
809        /* shifting in */
810        if(priv->source_sel.dir < 0) {
811            if(priv->source_sel.count <= 0) {
812                priv->source_sel.count = 0;
813                done = TRUE;
814            }
815        } else {
816            if(priv->source_sel.count >= 1000) {
817                priv->source_sel.expanded = FALSE;
818                osd_source_reallocate(osd);
819    
820                priv->source_sel.count = 1000;
821                done = TRUE;
822            }
823        }
824    
825    
826        /* count runs linearly from 0 to 1000, map this nicely onto a position */
827    
828        /* nice sinoid mapping */
829        float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;
830        priv->source_sel.shift =
831            (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
832            m * diff;
833    
834        /* make sure the screen is updated */
835        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
836    
837        /* stop animation if done */
838        if(done)
839            priv->source_sel.handler_id = 0;
840    
841        return !done;
842    }
843    
844    /* switch between expand and collapse mode of source selection */
845    static void
846    osd_source_toggle(osm_gps_map_osd_t *osd)
847    {
848        osd_priv_t *priv = (osd_priv_t*)osd->priv;
849    
850        /* ignore clicks while animation is running */
851        if(priv->source_sel.handler_id)
852            return;
853    
854        /* expand immediately, collapse is handle at the end of the */
855        /* collapse animation */
856        if(!priv->source_sel.expanded) {
857            priv->source_sel.expanded = TRUE;
858            osd_source_reallocate(osd);
859    
860            priv->source_sel.count = 1000;
861            priv->source_sel.shift = osd->widget->allocation.width - OSD_S_W;
862            priv->source_sel.dir = -1000/OSD_HZ;
863        } else {
864            priv->source_sel.count =  0;
865            priv->source_sel.shift = osd->widget->allocation.width -
866                OSD_S_EXP_W + OSD_S_X;
867            priv->source_sel.dir = +1000/OSD_HZ;
868        }
869    
870        /* start timer to handle animation */
871        priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
872                                                      osd_source_animate, osd);
873    }
874    
875    /* check if the user clicked inside the source selection area */
876    static osd_button_t
877    osd_source_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
878        osd_priv_t *priv = (osd_priv_t*)osd->priv;
879    
880        if(!priv->source_sel.expanded)
881            x -= osd->widget->allocation.width - OSD_S_W;
882        else
883            x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
884    
885        if(OSD_S_Y > 0)
886            y -= OSD_S_Y;
887        else
888            y -= osd->widget->allocation.height - OSD_S_PH + OSD_S_Y;
889    
890        /* within square around puller? */
891        if(y > 0 && y < OSD_S_PH && x > 0 && x < OSD_S_PW) {
892            /* really within puller shape? */
893            if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
894                /* expand source selector */
895                if(down)
896                    osd_source_toggle(osd);
897    
898                /* tell upper layers that user clicked some background element */
899                /* of the OSD */
900                return OSD_BG;
901            }
902        }
903    
904        /* check for clicks into data area */
905        if(priv->source_sel.expanded && !priv->source_sel.handler_id) {
906            /* re-adjust from puller top to content top */
907            if(OSD_S_Y < 0)
908                y += OSD_S_EXP_H - OSD_S_PH;
909    
910            if(x > OSD_S_PW &&
911               x < OSD_S_PW + OSD_S_EXP_W &&
912               y > 0 &&
913               y < OSD_S_EXP_H) {
914    
915                int step = (priv->source_sel.height - 2*OSD_TEXT_BORDER)
916                    / OSM_GPS_MAP_SOURCE_LAST;
917    
918                y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
919                y /= step;
920                y += 1;
921    
922                if(down) {
923                    gint old = 0;
924                    g_object_get(osd->widget, "map-source", &old, NULL);
925    
926                    if(y > OSM_GPS_MAP_SOURCE_NULL &&
927                       y <= OSM_GPS_MAP_SOURCE_LAST &&
928                       old != y) {
929                        g_object_set(osd->widget, "map-source", y, NULL);
930    
931                        osd_render_source_sel(osd, TRUE);
932                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
933                    }
934                }
935    
936                /* return "clicked in OSD background" to prevent further */
937                /* processing by application */
938                return OSD_BG;
939            }
940        }
941    
942        return OSD_NONE;
943  }  }
944    #endif // OSD_SOURCE_SEL
945    
946    static osd_button_t
947    osd_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
948        osd_button_t but = OSD_NONE;
949    
950    #ifdef OSD_BALLOON
951        if(down) {
952            /* needed to handle balloons that are created at click */
953            osd_priv_t *priv = (osd_priv_t*)osd->priv;
954            priv->balloon.just_created = FALSE;
955        }
956    #endif
957    
958    #ifdef OSD_SOURCE_SEL
959        /* the source selection area is handles internally */
960        but = osd_source_check(osd, down, x, y);
961    #endif
962    
963        if(but == OSD_NONE) {
964            gint mx = x - OSD_X;
965            gint my = y - OSD_Y;
966    
967            if(OSD_X < 0)
968                mx -= (osd->widget->allocation.width - OSD_W);
969    
970            if(OSD_Y < 0)
971                my -= (osd->widget->allocation.height - OSD_H);
972    
973            /* first do a rough test for the OSD area. */
974            /* this is just to avoid an unnecessary detailed test */
975            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
976    #ifndef OSD_NO_DPAD
977                but = osd_check_dpad(mx, my);
978    #endif
979            }
980    
981            if(but == OSD_NONE)
982                but = osd_check_zoom(mx, my);
983        }
984    
985    #ifdef OSD_BALLOON
986        if(but == OSD_NONE) {
987            /* check if user clicked into balloon */
988            if(osd_balloon_check(osd, down, x, y))
989                but = OSD_BG;
990        }
991    #endif
992    
993        return but;
994    }
995    
996    #ifndef OSD_NO_DPAD
997  static void  static void
998  osm_gps_map_osd_dpad_labels(cairo_t *cr, gint x, gint y) {  osd_dpad_labels(cairo_t *cr, gint x, gint y) {
999      /* move reference to dpad center */      /* move reference to dpad center */
1000      x += D_RAD;      x += D_RAD;
1001      y += D_RAD;      y += D_RAD;
# Line 222  osm_gps_map_osd_dpad_labels(cairo_t *cr, Line 1018  osm_gps_map_osd_dpad_labels(cairo_t *cr,
1018          cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);          cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);
1019      }      }
1020  }  }
1021    #endif
1022    
1023  /* draw the sattelite dish icon in the center of the dpad */  #ifdef OSD_GPS_BUTTON
1024  #define GPS_V0  (D_RAD/8)  /* draw the satellite dish icon in the center of the dpad */
1025    #define GPS_V0  (D_RAD/7)
1026  #define GPS_V1  (D_RAD/10)  #define GPS_V1  (D_RAD/10)
1027  #define GPS_V2  (D_RAD/5)  #define GPS_V2  (D_RAD/5)
1028    
1029  /* draw a satellite receiver dish */  /* draw a satellite receiver dish */
1030    /* this is either drawn in the center of the dpad (if present) */
1031    /* or in the middle of the zoom area */
1032  static void  static void
1033  osm_gps_map_osd_dpad_gps(cairo_t *cr, gint x, gint y) {  osd_dpad_gps(cairo_t *cr, gint x, gint y) {
1034      /* move reference to dpad center */      /* move reference to dpad center */
1035      x += D_RAD;      x += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD * 3;
1036      y += D_RAD + GPS_V0;      y += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD + GPS_V0;
1037    
1038      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
1039      cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);      cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);
# Line 248  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi Line 1048  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi
1048      cairo_move_to (cr, x, y-GPS_V2);      cairo_move_to (cr, x, y-GPS_V2);
1049      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
1050  }  }
1051    #endif
1052    
1053  #define Z_LEN  (2*Z_RAD/3)  #define Z_LEN  (2*Z_RAD/3)
1054    
1055  static void  static void
1056  osm_gps_map_osd_zoom_labels(cairo_t *cr, gint x, gint y) {  osd_zoom_labels(cairo_t *cr, gint x, gint y) {
1057      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);
1058      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);
1059    
# Line 262  osm_gps_map_osd_zoom_labels(cairo_t *cr, Line 1063  osm_gps_map_osd_zoom_labels(cairo_t *cr,
1063      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
1064  }  }
1065    
1066  static void  #ifdef OSD_COORDINATES
 osm_gps_map_osd_labels(cairo_t *cr, gint width, gboolean enabled) {  
     if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);  
     else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);  
     cairo_set_line_width (cr, width);  
     cairo_stroke (cr);  
 }  
   
 static void  
 osm_gps_map_osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {  
     cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);  
     cairo_set_line_width (cr, width);  
     cairo_stroke (cr);  
 }  
   
 static void  
 osm_gps_map_osd_render(OsmGpsMapPrivate *priv) {  
   
     /* first fill with transparency */  
     cairo_t *cr = cairo_create(priv->osd.overlay);  
     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);  
     cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);  
     cairo_paint(cr);  
   
     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);  
   
     /* --------- draw zoom and dpad shape shadow ----------- */  
     gint x = 0, y = 0;  
   
     osm_gps_map_osd_zoom_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);  
     osm_gps_map_osd_shape_shadow(cr);  
     osm_gps_map_osd_dpad_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);  
     osm_gps_map_osd_shape_shadow(cr);  
   
     /* --------- draw zoom and dpad shape ----------- */  
   
     osm_gps_map_osd_zoom_shape(cr, x, y);  
     osm_gps_map_osd_shape(cr);  
     osm_gps_map_osd_dpad_shape(cr, x, y);  
     osm_gps_map_osd_shape(cr);  
   
     /* --------- draw zoom and dpad labels --------- */  
   
     osm_gps_map_osd_zoom_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);  
     osm_gps_map_osd_dpad_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);  
     osm_gps_map_osd_labels_shadow(cr, Z_RAD/3, TRUE);  
     osm_gps_map_osd_dpad_gps(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);  
     osm_gps_map_osd_labels_shadow(cr, Z_RAD/6, priv->osd.cb != NULL);  
   
     osm_gps_map_osd_zoom_labels(cr, x, y);  
     osm_gps_map_osd_dpad_labels(cr, x, y);  
     osm_gps_map_osd_labels(cr, Z_RAD/3, TRUE);  
     osm_gps_map_osd_dpad_gps(cr, x, y);  
     osm_gps_map_osd_labels(cr, Z_RAD/6, priv->osd.cb != NULL);  
   
     cairo_destroy(cr);  
 }  
   
 static void  
 osm_gps_map_osd_draw_controls (OsmGpsMap *map, gint xoffset, gint yoffset)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
   
     /* backup previous contents */  
     if(!priv->osd.backup)  
         priv->osd.backup = gdk_pixmap_new(priv->pixmap, OSD_W+2, OSD_H+2, -1);  
   
     gint x = OSD_X + EXTRA_BORDER + xoffset;  
     gint y = OSD_Y + EXTRA_BORDER + yoffset;  
   
     /* create backup of background */  
     gdk_draw_drawable(priv->osd.backup,  
         GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))],  
                       priv->pixmap, x-1, y-1, 0, 0, OSD_W+2, OSD_H+2);  
   
     priv->osd.backup_x = x-1;  
     priv->osd.backup_y = y-1;  
1067    
1068    #ifndef OSD_COORDINATES_FONT_SIZE
1069  #ifdef USE_CAIRO  #define OSD_COORDINATES_FONT_SIZE 12
     /* OSD itself uses some off-screen rendering, so check if the */  
     /* offscreen buffer is present and create it if not */  
     if(!priv->osd.overlay) {  
         /* create overlay ... */  
         priv->osd.overlay =  
             cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W, OSD_H);  
         /* ... and render it */  
         osm_gps_map_osd_render(priv);  
     }  
   
     // now draw this onto the original context  
     cairo_t *cr = gdk_cairo_create(priv->pixmap);  
     cairo_set_source_surface(cr, priv->osd.overlay, x, y);  
     cairo_paint(cr);  
     cairo_destroy(cr);  
   
 #else  
 #warning "OSD control display lacks a non-cairo implementation!"  
1070  #endif  #endif
 }  
1071    
1072  static void  #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
 osm_gps_map_osd_restore (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
1073    
1074      /* restore backup of previous contents */  #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1075      if(priv->osd.backup) {  #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET+OSD_COORDINATES_FONT_SIZE/4)
         /* create backup of background */  
         gdk_draw_drawable(priv->pixmap,  
             GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))],  
                       priv->osd.backup, 0, 0,  
                       priv->osd.backup_x, priv->osd.backup_y, OSD_W+2, OSD_H+2);  
     }  
 }  
1076    
1077    /* these can be overwritten with versions that support */
1078    /* localization */
1079    #ifndef OSD_COORDINATES_CHR_N
1080    #define OSD_COORDINATES_CHR_N  "N"
1081  #endif  #endif
1082    #ifndef OSD_COORDINATES_CHR_S
1083  static gboolean  #define OSD_COORDINATES_CHR_S  "S"
 osm_gps_map_map_redraw (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
   
     priv->idle_map_redraw = 0;  
   
     /* the motion_notify handler uses priv->pixmap to redraw the area; if we  
      * change it while we are dragging, we will end up showing it in the wrong  
      * place. This could be fixed by carefully recompute the coordinates, but  
      * for now it's easier just to disable redrawing the map while dragging */  
     if (priv->dragging)  
         return FALSE;  
   
     priv->redraw_cycle++;  
   
     /* draw white background to initialise pixmap */  
     gdk_draw_rectangle (  
                         priv->pixmap,  
                         GTK_WIDGET(map)->style->white_gc,  
                         TRUE,  
                         0, 0,  
                         GTK_WIDGET(map)->allocation.width + EXTRA_BORDER * 2,  
                         GTK_WIDGET(map)->allocation.height + EXTRA_BORDER * 2);  
   
     osm_gps_map_fill_tiles_pixel(map);  
   
     osm_gps_map_print_tracks(map);  
     osm_gps_map_draw_gps_point(map);  
     osm_gps_map_print_images(map);  
 #ifdef ENABLE_BALLOON  
     osm_gps_map_draw_balloon_int(map);  
 #endif  
 #ifdef ENABLE_OSD  
     osm_gps_map_osd_draw_controls(map, 0, 0);  
 #endif  
   
     //osm_gps_map_osd_speed(map, 1.5);  
     osm_gps_map_purge_cache(map);  
     gtk_widget_queue_draw (GTK_WIDGET (map));  
   
     return FALSE;  
 }  
   
 static void  
 osm_gps_map_map_redraw_idle (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
   
     if (priv->idle_map_redraw == 0)  
         priv->idle_map_redraw = g_idle_add ((GSourceFunc)osm_gps_map_map_redraw, map);  
 }  
   
 static void  
 osm_gps_map_init (OsmGpsMap *object)  
 {  
     OsmGpsMapPrivate *priv;  
   
     priv = G_TYPE_INSTANCE_GET_PRIVATE (object, OSM_TYPE_GPS_MAP, OsmGpsMapPrivate);  
     object->priv = priv;  
   
     priv->pixmap = NULL;  
   
     priv->trip_history = NULL;  
     priv->gps = g_new0(coord_t, 1);  
     priv->gps_valid = FALSE;  
   
 #ifdef ENABLE_BALLOON  
     priv->balloon.coo = g_new0(coord_t, 1);  
     priv->balloon.valid = FALSE;  
     priv->balloon.cb = NULL;  
1084  #endif  #endif
1085    #ifndef OSD_COORDINATES_CHR_E
1086  #ifdef ENABLE_OSD  #define OSD_COORDINATES_CHR_E  "E"
     priv->osd.backup = NULL;  
     priv->osd.overlay = NULL;  
     priv->osd.cb = NULL;  
1087  #endif  #endif
1088    #ifndef OSD_COORDINATES_CHR_W
1089      priv->tracks = NULL;  #define OSD_COORDINATES_CHR_W  "W"
     priv->images = NULL;  
   
     priv->drag_counter = 0;  
     priv->drag_mouse_dx = 0;  
     priv->drag_mouse_dy = 0;  
     priv->drag_start_mouse_x = 0;  
     priv->drag_start_mouse_y = 0;  
   
     priv->uri_format = 0;  
     priv->the_google = FALSE;  
   
     priv->map_source = -1;  
   
 #ifndef LIBSOUP22  
     //Change naumber of concurrent connections option?  
     priv->soup_session = soup_session_async_new_with_options(  
                                                              SOUP_SESSION_USER_AGENT,  
                                                              "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",  
                                                              NULL);  
 #else  
     /* libsoup-2.2 seems not to be able to set the user agent */  
     priv->soup_session = soup_session_async_new();  
1090  #endif  #endif
1091    
     //Hash table which maps tile d/l URIs to SoupMessage requests  
     priv->tile_queue = g_hash_table_new (g_str_hash, g_str_equal);  
   
     //Some mapping providers (Google) have varying degrees of tiles at multiple  
     //zoom levels  
     priv->missing_tiles = g_hash_table_new (g_str_hash, g_str_equal);  
   
     /* memory cache for most recently used tiles */  
     priv->tile_cache = g_hash_table_new_full (g_str_hash, g_str_equal,  
                                               g_free, (GDestroyNotify)cached_tile_free);  
     priv->max_tile_cache_size = 20;  
   
     gtk_widget_add_events (GTK_WIDGET (object),  
                            GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |  
                            GDK_POINTER_MOTION_MASK |  
                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);  
     GTK_WIDGET_SET_FLAGS (object, GTK_CAN_FOCUS);  
   
     g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, my_log_handler, NULL);  
 }  
   
 #ifndef G_CHECKSUM_MD5  
 /* simple hash algorithm hack if md5 is not present */  
 static char *simple_hash(char *str) {  
     union {  
         char str[4];  
         gulong val;  
     } hash = { .val = 0x55555555 };  
1092    
     while(*str) {  
         hash.str[(int)str & 3] ^= *str;  
         str++;  
     }  
     return g_strdup_printf("%08lX", hash.val);  
 }  
 #endif  
1093    
1094  static GObject *  /* this is the classic geocaching notation */
1095  osm_gps_map_constructor (GType gtype, guint n_properties, GObjectConstructParam *properties)  static char
1096  {  *osd_latitude_str(float latitude) {
1097      GObject *object;      char *c = OSD_COORDINATES_CHR_N;
1098      OsmGpsMapPrivate *priv;      float integral, fractional;
1099      OsmGpsMap *map;  
1100      const char *uri;      if(isnan(latitude))
1101            return NULL;
1102      //Always chain up to the parent constructor  
1103      object = G_OBJECT_CLASS(osm_gps_map_parent_class)->constructor(gtype, n_properties, properties);      if(latitude < 0) {
1104      map = OSM_GPS_MAP(object);          latitude = fabs(latitude);
1105      priv = OSM_GPS_MAP_PRIVATE(object);          c = OSD_COORDINATES_CHR_S;
   
     //user can specify a map source ID, or a repo URI as the map source  
     uri = osm_gps_map_source_get_repo_uri(OSM_GPS_MAP_SOURCE_NULL);  
     if ( (priv->map_source == 0) || (strcmp(priv->repo_uri, uri) == 0) ) {  
         g_debug("Using null source");  
         priv->map_source = OSM_GPS_MAP_SOURCE_NULL;  
   
         priv->null_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 256, 256);  
         gdk_pixbuf_fill(priv->null_tile, 0xcccccc00);  
     }  
     else if (priv->map_source >= 0) {  
         //check if the source given is valid  
         uri = osm_gps_map_source_get_repo_uri(priv->map_source);  
         if (uri) {  
             g_debug("Setting map source from ID");  
             g_free(priv->repo_uri);  
   
             priv->repo_uri = g_strdup(uri);  
             priv->image_format = g_strdup(  
                 osm_gps_map_source_get_image_format(priv->map_source));  
             priv->max_zoom = osm_gps_map_source_get_max_zoom(priv->map_source);  
             priv->min_zoom = osm_gps_map_source_get_min_zoom(priv->map_source);  
         }  
1106      }      }
1107    
1108      if (!priv->cache_dir_is_full_path) {      fractional = modff(latitude, &integral);
1109  #ifdef G_CHECKSUM_MD5  
1110          char *md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, priv->repo_uri, -1);      return g_strdup_printf("%s %02d° %06.3f'",
1111  #else                             c, (int)integral, fractional*60.0);
1112          char *md5 = simple_hash(priv->repo_uri);  }
 #endif  
   
         if (priv->cache_dir) {  
             char *old = priv->cache_dir;  
             //the new cachedir is the given cache dir + the md5 of the repo_uri  
             priv->cache_dir = g_strdup_printf("%s%c%s", old, G_DIR_SEPARATOR, md5);  
             g_debug("Adjusting cache dir %s -> %s", old, priv->cache_dir);  
             g_free(old);  
         } else {  
             //the new cachedir is the current dir + the md5 of the repo_uri  
             priv->cache_dir = g_strdup(md5);  
         }  
1113    
1114          g_free(md5);  static char
1115    *osd_longitude_str(float longitude) {
1116        char *c = OSD_COORDINATES_CHR_E;
1117        float integral, fractional;
1118    
1119        if(isnan(longitude))
1120            return NULL;
1121    
1122        if(longitude < 0) {
1123            longitude = fabs(longitude);
1124            c = OSD_COORDINATES_CHR_W;
1125      }      }
1126    
1127      inspect_map_uri(map);      fractional = modff(longitude, &integral);
1128    
1129      return object;      return g_strdup_printf("%s %03d° %06.3f'",
1130                               c, (int)integral, fractional*60.0);
1131  }  }
1132    
1133  static void  /* render a string at the given screen position */
1134  osm_gps_map_dispose (GObject *object)  static int
1135  {  osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1136      OsmGpsMap *map = OSM_GPS_MAP(object);      char *p = g_strdup(text);
1137      OsmGpsMapPrivate *priv = map->priv;      cairo_text_extents_t extents;
1138        cairo_text_extents (cr, p, &extents);
1139      if (priv->is_disposed)  
1140          return;      /* check if text needs to be truncated */
1141        int len = strlen(text)-2;
1142      priv->is_disposed = TRUE;      while(extents.width > width) {
1143            len--;
1144      soup_session_abort(priv->soup_session);          strcpy(p+len, "...");
1145      g_object_unref(priv->soup_session);          cairo_text_extents (cr, p, &extents);
1146        }
1147      g_hash_table_destroy(priv->tile_queue);  
1148      g_hash_table_destroy(priv->missing_tiles);      cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1149      g_hash_table_destroy(priv->tile_cache);      cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1150        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1151      osm_gps_map_free_images(map);      cairo_text_path (cr, p);
1152        cairo_stroke (cr);
     if(priv->pixmap)  
         g_object_unref (priv->pixmap);  
   
     if (priv->null_tile)  
         g_object_unref (priv->null_tile);  
   
     if(priv->gc_map)  
         g_object_unref(priv->gc_map);  
   
     if (priv->idle_map_redraw != 0)  
         g_source_remove (priv->idle_map_redraw);  
   
     g_free(priv->gps);  
   
 #ifdef ENABLE_BALLOON  
     g_free(priv->balloon.coo);  
 #endif  
1153    
1154  #ifdef ENABLE_OSD      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1155      if (priv->osd.backup)      cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1156          g_object_unref(priv->osd.backup);      cairo_show_text (cr, p);
1157    
1158      if (priv->osd.overlay)      g_free(p);
          cairo_surface_destroy(priv->osd.overlay);  
 #endif  
1159    
1160      G_OBJECT_CLASS (osm_gps_map_parent_class)->dispose (object);      /* skip + 1/4 line */
1161        return y + 5*OSD_COORDINATES_FONT_SIZE/4;
1162  }  }
1163    
1164  static void  static void
1165  osm_gps_map_finalize (GObject *object)  osd_render_coordinates(osm_gps_map_osd_t *osd)
1166  {  {
1167      OsmGpsMap *map = OSM_GPS_MAP(object);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     OsmGpsMapPrivate *priv = map->priv;  
1168    
1169      g_free(priv->cache_dir);      /* get current map position */
1170      g_free(priv->repo_uri);      gfloat lat, lon;
1171      g_free(priv->image_format);      g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1172    
1173        /* check if position has changed enough to require redraw */
1174        if(!isnan(priv->coordinates.lat) && !isnan(priv->coordinates.lon))
1175            /* 1/60000 == 1/1000 minute */
1176            if((fabsf(lat - priv->coordinates.lat) < 1/60000) &&
1177               (fabsf(lon - priv->coordinates.lon) < 1/60000))
1178                return;
1179    
1180      osm_gps_map_free_trip(map);      priv->coordinates.lat = lat;
1181      osm_gps_map_free_tracks(map);      priv->coordinates.lon = lon;
1182    
1183      G_OBJECT_CLASS (osm_gps_map_parent_class)->finalize (object);      /* first fill with transparency */
1184  }      cairo_t *cr = cairo_create(priv->coordinates.surface);
1185        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1186  static void      //    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1187  osm_gps_map_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)      cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1188  {      cairo_paint(cr);
1189      g_return_if_fail (OSM_IS_GPS_MAP (object));      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
     OsmGpsMap *map = OSM_GPS_MAP(object);  
     OsmGpsMapPrivate *priv = map->priv;  
1190    
1191      switch (prop_id)      cairo_select_font_face (cr, "Sans",
1192      {                              CAIRO_FONT_SLANT_NORMAL,
1193          case PROP_AUTO_CENTER:                              CAIRO_FONT_WEIGHT_BOLD);
1194              priv->map_auto_center = g_value_get_boolean (value);      cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
             break;  
         case PROP_RECORD_TRIP_HISTORY:  
             priv->record_trip_history = g_value_get_boolean (value);  
             break;  
         case PROP_SHOW_TRIP_HISTORY:  
             priv->show_trip_history = g_value_get_boolean (value);  
             break;  
         case PROP_AUTO_DOWNLOAD:  
             priv->map_auto_download = g_value_get_boolean (value);  
             break;  
         case PROP_REPO_URI:  
             priv->repo_uri = g_value_dup_string (value);  
             break;  
         case PROP_PROXY_URI:  
             if ( g_value_get_string(value) ) {  
                 priv->proxy_uri = g_value_dup_string (value);  
                 g_debug("Setting proxy server: %s", priv->proxy_uri);  
   
 #ifndef LIBSOUP22  
                 GValue val = {0};  
   
                 SoupURI* uri = soup_uri_new(priv->proxy_uri);  
                 g_value_init(&val, SOUP_TYPE_URI);  
                 g_value_take_boxed(&val, uri);  
1195    
1196                  g_object_set_property(G_OBJECT(priv->soup_session),SOUP_SESSION_PROXY_URI,&val);      char *latitude = osd_latitude_str(lat);
1197  #else      char *longitude = osd_longitude_str(lon);
1198                  SoupUri* uri = soup_uri_new(priv->proxy_uri);  
1199                  g_object_set(G_OBJECT(priv->soup_session), SOUP_SESSION_PROXY_URI, uri, NULL);      int y = OSD_COORDINATES_OFFSET;
1200  #endif      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1201              } else      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1202                  priv->proxy_uri = NULL;  
1203        g_free(latitude);
1204        g_free(longitude);
1205    
1206              break;      cairo_destroy(cr);
         case PROP_TILE_CACHE_DIR:  
             if ( g_value_get_string(value) )  
                 priv->cache_dir = g_value_dup_string (value);  
             break;  
         case PROP_TILE_CACHE_DIR_IS_FULL_PATH:  
             priv->cache_dir_is_full_path = g_value_get_boolean (value);  
             break;  
         case PROP_ZOOM:  
             priv->map_zoom = g_value_get_int (value);  
             break;  
         case PROP_MAX_ZOOM:  
             priv->max_zoom = g_value_get_int (value);  
             break;  
         case PROP_MIN_ZOOM:  
             priv->min_zoom = g_value_get_int (value);  
             break;  
         case PROP_MAP_X:  
             priv->map_x = g_value_get_int (value);  
             priv->center_coord_set = FALSE;  
             break;  
         case PROP_MAP_Y:  
             priv->map_y = g_value_get_int (value);  
             priv->center_coord_set = FALSE;  
             break;  
         case PROP_GPS_TRACK_WIDTH:  
             priv->ui_gps_track_width = g_value_get_int (value);  
             break;  
         case PROP_GPS_POINT_R1:  
             priv->ui_gps_point_inner_radius = g_value_get_int (value);  
             break;  
         case PROP_GPS_POINT_R2:  
             priv->ui_gps_point_outer_radius = g_value_get_int (value);  
             break;  
         case PROP_MAP_SOURCE:  
             priv->map_source = g_value_get_int (value);  
             break;  
         case PROP_IMAGE_FORMAT:  
             priv->image_format = g_value_dup_string (value);  
             break;  
         default:  
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);  
             break;  
     }  
1207  }  }
1208    #endif  // OSD_COORDINATES
1209    
1210  static void  #ifdef OSD_NAV
1211  osm_gps_map_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)  #define OSD_NAV_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1212  {  #define OSD_NAV_H  (130)
     g_return_if_fail (OSM_IS_GPS_MAP (object));  
     float lat,lon;  
     OsmGpsMap *map = OSM_GPS_MAP(object);  
     OsmGpsMapPrivate *priv = map->priv;  
1213    
1214      switch (prop_id)  /* http://mathforum.org/library/drmath/view/55417.html */
1215      {  static float get_bearing(float lat1, float lon1, float lat2, float lon2) {
1216          case PROP_AUTO_CENTER:    return atan2( sin(lon2 - lon1) * cos(lat2),
1217              g_value_set_boolean(value, priv->map_auto_center);                  cos(lat1) * sin(lat2) -
1218              break;                  sin(lat1) * cos(lat2) * cos(lon2 - lon1));
         case PROP_RECORD_TRIP_HISTORY:  
             g_value_set_boolean(value, priv->record_trip_history);  
             break;  
         case PROP_SHOW_TRIP_HISTORY:  
             g_value_set_boolean(value, priv->show_trip_history);  
             break;  
         case PROP_AUTO_DOWNLOAD:  
             g_value_set_boolean(value, priv->map_auto_download);  
             break;  
         case PROP_REPO_URI:  
             g_value_set_string(value, priv->repo_uri);  
             break;  
         case PROP_PROXY_URI:  
             g_value_set_string(value, priv->proxy_uri);  
             break;  
         case PROP_TILE_CACHE_DIR:  
             g_value_set_string(value, priv->cache_dir);  
             break;  
         case PROP_TILE_CACHE_DIR_IS_FULL_PATH:  
             g_value_set_boolean(value, priv->cache_dir_is_full_path);  
             break;  
         case PROP_ZOOM:  
             g_value_set_int(value, priv->map_zoom);  
             break;  
         case PROP_MAX_ZOOM:  
             g_value_set_int(value, priv->max_zoom);  
             break;  
         case PROP_MIN_ZOOM:  
             g_value_set_int(value, priv->min_zoom);  
             break;  
         case PROP_LATITUDE:  
             lat = pixel2lat(priv->map_zoom,  
                             priv->map_y + (GTK_WIDGET(map)->allocation.height / 2));  
             g_value_set_float(value, rad2deg(lat));  
             break;  
         case PROP_LONGITUDE:  
             lon = pixel2lon(priv->map_zoom,  
                             priv->map_x + (GTK_WIDGET(map)->allocation.width / 2));  
             g_value_set_float(value, rad2deg(lon));  
             break;  
         case PROP_MAP_X:  
             g_value_set_int(value, priv->map_x);  
             break;  
         case PROP_MAP_Y:  
             g_value_set_int(value, priv->map_y);  
             break;  
         case PROP_TILES_QUEUED:  
             g_value_set_int(value, g_hash_table_size(priv->tile_queue));  
             break;  
         case PROP_GPS_TRACK_WIDTH:  
             g_value_set_int(value, priv->ui_gps_track_width);  
             break;  
         case PROP_GPS_POINT_R1:  
             g_value_set_int(value, priv->ui_gps_point_inner_radius);  
             break;  
         case PROP_GPS_POINT_R2:  
             g_value_set_int(value, priv->ui_gps_point_outer_radius);  
             break;  
         case PROP_MAP_SOURCE:  
             g_value_set_int(value, priv->map_source);  
             break;  
         case PROP_IMAGE_FORMAT:  
             g_value_set_string(value, priv->image_format);  
             break;  
         default:  
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);  
             break;  
     }  
1219  }  }
1220    
1221  static gboolean  /* http://mathforum.org/library/drmath/view/51722.html */
1222  osm_gps_map_scroll_event (GtkWidget *widget, GdkEventScroll  *event)  static float get_distance(float lat1, float lon1, float lat2, float lon2) {
1223  {    float aob = acos(cos(lat1) * cos(lat2) * cos(lon2 - lon1) +
1224      OsmGpsMap *map = OSM_GPS_MAP(widget);                     sin(lat1) * sin(lat2));
     OsmGpsMapPrivate *priv = map->priv;  
1225    
1226      if (event->direction == GDK_SCROLL_UP)    //  return(aob * 3959.0);   /* great circle radius in miles */
     {  
         osm_gps_map_set_zoom(map, priv->map_zoom+1);  
     }  
     else  
     {  
         osm_gps_map_set_zoom(map, priv->map_zoom-1);  
     }  
1227    
1228      return FALSE;    return(aob * 6371000.0);     /* great circle radius in meters */
1229  }  }
1230    
1231  static gboolean  static void
1232  osm_gps_map_button_press (GtkWidget *widget, GdkEventButton *event)  osd_render_nav(osm_gps_map_osd_t *osd)
1233  {  {
1234      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
   
 #ifdef ENABLE_BALLOON  
     /* don't drag if the user clicked within the balloon */  
     if (osm_gps_map_in_balloon(priv,  
                    event->x + EXTRA_BORDER,  
                    event->y + EXTRA_BORDER))  
     {  
         priv->drag_counter = -1;  
         return FALSE;  
     }  
 #endif  
   
 #ifdef ENABLE_OSD  
     #define SCROLL_STEP 10  
   
     /* pressed inside OSD control? */  
     osd_button_t but = osm_gps_map_osd_check(event->x, event->y);  
     if(but != OSD_NONE)  
     {  
         priv->drag_counter = -1;  
   
         switch(but) {  
         case OSD_GPS:  
             priv->osd.cb(priv->osd.data);  
             break;  
   
         case OSD_UP:  
             priv->map_y -= GTK_WIDGET(widget)->allocation.height/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_DOWN:  
             priv->map_y += GTK_WIDGET(widget)->allocation.height/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_LEFT:  
             priv->map_x -= GTK_WIDGET(widget)->allocation.width/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_RIGHT:  
             priv->map_x += GTK_WIDGET(widget)->allocation.width/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_IN:  
             osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom+1);  
             break;  
   
         case OSD_OUT:  
             osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom-1);  
             break;  
   
         default:  
             break;  
         }  
   
         osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));  
   
         return FALSE;  
     }  
 #endif  
   
     priv->drag_counter = 0;  
     priv->drag_start_mouse_x = (int) event->x;  
     priv->drag_start_mouse_y = (int) event->y;  
     priv->drag_start_map_x = priv->map_x;  
     priv->drag_start_map_y = priv->map_y;  
1235    
1236      return FALSE;      if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1237  }          return;
1238    
1239  static gboolean      /* first fill with transparency */
1240  osm_gps_map_button_release (GtkWidget *widget, GdkEventButton *event)      cairo_t *cr = cairo_create(priv->nav.surface);
1241  {      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1242      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1243        cairo_paint(cr);
1244        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1245    
1246  #ifdef ENABLE_BALLOON      cairo_select_font_face (cr, "Sans",
1247      /* released inside the balloon? */                              CAIRO_FONT_SLANT_NORMAL,
1248      if (osm_gps_map_in_balloon(priv,                              CAIRO_FONT_WEIGHT_BOLD);
1249                     event->x + EXTRA_BORDER,      cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
                    event->y + EXTRA_BORDER))  
     {  
         osm_gps_map_handle_balloon_click(OSM_GPS_MAP(widget),  
              event->x - priv->balloon.rect.x + EXTRA_BORDER,  
              event->y - priv->balloon.rect.y + EXTRA_BORDER);  
     }  
 #endif  
1250    
1251      if (priv->dragging)      char *latitude = osd_latitude_str(priv->nav.lat);
1252      {      char *longitude = osd_longitude_str(priv->nav.lon);
1253          priv->dragging = FALSE;  
1254        int y = OSD_COORDINATES_OFFSET;
1255        y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1256        y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1257        y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1258    
1259        g_free(latitude);
1260        g_free(longitude);
1261    
1262          priv->map_x = priv->drag_start_map_x;      /* draw the compass */
1263          priv->map_y = priv->drag_start_map_y;      int radius = (OSD_NAV_H - y - 5*OSD_COORDINATES_FONT_SIZE/4)/2;
1264        if(radius > OSD_NAV_W/2)
1265            radius = OSD_NAV_W/2;
1266    
1267          priv->map_x += (priv->drag_start_mouse_x - (int) event->x);      int x = OSD_NAV_W/2+1;
1268          priv->map_y += (priv->drag_start_mouse_y - (int) event->y);      y += radius;
1269    
1270          priv->center_coord_set = FALSE;      cairo_stroke (cr);
1271    
1272          osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));      /* draw background */
1273      }      cairo_arc(cr, x, y, radius, 0,  2*M_PI);
1274        cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
1275        cairo_fill_preserve (cr);
1276        cairo_set_source_rgb (cr, 0, 0, 0);
1277        cairo_set_line_width (cr, 1);
1278        cairo_stroke (cr);
1279    
1280      priv->drag_mouse_dx = 0;      /* draw pointer */
1281      priv->drag_mouse_dy = 0;  #define ARROW_WIDTH     0.3
1282      priv->drag_counter = -1;  #define ARROW_LENGTH    0.7
1283    
1284        coord_t *gps = osm_gps_map_get_gps (OSM_GPS_MAP(osd->widget));
1285        if(gps) {
1286            float arot = get_bearing(gps->rlat, gps->rlon,
1287                         deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1288    
1289            cairo_move_to(cr,
1290                          x + radius *  ARROW_LENGTH *  sin(arot),
1291                          y + radius *  ARROW_LENGTH * -cos(arot));
1292    
1293            cairo_line_to(cr,
1294                          x + radius * -ARROW_LENGTH *  sin(arot+ARROW_WIDTH),
1295                          y + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH));
1296    
1297            cairo_line_to(cr,
1298                          x + radius * -0.5 * ARROW_LENGTH *  sin(arot),
1299                          y + radius * -0.5 * ARROW_LENGTH * -cos(arot));
1300    
1301            cairo_line_to(cr,
1302                          x + radius * -ARROW_LENGTH *  sin(arot-ARROW_WIDTH),
1303                          y + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH));
1304    
1305            cairo_close_path(cr);
1306            cairo_set_source_rgb (cr, 0, 0, 0);
1307            cairo_fill (cr);
1308    
1309            y += radius + OSD_COORDINATES_FONT_SIZE/4;
1310    
1311            float dist = get_distance(gps->rlat, gps->rlon,
1312                            deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1313    
1314            char *dist_str = NULL;
1315            if(dist<1000)
1316                dist_str = g_strdup_printf("%u m", (int)dist);
1317            else
1318                dist_str = g_strdup_printf("%.1f km", dist/1000);
1319    
1320            y = osd_render_centered_text(cr, y, OSD_NAV_W, dist_str);
1321            g_free(dist_str);
1322        }
1323    
1324      return FALSE;      cairo_destroy(cr);
1325  }  }
1326    
1327  static gboolean  void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1328  osm_gps_map_expose (GtkWidget *widget, GdkEventExpose  *event);      g_return_if_fail (OSM_IS_GPS_MAP (map));
1329    
1330  static gboolean      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1331  osm_gps_map_motion_notify (GtkWidget *widget, GdkEventMotion  *event)      g_return_if_fail (osd);
 {  
     int x, y;  
     GdkModifierType state;  
     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
1332    
1333      if (event->is_hint)      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1334          gdk_window_get_pointer (event->window, &x, &y, &state);      g_return_if_fail (priv);
1335      else  
1336      {      if(priv->nav.surface) {
1337          x = event->x;          cairo_surface_destroy(priv->nav.surface);
1338          y = event->y;          priv->nav.surface = NULL;
1339          state = event->state;          priv->nav.lat = OSM_GPS_MAP_INVALID;
1340            priv->nav.lon = OSM_GPS_MAP_INVALID;
1341            if(priv->nav.name) g_free(priv->nav.name);
1342      }      }
1343        osm_gps_map_redraw(map);
1344    }
1345    
1346      // are we being dragged  void
1347      if (!(state & GDK_BUTTON1_MASK))  osm_gps_map_osd_draw_nav (OsmGpsMap *map, float latitude, float longitude,
1348          return FALSE;                            char *name) {
1349        g_return_if_fail (OSM_IS_GPS_MAP (map));
1350    
1351      if (priv->drag_counter < 0)      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1352          return FALSE;      g_return_if_fail (osd);
1353    
1354      priv->drag_counter++;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1355        g_return_if_fail (priv);
1356    
1357      // we havent dragged more than 6 pixels      osm_gps_map_osd_clear_nav (map);
     if (priv->drag_counter < 6)  
         return FALSE;  
1358    
1359  #ifdef USE_MAEMO      /* allocate balloon surface */
1360      /* reduce update frequency on maemo to keep screen update fluid */      priv->nav.surface =
1361      static guint32 last_time = 0;          cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1362                                       OSD_NAV_W+2, OSD_NAV_H+2);
1363    
1364      if(event->time - last_time < 100) return FALSE;      priv->nav.lat = latitude;
1365      last_time = event->time;      priv->nav.lon = longitude;
1366  #endif      priv->nav.name = g_strdup(name);
1367    
1368        osd_render_nav(osd);
1369    
1370      priv->dragging = TRUE;      osm_gps_map_redraw(map);
1371    }
1372    
1373      if (priv->map_auto_center)  #endif // OSD_NAV
         g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);  
1374    
     priv->drag_mouse_dx = x - priv->drag_start_mouse_x;  
     priv->drag_mouse_dy = y - priv->drag_start_mouse_y;  
1375    
1376  #ifdef ENABLE_OSD  #ifdef OSD_CROSSHAIR
     /* undo OSD */  
     osm_gps_map_osd_restore (OSM_GPS_MAP(widget));  
1377    
1378      /* draw new OSD */  #ifndef OSD_CROSSHAIR_RADIUS
1379      osm_gps_map_osd_draw_controls (OSM_GPS_MAP(widget),  #define OSD_CROSSHAIR_RADIUS 10
                                    -priv->drag_mouse_dx,  
                                    -priv->drag_mouse_dy);  
1380  #endif  #endif
1381    
1382      osm_gps_map_expose (widget, NULL);  #define OSD_CROSSHAIR_TICK  (OSD_CROSSHAIR_RADIUS/2)
1383    #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
1384    #define OSD_CROSSHAIR_W  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1385    #define OSD_CROSSHAIR_H  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1386    
1387    static void
1388    osd_render_crosshair_shape(cairo_t *cr) {
1389        cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
1390                   OSD_CROSSHAIR_RADIUS, 0,  2*M_PI);
1391    
1392        cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
1393                       OSD_CROSSHAIR_H/2);
1394        cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
1395        cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
1396                       OSD_CROSSHAIR_H/2);
1397        cairo_rel_line_to (cr,  OSD_CROSSHAIR_TICK, 0);
1398    
1399        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1400                       OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
1401        cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
1402        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1403                       OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
1404        cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
1405    
1406      return FALSE;      cairo_stroke (cr);
1407  }  }
1408    
1409  static gboolean  static void
1410  osm_gps_map_configure (GtkWidget *widget, GdkEventConfigure *event)  osd_render_crosshair(osm_gps_map_osd_t *osd)
1411  {  {
1412      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1413    
1414      /* create pixmap */      if(priv->crosshair.rendered)
1415      if (priv->pixmap)          return;
         g_object_unref (priv->pixmap);  
   
     priv->pixmap = gdk_pixmap_new (  
                                    widget->window,  
                                    widget->allocation.width + EXTRA_BORDER * 2,  
                                    widget->allocation.height + EXTRA_BORDER * 2,  
                                    -1);  
1416    
1417      /* and gc, used for clipping (I think......) */      priv->crosshair.rendered = TRUE;
     if(priv->gc_map)  
         g_object_unref(priv->gc_map);  
1418    
1419      priv->gc_map = gdk_gc_new(priv->pixmap);      /* first fill with transparency */
1420        cairo_t *cr = cairo_create(priv->crosshair.surface);
1421        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1422        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1423        cairo_paint(cr);
1424        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1425    
1426      osm_gps_map_map_redraw(OSM_GPS_MAP(widget));      cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1427    
1428        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
1429        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
1430        osd_render_crosshair_shape(cr);
1431    
1432        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1433        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
1434        osd_render_crosshair_shape(cr);
1435    
1436      return FALSE;      cairo_destroy(cr);
1437  }  }
1438    #endif
1439    
1440  static gboolean  #ifdef OSD_SCALE
 osm_gps_map_expose (GtkWidget *widget, GdkEventExpose  *event)  
 {  
     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
1441    
1442      gdk_draw_drawable (  #ifndef OSD_SCALE_FONT_SIZE
1443                         widget->window,  #define OSD_SCALE_FONT_SIZE 12
                        widget->style->fg_gc[GTK_WIDGET_STATE (widget)],  
                        priv->pixmap,  
                        0,0,  
                        priv->drag_mouse_dx - EXTRA_BORDER,  
                        priv->drag_mouse_dy - EXTRA_BORDER,  
                        -1,-1);  
   
     //Paint white outside of the map if dragging. Its less  
     //ugly than painting the corrupted map  
     if(priv->drag_mouse_dx>EXTRA_BORDER) {  
         gdk_draw_rectangle (  
                             widget->window,  
                             widget->style->white_gc,  
                             TRUE,  
                             0, 0,  
                             priv->drag_mouse_dx - EXTRA_BORDER,  
                             widget->allocation.height);  
     }  
     else if (-priv->drag_mouse_dx > EXTRA_BORDER)  
     {  
         gdk_draw_rectangle (  
                             widget->window,  
                             widget->style->white_gc,  
                             TRUE,  
                             priv->drag_mouse_dx + widget->allocation.width + EXTRA_BORDER, 0,  
                             -priv->drag_mouse_dx - EXTRA_BORDER,  
                             widget->allocation.height);  
     }  
   
     if (priv->drag_mouse_dy>EXTRA_BORDER) {  
         gdk_draw_rectangle (  
                             widget->window,  
                             widget->style->white_gc,  
                             TRUE,  
                             0, 0,  
                             widget->allocation.width,  
                             priv->drag_mouse_dy - EXTRA_BORDER);  
     }  
     else if (-priv->drag_mouse_dy > EXTRA_BORDER)  
     {  
         gdk_draw_rectangle (  
                             widget->window,  
                             widget->style->white_gc,  
                             TRUE,  
                             0, priv->drag_mouse_dy + widget->allocation.height + EXTRA_BORDER,  
                             widget->allocation.width,  
                             -priv->drag_mouse_dy - EXTRA_BORDER);  
     }  
 #if 0  
     if(!priv->dragging)  
         gdk_draw_drawable (  
                        widget->window,  
                        widget->style->fg_gc[GTK_WIDGET_STATE (widget)],  
                        priv->pixmap,  
                        event->area.x + EXTRA_BORDER,  
                        event->area.y + EXTRA_BORDER,  
                        event->area.x, event->area.y,  
                        event->area.width, event->area.height);  
1444  #endif  #endif
1445      return FALSE;  #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
1446  }  #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
1447    
1448    /* various parameters used to create the scale */
1449    #define OSD_SCALE_H2   (OSD_SCALE_H/2)
1450    #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
1451    #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
1452    #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
1453    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
1454    
1455  static void  static void
1456  osm_gps_map_class_init (OsmGpsMapClass *klass)  osd_render_scale(osm_gps_map_osd_t *osd)
1457  {  {
1458      GObjectClass* object_class = G_OBJECT_CLASS (klass);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);  
1459    
1460      g_type_class_add_private (klass, sizeof (OsmGpsMapPrivate));      /* this only needs to be rendered if the zoom has changed */
1461        gint zoom;
1462        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
1463        if(zoom == priv->scale.zoom)
1464            return;
1465    
1466      object_class->dispose = osm_gps_map_dispose;      priv->scale.zoom = zoom;
     object_class->finalize = osm_gps_map_finalize;  
     object_class->constructor = osm_gps_map_constructor;  
     object_class->set_property = osm_gps_map_set_property;  
     object_class->get_property = osm_gps_map_get_property;  
   
     widget_class->expose_event = osm_gps_map_expose;  
     widget_class->configure_event = osm_gps_map_configure;  
     widget_class->button_press_event = osm_gps_map_button_press;  
     widget_class->button_release_event = osm_gps_map_button_release;  
     widget_class->motion_notify_event = osm_gps_map_motion_notify;  
     widget_class->scroll_event = osm_gps_map_scroll_event;  
   
     g_object_class_install_property (object_class,  
                                      PROP_AUTO_CENTER,  
                                      g_param_spec_boolean ("auto-center",  
                                                            "auto center",  
                                                            "map auto center",  
                                                            TRUE,  
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_RECORD_TRIP_HISTORY,  
                                      g_param_spec_boolean ("record-trip-history",  
                                                            "record trip history",  
                                                            "should all gps points be recorded in a trip history",  
                                                            TRUE,  
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_SHOW_TRIP_HISTORY,  
                                      g_param_spec_boolean ("show-trip-history",  
                                                            "show trip history",  
                                                            "should the recorded trip history be shown on the map",  
                                                            TRUE,  
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_AUTO_DOWNLOAD,  
                                      g_param_spec_boolean ("auto-download",  
                                                            "auto download",  
                                                            "map auto download",  
                                                            TRUE,  
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_REPO_URI,  
                                      g_param_spec_string ("repo-uri",  
                                                           "repo uri",  
                                                           "map source tile repository uri",  
                                                           OSM_REPO_URI,  
                                                           G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
      g_object_class_install_property (object_class,  
                                      PROP_PROXY_URI,  
                                      g_param_spec_string ("proxy-uri",  
                                                           "proxy uri",  
                                                           "http proxy uri on NULL",  
                                                           NULL,  
                                                           G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_TILE_CACHE_DIR,  
                                      g_param_spec_string ("tile-cache",  
                                                           "tile cache",  
                                                           "osm local tile cache dir",  
                                                           NULL,  
                                                           G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_TILE_CACHE_DIR_IS_FULL_PATH,  
                                      g_param_spec_boolean ("tile-cache-is-full-path",  
                                                            "tile cache is full path",  
                                                            "if true, the path passed to tile-cache is interpreted as the full cache path",  
                                                            FALSE,  
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_ZOOM,  
                                      g_param_spec_int ("zoom",  
                                                        "zoom",  
                                                        "zoom level",  
                                                        MIN_ZOOM, /* minimum property value */  
                                                        MAX_ZOOM, /* maximum property value */  
                                                        3,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_MAX_ZOOM,  
                                      g_param_spec_int ("max-zoom",  
                                                        "max zoom",  
                                                        "maximum zoom level",  
                                                        MIN_ZOOM, /* minimum property value */  
                                                        MAX_ZOOM, /* maximum property value */  
                                                        OSM_MAX_ZOOM,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_MIN_ZOOM,  
                                      g_param_spec_int ("min-zoom",  
                                                        "min zoom",  
                                                        "minimum zoom level",  
                                                        MIN_ZOOM, /* minimum property value */  
                                                        MAX_ZOOM, /* maximum property value */  
                                                        OSM_MIN_ZOOM,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_LATITUDE,  
                                      g_param_spec_float ("latitude",  
                                                          "latitude",  
                                                          "latitude in degrees",  
                                                          -90.0, /* minimum property value */  
                                                          90.0, /* maximum property value */  
                                                          0,  
                                                          G_PARAM_READABLE));  
   
     g_object_class_install_property (object_class,  
                                      PROP_LONGITUDE,  
                                      g_param_spec_float ("longitude",  
                                                          "longitude",  
                                                          "longitude in degrees",  
                                                          -180.0, /* minimum property value */  
                                                          180.0, /* maximum property value */  
                                                          0,  
                                                          G_PARAM_READABLE));  
   
     g_object_class_install_property (object_class,  
                                      PROP_MAP_X,  
                                      g_param_spec_int ("map-x",  
                                                        "map-x",  
                                                        "initial map x location",  
                                                        G_MININT, /* minimum property value */  
                                                        G_MAXINT, /* maximum property value */  
                                                        890,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_MAP_Y,  
                                      g_param_spec_int ("map-y",  
                                                        "map-y",  
                                                        "initial map y location",  
                                                        G_MININT, /* minimum property value */  
                                                        G_MAXINT, /* maximum property value */  
                                                        515,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_TILES_QUEUED,  
                                      g_param_spec_int ("tiles-queued",  
                                                        "tiles-queued",  
                                                        "number of tiles currently waiting to download",  
                                                        G_MININT, /* minimum property value */  
                                                        G_MAXINT, /* maximum property value */  
                                                        0,  
                                                        G_PARAM_READABLE));  
   
     g_object_class_install_property (object_class,  
                                      PROP_GPS_TRACK_WIDTH,  
                                      g_param_spec_int ("gps-track-width",  
                                                        "gps-track-width",  
                                                        "width of the lines drawn for the gps track",  
                                                        1,           /* minimum property value */  
                                                        G_MAXINT,    /* maximum property value */  
                                                        4,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_GPS_POINT_R1,  
                                      g_param_spec_int ("gps-track-point-radius",  
                                                        "gps-track-point-radius",  
                                                        "radius of the gps point inner circle",  
                                                        0,           /* minimum property value */  
                                                        G_MAXINT,    /* maximum property value */  
                                                        10,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_GPS_POINT_R2,  
                                      g_param_spec_int ("gps-track-highlight-radius",  
                                                        "gps-track-highlight-radius",  
                                                        "radius of the gps point highlight circle",  
                                                        0,           /* minimum property value */  
                                                        G_MAXINT,    /* maximum property value */  
                                                        20,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));  
   
     g_object_class_install_property (object_class,  
                                      PROP_MAP_SOURCE,  
                                      g_param_spec_int ("map-source",  
                                                        "map source",  
                                                        "map source ID",  
                                                        -1,           /* minimum property value */  
                                                        G_MAXINT,    /* maximum property value */  
                                                        -1,  
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
   
     g_object_class_install_property (object_class,  
                                      PROP_IMAGE_FORMAT,  
                                      g_param_spec_string ("image-format",  
                                                           "image format",  
                                                           "map source tile repository image format (jpg, png)",  
                                                           OSM_IMAGE_FORMAT,  
                                                           G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  
 }  
1467    
1468  const char*      float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
 osm_gps_map_source_get_friendly_name(OsmGpsMapSource_t source)  
 {  
     switch(source)  
     {  
         case OSM_GPS_MAP_SOURCE_NULL:  
             return "None";  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:  
             return "OpenStreetMap";  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:  
             return "OpenStreetMap Renderer";  
         case OSM_GPS_MAP_SOURCE_OPENAERIALMAP:  
             return "OpenAerialMap";  
         case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:  
             return "Maps-For-Free";  
         case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:  
             return "Google Maps";  
         case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:  
             return "Google Satellite";  
         case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:  
             return "Google Hybrid";  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:  
             return "Virtual Earth";  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:  
             return "Virtual Earth Satellite";  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:  
             return "Virtual Earth Hybrid";  
         case OSM_GPS_MAP_SOURCE_YAHOO_STREET:  
             return "Yahoo Maps";  
         case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:  
             return "Yahoo Satellite";  
         case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:  
             return "Yahoo Hybrid";  
         default:  
             return NULL;  
     }  
     return NULL;  
 }  
   
 //http://www.internettablettalk.com/forums/showthread.php?t=5209  
 //https://garage.maemo.org/plugins/scmsvn/viewcvs.php/trunk/src/maps.c?root=maemo-mapper&view=markup  
 //http://www.ponies.me.uk/maps/GoogleTileUtils.java  
 //http://www.mgmaps.com/cache/MapTileCacher.perl  
 const char*  
 osm_gps_map_source_get_repo_uri(OsmGpsMapSource_t source)  
 {  
     switch(source)  
     {  
         case OSM_GPS_MAP_SOURCE_NULL:  
             return "none://";  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:  
             return OSM_REPO_URI;  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:  
             return "http://tah.openstreetmap.org/Tiles/tile/#Z/#X/#Y.png";  
         case OSM_GPS_MAP_SOURCE_OPENAERIALMAP:  
             return "http://tile.openaerialmap.org/tiles/1.0.0/openaerialmap-900913/#Z/#X/#Y.jpg";  
         case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:  
             return "http://maps-for-free.com/layer/relief/z#Z/row#Y/#Z_#X-#Y.jpg";  
         case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:  
             return "http://mt#R.google.com/vt/v=w2.97&x=#X&y=#Y&z=#Z";  
         case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:  
             return "http://khm#R.google.com/kh?n=404&v=3&t=#Q";  
         case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:  
             return NULL; /* No longer working  "http://mt#R.google.com/mt?n=404&v=w2t.99&x=#X&y=#Y&zoom=#S" */  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:  
             return "http://a#R.ortho.tiles.virtualearth.net/tiles/r#W.jpeg?g=50";  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:  
             return "http://a#R.ortho.tiles.virtualearth.net/tiles/a#W.jpeg?g=50";  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:  
             return "http://a#R.ortho.tiles.virtualearth.net/tiles/h#W.jpeg?g=50";  
         case OSM_GPS_MAP_SOURCE_YAHOO_STREET:  
         case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:  
         case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:  
             /* TODO: Implement signed Y, aka U  
              * http://us.maps3.yimg.com/aerial.maps.yimg.com/ximg?v=1.7&t=a&s=256&x=%d&y=%-d&z=%d  
              *  x = tilex,  
              *  y = (1 << (MAX_ZOOM - zoom)) - tiley - 1,  
              *  z = zoom - (MAX_ZOOM - 17));  
              */  
             return NULL;  
         default:  
             return NULL;  
     }  
     return NULL;  
 }  
1469    
1470  const char *      /* first fill with transparency */
1471  osm_gps_map_source_get_image_format(OsmGpsMapSource_t source)      cairo_t *cr = cairo_create(priv->scale.surface);
1472  {      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1473      switch(source) {      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1474          case OSM_GPS_MAP_SOURCE_NULL:      // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1475          case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:      cairo_paint(cr);
1476          case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1477              return "png";  
1478          case OSM_GPS_MAP_SOURCE_OPENAERIALMAP:      /* determine the size of the scale width in meters */
1479          case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:      float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
1480          case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:  
1481          case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:      /* scale this to useful values */
1482          case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:      int exp = logf(width)*M_LOG10E;
1483          case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:      int mant = width/pow(10,exp);
1484          case OSM_GPS_MAP_SOURCE_YAHOO_STREET:      int width_metric = mant * pow(10,exp);
1485          case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:      char *dist_str = NULL;
1486          case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:      if(width_metric<1000)
1487          case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:          dist_str = g_strdup_printf("%u m", width_metric);
1488          case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:      else
1489              return "jpg";          dist_str = g_strdup_printf("%u km", width_metric/1000);
1490          default:      width_metric /= m_per_pix;
1491              return "bin";  
1492        /* and now the hard part: scale for useful imperial values :-( */
1493        /* try to convert to feet, 1ft == 0.3048 m */
1494        width /= 0.3048;
1495        float imp_scale = 0.3048;
1496        char *dist_imp_unit = "ft";
1497    
1498        if(width >= 100) {
1499            /* 1yd == 3 feet */
1500            width /= 3.0;
1501            imp_scale *= 3.0;
1502            dist_imp_unit = "yd";
1503    
1504            if(width >= 1760.0) {
1505                /* 1mi == 1760 yd */
1506                width /= 1760.0;
1507                imp_scale *= 1760.0;
1508                dist_imp_unit = "mi";
1509            }
1510      }      }
     return "bin";  
 }  
1511    
1512        /* also convert this to full tens/hundreds */
1513        exp = logf(width)*M_LOG10E;
1514        mant = width/pow(10,exp);
1515        int width_imp = mant * pow(10,exp);
1516        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
1517    
1518        /* convert back to pixels */
1519        width_imp *= imp_scale;
1520        width_imp /= m_per_pix;
1521    
1522        cairo_select_font_face (cr, "Sans",
1523                                CAIRO_FONT_SLANT_NORMAL,
1524                                CAIRO_FONT_WEIGHT_BOLD);
1525        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1526        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1527    
1528        cairo_text_extents_t extents;
1529        cairo_text_extents (cr, dist_str, &extents);
1530    
1531        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1532        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1533        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1534        cairo_text_path (cr, dist_str);
1535        cairo_stroke (cr);
1536        cairo_move_to (cr, 2*OSD_SCALE_FD,
1537                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1538        cairo_text_path (cr, dist_str_imp);
1539        cairo_stroke (cr);
1540    
1541  int      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1542  osm_gps_map_source_get_min_zoom(OsmGpsMapSource_t source)      cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1543  {      cairo_show_text (cr, dist_str);
1544      return 1;      cairo_move_to (cr, 2*OSD_SCALE_FD,
1545  }                     OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1546        cairo_show_text (cr, dist_str_imp);
1547    
1548        g_free(dist_str);
1549        g_free(dist_str_imp);
1550    
1551        /* draw white line */
1552        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1553        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1554        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
1555        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1556        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1557        cairo_rel_line_to (cr, width_metric, 0);
1558        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1559        cairo_stroke(cr);
1560        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1561        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1562        cairo_rel_line_to (cr, width_imp, 0);
1563        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1564        cairo_stroke(cr);
1565    
1566        /* draw black line */
1567        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1568        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1569        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1570        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1571        cairo_rel_line_to (cr, width_metric, 0);
1572        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1573        cairo_stroke(cr);
1574        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1575        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1576        cairo_rel_line_to (cr, width_imp, 0);
1577        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1578        cairo_stroke(cr);
1579    
1580  int      cairo_destroy(cr);
 osm_gps_map_source_get_max_zoom(OsmGpsMapSource_t source)  
 {  
     switch(source) {  
         case OSM_GPS_MAP_SOURCE_NULL:  
             return 18;  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:  
             return OSM_MAX_ZOOM;  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:  
         case OSM_GPS_MAP_SOURCE_OPENAERIALMAP:  
         case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:  
         case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:  
         case OSM_GPS_MAP_SOURCE_YAHOO_STREET:  
         case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:  
         case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:  
             return 17;  
         case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:  
             return 11;  
         case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:  
             return 18;  
         default:  
             return 17;  
     }  
     return 17;  
1581  }  }
1582    #endif
1583    
1584  void  static void
1585  osm_gps_map_download_maps (OsmGpsMap *map, coord_t *pt1, coord_t *pt2, int zoom_start, int zoom_end)  osd_render_controls(osm_gps_map_osd_t *osd)
1586  {  {
1587      int i,j,zoom,num_tiles;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     OsmGpsMapPrivate *priv = map->priv;  
1588    
1589      if (pt1 && pt2)      if(priv->controls.rendered
1590      {  #ifdef OSD_GPS_BUTTON
1591          gchar *filename;         && (priv->controls.gps_enabled == (osd->cb != NULL))
1592          num_tiles = 0;  #endif
1593          zoom_end = CLAMP(zoom_end, priv->min_zoom, priv->max_zoom);         )
1594          g_debug("Download maps: z:%d->%d",zoom_start, zoom_end);          return;
   
         for(zoom=zoom_start; zoom<=zoom_end; zoom++)  
         {  
             int x1,y1,x2,y2;  
   
             x1 = (int)floor((float)lon2pixel(zoom, pt1->rlon) / (float)TILESIZE);  
             y1 = (int)floor((float)lat2pixel(zoom, pt1->rlat) / (float)TILESIZE);  
   
             x2 = (int)floor((float)lon2pixel(zoom, pt2->rlon) / (float)TILESIZE);  
             y2 = (int)floor((float)lat2pixel(zoom, pt2->rlat) / (float)TILESIZE);  
   
             // loop x1-x2  
             for(i=x1; i<=x2; i++)  
             {  
                 // loop y1 - y2  
                 for(j=y1; j<=y2; j++)  
                 {  
                     // x = i, y = j  
                     filename = g_strdup_printf("%s%c%d%c%d%c%d.%s",  
                                     priv->cache_dir, G_DIR_SEPARATOR,  
                                     zoom, G_DIR_SEPARATOR,  
                                     i, G_DIR_SEPARATOR,  
                                     j,  
                                     priv->image_format);  
                     if (!g_file_test(filename, G_FILE_TEST_EXISTS))  
                     {  
                         osm_gps_map_download_tile(map, zoom, i, j, FALSE);  
                         num_tiles++;  
                     }  
                     g_free(filename);  
                 }  
             }  
             g_debug("DL @Z:%d = %d tiles",zoom,num_tiles);  
         }  
     }  
 }  
1595    
1596  void  #ifdef OSD_GPS_BUTTON
1597  osm_gps_map_get_bbox (OsmGpsMap *map, coord_t *pt1, coord_t *pt2)      priv->controls.gps_enabled = (osd->cb != NULL);
1598  {  #endif
1599      OsmGpsMapPrivate *priv = map->priv;      priv->controls.rendered = TRUE;
1600    
1601      if (pt1 && pt2) {  #ifndef OSD_COLOR
1602          pt1->rlat = pixel2lat(priv->map_zoom, priv->map_y);      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1603          pt1->rlon = pixel2lon(priv->map_zoom, priv->map_x);      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
1604          pt2->rlat = pixel2lat(priv->map_zoom, priv->map_y + GTK_WIDGET(map)->allocation.height);      GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
1605          pt2->rlon = pixel2lon(priv->map_zoom, priv->map_x + GTK_WIDGET(map)->allocation.width);  #endif
1606    
1607          g_debug("BBOX: %f %f %f %f", pt1->rlat, pt1->rlon, pt2->rlat, pt2->rlon);      /* first fill with transparency */
1608      }      cairo_t *cr = cairo_create(priv->controls.surface);
1609  }      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1610        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1611        cairo_paint(cr);
1612        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1613    
1614  void      /* --------- draw zoom and dpad shape shadow ----------- */
1615  osm_gps_map_set_mapcenter (OsmGpsMap *map, float latitude, float longitude, int zoom)  #ifdef OSD_SHADOW_ENABLE
1616  {      osd_zoom_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1617      osm_gps_map_set_center (map, latitude, longitude);      osd_shape_shadow(cr);
1618      osm_gps_map_set_zoom (map, zoom);  #ifndef OSD_NO_DPAD
1619  }      osd_dpad_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1620        osd_shape_shadow(cr);
1621    #endif
1622    #endif
1623    
1624  void      /* --------- draw zoom and dpad shape ----------- */
 osm_gps_map_set_center (OsmGpsMap *map, float latitude, float longitude)  
 {  
     int pixel_x, pixel_y;  
     OsmGpsMapPrivate *priv;  
1625    
1626      g_return_if_fail (OSM_IS_GPS_MAP (map));      osd_zoom_shape(cr, 1, 1);
1627      priv = map->priv;  #ifndef OSD_COLOR
1628        osd_shape(cr, &bg, &fg);
1629    #else
1630        osd_shape(cr);
1631    #endif
1632    #ifndef OSD_NO_DPAD
1633        osd_dpad_shape(cr, 1, 1);
1634    #ifndef OSD_COLOR
1635        osd_shape(cr, &bg, &fg);
1636    #else
1637        osd_shape(cr);
1638    #endif
1639    #endif
1640    
1641      priv->center_rlat = deg2rad(latitude);      /* --------- draw zoom and dpad labels --------- */
     priv->center_rlon = deg2rad(longitude);  
     priv->center_coord_set = TRUE;  
1642    
1643      // pixel_x,y, offsets  #ifdef OSD_SHADOW_ENABLE
1644      pixel_x = lon2pixel(priv->map_zoom, priv->center_rlon);      osd_labels_shadow(cr, Z_RAD/3, TRUE);
1645      pixel_y = lat2pixel(priv->map_zoom, priv->center_rlat);      osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1646    #ifndef OSD_NO_DPAD
1647        osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1648    #endif
1649        cairo_stroke(cr);
1650    #ifdef OSD_GPS_BUTTON
1651        osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
1652        osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1653        cairo_stroke(cr);
1654    #endif
1655    #endif
1656    
1657      priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;  #ifndef OSD_COLOR
1658      priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;      osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
1659    #else
1660        osd_labels(cr, Z_RAD/3, TRUE);
1661    #endif
1662        osd_zoom_labels(cr, 1, 1);
1663    #ifndef OSD_NO_DPAD
1664        osd_dpad_labels(cr, 1, 1);
1665    #endif
1666        cairo_stroke(cr);
1667    
1668      osm_gps_map_map_redraw_idle(map);  #ifndef OSD_COLOR
1669        osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
1670    #else
1671        osd_labels(cr, Z_RAD/6, osd->cb != NULL);
1672    #endif
1673    #ifdef OSD_GPS_BUTTON
1674        osd_dpad_gps(cr, 1, 1);
1675    #endif
1676        cairo_stroke(cr);
1677    
1678        cairo_destroy(cr);
1679  }  }
1680    
1681  int  static void
1682  osm_gps_map_set_zoom (OsmGpsMap *map, int zoom)  osd_render(osm_gps_map_osd_t *osd)
1683  {  {
1684      int zoom_old;      /* this function is actually called pretty often since the */
1685      double factor = 0.0;      /* OSD contents may have changed (due to a coordinate/zoom change). */
1686      int width_center, height_center;      /* The different OSD parts have to make sure that they don't */
1687      OsmGpsMapPrivate *priv;      /* render unneccessarily often and thus waste CPU power */
1688    
1689      g_return_val_if_fail (OSM_IS_GPS_MAP (map), 0);      osd_render_controls(osd);
     priv = map->priv;  
   
     if (zoom != priv->map_zoom)  
     {  
         width_center  = GTK_WIDGET(map)->allocation.width / 2;  
         height_center = GTK_WIDGET(map)->allocation.height / 2;  
   
         zoom_old = priv->map_zoom;  
         //constrain zoom min_zoom -> max_zoom  
         priv->map_zoom = CLAMP(zoom, priv->min_zoom, priv->max_zoom);  
   
         if (priv->center_coord_set)  
         {  
             priv->map_x = lon2pixel(priv->map_zoom, priv->center_rlon) - width_center;  
             priv->map_y = lat2pixel(priv->map_zoom, priv->center_rlat) - height_center;  
         }  
         else  
         {  
             factor = exp(priv->map_zoom * M_LN2)/exp(zoom_old * M_LN2);  
             priv->map_x = ((priv->map_x + width_center) * factor) - width_center;  
             priv->map_y = ((priv->map_y + height_center) * factor) - height_center;  
         }  
1690    
1691          g_debug("Zoom changed from %d to %d factor:%f x:%d",  #ifdef OSD_SOURCE_SEL
1692                  zoom_old, priv->map_zoom, factor, priv->map_x);      osd_render_source_sel(osd, FALSE);
1693    #endif
1694    
1695          osm_gps_map_map_redraw_idle(map);  #ifdef OSD_SCALE
1696      }      osd_render_scale(osd);
1697      return priv->map_zoom;  #endif
 }  
1698    
1699  void  #ifdef OSD_CROSSHAIR
1700  osm_gps_map_add_track (OsmGpsMap *map, GSList *track)      osd_render_crosshair(osd);
1701  {  #endif
     OsmGpsMapPrivate *priv;  
1702    
1703      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_NAV
1704      priv = map->priv;      osd_render_nav(osd);
1705    #endif
1706    
1707      if (track) {  #ifdef OSD_COORDINATES
1708          priv->tracks = g_slist_append(priv->tracks, track);      osd_render_coordinates(osd);
1709          osm_gps_map_map_redraw_idle(map);  #endif
     }  
1710  }  }
1711    
1712  void  static void
1713  osm_gps_map_clear_tracks (OsmGpsMap *map)  osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
1714  {  {
1715      g_return_if_fail (OSM_IS_GPS_MAP (map));      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1716    
1717      osm_gps_map_free_tracks(map);      /* OSD itself uses some off-screen rendering, so check if the */
1718      osm_gps_map_map_redraw_idle(map);      /* offscreen buffer is present and create it if not */
1719  }      if(!priv->controls.surface) {
1720            /* create overlay ... */
1721            priv->controls.surface =
1722                cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
1723    
1724  void          priv->controls.rendered = FALSE;
1725  osm_gps_map_add_image (OsmGpsMap *map, float latitude, float longitude, GdkPixbuf *image)  #ifdef OSD_GPS_BUTTON
1726  {          priv->controls.gps_enabled = FALSE;
1727      g_return_if_fail (OSM_IS_GPS_MAP (map));  #endif
1728    
1729      if (image) {  #ifdef OSD_SOURCE_SEL
1730          OsmGpsMapPrivate *priv = map->priv;          /* the initial OSD state is alway not-expanded */
1731          image_t *im;          priv->source_sel.surface =
1732                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1733                                               OSD_S_W+2, OSD_S_H+2);
1734            priv->source_sel.rendered = FALSE;
1735    #endif
1736    
1737          //cache w/h for speed, and add image to list  #ifdef OSD_SCALE
1738          im = g_new0(image_t,1);          priv->scale.surface =
1739          im->w = gdk_pixbuf_get_width(image);              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1740          im->h = gdk_pixbuf_get_height(image);                                         OSD_SCALE_W, OSD_SCALE_H);
1741          im->pt.rlat = deg2rad(latitude);          priv->scale.zoom = -1;
1742          im->pt.rlon = deg2rad(longitude);  #endif
1743    
1744          g_object_ref(image);  #ifdef OSD_CROSSHAIR
1745          im->image = image;          priv->crosshair.surface =
1746                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1747                                           OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1748            priv->crosshair.rendered = FALSE;
1749    #endif
1750    
1751          priv->images = g_slist_append(priv->images, im);  #ifdef OSD_COORDINATES
1752            priv->coordinates.surface =
1753                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1754                                           OSD_COORDINATES_W, OSD_COORDINATES_H);
1755    
1756          osm_gps_map_map_redraw_idle(map);          priv->coordinates.lat = priv->coordinates.lon = OSM_GPS_MAP_INVALID;
1757      }  #endif
 }  
1758    
1759  gboolean          /* ... and render it */
1760  osm_gps_map_remove_image (OsmGpsMap *map, GdkPixbuf *image)          osd_render(osd);
 {  
     OsmGpsMapPrivate *priv = map->priv;  
     if (priv->images) {  
         GSList *list;  
         for(list = priv->images; list != NULL; list = list->next)  
         {  
             image_t *im = list->data;  
                 if (im->image == image)  
                 {  
                         priv->images = g_slist_remove_link(priv->images, list);  
                         g_object_unref(im->image);  
                         g_free(im);  
                         osm_gps_map_map_redraw_idle(map);  
                         return TRUE;  
                 }  
         }  
1761      }      }
     return FALSE;  
 }  
   
 void  
 osm_gps_map_clear_images (OsmGpsMap *map)  
 {  
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
1762    
1763      osm_gps_map_free_images(map);      // now draw this onto the original context
1764      osm_gps_map_map_redraw_idle(map);      cairo_t *cr = gdk_cairo_create(drawable);
 }  
1765    
1766  void      gint x, y;
 osm_gps_map_osd_speed (OsmGpsMap *map, float speed)  
 {  
     OsmGpsMapPrivate *priv;  
1767    
1768      PangoContext        *context = NULL;  #ifdef OSD_SCALE
1769      PangoLayout     *layout  = NULL;      x =  OSD_X;
1770      PangoFontDescription    *desc    = NULL;      y = -OSD_Y;
1771        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1772      GdkColor color;      if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
     GdkGC *gc;  
   
     gchar *buffer;  
     //static int x = 10, y = 10;  
     static int width = 0, height = 0;  
1773    
1774      g_return_if_fail (OSM_IS_GPS_MAP (map));      cairo_set_source_surface(cr, priv->scale.surface, x, y);
1775      priv = map->priv;      cairo_paint(cr);
1776    #endif
1777    
1778      buffer = g_strdup_printf("%.0f", speed);  #ifdef OSD_CROSSHAIR
1779        x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1780        y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1781    
1782      /* pango initialisation */      cairo_set_source_surface(cr, priv->crosshair.surface, x, y);
1783      context = gtk_widget_get_pango_context (GTK_WIDGET(map));      cairo_paint(cr);
1784      layout  = pango_layout_new (context);  #endif
     desc    = pango_font_description_new();  
   
     pango_font_description_set_size (desc, 40 * PANGO_SCALE);  
     pango_layout_set_font_description (layout, desc);  
     pango_layout_set_text (layout, buffer, strlen(buffer));  
   
     gc = gdk_gc_new (GTK_WIDGET(map)->window);  
   
     color.red = (0 > 50) ? 0xffff : 0;  
     color.green = 0;  
     color.blue = 0;  
   
     gdk_gc_set_rgb_fg_color (gc, &color);  
   
     /* faster / less flicker alternative:*/  
     gdk_draw_drawable (  
                        GTK_WIDGET(map)->window,  
                        GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(map)],  
                        priv->pixmap,  
                        0,0,  
                        0,0,  
                        width+10,width+10);  
   
     gdk_draw_layout(GTK_WIDGET(map)->window,  
                     gc,  
                     0, 0,  
                     layout);  
   
     /* set width and height */  
     pango_layout_get_pixel_size(layout, &width, &height);  
   
     g_free(buffer);  
     pango_font_description_free (desc);  
     g_object_unref (layout);  
     g_object_unref (gc);  
 }  
1785    
1786  void  #ifdef OSD_NAV
1787  osm_gps_map_draw_gps (OsmGpsMap *map, float latitude, float longitude, float heading)      if(priv->nav.surface) {
1788  {          x =  OSD_X;
1789      int pixel_x, pixel_y;          if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1790      OsmGpsMapPrivate *priv;          y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1791    
1792            cairo_set_source_surface(cr, priv->nav.surface, x, y);
1793            cairo_paint(cr);
1794        }
1795    #endif
1796    
1797      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_COORDINATES
1798      priv = map->priv;      x = -OSD_X;
1799        y = -OSD_Y;
1800        if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1801        if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1802    
1803      priv->gps->rlat = deg2rad(latitude);      cairo_set_source_surface(cr, priv->coordinates.surface, x, y);
1804      priv->gps->rlon = deg2rad(longitude);      cairo_paint(cr);
1805      priv->gps_valid = TRUE;  #endif
   
     // pixel_x,y, offsets  
     pixel_x = lon2pixel(priv->map_zoom, priv->gps->rlon);  
     pixel_y = lat2pixel(priv->map_zoom, priv->gps->rlat);  
   
     //If trip marker add to list of gps points.  
     if (priv->record_trip_history) {  
         coord_t *tp = g_new0(coord_t,1);  
         tp->rlat = priv->gps->rlat;  
         tp->rlon = priv->gps->rlon;  
         priv->trip_history = g_slist_append(priv->trip_history, tp);  
     }  
   
     // dont draw anything if we are dragging  
     if (priv->dragging) {  
         g_debug("Dragging");  
         return;  
     }  
1806    
1807      //Automatically center the map if the track approaches the edge  #ifdef OSD_BALLOON
1808      if(priv->map_auto_center)   {      if(priv->balloon.surface) {
1809          int x = pixel_x - priv->map_x;  
1810          int y = pixel_y - priv->map_y;          /* convert given lat lon into screen coordinates */
1811          int width = GTK_WIDGET(map)->allocation.width;          gint x, y;
1812          int height = GTK_WIDGET(map)->allocation.height;          osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1813          if( x < (width/2 - width/8)     || x > (width/2 + width/8)  ||                                        priv->balloon.lat, priv->balloon.lon,
1814              y < (height/2 - height/8)   || y > (height/2 + height/8)) {                                        &x, &y);
   
             priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;  
             priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;  
             priv->center_coord_set = FALSE;  
         }  
     }  
1815    
1816      // this redraws the map (including the gps track, and adjusts the          /* check if balloon needs to be rerendered */
1817      // map center if it was changed          osd_render_balloon(osd);
     osm_gps_map_map_redraw_idle(map);  
 }  
1818    
1819  void          cairo_set_source_surface(cr, priv->balloon.surface,
1820  osm_gps_map_clear_gps (OsmGpsMap *map)                                   x + priv->balloon.offset_x,
1821  {                                   y + priv->balloon.offset_y);
1822      osm_gps_map_free_trip(map);          cairo_paint(cr);
1823      osm_gps_map_map_redraw_idle(map);      }
1824  }  #endif
1825    
1826  coord_t      x = OSD_X;
1827  osm_gps_map_get_co_ordinates (OsmGpsMap *map, int pixel_x, int pixel_y)      if(x < 0)
1828  {          x += osd->widget->allocation.width - OSD_W;
     coord_t coord;  
     OsmGpsMapPrivate *priv = map->priv;  
1829    
1830      coord.rlat = pixel2lat(priv->map_zoom, priv->map_y + pixel_y);      y = OSD_Y;
1831      coord.rlon = pixel2lon(priv->map_zoom, priv->map_x + pixel_x);      if(y < 0)
1832      return coord;          y += osd->widget->allocation.height - OSD_H;
 }  
1833    
1834  GtkWidget *      cairo_set_source_surface(cr, priv->controls.surface, x, y);
1835  osm_gps_map_new (void)      cairo_paint(cr);
 {  
     return g_object_new (OSM_TYPE_GPS_MAP, NULL);  
 }  
1836    
1837  void  #ifdef OSD_SOURCE_SEL
1838  osm_gps_map_screen_to_geographic (OsmGpsMap *map, gint pixel_x, gint pixel_y,      if(!priv->source_sel.handler_id) {
1839                                    gfloat *latitude, gfloat *longitude)          /* the OSD source selection is not being animated */
1840  {          if(!priv->source_sel.expanded)
1841      OsmGpsMapPrivate *priv;              x = osd->widget->allocation.width - OSD_S_W;
1842            else
1843                x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1844        } else
1845            x = priv->source_sel.shift;
1846    
1847        y = OSD_S_Y;
1848        if(OSD_S_Y < 0) {
1849            if(!priv->source_sel.expanded)
1850                y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1851            else
1852                y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1853        }
1854    
1855      g_return_if_fail (OSM_IS_GPS_MAP (map));      cairo_set_source_surface(cr, priv->source_sel.surface, x, y);
1856      priv = map->priv;      cairo_paint(cr);
1857    #endif
1858    
1859      if (latitude)      cairo_destroy(cr);
         *latitude = rad2deg(pixel2lat(priv->map_zoom, priv->map_y + pixel_y));  
     if (longitude)  
         *longitude = rad2deg(pixel2lon(priv->map_zoom, priv->map_x + pixel_x));  
1860  }  }
1861    
1862  void  static void
1863  osm_gps_map_geographic_to_screen (OsmGpsMap *map,  osd_free(osm_gps_map_osd_t *osd)
                                   gfloat latitude, gfloat longitude,  
                                   gint *pixel_x, gint *pixel_y)  
1864  {  {
1865      OsmGpsMapPrivate *priv;      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1866    
1867      g_return_if_fail (OSM_IS_GPS_MAP (map));      if (priv->controls.surface)
1868      priv = map->priv;           cairo_surface_destroy(priv->controls.surface);
1869    
1870      if (pixel_x)  #ifdef OSD_SOURCE_SEL
1871          *pixel_x = lon2pixel(priv->map_zoom, deg2rad(longitude)) - priv->map_x;      if(priv->source_sel.handler_id)
1872      if (pixel_y)          gtk_timeout_remove(priv->source_sel.handler_id);
         *pixel_y = lat2pixel(priv->map_zoom, deg2rad(latitude)) - priv->map_y;  
 }  
1873    
1874  void      if (priv->source_sel.surface)
1875  osm_gps_map_scroll (OsmGpsMap *map, gint dx, gint dy)           cairo_surface_destroy(priv->source_sel.surface);
1876  {  #endif
     OsmGpsMapPrivate *priv;  
1877    
1878      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_SCALE
1879      priv = map->priv;      if (priv->scale.surface)
1880             cairo_surface_destroy(priv->scale.surface);
1881    #endif
1882    
1883      priv->center_coord_set = FALSE;  #ifdef OSD_CROSSHAIR
1884      priv->map_x += dx;      if (priv->crosshair.surface)
1885      priv->map_y += dy;           cairo_surface_destroy(priv->crosshair.surface);
1886    #endif
1887    
1888      osm_gps_map_map_redraw_idle (map);  #ifdef OSD_NAV
1889  }      if (priv->nav.surface)
1890             cairo_surface_destroy(priv->nav.surface);
1891    #endif
1892    
1893  float  #ifdef OSD_COORDINATES
1894  osm_gps_map_get_scale(OsmGpsMap *map)      if (priv->coordinates.surface)
1895  {           cairo_surface_destroy(priv->coordinates.surface);
1896      OsmGpsMapPrivate *priv;  #endif
1897    
1898      g_return_val_if_fail (OSM_IS_GPS_MAP (map), OSM_NAN);  #ifdef OSD_BALLOON
1899      priv = map->priv;      if (priv->balloon.surface)
1900             cairo_surface_destroy(priv->balloon.surface);
1901    #endif
1902    
1903      return osm_gps_map_get_scale_at_point(priv->map_zoom, priv->center_rlat, priv->center_rlon);      g_free(priv);
1904  }  }
1905    
1906  #ifdef ENABLE_BALLOON  static gboolean
1907  void  osd_busy(osm_gps_map_osd_t *osd)
 osm_gps_map_draw_balloon (OsmGpsMap *map, float latitude, float longitude,  
                           OsmGpsMapBalloonCallback cb, gpointer data)  
1908  {  {
1909      OsmGpsMapPrivate *priv;  #ifdef OSD_SOURCE_SEL
1910        osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1911      /* remove and previously installed balloon */      return (priv->source_sel.handler_id != 0);
1912      osm_gps_map_clear_balloon (map);  #else
1913        return FALSE;
1914    #endif
1915    }
1916    
1917      g_return_if_fail (OSM_IS_GPS_MAP (map));  static osm_gps_map_osd_t osd_classic = {
1918      priv = map->priv;      .widget     = NULL,
1919    
1920      priv->balloon.coo->rlat = deg2rad(latitude);      .draw       = osd_draw,
1921      priv->balloon.coo->rlon = deg2rad(longitude);      .check      = osd_check,
1922      priv->balloon.valid = TRUE;      .render     = osd_render,
1923        .free       = osd_free,
1924        .busy       = osd_busy,
1925    
1926      priv->balloon.cb = cb;      .cb         = NULL,
1927      priv->balloon.data = data;      .data       = NULL,
1928    
1929      // this redraws the map      .priv       = NULL
1930      osm_gps_map_map_redraw_idle(map);  };
 }  
1931    
1932  void  /* this is the only function that's externally visible */
1933  osm_gps_map_clear_balloon (OsmGpsMap *map)  void
1934    osm_gps_map_osd_classic_init(OsmGpsMap *map)
1935  {  {
1936      OsmGpsMapPrivate *priv;      osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
1937    
1938      priv->balloon.valid = FALSE;  #ifdef OSD_BALLOON
1939        priv->balloon.lat = OSM_GPS_MAP_INVALID;
1940      osm_gps_map_map_redraw_idle(map);      priv->balloon.lon = OSM_GPS_MAP_INVALID;
 }  
1941  #endif  #endif
1942    
1943  #ifdef ENABLE_OSD      osd_classic.priv = priv;
 void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdGpsCallback cb, gpointer data) {  
     OsmGpsMapPrivate *priv;  
1944    
1945      g_return_if_fail (OSM_IS_GPS_MAP (map));      osm_gps_map_register_osd(map, &osd_classic);
1946      priv = map->priv;  }
1947    
1948      priv->osd.cb = cb;  #ifdef OSD_GPS_BUTTON
1949      priv->osd.data = data;  /* below are osd specific functions which aren't used by osm-gps-map */
1950    /* but instead are to be used by the main application */
1951    void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
1952                                     gpointer data) {
1953        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1954        g_return_if_fail (osd);
1955    
1956        osd->cb = cb;
1957        osd->data = data;
1958    
1959      /* this may have changed the state of the gps button */      /* this may have changed the state of the gps button */
1960      /* we thus re-render the overlay */      /* we thus re-render the overlay */
1961      osm_gps_map_osd_render(priv);      osd->render(osd);
1962    
1963      osm_gps_map_map_redraw_idle(map);      osm_gps_map_redraw(map);
1964  }  }
1965  #endif  #endif
1966    
1967    osd_button_t
1968    osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
1969        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1970        g_return_val_if_fail (osd, OSD_NONE);
1971    
1972        return osd_check(osd, TRUE, x, y);
1973    }

Legend:
Removed from v.70  
changed lines
  Added in v.132