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

Parent Directory Parent Directory | Revision Log Revision Log


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