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

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

revision 112 by harbaum, Tue Sep 15 13:52:04 2009 UTC revision 133 by harbaum, Mon Oct 12 20:27:55 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 50  typedef struct { Line 51  typedef struct {
51      //a balloon with additional info      //a balloon with additional info
52      struct {      struct {
53          cairo_surface_t *surface;          cairo_surface_t *surface;
54            int orientation, offset_x, offset_y;
55    
56            gboolean just_created;
57          float lat, lon;          float lat, lon;
         gboolean valid;  
58          OsmGpsMapRect_t rect;          OsmGpsMapRect_t rect;
59    
60          /* function called to have the user app draw the contents */          /* function called to have the user app draw the contents */
# Line 75  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            gboolean imperial;    // display distance imperial/metric
86        } nav;
87    #endif
88    
89  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
90      struct {      struct {
91          cairo_surface_t *surface;          cairo_surface_t *surface;
# Line 119  typedef struct { Line 130  typedef struct {
130  #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)  #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
131  #define BALLOON_SHADOW_TRANSPARENCY  0.2  #define BALLOON_SHADOW_TRANSPARENCY  0.2
132    
133    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
134    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
135    
136  #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)  #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
137    
138    
# Line 158  osm_gps_map_draw_balloon_shape (cairo_t Line 172  osm_gps_map_draw_balloon_shape (cairo_t
172      cairo_close_path (cr);      cairo_close_path (cr);
173  }  }
174    
 /* xyz */  
   
175  static void  static void
176  osd_render_balloon(osm_gps_map_osd_t *osd) {  osd_render_balloon(osm_gps_map_osd_t *osd) {
177      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
# Line 169  osd_render_balloon(osm_gps_map_osd_t *os Line 181  osd_render_balloon(osm_gps_map_osd_t *os
181      g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);      g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
182    
183      /* ------- convert given coordinate into screen position --------- */      /* ------- convert given coordinate into screen position --------- */
184      gint x0, y0;      gint xs, ys;
185      osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),      osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
186                                        priv->balloon.lat, priv->balloon.lon,                                        priv->balloon.lat, priv->balloon.lon,
187                                        &x0, &y0);                                        &xs, &ys);
188    
189        gint x0 = 1, y0 = 1;
190    
191      /* check position of this relative to screen center to determine */      /* check position of this relative to screen center to determine */
192      /* pointer direction ... */      /* pointer direction ... */
193      int pointer_x = x0, pointer_x0, pointer_x1;      int pointer_x, pointer_x0, pointer_x1;
194      int pointer_y = y0;      int pointer_y;
195    
196      /* ... and calculate position */      /* ... and calculate position */
197      if(x0 > osd->widget->allocation.width/2) {      int orientation = 0;
198          x0 += POINTER_OFFSET;      if(xs > osd->widget->allocation.width/2) {
199            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
200            pointer_x = x0 - priv->balloon.offset_x;
201          pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);          pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
202          pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;          pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
203            orientation |= 1;
204      } else {      } else {
205          x0 -= POINTER_OFFSET;          priv->balloon.offset_x = -POINTER_OFFSET;
206            pointer_x = x0 - priv->balloon.offset_x;
207          pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);          pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
208          pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;          pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
209      }      }
210    
211      gboolean bottom = FALSE;      gboolean bottom = FALSE;
212      if(y0 > osd->widget->allocation.height/2) {      if(ys > osd->widget->allocation.height/2) {
213            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
214            pointer_y = y0 - priv->balloon.offset_y;
215          bottom = TRUE;          bottom = TRUE;
216          y0 -= BALLOON_HEIGHT + POINTER_HEIGHT;          orientation |= 2;
217      } else      } else {
218            priv->balloon.offset_y = 0;
219            pointer_y = y0 - priv->balloon.offset_y;
220          y0 += POINTER_HEIGHT;          y0 += POINTER_HEIGHT;
221        }
222    
223        /* if required orientation equals current one, then don't render */
224        /* anything */
225        if(orientation == priv->balloon.orientation)
226            return;
227    
228        priv->balloon.orientation = orientation;
229    
230      /* calculate bottom/right of box */      /* calculate bottom/right of box */
231      int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;      int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
232    
# Line 207  osd_render_balloon(osm_gps_map_osd_t *os Line 237  osd_render_balloon(osm_gps_map_osd_t *os
237      priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;      priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
238    
239      cairo_t *cr = cairo_create(priv->balloon.surface);      cairo_t *cr = cairo_create(priv->balloon.surface);
240        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
241        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
242        cairo_paint(cr);
243        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
244    
245      /* --------- draw shadow --------------- */      /* --------- draw shadow --------------- */
246      osm_gps_map_draw_balloon_shape (cr,      osm_gps_map_draw_balloon_shape (cr,
# Line 231  osd_render_balloon(osm_gps_map_osd_t *os Line 265  osd_render_balloon(osm_gps_map_osd_t *os
265      cairo_set_line_width (cr, 1);      cairo_set_line_width (cr, 1);
266      cairo_stroke (cr);      cairo_stroke (cr);
267    
   
     /* ---------- draw close button --------- */  
   
     int cx = x1 - BALLOON_BORDER - CLOSE_BUTTON_RADIUS;  
     int cy = y0 + BALLOON_BORDER + CLOSE_BUTTON_RADIUS;  
     int crad = CLOSE_BUTTON_RADIUS;  
   
     cairo_arc (cr, cx, cy, crad, 0, 2 * M_PI);  
     cairo_set_source_rgba (cr, 0.8, 0, 0, 1.0);  
     cairo_fill_preserve (cr);  
     cairo_set_source_rgba (cr, 0.3, 0, 0, 1.0);  
     cairo_set_line_width (cr, 2);  
     cairo_stroke(cr);  
   
     cairo_set_source_rgba (cr, 1, 1, 1, 1.0);  
     cairo_set_line_width (cr, BALLOON_CORNER_RADIUS/3.3);  
     cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);  
     cairo_move_to (cr, cx - crad/2, cy - crad/2);  
     cairo_line_to (cr, cx + crad/2, cy + crad/2);  
     cairo_stroke (cr);  
     cairo_move_to (cr, cx + crad/2, cy - crad/2);  
     cairo_line_to (cr, cx - crad/2, cy + crad/2);  
     cairo_stroke (cr);  
   
268      if (priv->balloon.cb) {      if (priv->balloon.cb) {
269          /* clip in case application tries to draw in */          /* clip in case application tries to draw in */
270              /* exceed of the balloon */              /* exceed of the balloon */
# Line 266  osd_render_balloon(osm_gps_map_osd_t *os Line 276  osd_render_balloon(osm_gps_map_osd_t *os
276    
277          priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);          priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
278      }      }
   
     cairo_destroy(cr);  
 }  
   
 #if 0  
 /* the user clicked into the balloons main area. handle this */  
 static void  
 osm_gps_map_handle_balloon_click(OsmGpsMap *map, gint x, gint y)  
 {  
     OsmGpsMapPrivate *priv = map->priv;  
   
     if (!priv->balloon.valid)  
         return;  
   
     /* check if the close button was clicked */  
     if ((x > priv->balloon.rect.w - 2*CLOSE_BUTTON_RADIUS) &&  
         (x < priv->balloon.rect.w) &&  
         (y > 0) && (y < 2*CLOSE_BUTTON_RADIUS)) {  
279    
280          priv->balloon.valid = FALSE;      cairo_destroy(cr);
         osm_gps_map_map_redraw_idle(map);  
     }  
281  }  }
282    
283  /* return true if balloon is being displayed and if */  /* return true if balloon is being displayed and if */
284  /* the given coordinate is within this balloon */  /* the given coordinate is within this balloon */
285  static gboolean  static gboolean
286  osm_gps_map_in_balloon(OsmGpsMapPrivate *priv, gint x, gint y)  osd_balloon_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y)
287  {  {
288      return (priv->balloon.valid &&      osd_priv_t *priv = (osd_priv_t*)osd->priv;
             (x > priv->balloon.rect.x) &&  
             (x < priv->balloon.rect.x + priv->balloon.rect.w) &&  
             (y > priv->balloon.rect.y) &&  
             (y < priv->balloon.rect.y + priv->balloon.rect.h));  
 }  
289    
290  #if 0      if(!priv->balloon.surface)
     /* don't drag if the user clicked within the balloon */  
     if (osm_gps_map_in_balloon(priv,  
                    event->x + EXTRA_BORDER,  
                    event->y + EXTRA_BORDER))  
     {  
         priv->drag_counter = -1;  
291          return FALSE;          return FALSE;
     }  
292    
293  #ifdef ENABLE_BALLOON      gint xs, ys;
294      /* released inside the balloon? */      osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
295      if (osm_gps_map_in_balloon(priv,                                        priv->balloon.lat, priv->balloon.lon,
296                     event->x + EXTRA_BORDER,                                        &xs, &ys);
                    event->y + EXTRA_BORDER))  
     {  
         osm_gps_map_handle_balloon_click(OSM_GPS_MAP(widget),  
              event->x - priv->balloon.rect.x + EXTRA_BORDER,  
              event->y - priv->balloon.rect.y + EXTRA_BORDER);  
     }  
 #endif  
