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 146 by harbaum, Tue Oct 27 13:28:41 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            gboolean imperial;    // display distance imperial/metric
86        } nav;
87    #endif
88    
89    #ifdef OSD_COORDINATES
90        struct {
91            cairo_surface_t *surface;
92            float lat, lon;
93        } coordinates;
94    #endif
95    
96    #ifdef OSD_SOURCE_SEL
97        struct {
98            /* values to handle the "source" menu */
99            cairo_surface_t *surface;
100            gboolean expanded;
101            gint shift, dir, count;
102            gint handler_id;
103            gint width, height;
104            gboolean rendered;
105        } source_sel;
106    #endif
107    
108  } osd_priv_t;  } osd_priv_t;
109    
110  typedef struct {  #ifdef OSD_BALLOON
111    /* most visual effects are hardcoded by now, but may be made */
112    /* available via properties later */
113    #ifndef BALLOON_AREA_WIDTH
114    #define BALLOON_AREA_WIDTH           290
115    #endif
116    #ifndef BALLOON_AREA_HEIGHT
117    #define BALLOON_AREA_HEIGHT           75
118    #endif
119    #ifndef BALLOON_CORNER_RADIUS
120    #define BALLOON_CORNER_RADIUS         10
121    #endif
122    
123    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
124    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
125    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
126    #define BALLOON_TRANSPARENCY         0.8
127    #define POINTER_HEIGHT                20
128    #define POINTER_FOOT_WIDTH            20
129    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
130    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
131    #define BALLOON_SHADOW_TRANSPARENCY  0.2
132    
133    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
134    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
135    
136    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
137    
138    #define FIN   printf("entering function %s\n", __func__);
139    #define FOUT  printf("leaving function %s\n", __func__);
140    
141    /* draw the bubble shape. this is used twice, once for the shape and once */
142    /* for the shadow */
143    static void
144    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
145           gboolean bottom, int px, int py, int px0, int px1) {
146    
147        FIN;
148    
149        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
150        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
151                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
152        if(!bottom) {
153            /* insert top pointer */
154            cairo_line_to (cr, px1, y0);
155            cairo_line_to (cr, px, py);
156            cairo_line_to (cr, px0, y0);
157        }
158    
159        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
160        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
161                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
162        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
163        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
164                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
165        if(bottom) {
166            /* insert bottom pointer */
167            cairo_line_to (cr, px0, y1);
168            cairo_line_to (cr, px, py);
169            cairo_line_to (cr, px1, y1);
170        }
171    
172        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
173        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
174                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
175    
176        cairo_close_path (cr);
177    
178        FOUT;
179    }
180    
181    static void
182    osd_render_balloon(osm_gps_map_osd_t *osd) {
183        FIN;
184    
185        osd_priv_t *priv = (osd_priv_t*)osd->priv;
186    
187        /* get zoom */
188        gint zoom;
189        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
190    
191        /* ------- convert given coordinate into screen position --------- */
192        gint xs, ys;
193        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
194                                          priv->balloon.lat, priv->balloon.lon,
195                                          &xs, &ys);
196    
197        gint x0 = 1, y0 = 1;
198    
199        /* check position of this relative to screen center to determine */
200        /* pointer direction ... */
201        int pointer_x, pointer_x0, pointer_x1;
202        int pointer_y;
203    
204        /* ... and calculate position */
205        int orientation = 0;
206        if(xs > osd->widget->allocation.width/2) {
207            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
208            pointer_x = x0 - priv->balloon.offset_x;
209            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
210            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
211            orientation |= 1;
212        } else {
213            priv->balloon.offset_x = -POINTER_OFFSET;
214            pointer_x = x0 - priv->balloon.offset_x;
215            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
216            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
217        }
218    
219        gboolean bottom = FALSE;
220        if(ys > osd->widget->allocation.height/2) {
221            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
222            pointer_y = y0 - priv->balloon.offset_y;
223            bottom = TRUE;
224            orientation |= 2;
225        } else {
226            priv->balloon.offset_y = 0;
227            pointer_y = y0 - priv->balloon.offset_y;
228            y0 += POINTER_HEIGHT;
229        }
230    
231        /* if required orientation equals current one, then don't render */
232        /* anything */
233        if(orientation == priv->balloon.orientation)
234            return;
235    
236        priv->balloon.orientation = orientation;
237    
238        /* calculate bottom/right of box */
239        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
240    
241        /* save balloon screen coordinates for later use */
242        priv->balloon.rect.x = x0 + BALLOON_BORDER;
243        priv->balloon.rect.y = y0 + BALLOON_BORDER;
244        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
245        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
246    
247        cairo_t *cr = cairo_create(priv->balloon.surface);
248        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
249        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
250        cairo_paint(cr);
251        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
252    
253        /* --------- draw shadow --------------- */
254        osm_gps_map_draw_balloon_shape (cr,
255                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
256                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
257                     bottom, pointer_x, pointer_y,
258                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
259    
260        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
261        cairo_fill_preserve (cr);
262        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
263        cairo_set_line_width (cr, 0);
264        cairo_stroke (cr);
265    
266        /* --------- draw main shape ----------- */
267        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
268                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
269    
270        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
271        cairo_fill_preserve (cr);
272        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
273        cairo_set_line_width (cr, 1);
274        cairo_stroke (cr);
275    
276        if (priv->balloon.cb) {
277            osm_gps_map_balloon_event_t event;
278    
279            /* clip in case application tries to draw in */
280                /* exceed of the balloon */
281            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
282                             priv->balloon.rect.w, priv->balloon.rect.h);
283            cairo_clip (cr);
284            cairo_new_path (cr);  /* current path is not consumed by cairo_clip */
285    
286            /* request the application to draw the balloon contents */
287            event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW;
288            event.data.draw.rect = &priv->balloon.rect;
289            event.data.draw.cr = cr;
290    
291            priv->balloon.cb(&event, priv->balloon.data);
292        }
293    
294        cairo_destroy(cr);
295    
296        FOUT;
297    }
298    
299    /* return true if balloon is being displayed and if */
300    /* the given coordinate is within this balloon */
301    static gboolean
302    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean click, gboolean down, gint x, gint y)
303    {
304        FIN;
305    
306        osd_priv_t *priv = (osd_priv_t*)osd->priv;
307    
308        if(!priv->balloon.surface)
309            return FALSE;
310    
311        gint xs, ys;
312        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
313                                          priv->balloon.lat, priv->balloon.lon,
314                                          &xs, &ys);
315    
316        xs += priv->balloon.rect.x + priv->balloon.offset_x;
317        ys += priv->balloon.rect.y + priv->balloon.offset_y;
318    
319        gboolean is_in =
320            (x > xs) && (x < xs + priv->balloon.rect.w) &&
321            (y > ys) && (y < ys + priv->balloon.rect.h);
322    
323        /* is this a real click or is the application just checking for something? */
324        if(click) {
325    
326            /* handle the fact that the balloon may have been created by the */
327            /* button down event */
328            if(!is_in && !down && !priv->balloon.just_created) {
329                /* the user actually clicked outside the balloon */
330    
331                /* close the balloon! */
332                osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
333    
334                /* and inform application about this */
335                if(priv->balloon.cb) {
336                    osm_gps_map_balloon_event_t event;
337                    event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED;
338                    priv->balloon.cb(&event, priv->balloon.data);
339                }
340    
341            }
342    
343            if(is_in && priv->balloon.cb) {
344                osm_gps_map_balloon_event_t event;
345    
346                /* notify application of click */
347                event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK;
348                event.data.click.x = x - xs;
349                event.data.click.y = y - ys;
350                event.data.click.down = down;
351    
352                priv->balloon.cb(&event, priv->balloon.data);
353            }
354        }
355        FOUT;
356        return is_in;
357    }
358    
359    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
360        FIN;
361    
362        g_return_if_fail (OSM_IS_GPS_MAP (map));
363    
364        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
365        g_return_if_fail (osd);
366    
367        osd_priv_t *priv = (osd_priv_t*)osd->priv;
368        g_return_if_fail (priv);
369    
370        if(priv->balloon.surface) {
371            cairo_surface_destroy(priv->balloon.surface);
372            priv->balloon.surface = NULL;
373            priv->balloon.lat = OSM_GPS_MAP_INVALID;
374            priv->balloon.lon = OSM_GPS_MAP_INVALID;
375        }
376        osm_gps_map_redraw(map);
377        FOUT;
378    }
379    
380    void
381    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
382                                  OsmGpsMapBalloonCallback cb, gpointer data) {
383        FIN;
384        g_return_if_fail (OSM_IS_GPS_MAP (map));
385    
386        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
387        g_return_if_fail (osd);
388    
389        osd_priv_t *priv = (osd_priv_t*)osd->priv;
390        g_return_if_fail (priv);
391    
392        osm_gps_map_osd_clear_balloon (map);
393    
394        /* allocate balloon surface */
395        priv->balloon.surface =
396            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
397                                       BALLOON_W+2, BALLOON_H+2);
398    
399        priv->balloon.lat = latitude;
400        priv->balloon.lon = longitude;
401        priv->balloon.cb = cb;
402        priv->balloon.data = data;
403        priv->balloon.just_created = TRUE;
404    
405      gpointer priv;      priv->balloon.orientation = -1;
406  } osm_gps_map_osd_t;  
407        osd_render_balloon(osd);
408    
409        osm_gps_map_redraw(map);
410        FOUT;
411    }
412    
413    #endif // OSD_BALLOON
414    
415  /* position and extent of bounding box */  /* position and extent of bounding box */
416    #ifndef OSD_X
417  #define OSD_X      (10)  #define OSD_X      (10)
418  #define OSD_Y      (10)  #endif
419    
420  #define OSD_COLOR            0.5, 0.5, 1  #ifndef OSD_Y
421  #define OSD_COLOR_DISABLED   0.8, 0.8, 0.8  #define OSD_Y      (10)
422    #endif
423    
424  /* parameters of the direction shape */  /* parameters of the direction shape */
425  #ifndef OSM_GPS_MAP_OSD_DIAMETER  #ifndef OSD_DIAMETER
426  #define D_RAD  (30)         // diameter of dpad  #define D_RAD  (30)         // diameter of dpad
427  #else  #else
428  #define D_RAD  (OSM_GPS_MAP_OSD_DIAMETER)  #define D_RAD  (OSD_DIAMETER)
429  #endif  #endif
430  #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
431  #define D_LEN  (D_RAD/4)    // length of arrow  #define D_LEN  (D_RAD/4)    // length of arrow
# Line 59  typedef struct { Line 435  typedef struct {
435  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom
436  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar
437    
438    #ifdef OSD_SHADOW_ENABLE
439  /* shadow also depends on control size */  /* shadow also depends on control size */
440  #define OSD_SHADOW (D_RAD/6)  #define OSD_SHADOW (D_RAD/6)
441    #else
442    #define OSD_SHADOW (0)
443    #endif
444    
445    /* normally the GPS button is in the center of the dpad. if there's */
446    /* no dpad it will go into the zoom area */
447    #if defined(OSD_GPS_BUTTON) && defined(OSD_NO_DPAD)
448    #define Z_GPS  1
449    #else
450    #define Z_GPS  0
451    #endif
452    
453  /* total width and height of controls incl. shadow */  /* total width and height of controls incl. shadow */
454  #define OSD_W    (2*D_RAD + OSD_SHADOW)  #define OSD_W    (2*D_RAD + OSD_SHADOW + Z_GPS * 2 * Z_RAD)
455    #if !Z_GPS
456  #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)
457    #else
458    #define OSD_H    (2*Z_RAD + OSD_SHADOW)
459    #endif
460    
461    #ifdef OSD_SHADOW_ENABLE
462  #define OSD_LBL_SHADOW (OSD_SHADOW/2)  #define OSD_LBL_SHADOW (OSD_SHADOW/2)
463    #endif
464    
465    #define Z_TOP    ((1-Z_GPS) * (2 * D_RAD + Z_STEP))
466    
 #define Z_TOP    (2 * D_RAD + Z_STEP)  
