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

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

revision 71 by harbaum, Fri Aug 21 13:20:40 2009 UTC revision 87 by harbaum, Mon Aug 31 12:01:28 2009 UTC
# Line 17  Line 17 
17   * with this program.  If not, see <http://www.gnu.org/licenses/>.   * with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */   */
19    
20  #include "config.h"  #include "config.h"
21    #include <stdlib.h>  // abs
22    #include <math.h>    // M_PI/cos()
23    
24  /* parameters that can be overwritten from the config file: */  /* parameters that can be overwritten from the config file: */
25  /* OSM_GPS_MAP_OSD_DIAMETER */  /* OSD_DIAMETER */
26    /* OSD_X, OSD_Y */
27    
28  #ifndef USE_CAIRO  #ifndef USE_CAIRO
29  #error "OSD control display lacks a non-cairo implementation!"  #error "OSD control display lacks a non-cairo implementation!"
# Line 29  Line 32 
32  #include <cairo.h>  #include <cairo.h>
33    
34  #include "osm-gps-map.h"  #include "osm-gps-map.h"
35    #include "osm-gps-map-osd-classic.h"
36    
37  //the osd controls  //the osd controls
38  typedef struct {  typedef struct {
39      GdkPixmap *backup;      /* the offscreen representation of the OSD */
     gint backup_x, backup_y;  
   
40      cairo_surface_t *overlay;      cairo_surface_t *overlay;
41    
42      OsmGpsMapOsdGpsCallback cb;      /* values to handle the "source" menu */
43      gpointer data;      cairo_surface_t *map_source;
44  } osd_priv_t;      gboolean expanded;
45        gint shift, dir, count;
46  /* the osd structure mainly contains various callbacks */      gint handler_id;
47  /* required to draw and update the OSD */      gint width, height;
 typedef struct {  
   
     void(enable_gps)(OsmGpsMap *, OsmGpsMapOsdGpsCallback, gpointer);  
     void(restore)(OsmGpsMap*);  
     void(draw)(OsmGpsMap *, gint, gint);  
     void(render)(OsmGpsMapPrivate *);  
     osd_button_t(*check)(gint, gint);       /* check if x/y lies within OSD */  
     void(free)(gpointer);  
   
     gpointer priv;  
 } osm_gps_map_osd_t;  
   
 static osm_gps_map_osd_t osd_classic = {  
     .restore    = osm_gps_map_osd_restore,  
     .draw       = osm_gps_map_osd_draw_controls,  
     .check      = osm_gps_map_osd_check,  
     .render     = osm_gps_map_osd_render,  
     .enable_gps = osm_gps_map_osd_enable_gps,  
     .free       = osm_gps_map_osd_free,  
     .priv       = NULL;  
 }  
   
 /* this is the only function that's externally visible */  
 osm_gps_map_osd_t *  
 osm_gps_map_osd_classic_init(void)  
 {  
     return osd_classic;  
 }  
   
 static void  
 osm_gps_map_osd_free(gpointer priv_ptr)  
 {  
     osd_priv_t *priv = (osd_priv_t *)priv_ptr;  
   
48    
49      g_free(priv);  } osd_priv_t;
 }  
50    
51  /* position and extent of bounding box */  /* position and extent of bounding box */
52    #ifndef OSD_X
53  #define OSD_X      (10)  #define OSD_X      (10)
54  #define OSD_Y      (10)  #endif
55    
56  #define OSD_COLOR            0.5, 0.5, 1  #ifndef OSD_Y
57  #define OSD_COLOR_DISABLED   0.8, 0.8, 0.8  #define OSD_Y      (10)
58    #endif
59    
60  /* parameters of the direction shape */  /* parameters of the direction shape */
61  #ifndef OSM_GPS_MAP_OSD_DIAMETER  #ifndef OSD_DIAMETER
62  #define D_RAD  (30)         // diameter of dpad  #define D_RAD  (30)         // diameter of dpad
63  #else  #else
64  #define D_RAD  (OSM_GPS_MAP_OSD_DIAMETER)  #define D_RAD  (OSD_DIAMETER)
65  #endif  #endif
66  #define D_TIP  (4*D_RAD/5)  // distance of arrow tip from dpad center  #define D_TIP  (4*D_RAD/5)  // distance of arrow tip from dpad center
67  #define D_LEN  (D_RAD/4)    // length of arrow  #define D_LEN  (D_RAD/4)    // length of arrow
# Line 102  osm_gps_map_osd_free(gpointer priv_ptr) Line 71  osm_gps_map_osd_free(gpointer priv_ptr)
71  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom  #define Z_STEP   (D_RAD/4)  // distance between dpad and zoom
72  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar  #define Z_RAD    (D_RAD/2)  // radius of "caps" of zoom bar
73    
74    #ifdef OSD_SHADOW_ENABLE
75  /* shadow also depends on control size */  /* shadow also depends on control size */
76  #define OSD_SHADOW (D_RAD/6)  #define OSD_SHADOW (D_RAD/6)
77    #else
78    #define OSD_SHADOW (0)
79    #endif
80    
81    /* normally the GPS button is in the center of the dpad. if there's */
82    /* no dpad it will go into the zoom area */
83    #if defined(OSD_GPS_BUTTON) && defined(OSD_NO_DPAD)
84    #define Z_GPS  1
85    #else
86    #define Z_GPS  0
87    #endif
88    
89  /* total width and height of controls incl. shadow */  /* total width and height of controls incl. shadow */
90  #define OSD_W    (2*D_RAD + OSD_SHADOW)  #define OSD_W    (2*D_RAD + OSD_SHADOW + Z_GPS * 2 * Z_RAD)
91    #if !Z_GPS
92  #define OSD_H    (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)  #define OSD_H    (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)
93    #else
94    #define OSD_H    (2*Z_RAD + OSD_SHADOW)
95    #endif
96    
97    #ifdef OSD_SHADOW_ENABLE
98  #define OSD_LBL_SHADOW (OSD_SHADOW/2)  #define OSD_LBL_SHADOW (OSD_SHADOW/2)
99    #endif
100    
101    #define Z_TOP    ((1-Z_GPS) * (2 * D_RAD + Z_STEP))
102    
 #define Z_TOP    (2 * D_RAD + Z_STEP)  