297    
298  #endif      xs += priv->balloon.rect.x + priv->balloon.offset_x;
299        ys += priv->balloon.rect.y + priv->balloon.offset_y;
300    
301  void      gboolean is_in =
302  osm_gps_map_draw_balloon (OsmGpsMap *map, float latitude, float longitude,          (x > xs) && (x < xs + priv->balloon.rect.w) &&
303                            OsmGpsMapBalloonCallback cb, gpointer data)          (y > ys) && (y < ys + priv->balloon.rect.h);
304  {  
305      OsmGpsMapPrivate *priv;      /* handle the fact that the balloon may have been created by the */
306        /* button down event */
307        if(!is_in && !down && !priv->balloon.just_created) {
308            /* the user actually clicked outside the balloon */
309    
310      /* remove and previously installed balloon */          /* close the balloon! */
311      osm_gps_map_clear_balloon (map);          osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
312        }
313    
314        return is_in;
315    }
316    
317    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
318      g_return_if_fail (OSM_IS_GPS_MAP (map));      g_return_if_fail (OSM_IS_GPS_MAP (map));
     priv = map->priv;  
319    
320      priv->balloon.lat = latitude;      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
321      priv->balloon.lon = longitude;      g_return_if_fail (osd);
     priv->balloon.valid = TRUE;  