467  #define Z_MID    (Z_TOP + Z_RAD)  #define Z_MID    (Z_TOP + Z_RAD)
468  #define Z_BOT    (Z_MID + Z_RAD)  #define Z_BOT    (Z_MID + Z_RAD)
469  #define Z_LEFT   (Z_RAD)  #define Z_LEFT   (Z_RAD)
470  #define Z_RIGHT  (2 * D_RAD - Z_RAD)  #define Z_RIGHT  (2 * D_RAD - Z_RAD + Z_GPS * 2 * Z_RAD)
471    #define Z_CENTER ((Z_RIGHT + Z_LEFT)/2)
472    
473  /* create the cairo shape used for the zoom buttons */  /* create the cairo shape used for the zoom buttons */
474  static void  static void
475  osm_gps_map_osd_zoom_shape(cairo_t *cr, gint x, gint y) {  osd_zoom_shape(cairo_t *cr, gint x, gint y)
476    {
477      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);
478      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);
479      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 481  osm_gps_map_osd_zoom_shape(cairo_t *cr,
481      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);
482  }  }
483    
484    /* ------------------- color/shadow functions ----------------- */
485    
486    #ifndef OSD_COLOR
487    /* if no color has been specified we just use the gdks default colors */
488    static void
489    osd_labels(cairo_t *cr, gint width, gboolean enabled,
490                           GdkColor *fg, GdkColor *disabled) {
491        if(enabled)  gdk_cairo_set_source_color(cr, fg);
492        else         gdk_cairo_set_source_color(cr, disabled);
493        cairo_set_line_width (cr, width);
494    }
495    #else
496    static void
497    osd_labels(cairo_t *cr, gint width, gboolean enabled) {
498        if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);
499        else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
500        cairo_set_line_width (cr, width);
501    }
502    #endif
503    
504    #ifdef OSD_SHADOW_ENABLE
505    static void
506    osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
507        cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
508        cairo_set_line_width (cr, width);
509    }
510    #endif
511    
512    #ifndef OSD_NO_DPAD
513  /* create the cairo shape used for the dpad */  /* create the cairo shape used for the dpad */
514  static void  static void
515  osm_gps_map_osd_dpad_shape(cairo_t *cr, gint x, gint y) {  osd_dpad_shape(cairo_t *cr, gint x, gint y)
516    {
517      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);
518  }  }
519    #endif
520    
521    #ifdef OSD_SHADOW_ENABLE
522    static void
523    osd_shape_shadow(cairo_t *cr) {
524        cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
525        cairo_fill (cr);
526        cairo_stroke (cr);
527    }
528    #endif
529    
530    #ifndef OSD_COLOR
531    /* if no color has been specified we just use the gdks default colors */
532    static void
533    osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
534        gdk_cairo_set_source_color(cr, bg);
535        cairo_fill_preserve (cr);
536        gdk_cairo_set_source_color(cr, fg);
537        cairo_set_line_width (cr, 1);
538        cairo_stroke (cr);
539    }
540    #else
541    static void
542    osd_shape(cairo_t *cr) {
543        cairo_set_source_rgb (cr, OSD_COLOR_BG);
544        cairo_fill_preserve (cr);
545        cairo_set_source_rgb (cr, OSD_COLOR);
546        cairo_set_line_width (cr, 1);
547        cairo_stroke (cr);
548    }
549    #endif
550    
 typedef enum {  
     OSD_NONE = 0,  
     OSD_BG,  
     OSD_UP,  
     OSD_DOWN,  
     OSD_LEFT,  
     OSD_RIGHT,  
     OSD_IN,  
     OSD_OUT,  
     OSD_GPS  
 } osd_button_t;  
