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

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

revision 94 by harbaum, Thu Sep 3 11:04:05 2009 UTC revision 233 by harbaum, Wed Dec 9 19:45:36 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 <string.h>
23  #include <math.h>    // M_PI/cos()  #include <math.h>    // M_PI/cos()
24    
25  /* parameters that can be overwritten from the config file: */  /* parameters that can be overwritten from the config file: */
# Line 32  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, mode;    // display distance imperial/metric
86            int click_sep;
87        } nav;
88    #endif
89    
90    #ifdef OSD_COORDINATES
91        struct {
92            cairo_surface_t *surface;
93            float lat, lon;
94        } coordinates;
95    #endif
96    
97  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
98      /* values to handle the "source" menu */      struct {
99      cairo_surface_t *map_source;          /* values to handle the "source" menu */
100      gboolean expanded;          cairo_surface_t *surface;
101      gint shift, dir, count;          gboolean expanded;
102      gint handler_id;          gint shift, dir, count;
103      gint width, height;          gint handler_id;
104            gint width, height;
105            gboolean rendered;
106        } source_sel;
107  #endif  #endif
108    
109  } osd_priv_t;  } osd_priv_t;
110    
111    #ifdef OSD_BALLOON
112    /* most visual effects are hardcoded by now, but may be made */
113    /* available via properties later */
114    #ifndef BALLOON_AREA_WIDTH
115    #define BALLOON_AREA_WIDTH           290
116    #endif
117    #ifndef BALLOON_AREA_HEIGHT
118    #define BALLOON_AREA_HEIGHT           75
119    #endif
120    #ifndef BALLOON_CORNER_RADIUS
121    #define BALLOON_CORNER_RADIUS         10
122    #endif
123    
124    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
125    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
126    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
127    #define BALLOON_TRANSPARENCY         0.8
128    #define POINTER_HEIGHT                20
129    #define POINTER_FOOT_WIDTH            20
130    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
131    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
132    #define BALLOON_SHADOW_TRANSPARENCY  0.2
133    
134    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
135    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
136    
137    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
138    
139    /* draw the bubble shape. this is used twice, once for the shape and once */
140    /* for the shadow */
141    static void
142    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
143           gboolean bottom, int px, int py, int px0, int px1) {
144    
145        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
146        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
147                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
148        if(!bottom) {
149            /* insert top pointer */
150            cairo_line_to (cr, px1, y0);
151            cairo_line_to (cr, px, py);
152            cairo_line_to (cr, px0, y0);
153        }
154    
155        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
156        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
157                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
158        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
159        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
160                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
161        if(bottom) {
162            /* insert bottom pointer */
163            cairo_line_to (cr, px0, y1);
164            cairo_line_to (cr, px, py);
165            cairo_line_to (cr, px1, y1);
166        }
167    
168        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
169        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
170                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
171    
172        cairo_close_path (cr);
173    }
174    
175    static void
176    osd_render_balloon(osm_gps_map_osd_t *osd) {
177        osd_priv_t *priv = (osd_priv_t*)osd->priv;
178    
179        if(!priv->balloon.surface)
180            return;
181    
182        /* get zoom */
183        gint zoom;
184        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
185    
186        /* ------- convert given coordinate into screen position --------- */
187        gint xs, ys;
188        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
189                                          priv->balloon.lat, priv->balloon.lon,
190                                          &xs, &ys);
191    
192        gint x0 = 1, y0 = 1;
193    
194        /* check position of this relative to screen center to determine */
195        /* pointer direction ... */
196        int pointer_x, pointer_x0, pointer_x1;
197        int pointer_y;
198    
199        /* ... and calculate position */
200        int orientation = 0;
201        if(xs > osd->widget->allocation.width/2) {
202            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
203            pointer_x = x0 - priv->balloon.offset_x;
204            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
205            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
206            orientation |= 1;
207        } else {
208            priv->balloon.offset_x = -POINTER_OFFSET;
209            pointer_x = x0 - priv->balloon.offset_x;
210            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
211            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
212        }
213    
214        gboolean bottom = FALSE;
215        if(ys > osd->widget->allocation.height/2) {
216            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
217            pointer_y = y0 - priv->balloon.offset_y;
218            bottom = TRUE;
219            orientation |= 2;
220        } else {
221            priv->balloon.offset_y = 0;
222            pointer_y = y0 - priv->balloon.offset_y;
223            y0 += POINTER_HEIGHT;
224        }
225    
226        /* if required orientation equals current one, then don't render */
227        /* anything */
228        if(orientation == priv->balloon.orientation)
229            return;
230    
231        priv->balloon.orientation = orientation;
232    
233        /* calculate bottom/right of box */
234        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
235    
236        /* save balloon screen coordinates for later use */
237        priv->balloon.rect.x = x0 + BALLOON_BORDER;
238        priv->balloon.rect.y = y0 + BALLOON_BORDER;
239        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
240        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
241    
242        g_assert(priv->balloon.surface);
243        cairo_t *cr = cairo_create(priv->balloon.surface);
244        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
245        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
246        cairo_paint(cr);
247        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
248    
249        /* --------- draw shadow --------------- */
250        osm_gps_map_draw_balloon_shape (cr,
251                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
252                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
253                     bottom, pointer_x, pointer_y,
254                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
255    
256        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
257        cairo_fill_preserve (cr);
258        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
259        cairo_set_line_width (cr, 0);
260        cairo_stroke (cr);
261    
262        /* --------- draw main shape ----------- */
263        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
264                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
265    
266        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
267        cairo_fill_preserve (cr);
268        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
269        cairo_set_line_width (cr, 1);
270        cairo_stroke (cr);
271    
272        if (priv->balloon.cb) {
273            osm_gps_map_balloon_event_t event;
274    
275            /* clip in case application tries to draw in */
276                /* exceed of the balloon */
277            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
278                             priv->balloon.rect.w, priv->balloon.rect.h);
279            cairo_clip (cr);
280            cairo_new_path (cr);  /* current path is not consumed by cairo_clip */
281    
282            /* request the application to draw the balloon contents */
283            event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_DRAW;
284            event.data.draw.rect = &priv->balloon.rect;
285            event.data.draw.cr = cr;
286    
287            priv->balloon.cb(&event, priv->balloon.data);
288        }
289    
290        cairo_destroy(cr);
291    }
292    
293    /* return true if balloon is being displayed and if */
294    /* the given coordinate is within this balloon */
295    static gboolean
296    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean click, gboolean down, gint x, gint y)
297    {
298        osd_priv_t *priv = (osd_priv_t*)osd->priv;
299    
300        if(!priv->balloon.surface)
301            return FALSE;
302    
303        gint xs, ys;
304        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
305                                          priv->balloon.lat, priv->balloon.lon,
306                                          &xs, &ys);
307    
308        xs += priv->balloon.rect.x + priv->balloon.offset_x;
309        ys += priv->balloon.rect.y + priv->balloon.offset_y;
310    
311        gboolean is_in =
312            (x > xs) && (x < xs + priv->balloon.rect.w) &&
313            (y > ys) && (y < ys + priv->balloon.rect.h);
314    
315        /* is this a real click or is the application just checking for something? */
316        if(click) {
317    
318            /* handle the fact that the balloon may have been created by the */
319            /* button down event */
320            if(!is_in && !down && !priv->balloon.just_created) {
321                /* the user actually clicked outside the balloon */
322    
323                /* close the balloon! */
324                osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
325    
326                /* and inform application about this */
327                if(priv->balloon.cb) {
328                    osm_gps_map_balloon_event_t event;
329                    event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_REMOVED;
330                    priv->balloon.cb(&event, priv->balloon.data);
331                }
332    
333            }
334    
335            if(is_in && priv->balloon.cb) {
336                osm_gps_map_balloon_event_t event;
337    
338                /* notify application of click */
339                event.type = OSM_GPS_MAP_BALLOON_EVENT_TYPE_CLICK;
340                event.data.click.x = x - xs;
341                event.data.click.y = y - ys;
342                event.data.click.down = down;
343    
344                priv->balloon.cb(&event, priv->balloon.data);
345            }
346        }
347        return is_in;
348    }
349    
350    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
351        g_return_if_fail (OSM_IS_GPS_MAP (map));
352    
353        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
354        g_return_if_fail (osd);
355    
356        osd_priv_t *priv = (osd_priv_t*)osd->priv;
357        g_return_if_fail (priv);
358    
359        if(priv->balloon.surface) {
360            cairo_surface_destroy(priv->balloon.surface);
361            priv->balloon.surface = NULL;
362            priv->balloon.lat = OSM_GPS_MAP_INVALID;
363            priv->balloon.lon = OSM_GPS_MAP_INVALID;
364        }
365        osm_gps_map_redraw(map);
366    }
367    
368    void
369    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
370                                  OsmGpsMapBalloonCallback cb, gpointer data) {
371        g_return_if_fail (OSM_IS_GPS_MAP (map));
372    
373        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
374        g_return_if_fail (osd);
375    
376        osd_priv_t *priv = (osd_priv_t*)osd->priv;
377        g_return_if_fail (priv);
378    
379        osm_gps_map_osd_clear_balloon (map);
380    
381        /* allocate balloon surface */
382        priv->balloon.surface =
383            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
384                                       BALLOON_W+2, BALLOON_H+2);
385    
386        priv->balloon.lat = latitude;
387        priv->balloon.lon = longitude;
388        priv->balloon.cb = cb;
389        priv->balloon.data = data;
390        priv->balloon.just_created = TRUE;
391    
392        priv->balloon.orientation = -1;
393    
394        osd_render_balloon(osd);
395    
396        osm_gps_map_redraw(map);
397    }
398    
399    #endif // OSD_BALLOON
400    
401  /* position and extent of bounding box */  /* position and extent of bounding box */
402  #ifndef OSD_X  #ifndef OSD_X
403  #define OSD_X      (10)  #define OSD_X      (10)
# Line 272  osd_check_zoom(gint x, gint y) { Line 620  osd_check_zoom(gint x, gint y) {
620  #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)  #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
621    
622  /* size of usable area when expanded */  /* size of usable area when expanded */
623  #define OSD_S_AREA_W (priv->width)  #define OSD_S_AREA_W (priv->source_sel.width)
624  #define OSD_S_AREA_H (priv->height)  #define OSD_S_AREA_H (priv->source_sel.height)
625  #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)
626  #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)  #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
627    
628  /* internal value to draw the arrow on the "puller" */  /* internal value to draw the arrow on the "puller" */
629  #define OSD_S_D0  (OSD_S_RAD/2)  #define OSD_S_D0  (OSD_S_RAD/2)
630  #ifndef OSD_FONT_SIZE  #ifndef OSD_FONT_SIZE
631  #define OSD_FONT_SIZE 16.0  #define OSD_FONT_SIZE (16.0)
632  #endif  #endif
633  #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)  #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)
634  #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)  #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)
# Line 289  osd_check_zoom(gint x, gint y) { Line 637  osd_check_zoom(gint x, gint y) {
637  /* or the entire menu incl. the puller (expanded) */  /* or the entire menu incl. the puller (expanded) */
638  static void  static void
639  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) {
640      if(!priv->expanded) {      if(!priv->source_sel.expanded) {
641          /* just draw the puller */          /* just draw the puller */
642          cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);          cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
643          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 317  osd_source_content(osm_gps_map_osd_t *os Line 665  osd_source_content(osm_gps_map_osd_t *os
665    
666      int py = offset + OSD_S_RAD - OSD_S_D0;      int py = offset + OSD_S_RAD - OSD_S_D0;
667    
668      if(!priv->expanded) {      if(!priv->source_sel.expanded) {
669          /* draw the "puller" open (<) arrow */          /* draw the "puller" open (<) arrow */
670          cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);          cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
671          cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);          cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
# Line 342  osd_source_content(osm_gps_map_osd_t *os Line 690  osd_source_content(osm_gps_map_osd_t *os
690                                      CAIRO_FONT_WEIGHT_BOLD);                                      CAIRO_FONT_WEIGHT_BOLD);
691              cairo_set_font_size (cr, OSD_FONT_SIZE);              cairo_set_font_size (cr, OSD_FONT_SIZE);
692    
693              int i, step = (priv->height - 2*OSD_TEXT_BORDER) /              int i, step = (priv->source_sel.height - 2*OSD_TEXT_BORDER) /
694                  OSM_GPS_MAP_SOURCE_LAST;                  OSM_GPS_MAP_SOURCE_LAST;
695              for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {              for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
696                  cairo_text_extents_t extents;                  cairo_text_extents_t extents;
# Line 356  osd_source_content(osm_gps_map_osd_t *os Line 704  osd_source_content(osm_gps_map_osd_t *os
704                  if(source == i) {                  if(source == i) {
705                      cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,                      cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
706                                      y - OSD_TEXT_SKIP,                                      y - OSD_TEXT_SKIP,
707                                      priv->width - OSD_TEXT_BORDER,                                      priv->source_sel.width - OSD_TEXT_BORDER,
708                                      step + OSD_TEXT_SKIP);                                      step + OSD_TEXT_SKIP);
709                      cairo_fill(cr);                      cairo_fill(cr);
710    
# Line 387  osd_source_content(osm_gps_map_osd_t *os Line 735  osd_source_content(osm_gps_map_osd_t *os
735  }  }
736    
737  static void  static void
738  osd_render_source_sel(osm_gps_map_osd_t *osd) {  osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
739      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
740    
741        if(!priv->source_sel.surface ||
742           (priv->source_sel.rendered && !force_rerender))
743            return;
744    
745        priv->source_sel.rendered = TRUE;
746    
747  #ifndef OSD_COLOR  #ifndef OSD_COLOR
748      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
749      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
# Line 397  osd_render_source_sel(osm_gps_map_osd_t Line 751  osd_render_source_sel(osm_gps_map_osd_t
751  #endif  #endif
752    
753      /* draw source selector */      /* draw source selector */
754      cairo_t *cr = cairo_create(priv->map_source);      g_assert(priv->source_sel.surface);
755        cairo_t *cr = cairo_create(priv->source_sel.surface);
756      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
757      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);
758      cairo_paint(cr);      cairo_paint(cr);
# Line 438  osd_source_reallocate(osm_gps_map_osd_t Line 793  osd_source_reallocate(osm_gps_map_osd_t
793      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
794    
795      /* re-allocate offscreen bitmap */      /* re-allocate offscreen bitmap */
796      g_assert (priv->map_source);      g_assert (priv->source_sel.surface);
797    
798      int w = OSD_S_W, h = OSD_S_H;      int w = OSD_S_W, h = OSD_S_H;
799      if(priv->expanded) {      if(priv->source_sel.expanded) {
         /* ... and right of it the waypoint id */  
800          cairo_text_extents_t extents;          cairo_text_extents_t extents;
801    
802          /* determine content size */          /* determine content size */
803          cairo_t *cr = cairo_create(priv->map_source);          g_assert(priv->source_sel.surface);
804            cairo_t *cr = cairo_create(priv->source_sel.surface);
805          cairo_select_font_face (cr, "Sans",          cairo_select_font_face (cr, "Sans",
806                                  CAIRO_FONT_SLANT_NORMAL,                                  CAIRO_FONT_SLANT_NORMAL,
807                                  CAIRO_FONT_WEIGHT_BOLD);                                  CAIRO_FONT_WEIGHT_BOLD);
# Line 463  osd_source_reallocate(osm_gps_map_osd_t Line 818  osd_source_reallocate(osm_gps_map_osd_t
818          }          }
819          cairo_destroy(cr);          cairo_destroy(cr);
820    
821          priv->width  = max_w + 2*OSD_TEXT_BORDER;          priv->source_sel.width  = max_w + 2*OSD_TEXT_BORDER;
822          priv->height = OSM_GPS_MAP_SOURCE_LAST *          priv->source_sel.height = OSM_GPS_MAP_SOURCE_LAST *
823              (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;              (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
824    
825          w = OSD_S_EXP_W;          w = OSD_S_EXP_W;
826          h = OSD_S_EXP_H;          h = OSD_S_EXP_H;
827      }      }
828    
829      cairo_surface_destroy(priv->map_source);      cairo_surface_destroy(priv->source_sel.surface);
830      priv->map_source =      priv->source_sel.surface =
831          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
832    
833      osd_render_source_sel(osd);      osd_render_source_sel(osd, TRUE);
   
834  }  }
835    
836  #define OSD_HZ      15  #define OSD_HZ      15
# Line 487  static gboolean osd_source_animate(gpoin Line 841  static gboolean osd_source_animate(gpoin
841      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
842      int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;      int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
843      gboolean done = FALSE;      gboolean done = FALSE;
844      priv->count += priv->dir;      priv->source_sel.count += priv->source_sel.dir;
845    
846      /* shifting in */      /* shifting in */
847      if(priv->dir < 0) {      if(priv->source_sel.dir < 0) {
848          if(priv->count <= 0) {          if(priv->source_sel.count <= 0) {
849              priv->count = 0;              priv->source_sel.count = 0;
850              done = TRUE;              done = TRUE;
851          }          }
852      } else {      } else {
853          if(priv->count >= 1000) {          if(priv->source_sel.count >= 1000) {
854              priv->expanded = FALSE;              priv->source_sel.expanded = FALSE;
855              osd_source_reallocate(osd);              osd_source_reallocate(osd);
856    
857              priv->count = 1000;              priv->source_sel.count = 1000;
858              done = TRUE;              done = TRUE;
859          }          }
860      }      }
# Line 508  static gboolean osd_source_animate(gpoin Line 862  static gboolean osd_source_animate(gpoin
862    
863      /* 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 */
864    
865      /* nicer sinoid mapping */      /* nice sinoid mapping */
866      float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;      float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;
867      priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +      priv->source_sel.shift =
868            (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
869          m * diff;          m * diff;
870    
871        /* make sure the screen is updated */
872      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
873    
874        /* stop animation if done */
875      if(done)      if(done)
876          priv->handler_id = 0;          priv->source_sel.handler_id = 0;
877    
878      return !done;      return !done;
879  }  }
# Line 528  osd_source_toggle(osm_gps_map_osd_t *osd Line 885  osd_source_toggle(osm_gps_map_osd_t *osd
885      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
886    
887      /* ignore clicks while animation is running */      /* ignore clicks while animation is running */
888      if(priv->handler_id)      if(priv->source_sel.handler_id)
889          return;          return;
890    
891      /* expand immediately, collapse is handle at the end of the collapse animation */      /* expand immediately, collapse is handle at the end of the */
892      if(!priv->expanded) {      /* collapse animation */
893          priv->expanded = TRUE;      if(!priv->source_sel.expanded) {
894            priv->source_sel.expanded = TRUE;
895          osd_source_reallocate(osd);          osd_source_reallocate(osd);
896    
897          priv->count = 1000;          priv->source_sel.count = 1000;
898          priv->shift = osd->widget->allocation.width - OSD_S_W;          priv->source_sel.shift = osd->widget->allocation.width - OSD_S_W;
899          priv->dir = -1000/OSD_HZ;          priv->source_sel.dir = -1000/OSD_HZ;
900      } else {      } else {
901          priv->count =  0;          priv->source_sel.count =  0;
902          priv->shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;          priv->source_sel.shift = osd->widget->allocation.width -
903          priv->dir = +1000/OSD_HZ;              OSD_S_EXP_W + OSD_S_X;
904            priv->source_sel.dir = +1000/OSD_HZ;
905      }      }
906    
907      priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);      /* start timer to handle animation */
908        priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
909                                                      osd_source_animate, osd);
910  }  }
911    
912  /* check if the user clicked inside the source selection area */  /* check if the user clicked inside the source selection area */
913  static osd_button_t  static osd_button_t
914  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) {
915      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
916    
917      if(!priv->expanded)      if(!priv->source_sel.expanded)
918          x -= osd->widget->allocation.width - OSD_S_W;          x -= osd->widget->allocation.width - OSD_S_W;
919      else      else
920          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 568  osd_source_check(osm_gps_map_osd_t *osd, Line 929  osd_source_check(osm_gps_map_osd_t *osd,
929          /* really within puller shape? */          /* really within puller shape? */
930          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)) {
931              /* expand source selector */              /* expand source selector */
932              osd_source_toggle(osd);              if(down)
933                    osd_source_toggle(osd);
934    
935              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
936              /* of the OSD */              /* of the OSD */
# Line 577  osd_source_check(osm_gps_map_osd_t *osd, Line 939  osd_source_check(osm_gps_map_osd_t *osd,
939      }      }
940    
941      /* check for clicks into data area */      /* check for clicks into data area */
942      if(priv->expanded && !priv->handler_id) {      if(priv->source_sel.expanded && !priv->source_sel.handler_id) {
943          /* re-adjust from puller top to content top */          /* re-adjust from puller top to content top */
944          if(OSD_S_Y < 0)          if(OSD_S_Y < 0)
945              y += OSD_S_EXP_H - OSD_S_PH;              y += OSD_S_EXP_H - OSD_S_PH;
# Line 587  osd_source_check(osm_gps_map_osd_t *osd, Line 949  osd_source_check(osm_gps_map_osd_t *osd,
949             y > 0 &&             y > 0 &&
950             y < OSD_S_EXP_H) {             y < OSD_S_EXP_H) {
951    
952              int step = (priv->height - 2*OSD_TEXT_BORDER)              int step = (priv->source_sel.height - 2*OSD_TEXT_BORDER)
953                  / OSM_GPS_MAP_SOURCE_LAST;                  / OSM_GPS_MAP_SOURCE_LAST;
954    
955              y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;              y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
956              y /= step;              y /= step;
957              y += 1;              y += 1;
958    
959              gint old = 0;              if(down) {
960              g_object_get(osd->widget, "map-source", &old, NULL);                  gint old = 0;
961                    g_object_get(osd->widget, "map-source", &old, NULL);
962              if(y > OSM_GPS_MAP_SOURCE_NULL &&  
963                 y <= OSM_GPS_MAP_SOURCE_LAST &&                  if(y > OSM_GPS_MAP_SOURCE_NULL &&
964                 old != y) {                     y <= OSM_GPS_MAP_SOURCE_LAST &&
965                  g_object_set(osd->widget, "map-source", y, NULL);                     old != y) {
966                        g_object_set(osd->widget, "map-source", y, NULL);
967                  osd_render_source_sel(osd);  
968                  osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));                      osd_render_source_sel(osd, TRUE);
969                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
970                        osm_gps_map_redraw(OSM_GPS_MAP(osd->widget));
971                    }
972              }              }
973    
974              /* return "clicked in OSD background" to prevent further */              /* return "clicked in OSD background" to prevent further */
975              /* processing by application */              /* processing by application */
976              return OSD_BG;              return OSD_BG;
# Line 616  osd_source_check(osm_gps_map_osd_t *osd, Line 981  osd_source_check(osm_gps_map_osd_t *osd,
981  }  }
982  #endif // OSD_SOURCE_SEL  #endif // OSD_SOURCE_SEL
983    
 static osd_button_t  
 osd_check(osm_gps_map_osd_t *osd, gint x, gint y) {  
     osd_button_t but = OSD_NONE;  
   
 #ifdef OSD_SOURCE_SEL  
     /* the source selection area is handles internally */  
     but = osd_source_check(osd, x, y);  
     if(but != OSD_NONE)  
         return but;  
 #endif  
   
     x -= OSD_X;  
     y -= OSD_Y;  
   
     if(OSD_X < 0)  
         x -= (osd->widget->allocation.width - OSD_W);  
   
     if(OSD_Y < 0)  
         y -= (osd->widget->allocation.height - OSD_H);  
   
     /* first do a rough test for the OSD area. */  
     /* this is just to avoid an unnecessary detailed test */  
     if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {  
 #ifndef OSD_NO_DPAD  
         but = osd_check_dpad(x, y);  
 #endif  
   
         if(but == OSD_NONE)  
             but = osd_check_zoom(x, y);  
     }  
   
     return but;  
 }  
   