103  #define Z_MID    (Z_TOP + Z_RAD)  #define Z_MID    (Z_TOP + Z_RAD)
104  #define Z_BOT    (Z_MID + Z_RAD)  #define Z_BOT    (Z_MID + Z_RAD)
105  #define Z_LEFT   (Z_RAD)  #define Z_LEFT   (Z_RAD)
106  #define Z_RIGHT  (2 * D_RAD - Z_RAD)  #define Z_RIGHT  (2 * D_RAD - Z_RAD + Z_GPS * 2 * Z_RAD)
107    #define Z_CENTER ((Z_RIGHT + Z_LEFT)/2)
108    
109  /* create the cairo shape used for the zoom buttons */  /* create the cairo shape used for the zoom buttons */
110  static void  static void
111  osm_gps_map_osd_zoom_shape(cairo_t *cr, gint x, gint y)  osd_zoom_shape(cairo_t *cr, gint x, gint y)
112  {  {
113      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);      cairo_move_to (cr, x+Z_LEFT,    y+Z_TOP);
114      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);      cairo_line_to (cr, x+Z_RIGHT,   y+Z_TOP);
# Line 129  osm_gps_map_osd_zoom_shape(cairo_t *cr, Line 117  osm_gps_map_osd_zoom_shape(cairo_t *cr,
117      cairo_arc     (cr, x+Z_LEFT,    y+Z_MID, Z_RAD,  M_PI/2, -M_PI/2);      cairo_arc     (cr, x+Z_LEFT,    y+Z_MID, Z_RAD,  M_PI/2, -M_PI/2);
118  }  }
119    
120    /* ------------------- color/shadow functions ----------------- */
121    
122    #ifndef OSD_COLOR
123    /* if no color has been specified we just use the gdks default colors */
124    static void
125    osd_labels(cairo_t *cr, gint width, gboolean enabled,
126                           GdkColor *fg, GdkColor *disabled) {
127        if(enabled)  gdk_cairo_set_source_color(cr, fg);
128        else         gdk_cairo_set_source_color(cr, disabled);
129        cairo_set_line_width (cr, width);
130    }
131    #else
132    static void
133    osd_labels(cairo_t *cr, gint width, gboolean enabled) {
134        if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);
135        else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
136        cairo_set_line_width (cr, width);
137    }
138    #endif
139    
140    #ifdef OSD_SHADOW_ENABLE
141    static void
142    osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
143        cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
144        cairo_set_line_width (cr, width);
145    }
146    #endif
147    
148    #ifndef OSD_NO_DPAD
149  /* create the cairo shape used for the dpad */  /* create the cairo shape used for the dpad */
150  static void  static void
151  osm_gps_map_osd_dpad_shape(cairo_t *cr, gint x, gint y)  osd_dpad_shape(cairo_t *cr, gint x, gint y)
152  {  {
153      cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);      cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);
154  }  }
155    #endif
156    
157    #ifdef OSD_SHADOW_ENABLE
158    static void
159    osd_shape_shadow(cairo_t *cr) {
160        cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
161        cairo_fill (cr);
162        cairo_stroke (cr);
163    }
164    #endif
165    
166    #ifndef OSD_COLOR
167    /* if no color has been specified we just use the gdks default colors */
168    static void
169    osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
170        gdk_cairo_set_source_color(cr, bg);
171        cairo_fill_preserve (cr);
172        gdk_cairo_set_source_color(cr, fg);
173        cairo_set_line_width (cr, 1);
174        cairo_stroke (cr);
175    }
176    #else
177    static void
178    osd_shape(cairo_t *cr) {
179        cairo_set_source_rgb (cr, OSD_COLOR_BG);
180        cairo_fill_preserve (cr);
181        cairo_set_source_rgb (cr, OSD_COLOR);
182        cairo_set_line_width (cr, 1);
183        cairo_stroke (cr);
184    }
185    #endif
186    
187    
188  static gboolean  static gboolean
189  osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)  osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)
# Line 142  osm_gps_map_in_circle(gint x, gint y, gi Line 191  osm_gps_map_in_circle(gint x, gint y, gi
191      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);      return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
192  }  }
193    
194    #ifndef OSD_NO_DPAD
195  /* check whether x/y is within the dpad */  /* check whether x/y is within the dpad */
196  static osd_button_t  static osd_button_t
197  osm_gps_map_osd_check_dpad(gint x, gint y)  osd_check_dpad(gint x, gint y)
198  {  {
199      /* within entire dpad circle */      /* within entire dpad circle */
200      if( osm_gps_map_in_circle(x, y, OSD_X + D_RAD, OSD_Y + D_RAD, D_RAD))      if( osm_gps_map_in_circle(x, y, D_RAD, D_RAD, D_RAD))
201      {      {
202          /* convert into position relative to dpads centre */          /* convert into position relative to dpads centre */
203          x -= (OSD_X + D_RAD);          x -= D_RAD;
204          y -= (OSD_Y + D_RAD);          y -= D_RAD;
205    
206    #ifdef OSD_GPS_BUTTON
207          /* check for dpad center goes here! */          /* check for dpad center goes here! */
208          if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))          if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))
209              return OSD_GPS;              return OSD_GPS;
210    #endif
211    
212          if( y < 0 && abs(x) < abs(y))          if( y < 0 && abs(x) < abs(y))
213              return OSD_UP;              return OSD_UP;
# Line 173  osm_gps_map_osd_check_dpad(gint x, gint Line 225  osm_gps_map_osd_check_dpad(gint x, gint
225      }      }
226      return OSD_NONE;      return OSD_NONE;
227  }  }
228    #endif
229    
230  /* check whether x/y is within the zoom pads */  /* check whether x/y is within the zoom pads */
231  static osd_button_t  static osd_button_t
232  osm_gps_map_osd_check_zoom(gint x, gint y) {  osd_check_zoom(gint x, gint y) {
233      if( x > OSD_X && x < (OSD_X + OSD_W) && y > Z_TOP && y < (OSD_Y+Z_BOT)) {      if( x > 0 && x < OSD_W && y > Z_TOP && y < Z_BOT) {
234    
235          /* within circle around (-) label */          /* within circle around (-) label */
236          if( osm_gps_map_in_circle(x, y, OSD_X + Z_LEFT, OSD_Y + Z_MID, Z_RAD))          if( osm_gps_map_in_circle(x, y, Z_LEFT, Z_MID, Z_RAD))
             return OSD_OUT;  
   
         /* between center of (-) button and center of entire zoom control area */  
         if(x > OSD_LEFT && x < OSD_X + D_RAD)  
237              return OSD_OUT;              return OSD_OUT;
238    
239          /* within circle around (+) label */          /* within circle around (+) label */
240          if( osm_gps_map_in_circle(x, y, OSD_X + Z_RIGHT, OSD_Y + Z_MID, Z_RAD))          if( osm_gps_map_in_circle(x, y, Z_RIGHT, Z_MID, Z_RAD))
241              return OSD_IN;              return OSD_IN;
242    
243    #if Z_GPS == 1
244            /* within square around center */
245            if( x > Z_CENTER - Z_RAD && x < Z_CENTER + Z_RAD)
246                return OSD_GPS;
247    #endif
248    
249            /* between center of (-) button and center of entire zoom control area */
250            if(x > OSD_LEFT && x < D_RAD)
251                return OSD_OUT;
252    
253          /* between center of (+) button and center of entire zoom control area */          /* between center of (+) button and center of entire zoom control area */
254          if(x < OSD_RIGHT && x > OSD_X + D_RAD)          if(x < OSD_RIGHT && x > D_RAD)
255              return OSD_IN;              return OSD_IN;
256      }      }
257    
258      return OSD_NONE;      return OSD_NONE;
259  }  }
260    
261    /* place source selection at right border */
262    #define OSD_S_RAD (Z_RAD)
263    #define OSD_S_X   (-OSD_X)
264    #define OSD_S_Y   (OSD_Y)
265    #define OSD_S_PW  (2 * Z_RAD)
266    #define OSD_S_W   (OSD_S_PW)
267    #define OSD_S_PH  (2 * Z_RAD)
268    #define OSD_S_H   (OSD_S_PH + OSD_SHADOW)
269    
270    /* size of usable area when expanded */
271    #define OSD_S_AREA_W (priv->width)
272    #define OSD_S_AREA_H (priv->height)
273    #define OSD_S_EXP_W  (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
274    #define OSD_S_EXP_H  (OSD_S_AREA_H + OSD_SHADOW)
275    
276    /* internal value to draw the arrow on the "puller" */
277    #define OSD_S_D0  (OSD_S_RAD/2)
278    #ifndef OSD_FONT_SIZE
279    #define OSD_FONT_SIZE 16.0
280    #endif
281    #define OSD_TEXT_BORDER   (OSD_FONT_SIZE/2)
282    #define OSD_TEXT_SKIP     (OSD_FONT_SIZE/8)
283    
284    static void
285    osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
286        if(!priv->expanded) {
287            /* just draw the puller */
288            cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
289            cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
290            cairo_line_to (cr, x + OSD_S_PW, y);
291        } else {
292            /* draw the puller and the area itself */
293            cairo_move_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y + OSD_S_AREA_H);
294            cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H);
295            if(OSD_S_Y > 0) {
296                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_PH);
297                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
298            } else {
299                cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_AREA_H-OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
300                cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H - OSD_S_PH);
301                cairo_line_to (cr, x + OSD_S_PW, y);
302            }
303            cairo_line_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y);
304            cairo_close_path (cr);
305        }
306    }
307    
308    static void
309    osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
310        osd_priv_t *priv = (osd_priv_t*)osd->priv;
311    
312        int py = offset + OSD_S_RAD - OSD_S_D0;
313    
314        if(!priv->expanded) {
315            /* draw the "puller" open (<) arrow */
316            cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
317            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
318            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
319        } else {
320            if(OSD_S_Y < 0)
321                py += OSD_S_AREA_H - OSD_S_PH;
322    
323            /* draw the "puller" close (>) arrow */
324            cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
325            cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
326            cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
327            cairo_stroke(cr);
328    
329            /* don't draw a shadow for the text content */
330            if(offset == 1) {
331                gint source;
332                g_object_get(osd->widget, "map-source", &source, NULL);
333    
334                cairo_select_font_face (cr, "Sans",
335                                        CAIRO_FONT_SLANT_NORMAL,
336                                        CAIRO_FONT_WEIGHT_BOLD);
337                cairo_set_font_size (cr, OSD_FONT_SIZE);
338    
339                int i, step = (priv->height - 2*OSD_TEXT_BORDER)
340                    / OSM_GPS_MAP_SOURCE_LAST;
341                for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
342                    cairo_text_extents_t extents;
343                    const char *src = osm_gps_map_source_get_friendly_name(i);
344                    cairo_text_extents (cr, src, &extents);
345    
346                    int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
347                    int y = offset + step * (i-1) + OSD_TEXT_BORDER;
348    
349                    /* draw filled rectangle if selected */
350                    if(source == i) {
351                        cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
352                                        y - OSD_TEXT_SKIP,
353                                        priv->width - OSD_TEXT_BORDER,
354                                        step + OSD_TEXT_SKIP);
355                        cairo_fill(cr);
356    
357                        /* temprarily draw with background color */
358    #ifndef OSD_COLOR
359                        GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
360                        gdk_cairo_set_source_color(cr, &bg);
361    #else
362                        cairo_set_source_rgb (cr, OSD_COLOR_BG);
363    #endif
364                    }
365    
366                    cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
367                    cairo_show_text (cr, src);
368    
369                    /* restore color */
370                    if(source == i) {
371    #ifndef OSD_COLOR
372                        GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
373                        gdk_cairo_set_source_color(cr, &fg);
374    #else
375                        cairo_set_source_rgb (cr, OSD_COLOR);
376    #endif
377                    }
378                }
379            }
380        }
381    }
382    
383    static void
384    osd_render_source_sel(osm_gps_map_osd_t *osd) {
385        osd_priv_t *priv = (osd_priv_t*)osd->priv;
386    
387    #ifndef OSD_COLOR
388        GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
389        GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
390        GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
391    #endif
392    
393        /* draw source selector */
394        cairo_t *cr = cairo_create(priv->map_source);
395        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
396        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
397        cairo_paint(cr);
398        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
399    
400    #ifdef OSD_SHADOW_ENABLE
401        osd_source_shape(priv, cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
402        osd_shape_shadow(cr);
403    #endif
404    
405        osd_source_shape(priv, cr, 1, 1);
406    #ifndef OSD_COLOR
407        osd_shape(cr, &bg, &fg);
408    #else
409        osd_shape(cr);
410    #endif
411    
412    #ifdef OSD_SHADOW_ENABLE
413        osd_labels_shadow(cr, Z_RAD/3, TRUE);
414        osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
415        cairo_stroke (cr);
416    #endif
417    #ifndef OSD_COLOR
418        osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
419    #else
420        osd_labels(cr, Z_RAD/3, TRUE);
421    #endif
422        osd_source_content(osd, cr, 1);
423        cairo_stroke (cr);
424    
425        cairo_destroy(cr);
426    }
427    
428    static void
429    osd_source_reallocate(osm_gps_map_osd_t *osd) {
430        osd_priv_t *priv = (osd_priv_t*)osd->priv;
431    
432        /* re-allocate offscreen bitmap */
433        g_assert (priv->map_source);
434    
435        int w = OSD_S_W, h = OSD_S_H;
436        if(priv->expanded) {
437            /* ... and right of it the waypoint id */
438            cairo_text_extents_t extents;
439    
440            /* determine content size */
441            cairo_t *cr = cairo_create(priv->map_source);
442            cairo_select_font_face (cr, "Sans",
443                                    CAIRO_FONT_SLANT_NORMAL,
444                                    CAIRO_FONT_WEIGHT_BOLD);
445            cairo_set_font_size (cr, OSD_FONT_SIZE);
446    
447            /* calculate menu size */
448            int i, max_h = 0, max_w = 0;
449            for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
450                const char *src = osm_gps_map_source_get_friendly_name(i);
451                cairo_text_extents (cr, src, &extents);
452    
453                //            printf("Source %d: %s = %f %f\n", i, src,
454                //                   extents.width, extents.height);
455    
456                if(extents.width > max_w) max_w = extents.width;
457                if(extents.height > max_h) max_h = extents.height;
458            }
459            cairo_destroy(cr);
460    
461            priv->width  = max_w + 2*OSD_TEXT_BORDER;
462            priv->height = OSM_GPS_MAP_SOURCE_LAST *
463                (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
464    
465            w = OSD_S_EXP_W;
466            h = OSD_S_EXP_H;
467        }
468    
469        cairo_surface_destroy(priv->map_source);
470        priv->map_source =
471            cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
472    
473        osd_render_source_sel(osd);
474    
475    }
476    
477    #define OSD_HZ      15
478    #define OSD_TIME    500
479    
480    static gboolean osd_source_animate(gpointer data) {
481        osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
482        osd_priv_t *priv = (osd_priv_t*)osd->priv;
483        int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
484        gboolean done = FALSE;
485        priv->count += priv->dir;
486    
487        /* shifting in */
488        if(priv->dir < 0) {
489            if(priv->count <= 0) {
490                priv->count = 0;
491                done = TRUE;
492            }
493        } else {
494            if(priv->count >= 1000) {
495                priv->expanded = FALSE;
496                osd_source_reallocate(osd);
497    
498                priv->count = 1000;
499                done = TRUE;
500            }
501        }
502    
503    
504        /* count runs linearly from 0 to 1000, map this nicely onto a position */
505    
506        /* simple linear mapping */
507        //    priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
508        //        (diff * priv->count)/1000;
509    
510        /* nicer sinoid mapping */
511        float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;
512        priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
513            m * diff;
514    
515        osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
516    
517        if(done)
518            priv->handler_id = 0;
519    
520        return !done;
521    }
522    
523    /* switch between expand and collapse mode of source selection */
524    static void
525    osd_source_toggle(osm_gps_map_osd_t *osd)
526    {
527        osd_priv_t *priv = (osd_priv_t*)osd->priv;
528    
529        /* ignore clicks while animation is running */
530        if(priv->handler_id)
531            return;
532    
533        /* expand immediately, collapse is handle at the end of the collapse animation */
534        if(!priv->expanded) {
535            priv->expanded = TRUE;
536            osd_source_reallocate(osd);
537    
538            priv->count = 1000;
539            priv->shift = osd->widget->allocation.width - OSD_S_W;
540            priv->dir = -1000/OSD_HZ;
541        } else {
542            priv->count =  0;
543            priv->shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
544            priv->dir = +1000/OSD_HZ;
545        }
546    
547        priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);
548    }
549    
550    static osd_button_t
551    osd_source_check(osm_gps_map_osd_t *osd, gint x, gint y) {
552        osd_priv_t *priv = (osd_priv_t*)osd->priv;
553    
554        if(!priv->expanded)
555            x -= osd->widget->allocation.width - OSD_S_W;
556        else
557            x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
558    
559        if(OSD_S_Y > 0)
560            y -= OSD_S_Y;
561        else
562            y -= osd->widget->allocation.height - OSD_S_PH + OSD_S_Y;
563    
564        /* within square around puller? */
565        if(y > 0 && y < OSD_S_PH && x > 0 && x < OSD_S_PW) {
566            /* really within puller shape? */
567            if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
568                /* expand source selector */
569                osd_source_toggle(osd);
570    
571                /* tell upper layers that user clicked some background element */
572                /* of the OSD */
573                return OSD_BG;
574            }
575        }
576        return OSD_NONE;
577    }
578    
579  static osd_button_t  static osd_button_t
580  osm_gps_map_osd_check(gint x, gint y) {  osd_check(osm_gps_map_osd_t *osd, gint x, gint y) {
581      osd_button_t but = OSD_NONE;      osd_button_t but = OSD_NONE;
582    
583    #ifdef OSD_SOURCE_SEL
584        /* the source selection area is handles internally */
585        but = osd_source_check(osd, x, y);
586        if(but != OSD_NONE)
587            return but;
588    #endif
589    
590        x -= OSD_X;
591        y -= OSD_Y;
592    
593        if(OSD_X < 0)
594            x -= (osd->widget->allocation.width - OSD_W);
595    
596        if(OSD_Y < 0)
597            y -= (osd->widget->allocation.height - OSD_H);
598    
599      /* first do a rough test for the OSD area. */      /* first do a rough test for the OSD area. */
600      /* this is just to avoid an unnecessary detailed test */      /* this is just to avoid an unnecessary detailed test */
601      if(x > OSD_X && x < OSD_X + OSD_W &&      if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {
602         y > OSD_Y && y < OSD_Y + OSD_H) {  #ifndef OSD_NO_DPAD
603          but = osm_gps_map_osd_check_dpad(x, y);          but = osd_check_dpad(x, y);
604    #endif
605    
606          if(but == OSD_NONE)          if(but == OSD_NONE)
607              but = osm_gps_map_osd_check_zoom(x, y);              but = osd_check_zoom(x, y);
608      }      }
609    
610      return but;      return but;
611  }  }
612    
613  static void  #ifndef OSD_NO_DPAD
 osm_gps_map_osd_shape_shadow(cairo_t *cr) {  
     cairo_set_source_rgba (cr, 0, 0, 0, 0.2);  
     cairo_fill (cr);  
     cairo_stroke (cr);  
 }  
   
 static void  
 osm_gps_map_osd_shape(cairo_t *cr) {  
     cairo_set_source_rgb (cr, 1, 1, 1);  
     cairo_fill_preserve (cr);  
     cairo_set_source_rgb (cr, OSD_COLOR);  
     cairo_set_line_width (cr, 1);  
     cairo_stroke (cr);  
 }  
   
