Diff of /trunk/src/osm-gps-map-osd-classic.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 86 by harbaum, Sun Aug 30 19:05:44 2009 UTC revision 150 by harbaum, Thu Oct 29 20:25:11 2009 UTC
# Line 19  Line 19 
19    
20  #include "config.h"  #include "config.h"
21  #include <stdlib.h>  // abs  #include <stdlib.h>  // abs
22  #include <math.h>    // M_PI  #include <string.h>
23    #include <math.h>    // M_PI/cos()
 #define OSD_SOURCE_SEL  
   
 #define OSD_Y  (-10)  
24    
25  /* parameters that can be overwritten from the config file: */  /* parameters that can be overwritten from the config file: */
26  /* OSD_DIAMETER */  /* OSD_DIAMETER */
# Line 36  Line 33 
33  #include <cairo.h>  #include <cairo.h>
34    
35  #include "osm-gps-map.h"  #include "osm-gps-map.h"
36    #include "converter.h"
37  #include "osm-gps-map-osd-classic.h"  #include "osm-gps-map-osd-classic.h"
38    
39  //the osd controls  //the osd controls
40  typedef struct {  typedef struct {
41      /* the offscreen representation of the OSD */      /* the offscreen representation of the OSD */
42      cairo_surface_t *overlay;      struct {
43            cairo_surface_t *surface;
44            gboolean rendered;
45    #ifdef OSD_GPS_BUTTON
46            gboolean gps_enabled;
47    #endif
48        } controls;
49    
50    #ifdef OSD_BALLOON
51        //a balloon with additional info
52        struct {
53            cairo_surface_t *surface;
54            int orientation, offset_x, offset_y;
55    
56            gboolean just_created;
57            float lat, lon;
58            OsmGpsMapRect_t rect;
59    
60            /* function called to have the user app draw the contents */
61            OsmGpsMapBalloonCallback cb;
62            gpointer data;
63        } balloon;
64    #endif
65    
66    #ifdef OSD_SCALE
67        struct {
68            cairo_surface_t *surface;
69            int zoom;
70        } scale;
71    #endif
72    
73    #ifdef OSD_CROSSHAIR
74        struct {
75            cairo_surface_t *surface;
76            gboolean rendered;
77        } crosshair;
78    #endif
79    
80    #ifdef OSD_NAV
81        struct {
82            cairo_surface_t *surface;
83            float lat, lon;
84            char *name;
85            gboolean imperial;    // display distance imperial/metric
86        } nav;
87    #endif
88    
89    #ifdef OSD_COORDINATES
90        struct {
91            cairo_surface_t *surface;
92            float lat, lon;
93        } coordinates;
94    #endif
95    
96    #ifdef OSD_SOURCE_SEL
97        struct {
98            /* values to handle the "source" menu */
99            cairo_surface_t *surface;
100            gboolean expanded;
101            gint shift, dir, count;
102            gint handler_id;
103            gint width, height;
104            gboolean rendered;
105        } source_sel;
106    #endif
107    
     cairo_surface_t *map_source;  
     gboolean expanded;  
     gint shift, dir, count;  
     gint handler_id;  
108  } osd_priv_t;  } osd_priv_t;
109    
110    #ifdef OSD_BALLOON
111    /* most visual effects are hardcoded by now, but may be made */
112    /* available via properties later */
113    #ifndef BALLOON_AREA_WIDTH
114    #define BALLOON_AREA_WIDTH           290
115    #endif
116    #ifndef BALLOON_AREA_HEIGHT
117    #define BALLOON_AREA_HEIGHT           75
118    #endif
119    #ifndef BALLOON_CORNER_RADIUS
120    #define BALLOON_CORNER_RADIUS         10
121    #endif
122    
123    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
124    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
125    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
126    #define BALLOON_TRANSPARENCY         0.8
127    #define POINTER_HEIGHT                20
128    #define POINTER_FOOT_WIDTH            20
129    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
130    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
131    #define BALLOON_SHADOW_TRANSPARENCY  0.2
132    
133    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
134    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
135    
136    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
137    
138    #if 0
139    #define FIN   printf("entering function %s\n", __func__);
140    #define FOUT  printf("leaving function %s\n", __func__);
141    #else
142    #define FIN   ;
143    #define FOUT  ;
144    #endif
145    
146    /* draw the bubble shape. this is used twice, once for the shape and once */
147    /* for the shadow */
148    static void
149    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
150           gboolean bottom, int px, int py, int px0, int px1) {
151    
152        FIN;
153    
154        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
155        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
156                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
157        if(!bottom) {
158            /* insert top pointer */
159            cairo_line_to (cr, px1, y0);
160            cairo_line_to (cr, px, py);
161            cairo_line_to (cr, px0, y0);
162        }
163    
164        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
165        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
166                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
167        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
168        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
169                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
170        if(bottom) {
171            /* insert bottom pointer */
172            cairo_line_to (cr, px0, y1);
173            cairo_line_to (cr, px, py);
174            cairo_line_to (cr, px1, y1);
175        }
176    
177        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
178        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
179                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
180    
181        cairo_close_path (cr);
182    
183        FOUT;
184    }
185    
186    static void
187    osd_render_balloon(osm_gps_map_osd_t *osd) {
188        FIN;
189    
190        osd_priv_t *priv = (osd_priv_t*)osd->priv;
191    
192        if(!priv->balloon.surface)
193            return;
194    
195        /* get zoom */
196        gint zoom;
197        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
198    
199        /* ------- convert given coordinate into screen position --------- */
200        gint xs, ys;
201        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
202                                          priv->balloon.lat, priv->balloon.lon,
203                                          &xs, &ys);
204    
205        gint x0 = 1, y0 = 1;
206    
207        /* check position of this relative to screen center to determine */
208        /* pointer direction ... */
209        int pointer_x, pointer_x0, pointer_x1;
210        int pointer_y;
211    
212        /* ... and calculate position */
213        int orientation = 0;
214        if(xs > osd->widget->allocation.width/2) {
215            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
216            pointer_x = x0 - priv->balloon.offset_x;
217            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
218            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
219            orientation |= 1;
220        } else {
221            priv->balloon.offset_x = -POINTER_OFFSET;
222            pointer_x = x0 - priv->balloon.offset_x;
223            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
224            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
225        }
226    
227        gboolean bottom = FALSE;
228        if(ys > osd->widget->allocation.height/2) {
229            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
230            pointer_y = y0 - priv->balloon.offset_y;
231            bottom = TRUE;
232            orientation |= 2;
233        } else {
234            priv->balloon.offset_y = 0;
235            pointer_y = y0 - priv->balloon.offset_y;
236            y0 += POINTER_HEIGHT;
237        }
238    
239        /* if required orientation equals current one, then don't render */
240        /* anything */
241        if(orientation == priv->balloon.orientation)
242            return;
243    
244        priv->balloon.orientation = orientation;
245    
246        /* calculate bottom/right of box */
247        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
248    
249        /* save balloon screen coordinates for later use */
250        priv->balloon.rect.x = x0 + BALLOON_BORDER;
251        priv->balloon.rect.y = y0 + BALLOON_BORDER;
252        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
253        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
254    
255        cairo_t *cr = cairo_create(priv->balloon.surface);
256        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
257        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
258        cairo_paint(cr);
259        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
260    
261        /* --------- draw shadow --------------- */
262        osm_gps_map_draw_balloon_shape (cr,
263                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
264                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
265                     bottom, pointer_x, pointer_y,
266                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
267    
268        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
269        cairo_fill_preserve (cr);
270        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
271        cairo_set_line_width (cr, 0);
272        cairo_stroke (cr);
273    
274        /* --------- draw main shape ----------- */
275        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
276                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
277    
278        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
279        cairo_fill_preserve (cr);
280        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
281        cairo_set_line_width (cr, 1);
282        cairo_stroke (cr);
283    
284        if (priv->balloon.cb) {
285            osm_gps_map_balloon_event_t event;
286    
287            /* clip in case application tries to draw in */
288                /* exceed of the balloon */
289            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
290                             priv->balloon.rect.w, priv->balloon.rect.h);
291            cairo_clip (cr);
292            cairo_new_path (cr);  /* current path is not consumed by cairo_clip */
293    
294            /* request the application to draw the balloon contents */
295            event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW;
296            event.data.draw.rect = &priv->balloon.rect;
297            event.data.draw.cr = cr;
298    
299            priv->balloon.cb(&event, priv->balloon.data);
300        }
301    
302        cairo_destroy(cr);
303    
304        FOUT;
305    }
306    
307    /* return true if balloon is being displayed and if */
308    /* the given coordinate is within this balloon */
309    static gboolean
310    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean click, gboolean down, gint x, gint y)
311    {
312        FIN;
313    
314        osd_priv_t *priv = (osd_priv_t*)osd->priv;
315    
316        if(!priv->balloon.surface)
317            return FALSE;
318    
319        gint xs, ys;
320        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
321                                          priv->balloon.lat, priv->balloon.lon,
322                                          &xs, &ys);
323    
324        xs += priv->balloon.rect.x + priv->balloon.offset_x;
325        ys += priv->balloon.rect.y + priv->balloon.offset_y;
326    
327        gboolean is_in =
328            (x > xs) && (x < xs + priv->balloon.rect.w) &&
329            (y > ys) && (y < ys + priv->balloon.rect.h);
330    
331        /* is this a real click or is the application just checking for something? */
332        if(click) {
333    
334            /* handle the fact that the balloon may have been created by the */
335            /* button down event */
336            if(!is_in && !down && !priv->balloon.just_created) {
337                /* the user actually clicked outside the balloon */
338    
339                /* close the balloon! */
340                osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
341    
342                /* and inform application about this */
343                if(priv->balloon.cb) {
344                    osm_gps_map_balloon_event_t event;
345                    event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED;
346                    priv->balloon.cb(&event, priv->balloon.data);
347                }
348    
349            }
350    
351            if(is_in && priv->balloon.cb) {
352                osm_gps_map_balloon_event_t event;
353    
354                /* notify application of click */
355                event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK;
356                event.data.click.x = x - xs;
357                event.data.click.y = y - ys;
358                event.data.click.down = down;
359    
360                priv->balloon.cb(&event, priv->balloon.data);
361            }
362        }
363        FOUT;
364        return is_in;
365    }
366    
367    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
368        FIN;
369    
370        g_return_if_fail (OSM_IS_GPS_MAP (map));
371    
372        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
373        g_return_if_fail (osd);
374    
375        osd_priv_t *priv = (osd_priv_t*)osd->priv;
376        g_return_if_fail (priv);
377    
378        if(priv->balloon.surface) {
379            cairo_surface_destroy(priv->balloon.surface);
380            priv->balloon.surface = NULL;
381            priv->balloon.lat = OSM_GPS_MAP_INVALID;
382            priv->balloon.lon = OSM_GPS_MAP_INVALID;
383        }
384        osm_gps_map_redraw(map);
385        FOUT;
386    }
387    
388    void
389    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
390                                  OsmGpsMapBalloonCallback cb, gpointer data) {
391        FIN;
392        g_return_if_fail (OSM_IS_GPS_MAP (map));
393    
394        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
395        g_return_if_fail (osd);
396    
397        osd_priv_t *priv = (osd_priv_t*)osd->priv;
398        g_return_if_fail (priv);
399    
400        osm_gps_map_osd_clear_balloon (map);
401    
402        /* allocate balloon surface */
403        priv->balloon.surface =
404            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
405                                       BALLOON_W+2, BALLOON_H+2);
406    
407        priv->balloon.lat = latitude;
408        priv->balloon.lon = longitude;
409        priv->balloon.cb = cb;
410        priv->balloon.data = data;
411        priv->balloon.just_created = TRUE;
412    
413        priv->balloon.orientation = -1;
414    
415        osd_render_balloon(osd);
416    
417        osm_gps_map_redraw(map);
418        FOUT;
419    }
420    
421    #endif // OSD_BALLOON
422    
423  /* position and extent of bounding box */  /* position and extent of bounding box */
424  #ifndef OSD_X  #ifndef OSD_X
425  #define OSD_X      (10)  #define OSD_X      (10)
# Line 128  osd_labels(cairo_t *cr, gint width, gboo Line 499  osd_labels(cairo_t *cr, gint width, gboo
499      if(enabled)  gdk_cairo_set_source_color(cr, fg);      if(enabled)  gdk_cairo_set_source_color(cr, fg);
500      else         gdk_cairo_set_source_color(cr, disabled);      else         gdk_cairo_set_source_color(cr, disabled);
501      cairo_set_line_width (cr, width);      cairo_set_line_width (cr, width);
     cairo_stroke (cr);  
