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 107 by harbaum, Fri Sep 11 12:16:50 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 "osm-gps-map-osd-classic.h"
36    
37  //the osd controls  //the osd controls
38  typedef struct {  typedef struct {
39      GdkPixmap *backup;      /* the offscreen representation of the OSD */
40      cairo_surface_t *overlay;      cairo_surface_t *overlay;
     gint backup_x, backup_y;  
     OsmGpsMapOsdGpsCallback cb;  
     gpointer data;  
 } osd_priv_t;  
41    
42  typedef struct {  #ifdef OSD_SCALE
43        cairo_surface_t *scale;
44        int scale_zoom;
45    #endif
46    
47    #ifdef OSD_CROSSHAIR
48        cairo_surface_t *crosshair;
49    #endif
50    
51    #ifdef OSD_COORDINATES
52        cairo_surface_t *coordinates;
53    #endif
54    
55    #ifdef OSD_SOURCE_SEL
56        /* values to handle the "source" menu */
57        cairo_surface_t *map_source;
58        gboolean expanded;
59        gint shift, dir, count;
60        gint handler_id;
61        gint width, height;
62    #endif
63    
64      gpointer priv;  } osd_priv_t;
 } osm_gps_map_osd_t;  
65    
66  /* position and extent of bounding box */  /* position and extent of bounding box */
67    #ifndef OSD_X
68  #define OSD_X      (10)  #define OSD_X      (10)
69  #define OSD_Y      (10)  #endif
70    
71  #define OSD_COLOR            0.5, 0.5, 1  #ifndef OSD_Y
72  #define OSD_COLOR_DISABLED   0.8, 0.8, 0.8  #define OSD_Y      (10)
73    #endif
74    
75  /* parameters of the direction shape */  /* parameters of the direction shape */
76  #ifndef OSM_GPS_MAP_OSD_DIAMETER  #ifndef OSD_DIAMETER
77  #define D_RAD  (30)         // diameter of dpad  #define D_RAD  (30)         // diameter of dpad
78  #else  #else
79  #define D_RAD  (OSM_GPS_MAP_OSD_DIAMETER)  #define D_RAD  (OSD_DIAMETER)
80  #endif  #endif
81  #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
82  #define D_LEN  (D_RAD/4)    // length of arrow  #define D_LEN  (D_RAD/4)    // length of arrow
# Line 59  typedef struct { Line 86  typedef struct {
86  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom
87  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar
88    
89    #ifdef OSD_SHADOW_ENABLE
90  /* shadow also depends on control size */  /* shadow also depends on control size */
91  #define OSD_SHADOW (D_RAD/6)  #define OSD_SHADOW (D_RAD/6)
92    #else
93    #define OSD_SHADOW (0)
94    #endif
95    
96    /* normally the GPS button is in the center of the dpad. if there's */
97    /* no dpad it will go into the zoom area */
98    #if defined(OSD_GPS_BUTTON) && defined(OSD_NO_DPAD)
99    #define Z_GPS  1
100    #else
101    #define Z_GPS  0
102    #endif
103    
104  /* total width and height of controls incl. shadow */  /* total width and height of controls incl. shadow */
105  #define OSD_W    (2*D_RAD + OSD_SHADOW)  #define OSD_W    (2*D_RAD + OSD_SHADOW + Z_GPS * 2 * Z_RAD)
106    #if !Z_GPS
107  #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)
108    #else
109    #define OSD_H    (2*Z_RAD + OSD_SHADOW)
110    #endif
111    
112    #ifdef OSD_SHADOW_ENABLE
113  #define OSD_LBL_SHADOW (OSD_SHADOW/2)  #define OSD_LBL_SHADOW (OSD_SHADOW/2)
114    #endif
115    
116    #define Z_TOP    ((1-Z_GPS) * (2 * D_RAD + Z_STEP))
117    
 #define Z_TOP    (2 * D_RAD + Z_STEP)  
118  #define Z_MID    (Z_TOP + Z_RAD)  #define Z_MID    (Z_TOP + Z_RAD)
119  #define Z_BOT    (Z_MID + Z_RAD)  #define Z_BOT    (Z_MID + Z_RAD)
120  #define Z_LEFT   (Z_RAD)  #define Z_LEFT   (Z_RAD)
121  #define Z_RIGHT  (2 * D_RAD - Z_RAD)  #define Z_RIGHT  (2 * D_RAD - Z_RAD + Z_GPS * 2 * Z_RAD)
122    #define Z_CENTER ((Z_RIGHT + Z_LEFT)/2)
123    
124  /* create the cairo shape used for the zoom buttons */  /* create the cairo shape used for the zoom buttons */
125  static void  static void
126  osm_gps_map_osd_zoom_shape(cairo_t *cr, gint x, gint y) {  osd_zoom_shape(cairo_t *cr, gint x, gint y)
127    {
128      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);
129      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);
130      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 132  osm_gps_map_osd_zoom_shape(cairo_t *cr,
132      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);
133  }  }
134    
135    /* ------------------- color/shadow functions ----------------- */
136    
137    #ifndef OSD_COLOR
138    /* if no color has been specified we just use the gdks default colors */
139    static void
140    osd_labels(cairo_t *cr, gint width, gboolean enabled,
141                           GdkColor *fg, GdkColor *disabled) {
142        if(enabled)  gdk_cairo_set_source_color(cr, fg);
143        else         gdk_cairo_set_source_color(cr, disabled);
144        cairo_set_line_width (cr, width);
145    }
146    #else
147    static void
148    osd_labels(cairo_t *cr, gint width, gboolean enabled) {
149        if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);
150        else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
151        cairo_set_line_width (cr, width);
152    }
153    #endif
154    
155    #ifdef OSD_SHADOW_ENABLE
156    static void
157    osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
158        cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
159        cairo_set_line_width (cr, width);
160    }
161    #endif
162    
163    #ifndef OSD_NO_DPAD
164  /* create the cairo shape used for the dpad */  /* create the cairo shape used for the dpad */
165  static void  static void
166  osm_gps_map_osd_dpad_shape(cairo_t *cr, gint x, gint y) {  osd_dpad_shape(cairo_t *cr, gint x, gint y)
167    {
168      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);
169  }  }
170    #endif
171    
172    #ifdef OSD_SHADOW_ENABLE
173    static void
174    osd_shape_shadow(cairo_t *cr) {
175        cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
176        cairo_fill (cr);
177        cairo_stroke (cr);
178    }
179    #endif
180    
181    #ifndef OSD_COLOR
182    /* if no color has been specified we just use the gdks default colors */
183    static void
184    osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
185        gdk_cairo_set_source_color(cr, bg);
186        cairo_fill_preserve (cr);
187        gdk_cairo_set_source_color(cr, fg);
188        cairo_set_line_width (cr, 1);
189        cairo_stroke (cr);
190    }
191    #else
192    static void
193    osd_shape(cairo_t *cr) {
194        cairo_set_source_rgb (cr, OSD_COLOR_BG);
195        cairo_fill_preserve (cr);
196        cairo_set_source_rgb (cr, OSD_COLOR);
197        cairo_set_line_width (cr, 1);
198        cairo_stroke (cr);
199    }
200    #endif
201    
 typedef enum {  
     OSD_NONE = 0,  
     OSD_BG,  
     OSD_UP,  
     OSD_DOWN,  
     OSD_LEFT,  
     OSD_RIGHT,  
     OSD_IN,  
     OSD_OUT,  
     OSD_GPS  
 } osd_button_t;  
