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 120 by harbaum, Sat Sep 19 19:19:42 2009 UTC
# Line 50  typedef struct { Line 50  typedef struct {
50      //a balloon with additional info      //a balloon with additional info
51      struct {      struct {
52          cairo_surface_t *surface;          cairo_surface_t *surface;
53            int orientation, offset_x, offset_y;
54    
55            gboolean just_created;
56          float lat, lon;          float lat, lon;
         gboolean valid;  
57          OsmGpsMapRect_t rect;          OsmGpsMapRect_t rect;
58    
59          /* function called to have the user app draw the contents */          /* function called to have the user app draw the contents */
# Line 119  typedef struct { Line 120  typedef struct {
120  #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)  #define BALLOON_SHADOW               (BALLOON_CORNER_RADIUS/2)
121  #define BALLOON_SHADOW_TRANSPARENCY  0.2  #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)  #define CLOSE_BUTTON_RADIUS   (BALLOON_CORNER_RADIUS)
127    
128    
# Line 158  osm_gps_map_draw_balloon_shape (cairo_t Line 162  osm_gps_map_draw_balloon_shape (cairo_t
162      cairo_close_path (cr);      cairo_close_path (cr);
163  }  }
164    
 /* xyz */  
   
165  static void  static void
166  osd_render_balloon(osm_gps_map_osd_t *osd) {  osd_render_balloon(osm_gps_map_osd_t *osd) {
167      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 171  osd_render_balloon(osm_gps_map_osd_t *os
171      g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);      g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
172    
173      /* ------- convert given coordinate into screen position --------- */      /* ------- convert given coordinate into screen position --------- */
174      gint x0, y0;      gint xs, ys;
175      osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),      osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
176                                        priv->balloon.lat, priv->balloon.lon,                                        priv->balloon.lat, priv->balloon.lon,
177                                        &x0, &y0);                                        &xs, &ys);
178    
179        gint x0 = 1, y0 = 1;
180    
181      /* check position of this relative to screen center to determine */      /* check position of this relative to screen center to determine */
182      /* pointer direction ... */      /* pointer direction ... */
183      int pointer_x = x0, pointer_x0, pointer_x1;      int pointer_x, pointer_x0, pointer_x1;
184      int pointer_y = y0;      int pointer_y;
185    
186      /* ... and calculate position */      /* ... and calculate position */
187      if(x0 > osd->widget->allocation.width/2) {      int orientation = 0;
188          x0 += POINTER_OFFSET;      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);          pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
192          pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;          pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
193            orientation |= 1;
194      } else {      } else {
195          x0 -= POINTER_OFFSET;          priv->balloon.offset_x = -POINTER_OFFSET;
196            pointer_x = x0 - priv->balloon.offset_x;
197          pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);          pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
198          pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;          pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
199      }      }
200    
201      gboolean bottom = FALSE;      gboolean bottom = FALSE;
202      if(y0 > osd->widget->allocation.height/2) {      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;          bottom = TRUE;
206          y0 -= BALLOON_HEIGHT + POINTER_HEIGHT;          orientation |= 2;
207      } else      } else {
208            priv->balloon.offset_y = 0;
209            pointer_y = y0 - priv->balloon.offset_y;
210          y0 += POINTER_HEIGHT;          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 */      /* calculate bottom/right of box */
221      int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;      int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
222    
# Line 207  osd_render_balloon(osm_gps_map_osd_t *os Line 227  osd_render_balloon(osm_gps_map_osd_t *os
227      priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;      priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
228    
229      cairo_t *cr = cairo_create(priv->balloon.surface);      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 --------------- */      /* --------- draw shadow --------------- */
236      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 255  osd_render_balloon(osm_gps_map_osd_t *os
255      cairo_set_line_width (cr, 1);      cairo_set_line_width (cr, 1);
256      cairo_stroke (cr);      cairo_stroke (cr);
257    
   
     /* ---------- 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);  
   
258      if (priv->balloon.cb) {      if (priv->balloon.cb) {
259          /* clip in case application tries to draw in */          /* clip in case application tries to draw in */
260              /* exceed of the balloon */              /* exceed of the balloon */
# Line 266  osd_render_balloon(osm_gps_map_osd_t *os Line 266  osd_render_balloon(osm_gps_map_osd_t *os
266    
267          priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);          priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
268      }      }
   
     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;  
269    
270      if (!priv->balloon.valid)      cairo_destroy(cr);
         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)) {  
   
         priv->balloon.valid = FALSE;  
         osm_gps_map_map_redraw_idle(map);  
     }  
271  }  }
272    
273  /* return true if balloon is being displayed and if */  /* return true if balloon is being displayed and if */
274  /* the given coordinate is within this balloon */  /* the given coordinate is within this balloon */
275  static gboolean  static gboolean
276  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)
277  {  {
278      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));  
 }  
279    
280  #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;  
281          return FALSE;          return FALSE;
     }  