551    
552  static gboolean  static gboolean
553  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)
554  {  {
555        FIN;
556      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
557  }  }
558    
559    #ifndef OSD_NO_DPAD
560  /* check whether x/y is within the dpad */  /* check whether x/y is within the dpad */
561  static osd_button_t  static osd_button_t
562  osm_gps_map_osd_check_dpad(gint x, gint y)  osd_check_dpad(gint x, gint y)
563  {  {
564      /* within entire dpad circle */      /* within entire dpad circle */
565      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))
566      {      {
567          /* convert into position relative to dpads centre */          /* convert into position relative to dpads centre */
568          x -= (OSD_X + D_RAD);          x -= D_RAD;
569          y -= (OSD_Y + D_RAD);          y -= D_RAD;
570    
571    #ifdef OSD_GPS_BUTTON
572          /* check for dpad center goes here! */          /* check for dpad center goes here! */
573          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))
574              return OSD_GPS;              return OSD_GPS;
575    #endif
576    
577          if( y < 0 && abs(x) < abs(y))          if( y < 0 && abs(x) < abs(y))
578              return OSD_UP;              return OSD_UP;
# Line 139  osm_gps_map_osd_check_dpad(gint x, gint Line 590  osm_gps_map_osd_check_dpad(gint x, gint
590      }      }
591      return OSD_NONE;      return OSD_NONE;
592  }  }
593    #endif
594    
595  /* check whether x/y is within the zoom pads */  /* check whether x/y is within the zoom pads */
596  static osd_button_t  static osd_button_t
597  osm_gps_map_osd_check_zoom(gint x, gint y) {  osd_check_zoom(gint x, gint y) {
598      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) {
599    
600          /* within circle around (-) label */          /* within circle around (-) label */
601          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)  
602              return OSD_OUT;              return OSD_OUT;
603    
604          /* within circle around (+) label */          /* within circle around (+) label */
605          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))
606              return OSD_IN;              return OSD_IN;
607    
608    #if Z_GPS == 1
609            /* within square around center */
610            if( x > Z_CENTER - Z_RAD && x < Z_CENTER + Z_RAD)
611                return OSD_GPS;
612    #endif
613    
614            /* between center of (-) button and center of entire zoom control area */
615            if(x > OSD_LEFT && x < D_RAD)
616                return OSD_OUT;
617    
618          /* between center of (+) button and center of entire zoom control area */          /* between center of (+) button and center of entire zoom control area */
619          if(x < OSD_RIGHT && x > OSD_X + D_RAD)          if(x < OSD_RIGHT && x > D_RAD)
620              return OSD_IN;              return OSD_IN;
621      }      }
622    
623      return OSD_NONE;      return OSD_NONE;
624  }  }
625    
626  static osd_button_t  #ifdef OSD_SOURCE_SEL
 osm_gps_map_osd_check(gint x, gint y) {  
     osd_button_t but = OSD_NONE;  
627    
628      /* first do a rough test for the OSD area. */  /* place source selection at right border */
629      /* this is just to avoid an unnecessary detailed test */  #define OSD_S_RAD (Z_RAD)
630      if(x > OSD_X && x < OSD_X + OSD_W &&  #define OSD_S_X   (-OSD_X)
631         y > OSD_Y && y < OSD_Y + OSD_H) {  #define OSD_S_Y   (OSD_Y)
632          but = osm_gps_map_osd_check_dpad(x, y);  #define OSD_S_PW  (2 * Z_RAD)
633    #define OSD_S_W   (OSD_S_PW)
634    #define OSD_S_PH  (2 * Z_RAD)
635    #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
636    
637    /* size of usable area when expanded */
638    #define OSD_S_AREA_W (priv->source_sel.width)
639    #define OSD_S_AREA_H (priv->source_sel.height)
640    #define OSD_S_EXP_W  (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
641    #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
642    
643    /* internal value to draw the arrow on the "puller" */
644    #define OSD_S_D0  (OSD_S_RAD/2)
645    #ifndef OSD_FONT_SIZE
646    #define OSD_FONT_SIZE (16.0)
647    #endif
648    #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)
649    #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)
650    
651          if(but == OSD_NONE)  /* draw the shape of the source selection OSD, either only the puller (not expanded) */
652              but = osm_gps_map_osd_check_zoom(x, y);  /* or the entire menu incl. the puller (expanded) */
653    static void
654    osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
655        if(!priv->source_sel.expanded) {
656            /* just draw the puller */
657            cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
658            cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
659            cairo_line_to (cr, x + OSD_S_PW, y);
660        } else {
661            /* draw the puller and the area itself */
662            cairo_move_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y + OSD_S_AREA_H);
663            cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H);
664            if(OSD_S_Y > 0) {
665                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_PH);
666                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
667            } else {
668                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_AREA_H-OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
669                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H - OSD_S_PH);
670                cairo_line_to (cr, x + OSD_S_PW, y);
671            }
672            cairo_line_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y);
673            cairo_close_path (cr);
674      }      }
675    }
676    
677      return but;  static void
678    osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
679        osd_priv_t *priv = (osd_priv_t*)osd->priv;
680    
681        int py = offset + OSD_S_RAD - OSD_S_D0;
682    
683        if(!priv->source_sel.expanded) {
684            /* draw the "puller" open (<) arrow */
685            cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
686            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
687            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
688        } else {
689            if(OSD_S_Y < 0)
690                py += OSD_S_AREA_H - OSD_S_PH;
691    
692            /* draw the "puller" close (>) arrow */
693            cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
694            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
695            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
696            cairo_stroke(cr);
697    
698            /* don't draw a shadow for the text content */
699            if(offset == 1) {
700                gint source;
701                g_object_get(osd->widget, "map-source", &source, NULL);
702    
703                cairo_select_font_face (cr, "Sans",
704                                        CAIRO_FONT_SLANT_NORMAL,
705                                        CAIRO_FONT_WEIGHT_BOLD);
706                cairo_set_font_size (cr, OSD_FONT_SIZE);
707    
708                int i, step = (priv->source_sel.height - 2*OSD_TEXT_BORDER) /
709                    OSM_GPS_MAP_SOURCE_LAST;
710                for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
711                    cairo_text_extents_t extents;
712                    const char *src = osm_gps_map_source_get_friendly_name(i);
713                    cairo_text_extents (cr, src, &extents);
714    
715                    int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
716                    int y = offset + step * (i-1) + OSD_TEXT_BORDER;
717    
718                    /* draw filled rectangle if selected */
719                    if(source == i) {
720                        cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
721                                        y - OSD_TEXT_SKIP,
722                                        priv->source_sel.width - OSD_TEXT_BORDER,
723                                        step + OSD_TEXT_SKIP);
724                        cairo_fill(cr);
725    
726                        /* temprarily draw with background color */
727    #ifndef OSD_COLOR
728                        GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
729                        gdk_cairo_set_source_color(cr, &bg);
730    #else
731                        cairo_set_source_rgb (cr, OSD_COLOR_BG);
732    #endif
733                    }
734    
735                    cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
736                    cairo_show_text (cr, src);
737    
738                    /* restore color */
739                    if(source == i) {
740    #ifndef OSD_COLOR
741                        GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
742                        gdk_cairo_set_source_color(cr, &fg);
743    #else
744                        cairo_set_source_rgb (cr, OSD_COLOR);
745    #endif
746                    }
747                }
748            }
749        }
750  }  }
751    
752  static void  static void
753  osm_gps_map_osd_shape_shadow(cairo_t *cr) {  osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
754      cairo_set_source_rgba (cr, 0, 0, 0, 0.2);      FIN;
755      cairo_fill (cr);  
756        osd_priv_t *priv = (osd_priv_t*)osd->priv;
757    
758        if(priv->source_sel.rendered && !force_rerender)
759            return;
760    
761        priv->source_sel.rendered = TRUE;
762    
763    #ifndef OSD_COLOR
764        GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
765        GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
766        GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
767    #endif
768    
769        /* draw source selector */
770        cairo_t *cr = cairo_create(priv->source_sel.surface);
771        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
772        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
773        cairo_paint(cr);
774        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
775    
776    #ifdef OSD_SHADOW_ENABLE
777        osd_source_shape(priv, cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
778        osd_shape_shadow(cr);
779    #endif
780    
781        osd_source_shape(priv, cr, 1, 1);
782    #ifndef OSD_COLOR
783        osd_shape(cr, &bg, &fg);
784    #else
785        osd_shape(cr);
786    #endif
787    
788    #ifdef OSD_SHADOW_ENABLE
789        osd_labels_shadow(cr, Z_RAD/3, TRUE);
790        osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
791        cairo_stroke (cr);
792    #endif
793    #ifndef OSD_COLOR
794        osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
795    #else
796        osd_labels(cr, Z_RAD/3, TRUE);
797    #endif
798        osd_source_content(osd, cr, 1);
799      cairo_stroke (cr);      cairo_stroke (cr);
800    
801        cairo_destroy(cr);
802        FOUT;
803  }  }
804    
805  static void  /* re-allocate the buffer used to draw the menu. This is used */
806  osm_gps_map_osd_shape(cairo_t *cr) {  /* to collapse/expand the buffer */
807      cairo_set_source_rgb (cr, 1, 1, 1);  static void
808      cairo_fill_preserve (cr);  osd_source_reallocate(osm_gps_map_osd_t *osd) {
809      cairo_set_source_rgb (cr, OSD_COLOR);      FIN;
810      cairo_set_line_width (cr, 1);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
811      cairo_stroke (cr);  
812        /* re-allocate offscreen bitmap */
813        g_assert (priv->source_sel.surface);
814    
815        int w = OSD_S_W, h = OSD_S_H;
816        if(priv->source_sel.expanded) {
817            cairo_text_extents_t extents;
818    
819            /* determine content size */
820            cairo_t *cr = cairo_create(priv->source_sel.surface);
821            cairo_select_font_face (cr, "Sans",
822                                    CAIRO_FONT_SLANT_NORMAL,
823                                    CAIRO_FONT_WEIGHT_BOLD);
824            cairo_set_font_size (cr, OSD_FONT_SIZE);
825    
826            /* calculate menu size */
827            int i, max_h = 0, max_w = 0;
828            for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
829                const char *src = osm_gps_map_source_get_friendly_name(i);
830                cairo_text_extents (cr, src, &extents);
831    
832                if(extents.width > max_w) max_w = extents.width;
833                if(extents.height > max_h) max_h = extents.height;
834            }
835            cairo_destroy(cr);
836    
837            priv->source_sel.width  = max_w + 2*OSD_TEXT_BORDER;
838            priv->source_sel.height = OSM_GPS_MAP_SOURCE_LAST *
839                (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
840    
841            w = OSD_S_EXP_W;
842            h = OSD_S_EXP_H;
843        }
844    
845        cairo_surface_destroy(priv->source_sel.surface);
846        priv->source_sel.surface =
847            cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
848    
849        osd_render_source_sel(osd, TRUE);
850        FOUT;
851    }
852    
853    #define OSD_HZ      15
854    #define OSD_TIME    500
855    
856    static gboolean osd_source_animate(gpointer data) {
857        FIN;
858        osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
859        osd_priv_t *priv = (osd_priv_t*)osd->priv;
860        int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
861        gboolean done = FALSE;
862        priv->source_sel.count += priv->source_sel.dir;
863    
864        /* shifting in */
865        if(priv->source_sel.dir < 0) {
866            if(priv->source_sel.count <= 0) {
867                priv->source_sel.count = 0;
868                done = TRUE;
869            }
870        } else {
871            if(priv->source_sel.count >= 1000) {
872                priv->source_sel.expanded = FALSE;
873                osd_source_reallocate(osd);
874    
875                priv->source_sel.count = 1000;
876                done = TRUE;
877            }
878        }
879    
880    
881        /* count runs linearly from 0 to 1000, map this nicely onto a position */
882    
883        /* nice sinoid mapping */
884        float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;
885        priv->source_sel.shift =
886            (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
887            m * diff;
888    
889        /* make sure the screen is updated */
890        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
891    
892        /* stop animation if done */
893        if(done)
894            priv->source_sel.handler_id = 0;
895    
896        FOUT;
897        return !done;
898  }  }
899    
900    /* switch between expand and collapse mode of source selection */
901  static void  static void
902  osm_gps_map_osd_dpad_labels(cairo_t *cr, gint x, gint y) {  osd_source_toggle(osm_gps_map_osd_t *osd)
903    {
904        FIN;
905        osd_priv_t *priv = (osd_priv_t*)osd->priv;
906    
907        /* ignore clicks while animation is running */
908        if(priv->source_sel.handler_id)
909            return;
910    
911        /* expand immediately, collapse is handle at the end of the */
912        /* collapse animation */
913        if(!priv->source_sel.expanded) {
914            priv->source_sel.expanded = TRUE;
915            osd_source_reallocate(osd);
916    
917            priv->source_sel.count = 1000;
918            priv->source_sel.shift = osd->widget->allocation.width - OSD_S_W;
919            priv->source_sel.dir = -1000/OSD_HZ;
920        } else {
921            priv->source_sel.count =  0;
922            priv->source_sel.shift = osd->widget->allocation.width -
923                OSD_S_EXP_W + OSD_S_X;
924            priv->source_sel.dir = +1000/OSD_HZ;
925        }
926    
927        /* start timer to handle animation */
928        priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
929                                                      osd_source_animate, osd);
930        FOUT;
931    }
932    
933    /* check if the user clicked inside the source selection area */
934    static osd_button_t
935    osd_source_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
936        FIN;
937        osd_priv_t *priv = (osd_priv_t*)osd->priv;
938    
939        if(!priv->source_sel.expanded)
940            x -= osd->widget->allocation.width - OSD_S_W;
941        else
942            x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
943    
944        if(OSD_S_Y > 0)
945            y -= OSD_S_Y;
946        else
947            y -= osd->widget->allocation.height - OSD_S_PH + OSD_S_Y;
948    
949        /* within square around puller? */
950        if(y > 0 && y < OSD_S_PH && x > 0 && x < OSD_S_PW) {
951            /* really within puller shape? */
952            if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
953                /* expand source selector */
954                if(down)
955                    osd_source_toggle(osd);
956    
957                /* tell upper layers that user clicked some background element */
958                /* of the OSD */
959                return OSD_BG;
960            }
961        }
962    
963        /* check for clicks into data area */
964        if(priv->source_sel.expanded && !priv->source_sel.handler_id) {
965            /* re-adjust from puller top to content top */
966            if(OSD_S_Y < 0)
967                y += OSD_S_EXP_H - OSD_S_PH;
968    
969            if(x > OSD_S_PW &&
970               x < OSD_S_PW + OSD_S_EXP_W &&
971               y > 0 &&
972               y < OSD_S_EXP_H) {
973    
974                int step = (priv->source_sel.height - 2*OSD_TEXT_BORDER)
975                    / OSM_GPS_MAP_SOURCE_LAST;
976    
977                y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
978                y /= step;
979                y += 1;
980    
981                if(down) {
982                    gint old = 0;
983                    g_object_get(osd->widget, "map-source", &old, NULL);
984    
985                    if(y > OSM_GPS_MAP_SOURCE_NULL &&
986                       y <= OSM_GPS_MAP_SOURCE_LAST &&
987                       old != y) {
988                        g_object_set(osd->widget, "map-source", y, NULL);
989    
990                        osd_render_source_sel(osd, TRUE);
991                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
992                    }
993                }
994    
995                /* return "clicked in OSD background" to prevent further */
996                /* processing by application */
997                return OSD_BG;
998            }
999        }
1000    
1001        FOUT;
1002        return OSD_NONE;
1003    }
1004    #endif // OSD_SOURCE_SEL
1005    
1006    static osd_button_t
1007    osd_check_int(osm_gps_map_osd_t *osd, gboolean click, gboolean down, gint x, gint y) {
1008        FIN;
1009        osd_button_t but = OSD_NONE;
1010    
1011    #ifdef OSD_BALLOON
1012        if(down) {
1013            /* needed to handle balloons that are created at click */
1014            osd_priv_t *priv = (osd_priv_t*)osd->priv;
1015            priv->balloon.just_created = FALSE;
1016        }
1017    #endif
1018    
1019    #ifdef OSD_SOURCE_SEL
1020        /* the source selection area is handles internally */
1021        but = osd_source_check(osd, down, x, y);
1022    #endif
1023    
1024        if(but == OSD_NONE) {
1025            gint mx = x - OSD_X;
1026            gint my = y - OSD_Y;
1027    
1028            if(OSD_X < 0)
1029                mx -= (osd->widget->allocation.width - OSD_W);
1030    
1031            if(OSD_Y < 0)
1032                my -= (osd->widget->allocation.height - OSD_H);
1033    
1034            /* first do a rough test for the OSD area. */
1035            /* this is just to avoid an unnecessary detailed test */
1036            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
1037    #ifndef OSD_NO_DPAD
1038                but = osd_check_dpad(mx, my);
1039    #endif
1040            }
1041    
1042            if(but == OSD_NONE)
1043                but = osd_check_zoom(mx, my);
1044        }
1045    
1046    #ifdef OSD_BALLOON
1047        if(but == OSD_NONE) {
1048            /* check if user clicked into balloon */
1049            if(osd_balloon_check(osd, click, down, x, y))
1050                but = OSD_BG;
1051        }
1052    #endif
1053    
1054        FOUT;
1055        return but;
1056    }
1057    
1058    #ifndef OSD_NO_DPAD
1059    static void
1060    osd_dpad_labels(cairo_t *cr, gint x, gint y) {
1061      /* move reference to dpad center */      /* move reference to dpad center */
1062      x += D_RAD;      x += D_RAD;
1063      y += D_RAD;      y += D_RAD;
# Line 222  osm_gps_map_osd_dpad_labels(cairo_t *cr, Line 1080  osm_gps_map_osd_dpad_labels(cairo_t *cr,
1080          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]);
1081      }      }
1082  }  }
1083    #endif
1084    
1085  /* draw the sattelite dish icon in the center of the dpad */  #ifdef OSD_GPS_BUTTON
1086  #define GPS_V0  (D_RAD/8)  /* draw the satellite dish icon in the center of the dpad */
1087    #define GPS_V0  (D_RAD/7)
1088  #define GPS_V1  (D_RAD/10)  #define GPS_V1  (D_RAD/10)
1089  #define GPS_V2  (D_RAD/5)  #define GPS_V2  (D_RAD/5)
1090    
1091  /* draw a satellite receiver dish */  /* draw a satellite receiver dish */
1092    /* this is either drawn in the center of the dpad (if present) */
1093    /* or in the middle of the zoom area */
1094  static void  static void
1095  osm_gps_map_osd_dpad_gps(cairo_t *cr, gint x, gint y) {  osd_dpad_gps(cairo_t *cr, gint x, gint y) {
1096      /* move reference to dpad center */      /* move reference to dpad center */
1097      x += D_RAD;      x += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD * 3;
1098      y += D_RAD + GPS_V0;      y += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD + GPS_V0;
1099    
1100      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
1101      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 1110  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi
1110      cairo_move_to (cr, x, y-GPS_V2);      cairo_move_to (cr, x, y-GPS_V2);
1111      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
1112  }  }
1113    #endif
1114    
1115  #define Z_LEN  (2*Z_RAD/3)  #define Z_LEN  (2*Z_RAD/3)
1116    
1117  static void  static void
1118  osm_gps_map_osd_zoom_labels(cairo_t *cr, gint x, gint y) {  osd_zoom_labels(cairo_t *cr, gint x, gint y) {
1119      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);
1120      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);
1121    
# Line 262  osm_gps_map_osd_zoom_labels(cairo_t *cr, Line 1125  osm_gps_map_osd_zoom_labels(cairo_t *cr,
1125      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
1126  }  }
1127    
1128  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;  
   
   
 #ifdef USE_CAIRO  
     /* 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);  
1129    
1130  #else  #ifndef OSD_COORDINATES_FONT_SIZE
1131  #warning "OSD control display lacks a non-cairo implementation!"  #define OSD_COORDINATES_FONT_SIZE (12.0)
1132  #endif  #endif
 }  
1133    
1134  static void  #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
 osm_gps_map_osd_restore (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
1135    
1136      /* restore backup of previous contents */  #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1137      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);  
     }  
 }  
