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

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

revision 111 by harbaum, Mon Sep 14 12:16:08 2009 UTC revision 121 by harbaum, Sun Sep 20 19:26:29 2009 UTC
# Line 32  Line 32 
32  #include <cairo.h>  #include <cairo.h>
33    
34  #include "osm-gps-map.h"  #include "osm-gps-map.h"
35    #include "converter.h"
36  #include "osm-gps-map-osd-classic.h"  #include "osm-gps-map-osd-classic.h"
37    
38  //the osd controls  //the osd controls
# Line 45  typedef struct { Line 46  typedef struct {
46  #endif  #endif
47      } controls;      } controls;
48    
49    #ifdef OSD_BALLOON
50        //a balloon with additional info
51        struct {
52            cairo_surface_t *surface;
53            int orientation, offset_x, offset_y;
54    
55            gboolean just_created;
56            float lat, lon;
57            OsmGpsMapRect_t rect;
58    
59            /* function called to have the user app draw the contents */
60            OsmGpsMapBalloonCallback cb;
61            gpointer data;
62        } balloon;
63    #endif
64    
65  #ifdef OSD_SCALE  #ifdef OSD_SCALE
66      struct {      struct {
67          cairo_surface_t *surface;          cairo_surface_t *surface;
# Line 80  typedef struct { Line 97  typedef struct {
97    
98  } osd_priv_t;  } osd_priv_t;
99    
100    #ifdef OSD_BALLOON
101    /* most visual effects are hardcoded by now, but may be made */
102    /* available via properties later */
103    #ifndef BALLOON_AREA_WIDTH
104    #define BALLOON_AREA_WIDTH           290
105    #endif
106    #ifndef BALLOON_AREA_HEIGHT
107    #define BALLOON_AREA_HEIGHT           75
108    #endif
109    #ifndef BALLOON_CORNER_RADIUS
110    #define BALLOON_CORNER_RADIUS         10
111    #endif
112    
113    #define BALLOON_BORDER               (BALLOON_CORNER_RADIUS/2)
114    #define BALLOON_WIDTH                (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
115    #define BALLOON_HEIGHT               (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
116    #define BALLOON_TRANSPARENCY         0.8
117    #define POINTER_HEIGHT                20
118    #define POINTER_FOOT_WIDTH            20
119    #define POINTER_OFFSET               (BALLOON_CORNER_RADIUS*3/4)
120    #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
121    #define BALLOON_SHADOW_TRANSPARENCY  0.2
122    
123    #define BALLOON_W  (BALLOON_WIDTH + BALLOON_SHADOW)
124    #define BALLOON_H  (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
125    
126    #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
127    
128    
129    /* draw the bubble shape. this is used twice, once for the shape and once */
130    /* for the shadow */
131    static void
132    osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
133           gboolean bottom, int px, int py, int px0, int px1) {
134    
135        cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
136        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
137                   BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
138        if(!bottom) {
139            /* insert top pointer */
140            cairo_line_to (cr, px1, y0);
141            cairo_line_to (cr, px, py);
142            cairo_line_to (cr, px0, y0);
143        }
144    
145        cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
146        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
147                   BALLOON_CORNER_RADIUS, -M_PI/2, 0);
148        cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
149        cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
150                   BALLOON_CORNER_RADIUS, 0, M_PI/2);
151        if(bottom) {
152            /* insert bottom pointer */
153            cairo_line_to (cr, px0, y1);
154            cairo_line_to (cr, px, py);
155            cairo_line_to (cr, px1, y1);
156        }
157    
158        cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
159        cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
160                   BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
161    
162        cairo_close_path (cr);
163    }
164    
165    static void
166    osd_render_balloon(osm_gps_map_osd_t *osd) {
167        osd_priv_t *priv = (osd_priv_t*)osd->priv;
168    
169        /* get zoom */
170        gint zoom;
171        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
172    
173        /* ------- convert given coordinate into screen position --------- */
174        gint xs, ys;
175        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
176                                          priv->balloon.lat, priv->balloon.lon,
177                                          &xs, &ys);
178    
179        gint x0 = 1, y0 = 1;
180    
181        /* check position of this relative to screen center to determine */
182        /* pointer direction ... */
183        int pointer_x, pointer_x0, pointer_x1;
184        int pointer_y;
185    
186        /* ... and calculate position */
187        int orientation = 0;
188        if(xs > osd->widget->allocation.width/2) {
189            priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
190            pointer_x = x0 - priv->balloon.offset_x;
191            pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
192            pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
193            orientation |= 1;
194        } else {
195            priv->balloon.offset_x = -POINTER_OFFSET;
196            pointer_x = x0 - priv->balloon.offset_x;
197            pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
198            pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
199        }
200    
201        gboolean bottom = FALSE;
202        if(ys > osd->widget->allocation.height/2) {
203            priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
204            pointer_y = y0 - priv->balloon.offset_y;
205            bottom = TRUE;
206            orientation |= 2;
207        } else {
208            priv->balloon.offset_y = 0;
209            pointer_y = y0 - priv->balloon.offset_y;
210            y0 += POINTER_HEIGHT;
211        }
212    
213        /* if required orientation equals current one, then don't render */
214        /* anything */
215        if(orientation == priv->balloon.orientation)
216            return;
217    
218        priv->balloon.orientation = orientation;
219    
220        /* calculate bottom/right of box */
221        int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
222    
223        /* save balloon screen coordinates for later use */
224        priv->balloon.rect.x = x0 + BALLOON_BORDER;
225        priv->balloon.rect.y = y0 + BALLOON_BORDER;
226        priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
227        priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
228    
229        cairo_t *cr = cairo_create(priv->balloon.surface);
230        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
231        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
232        cairo_paint(cr);
233        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
234    
235        /* --------- draw shadow --------------- */
236        osm_gps_map_draw_balloon_shape (cr,
237                     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
238                     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
239                     bottom, pointer_x, pointer_y,
240                     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
241    
242        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
243        cairo_fill_preserve (cr);
244        cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
245        cairo_set_line_width (cr, 0);
246        cairo_stroke (cr);
247    
248        /* --------- draw main shape ----------- */
249        osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
250                     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
251    
252        cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
253        cairo_fill_preserve (cr);
254        cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
255        cairo_set_line_width (cr, 1);
256        cairo_stroke (cr);
257    
258        if (priv->balloon.cb) {
259            /* clip in case application tries to draw in */
260                /* exceed of the balloon */
261            cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
262                             priv->balloon.rect.w, priv->balloon.rect.h);
263            cairo_clip (cr);
264            cairo_new_path (cr);  /* current path is not
265                                     consumed by cairo_clip() */
266    
267            priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
268        }
269    
270        cairo_destroy(cr);
271    }
272    
273    /* return true if balloon is being displayed and if */
274    /* the given coordinate is within this balloon */
275    static gboolean
276    osd_balloon_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y)
277    {
278        osd_priv_t *priv = (osd_priv_t*)osd->priv;
279    
280        if(!priv->balloon.surface)
281            return FALSE;
282    
283        gint xs, ys;
284        osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
285                                          priv->balloon.lat, priv->balloon.lon,
286                                          &xs, &ys);
287    
288        xs += priv->balloon.rect.x + priv->balloon.offset_x;
289        ys += priv->balloon.rect.y + priv->balloon.offset_y;
290    
291        gboolean is_in =
292            (x > xs) && (x < xs + priv->balloon.rect.w) &&
293            (y > ys) && (y < ys + priv->balloon.rect.h);
294    
295        /* handle the fact that the balloon may have been created by the */
296        /* button down event */
297        if(!is_in && !down && !priv->balloon.just_created) {
298            /* the user actually clicked outside the balloon */
299    
300            /* close the balloon! */
301            osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
302        }
303    
304        return is_in;
305    }
306    
307    void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
308        g_return_if_fail (OSM_IS_GPS_MAP (map));
309    
310        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
311        g_return_if_fail (osd);
312    
313        osd_priv_t *priv = (osd_priv_t*)osd->priv;
314        g_return_if_fail (priv);
315    
316        if(priv->balloon.surface) {
317            cairo_surface_destroy(priv->balloon.surface);
318            priv->balloon.surface = NULL;
319            priv->balloon.lat = OSM_GPS_MAP_INVALID;
320            priv->balloon.lon = OSM_GPS_MAP_INVALID;
321        }
322        osm_gps_map_redraw(map);
323    }
324    
325    void
326    osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
327                                  OsmGpsMapBalloonCallback cb, gpointer data) {
328        g_return_if_fail (OSM_IS_GPS_MAP (map));
329    
330        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
331        g_return_if_fail (osd);
332    
333        osd_priv_t *priv = (osd_priv_t*)osd->priv;
334        g_return_if_fail (priv);
335    
336        osm_gps_map_osd_clear_balloon (map);
337    
338        /* allocate balloon surface */
339        priv->balloon.surface =
340            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
341                                       BALLOON_W+2, BALLOON_H+2);
342    
343        priv->balloon.lat = latitude;
344        priv->balloon.lon = longitude;
345        priv->balloon.cb = cb;
346        priv->balloon.data = data;
347        priv->balloon.just_created = TRUE;
348    
349        priv->balloon.orientation = -1;
350    
351        osd_render_balloon(osd);
352    
353        osm_gps_map_redraw(map);
354    }
355    
356    #endif // OSD_BALLOON
357    
358  /* position and extent of bounding box */  /* position and extent of bounding box */
359  #ifndef OSD_X  #ifndef OSD_X
360  #define OSD_X      (10)  #define OSD_X      (10)
# Line 590  osd_source_toggle(osm_gps_map_osd_t *osd Line 865  osd_source_toggle(osm_gps_map_osd_t *osd
865    
866  /* check if the user clicked inside the source selection area */  /* check if the user clicked inside the source selection area */
867  static osd_button_t  static osd_button_t
868  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) {
869      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
870    
871      if(!priv->source_sel.expanded)      if(!priv->source_sel.expanded)
# Line 608  osd_source_check(osm_gps_map_osd_t *osd, Line 883  osd_source_check(osm_gps_map_osd_t *osd,
883          /* really within puller shape? */          /* really within puller shape? */
884          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)) {
885              /* expand source selector */              /* expand source selector */
886              osd_source_toggle(osd);              if(down)
887                    osd_source_toggle(osd);
888    
889              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
890              /* of the OSD */              /* of the OSD */
# Line 634  osd_source_check(osm_gps_map_osd_t *osd, Line 910  osd_source_check(osm_gps_map_osd_t *osd,
910              y /= step;              y /= step;
911              y += 1;              y += 1;
912    
913              gint old = 0;              if(down) {
914              g_object_get(osd->widget, "map-source", &old, NULL);                  gint old = 0;
915                    g_object_get(osd->widget, "map-source", &old, NULL);
916              if(y > OSM_GPS_MAP_SOURCE_NULL &&  
917                 y <= OSM_GPS_MAP_SOURCE_LAST &&                  if(y > OSM_GPS_MAP_SOURCE_NULL &&
918                 old != y) {                     y <= OSM_GPS_MAP_SOURCE_LAST &&
919                  g_object_set(osd->widget, "map-source", y, NULL);                     old != y) {
920                        g_object_set(osd->widget, "map-source", y, NULL);
921                  osd_render_source_sel(osd, TRUE);  
922                  osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));                      osd_render_source_sel(osd, TRUE);
923                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
924                    }
925              }              }
926    
927              /* return "clicked in OSD background" to prevent further */              /* return "clicked in OSD background" to prevent further */
# Line 657  osd_source_check(osm_gps_map_osd_t *osd, Line 935  osd_source_check(osm_gps_map_osd_t *osd,
935  #endif // OSD_SOURCE_SEL  #endif // OSD_SOURCE_SEL
936    
937  static osd_button_t  static osd_button_t
938  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) {
939      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
940    
941    #ifdef OSD_BALLOON
942        if(down) {
943            /* needed to handle balloons that are created at click */
944            osd_priv_t *priv = (osd_priv_t*)osd->priv;
945            priv->balloon.just_created = FALSE;
946        }
947    #endif
948    
949  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
950      /* the source selection area is handles internally */      /* the source selection area is handles internally */
951      but = osd_source_check(osd, x, y);      but = osd_source_check(osd, down, x, y);
     if(but != OSD_NONE)  
         return but;  
952  #endif  #endif
953    
954      x -= OSD_X;      if(but == OSD_NONE) {
955      y -= OSD_Y;          gint mx = x - OSD_X;
956            gint my = y - OSD_Y;
957      if(OSD_X < 0)  
958          x -= (osd->widget->allocation.width - OSD_W);          if(OSD_X < 0)
959                mx -= (osd->widget->allocation.width - OSD_W);
960      if(OSD_Y < 0)  
961          y -= (osd->widget->allocation.height - OSD_H);          if(OSD_Y < 0)
962                my -= (osd->widget->allocation.height - OSD_H);
963      /* first do a rough test for the OSD area. */  
964      /* this is just to avoid an unnecessary detailed test */          /* first do a rough test for the OSD area. */
965      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {          /* this is just to avoid an unnecessary detailed test */
966            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
967  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
968          but = osd_check_dpad(x, y);              but = osd_check_dpad(mx, my);
969  #endif  #endif
970            }
971    
972          if(but == OSD_NONE)          if(but == OSD_NONE)
973              but = osd_check_zoom(x, y);              but = osd_check_zoom(mx, my);
974      }      }
975    
976    #ifdef OSD_BALLOON
977        if(but == OSD_NONE) {
978            /* check if user clicked into balloon */
979            if(osd_balloon_check(osd, down, x, y))
980                but = OSD_BG;
981        }
982    #endif
983    
984      return but;      return but;
985  }  }
986    
# Line 1283  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1577  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1577      // now draw this onto the original context      // now draw this onto the original context
1578      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1579    
1580      int x, y;      gint x, y;
1581    
1582  #ifdef OSD_SCALE  #ifdef OSD_SCALE
1583      x =  OSD_X;      x =  OSD_X;
# Line 1313  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1607  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1607      cairo_paint(cr);      cairo_paint(cr);
1608  #endif  #endif
1609    
1610    #ifdef OSD_BALLOON
1611        if(priv->balloon.surface) {
1612    
1613            /* convert given lat lon into screen coordinates */
1614            gint x, y;
1615            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1616                                          priv->balloon.lat, priv->balloon.lon,
1617                                          &x, &y);
1618    
1619            /* check if balloon needs to be rerendered */
1620            osd_render_balloon(osd);
1621    
1622            cairo_set_source_surface(cr, priv->balloon.surface,
1623                                     x + priv->balloon.offset_x,
1624                                     y + priv->balloon.offset_y);
1625            cairo_paint(cr);
1626        }
1627    #endif
1628    
1629      x = OSD_X;      x = OSD_X;
1630      if(x < 0)      if(x < 0)
1631          x += osd->widget->allocation.width - OSD_W;          x += osd->widget->allocation.width - OSD_W;
# Line 1380  osd_free(osm_gps_map_osd_t *osd) Line 1693  osd_free(osm_gps_map_osd_t *osd)
1693           cairo_surface_destroy(priv->coordinates.surface);           cairo_surface_destroy(priv->coordinates.surface);
1694  #endif  #endif
1695    
1696    #ifdef OSD_BALLOON
1697        if (priv->balloon.surface)
1698             cairo_surface_destroy(priv->balloon.surface);
1699    #endif
1700    
1701      g_free(priv);      g_free(priv);
1702  }  }
1703    
# Line 1415  osm_gps_map_osd_classic_init(OsmGpsMap * Line 1733  osm_gps_map_osd_classic_init(OsmGpsMap *
1733  {  {
1734      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);
1735    
1736    #ifdef OSD_BALLOON
1737        priv->balloon.lat = OSM_GPS_MAP_INVALID;
1738        priv->balloon.lon = OSM_GPS_MAP_INVALID;
1739    #endif
1740    
1741      osd_classic.priv = priv;      osd_classic.priv = priv;
1742    
1743      osm_gps_map_register_osd(map, &osd_classic);      osm_gps_map_register_osd(map, &osd_classic);
# Line 1444  osm_gps_map_osd_check(OsmGpsMap *map, gi Line 1767  osm_gps_map_osd_check(OsmGpsMap *map, gi
1767      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1768      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
1769    
1770      return osd_check(osd, x, y);      return osd_check(osd, TRUE, x, y);
1771  }  }

Legend:
Removed from v.111  
changed lines
  Added in v.121