282    
283  #ifdef ENABLE_BALLOON      gint xs, ys;
284      /* released inside the balloon? */      osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
285      if (osm_gps_map_in_balloon(priv,                                        priv->balloon.lat, priv->balloon.lon,
286                     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  
   
 #endif  
287    
288  void      printf("click at %d/%d\n", x, y);
 osm_gps_map_draw_balloon (OsmGpsMap *map, float latitude, float longitude,  
                           OsmGpsMapBalloonCallback cb, gpointer data)  
 {  
     OsmGpsMapPrivate *priv;  
289    
290      /* remove and previously installed balloon */      xs += priv->balloon.rect.x + priv->balloon.offset_x;
291      osm_gps_map_clear_balloon (map);      ys += priv->balloon.rect.y + priv->balloon.offset_y;
292    
293      g_return_if_fail (OSM_IS_GPS_MAP (map));      printf("balloon at %d/%d/%d/%d\n",
294      priv = map->priv;             xs, ys,
295               xs + priv->balloon.rect.w,
296               ys + priv->balloon.rect.h);
297    
298      priv->balloon.lat = latitude;      gboolean is_in =
299      priv->balloon.lon = longitude;          (x > xs) && (x < xs + priv->balloon.rect.w) &&
300      priv->balloon.valid = TRUE;          (y > ys) && (y < ys + priv->balloon.rect.h);
301    
302        /* handle the fact that the balloon may have been created by the */
303        /* button down event */
304        if(!is_in && !down && !priv->balloon.just_created) {
305            /* the user actually clicked outside the balloon */
306    
307      priv->balloon.cb = cb;          /* close the balloon! */
308      priv->balloon.data = data;          osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
309        }
310    
311      // this redraws the map      return is_in;
     osm_gps_map_map_redraw_idle(map);  
312  }  }
313    
314  void  void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
 osm_gps_map_clear_balloon (OsmGpsMap *map)  
 {  
     OsmGpsMapPrivate *priv;  
   
315      g_return_if_fail (OSM_IS_GPS_MAP (map));      g_return_if_fail (OSM_IS_GPS_MAP (map));
     priv = map->priv;  
316    
317      priv->balloon.valid = FALSE;      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
318        g_return_if_fail (osd);
     osm_gps_map_map_redraw_idle(map);  
 }  
 #endif  
319    
320  void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {      osd_priv_t *priv = (osd_priv_t*)osd->priv;
321      printf("request to clear balloon\n");      g_return_if_fail (priv);
322    
323        if(priv->balloon.surface) {
324            cairo_surface_destroy(priv->balloon.surface);
325            priv->balloon.surface = NULL;
326            priv->balloon.lat = OSM_GPS_MAP_INVALID;
327            priv->balloon.lon = OSM_GPS_MAP_INVALID;
328        }
329        osm_gps_map_redraw(map);
330  }  }
331    
332  void  void
333  osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,  osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
334                                OsmGpsMapBalloonCallback cb, gpointer data)                                OsmGpsMapBalloonCallback cb, gpointer data) {
335  {      g_return_if_fail (OSM_IS_GPS_MAP (map));
336      printf("request to draw balloon at %f %f\n", latitude, longitude);  
337        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
338        g_return_if_fail (osd);
339    
340        osd_priv_t *priv = (osd_priv_t*)osd->priv;
341        g_return_if_fail (priv);
342    
343        osm_gps_map_osd_clear_balloon (map);
344    
345        /* allocate balloon surface */
346        priv->balloon.surface =
347            cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
348                                       BALLOON_W+2, BALLOON_H+2);
349    
350        priv->balloon.lat = latitude;
351        priv->balloon.lon = longitude;
352        priv->balloon.cb = cb;
353        priv->balloon.data = data;
354        priv->balloon.just_created = TRUE;
355    
356        priv->balloon.orientation = -1;
357    
358        osd_render_balloon(osd);
359    
360        osm_gps_map_redraw(map);
361  }  }
362    
363  #endif // OSD_BALLOON  #endif // OSD_BALLOON
# Line 886  osd_source_toggle(osm_gps_map_osd_t *osd Line 872  osd_source_toggle(osm_gps_map_osd_t *osd
872    
873  /* check if the user clicked inside the source selection area */  /* check if the user clicked inside the source selection area */
874  static osd_button_t  static osd_button_t
875  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) {
876      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
877    
878      if(!priv->source_sel.expanded)      if(!priv->source_sel.expanded)
# Line 904  osd_source_check(osm_gps_map_osd_t *osd, Line 890  osd_source_check(osm_gps_map_osd_t *osd,
890          /* really within puller shape? */          /* really within puller shape? */
891          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)) {
892              /* expand source selector */              /* expand source selector */
893              osd_source_toggle(osd);              if(down)
894                    osd_source_toggle(osd);
895    
896              /* tell upper layers that user clicked some background element */              /* tell upper layers that user clicked some background element */
897              /* of the OSD */              /* of the OSD */
# Line 930  osd_source_check(osm_gps_map_osd_t *osd, Line 917  osd_source_check(osm_gps_map_osd_t *osd,
917              y /= step;              y /= step;
918              y += 1;              y += 1;
919    
920              gint old = 0;              if(down) {
921              g_object_get(osd->widget, "map-source", &old, NULL);                  gint old = 0;
922                    g_object_get(osd->widget, "map-source", &old, NULL);
923              if(y > OSM_GPS_MAP_SOURCE_NULL &&  
924                 y <= OSM_GPS_MAP_SOURCE_LAST &&                  if(y > OSM_GPS_MAP_SOURCE_NULL &&
925                 old != y) {                     y <= OSM_GPS_MAP_SOURCE_LAST &&
926                  g_object_set(osd->widget, "map-source", y, NULL);                     old != y) {
927                        g_object_set(osd->widget, "map-source", y, NULL);
928                  osd_render_source_sel(osd, TRUE);  
929                  osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));                      osd_render_source_sel(osd, TRUE);
930                        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
931                    }
932              }              }
933    
934              /* 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 942  osd_source_check(osm_gps_map_osd_t *osd,
942  #endif // OSD_SOURCE_SEL  #endif // OSD_SOURCE_SEL
943    
944  static osd_button_t  static osd_button_t
945  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) {
946      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
947    
948    #ifdef OSD_BALLOON
949        if(down) {
950            /* needed to handle balloons that are created at click */
951            osd_priv_t *priv = (osd_priv_t*)osd->priv;
952            priv->balloon.just_created = FALSE;
953        }
954    #endif
955    
956  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
957      /* the source selection area is handles internally */      /* the source selection area is handles internally */
958      but = osd_source_check(osd, x, y);      but = osd_source_check(osd, down, x, y);
     if(but != OSD_NONE)  
         return but;  
