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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 124 - (hide annotations)
Thu Sep 24 14:09:53 2009 UTC (14 years, 7 months ago) by harbaum
File MIME type: text/plain
File size: 56468 byte(s)
OSD text handling
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 124 #include <string.h>
23 harbaum 87 #include <math.h> // M_PI/cos()
24 harbaum 70
25 harbaum 71 /* parameters that can be overwritten from the config file: */
26 harbaum 77 /* OSD_DIAMETER */
27     /* OSD_X, OSD_Y */
28 harbaum 71
29     #ifndef USE_CAIRO
30     #error "OSD control display lacks a non-cairo implementation!"
31 harbaum 70 #endif
32    
33 harbaum 71 #include <cairo.h>
34    
35     #include "osm-gps-map.h"
36 harbaum 112 #include "converter.h"
37 harbaum 73 #include "osm-gps-map-osd-classic.h"
38 harbaum 71
39 harbaum 70 //the osd controls
40     typedef struct {
41 harbaum 74 /* the offscreen representation of the OSD */
42 harbaum 108 struct {
43     cairo_surface_t *surface;
44     gboolean rendered;
45     #ifdef OSD_GPS_BUTTON
46     gboolean gps_enabled;
47     #endif
48     } controls;
49 harbaum 86
50 harbaum 112 #ifdef OSD_BALLOON
51     //a balloon with additional info
52     struct {
53     cairo_surface_t *surface;
54 harbaum 113 int orientation, offset_x, offset_y;
55 harbaum 112
56 harbaum 114 gboolean just_created;
57 harbaum 112 float lat, lon;
58     OsmGpsMapRect_t rect;
59    
60     /* function called to have the user app draw the contents */
61     OsmGpsMapBalloonCallback cb;
62     gpointer data;
63     } balloon;
64     #endif
65    
66 harbaum 98 #ifdef OSD_SCALE
67 harbaum 110 struct {
68     cairo_surface_t *surface;
69     int zoom;
70     } scale;
71 harbaum 98 #endif
72 harbaum 110
73 harbaum 105 #ifdef OSD_CROSSHAIR
74 harbaum 110 struct {
75     cairo_surface_t *surface;
76     gboolean rendered;
77     } crosshair;
78 harbaum 105 #endif
79    
80 harbaum 122 #ifdef OSD_NAV
81     struct {
82     cairo_surface_t *surface;
83 harbaum 123 float lat, lon;
84     char *name;
85 harbaum 122 } nav;
86     #endif
87    
88 harbaum 106 #ifdef OSD_COORDINATES
89 harbaum 110 struct {
90     cairo_surface_t *surface;
91     float lat, lon;
92     } coordinates;
93 harbaum 106 #endif
94    
95 harbaum 88 #ifdef OSD_SOURCE_SEL
96 harbaum 110 struct {
97     /* values to handle the "source" menu */
98     cairo_surface_t *surface;
99     gboolean expanded;
100     gint shift, dir, count;
101     gint handler_id;
102     gint width, height;
103 harbaum 111 gboolean rendered;
104 harbaum 110 } source_sel;
105 harbaum 88 #endif
106 harbaum 87
107 harbaum 70 } osd_priv_t;
108    
109 harbaum 112 #ifdef OSD_BALLOON
110     /* most visual effects are hardcoded by now, but may be made */
111     /* available via properties later */
112     #ifndef BALLOON_AREA_WIDTH
113     #define BALLOON_AREA_WIDTH 290
114     #endif
115     #ifndef BALLOON_AREA_HEIGHT
116     #define BALLOON_AREA_HEIGHT 75
117     #endif
118     #ifndef BALLOON_CORNER_RADIUS
119     #define BALLOON_CORNER_RADIUS 10
120     #endif
121    
122     #define BALLOON_BORDER (BALLOON_CORNER_RADIUS/2)
123     #define BALLOON_WIDTH (BALLOON_AREA_WIDTH + 2 * BALLOON_BORDER)
124     #define BALLOON_HEIGHT (BALLOON_AREA_HEIGHT + 2 * BALLOON_BORDER)
125     #define BALLOON_TRANSPARENCY 0.8
126     #define POINTER_HEIGHT 20
127     #define POINTER_FOOT_WIDTH 20
128     #define POINTER_OFFSET (BALLOON_CORNER_RADIUS*3/4)
129     #define BALLOON_SHADOW (BALLOON_CORNER_RADIUS/2)
130     #define BALLOON_SHADOW_TRANSPARENCY 0.2
131    
132 harbaum 113 #define BALLOON_W (BALLOON_WIDTH + BALLOON_SHADOW)
133     #define BALLOON_H (BALLOON_HEIGHT + POINTER_HEIGHT + BALLOON_SHADOW)
134    
135 harbaum 112 #define CLOSE_BUTTON_RADIUS (BALLOON_CORNER_RADIUS)
136    
137    
138     /* draw the bubble shape. this is used twice, once for the shape and once */
139     /* for the shadow */
140     static void
141     osm_gps_map_draw_balloon_shape (cairo_t *cr, int x0, int y0, int x1, int y1,
142     gboolean bottom, int px, int py, int px0, int px1) {
143    
144     cairo_move_to (cr, x0, y0 + BALLOON_CORNER_RADIUS);
145     cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
146     BALLOON_CORNER_RADIUS, -M_PI, -M_PI/2);
147     if(!bottom) {
148     /* insert top pointer */
149     cairo_line_to (cr, px1, y0);
150     cairo_line_to (cr, px, py);
151     cairo_line_to (cr, px0, y0);
152     }
153    
154     cairo_line_to (cr, x1 - BALLOON_CORNER_RADIUS, y0);
155     cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y0 + BALLOON_CORNER_RADIUS,
156     BALLOON_CORNER_RADIUS, -M_PI/2, 0);
157     cairo_line_to (cr, x1 , y1 - BALLOON_CORNER_RADIUS);
158     cairo_arc (cr, x1 - BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
159     BALLOON_CORNER_RADIUS, 0, M_PI/2);
160     if(bottom) {
161     /* insert bottom pointer */
162     cairo_line_to (cr, px0, y1);
163     cairo_line_to (cr, px, py);
164     cairo_line_to (cr, px1, y1);
165     }
166    
167     cairo_line_to (cr, x0 + BALLOON_CORNER_RADIUS, y1);
168     cairo_arc (cr, x0 + BALLOON_CORNER_RADIUS, y1 - BALLOON_CORNER_RADIUS,
169     BALLOON_CORNER_RADIUS, M_PI/2, M_PI);
170    
171     cairo_close_path (cr);
172     }
173    
174     static void
175     osd_render_balloon(osm_gps_map_osd_t *osd) {
176     osd_priv_t *priv = (osd_priv_t*)osd->priv;
177    
178     /* get zoom */
179     gint zoom;
180     g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
181    
182     /* ------- convert given coordinate into screen position --------- */
183 harbaum 113 gint xs, ys;
184 harbaum 112 osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
185     priv->balloon.lat, priv->balloon.lon,
186 harbaum 113 &xs, &ys);
187 harbaum 112
188 harbaum 113 gint x0 = 1, y0 = 1;
189    
190 harbaum 112 /* check position of this relative to screen center to determine */
191     /* pointer direction ... */
192 harbaum 113 int pointer_x, pointer_x0, pointer_x1;
193     int pointer_y;
194 harbaum 112
195     /* ... and calculate position */
196 harbaum 113 int orientation = 0;
197     if(xs > osd->widget->allocation.width/2) {
198     priv->balloon.offset_x = -BALLOON_WIDTH + POINTER_OFFSET;
199     pointer_x = x0 - priv->balloon.offset_x;
200 harbaum 112 pointer_x0 = pointer_x - (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
201     pointer_x1 = pointer_x0 - POINTER_FOOT_WIDTH;
202 harbaum 113 orientation |= 1;
203 harbaum 112 } else {
204 harbaum 113 priv->balloon.offset_x = -POINTER_OFFSET;
205     pointer_x = x0 - priv->balloon.offset_x;
206 harbaum 112 pointer_x1 = pointer_x + (BALLOON_CORNER_RADIUS - POINTER_OFFSET);
207     pointer_x0 = pointer_x1 + POINTER_FOOT_WIDTH;
208     }
209    
210     gboolean bottom = FALSE;
211 harbaum 113 if(ys > osd->widget->allocation.height/2) {
212     priv->balloon.offset_y = -BALLOON_HEIGHT - POINTER_HEIGHT;
213     pointer_y = y0 - priv->balloon.offset_y;
214 harbaum 112 bottom = TRUE;
215 harbaum 113 orientation |= 2;
216     } else {
217     priv->balloon.offset_y = 0;
218     pointer_y = y0 - priv->balloon.offset_y;
219 harbaum 112 y0 += POINTER_HEIGHT;
220 harbaum 113 }
221 harbaum 112
222 harbaum 113 /* if required orientation equals current one, then don't render */
223     /* anything */
224     if(orientation == priv->balloon.orientation)
225     return;
226    
227     priv->balloon.orientation = orientation;
228    
229 harbaum 112 /* calculate bottom/right of box */
230     int x1 = x0 + BALLOON_WIDTH, y1 = y0 + BALLOON_HEIGHT;
231    
232     /* save balloon screen coordinates for later use */
233     priv->balloon.rect.x = x0 + BALLOON_BORDER;
234     priv->balloon.rect.y = y0 + BALLOON_BORDER;
235     priv->balloon.rect.w = x1 - x0 - 2*BALLOON_BORDER;
236     priv->balloon.rect.h = y1 - y0 - 2*BALLOON_BORDER;
237    
238     cairo_t *cr = cairo_create(priv->balloon.surface);
239 harbaum 113 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
240     cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
241     cairo_paint(cr);
242     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
243 harbaum 112
244     /* --------- draw shadow --------------- */
245     osm_gps_map_draw_balloon_shape (cr,
246     x0 + BALLOON_SHADOW, y0 + BALLOON_SHADOW,
247     x1 + BALLOON_SHADOW, y1 + BALLOON_SHADOW,
248     bottom, pointer_x, pointer_y,
249     pointer_x0 + BALLOON_SHADOW, pointer_x1 + BALLOON_SHADOW);
250    
251     cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_SHADOW_TRANSPARENCY);
252     cairo_fill_preserve (cr);
253     cairo_set_source_rgba (cr, 1, 0, 0, 1.0);
254     cairo_set_line_width (cr, 0);
255     cairo_stroke (cr);
256    
257     /* --------- draw main shape ----------- */
258     osm_gps_map_draw_balloon_shape (cr, x0, y0, x1, y1,
259     bottom, pointer_x, pointer_y, pointer_x0, pointer_x1);
260    
261     cairo_set_source_rgba (cr, 1, 1, 1, BALLOON_TRANSPARENCY);
262     cairo_fill_preserve (cr);
263     cairo_set_source_rgba (cr, 0, 0, 0, BALLOON_TRANSPARENCY);
264     cairo_set_line_width (cr, 1);
265     cairo_stroke (cr);
266    
267     if (priv->balloon.cb) {
268     /* clip in case application tries to draw in */
269     /* exceed of the balloon */
270     cairo_rectangle (cr, priv->balloon.rect.x, priv->balloon.rect.y,
271     priv->balloon.rect.w, priv->balloon.rect.h);
272     cairo_clip (cr);
273     cairo_new_path (cr); /* current path is not
274     consumed by cairo_clip() */
275    
276     priv->balloon.cb(cr, &priv->balloon.rect, priv->balloon.data);
277     }
278 harbaum 113
279 harbaum 112 cairo_destroy(cr);
280     }
281    
282     /* return true if balloon is being displayed and if */
283     /* the given coordinate is within this balloon */
284     static gboolean
285 harbaum 114 osd_balloon_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y)
286 harbaum 112 {
287 harbaum 113 osd_priv_t *priv = (osd_priv_t*)osd->priv;
288 harbaum 112
289 harbaum 113 if(!priv->balloon.surface)
290 harbaum 112 return FALSE;
291    
292 harbaum 113 gint xs, ys;
293     osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
294     priv->balloon.lat, priv->balloon.lon,
295     &xs, &ys);
296 harbaum 112
297 harbaum 113 xs += priv->balloon.rect.x + priv->balloon.offset_x;
298     ys += priv->balloon.rect.y + priv->balloon.offset_y;
299 harbaum 112
300 harbaum 114 gboolean is_in =
301     (x > xs) && (x < xs + priv->balloon.rect.w) &&
302     (y > ys) && (y < ys + priv->balloon.rect.h);
303    
304 harbaum 120 /* handle the fact that the balloon may have been created by the */
305     /* button down event */
306 harbaum 114 if(!is_in && !down && !priv->balloon.just_created) {
307     /* the user actually clicked outside the balloon */
308    
309     /* close the balloon! */
310     osm_gps_map_osd_clear_balloon (OSM_GPS_MAP(osd->widget));
311     }
312    
313     return is_in;
314 harbaum 113 }
315 harbaum 112
316 harbaum 113 void osm_gps_map_osd_clear_balloon (OsmGpsMap *map) {
317 harbaum 112 g_return_if_fail (OSM_IS_GPS_MAP (map));
318    
319 harbaum 113 osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
320     g_return_if_fail (osd);
321 harbaum 112
322 harbaum 113 osd_priv_t *priv = (osd_priv_t*)osd->priv;
323     g_return_if_fail (priv);
324 harbaum 112
325 harbaum 113 if(priv->balloon.surface) {
326     cairo_surface_destroy(priv->balloon.surface);
327     priv->balloon.surface = NULL;
328     priv->balloon.lat = OSM_GPS_MAP_INVALID;
329     priv->balloon.lon = OSM_GPS_MAP_INVALID;
330     }
331     osm_gps_map_redraw(map);
332 harbaum 112 }
333    
334     void
335 harbaum 113 osm_gps_map_osd_draw_balloon (OsmGpsMap *map, float latitude, float longitude,
336     OsmGpsMapBalloonCallback cb, gpointer data) {
337 harbaum 112 g_return_if_fail (OSM_IS_GPS_MAP (map));
338    
339 harbaum 113 osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
340     g_return_if_fail (osd);
341 harbaum 112
342 harbaum 113 osd_priv_t *priv = (osd_priv_t*)osd->priv;
343     g_return_if_fail (priv);
344 harbaum 112
345 harbaum 113 osm_gps_map_osd_clear_balloon (map);
346    
347     /* allocate balloon surface */
348     priv->balloon.surface =
349     cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
350     BALLOON_W+2, BALLOON_H+2);
351    
352     priv->balloon.lat = latitude;
353     priv->balloon.lon = longitude;
354     priv->balloon.cb = cb;
355     priv->balloon.data = data;
356 harbaum 114 priv->balloon.just_created = TRUE;
357 harbaum 113
358     priv->balloon.orientation = -1;
359    
360     osd_render_balloon(osd);
361    
362     osm_gps_map_redraw(map);
363 harbaum 112 }
364    
365     #endif // OSD_BALLOON
366    
367 harbaum 70 /* position and extent of bounding box */
368 harbaum 77 #ifndef OSD_X
369 harbaum 70 #define OSD_X (10)
370 harbaum 77 #endif
371    
372     #ifndef OSD_Y
373 harbaum 70 #define OSD_Y (10)
374 harbaum 77 #endif
375 harbaum 70
376     /* parameters of the direction shape */
377 harbaum 77 #ifndef OSD_DIAMETER
378 harbaum 70 #define D_RAD (30) // diameter of dpad
379     #else
380 harbaum 77 #define D_RAD (OSD_DIAMETER)
381 harbaum 70 #endif
382     #define D_TIP (4*D_RAD/5) // distance of arrow tip from dpad center
383     #define D_LEN (D_RAD/4) // length of arrow
384     #define D_WID (D_LEN) // width of arrow
385    
386     /* parameters of the "zoom" pad */
387     #define Z_STEP (D_RAD/4) // distance between dpad and zoom
388     #define Z_RAD (D_RAD/2) // radius of "caps" of zoom bar
389    
390 harbaum 74 #ifdef OSD_SHADOW_ENABLE
391 harbaum 70 /* shadow also depends on control size */
392     #define OSD_SHADOW (D_RAD/6)
393 harbaum 74 #else
394     #define OSD_SHADOW (0)
395     #endif
396 harbaum 70
397 harbaum 77 /* normally the GPS button is in the center of the dpad. if there's */
398     /* no dpad it will go into the zoom area */
399     #if defined(OSD_GPS_BUTTON) && defined(OSD_NO_DPAD)
400     #define Z_GPS 1
401     #else
402     #define Z_GPS 0
403     #endif
404    
405 harbaum 70 /* total width and height of controls incl. shadow */
406 harbaum 77 #define OSD_W (2*D_RAD + OSD_SHADOW + Z_GPS * 2 * Z_RAD)
407     #if !Z_GPS
408 harbaum 70 #define OSD_H (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)
409 harbaum 77 #else
410     #define OSD_H (2*Z_RAD + OSD_SHADOW)
411     #endif
412 harbaum 70
413 harbaum 74 #ifdef OSD_SHADOW_ENABLE
414 harbaum 70 #define OSD_LBL_SHADOW (OSD_SHADOW/2)
415 harbaum 74 #endif
416 harbaum 70
417 harbaum 77 #define Z_TOP ((1-Z_GPS) * (2 * D_RAD + Z_STEP))
418    
419 harbaum 70 #define Z_MID (Z_TOP + Z_RAD)
420     #define Z_BOT (Z_MID + Z_RAD)
421     #define Z_LEFT (Z_RAD)
422 harbaum 77 #define Z_RIGHT (2 * D_RAD - Z_RAD + Z_GPS * 2 * Z_RAD)
423     #define Z_CENTER ((Z_RIGHT + Z_LEFT)/2)
424 harbaum 70
425     /* create the cairo shape used for the zoom buttons */
426     static void
427 harbaum 86 osd_zoom_shape(cairo_t *cr, gint x, gint y)
428 harbaum 71 {
429 harbaum 70 cairo_move_to (cr, x+Z_LEFT, y+Z_TOP);
430     cairo_line_to (cr, x+Z_RIGHT, y+Z_TOP);
431     cairo_arc (cr, x+Z_RIGHT, y+Z_MID, Z_RAD, -M_PI/2, M_PI/2);
432     cairo_line_to (cr, x+Z_LEFT, y+Z_BOT);
433     cairo_arc (cr, x+Z_LEFT, y+Z_MID, Z_RAD, M_PI/2, -M_PI/2);
434     }
435    
436 harbaum 86 /* ------------------- color/shadow functions ----------------- */
437    
438     #ifndef OSD_COLOR
439     /* if no color has been specified we just use the gdks default colors */
440     static void
441     osd_labels(cairo_t *cr, gint width, gboolean enabled,
442     GdkColor *fg, GdkColor *disabled) {
443     if(enabled) gdk_cairo_set_source_color(cr, fg);
444     else gdk_cairo_set_source_color(cr, disabled);
445     cairo_set_line_width (cr, width);
446     }
447     #else
448     static void
449     osd_labels(cairo_t *cr, gint width, gboolean enabled) {
450     if(enabled) cairo_set_source_rgb (cr, OSD_COLOR);
451     else cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
452     cairo_set_line_width (cr, width);
453     }
454     #endif
455    
456     #ifdef OSD_SHADOW_ENABLE
457     static void
458     osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
459     cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
460     cairo_set_line_width (cr, width);
461     }
462     #endif
463    
464 harbaum 76 #ifndef OSD_NO_DPAD
465 harbaum 70 /* create the cairo shape used for the dpad */
466     static void
467 harbaum 86 osd_dpad_shape(cairo_t *cr, gint x, gint y)
468 harbaum 71 {
469 harbaum 70 cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);
470     }
471 harbaum 76 #endif
472 harbaum 70
473 harbaum 86 #ifdef OSD_SHADOW_ENABLE
474     static void
475     osd_shape_shadow(cairo_t *cr) {
476     cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
477     cairo_fill (cr);
478     cairo_stroke (cr);
479     }
480     #endif
481    
482     #ifndef OSD_COLOR
483     /* if no color has been specified we just use the gdks default colors */
484     static void
485     osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
486     gdk_cairo_set_source_color(cr, bg);
487     cairo_fill_preserve (cr);
488     gdk_cairo_set_source_color(cr, fg);
489     cairo_set_line_width (cr, 1);
490     cairo_stroke (cr);
491     }
492     #else
493     static void
494     osd_shape(cairo_t *cr) {
495     cairo_set_source_rgb (cr, OSD_COLOR_BG);
496     cairo_fill_preserve (cr);
497     cairo_set_source_rgb (cr, OSD_COLOR);
498     cairo_set_line_width (cr, 1);
499     cairo_stroke (cr);
500     }
501     #endif
502    
503    
504 harbaum 70 static gboolean
505     osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)
506     {
507     return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
508     }
509    
510 harbaum 76 #ifndef OSD_NO_DPAD
511 harbaum 70 /* check whether x/y is within the dpad */
512     static osd_button_t
513 harbaum 86 osd_check_dpad(gint x, gint y)
514 harbaum 70 {
515     /* within entire dpad circle */
516 harbaum 77 if( osm_gps_map_in_circle(x, y, D_RAD, D_RAD, D_RAD))
517 harbaum 70 {
518     /* convert into position relative to dpads centre */
519 harbaum 77 x -= D_RAD;
520     y -= D_RAD;
521 harbaum 70
522 harbaum 76 #ifdef OSD_GPS_BUTTON
523 harbaum 70 /* check for dpad center goes here! */
524     if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))
525     return OSD_GPS;
526 harbaum 76 #endif
527 harbaum 70
528     if( y < 0 && abs(x) < abs(y))
529     return OSD_UP;
530    
531     if( y > 0 && abs(x) < abs(y))
532     return OSD_DOWN;
533    
534     if( x < 0 && abs(y) < abs(x))
535     return OSD_LEFT;
536    
537     if( x > 0 && abs(y) < abs(x))
538     return OSD_RIGHT;
539    
540     return OSD_BG;
541     }
542     return OSD_NONE;
543     }
544 harbaum 76 #endif
545 harbaum 70
546     /* check whether x/y is within the zoom pads */
547     static osd_button_t
548 harbaum 86 osd_check_zoom(gint x, gint y) {
549 harbaum 77 if( x > 0 && x < OSD_W && y > Z_TOP && y < Z_BOT) {
550 harbaum 70
551     /* within circle around (-) label */
552 harbaum 77 if( osm_gps_map_in_circle(x, y, Z_LEFT, Z_MID, Z_RAD))
553 harbaum 70 return OSD_OUT;
554    
555 harbaum 77 /* within circle around (+) label */
556     if( osm_gps_map_in_circle(x, y, Z_RIGHT, Z_MID, Z_RAD))
557     return OSD_IN;
558    
559     #if Z_GPS == 1
560     /* within square around center */
561     if( x > Z_CENTER - Z_RAD && x < Z_CENTER + Z_RAD)
562     return OSD_GPS;
563     #endif
564    
565 harbaum 70 /* between center of (-) button and center of entire zoom control area */
566 harbaum 77 if(x > OSD_LEFT && x < D_RAD)
567 harbaum 70 return OSD_OUT;
568    
569     /* between center of (+) button and center of entire zoom control area */
570 harbaum 77 if(x < OSD_RIGHT && x > D_RAD)
571 harbaum 70 return OSD_IN;
572     }
573    
574     return OSD_NONE;
575     }
576    
577 harbaum 88 #ifdef OSD_SOURCE_SEL
578    
579 harbaum 86 /* place source selection at right border */
580     #define OSD_S_RAD (Z_RAD)
581     #define OSD_S_X (-OSD_X)
582     #define OSD_S_Y (OSD_Y)
583     #define OSD_S_PW (2 * Z_RAD)
584     #define OSD_S_W (OSD_S_PW)
585     #define OSD_S_PH (2 * Z_RAD)
586     #define OSD_S_H (OSD_S_PH + OSD_SHADOW)
587    
588 harbaum 87 /* size of usable area when expanded */
589 harbaum 110 #define OSD_S_AREA_W (priv->source_sel.width)
590     #define OSD_S_AREA_H (priv->source_sel.height)
591 harbaum 86 #define OSD_S_EXP_W (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
592     #define OSD_S_EXP_H (OSD_S_AREA_H + OSD_SHADOW)
593    
594     /* internal value to draw the arrow on the "puller" */
595     #define OSD_S_D0 (OSD_S_RAD/2)
596 harbaum 87 #ifndef OSD_FONT_SIZE
597     #define OSD_FONT_SIZE 16.0
598     #endif
599     #define OSD_TEXT_BORDER (OSD_FONT_SIZE/2)
600     #define OSD_TEXT_SKIP (OSD_FONT_SIZE/8)
601 harbaum 86
602 harbaum 88 /* draw the shape of the source selection OSD, either only the puller (not expanded) */
603     /* or the entire menu incl. the puller (expanded) */
604 harbaum 86 static void
605     osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
606 harbaum 110 if(!priv->source_sel.expanded) {
607 harbaum 86 /* just draw the puller */
608     cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
609     cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
610     cairo_line_to (cr, x + OSD_S_PW, y);
611     } else {
612     /* draw the puller and the area itself */
613     cairo_move_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y + OSD_S_AREA_H);
614     cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H);
615     if(OSD_S_Y > 0) {
616     cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_PH);
617     cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
618     } else {
619     cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_AREA_H-OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
620     cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H - OSD_S_PH);
621     cairo_line_to (cr, x + OSD_S_PW, y);
622     }
623     cairo_line_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y);
624     cairo_close_path (cr);
625     }
626     }
627    
628     static void
629 harbaum 87 osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
630     osd_priv_t *priv = (osd_priv_t*)osd->priv;
631 harbaum 86
632 harbaum 87 int py = offset + OSD_S_RAD - OSD_S_D0;
633    
634 harbaum 110 if(!priv->source_sel.expanded) {
635 harbaum 86 /* draw the "puller" open (<) arrow */
636 harbaum 87 cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
637 harbaum 86 cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
638     cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
639     } else {
640     if(OSD_S_Y < 0)
641 harbaum 87 py += OSD_S_AREA_H - OSD_S_PH;
642 harbaum 86
643     /* draw the "puller" close (>) arrow */
644 harbaum 87 cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
645 harbaum 86 cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
646     cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
647 harbaum 87 cairo_stroke(cr);
648    
649     /* don't draw a shadow for the text content */
650     if(offset == 1) {
651     gint source;
652     g_object_get(osd->widget, "map-source", &source, NULL);
653    
654     cairo_select_font_face (cr, "Sans",
655     CAIRO_FONT_SLANT_NORMAL,
656     CAIRO_FONT_WEIGHT_BOLD);
657     cairo_set_font_size (cr, OSD_FONT_SIZE);
658    
659 harbaum 110 int i, step = (priv->source_sel.height - 2*OSD_TEXT_BORDER) /
660 harbaum 89 OSM_GPS_MAP_SOURCE_LAST;
661 harbaum 87 for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
662     cairo_text_extents_t extents;
663     const char *src = osm_gps_map_source_get_friendly_name(i);
664     cairo_text_extents (cr, src, &extents);
665    
666     int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
667     int y = offset + step * (i-1) + OSD_TEXT_BORDER;
668    
669     /* draw filled rectangle if selected */
670     if(source == i) {
671     cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
672     y - OSD_TEXT_SKIP,
673 harbaum 110 priv->source_sel.width - OSD_TEXT_BORDER,
674 harbaum 87 step + OSD_TEXT_SKIP);
675     cairo_fill(cr);
676    
677     /* temprarily draw with background color */
678     #ifndef OSD_COLOR
679     GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
680     gdk_cairo_set_source_color(cr, &bg);
681     #else
682     cairo_set_source_rgb (cr, OSD_COLOR_BG);
683     #endif
684     }
685    
686     cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
687     cairo_show_text (cr, src);
688    
689     /* restore color */
690     if(source == i) {
691     #ifndef OSD_COLOR
692     GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
693     gdk_cairo_set_source_color(cr, &fg);
694     #else
695     cairo_set_source_rgb (cr, OSD_COLOR);
696     #endif
697     }
698     }
699     }
700 harbaum 86 }
701     }
702    
703     static void
704 harbaum 111 osd_render_source_sel(osm_gps_map_osd_t *osd, gboolean force_rerender) {
705 harbaum 86 osd_priv_t *priv = (osd_priv_t*)osd->priv;
706    
707 harbaum 111 if(priv->source_sel.rendered && !force_rerender)
708     return;
709    
710     priv->source_sel.rendered = TRUE;
711    
712 harbaum 86 #ifndef OSD_COLOR
713     GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
714     GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
715     GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
716     #endif
717    
718     /* draw source selector */
719 harbaum 110 cairo_t *cr = cairo_create(priv->source_sel.surface);
720 harbaum 86 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
721     cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
722     cairo_paint(cr);
723     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
724    
725     #ifdef OSD_SHADOW_ENABLE
726     osd_source_shape(priv, cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
727     osd_shape_shadow(cr);
728     #endif
729    
730     osd_source_shape(priv, cr, 1, 1);
731     #ifndef OSD_COLOR
732     osd_shape(cr, &bg, &fg);
733     #else
734     osd_shape(cr);
735     #endif
736    
737     #ifdef OSD_SHADOW_ENABLE
738     osd_labels_shadow(cr, Z_RAD/3, TRUE);
739 harbaum 87 osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
740     cairo_stroke (cr);
741 harbaum 86 #endif
742     #ifndef OSD_COLOR
743     osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
744     #else
745     osd_labels(cr, Z_RAD/3, TRUE);
746     #endif
747 harbaum 87 osd_source_content(osd, cr, 1);
748     cairo_stroke (cr);
749 harbaum 86
750     cairo_destroy(cr);
751     }
752    
753 harbaum 89 /* re-allocate the buffer used to draw the menu. This is used */
754     /* to collapse/expand the buffer */
755 harbaum 86 static void
756     osd_source_reallocate(osm_gps_map_osd_t *osd) {
757     osd_priv_t *priv = (osd_priv_t*)osd->priv;
758    
759     /* re-allocate offscreen bitmap */
760 harbaum 110 g_assert (priv->source_sel.surface);
761 harbaum 86
762     int w = OSD_S_W, h = OSD_S_H;
763 harbaum 110 if(priv->source_sel.expanded) {
764 harbaum 87 cairo_text_extents_t extents;
765    
766     /* determine content size */
767 harbaum 110 cairo_t *cr = cairo_create(priv->source_sel.surface);
768 harbaum 87 cairo_select_font_face (cr, "Sans",
769     CAIRO_FONT_SLANT_NORMAL,
770     CAIRO_FONT_WEIGHT_BOLD);
771     cairo_set_font_size (cr, OSD_FONT_SIZE);
772    
773     /* calculate menu size */
774     int i, max_h = 0, max_w = 0;
775     for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
776     const char *src = osm_gps_map_source_get_friendly_name(i);
777     cairo_text_extents (cr, src, &extents);
778    
779     if(extents.width > max_w) max_w = extents.width;
780     if(extents.height > max_h) max_h = extents.height;
781     }
782     cairo_destroy(cr);
783    
784 harbaum 110 priv->source_sel.width = max_w + 2*OSD_TEXT_BORDER;
785     priv->source_sel.height = OSM_GPS_MAP_SOURCE_LAST *
786 harbaum 87 (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
787    
788 harbaum 86 w = OSD_S_EXP_W;
789     h = OSD_S_EXP_H;
790     }
791    
792 harbaum 110 cairo_surface_destroy(priv->source_sel.surface);
793     priv->source_sel.surface =
794 harbaum 86 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
795    
796 harbaum 111 osd_render_source_sel(osd, TRUE);
797 harbaum 86 }
798    
799     #define OSD_HZ 15
800 harbaum 87 #define OSD_TIME 500
801 harbaum 86
802     static gboolean osd_source_animate(gpointer data) {
803     osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
804     osd_priv_t *priv = (osd_priv_t*)osd->priv;
805     int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
806     gboolean done = FALSE;
807 harbaum 110 priv->source_sel.count += priv->source_sel.dir;
808 harbaum 86
809     /* shifting in */
810 harbaum 110 if(priv->source_sel.dir < 0) {
811     if(priv->source_sel.count <= 0) {
812     priv->source_sel.count = 0;
813 harbaum 86 done = TRUE;
814     }
815     } else {
816 harbaum 110 if(priv->source_sel.count >= 1000) {
817     priv->source_sel.expanded = FALSE;
818 harbaum 86 osd_source_reallocate(osd);
819    
820 harbaum 110 priv->source_sel.count = 1000;
821 harbaum 86 done = TRUE;
822     }
823     }
824    
825    
826     /* count runs linearly from 0 to 1000, map this nicely onto a position */
827    
828 harbaum 111 /* nice sinoid mapping */
829 harbaum 110 float m = 0.5-cos(priv->source_sel.count * M_PI / 1000.0)/2;
830 harbaum 111 priv->source_sel.shift =
831     (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
832 harbaum 86 m * diff;
833    
834 harbaum 111 /* make sure the screen is updated */
835 harbaum 86 osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
836    
837 harbaum 111 /* stop animation if done */
838 harbaum 86 if(done)
839 harbaum 110 priv->source_sel.handler_id = 0;
840 harbaum 86
841     return !done;
842     }
843    
844     /* switch between expand and collapse mode of source selection */
845     static void
846     osd_source_toggle(osm_gps_map_osd_t *osd)
847     {
848     osd_priv_t *priv = (osd_priv_t*)osd->priv;
849    
850     /* ignore clicks while animation is running */
851 harbaum 110 if(priv->source_sel.handler_id)
852 harbaum 86 return;
853    
854 harbaum 111 /* expand immediately, collapse is handle at the end of the */
855     /* collapse animation */
856 harbaum 110 if(!priv->source_sel.expanded) {
857     priv->source_sel.expanded = TRUE;
858 harbaum 86 osd_source_reallocate(osd);
859    
860 harbaum 110 priv->source_sel.count = 1000;
861     priv->source_sel.shift = osd->widget->allocation.width - OSD_S_W;
862     priv->source_sel.dir = -1000/OSD_HZ;
863 harbaum 86 } else {
864 harbaum 110 priv->source_sel.count = 0;
865 harbaum 111 priv->source_sel.shift = osd->widget->allocation.width -
866     OSD_S_EXP_W + OSD_S_X;
867 harbaum 110 priv->source_sel.dir = +1000/OSD_HZ;
868 harbaum 86 }
869    
870 harbaum 111 /* start timer to handle animation */
871     priv->source_sel.handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ,
872     osd_source_animate, osd);
873 harbaum 86 }
874    
875 harbaum 89 /* check if the user clicked inside the source selection area */
876 harbaum 70 static osd_button_t
877 harbaum 114 osd_source_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
878 harbaum 86 osd_priv_t *priv = (osd_priv_t*)osd->priv;
879    
880 harbaum 110 if(!priv->source_sel.expanded)
881 harbaum 86 x -= osd->widget->allocation.width - OSD_S_W;
882     else
883     x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
884    
885     if(OSD_S_Y > 0)
886     y -= OSD_S_Y;
887     else
888     y -= osd->widget->allocation.height - OSD_S_PH + OSD_S_Y;
889    
890     /* within square around puller? */
891     if(y > 0 && y < OSD_S_PH && x > 0 && x < OSD_S_PW) {
892     /* really within puller shape? */
893     if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
894     /* expand source selector */
895 harbaum 114 if(down)
896     osd_source_toggle(osd);
897 harbaum 86
898     /* tell upper layers that user clicked some background element */
899     /* of the OSD */
900     return OSD_BG;
901     }
902     }
903 harbaum 88
904     /* check for clicks into data area */
905 harbaum 110 if(priv->source_sel.expanded && !priv->source_sel.handler_id) {
906 harbaum 94 /* re-adjust from puller top to content top */
907     if(OSD_S_Y < 0)
908     y += OSD_S_EXP_H - OSD_S_PH;
909    
910 harbaum 88 if(x > OSD_S_PW &&
911     x < OSD_S_PW + OSD_S_EXP_W &&
912     y > 0 &&
913     y < OSD_S_EXP_H) {
914 harbaum 94
915 harbaum 110 int step = (priv->source_sel.height - 2*OSD_TEXT_BORDER)
916 harbaum 89 / OSM_GPS_MAP_SOURCE_LAST;
917 harbaum 88
918 harbaum 89 y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
919     y /= step;
920     y += 1;
921    
922 harbaum 114 if(down) {
923     gint old = 0;
924     g_object_get(osd->widget, "map-source", &old, NULL);
925 harbaum 89
926 harbaum 114 if(y > OSM_GPS_MAP_SOURCE_NULL &&
927     y <= OSM_GPS_MAP_SOURCE_LAST &&
928     old != y) {
929     g_object_set(osd->widget, "map-source", y, NULL);
930    
931     osd_render_source_sel(osd, TRUE);
932     osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
933     }
934 harbaum 89 }
935    
936     /* return "clicked in OSD background" to prevent further */
937     /* processing by application */
938 harbaum 88 return OSD_BG;
939     }
940     }
941    
942 harbaum 86 return OSD_NONE;
943     }
944 harbaum 88 #endif // OSD_SOURCE_SEL
945 harbaum 86
946     static osd_button_t
947 harbaum 114 osd_check(osm_gps_map_osd_t *osd, gboolean down, gint x, gint y) {
948 harbaum 70 osd_button_t but = OSD_NONE;
949    
950 harbaum 113 #ifdef OSD_BALLOON
951 harbaum 114 if(down) {
952     /* needed to handle balloons that are created at click */
953     osd_priv_t *priv = (osd_priv_t*)osd->priv;
954     priv->balloon.just_created = FALSE;
955     }
956 harbaum 113 #endif
957    
958 harbaum 86 #ifdef OSD_SOURCE_SEL
959     /* the source selection area is handles internally */
960 harbaum 114 but = osd_source_check(osd, down, x, y);
961 harbaum 86 #endif
962 harbaum 114
963     if(but == OSD_NONE) {
964 harbaum 120 gint mx = x - OSD_X;
965     gint my = y - OSD_Y;
966 harbaum 114
967     if(OSD_X < 0)
968 harbaum 120 mx -= (osd->widget->allocation.width - OSD_W);
969 harbaum 114
970     if(OSD_Y < 0)
971 harbaum 120 my -= (osd->widget->allocation.height - OSD_H);
972 harbaum 114
973     /* first do a rough test for the OSD area. */
974     /* this is just to avoid an unnecessary detailed test */
975 harbaum 120 if(mx > 0 && mx < OSD_W && my > 0 && my < OSD_H) {
976 harbaum 76 #ifndef OSD_NO_DPAD
977 harbaum 120 but = osd_check_dpad(mx, my);
978 harbaum 76 #endif
979 harbaum 114 }
980 harbaum 70
981     if(but == OSD_NONE)
982 harbaum 120 but = osd_check_zoom(mx, my);
983 harbaum 70 }
984    
985 harbaum 114 #ifdef OSD_BALLOON
986     if(but == OSD_NONE) {
987     /* check if user clicked into balloon */
988     if(osd_balloon_check(osd, down, x, y))
989     but = OSD_BG;
990     }
991     #endif
992    
993 harbaum 70 return but;
994     }
995    
996 harbaum 76 #ifndef OSD_NO_DPAD
997 harbaum 70 static void
998 harbaum 86 osd_dpad_labels(cairo_t *cr, gint x, gint y) {
999 harbaum 70 /* move reference to dpad center */
1000     x += D_RAD;
1001     y += D_RAD;
1002    
1003     const static gint offset[][3][2] = {
1004     /* left arrow/triangle */
1005     { { -D_TIP+D_LEN, -D_WID }, { -D_LEN, D_WID }, { +D_LEN, D_WID } },
1006     /* right arrow/triangle */
1007     { { +D_TIP-D_LEN, -D_WID }, { +D_LEN, D_WID }, { -D_LEN, D_WID } },
1008     /* top arrow/triangle */
1009     { { -D_WID, -D_TIP+D_LEN }, { D_WID, -D_LEN }, { D_WID, +D_LEN } },
1010     /* bottom arrow/triangle */
1011     { { -D_WID, +D_TIP-D_LEN }, { D_WID, +D_LEN }, { D_WID, -D_LEN } }
1012     };
1013    
1014     int i;
1015     for(i=0;i<4;i++) {
1016     cairo_move_to (cr, x + offset[i][0][0], y + offset[i][0][1]);
1017     cairo_rel_line_to (cr, offset[i][1][0], offset[i][1][1]);
1018     cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);
1019     }
1020     }
1021 harbaum 76 #endif
1022 harbaum 70
1023 harbaum 76 #ifdef OSD_GPS_BUTTON
1024     /* draw the satellite dish icon in the center of the dpad */
1025 harbaum 77 #define GPS_V0 (D_RAD/7)
1026 harbaum 70 #define GPS_V1 (D_RAD/10)
1027     #define GPS_V2 (D_RAD/5)
1028    
1029     /* draw a satellite receiver dish */
1030 harbaum 77 /* this is either drawn in the center of the dpad (if present) */
1031     /* or in the middle of the zoom area */
1032 harbaum 70 static void
1033 harbaum 86 osd_dpad_gps(cairo_t *cr, gint x, gint y) {
1034 harbaum 70 /* move reference to dpad center */
1035 harbaum 77 x += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD * 3;
1036     y += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD + GPS_V0;
1037 harbaum 70
1038     cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
1039     cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);
1040     cairo_rel_line_to (cr, +GPS_V0, +GPS_V0);
1041     cairo_close_path (cr);
1042    
1043     cairo_move_to (cr, x+GPS_V1-GPS_V2, y-2*GPS_V2);
1044     cairo_curve_to (cr, x-GPS_V2, y, x+GPS_V1, y+GPS_V1, x+GPS_V1+GPS_V2, y);
1045     cairo_close_path (cr);
1046    
1047     x += GPS_V1;
1048     cairo_move_to (cr, x, y-GPS_V2);
1049     cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
1050     }
1051 harbaum 76 #endif
1052 harbaum 70
1053     #define Z_LEN (2*Z_RAD/3)
1054    
1055     static void
1056 harbaum 86 osd_zoom_labels(cairo_t *cr, gint x, gint y) {
1057 harbaum 70 cairo_move_to (cr, x + Z_LEFT - Z_LEN, y + Z_MID);
1058     cairo_line_to (cr, x + Z_LEFT + Z_LEN, y + Z_MID);
1059    
1060     cairo_move_to (cr, x + Z_RIGHT, y + Z_MID - Z_LEN);
1061     cairo_line_to (cr, x + Z_RIGHT, y + Z_MID + Z_LEN);
1062     cairo_move_to (cr, x + Z_RIGHT - Z_LEN, y + Z_MID);
1063     cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
1064     }
1065    
1066 harbaum 106 #ifdef OSD_COORDINATES
1067    
1068 harbaum 107 #ifndef OSD_COORDINATES_FONT_SIZE
1069     #define OSD_COORDINATES_FONT_SIZE 12
1070     #endif
1071    
1072 harbaum 108 #define OSD_COORDINATES_OFFSET (OSD_COORDINATES_FONT_SIZE/6)
1073 harbaum 107
1074 harbaum 108 #define OSD_COORDINATES_W (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1075 harbaum 124 #define OSD_COORDINATES_H (2*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1076 harbaum 108
1077 harbaum 107 /* these can be overwritten with versions that support */
1078     /* localization */
1079     #ifndef OSD_COORDINATES_CHR_N
1080     #define OSD_COORDINATES_CHR_N "N"
1081     #endif
1082     #ifndef OSD_COORDINATES_CHR_S
1083     #define OSD_COORDINATES_CHR_S "S"
1084     #endif
1085     #ifndef OSD_COORDINATES_CHR_E
1086     #define OSD_COORDINATES_CHR_E "E"
1087     #endif
1088     #ifndef OSD_COORDINATES_CHR_W
1089     #define OSD_COORDINATES_CHR_W "W"
1090     #endif
1091    
1092    
1093    
1094     /* this is the classic geocaching notation */
1095     static char
1096     *osd_latitude_str(float latitude) {
1097     char *c = OSD_COORDINATES_CHR_N;
1098     float integral, fractional;
1099    
1100     if(isnan(latitude))
1101     return NULL;
1102    
1103     if(latitude < 0) {
1104     latitude = fabs(latitude);
1105     c = OSD_COORDINATES_CHR_S;
1106     }
1107    
1108     fractional = modff(latitude, &integral);
1109    
1110     return g_strdup_printf("%s %02d° %06.3f'",
1111     c, (int)integral, fractional*60.0);
1112     }
1113    
1114     static char
1115     *osd_longitude_str(float longitude) {
1116     char *c = OSD_COORDINATES_CHR_E;
1117     float integral, fractional;
1118    
1119     if(isnan(longitude))
1120     return NULL;
1121    
1122     if(longitude < 0) {
1123     longitude = fabs(longitude);
1124     c = OSD_COORDINATES_CHR_W;
1125     }
1126    
1127     fractional = modff(longitude, &integral);
1128    
1129     return g_strdup_printf("%s %03d° %06.3f'",
1130     c, (int)integral, fractional*60.0);
1131     }
1132    
1133 harbaum 124 /* render a string at the given screen position */
1134     static int
1135     osd_render_centered_text(cairo_t *cr, int y, int width, char *text) {
1136     char *p = g_strdup(text);
1137     cairo_text_extents_t extents;
1138     cairo_text_extents (cr, p, &extents);
1139    
1140     /* check if text needs to be truncated */
1141     int len = strlen(text)-2;
1142     while(extents.width > width) {
1143     len--;
1144     strcpy(p+len, "...");
1145     cairo_text_extents (cr, p, &extents);
1146     }
1147    
1148 harbaum 123 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1149     cairo_set_line_width (cr, OSD_COORDINATES_FONT_SIZE/6);
1150 harbaum 124 cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1151     cairo_text_path (cr, p);
1152 harbaum 123 cairo_stroke (cr);
1153    
1154     cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1155 harbaum 124 cairo_move_to (cr, (width - extents.width)/2, y - extents.y_bearing);
1156     cairo_show_text (cr, p);
1157    
1158     g_free(p);
1159    
1160     /* skip + 1/4 line */
1161     return y + 5*OSD_COORDINATES_FONT_SIZE/4;
1162 harbaum 123 }
1163    
1164     static void
1165 harbaum 106 osd_render_coordinates(osm_gps_map_osd_t *osd)
1166     {
1167     osd_priv_t *priv = (osd_priv_t*)osd->priv;
1168    
1169 harbaum 107 /* get current map position */
1170     gfloat lat, lon;
1171     g_object_get(osd->widget, "latitude", &lat, "longitude", &lon, NULL);
1172    
1173 harbaum 108 /* check if position has changed enough to require redraw */
1174 harbaum 110 if(!isnan(priv->coordinates.lat) && !isnan(priv->coordinates.lon))
1175     /* 1/60000 == 1/1000 minute */
1176     if((fabsf(lat - priv->coordinates.lat) < 1/60000) &&
1177     (fabsf(lon - priv->coordinates.lon) < 1/60000))
1178 harbaum 108 return;
1179    
1180 harbaum 110 priv->coordinates.lat = lat;
1181     priv->coordinates.lon = lon;
1182 harbaum 108
1183 harbaum 111 /* first fill with transparency */
1184 harbaum 110 cairo_t *cr = cairo_create(priv->coordinates.surface);
1185 harbaum 106 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1186 harbaum 111 // cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
1187     cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1188 harbaum 106 cairo_paint(cr);
1189     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1190    
1191 harbaum 107 cairo_select_font_face (cr, "Sans",
1192     CAIRO_FONT_SLANT_NORMAL,
1193     CAIRO_FONT_WEIGHT_BOLD);
1194     cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1195    
1196     char *latitude = osd_latitude_str(lat);
1197     char *longitude = osd_longitude_str(lon);
1198    
1199 harbaum 124 int y = OSD_COORDINATES_OFFSET;
1200     y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, latitude);
1201     y = osd_render_centered_text(cr, y, OSD_COORDINATES_W, longitude);
1202 harbaum 123
1203 harbaum 107 g_free(latitude);
1204     g_free(longitude);
1205    
1206 harbaum 106 cairo_destroy(cr);
1207     }
1208     #endif // OSD_COORDINATES
1209    
1210 harbaum 122 #ifdef OSD_NAV
1211 harbaum 123 #define OSD_NAV_W (8*OSD_COORDINATES_FONT_SIZE+2*OSD_COORDINATES_OFFSET)
1212 harbaum 122 #define OSD_NAV_H (100)
1213    
1214     static void
1215     osd_render_nav(osm_gps_map_osd_t *osd)
1216     {
1217     osd_priv_t *priv = (osd_priv_t*)osd->priv;
1218    
1219 harbaum 123 if(!priv->nav.surface || isnan(priv->nav.lat) || isnan(priv->nav.lon))
1220     return;
1221    
1222 harbaum 122 /* first fill with transparency */
1223     cairo_t *cr = cairo_create(priv->nav.surface);
1224     cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1225     cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1226     cairo_paint(cr);
1227     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1228    
1229 harbaum 123 cairo_select_font_face (cr, "Sans",
1230     CAIRO_FONT_SLANT_NORMAL,
1231     CAIRO_FONT_WEIGHT_BOLD);
1232     cairo_set_font_size (cr, OSD_COORDINATES_FONT_SIZE);
1233    
1234     char *latitude = osd_latitude_str(priv->nav.lat);
1235     char *longitude = osd_longitude_str(priv->nav.lon);
1236    
1237 harbaum 124 int y = OSD_COORDINATES_OFFSET;
1238     y = osd_render_centered_text(cr, y, OSD_NAV_W, priv->nav.name);
1239     y = osd_render_centered_text(cr, y, OSD_NAV_W, latitude);
1240     y = osd_render_centered_text(cr, y, OSD_NAV_W, longitude);
1241 harbaum 123
1242     g_free(latitude);
1243     g_free(longitude);
1244    
1245 harbaum 122 cairo_destroy(cr);
1246     }
1247    
1248 harbaum 123 void osm_gps_map_osd_clear_nav (OsmGpsMap *map) {
1249     g_return_if_fail (OSM_IS_GPS_MAP (map));
1250    
1251     osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1252     g_return_if_fail (osd);
1253    
1254     osd_priv_t *priv = (osd_priv_t*)osd->priv;
1255     g_return_if_fail (priv);
1256    
1257     if(priv->nav.surface) {
1258     cairo_surface_destroy(priv->nav.surface);
1259     priv->nav.surface = NULL;
1260     priv->nav.lat = OSM_GPS_MAP_INVALID;
1261     priv->nav.lon = OSM_GPS_MAP_INVALID;
1262     if(priv->nav.name) g_free(priv->nav.name);
1263     }
1264     osm_gps_map_redraw(map);
1265     }
1266    
1267     void
1268     osm_gps_map_osd_draw_nav (OsmGpsMap *map, float latitude, float longitude,
1269     char *name) {
1270     g_return_if_fail (OSM_IS_GPS_MAP (map));
1271    
1272     osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1273     g_return_if_fail (osd);
1274    
1275     osd_priv_t *priv = (osd_priv_t*)osd->priv;
1276     g_return_if_fail (priv);
1277    
1278     osm_gps_map_osd_clear_nav (map);
1279    
1280     /* allocate balloon surface */
1281     priv->nav.surface =
1282     cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1283     OSD_NAV_W+2, OSD_NAV_H+2);
1284    
1285     priv->nav.lat = latitude;
1286     priv->nav.lon = longitude;
1287     priv->nav.name = g_strdup(name);
1288    
1289     osd_render_nav(osd);
1290    
1291     osm_gps_map_redraw(map);
1292     }
1293    
1294 harbaum 122 #endif // OSD_NAV
1295    
1296    
1297 harbaum 105 #ifdef OSD_CROSSHAIR
1298    
1299     #ifndef OSD_CROSSHAIR_RADIUS
1300 harbaum 106 #define OSD_CROSSHAIR_RADIUS 10
1301 harbaum 105 #endif
1302    
1303 harbaum 106 #define OSD_CROSSHAIR_TICK (OSD_CROSSHAIR_RADIUS/2)
1304     #define OSD_CROSSHAIR_BORDER (OSD_CROSSHAIR_TICK + OSD_CROSSHAIR_RADIUS/4)
1305     #define OSD_CROSSHAIR_W ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1306     #define OSD_CROSSHAIR_H ((OSD_CROSSHAIR_RADIUS+OSD_CROSSHAIR_BORDER)*2)
1307 harbaum 105
1308     static void
1309 harbaum 106 osd_render_crosshair_shape(cairo_t *cr) {
1310     cairo_arc (cr, OSD_CROSSHAIR_W/2, OSD_CROSSHAIR_H/2,
1311     OSD_CROSSHAIR_RADIUS, 0, 2*M_PI);
1312    
1313     cairo_move_to (cr, OSD_CROSSHAIR_W/2 - OSD_CROSSHAIR_RADIUS,
1314     OSD_CROSSHAIR_H/2);
1315     cairo_rel_line_to (cr, -OSD_CROSSHAIR_TICK, 0);
1316     cairo_move_to (cr, OSD_CROSSHAIR_W/2 + OSD_CROSSHAIR_RADIUS,
1317     OSD_CROSSHAIR_H/2);
1318     cairo_rel_line_to (cr, OSD_CROSSHAIR_TICK, 0);
1319    
1320     cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1321     OSD_CROSSHAIR_H/2 - OSD_CROSSHAIR_RADIUS);
1322     cairo_rel_line_to (cr, 0, -OSD_CROSSHAIR_TICK);
1323     cairo_move_to (cr, OSD_CROSSHAIR_W/2,
1324     OSD_CROSSHAIR_H/2 + OSD_CROSSHAIR_RADIUS);
1325     cairo_rel_line_to (cr, 0, OSD_CROSSHAIR_TICK);
1326    
1327     cairo_stroke (cr);
1328     }
1329    
1330     static void
1331 harbaum 105 osd_render_crosshair(osm_gps_map_osd_t *osd)
1332     {
1333     osd_priv_t *priv = (osd_priv_t*)osd->priv;
1334    
1335 harbaum 110 if(priv->crosshair.rendered)
1336     return;
1337    
1338     priv->crosshair.rendered = TRUE;
1339    
1340 harbaum 105 /* first fill with transparency */
1341 harbaum 110 cairo_t *cr = cairo_create(priv->crosshair.surface);
1342 harbaum 105 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1343 harbaum 106 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1344 harbaum 105 cairo_paint(cr);
1345     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1346    
1347 harbaum 106 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1348    
1349     cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
1350     cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/2);
1351     osd_render_crosshair_shape(cr);
1352    
1353     cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1354     cairo_set_line_width (cr, OSD_CROSSHAIR_RADIUS/4);
1355     osd_render_crosshair_shape(cr);
1356    
1357 harbaum 105 cairo_destroy(cr);
1358     }
1359     #endif
1360    
1361     #ifdef OSD_SCALE
1362    
1363     #ifndef OSD_SCALE_FONT_SIZE
1364     #define OSD_SCALE_FONT_SIZE 12
1365     #endif
1366     #define OSD_SCALE_W (10*OSD_SCALE_FONT_SIZE)
1367     #define OSD_SCALE_H (5*OSD_SCALE_FONT_SIZE/2)
1368    
1369 harbaum 103 /* various parameters used to create the scale */
1370     #define OSD_SCALE_H2 (OSD_SCALE_H/2)
1371     #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
1372     #define OSD_SCALE_M (OSD_SCALE_H2 - OSD_SCALE_TICK)
1373     #define OSD_SCALE_I (OSD_SCALE_H2 + OSD_SCALE_TICK)
1374     #define OSD_SCALE_FD (OSD_SCALE_FONT_SIZE/4)
1375 harbaum 99
1376 harbaum 70 static void
1377 harbaum 98 osd_render_scale(osm_gps_map_osd_t *osd)
1378     {
1379 harbaum 86 osd_priv_t *priv = (osd_priv_t*)osd->priv;
1380 harbaum 103
1381     /* this only needs to be rendered if the zoom has changed */
1382     gint zoom;
1383     g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
1384 harbaum 110 if(zoom == priv->scale.zoom)
1385 harbaum 103 return;
1386    
1387 harbaum 110 priv->scale.zoom = zoom;
1388 harbaum 103
1389 harbaum 99 float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
1390 harbaum 70
1391 harbaum 98 /* first fill with transparency */
1392 harbaum 110 cairo_t *cr = cairo_create(priv->scale.surface);
1393 harbaum 98 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1394 harbaum 100 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1395 harbaum 103 // pink for testing: cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
1396 harbaum 98 cairo_paint(cr);
1397     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1398    
1399     /* determine the size of the scale width in meters */
1400 harbaum 103 float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
1401 harbaum 102
1402 harbaum 98 /* scale this to useful values */
1403     int exp = logf(width)*M_LOG10E;
1404     int mant = width/pow(10,exp);
1405 harbaum 99 int width_metric = mant * pow(10,exp);
1406 harbaum 103 char *dist_str = NULL;
1407     if(width_metric<1000)
1408     dist_str = g_strdup_printf("%u m", width_metric);
1409     else
1410     dist_str = g_strdup_printf("%u km", width_metric/1000);
1411 harbaum 99 width_metric /= m_per_pix;
1412    
1413 harbaum 102 /* and now the hard part: scale for useful imperial values :-( */
1414     /* try to convert to feet, 1ft == 0.3048 m */
1415     width /= 0.3048;
1416     float imp_scale = 0.3048;
1417     char *dist_imp_unit = "ft";
1418 harbaum 99
1419 harbaum 102 if(width >= 100) {
1420     /* 1yd == 3 feet */
1421     width /= 3.0;
1422     imp_scale *= 3.0;
1423     dist_imp_unit = "yd";
1424    
1425     if(width >= 1760.0) {
1426     /* 1mi == 1760 yd */
1427     width /= 1760.0;
1428     imp_scale *= 1760.0;
1429     dist_imp_unit = "mi";
1430     }
1431     }
1432    
1433 harbaum 103 /* also convert this to full tens/hundreds */
1434     exp = logf(width)*M_LOG10E;
1435     mant = width/pow(10,exp);
1436     int width_imp = mant * pow(10,exp);
1437     char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
1438 harbaum 102
1439 harbaum 103 /* convert back to pixels */
1440     width_imp *= imp_scale;
1441     width_imp /= m_per_pix;
1442    
1443 harbaum 99 cairo_select_font_face (cr, "Sans",
1444     CAIRO_FONT_SLANT_NORMAL,
1445     CAIRO_FONT_WEIGHT_BOLD);
1446 harbaum 102 cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
1447 harbaum 99 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1448    
1449     cairo_text_extents_t extents;
1450     cairo_text_extents (cr, dist_str, &extents);
1451    
1452 harbaum 100 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1453 harbaum 103 cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1454     cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1455 harbaum 100 cairo_text_path (cr, dist_str);
1456     cairo_stroke (cr);
1457 harbaum 103 cairo_move_to (cr, 2*OSD_SCALE_FD,
1458     OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1459     cairo_text_path (cr, dist_str_imp);
1460     cairo_stroke (cr);
1461 harbaum 100
1462     cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1463 harbaum 103 cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
1464 harbaum 99 cairo_show_text (cr, dist_str);
1465 harbaum 103 cairo_move_to (cr, 2*OSD_SCALE_FD,
1466     OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
1467     cairo_show_text (cr, dist_str_imp);
1468 harbaum 99
1469 harbaum 103 g_free(dist_str);
1470     g_free(dist_str_imp);
1471    
1472 harbaum 99 /* draw white line */
1473     cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1474     cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
1475 harbaum 103 cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
1476     cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1477     cairo_rel_line_to (cr, 0, OSD_SCALE_TICK);
1478 harbaum 99 cairo_rel_line_to (cr, width_metric, 0);
1479 harbaum 103 cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1480 harbaum 99 cairo_stroke(cr);
1481 harbaum 103 cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1482     cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1483     cairo_rel_line_to (cr, width_imp, 0);
1484     cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1485     cairo_stroke(cr);
1486 harbaum 99
1487     /* draw black line */
1488     cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
1489 harbaum 103 cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
1490     cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
1491     cairo_rel_line_to (cr, 0, OSD_SCALE_TICK);
1492 harbaum 99 cairo_rel_line_to (cr, width_metric, 0);
1493 harbaum 103 cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1494 harbaum 99 cairo_stroke(cr);
1495 harbaum 103 cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
1496     cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
1497     cairo_rel_line_to (cr, width_imp, 0);
1498     cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
1499     cairo_stroke(cr);
1500 harbaum 99
1501 harbaum 98 cairo_destroy(cr);
1502     }
1503 harbaum 105 #endif
1504 harbaum 98
1505     static void
1506 harbaum 108 osd_render_controls(osm_gps_map_osd_t *osd)
1507 harbaum 98 {
1508     osd_priv_t *priv = (osd_priv_t*)osd->priv;
1509    
1510 harbaum 108 if(priv->controls.rendered
1511     #ifdef OSD_GPS_BUTTON
1512     && (priv->controls.gps_enabled == (osd->cb != NULL))
1513     #endif
1514     )
1515     return;
1516    
1517     #ifdef OSD_GPS_BUTTON
1518     priv->controls.gps_enabled = (osd->cb != NULL);
1519     #endif
1520 harbaum 110 priv->controls.rendered = TRUE;
1521 harbaum 108
1522 harbaum 86 #ifndef OSD_COLOR
1523     GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
1524     GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
1525     GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
1526 harbaum 74 #endif
1527 harbaum 70
1528     /* first fill with transparency */
1529 harbaum 108 cairo_t *cr = cairo_create(priv->controls.surface);
1530 harbaum 70 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1531     cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
1532     cairo_paint(cr);
1533     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1534    
1535     /* --------- draw zoom and dpad shape shadow ----------- */
1536 harbaum 74 #ifdef OSD_SHADOW_ENABLE
1537 harbaum 86 osd_zoom_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1538     osd_shape_shadow(cr);
1539 harbaum 76 #ifndef OSD_NO_DPAD
1540 harbaum 86 osd_dpad_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
1541     osd_shape_shadow(cr);
1542 harbaum 74 #endif
1543 harbaum 76 #endif
1544 harbaum 70
1545     /* --------- draw zoom and dpad shape ----------- */
1546    
1547 harbaum 86 osd_zoom_shape(cr, 1, 1);
1548 harbaum 74 #ifndef OSD_COLOR
1549 harbaum 86 osd_shape(cr, &bg, &fg);
1550 harbaum 74 #else
1551 harbaum 86 osd_shape(cr);
1552 harbaum 74 #endif
1553 harbaum 76 #ifndef OSD_NO_DPAD
1554 harbaum 86 osd_dpad_shape(cr, 1, 1);
1555 harbaum 74 #ifndef OSD_COLOR
1556 harbaum 86 osd_shape(cr, &bg, &fg);
1557 harbaum 74 #else
1558 harbaum 86 osd_shape(cr);
1559 harbaum 74 #endif
1560 harbaum 76 #endif
1561 harbaum 70
1562     /* --------- draw zoom and dpad labels --------- */
1563    
1564 harbaum 74 #ifdef OSD_SHADOW_ENABLE
1565 harbaum 87 osd_labels_shadow(cr, Z_RAD/3, TRUE);
1566 harbaum 86 osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1567 harbaum 76 #ifndef OSD_NO_DPAD
1568 harbaum 86 osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1569 harbaum 76 #endif
1570 harbaum 87 cairo_stroke(cr);
1571 harbaum 76 #ifdef OSD_GPS_BUTTON
1572 harbaum 87 osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
1573 harbaum 86 osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
1574 harbaum 87 cairo_stroke(cr);
1575 harbaum 74 #endif
1576 harbaum 76 #endif
1577 harbaum 70
1578 harbaum 74 #ifndef OSD_COLOR
1579 harbaum 86 osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
1580 harbaum 74 #else
1581 harbaum 86 osd_labels(cr, Z_RAD/3, TRUE);
1582 harbaum 74 #endif
1583 harbaum 87 osd_zoom_labels(cr, 1, 1);
1584     #ifndef OSD_NO_DPAD
1585     osd_dpad_labels(cr, 1, 1);
1586     #endif
1587     cairo_stroke(cr);
1588    
1589 harbaum 74 #ifndef OSD_COLOR
1590 harbaum 86 osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
1591 harbaum 74 #else
1592 harbaum 86 osd_labels(cr, Z_RAD/6, osd->cb != NULL);
1593 harbaum 74 #endif
1594 harbaum 87 #ifdef OSD_GPS_BUTTON
1595     osd_dpad_gps(cr, 1, 1);
1596 harbaum 76 #endif
1597 harbaum 87 cairo_stroke(cr);
1598 harbaum 70
1599     cairo_destroy(cr);
1600 harbaum 108 }
1601 harbaum 86
1602 harbaum 108 static void
1603     osd_render(osm_gps_map_osd_t *osd)
1604     {
1605 harbaum 111 /* this function is actually called pretty often since the */
1606     /* OSD contents may have changed (due to a coordinate/zoom change). */
1607     /* The different OSD parts have to make sure that they don't */
1608     /* render unneccessarily often and thus waste CPU power */
1609    
1610 harbaum 108 osd_render_controls(osd);
1611    
1612 harbaum 88 #ifdef OSD_SOURCE_SEL
1613 harbaum 111 osd_render_source_sel(osd, FALSE);
1614 harbaum 88 #endif
1615 harbaum 98
1616     #ifdef OSD_SCALE
1617     osd_render_scale(osd);
1618     #endif
1619 harbaum 105
1620     #ifdef OSD_CROSSHAIR
1621     osd_render_crosshair(osd);
1622     #endif
1623 harbaum 106
1624 harbaum 122 #ifdef OSD_NAV
1625     osd_render_nav(osd);
1626     #endif
1627    
1628 harbaum 106 #ifdef OSD_COORDINATES
1629     osd_render_coordinates(osd);
1630     #endif
1631 harbaum 70 }
1632    
1633     static void
1634 harbaum 86 osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
1635 harbaum 70 {
1636 harbaum 73 osd_priv_t *priv = (osd_priv_t*)osd->priv;
1637 harbaum 70
1638     /* OSD itself uses some off-screen rendering, so check if the */
1639     /* offscreen buffer is present and create it if not */
1640 harbaum 108 if(!priv->controls.surface) {
1641 harbaum 70 /* create overlay ... */
1642 harbaum 108 priv->controls.surface =
1643 harbaum 86 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
1644    
1645 harbaum 108 priv->controls.rendered = FALSE;
1646     #ifdef OSD_GPS_BUTTON
1647     priv->controls.gps_enabled = FALSE;
1648     #endif
1649    
1650 harbaum 88 #ifdef OSD_SOURCE_SEL
1651 harbaum 86 /* the initial OSD state is alway not-expanded */
1652 harbaum 110 priv->source_sel.surface =
1653 harbaum 86 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1654     OSD_S_W+2, OSD_S_H+2);
1655 harbaum 111 priv->source_sel.rendered = FALSE;
1656 harbaum 88 #endif
1657 harbaum 86
1658 harbaum 98 #ifdef OSD_SCALE
1659 harbaum 110 priv->scale.surface =
1660 harbaum 98 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1661 harbaum 103 OSD_SCALE_W, OSD_SCALE_H);
1662 harbaum 110 priv->scale.zoom = -1;
1663 harbaum 98 #endif
1664    
1665 harbaum 105 #ifdef OSD_CROSSHAIR
1666 harbaum 110 priv->crosshair.surface =
1667 harbaum 105 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1668     OSD_CROSSHAIR_W, OSD_CROSSHAIR_H);
1669 harbaum 110 priv->crosshair.rendered = FALSE;
1670 harbaum 105 #endif
1671    
1672 harbaum 106 #ifdef OSD_COORDINATES
1673 harbaum 110 priv->coordinates.surface =
1674 harbaum 106 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
1675     OSD_COORDINATES_W, OSD_COORDINATES_H);
1676 harbaum 108
1677 harbaum 110 priv->coordinates.lat = priv->coordinates.lon = OSM_GPS_MAP_INVALID;
1678 harbaum 106 #endif
1679    
1680 harbaum 70 /* ... and render it */
1681 harbaum 86 osd_render(osd);
1682 harbaum 70 }
1683    
1684     // now draw this onto the original context
1685 harbaum 75 cairo_t *cr = gdk_cairo_create(drawable);
1686 harbaum 77
1687 harbaum 113 gint x, y;
1688 harbaum 77
1689 harbaum 106 #ifdef OSD_SCALE
1690     x = OSD_X;
1691     y = -OSD_Y;
1692     if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1693     if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1694 harbaum 77
1695 harbaum 110 cairo_set_source_surface(cr, priv->scale.surface, x, y);
1696 harbaum 106 cairo_paint(cr);
1697     #endif
1698    
1699     #ifdef OSD_CROSSHAIR
1700     x = (osd->widget->allocation.width - OSD_CROSSHAIR_W)/2;
1701     y = (osd->widget->allocation.height - OSD_CROSSHAIR_H)/2;
1702    
1703 harbaum 110 cairo_set_source_surface(cr, priv->crosshair.surface, x, y);
1704 harbaum 106 cairo_paint(cr);
1705     #endif
1706    
1707 harbaum 122 #ifdef OSD_NAV
1708 harbaum 123 if(priv->nav.surface) {
1709     x = OSD_X;
1710     if(x < 0) x += osd->widget->allocation.width - OSD_NAV_W;
1711     y = (osd->widget->allocation.height - OSD_NAV_H)/2;
1712    
1713     cairo_set_source_surface(cr, priv->nav.surface, x, y);
1714     cairo_paint(cr);
1715     }
1716 harbaum 122 #endif
1717    
1718 harbaum 106 #ifdef OSD_COORDINATES
1719     x = -OSD_X;
1720     y = -OSD_Y;
1721     if(x < 0) x += osd->widget->allocation.width - OSD_COORDINATES_W;
1722     if(y < 0) y += osd->widget->allocation.height - OSD_COORDINATES_H;
1723    
1724 harbaum 110 cairo_set_source_surface(cr, priv->coordinates.surface, x, y);
1725 harbaum 106 cairo_paint(cr);
1726     #endif
1727    
1728 harbaum 113 #ifdef OSD_BALLOON
1729     if(priv->balloon.surface) {
1730    
1731     /* convert given lat lon into screen coordinates */
1732     gint x, y;
1733     osm_gps_map_geographic_to_screen (OSM_GPS_MAP(osd->widget),
1734     priv->balloon.lat, priv->balloon.lon,
1735     &x, &y);
1736    
1737     /* check if balloon needs to be rerendered */
1738     osd_render_balloon(osd);
1739    
1740     cairo_set_source_surface(cr, priv->balloon.surface,
1741     x + priv->balloon.offset_x,
1742     y + priv->balloon.offset_y);
1743     cairo_paint(cr);
1744     }
1745     #endif
1746    
1747 harbaum 106 x = OSD_X;
1748     if(x < 0)
1749     x += osd->widget->allocation.width - OSD_W;
1750    
1751     y = OSD_Y;
1752     if(y < 0)
1753     y += osd->widget->allocation.height - OSD_H;
1754    
1755 harbaum 108 cairo_set_source_surface(cr, priv->controls.surface, x, y);
1756 harbaum 70 cairo_paint(cr);
1757 harbaum 86
1758     #ifdef OSD_SOURCE_SEL
1759 harbaum 110 if(!priv->source_sel.handler_id) {
1760 harbaum 86 /* the OSD source selection is not being animated */
1761 harbaum 110 if(!priv->source_sel.expanded)
1762 harbaum 86 x = osd->widget->allocation.width - OSD_S_W;
1763     else
1764     x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1765     } else
1766 harbaum 110 x = priv->source_sel.shift;
1767 harbaum 86
1768     y = OSD_S_Y;
1769     if(OSD_S_Y < 0) {
1770 harbaum 110 if(!priv->source_sel.expanded)
1771 harbaum 86 y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1772     else
1773     y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1774     }
1775    
1776 harbaum 110 cairo_set_source_surface(cr, priv->source_sel.surface, x, y);
1777 harbaum 86 cairo_paint(cr);
1778     #endif
1779    
1780 harbaum 70 cairo_destroy(cr);
1781     }
1782    
1783     static void
1784 harbaum 86 osd_free(osm_gps_map_osd_t *osd)
1785 harbaum 70 {
1786 harbaum 73 osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1787 harbaum 70
1788 harbaum 108 if (priv->controls.surface)
1789     cairo_surface_destroy(priv->controls.surface);
1790 harbaum 88
1791     #ifdef OSD_SOURCE_SEL
1792 harbaum 110 if(priv->source_sel.handler_id)
1793     gtk_timeout_remove(priv->source_sel.handler_id);
1794 harbaum 86
1795 harbaum 110 if (priv->source_sel.surface)
1796     cairo_surface_destroy(priv->source_sel.surface);
1797 harbaum 88 #endif
1798 harbaum 86
1799 harbaum 98 #ifdef OSD_SCALE
1800 harbaum 110 if (priv->scale.surface)
1801     cairo_surface_destroy(priv->scale.surface);
1802 harbaum 98 #endif
1803    
1804 harbaum 105 #ifdef OSD_CROSSHAIR
1805 harbaum 110 if (priv->crosshair.surface)
1806     cairo_surface_destroy(priv->crosshair.surface);
1807 harbaum 105 #endif
1808    
1809 harbaum 122 #ifdef OSD_NAV
1810     if (priv->nav.surface)
1811     cairo_surface_destroy(priv->nav.surface);
1812     #endif
1813    
1814 harbaum 106 #ifdef OSD_COORDINATES
1815 harbaum 110 if (priv->coordinates.surface)
1816     cairo_surface_destroy(priv->coordinates.surface);
1817 harbaum 106 #endif
1818    
1819 harbaum 112 #ifdef OSD_BALLOON
1820     if (priv->balloon.surface)
1821     cairo_surface_destroy(priv->balloon.surface);
1822     #endif
1823    
1824 harbaum 73 g_free(priv);
1825     }
1826    
1827 harbaum 87 static gboolean
1828     osd_busy(osm_gps_map_osd_t *osd)
1829     {
1830 harbaum 88 #ifdef OSD_SOURCE_SEL
1831 harbaum 87 osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1832 harbaum 110 return (priv->source_sel.handler_id != 0);
1833 harbaum 88 #else
1834     return FALSE;
1835     #endif
1836 harbaum 87 }
1837    
1838 harbaum 73 static osm_gps_map_osd_t osd_classic = {
1839 harbaum 88 .widget = NULL,
1840    
1841 harbaum 86 .draw = osd_draw,
1842     .check = osd_check,
1843     .render = osd_render,
1844     .free = osd_free,
1845 harbaum 87 .busy = osd_busy,
1846 harbaum 73
1847     .cb = NULL,
1848     .data = NULL,
1849    
1850     .priv = NULL
1851     };
1852    
1853     /* this is the only function that's externally visible */
1854     void
1855     osm_gps_map_osd_classic_init(OsmGpsMap *map)
1856     {
1857     osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
1858    
1859 harbaum 112 #ifdef OSD_BALLOON
1860     priv->balloon.lat = OSM_GPS_MAP_INVALID;
1861     priv->balloon.lon = OSM_GPS_MAP_INVALID;
1862     #endif
1863    
1864 harbaum 73 osd_classic.priv = priv;
1865    
1866     osm_gps_map_register_osd(map, &osd_classic);
1867     }
1868    
1869 harbaum 76 #ifdef OSD_GPS_BUTTON
1870 harbaum 74 /* below are osd specific functions which aren't used by osm-gps-map */
1871     /* but instead are to be used by the main application */
1872 harbaum 76 void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
1873     gpointer data) {
1874 harbaum 73 osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1875     g_return_if_fail (osd);
1876    
1877     osd->cb = cb;
1878     osd->data = data;
1879    
1880 harbaum 70 /* this may have changed the state of the gps button */
1881     /* we thus re-render the overlay */
1882 harbaum 73 osd->render(osd);
1883 harbaum 70
1884 harbaum 73 osm_gps_map_redraw(map);
1885 harbaum 70 }
1886 harbaum 76 #endif
1887 harbaum 86
1888     osd_button_t
1889     osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
1890     osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1891     g_return_val_if_fail (osd, OSD_NONE);
1892    
1893 harbaum 114 return osd_check(osd, TRUE, x, y);
1894 harbaum 86 }