202    
203  static gboolean  static gboolean
204  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 206  osm_gps_map_in_circle(gint x, gint y, gi
206      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
207  }  }
208    
209    #ifndef OSD_NO_DPAD
210  /* check whether x/y is within the dpad */  /* check whether x/y is within the dpad */
211  static osd_button_t  static osd_button_t
212  osm_gps_map_osd_check_dpad(gint x, gint y)  osd_check_dpad(gint x, gint y)
213  {  {
214      /* within entire dpad circle */      /* within entire dpad circle */
215      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))
216      {      {
217          /* convert into position relative to dpads centre */          /* convert into position relative to dpads centre */
218          x -= (OSD_X + D_RAD);          x -= D_RAD;
219          y -= (OSD_Y + D_RAD);          y -= D_RAD;
220    
221    #ifdef OSD_GPS_BUTTON
222          /* check for dpad center goes here! */          /* check for dpad center goes here! */
223          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))
224              return OSD_GPS;              return OSD_GPS;
225    #endif
226    
227          if( y < 0 && abs(x) < abs(y))          if( y < 0 && abs(x) < abs(y))
228              return OSD_UP;              return OSD_UP;
# Line 139  osm_gps_map_osd_check_dpad(gint x, gint Line 240  osm_gps_map_osd_check_dpad(gint x, gint
240      }      }
241      return OSD_NONE;      return OSD_NONE;
242  }  }
243    #endif
244    
245  /* check whether x/y is within the zoom pads */  /* check whether x/y is within the zoom pads */
246  static osd_button_t  static osd_button_t
247  osm_gps_map_osd_check_zoom(gint x, gint y) {  osd_check_zoom(gint x, gint y) {
248      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) {
249    
250          /* within circle around (-) label */          /* within circle around (-) label */
251          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)  
252              return OSD_OUT;              return OSD_OUT;
253    
254          /* within circle around (+) label */          /* within circle around (+) label */
255          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))
256              return OSD_IN;              return OSD_IN;
257    
258    #if Z_GPS == 1
259            /* within square around center */
260            if( x > Z_CENTER - Z_RAD && x < Z_CENTER + Z_RAD)
261                return OSD_GPS;
262    #endif
263    
264            /* between center of (-) button and center of entire zoom control area */
265            if(x > OSD_LEFT && x < D_RAD)
266                return OSD_OUT;
267    
268          /* between center of (+) button and center of entire zoom control area */          /* between center of (+) button and center of entire zoom control area */
269          if(x < OSD_RIGHT && x > OSD_X + D_RAD)          if(x < OSD_RIGHT && x > D_RAD)
270              return OSD_IN;              return OSD_IN;
271      }      }
272    
273      return OSD_NONE;      return OSD_NONE;
274  }  }
275    
276    #ifdef OSD_SOURCE_SEL
277    
278    /* place source selection at right border */
279    #define OSD_S_RAD (Z_RAD)
280    #define OSD_S_X   (-OSD_X)
281    #define OSD_S_Y   (OSD_Y)
282    #define OSD_S_PW  (2 * Z_RAD)
283    #define OSD_S_W   (OSD_S_PW)
284    #define OSD_S_PH  (2 * Z_RAD)
285    #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
286    
287    /* size of usable area when expanded */
288    #define OSD_S_AREA_W (priv->width)
289    #define OSD_S_AREA_H (priv->height)
290    #define OSD_S_EXP_W  (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
291    #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
292    
293    /* internal value to draw the arrow on the "puller" */
294    #define OSD_S_D0  (OSD_S_RAD/2)
295    #ifndef OSD_FONT_SIZE
296    #define OSD_FONT_SIZE 16.0
297    #endif
298    #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)
299    #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)
300    
301    /* draw the shape of the source selection OSD, either only the puller (not expanded) */
302    /* or the entire menu incl. the puller (expanded) */
303    static void
304    osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
305        if(!priv->expanded) {
306            /* just draw the puller */
307            cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
308            cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
309            cairo_line_to (cr, x + OSD_S_PW, y);
310        } else {
311            /* draw the puller and the area itself */
312            cairo_move_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y + OSD_S_AREA_H);
313            cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H);
314            if(OSD_S_Y > 0) {
315                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_PH);
316                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
317            } else {
318                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_AREA_H-OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
319                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H - OSD_S_PH);
320                cairo_line_to (cr, x + OSD_S_PW, y);
321            }
322            cairo_line_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y);
323            cairo_close_path (cr);
324        }
325    }
326    
327    static void
328    osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
329        osd_priv_t *priv = (osd_priv_t*)osd->priv;
330    
331        int py = offset + OSD_S_RAD - OSD_S_D0;
332    
333        if(!priv->expanded) {
334            /* draw the "puller" open (<) arrow */
335            cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
336            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
337            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
338        } else {
339            if(OSD_S_Y < 0)
340                py += OSD_S_AREA_H - OSD_S_PH;
341    
342            /* draw the "puller" close (>) arrow */
343            cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
344            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
345            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
346            cairo_stroke(cr);
347    
348            /* don't draw a shadow for the text content */
349            if(offset == 1) {
350                gint source;
351                g_object_get(osd->widget, "map-source", &source, NULL);
352    
353                cairo_select_font_face (cr, "Sans",
354                                        CAIRO_FONT_SLANT_NORMAL,
355                                        CAIRO_FONT_WEIGHT_BOLD);
356                cairo_set_font_size (cr, OSD_FONT_SIZE);
357    
358                int i, step = (priv->height - 2*OSD_TEXT_BORDER) /
359                    OSM_GPS_MAP_SOURCE_LAST;
360                for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
361                    cairo_text_extents_t extents;
362                    const char *src = osm_gps_map_source_get_friendly_name(i);
363                    cairo_text_extents (cr, src, &extents);
364    
365                    int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
366                    int y = offset + step * (i-1) + OSD_TEXT_BORDER;
367    
368                    /* draw filled rectangle if selected */
369                    if(source == i) {
370                        cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
371                                        y - OSD_TEXT_SKIP,
372                                        priv->width - OSD_TEXT_BORDER,
373                                        step + OSD_TEXT_SKIP);
374                        cairo_fill(cr);
375    
376                        /* temprarily draw with background color */
377    #ifndef OSD_COLOR
378                        GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
379                        gdk_cairo_set_source_color(cr, &bg);
380    #else
381                        cairo_set_source_rgb (cr, OSD_COLOR_BG);
382    #endif
383                    }
384    
385                    cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
386                    cairo_show_text (cr, src);
387    
388                    /* restore color */
389                    if(source == i) {
390    #ifndef OSD_COLOR
391                        GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
392                        gdk_cairo_set_source_color(cr, &fg);
393    #else
394                        cairo_set_source_rgb (cr, OSD_COLOR);
395    #endif
396                    }
397                }
398            }
399        }
400    }
401    
402    static void
403    osd_render_source_sel(osm_gps_map_osd_t *osd) {
404        osd_priv_t *priv = (osd_priv_t*)osd->priv;
405    
406    #ifndef OSD_COLOR
407        GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
408        GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
409        GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
410    #endif
411    
412        /* draw source selector */
413        cairo_t *cr = cairo_create(priv->map_source);
414        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
415        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
416        cairo_paint(cr);
417        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
418    
419    #ifdef OSD_SHADOW_ENABLE
420        osd_source_shape(priv, cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
421        osd_shape_shadow(cr);
422    #endif
423    
424        osd_source_shape(priv, cr, 1, 1);
425    #ifndef OSD_COLOR
426        osd_shape(cr, &bg, &fg);
427    #else
428        osd_shape(cr);
429    #endif
430    
431    #ifdef OSD_SHADOW_ENABLE
432        osd_labels_shadow(cr, Z_RAD/3, TRUE);
433        osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
434        cairo_stroke (cr);
435    #endif
436    #ifndef OSD_COLOR
437        osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
438    #else
439        osd_labels(cr, Z_RAD/3, TRUE);
440    #endif
441        osd_source_content(osd, cr, 1);
442        cairo_stroke (cr);
443    
444        cairo_destroy(cr);
445    }
446    
447    /* re-allocate the buffer used to draw the menu. This is used */
448    /* to collapse/expand the buffer */
449    static void
450    osd_source_reallocate(osm_gps_map_osd_t *osd) {
451        osd_priv_t *priv = (osd_priv_t*)osd->priv;
452    
453        /* re-allocate offscreen bitmap */
454        g_assert (priv->map_source);
455    
456        int w = OSD_S_W, h = OSD_S_H;
457        if(priv->expanded) {
458            cairo_text_extents_t extents;
459    
460            /* determine content size */
461            cairo_t *cr = cairo_create(priv->map_source);
462            cairo_select_font_face (cr, "Sans",
463                                    CAIRO_FONT_SLANT_NORMAL,
464                                    CAIRO_FONT_WEIGHT_BOLD);
465            cairo_set_font_size (cr, OSD_FONT_SIZE);
466    
467            /* calculate menu size */
468            int i, max_h = 0, max_w = 0;
469            for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
470                const char *src = osm_gps_map_source_get_friendly_name(i);
471                cairo_text_extents (cr, src, &extents);
472    
473                if(extents.width > max_w) max_w = extents.width;
474                if(extents.height > max_h) max_h = extents.height;
475            }
476            cairo_destroy(cr);
477    
478            priv->width  = max_w + 2*OSD_TEXT_BORDER;
479            priv->height = OSM_GPS_MAP_SOURCE_LAST *
480                (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
481    
482            w = OSD_S_EXP_W;
483            h = OSD_S_EXP_H;
484        }
485    
486        cairo_surface_destroy(priv->map_source);
487        priv->map_source =
488            cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
489    
490        osd_render_source_sel(osd);
491    }
492    
493    #define OSD_HZ      15
494    #define OSD_TIME    500
495    
496    static gboolean osd_source_animate(gpointer data) {
497        osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
498        osd_priv_t *priv = (osd_priv_t*)osd->priv;
499        int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
500        gboolean done = FALSE;
501        priv->count += priv->dir;
502    
503        /* shifting in */
504        if(priv->dir < 0) {
505            if(priv->count <= 0) {
506                priv->count = 0;
507                done = TRUE;
508            }
509        } else {
510            if(priv->count >= 1000) {
511                priv->expanded = FALSE;
512                osd_source_reallocate(osd);
513    
514                priv->count = 1000;
515                done = TRUE;
516            }
517        }
518    
519    
520        /* count runs linearly from 0 to 1000, map this nicely onto a position */
521    
522        /* nicer sinoid mapping */
523        float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;
524        priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
525            m * diff;
526    
527        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
528    
529        if(done)
530            priv->handler_id = 0;
531    
532        return !done;
533    }
534    
535    /* switch between expand and collapse mode of source selection */
536    static void
537    osd_source_toggle(osm_gps_map_osd_t *osd)
538    {
539        osd_priv_t *priv = (osd_priv_t*)osd->priv;
540    
541        /* ignore clicks while animation is running */
542        if(priv->handler_id)
543            return;
544    
545        /* expand immediately, collapse is handle at the end of the collapse animation */
546        if(!priv->expanded) {
547            priv->expanded = TRUE;
548            osd_source_reallocate(osd);
549    
550            priv->count = 1000;
551            priv->shift = osd->widget->allocation.width - OSD_S_W;
552            priv->dir = -1000/OSD_HZ;
553        } else {
554            priv->count =  0;
555            priv->shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
556            priv->dir = +1000/OSD_HZ;
557        }
558    
559        priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);
560    }
561    
562    /* check if the user clicked inside the source selection area */
563    static osd_button_t
564    osd_source_check(osm_gps_map_osd_t *osd, gint x, gint y) {
565        osd_priv_t *priv = (osd_priv_t*)osd->priv;
566    
567        if(!priv->expanded)
568            x -= osd->widget->allocation.width - OSD_S_W;
569        else
570            x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
571    
572        if(OSD_S_Y > 0)
573            y -= OSD_S_Y;
574        else
575            y -= osd->widget->allocation.height - OSD_S_PH + OSD_S_Y;
576    
577        /* within square around puller? */
578        if(y > 0 && y < OSD_S_PH && x > 0 && x < OSD_S_PW) {
579            /* really within puller shape? */
580            if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
581                /* expand source selector */
582                osd_source_toggle(osd);
583    
584                /* tell upper layers that user clicked some background element */
585                /* of the OSD */
586                return OSD_BG;
587            }
588        }
589    
590        /* check for clicks into data area */
591        if(priv->expanded && !priv->handler_id) {
592            /* re-adjust from puller top to content top */
593            if(OSD_S_Y < 0)
594                y += OSD_S_EXP_H - OSD_S_PH;
595    
596            if(x > OSD_S_PW &&
597               x < OSD_S_PW + OSD_S_EXP_W &&
598               y > 0 &&
599               y < OSD_S_EXP_H) {
600    
601                int step = (priv->height - 2*OSD_TEXT_BORDER)
602                    / OSM_GPS_MAP_SOURCE_LAST;
603    
604                y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
605                y /= step;
606                y += 1;
607    
608                gint old = 0;
609                g_object_get(osd->widget, "map-source", &old, NULL);
610    
611                if(y > OSM_GPS_MAP_SOURCE_NULL &&
612                   y <= OSM_GPS_MAP_SOURCE_LAST &&
613                   old != y) {
614                    g_object_set(osd->widget, "map-source", y, NULL);
615    
616                    osd_render_source_sel(osd);
617                    osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
618                }
619    
620                /* return "clicked in OSD background" to prevent further */
621                /* processing by application */
622                return OSD_BG;
623            }
624        }
625    
626        return OSD_NONE;
627    }
628    #endif // OSD_SOURCE_SEL
629    
630  static osd_button_t  static osd_button_t
631  osm_gps_map_osd_check(gint x, gint y) {  osd_check(osm_gps_map_osd_t *osd, gint x, gint y) {
632      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
633    
634    #ifdef OSD_SOURCE_SEL
635        /* the source selection area is handles internally */
636        but = osd_source_check(osd, x, y);
637        if(but != OSD_NONE)
638            return but;
639    #endif
640    
641        x -= OSD_X;
642        y -= OSD_Y;
643    
644        if(OSD_X < 0)
645            x -= (osd->widget->allocation.width - OSD_W);
646    
647        if(OSD_Y < 0)
648            y -= (osd->widget->allocation.height - OSD_H);
649    
650      /* first do a rough test for the OSD area. */      /* first do a rough test for the OSD area. */
651      /* this is just to avoid an unnecessary detailed test */      /* this is just to avoid an unnecessary detailed test */
652      if(x > OSD_X && x < OSD_X + OSD_W &&      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {
653         y > OSD_Y && y < OSD_Y + OSD_H) {  #ifndef OSD_NO_DPAD
654          but = osm_gps_map_osd_check_dpad(x, y);          but = osd_check_dpad(x, y);
655    #endif
656    
657          if(but == OSD_NONE)          if(but == OSD_NONE)
658              but = osm_gps_map_osd_check_zoom(x, y);              but = osd_check_zoom(x, y);
659      }      }
660    
661      return but;      return but;
662  }  }
663    
664  static void  #ifndef OSD_NO_DPAD
 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);  
 }  
   
 static void  
 osm_gps_map_osd_shape(cairo_t *cr) {  
     cairo_set_source_rgb (cr, 1, 1, 1);  
     cairo_fill_preserve (cr);  
     cairo_set_source_rgb (cr, OSD_COLOR);  
     cairo_set_line_width (cr, 1);  
     cairo_stroke (cr);  
 }  
   
