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

Parent Directory Parent Directory | Revision Log Revision Log


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