614  static void  static void
615  osm_gps_map_osd_dpad_labels(cairo_t *cr, gint x, gint y) {  osd_dpad_labels(cairo_t *cr, gint x, gint y) {
616      /* move reference to dpad center */      /* move reference to dpad center */
617      x += D_RAD;      x += D_RAD;
618      y += D_RAD;      y += D_RAD;
# Line 256  osm_gps_map_osd_dpad_labels(cairo_t *cr, Line 635  osm_gps_map_osd_dpad_labels(cairo_t *cr,
635          cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);          cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);
636      }      }
637  }  }
638    #endif
639    
640  /* draw the sattelite dish icon in the center of the dpad */  #ifdef OSD_GPS_BUTTON
641  #define GPS_V0  (D_RAD/8)  /* draw the satellite dish icon in the center of the dpad */
642    #define GPS_V0  (D_RAD/7)
643  #define GPS_V1  (D_RAD/10)  #define GPS_V1  (D_RAD/10)
644  #define GPS_V2  (D_RAD/5)  #define GPS_V2  (D_RAD/5)
645    
646  /* draw a satellite receiver dish */  /* draw a satellite receiver dish */
647    /* this is either drawn in the center of the dpad (if present) */
648    /* or in the middle of the zoom area */
649  static void  static void
650  osm_gps_map_osd_dpad_gps(cairo_t *cr, gint x, gint y) {  osd_dpad_gps(cairo_t *cr, gint x, gint y) {
651      /* move reference to dpad center */      /* move reference to dpad center */
652      x += D_RAD;      x += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD * 3;
653      y += D_RAD + GPS_V0;      y += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD + GPS_V0;
654    
655      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);      cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
656      cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);      cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);
# Line 282  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi Line 665  osm_gps_map_osd_dpad_gps(cairo_t *cr, gi
665      cairo_move_to (cr, x, y-GPS_V2);      cairo_move_to (cr, x, y-GPS_V2);
666      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);      cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
667  }  }
668    #endif
669    
670  #define Z_LEN  (2*Z_RAD/3)  #define Z_LEN  (2*Z_RAD/3)
671    
672  static void  static void
673  osm_gps_map_osd_zoom_labels(cairo_t *cr, gint x, gint y) {  osd_zoom_labels(cairo_t *cr, gint x, gint y) {
674      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);      cairo_move_to (cr, x + Z_LEFT  - Z_LEN, y + Z_MID);
675      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_LEFT  + Z_LEN, y + Z_MID);
676    
# Line 297  osm_gps_map_osd_zoom_labels(cairo_t *cr, Line 681  osm_gps_map_osd_zoom_labels(cairo_t *cr,
681  }  }
682    
683  static void  static void
684  osm_gps_map_osd_labels(cairo_t *cr, gint width, gboolean enabled) {  osd_render(osm_gps_map_osd_t *osd) {
685      if(enabled)  cairo_set_source_rgb (cr, OSD_COLOR);      osd_priv_t *priv = (osd_priv_t*)osd->priv;
     else         cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);  
     cairo_set_line_width (cr, width);  
     cairo_stroke (cr);  
 }  
   
 static void  
 osm_gps_map_osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {  
     cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);  
     cairo_set_line_width (cr, width);  
     cairo_stroke (cr);  
 }  