1138    
1139    /* these can be overwritten with versions that support */
1140    /* localization */
1141    #ifndef OSD_COORDINATES_CHR_N
1142    #define OSD_COORDINATES_CHR_N  "N"
1143  #endif  #endif
1144    #ifndef OSD_COORDINATES_CHR_S
1145  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;  
1146  #endif  #endif
1147    #ifndef OSD_COORDINATES_CHR_E
1148  #ifdef ENABLE_OSD  #define OSD_COORDINATES_CHR_E  "E"
     priv->osd.backup = NULL;  
     priv->osd.overlay = NULL;  
     priv->osd.cb = NULL;  
1149  #endif  #endif
1150    #ifndef OSD_COORDINATES_CHR_W
1151      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();  
1152  #endif  #endif
1153    
     //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);  
1154    
     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 };  
1155    
1156      while(*str) {  /* this is the classic geocaching notation */
1157          hash.str[(int)str & 3] ^= *str;  static char
1158          str++;  *osd_latitude_str(float latitude) {
1159        char *c = OSD_COORDINATES_CHR_N;
1160        float integral, fractional;
1161    
1162        if(isnan(latitude))
1163            return NULL;
1164    
1165        if(latitude < 0) {
1166            latitude = fabs(latitude);
1167            c = OSD_COORDINATES_CHR_S;
1168      }      }
1169      return g_strdup_printf("%08lX", hash.val);  
1170        fractional = modff(latitude, &integral);
1171    
1172        return g_strdup_printf("%s %02d° %06.3f'",
1173                               c, (int)integral, fractional*60.0);
1174  }  }
 #endif  
1175    
1176  static GObject *  static char
1177  osm_gps_map_constructor (GType gtype, guint n_properties, GObjectConstructParam *properties)  *osd_longitude_str(float longitude) {
1178  {      char *c = OSD_COORDINATES_CHR_E;
1179      GObject *object;      float integral, fractional;
1180      OsmGpsMapPrivate *priv;  
1181      OsmGpsMap *map;      if(isnan(longitude))
1182      const char *uri;          return NULL;
1183    
1184      //Always chain up to the parent constructor      if(longitude < 0) {
1185      object = G_OBJECT_CLASS(osm_gps_map_parent_class)->constructor(gtype, n_properties, properties);          longitude = fabs(longitude);
1186      map = OSM_GPS_MAP(object);          c = OSD_COORDINATES_CHR_W;
     priv = OSM_GPS_MAP_PRIVATE(object);  
   
     //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);  
         }  
1187      }      }
1188    
1189      if (!priv->cache_dir_is_full_path) {      fractional = modff(longitude, &integral);
1190  #ifdef G_CHECKSUM_MD5  
1191          char *md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, priv->repo_uri, -1);      return g_strdup_printf("%s %03d° %06.3f'",
1192  #else                             c, (int)integral, fractional*60.0);
1193          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);  
         }  
   
         g_free(md5);  
     }  
1194    
1195      inspect_map_uri(map);  /* render a string at the given screen position */
1196    static int
1197    osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1198        FIN;
1199    
1200      return object;      printf("params: %p %d %d %p\n", cr, y, width, text); // XXX
 }  
