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

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