322    
323      priv->balloon.cb = cb;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
324      priv->balloon.data = data;      g_return_if_fail (priv);
325    
326      // this redraws the map      if(priv->balloon.surface) {
327      osm_gps_map_map_redraw_idle(map);          cairo_surface_destroy(priv->balloon.surface);
328            priv->balloon.surface = NULL;
329            priv->balloon.lat = OSM_GPS_MAP_INVALID;
330            priv->balloon.lon = OSM_GPS_MAP_INVALID;
331        }
332        osm_gps_map_redraw(map);
333  }  }
334    
335  void  void
336  osm_gps_map_clear_balloon (OsmGpsMap *map)  osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
337  {                                OsmGpsMapBalloonCallback cb, gpointer data) {
     OsmGpsMapPrivate *priv;  
   
338      g_return_if_fail (OSM_IS_GPS_MAP (map));      g_return_if_fail (OSM_IS_GPS_MAP (map));
     priv = map->priv;  
339    
340      priv->balloon.valid = FALSE;      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
341        g_return_if_fail (osd);
     osm_gps_map_map_redraw_idle(map);  
 }  
 #endif  
342    
343  void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {      osd_priv_t *priv = (osd_priv_t*)osd->priv;
344      printf("request to clear balloon\n");      g_return_if_fail (priv);
 }  