1201    
1202  static void      if(!text) return y;
 osm_gps_map_dispose (GObject *object)  
 {  
     OsmGpsMap *map = OSM_GPS_MAP(object);  
     OsmGpsMapPrivate *priv = map->priv;  
1203    
1204      if (priv->is_disposed)      printf("text given: %s\n", text); // XXX
         return;  
1205    
1206      priv->is_disposed = TRUE;      char *p = g_malloc(strlen(text)+4);  // space for "...\n"
1207        strcpy(p, text);
1208    
1209      soup_session_abort(priv->soup_session);      cairo_text_extents_t extents;
     g_object_unref(priv->soup_session);  
1210    
1211      g_hash_table_destroy(priv->tile_queue);      printf("test extents of \"abc\"\n");
1212      g_hash_table_destroy(priv->missing_tiles);      memset(&extents, 0, sizeof(cairo_text_extents_t));
1213      g_hash_table_destroy(priv->tile_cache);      cairo_text_extents (cr, "abc", &extents);
1214        printf("-> %f\n", extents.width);
1215    
1216      osm_gps_map_free_images(map);      printf("test extents of \"49 00.000\"\n");
1217        memset(&extents, 0, sizeof(cairo_text_extents_t));
1218        cairo_text_extents (cr, "49 00.000", &extents);
1219        printf("-> %f\n", extents.width);
1220    
1221      if(priv->pixmap)      printf("test extents of \"49° 00.000\"\n");
1222          g_object_unref (priv->pixmap);      memset(&extents, 0, sizeof(cairo_text_extents_t));
1223        cairo_text_extents (cr, "49° 00.000", &extents);
1224        printf("-> %f\n", extents.width);
1225    
1226      if (priv->null_tile)      printf("getting extents for \"%s\"\n", p);  // XXX
1227          g_object_unref (priv->null_tile);      memset(&extents, 0, sizeof(cairo_text_extents_t));
1228        cairo_text_extents (cr, p, &extents);
1229        printf("initial extents.width = %f\n", extents.width);  // XXX
1230    
1231      if(priv->gc_map)      /* check if text needs to be truncated */
1232          g_object_unref(priv->gc_map);      int trunc_at = strlen(text)-1;
1233        while(extents.width > width) {
1234            g_assert(trunc_at > 0);
1235    
1236      if (priv->idle_map_redraw != 0)          printf("trunc at %d\n", trunc_at);  // XXX
         g_source_remove (priv->idle_map_redraw);  
1237    
1238      g_free(priv->gps);          trunc_at--;
1239            strcpy(p+trunc_at, "...");
1240            printf("getting extents for \"%s\"\n", p);  // XXX
1241    
1242  #ifdef ENABLE_BALLOON          memset(&extents, 0, sizeof(cairo_text_extents_t));
1243      g_free(priv->balloon.coo);          cairo_text_extents (cr, p, &extents);
1244  #endif          printf("extents.width = %f > %d\n", extents.width, width);  // XXX
1245        }
1246    
1247  #ifdef ENABLE_OSD      printf("painting\n"); // XXX
     if (priv->osd.backup)  
         g_object_unref(priv->osd.backup);  
1248    
1249      if (priv->osd.overlay)      cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1250           cairo_surface_destroy(priv->osd.overlay);      cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1251  #endif      cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1252        cairo_text_path (cr, p);
1253        cairo_stroke (cr);
1254    
1255      G_OBJECT_CLASS (osm_gps_map_parent_class)->dispose (object);      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1256        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1257        cairo_show_text (cr, p);
1258    
1259        g_free(p);
1260    
1261        /* skip + 1/5 line */
1262        FOUT;
1263        return y + 6*OSD_COORDINATES_FONT_SIZE/5;
1264  }  }
1265    
1266  static void  static void
1267  osm_gps_map_finalize (GObject *object)  osd_render_coordinates(osm_gps_map_osd_t *osd)
1268  {  {
1269      OsmGpsMap *map = OSM_GPS_MAP(object);      FIN;
1270      OsmGpsMapPrivate *priv = map->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1271    
1272      g_free(priv->cache_dir);      /* get current map position */
1273      g_free(priv->repo_uri);      gfloat lat, lon;
1274      g_free(priv->image_format);      g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1275    
1276        /* check if position has changed enough to require redraw */
1277        if(!isnan(priv->coordinates.lat) && !isnan(priv->coordinates.lon))
1278            /* 1/60000 == 1/1000 minute */
1279            if((fabsf(lat - priv->coordinates.lat) < 1/60000) &&
1280               (fabsf(lon - priv->coordinates.lon) < 1/60000))
1281                return;
1282    
1283      osm_gps_map_free_trip(map);      priv->coordinates.lat = lat;
1284      osm_gps_map_free_tracks(map);      priv->coordinates.lon = lon;
1285    
1286      G_OBJECT_CLASS (osm_gps_map_parent_class)->finalize (object);      /* first fill with transparency */
1287  }      cairo_t *cr = cairo_create(priv->coordinates.surface);
1288        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1289  static void      //    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1290  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);
1291  {      cairo_paint(cr);
1292      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;  
1293    
1294      switch (prop_id)      cairo_select_font_face (cr, "Sans",
1295      {                              CAIRO_FONT_SLANT_NORMAL,
1296          case PROP_AUTO_CENTER:                              CAIRO_FONT_WEIGHT_BOLD);
1297              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);  
1298    
1299                  g_object_set_property(G_OBJECT(priv->soup_session),SOUP_SESSION_PROXY_URI,&val);      char *latitude = osd_latitude_str(lat);
1300  #else      char *longitude = osd_longitude_str(lon);
1301                  SoupUri* uri = soup_uri_new(priv->proxy_uri);  
1302                  g_object_set(G_OBJECT(priv->soup_session), SOUP_SESSION_PROXY_URI, uri, NULL);      int y = OSD_COORDINATES_OFFSET;
1303  #endif      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1304              } else      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1305                  priv->proxy_uri = NULL;  
1306        g_free(latitude);
1307        g_free(longitude);
1308    
1309              break;      cairo_destroy(cr);
1310          case PROP_TILE_CACHE_DIR:      FOUT;
             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;  
     }  
1311  }  }
1312    #endif  // OSD_COORDINATES
1313    
1314  static void  #ifdef OSD_NAV
1315  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)
1316  {  #define OSD_NAV_H  (11*OSD_COORDINATES_FONT_SIZE)
     g_return_if_fail (OSM_IS_GPS_MAP (object));  
     float lat,lon;  
     OsmGpsMap *map = OSM_GPS_MAP(object);  
     OsmGpsMapPrivate *priv = map->priv;  
1317    
1318      switch (prop_id)  /* http://mathforum.org/library/drmath/view/55417.html */
1319      {  static float get_bearing(float lat1, float lon1, float lat2, float lon2) {
1320          case PROP_AUTO_CENTER:    return atan2( sin(lon2 - lon1) * cos(lat2),
1321              g_value_set_boolean(value, priv->map_auto_center);                  cos(lat1) * sin(lat2) -
1322              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;  
     }  
1323  }  }
1324    
1325  static gboolean  /* http://mathforum.org/library/drmath/view/51722.html */
1326  osm_gps_map_scroll_event (GtkWidget *widget, GdkEventScroll  *event)  static float get_distance(float lat1, float lon1, float lat2, float lon2) {
1327  {    float aob = acos(cos(lat1) * cos(lat2) * cos(lon2 - lon1) +
1328      OsmGpsMap *map = OSM_GPS_MAP(widget);                     sin(lat1) * sin(lat2));
     OsmGpsMapPrivate *priv = map->priv;  
1329    
1330      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);  
     }  
1331    
1332      return FALSE;    return(aob * 6371000.0);     /* great circle radius in meters */
1333  }  }
1334    
1335  static gboolean  static void
1336  osm_gps_map_button_press (GtkWidget *widget, GdkEventButton *event)  osd_render_nav(osm_gps_map_osd_t *osd)
1337  {  {
1338      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);      FIN;
1339        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  
1340    
1341      /* pressed inside OSD control? */      if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1342      osd_button_t but = osm_gps_map_osd_check(event->x, event->y);          return;
     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  
1343    
1344      priv->drag_counter = 0;      /* first fill with transparency */
1345      priv->drag_start_mouse_x = (int) event->x;      cairo_t *cr = cairo_create(priv->nav.surface);
1346      priv->drag_start_mouse_y = (int) event->y;      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1347      priv->drag_start_map_x = priv->map_x;      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1348      priv->drag_start_map_y = priv->map_y;      cairo_paint(cr);
1349        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1350    
1351      return FALSE;      cairo_select_font_face (cr, "Sans",
1352  }                              CAIRO_FONT_SLANT_NORMAL,
1353                                CAIRO_FONT_WEIGHT_BOLD);
1354        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1355    
1356  static gboolean      char *latitude = osd_latitude_str(priv->nav.lat);
1357  osm_gps_map_button_release (GtkWidget *widget, GdkEventButton *event)      char *longitude = osd_longitude_str(priv->nav.lon);
1358  {  
1359      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);      int y = OSD_COORDINATES_OFFSET;
1360        y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1361        y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1362        y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1363    
1364        g_free(latitude);
1365        g_free(longitude);
1366    
1367  #ifdef ENABLE_BALLOON      /* draw the compass */
1368      /* released inside the balloon? */      int radius = (OSD_NAV_H - y - 5*OSD_COORDINATES_FONT_SIZE/4)/2;
1369      if (osm_gps_map_in_balloon(priv,      if(radius > OSD_NAV_W/2)
1370                     event->x + EXTRA_BORDER,          radius = OSD_NAV_W/2;
                    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  
1371    
1372      if (priv->dragging)      int x = OSD_NAV_W/2+1;
1373      {      y += radius;
         priv->dragging = FALSE;  
1374    
1375          priv->map_x = priv->drag_start_map_x;      cairo_stroke (cr);
         priv->map_y = priv->drag_start_map_y;  
1376    
1377          priv->map_x += (priv->drag_start_mouse_x - (int) event->x);      /* draw background */
1378          priv->map_y += (priv->drag_start_mouse_y - (int) event->y);      cairo_arc(cr, x, y, radius, 0,  2*M_PI);
1379        cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
1380        cairo_fill_preserve (cr);
1381        cairo_set_source_rgb (cr, 0, 0, 0);
1382        cairo_set_line_width (cr, 1);
1383        cairo_stroke (cr);
1384    
1385          priv->center_coord_set = FALSE;      /* draw pointer */
1386    #define ARROW_WIDTH     0.3
1387    #define ARROW_LENGTH    0.7
1388    
1389        coord_t *gps = osm_gps_map_get_gps (OSM_GPS_MAP(osd->widget));
1390        if(gps) {
1391            float arot = get_bearing(gps->rlat, gps->rlon,
1392                         deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1393    
1394            cairo_move_to(cr,
1395                          x + radius *  ARROW_LENGTH *  sin(arot),
1396                          y + radius *  ARROW_LENGTH * -cos(arot));
1397    
1398            cairo_line_to(cr,
1399                          x + radius * -ARROW_LENGTH *  sin(arot+ARROW_WIDTH),
1400                          y + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH));
1401    
1402            cairo_line_to(cr,
1403                          x + radius * -0.5 * ARROW_LENGTH *  sin(arot),
1404                          y + radius * -0.5 * ARROW_LENGTH * -cos(arot));
1405    
1406            cairo_line_to(cr,
1407                          x + radius * -ARROW_LENGTH *  sin(arot-ARROW_WIDTH),
1408                          y + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH));
1409    
1410            cairo_close_path(cr);
1411            cairo_set_source_rgb (cr, 0, 0, 0);
1412            cairo_fill (cr);
1413    
1414            y += radius + OSD_COORDINATES_FONT_SIZE/4;
1415    
1416            float dist = get_distance(gps->rlat, gps->rlon,
1417                            deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1418    
1419            char *dist_str = NULL;
1420            if(!priv->nav.imperial) {
1421                /* metric is easy ... */
1422                if(dist<1000)
1423                    dist_str = g_strdup_printf("%u m", (int)dist);
1424                else
1425                    dist_str = g_strdup_printf("%.1f km", dist/1000);
1426            } else {
1427                /* and now the hard part: scale for useful imperial values :-( */
1428                /* try to convert to feet, 1ft == 0.3048 m */
1429    
1430          osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));              if(dist/(3*0.3048) >= 1760.0)      /* more than 1760 yard? */
1431                    dist_str = g_strdup_printf("%.1f mi", dist/(0.3048*3*1760.0));
1432                else if(dist/0.3048 >= 100)        /* more than 100 feet? */
1433                    dist_str = g_strdup_printf("%.1f yd", dist/(0.3048*3));
1434                else
1435                    dist_str = g_strdup_printf("%.0f ft", dist/0.3048);
1436            }
1437    
1438            y = osd_render_centered_text(cr, y, OSD_NAV_W, dist_str);
1439            g_free(dist_str);
1440      }      }
1441    
1442      priv->drag_mouse_dx = 0;      cairo_destroy(cr);
1443      priv->drag_mouse_dy = 0;      FOUT;
     priv->drag_counter = -1;  
   
     return FALSE;  
