1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
#include <glib.h>
|
21 |
#include "debug.h"
|
22 |
#include "plugin.h"
|
23 |
#include "item.h"
|
24 |
#include "color.h"
|
25 |
#include "point.h"
|
26 |
#include "navit.h"
|
27 |
#include "graphics.h"
|
28 |
#include "command.h"
|
29 |
#include "callback.h"
|
30 |
#include "osd.h"
|
31 |
|
32 |
|
33 |
struct osd {
|
34 |
struct osd_methods meth;
|
35 |
struct osd_priv *priv;
|
36 |
};
|
37 |
|
38 |
static GHashTable *osd_hash = NULL;
|
39 |
|
40 |
struct osd *
|
41 |
osd_new(struct attr *parent, struct attr **attrs)
|
42 |
{
|
43 |
struct attr *attr;
|
44 |
struct osd *o;
|
45 |
struct osd_priv *(*new)(struct navit *nav, struct osd_methods *meth, struct attr **attrs);
|
46 |
struct attr *type=attr_search(attrs, NULL, attr_type);
|
47 |
|
48 |
if (! type)
|
49 |
return NULL;
|
50 |
new=plugin_get_osd_type(type->u.str);
|
51 |
if (! new)
|
52 |
return NULL;
|
53 |
o=g_new0(struct osd, 1);
|
54 |
o->priv=new(parent->u.navit, &o->meth, attrs);
|
55 |
|
56 |
attr=attr_search(attrs, NULL, attr_name);
|
57 |
if (attr && attr->u.str) {
|
58 |
if(NULL == osd_hash) {
|
59 |
osd_hash = g_hash_table_new_full(g_str_hash,g_str_equal,g_free,NULL);
|
60 |
}
|
61 |
g_hash_table_insert(osd_hash, g_strdup(attr->u.str), o);
|
62 |
}
|
63 |
|
64 |
|
65 |
return o;
|
66 |
}
|
67 |
|
68 |
struct osd*
|
69 |
osd_get_osd_by_name(char *name)
|
70 |
{
|
71 |
return g_hash_table_lookup(osd_hash, name);
|
72 |
}
|
73 |
|
74 |
int
|
75 |
osd_set_attr(struct osd *osd, struct attr* attr)
|
76 |
{
|
77 |
if(osd && osd->meth.set_attr) {
|
78 |
osd->meth.set_attr(osd->priv, attr);
|
79 |
}
|
80 |
return 0;
|
81 |
}
|
82 |
|
83 |
void
|
84 |
osd_wrap_point(struct point *p, struct navit *nav)
|
85 |
{
|
86 |
if (p->x < 0)
|
87 |
p->x += navit_get_width(nav);
|
88 |
if (p->y < 0)
|
89 |
p->y += navit_get_height(nav);
|
90 |
|
91 |
}
|
92 |
|
93 |
static void
|
94 |
osd_evaluate_command(struct osd_item *this, struct navit *nav)
|
95 |
{
|
96 |
struct attr navit;
|
97 |
navit.type=attr_navit;
|
98 |
navit.u.navit=nav;
|
99 |
dbg(0, "calling command '%s'\n", this->command);
|
100 |
command_evaluate(&navit, this->command);
|
101 |
}
|
102 |
|
103 |
void
|
104 |
osd_std_click(struct osd_item *this, struct navit *nav, int pressed, int button, struct point *p)
|
105 |
{
|
106 |
struct point bp = this->p;
|
107 |
if (!this->command || !this->command[0])
|
108 |
return;
|
109 |
osd_wrap_point(&bp, nav);
|
110 |
if ((p->x < bp.x || p->y < bp.y || p->x > bp.x + this->w || p->y > bp.y + this->h || !this->configured) && !this->pressed)
|
111 |
return;
|
112 |
if (button != 1)
|
113 |
return;
|
114 |
if (!!pressed == !!this->pressed)
|
115 |
return;
|
116 |
if (navit_ignore_button(nav))
|
117 |
return;
|
118 |
this->pressed = pressed;
|
119 |
if (pressed && this->command)
|
120 |
osd_evaluate_command(this, nav);
|
121 |
}
|
122 |
|
123 |
void
|
124 |
osd_std_resize(struct osd_item *item)
|
125 |
{
|
126 |
graphics_overlay_resize(item->gr, &item->p, item->w, item->h, 65535, 1);
|
127 |
}
|
128 |
|
129 |
static void
|
130 |
osd_std_calculate_sizes(struct osd_item *item, struct osd_priv *priv, int w, int h)
|
131 |
{
|
132 |
struct attr vehicle_attr;
|
133 |
|
134 |
if (item->rel_w) {
|
135 |
item->w = (item->rel_w * w) / 100;
|
136 |
}
|
137 |
|
138 |
if (item->rel_h) {
|
139 |
item->h = (item->rel_h * h) / 100;
|
140 |
}
|
141 |
|
142 |
if (item->rel_x) {
|
143 |
item->p.x = (item->rel_x * w) / 100;
|
144 |
}
|
145 |
|
146 |
if (item->rel_y) {
|
147 |
item->p.y = (item->rel_y * h) / 100;
|
148 |
}
|
149 |
|
150 |
osd_std_resize(item);
|
151 |
if (item->meth.draw) {
|
152 |
if (navit_get_attr(item->navit, attr_vehicle, &vehicle_attr, NULL)) {
|
153 |
item->meth.draw(priv, item->navit, vehicle_attr.u.vehicle);
|
154 |
}
|
155 |
}
|
156 |
}
|
157 |
|
158 |
static void
|
159 |
osd_std_keypress(struct osd_item *item, struct navit *nav, char *key)
|
160 |
{
|
161 |
#if 0
|
162 |
int i;
|
163 |
dbg(0,"key=%s\n",key);
|
164 |
for (i = 0 ; i < strlen(key) ; i++) {
|
165 |
dbg(0,"key:0x%02x\n",key[i]);
|
166 |
}
|
167 |
for (i = 0 ; i < strlen(item->accesskey) ; i++) {
|
168 |
dbg(0,"accesskey:0x%02x\n",item->accesskey[i]);
|
169 |
}
|
170 |
#endif
|
171 |
if (item->accesskey && key && !strcmp(key, item->accesskey))
|
172 |
osd_evaluate_command(item, nav);
|
173 |
}
|
174 |
|
175 |
static void
|
176 |
osd_std_reconfigure(struct osd_item *item, struct command_saved *cs)
|
177 |
{
|
178 |
if (!command_saved_error(cs)) {
|
179 |
graphics_overlay_disable(item->gr, !command_saved_get_int(cs));
|
180 |
} else {
|
181 |
dbg(0, "Error in saved command: %i\n", command_saved_error(cs));
|
182 |
}
|
183 |
}
|
184 |
|
185 |
void
|
186 |
osd_set_std_attr(struct attr **attrs, struct osd_item *item, int flags)
|
187 |
{
|
188 |
struct attr *attr;
|
189 |
|
190 |
item->flags=flags;
|
191 |
item->osd_configuration=-1;
|
192 |
item->color_white.r = 0xffff;
|
193 |
item->color_white.g = 0xffff;
|
194 |
item->color_white.b = 0xffff;
|
195 |
item->color_white.a = 0xffff;
|
196 |
item->text_color.r = 0xffff;
|
197 |
item->text_color.g = 0xffff;
|
198 |
item->text_color.b = 0xffff;
|
199 |
item->text_color.a = 0xffff;
|
200 |
if (flags & 1) {
|
201 |
item->color_bg.r = 0x0808;
|
202 |
item->color_bg.g = 0x0808;
|
203 |
item->color_bg.b = 0xf8f8;
|
204 |
item->color_bg.a = 0x0000;
|
205 |
} else {
|
206 |
item->color_bg.r = 0x0;
|
207 |
item->color_bg.g = 0x0;
|
208 |
item->color_bg.b = 0x0;
|
209 |
item->color_bg.a = 0x5fff;
|
210 |
}
|
211 |
|
212 |
attr=attr_search(attrs, NULL, attr_osd_configuration);
|
213 |
if (attr)
|
214 |
item->osd_configuration = attr->u.num;
|
215 |
|
216 |
attr=attr_search(attrs, NULL, attr_enable_expression);
|
217 |
if (attr) {
|
218 |
item->enable_cs = command_saved_new(attr->u.str, item->navit, NULL);
|
219 |
}
|
220 |
|
221 |
attr = attr_search(attrs, NULL, attr_w);
|
222 |
if (attr) {
|
223 |
if (attr->u.num > ATTR_REL_MAXABS) {
|
224 |
item->rel_w = attr->u.num - ATTR_REL_RELSHIFT;
|
225 |
} else {
|
226 |
item->rel_w = 0;
|
227 |
item->w = attr->u.num;
|
228 |
}
|
229 |
}
|
230 |
|
231 |
attr = attr_search(attrs, NULL, attr_h);
|
232 |
if (attr) {
|
233 |
if (attr->u.num > ATTR_REL_MAXABS) {
|
234 |
item->rel_h = attr->u.num - ATTR_REL_RELSHIFT;
|
235 |
} else {
|
236 |
item->rel_h = 0;
|
237 |
item->h = attr->u.num;
|
238 |
}
|
239 |
}
|
240 |
|
241 |
attr = attr_search(attrs, NULL, attr_x);
|
242 |
if (attr) {
|
243 |
if (attr->u.num > ATTR_REL_MAXABS) {
|
244 |
item->rel_x = attr->u.num - ATTR_REL_RELSHIFT;
|
245 |
} else {
|
246 |
item->rel_x = 0;
|
247 |
item->p.x = attr->u.num;
|
248 |
}
|
249 |
}
|
250 |
|
251 |
attr = attr_search(attrs, NULL, attr_y);
|
252 |
if (attr) {
|
253 |
if (attr->u.num > ATTR_REL_MAXABS) {
|
254 |
item->rel_y = attr->u.num - ATTR_REL_RELSHIFT;
|
255 |
} else {
|
256 |
item->rel_y = 0;
|
257 |
item->p.y = attr->u.num;
|
258 |
}
|
259 |
}
|
260 |
|
261 |
attr = attr_search(attrs, NULL, attr_font_size);
|
262 |
if (attr)
|
263 |
item->font_size = attr->u.num;
|
264 |
|
265 |
attr=attr_search(attrs, NULL, attr_background_color);
|
266 |
if (attr)
|
267 |
item->color_bg=*attr->u.color;
|
268 |
attr = attr_search(attrs, NULL, attr_command);
|
269 |
if (attr)
|
270 |
item->command = g_strdup(attr->u.str);
|
271 |
attr=attr_search(attrs, NULL, attr_text_color);
|
272 |
if (attr)
|
273 |
item->text_color=*attr->u.color;
|
274 |
attr=attr_search(attrs, NULL, attr_flags);
|
275 |
if (attr)
|
276 |
item->attr_flags=attr->u.num;
|
277 |
attr=attr_search(attrs, NULL, attr_accesskey);
|
278 |
if (attr)
|
279 |
item->accesskey = g_strdup(attr->u.str);
|
280 |
attr=attr_search(attrs, NULL, attr_font);
|
281 |
if (attr)
|
282 |
item->font_name = g_strdup(attr->u.str);
|
283 |
|
284 |
}
|
285 |
void
|
286 |
osd_std_config(struct osd_item *item, struct navit *navit)
|
287 |
{
|
288 |
struct attr attr;
|
289 |
dbg(1,"enter\n");
|
290 |
if (item->enable_cs) {
|
291 |
item->reconfig_cb = callback_new_1(callback_cast(osd_std_reconfigure), item);
|
292 |
command_saved_set_cb(item->enable_cs, item->reconfig_cb);
|
293 |
|
294 |
if (!command_saved_error(item->enable_cs)) {
|
295 |
item->configured = !! command_saved_get_int(item->enable_cs);
|
296 |
} else {
|
297 |
dbg(0, "Error in saved command: %i.\n", command_saved_error(item->enable_cs));
|
298 |
}
|
299 |
} else {
|
300 |
if (!navit_get_attr(navit, attr_osd_configuration, &attr, NULL))
|
301 |
attr.u.num=-1;
|
302 |
item->configured = !!(attr.u.num & item->osd_configuration);
|
303 |
}
|
304 |
if (item->gr && !(item->flags & 16))
|
305 |
graphics_overlay_disable(item->gr, !item->configured);
|
306 |
}
|
307 |
|
308 |
void
|
309 |
osd_set_std_config(struct navit *nav, struct osd_item *item)
|
310 |
{
|
311 |
item->cb = callback_new_attr_2(callback_cast(osd_std_config), attr_osd_configuration, item, nav);
|
312 |
navit_add_callback(nav, item->cb);
|
313 |
osd_std_config(item, nav);
|
314 |
}
|
315 |
|
316 |
void
|
317 |
osd_set_std_graphic(struct navit *nav, struct osd_item *item, struct osd_priv *priv)
|
318 |
{
|
319 |
struct graphics *navit_gr;
|
320 |
|
321 |
navit_gr = navit_get_graphics(nav);
|
322 |
item->gr = graphics_overlay_new(navit_gr, &item->p, item->w, item->h, 65535, 1);
|
323 |
|
324 |
item->graphic_bg = graphics_gc_new(item->gr);
|
325 |
graphics_gc_set_foreground(item->graphic_bg, &item->color_bg);
|
326 |
graphics_background_gc(item->gr, item->graphic_bg);
|
327 |
|
328 |
item->graphic_fg_white = graphics_gc_new(item->gr);
|
329 |
graphics_gc_set_foreground(item->graphic_fg_white, &item->color_white);
|
330 |
|
331 |
if (item->flags & 2) {
|
332 |
item->font = graphics_named_font_new(item->gr, item->font_name, item->font_size, 1);
|
333 |
item->graphic_fg_text = graphics_gc_new(item->gr);
|
334 |
graphics_gc_set_foreground(item->graphic_fg_text, &item->text_color);
|
335 |
}
|
336 |
|
337 |
osd_set_std_config(nav, item);
|
338 |
|
339 |
item->resize_cb = callback_new_attr_2(callback_cast(osd_std_calculate_sizes), attr_resize, item, priv);
|
340 |
graphics_add_callback(navit_gr, item->resize_cb);
|
341 |
dbg(0,"accesskey %s\n",item->accesskey);
|
342 |
if (item->accesskey) {
|
343 |
item->keypress_cb=callback_new_attr_2(callback_cast(osd_std_keypress), attr_keypress, item, nav);
|
344 |
graphics_add_callback(navit_gr, item->keypress_cb);
|
345 |
}
|
346 |
|
347 |
}
|
348 |
|
349 |
void
|
350 |
osd_std_draw(struct osd_item *item)
|
351 |
{
|
352 |
struct point p[2];
|
353 |
int flags=item->attr_flags;
|
354 |
|
355 |
graphics_draw_mode(item->gr, draw_mode_begin);
|
356 |
p[0].x=0;
|
357 |
p[0].y=0;
|
358 |
graphics_draw_rectangle(item->gr, item->graphic_bg, p, item->w, item->h);
|
359 |
p[1].x=item->w-1;
|
360 |
p[1].y=0;
|
361 |
if (flags & 1)
|
362 |
graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
|
363 |
p[0].x=item->w-1;
|
364 |
p[0].y=item->h-1;
|
365 |
if (flags & 2)
|
366 |
graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
|
367 |
p[1].x=0;
|
368 |
p[1].y=item->h-1;
|
369 |
if (flags & 4)
|
370 |
graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
|
371 |
p[0].x=0;
|
372 |
p[0].y=0;
|
373 |
if (flags & 8)
|
374 |
graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
|
375 |
}
|