345    
346  void      osm_gps_map_osd_clear_balloon (map);
347  osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,  
348                                OsmGpsMapBalloonCallback cb, gpointer data)      /* allocate balloon surface */
349  {      priv->balloon.surface =
350      printf("request to draw balloon at %f %f\n", latitude, longitude);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
351                                       BALLOON_W+2, BALLOON_H+2);
352    
353        priv->balloon.lat = latitude;
354        priv->balloon.lon = longitude;
355        priv->balloon.cb = cb;
356        priv->balloon.data = data;
357        priv->balloon.just_created = TRUE;
358    
359        priv->balloon.orientation = -1;
360    
361        osd_render_balloon(osd);
362    
363        osm_gps_map_redraw(map);
364  }  }
365    
366  #endif // OSD_BALLOON  #endif // OSD_BALLOON
# Line 886  osd_source_toggle(osm_gps_map_osd_t *osd Line 875  osd_source_toggle(osm_gps_map_osd_t *osd
875    
876  /* check if the user clicked inside the source selection area */  /* check if the user clicked inside the source selection area */
877  static osd_button_t  static osd_button_t
878  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) {
879      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
880    
881      if(!priv->source_sel.expanded)      if(!priv->source_sel.expanded)
# Line 904  osd_source_check(osm_gps_map_osd_t *osd, Line 893  osd_source_check(osm_gps_map_osd_t *osd,
893          /* really within puller shape? */          /* really within puller shape? */
894          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)) {
895              /* expand source selector */              /* expand source selector */
896              osd_source_toggle(osd);              if(down)
897                    osd_source_toggle(osd);
898    
899              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
900              /* of the OSD */              /* of the OSD */
# Line 930  osd_source_check(osm_gps_map_osd_t *osd, Line 920  osd_source_check(osm_gps_map_osd_t *osd,
920              y /= step;              y /= step;
921              y += 1;              y += 1;
922    
923              gint old = 0;              if(down) {
924              g_object_get(osd->widget, "map-source", &old, NULL);                  gint old = 0;
925                    g_object_get(osd->widget, "map-source", &old, NULL);
926              if(y > OSM_GPS_MAP_SOURCE_NULL &&  
927                 y <= OSM_GPS_MAP_SOURCE_LAST &&                  if(y > OSM_GPS_MAP_SOURCE_NULL &&
928                 old != y) {                     y <= OSM_GPS_MAP_SOURCE_LAST &&
929                  g_object_set(osd->widget, "map-source", y, NULL);                     old != y) {
930                        g_object_set(osd->widget, "map-source", y, NULL);
931                  osd_render_source_sel(osd, TRUE);  
932                  osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));                      osd_render_source_sel(osd, TRUE);
933                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
934                    }
935              }              }
936    
937              /* return "clicked in OSD background" to prevent further */              /* return "clicked in OSD background" to prevent further */
# Line 953  osd_source_check(osm_gps_map_osd_t *osd, Line 945  osd_source_check(osm_gps_map_osd_t *osd,
945  #endif // OSD_SOURCE_SEL  #endif // OSD_SOURCE_SEL
946    
947  static osd_button_t  static osd_button_t
948  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) {
949      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
950    
951    #ifdef OSD_BALLOON
952        if(down) {
953            /* needed to handle balloons that are created at click */
954            osd_priv_t *priv = (osd_priv_t*)osd->priv;
955            priv->balloon.just_created = FALSE;
956        }
957    #endif
958    
959  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
960      /* the source selection area is handles internally */      /* the source selection area is handles internally */
961      but = osd_source_check(osd, x, y);      but = osd_source_check(osd, down, x, y);
     if(but != OSD_NONE)  
         return but;  
962  #endif  #endif
963    
964      x -= OSD_X;      if(but == OSD_NONE) {
965      y -= OSD_Y;          gint mx = x - OSD_X;
966            gint my = y - OSD_Y;
967      if(OSD_X < 0)  
968          x -= (osd->widget->allocation.width - OSD_W);          if(OSD_X < 0)
969                mx -= (osd->widget->allocation.width - OSD_W);
970      if(OSD_Y < 0)  
971          y -= (osd->widget->allocation.height - OSD_H);          if(OSD_Y < 0)
972                my -= (osd->widget->allocation.height - OSD_H);
973      /* first do a rough test for the OSD area. */  
974      /* this is just to avoid an unnecessary detailed test */          /* first do a rough test for the OSD area. */
975      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {          /* this is just to avoid an unnecessary detailed test */
976            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
977  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
978          but = osd_check_dpad(x, y);              but = osd_check_dpad(mx, my);
979  #endif  #endif
980            }
981    
982          if(but == OSD_NONE)          if(but == OSD_NONE)
983              but = osd_check_zoom(x, y);              but = osd_check_zoom(mx, my);
984      }      }
985    
986    #ifdef OSD_BALLOON
987        if(but == OSD_NONE) {
988            /* check if user clicked into balloon */
989            if(osd_balloon_check(osd, down, x, y))
990                but = OSD_BG;
991        }
992    #endif
993    
994      return but;      return but;
995  }  }
996    
# Line 1065  osd_zoom_labels(cairo_t *cr, gint x, gin Line 1073  osd_zoom_labels(cairo_t *cr, gint x, gin
1073  #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)  #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
1074    
1075  #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)
1076  #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)
1077    
1078  /* these can be overwritten with versions that support */  /* these can be overwritten with versions that support */
1079  /* localization */  /* localization */
# Line 1123  static char Line 1131  static char
1131                             c, (int)integral, fractional*60.0);                             c, (int)integral, fractional*60.0);
1132  }  }
1133    
1134    /* render a string at the given screen position */
1135    static int
1136    osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1137        char *p = g_strdup(text);
1138        cairo_text_extents_t extents;
1139        cairo_text_extents (cr, p, &extents);
1140    
1141        /* check if text needs to be truncated */
1142        int len = strlen(text)-2;
1143        while(extents.width > width) {
1144            len--;
1145            strcpy(p+len, "...");
1146            cairo_text_extents (cr, p, &extents);
1147        }
1148    
1149        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1150        cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1151        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1152        cairo_text_path (cr, p);
1153        cairo_stroke (cr);
1154    
1155        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1156        cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1157        cairo_show_text (cr, p);
1158    
1159        g_free(p);
1160    
1161        /* skip + 1/4 line */
1162        return y + 5*OSD_COORDINATES_FONT_SIZE/4;
1163    }
1164    
1165  static void  static void
1166  osd_render_coordinates(osm_gps_map_osd_t *osd)  osd_render_coordinates(osm_gps_map_osd_t *osd)
1167  {  {
# Line 1158  osd_render_coordinates(osm_gps_map_osd_t Line 1197  osd_render_coordinates(osm_gps_map_osd_t
1197      char *latitude = osd_latitude_str(lat);      char *latitude = osd_latitude_str(lat);
1198      char *longitude = osd_longitude_str(lon);      char *longitude = osd_longitude_str(lon);
1199    
1200      cairo_text_extents_t lat_extents, lon_extents;      int y = OSD_COORDINATES_OFFSET;
1201      cairo_text_extents (cr, latitude, &lat_extents);      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1202      cairo_text_extents (cr, longitude, &lon_extents);      y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1203    
1204        g_free(latitude);
1205        g_free(longitude);
1206    
1207      cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);      cairo_destroy(cr);
1208      cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);  }
1209      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);  
1210    
1211      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);  #ifdef OSD_NAV
1212      cairo_move_to (cr,  #define OSD_NAV_W  (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1213                     (OSD_COORDINATES_W - lat_extents.width)/2,  #define OSD_NAV_H  (11*OSD_COORDINATES_FONT_SIZE)
1214                     OSD_COORDINATES_OFFSET - lat_extents.y_bearing);  
1215      cairo_show_text (cr, latitude);  /* http://mathforum.org/library/drmath/view/55417.html */
1216      cairo_move_to (cr,  static float get_bearing(float lat1, float lon1, float lat2, float lon2) {
1217                     (OSD_COORDINATES_W - lon_extents.width)/2,    return atan2( sin(lon2 - lon1) * cos(lat2),
1218                     OSD_COORDINATES_OFFSET - lon_extents.y_bearing +                  cos(lat1) * sin(lat2) -
1219                     OSD_COORDINATES_FONT_SIZE);                  sin(lat1) * cos(lat2) * cos(lon2 - lon1));
1220      cairo_show_text (cr, longitude);  }
1221    
1222    /* http://mathforum.org/library/drmath/view/51722.html */
1223    static float get_distance(float lat1, float lon1, float lat2, float lon2) {
1224      float aob = acos(cos(lat1) * cos(lat2) * cos(lon2 - lon1) +
1225                       sin(lat1) * sin(lat2));
1226    
1227      //  return(aob * 3959.0);   /* great circle radius in miles */
1228    
1229      return(aob * 6371000.0);     /* great circle radius in meters */
1230    }
1231    
1232    static void
1233    osd_render_nav(osm_gps_map_osd_t *osd)
1234    {
1235        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1236    
1237        if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1238            return;
1239    
1240        /* first fill with transparency */
1241        cairo_t *cr = cairo_create(priv->nav.surface);
1242        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1243        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1244        cairo_paint(cr);
1245        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1246    
1247        cairo_select_font_face (cr, "Sans",
1248                                CAIRO_FONT_SLANT_NORMAL,
1249                                CAIRO_FONT_WEIGHT_BOLD);
1250        cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1251    
1252        char *latitude = osd_latitude_str(priv->nav.lat);
1253        char *longitude = osd_longitude_str(priv->nav.lon);
1254    
1255        int y = OSD_COORDINATES_OFFSET;
1256        y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1257        y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1258        y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1259    
1260      g_free(latitude);      g_free(latitude);
1261      g_free(longitude);      g_free(longitude);
1262    
1263        /* draw the compass */
1264        int radius = (OSD_NAV_H - y - 5*OSD_COORDINATES_FONT_SIZE/4)/2;
1265        if(radius > OSD_NAV_W/2)
1266            radius = OSD_NAV_W/2;
1267    
1268        int x = OSD_NAV_W/2+1;
1269        y += radius;
1270    
1271        cairo_stroke (cr);
1272    
1273        /* draw background */
1274        cairo_arc(cr, x, y, radius, 0,  2*M_PI);
1275        cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
1276        cairo_fill_preserve (cr);
1277        cairo_set_source_rgb (cr, 0, 0, 0);
1278        cairo_set_line_width (cr, 1);
1279        cairo_stroke (cr);
1280    
1281        /* draw pointer */
1282    #define ARROW_WIDTH     0.3
1283    #define ARROW_LENGTH    0.7
1284    
1285        coord_t *gps = osm_gps_map_get_gps (OSM_GPS_MAP(osd->widget));
1286        if(gps) {
1287            float arot = get_bearing(gps->rlat, gps->rlon,
1288                         deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1289    
1290            cairo_move_to(cr,
1291                          x + radius *  ARROW_LENGTH *  sin(arot),
1292                          y + radius *  ARROW_LENGTH * -cos(arot));
1293    
1294            cairo_line_to(cr,
1295                          x + radius * -ARROW_LENGTH *  sin(arot+ARROW_WIDTH),
1296                          y + radius * -ARROW_LENGTH * -cos(arot+ARROW_WIDTH));
1297    
1298            cairo_line_to(cr,
1299                          x + radius * -0.5 * ARROW_LENGTH *  sin(arot),
1300                          y + radius * -0.5 * ARROW_LENGTH * -cos(arot));
1301    
1302            cairo_line_to(cr,
1303                          x + radius * -ARROW_LENGTH *  sin(arot-ARROW_WIDTH),
1304                          y + radius * -ARROW_LENGTH * -cos(arot-ARROW_WIDTH));
1305    
1306            cairo_close_path(cr);
1307            cairo_set_source_rgb (cr, 0, 0, 0);
1308            cairo_fill (cr);
1309    
1310            y += radius + OSD_COORDINATES_FONT_SIZE/4;
1311    
1312            float dist = get_distance(gps->rlat, gps->rlon,
1313                            deg2rad(priv->nav.lat), deg2rad(priv->nav.lon));
1314    
1315            char *dist_str = NULL;
1316            if(!priv->nav.imperial) {
1317                /* metric is easy ... */
1318                if(dist<1000)
1319                    dist_str = g_strdup_printf("%u m", (int)dist);
1320                else
1321                    dist_str = g_strdup_printf("%.1f km", dist/1000);
1322            } else {
1323                /* and now the hard part: scale for useful imperial values :-( */
1324                /* try to convert to feet, 1ft == 0.3048 m */
1325    
1326                if(dist/(3*0.3048) >= 1760.0)      /* more than 1760 yard? */
1327                    dist_str = g_strdup_printf("%.1f mi", dist/(0.3048*3*1760.0));
1328                else if(dist/0.3048 >= 100)        /* more than 100 feet? */
1329                    dist_str = g_strdup_printf("%.1f yd", dist/(0.3048*3));
1330                else
1331                    dist_str = g_strdup_printf("%.0f ft", dist/0.3048);
1332            }
1333    
1334            y = osd_render_centered_text(cr, y, OSD_NAV_W, dist_str);
1335            g_free(dist_str);
1336        }
1337    
1338      cairo_destroy(cr);      cairo_destroy(cr);
1339  }  }
1340  #endif  // OSD_COORDINATES  
1341    void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1342        g_return_if_fail (OSM_IS_GPS_MAP (map));
1343    
1344        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1345        g_return_if_fail (osd);
1346    
1347        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1348        g_return_if_fail (priv);
1349    
1350        if(priv->nav.surface) {
1351            cairo_surface_destroy(priv->nav.surface);
1352            priv->nav.surface = NULL;
1353            priv->nav.lat = OSM_GPS_MAP_INVALID;
1354            priv->nav.lon = OSM_GPS_MAP_INVALID;
1355            if(priv->nav.name) g_free(priv->nav.name);
1356        }
1357        osm_gps_map_redraw(map);
1358    }
1359    
1360    void
1361    osm_gps_map_osd_draw_nav (OsmGpsMap *map, gboolean imperial,
1362                              float latitude, float longitude, char *name) {
1363        g_return_if_fail (OSM_IS_GPS_MAP (map));
1364    
1365        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1366        g_return_if_fail (osd);
1367    
1368        osd_priv_t *priv = (osd_priv_t*)osd->priv;
1369        g_return_if_fail (priv);
1370    
1371        osm_gps_map_osd_clear_nav (map);
1372    
1373        /* allocate balloon surface */
1374        priv->nav.surface =
1375            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1376                                       OSD_NAV_W+2, OSD_NAV_H+2);
1377    
1378        priv->nav.lat = latitude;
1379        priv->nav.lon = longitude;
1380        priv->nav.name = g_strdup(name);
1381        priv->nav.imperial = imperial;
1382    
1383        osd_render_nav(osd);
1384    
1385        osm_gps_map_redraw(map);
1386    }
1387    
1388    #endif // OSD_NAV
1389    
1390    
1391  #ifdef OSD_CROSSHAIR  #ifdef OSD_CROSSHAIR
1392    
# Line 1520  osd_render(osm_gps_map_osd_t *osd) Line 1715  osd_render(osm_gps_map_osd_t *osd)
1715      osd_render_crosshair(osd);      osd_render_crosshair(osd);
1716  #endif  #endif
1717    
1718    #ifdef OSD_NAV
1719        osd_render_nav(osd);
1720    #endif
1721    
1722  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
1723      osd_render_coordinates(osd);      osd_render_coordinates(osd);
1724  #endif  #endif
# Line 1579  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1778  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1778      // now draw this onto the original context      // now draw this onto the original context
1779      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1780    
1781      int x, y;      gint x, y;
1782    
1783  #ifdef OSD_SCALE  #ifdef OSD_SCALE
1784      x =  OSD_X;      x =  OSD_X;
# Line 1599  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1798  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1798      cairo_paint(cr);      cairo_paint(cr);
1799  #endif  #endif
1800    
1801    #ifdef OSD_NAV
1802        if(priv->nav.surface) {
1803            x =  OSD_X;
1804            if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1805            y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1806    
1807            cairo_set_source_surface(cr, priv->nav.surface, x, y);
1808            cairo_paint(cr);
1809        }
1810    #endif
1811    
1812  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
1813      x = -OSD_X;      x = -OSD_X;
1814      y = -OSD_Y;      y = -OSD_Y;
# Line 1609  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1819  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1819      cairo_paint(cr);      cairo_paint(cr);
1820  #endif  #endif
1821    
1822    #ifdef OSD_BALLOON
1823        if(priv->balloon.surface) {
1824    
1825            /* convert given lat lon into screen coordinates */
1826            gint x, y;
1827            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1828                                          priv->balloon.lat, priv->balloon.lon,
1829                                          &x, &y);
1830    
1831            /* check if balloon needs to be rerendered */
1832            osd_render_balloon(osd);
1833    
1834            cairo_set_source_surface(cr, priv->balloon.surface,
1835                                     x + priv->balloon.offset_x,
1836                                     y + priv->balloon.offset_y);
1837            cairo_paint(cr);
1838        }
1839    #endif
1840    
1841      x = OSD_X;      x = OSD_X;
1842      if(x < 0)      if(x < 0)
1843          x += osd->widget->allocation.width - OSD_W;          x += osd->widget->allocation.width - OSD_W;
# Line 1671  osd_free(osm_gps_map_osd_t *osd) Line 1900  osd_free(osm_gps_map_osd_t *osd)
1900           cairo_surface_destroy(priv->crosshair.surface);           cairo_surface_destroy(priv->crosshair.surface);
1901  #endif  #endif
1902    
1903    #ifdef OSD_NAV
1904        if (priv->nav.surface)
1905             cairo_surface_destroy(priv->nav.surface);
1906    #endif
1907    
1908  #ifdef OSD_COORDINATES  #ifdef OSD_COORDINATES
1909      if (priv->coordinates.surface)      if (priv->coordinates.surface)
1910           cairo_surface_destroy(priv->coordinates.surface);           cairo_surface_destroy(priv->coordinates.surface);
# Line 1750  osm_gps_map_osd_check(OsmGpsMap *map, gi Line 1984  osm_gps_map_osd_check(OsmGpsMap *map, gi
1984      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1985      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
1986    
1987      return osd_check(osd, x, y);      return osd_check(osd, TRUE, x, y);
1988  }  }

Legend:
Removed from v.112  
changed lines
  Added in v.133