984  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
985  static void  static void
986  osd_dpad_labels(cairo_t *cr, gint x, gint y) {  osd_dpad_labels(cairo_t *cr, gint x, gint y) {
# Line 720  osd_zoom_labels(cairo_t *cr, gint x, gin Line 1051  osd_zoom_labels(cairo_t *cr, gint x, gin
1051      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
1052  }  }
1053    
1054    #ifdef OSD_COORDINATES
1055    
1056    #ifndef OSD_COORDINATES_FONT_SIZE
1057    #define OSD_COORDINATES_FONT_SIZE (12.0)
1058    #endif
1059    
1060    #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
1061    
1062    #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1063    #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET+OSD_COORDINATES_FONT_SIZE/4)
1064    
1065    /* these can be overwritten with versions that support */
1066    /* localization */
1067    #ifndef OSD_COORDINATES_CHR_N
1068    #define OSD_COORDINATES_CHR_N  "N"
1069    #endif
1070    #ifndef OSD_COORDINATES_CHR_S
1071    #define OSD_COORDINATES_CHR_S  "S"
1072    #endif
1073    #ifndef OSD_COORDINATES_CHR_E
1074    #define OSD_COORDINATES_CHR_E  "E"
1075    #endif
1076    #ifndef OSD_COORDINATES_CHR_W
1077    #define OSD_COORDINATES_CHR_W  "W"
1078    #endif
1079    
1080    
1081    
1082    /* this is the classic geocaching notation */
1083    static char
1084    *osd_latitude_str(float latitude) {
1085        char *c = OSD_COORDINATES_CHR_N;
1086        float integral, fractional;
1087    
1088        if(isnan(latitude))
1089            return NULL;
1090    
1091        if(latitude < 0) {
1092            latitude = fabs(latitude);
1093            c = OSD_COORDINATES_CHR_S;
1094        }
1095    
1096        fractional = modff(latitude, &integral);
1097    
1098        return g_strdup_printf("%s %02d° %06.3f'",
1099                               c, (int)integral, fractional*60.0);
1100    }
1101    
1102    static char
1103    *osd_longitude_str(float longitude) {
1104        char *c = OSD_COORDINATES_CHR_E;
1105        float integral, fractional;
1106    
1107        if(isnan(longitude))
1108            return NULL;
1109    
1110        if(longitude < 0) {
1111            longitude = fabs(longitude);
1112            c = OSD_COORDINATES_CHR_W;
1113        }
1114    
1115        fractional = modff(longitude, &integral);
1116    
1117        return g_strdup_printf("%s %03d° %06.3f'",
1118                               c, (int)integral, fractional*60.0);
1119    }
1120    
1121    /* render a string at the given screen position */
1122    static int
1123    osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1124        if(!text) return y;
1125    
1126        char *p = g_malloc(strlen(text)+4);  // space for "...\n"
1127        strcpy(p, text);
1128    
1129        cairo_text_extents_t extents;
1130        memset(&extents, 0, sizeof(cairo_text_extents_t));
1131        cairo_text_extents (cr, p, &extents);
1132        g_assert(extents.width != 0.0);
1133    
1134        /* check if text needs to be truncated */
1135        int trunc_at = strlen(text);
1136        while(extents.width > width) {
1137    
1138            /* cut off all utf8 multibyte remains so the actual */
1139            /* truncation only deals with one byte */
1140            while((p[trunc_at-1] & 0xc0) == 0x80) {
1141                trunc_at--;
1142                g_assert(trunc_at > 0);
1143            }
1144    
1145            trunc_at--;
1146            g_assert(trunc_at > 0);
1147    
1148            strcpy(p+trunc_at, "...");
1149            cairo_text_extents (cr, p, &extents);
1150        }
1151    
1152        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1153        cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1154        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1155        cairo_text_path (cr, p);
1156        cairo_stroke (cr);
1157    
1158        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1159        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1160        cairo_show_text (cr, p);
1161    
1162        g_free(p);
1163    
1164        /* skip + 1/5 line */
1165        return y + 6*OSD_COORDINATES_FONT_SIZE/5;
1166    }
1167    
1168    static void
1169    osd_render_coordinates(osm_gps_map_osd_t *osd)
1170    {
1171        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1172    
1173        if(!priv->coordinates.surface)
1174            return;
1175    
1176        /* get current map position */
1177        gfloat lat, lon;
1178        g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1179    
1180        /* check if position has changed enough to require redraw */
1181        if(!isnan(priv->coordinates.lat) && !isnan(priv->coordinates.lon))
1182            /* 1/60000 == 1/1000 minute */
1183            if((fabsf(lat - priv->coordinates.lat) < 1/60000) &&
1184               (fabsf(lon - priv->coordinates.lon) < 1/60000))
1185                return;
1186    
1187        priv->coordinates.lat = lat;
1188        priv->coordinates.lon = lon;
1189    
1190        /* first fill with transparency */
1191    
1192        g_assert(priv->coordinates.surface);
1193        cairo_t *cr = cairo_create(priv->coordinates.surface);
1194        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1195        //    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1196        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1197        cairo_paint(cr);
1198        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1199    
1200        cairo_select_font_face (cr, "Sans",
1201                                CAIRO_FONT_SLANT_NORMAL,
1202                                CAIRO_FONT_WEIGHT_BOLD);
1203        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1204    
1205        char *latitude = osd_latitude_str(lat);
1206        char *longitude = osd_longitude_str(lon);
1207    
1208        int y = OSD_COORDINATES_OFFSET;
1209        y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1210        y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1211    
1212        g_free(latitude);
1213        g_free(longitude);
1214    
1215        cairo_destroy(cr);
1216    }
1217    #endif  // OSD_COORDINATES
1218    
1219    #ifdef OSD_NAV
1220    #define OSD_NAV_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1221    #define OSD_NAV_H  (11*OSD_COORDINATES_FONT_SIZE)
1222    
1223    /* http://mathforum.org/library/drmath/view/55417.html */
1224    static float get_bearing(float lat1, float lon1, float lat2, float lon2) {
1225      return atan2( sin(lon2 - lon1) * cos(lat2),
1226                    cos(lat1) * sin(lat2) -
1227                    sin(lat1) * cos(lat2) * cos(lon2 - lon1));
1228    }
1229    
1230    /* http://mathforum.org/library/drmath/view/51722.html */
1231    static float get_distance(float lat1, float lon1, float lat2, float lon2) {
1232      float aob = acos(cos(lat1) * cos(lat2) * cos(lon2 - lon1) +
1233                       sin(lat1) * sin(lat2));
1234    
1235      //  return(aob * 3959.0);   /* great circle radius in miles */
1236    
1237      return(aob * 6371000.0);     /* great circle radius in meters */
1238    }
1239    
1240    static void
1241    osd_render_nav(osm_gps_map_osd_t *osd)
1242    {
1243        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1244    
1245        if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1246            return;
1247    
1248        /* first fill with transparency */
1249        g_assert(priv->nav.surface);
1250        cairo_t *cr = cairo_create(priv->nav.surface);
1251        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1252        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1253        cairo_paint(cr);
1254        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1255    
1256        cairo_select_font_face (cr, "Sans",
1257                                CAIRO_FONT_SLANT_NORMAL,
1258                                CAIRO_FONT_WEIGHT_BOLD);
1259        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1260    
1261        char *latitude = osd_latitude_str(priv->nav.lat);
1262        char *longitude = osd_longitude_str(priv->nav.lon);
1263    
1264        int y = OSD_COORDINATES_OFFSET;
1265        y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1266        y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1267        y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1268    
1269        g_free(latitude);
1270        g_free(longitude);
1271    
1272        /* everything below this is the compass. we need to know this */
1273        /* to differ between coordinate clicks and compass clicks */
1274        priv->nav.click_sep = y;
1275    
1276        /* draw the compass */
1277        int radius = (OSD_NAV_H - y - 5*OSD_COORDINATES_FONT_SIZE/4)/2;
1278        if(radius > OSD_NAV_W/2)
1279            radius = OSD_NAV_W/2;
1280    
1281        int x = OSD_NAV_W/2+1;
1282        y += radius;
1283    
1284        cairo_stroke (cr);
1285    
1286        /* draw background */
1287        cairo_arc(cr, x, y, radius, 0,  2*M_PI);
1288        cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
1289        cairo_fill_preserve (cr);
1290        cairo_set_source_rgb (cr, 0, 0, 0);
1291        cairo_set_line_width (cr, 1);
1292        cairo_stroke (cr);
1293    
1294        /* draw pointer */
1295    #define ARROW_WIDTH     0.3
1296    #define ARROW_LENGTH    0.7
1297    
1298        coord_t mpos, *pos = osm_gps_map_get_gps (OSM_GPS_MAP(osd->widget));
1299        if(priv->nav.mode) {
1300            gfloat lat, lon;
1301            g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1302            mpos.rlat = deg2rad(lat);
1303            mpos.rlon = deg2rad(lon);
1304            pos = &mpos;
1305        }
1306    
1307        if(pos) {
1308            float arot = get_bearing(pos->rlat, pos->rlon,
1309                         deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1310    
1311            cairo_move_to(cr,
1312                          x + radius *  ARROW_LENGTH *  sin(arot),
1313                          y + radius *  ARROW_LENGTH * -cos(arot));
1314    
1315            cairo_line_to(cr,
1316                          x + radius * -ARROW_LENGTH *  sin(arot+ARROW_WIDTH),
1317                          y + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH));
1318    
1319            cairo_line_to(cr,
1320                          x + radius * -0.5 * ARROW_LENGTH *  sin(arot),
1321                          y + radius * -0.5 * ARROW_LENGTH * -cos(arot));
1322    
1323            cairo_line_to(cr,
1324                          x + radius * -ARROW_LENGTH *  sin(arot-ARROW_WIDTH),
1325                          y + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH));
1326    
1327            cairo_close_path(cr);
1328            if(priv->nav.mode)
1329                cairo_set_source_rgb (cr, 0, 0, 0);
1330            else
1331                cairo_set_source_rgb (cr, 0, 0, 0.8);
1332    
1333            cairo_fill (cr);
1334    
1335            y += radius + OSD_COORDINATES_FONT_SIZE/4;
1336    
1337            float dist = get_distance(pos->rlat, pos->rlon,
1338                            deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1339    
1340            char *dist_str = NULL;
1341            if(!priv->nav.imperial) {
1342                /* metric is easy ... */
1343                if(dist<1000)
1344                    dist_str = g_strdup_printf("%u m", (int)dist);
1345                else
1346                    dist_str = g_strdup_printf("%.1f km", dist/1000);
1347            } else {
1348                /* and now the hard part: scale for useful imperial values :-( */
1349                /* try to convert to feet, 1ft == 0.3048 m */
1350    
1351                if(dist/(3*0.3048) >= 1760.0)      /* more than 1760 yard? */
1352                    dist_str = g_strdup_printf("%.1f mi", dist/(0.3048*3*1760.0));
1353                else if(dist/0.3048 >= 100)        /* more than 100 feet? */
1354                    dist_str = g_strdup_printf("%.1f yd", dist/(0.3048*3));
1355                else
1356                    dist_str = g_strdup_printf("%.0f ft", dist/0.3048);
1357            }
1358    
1359            y = osd_render_centered_text(cr, y, OSD_NAV_W, dist_str);
1360            g_free(dist_str);
1361        }
1362    
1363        cairo_destroy(cr);
1364    }
1365    
1366    /* check if the user clicked inside the source selection area */
1367    static osd_button_t
1368    osd_nav_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
1369        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1370    
1371        if(!priv->nav.surface || down)
1372            return OSD_NONE;
1373    
1374        x -= OSD_X;
1375        if(OSD_X < 0)
1376            x -= (osd->widget->allocation.width - OSD_NAV_W);
1377    
1378        y -= (osd->widget->allocation.height - OSD_NAV_H)/2;
1379    
1380        if(x >= 0 && y >= 0 && x <= OSD_NAV_W && y <= OSD_NAV_H) {
1381            if(y < priv->nav.click_sep)
1382                osm_gps_map_set_center(OSM_GPS_MAP(osd->widget),
1383                                       priv->nav.lat, priv->nav.lon);
1384            else {
1385                priv->nav.mode = !priv->nav.mode;
1386                osm_gps_map_redraw(OSM_GPS_MAP(osd->widget));
1387            }
1388        }
1389    
1390        return OSD_NONE;
1391    }
1392    
1393    void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1394        g_return_if_fail (OSM_IS_GPS_MAP (map));
1395    
1396        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1397        g_return_if_fail (osd);
1398    
1399        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1400        g_return_if_fail (priv);
1401    
1402        if(priv->nav.surface) {
1403            cairo_surface_destroy(priv->nav.surface);
1404            priv->nav.surface = NULL;
1405            priv->nav.lat = OSM_GPS_MAP_INVALID;
1406            priv->nav.lon = OSM_GPS_MAP_INVALID;
1407            if(priv->nav.name) g_free(priv->nav.name);
1408        }
1409        osm_gps_map_redraw(map);
1410    }
1411    
1412    void
1413    osm_gps_map_osd_draw_nav (OsmGpsMap *map, gboolean imperial,
1414                              float latitude, float longitude, char *name) {
1415        g_return_if_fail (OSM_IS_GPS_MAP (map));
1416    
1417        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1418        g_return_if_fail (osd);
1419    
1420        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1421        g_return_if_fail (priv);
1422    
1423        osm_gps_map_osd_clear_nav (map);
1424    
1425        /* allocate balloon surface */
1426        priv->nav.surface =
1427            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1428                                       OSD_NAV_W+2, OSD_NAV_H+2);
1429    
1430        priv->nav.lat = latitude;
1431        priv->nav.lon = longitude;
1432        priv->nav.name = g_strdup(name);
1433        priv->nav.imperial = imperial;
1434    
1435        osd_render_nav(osd);
1436    
1437        osm_gps_map_redraw(map);
1438    }
1439    
1440    #endif // OSD_NAV
1441    
1442    static osd_button_t
1443    osd_check_int(osm_gps_map_osd_t *osd, gboolean click, gboolean down, gint x, gint y) {
1444        osd_button_t but = OSD_NONE;
1445    
1446    #ifdef OSD_BALLOON
1447        if(down) {
1448            /* needed to handle balloons that are created at click */
1449            osd_priv_t *priv = (osd_priv_t*)osd->priv;
1450            priv->balloon.just_created = FALSE;
1451        }
1452    #endif
1453    
1454    #ifdef OSD_SOURCE_SEL
1455        /* the source selection area is handles internally */
1456        but = osd_source_check(osd, down, x, y);
1457    #endif
1458    
1459    #ifdef OSD_NAV
1460        if(but == OSD_NONE) {
1461            /* the source selection area is handles internally */
1462            but = osd_nav_check(osd, down, x, y);
1463        }
1464    #endif
1465    
1466        if(but == OSD_NONE) {
1467            gint mx = x - OSD_X;
1468            gint my = y - OSD_Y;
1469    
1470            if(OSD_X < 0)
1471                mx -= (osd->widget->allocation.width - OSD_W);
1472    
1473            if(OSD_Y < 0)
1474                my -= (osd->widget->allocation.height - OSD_H);
1475    
1476            /* first do a rough test for the OSD area. */
1477            /* this is just to avoid an unnecessary detailed test */
1478            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
1479    #ifndef OSD_NO_DPAD
1480                but = osd_check_dpad(mx, my);
1481    #endif
1482            }
1483    
1484            if(but == OSD_NONE)
1485                but = osd_check_zoom(mx, my);
1486        }
1487    
1488    #ifdef OSD_BALLOON
1489        if(but == OSD_NONE) {
1490            /* check if user clicked into balloon */
1491            if(osd_balloon_check(osd, click, down, x, y))
1492                but = OSD_BG;
1493        }
1494    #endif
1495    
1496        return but;
1497    }
1498    
1499    
1500    #ifdef OSD_CROSSHAIR
1501    
1502    #ifndef OSD_CROSSHAIR_RADIUS
1503    #define OSD_CROSSHAIR_RADIUS 10
1504    #endif
1505    
1506    #define OSD_CROSSHAIR_TICK  (OSD_CROSSHAIR_RADIUS/2)
1507    #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
1508    #define OSD_CROSSHAIR_W  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1509    #define OSD_CROSSHAIR_H  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1510    
1511    static void
1512    osd_render_crosshair_shape(cairo_t *cr) {
1513        cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
1514                   OSD_CROSSHAIR_RADIUS, 0,  2*M_PI);
1515    
1516        cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
1517                       OSD_CROSSHAIR_H/2);
1518        cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
1519        cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
1520                       OSD_CROSSHAIR_H/2);
1521        cairo_rel_line_to (cr,  OSD_CROSSHAIR_TICK, 0);
1522    
1523        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1524                       OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
1525        cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
1526        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1527                       OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
1528        cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
1529    
1530        cairo_stroke (cr);
1531    }
1532    
1533    static void
1534    osd_render_crosshair(osm_gps_map_osd_t *osd)
1535    {
1536        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1537    
1538        if(!priv->crosshair.surface || priv->crosshair.rendered)
1539            return;
1540    
1541        priv->crosshair.rendered = TRUE;
1542    
1543        /* first fill with transparency */
1544        g_assert(priv->crosshair.surface);
1545        cairo_t *cr = cairo_create(priv->crosshair.surface);
1546        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1547        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1548        cairo_paint(cr);
1549        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1550    
1551        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1552    
1553        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
1554        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
1555        osd_render_crosshair_shape(cr);
1556    
1557        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1558        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
1559        osd_render_crosshair_shape(cr);
1560    
1561        cairo_destroy(cr);
1562    }
1563    #endif
1564    
1565    #ifdef OSD_SCALE
1566    
1567    #ifndef OSD_SCALE_FONT_SIZE
1568    #define OSD_SCALE_FONT_SIZE (12.0)
1569    #endif
1570    #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
1571    #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
1572    
1573    /* various parameters used to create the scale */
1574    #define OSD_SCALE_H2   (OSD_SCALE_H/2)
1575    #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
1576    #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
1577    #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
1578    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
1579    
1580    static void
1581    osd_render_scale(osm_gps_map_osd_t *osd)
1582    {
1583        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1584    
1585        if(!priv->scale.surface)
1586            return;
1587    
1588        /* this only needs to be rendered if the zoom has changed */
1589        gint zoom;
1590        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
1591        if(zoom == priv->scale.zoom)
1592            return;
1593    
1594        priv->scale.zoom = zoom;
1595    
1596        float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
1597    
1598        /* first fill with transparency */
1599        g_assert(priv->scale.surface);
1600        cairo_t *cr = cairo_create(priv->scale.surface);
1601        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1602        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1603        // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1604        cairo_paint(cr);
1605        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1606    
1607        /* determine the size of the scale width in meters */
1608        float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
1609    
1610        /* scale this to useful values */
1611        int exp = logf(width)*M_LOG10E;
1612        int mant = width/pow(10,exp);
1613        int width_metric = mant * pow(10,exp);
1614        char *dist_str = NULL;
1615        if(width_metric<1000)
1616            dist_str = g_strdup_printf("%u m", width_metric);
1617        else
1618            dist_str = g_strdup_printf("%u km", width_metric/1000);
1619        width_metric /= m_per_pix;
1620    
1621        /* and now the hard part: scale for useful imperial values :-( */
1622        /* try to convert to feet, 1ft == 0.3048 m */
1623        width /= 0.3048;
1624        float imp_scale = 0.3048;
1625        char *dist_imp_unit = "ft";
1626    
1627        if(width >= 100) {
1628            /* 1yd == 3 feet */
1629            width /= 3.0;
1630            imp_scale *= 3.0;
1631            dist_imp_unit = "yd";
1632    
1633            if(width >= 1760.0) {
1634                /* 1mi == 1760 yd */
1635                width /= 1760.0;
1636                imp_scale *= 1760.0;
1637                dist_imp_unit = "mi";
1638            }
1639        }
1640    
1641        /* also convert this to full tens/hundreds */
1642        exp = logf(width)*M_LOG10E;
1643        mant = width/pow(10,exp);
1644        int width_imp = mant * pow(10,exp);
1645        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
1646    
1647        /* convert back to pixels */
1648        width_imp *= imp_scale;
1649        width_imp /= m_per_pix;
1650    
1651        cairo_select_font_face (cr, "Sans",
1652                                CAIRO_FONT_SLANT_NORMAL,
1653                                CAIRO_FONT_WEIGHT_BOLD);
1654        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1655        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1656    
1657        cairo_text_extents_t extents;
1658        cairo_text_extents (cr, dist_str, &extents);
1659    
1660        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1661        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1662        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1663        cairo_text_path (cr, dist_str);
1664        cairo_stroke (cr);
1665        cairo_move_to (cr, 2*OSD_SCALE_FD,
1666                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1667        cairo_text_path (cr, dist_str_imp);
1668        cairo_stroke (cr);
1669    
1670        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1671        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1672        cairo_show_text (cr, dist_str);
1673        cairo_move_to (cr, 2*OSD_SCALE_FD,
1674                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1675        cairo_show_text (cr, dist_str_imp);
1676    
1677        g_free(dist_str);
1678        g_free(dist_str_imp);
1679    
1680        /* draw white line */
1681        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1682        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1683        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
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        /* draw black line */
1696        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1697        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1698        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1699        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1700        cairo_rel_line_to (cr, width_metric, 0);
1701        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1702        cairo_stroke(cr);
1703        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1704        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1705        cairo_rel_line_to (cr, width_imp, 0);
1706        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1707        cairo_stroke(cr);
1708    
1709        cairo_destroy(cr);
1710    }
1711    #endif
1712    
1713  static void  static void
1714  osd_render(osm_gps_map_osd_t *osd) {  osd_render_controls(osm_gps_map_osd_t *osd)
1715    {
1716      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1717    
1718        if(!priv->controls.surface)
1719            return;
1720    
1721        if(priv->controls.rendered
1722    #ifdef OSD_GPS_BUTTON
1723           && (priv->controls.gps_enabled == (osd->cb != NULL))
1724    #endif
1725           )
1726            return;
1727    
1728    #ifdef OSD_GPS_BUTTON
1729        priv->controls.gps_enabled = (osd->cb != NULL);
1730    #endif
1731        priv->controls.rendered = TRUE;
1732    
1733  #ifndef OSD_COLOR  #ifndef OSD_COLOR
1734      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1735      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
# Line 731  osd_render(osm_gps_map_osd_t *osd) { Line 1737  osd_render(osm_gps_map_osd_t *osd) {
1737  #endif  #endif
1738    
1739      /* first fill with transparency */      /* first fill with transparency */
1740      cairo_t *cr = cairo_create(priv->overlay);      g_assert(priv->controls.surface);
1741        cairo_t *cr = cairo_create(priv->controls.surface);
1742      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1743      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);
1744      cairo_paint(cr);      cairo_paint(cr);
# Line 802  osd_render(osm_gps_map_osd_t *osd) { Line 1809  osd_render(osm_gps_map_osd_t *osd) {
1809      cairo_stroke(cr);      cairo_stroke(cr);
1810    
1811      cairo_destroy(cr);      cairo_destroy(cr);
1812    }
1813    
1814    static void
1815    osd_render(osm_gps_map_osd_t *osd)
1816    {
1817        /* this function is actually called pretty often since the */
1818        /* OSD contents may have changed (due to a coordinate/zoom change). */
1819        /* The different OSD parts have to make sure that they don't */
1820        /* render unneccessarily often and thus waste CPU power */
1821    
1822        osd_render_controls(osd);
1823    
1824  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1825      osd_render_source_sel(osd);      osd_render_source_sel(osd, FALSE);
1826    #endif
1827    
1828    #ifdef OSD_SCALE
1829        osd_render_scale(osd);
1830    #endif
1831    
1832    #ifdef OSD_CROSSHAIR
1833        osd_render_crosshair(osd);
1834    #endif
1835    
1836    #ifdef OSD_NAV
1837        osd_render_nav(osd);
1838    #endif
1839    
1840    #ifdef OSD_COORDINATES
1841        osd_render_coordinates(osd);
1842  #endif  #endif
1843  }  }
1844    
# Line 815  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1849  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1849    
1850      /* OSD itself uses some off-screen rendering, so check if the */      /* OSD itself uses some off-screen rendering, so check if the */
1851      /* offscreen buffer is present and create it if not */      /* offscreen buffer is present and create it if not */
1852      if(!priv->overlay) {      if(!priv->controls.surface) {
1853          /* create overlay ... */          /* create overlay ... */
1854          priv->overlay =          priv->controls.surface =
1855              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);
1856    
1857            priv->controls.rendered = FALSE;
1858    #ifdef OSD_GPS_BUTTON
1859            priv->controls.gps_enabled = FALSE;
1860    #endif
1861    
1862  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1863          /* the initial OSD state is alway not-expanded */          /* the initial OSD state is alway not-expanded */
1864          priv->map_source =          priv->source_sel.surface =
1865              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1866                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
1867            priv->source_sel.rendered = FALSE;
1868    #endif
1869    
1870    #ifdef OSD_SCALE
1871            priv->scale.surface =
1872                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1873                                           OSD_SCALE_W, OSD_SCALE_H);
1874            priv->scale.zoom = -1;
1875    #endif
1876    
1877    #ifdef OSD_CROSSHAIR
1878            priv->crosshair.surface =
1879                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1880                                           OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1881            priv->crosshair.rendered = FALSE;
1882    #endif
1883    
1884    #ifdef OSD_COORDINATES
1885            priv->coordinates.surface =
1886                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1887                                           OSD_COORDINATES_W, OSD_COORDINATES_H);
1888    
1889            priv->coordinates.lat = priv->coordinates.lon = OSM_GPS_MAP_INVALID;
1890  #endif  #endif
1891    
1892          /* ... and render it */          /* ... and render it */
# Line 834  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1896  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1896      // now draw this onto the original context      // now draw this onto the original context
1897      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1898    
1899      int x = OSD_X, y = OSD_Y;      gint x, y;
     if(OSD_X < 0)  
         x = osd->widget->allocation.width - OSD_W + OSD_X;  
