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

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

revision 89 by harbaum, Tue Sep 1 11:16:30 2009 UTC revision 122 by harbaum, Mon Sep 21 13:15:25 2009 UTC
# Line 25  Line 25 
25  /* OSD_DIAMETER */  /* OSD_DIAMETER */
26  /* OSD_X, OSD_Y */  /* OSD_X, OSD_Y */
27    
28    #define OSD_NAV
29    
30  #ifndef USE_CAIRO  #ifndef USE_CAIRO
31  #error "OSD control display lacks a non-cairo implementation!"  #error "OSD control display lacks a non-cairo implementation!"
32  #endif  #endif
# Line 32  Line 34 
34  #include <cairo.h>  #include <cairo.h>
35    
36  #include "osm-gps-map.h"  #include "osm-gps-map.h"
37    #include "converter.h"
38  #include "osm-gps-map-osd-classic.h"  #include "osm-gps-map-osd-classic.h"
39    
40  //the osd controls  //the osd controls
41  typedef struct {  typedef struct {
42      /* the offscreen representation of the OSD */      /* the offscreen representation of the OSD */
43      cairo_surface_t *overlay;      struct {
44            cairo_surface_t *surface;
45            gboolean rendered;
46    #ifdef OSD_GPS_BUTTON
47            gboolean gps_enabled;
48    #endif
49        } controls;
50    
51    #ifdef OSD_BALLOON
52        //a balloon with additional info
53        struct {
54            cairo_surface_t *surface;
55            int orientation, offset_x, offset_y;
56    
57            gboolean just_created;
58            float lat, lon;
59            OsmGpsMapRect_t rect;
60    
61            /* function called to have the user app draw the contents */
62            OsmGpsMapBalloonCallback cb;
63            gpointer data;
64        } balloon;
65    #endif
66    
67    #ifdef OSD_SCALE
68        struct {
69            cairo_surface_t *surface;
70            int zoom;
71        } scale;
72    #endif
73    
74    #ifdef OSD_CROSSHAIR
75        struct {
76            cairo_surface_t *surface;
77            gboolean rendered;
78        } crosshair;
79    #endif
80    
81    #ifdef OSD_NAV
82        struct {
83            cairo_surface_t *surface;
84        } nav;
85    #endif
86    
87    #ifdef OSD_COORDINATES
88        struct {
89            cairo_surface_t *surface;
90            float lat, lon;
91        } coordinates;
92    #endif
93    
94  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
95      /* values to handle the "source" menu */      struct {
96      cairo_surface_t *map_source;          /* values to handle the "source" menu */
97      gboolean expanded;          cairo_surface_t *surface;
98      gint shift, dir, count;          gboolean expanded;
99      gint handler_id;          gint shift, dir, count;
100      gint width, height;          gint handler_id;
101            gint width, height;
102            gboolean rendered;
103        } source_sel;
104  #endif  #endif
105    
106  } osd_priv_t;  } osd_priv_t;
107    
108    #ifdef OSD_BALLOON
109    /* most visual effects are hardcoded by now, but may be made */
110    /* available via properties later */
111    #ifndef BALLOON_AREA_WIDTH
112    #define BALLOON_AREA_WIDTH           290
113    #endif
114    #ifndef BALLOON_AREA_HEIGHT
115    #define BALLOON_AREA_HEIGHT           75
116    #endif
117    #ifndef BALLOON_CORNER_RADIUS
118    #define BALLOON_CORNER_RADIUS         10
119    #endif
120    
121    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
122    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
123    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
124    #define BALLOON_TRANSPARENCY         0.8
125    #define POINTER_HEIGHT                20
126    #define POINTER_FOOT_WIDTH            20
127    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
128    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
129    #define BALLOON_SHADOW_TRANSPARENCY  0.2
130    
131    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
132    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
133    
134    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
135    
136    
137    /* draw the bubble shape. this is used twice, once for the shape and once */
138    /* for the shadow */
139    static void
140    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
141           gboolean bottom, int px, int py, int px0, int px1) {
142    
143        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
144        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
145                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
146        if(!bottom) {
147            /* insert top pointer */
148            cairo_line_to (cr, px1, y0);
149            cairo_line_to (cr, px, py);
150            cairo_line_to (cr, px0, y0);
151        }
152    
153        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
154        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
155                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
156        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
157        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
158                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
159        if(bottom) {
160            /* insert bottom pointer */
161            cairo_line_to (cr, px0, y1);
162            cairo_line_to (cr, px, py);
163            cairo_line_to (cr, px1, y1);
164        }
165    
166        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
167        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
168                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
169    
170        cairo_close_path (cr);
171    }
172    
173    static void
174    osd_render_balloon(osm_gps_map_osd_t *osd) {
175        osd_priv_t *priv = (osd_priv_t*)osd->priv;
176    
177        /* get zoom */
178        gint zoom;
179        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
180    
181        /* ------- convert given coordinate into screen position --------- */
182        gint xs, ys;
183        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
184                                          priv->balloon.lat, priv->balloon.lon,
185                                          &xs, &ys);
186    
187        gint x0 = 1, y0 = 1;
188    
189        /* check position of this relative to screen center to determine */
190        /* pointer direction ... */
191        int pointer_x, pointer_x0, pointer_x1;
192        int pointer_y;
193    
194        /* ... and calculate position */
195        int orientation = 0;
196        if(xs > osd->widget->allocation.width/2) {
197            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
198            pointer_x = x0 - priv->balloon.offset_x;
199            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
200            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
201            orientation |= 1;
202        } else {
203            priv->balloon.offset_x = -POINTER_OFFSET;
204            pointer_x = x0 - priv->balloon.offset_x;
205            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
206            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
207        }
208    
209        gboolean bottom = FALSE;
210        if(ys > osd->widget->allocation.height/2) {
211            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
212            pointer_y = y0 - priv->balloon.offset_y;
213            bottom = TRUE;
214            orientation |= 2;
215        } else {
216            priv->balloon.offset_y = 0;
217            pointer_y = y0 - priv->balloon.offset_y;
218            y0 += POINTER_HEIGHT;
219        }
220    
221        /* if required orientation equals current one, then don't render */
222        /* anything */
223        if(orientation == priv->balloon.orientation)
224            return;
225    
226        priv->balloon.orientation = orientation;
227    
228        /* calculate bottom/right of box */
229        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
230    
231        /* save balloon screen coordinates for later use */
232        priv->balloon.rect.x = x0 + BALLOON_BORDER;
233        priv->balloon.rect.y = y0 + BALLOON_BORDER;
234        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
235        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
236    
237        cairo_t *cr = cairo_create(priv->balloon.surface);
238        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
239        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
240        cairo_paint(cr);
241        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
242    
243        /* --------- draw shadow --------------- */
244        osm_gps_map_draw_balloon_shape (cr,
245                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
246                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
247                     bottom, pointer_x, pointer_y,
248                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
249    
250        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
251        cairo_fill_preserve (cr);
252        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
253        cairo_set_line_width (cr, 0);
254        cairo_stroke (cr);
255    
256        /* --------- draw main shape ----------- */
257        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
258                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
259    
260        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
261        cairo_fill_preserve (cr);
262        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
263        cairo_set_line_width (cr, 1);
264        cairo_stroke (cr);
265    
266        if (priv->balloon.cb) {
267            /* clip in case application tries to draw in */
268                /* exceed of the balloon */
269            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
270                             priv->balloon.rect.w, priv->balloon.rect.h);
271            cairo_clip (cr);
272            cairo_new_path (cr);  /* current path is not
273                                     consumed by cairo_clip() */
274    
275            priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
276        }
277    
278        cairo_destroy(cr);
279    }
280    
281    /* return true if balloon is being displayed and if */
282    /* the given coordinate is within this balloon */
283    static gboolean
284    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y)
285    {
286        osd_priv_t *priv = (osd_priv_t*)osd->priv;
287    
288        if(!priv->balloon.surface)
289            return FALSE;
290    
291        gint xs, ys;
292        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
293                                          priv->balloon.lat, priv->balloon.lon,
294                                          &xs, &ys);
295    
296        xs += priv->balloon.rect.x + priv->balloon.offset_x;
297        ys += priv->balloon.rect.y + priv->balloon.offset_y;
298    
299        gboolean is_in =
300            (x > xs) && (x < xs + priv->balloon.rect.w) &&
301            (y > ys) && (y < ys + priv->balloon.rect.h);
302    
303        /* handle the fact that the balloon may have been created by the */
304        /* button down event */
305        if(!is_in && !down && !priv->balloon.just_created) {
306            /* the user actually clicked outside the balloon */
307    
308            /* close the balloon! */
309            osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
310        }
311    
312        return is_in;
313    }
314    
315    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
316        g_return_if_fail (OSM_IS_GPS_MAP (map));
317    
318        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
319        g_return_if_fail (osd);
320    
321        osd_priv_t *priv = (osd_priv_t*)osd->priv;
322        g_return_if_fail (priv);
323    
324        if(priv->balloon.surface) {
325            cairo_surface_destroy(priv->balloon.surface);
326            priv->balloon.surface = NULL;
327            priv->balloon.lat = OSM_GPS_MAP_INVALID;
328            priv->balloon.lon = OSM_GPS_MAP_INVALID;
329        }
330        osm_gps_map_redraw(map);
331    }
332    
333    void
334    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
335                                  OsmGpsMapBalloonCallback cb, gpointer data) {
336        g_return_if_fail (OSM_IS_GPS_MAP (map));
337    
338        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
339        g_return_if_fail (osd);
340    
341        osd_priv_t *priv = (osd_priv_t*)osd->priv;
342        g_return_if_fail (priv);
343    
344        osm_gps_map_osd_clear_balloon (map);
345    
346        /* allocate balloon surface */
347        priv->balloon.surface =
348            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
349                                       BALLOON_W+2, BALLOON_H+2);
350    
351        priv->balloon.lat = latitude;
352        priv->balloon.lon = longitude;
353        priv->balloon.cb = cb;
354        priv->balloon.data = data;
355        priv->balloon.just_created = TRUE;
356    
357        priv->balloon.orientation = -1;
358    
359        osd_render_balloon(osd);
360    
361        osm_gps_map_redraw(map);
362    }
363    
364    #endif // OSD_BALLOON
365    
366  /* position and extent of bounding box */  /* position and extent of bounding box */
367  #ifndef OSD_X  #ifndef OSD_X
368  #define OSD_X      (10)  #define OSD_X      (10)
# Line 272  osd_check_zoom(gint x, gint y) { Line 585  osd_check_zoom(gint x, gint y) {
585  #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)  #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
586    
587  /* size of usable area when expanded */  /* size of usable area when expanded */
588  #define OSD_S_AREA_W (priv->width)  #define OSD_S_AREA_W (priv->source_sel.width)
589  #define OSD_S_AREA_H (priv->height)  #define OSD_S_AREA_H (priv->source_sel.height)
590  #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)
591  #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)  #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
592    
# Line 289  osd_check_zoom(gint x, gint y) { Line 602  osd_check_zoom(gint x, gint y) {
602  /* or the entire menu incl. the puller (expanded) */  /* or the entire menu incl. the puller (expanded) */
603  static void  static void
604  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) {
605      if(!priv->expanded) {      if(!priv->source_sel.expanded) {
606          /* just draw the puller */          /* just draw the puller */
607          cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);          cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
608          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 630  osd_source_content(osm_gps_map_osd_t *os
630    
631      int py = offset + OSD_S_RAD - OSD_S_D0;      int py = offset + OSD_S_RAD - OSD_S_D0;
632    
633      if(!priv->expanded) {      if(!priv->source_sel.expanded) {
634          /* draw the "puller" open (<) arrow */          /* draw the "puller" open (<) arrow */
635          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);
636          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 655  osd_source_content(osm_gps_map_osd_t *os
655                                      CAIRO_FONT_WEIGHT_BOLD);                                      CAIRO_FONT_WEIGHT_BOLD);
656              cairo_set_font_size (cr, OSD_FONT_SIZE);              cairo_set_font_size (cr, OSD_FONT_SIZE);
657    
658              int i, step = (priv->height - 2*OSD_TEXT_BORDER) /              int i, step = (priv->source_sel.height - 2*OSD_TEXT_BORDER) /
659                  OSM_GPS_MAP_SOURCE_LAST;                  OSM_GPS_MAP_SOURCE_LAST;
660              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++) {
661                  cairo_text_extents_t extents;                  cairo_text_extents_t extents;
# Line 356  osd_source_content(osm_gps_map_osd_t *os Line 669  osd_source_content(osm_gps_map_osd_t *os
669                  if(source == i) {                  if(source == i) {
670                      cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,                      cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
671                                      y - OSD_TEXT_SKIP,                                      y - OSD_TEXT_SKIP,
672                                      priv->width - OSD_TEXT_BORDER,                                      priv->source_sel.width - OSD_TEXT_BORDER,
673                                      step + OSD_TEXT_SKIP);                                      step + OSD_TEXT_SKIP);
674                      cairo_fill(cr);                      cairo_fill(cr);
675    
# Line 387  osd_source_content(osm_gps_map_osd_t *os Line 700  osd_source_content(osm_gps_map_osd_t *os
700  }  }
701    
702  static void  static void
703  osd_render_source_sel(osm_gps_map_osd_t *osd) {  osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
704      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
705    
706        if(priv->source_sel.rendered && !force_rerender)
707            return;
708    
709        priv->source_sel.rendered = TRUE;
710    
711  #ifndef OSD_COLOR  #ifndef OSD_COLOR
712      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
713      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 715  osd_render_source_sel(osm_gps_map_osd_t
715  #endif  #endif
716    
717      /* draw source selector */      /* draw source selector */
718      cairo_t *cr = cairo_create(priv->map_source);      cairo_t *cr = cairo_create(priv->source_sel.surface);
719      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
720      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);
721      cairo_paint(cr);      cairo_paint(cr);
# Line 438  osd_source_reallocate(osm_gps_map_osd_t Line 756  osd_source_reallocate(osm_gps_map_osd_t
756      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
757    
758      /* re-allocate offscreen bitmap */      /* re-allocate offscreen bitmap */
759      g_assert (priv->map_source);      g_assert (priv->source_sel.surface);
760    
761      int w = OSD_S_W, h = OSD_S_H;      int w = OSD_S_W, h = OSD_S_H;
762      if(priv->expanded) {      if(priv->source_sel.expanded) {
         /* ... and right of it the waypoint id */  
763          cairo_text_extents_t extents;          cairo_text_extents_t extents;
764    
765          /* determine content size */          /* determine content size */
766          cairo_t *cr = cairo_create(priv->map_source);          cairo_t *cr = cairo_create(priv->source_sel.surface);
767          cairo_select_font_face (cr, "Sans",          cairo_select_font_face (cr, "Sans",
768                                  CAIRO_FONT_SLANT_NORMAL,                                  CAIRO_FONT_SLANT_NORMAL,
769                                  CAIRO_FONT_WEIGHT_BOLD);                                  CAIRO_FONT_WEIGHT_BOLD);
# Line 463  osd_source_reallocate(osm_gps_map_osd_t Line 780  osd_source_reallocate(osm_gps_map_osd_t
780          }          }
781          cairo_destroy(cr);          cairo_destroy(cr);
782    
783          priv->width  = max_w + 2*OSD_TEXT_BORDER;          priv->source_sel.width  = max_w + 2*OSD_TEXT_BORDER;
784          priv->height = OSM_GPS_MAP_SOURCE_LAST *          priv->source_sel.height = OSM_GPS_MAP_SOURCE_LAST *
785              (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;              (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
786    
787          w = OSD_S_EXP_W;          w = OSD_S_EXP_W;
788          h = OSD_S_EXP_H;          h = OSD_S_EXP_H;
789      }      }
790    
791      cairo_surface_destroy(priv->map_source);      cairo_surface_destroy(priv->source_sel.surface);
792      priv->map_source =      priv->source_sel.surface =
793          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
794    
795      osd_render_source_sel(osd);      osd_render_source_sel(osd, TRUE);
   
796  }  }
797    
798  #define OSD_HZ      15  #define OSD_HZ      15
# Line 487  static gboolean osd_source_animate(gpoin Line 803  static gboolean osd_source_animate(gpoin
803      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
804      int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;      int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
805      gboolean done = FALSE;      gboolean done = FALSE;
806      priv->count += priv->dir;      priv->source_sel.count += priv->source_sel.dir;
807    
808      /* shifting in */      /* shifting in */
809      if(priv->dir < 0) {      if(priv->source_sel.dir < 0) {
810          if(priv->count <= 0) {          if(priv->source_sel.count <= 0) {
811              priv->count = 0;              priv->source_sel.count = 0;
812              done = TRUE;              done = TRUE;
813          }          }
814      } else {      } else {
815          if(priv->count >= 1000) {          if(priv->source_sel.count >= 1000) {
816              priv->expanded = FALSE;              priv->source_sel.expanded = FALSE;
817              osd_source_reallocate(osd);              osd_source_reallocate(osd);
818    
819              priv->count = 1000;              priv->source_sel.count = 1000;
820              done = TRUE;              done = TRUE;
821          }          }
822      }      }
# Line 508  static gboolean osd_source_animate(gpoin Line 824  static gboolean osd_source_animate(gpoin
824    
825      /* 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 */
826    
827      /* nicer sinoid mapping */      /* nice sinoid mapping */
828      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;
829      priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +      priv->source_sel.shift =
830            (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
831          m * diff;          m * diff;
832    
833        /* make sure the screen is updated */
834      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
835    
836        /* stop animation if done */
837      if(done)      if(done)
838          priv->handler_id = 0;          priv->source_sel.handler_id = 0;
839    
840      return !done;      return !done;
841  }  }
# Line 528  osd_source_toggle(osm_gps_map_osd_t *osd Line 847  osd_source_toggle(osm_gps_map_osd_t *osd
847      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
848    
849      /* ignore clicks while animation is running */      /* ignore clicks while animation is running */
850      if(priv->handler_id)      if(priv->source_sel.handler_id)
851          return;          return;
852    
853      /* expand immediately, collapse is handle at the end of the collapse animation */      /* expand immediately, collapse is handle at the end of the */
854      if(!priv->expanded) {      /* collapse animation */
855          priv->expanded = TRUE;      if(!priv->source_sel.expanded) {
856            priv->source_sel.expanded = TRUE;
857          osd_source_reallocate(osd);          osd_source_reallocate(osd);
858    
859          priv->count = 1000;          priv->source_sel.count = 1000;
860          priv->shift = osd->widget->allocation.width - OSD_S_W;          priv->source_sel.shift = osd->widget->allocation.width - OSD_S_W;
861          priv->dir = -1000/OSD_HZ;          priv->source_sel.dir = -1000/OSD_HZ;
862      } else {      } else {
863          priv->count =  0;          priv->source_sel.count =  0;
864          priv->shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;          priv->source_sel.shift = osd->widget->allocation.width -
865          priv->dir = +1000/OSD_HZ;              OSD_S_EXP_W + OSD_S_X;
866            priv->source_sel.dir = +1000/OSD_HZ;
867      }      }
868    
869      priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);      /* start timer to handle animation */
870        priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
871                                                      osd_source_animate, osd);
872  }  }
873    
874  /* check if the user clicked inside the source selection area */  /* check if the user clicked inside the source selection area */
875  static osd_button_t  static osd_button_t
876  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) {
877      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
878    
879      if(!priv->expanded)      if(!priv->source_sel.expanded)
880          x -= osd->widget->allocation.width - OSD_S_W;          x -= osd->widget->allocation.width - OSD_S_W;
881      else      else
882          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 891  osd_source_check(osm_gps_map_osd_t *osd,
891          /* really within puller shape? */          /* really within puller shape? */
892          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)) {
893              /* expand source selector */              /* expand source selector */
894              osd_source_toggle(osd);              if(down)
895                    osd_source_toggle(osd);
896    
897              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
898              /* of the OSD */              /* of the OSD */
# Line 577  osd_source_check(osm_gps_map_osd_t *osd, Line 901  osd_source_check(osm_gps_map_osd_t *osd,
901      }      }
902    
903      /* check for clicks into data area */      /* check for clicks into data area */
904      if(priv->expanded && !priv->handler_id) {      if(priv->source_sel.expanded && !priv->source_sel.handler_id) {
905            /* re-adjust from puller top to content top */
906            if(OSD_S_Y < 0)
907                y += OSD_S_EXP_H - OSD_S_PH;
908    
909          if(x > OSD_S_PW &&          if(x > OSD_S_PW &&
910             x < OSD_S_PW + OSD_S_EXP_W &&             x < OSD_S_PW + OSD_S_EXP_W &&
911             y > 0 &&             y > 0 &&
912             y < OSD_S_EXP_H) {             y < OSD_S_EXP_H) {
913    
914              int step = (priv->height - 2*OSD_TEXT_BORDER)              int step = (priv->source_sel.height - 2*OSD_TEXT_BORDER)
915                  / OSM_GPS_MAP_SOURCE_LAST;                  / OSM_GPS_MAP_SOURCE_LAST;
916    
917              y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;              y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
918              y /= step;              y /= step;
919              y += 1;              y += 1;
920    
921              gint old = 0;              if(down) {
922              g_object_get(osd->widget, "map-source", &old, NULL);                  gint old = 0;
923                    g_object_get(osd->widget, "map-source", &old, NULL);
924              if(y > OSM_GPS_MAP_SOURCE_NULL &&  
925                 y <= OSM_GPS_MAP_SOURCE_LAST &&                  if(y > OSM_GPS_MAP_SOURCE_NULL &&
926                 old != y) {                     y <= OSM_GPS_MAP_SOURCE_LAST &&
927                  g_object_set(osd->widget, "map-source", y, NULL);                     old != y) {
928                        g_object_set(osd->widget, "map-source", y, NULL);
929                  osd_render_source_sel(osd);  
930                  osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));                      osd_render_source_sel(osd, TRUE);
931                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
932                    }
933              }              }
934    
935              /* return "clicked in OSD background" to prevent further */              /* return "clicked in OSD background" to prevent further */
# Line 613  osd_source_check(osm_gps_map_osd_t *osd, Line 943  osd_source_check(osm_gps_map_osd_t *osd,
943  #endif // OSD_SOURCE_SEL  #endif // OSD_SOURCE_SEL
944    
945  static osd_button_t  static osd_button_t
946  osd_check(osm_gps_map_osd_t *osd, gint x, gint y) {  osd_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
947      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
948    
949    #ifdef OSD_BALLOON
950        if(down) {
951            /* needed to handle balloons that are created at click */
952            osd_priv_t *priv = (osd_priv_t*)osd->priv;
953            priv->balloon.just_created = FALSE;
954        }
955    #endif
956    
957  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
958      /* the source selection area is handles internally */      /* the source selection area is handles internally */
959      but = osd_source_check(osd, x, y);      but = osd_source_check(osd, down, x, y);
     if(but != OSD_NONE)  
         return but;  
960  #endif  #endif
961    
962      x -= OSD_X;      if(but == OSD_NONE) {
963      y -= OSD_Y;          gint mx = x - OSD_X;
964            gint my = y - OSD_Y;
965      if(OSD_X < 0)  
966          x -= (osd->widget->allocation.width - OSD_W);          if(OSD_X < 0)
967                mx -= (osd->widget->allocation.width - OSD_W);
968      if(OSD_Y < 0)  
969          y -= (osd->widget->allocation.height - OSD_H);          if(OSD_Y < 0)
970                my -= (osd->widget->allocation.height - OSD_H);
971      /* first do a rough test for the OSD area. */  
972      /* this is just to avoid an unnecessary detailed test */          /* first do a rough test for the OSD area. */
973      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {          /* this is just to avoid an unnecessary detailed test */
974            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
975  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
976          but = osd_check_dpad(x, y);              but = osd_check_dpad(mx, my);
977  #endif  #endif
978            }
979    
980          if(but == OSD_NONE)          if(but == OSD_NONE)
981              but = osd_check_zoom(x, y);              but = osd_check_zoom(mx, my);
982      }      }
983    
984    #ifdef OSD_BALLOON
985        if(but == OSD_NONE) {
986            /* check if user clicked into balloon */
987            if(osd_balloon_check(osd, down, x, y))
988                but = OSD_BG;
989        }
990    #endif
991    
992      return but;      return but;
993  }  }
994    
# Line 716  osd_zoom_labels(cairo_t *cr, gint x, gin Line 1062  osd_zoom_labels(cairo_t *cr, gint x, gin
1062      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
1063  }  }
1064    
1065    #ifdef OSD_COORDINATES
1066    
1067    #ifndef OSD_COORDINATES_FONT_SIZE
1068    #define OSD_COORDINATES_FONT_SIZE 12
1069    #endif
1070    
1071    #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
1072    
1073    #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1074    #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE+OSD_COORDINATES_OFFSET)
1075    
1076    /* these can be overwritten with versions that support */
1077    /* localization */
1078    #ifndef OSD_COORDINATES_CHR_N
1079    #define OSD_COORDINATES_CHR_N  "N"
1080    #endif
1081    #ifndef OSD_COORDINATES_CHR_S
1082    #define OSD_COORDINATES_CHR_S  "S"
1083    #endif
1084    #ifndef OSD_COORDINATES_CHR_E
1085    #define OSD_COORDINATES_CHR_E  "E"
1086    #endif
1087    #ifndef OSD_COORDINATES_CHR_W
1088    #define OSD_COORDINATES_CHR_W  "W"
1089    #endif
1090    
1091    
1092    
1093    /* this is the classic geocaching notation */
1094    static char
1095    *osd_latitude_str(float latitude) {
1096        char *c = OSD_COORDINATES_CHR_N;
1097        float integral, fractional;
1098    
1099        if(isnan(latitude))
1100            return NULL;
1101    
1102        if(latitude < 0) {
1103            latitude = fabs(latitude);
1104            c = OSD_COORDINATES_CHR_S;
1105        }
1106    
1107        fractional = modff(latitude, &integral);
1108    
1109        return g_strdup_printf("%s %02d° %06.3f'",
1110                               c, (int)integral, fractional*60.0);
1111    }
1112    
1113    static char
1114    *osd_longitude_str(float longitude) {
1115        char *c = OSD_COORDINATES_CHR_E;
1116        float integral, fractional;
1117    
1118        if(isnan(longitude))
1119            return NULL;
1120    
1121        if(longitude < 0) {
1122            longitude = fabs(longitude);
1123            c = OSD_COORDINATES_CHR_W;
1124        }
1125    
1126        fractional = modff(longitude, &integral);
1127    
1128        return g_strdup_printf("%s %03d° %06.3f'",
1129                               c, (int)integral, fractional*60.0);
1130    }
1131    
1132  static void  static void
1133  osd_render(osm_gps_map_osd_t *osd) {  osd_render_coordinates(osm_gps_map_osd_t *osd)
1134    {
1135      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
1136    
1137        /* get current map position */
1138        gfloat lat, lon;
1139        g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1140    
1141        /* check if position has changed enough to require redraw */
1142        if(!isnan(priv->coordinates.lat) && !isnan(priv->coordinates.lon))
1143            /* 1/60000 == 1/1000 minute */
1144            if((fabsf(lat - priv->coordinates.lat) < 1/60000) &&
1145               (fabsf(lon - priv->coordinates.lon) < 1/60000))
1146                return;
1147    
1148        priv->coordinates.lat = lat;
1149        priv->coordinates.lon = lon;
1150    
1151        /* first fill with transparency */
1152        cairo_t *cr = cairo_create(priv->coordinates.surface);
1153        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1154        //    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1155        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1156        cairo_paint(cr);
1157        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1158    
1159        cairo_select_font_face (cr, "Sans",
1160                                CAIRO_FONT_SLANT_NORMAL,
1161                                CAIRO_FONT_WEIGHT_BOLD);
1162        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1163    
1164        char *latitude = osd_latitude_str(lat);
1165        char *longitude = osd_longitude_str(lon);
1166    
1167        cairo_text_extents_t lat_extents, lon_extents;
1168        cairo_text_extents (cr, latitude, &lat_extents);
1169        cairo_text_extents (cr, longitude, &lon_extents);
1170    
1171        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1172        cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1173        cairo_move_to (cr,
1174                       (OSD_COORDINATES_W - lat_extents.width)/2,
1175                       OSD_COORDINATES_OFFSET - lat_extents.y_bearing);
1176        cairo_text_path (cr, latitude);
1177        cairo_move_to (cr,
1178                       (OSD_COORDINATES_W - lon_extents.width)/2,
1179                       OSD_COORDINATES_OFFSET - lon_extents.y_bearing +
1180                       OSD_COORDINATES_FONT_SIZE);
1181        cairo_text_path (cr, longitude);
1182        cairo_stroke (cr);
1183    
1184        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1185        cairo_move_to (cr,
1186                       (OSD_COORDINATES_W - lat_extents.width)/2,
1187                       OSD_COORDINATES_OFFSET - lat_extents.y_bearing);
1188        cairo_show_text (cr, latitude);
1189        cairo_move_to (cr,
1190                       (OSD_COORDINATES_W - lon_extents.width)/2,
1191                       OSD_COORDINATES_OFFSET - lon_extents.y_bearing +
1192                       OSD_COORDINATES_FONT_SIZE);
1193        cairo_show_text (cr, longitude);
1194    
1195        g_free(latitude);
1196        g_free(longitude);
1197    
1198        cairo_destroy(cr);
1199    }
1200    #endif  // OSD_COORDINATES
1201    
1202    #ifdef OSD_NAV
1203    #define OSD_NAV_W  (100)
1204    #define OSD_NAV_H  (100)
1205    
1206    static void
1207    osd_render_nav(osm_gps_map_osd_t *osd)
1208    {
1209        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1210    
1211        /* first fill with transparency */
1212        cairo_t *cr = cairo_create(priv->nav.surface);
1213        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1214        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1215        cairo_paint(cr);
1216        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1217    
1218        cairo_destroy(cr);
1219    }
1220    
1221    #endif // OSD_NAV
1222    
1223    
1224    #ifdef OSD_CROSSHAIR
1225    
1226    #ifndef OSD_CROSSHAIR_RADIUS
1227    #define OSD_CROSSHAIR_RADIUS 10
1228    #endif
1229    
1230    #define OSD_CROSSHAIR_TICK  (OSD_CROSSHAIR_RADIUS/2)
1231    #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
1232    #define OSD_CROSSHAIR_W  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1233    #define OSD_CROSSHAIR_H  ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1234    
1235    static void
1236    osd_render_crosshair_shape(cairo_t *cr) {
1237        cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
1238                   OSD_CROSSHAIR_RADIUS, 0,  2*M_PI);
1239    
1240        cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
1241                       OSD_CROSSHAIR_H/2);
1242        cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
1243        cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
1244                       OSD_CROSSHAIR_H/2);
1245        cairo_rel_line_to (cr,  OSD_CROSSHAIR_TICK, 0);
1246    
1247        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1248                       OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
1249        cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
1250        cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1251                       OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
1252        cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
1253    
1254        cairo_stroke (cr);
1255    }
1256    
1257    static void
1258    osd_render_crosshair(osm_gps_map_osd_t *osd)
1259    {
1260        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1261    
1262        if(priv->crosshair.rendered)
1263            return;
1264    
1265        priv->crosshair.rendered = TRUE;
1266    
1267        /* first fill with transparency */
1268        cairo_t *cr = cairo_create(priv->crosshair.surface);
1269        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1270        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1271        cairo_paint(cr);
1272        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1273    
1274        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1275    
1276        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
1277        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
1278        osd_render_crosshair_shape(cr);
1279    
1280        cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1281        cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
1282        osd_render_crosshair_shape(cr);
1283    
1284        cairo_destroy(cr);
1285    }
1286    #endif
1287    
1288    #ifdef OSD_SCALE
1289    
1290    #ifndef OSD_SCALE_FONT_SIZE
1291    #define OSD_SCALE_FONT_SIZE 12
1292    #endif
1293    #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
1294    #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
1295    
1296    /* various parameters used to create the scale */
1297    #define OSD_SCALE_H2   (OSD_SCALE_H/2)
1298    #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
1299    #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
1300    #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
1301    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
1302    
1303    static void
1304    osd_render_scale(osm_gps_map_osd_t *osd)
1305    {
1306        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1307    
1308        /* this only needs to be rendered if the zoom has changed */
1309        gint zoom;
1310        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
1311        if(zoom == priv->scale.zoom)
1312            return;
1313    
1314        priv->scale.zoom = zoom;
1315    
1316        float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
1317    
1318        /* first fill with transparency */
1319        cairo_t *cr = cairo_create(priv->scale.surface);
1320        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1321        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1322        // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1323        cairo_paint(cr);
1324        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1325    
1326        /* determine the size of the scale width in meters */
1327        float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
1328    
1329        /* scale this to useful values */
1330        int exp = logf(width)*M_LOG10E;
1331        int mant = width/pow(10,exp);
1332        int width_metric = mant * pow(10,exp);
1333        char *dist_str = NULL;
1334        if(width_metric<1000)
1335            dist_str = g_strdup_printf("%u m", width_metric);
1336        else
1337            dist_str = g_strdup_printf("%u km", width_metric/1000);
1338        width_metric /= m_per_pix;
1339    
1340        /* and now the hard part: scale for useful imperial values :-( */
1341        /* try to convert to feet, 1ft == 0.3048 m */
1342        width /= 0.3048;
1343        float imp_scale = 0.3048;
1344        char *dist_imp_unit = "ft";
1345    
1346        if(width >= 100) {
1347            /* 1yd == 3 feet */
1348            width /= 3.0;
1349            imp_scale *= 3.0;
1350            dist_imp_unit = "yd";
1351    
1352            if(width >= 1760.0) {
1353                /* 1mi == 1760 yd */
1354                width /= 1760.0;
1355                imp_scale *= 1760.0;
1356                dist_imp_unit = "mi";
1357            }
1358        }
1359    
1360        /* also convert this to full tens/hundreds */
1361        exp = logf(width)*M_LOG10E;
1362        mant = width/pow(10,exp);
1363        int width_imp = mant * pow(10,exp);
1364        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
1365    
1366        /* convert back to pixels */
1367        width_imp *= imp_scale;
1368        width_imp /= m_per_pix;
1369    
1370        cairo_select_font_face (cr, "Sans",
1371                                CAIRO_FONT_SLANT_NORMAL,
1372                                CAIRO_FONT_WEIGHT_BOLD);
1373        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1374        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1375    
1376        cairo_text_extents_t extents;
1377        cairo_text_extents (cr, dist_str, &extents);
1378    
1379        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1380        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1381        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1382        cairo_text_path (cr, dist_str);
1383        cairo_stroke (cr);
1384        cairo_move_to (cr, 2*OSD_SCALE_FD,
1385                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1386        cairo_text_path (cr, dist_str_imp);
1387        cairo_stroke (cr);
1388    
1389        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1390        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1391        cairo_show_text (cr, dist_str);
1392        cairo_move_to (cr, 2*OSD_SCALE_FD,
1393                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1394        cairo_show_text (cr, dist_str_imp);
1395    
1396        g_free(dist_str);
1397        g_free(dist_str_imp);
1398    
1399        /* draw white line */
1400        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
1401        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1402        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
1403        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1404        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1405        cairo_rel_line_to (cr, width_metric, 0);
1406        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1407        cairo_stroke(cr);
1408        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1409        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1410        cairo_rel_line_to (cr, width_imp, 0);
1411        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1412        cairo_stroke(cr);
1413    
1414        /* draw black line */
1415        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1416        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1417        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1418        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
1419        cairo_rel_line_to (cr, width_metric, 0);
1420        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1421        cairo_stroke(cr);
1422        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1423        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1424        cairo_rel_line_to (cr, width_imp, 0);
1425        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1426        cairo_stroke(cr);
1427    
1428        cairo_destroy(cr);
1429    }
1430    #endif
1431    
1432    static void
1433    osd_render_controls(osm_gps_map_osd_t *osd)
1434    {
1435        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1436    
1437        if(priv->controls.rendered
1438    #ifdef OSD_GPS_BUTTON
1439           && (priv->controls.gps_enabled == (osd->cb != NULL))
1440    #endif
1441           )
1442            return;
1443    
1444    #ifdef OSD_GPS_BUTTON
1445        priv->controls.gps_enabled = (osd->cb != NULL);
1446    #endif
1447        priv->controls.rendered = TRUE;
1448    
1449  #ifndef OSD_COLOR  #ifndef OSD_COLOR
1450      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1451      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
# Line 727  osd_render(osm_gps_map_osd_t *osd) { Line 1453  osd_render(osm_gps_map_osd_t *osd) {
1453  #endif  #endif
1454    
1455      /* first fill with transparency */      /* first fill with transparency */
1456      cairo_t *cr = cairo_create(priv->overlay);      cairo_t *cr = cairo_create(priv->controls.surface);
1457      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1458      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);
1459      cairo_paint(cr);      cairo_paint(cr);
# Line 798  osd_render(osm_gps_map_osd_t *osd) { Line 1524  osd_render(osm_gps_map_osd_t *osd) {
1524      cairo_stroke(cr);      cairo_stroke(cr);
1525    
1526      cairo_destroy(cr);      cairo_destroy(cr);
1527    }
1528    
1529    static void
1530    osd_render(osm_gps_map_osd_t *osd)
1531    {
1532        /* this function is actually called pretty often since the */
1533        /* OSD contents may have changed (due to a coordinate/zoom change). */
1534        /* The different OSD parts have to make sure that they don't */
1535        /* render unneccessarily often and thus waste CPU power */
1536    
1537        osd_render_controls(osd);
1538    
1539  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1540      osd_render_source_sel(osd);      osd_render_source_sel(osd, FALSE);
1541    #endif
1542    
1543    #ifdef OSD_SCALE
1544        osd_render_scale(osd);
1545    #endif
1546    
1547    #ifdef OSD_CROSSHAIR
1548        osd_render_crosshair(osd);
1549    #endif
1550    
1551    #ifdef OSD_NAV
1552        osd_render_nav(osd);
1553    #endif
1554    
1555    #ifdef OSD_COORDINATES
1556        osd_render_coordinates(osd);
1557  #endif  #endif
1558  }  }
1559    
# Line 811  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1564  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1564    
1565      /* OSD itself uses some off-screen rendering, so check if the */      /* OSD itself uses some off-screen rendering, so check if the */
1566      /* offscreen buffer is present and create it if not */      /* offscreen buffer is present and create it if not */
1567      if(!priv->overlay) {      if(!priv->controls.surface) {
1568          /* create overlay ... */          /* create overlay ... */
1569          priv->overlay =          priv->controls.surface =
1570              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);
1571    
1572            priv->controls.rendered = FALSE;
1573    #ifdef OSD_GPS_BUTTON
1574            priv->controls.gps_enabled = FALSE;
1575    #endif
1576    
1577  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1578          /* the initial OSD state is alway not-expanded */          /* the initial OSD state is alway not-expanded */
1579          priv->map_source =          priv->source_sel.surface =
1580              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1581                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
1582            priv->source_sel.rendered = FALSE;
1583    #endif
1584    
1585    #ifdef OSD_SCALE
1586            priv->scale.surface =
1587                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1588                                           OSD_SCALE_W, OSD_SCALE_H);
1589            priv->scale.zoom = -1;
1590    #endif
1591    
1592    #ifdef OSD_CROSSHAIR
1593            priv->crosshair.surface =
1594                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1595                                           OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1596            priv->crosshair.rendered = FALSE;
1597    #endif
1598    
1599    #ifdef OSD_NAV
1600            priv->nav.surface =
1601                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1602                                           OSD_NAV_W, OSD_NAV_H);
1603    #endif
1604    
1605    #ifdef OSD_COORDINATES
1606            priv->coordinates.surface =
1607                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1608                                           OSD_COORDINATES_W, OSD_COORDINATES_H);
1609    
1610            priv->coordinates.lat = priv->coordinates.lon = OSM_GPS_MAP_INVALID;
1611  #endif  #endif
1612    
1613          /* ... and render it */          /* ... and render it */
# Line 830  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1617  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1617      // now draw this onto the original context      // now draw this onto the original context
1618      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1619    
1620      int x = OSD_X, y = OSD_Y;      gint x, y;
     if(OSD_X < 0)  
         x = osd->widget->allocation.width - OSD_W + OSD_X;  