686    
687  static void  #ifndef OSD_COLOR
688  osm_gps_map_osd_render(OsmGpsMapPrivate *priv) {      GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
689        GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
690        GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
691    #endif
692    
693      /* first fill with transparency */      /* first fill with transparency */
694      cairo_t *cr = cairo_create(priv->osd.overlay);      cairo_t *cr = cairo_create(priv->overlay);
695      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
696      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
697      cairo_paint(cr);      cairo_paint(cr);
   
698      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
699    
700      /* --------- draw zoom and dpad shape shadow ----------- */      /* --------- draw zoom and dpad shape shadow ----------- */
701      gint x = 0, y = 0;  #ifdef OSD_SHADOW_ENABLE
702        osd_zoom_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
703      osm_gps_map_osd_zoom_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);      osd_shape_shadow(cr);
704      osm_gps_map_osd_shape_shadow(cr);  #ifndef OSD_NO_DPAD
705      osm_gps_map_osd_dpad_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);      osd_dpad_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
706      osm_gps_map_osd_shape_shadow(cr);      osd_shape_shadow(cr);
707    #endif
708    #endif
709    
710      /* --------- draw zoom and dpad shape ----------- */      /* --------- draw zoom and dpad shape ----------- */
711    
712      osm_gps_map_osd_zoom_shape(cr, x, y);      osd_zoom_shape(cr, 1, 1);
713      osm_gps_map_osd_shape(cr);  #ifndef OSD_COLOR
714      osm_gps_map_osd_dpad_shape(cr, x, y);      osd_shape(cr, &bg, &fg);
715      osm_gps_map_osd_shape(cr);  #else
716        osd_shape(cr);
717    #endif
718    #ifndef OSD_NO_DPAD
719        osd_dpad_shape(cr, 1, 1);
720    #ifndef OSD_COLOR
721        osd_shape(cr, &bg, &fg);
722    #else
723        osd_shape(cr);
724    #endif
725    #endif
726    
727      /* --------- draw zoom and dpad labels --------- */      /* --------- draw zoom and dpad labels --------- */
728    
729      osm_gps_map_osd_zoom_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);  #ifdef OSD_SHADOW_ENABLE
730      osm_gps_map_osd_dpad_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);      osd_labels_shadow(cr, Z_RAD/3, TRUE);
731      osm_gps_map_osd_labels_shadow(cr, Z_RAD/3, TRUE);      osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
732      osm_gps_map_osd_dpad_gps(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);  #ifndef OSD_NO_DPAD
733      osm_gps_map_osd_labels_shadow(cr, Z_RAD/6, priv->osd.cb != NULL);      osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
734    #endif
735      osm_gps_map_osd_zoom_labels(cr, x, y);      cairo_stroke(cr);
736      osm_gps_map_osd_dpad_labels(cr, x, y);  #ifdef OSD_GPS_BUTTON
737      osm_gps_map_osd_labels(cr, Z_RAD/3, TRUE);      osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
738      osm_gps_map_osd_dpad_gps(cr, x, y);      osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
739      osm_gps_map_osd_labels(cr, Z_RAD/6, priv->osd.cb != NULL);      cairo_stroke(cr);
740    #endif
741    #endif
742    
743    #ifndef OSD_COLOR
744        osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
745    #else
746        osd_labels(cr, Z_RAD/3, TRUE);
747    #endif
748        osd_zoom_labels(cr, 1, 1);
749    #ifndef OSD_NO_DPAD
750        osd_dpad_labels(cr, 1, 1);
751    #endif
752        cairo_stroke(cr);
753    
754    #ifndef OSD_COLOR
755        osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
756    #else
757        osd_labels(cr, Z_RAD/6, osd->cb != NULL);
758    #endif
759    #ifdef OSD_GPS_BUTTON
760        osd_dpad_gps(cr, 1, 1);
761    #endif
762        cairo_stroke(cr);
763    
764      cairo_destroy(cr);      cairo_destroy(cr);
765    
766        osd_render_source_sel(osd);
767  }  }
768    
769  static void  static void
770  osm_gps_map_osd_draw_controls (OsmGpsMap *map, gint xoffset, gint yoffset)  osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
771  {  {
772      OsmGpsMapPrivate *priv = map->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
   
     /* backup previous contents */  
     if(!priv->osd.backup)  
         priv->osd.backup = gdk_pixmap_new(priv->pixmap, OSD_W+2, OSD_H+2, -1);  
   
     gint x = OSD_X + EXTRA_BORDER + xoffset;  
     gint y = OSD_Y + EXTRA_BORDER + yoffset;  
   
     /* create backup of background */  
     gdk_draw_drawable(priv->osd.backup,  
         GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))],  
                       priv->pixmap, x-1, y-1, 0, 0, OSD_W+2, OSD_H+2);  
   
     priv->osd.backup_x = x-1;  
     priv->osd.backup_y = y-1;  