1444  }  }
1445    
1446  static gboolean  void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1447  osm_gps_map_expose (GtkWidget *widget, GdkEventExpose  *event);      FIN;
1448        g_return_if_fail (OSM_IS_GPS_MAP (map));
1449    
1450  static gboolean      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1451  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);  
1452    
1453      if (event->is_hint)      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1454          gdk_window_get_pointer (event->window, &x, &y, &state);      g_return_if_fail (priv);
1455      else  
1456      {      if(priv->nav.surface) {
1457          x = event->x;          cairo_surface_destroy(priv->nav.surface);
1458          y = event->y;          priv->nav.surface = NULL;
1459          state = event->state;          priv->nav.lat = OSM_GPS_MAP_INVALID;
1460            priv->nav.lon = OSM_GPS_MAP_INVALID;
1461            if(priv->nav.name) g_free(priv->nav.name);
1462      }      }
1463        osm_gps_map_redraw(map);
1464        FOUT;
1465    }
1466    
1467      // are we being dragged  void
1468      if (!(state & GDK_BUTTON1_MASK))  osm_gps_map_osd_draw_nav (OsmGpsMap *map, gboolean imperial,
1469          return FALSE;                            float latitude, float longitude, char *name) {
1470        FIN;
1471        g_return_if_fail (OSM_IS_GPS_MAP (map));
1472    
1473      if (priv->drag_counter < 0)      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1474          return FALSE;      g_return_if_fail (osd);
1475    
1476      priv->drag_counter++;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1477        g_return_if_fail (priv);
1478    
1479      // we havent dragged more than 6 pixels      osm_gps_map_osd_clear_nav (map);
     if (priv->drag_counter < 6)  
         return FALSE;  
1480    
1481  #ifdef USE_MAEMO      /* allocate balloon surface */
1482      /* reduce update frequency on maemo to keep screen update fluid */      priv->nav.surface =
1483      static guint32 last_time = 0;          cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1484                                       OSD_NAV_W+2, OSD_NAV_H+2);
1485    
1486      if(event->time - last_time < 100) return FALSE;      priv->nav.lat = latitude;
1487      last_time = event->time;      priv->nav.lon = longitude;
1488  #endif      priv->nav.name = g_strdup(name);
1489        priv->nav.imperial = imperial;
1490    
1491      priv->dragging = TRUE;      osd_render_nav(osd);
1492    
1493      if (priv->map_auto_center)      osm_gps_map_redraw(map);
1494          g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);      FOUT;
1495    }
1496    
1497    #endif // OSD_NAV
1498    
     priv->drag_mouse_dx = x - priv->drag_start_mouse_x;  
     priv->drag_mouse_dy = y - priv->drag_start_mouse_y;  
1499    
1500  #ifdef ENABLE_OSD  #ifdef OSD_CROSSHAIR
     /* undo OSD */  
     osm_gps_map_osd_restore (OSM_GPS_MAP(widget));  
1501    
1502      /* draw new OSD */  #ifndef OSD_CROSSHAIR_RADIUS
1503      osm_gps_map_osd_draw_controls (OSM_GPS_MAP(widget),  #define OSD_CROSSHAIR_RADIUS 10
                                    -priv->drag_mouse_dx,  
                                    -priv->drag_mouse_dy);  
1504  #endif  #endif
1505    
1506      osm_gps_map_expose (widget, NULL);  #define OSD_CROSSHAIR_TICK  (OSD_CROSSHAIR_RADIUS/2)
1507    #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
1508    #define OSD_CROSSHAIR_W  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1509    #define OSD_CROSSHAIR_H  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1510    
1511    static void
1512    osd_render_crosshair_shape(cairo_t *cr) {
1513        FIN;
1514        cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
1515                   OSD_CROSSHAIR_RADIUS, 0,  2*M_PI);
1516    
1517        cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
1518                       OSD_CROSSHAIR_H/2);
1519        cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
1520        cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
1521                       OSD_CROSSHAIR_H/2);
1522        cairo_rel_line_to (cr,  OSD_CROSSHAIR_TICK, 0);
1523    
1524        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1525                       OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
1526        cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
1527        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1528                       OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
1529        cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
1530    
1531      return FALSE;      cairo_stroke (cr);
1532        FOUT;
1533  }  }
1534    
1535  static gboolean  static void
1536  osm_gps_map_configure (GtkWidget *widget, GdkEventConfigure *event)  osd_render_crosshair(osm_gps_map_osd_t *osd)
1537  {  {
1538      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);      FIN;
1539        osd_priv_t *priv = (osd_priv_t*)osd->priv;
     /* create pixmap */  
     if (priv->pixmap)  
         g_object_unref (priv->pixmap);  
1540    
1541      priv->pixmap = gdk_pixmap_new (      if(priv->crosshair.rendered)
1542                                     widget->window,          return;
                                    widget->allocation.width + EXTRA_BORDER * 2,  
                                    widget->allocation.height + EXTRA_BORDER * 2,  
                                    -1);  
1543    
1544      /* and gc, used for clipping (I think......) */      priv->crosshair.rendered = TRUE;
     if(priv->gc_map)  
         g_object_unref(priv->gc_map);  
1545    
1546      priv->gc_map = gdk_gc_new(priv->pixmap);      /* first fill with transparency */
1547        cairo_t *cr = cairo_create(priv->crosshair.surface);
1548        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1549        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1550        cairo_paint(cr);
1551        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1552    
1553      osm_gps_map_map_redraw(OSM_GPS_MAP(widget));      cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1554    
1555        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
1556        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
1557        osd_render_crosshair_shape(cr);
1558    
1559        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1560        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
1561        osd_render_crosshair_shape(cr);
1562    
1563      return FALSE;      cairo_destroy(cr);
1564        FOUT;
1565  }  }
1566    #endif
1567    
1568  static gboolean  #ifdef OSD_SCALE
 osm_gps_map_expose (GtkWidget *widget, GdkEventExpose  *event)  
 {  
     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
1569    
1570      gdk_draw_drawable (  #ifndef OSD_SCALE_FONT_SIZE
1571                         widget->window,  #define OSD_SCALE_FONT_SIZE (12.0)
                        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);  
1572  #endif  #endif
1573      return FALSE;  #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
1574  }  #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
1575    
1576    /* various parameters used to create the scale */
1577    #define OSD_SCALE_H2   (OSD_SCALE_H/2)
1578    #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
1579    #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
1580    #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
1581    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
1582    
1583  static void  static void
1584  osm_gps_map_class_init (OsmGpsMapClass *klass)  osd_render_scale(osm_gps_map_osd_t *osd)
1585  {  {
1586      GObjectClass* object_class = G_OBJECT_CLASS (klass);      FIN;
1587      GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1588    
1589      g_type_class_add_private (klass, sizeof (OsmGpsMapPrivate));      /* this only needs to be rendered if the zoom has changed */
1590        gint zoom;
1591        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
1592        if(zoom == priv->scale.zoom)
1593            return;
1594    
1595      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));  
 }  
1596    
1597  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;  
 }  
1598    
1599  const char *      /* first fill with transparency */
1600  osm_gps_map_source_get_image_format(OsmGpsMapSource_t source)      cairo_t *cr = cairo_create(priv->scale.surface);
1601  {      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1602      switch(source) {      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1603          case OSM_GPS_MAP_SOURCE_NULL:      // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1604          case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:      cairo_paint(cr);
1605          case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1606              return "png";  
1607          case OSM_GPS_MAP_SOURCE_OPENAERIALMAP:      /* determine the size of the scale width in meters */
1608          case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:      float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
1609          case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:  
1610          case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:      /* scale this to useful values */
1611          case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:      int exp = logf(width)*M_LOG10E;
1612          case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:      int mant = width/pow(10,exp);
1613          case OSM_GPS_MAP_SOURCE_YAHOO_STREET:      int width_metric = mant * pow(10,exp);
1614          case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:      char *dist_str = NULL;
1615          case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:      if(width_metric<1000)
1616          case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:          dist_str = g_strdup_printf("%u m", width_metric);
1617          case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:      else
1618              return "jpg";          dist_str = g_strdup_printf("%u km", width_metric/1000);
1619          default:      width_metric /= m_per_pix;
1620              return "bin";  
1621        /* and now the hard part: scale for useful imperial values :-( */
1622        /* try to convert to feet, 1ft == 0.3048 m */
1623        width /= 0.3048;
1624        float imp_scale = 0.3048;
1625        char *dist_imp_unit = "ft";
1626    
1627        if(width >= 100) {
1628            /* 1yd == 3 feet */
1629            width /= 3.0;
1630            imp_scale *= 3.0;
1631            dist_imp_unit = "yd";
1632    
1633            if(width >= 1760.0) {
1634                /* 1mi == 1760 yd */
1635                width /= 1760.0;
1636                imp_scale *= 1760.0;
1637                dist_imp_unit = "mi";
1638            }
1639      }      }
     return "bin";  
 }  
1640    
1641        /* also convert this to full tens/hundreds */
1642        exp = logf(width)*M_LOG10E;
1643        mant = width/pow(10,exp);
1644        int width_imp = mant * pow(10,exp);
1645        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
1646    
1647        /* convert back to pixels */
1648        width_imp *= imp_scale;
1649        width_imp /= m_per_pix;
1650    
1651        cairo_select_font_face (cr, "Sans",
1652                                CAIRO_FONT_SLANT_NORMAL,
1653                                CAIRO_FONT_WEIGHT_BOLD);
1654        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1655        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1656    
1657        cairo_text_extents_t extents;
1658        cairo_text_extents (cr, dist_str, &extents);
1659    
1660        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1661        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1662        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1663        cairo_text_path (cr, dist_str);
1664        cairo_stroke (cr);
1665        cairo_move_to (cr, 2*OSD_SCALE_FD,
1666                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1667        cairo_text_path (cr, dist_str_imp);
1668        cairo_stroke (cr);
1669    
1670  int      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1671  osm_gps_map_source_get_min_zoom(OsmGpsMapSource_t source)      cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1672  {      cairo_show_text (cr, dist_str);
1673      return 1;      cairo_move_to (cr, 2*OSD_SCALE_FD,
1674  }                     OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1675        cairo_show_text (cr, dist_str_imp);
1676    
1677        g_free(dist_str);
1678        g_free(dist_str_imp);
1679    
1680        /* draw white line */
1681        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1682        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1683        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
1684        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1685        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1686        cairo_rel_line_to (cr, width_metric, 0);
1687        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1688        cairo_stroke(cr);
1689        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1690        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1691        cairo_rel_line_to (cr, width_imp, 0);
1692        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1693        cairo_stroke(cr);
1694    
1695        /* draw black line */
1696        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1697        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1698        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1699        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1700        cairo_rel_line_to (cr, width_metric, 0);
1701        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1702        cairo_stroke(cr);
1703        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1704        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1705        cairo_rel_line_to (cr, width_imp, 0);
1706        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1707        cairo_stroke(cr);
1708    
1709  int      cairo_destroy(cr);
1710  osm_gps_map_source_get_max_zoom(OsmGpsMapSource_t source)      FOUT;
 {  
     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;  
1711  }  }
1712    #endif
1713    
1714  void  static void
1715  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)
1716  {  {
1717      int i,j,zoom,num_tiles;      FIN;
1718      OsmGpsMapPrivate *priv = map->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1719    
1720      if (pt1 && pt2)      if(priv->controls.rendered
1721      {  #ifdef OSD_GPS_BUTTON
1722          gchar *filename;         && (priv->controls.gps_enabled == (osd->cb != NULL))
1723          num_tiles = 0;  #endif
1724          zoom_end = CLAMP(zoom_end, priv->min_zoom, priv->max_zoom);         )
1725          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);  
         }  
     }  
 }  
