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

Parent Directory Parent Directory | Revision Log Revision Log


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