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

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