665  static void  static void
666  osm_gps_map_osd_dpad_labels(cairo_t *cr, gint x, gint y) {  osd_dpad_labels(cairo_t *cr, gint x, gint y) {
667      /* move reference to dpad center */      /* move reference to dpad center */
668      x += D_RAD;      x += D_RAD;
669      y += D_RAD;      y += D_RAD;
# Line 222  osm_gps_map_osd_dpad_labels(cairo_t *cr, Line 686  osm_gps_map_osd_dpad_labels(cairo_t *cr,
686          cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);          cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);
687      }      }
688  }  }
689    #endif
690    
691  /* draw the sattelite dish icon in the center of the dpad */  #ifdef OSD_GPS_BUTTON
692  #define GPS_V0  (D_RAD/8)  /* draw the satellite dish icon in the center of the dpad */
693    #define GPS_V0  (D_RAD/7)
694  #define GPS_V1  (D_RAD/10)  #define GPS_V1  (D_RAD/10)
695  #define GPS_V2  (D_RAD/5)  #define GPS_V2  (D_RAD/5)
696    
697  /* draw a satellite receiver dish */  /* draw a satellite receiver dish */
698    /* this is either drawn in the center of the dpad (if present) */
699    /* or in the middle of the zoom area */
700  static void  static void
701  osm_gps_map_osd_dpad_gps(cairo_t *cr, gint x, gint y) {  osd_dpad_gps(cairo_t *cr, gint x, gint y) {
702      /* move reference to dpad center */      /* move reference to dpad center */
703      x += D_RAD;      x += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD * 3;
704      y += D_RAD + GPS_V0;      y += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD + GPS_V0;
705    
706      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
707      cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);      cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);
# Line 248  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi Line 716  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi
716      cairo_move_to (cr, x, y-GPS_V2);      cairo_move_to (cr, x, y-GPS_V2);
717      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
718  }  }
719    #endif
720    
721  #define Z_LEN  (2*Z_RAD/3)  #define Z_LEN  (2*Z_RAD/3)
722    
723  static void  static void
724  osm_gps_map_osd_zoom_labels(cairo_t *cr, gint x, gint y) {  osd_zoom_labels(cairo_t *cr, gint x, gint y) {
725      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);
726      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);
727    
# Line 262  osm_gps_map_osd_zoom_labels(cairo_t *cr, Line 731  osm_gps_map_osd_zoom_labels(cairo_t *cr,
731      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
732  }  }
733    
734  static void  #ifdef OSD_COORDINATES
 osm_gps_map_osd_labels(cairo_t *cr, gint width, gboolean enabled) {  
     if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);  
     else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);  
     cairo_set_line_width (cr, width);  
     cairo_stroke (cr);  
 }  
   
 static void  
 osm_gps_map_osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {  
     cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);  
     cairo_set_line_width (cr, width);  
     cairo_stroke (cr);  
 }  
735    
736  static void  #ifndef OSD_COORDINATES_FONT_SIZE
737  osm_gps_map_osd_render(OsmGpsMapPrivate *priv) {  #define OSD_COORDINATES_FONT_SIZE 12
738    #endif
739    
740      /* first fill with transparency */  #define OSD_COORDINATES_W  (9*OSD_COORDINATES_FONT_SIZE)
741      cairo_t *cr = cairo_create(priv->osd.overlay);  #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE)
     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);  
     cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);  
     cairo_paint(cr);  
742    
743      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);  /* these can be overwritten with versions that support */
744    /* localization */
745    #ifndef OSD_COORDINATES_CHR_N
746    #define OSD_COORDINATES_CHR_N  "N"
747    #endif
748    #ifndef OSD_COORDINATES_CHR_S
749    #define OSD_COORDINATES_CHR_S  "S"
750    #endif
751    #ifndef OSD_COORDINATES_CHR_E
752    #define OSD_COORDINATES_CHR_E  "E"
753    #endif
754    #ifndef OSD_COORDINATES_CHR_W
755    #define OSD_COORDINATES_CHR_W  "W"
756    #endif
757    
     /* --------- draw zoom and dpad shape shadow ----------- */  
     gint x = 0, y = 0;  
758    
     osm_gps_map_osd_zoom_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);  
     osm_gps_map_osd_shape_shadow(cr);  
     osm_gps_map_osd_dpad_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);  
     osm_gps_map_osd_shape_shadow(cr);  