1900    
1901      if(OSD_Y < 0)  #ifdef OSD_SCALE
1902          y = osd->widget->allocation.height - OSD_H + OSD_Y;      x =  OSD_X;
1903        y = -OSD_Y;
1904        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1905        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1906    
1907      cairo_set_source_surface(cr, priv->overlay, x, y);      cairo_set_source_surface(cr, priv->scale.surface, x, y);
1908        cairo_paint(cr);
1909    #endif
1910    
1911    #ifdef OSD_CROSSHAIR
1912        x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1913        y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1914    
1915        cairo_set_source_surface(cr, priv->crosshair.surface, x, y);
1916        cairo_paint(cr);
1917    #endif
1918    
1919    #ifdef OSD_NAV
1920        if(priv->nav.surface) {
1921            x =  OSD_X;
1922            if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1923            y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1924    
1925            cairo_set_source_surface(cr, priv->nav.surface, x, y);
1926            cairo_paint(cr);
1927        }
1928    #endif
1929    
1930    #ifdef OSD_COORDINATES
1931        x = -OSD_X;
1932        y = -OSD_Y;
1933        if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1934        if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1935    
1936        cairo_set_source_surface(cr, priv->coordinates.surface, x, y);
1937        cairo_paint(cr);
1938    #endif
1939    
1940    #ifdef OSD_BALLOON
1941        if(priv->balloon.surface) {
1942    
1943            /* convert given lat lon into screen coordinates */
1944            gint x, y;
1945            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1946                                          priv->balloon.lat, priv->balloon.lon,
1947                                          &x, &y);
1948    
1949            /* check if balloon needs to be rerendered */
1950            osd_render_balloon(osd);
1951    
1952            cairo_set_source_surface(cr, priv->balloon.surface,
1953                                     x + priv->balloon.offset_x,
1954                                     y + priv->balloon.offset_y);
1955            cairo_paint(cr);
1956        }
1957    #endif
1958    
1959        x = OSD_X;
1960        if(x < 0)
1961            x += osd->widget->allocation.width - OSD_W;
1962    
1963        y = OSD_Y;
1964        if(y < 0)
1965            y += osd->widget->allocation.height - OSD_H;
1966    
1967        cairo_set_source_surface(cr, priv->controls.surface, x, y);
1968      cairo_paint(cr);      cairo_paint(cr);
1969    
1970  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1971      if(!priv->handler_id) {      if(!priv->source_sel.handler_id) {
1972          /* the OSD source selection is not being animated */          /* the OSD source selection is not being animated */
1973          if(!priv->expanded)          if(!priv->source_sel.expanded)
1974              x = osd->widget->allocation.width - OSD_S_W;              x = osd->widget->allocation.width - OSD_S_W;
1975          else          else
1976              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1977      } else      } else
1978          x = priv->shift;          x = priv->source_sel.shift;
1979    
1980      y = OSD_S_Y;      y = OSD_S_Y;
1981      if(OSD_S_Y < 0) {      if(OSD_S_Y < 0) {
1982          if(!priv->expanded)          if(!priv->source_sel.expanded)
1983              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1984          else          else
1985              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1986      }      }
1987    
1988      cairo_set_source_surface(cr, priv->map_source, x, y);      cairo_set_source_surface(cr, priv->source_sel.surface, x, y);
1989      cairo_paint(cr);      cairo_paint(cr);
1990  #endif  #endif
1991    
# Line 874  osd_free(osm_gps_map_osd_t *osd) Line 1997  osd_free(osm_gps_map_osd_t *osd)
1997  {  {
1998      osd_priv_t *priv = (osd_priv_t *)(osd->priv);      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1999    
2000      if (priv->overlay)      if (priv->controls.surface)
2001           cairo_surface_destroy(priv->overlay);           cairo_surface_destroy(priv->controls.surface);
2002    
2003  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
2004      if(priv->handler_id)      if(priv->source_sel.handler_id)
2005          gtk_timeout_remove(priv->handler_id);          gtk_timeout_remove(priv->source_sel.handler_id);
2006    
2007        if (priv->source_sel.surface)
2008             cairo_surface_destroy(priv->source_sel.surface);
2009    #endif
2010    
2011    #ifdef OSD_SCALE
2012        if (priv->scale.surface)
2013             cairo_surface_destroy(priv->scale.surface);
2014    #endif
2015    
2016    #ifdef OSD_CROSSHAIR
2017        if (priv->crosshair.surface)
2018             cairo_surface_destroy(priv->crosshair.surface);
2019    #endif
2020    
2021    #ifdef OSD_NAV
2022        if (priv->nav.surface)
2023             cairo_surface_destroy(priv->nav.surface);
2024    #endif
2025    
2026      if (priv->map_source)  #ifdef OSD_COORDINATES
2027           cairo_surface_destroy(priv->map_source);      if (priv->coordinates.surface)
2028             cairo_surface_destroy(priv->coordinates.surface);
2029    #endif
2030    
2031    #ifdef OSD_BALLOON
2032        if (priv->balloon.surface)
2033             cairo_surface_destroy(priv->balloon.surface);
2034  #endif  #endif
2035    
2036      g_free(priv);      g_free(priv);
# Line 893  osd_busy(osm_gps_map_osd_t *osd) Line 2041  osd_busy(osm_gps_map_osd_t *osd)
2041  {  {
2042  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
2043      osd_priv_t *priv = (osd_priv_t *)(osd->priv);      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
2044      return (priv->handler_id != 0);      return (priv->source_sel.handler_id != 0);
2045  #else  #else
2046      return FALSE;      return FALSE;
2047  #endif  #endif
2048  }  }
2049    
2050    static osd_button_t
2051    osd_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
2052        return osd_check_int(osd, TRUE, down, x, y);
2053    }
2054    
2055  static osm_gps_map_osd_t osd_classic = {  static osm_gps_map_osd_t osd_classic = {
2056      .widget     = NULL,      .widget     = NULL,
2057    
# Line 918  static osm_gps_map_osd_t osd_classic = { Line 2071  static osm_gps_map_osd_t osd_classic = {
2071  void  void
2072  osm_gps_map_osd_classic_init(OsmGpsMap *map)  osm_gps_map_osd_classic_init(OsmGpsMap *map)
2073  {  {
2074      osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);      osd_priv_t *priv = g_new0(osd_priv_t, 1);
2075    
2076      osd_classic.priv = priv;      /* reset entries to default value */
2077        osd_classic.widget = NULL;
2078        osd_classic.cb     = NULL;
2079        osd_classic.data   = NULL;
2080        osd_classic.priv   = priv;
2081    
2082    #ifdef OSD_BALLOON
2083        priv->balloon.lat = OSM_GPS_MAP_INVALID;
2084        priv->balloon.lon = OSM_GPS_MAP_INVALID;
2085    #endif
2086    
2087      osm_gps_map_register_osd(map, &osd_classic);      osm_gps_map_register_osd(map, &osd_classic);
2088  }  }
# Line 949  osm_gps_map_osd_check(OsmGpsMap *map, gi Line 2111  osm_gps_map_osd_check(OsmGpsMap *map, gi
2111      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
2112      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
2113    
2114      return osd_check(osd, x, y);      return osd_check_int(osd, FALSE, TRUE, x, y);
2115  }  }

Legend:
Removed from v.94  
changed lines
  Added in v.233