1726    
1727  void  #ifdef OSD_GPS_BUTTON
1728  osm_gps_map_get_bbox (OsmGpsMap *map, coord_t *pt1, coord_t *pt2)      priv->controls.gps_enabled = (osd->cb != NULL);
1729  {  #endif
1730      OsmGpsMapPrivate *priv = map->priv;      priv->controls.rendered = TRUE;
1731    
1732      if (pt1 && pt2) {  #ifndef OSD_COLOR
1733          pt1->rlat = pixel2lat(priv->map_zoom, priv->map_y);      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1734          pt1->rlon = pixel2lon(priv->map_zoom, priv->map_x);      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
1735          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];
1736          pt2->rlon = pixel2lon(priv->map_zoom, priv->map_x + GTK_WIDGET(map)->allocation.width);  #endif
1737    
1738          g_debug("BBOX: %f %f %f %f", pt1->rlat, pt1->rlon, pt2->rlat, pt2->rlon);      /* first fill with transparency */
1739      }      cairo_t *cr = cairo_create(priv->controls.surface);
1740  }      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1741        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1742        cairo_paint(cr);
1743        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1744    
1745  void      /* --------- draw zoom and dpad shape shadow ----------- */
1746  osm_gps_map_set_mapcenter (OsmGpsMap *map, float latitude, float longitude, int zoom)  #ifdef OSD_SHADOW_ENABLE
1747  {      osd_zoom_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1748      osm_gps_map_set_center (map, latitude, longitude);      osd_shape_shadow(cr);
1749      osm_gps_map_set_zoom (map, zoom);  #ifndef OSD_NO_DPAD
1750  }      osd_dpad_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1751        osd_shape_shadow(cr);
1752    #endif
1753    #endif
1754    
1755  void      /* --------- draw zoom and dpad shape ----------- */
 osm_gps_map_set_center (OsmGpsMap *map, float latitude, float longitude)  
 {  
     int pixel_x, pixel_y;  
     OsmGpsMapPrivate *priv;  
1756    
1757      g_return_if_fail (OSM_IS_GPS_MAP (map));      osd_zoom_shape(cr, 1, 1);
1758      priv = map->priv;  #ifndef OSD_COLOR
1759        osd_shape(cr, &bg, &fg);
1760    #else
1761        osd_shape(cr);
1762    #endif
1763    #ifndef OSD_NO_DPAD
1764        osd_dpad_shape(cr, 1, 1);
1765    #ifndef OSD_COLOR
1766        osd_shape(cr, &bg, &fg);
1767    #else
1768        osd_shape(cr);
1769    #endif
1770    #endif
1771    
1772      priv->center_rlat = deg2rad(latitude);      /* --------- draw zoom and dpad labels --------- */
     priv->center_rlon = deg2rad(longitude);  
     priv->center_coord_set = TRUE;  
1773    
1774      // pixel_x,y, offsets  #ifdef OSD_SHADOW_ENABLE
1775      pixel_x = lon2pixel(priv->map_zoom, priv->center_rlon);      osd_labels_shadow(cr, Z_RAD/3, TRUE);
1776      pixel_y = lat2pixel(priv->map_zoom, priv->center_rlat);      osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1777    #ifndef OSD_NO_DPAD
1778        osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1779    #endif
1780        cairo_stroke(cr);
1781    #ifdef OSD_GPS_BUTTON
1782        osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
1783        osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1784        cairo_stroke(cr);
1785    #endif
1786    #endif
1787    
1788      priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;  #ifndef OSD_COLOR
1789      priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;      osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
1790    #else
1791        osd_labels(cr, Z_RAD/3, TRUE);
1792    #endif
1793        osd_zoom_labels(cr, 1, 1);
1794    #ifndef OSD_NO_DPAD
1795        osd_dpad_labels(cr, 1, 1);
1796    #endif
1797        cairo_stroke(cr);
1798    
1799      osm_gps_map_map_redraw_idle(map);  #ifndef OSD_COLOR
1800        osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
1801    #else
1802        osd_labels(cr, Z_RAD/6, osd->cb != NULL);
1803    #endif
1804    #ifdef OSD_GPS_BUTTON
1805        osd_dpad_gps(cr, 1, 1);
1806    #endif
1807        cairo_stroke(cr);
1808    
1809        cairo_destroy(cr);
1810  }  }
1811    
1812  int  static void
1813  osm_gps_map_set_zoom (OsmGpsMap *map, int zoom)  osd_render(osm_gps_map_osd_t *osd)
1814  {  {
1815      int zoom_old;      /* this function is actually called pretty often since the */
1816      double factor = 0.0;      /* OSD contents may have changed (due to a coordinate/zoom change). */
1817      int width_center, height_center;      /* The different OSD parts have to make sure that they don't */
1818      OsmGpsMapPrivate *priv;      /* render unneccessarily often and thus waste CPU power */
   
     g_return_val_if_fail (OSM_IS_GPS_MAP (map), 0);  
     priv = map->priv;  
1819    
1820      if (zoom != priv->map_zoom)      osd_render_controls(osd);
     {  
         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;  
         }  
1821    
1822          g_debug("Zoom changed from %d to %d factor:%f x:%d",  #ifdef OSD_SOURCE_SEL
1823                  zoom_old, priv->map_zoom, factor, priv->map_x);      osd_render_source_sel(osd, FALSE);
1824    #endif
1825    
1826          osm_gps_map_map_redraw_idle(map);  #ifdef OSD_SCALE
1827      }      osd_render_scale(osd);
1828      return priv->map_zoom;  #endif
 }  
1829    
1830  void  #ifdef OSD_CROSSHAIR
1831  osm_gps_map_add_track (OsmGpsMap *map, GSList *track)      osd_render_crosshair(osd);
1832  {  #endif
     OsmGpsMapPrivate *priv;  
1833    
1834      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_NAV
1835      priv = map->priv;      osd_render_nav(osd);
1836    #endif
1837    
1838      if (track) {  #ifdef OSD_COORDINATES
1839          priv->tracks = g_slist_append(priv->tracks, track);      osd_render_coordinates(osd);
1840          osm_gps_map_map_redraw_idle(map);  #endif
     }  
1841  }  }
1842    
1843  void  static void
1844  osm_gps_map_clear_tracks (OsmGpsMap *map)  osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
1845  {  {
1846      g_return_if_fail (OSM_IS_GPS_MAP (map));      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1847    
1848      osm_gps_map_free_tracks(map);      /* OSD itself uses some off-screen rendering, so check if the */
1849      osm_gps_map_map_redraw_idle(map);      /* offscreen buffer is present and create it if not */
1850  }      if(!priv->controls.surface) {
1851            /* create overlay ... */
1852            priv->controls.surface =
1853                cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
1854    
1855  void          priv->controls.rendered = FALSE;
1856  osm_gps_map_add_image (OsmGpsMap *map, float latitude, float longitude, GdkPixbuf *image)  #ifdef OSD_GPS_BUTTON
1857  {          priv->controls.gps_enabled = FALSE;
1858      g_return_if_fail (OSM_IS_GPS_MAP (map));  #endif
1859    
1860      if (image) {  #ifdef OSD_SOURCE_SEL
1861          OsmGpsMapPrivate *priv = map->priv;          /* the initial OSD state is alway not-expanded */
1862          image_t *im;          priv->source_sel.surface =
1863                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1864                                               OSD_S_W+2, OSD_S_H+2);
1865            priv->source_sel.rendered = FALSE;
1866    #endif
1867    
1868          //cache w/h for speed, and add image to list  #ifdef OSD_SCALE
1869          im = g_new0(image_t,1);          priv->scale.surface =
1870          im->w = gdk_pixbuf_get_width(image);              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1871          im->h = gdk_pixbuf_get_height(image);                                         OSD_SCALE_W, OSD_SCALE_H);
1872          im->pt.rlat = deg2rad(latitude);          priv->scale.zoom = -1;
1873          im->pt.rlon = deg2rad(longitude);  #endif
1874    
1875          g_object_ref(image);  #ifdef OSD_CROSSHAIR
1876          im->image = image;          priv->crosshair.surface =
1877                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1878                                           OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1879            priv->crosshair.rendered = FALSE;
1880    #endif
1881    
1882          priv->images = g_slist_append(priv->images, im);  #ifdef OSD_COORDINATES
1883            priv->coordinates.surface =
1884                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1885                                           OSD_COORDINATES_W, OSD_COORDINATES_H);
1886    
1887          osm_gps_map_map_redraw_idle(map);          priv->coordinates.lat = priv->coordinates.lon = OSM_GPS_MAP_INVALID;
1888      }  #endif
 }  
1889    
1890  gboolean          /* ... and render it */
1891  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;  
                 }  
         }  
1892      }      }
     return FALSE;  
 }  
   
 void  
 osm_gps_map_clear_images (OsmGpsMap *map)  
 {  
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
1893    
1894      osm_gps_map_free_images(map);      // now draw this onto the original context
1895      osm_gps_map_map_redraw_idle(map);      cairo_t *cr = gdk_cairo_create(drawable);
 }  
1896    
1897  void      gint x, y;
 osm_gps_map_osd_speed (OsmGpsMap *map, float speed)  
 {  
     OsmGpsMapPrivate *priv;  
1898    
1899      PangoContext        *context = NULL;  #ifdef OSD_SCALE
1900      PangoLayout     *layout  = NULL;      x =  OSD_X;
1901      PangoFontDescription    *desc    = NULL;      y = -OSD_Y;
1902        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1903      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;  
1904    
1905      g_return_if_fail (OSM_IS_GPS_MAP (map));      cairo_set_source_surface(cr, priv->scale.surface, x, y);
1906      priv = map->priv;      cairo_paint(cr);
1907    #endif
1908    
1909      buffer = g_strdup_printf("%.0f", speed);  #ifdef OSD_CROSSHAIR
1910        x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1911        y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1912    
1913      /* pango initialisation */      cairo_set_source_surface(cr, priv->crosshair.surface, x, y);
1914      context = gtk_widget_get_pango_context (GTK_WIDGET(map));      cairo_paint(cr);
1915      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);  
 }  