502  }  }
503  #else  #else
504  static void  static void
# Line 136  osd_labels(cairo_t *cr, gint width, gboo Line 506  osd_labels(cairo_t *cr, gint width, gboo
506      if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);      if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);
507      else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);      else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
508      cairo_set_line_width (cr, width);      cairo_set_line_width (cr, width);
     cairo_stroke (cr);  
509  }  }
510  #endif  #endif
511    
# Line 145  static void Line 514  static void
514  osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {  osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
515      cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);      cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
516      cairo_set_line_width (cr, width);      cairo_set_line_width (cr, width);
     cairo_stroke (cr);  
517  }  }
518  #endif  #endif
519    
# Line 192  osd_shape(cairo_t *cr) { Line 560  osd_shape(cairo_t *cr) {
560  static gboolean  static gboolean
561  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)
562  {  {
563        FIN;
564      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
565  }  }
566    
# Line 262  osd_check_zoom(gint x, gint y) { Line 631  osd_check_zoom(gint x, gint y) {
631      return OSD_NONE;      return OSD_NONE;
632  }  }
633    
634    #ifdef OSD_SOURCE_SEL
635    
636  /* place source selection at right border */  /* place source selection at right border */
637  #define OSD_S_RAD (Z_RAD)  #define OSD_S_RAD (Z_RAD)
638  #define OSD_S_X   (-OSD_X)  #define OSD_S_X   (-OSD_X)
# Line 271  osd_check_zoom(gint x, gint y) { Line 642  osd_check_zoom(gint x, gint y) {
642  #define OSD_S_PH  (2 * Z_RAD)  #define OSD_S_PH  (2 * Z_RAD)
643  #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)  #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
644    
645  /* size of usable area when exapnded */  /* size of usable area when expanded */
646  #define OSD_S_AREA_W (200)  #define OSD_S_AREA_W (priv->source_sel.width)
647  #define OSD_S_AREA_H (200)  #define OSD_S_AREA_H (priv->source_sel.height)
648  #define OSD_S_EXP_W  (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)  #define OSD_S_EXP_W  (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
649  #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)  #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
650    
651  /* internal value to draw the arrow on the "puller" */  /* internal value to draw the arrow on the "puller" */
652  #define OSD_S_D0  (OSD_S_RAD/2)  #define OSD_S_D0  (OSD_S_RAD/2)
653    #ifndef OSD_FONT_SIZE
654    #define OSD_FONT_SIZE (16.0)
655    #endif
656    #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)
657    #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)
658    
659    /* draw the shape of the source selection OSD, either only the puller (not expanded) */
660    /* or the entire menu incl. the puller (expanded) */
661  static void  static void
662  osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {  osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
663      if(!priv->expanded) {      if(!priv->source_sel.expanded) {
664          /* just draw the puller */          /* just draw the puller */
665          cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);          cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
666          cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);          cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
# Line 305  osd_source_shape(osd_priv_t *priv, cairo Line 683  osd_source_shape(osd_priv_t *priv, cairo
683  }  }
684    
685  static void  static void
686  osd_source_content(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {  osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
687      y += OSD_S_RAD - OSD_S_D0;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
688    
689      if(!priv->expanded) {      int py = offset + OSD_S_RAD - OSD_S_D0;
690    
691        if(!priv->source_sel.expanded) {
692          /* draw the "puller" open (<) arrow */          /* draw the "puller" open (<) arrow */
693          cairo_move_to (cr, x + OSD_S_RAD + OSD_S_D0/2, y);          cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
694          cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);          cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
695          cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);          cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
696      } else {      } else {
697          if(OSD_S_Y < 0)          if(OSD_S_Y < 0)
698              y += OSD_S_AREA_H - OSD_S_PH;              py += OSD_S_AREA_H - OSD_S_PH;
699    
700          /* draw the "puller" close (>) arrow */          /* draw the "puller" close (>) arrow */
701          cairo_move_to (cr, x + OSD_S_RAD - OSD_S_D0/2, y);          cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
702          cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);          cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
703          cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);          cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
704            cairo_stroke(cr);
705    
706            /* don't draw a shadow for the text content */
707            if(offset == 1) {
708                gint source;
709                g_object_get(osd->widget, "map-source", &source, NULL);
710    
711                cairo_select_font_face (cr, "Sans",
712                                        CAIRO_FONT_SLANT_NORMAL,
713                                        CAIRO_FONT_WEIGHT_BOLD);
714                cairo_set_font_size (cr, OSD_FONT_SIZE);
715    
716                int i, step = (priv->source_sel.height - 2*OSD_TEXT_BORDER) /
717                    OSM_GPS_MAP_SOURCE_LAST;
718                for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
719                    cairo_text_extents_t extents;
720                    const char *src = osm_gps_map_source_get_friendly_name(i);
721                    cairo_text_extents (cr, src, &extents);
722    
723                    int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
724                    int y = offset + step * (i-1) + OSD_TEXT_BORDER;
725    
726                    /* draw filled rectangle if selected */
727                    if(source == i) {
728                        cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
729                                        y - OSD_TEXT_SKIP,
730                                        priv->source_sel.width - OSD_TEXT_BORDER,
731                                        step + OSD_TEXT_SKIP);
732                        cairo_fill(cr);
733    
734                        /* temprarily draw with background color */
735    #ifndef OSD_COLOR
736                        GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
737                        gdk_cairo_set_source_color(cr, &bg);
738    #else
739                        cairo_set_source_rgb (cr, OSD_COLOR_BG);
740    #endif
741                    }
742    
743                    cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
744                    cairo_show_text (cr, src);
745    
746                    /* restore color */
747                    if(source == i) {
748    #ifndef OSD_COLOR
749                        GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
750                        gdk_cairo_set_source_color(cr, &fg);
751    #else
752                        cairo_set_source_rgb (cr, OSD_COLOR);
753    #endif
754                    }
755                }
756            }
757      }      }
758  }  }
759    
760  static void  static void
761  osd_render_source_sel(osm_gps_map_osd_t *osd) {  osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
762        FIN;
763    
764      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
765    
766        if(!priv->source_sel.surface ||
767           (priv->source_sel.rendered && !force_rerender))
768            return;
769    
770        priv->source_sel.rendered = TRUE;
771    
772  #ifndef OSD_COLOR  #ifndef OSD_COLOR
773      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
774      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
# Line 335  osd_render_source_sel(osm_gps_map_osd_t Line 776  osd_render_source_sel(osm_gps_map_osd_t
776  #endif  #endif
777    
778      /* draw source selector */      /* draw source selector */
779      cairo_t *cr = cairo_create(priv->map_source);      cairo_t *cr = cairo_create(priv->source_sel.surface);
780      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
781      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
782      cairo_paint(cr);      cairo_paint(cr);
# Line 354  osd_render_source_sel(osm_gps_map_osd_t Line 795  osd_render_source_sel(osm_gps_map_osd_t
795  #endif  #endif
796    
797  #ifdef OSD_SHADOW_ENABLE  #ifdef OSD_SHADOW_ENABLE
     osd_source_content(priv, cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);  
798      osd_labels_shadow(cr, Z_RAD/3, TRUE);      osd_labels_shadow(cr, Z_RAD/3, TRUE);
799        osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
800        cairo_stroke (cr);
801  #endif  #endif
     osd_source_content(priv, cr, 1, 1);  
802  #ifndef OSD_COLOR  #ifndef OSD_COLOR
803      osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);      osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
804  #else  #else
805      osd_labels(cr, Z_RAD/3, TRUE);      osd_labels(cr, Z_RAD/3, TRUE);
806  #endif  #endif
807        osd_source_content(osd, cr, 1);
808        cairo_stroke (cr);
809    
810      cairo_destroy(cr);      cairo_destroy(cr);
811        FOUT;
812  }  }
813    
814    /* re-allocate the buffer used to draw the menu. This is used */
815    /* to collapse/expand the buffer */
816  static void  static void
817  osd_source_reallocate(osm_gps_map_osd_t *osd) {  osd_source_reallocate(osm_gps_map_osd_t *osd) {
818        FIN;
819      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
820    
821      /* re-allocate offscreen bitmap */      /* re-allocate offscreen bitmap */
822      g_assert (priv->map_source);      g_assert (priv->source_sel.surface);
     cairo_surface_destroy(priv->map_source);  
823    
824      int w = OSD_S_W, h = OSD_S_H;      int w = OSD_S_W, h = OSD_S_H;
825      if(priv->expanded) {      if(priv->source_sel.expanded) {
826            cairo_text_extents_t extents;
827    
828            /* determine content size */
829            cairo_t *cr = cairo_create(priv->source_sel.surface);
830            cairo_select_font_face (cr, "Sans",
831                                    CAIRO_FONT_SLANT_NORMAL,
832                                    CAIRO_FONT_WEIGHT_BOLD);
833            cairo_set_font_size (cr, OSD_FONT_SIZE);
834    
835            /* calculate menu size */
836            int i, max_h = 0, max_w = 0;
837            for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
838                const char *src = osm_gps_map_source_get_friendly_name(i);
839                cairo_text_extents (cr, src, &extents);
840    
841                if(extents.width > max_w) max_w = extents.width;
842                if(extents.height > max_h) max_h = extents.height;
843            }
844            cairo_destroy(cr);
845    
846            priv->source_sel.width  = max_w + 2*OSD_TEXT_BORDER;
847            priv->source_sel.height = OSM_GPS_MAP_SOURCE_LAST *
848                (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
849    
850          w = OSD_S_EXP_W;          w = OSD_S_EXP_W;
851          h = OSD_S_EXP_H;          h = OSD_S_EXP_H;
852      }      }
853    
854      priv->map_source =      cairo_surface_destroy(priv->source_sel.surface);
855        priv->source_sel.surface =
856          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
857    
858      osd_render_source_sel(osd);      osd_render_source_sel(osd, TRUE);
859        FOUT;
860  }  }
861    
862  #define OSD_HZ      15  #define OSD_HZ      15
863  #define OSD_TIME    1000  #define OSD_TIME    500
864    
865  static gboolean osd_source_animate(gpointer data) {  static gboolean osd_source_animate(gpointer data) {
866        FIN;
867      osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;      osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
868      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
869      int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;      int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
870      gboolean done = FALSE;      gboolean done = FALSE;
871      priv->count += priv->dir;      priv->source_sel.count += priv->source_sel.dir;
872    
873      /* shifting in */      /* shifting in */
874      if(priv->dir < 0) {      if(priv->source_sel.dir < 0) {
875          if(priv->count <= 0) {          if(priv->source_sel.count <= 0) {
876              priv->count = 0;              priv->source_sel.count = 0;
877              done = TRUE;              done = TRUE;
878          }          }
879      } else {      } else {
880          if(priv->count >= 1000) {          if(priv->source_sel.count >= 1000) {
881              priv->expanded = FALSE;              priv->source_sel.expanded = FALSE;
882              osd_source_reallocate(osd);              osd_source_reallocate(osd);
883    
884              priv->count = 1000;              priv->source_sel.count = 1000;
885              done = TRUE;              done = TRUE;
886          }          }
887      }      }
# Line 417  static gboolean osd_source_animate(gpoin Line 889  static gboolean osd_source_animate(gpoin
889    
890      /* count runs linearly from 0 to 1000, map this nicely onto a position */      /* count runs linearly from 0 to 1000, map this nicely onto a position */
891    
892      /* simple linear mapping */      /* nice sinoid mapping */
893      //    priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +      float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;
894      //        (diff * priv->count)/1000;      priv->source_sel.shift =
895            (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
     /* nicer sinoid mapping */  
     float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;  
     priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +  
896          m * diff;          m * diff;
897    
898        /* make sure the screen is updated */
899      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
900    
901        /* stop animation if done */
902      if(done)      if(done)
903          priv->handler_id = 0;          priv->source_sel.handler_id = 0;
904    
905        FOUT;
906      return !done;      return !done;
907  }  }
908    
# Line 438  static gboolean osd_source_animate(gpoin Line 910  static gboolean osd_source_animate(gpoin
910  static void  static void
911  osd_source_toggle(osm_gps_map_osd_t *osd)  osd_source_toggle(osm_gps_map_osd_t *osd)
912  {  {
913        FIN;
914      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
915    
916      /* ignore clicks while animation is running */      /* ignore clicks while animation is running */
917      if(priv->handler_id)      if(priv->source_sel.handler_id)
918          return;          return;
919    
920      /* expand immediately, collapse is handle at the end of the collapse animation */      /* expand immediately, collapse is handle at the end of the */
921      if(!priv->expanded) {      /* collapse animation */
922          priv->expanded = TRUE;      if(!priv->source_sel.expanded) {
923            priv->source_sel.expanded = TRUE;
924          osd_source_reallocate(osd);          osd_source_reallocate(osd);
925    
926          priv->count = 1000;          priv->source_sel.count = 1000;
927          priv->shift = osd->widget->allocation.width - OSD_S_W;          priv->source_sel.shift = osd->widget->allocation.width - OSD_S_W;
928          priv->dir = -1000/OSD_HZ;          priv->source_sel.dir = -1000/OSD_HZ;
929      } else {      } else {
930          priv->count =  0;          priv->source_sel.count =  0;
931          priv->shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;          priv->source_sel.shift = osd->widget->allocation.width -
932          priv->dir = +1000/OSD_HZ;              OSD_S_EXP_W + OSD_S_X;
933            priv->source_sel.dir = +1000/OSD_HZ;
934      }      }
935    
936      priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);      /* start timer to handle animation */
937        priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
938                                                      osd_source_animate, osd);
939        FOUT;
940  }  }
941    
942    /* check if the user clicked inside the source selection area */
943  static osd_button_t  static osd_button_t
944  osd_source_check(osm_gps_map_osd_t *osd, gint x, gint y) {  osd_source_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
945        FIN;
946      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
947    
948      if(!priv->expanded)      if(!priv->source_sel.expanded)
949          x -= osd->widget->allocation.width - OSD_S_W;          x -= osd->widget->allocation.width - OSD_S_W;
950      else      else
951          x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;          x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
# Line 480  osd_source_check(osm_gps_map_osd_t *osd, Line 960  osd_source_check(osm_gps_map_osd_t *osd,
960          /* really within puller shape? */          /* really within puller shape? */
961          if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {          if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
962              /* expand source selector */              /* expand source selector */
963              osd_source_toggle(osd);              if(down)
964                    osd_source_toggle(osd);
965    
966              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
967              /* of the OSD */              /* of the OSD */
968              return OSD_BG;              return OSD_BG;
969          }          }
970      }      }
971    
972        /* check for clicks into data area */
973        if(priv->source_sel.expanded && !priv->source_sel.handler_id) {
974            /* re-adjust from puller top to content top */
975            if(OSD_S_Y < 0)
976                y += OSD_S_EXP_H - OSD_S_PH;
977    
978            if(x > OSD_S_PW &&
979               x < OSD_S_PW + OSD_S_EXP_W &&
980               y > 0 &&
981               y < OSD_S_EXP_H) {
982    
983                int step = (priv->source_sel.height - 2*OSD_TEXT_BORDER)
984                    / OSM_GPS_MAP_SOURCE_LAST;
985    
986                y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
987                y /= step;
988                y += 1;
989    
990                if(down) {
991                    gint old = 0;
992                    g_object_get(osd->widget, "map-source", &old, NULL);
993    
994                    if(y > OSM_GPS_MAP_SOURCE_NULL &&
995                       y <= OSM_GPS_MAP_SOURCE_LAST &&
996                       old != y) {
997                        g_object_set(osd->widget, "map-source", y, NULL);
998    
999                        osd_render_source_sel(osd, TRUE);
1000                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
1001                    }
1002                }
1003    
1004                /* return "clicked in OSD background" to prevent further */
1005                /* processing by application */
1006                return OSD_BG;
1007            }
1008        }
1009    
1010        FOUT;
1011      return OSD_NONE;      return OSD_NONE;
1012  }  }
1013    #endif // OSD_SOURCE_SEL
1014    
1015  static osd_button_t  static osd_button_t
1016  osd_check(osm_gps_map_osd_t *osd, gint x, gint y) {  osd_check_int(osm_gps_map_osd_t *osd, gboolean click, gboolean down, gint x, gint y) {
1017        FIN;
1018      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
1019    
1020    #ifdef OSD_BALLOON
1021        if(down) {
1022            /* needed to handle balloons that are created at click */
1023            osd_priv_t *priv = (osd_priv_t*)osd->priv;
1024            priv->balloon.just_created = FALSE;
1025        }
1026    #endif
1027    
1028  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1029      /* the source selection area is handles internally */      /* the source selection area is handles internally */
1030      but = osd_source_check(osd, x, y);      but = osd_source_check(osd, down, x, y);
     if(but != OSD_NONE)  
         return but;  
1031  #endif  #endif
1032    
1033      x -= OSD_X;      if(but == OSD_NONE) {
1034      y -= OSD_Y;          gint mx = x - OSD_X;
1035            gint my = y - OSD_Y;
1036      if(OSD_X < 0)  
1037          x -= (osd->widget->allocation.width - OSD_W);          if(OSD_X < 0)
1038                mx -= (osd->widget->allocation.width - OSD_W);
1039      if(OSD_Y < 0)  
1040          y -= (osd->widget->allocation.height - OSD_H);          if(OSD_Y < 0)
1041                my -= (osd->widget->allocation.height - OSD_H);
1042      /* first do a rough test for the OSD area. */  
1043      /* this is just to avoid an unnecessary detailed test */          /* first do a rough test for the OSD area. */
1044      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {          /* this is just to avoid an unnecessary detailed test */
1045            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
1046  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
1047          but = osd_check_dpad(x, y);              but = osd_check_dpad(mx, my);
1048  #endif  #endif
1049            }
1050    
1051          if(but == OSD_NONE)          if(but == OSD_NONE)
1052              but = osd_check_zoom(x, y);              but = osd_check_zoom(mx, my);
1053      }      }
1054    
1055    #ifdef OSD_BALLOON
1056        if(but == OSD_NONE) {
1057            /* check if user clicked into balloon */
1058            if(osd_balloon_check(osd, click, down, x, y))
1059                but = OSD_BG;
1060        }
1061    #endif
1062    
1063        FOUT;
1064      return but;      return but;
1065  }  }
1066    
# Line 594  osd_zoom_labels(cairo_t *cr, gint x, gin Line 1134  osd_zoom_labels(cairo_t *cr, gint x, gin
1134      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
1135  }  }
1136    
1137    #ifdef OSD_COORDINATES
1138    
1139    #ifndef OSD_COORDINATES_FONT_SIZE
1140    #define OSD_COORDINATES_FONT_SIZE (12.0)
1141    #endif
1142    
1143    #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
1144    
1145    #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1146    #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET+OSD_COORDINATES_FONT_SIZE/4)
1147    
1148    /* these can be overwritten with versions that support */
1149    /* localization */
1150    #ifndef OSD_COORDINATES_CHR_N
1151    #define OSD_COORDINATES_CHR_N  "N"
1152    #endif
1153    #ifndef OSD_COORDINATES_CHR_S
1154    #define OSD_COORDINATES_CHR_S  "S"
1155    #endif
1156    #ifndef OSD_COORDINATES_CHR_E
1157    #define OSD_COORDINATES_CHR_E  "E"
1158    #endif
1159    #ifndef OSD_COORDINATES_CHR_W
1160    #define OSD_COORDINATES_CHR_W  "W"
1161    #endif
1162    
1163    
1164    
1165    /* this is the classic geocaching notation */
1166    static char
1167    *osd_latitude_str(float latitude) {
1168        char *c = OSD_COORDINATES_CHR_N;
1169        float integral, fractional;
1170    
1171        if(isnan(latitude))
1172            return NULL;
1173    
1174        if(latitude < 0) {
1175            latitude = fabs(latitude);
1176            c = OSD_COORDINATES_CHR_S;
1177        }
1178    
1179        fractional = modff(latitude, &integral);
1180    
1181        return g_strdup_printf("%s %02d° %06.3f'",
1182                               c, (int)integral, fractional*60.0);
1183    }
1184    
1185    static char
1186    *osd_longitude_str(float longitude) {
1187        char *c = OSD_COORDINATES_CHR_E;
1188        float integral, fractional;
1189    
1190        if(isnan(longitude))
1191            return NULL;
1192    
1193        if(longitude < 0) {
1194            longitude = fabs(longitude);
1195            c = OSD_COORDINATES_CHR_W;
1196        }
1197    
1198        fractional = modff(longitude, &integral);
1199    
1200        return g_strdup_printf("%s %03d° %06.3f'",
1201                               c, (int)integral, fractional*60.0);
1202    }
1203    
1204    /* render a string at the given screen position */
1205    static int
1206    osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1207        FIN;
1208    
1209        if(!text) return y;
1210    
1211        char *p = g_malloc(strlen(text)+4);  // space for "...\n"
1212        strcpy(p, text);
1213    
1214        cairo_text_extents_t extents;
1215        cairo_text_extents (cr, p, &extents);
1216        g_assert(extents.width != 0.0);
1217    
1218        /* check if text needs to be truncated */
1219        int trunc_at = strlen(text)-1;
1220        while(extents.width > width) {
1221            g_assert(trunc_at > 0);
1222    
1223            trunc_at--;
1224            strcpy(p+trunc_at, "...");
1225            cairo_text_extents (cr, p, &extents);
1226        }
1227    
1228        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1229        cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1230        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1231        cairo_text_path (cr, p);
1232        cairo_stroke (cr);
1233    
1234        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1235        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1236        cairo_show_text (cr, p);
1237    
1238        g_free(p);
1239    
1240        /* skip + 1/5 line */
1241        FOUT;
1242        return y + 6*OSD_COORDINATES_FONT_SIZE/5;
1243    }
1244    
1245  static void  static void
1246  osd_render(osm_gps_map_osd_t *osd) {  osd_render_coordinates(osm_gps_map_osd_t *osd)
1247    {
1248        FIN;
1249      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1250    
1251        if(!priv->coordinates.surface)
1252            return;
1253    
1254        /* get current map position */
1255        gfloat lat, lon;
1256        g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1257    
1258        /* check if position has changed enough to require redraw */
1259        if(!isnan(priv->coordinates.lat) && !isnan(priv->coordinates.lon))
1260            /* 1/60000 == 1/1000 minute */
1261            if((fabsf(lat - priv->coordinates.lat) < 1/60000) &&
1262               (fabsf(lon - priv->coordinates.lon) < 1/60000))
1263                return;
1264    
1265        priv->coordinates.lat = lat;
1266        priv->coordinates.lon = lon;
1267    
1268        /* first fill with transparency */
1269    
1270        cairo_t *cr = cairo_create(priv->coordinates.surface);
1271        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1272        //    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1273        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1274        cairo_paint(cr);
1275        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1276    
1277        cairo_select_font_face (cr, "Sans",
1278                                CAIRO_FONT_SLANT_NORMAL,
1279                                CAIRO_FONT_WEIGHT_BOLD);
1280        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1281    
1282        char *latitude = osd_latitude_str(lat);
1283        char *longitude = osd_longitude_str(lon);
1284    
1285        int y = OSD_COORDINATES_OFFSET;
1286        y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1287        y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1288    
1289        g_free(latitude);
1290        g_free(longitude);
1291    
1292        cairo_destroy(cr);
1293        FOUT;
1294    }
1295    #endif  // OSD_COORDINATES
1296    
1297    #ifdef OSD_NAV
1298    #define OSD_NAV_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1299    #define OSD_NAV_H  (11*OSD_COORDINATES_FONT_SIZE)
1300    
1301    /* http://mathforum.org/library/drmath/view/55417.html */
1302    static float get_bearing(float lat1, float lon1, float lat2, float lon2) {
1303      return atan2( sin(lon2 - lon1) * cos(lat2),
1304                    cos(lat1) * sin(lat2) -
1305                    sin(lat1) * cos(lat2) * cos(lon2 - lon1));
1306    }
1307    
1308    /* http://mathforum.org/library/drmath/view/51722.html */
1309    static float get_distance(float lat1, float lon1, float lat2, float lon2) {
1310      float aob = acos(cos(lat1) * cos(lat2) * cos(lon2 - lon1) +
1311                       sin(lat1) * sin(lat2));
1312    
1313      //  return(aob * 3959.0);   /* great circle radius in miles */
1314    
1315      return(aob * 6371000.0);     /* great circle radius in meters */
1316    }
1317    
1318    static void
1319    osd_render_nav(osm_gps_map_osd_t *osd)
1320    {
1321        FIN;
1322        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1323    
1324        if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1325            return;
1326    
1327        /* first fill with transparency */
1328        cairo_t *cr = cairo_create(priv->nav.surface);
1329        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1330        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1331        cairo_paint(cr);
1332        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1333    
1334        cairo_select_font_face (cr, "Sans",
1335                                CAIRO_FONT_SLANT_NORMAL,
1336                                CAIRO_FONT_WEIGHT_BOLD);
1337        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1338    
1339        char *latitude = osd_latitude_str(priv->nav.lat);
1340        char *longitude = osd_longitude_str(priv->nav.lon);
1341    
1342        int y = OSD_COORDINATES_OFFSET;
1343        y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1344        y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1345        y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1346    
1347        g_free(latitude);
1348        g_free(longitude);
1349    
1350        /* draw the compass */
1351        int radius = (OSD_NAV_H - y - 5*OSD_COORDINATES_FONT_SIZE/4)/2;
1352        if(radius > OSD_NAV_W/2)
1353            radius = OSD_NAV_W/2;
1354    
1355        int x = OSD_NAV_W/2+1;
1356        y += radius;
1357    
1358        cairo_stroke (cr);
1359    
1360        /* draw background */
1361        cairo_arc(cr, x, y, radius, 0,  2*M_PI);
1362        cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
1363        cairo_fill_preserve (cr);
1364        cairo_set_source_rgb (cr, 0, 0, 0);
1365        cairo_set_line_width (cr, 1);
1366        cairo_stroke (cr);
1367    
1368        /* draw pointer */
1369    #define ARROW_WIDTH     0.3
1370    #define ARROW_LENGTH    0.7
1371    
1372        coord_t *gps = osm_gps_map_get_gps (OSM_GPS_MAP(osd->widget));
1373        if(gps) {
1374            float arot = get_bearing(gps->rlat, gps->rlon,
1375                         deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1376    
1377            cairo_move_to(cr,
1378                          x + radius *  ARROW_LENGTH *  sin(arot),
1379                          y + radius *  ARROW_LENGTH * -cos(arot));
1380    
1381            cairo_line_to(cr,
1382                          x + radius * -ARROW_LENGTH *  sin(arot+ARROW_WIDTH),
1383                          y + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH));
1384    
1385            cairo_line_to(cr,
1386                          x + radius * -0.5 * ARROW_LENGTH *  sin(arot),
1387                          y + radius * -0.5 * ARROW_LENGTH * -cos(arot));
1388    
1389            cairo_line_to(cr,
1390                          x + radius * -ARROW_LENGTH *  sin(arot-ARROW_WIDTH),
1391                          y + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH));
1392    
1393            cairo_close_path(cr);
1394            cairo_set_source_rgb (cr, 0, 0, 0);
1395            cairo_fill (cr);
1396    
1397            y += radius + OSD_COORDINATES_FONT_SIZE/4;
1398    
1399            float dist = get_distance(gps->rlat, gps->rlon,
1400                            deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1401    
1402            char *dist_str = NULL;
1403            if(!priv->nav.imperial) {
1404                /* metric is easy ... */
1405                if(dist<1000)
1406                    dist_str = g_strdup_printf("%u m", (int)dist);
1407                else
1408                    dist_str = g_strdup_printf("%.1f km", dist/1000);
1409            } else {
1410                /* and now the hard part: scale for useful imperial values :-( */
1411                /* try to convert to feet, 1ft == 0.3048 m */
1412    
1413                if(dist/(3*0.3048) >= 1760.0)      /* more than 1760 yard? */
1414                    dist_str = g_strdup_printf("%.1f mi", dist/(0.3048*3*1760.0));
1415                else if(dist/0.3048 >= 100)        /* more than 100 feet? */
1416                    dist_str = g_strdup_printf("%.1f yd", dist/(0.3048*3));
1417                else
1418                    dist_str = g_strdup_printf("%.0f ft", dist/0.3048);
1419            }
1420    
1421            y = osd_render_centered_text(cr, y, OSD_NAV_W, dist_str);
1422            g_free(dist_str);
1423        }
1424    
1425        cairo_destroy(cr);
1426        FOUT;
1427    }
1428    
1429    void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1430        FIN;
1431        g_return_if_fail (OSM_IS_GPS_MAP (map));
1432    
1433        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1434        g_return_if_fail (osd);
1435    
1436        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1437        g_return_if_fail (priv);
1438    
1439        if(priv->nav.surface) {
1440            cairo_surface_destroy(priv->nav.surface);
1441            priv->nav.surface = NULL;
1442            priv->nav.lat = OSM_GPS_MAP_INVALID;
1443            priv->nav.lon = OSM_GPS_MAP_INVALID;
1444            if(priv->nav.name) g_free(priv->nav.name);
1445        }
1446        osm_gps_map_redraw(map);
1447        FOUT;
1448    }
1449    
1450    void
1451    osm_gps_map_osd_draw_nav (OsmGpsMap *map, gboolean imperial,
1452                              float latitude, float longitude, char *name) {
1453        FIN;
1454        g_return_if_fail (OSM_IS_GPS_MAP (map));
1455    
1456        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1457        g_return_if_fail (osd);
1458    
1459        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1460        g_return_if_fail (priv);
1461    
1462        osm_gps_map_osd_clear_nav (map);
1463    
1464        /* allocate balloon surface */
1465        priv->nav.surface =
1466            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1467                                       OSD_NAV_W+2, OSD_NAV_H+2);
1468    
1469        priv->nav.lat = latitude;
1470        priv->nav.lon = longitude;
1471        priv->nav.name = g_strdup(name);
1472        priv->nav.imperial = imperial;
1473    
1474        osd_render_nav(osd);
1475    
1476        osm_gps_map_redraw(map);
1477        FOUT;
1478    }
1479    
1480    #endif // OSD_NAV
1481    
1482    
1483    #ifdef OSD_CROSSHAIR
1484    
1485    #ifndef OSD_CROSSHAIR_RADIUS
1486    #define OSD_CROSSHAIR_RADIUS 10
1487    #endif
1488    
1489    #define OSD_CROSSHAIR_TICK  (OSD_CROSSHAIR_RADIUS/2)
1490    #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
1491    #define OSD_CROSSHAIR_W  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1492    #define OSD_CROSSHAIR_H  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1493    
1494    static void
1495    osd_render_crosshair_shape(cairo_t *cr) {
1496        FIN;
1497        cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
1498                   OSD_CROSSHAIR_RADIUS, 0,  2*M_PI);
1499    
1500        cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
1501                       OSD_CROSSHAIR_H/2);
1502        cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
1503        cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
1504                       OSD_CROSSHAIR_H/2);
1505        cairo_rel_line_to (cr,  OSD_CROSSHAIR_TICK, 0);
1506    
1507        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1508                       OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
1509        cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
1510        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1511                       OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
1512        cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
1513    
1514        cairo_stroke (cr);
1515        FOUT;
1516    }
1517    
1518    static void
1519    osd_render_crosshair(osm_gps_map_osd_t *osd)
1520    {
1521        FIN;
1522        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1523    
1524        if(!priv->crosshair.surface || priv->crosshair.rendered)
1525            return;
1526    
1527        priv->crosshair.rendered = TRUE;
1528    
1529        /* first fill with transparency */
1530        cairo_t *cr = cairo_create(priv->crosshair.surface);
1531        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1532        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1533        cairo_paint(cr);
1534        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1535    
1536        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1537    
1538        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
1539        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
1540        osd_render_crosshair_shape(cr);
1541    
1542        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1543        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
1544        osd_render_crosshair_shape(cr);
1545    
1546        cairo_destroy(cr);
1547        FOUT;
1548    }
1549    #endif
1550    
1551    #ifdef OSD_SCALE
1552    
1553    #ifndef OSD_SCALE_FONT_SIZE
1554    #define OSD_SCALE_FONT_SIZE (12.0)
1555    #endif
1556    #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
1557    #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
1558    
1559    /* various parameters used to create the scale */
1560    #define OSD_SCALE_H2   (OSD_SCALE_H/2)
1561    #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
1562    #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
1563    #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
1564    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
1565    
1566    static void
1567    osd_render_scale(osm_gps_map_osd_t *osd)
1568    {
1569        FIN;
1570        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1571    
1572        if(!priv->scale.surface)
1573            return;
1574    
1575        /* this only needs to be rendered if the zoom has changed */
1576        gint zoom;
1577        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
1578        if(zoom == priv->scale.zoom)
1579            return;
1580    
1581        priv->scale.zoom = zoom;
1582    
1583        float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
1584    
1585        /* first fill with transparency */
1586        cairo_t *cr = cairo_create(priv->scale.surface);
1587        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1588        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1589        // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1590        cairo_paint(cr);
1591        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1592    
1593        /* determine the size of the scale width in meters */
1594        float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
1595    
1596        /* scale this to useful values */
1597        int exp = logf(width)*M_LOG10E;
1598        int mant = width/pow(10,exp);
1599        int width_metric = mant * pow(10,exp);
1600        char *dist_str = NULL;
1601        if(width_metric<1000)
1602            dist_str = g_strdup_printf("%u m", width_metric);
1603        else
1604            dist_str = g_strdup_printf("%u km", width_metric/1000);
1605        width_metric /= m_per_pix;
1606    
1607        /* and now the hard part: scale for useful imperial values :-( */
1608        /* try to convert to feet, 1ft == 0.3048 m */
1609        width /= 0.3048;
1610        float imp_scale = 0.3048;
1611        char *dist_imp_unit = "ft";
1612    
1613        if(width >= 100) {
1614            /* 1yd == 3 feet */
1615            width /= 3.0;
1616            imp_scale *= 3.0;
1617            dist_imp_unit = "yd";
1618    
1619            if(width >= 1760.0) {
1620                /* 1mi == 1760 yd */
1621                width /= 1760.0;
1622                imp_scale *= 1760.0;
1623                dist_imp_unit = "mi";
1624            }
1625        }
1626    
1627        /* also convert this to full tens/hundreds */
1628        exp = logf(width)*M_LOG10E;
1629        mant = width/pow(10,exp);
1630        int width_imp = mant * pow(10,exp);
1631        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
1632    
1633        /* convert back to pixels */
1634        width_imp *= imp_scale;
1635        width_imp /= m_per_pix;
1636    
1637        cairo_select_font_face (cr, "Sans",
1638                                CAIRO_FONT_SLANT_NORMAL,
1639                                CAIRO_FONT_WEIGHT_BOLD);
1640        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1641        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1642    
1643        cairo_text_extents_t extents;
1644        cairo_text_extents (cr, dist_str, &extents);
1645    
1646        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1647        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1648        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1649        cairo_text_path (cr, dist_str);
1650        cairo_stroke (cr);
1651        cairo_move_to (cr, 2*OSD_SCALE_FD,
1652                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1653        cairo_text_path (cr, dist_str_imp);
1654        cairo_stroke (cr);
1655    
1656        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1657        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1658        cairo_show_text (cr, dist_str);
1659        cairo_move_to (cr, 2*OSD_SCALE_FD,
1660                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1661        cairo_show_text (cr, dist_str_imp);
1662    
1663        g_free(dist_str);
1664        g_free(dist_str_imp);
1665    
1666        /* draw white line */
1667        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1668        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1669        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
1670        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1671        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1672        cairo_rel_line_to (cr, width_metric, 0);
1673        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1674        cairo_stroke(cr);
1675        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1676        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1677        cairo_rel_line_to (cr, width_imp, 0);
1678        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1679        cairo_stroke(cr);
1680    
1681        /* draw black line */
1682        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1683        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1684        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1685        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1686        cairo_rel_line_to (cr, width_metric, 0);
1687        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1688        cairo_stroke(cr);
1689        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1690        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1691        cairo_rel_line_to (cr, width_imp, 0);
1692        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1693        cairo_stroke(cr);
1694    
1695        cairo_destroy(cr);
1696        FOUT;
1697    }
1698    #endif
1699    
1700    static void
1701    osd_render_controls(osm_gps_map_osd_t *osd)
1702    {
1703        FIN;
1704        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1705    
1706        if(!priv->controls.surface)
1707            return;
1708    
1709        if(priv->controls.rendered
1710    #ifdef OSD_GPS_BUTTON
1711           && (priv->controls.gps_enabled == (osd->cb != NULL))
1712    #endif
1713           )
1714            return;
1715    
1716    #ifdef OSD_GPS_BUTTON
1717        priv->controls.gps_enabled = (osd->cb != NULL);
1718    #endif
1719        priv->controls.rendered = TRUE;
1720    
1721  #ifndef OSD_COLOR  #ifndef OSD_COLOR
1722      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1723      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
# Line 605  osd_render(osm_gps_map_osd_t *osd) { Line 1725  osd_render(osm_gps_map_osd_t *osd) {
1725  #endif  #endif
1726    
1727      /* first fill with transparency */      /* first fill with transparency */
1728      cairo_t *cr = cairo_create(priv->overlay);      cairo_t *cr = cairo_create(priv->controls.surface);
1729      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1730      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1731      cairo_paint(cr);      cairo_paint(cr);
# Line 641  osd_render(osm_gps_map_osd_t *osd) { Line 1761  osd_render(osm_gps_map_osd_t *osd) {
1761      /* --------- draw zoom and dpad labels --------- */      /* --------- draw zoom and dpad labels --------- */
1762    
1763  #ifdef OSD_SHADOW_ENABLE  #ifdef OSD_SHADOW_ENABLE
1764        osd_labels_shadow(cr, Z_RAD/3, TRUE);
1765      osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);      osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1766  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
1767      osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);      osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1768  #endif  #endif
1769      osd_labels_shadow(cr, Z_RAD/3, TRUE);      cairo_stroke(cr);
1770  #ifdef OSD_GPS_BUTTON  #ifdef OSD_GPS_BUTTON
     osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);  