773    
774      /* OSD itself uses some off-screen rendering, so check if the */      /* OSD itself uses some off-screen rendering, so check if the */
775      /* offscreen buffer is present and create it if not */      /* offscreen buffer is present and create it if not */
776      if(!priv->osd.overlay) {      if(!priv->overlay) {
777          /* create overlay ... */          /* create overlay ... */
778          priv->osd.overlay =          priv->overlay =
779              cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W, OSD_H);              cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
780    
781            /* the initial OSD state is alway not-expanded */
782            priv->map_source =
783                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
784                                               OSD_S_W+2, OSD_S_H+2);
785    
786          /* ... and render it */          /* ... and render it */
787          osm_gps_map_osd_render(priv);          osd_render(osd);
788      }      }
789    
790      // now draw this onto the original context      // now draw this onto the original context
791      cairo_t *cr = gdk_cairo_create(priv->pixmap);      cairo_t *cr = gdk_cairo_create(drawable);
792      cairo_set_source_surface(cr, priv->osd.overlay, x, y);  
793        int x = OSD_X, y = OSD_Y;
794        if(OSD_X < 0)
795            x = osd->widget->allocation.width - OSD_W + OSD_X;
796    
797        if(OSD_Y < 0)
798            y = osd->widget->allocation.height - OSD_H + OSD_Y;
799    
800        cairo_set_source_surface(cr, priv->overlay, x, y);
801        cairo_paint(cr);
802    
803    #ifdef OSD_SOURCE_SEL
804        if(!priv->handler_id) {
805            /* the OSD source selection is not being animated */
806            if(!priv->expanded)
807                x = osd->widget->allocation.width - OSD_S_W;
808            else
809                x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
810        } else
811            x = priv->shift;
812    
813        y = OSD_S_Y;
814        if(OSD_S_Y < 0) {
815            if(!priv->expanded)
816                y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
817            else
818                y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
819        }
820    
821        cairo_set_source_surface(cr, priv->map_source, x, y);
822      cairo_paint(cr);      cairo_paint(cr);
823    #endif
824    
825      cairo_destroy(cr);      cairo_destroy(cr);
826  }  }
827    
828  static void  static void
829  osm_gps_map_osd_restore (OsmGpsMap *map)  osd_free(osm_gps_map_osd_t *osd)
830  {  {
831      OsmGpsMapPrivate *priv = map->priv;      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
832    
833      /* restore backup of previous contents */      if(priv->handler_id)
834      if(priv->osd.backup) {          gtk_timeout_remove(priv->handler_id);
835          /* create backup of background */  
836          gdk_draw_drawable(priv->pixmap,      if (priv->overlay)
837              GTK_WIDGET(map)->style->fg_gc[GTK_WIDGET_STATE(GTK_WIDGET(map))],           cairo_surface_destroy(priv->overlay);
838                        priv->osd.backup, 0, 0,  
839                        priv->osd.backup_x, priv->osd.backup_y, OSD_W+2, OSD_H+2);      if (priv->map_source)
840      }           cairo_surface_destroy(priv->map_source);
841    
842        g_free(priv);
843  }  }
844    
845  static void  /* this is the only function that's externally visible */
846  osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdGpsCallback cb, gpointer data)  static gboolean
847    osd_busy(osm_gps_map_osd_t *osd)
848  {  {
849      OsmGpsMapPrivate *priv;      osd_priv_t *priv = (osd_priv_t *)(osd->priv);
850        return (priv->handler_id != 0);
851    }
852    
853    static osm_gps_map_osd_t osd_classic = {
854        .draw       = osd_draw,
855        .check      = osd_check,
856        .render     = osd_render,
857        .free       = osd_free,
858        .busy       = osd_busy,
859    
860        .cb         = NULL,
861        .data       = NULL,
862    
863      g_return_if_fail (OSM_IS_GPS_MAP (map));      .priv       = NULL
864      priv = map->priv;  };
865    
866      priv->osd.cb = cb;  /* this is the only function that's externally visible */
867      priv->osd.data = data;  void
868    osm_gps_map_osd_classic_init(OsmGpsMap *map)
869    {
870        osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
871    
872        osd_classic.priv = priv;
873    
874        osm_gps_map_register_osd(map, &osd_classic);
875    }
876    
877    #ifdef OSD_GPS_BUTTON
878    /* below are osd specific functions which aren't used by osm-gps-map */
879    /* but instead are to be used by the main application */
880    void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
881                                     gpointer data) {
882        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
883        g_return_if_fail (osd);
884    
885        osd->cb = cb;
886        osd->data = data;
887    
888      /* this may have changed the state of the gps button */      /* this may have changed the state of the gps button */
889      /* we thus re-render the overlay */      /* we thus re-render the overlay */
890      osm_gps_map_osd_render(priv);      osd->render(osd);
891    
892      osm_gps_map_map_redraw_idle(map);      osm_gps_map_redraw(map);
893    }
894    #endif
895    
896    osd_button_t
897    osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
898        osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
899        g_return_val_if_fail (osd, OSD_NONE);
900    
901        return osd_check(osd, x, y);
902  }  }

Legend:
Removed from v.71  
changed lines
  Added in v.87