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

Parent Directory Parent Directory | Revision Log Revision Log


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