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

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

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

Legend:
Removed from v.75  
changed lines
  Added in v.94