759    
760      /* --------- draw zoom and dpad shape ----------- */  /* this is the classic geocaching notation */
761    static char
762    *osd_latitude_str(float latitude) {
763        char *c = OSD_COORDINATES_CHR_N;
764        float integral, fractional;
765    
766        if(isnan(latitude))
767            return NULL;
768    
769        if(latitude < 0) {
770            latitude = fabs(latitude);
771            c = OSD_COORDINATES_CHR_S;
772        }
773    
774      osm_gps_map_osd_zoom_shape(cr, x, y);      fractional = modff(latitude, &integral);
775      osm_gps_map_osd_shape(cr);  
776      osm_gps_map_osd_dpad_shape(cr, x, y);      return g_strdup_printf("%s %02d° %06.3f'",
777      osm_gps_map_osd_shape(cr);                             c, (int)integral, fractional*60.0);
778    }
779    
780      /* --------- draw zoom and dpad labels --------- */  static char
781    *osd_longitude_str(float longitude) {
782        char *c = OSD_COORDINATES_CHR_E;
783        float integral, fractional;
784    
785        if(isnan(longitude))
786            return NULL;
787    
788        if(longitude < 0) {
789            longitude = fabs(longitude);
790            c = OSD_COORDINATES_CHR_W;
791        }
792    
793      osm_gps_map_osd_zoom_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);      fractional = modff(longitude, &integral);
     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);  
794    
795      cairo_destroy(cr);      return g_strdup_printf("%s %03d° %06.3f'",
796                               c, (int)integral, fractional*60.0);
797  }  }
798    
799    #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
800    
801  static void  static void
802  osm_gps_map_osd_draw_controls (OsmGpsMap *map, gint xoffset, gint yoffset)  osd_render_coordinates(osm_gps_map_osd_t *osd)
803  {  {
804      OsmGpsMapPrivate *priv = map->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
805    
806      /* backup previous contents */      /* get current map position */
807      if(!priv->osd.backup)      gfloat lat, lon;
808          priv->osd.backup = gdk_pixmap_new(priv->pixmap, OSD_W+2, OSD_H+2, -1);      g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
809    
810      gint x = OSD_X + EXTRA_BORDER + xoffset;      /* first fill with transparency */
811      gint y = OSD_Y + EXTRA_BORDER + yoffset;      cairo_t *cr = cairo_create(priv->coordinates);
812        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
813        //    cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
814        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
815        cairo_paint(cr);
816        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
817    
818      /* create backup of background */      cairo_select_font_face (cr, "Sans",
819      gdk_draw_drawable(priv->osd.backup,                              CAIRO_FONT_SLANT_NORMAL,
820          GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))],                              CAIRO_FONT_WEIGHT_BOLD);
821                        priv->pixmap, x-1, y-1, 0, 0, OSD_W+2, OSD_H+2);      cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
822    
823      priv->osd.backup_x = x-1;      char *latitude = osd_latitude_str(lat);
824      priv->osd.backup_y = y-1;      char *longitude = osd_longitude_str(lon);
825    
826        cairo_text_extents_t lat_extents, lon_extents;
827        cairo_text_extents (cr, latitude, &lat_extents);
828        cairo_text_extents (cr, longitude, &lon_extents);
829    
830        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
831        cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
832        cairo_move_to (cr,
833                       OSD_COORDINATES_OFFSET - lat_extents.x_bearing,
834                       OSD_COORDINATES_OFFSET - lat_extents.y_bearing);
835        cairo_text_path (cr, latitude);
836        cairo_move_to (cr,
837                       OSD_COORDINATES_OFFSET - lon_extents.x_bearing,
838                       OSD_COORDINATES_OFFSET - lon_extents.y_bearing +
839                       OSD_COORDINATES_FONT_SIZE);
840        cairo_text_path (cr, longitude);
841        cairo_stroke (cr);
842    
843        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
844        cairo_move_to (cr,
845                       OSD_COORDINATES_OFFSET - lat_extents.x_bearing,
846                       OSD_COORDINATES_OFFSET - lat_extents.y_bearing);
847        cairo_show_text (cr, latitude);
848        cairo_move_to (cr,
849                       OSD_COORDINATES_OFFSET - lon_extents.x_bearing,
850                       OSD_COORDINATES_OFFSET - lon_extents.y_bearing +
851                       OSD_COORDINATES_FONT_SIZE);
852        cairo_show_text (cr, longitude);
853    
854  #ifdef USE_CAIRO      g_free(latitude);
855      /* OSD itself uses some off-screen rendering, so check if the */      g_free(longitude);
     /* 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);  
     }  
856    
     // 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);  
857      cairo_destroy(cr);      cairo_destroy(cr);
   
 #else  
 #warning "OSD control display lacks a non-cairo implementation!"  
 #endif  
858  }  }
859    #endif  // OSD_COORDINATES
860    
861  static void  #ifdef OSD_CROSSHAIR
 osm_gps_map_osd_restore (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
   
     /* restore backup of previous contents */  
     if(priv->osd.backup) {  
         /* 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);  
     }  
 }  
862    
863    #ifndef OSD_CROSSHAIR_RADIUS
864    #define OSD_CROSSHAIR_RADIUS 10
865  #endif  #endif
866    
867  static gboolean  #define OSD_CROSSHAIR_TICK  (OSD_CROSSHAIR_RADIUS/2)
868  osm_gps_map_map_redraw (OsmGpsMap *map)  #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
869  {  #define OSD_CROSSHAIR_W  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
870      OsmGpsMapPrivate *priv = map->priv;  #define OSD_CROSSHAIR_H  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
   
     priv->idle_map_redraw = 0;  
871    
872      /* the motion_notify handler uses priv->pixmap to redraw the area; if we  static void
873       * change it while we are dragging, we will end up showing it in the wrong  osd_render_crosshair_shape(cairo_t *cr) {
874       * place. This could be fixed by carefully recompute the coordinates, but      cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
875       * for now it's easier just to disable redrawing the map while dragging */                 OSD_CROSSHAIR_RADIUS, 0,  2*M_PI);
876      if (priv->dragging)  
877          return FALSE;      cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
878                       OSD_CROSSHAIR_H/2);
879      priv->redraw_cycle++;      cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
880        cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
881      /* draw white background to initialise pixmap */                     OSD_CROSSHAIR_H/2);
882      gdk_draw_rectangle (      cairo_rel_line_to (cr,  OSD_CROSSHAIR_TICK, 0);
883                          priv->pixmap,  
884                          GTK_WIDGET(map)->style->white_gc,      cairo_move_to (cr, OSD_CROSSHAIR_W/2,
885                          TRUE,                     OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
886                          0, 0,      cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
887                          GTK_WIDGET(map)->allocation.width + EXTRA_BORDER * 2,      cairo_move_to (cr, OSD_CROSSHAIR_W/2,
888                          GTK_WIDGET(map)->allocation.height + EXTRA_BORDER * 2);                     OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
889        cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
     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));  
890    
891      return FALSE;      cairo_stroke (cr);
892  }  }
893    
894  static void  static void
895  osm_gps_map_map_redraw_idle (OsmGpsMap *map)  osd_render_crosshair(osm_gps_map_osd_t *osd)
896  {  {
897      OsmGpsMapPrivate *priv = map->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
898    
899      if (priv->idle_map_redraw == 0)      /* first fill with transparency */
900          priv->idle_map_redraw = g_idle_add ((GSourceFunc)osm_gps_map_map_redraw, map);      cairo_t *cr = cairo_create(priv->crosshair);
901  }      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
902        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
903  static void      //    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
904  osm_gps_map_init (OsmGpsMap *object)      cairo_paint(cr);
905  {      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
     OsmGpsMapPrivate *priv;  
   
     priv = G_TYPE_INSTANCE_GET_PRIVATE (object, OSM_TYPE_GPS_MAP, OsmGpsMapPrivate);  
     object->priv = priv;  
   
     priv->pixmap = NULL;  
   
     priv->trip_history = NULL;  
     priv->gps = g_new0(coord_t, 1);  
     priv->gps_valid = FALSE;  
   
 #ifdef ENABLE_BALLOON  
     priv->balloon.coo = g_new0(coord_t, 1);  
     priv->balloon.valid = FALSE;  
     priv->balloon.cb = NULL;  
 #endif  
   
 #ifdef ENABLE_OSD  
     priv->osd.backup = NULL;  
     priv->osd.overlay = NULL;  
     priv->osd.cb = NULL;  
 #endif  
   
     priv->tracks = NULL;  
     priv->images = NULL;  
   
     priv->drag_counter = 0;  
     priv->drag_mouse_dx = 0;  
     priv->drag_mouse_dy = 0;  
     priv->drag_start_mouse_x = 0;  
     priv->drag_start_mouse_y = 0;  
   
     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  
906    
907      //Hash table which maps tile d/l URIs to SoupMessage requests      cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
908      priv->tile_queue = g_hash_table_new (g_str_hash, g_str_equal);  
909        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
910        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
911        osd_render_crosshair_shape(cr);
912    
913        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
914        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
915        osd_render_crosshair_shape(cr);
916    
917      //Some mapping providers (Google) have varying degrees of tiles at multiple      cairo_destroy(cr);
     //zoom levels  
     priv->missing_tiles = g_hash_table_new (g_str_hash, g_str_equal);  
   
     /* memory cache for most recently used tiles */  
     priv->tile_cache = g_hash_table_new_full (g_str_hash, g_str_equal,  
                                               g_free, (GDestroyNotify)cached_tile_free);  
     priv->max_tile_cache_size = 20;  
   
     gtk_widget_add_events (GTK_WIDGET (object),  
                            GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |  
                            GDK_POINTER_MOTION_MASK |  
                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);  
     GTK_WIDGET_SET_FLAGS (object, GTK_CAN_FOCUS);  
   
     g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, my_log_handler, NULL);  
 }  
   
 #ifndef G_CHECKSUM_MD5  
 /* simple hash algorithm hack if md5 is not present */  
 static char *simple_hash(char *str) {  
     union {  
         char str[4];  
         gulong val;  
     } hash = { .val = 0x55555555 };  
   
     while(*str) {  
         hash.str[(int)str & 3] ^= *str;  
         str++;  
     }  
     return g_strdup_printf("%08lX", hash.val);  
918  }  }
919  #endif  #endif
920    
921  static GObject *  #ifdef OSD_SCALE
 osm_gps_map_constructor (GType gtype, guint n_properties, GObjectConstructParam *properties)  
 {  
     GObject *object;  
     OsmGpsMapPrivate *priv;  
     OsmGpsMap *map;  
     const char *uri;  
   
     //Always chain up to the parent constructor  
     object = G_OBJECT_CLASS(osm_gps_map_parent_class)->constructor(gtype, n_properties, properties);  
     map = OSM_GPS_MAP(object);  
     priv = OSM_GPS_MAP_PRIVATE(object);  
   
     //user can specify a map source ID, or a repo URI as the map source  
     uri = osm_gps_map_source_get_repo_uri(OSM_GPS_MAP_SOURCE_NULL);  
     if ( (priv->map_source == 0) || (strcmp(priv->repo_uri, uri) == 0) ) {  
         g_debug("Using null source");  
         priv->map_source = OSM_GPS_MAP_SOURCE_NULL;  
922    
923          priv->null_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 256, 256);  #ifndef OSD_SCALE_FONT_SIZE
924          gdk_pixbuf_fill(priv->null_tile, 0xcccccc00);  #define OSD_SCALE_FONT_SIZE 12
     }  
     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);  
         }  
     }  
   
     if (!priv->cache_dir_is_full_path) {  
 #ifdef G_CHECKSUM_MD5  
         char *md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, priv->repo_uri, -1);  
 #else  
         char *md5 = simple_hash(priv->repo_uri);  
