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

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

revision 110 by harbaum, Sat Sep 12 20:56:35 2009 UTC revision 132 by harbaum, Thu Oct 1 08:09:00 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
# Line 45  typedef struct { Line 47  typedef struct {
47  #endif  #endif
48      } controls;      } 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  #ifdef OSD_SCALE
67      struct {      struct {
68          cairo_surface_t *surface;          cairo_surface_t *surface;
# Line 59  typedef struct { Line 77  typedef struct {
77      } crosshair;      } crosshair;
78  #endif  #endif
79    
80    #ifdef OSD_NAV
81        struct {
82            cairo_surface_t *surface;
83            float lat, lon;
84            char *name;
85        } nav;
86    #endif
87    
88  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
89      struct {      struct {
90          cairo_surface_t *surface;          cairo_surface_t *surface;
# Line 74  typedef struct { Line 100  typedef struct {
100          gint shift, dir, count;          gint shift, dir, count;
101          gint handler_id;          gint handler_id;
102          gint width, height;          gint width, height;
103            gboolean rendered;
104      } source_sel;      } source_sel;
105  #endif  #endif
106    
107  } osd_priv_t;  } osd_priv_t;
108    
109    #ifdef OSD_BALLOON
110    /* most visual effects are hardcoded by now, but may be made */
111    /* available via properties later */
112    #ifndef BALLOON_AREA_WIDTH
113    #define BALLOON_AREA_WIDTH           290
114    #endif
115    #ifndef BALLOON_AREA_HEIGHT
116    #define BALLOON_AREA_HEIGHT           75
117    #endif
118    #ifndef BALLOON_CORNER_RADIUS
119    #define BALLOON_CORNER_RADIUS         10
120    #endif
121    
122    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
123    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
124    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
125    #define BALLOON_TRANSPARENCY         0.8
126    #define POINTER_HEIGHT                20
127    #define POINTER_FOOT_WIDTH            20
128    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
129    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
130    #define BALLOON_SHADOW_TRANSPARENCY  0.2
131    
132    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
133    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
134    
135    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
136    
137    
138    /* draw the bubble shape. this is used twice, once for the shape and once */
139    /* for the shadow */
140    static void
141    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
142           gboolean bottom, int px, int py, int px0, int px1) {
143    
144        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
145        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
146                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
147        if(!bottom) {
148            /* insert top pointer */
149            cairo_line_to (cr, px1, y0);
150            cairo_line_to (cr, px, py);
151            cairo_line_to (cr, px0, y0);
152        }
153    
154        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
155        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
156                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
157        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
158        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
159                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
160        if(bottom) {
161            /* insert bottom pointer */
162            cairo_line_to (cr, px0, y1);
163            cairo_line_to (cr, px, py);
164            cairo_line_to (cr, px1, y1);
165        }
166    
167        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
168        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
169                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
170    
171        cairo_close_path (cr);
172    }
173    
174    static void
175    osd_render_balloon(osm_gps_map_osd_t *osd) {
176        osd_priv_t *priv = (osd_priv_t*)osd->priv;
177    
178        /* get zoom */
179        gint zoom;
180        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
181    
182        /* ------- convert given coordinate into screen position --------- */
183        gint xs, ys;
184        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
185                                          priv->balloon.lat, priv->balloon.lon,
186                                          &xs, &ys);
187    
188        gint x0 = 1, y0 = 1;
189    
190        /* check position of this relative to screen center to determine */
191        /* pointer direction ... */
192        int pointer_x, pointer_x0, pointer_x1;
193        int pointer_y;
194    
195        /* ... and calculate position */
196        int orientation = 0;
197        if(xs > osd->widget->allocation.width/2) {
198            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
199            pointer_x = x0 - priv->balloon.offset_x;
200            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
201            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
202            orientation |= 1;
203        } else {
204            priv->balloon.offset_x = -POINTER_OFFSET;
205            pointer_x = x0 - priv->balloon.offset_x;
206            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
207            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
208        }
209    
210        gboolean bottom = FALSE;
211        if(ys > osd->widget->allocation.height/2) {
212            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
213            pointer_y = y0 - priv->balloon.offset_y;
214            bottom = TRUE;
215            orientation |= 2;
216        } else {
217            priv->balloon.offset_y = 0;
218            pointer_y = y0 - priv->balloon.offset_y;
219            y0 += POINTER_HEIGHT;
220        }
221    
222        /* if required orientation equals current one, then don't render */
223        /* anything */
224        if(orientation == priv->balloon.orientation)
225            return;
226    
227        priv->balloon.orientation = orientation;
228    
229        /* calculate bottom/right of box */
230        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
231    
232        /* save balloon screen coordinates for later use */
233        priv->balloon.rect.x = x0 + BALLOON_BORDER;
234        priv->balloon.rect.y = y0 + BALLOON_BORDER;
235        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
236        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
237    
238        cairo_t *cr = cairo_create(priv->balloon.surface);
239        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
240        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
241        cairo_paint(cr);
242        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
243    
244        /* --------- draw shadow --------------- */
245        osm_gps_map_draw_balloon_shape (cr,
246                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
247                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
248                     bottom, pointer_x, pointer_y,
249                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
250    
251        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
252        cairo_fill_preserve (cr);
253        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
254        cairo_set_line_width (cr, 0);
255        cairo_stroke (cr);
256    
257        /* --------- draw main shape ----------- */
258        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
259                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
260    
261        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
262        cairo_fill_preserve (cr);
263        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
264        cairo_set_line_width (cr, 1);
265        cairo_stroke (cr);
266    
267        if (priv->balloon.cb) {
268            /* clip in case application tries to draw in */
269                /* exceed of the balloon */
270            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
271                             priv->balloon.rect.w, priv->balloon.rect.h);
272            cairo_clip (cr);
273            cairo_new_path (cr);  /* current path is not
274                                     consumed by cairo_clip() */
275    
276            priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
277        }
278    
279        cairo_destroy(cr);
280    }
281    
282    /* return true if balloon is being displayed and if */
283    /* the given coordinate is within this balloon */
284    static gboolean
285    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y)
286    {
287        osd_priv_t *priv = (osd_priv_t*)osd->priv;
288    
289        if(!priv->balloon.surface)
290            return FALSE;
291    
292        gint xs, ys;
293        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
294                                          priv->balloon.lat, priv->balloon.lon,
295                                          &xs, &ys);
296    
297        xs += priv->balloon.rect.x + priv->balloon.offset_x;
298        ys += priv->balloon.rect.y + priv->balloon.offset_y;
299    
300        gboolean is_in =
301            (x > xs) && (x < xs + priv->balloon.rect.w) &&
302            (y > ys) && (y < ys + priv->balloon.rect.h);
303    
304        /* handle the fact that the balloon may have been created by the */
305        /* button down event */
306        if(!is_in && !down && !priv->balloon.just_created) {
307            /* the user actually clicked outside the balloon */
308    
309            /* close the balloon! */
310            osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
311        }
312    
313        return is_in;
314    }
315    
316    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
317        g_return_if_fail (OSM_IS_GPS_MAP (map));
318    
319        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
320        g_return_if_fail (osd);
321    
322        osd_priv_t *priv = (osd_priv_t*)osd->priv;
323        g_return_if_fail (priv);
324    
325        if(priv->balloon.surface) {
326            cairo_surface_destroy(priv->balloon.surface);
327            priv->balloon.surface = NULL;
328            priv->balloon.lat = OSM_GPS_MAP_INVALID;
329            priv->balloon.lon = OSM_GPS_MAP_INVALID;
330        }
331        osm_gps_map_redraw(map);
332    }
333    
334    void
335    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
336                                  OsmGpsMapBalloonCallback cb, gpointer data) {
337        g_return_if_fail (OSM_IS_GPS_MAP (map));
338    
339        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
340        g_return_if_fail (osd);
341    
342        osd_priv_t *priv = (osd_priv_t*)osd->priv;
343        g_return_if_fail (priv);
344    
345        osm_gps_map_osd_clear_balloon (map);
346    
347        /* allocate balloon surface */
348        priv->balloon.surface =
349            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
350                                       BALLOON_W+2, BALLOON_H+2);
351    
352        priv->balloon.lat = latitude;
353        priv->balloon.lon = longitude;
354        priv->balloon.cb = cb;
355        priv->balloon.data = data;
356        priv->balloon.just_created = TRUE;
357    
358        priv->balloon.orientation = -1;
359    
360        osd_render_balloon(osd);
361    
362        osm_gps_map_redraw(map);
363    }
364    
365    #endif // OSD_BALLOON
366    
367  /* position and extent of bounding box */  /* position and extent of bounding box */
368  #ifndef OSD_X  #ifndef OSD_X
369  #define OSD_X      (10)  #define OSD_X      (10)
# Line 416  osd_source_content(osm_gps_map_osd_t *os Line 701  osd_source_content(osm_gps_map_osd_t *os
701  }  }
702    
703  static void  static void
704  osd_render_source_sel(osm_gps_map_osd_t *osd) {  osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
705      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
706    
707        if(priv->source_sel.rendered && !force_rerender)
708            return;
709    
710        priv->source_sel.rendered = TRUE;
711    
712  #ifndef OSD_COLOR  #ifndef OSD_COLOR
713      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
714      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];      GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
# Line 503  osd_source_reallocate(osm_gps_map_osd_t Line 793  osd_source_reallocate(osm_gps_map_osd_t
793      priv->source_sel.surface =      priv->source_sel.surface =
794          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
795    
796      osd_render_source_sel(osd);      osd_render_source_sel(osd, TRUE);
797  }  }
798    
799  #define OSD_HZ      15  #define OSD_HZ      15
# Line 535  static gboolean osd_source_animate(gpoin Line 825  static gboolean osd_source_animate(gpoin
825    
826      /* 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 */
827    
828      /* nicer sinoid mapping */      /* nice sinoid mapping */
829      float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;      float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;
830      priv->source_sel.shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +      priv->source_sel.shift =
831            (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
832          m * diff;          m * diff;
833    
834        /* make sure the screen is updated */
835      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));      osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
836    
837        /* stop animation if done */
838      if(done)      if(done)
839          priv->source_sel.handler_id = 0;          priv->source_sel.handler_id = 0;
840    
# Line 558  osd_source_toggle(osm_gps_map_osd_t *osd Line 851  osd_source_toggle(osm_gps_map_osd_t *osd
851      if(priv->source_sel.handler_id)      if(priv->source_sel.handler_id)
852          return;          return;
853    
854      /* expand immediately, collapse is handle at the end of the collapse animation */      /* expand immediately, collapse is handle at the end of the */
855        /* collapse animation */
856      if(!priv->source_sel.expanded) {      if(!priv->source_sel.expanded) {
857          priv->source_sel.expanded = TRUE;          priv->source_sel.expanded = TRUE;
858          osd_source_reallocate(osd);          osd_source_reallocate(osd);
# Line 568  osd_source_toggle(osm_gps_map_osd_t *osd Line 862  osd_source_toggle(osm_gps_map_osd_t *osd
862          priv->source_sel.dir = -1000/OSD_HZ;          priv->source_sel.dir = -1000/OSD_HZ;
863      } else {      } else {
864          priv->source_sel.count =  0;          priv->source_sel.count =  0;
865          priv->source_sel.shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;          priv->source_sel.shift = osd->widget->allocation.width -
866                OSD_S_EXP_W + OSD_S_X;
867          priv->source_sel.dir = +1000/OSD_HZ;          priv->source_sel.dir = +1000/OSD_HZ;
868      }      }
869    
870      priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);      /* start timer to handle animation */
871        priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
872                                                      osd_source_animate, osd);
873  }  }
874    
875  /* check if the user clicked inside the source selection area */  /* check if the user clicked inside the source selection area */
876  static osd_button_t  static osd_button_t
877  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) {
878      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
879    
880      if(!priv->source_sel.expanded)      if(!priv->source_sel.expanded)
# Line 595  osd_source_check(osm_gps_map_osd_t *osd, Line 892  osd_source_check(osm_gps_map_osd_t *osd,
892          /* really within puller shape? */          /* really within puller shape? */
893          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)) {
894              /* expand source selector */              /* expand source selector */
895              osd_source_toggle(osd);              if(down)
896                    osd_source_toggle(osd);
897    
898              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
899              /* of the OSD */              /* of the OSD */
# Line 621  osd_source_check(osm_gps_map_osd_t *osd, Line 919  osd_source_check(osm_gps_map_osd_t *osd,
919              y /= step;              y /= step;
920              y += 1;              y += 1;
921    
922              gint old = 0;              if(down) {
923              g_object_get(osd->widget, "map-source", &old, NULL);                  gint old = 0;
924                    g_object_get(osd->widget, "map-source", &old, NULL);
925              if(y > OSM_GPS_MAP_SOURCE_NULL &&  
926                 y <= OSM_GPS_MAP_SOURCE_LAST &&                  if(y > OSM_GPS_MAP_SOURCE_NULL &&
927                 old != y) {                     y <= OSM_GPS_MAP_SOURCE_LAST &&
928                  g_object_set(osd->widget, "map-source", y, NULL);                     old != y) {
929                        g_object_set(osd->widget, "map-source", y, NULL);
930                  osd_render_source_sel(osd);  
931                  osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));                      osd_render_source_sel(osd, TRUE);
932                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
933                    }
934              }              }
935    
936              /* return "clicked in OSD background" to prevent further */              /* return "clicked in OSD background" to prevent further */
# Line 644  osd_source_check(osm_gps_map_osd_t *osd, Line 944  osd_source_check(osm_gps_map_osd_t *osd,
944  #endif // OSD_SOURCE_SEL  #endif // OSD_SOURCE_SEL
945    
946  static osd_button_t  static osd_button_t
947  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) {
948      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
949    
950    #ifdef OSD_BALLOON
951        if(down) {
952            /* needed to handle balloons that are created at click */
953            osd_priv_t *priv = (osd_priv_t*)osd->priv;
954            priv->balloon.just_created = FALSE;
955        }
956    #endif
957    
958  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
959      /* the source selection area is handles internally */      /* the source selection area is handles internally */
960      but = osd_source_check(osd, x, y);      but = osd_source_check(osd, down, x, y);
     if(but != OSD_NONE)  
         return but;  
961  #endif  #endif
962    
963      x -= OSD_X;      if(but == OSD_NONE) {
964      y -= OSD_Y;          gint mx = x - OSD_X;
965            gint my = y - OSD_Y;
966      if(OSD_X < 0)  
967          x -= (osd->widget->allocation.width - OSD_W);          if(OSD_X < 0)
968                mx -= (osd->widget->allocation.width - OSD_W);
969      if(OSD_Y < 0)  
970          y -= (osd->widget->allocation.height - OSD_H);          if(OSD_Y < 0)
971                my -= (osd->widget->allocation.height - OSD_H);
972      /* first do a rough test for the OSD area. */  
973      /* this is just to avoid an unnecessary detailed test */          /* first do a rough test for the OSD area. */
974      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {          /* this is just to avoid an unnecessary detailed test */
975            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
976  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
977          but = osd_check_dpad(x, y);              but = osd_check_dpad(mx, my);
978  #endif  #endif
979            }
980    
981          if(but == OSD_NONE)          if(but == OSD_NONE)
982              but = osd_check_zoom(x, y);              but = osd_check_zoom(mx, my);
983      }      }
984    
985    #ifdef OSD_BALLOON
986        if(but == OSD_NONE) {
987            /* check if user clicked into balloon */
988            if(osd_balloon_check(osd, down, x, y))
989                but = OSD_BG;
990        }
991    #endif
992    
993      return but;      return but;
994  }  }
995    
# Line 756  osd_zoom_labels(cairo_t *cr, gint x, gin Line 1072  osd_zoom_labels(cairo_t *cr, gint x, gin
1072  #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)  #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
1073    
1074  #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)  #define OSD_COORDINATES_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1075  #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE+OSD_COORDINATES_OFFSET)  #define OSD_COORDINATES_H  (2*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET+OSD_COORDINATES_FONT_SIZE/4)
1076    
1077  /* these can be overwritten with versions that support */  /* these can be overwritten with versions that support */
1078  /* localization */  /* localization */
# Line 814  static char Line 1130  static char
1130                             c, (int)integral, fractional*60.0);                             c, (int)integral, fractional*60.0);
1131  }  }
1132    
1133    /* render a string at the given screen position */
1134    static int
1135    osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1136        char *p = g_strdup(text);
1137        cairo_text_extents_t extents;
1138        cairo_text_extents (cr, p, &extents);
1139    
1140        /* check if text needs to be truncated */
1141        int len = strlen(text)-2;
1142        while(extents.width > width) {
1143            len--;
1144            strcpy(p+len, "...");
1145            cairo_text_extents (cr, p, &extents);
1146        }
1147    
1148        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1149        cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1150        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1151        cairo_text_path (cr, p);
1152        cairo_stroke (cr);
1153    
1154        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1155        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1156        cairo_show_text (cr, p);
1157    
1158        g_free(p);
1159    
1160        /* skip + 1/4 line */
1161        return y + 5*OSD_COORDINATES_FONT_SIZE/4;
1162    }
1163    
1164  static void  static void
1165  osd_render_coordinates(osm_gps_map_osd_t *osd)  osd_render_coordinates(osm_gps_map_osd_t *osd)
1166  {  {
# Line 833  osd_render_coordinates(osm_gps_map_osd_t Line 1180  osd_render_coordinates(osm_gps_map_osd_t
1180      priv->coordinates.lat = lat;      priv->coordinates.lat = lat;
1181      priv->coordinates.lon = lon;      priv->coordinates.lon = lon;
1182    
1183      /* first fill with light transparency */      /* first fill with transparency */
1184      cairo_t *cr = cairo_create(priv->coordinates.surface);      cairo_t *cr = cairo_create(priv->coordinates.surface);
1185      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1186      cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);      //    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1187        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1188      cairo_paint(cr);      cairo_paint(cr);
1189      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1190    
# Line 848  osd_render_coordinates(osm_gps_map_osd_t Line 1196  osd_render_coordinates(osm_gps_map_osd_t
1196      char *latitude = osd_latitude_str(lat);      char *latitude = osd_latitude_str(lat);
1197      char *longitude = osd_longitude_str(lon);      char *longitude = osd_longitude_str(lon);
1198    
1199      cairo_text_extents_t lat_extents, lon_extents;      int y = OSD_COORDINATES_OFFSET;
1200      cairo_text_extents (cr, latitude, &lat_extents);      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1201      cairo_text_extents (cr, longitude, &lon_extents);      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1202    
1203        g_free(latitude);
1204        g_free(longitude);
1205    
1206      cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);      cairo_destroy(cr);
1207      cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);  }
1208      cairo_move_to (cr,  #endif  // OSD_COORDINATES
                    (OSD_COORDINATES_W - lat_extents.width)/2,  
                    OSD_COORDINATES_OFFSET - lat_extents.y_bearing);  
     cairo_text_path (cr, latitude);  
     cairo_move_to (cr,  
                    (OSD_COORDINATES_W - lon_extents.width)/2,  
                    OSD_COORDINATES_OFFSET - lon_extents.y_bearing +  
                    OSD_COORDINATES_FONT_SIZE);  
     cairo_text_path (cr, longitude);  
     cairo_stroke (cr);  
