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

Parent Directory Parent Directory | Revision Log Revision Log


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