1916    
1917  void  #ifdef OSD_NAV
1918  osm_gps_map_draw_gps (OsmGpsMap *map, float latitude, float longitude, float heading)      if(priv->nav.surface) {
1919  {          x =  OSD_X;
1920      int pixel_x, pixel_y;          if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1921      OsmGpsMapPrivate *priv;          y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1922    
1923            cairo_set_source_surface(cr, priv->nav.surface, x, y);
1924            cairo_paint(cr);
1925        }
1926    #endif
1927    
1928      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_COORDINATES
1929      priv = map->priv;      x = -OSD_X;
1930        y = -OSD_Y;
1931        if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1932        if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1933    
1934      priv->gps->rlat = deg2rad(latitude);      cairo_set_source_surface(cr, priv->coordinates.surface, x, y);
1935      priv->gps->rlon = deg2rad(longitude);      cairo_paint(cr);
1936      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;  
     }  
1937    
1938      //Automatically center the map if the track approaches the edge  #ifdef OSD_BALLOON
1939      if(priv->map_auto_center)   {      if(priv->balloon.surface) {
1940          int x = pixel_x - priv->map_x;  
1941          int y = pixel_y - priv->map_y;          /* convert given lat lon into screen coordinates */
1942          int width = GTK_WIDGET(map)->allocation.width;          gint x, y;
1943          int height = GTK_WIDGET(map)->allocation.height;          osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1944          if( x < (width/2 - width/8)     || x > (width/2 + width/8)  ||                                        priv->balloon.lat, priv->balloon.lon,
1945              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;  
         }  
     }  
1946    
1947      // this redraws the map (including the gps track, and adjusts the          /* check if balloon needs to be rerendered */
1948      // map center if it was changed          osd_render_balloon(osd);
     osm_gps_map_map_redraw_idle(map);  
 }  
1949    
1950  void          cairo_set_source_surface(cr, priv->balloon.surface,
1951  osm_gps_map_clear_gps (OsmGpsMap *map)                                   x + priv->balloon.offset_x,
1952  {                                   y + priv->balloon.offset_y);
1953      osm_gps_map_free_trip(map);          cairo_paint(cr);
1954      osm_gps_map_map_redraw_idle(map);      }
1955  }  #endif
1956    
1957  coord_t      x = OSD_X;
1958  osm_gps_map_get_co_ordinates (OsmGpsMap *map, int pixel_x, int pixel_y)      if(x < 0)
1959  {          x += osd->widget->allocation.width - OSD_W;
     coord_t coord;  
     OsmGpsMapPrivate *priv = map->priv;  
1960    
1961      coord.rlat = pixel2lat(priv->map_zoom, priv->map_y + pixel_y);      y = OSD_Y;
1962      coord.rlon = pixel2lon(priv->map_zoom, priv->map_x + pixel_x);      if(y < 0)
1963      return coord;          y += osd->widget->allocation.height - OSD_H;
 }  
1964    
1965  GtkWidget *      cairo_set_source_surface(cr, priv->controls.surface, x, y);
1966  osm_gps_map_new (void)      cairo_paint(cr);
 {  
     return g_object_new (OSM_TYPE_GPS_MAP, NULL);  
 }  
1967    
1968  void  #ifdef OSD_SOURCE_SEL
1969  osm_gps_map_screen_to_geographic (OsmGpsMap *map, gint pixel_x, gint pixel_y,      if(!priv->source_sel.handler_id) {
1970                                    gfloat *latitude, gfloat *longitude)          /* the OSD source selection is not being animated */
1971  {          if(!priv->source_sel.expanded)
1972      OsmGpsMapPrivate *priv;              x = osd->widget->allocation.width - OSD_S_W;
1973            else
1974                x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1975        } else
1976            x = priv->source_sel.shift;
1977    
1978        y = OSD_S_Y;
1979        if(OSD_S_Y < 0) {
1980            if(!priv->source_sel.expanded)
1981                y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1982            else
1983                y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1984        }
1985    
1986      g_return_if_fail (OSM_IS_GPS_MAP (map));      cairo_set_source_surface(cr, priv->source_sel.surface, x, y);
1987      priv = map->priv;      cairo_paint(cr);
1988    #endif
1989    
1990      if (latitude)      cairo_destroy(cr);
1991          *latitude = rad2deg(pixel2lat(priv->map_zoom, priv->map_y + pixel_y));      FOUT;
     if (longitude)  
         *longitude = rad2deg(pixel2lon(priv->map_zoom, priv->map_x + pixel_x));  
1992  }  }
1993    
1994  void  static void
1995  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)  
1996  {  {
1997      OsmGpsMapPrivate *priv;      FIN;
1998        osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1999    
2000      g_return_if_fail (OSM_IS_GPS_MAP (map));      if (priv->controls.surface)
2001      priv = map->priv;           cairo_surface_destroy(priv->controls.surface);
2002    
2003      if (pixel_x)  #ifdef OSD_SOURCE_SEL
2004          *pixel_x = lon2pixel(priv->map_zoom, deg2rad(longitude)) - priv->map_x;      if(priv->source_sel.handler_id)
2005      if (pixel_y)          gtk_timeout_remove(priv->source_sel.handler_id);
         *pixel_y = lat2pixel(priv->map_zoom, deg2rad(latitude)) - priv->map_y;  
 }  
2006    
2007  void      if (priv->source_sel.surface)
2008  osm_gps_map_scroll (OsmGpsMap *map, gint dx, gint dy)           cairo_surface_destroy(priv->source_sel.surface);
2009  {  #endif
     OsmGpsMapPrivate *priv;  
2010    
2011      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_SCALE
2012      priv = map->priv;      if (priv->scale.surface)
2013             cairo_surface_destroy(priv->scale.surface);
2014    #endif
2015    
2016      priv->center_coord_set = FALSE;  #ifdef OSD_CROSSHAIR
2017      priv->map_x += dx;      if (priv->crosshair.surface)
2018      priv->map_y += dy;           cairo_surface_destroy(priv->crosshair.surface);
2019    #endif
2020    
2021      osm_gps_map_map_redraw_idle (map);  #ifdef OSD_NAV
2022  }      if (priv->nav.surface)
2023             cairo_surface_destroy(priv->nav.surface);
2024    #endif
2025    
2026  float  #ifdef OSD_COORDINATES
2027  osm_gps_map_get_scale(OsmGpsMap *map)      if (priv->coordinates.surface)
2028  {           cairo_surface_destroy(priv->coordinates.surface);
2029      OsmGpsMapPrivate *priv;  #endif
2030    
2031      g_return_val_if_fail (OSM_IS_GPS_MAP (map), OSM_NAN);  #ifdef OSD_BALLOON
2032      priv = map->priv;      if (priv->balloon.surface)
2033             cairo_surface_destroy(priv->balloon.surface);
2034    #endif
2035    
2036      return osm_gps_map_get_scale_at_point(priv->map_zoom, priv->center_rlat, priv->center_rlon);      g_free(priv);
2037        FOUT;
2038  }  }
2039    
2040  #ifdef ENABLE_BALLOON  static gboolean
2041  void  osd_busy(osm_gps_map_osd_t *osd)
 osm_gps_map_draw_balloon (OsmGpsMap *map, float latitude, float longitude,  
                           OsmGpsMapBalloonCallback cb, gpointer data)  
2042  {  {
2043      OsmGpsMapPrivate *priv;      FIN;
2044    #ifdef OSD_SOURCE_SEL
2045        osd_priv_t *priv = (osd_priv_t *)(osd->priv);
2046        return (priv->source_sel.handler_id != 0);
2047    #else
2048        return FALSE;
2049    #endif
2050    }
2051    
2052      /* remove and previously installed balloon */  static osd_button_t
2053      osm_gps_map_clear_balloon (map);  osd_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
2054        FIN;
2055        return osd_check_int(osd, TRUE, down, x, y);
2056    }
2057    
2058      g_return_if_fail (OSM_IS_GPS_MAP (map));  static osm_gps_map_osd_t osd_classic = {
2059      priv = map->priv;      .widget     = NULL,
2060    
2061      priv->balloon.coo->rlat = deg2rad(latitude);      .draw       = osd_draw,
2062      priv->balloon.coo->rlon = deg2rad(longitude);      .check      = osd_check,
2063      priv->balloon.valid = TRUE;      .render     = osd_render,
2064        .free       = osd_free,
2065        .busy       = osd_busy,
2066    
2067      priv->balloon.cb = cb;      .cb         = NULL,
2068      priv->balloon.data = data;      .data       = NULL,
2069    
2070      // this redraws the map      .priv       = NULL
2071      osm_gps_map_map_redraw_idle(map);  };
 }  
2072    
2073  void  /* this is the only function that's externally visible */
2074  osm_gps_map_clear_balloon (OsmGpsMap *map)  void
2075    osm_gps_map_osd_classic_init(OsmGpsMap *map)
2076  {  {
2077      OsmGpsMapPrivate *priv;      FIN;
2078        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;  
2079    
2080      priv->balloon.valid = FALSE;  #ifdef OSD_BALLOON
2081        priv->balloon.lat = OSM_GPS_MAP_INVALID;
2082      osm_gps_map_map_redraw_idle(map);      priv->balloon.lon = OSM_GPS_MAP_INVALID;
 }  
2083  #endif  #endif
2084    
2085  #ifdef ENABLE_OSD      osd_classic.priv = priv;
 void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdGpsCallback cb, gpointer data) {  
     OsmGpsMapPrivate *priv;  
2086    
2087      g_return_if_fail (OSM_IS_GPS_MAP (map));      osm_gps_map_register_osd(map, &osd_classic);
2088      priv = map->priv;      FOUT;
2089    }
2090    
2091      priv->osd.cb = cb;  #ifdef OSD_GPS_BUTTON
2092      priv->osd.data = data;  /* below are osd specific functions which aren't used by osm-gps-map */
2093    /* but instead are to be used by the main application */
2094    void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
2095                                     gpointer data) {
2096        FIN;
2097        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
2098        g_return_if_fail (osd);
2099    
2100        osd->cb = cb;
2101        osd->data = data;
2102    
2103      /* this may have changed the state of the gps button */      /* this may have changed the state of the gps button */
2104      /* we thus re-render the overlay */      /* we thus re-render the overlay */
2105      osm_gps_map_osd_render(priv);      osd->render(osd);
2106    
2107      osm_gps_map_map_redraw_idle(map);      osm_gps_map_redraw(map);
2108        FOUT;
2109  }  }
2110  #endif  #endif
2111    
2112    osd_button_t
2113    osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
2114        FIN;
2115        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
2116        g_return_val_if_fail (osd, OSD_NONE);
2117    
2118        return osd_check_int(osd, FALSE, TRUE, x, y);
2119    }

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