959  #endif  #endif
960    
961      x -= OSD_X;      if(but == OSD_NONE) {
962      y -= OSD_Y;          gint mx = x - OSD_X;
963            gint my = y - OSD_Y;
964      if(OSD_X < 0)  
965          x -= (osd->widget->allocation.width - OSD_W);          if(OSD_X < 0)
966                mx -= (osd->widget->allocation.width - OSD_W);
967      if(OSD_Y < 0)  
968          y -= (osd->widget->allocation.height - OSD_H);          if(OSD_Y < 0)
969                my -= (osd->widget->allocation.height - OSD_H);
970      /* first do a rough test for the OSD area. */  
971      /* this is just to avoid an unnecessary detailed test */          /* first do a rough test for the OSD area. */
972      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {          /* this is just to avoid an unnecessary detailed test */
973            if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
974  #ifndef OSD_NO_DPAD  #ifndef OSD_NO_DPAD
975          but = osd_check_dpad(x, y);              but = osd_check_dpad(mx, my);
976  #endif  #endif
977            }
978    
979          if(but == OSD_NONE)          if(but == OSD_NONE)
980              but = osd_check_zoom(x, y);              but = osd_check_zoom(mx, my);
981      }      }
982    
983    #ifdef OSD_BALLOON
984        if(but == OSD_NONE) {
985            /* check if user clicked into balloon */
986            if(osd_balloon_check(osd, down, x, y))
987                but = OSD_BG;
988        }
989    #endif
990    
991      return but;      return but;
992  }  }
993    
# Line 1579  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1584  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1584      // now draw this onto the original context      // now draw this onto the original context
1585      cairo_t *cr = gdk_cairo_create(drawable);      cairo_t *cr = gdk_cairo_create(drawable);
1586    
1587      int x, y;      gint x, y;
1588    
1589  #ifdef OSD_SCALE  #ifdef OSD_SCALE
1590      x =  OSD_X;      x =  OSD_X;
# Line 1609  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1614  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1614      cairo_paint(cr);      cairo_paint(cr);
1615  #endif  #endif
1616    
1617    #ifdef OSD_BALLOON
1618        if(priv->balloon.surface) {
1619    
1620            /* convert given lat lon into screen coordinates */
1621            gint x, y;
1622            osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1623                                          priv->balloon.lat, priv->balloon.lon,
1624                                          &x, &y);
1625    
1626            /* check if balloon needs to be rerendered */
1627            osd_render_balloon(osd);
1628    
1629            cairo_set_source_surface(cr, priv->balloon.surface,
1630                                     x + priv->balloon.offset_x,
1631                                     y + priv->balloon.offset_y);
1632            cairo_paint(cr);
1633        }
1634    #endif
1635    
1636      x = OSD_X;      x = OSD_X;
1637      if(x < 0)      if(x < 0)
1638          x += osd->widget->allocation.width - OSD_W;          x += osd->widget->allocation.width - OSD_W;
# Line 1750  osm_gps_map_osd_check(OsmGpsMap *map, gi Line 1774  osm_gps_map_osd_check(OsmGpsMap *map, gi
1774      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);      osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1775      g_return_val_if_fail (osd, OSD_NONE);      g_return_val_if_fail (osd, OSD_NONE);
1776    
1777      return osd_check(osd, x, y);      return osd_check(osd, TRUE, x, y);
1778  }  }

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