1621    
1622      if(OSD_Y < 0)  #ifdef OSD_SCALE
1623          y = osd->widget->allocation.height - OSD_H + OSD_Y;      x =  OSD_X;
1624        y = -OSD_Y;
1625        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1626        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1627    
1628      cairo_set_source_surface(cr, priv->overlay, x, y);      cairo_set_source_surface(cr, priv->scale.surface, x, y);
1629        cairo_paint(cr);
1630    #endif
1631    
1632    #ifdef OSD_CROSSHAIR
1633        x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1634        y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1635    
1636        cairo_set_source_surface(cr, priv->crosshair.surface, x, y);
1637        cairo_paint(cr);
1638    #endif
1639    
1640    #ifdef OSD_NAV
1641        x =  OSD_X;
1642        if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1643        y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1644    
1645        cairo_set_source_surface(cr, priv->nav.surface, x, y);
1646        cairo_paint(cr);
1647    #endif
1648    
1649    #ifdef OSD_COORDINATES
1650        x = -OSD_X;
1651        y = -OSD_Y;
1652        if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1653        if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1654    
1655        cairo_set_source_surface(cr, priv->coordinates.surface, x, y);
1656        cairo_paint(cr);
1657    #endif
1658    
1659    #ifdef OSD_BALLOON
1660        if(priv->balloon.surface) {
1661    
1662            /* convert given lat lon into screen coordinates */
1663            gint x, y;
1664            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1665                                          priv->balloon.lat, priv->balloon.lon,
1666                                          &x, &y);
1667    
1668            /* check if balloon needs to be rerendered */
1669            osd_render_balloon(osd);
1670    
1671            cairo_set_source_surface(cr, priv->balloon.surface,
1672                                     x + priv->balloon.offset_x,
1673                                     y + priv->balloon.offset_y);
1674            cairo_paint(cr);
1675        }
1676    #endif
1677    
1678        x = OSD_X;
1679        if(x < 0)
1680            x += osd->widget->allocation.width - OSD_W;
1681    
1682        y = OSD_Y;
1683        if(y < 0)
1684            y += osd->widget->allocation.height - OSD_H;
1685    
1686        cairo_set_source_surface(cr, priv->controls.surface, x, y);
1687      cairo_paint(cr);      cairo_paint(cr);
1688    
1689  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1690      if(!priv->handler_id) {      if(!priv->source_sel.handler_id) {
1691          /* the OSD source selection is not being animated */          /* the OSD source selection is not being animated */
1692          if(!priv->expanded)          if(!priv->source_sel.expanded)
1693              x = osd->widget->allocation.width - OSD_S_W;              x = osd->widget->allocation.width - OSD_S_W;
1694          else          else
1695              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;              x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1696      } else      } else
1697          x = priv->shift;          x = priv->source_sel.shift;
1698    
1699      y = OSD_S_Y;      y = OSD_S_Y;
1700      if(OSD_S_Y < 0) {      if(OSD_S_Y < 0) {
1701          if(!priv->expanded)          if(!priv->source_sel.expanded)
1702              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;              y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1703          else          else
1704              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;              y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1705      }      }
1706    
1707      cairo_set_source_surface(cr, priv->map_source, x, y);      cairo_set_source_surface(cr, priv->source_sel.surface, x, y);
1708      cairo_paint(cr);      cairo_paint(cr);
1709  #endif  #endif
1710    
# Line 870  osd_free(osm_gps_map_osd_t *osd) Line 1716  osd_free(osm_gps_map_osd_t *osd)
1716  {  {
1717      osd_priv_t *priv = (osd_priv_t *)(osd->priv);      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1718    
1719      if (priv->overlay)      if (priv->controls.surface)
1720           cairo_surface_destroy(priv->overlay);           cairo_surface_destroy(priv->controls.surface);
1721    
1722  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1723      if(priv->handler_id)      if(priv->source_sel.handler_id)
1724          gtk_timeout_remove(priv->handler_id);          gtk_timeout_remove(priv->source_sel.handler_id);
1725    
1726        if (priv->source_sel.surface)
1727             cairo_surface_destroy(priv->source_sel.surface);
1728    #endif
1729    
1730    #ifdef OSD_SCALE
1731        if (priv->scale.surface)
1732             cairo_surface_destroy(priv->scale.surface);
1733    #endif
1734    
1735    #ifdef OSD_CROSSHAIR
1736        if (priv->crosshair.surface)
1737             cairo_surface_destroy(priv->crosshair.surface);
1738    #endif
1739    
1740    #ifdef OSD_NAV
1741        if (priv->nav.surface)
1742             cairo_surface_destroy(priv->nav.surface);
1743    #endif
1744    
1745      if (priv->map_source)  #ifdef OSD_COORDINATES
1746           cairo_surface_destroy(priv->map_source);      if (priv->coordinates.surface)
1747             cairo_surface_destroy(priv->coordinates.surface);
1748    #endif
1749    
1750    #ifdef OSD_BALLOON
1751        if (priv->balloon.surface)
1752             cairo_surface_destroy(priv->balloon.surface);
1753  #endif  #endif
1754    
1755      g_free(priv);      g_free(priv);
# Line 889  osd_busy(osm_gps_map_osd_t *osd) Line 1760  osd_busy(osm_gps_map_osd_t *osd)
1760  {  {
1761  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1762      osd_priv_t *priv = (osd_priv_t *)(osd->priv);      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1763      return (priv->handler_id != 0);      return (priv->source_sel.handler_id != 0);
1764  #else  #else
1765      return FALSE;      return FALSE;
1766  #endif  #endif
# Line 916  osm_gps_map_osd_classic_init(OsmGpsMap * Line 1787  osm_gps_map_osd_classic_init(OsmGpsMap *
1787  {  {
1788      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);
1789    
1790    #ifdef OSD_BALLOON
1791        priv->balloon.lat = OSM_GPS_MAP_INVALID;
1792        priv->balloon.lon = OSM_GPS_MAP_INVALID;
1793    #endif
1794    
1795      osd_classic.priv = priv;      osd_classic.priv = priv;
1796    
1797      osm_gps_map_register_osd(map, &osd_classic);      osm_gps_map_register_osd(map, &osd_classic);
# Line 945  osm_gps_map_osd_check(OsmGpsMap *map, gi Line 1821  osm_gps_map_osd_check(OsmGpsMap *map, gi
1821      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1822      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
1823    
1824      return osd_check(osd, x, y);      return osd_check(osd, TRUE, x, y);
1825  }  }

Legend:
Removed from v.89  
changed lines
  Added in v.122