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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (show annotations)
Wed Sep 9 11:50:50 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 32825 byte(s)
OSD scale done
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 <math.h> // M_PI/cos()
23
24 /* parameters that can be overwritten from the config file: */
25 /* OSD_DIAMETER */
26 /* OSD_X, OSD_Y */
27
28 #ifndef OSD_SCALE_FONT_SIZE
29 #define OSD_SCALE_FONT_SIZE 12
30 #endif
31 #define OSD_SCALE_W (10*OSD_SCALE_FONT_SIZE)
32 #define OSD_SCALE_H (5*OSD_SCALE_FONT_SIZE/2)
33
34 #ifndef USE_CAIRO
35 #error "OSD control display lacks a non-cairo implementation!"
36 #endif
37
38 #include <cairo.h>
39
40 #include "osm-gps-map.h"
41 #include "osm-gps-map-osd-classic.h"
42
43 //the osd controls
44 typedef struct {
45 /* the offscreen representation of the OSD */
46 cairo_surface_t *overlay;
47
48 #ifdef OSD_SCALE
49 cairo_surface_t *scale;
50 int scale_zoom;
51 #endif
52
53 #ifdef OSD_SOURCE_SEL
54 /* values to handle the "source" menu */
55 cairo_surface_t *map_source;
56 gboolean expanded;
57 gint shift, dir, count;
58 gint handler_id;
59 gint width, height;
60 #endif
61
62 } osd_priv_t;
63
64 /* position and extent of bounding box */
65 #ifndef OSD_X
66 #define OSD_X (10)
67 #endif
68
69 #ifndef OSD_Y
70 #define OSD_Y (10)
71 #endif
72
73 /* parameters of the direction shape */
74 #ifndef OSD_DIAMETER
75 #define D_RAD (30) // diameter of dpad
76 #else
77 #define D_RAD (OSD_DIAMETER)
78 #endif
79 #define D_TIP (4*D_RAD/5) // distance of arrow tip from dpad center
80 #define D_LEN (D_RAD/4) // length of arrow
81 #define D_WID (D_LEN) // width of arrow
82
83 /* parameters of the "zoom" pad */
84 #define Z_STEP (D_RAD/4) // distance between dpad and zoom
85 #define Z_RAD (D_RAD/2) // radius of "caps" of zoom bar
86
87 #ifdef OSD_SHADOW_ENABLE
88 /* shadow also depends on control size */
89 #define OSD_SHADOW (D_RAD/6)
90 #else
91 #define OSD_SHADOW (0)
92 #endif
93
94 /* normally the GPS button is in the center of the dpad. if there's */
95 /* no dpad it will go into the zoom area */
96 #if defined(OSD_GPS_BUTTON) && defined(OSD_NO_DPAD)
97 #define Z_GPS 1
98 #else
99 #define Z_GPS 0
100 #endif
101
102 /* total width and height of controls incl. shadow */
103 #define OSD_W (2*D_RAD + OSD_SHADOW + Z_GPS * 2 * Z_RAD)
104 #if !Z_GPS
105 #define OSD_H (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)
106 #else
107 #define OSD_H (2*Z_RAD + OSD_SHADOW)
108 #endif
109
110 #ifdef OSD_SHADOW_ENABLE
111 #define OSD_LBL_SHADOW (OSD_SHADOW/2)
112 #endif
113
114 #define Z_TOP ((1-Z_GPS) * (2 * D_RAD + Z_STEP))
115
116 #define Z_MID (Z_TOP + Z_RAD)
117 #define Z_BOT (Z_MID + Z_RAD)
118 #define Z_LEFT (Z_RAD)
119 #define Z_RIGHT (2 * D_RAD - Z_RAD + Z_GPS * 2 * Z_RAD)
120 #define Z_CENTER ((Z_RIGHT + Z_LEFT)/2)
121
122 /* create the cairo shape used for the zoom buttons */
123 static void
124 osd_zoom_shape(cairo_t *cr, gint x, gint y)
125 {
126 cairo_move_to (cr, x+Z_LEFT, y+Z_TOP);
127 cairo_line_to (cr, x+Z_RIGHT, y+Z_TOP);
128 cairo_arc (cr, x+Z_RIGHT, y+Z_MID, Z_RAD, -M_PI/2, M_PI/2);
129 cairo_line_to (cr, x+Z_LEFT, y+Z_BOT);
130 cairo_arc (cr, x+Z_LEFT, y+Z_MID, Z_RAD, M_PI/2, -M_PI/2);
131 }
132
133 /* ------------------- color/shadow functions ----------------- */
134
135 #ifndef OSD_COLOR
136 /* if no color has been specified we just use the gdks default colors */
137 static void
138 osd_labels(cairo_t *cr, gint width, gboolean enabled,
139 GdkColor *fg, GdkColor *disabled) {
140 if(enabled) gdk_cairo_set_source_color(cr, fg);
141 else gdk_cairo_set_source_color(cr, disabled);
142 cairo_set_line_width (cr, width);
143 }
144 #else
145 static void
146 osd_labels(cairo_t *cr, gint width, gboolean enabled) {
147 if(enabled) cairo_set_source_rgb (cr, OSD_COLOR);
148 else cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
149 cairo_set_line_width (cr, width);
150 }
151 #endif
152
153 #ifdef OSD_SHADOW_ENABLE
154 static void
155 osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
156 cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
157 cairo_set_line_width (cr, width);
158 }
159 #endif
160
161 #ifndef OSD_NO_DPAD
162 /* create the cairo shape used for the dpad */
163 static void
164 osd_dpad_shape(cairo_t *cr, gint x, gint y)
165 {
166 cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);
167 }
168 #endif
169
170 #ifdef OSD_SHADOW_ENABLE
171 static void
172 osd_shape_shadow(cairo_t *cr) {
173 cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
174 cairo_fill (cr);
175 cairo_stroke (cr);
176 }
177 #endif
178
179 #ifndef OSD_COLOR
180 /* if no color has been specified we just use the gdks default colors */
181 static void
182 osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
183 gdk_cairo_set_source_color(cr, bg);
184 cairo_fill_preserve (cr);
185 gdk_cairo_set_source_color(cr, fg);
186 cairo_set_line_width (cr, 1);
187 cairo_stroke (cr);
188 }
189 #else
190 static void
191 osd_shape(cairo_t *cr) {
192 cairo_set_source_rgb (cr, OSD_COLOR_BG);
193 cairo_fill_preserve (cr);
194 cairo_set_source_rgb (cr, OSD_COLOR);
195 cairo_set_line_width (cr, 1);
196 cairo_stroke (cr);
197 }
198 #endif
199
200
201 static gboolean
202 osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)
203 {
204 return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
205 }
206
207 #ifndef OSD_NO_DPAD
208 /* check whether x/y is within the dpad */
209 static osd_button_t
210 osd_check_dpad(gint x, gint y)
211 {
212 /* within entire dpad circle */
213 if( osm_gps_map_in_circle(x, y, D_RAD, D_RAD, D_RAD))
214 {
215 /* convert into position relative to dpads centre */
216 x -= D_RAD;
217 y -= D_RAD;
218
219 #ifdef OSD_GPS_BUTTON
220 /* check for dpad center goes here! */
221 if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))
222 return OSD_GPS;
223 #endif
224
225 if( y < 0 && abs(x) < abs(y))
226 return OSD_UP;
227
228 if( y > 0 && abs(x) < abs(y))
229 return OSD_DOWN;
230
231 if( x < 0 && abs(y) < abs(x))
232 return OSD_LEFT;
233
234 if( x > 0 && abs(y) < abs(x))
235 return OSD_RIGHT;
236
237 return OSD_BG;
238 }
239 return OSD_NONE;
240 }
241 #endif
242
243 /* check whether x/y is within the zoom pads */
244 static osd_button_t
245 osd_check_zoom(gint x, gint y) {
246 if( x > 0 && x < OSD_W && y > Z_TOP && y < Z_BOT) {
247
248 /* within circle around (-) label */
249 if( osm_gps_map_in_circle(x, y, Z_LEFT, Z_MID, Z_RAD))
250 return OSD_OUT;
251
252 /* within circle around (+) label */
253 if( osm_gps_map_in_circle(x, y, Z_RIGHT, Z_MID, Z_RAD))
254 return OSD_IN;
255
256 #if Z_GPS == 1
257 /* within square around center */
258 if( x > Z_CENTER - Z_RAD && x < Z_CENTER + Z_RAD)
259 return OSD_GPS;
260 #endif
261
262 /* between center of (-) button and center of entire zoom control area */
263 if(x > OSD_LEFT && x < D_RAD)
264 return OSD_OUT;
265
266 /* between center of (+) button and center of entire zoom control area */
267 if(x < OSD_RIGHT && x > D_RAD)
268 return OSD_IN;
269 }
270
271 return OSD_NONE;
272 }
273
274 #ifdef OSD_SOURCE_SEL
275
276 /* place source selection at right border */
277 #define OSD_S_RAD (Z_RAD)
278 #define OSD_S_X (-OSD_X)
279 #define OSD_S_Y (OSD_Y)
280 #define OSD_S_PW (2 * Z_RAD)
281 #define OSD_S_W (OSD_S_PW)
282 #define OSD_S_PH (2 * Z_RAD)
283 #define OSD_S_H (OSD_S_PH + OSD_SHADOW)
284
285 /* size of usable area when expanded */
286 #define OSD_S_AREA_W (priv->width)
287 #define OSD_S_AREA_H (priv->height)
288 #define OSD_S_EXP_W (OSD_S_PW + OSD_S_AREA_W + OSD_SHADOW)
289 #define OSD_S_EXP_H (OSD_S_AREA_H + OSD_SHADOW)
290
291 /* internal value to draw the arrow on the "puller" */
292 #define OSD_S_D0 (OSD_S_RAD/2)
293 #ifndef OSD_FONT_SIZE
294 #define OSD_FONT_SIZE 16.0
295 #endif
296 #define OSD_TEXT_BORDER (OSD_FONT_SIZE/2)
297 #define OSD_TEXT_SKIP (OSD_FONT_SIZE/8)
298
299 /* draw the shape of the source selection OSD, either only the puller (not expanded) */
300 /* or the entire menu incl. the puller (expanded) */
301 static void
302 osd_source_shape(osd_priv_t *priv, cairo_t *cr, gint x, gint y) {
303 if(!priv->expanded) {
304 /* just draw the puller */
305 cairo_move_to (cr, x + OSD_S_PW, y + OSD_S_PH);
306 cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
307 cairo_line_to (cr, x + OSD_S_PW, y);
308 } else {
309 /* draw the puller and the area itself */
310 cairo_move_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y + OSD_S_AREA_H);
311 cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H);
312 if(OSD_S_Y > 0) {
313 cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_PH);
314 cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
315 } else {
316 cairo_arc (cr, x+OSD_S_RAD, y+OSD_S_AREA_H-OSD_S_RAD, OSD_S_RAD, M_PI/2, -M_PI/2);
317 cairo_line_to (cr, x + OSD_S_PW, y + OSD_S_AREA_H - OSD_S_PH);
318 cairo_line_to (cr, x + OSD_S_PW, y);
319 }
320 cairo_line_to (cr, x + OSD_S_PW + OSD_S_AREA_W, y);
321 cairo_close_path (cr);
322 }
323 }
324
325 static void
326 osd_source_content(osm_gps_map_osd_t *osd, cairo_t *cr, gint offset) {
327 osd_priv_t *priv = (osd_priv_t*)osd->priv;
328
329 int py = offset + OSD_S_RAD - OSD_S_D0;
330
331 if(!priv->expanded) {
332 /* draw the "puller" open (<) arrow */
333 cairo_move_to (cr, offset + OSD_S_RAD + OSD_S_D0/2, py);
334 cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
335 cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
336 } else {
337 if(OSD_S_Y < 0)
338 py += OSD_S_AREA_H - OSD_S_PH;
339
340 /* draw the "puller" close (>) arrow */
341 cairo_move_to (cr, offset + OSD_S_RAD - OSD_S_D0/2, py);
342 cairo_rel_line_to (cr, +OSD_S_D0, +OSD_S_D0);
343 cairo_rel_line_to (cr, -OSD_S_D0, +OSD_S_D0);
344 cairo_stroke(cr);
345
346 /* don't draw a shadow for the text content */
347 if(offset == 1) {
348 gint source;
349 g_object_get(osd->widget, "map-source", &source, NULL);
350
351 cairo_select_font_face (cr, "Sans",
352 CAIRO_FONT_SLANT_NORMAL,
353 CAIRO_FONT_WEIGHT_BOLD);
354 cairo_set_font_size (cr, OSD_FONT_SIZE);
355
356 int i, step = (priv->height - 2*OSD_TEXT_BORDER) /
357 OSM_GPS_MAP_SOURCE_LAST;
358 for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
359 cairo_text_extents_t extents;
360 const char *src = osm_gps_map_source_get_friendly_name(i);
361 cairo_text_extents (cr, src, &extents);
362
363 int x = offset + OSD_S_PW + OSD_TEXT_BORDER;
364 int y = offset + step * (i-1) + OSD_TEXT_BORDER;
365
366 /* draw filled rectangle if selected */
367 if(source == i) {
368 cairo_rectangle(cr, x - OSD_TEXT_BORDER/2,
369 y - OSD_TEXT_SKIP,
370 priv->width - OSD_TEXT_BORDER,
371 step + OSD_TEXT_SKIP);
372 cairo_fill(cr);
373
374 /* temprarily draw with background color */
375 #ifndef OSD_COLOR
376 GdkColor bg = osd->widget->style->bg[GTK_STATE_NORMAL];
377 gdk_cairo_set_source_color(cr, &bg);
378 #else
379 cairo_set_source_rgb (cr, OSD_COLOR_BG);
380 #endif
381 }
382
383 cairo_move_to (cr, x, y + OSD_TEXT_SKIP - extents.y_bearing);
384 cairo_show_text (cr, src);
385
386 /* restore color */
387 if(source == i) {
388 #ifndef OSD_COLOR
389 GdkColor fg = osd->widget->style->fg[GTK_STATE_NORMAL];
390 gdk_cairo_set_source_color(cr, &fg);
391 #else
392 cairo_set_source_rgb (cr, OSD_COLOR);
393 #endif
394 }
395 }
396 }
397 }
398 }
399
400 static void
401 osd_render_source_sel(osm_gps_map_osd_t *osd) {
402 osd_priv_t *priv = (osd_priv_t*)osd->priv;
403
404 #ifndef OSD_COLOR
405 GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
406 GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
407 GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
408 #endif
409
410 /* draw source selector */
411 cairo_t *cr = cairo_create(priv->map_source);
412 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
413 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
414 cairo_paint(cr);
415 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
416
417 #ifdef OSD_SHADOW_ENABLE
418 osd_source_shape(priv, cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
419 osd_shape_shadow(cr);
420 #endif
421
422 osd_source_shape(priv, cr, 1, 1);
423 #ifndef OSD_COLOR
424 osd_shape(cr, &bg, &fg);
425 #else
426 osd_shape(cr);
427 #endif
428
429 #ifdef OSD_SHADOW_ENABLE
430 osd_labels_shadow(cr, Z_RAD/3, TRUE);
431 osd_source_content(osd, cr, 1+OSD_LBL_SHADOW);
432 cairo_stroke (cr);
433 #endif
434 #ifndef OSD_COLOR
435 osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
436 #else
437 osd_labels(cr, Z_RAD/3, TRUE);
438 #endif
439 osd_source_content(osd, cr, 1);
440 cairo_stroke (cr);
441
442 cairo_destroy(cr);
443 }
444
445 /* re-allocate the buffer used to draw the menu. This is used */
446 /* to collapse/expand the buffer */
447 static void
448 osd_source_reallocate(osm_gps_map_osd_t *osd) {
449 osd_priv_t *priv = (osd_priv_t*)osd->priv;
450
451 /* re-allocate offscreen bitmap */
452 g_assert (priv->map_source);
453
454 int w = OSD_S_W, h = OSD_S_H;
455 if(priv->expanded) {
456 cairo_text_extents_t extents;
457
458 /* determine content size */
459 cairo_t *cr = cairo_create(priv->map_source);
460 cairo_select_font_face (cr, "Sans",
461 CAIRO_FONT_SLANT_NORMAL,
462 CAIRO_FONT_WEIGHT_BOLD);
463 cairo_set_font_size (cr, OSD_FONT_SIZE);
464
465 /* calculate menu size */
466 int i, max_h = 0, max_w = 0;
467 for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
468 const char *src = osm_gps_map_source_get_friendly_name(i);
469 cairo_text_extents (cr, src, &extents);
470
471 if(extents.width > max_w) max_w = extents.width;
472 if(extents.height > max_h) max_h = extents.height;
473 }
474 cairo_destroy(cr);
475
476 priv->width = max_w + 2*OSD_TEXT_BORDER;
477 priv->height = OSM_GPS_MAP_SOURCE_LAST *
478 (max_h + 2*OSD_TEXT_SKIP) + 2*OSD_TEXT_BORDER;
479
480 w = OSD_S_EXP_W;
481 h = OSD_S_EXP_H;
482 }
483
484 cairo_surface_destroy(priv->map_source);
485 priv->map_source =
486 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
487
488 osd_render_source_sel(osd);
489 }
490
491 #define OSD_HZ 15
492 #define OSD_TIME 500
493
494 static gboolean osd_source_animate(gpointer data) {
495 osm_gps_map_osd_t *osd = (osm_gps_map_osd_t*)data;
496 osd_priv_t *priv = (osd_priv_t*)osd->priv;
497 int diff = OSD_S_EXP_W - OSD_S_W - OSD_S_X;
498 gboolean done = FALSE;
499 priv->count += priv->dir;
500
501 /* shifting in */
502 if(priv->dir < 0) {
503 if(priv->count <= 0) {
504 priv->count = 0;
505 done = TRUE;
506 }
507 } else {
508 if(priv->count >= 1000) {
509 priv->expanded = FALSE;
510 osd_source_reallocate(osd);
511
512 priv->count = 1000;
513 done = TRUE;
514 }
515 }
516
517
518 /* count runs linearly from 0 to 1000, map this nicely onto a position */
519
520 /* nicer sinoid mapping */
521 float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;
522 priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
523 m * diff;
524
525 osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
526
527 if(done)
528 priv->handler_id = 0;
529
530 return !done;
531 }
532
533 /* switch between expand and collapse mode of source selection */
534 static void
535 osd_source_toggle(osm_gps_map_osd_t *osd)
536 {
537 osd_priv_t *priv = (osd_priv_t*)osd->priv;
538
539 /* ignore clicks while animation is running */
540 if(priv->handler_id)
541 return;
542
543 /* expand immediately, collapse is handle at the end of the collapse animation */
544 if(!priv->expanded) {
545 priv->expanded = TRUE;
546 osd_source_reallocate(osd);
547
548 priv->count = 1000;
549 priv->shift = osd->widget->allocation.width - OSD_S_W;
550 priv->dir = -1000/OSD_HZ;
551 } else {
552 priv->count = 0;
553 priv->shift = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
554 priv->dir = +1000/OSD_HZ;
555 }
556
557 priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);
558 }
559
560 /* check if the user clicked inside the source selection area */
561 static osd_button_t
562 osd_source_check(osm_gps_map_osd_t *osd, gint x, gint y) {
563 osd_priv_t *priv = (osd_priv_t*)osd->priv;
564
565 if(!priv->expanded)
566 x -= osd->widget->allocation.width - OSD_S_W;
567 else
568 x -= osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
569
570 if(OSD_S_Y > 0)
571 y -= OSD_S_Y;
572 else
573 y -= osd->widget->allocation.height - OSD_S_PH + OSD_S_Y;
574
575 /* within square around puller? */
576 if(y > 0 && y < OSD_S_PH && x > 0 && x < OSD_S_PW) {
577 /* really within puller shape? */
578 if(x > Z_RAD || osm_gps_map_in_circle(x, y, Z_RAD, Z_RAD, Z_RAD)) {
579 /* expand source selector */
580 osd_source_toggle(osd);
581
582 /* tell upper layers that user clicked some background element */
583 /* of the OSD */
584 return OSD_BG;
585 }
586 }
587
588 /* check for clicks into data area */
589 if(priv->expanded && !priv->handler_id) {
590 /* re-adjust from puller top to content top */
591 if(OSD_S_Y < 0)
592 y += OSD_S_EXP_H - OSD_S_PH;
593
594 if(x > OSD_S_PW &&
595 x < OSD_S_PW + OSD_S_EXP_W &&
596 y > 0 &&
597 y < OSD_S_EXP_H) {
598
599 int step = (priv->height - 2*OSD_TEXT_BORDER)
600 / OSM_GPS_MAP_SOURCE_LAST;
601
602 y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
603 y /= step;
604 y += 1;
605
606 gint old = 0;
607 g_object_get(osd->widget, "map-source", &old, NULL);
608
609 if(y > OSM_GPS_MAP_SOURCE_NULL &&
610 y <= OSM_GPS_MAP_SOURCE_LAST &&
611 old != y) {
612 g_object_set(osd->widget, "map-source", y, NULL);
613
614 osd_render_source_sel(osd);
615 osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
616 }
617
618 /* return "clicked in OSD background" to prevent further */
619 /* processing by application */
620 return OSD_BG;
621 }
622 }
623
624 return OSD_NONE;
625 }
626 #endif // OSD_SOURCE_SEL
627
628 static osd_button_t
629 osd_check(osm_gps_map_osd_t *osd, gint x, gint y) {
630 osd_button_t but = OSD_NONE;
631
632 #ifdef OSD_SOURCE_SEL
633 /* the source selection area is handles internally */
634 but = osd_source_check(osd, x, y);
635 if(but != OSD_NONE)
636 return but;
637 #endif
638
639 x -= OSD_X;
640 y -= OSD_Y;
641
642 if(OSD_X < 0)
643 x -= (osd->widget->allocation.width - OSD_W);
644
645 if(OSD_Y < 0)
646 y -= (osd->widget->allocation.height - OSD_H);
647
648 /* first do a rough test for the OSD area. */
649 /* this is just to avoid an unnecessary detailed test */
650 if(x > 0 && x < OSD_W && y > 0 && y < OSD_H) {
651 #ifndef OSD_NO_DPAD
652 but = osd_check_dpad(x, y);
653 #endif
654
655 if(but == OSD_NONE)
656 but = osd_check_zoom(x, y);
657 }
658
659 return but;
660 }
661
662 #ifndef OSD_NO_DPAD
663 static void
664 osd_dpad_labels(cairo_t *cr, gint x, gint y) {
665 /* move reference to dpad center */
666 x += D_RAD;
667 y += D_RAD;
668
669 const static gint offset[][3][2] = {
670 /* left arrow/triangle */
671 { { -D_TIP+D_LEN, -D_WID }, { -D_LEN, D_WID }, { +D_LEN, D_WID } },
672 /* right arrow/triangle */
673 { { +D_TIP-D_LEN, -D_WID }, { +D_LEN, D_WID }, { -D_LEN, D_WID } },
674 /* top arrow/triangle */
675 { { -D_WID, -D_TIP+D_LEN }, { D_WID, -D_LEN }, { D_WID, +D_LEN } },
676 /* bottom arrow/triangle */
677 { { -D_WID, +D_TIP-D_LEN }, { D_WID, +D_LEN }, { D_WID, -D_LEN } }
678 };
679
680 int i;
681 for(i=0;i<4;i++) {
682 cairo_move_to (cr, x + offset[i][0][0], y + offset[i][0][1]);
683 cairo_rel_line_to (cr, offset[i][1][0], offset[i][1][1]);
684 cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);
685 }
686 }
687 #endif
688
689 #ifdef OSD_GPS_BUTTON
690 /* draw the satellite dish icon in the center of the dpad */
691 #define GPS_V0 (D_RAD/7)
692 #define GPS_V1 (D_RAD/10)
693 #define GPS_V2 (D_RAD/5)
694
695 /* draw a satellite receiver dish */
696 /* this is either drawn in the center of the dpad (if present) */
697 /* or in the middle of the zoom area */
698 static void
699 osd_dpad_gps(cairo_t *cr, gint x, gint y) {
700 /* move reference to dpad center */
701 x += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD * 3;
702 y += (1-Z_GPS) * D_RAD + Z_GPS * Z_RAD + GPS_V0;
703
704 cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
705 cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);
706 cairo_rel_line_to (cr, +GPS_V0, +GPS_V0);
707 cairo_close_path (cr);
708
709 cairo_move_to (cr, x+GPS_V1-GPS_V2, y-2*GPS_V2);
710 cairo_curve_to (cr, x-GPS_V2, y, x+GPS_V1, y+GPS_V1, x+GPS_V1+GPS_V2, y);
711 cairo_close_path (cr);
712
713 x += GPS_V1;
714 cairo_move_to (cr, x, y-GPS_V2);
715 cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
716 }
717 #endif
718
719 #define Z_LEN (2*Z_RAD/3)
720
721 static void
722 osd_zoom_labels(cairo_t *cr, gint x, gint y) {
723 cairo_move_to (cr, x + Z_LEFT - Z_LEN, y + Z_MID);
724 cairo_line_to (cr, x + Z_LEFT + Z_LEN, y + Z_MID);
725
726 cairo_move_to (cr, x + Z_RIGHT, y + Z_MID - Z_LEN);
727 cairo_line_to (cr, x + Z_RIGHT, y + Z_MID + Z_LEN);
728 cairo_move_to (cr, x + Z_RIGHT - Z_LEN, y + Z_MID);
729 cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
730 }
731
732 /* various parameters used to create the scale */
733 #define OSD_SCALE_H2 (OSD_SCALE_H/2)
734 #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
735 #define OSD_SCALE_M (OSD_SCALE_H2 - OSD_SCALE_TICK)
736 #define OSD_SCALE_I (OSD_SCALE_H2 + OSD_SCALE_TICK)
737 #define OSD_SCALE_FD (OSD_SCALE_FONT_SIZE/4)
738
739 static void
740 osd_render_scale(osm_gps_map_osd_t *osd)
741 {
742 osd_priv_t *priv = (osd_priv_t*)osd->priv;
743
744 /* this only needs to be rendered if the zoom has changed */
745 gint zoom;
746 g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
747 if(zoom == priv->scale_zoom)
748 return;
749
750 priv->scale_zoom = zoom;
751
752 float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
753
754 /* first fill with transparency */
755 cairo_t *cr = cairo_create(priv->scale);
756 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
757 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
758 // pink for testing: cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
759 cairo_paint(cr);
760 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
761
762 /* determine the size of the scale width in meters */
763 float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
764
765 /* scale this to useful values */
766 int exp = logf(width)*M_LOG10E;
767 int mant = width/pow(10,exp);
768 int width_metric = mant * pow(10,exp);
769 char *dist_str = NULL;
770 if(width_metric<1000)
771 dist_str = g_strdup_printf("%u m", width_metric);
772 else
773 dist_str = g_strdup_printf("%u km", width_metric/1000);
774 width_metric /= m_per_pix;
775
776 /* and now the hard part: scale for useful imperial values :-( */
777 /* try to convert to feet, 1ft == 0.3048 m */
778 width /= 0.3048;
779 float imp_scale = 0.3048;
780 char *dist_imp_unit = "ft";
781
782 if(width >= 100) {
783 /* 1yd == 3 feet */
784 width /= 3.0;
785 imp_scale *= 3.0;
786 dist_imp_unit = "yd";
787
788 if(width >= 1760.0) {
789 /* 1mi == 1760 yd */
790 width /= 1760.0;
791 imp_scale *= 1760.0;
792 dist_imp_unit = "mi";
793 }
794 }
795
796 /* also convert this to full tens/hundreds */
797 exp = logf(width)*M_LOG10E;
798 mant = width/pow(10,exp);
799 int width_imp = mant * pow(10,exp);
800 char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
801
802 /* convert back to pixels */
803 width_imp *= imp_scale;
804 width_imp /= m_per_pix;
805
806 cairo_select_font_face (cr, "Sans",
807 CAIRO_FONT_SLANT_NORMAL,
808 CAIRO_FONT_WEIGHT_BOLD);
809 cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
810 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
811
812 cairo_text_extents_t extents;
813 cairo_text_extents (cr, dist_str, &extents);
814
815 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
816 cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
817 cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
818 cairo_text_path (cr, dist_str);
819 cairo_stroke (cr);
820 cairo_move_to (cr, 2*OSD_SCALE_FD,
821 OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
822 cairo_text_path (cr, dist_str_imp);
823 cairo_stroke (cr);
824
825 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
826 cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
827 cairo_show_text (cr, dist_str);
828 cairo_move_to (cr, 2*OSD_SCALE_FD,
829 OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
830 cairo_show_text (cr, dist_str_imp);
831
832 g_free(dist_str);
833 g_free(dist_str_imp);
834
835 /* draw white line */
836 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
837 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
838 cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
839 cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
840 cairo_rel_line_to (cr, 0, OSD_SCALE_TICK);
841 cairo_rel_line_to (cr, width_metric, 0);
842 cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
843 cairo_stroke(cr);
844 cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
845 cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
846 cairo_rel_line_to (cr, width_imp, 0);
847 cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
848 cairo_stroke(cr);
849
850 /* draw black line */
851 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
852 cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
853 cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
854 cairo_rel_line_to (cr, 0, OSD_SCALE_TICK);
855 cairo_rel_line_to (cr, width_metric, 0);
856 cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
857 cairo_stroke(cr);
858 cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
859 cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
860 cairo_rel_line_to (cr, width_imp, 0);
861 cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
862 cairo_stroke(cr);
863
864 cairo_destroy(cr);
865 }
866
867 static void
868 osd_render(osm_gps_map_osd_t *osd)
869 {
870 osd_priv_t *priv = (osd_priv_t*)osd->priv;
871
872 #ifndef OSD_COLOR
873 GdkColor bg = GTK_WIDGET(osd->widget)->style->bg[GTK_STATE_NORMAL];
874 GdkColor fg = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_NORMAL];
875 GdkColor da = GTK_WIDGET(osd->widget)->style->fg[GTK_STATE_INSENSITIVE];
876 #endif
877
878 /* first fill with transparency */
879 cairo_t *cr = cairo_create(priv->overlay);
880 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
881 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
882 cairo_paint(cr);
883 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
884
885 /* --------- draw zoom and dpad shape shadow ----------- */
886 #ifdef OSD_SHADOW_ENABLE
887 osd_zoom_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
888 osd_shape_shadow(cr);
889 #ifndef OSD_NO_DPAD
890 osd_dpad_shape(cr, 1+OSD_SHADOW, 1+OSD_SHADOW);
891 osd_shape_shadow(cr);
892 #endif
893 #endif
894
895 /* --------- draw zoom and dpad shape ----------- */
896
897 osd_zoom_shape(cr, 1, 1);
898 #ifndef OSD_COLOR
899 osd_shape(cr, &bg, &fg);
900 #else
901 osd_shape(cr);
902 #endif
903 #ifndef OSD_NO_DPAD
904 osd_dpad_shape(cr, 1, 1);
905 #ifndef OSD_COLOR
906 osd_shape(cr, &bg, &fg);
907 #else
908 osd_shape(cr);
909 #endif
910 #endif
911
912 /* --------- draw zoom and dpad labels --------- */
913
914 #ifdef OSD_SHADOW_ENABLE
915 osd_labels_shadow(cr, Z_RAD/3, TRUE);
916 osd_zoom_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
917 #ifndef OSD_NO_DPAD
918 osd_dpad_labels(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
919 #endif
920 cairo_stroke(cr);
921 #ifdef OSD_GPS_BUTTON
922 osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
923 osd_dpad_gps(cr, 1+OSD_LBL_SHADOW, 1+OSD_LBL_SHADOW);
924 cairo_stroke(cr);
925 #endif
926 #endif
927
928 #ifndef OSD_COLOR
929 osd_labels(cr, Z_RAD/3, TRUE, &fg, &da);
930 #else
931 osd_labels(cr, Z_RAD/3, TRUE);
932 #endif
933 osd_zoom_labels(cr, 1, 1);
934 #ifndef OSD_NO_DPAD
935 osd_dpad_labels(cr, 1, 1);
936 #endif
937 cairo_stroke(cr);
938
939 #ifndef OSD_COLOR
940 osd_labels(cr, Z_RAD/6, osd->cb != NULL, &fg, &da);
941 #else
942 osd_labels(cr, Z_RAD/6, osd->cb != NULL);
943 #endif
944 #ifdef OSD_GPS_BUTTON
945 osd_dpad_gps(cr, 1, 1);
946 #endif
947 cairo_stroke(cr);
948
949 cairo_destroy(cr);
950
951 #ifdef OSD_SOURCE_SEL
952 osd_render_source_sel(osd);
953 #endif
954
955 #ifdef OSD_SCALE
956 osd_render_scale(osd);
957 #endif
958 }
959
960 static void
961 osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
962 {
963 osd_priv_t *priv = (osd_priv_t*)osd->priv;
964
965 /* OSD itself uses some off-screen rendering, so check if the */
966 /* offscreen buffer is present and create it if not */
967 if(!priv->overlay) {
968 /* create overlay ... */
969 priv->overlay =
970 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W+2, OSD_H+2);
971
972 #ifdef OSD_SOURCE_SEL
973 /* the initial OSD state is alway not-expanded */
974 priv->map_source =
975 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
976 OSD_S_W+2, OSD_S_H+2);
977 #endif
978
979 #ifdef OSD_SCALE
980 priv->scale =
981 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
982 OSD_SCALE_W, OSD_SCALE_H);
983 priv->scale_zoom = -1;
984 #endif
985
986 /* ... and render it */
987 osd_render(osd);
988 }
989
990 // now draw this onto the original context
991 cairo_t *cr = gdk_cairo_create(drawable);
992
993 int x = OSD_X, y = OSD_Y;
994 if(OSD_X < 0)
995 x = osd->widget->allocation.width - OSD_W + OSD_X;
996
997 if(OSD_Y < 0)
998 y = osd->widget->allocation.height - OSD_H + OSD_Y;
999
1000 cairo_set_source_surface(cr, priv->overlay, x, y);
1001 cairo_paint(cr);
1002
1003 #ifdef OSD_SOURCE_SEL
1004 if(!priv->handler_id) {
1005 /* the OSD source selection is not being animated */
1006 if(!priv->expanded)
1007 x = osd->widget->allocation.width - OSD_S_W;
1008 else
1009 x = osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X;
1010 } else
1011 x = priv->shift;
1012
1013 y = OSD_S_Y;
1014 if(OSD_S_Y < 0) {
1015 if(!priv->expanded)
1016 y = osd->widget->allocation.height - OSD_S_H + OSD_S_Y;
1017 else
1018 y = osd->widget->allocation.height - OSD_S_EXP_H + OSD_S_Y;
1019 }
1020
1021 cairo_set_source_surface(cr, priv->map_source, x, y);
1022 cairo_paint(cr);
1023 #endif
1024
1025 #ifdef OSD_SCALE
1026 x = OSD_X;
1027 y = -OSD_Y;
1028 if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1029 if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1030
1031 cairo_set_source_surface(cr, priv->scale, x, y);
1032 cairo_paint(cr);
1033 #endif
1034
1035 cairo_destroy(cr);
1036 }
1037
1038 static void
1039 osd_free(osm_gps_map_osd_t *osd)
1040 {
1041 osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1042
1043 if (priv->overlay)
1044 cairo_surface_destroy(priv->overlay);
1045
1046 #ifdef OSD_SOURCE_SEL
1047 if(priv->handler_id)
1048 gtk_timeout_remove(priv->handler_id);
1049
1050 if (priv->map_source)
1051 cairo_surface_destroy(priv->map_source);
1052 #endif
1053
1054 #ifdef OSD_SCALE
1055 if (priv->scale)
1056 cairo_surface_destroy(priv->scale);
1057 #endif
1058
1059 g_free(priv);
1060 }
1061
1062 static gboolean
1063 osd_busy(osm_gps_map_osd_t *osd)
1064 {
1065 #ifdef OSD_SOURCE_SEL
1066 osd_priv_t *priv = (osd_priv_t *)(osd->priv);
1067 return (priv->handler_id != 0);
1068 #else
1069 return FALSE;
1070 #endif
1071 }
1072
1073 static osm_gps_map_osd_t osd_classic = {
1074 .widget = NULL,
1075
1076 .draw = osd_draw,
1077 .check = osd_check,
1078 .render = osd_render,
1079 .free = osd_free,
1080 .busy = osd_busy,
1081
1082 .cb = NULL,
1083 .data = NULL,
1084
1085 .priv = NULL
1086 };
1087
1088 /* this is the only function that's externally visible */
1089 void
1090 osm_gps_map_osd_classic_init(OsmGpsMap *map)
1091 {
1092 osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
1093
1094 osd_classic.priv = priv;
1095
1096 osm_gps_map_register_osd(map, &osd_classic);
1097 }
1098
1099 #ifdef OSD_GPS_BUTTON
1100 /* below are osd specific functions which aren't used by osm-gps-map */
1101 /* but instead are to be used by the main application */
1102 void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb,
1103 gpointer data) {
1104 osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1105 g_return_if_fail (osd);
1106
1107 osd->cb = cb;
1108 osd->data = data;
1109
1110 /* this may have changed the state of the gps button */
1111 /* we thus re-render the overlay */
1112 osd->render(osd);
1113
1114 osm_gps_map_redraw(map);
1115 }
1116 #endif
1117
1118 osd_button_t
1119 osm_gps_map_osd_check(OsmGpsMap *map, gint x, gint y) {
1120 osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
1121 g_return_val_if_fail (osd, OSD_NONE);
1122
1123 return osd_check(osd, x, y);
1124 }