Fix:map_csv:Disable default notification of each deleted item.
[profile/ivi/navit.git] / navit / navit / osd.c
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         struct attr** osd_attrs;
37 };
38
39 struct osd *
40 osd_new(struct attr *parent, struct attr **attrs)
41 {
42         struct osd *o;
43         struct osd_priv *(*new)(struct navit *nav, struct osd_methods *meth, struct attr **attrs);
44         struct attr *type=attr_search(attrs, NULL, attr_type);
45
46         if (! type)
47                 return NULL;
48         new=plugin_get_osd_type(type->u.str);
49         if (! new)
50                 return NULL;
51         o=g_new0(struct osd, 1);
52         o->priv=new(parent->u.navit, &o->meth, attrs);
53
54                 o->osd_attrs = attr_list_dup(attrs);
55
56         return o;
57 }
58
59 int
60 osd_get_attr(struct osd *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
61 {
62         return attr_generic_get_attr(this_->osd_attrs, NULL, type, attr, NULL);
63 }
64
65 int
66 osd_set_attr(struct osd *osd, struct attr* attr)
67 {
68         osd->osd_attrs=attr_generic_set_attr(osd->osd_attrs,attr);
69         if(osd && osd->meth.set_attr) {
70                 osd->meth.set_attr(osd->priv, attr);
71                 return 1;
72         }
73         return 0;
74 }
75
76 void
77 osd_wrap_point(struct point *p, struct navit *nav)
78 {
79         if (p->x < 0)
80                 p->x += navit_get_width(nav);
81         if (p->y < 0)
82                 p->y += navit_get_height(nav);
83
84 }
85
86 static void
87 osd_evaluate_command(struct osd_item *this, struct navit *nav)
88 {
89         struct attr navit;
90         navit.type=attr_navit;
91         navit.u.navit=nav;
92         dbg(0, "calling command '%s'\n", this->command);
93         command_evaluate(&navit, this->command);
94 }
95
96 void
97 osd_std_click(struct osd_item *this, struct navit *nav, int pressed, int button, struct point *p)
98 {
99         struct point bp = this->p;
100         if (!this->command || !this->command[0])
101                 return;
102         osd_wrap_point(&bp, nav);
103         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)
104                 return;
105         if (button != 1)
106                 return;
107         if (!!pressed == !!this->pressed)
108                 return;
109         if (navit_ignore_button(nav))
110                 return;
111         this->pressed = pressed;
112         if (pressed && this->command) 
113                 osd_evaluate_command(this, nav);
114 }
115
116 void
117 osd_std_resize(struct osd_item *item)
118 {
119         graphics_overlay_resize(item->gr, &item->p, item->w, item->h, 65535, 1);
120 }
121  
122 static void
123 osd_std_calculate_sizes(struct osd_item *item, struct osd_priv *priv, int w, int h) 
124 {
125         struct attr vehicle_attr;
126
127         if (item->rel_w) {
128                 item->w = (item->rel_w * w) / 100;
129         }
130  
131         if (item->rel_h) {
132                 item->h = (item->rel_h * h) / 100;
133         }
134  
135         if (item->rel_x) {
136                 item->p.x = (item->rel_x * w) / 100;
137         }
138  
139         if (item->rel_y) {
140                 item->p.y = (item->rel_y * h) / 100;
141         }
142
143         osd_std_resize(item);
144         if (item->meth.draw) {
145                 if (navit_get_attr(item->navit, attr_vehicle, &vehicle_attr, NULL)) {
146                         item->meth.draw(priv, item->navit, vehicle_attr.u.vehicle);
147                 }
148         }
149 }
150
151 static void
152 osd_std_keypress(struct osd_item *item, struct navit *nav, char *key) 
153 {
154 #if 0
155         int i;
156         dbg(0,"key=%s\n",key);
157         for (i = 0 ; i < strlen(key) ; i++) {
158                 dbg(0,"key:0x%02x\n",key[i]);
159         }
160         for (i = 0 ; i < strlen(item->accesskey) ; i++) {
161                 dbg(0,"accesskey:0x%02x\n",item->accesskey[i]);
162         }
163 #endif
164         if ( ! graphics_is_disabled(item->gr) && item->accesskey && key && !strcmp(key, item->accesskey)) 
165                 osd_evaluate_command(item, nav);
166 }
167
168 static void
169 osd_std_reconfigure(struct osd_item *item, struct command_saved *cs)
170 {
171         if (!command_saved_error(cs)) {
172                 graphics_overlay_disable(item->gr, !command_saved_get_int(cs));
173         } else {
174                 dbg(0, "Error in saved command: %i\n", command_saved_error(cs));
175         }
176 }
177
178 void
179 osd_set_std_attr(struct attr **attrs, struct osd_item *item, int flags)
180 {
181         struct attr *attr;
182
183         item->flags=flags;
184         item->osd_configuration=-1;
185         item->color_white.r = 0xffff;
186         item->color_white.g = 0xffff;
187         item->color_white.b = 0xffff;
188         item->color_white.a = 0xffff;
189         item->text_color.r = 0xffff;
190         item->text_color.g = 0xffff;
191         item->text_color.b = 0xffff;
192         item->text_color.a = 0xffff;
193         if (flags & 1) {
194                 item->color_bg.r = 0x0808;
195                 item->color_bg.g = 0x0808;
196                 item->color_bg.b = 0xf8f8;
197                 item->color_bg.a = 0x0000;
198         } else {
199                 item->color_bg.r = 0x0;
200                 item->color_bg.g = 0x0;
201                 item->color_bg.b = 0x0;
202                 item->color_bg.a = 0x5fff;
203         }
204
205         attr=attr_search(attrs, NULL, attr_osd_configuration);
206         if (attr)
207                 item->osd_configuration = attr->u.num;
208
209         attr=attr_search(attrs, NULL, attr_enable_expression);
210         if (attr) {
211                 item->enable_cs = command_saved_new(attr->u.str, item->navit, NULL);
212         }
213
214         attr = attr_search(attrs, NULL, attr_w);
215         if (attr) {
216                 if (attr->u.num > ATTR_REL_MAXABS) {
217                         item->rel_w = attr->u.num - ATTR_REL_RELSHIFT;
218                 } else {
219                         item->rel_w = 0;
220                         item->w = attr->u.num;
221                 }
222         }
223
224         attr = attr_search(attrs, NULL, attr_h);
225         if (attr) {
226                 if (attr->u.num > ATTR_REL_MAXABS) {
227                         item->rel_h = attr->u.num - ATTR_REL_RELSHIFT;
228                 } else {
229                         item->rel_h = 0;
230                         item->h = attr->u.num;
231                 }
232         }
233
234         attr = attr_search(attrs, NULL, attr_x);
235         if (attr) {
236                 if (attr->u.num > ATTR_REL_MAXABS) {
237                         item->rel_x = attr->u.num - ATTR_REL_RELSHIFT;
238                 } else {
239                         item->rel_x = 0;
240                         item->p.x = attr->u.num;
241                 }
242         }
243
244         attr = attr_search(attrs, NULL, attr_y);
245         if (attr) {
246                 if (attr->u.num > ATTR_REL_MAXABS) {
247                         item->rel_y = attr->u.num - ATTR_REL_RELSHIFT;
248                 } else {
249                         item->rel_y = 0;
250                         item->p.y = attr->u.num;
251                 }
252         }
253
254         attr = attr_search(attrs, NULL, attr_font_size);
255         if (attr)
256                 item->font_size = attr->u.num;
257         
258         attr=attr_search(attrs, NULL, attr_background_color);
259         if (attr)
260                 item->color_bg=*attr->u.color;
261         attr = attr_search(attrs, NULL, attr_command);
262         if (attr) 
263                 item->command = g_strdup(attr->u.str);
264         attr=attr_search(attrs, NULL, attr_text_color);
265         if (attr)
266                 item->text_color=*attr->u.color;
267         attr=attr_search(attrs, NULL, attr_flags);
268         if (attr)
269                 item->attr_flags=attr->u.num;
270         attr=attr_search(attrs, NULL, attr_accesskey);
271         if (attr)
272                 item->accesskey = g_strdup(attr->u.str);
273         attr=attr_search(attrs, NULL, attr_font);
274         if (attr)
275                 item->font_name = g_strdup(attr->u.str);
276
277 }
278 void
279 osd_std_config(struct osd_item *item, struct navit *navit)
280 {
281         struct attr attr;
282         dbg(1,"enter\n");
283         if (item->enable_cs) {
284                 item->reconfig_cb = callback_new_1(callback_cast(osd_std_reconfigure), item);
285                 command_saved_set_cb(item->enable_cs, item->reconfig_cb);
286
287                 if (!command_saved_error(item->enable_cs)) {
288                         item->configured = !! command_saved_get_int(item->enable_cs);
289                 } else {
290                         dbg(0, "Error in saved command: %i.\n", command_saved_error(item->enable_cs));
291                 }
292         } else {
293                 if (!navit_get_attr(navit, attr_osd_configuration, &attr, NULL))
294                         attr.u.num=-1;
295                 item->configured = !!(attr.u.num & item->osd_configuration);
296         }
297         if (item->gr && !(item->flags & 16)) 
298                 graphics_overlay_disable(item->gr, !item->configured);
299 }
300
301 void
302 osd_set_std_config(struct navit *nav, struct osd_item *item)
303 {
304         item->cb = callback_new_attr_2(callback_cast(osd_std_config), attr_osd_configuration, item, nav);
305         navit_add_callback(nav, item->cb);
306         osd_std_config(item, nav);
307 }
308
309 void
310 osd_set_std_graphic(struct navit *nav, struct osd_item *item, struct osd_priv *priv)
311 {
312         struct graphics *navit_gr;
313
314         navit_gr = navit_get_graphics(nav);
315         item->gr = graphics_overlay_new(navit_gr, &item->p, item->w, item->h, 65535, 1);
316
317         item->graphic_bg = graphics_gc_new(item->gr);
318         graphics_gc_set_foreground(item->graphic_bg, &item->color_bg);
319         graphics_background_gc(item->gr, item->graphic_bg);
320
321         item->graphic_fg_white = graphics_gc_new(item->gr);
322         graphics_gc_set_foreground(item->graphic_fg_white, &item->color_white);
323
324         if (item->flags & 2) {
325                 item->font = graphics_named_font_new(item->gr, item->font_name, item->font_size, 1);
326                 item->graphic_fg_text = graphics_gc_new(item->gr);
327                 graphics_gc_set_foreground(item->graphic_fg_text, &item->text_color);
328         }
329
330         osd_set_std_config(nav, item);
331
332         item->resize_cb = callback_new_attr_2(callback_cast(osd_std_calculate_sizes), attr_resize, item, priv);
333         graphics_add_callback(navit_gr, item->resize_cb);
334         dbg(0,"accesskey %s\n",item->accesskey);
335         if (item->accesskey) {
336                 item->keypress_cb=callback_new_attr_2(callback_cast(osd_std_keypress), attr_keypress, item, nav);
337                 graphics_add_callback(navit_gr, item->keypress_cb);
338         }
339
340 }
341
342 void
343 osd_std_draw(struct osd_item *item)
344 {
345         struct point p[2];
346         int flags=item->attr_flags;
347
348         graphics_draw_mode(item->gr, draw_mode_begin);
349         p[0].x=0;
350         p[0].y=0;
351         graphics_draw_rectangle(item->gr, item->graphic_bg, p, item->w, item->h);
352         p[1].x=item->w-1;
353         p[1].y=0;
354         if (flags & 1) 
355                 graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
356         p[0].x=item->w-1;
357         p[0].y=item->h-1;
358         if (flags & 2) 
359                 graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
360         p[1].x=0;
361         p[1].y=item->h-1;
362         if (flags & 4) 
363                 graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
364         p[0].x=0;
365         p[0].y=0;
366         if (flags & 8) 
367                 graphics_draw_lines(item->gr, item->graphic_fg_text, p, 2);
368 }