1209    
1210      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);  #ifdef OSD_NAV
1211      cairo_move_to (cr,  #define OSD_NAV_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1212                     (OSD_COORDINATES_W - lat_extents.width)/2,  #define OSD_NAV_H  (130)
1213                     OSD_COORDINATES_OFFSET - lat_extents.y_bearing);  
1214      cairo_show_text (cr, latitude);  /* http://mathforum.org/library/drmath/view/55417.html */
1215      cairo_move_to (cr,  static float get_bearing(float lat1, float lon1, float lat2, float lon2) {
1216                     (OSD_COORDINATES_W - lon_extents.width)/2,    return atan2( sin(lon2 - lon1) * cos(lat2),
1217                     OSD_COORDINATES_OFFSET - lon_extents.y_bearing +                  cos(lat1) * sin(lat2) -
1218                     OSD_COORDINATES_FONT_SIZE);                  sin(lat1) * cos(lat2) * cos(lon2 - lon1));
1219      cairo_show_text (cr, longitude);  }
1220    
1221    /* http://mathforum.org/library/drmath/view/51722.html */
1222    static float get_distance(float lat1, float lon1, float lat2, float lon2) {
1223      float aob = acos(cos(lat1) * cos(lat2) * cos(lon2 - lon1) +
1224                       sin(lat1) * sin(lat2));
1225    
1226      //  return(aob * 3959.0);   /* great circle radius in miles */
1227    
1228      return(aob * 6371000.0);     /* great circle radius in meters */
1229    }
1230    
1231    static void
1232    osd_render_nav(osm_gps_map_osd_t *osd)
1233    {
1234        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1235    
1236        if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1237            return;
1238    
1239        /* first fill with transparency */
1240        cairo_t *cr = cairo_create(priv->nav.surface);
1241        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1242        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1243        cairo_paint(cr);
1244        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1245    
1246        cairo_select_font_face (cr, "Sans",
1247                                CAIRO_FONT_SLANT_NORMAL,
1248                                CAIRO_FONT_WEIGHT_BOLD);
1249        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1250    
1251        char *latitude = osd_latitude_str(priv->nav.lat);
1252        char *longitude = osd_longitude_str(priv->nav.lon);
1253    
1254        int y = OSD_COORDINATES_OFFSET;
1255        y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1256        y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1257        y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1258    
1259      g_free(latitude);      g_free(latitude);
1260      g_free(longitude);      g_free(longitude);
1261    
1262        /* draw the compass */
1263        int radius = (OSD_NAV_H - y - 5*OSD_COORDINATES_FONT_SIZE/4)/2;
1264        if(radius > OSD_NAV_W/2)
1265            radius = OSD_NAV_W/2;
1266    
1267        int x = OSD_NAV_W/2+1;
1268        y += radius;
1269    
1270        cairo_stroke (cr);
1271    
1272        /* draw background */
1273        cairo_arc(cr, x, y, radius, 0,  2*M_PI);
1274        cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
1275        cairo_fill_preserve (cr);
1276        cairo_set_source_rgb (cr, 0, 0, 0);
1277        cairo_set_line_width (cr, 1);
1278        cairo_stroke (cr);
1279    
1280        /* draw pointer */
1281    #define ARROW_WIDTH     0.3
1282    #define ARROW_LENGTH    0.7
1283    
1284        coord_t *gps = osm_gps_map_get_gps (OSM_GPS_MAP(osd->widget));
1285        if(gps) {
1286            float arot = get_bearing(gps->rlat, gps->rlon,
1287                         deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1288    
1289            cairo_move_to(cr,
1290                          x + radius *  ARROW_LENGTH *  sin(arot),
1291                          y + radius *  ARROW_LENGTH * -cos(arot));
1292    
1293            cairo_line_to(cr,
1294                          x + radius * -ARROW_LENGTH *  sin(arot+ARROW_WIDTH),
1295                          y + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH));
1296    
1297            cairo_line_to(cr,
1298                          x + radius * -0.5 * ARROW_LENGTH *  sin(arot),
1299                          y + radius * -0.5 * ARROW_LENGTH * -cos(arot));
1300    
1301            cairo_line_to(cr,
1302                          x + radius * -ARROW_LENGTH *  sin(arot-ARROW_WIDTH),
1303                          y + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH));
1304    
1305            cairo_close_path(cr);
1306            cairo_set_source_rgb (cr, 0, 0, 0);
1307            cairo_fill (cr);
1308    
1309            y += radius + OSD_COORDINATES_FONT_SIZE/4;
1310    
1311            float dist = get_distance(gps->rlat, gps->rlon,
1312                            deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1313    
1314            char *dist_str = NULL;
1315            if(dist<1000)
1316                dist_str = g_strdup_printf("%u m", (int)dist);
1317            else
1318                dist_str = g_strdup_printf("%.1f km", dist/1000);
1319    
1320            y = osd_render_centered_text(cr, y, OSD_NAV_W, dist_str);
1321            g_free(dist_str);
1322        }
1323    
1324      cairo_destroy(cr);      cairo_destroy(cr);
1325  }  }
1326  #endif  // OSD_COORDINATES  
1327    void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1328        g_return_if_fail (OSM_IS_GPS_MAP (map));
1329    
1330        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1331        g_return_if_fail (osd);
1332    
1333        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1334        g_return_if_fail (priv);
1335    
1336        if(priv->nav.surface) {
1337            cairo_surface_destroy(priv->nav.surface);
1338            priv->nav.surface = NULL;
1339            priv->nav.lat = OSM_GPS_MAP_INVALID;
1340            priv->nav.lon = OSM_GPS_MAP_INVALID;
1341            if(priv->nav.name) g_free(priv->nav.name);
1342        }
1343        osm_gps_map_redraw(map);
1344    }
1345    
1346    void
1347    osm_gps_map_osd_draw_nav (OsmGpsMap *map, float latitude, float longitude,
1348                              char *name) {
1349        g_return_if_fail (OSM_IS_GPS_MAP (map));
1350    
1351        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1352        g_return_if_fail (osd);
1353    
1354        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1355        g_return_if_fail (priv);
1356    
1357        osm_gps_map_osd_clear_nav (map);
1358    
1359        /* allocate balloon surface */
1360        priv->nav.surface =
1361            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1362                                       OSD_NAV_W+2, OSD_NAV_H+2);
1363    
1364        priv->nav.lat = latitude;
1365        priv->nav.lon = longitude;
1366        priv->nav.name = g_strdup(name);
1367    
1368        osd_render_nav(osd);
1369    
1370        osm_gps_map_redraw(map);
1371    }
1372    
1373    #endif // OSD_NAV
1374    
1375    
1376  #ifdef OSD_CROSSHAIR  #ifdef OSD_CROSSHAIR
1377    
# Line 1191  osd_render_controls(osm_gps_map_osd_t *o Line 1681  osd_render_controls(osm_gps_map_osd_t *o
1681  static void  static void
1682  osd_render(osm_gps_map_osd_t *osd)  osd_render(osm_gps_map_osd_t *osd)
1683  {  {
1684        /* this function is actually called pretty often since the */
1685        /* OSD contents may have changed (due to a coordinate/zoom change). */
1686        /* The different OSD parts have to make sure that they don't */
1687        /* render unneccessarily often and thus waste CPU power */
1688    
1689      osd_render_controls(osd);      osd_render_controls(osd);
1690    
1691  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
1692      osd_render_source_sel(osd);      osd_render_source_sel(osd, FALSE);
1693  #endif  #endif
1694    
1695  #ifdef OSD_SCALE  #ifdef OSD_SCALE
# Line 1205  osd_render(osm_gps_map_osd_t *osd) Line 1700  osd_render(osm_gps_map_osd_t *osd)
1700      osd_render_crosshair(osd);      osd_render_crosshair(osd);
1701  #endif  #endif
1702    
1703    #ifdef OSD_NAV
1704        osd_render_nav(osd);
1705    #endif
1706    
1707  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
1708      osd_render_coordinates(osd);      osd_render_coordinates(osd);
1709  #endif  #endif
# Line 1232  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1731  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1731          priv->source_sel.surface =          priv->source_sel.surface =
1732              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1733                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
1734            priv->source_sel.rendered = FALSE;
1735  #endif  #endif
1736    
1737  #ifdef OSD_SCALE  #ifdef OSD_SCALE
# Line 1263  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1763  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1763      // now draw this onto the original context      // now draw this onto the original context
1764      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1765    
1766      int x, y;      gint x, y;
1767    
1768  #ifdef OSD_SCALE  #ifdef OSD_SCALE
1769      x =  OSD_X;      x =  OSD_X;
# Line 1283  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1783  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1783      cairo_paint(cr);      cairo_paint(cr);
1784  #endif  #endif
1785    
1786    #ifdef OSD_NAV
1787        if(priv->nav.surface) {
1788            x =  OSD_X;
1789            if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1790            y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1791    
1792            cairo_set_source_surface(cr, priv->nav.surface, x, y);
1793            cairo_paint(cr);
1794        }
1795    #endif
1796    
1797  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
1798      x = -OSD_X;      x = -OSD_X;
1799      y = -OSD_Y;      y = -OSD_Y;
# Line 1293  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1804  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1804      cairo_paint(cr);      cairo_paint(cr);
1805  #endif  #endif
1806    
1807    #ifdef OSD_BALLOON
1808        if(priv->balloon.surface) {
1809    
1810            /* convert given lat lon into screen coordinates */
1811            gint x, y;
1812            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1813                                          priv->balloon.lat, priv->balloon.lon,
1814                                          &x, &y);
1815    
1816            /* check if balloon needs to be rerendered */
1817            osd_render_balloon(osd);
1818    
1819            cairo_set_source_surface(cr, priv->balloon.surface,
1820                                     x + priv->balloon.offset_x,
1821                                     y + priv->balloon.offset_y);
1822            cairo_paint(cr);
1823        }
1824    #endif
1825    
1826      x = OSD_X;      x = OSD_X;
1827      if(x < 0)      if(x < 0)
1828          x += osd->widget->allocation.width - OSD_W;          x += osd->widget->allocation.width - OSD_W;
# Line 1355  osd_free(osm_gps_map_osd_t *osd) Line 1885  osd_free(osm_gps_map_osd_t *osd)
1885           cairo_surface_destroy(priv->crosshair.surface);           cairo_surface_destroy(priv->crosshair.surface);
1886  #endif  #endif
1887    
1888    #ifdef OSD_NAV
1889        if (priv->nav.surface)
1890             cairo_surface_destroy(priv->nav.surface);
1891    #endif
1892    
1893  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
1894      if (priv->coordinates.surface)      if (priv->coordinates.surface)
1895           cairo_surface_destroy(priv->coordinates.surface);           cairo_surface_destroy(priv->coordinates.surface);
1896  #endif  #endif
1897    
1898    #ifdef OSD_BALLOON
1899        if (priv->balloon.surface)
1900             cairo_surface_destroy(priv->balloon.surface);
1901    #endif
1902    
1903      g_free(priv);      g_free(priv);
1904  }  }
1905    
# Line 1395  osm_gps_map_osd_classic_init(OsmGpsMap * Line 1935  osm_gps_map_osd_classic_init(OsmGpsMap *
1935  {  {
1936      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);
1937    
1938    #ifdef OSD_BALLOON
1939        priv->balloon.lat = OSM_GPS_MAP_INVALID;
1940        priv->balloon.lon = OSM_GPS_MAP_INVALID;
1941    #endif
1942    
1943      osd_classic.priv = priv;      osd_classic.priv = priv;
1944    
1945      osm_gps_map_register_osd(map, &osd_classic);      osm_gps_map_register_osd(map, &osd_classic);
# Line 1424  osm_gps_map_osd_check(OsmGpsMap *map, gi Line 1969  osm_gps_map_osd_check(OsmGpsMap *map, gi
1969      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1970      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
1971    
1972      return osd_check(osd, x, y);      return osd_check(osd, TRUE, x, y);
1973  }  }

Legend:
Removed from v.110  
changed lines
  Added in v.132