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

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