925  #endif  #endif
926    #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
927    #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
928    
929          if (priv->cache_dir) {  /* various parameters used to create the scale */
930              char *old = priv->cache_dir;  #define OSD_SCALE_H2   (OSD_SCALE_H/2)
931              //the new cachedir is the given cache dir + the md5 of the repo_uri  #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
932              priv->cache_dir = g_strdup_printf("%s%c%s", old, G_DIR_SEPARATOR, md5);  #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
933              g_debug("Adjusting cache dir %s -> %s", old, priv->cache_dir);  #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
934              g_free(old);  #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
         } else {  
             //the new cachedir is the current dir + the md5 of the repo_uri  
             priv->cache_dir = g_strdup(md5);  
         }  
   
         g_free(md5);  
     }  
   
     inspect_map_uri(map);  
   
     return object;  
 }  
935    
936  static void  static void
937  osm_gps_map_dispose (GObject *object)  osd_render_scale(osm_gps_map_osd_t *osd)
938  {  {
939      OsmGpsMap *map = OSM_GPS_MAP(object);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     OsmGpsMapPrivate *priv = map->priv;  
940    
941      if (priv->is_disposed)      /* this only needs to be rendered if the zoom has changed */
942        gint zoom;
943        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
944        if(zoom == priv->scale_zoom)
945          return;          return;
946    
947      priv->is_disposed = TRUE;      priv->scale_zoom = zoom;
   
     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);  
948    
949      if (priv->idle_map_redraw != 0)      float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
         g_source_remove (priv->idle_map_redraw);  
950    
951      g_free(priv->gps);      /* first fill with transparency */
952        cairo_t *cr = cairo_create(priv->scale);
953        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
954        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
955        // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
956        cairo_paint(cr);
957        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
958    
959  #ifdef ENABLE_BALLOON      /* determine the size of the scale width in meters */
960      g_free(priv->balloon.coo);      float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
961  #endif  
962        /* scale this to useful values */
963        int exp = logf(width)*M_LOG10E;
964        int mant = width/pow(10,exp);
965        int width_metric = mant * pow(10,exp);
966        char *dist_str = NULL;
967        if(width_metric<1000)
968            dist_str = g_strdup_printf("%u m", width_metric);
969        else
970            dist_str = g_strdup_printf("%u km", width_metric/1000);
971        width_metric /= m_per_pix;
972    
973        /* and now the hard part: scale for useful imperial values :-( */
974        /* try to convert to feet, 1ft == 0.3048 m */
975        width /= 0.3048;
976        float imp_scale = 0.3048;
977        char *dist_imp_unit = "ft";
978    
979        if(width >= 100) {
980            /* 1yd == 3 feet */
981            width /= 3.0;
982            imp_scale *= 3.0;
983            dist_imp_unit = "yd";
984    
985            if(width >= 1760.0) {
986                /* 1mi == 1760 yd */
987                width /= 1760.0;
988                imp_scale *= 1760.0;
989                dist_imp_unit = "mi";
990            }
991        }
992    
993  #ifdef ENABLE_OSD      /* also convert this to full tens/hundreds */
994      if (priv->osd.backup)      exp = logf(width)*M_LOG10E;
995          g_object_unref(priv->osd.backup);      mant = width/pow(10,exp);
996        int width_imp = mant * pow(10,exp);
997        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
998    
999        /* convert back to pixels */
1000        width_imp *= imp_scale;
1001        width_imp /= m_per_pix;
1002    
1003        cairo_select_font_face (cr, "Sans",
1004                                CAIRO_FONT_SLANT_NORMAL,
1005                                CAIRO_FONT_WEIGHT_BOLD);
1006        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1007        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1008    
1009        cairo_text_extents_t extents;
1010        cairo_text_extents (cr, dist_str, &extents);
1011    
1012        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1013        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1014        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1015        cairo_text_path (cr, dist_str);
1016        cairo_stroke (cr);
1017        cairo_move_to (cr, 2*OSD_SCALE_FD,
1018                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1019        cairo_text_path (cr, dist_str_imp);
1020        cairo_stroke (cr);
1021    
1022      if (priv->osd.overlay)      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1023           cairo_surface_destroy(priv->osd.overlay);      cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1024  #endif      cairo_show_text (cr, dist_str);
1025        cairo_move_to (cr, 2*OSD_SCALE_FD,
1026                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1027        cairo_show_text (cr, dist_str_imp);
1028    
1029        g_free(dist_str);
1030        g_free(dist_str_imp);
1031    
1032        /* draw white line */
1033        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1034        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1035        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
1036        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1037        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1038        cairo_rel_line_to (cr, width_metric, 0);
1039        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1040        cairo_stroke(cr);
1041        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1042        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1043        cairo_rel_line_to (cr, width_imp, 0);
1044        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1045        cairo_stroke(cr);
1046    
1047        /* draw black line */
1048        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1049        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1050        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1051        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1052        cairo_rel_line_to (cr, width_metric, 0);
1053        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1054        cairo_stroke(cr);
1055        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1056        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1057        cairo_rel_line_to (cr, width_imp, 0);
1058        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1059        cairo_stroke(cr);
1060    
1061      G_OBJECT_CLASS (osm_gps_map_parent_class)->dispose (object);      cairo_destroy(cr);
1062  }  }
1063    #endif
1064    
1065  static void  static void
1066  osm_gps_map_finalize (GObject *object)  osd_render(osm_gps_map_osd_t *osd)
1067  {  {
1068      OsmGpsMap *map = OSM_GPS_MAP(object);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     OsmGpsMapPrivate *priv = map->priv;  
1069    
1070      g_free(priv->cache_dir);  #ifndef OSD_COLOR
1071      g_free(priv->repo_uri);      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1072      g_free(priv->image_format);      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
1073        GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
1074      osm_gps_map_free_trip(map);  #endif
     osm_gps_map_free_tracks(map);  
1075    
1076      G_OBJECT_CLASS (osm_gps_map_parent_class)->finalize (object);      /* first fill with transparency */
1077  }      cairo_t *cr = cairo_create(priv->overlay);
1078        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1079        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1080        cairo_paint(cr);
1081        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1082    
1083  static void      /* --------- draw zoom and dpad shape shadow ----------- */
1084  osm_gps_map_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)  #ifdef OSD_SHADOW_ENABLE
1085  {      osd_zoom_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1086      g_return_if_fail (OSM_IS_GPS_MAP (object));      osd_shape_shadow(cr);
1087      OsmGpsMap *map = OSM_GPS_MAP(object);  #ifndef OSD_NO_DPAD
1088      OsmGpsMapPrivate *priv = map->priv;      osd_dpad_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1089        osd_shape_shadow(cr);
1090    #endif
1091    #endif
1092    
1093      switch (prop_id)      /* --------- draw zoom and dpad shape ----------- */
     {  
         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);  
1094    
1095                  g_object_set_property(G_OBJECT(priv->soup_session),SOUP_SESSION_PROXY_URI,&val);      osd_zoom_shape(cr, 1, 1);
1096    #ifndef OSD_COLOR
1097        osd_shape(cr, &bg, &fg);
1098  #else  #else
1099                  SoupUri* uri = soup_uri_new(priv->proxy_uri);      osd_shape(cr);
                 g_object_set(G_OBJECT(priv->soup_session), SOUP_SESSION_PROXY_URI, uri, NULL);  
1100  #endif  #endif
1101              } else  #ifndef OSD_NO_DPAD
1102                  priv->proxy_uri = NULL;      osd_dpad_shape(cr, 1, 1);
1103    #ifndef OSD_COLOR
1104              break;      osd_shape(cr, &bg, &fg);
1105          case PROP_TILE_CACHE_DIR:  #else
1106              if ( g_value_get_string(value) )      osd_shape(cr);
                 priv->cache_dir = g_value_dup_string (value);  
             break;  
         case PROP_TILE_CACHE_DIR_IS_FULL_PATH:  
             priv->cache_dir_is_full_path = g_value_get_boolean (value);  
             break;  
         case PROP_ZOOM:  
             priv->map_zoom = g_value_get_int (value);  
             break;  
         case PROP_MAX_ZOOM:  
             priv->max_zoom = g_value_get_int (value);  
             break;  
         case PROP_MIN_ZOOM:  
             priv->min_zoom = g_value_get_int (value);  
             break;  
         case PROP_MAP_X:  
             priv->map_x = g_value_get_int (value);  
             priv->center_coord_set = FALSE;  
             break;  
         case PROP_MAP_Y:  
             priv->map_y = g_value_get_int (value);  
             priv->center_coord_set = FALSE;  
             break;  
         case PROP_GPS_TRACK_WIDTH:  
             priv->ui_gps_track_width = g_value_get_int (value);  
             break;  
         case PROP_GPS_POINT_R1:  
             priv->ui_gps_point_inner_radius = g_value_get_int (value);  
             break;  
         case PROP_GPS_POINT_R2:  
             priv->ui_gps_point_outer_radius = g_value_get_int (value);  
             break;  
         case PROP_MAP_SOURCE:  
             priv->map_source = g_value_get_int (value);  
             break;  
         case PROP_IMAGE_FORMAT:  
             priv->image_format = g_value_dup_string (value);  
             break;  
         default:  
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);  
             break;  
     }  
 }  
   
 static void  
 osm_gps_map_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)  
 {  
     g_return_if_fail (OSM_IS_GPS_MAP (object));  
     float lat,lon;  
     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;  
     }  
 }  
   
 static gboolean  
 osm_gps_map_scroll_event (GtkWidget *widget, GdkEventScroll  *event)  
 {  
     OsmGpsMap *map = OSM_GPS_MAP(widget);  
     OsmGpsMapPrivate *priv = map->priv;  
   
     if (event->direction == GDK_SCROLL_UP)  
     {  
         osm_gps_map_set_zoom(map, priv->map_zoom+1);  
     }  
     else  
     {  
         osm_gps_map_set_zoom(map, priv->map_zoom-1);  
     }  
   
     return FALSE;  
 }  
   
 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;  
     }  