1771      osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);      osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
1772        osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1773        cairo_stroke(cr);
1774  #endif  #endif
1775  #endif  #endif
1776    
     osd_zoom_labels(cr, 1, 1);  
 #ifndef OSD_NO_DPAD  
     osd_dpad_labels(cr, 1, 1);  
 #endif  
1777  #ifndef OSD_COLOR  #ifndef OSD_COLOR
1778      osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);      osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
1779  #else  #else
1780      osd_labels(cr, Z_RAD/3, TRUE);      osd_labels(cr, Z_RAD/3, TRUE);
1781  #endif  #endif
1782  #ifdef OSD_GPS_BUTTON      osd_zoom_labels(cr, 1, 1);
1783      osd_dpad_gps(cr, 1, 1);  #ifndef OSD_NO_DPAD
1784        osd_dpad_labels(cr, 1, 1);
1785    #endif
1786        cairo_stroke(cr);
1787    
1788  #ifndef OSD_COLOR  #ifndef OSD_COLOR
1789      osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);      osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
1790  #else  #else
1791      osd_labels(cr, Z_RAD/6, osd->cb != NULL);      osd_labels(cr, Z_RAD/6, osd->cb != NULL);
1792  #endif  #endif
1793    #ifdef OSD_GPS_BUTTON
1794        osd_dpad_gps(cr, 1, 1);
1795  #endif  #endif
1796        cairo_stroke(cr);
1797    
1798      cairo_destroy(cr);      cairo_destroy(cr);
1799    }
1800    
1801    static void
1802    osd_render(osm_gps_map_osd_t *osd)
1803    {
1804        /* this function is actually called pretty often since the */
1805        /* OSD contents may have changed (due to a coordinate/zoom change). */
1806        /* The different OSD parts have to make sure that they don't */
1807        /* render unneccessarily often and thus waste CPU power */
1808    
1809        osd_render_controls(osd);
1810    
1811    #ifdef OSD_SOURCE_SEL
1812        osd_render_source_sel(osd, FALSE);
1813    #endif
1814    
1815    #ifdef OSD_SCALE
1816        osd_render_scale(osd);
1817    #endif
1818    
1819    #ifdef OSD_CROSSHAIR
1820        osd_render_crosshair(osd);
1821    #endif
1822    
1823      osd_render_source_sel(osd);  #ifdef OSD_NAV
1824        osd_render_nav(osd);
1825    #endif
1826    
1827    #ifdef OSD_COORDINATES
1828        osd_render_coordinates(osd);
1829    #endif
1830  }  }
1831    
1832  static void  static void
# Line 682  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1836  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1836    
1837      /* OSD itself uses some off-screen rendering, so check if the */      /* OSD itself uses some off-screen rendering, so check if the */
1838      /* offscreen buffer is present and create it if not */      /* offscreen buffer is present and create it if not */
1839      if(!priv->overlay) {      if(!priv->controls.surface) {
1840          /* create overlay ... */          /* create overlay ... */
1841          priv->overlay =          priv->controls.surface =
1842              cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);              cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
1843    
1844            priv->controls.rendered = FALSE;
1845    #ifdef OSD_GPS_BUTTON
1846            priv->controls.gps_enabled = FALSE;
1847    #endif
1848    
1849    #ifdef OSD_SOURCE_SEL
1850          /* the initial OSD state is alway not-expanded */          /* the initial OSD state is alway not-expanded */
1851          priv->map_source =          priv->source_sel.surface =
1852              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1853                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
1854            priv->source_sel.rendered = FALSE;
1855    #endif
1856    
1857    #ifdef OSD_SCALE
1858            priv->scale.surface =
1859                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1860                                           OSD_SCALE_W, OSD_SCALE_H);
1861            priv->scale.zoom = -1;
1862    #endif
1863    
1864    #ifdef OSD_CROSSHAIR
1865            priv->crosshair.surface =
1866                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1867                                           OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1868            priv->crosshair.rendered = FALSE;
1869    #endif
1870    
1871    #ifdef OSD_COORDINATES
1872            priv->coordinates.surface =
1873                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1874                                           OSD_COORDINATES_W, OSD_COORDINATES_H);
1875    
1876            priv->coordinates.lat = priv->coordinates.lon = OSM_GPS_MAP_INVALID;
1877    #endif
1878    
1879          /* ... and render it */          /* ... and render it */
1880          osd_render(osd);          osd_render(osd);
# Line 699  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1883  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1883      // now draw this onto the original context      // now draw this onto the original context
1884      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1885    
1886      int x = OSD_X, y = OSD_Y;      gint x, y;
1887      if(OSD_X < 0)  
1888          x = osd->widget->allocation.width - OSD_W + OSD_X;  #ifdef OSD_SCALE
1889        x =  OSD_X;
1890        y = -OSD_Y;
1891        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1892        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1893    
1894        cairo_set_source_surface(cr, priv->scale.surface, x, y);
1895        cairo_paint(cr);
1896    #endif
1897    
1898    #ifdef OSD_CROSSHAIR
1899        x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1900        y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1901    
1902        cairo_set_source_surface(cr, priv->crosshair.surface, x, y);
1903        cairo_paint(cr);
1904    #endif
1905    
1906    #ifdef OSD_NAV
1907        if(priv->nav.surface) {
1908            x =  OSD_X;
1909            if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1910            y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1911    
1912            cairo_set_source_surface(cr, priv->nav.surface, x, y);
1913            cairo_paint(cr);
1914        }
1915    #endif
1916    
1917    #ifdef OSD_COORDINATES
1918        x = -OSD_X;
1919        y = -OSD_Y;
1920        if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1921        if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1922    
1923        cairo_set_source_surface(cr, priv->coordinates.surface, x, y);
1924        cairo_paint(cr);
1925    #endif
1926    
1927    #ifdef OSD_BALLOON
1928        if(priv->balloon.surface) {
1929    
1930            /* convert given lat lon into screen coordinates */
1931            gint x, y;
1932            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1933                                          priv->balloon.lat, priv->balloon.lon,
1934                                          &x, &y);
1935    
1936            /* check if balloon needs to be rerendered */
1937            osd_render_balloon(osd);
1938    
1939            cairo_set_source_surface(cr, priv->balloon.surface,
1940                                     x + priv->balloon.offset_x,
1941                                     y + priv->balloon.offset_y);
1942            cairo_paint(cr);
1943        }
1944    #endif
1945    
1946      if(OSD_Y < 0)      x = OSD_X;
1947          y = osd->widget->allocation.height - OSD_H + OSD_Y;      if(x < 0)
1948            x += osd->widget->allocation.width - OSD_W;
1949    
1950        y = OSD_Y;
1951        if(y < 0)
1952            y += osd->widget->allocation.height - OSD_H;
1953    
1954      cairo_set_source_surface(cr, priv->overlay, x, y);      cairo_set_source_surface(cr, priv->controls.surface, x, y);
1955      cairo_paint(cr);      cairo_paint(cr);
1956    
1957  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1958      if(!priv->handler_id) {      if(!priv->source_sel.handler_id) {
1959          /* the OSD source selection is not being animated */          /* the OSD source selection is not being animated */
1960          if(!priv->expanded)          if(!priv->source_sel.expanded)
1961              x = osd->widget->allocation.width - OSD_S_W;              x = osd->widget->allocation.width - OSD_S_W;
1962          else          else
1963              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1964      } else      } else
1965          x = priv->shift;          x = priv->source_sel.shift;
1966    
1967      y = OSD_S_Y;      y = OSD_S_Y;
1968      if(OSD_S_Y < 0) {      if(OSD_S_Y < 0) {
1969          if(!priv->expanded)          if(!priv->source_sel.expanded)
1970              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1971          else          else
1972              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1973      }      }
1974    
1975      cairo_set_source_surface(cr, priv->map_source, x, y);      cairo_set_source_surface(cr, priv->source_sel.surface, x, y);
1976      cairo_paint(cr);      cairo_paint(cr);
1977  #endif  #endif
1978    
1979      cairo_destroy(cr);      cairo_destroy(cr);
1980        FOUT;
1981  }  }
1982    
1983  static void  static void
1984  osd_free(osm_gps_map_osd_t *osd)  osd_free(osm_gps_map_osd_t *osd)
1985  {  {
1986        FIN;
1987      osd_priv_t *priv = (osd_priv_t *)(osd->priv);      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1988    
1989      if(priv->handler_id)      if (priv->controls.surface)
1990          gtk_timeout_remove(priv->handler_id);           cairo_surface_destroy(priv->controls.surface);
1991    
1992      if (priv->overlay)  #ifdef OSD_SOURCE_SEL
1993           cairo_surface_destroy(priv->overlay);      if(priv->source_sel.handler_id)
1994            gtk_timeout_remove(priv->source_sel.handler_id);
1995    
1996        if (priv->source_sel.surface)
1997             cairo_surface_destroy(priv->source_sel.surface);
1998    #endif
1999    
2000      if (priv->map_source)  #ifdef OSD_SCALE
2001           cairo_surface_destroy(priv->map_source);      if (priv->scale.surface)
2002             cairo_surface_destroy(priv->scale.surface);
2003    #endif
2004    
2005    #ifdef OSD_CROSSHAIR
2006        if (priv->crosshair.surface)
2007             cairo_surface_destroy(priv->crosshair.surface);
2008    #endif
2009    
2010    #ifdef OSD_NAV
2011        if (priv->nav.surface)
2012             cairo_surface_destroy(priv->nav.surface);
2013    #endif
2014    
2015    #ifdef OSD_COORDINATES
2016        if (priv->coordinates.surface)
2017             cairo_surface_destroy(priv->coordinates.surface);
2018    #endif
2019    
2020    #ifdef OSD_BALLOON
2021        if (priv->balloon.surface)
2022             cairo_surface_destroy(priv->balloon.surface);
2023    #endif
2024    
2025      g_free(priv);      g_free(priv);
2026        FOUT;
2027    }
2028    
2029    static gboolean
2030    osd_busy(osm_gps_map_osd_t *osd)
2031    {
2032        FIN;
2033    #ifdef OSD_SOURCE_SEL
2034        osd_priv_t *priv = (osd_priv_t *)(osd->priv);
2035        return (priv->source_sel.handler_id != 0);
2036    #else
2037        return FALSE;
2038    #endif
2039    }
2040    
2041    static osd_button_t
2042    osd_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
2043        FIN;
2044        return osd_check_int(osd, TRUE, down, x, y);
2045  }  }
2046    
2047  static osm_gps_map_osd_t osd_classic = {  static osm_gps_map_osd_t osd_classic = {
2048        .widget     = NULL,
2049    
2050      .draw       = osd_draw,      .draw       = osd_draw,
2051      .check      = osd_check,      .check      = osd_check,
2052      .render     = osd_render,      .render     = osd_render,
2053      .free       = osd_free,      .free       = osd_free,
2054        .busy       = osd_busy,
2055    
2056      .cb         = NULL,      .cb         = NULL,
2057      .data       = NULL,      .data       = NULL,
# Line 767  static osm_gps_map_osd_t osd_classic = { Line 2063  static osm_gps_map_osd_t osd_classic = {
2063  void  void
2064  osm_gps_map_osd_classic_init(OsmGpsMap *map)  osm_gps_map_osd_classic_init(OsmGpsMap *map)
2065  {  {
2066        FIN;
2067      osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);      osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
2068    
2069    #ifdef OSD_BALLOON
2070        priv->balloon.lat = OSM_GPS_MAP_INVALID;
2071        priv->balloon.lon = OSM_GPS_MAP_INVALID;
2072    #endif
2073    
2074      osd_classic.priv = priv;      osd_classic.priv = priv;
2075    
2076      osm_gps_map_register_osd(map, &osd_classic);      osm_gps_map_register_osd(map, &osd_classic);
2077        FOUT;
2078  }  }
2079    
2080  #ifdef OSD_GPS_BUTTON  #ifdef OSD_GPS_BUTTON
# Line 779  osm_gps_map_osd_classic_init(OsmGpsMap * Line 2082  osm_gps_map_osd_classic_init(OsmGpsMap *
2082  /* but instead are to be used by the main application */  /* but instead are to be used by the main application */
2083  void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,  void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
2084                                   gpointer data) {                                   gpointer data) {
2085        FIN;
2086      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
2087      g_return_if_fail (osd);      g_return_if_fail (osd);
2088    
# Line 790  void osm_gps_map_osd_enable_gps (OsmGpsM Line 2094  void osm_gps_map_osd_enable_gps (OsmGpsM
2094      osd->render(osd);      osd->render(osd);
2095    
2096      osm_gps_map_redraw(map);      osm_gps_map_redraw(map);
2097        FOUT;
2098  }  }
2099  #endif  #endif
2100    
2101  osd_button_t  osd_button_t
2102  osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {  osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
2103        FIN;
2104      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
2105      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
2106    
2107      return osd_check(osd, x, y);      return osd_check_int(osd, FALSE, TRUE, x, y);
2108  }  }

Legend:
Removed from v.86  
changed lines
  Added in v.150