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

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