1107  #endif  #endif
   
 #ifdef ENABLE_OSD  
     #define SCROLL_STEP 10  
   
     /* pressed inside OSD control? */  
     osd_button_t but = osm_gps_map_osd_check(event->x, event->y);  
     if(but != OSD_NONE)  
     {  
         priv->drag_counter = -1;  
   
         switch(but) {  
         case OSD_GPS:  
             priv->osd.cb(priv->osd.data);  
             break;  
   
         case OSD_UP:  
             priv->map_y -= GTK_WIDGET(widget)->allocation.height/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_DOWN:  
             priv->map_y += GTK_WIDGET(widget)->allocation.height/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_LEFT:  
             priv->map_x -= GTK_WIDGET(widget)->allocation.width/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_RIGHT:  
             priv->map_x += GTK_WIDGET(widget)->allocation.width/SCROLL_STEP;  
             priv->center_coord_set = FALSE;  
             break;  
   
         case OSD_IN:  
             osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom+1);  
             break;  
   
         case OSD_OUT:  
             osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom-1);  
             break;  
   
         default:  
             break;  
         }  
   
         osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));  
   
         return FALSE;  
     }  
1108  #endif  #endif
1109    
1110      priv->drag_counter = 0;      /* --------- draw zoom and dpad labels --------- */
     priv->drag_start_mouse_x = (int) event->x;  
     priv->drag_start_mouse_y = (int) event->y;  
     priv->drag_start_map_x = priv->map_x;  
     priv->drag_start_map_y = priv->map_y;  
   
     return FALSE;  
 }  
   
 static gboolean  
 osm_gps_map_button_release (GtkWidget *widget, GdkEventButton *event)  
 {  
     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
1111    
1112  #ifdef ENABLE_BALLOON  #ifdef OSD_SHADOW_ENABLE
1113      /* released inside the balloon? */      osd_labels_shadow(cr, Z_RAD/3, TRUE);
1114      if (osm_gps_map_in_balloon(priv,      osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1115                     event->x + EXTRA_BORDER,  #ifndef OSD_NO_DPAD
1116                     event->y + EXTRA_BORDER))      osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1117      {  #endif
1118          osm_gps_map_handle_balloon_click(OSM_GPS_MAP(widget),      cairo_stroke(cr);
1119               event->x - priv->balloon.rect.x + EXTRA_BORDER,  #ifdef OSD_GPS_BUTTON
1120               event->y - priv->balloon.rect.y + EXTRA_BORDER);      osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
1121      }      osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1122        cairo_stroke(cr);
1123  #endif  #endif
   
     if (priv->dragging)  
     {  
         priv->dragging = FALSE;  
   
         priv->map_x = priv->drag_start_map_x;  
         priv->map_y = priv->drag_start_map_y;  
   
         priv->map_x += (priv->drag_start_mouse_x - (int) event->x);  
         priv->map_y += (priv->drag_start_mouse_y - (int) event->y);  
   
         priv->center_coord_set = FALSE;  
   
         osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));  
     }  
   
     priv->drag_mouse_dx = 0;  
     priv->drag_mouse_dy = 0;  
     priv->drag_counter = -1;  
   
     return FALSE;  
 }  
   
 static gboolean  
 osm_gps_map_expose (GtkWidget *widget, GdkEventExpose  *event);  
   
 static gboolean  
 osm_gps_map_motion_notify (GtkWidget *widget, GdkEventMotion  *event)  
 {  
     int x, y;  
     GdkModifierType state;  
     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
   
     if (event->is_hint)  
         gdk_window_get_pointer (event->window, &x, &y, &state);  
     else  
     {  
         x = event->x;  
         y = event->y;  
         state = event->state;  
     }  
   
     // are we being dragged  
     if (!(state & GDK_BUTTON1_MASK))  
         return FALSE;  
   
     if (priv->drag_counter < 0)  
         return FALSE;  
   
     priv->drag_counter++;  
   
     // we havent dragged more than 6 pixels  
     if (priv->drag_counter < 6)  
         return FALSE;  
   
 #ifdef USE_MAEMO  
     /* reduce update frequency on maemo to keep screen update fluid */  
     static guint32 last_time = 0;  
   
     if(event->time - last_time < 100) return FALSE;  
     last_time = event->time;  
1124  #endif  #endif
1125    
1126      priv->dragging = TRUE;  #ifndef OSD_COLOR
1127        osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
1128      if (priv->map_auto_center)  #else
1129          g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);      osd_labels(cr, Z_RAD/3, TRUE);
   
     priv->drag_mouse_dx = x - priv->drag_start_mouse_x;  
     priv->drag_mouse_dy = y - priv->drag_start_mouse_y;  
   
 #ifdef ENABLE_OSD  
     /* undo OSD */  
     osm_gps_map_osd_restore (OSM_GPS_MAP(widget));  
   
     /* draw new OSD */  
     osm_gps_map_osd_draw_controls (OSM_GPS_MAP(widget),  
                                    -priv->drag_mouse_dx,  
                                    -priv->drag_mouse_dy);  
1130  #endif  #endif
1131        osd_zoom_labels(cr, 1, 1);
1132    #ifndef OSD_NO_DPAD
1133        osd_dpad_labels(cr, 1, 1);
1134    #endif
1135        cairo_stroke(cr);
1136    
1137      osm_gps_map_expose (widget, NULL);  #ifndef OSD_COLOR
1138        osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
1139    #else
1140      return FALSE;      osd_labels(cr, Z_RAD/6, osd->cb != NULL);
1141  }  #endif
1142    #ifdef OSD_GPS_BUTTON
1143  static gboolean      osd_dpad_gps(cr, 1, 1);
1144  osm_gps_map_configure (GtkWidget *widget, GdkEventConfigure *event)  #endif
1145  {      cairo_stroke(cr);
1146      OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
1147        cairo_destroy(cr);
     /* create pixmap */  
     if (priv->pixmap)  
         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);  
   
     /* and gc, used for clipping (I think......) */  
     if(priv->gc_map)  
         g_object_unref(priv->gc_map);  
   
     priv->gc_map = gdk_gc_new(priv->pixmap);  
   
     osm_gps_map_map_redraw(OSM_GPS_MAP(widget));  
1148    
1149      return FALSE;  #ifdef OSD_SOURCE_SEL
1150  }      osd_render_source_sel(osd);
1151    #endif
1152    
1153  static gboolean  #ifdef OSD_SCALE
1154  osm_gps_map_expose (GtkWidget *widget, GdkEventExpose  *event)      osd_render_scale(osd);
1155  {  #endif
     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);  
1156    
1157      gdk_draw_drawable (  #ifdef OSD_CROSSHAIR
1158                         widget->window,      osd_render_crosshair(osd);
1159                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],  #endif
                        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);  
     }  
1160    
1161      if (priv->drag_mouse_dy>EXTRA_BORDER) {  #ifdef OSD_COORDINATES
1162          gdk_draw_rectangle (      osd_render_coordinates(osd);
                             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);  
1163  #endif  #endif
     return FALSE;  
1164  }  }
1165    
1166  static void  static void
1167  osm_gps_map_class_init (OsmGpsMapClass *klass)  osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
1168  {  {
1169      GObjectClass* object_class = G_OBJECT_CLASS (klass);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);  
   
     g_type_class_add_private (klass, sizeof (OsmGpsMapPrivate));  
1170    
1171      object_class->dispose = osm_gps_map_dispose;      /* OSD itself uses some off-screen rendering, so check if the */
1172      object_class->finalize = osm_gps_map_finalize;      /* offscreen buffer is present and create it if not */
1173      object_class->constructor = osm_gps_map_constructor;      if(!priv->overlay) {
1174      object_class->set_property = osm_gps_map_set_property;          /* create overlay ... */
1175      object_class->get_property = osm_gps_map_get_property;          priv->overlay =
1176                cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
     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));  
 }  
   
 const char*  
 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;  
 }  
   
 const char *  
 osm_gps_map_source_get_image_format(OsmGpsMapSource_t source)  
 {  
     switch(source) {  
         case OSM_GPS_MAP_SOURCE_NULL:  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:  
         case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:  
             return "png";  
         case OSM_GPS_MAP_SOURCE_OPENAERIALMAP:  
         case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:  
         case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:  
         case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:  
         case OSM_GPS_MAP_SOURCE_YAHOO_STREET:  
         case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:  
         case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:  
         case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:  
         case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:  
             return "jpg";  
         default:  
             return "bin";  
     }  
     return "bin";  
 }  
   
   
 int  
 osm_gps_map_source_get_min_zoom(OsmGpsMapSource_t source)  
 {  
     return 1;  
 }  
   
 int  
 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;  
 }  
1177    
1178  void  #ifdef OSD_SOURCE_SEL
1179  osm_gps_map_download_maps (OsmGpsMap *map, coord_t *pt1, coord_t *pt2, int zoom_start, int zoom_end)          /* the initial OSD state is alway not-expanded */
1180  {          priv->map_source =
1181      int i,j,zoom,num_tiles;              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1182      OsmGpsMapPrivate *priv = map->priv;                                             OSD_S_W+2, OSD_S_H+2);
1183    #endif
1184    
1185      if (pt1 && pt2)  #ifdef OSD_SCALE
1186      {          priv->scale =
1187          gchar *filename;              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1188          num_tiles = 0;                                         OSD_SCALE_W, OSD_SCALE_H);
1189          zoom_end = CLAMP(zoom_end, priv->min_zoom, priv->max_zoom);          priv->scale_zoom = -1;
1190          g_debug("Download maps: z:%d->%d",zoom_start, zoom_end);  #endif
   
         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);  
         }  
     }  
 }  
1191    
1192  void  #ifdef OSD_CROSSHAIR
1193  osm_gps_map_get_bbox (OsmGpsMap *map, coord_t *pt1, coord_t *pt2)          priv->crosshair =
1194  {              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1195      OsmGpsMapPrivate *priv = map->priv;                                         OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1196    #endif
1197    
1198      if (pt1 && pt2) {  #ifdef OSD_COORDINATES
1199          pt1->rlat = pixel2lat(priv->map_zoom, priv->map_y);          priv->coordinates =
1200          pt1->rlon = pixel2lon(priv->map_zoom, priv->map_x);              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1201          pt2->rlat = pixel2lat(priv->map_zoom, priv->map_y + GTK_WIDGET(map)->allocation.height);                                         OSD_COORDINATES_W, OSD_COORDINATES_H);
1202          pt2->rlon = pixel2lon(priv->map_zoom, priv->map_x + GTK_WIDGET(map)->allocation.width);  #endif
1203    
1204          g_debug("BBOX: %f %f %f %f", pt1->rlat, pt1->rlon, pt2->rlat, pt2->rlon);          /* ... and render it */
1205            osd_render(osd);
1206      }      }
 }  
1207    
1208  void      // now draw this onto the original context
1209  osm_gps_map_set_mapcenter (OsmGpsMap *map, float latitude, float longitude, int zoom)      cairo_t *cr = gdk_cairo_create(drawable);
 {  
     osm_gps_map_set_center (map, latitude, longitude);  
     osm_gps_map_set_zoom (map, zoom);  
 }  
1210    
1211  void      int x, y;
 osm_gps_map_set_center (OsmGpsMap *map, float latitude, float longitude)  
 {  
     int pixel_x, pixel_y;  
     OsmGpsMapPrivate *priv;  
1212    
1213      g_return_if_fail (OSM_IS_GPS_MAP (map));  #ifdef OSD_SCALE
1214      priv = map->priv;      x =  OSD_X;
1215        y = -OSD_Y;
1216        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1217        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1218    
1219      priv->center_rlat = deg2rad(latitude);      cairo_set_source_surface(cr, priv->scale, x, y);
1220      priv->center_rlon = deg2rad(longitude);      cairo_paint(cr);
1221      priv->center_coord_set = TRUE;  #endif
1222    
1223      // pixel_x,y, offsets  #ifdef OSD_CROSSHAIR
1224      pixel_x = lon2pixel(priv->map_zoom, priv->center_rlon);      x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1225      pixel_y = lat2pixel(priv->map_zoom, priv->center_rlat);      y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1226    
1227      priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;      cairo_set_source_surface(cr, priv->crosshair, x, y);
1228      priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;      cairo_paint(cr);
1229    #endif
1230    
1231      osm_gps_map_map_redraw_idle(map);  #ifdef OSD_COORDINATES
1232  }      x = -OSD_X;
1233        y = -OSD_Y;
1234        if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1235        if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1236    
1237  int      cairo_set_source_surface(cr, priv->coordinates, x, y);
1238  osm_gps_map_set_zoom (OsmGpsMap *map, int zoom)      cairo_paint(cr);
1239  {  #endif
     int zoom_old;  
     double factor = 0.0;  
     int width_center, height_center;  
     OsmGpsMapPrivate *priv;  
1240    
1241      g_return_val_if_fail (OSM_IS_GPS_MAP (map), 0);      x = OSD_X;
1242      priv = map->priv;      if(x < 0)
1243            x += osd->widget->allocation.width - OSD_W;
1244    
1245        y = OSD_Y;
1246        if(y < 0)
1247            y += osd->widget->allocation.height - OSD_H;
1248    
1249      if (zoom != priv->map_zoom)      cairo_set_source_surface(cr, priv->overlay, x, y);
1250      {      cairo_paint(cr);
         width_center  = GTK_WIDGET(map)->allocation.width / 2;  
         height_center = GTK_WIDGET(map)->allocation.height / 2;  
1251    
1252          zoom_old = priv->map_zoom;  #ifdef OSD_SOURCE_SEL
1253          //constrain zoom min_zoom -> max_zoom      if(!priv->handler_id) {
1254          priv->map_zoom = CLAMP(zoom, priv->min_zoom, priv->max_zoom);          /* the OSD source selection is not being animated */
1255            if(!priv->expanded)
1256          if (priv->center_coord_set)              x = osd->widget->allocation.width - OSD_S_W;
         {  
             priv->map_x = lon2pixel(priv->map_zoom, priv->center_rlon) - width_center;  
             priv->map_y = lat2pixel(priv->map_zoom, priv->center_rlat) - height_center;  
         }  
1257          else          else
1258          {              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1259              factor = exp(priv->map_zoom * M_LN2)/exp(zoom_old * M_LN2);      } else
1260              priv->map_x = ((priv->map_x + width_center) * factor) - width_center;          x = priv->shift;
1261              priv->map_y = ((priv->map_y + height_center) * factor) - height_center;  
1262          }      y = OSD_S_Y;
1263        if(OSD_S_Y < 0) {
1264          g_debug("Zoom changed from %d to %d factor:%f x:%d",          if(!priv->expanded)
1265                  zoom_old, priv->map_zoom, factor, priv->map_x);              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1266            else
1267          osm_gps_map_map_redraw_idle(map);              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1268      }      }
     return priv->map_zoom;  
 }  
   
 void  
 osm_gps_map_add_track (OsmGpsMap *map, GSList *track)  
 {  
     OsmGpsMapPrivate *priv;  
1269    
1270      g_return_if_fail (OSM_IS_GPS_MAP (map));      cairo_set_source_surface(cr, priv->map_source, x, y);
1271      priv = map->priv;      cairo_paint(cr);
1272    #endif
1273    
1274      if (track) {      cairo_destroy(cr);
         priv->tracks = g_slist_append(priv->tracks, track);  
         osm_gps_map_map_redraw_idle(map);  
     }  
1275  }  }
1276    
1277  void  static void
1278  osm_gps_map_clear_tracks (OsmGpsMap *map)  osd_free(osm_gps_map_osd_t *osd)
1279  {  {
1280      g_return_if_fail (OSM_IS_GPS_MAP (map));      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1281    
1282      osm_gps_map_free_tracks(map);      if (priv->overlay)
1283      osm_gps_map_map_redraw_idle(map);           cairo_surface_destroy(priv->overlay);
 }  
1284    
1285  void  #ifdef OSD_SOURCE_SEL
1286  osm_gps_map_add_image (OsmGpsMap *map, float latitude, float longitude, GdkPixbuf *image)      if(priv->handler_id)
1287  {          gtk_timeout_remove(priv->handler_id);
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
1288    
1289      if (image) {      if (priv->map_source)
1290          OsmGpsMapPrivate *priv = map->priv;           cairo_surface_destroy(priv->map_source);
1291          image_t *im;  #endif
1292    
1293          //cache w/h for speed, and add image to list  #ifdef OSD_SCALE
1294          im = g_new0(image_t,1);      if (priv->scale)
1295          im->w = gdk_pixbuf_get_width(image);           cairo_surface_destroy(priv->scale);
1296          im->h = gdk_pixbuf_get_height(image);  #endif
         im->pt.rlat = deg2rad(latitude);  
         im->pt.rlon = deg2rad(longitude);  
1297    
1298          g_object_ref(image);  #ifdef OSD_CROSSHAIR
1299          im->image = image;      if (priv->crosshair)
1300             cairo_surface_destroy(priv->crosshair);
1301    #endif
1302    
1303          priv->images = g_slist_append(priv->images, im);  #ifdef OSD_COORDINATES
1304        if (priv->coordinates)
1305             cairo_surface_destroy(priv->coordinates);
1306    #endif
1307    
1308          osm_gps_map_map_redraw_idle(map);      g_free(priv);
     }  
1309  }  }
1310    
1311  gboolean  static gboolean
1312  osm_gps_map_remove_image (OsmGpsMap *map, GdkPixbuf *image)  osd_busy(osm_gps_map_osd_t *osd)
1313  {  {
1314      OsmGpsMapPrivate *priv = map->priv;  #ifdef OSD_SOURCE_SEL
1315      if (priv->images) {      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1316          GSList *list;      return (priv->handler_id != 0);
1317          for(list = priv->images; list != NULL; list = list->next)  #else
         {  
             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;  
                 }  
         }  
     }  
1318      return FALSE;      return FALSE;
1319    #endif
1320  }  }
1321    
1322  void  static osm_gps_map_osd_t osd_classic = {
1323  osm_gps_map_clear_images (OsmGpsMap *map)      .widget     = NULL,
 {  
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
   
     osm_gps_map_free_images(map);  
     osm_gps_map_map_redraw_idle(map);  
 }  
   
 void  
 osm_gps_map_osd_speed (OsmGpsMap *map, float speed)  
 {  
     OsmGpsMapPrivate *priv;  
   
     PangoContext        *context = NULL;  
     PangoLayout     *layout  = NULL;  
     PangoFontDescription    *desc    = NULL;  
   
     GdkColor color;  
     GdkGC *gc;  
   
     gchar *buffer;  
     //static int x = 10, y = 10;  
     static int width = 0, height = 0;  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     buffer = g_strdup_printf("%.0f", speed);  
   
     /* pango initialisation */  
     context = gtk_widget_get_pango_context (GTK_WIDGET(map));  
     layout  = pango_layout_new (context);  
     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);  
 }  
   
 void  
 osm_gps_map_draw_gps (OsmGpsMap *map, float latitude, float longitude, float heading)  
 {  
     int pixel_x, pixel_y;  
     OsmGpsMapPrivate *priv;  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     priv->gps->rlat = deg2rad(latitude);  
     priv->gps->rlon = deg2rad(longitude);  
     priv->gps_valid = TRUE;  
   
     // 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;  
     }  
   
     //Automatically center the map if the track approaches the edge  
     if(priv->map_auto_center)   {  
         int x = pixel_x - priv->map_x;  
         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;  
         }  
     }  
   
     // this redraws the map (including the gps track, and adjusts the  
     // map center if it was changed  
     osm_gps_map_map_redraw_idle(map);  
 }  
   
 void  
 osm_gps_map_clear_gps (OsmGpsMap *map)  
 {  
     osm_gps_map_free_trip(map);  
     osm_gps_map_map_redraw_idle(map);  
 }  
   
 coord_t  
 osm_gps_map_get_co_ordinates (OsmGpsMap *map, int pixel_x, int pixel_y)  
 {  
     coord_t coord;  
     OsmGpsMapPrivate *priv = map->priv;  
1324    
1325      coord.rlat = pixel2lat(priv->map_zoom, priv->map_y + pixel_y);      .draw       = osd_draw,
1326      coord.rlon = pixel2lon(priv->map_zoom, priv->map_x + pixel_x);      .check      = osd_check,
1327      return coord;      .render     = osd_render,
1328  }      .free       = osd_free,
1329        .busy       = osd_busy,
1330    
1331  GtkWidget *      .cb         = NULL,
1332  osm_gps_map_new (void)      .data       = NULL,
 {  
     return g_object_new (OSM_TYPE_GPS_MAP, NULL);  
 }  
1333    
1334  void      .priv       = NULL
1335  osm_gps_map_screen_to_geographic (OsmGpsMap *map, gint pixel_x, gint pixel_y,  };
                                   gfloat *latitude, gfloat *longitude)  
 {  
     OsmGpsMapPrivate *priv;  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     if (latitude)  
         *latitude = rad2deg(pixel2lat(priv->map_zoom, priv->map_y + pixel_y));  
     if (longitude)  
         *longitude = rad2deg(pixel2lon(priv->map_zoom, priv->map_x + pixel_x));  
 }  
1336    
1337    /* this is the only function that's externally visible */
1338  void  void
1339  osm_gps_map_geographic_to_screen (OsmGpsMap *map,  osm_gps_map_osd_classic_init(OsmGpsMap *map)
                                   gfloat latitude, gfloat longitude,  
                                   gint *pixel_x, gint *pixel_y)  
 {  
     OsmGpsMapPrivate *priv;  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     if (pixel_x)  
         *pixel_x = lon2pixel(priv->map_zoom, deg2rad(longitude)) - priv->map_x;  
     if (pixel_y)  
         *pixel_y = lat2pixel(priv->map_zoom, deg2rad(latitude)) - priv->map_y;  
 }  
   
 void  
 osm_gps_map_scroll (OsmGpsMap *map, gint dx, gint dy)  
 {  
     OsmGpsMapPrivate *priv;  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     priv->center_coord_set = FALSE;  
     priv->map_x += dx;  
     priv->map_y += dy;  
   
     osm_gps_map_map_redraw_idle (map);  
 }  
   
 float  
 osm_gps_map_get_scale(OsmGpsMap *map)  
1340  {  {
1341      OsmGpsMapPrivate *priv;      osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
1342    
1343      g_return_val_if_fail (OSM_IS_GPS_MAP (map), OSM_NAN);      osd_classic.priv = priv;
     priv = map->priv;  
1344    
1345      return osm_gps_map_get_scale_at_point(priv->map_zoom, priv->center_rlat, priv->center_rlon);      osm_gps_map_register_osd(map, &osd_classic);
1346  }  }
1347    
1348  #ifdef ENABLE_BALLOON  #ifdef OSD_GPS_BUTTON
1349  void  /* below are osd specific functions which aren't used by osm-gps-map */
1350  osm_gps_map_draw_balloon (OsmGpsMap *map, float latitude, float longitude,  /* but instead are to be used by the main application */
1351                            OsmGpsMapBalloonCallback cb, gpointer data)  void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
1352  {                                   gpointer data) {
1353      OsmGpsMapPrivate *priv;      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1354        g_return_if_fail (osd);
     /* remove and previously installed balloon */  
     osm_gps_map_clear_balloon (map);  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     priv->balloon.coo->rlat = deg2rad(latitude);  
     priv->balloon.coo->rlon = deg2rad(longitude);  
     priv->balloon.valid = TRUE;  
   
     priv->balloon.cb = cb;  
     priv->balloon.data = data;  
   
     // this redraws the map  
     osm_gps_map_map_redraw_idle(map);  
 }  
   
 void  
 osm_gps_map_clear_balloon (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv;  
1355    
1356      g_return_if_fail (OSM_IS_GPS_MAP (map));      osd->cb = cb;
1357      priv = map->priv;      osd->data = data;
   
     priv->balloon.valid = FALSE;  
   
     osm_gps_map_map_redraw_idle(map);  
 }  
 #endif  
   
 #ifdef ENABLE_OSD  
 void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdGpsCallback cb, gpointer data) {  
     OsmGpsMapPrivate *priv;  
   
     g_return_if_fail (OSM_IS_GPS_MAP (map));  
     priv = map->priv;  
   
     priv->osd.cb = cb;  
     priv->osd.data = data;  
1358    
1359      /* this may have changed the state of the gps button */      /* this may have changed the state of the gps button */
1360      /* we thus re-render the overlay */      /* we thus re-render the overlay */
1361      osm_gps_map_osd_render(priv);      osd->render(osd);
1362    
1363      osm_gps_map_map_redraw_idle(map);      osm_gps_map_redraw(map);
1364  }  }
1365  #endif  #endif
1366    
1367    osd_button_t
1368    osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
1369        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1370        g_return_val_if_fail (osd, OSD_NONE);
1371    
1372        return osd_check(osd, x, y);
1373    }

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