0b26aadbc14ebffbcc3c94474ba8ec0919c4a0c2
[navit-package] / navit / gui / internal / gui_internal.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 //##############################################################################################################
21 //#
22 //# File: gui_internal.c
23 //# Description: New "internal" GUI for use with any graphics library
24 //# Comment: Trying to make a touchscreen friendly GUI
25 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
26 //#
27 //##############################################################################################################
28
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <glib.h>
35 #include <time.h>
36 #ifdef HAVE_API_WIN32_BASE
37 #include <windows.h>
38 #endif
39 #include "config.h"
40 #include "item.h"
41 #include "file.h"
42 #include "navit.h"
43 #include "navit_nls.h"
44 #include "debug.h"
45 #include "gui.h"
46 #include "coord.h"
47 #include "point.h"
48 #include "plugin.h"
49 #include "graphics.h"
50 #include "transform.h"
51 #include "color.h"
52 #include "map.h"
53 #include "layout.h"
54 #include "callback.h"
55 #include "vehicle.h"
56 #include "vehicleprofile.h"
57 #include "window.h"
58 #include "config_.h"
59 #include "keys.h"
60 #include "mapset.h"
61 #include "route.h"
62 #include "search.h"
63 #include "track.h"
64 #include "country.h"
65 #include "config.h"
66 #include "event.h"
67 #include "navit_nls.h"
68 #include "navigation.h"
69 #include "gui_internal.h"
70 #include "command.h"
71 #include "xmlconfig.h"
72 #include "util.h"
73
74 struct menu_data {
75         struct widget *search_list;
76         struct widget *keyboard;
77         struct widget *button_bar;
78         int keyboard_mode;
79         void (*redisplay)(struct gui_priv *priv, struct widget *widget, void *data);
80         struct widget *redisplay_widget;
81 };
82
83 //##############################################################################################################
84 //# Description:
85 //# Comment:
86 //# Authors: Martin Schaller (04/2008)
87 //##############################################################################################################
88 struct widget {
89         enum widget_type type;
90         struct graphics_gc *background,*text_background;
91         struct graphics_gc *foreground_frame;
92         struct graphics_gc *foreground;
93         char *text;
94         struct graphics_image *img;
95          /**
96           * A function to be invoked on actions.
97           * @li widget The widget that is receiving the button press.
98           *
99           */
100         void (*func)(struct gui_priv *priv, struct widget *widget, void *data);
101         int reason;
102         int datai;
103         void *data;
104         /**
105          * @brief A function to deallocate data
106          */
107         void (*data_free)(void *data);
108
109         /**
110          * @brief a function that will be called as the widget is being destroyed.
111          * This function can act as a destructor for the widget. It allows for
112          * on deallocation actions to be specified on a per widget basis.
113          * This function will call g_free on the widget (if required).
114          */
115         void (*free) (struct gui_priv *this_, struct widget * w);
116         char *prefix;
117         char *name;
118         char *speech;
119         char *command;
120         struct pcoord c;
121         struct item item;
122         int selection_id;
123         int state;
124         struct point p;
125         int wmin,hmin;
126         int w,h;
127         int textw,texth;
128         int bl,br,bt,bb,spx,spy;
129         int border;
130         /**
131          * The number of widgets to layout horizontally when doing
132          * a orientation_horizontal_vertical layout
133          */
134         int cols;
135         enum flags flags;
136         void *instance;
137         int (*set_attr)(void *, struct attr *);
138         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
139         void (*remove_cb)(void *, struct callback *cb);
140         struct callback *cb;
141         struct attr on;
142         struct attr off;
143         int deflt;
144         int is_on;
145         int redraw;
146         struct menu_data *menu_data;
147         GList *children;
148 };
149
150 /**
151  * @brief A structure to store configuration values.
152  *
153  * This structure stores configuration values for how gui elements in the internal GUI
154  * should be drawn.
155  */
156 struct gui_config_settings {
157
158   /**
159    * The base size (in fractions of a point) to use for text.
160    */
161   int font_size;
162   /**
163    * The size (in pixels) that xs style icons should be scaled to.
164    */
165   int icon_xs;
166   /**
167    * The size (in pixels) that s style icons (small) should be scaled to
168    */
169   int icon_s;
170   /**
171    * The size (in pixels) that l style icons should be scaled to
172    */
173   int icon_l;
174   /**
175    * The default amount of spacing (in pixels) to place between GUI elements.
176    */
177   int spacing;
178
179 };
180
181 /**
182  * Indexes into the config_profiles array.
183  */
184 const int LARGE_PROFILE=0;
185 const int MEDIUM_PROFILE=1;
186 const int SMALL_PROFILE=2;
187
188 /**
189  * The default config profiles.
190  *
191  * [0] =>  LARGE_PROFILE (screens 640 in one dimension)
192  * [1] =>  MEDIUM PROFILE (screens larger than 320 in one dimension
193  * [2] => Small profile (default)
194  */
195 static struct gui_config_settings config_profiles[]={
196       {545,32,48,96,10}
197     , {300,32,48,64,3}
198       ,{200,16,32,48,2}
199 };
200
201 struct route_data {
202   struct widget * route_table;
203   int route_showing;
204
205 };
206
207 //##############################################################################################################
208 //# Description:
209 //# Comment:
210 //# Authors: Martin Schaller (04/2008)
211 //##############################################################################################################
212 struct gui_priv {
213         struct navit *nav;
214         struct window *win;
215         struct graphics *gra;
216         struct graphics_gc *background;
217         struct graphics_gc *background2;
218         struct graphics_gc *highlight_background;
219         struct graphics_gc *foreground;
220         struct graphics_gc *text_foreground;
221         struct graphics_gc *text_background;
222         struct color background_color, background2_color, text_foreground_color, text_background_color;
223         int spacing;
224         int font_size;
225         int fullscreen;
226         struct graphics_font *font;
227         int icon_xs, icon_s, icon_l;
228         int pressed;
229         struct widget *widgets;
230         int widgets_count;
231         int redraw;
232         struct widget root;
233         struct widget *highlighted;
234         struct widget *highlighted_menu;
235         int clickp_valid, vehicle_valid;
236         struct pcoord clickp, vehiclep;
237         struct search_list *sl;
238         int ignore_button;
239         int menu_on_map_click;
240         char *country_iso2;
241         int speech;
242         int keyboard;
243         /**
244          * The setting information read from the configuration file.
245          * values of -1 indicate no value was specified in the config file.
246          */
247         struct gui_config_settings config;
248         struct event_idle *idle;
249         struct callback *motion_cb,*button_cb,*resize_cb,*keypress_cb,*idle_cb, *motion_timeout_callback;
250         struct event_timeout *motion_timeout_event;
251         struct point current;
252
253         struct callback * vehicle_cb;
254           /**
255            * Stores information about the route.
256            */
257         struct route_data route_data;
258
259         struct gui_internal_data data;
260         struct callback_list *cbl;
261         int flags;
262         int cols;
263         struct attr osd_configuration;
264         int pitch;
265 /* html */
266         char *html_tag,*html_command;
267         struct widget *html_container, *html_curr;
268 };
269
270
271 /**
272  * @brief A structure to store information about a table.
273  *
274  * The table_data widget stores pointers to extra information needed by the
275  * table widget.
276  *
277  * The table_data structure needs to be freed with data_free along with the widget.
278  *
279  */
280 struct table_data
281 {
282   /**
283    * A GList pointer into a widget->children list that indicates the row
284    * currently being rendered at the top of the table.
285    */
286   GList * top_row;
287   /**
288    * A Glist pointer into a widget->children list that indicates the row
289    * currently being rendered at the bottom of the table.
290    */
291   GList * bottom_row;
292
293   /**
294    * A list of table_row widgets that mark the
295    * top rows for each page of the table.
296    * This is needed for the 'previous page' function of the table.
297    */
298   GList * page_headers;
299
300   /**
301    * A container box that is the child of the table widget that contains+groups
302    * the next and previous button.
303    */
304   struct widget * button_box;
305
306   /**
307    * A button widget to handle 'next page' requests
308    */
309   struct widget * next_button;
310   /**
311    * A button widget to handle 'previous page' requests.
312    */
313   struct widget * prev_button;
314
315
316   /**
317    * a pointer to the gui context.
318    * This is needed by the free function to destory the buttons.
319    */
320   struct  gui_priv *  this;
321 };
322
323 /**
324  * A data structure that holds information about a column that makes up a table.
325  *
326  *
327  */
328 struct table_column_desc
329 {
330
331   /**
332    * The computed height of a cell in the table.
333    */
334   int height;
335
336   /**
337    * The computed width of a cell in the table.
338    */
339   int width;
340 };
341
342
343 static void gui_internal_widget_render(struct gui_priv *this, struct widget *w);
344 static void gui_internal_widget_pack(struct gui_priv *this, struct widget *w);
345 static struct widget * gui_internal_box_new(struct gui_priv *this, enum flags flags);
346 static void gui_internal_widget_append(struct widget *parent, struct widget *child);
347 static void gui_internal_widget_destroy(struct gui_priv *this, struct widget *w);
348 static void gui_internal_apply_config(struct gui_priv *this);
349
350 static struct widget* gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons);
351 static struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags);
352 static void gui_internal_table_render(struct gui_priv * this, struct widget * w);
353 static void gui_internal_cmd_tools(struct gui_priv * this, struct widget * w,void *);
354 static void gui_internal_cmd_route(struct gui_priv * this, struct widget * w,void *);
355 static void gui_internal_table_pack(struct gui_priv * this, struct widget * w);
356 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data);
357 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data);
358 static void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table);
359 static struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags);
360 static void gui_internal_table_data_free(void * d);
361 static void gui_internal_route_update(struct gui_priv * this, struct navit * navit,
362                                       struct vehicle * v);
363 static void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w);
364 static void gui_internal_populate_route_table(struct gui_priv * this,
365                                        struct navit * navit);
366 static void gui_internal_search_idle_end(struct gui_priv *this);
367 static void gui_internal_search(struct gui_priv *this, char *what, char *type, int flags);
368 static void gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data);
369 static void gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data);
370 static void gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data);
371 static void gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data);
372 static void gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data);
373 static void gui_internal_search_town_in_country(struct gui_priv *this, struct widget *wm);
374 static void gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data);
375 static void gui_internal_check_exit(struct gui_priv *this);
376 static void gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data);
377
378 static struct widget *gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode);
379 static struct menu_data * gui_internal_menu_data(struct gui_priv *this);
380
381 static int gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle *vehicle);
382
383 /*
384  * * Display image scaled to specific size
385  * * searches for scaleable and pre-scaled image
386  * * @param this Our gui context
387  * * @param name image name
388  * * @param w desired width of image
389  * * @param h desired height of image
390  * * @returns image_struct Ptr to scaled image struct or NULL if not scaled or found
391  * */
392 static struct graphics_image *
393 image_new_scaled(struct gui_priv *this, const char *name, int w, int h)
394 {
395         struct graphics_image *ret=NULL;
396         char *full_name=NULL;
397         char *full_path=NULL;
398         int i;
399
400         for (i = 1 ; i < 6 ; i++) {
401                 full_name=NULL;
402                 switch (i) {
403                 case 3:
404                         full_name=g_strdup_printf("%s.svg", name);
405                         break;
406                 case 2:
407                         full_name=g_strdup_printf("%s.svgz", name);
408                         break;
409                 case 1:
410                         if (w != -1 && h != -1) {
411                                 full_name=g_strdup_printf("%s_%d_%d.png", name, w, h);
412                         }
413                         break;
414                 case 4:
415                         full_name=g_strdup_printf("%s.png", name);
416                         break;
417                 case 5:
418                         full_name=g_strdup_printf("%s.xpm", name);
419                         break;
420                 }
421                 dbg(1,"trying '%s'\n", full_name);
422                 if (! full_name)
423                         continue;
424 #if 0
425                 /* needs to be checked in the driver */
426                 if (!file_exists(full_name)) {
427                         g_free(full_name);
428                         continue;
429                 }
430 #endif
431                 full_path=graphics_icon_path(full_name);
432                 ret=graphics_image_new_scaled(this->gra, full_path, w, h);
433                 dbg(1,"ret=%p\n", ret);
434                 g_free(full_path);
435                 g_free(full_name);
436                 if (ret)
437                         return ret;
438         }
439         dbg(0,"failed to load %s with %d,%d\n", name, w, h);
440         return NULL;
441 }
442
443 #if 0
444 static struct graphics_image *
445 image_new_o(struct gui_priv *this, char *name)
446 {
447         return image_new_scaled(this, name, -1, -1);
448 }
449 #endif
450
451 static struct graphics_image *
452 image_new_xs(struct gui_priv *this, const char *name)
453 {
454         return image_new_scaled(this, name, this->icon_xs, this->icon_xs);
455 }
456
457
458 static struct graphics_image *
459 image_new_s(struct gui_priv *this, const char *name)
460 {
461         return image_new_scaled(this, name, this->icon_s, this->icon_s);
462 }
463
464 static struct graphics_image *
465 image_new_l(struct gui_priv *this, const char *name)
466 {
467         return image_new_scaled(this, name, this->icon_l, this->icon_l);
468 }
469
470 static char *
471 coordinates(struct pcoord *pc, char sep)
472 {
473         char latc='N',lngc='E';
474         int lat_deg,lat_min,lat_sec;
475         int lng_deg,lng_min,lng_sec;
476         struct coord_geo g;
477         struct coord c;
478         c.x=pc->x;
479         c.y=pc->y;
480         transform_to_geo(pc->pro, &c, &g);
481
482         if (g.lat < 0) {
483                 g.lat=-g.lat;
484                 latc='S';
485         }
486         if (g.lng < 0) {
487                 g.lng=-g.lng;
488                 lngc='W';
489         }
490         lat_deg=g.lat;
491         lat_min=fmod(g.lat*60,60);
492         lat_sec=fmod(g.lat*3600,60);
493         lng_deg=g.lng;
494         lng_min=fmod(g.lng*60,60);
495         lng_sec=fmod(g.lng*3600,60);
496         return g_strdup_printf("%d°%d'%d\" %c%c%d°%d'%d\" %c",lat_deg,lat_min,lat_sec,latc,sep,lng_deg,lng_min,lng_sec,lngc);
497 }
498
499 static void
500 gui_internal_background_render(struct gui_priv *this, struct widget *w)
501 {
502         struct point pnt=w->p;
503         if (w->state & STATE_HIGHLIGHTED)
504                 graphics_draw_rectangle(this->gra, this->highlight_background, &pnt, w->w, w->h);
505         else {
506                 if (w->background)
507                         graphics_draw_rectangle(this->gra, w->background, &pnt, w->w, w->h);
508         }
509 }
510 static struct widget *
511 gui_internal_label_new(struct gui_priv *this, char *text)
512 {
513         struct point p[4];
514         int w=0;
515         int h=0;
516
517         struct widget *widget=g_new0(struct widget, 1);
518         widget->type=widget_label;
519         if (text) {
520                 widget->text=g_strdup(text);
521                 graphics_get_text_bbox(this->gra, this->font, text, 0x10000, 0x0, p, 0);
522                 w=p[2].x-p[0].x;
523                 h=p[0].y-p[2].y;
524         }
525         widget->h=h+this->spacing;
526         widget->texth=h;
527         widget->w=w+this->spacing;
528         widget->textw=w;
529         widget->flags=gravity_center;
530         widget->foreground=this->text_foreground;
531         widget->text_background=this->text_background;
532
533         return widget;
534 }
535
536 static struct widget *
537 gui_internal_label_new_abbrev(struct gui_priv *this, char *text, int maxwidth)
538 {
539         struct widget *ret=NULL;
540         char *tmp=g_malloc(strlen(text)+3);
541         int i;
542
543         i=strlen(text)-1;
544         while (i >= 0) {
545                 strcpy(tmp, text);
546                 strcpy(tmp+i,"..");
547                 ret=gui_internal_label_new(this, tmp);
548                 if (ret->w < maxwidth)
549                         break;
550                 gui_internal_widget_destroy(this, ret);
551                 ret=NULL;
552                 i--;
553         }
554         g_free(tmp);
555         return ret;
556 }
557
558 static struct widget *
559 gui_internal_image_new(struct gui_priv *this, struct graphics_image *image)
560 {
561         struct widget *widget=g_new0(struct widget, 1);
562         widget->type=widget_image;
563         widget->img=image;
564         if (image) {
565                 widget->w=image->width;
566                 widget->h=image->height;
567         }
568         return widget;
569 }
570
571 static void
572 gui_internal_image_render(struct gui_priv *this, struct widget *w)
573 {
574         struct point pnt;
575
576         gui_internal_background_render(this, w);
577         if (w->img) {
578                 pnt=w->p;
579                 pnt.x+=w->w/2-w->img->hot.x;
580                 pnt.y+=w->h/2-w->img->hot.y;
581                 graphics_draw_image(this->gra, this->foreground, &pnt, w->img);
582         }
583 }
584
585 static void
586 gui_internal_label_render(struct gui_priv *this, struct widget *w)
587 {
588         struct point pnt=w->p;
589         gui_internal_background_render(this, w);
590         if (w->text) {
591                 if (w->flags & gravity_right) {
592                         pnt.y+=w->h-this->spacing;
593                         pnt.x+=w->w-w->textw-this->spacing;
594                         graphics_draw_text(this->gra, w->foreground, w->text_background, this->font, w->text, &pnt, 0x10000, 0x0);
595                 } else {
596                         pnt.y+=w->h-this->spacing;
597                         graphics_draw_text(this->gra, w->foreground, w->text_background, this->font, w->text, &pnt, 0x10000, 0x0);
598                 }
599         }
600 }
601
602 /**
603  * @brief A text box is a widget that renders a text string containing newlines.
604  * The string will be broken up into label widgets at each newline with a vertical layout.
605  *
606  */
607 static struct widget *
608 gui_internal_text_new(struct gui_priv *this, char *text, enum flags flags)
609 {
610         char *s=g_strdup(text),*s2,*tok;
611         struct widget *ret=gui_internal_box_new(this, flags);
612         s2=s;
613         while ((tok=strtok(s2,"\n"))) {
614                 gui_internal_widget_append(ret, gui_internal_label_new(this, tok));
615                 s2=NULL;
616         }
617         gui_internal_widget_pack(this,ret);
618         return ret;
619 }
620
621
622 static struct widget *
623 gui_internal_button_new_with_callback(struct gui_priv *this, char *text, struct graphics_image *image, enum flags flags, void(*func)(struct gui_priv *priv, struct widget *widget, void *data), void *data)
624 {
625         struct widget *ret=NULL;
626         ret=gui_internal_box_new(this, flags);
627         if (ret) {
628                 if (image)
629                         gui_internal_widget_append(ret, gui_internal_image_new(this, image));
630                 if (text)
631                         gui_internal_widget_append(ret, gui_internal_text_new(this, text, gravity_center|orientation_vertical));
632                 ret->func=func;
633                 ret->data=data;
634                 if (func) {
635                         ret->state |= STATE_SENSITIVE;
636                         ret->speech=g_strdup(text);
637                 }
638         }
639         return ret;
640
641 }
642
643 static int
644 gui_internal_button_attr_update(struct gui_priv *this, struct widget *w)
645 {
646         struct widget *wi;
647         int is_on=0;
648         struct attr curr;
649         GList *l;
650
651         if (w->get_attr(w->instance, w->on.type, &curr, NULL))
652                 is_on=curr.u.data == w->on.u.data;
653         else
654                 is_on=w->deflt;
655         if (is_on != w->is_on) {
656                 if (w->redraw)
657                         this->redraw=1;
658                 w->is_on=is_on;
659                 l=g_list_first(w->children);
660                 if (l) {
661                         wi=l->data;
662                         if (wi->img)
663                                 graphics_image_free(this->gra, wi->img);
664                         wi->img=image_new_xs(this, is_on ? "gui_active" : "gui_inactive");
665                 }
666                 if (w->is_on && w->off.type == attr_none)
667                         w->state &= ~STATE_SENSITIVE;
668                 else
669                         w->state |= STATE_SENSITIVE;
670                 return 1;
671         }
672         return 0;
673 }
674
675 static void
676 gui_internal_button_attr_callback(struct gui_priv *this, struct widget *w)
677 {
678         if (gui_internal_button_attr_update(this, w))
679                 gui_internal_widget_render(this, w);
680 }
681 static void
682 gui_internal_button_attr_pressed(struct gui_priv *this, struct widget *w, void *data)
683 {
684         if (w->is_on)
685                 w->set_attr(w->instance, &w->off);
686         else
687                 w->set_attr(w->instance, &w->on);
688         gui_internal_button_attr_update(this, w);
689
690 }
691
692 static struct widget *
693 gui_internal_button_navit_attr_new(struct gui_priv *this, char *text, enum flags flags, struct attr *on, struct attr *off)
694 {
695         struct graphics_image *image=NULL;
696         struct widget *ret;
697         if (!on && !off)
698                 return NULL;
699         image=image_new_xs(this, "gui_inactive");
700         ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
701         if (on)
702                 ret->on=*on;
703         if (off)
704                 ret->off=*off;
705         ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))navit_get_attr;
706         ret->set_attr=(int (*)(void *, struct attr *))navit_set_attr;
707         ret->remove_cb=(void (*)(void *, struct callback *))navit_remove_callback;
708         ret->instance=this->nav;
709         ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
710         navit_add_callback(this->nav, ret->cb);
711         gui_internal_button_attr_update(this, ret);
712         return ret;
713 }
714
715 static struct widget *
716 gui_internal_button_map_attr_new(struct gui_priv *this, char *text, enum flags flags, struct map *map, struct attr *on, struct attr *off, int deflt)
717 {
718         struct graphics_image *image=NULL;
719         struct widget *ret;
720         image=image_new_xs(this, "gui_inactive");
721         if (!on && !off)
722                 return NULL;
723         ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
724         if (on)
725                 ret->on=*on;
726         if (off)
727                 ret->off=*off;
728         ret->deflt=deflt;
729         ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))map_get_attr;
730         ret->set_attr=(int (*)(void *, struct attr *))map_set_attr;
731         ret->remove_cb=(void (*)(void *, struct callback *))map_remove_callback;
732         ret->instance=map;
733         ret->redraw=1;
734         ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
735         map_add_callback(map, ret->cb);
736         gui_internal_button_attr_update(this, ret);
737         return ret;
738 }
739
740 static struct widget *
741 gui_internal_button_new(struct gui_priv *this, char *text, struct graphics_image *image, enum flags flags)
742 {
743         return gui_internal_button_new_with_callback(this, text, image, flags, NULL, NULL);
744 }
745
746 #if 0
747 //##############################################################################################################
748 //# Description:
749 //# Comment:
750 //# Authors: Martin Schaller (04/2008)
751 //##############################################################################################################
752 static void gui_internal_clear(struct gui_priv *this)
753 {
754         struct graphics *gra=this->gra;
755         struct point pnt;
756         pnt.x=0;
757         pnt.y=0;
758         graphics_draw_rectangle(gra, this->background, &pnt, this->root.w, this->root.h);
759 }
760 #endif
761
762 static struct widget *
763 gui_internal_find_widget(struct widget *wi, struct point *p, int flags)
764 {
765         struct widget *ret,*child;
766         GList *l=wi->children;
767
768         if (p) {
769                 if (wi->p.x > p->x )
770                         return NULL;
771                 if (wi->p.y > p->y )
772                         return NULL;
773                 if ( wi->p.x + wi->w < p->x)
774                         return NULL;
775                 if ( wi->p.y + wi->h < p->y)
776                         return NULL;
777         }
778         if (wi->state & flags)
779                 return wi;
780         while (l) {
781                 child=l->data;
782                 ret=gui_internal_find_widget(child, p, flags);
783                 if (ret) {
784                         return ret;
785                 }
786                 l=g_list_next(l);
787         }
788         return NULL;
789
790 }
791
792 static void
793 gui_internal_highlight_do(struct gui_priv *this, struct widget *found)
794 {
795         if (found == this->highlighted)
796                 return;
797
798         graphics_draw_mode(this->gra, draw_mode_begin);
799         if (this->highlighted) {
800                 this->highlighted->state &= ~STATE_HIGHLIGHTED;
801                 if (this->root.children && this->highlighted_menu == g_list_last(this->root.children)->data)
802                         gui_internal_widget_render(this, this->highlighted);
803                 this->highlighted=NULL;
804                 this->highlighted_menu=NULL;
805         }
806         if (found) {
807                 this->highlighted=found;
808                 this->highlighted_menu=g_list_last(this->root.children)->data;
809                 this->highlighted->state |= STATE_HIGHLIGHTED;
810                 gui_internal_widget_render(this, this->highlighted);
811                 dbg(1,"%d,%d %dx%d\n", found->p.x, found->p.y, found->w, found->h);
812         }
813         graphics_draw_mode(this->gra, draw_mode_end);
814 }
815 //##############################################################################################################
816 //# Description:
817 //# Comment:
818 //# Authors: Martin Schaller (04/2008)
819 //##############################################################################################################
820 static void gui_internal_highlight(struct gui_priv *this)
821 {
822         struct widget *menu,*found=NULL;
823         if (this->current.x > -1 && this->current.y > -1) {
824                 menu=g_list_last(this->root.children)->data;
825                 found=gui_internal_find_widget(menu, &this->current, STATE_SENSITIVE);
826         }
827         gui_internal_highlight_do(this, found);
828         this->motion_timeout_event=NULL;
829 }
830
831 static struct widget *
832 gui_internal_box_new_with_label(struct gui_priv *this, enum flags flags, char *label)
833 {
834         struct widget *widget=g_new0(struct widget, 1);
835
836         if (label)
837                 widget->text=g_strdup(label);
838         widget->type=widget_box;
839         widget->flags=flags;
840         return widget;
841 }
842
843 static struct widget *
844 gui_internal_box_new(struct gui_priv *this, enum flags flags)
845 {
846         return gui_internal_box_new_with_label(this, flags, NULL);
847 }
848
849
850 static void gui_internal_box_render(struct gui_priv *this, struct widget *w)
851 {
852         struct widget *wc;
853         GList *l;
854
855         gui_internal_background_render(this, w);
856 #if 1
857         if (w->foreground && w->border) {
858         struct point pnt[5];
859         pnt[0]=w->p;
860         pnt[1].x=pnt[0].x+w->w;
861         pnt[1].y=pnt[0].y;
862         pnt[2].x=pnt[0].x+w->w;
863         pnt[2].y=pnt[0].y+w->h;
864         pnt[3].x=pnt[0].x;
865         pnt[3].y=pnt[0].y+w->h;
866         pnt[4]=pnt[0];
867         graphics_gc_set_linewidth(w->foreground, w->border ? w->border : 1);
868         graphics_draw_lines(this->gra, w->foreground, pnt, 5);
869         graphics_gc_set_linewidth(w->foreground, 1);
870         }
871 #endif
872
873         l=w->children;
874         while (l) {
875                 wc=l->data;
876                 gui_internal_widget_render(this, wc);
877                 l=g_list_next(l);
878         }
879 }
880
881
882 /**
883  * @brief Compute the size and location for the widget.
884  *
885  *
886  */
887 static void gui_internal_box_pack(struct gui_priv *this, struct widget *w)
888 {
889         struct widget *wc;
890         int x0,x=0,y=0,width=0,height=0,owidth=0,oheight=0,expand=0,count=0,rows=0,cols=w->cols ? w->cols : 0;
891         GList *l;
892         int orientation=w->flags & 0xffff0000;
893
894         if (!cols)
895                 cols=this->cols;
896         if (!cols) {
897                  if ( this->root.w > this->root.h )
898                          cols=3;
899                  else
900                          cols=2;
901                  width=0;
902                  height=0;
903         }
904
905
906         /**
907          * count the number of children
908          */
909         l=w->children;
910         while (l) {
911                 count++;
912                 l=g_list_next(l);
913         }
914         if (orientation == orientation_horizontal_vertical && count <= cols)
915                 orientation=orientation_horizontal;
916         switch (orientation) {
917         case orientation_horizontal:
918                 /**
919                  * For horizontal orientation:
920                  * pack each child and find the largest height and
921                  * compute the total width. x spacing (spx) is considered
922                  *
923                  * If any children want to be expanded
924                  * we keep track of this
925                  */
926                 l=w->children;
927                 while (l) {
928                         wc=l->data;
929                         gui_internal_widget_pack(this, wc);
930                         if (height < wc->h)
931                                 height=wc->h;
932                         width+=wc->w;
933                         if (wc->flags & flags_expand)
934                                 expand+=wc->w ? wc->w : 1;
935                         l=g_list_next(l);
936                         if (l)
937                                 width+=w->spx;
938                 }
939                 owidth=width;
940                 if (expand && w->w) {
941                         expand=100*(w->w-width+expand)/expand;
942                         owidth=w->w;
943                 } else
944                         expand=100;
945                 break;
946         case orientation_vertical:
947                 /**
948                  * For vertical layouts:
949                  * We pack each child and compute the largest width and
950                  * the total height.  y spacing (spy) is considered
951                  *
952                  * If the expand flag is set then teh expansion amount
953                  * is computed.
954                  */
955                 l=w->children;
956                 while (l) {
957                         wc=l->data;
958                         gui_internal_widget_pack(this, wc);
959                         if (width < wc->w)
960                                 width=wc->w;
961                         height+=wc->h;
962                         if (wc->flags & flags_expand)
963                                 expand+=wc->h ? wc->h : 1;
964                         l=g_list_next(l);
965                         if (l)
966                                 height+=w->spy;
967                 }
968                 oheight=height;
969                 if (expand && w->h) {
970                         expand=100*(w->h-height+expand)/expand;
971                         oheight=w->h;
972                 } else
973                         expand=100;
974                 break;
975         case orientation_horizontal_vertical:
976                 /**
977                  * For horizontal_vertical orientation
978                  * pack the children.
979                  * Compute the largest height and largest width.
980                  * Layout the widgets based on having the
981                  * number of columns specified by (cols)
982                  */
983                 count=0;
984                 l=w->children;
985                 while (l) {
986                         wc=l->data;
987                         gui_internal_widget_pack(this, wc);
988                         if (height < wc->h)
989                                 height=wc->h;
990                         if (width < wc->w)
991                                 width=wc->w;
992                         l=g_list_next(l);
993                         count++;
994                 }
995                 if (count < cols)
996                         cols=count;
997                 rows=(count+cols-1)/cols;
998                 width*=cols;
999                 height*=rows;
1000                 width+=w->spx*(cols-1);
1001                 height+=w->spy*(rows-1);
1002                 owidth=width;
1003                 oheight=height;
1004                 expand=100;
1005                 break;
1006         default:
1007                 /**
1008                  * No orientation was specified.
1009                  * width and height are both 0.
1010                  * The width & height of this widget
1011                  * will be used.
1012                  */
1013                 if(!w->w && !w->h)
1014                         dbg(0,"Warning width and height of a widget are 0");
1015                 break;
1016         }
1017         if (! w->w && ! w->h) {
1018                 w->w=w->bl+w->br+width;
1019                 w->h=w->bt+w->bb+height;
1020         }
1021         if (expand < 100)
1022                 expand=100;
1023
1024         /**
1025          * At this stage the width and height of this
1026          * widget has been computed.
1027          * We now make a second pass assigning heights,
1028          * widths and coordinates to each child widget.
1029          */
1030
1031         if (w->flags & gravity_left)
1032                 x=w->p.x+w->bl;
1033         if (w->flags & gravity_xcenter)
1034                 x=w->p.x+w->w/2-owidth/2;
1035         if (w->flags & gravity_right)
1036                 x=w->p.x+w->w-w->br-owidth;
1037         if (w->flags & gravity_top)
1038                 y=w->p.y+w->bt;
1039         if (w->flags & gravity_ycenter)
1040                 y=w->p.y+w->h/2-oheight/2;
1041         if (w->flags & gravity_bottom)
1042                 y=w->p.y+w->h-w->bb-oheight;
1043         l=w->children;
1044         switch (orientation) {
1045         case orientation_horizontal:
1046                 l=w->children;
1047                 while (l) {
1048                         wc=l->data;
1049                         wc->p.x=x;
1050                         if (wc->flags & flags_fill)
1051                                 wc->h=w->h;
1052                         if (wc->flags & flags_expand) {
1053                                 if (! wc->w)
1054                                         wc->w=1;
1055                                 wc->w=wc->w*expand/100;
1056                         }
1057                         if (w->flags & gravity_top)
1058                                 wc->p.y=y;
1059                         if (w->flags & gravity_ycenter)
1060                                 wc->p.y=y-wc->h/2;
1061                         if (w->flags & gravity_bottom)
1062                                 wc->p.y=y-wc->h;
1063                         x+=wc->w+w->spx;
1064                         l=g_list_next(l);
1065                 }
1066                 break;
1067         case orientation_vertical:
1068                 l=w->children;
1069                 while (l) {
1070                         wc=l->data;
1071                         wc->p.y=y;
1072                         if (wc->flags & flags_fill)
1073                                 wc->w=w->w;
1074                         if (wc->flags & flags_expand) {
1075                                 if (! wc->h)
1076                                         wc->h=1;
1077                                 wc->h=wc->h*expand/100;
1078                         }
1079                         if (w->flags & gravity_left)
1080                                 wc->p.x=x;
1081                         if (w->flags & gravity_xcenter)
1082                                 wc->p.x=x-wc->w/2;
1083                         if (w->flags & gravity_right)
1084                                 wc->p.x=x-wc->w;
1085                         y+=wc->h+w->spy;
1086                         l=g_list_next(l);
1087                 }
1088                 break;
1089         case orientation_horizontal_vertical:
1090                 l=w->children;
1091                 x0=x;
1092                 count=0;
1093                 width/=cols;
1094                 height/=rows;
1095                 while (l) {
1096                         wc=l->data;
1097                         wc->p.x=x;
1098                         wc->p.y=y;
1099                         if (wc->flags & flags_fill) {
1100                                 wc->w=width;
1101                                 wc->h=height;
1102                         }
1103                         if (w->flags & gravity_left)
1104                                 wc->p.x=x;
1105                         if (w->flags & gravity_xcenter)
1106                                 wc->p.x=x+(width-wc->w)/2;
1107                         if (w->flags & gravity_right)
1108                                 wc->p.x=x+width-wc->w;
1109                         if (w->flags & gravity_top)
1110                                 wc->p.y=y;
1111                         if (w->flags & gravity_ycenter)
1112                                 wc->p.y=y+(height-wc->h)/2;
1113                         if (w->flags & gravity_bottom)
1114                                 wc->p.y=y-height-wc->h;
1115                         x+=width;
1116                         if (++count == cols) {
1117                                 count=0;
1118                                 x=x0;
1119                                 y+=height;
1120                         }
1121                         l=g_list_next(l);
1122                 }
1123                 break;
1124         default:
1125                 break;
1126         }
1127         /**
1128          * Call pack again on each child,
1129          * the child has now had its size and coordinates
1130          * set so they can repack their children.
1131          */
1132         l=w->children;
1133         while (l) {
1134                 wc=l->data;
1135                 gui_internal_widget_pack(this, wc);
1136                 l=g_list_next(l);
1137         }
1138 }
1139
1140 static void
1141 gui_internal_widget_append(struct widget *parent, struct widget *child)
1142 {
1143         if (! child)
1144                 return;
1145         if (! child->background)
1146                 child->background=parent->background;
1147         parent->children=g_list_append(parent->children, child);
1148 }
1149
1150 static void gui_internal_widget_prepend(struct widget *parent, struct widget *child)
1151 {
1152         if (! child->background)
1153                 child->background=parent->background;
1154         parent->children=g_list_prepend(parent->children, child);
1155 }
1156
1157 static void gui_internal_widget_children_destroy(struct gui_priv *this, struct widget *w)
1158 {
1159         GList *l;
1160         struct widget *wc;
1161
1162         l=w->children;
1163         while (l) {
1164                 wc=l->data;
1165                 gui_internal_widget_destroy(this, wc);
1166                 l=g_list_next(l);
1167         }
1168         g_list_free(w->children);
1169         w->children=NULL;
1170 }
1171
1172 static void gui_internal_widget_destroy(struct gui_priv *this, struct widget *w)
1173 {
1174         gui_internal_widget_children_destroy(this, w);
1175         g_free(w->command);
1176         g_free(w->speech);
1177         g_free(w->text);
1178         if (w->img)
1179                 graphics_image_free(this->gra, w->img);
1180         if (w->prefix)
1181                 g_free(w->prefix);
1182         if (w->name)
1183                 g_free(w->name);
1184         if (w->data_free)
1185                 w->data_free(w->data);
1186         if (w->cb && w->remove_cb)
1187                 w->remove_cb(w->instance, w->cb);
1188         if(w->free)
1189                 w->free(this,w);
1190         else
1191                 g_free(w);
1192 }
1193
1194
1195 static void
1196 gui_internal_widget_render(struct gui_priv *this, struct widget *w)
1197 {
1198         if(w->p.x > this->root.w || w->p.y > this->root.h)
1199                 return;
1200
1201         switch (w->type) {
1202         case widget_box:
1203                 gui_internal_box_render(this, w);
1204                 break;
1205         case widget_label:
1206                 gui_internal_label_render(this, w);
1207                 break;
1208         case widget_image:
1209                 gui_internal_image_render(this, w);
1210                 break;
1211         case widget_table:
1212                 gui_internal_table_render(this,w);
1213                 break;
1214         default:
1215                 break;
1216         }
1217 }
1218
1219 static void
1220 gui_internal_widget_pack(struct gui_priv *this, struct widget *w)
1221 {
1222         switch (w->type) {
1223         case widget_box:
1224                 gui_internal_box_pack(this, w);
1225                 break;
1226         case widget_table:
1227           gui_internal_table_pack(this,w);
1228         default:
1229                 break;
1230         }
1231 }
1232
1233 //##############################################################################################################
1234 //# Description:
1235 //# Comment:
1236 //# Authors: Martin Schaller (04/2008)
1237 //##############################################################################################################
1238 static void gui_internal_call_highlighted(struct gui_priv *this)
1239 {
1240         if (! this->highlighted || ! this->highlighted->func)
1241                 return;
1242         this->highlighted->reason=1;
1243         this->highlighted->func(this, this->highlighted, this->highlighted->data);
1244 }
1245
1246 static void
1247 gui_internal_say(struct gui_priv *this, struct widget *w, int questionmark)
1248 {
1249         char *text=w->speech;
1250         if (! this->speech)
1251                 return;
1252         if (!text)
1253                 text=w->text;
1254         if (!text)
1255                 text=w->name;
1256         if (text) {
1257                 text=g_strdup_printf("%s%c", text, questionmark ? '?':'\0');
1258                 navit_say(this->nav, text);
1259                 g_free(text);
1260         }
1261 }
1262
1263 static void
1264 gui_internal_prune_menu(struct gui_priv *this, struct widget *w)
1265 {
1266         GList *l;
1267         struct widget *wr;
1268         gui_internal_search_idle_end(this);
1269         while ((l = g_list_last(this->root.children))) {
1270                 if (l->data == w) {
1271                         void (*redisplay)(struct gui_priv *priv, struct widget *widget, void *data);
1272                         gui_internal_say(this, w, 0);
1273                         redisplay=w->menu_data->redisplay;
1274                         wr=w->menu_data->redisplay_widget;
1275                         if (!w->menu_data->redisplay) {
1276                                 gui_internal_widget_render(this, w);
1277                                 return;
1278                         }
1279                         gui_internal_widget_destroy(this, l->data);
1280                         this->root.children=g_list_remove(this->root.children, l->data);
1281                         redisplay(this, wr, wr->data);
1282                         return;
1283                 }
1284                 gui_internal_widget_destroy(this, l->data);
1285                 this->root.children=g_list_remove(this->root.children, l->data);
1286         }
1287 }
1288
1289 static void
1290 gui_internal_prune_menu_count(struct gui_priv *this, int count, int render)
1291 {
1292         GList *l;
1293         gui_internal_search_idle_end(this);
1294         while ((l = g_list_last(this->root.children)) && count-- > 0) {
1295                 gui_internal_widget_destroy(this, l->data);
1296                 this->root.children=g_list_remove(this->root.children, l->data);
1297         }
1298         if (l && render) {
1299                 gui_internal_say(this, l->data, 0);
1300                 gui_internal_widget_render(this, l->data);
1301         }
1302 }
1303
1304 static void
1305 gui_internal_back(struct gui_priv *this, struct widget *w, void *data)
1306 {
1307         gui_internal_prune_menu_count(this, 1, 1);
1308 }
1309
1310 static void
1311 gui_internal_cmd_return(struct gui_priv *this, struct widget *wm, void *data)
1312 {
1313         gui_internal_prune_menu(this, wm->data);
1314 }
1315
1316 static void
1317 gui_internal_cmd_main_menu(struct gui_priv *this, struct widget *wm, void *data)
1318 {
1319         gui_internal_prune_menu(this, this->root.children->data);
1320 }
1321
1322 static struct widget *
1323 gui_internal_top_bar(struct gui_priv *this)
1324 {
1325         struct widget *w,*wm,*wh,*wc,*wcn;
1326         int dots_len, sep_len;
1327         GList *res=NULL,*l;
1328         int width,width_used=0,use_sep=0,incomplete=0;
1329         struct graphics_gc *foreground=(this->flags & 256 ? this->background : this->text_foreground);
1330 /* flags
1331         1:Don't expand bar to screen width
1332         2:Don't show Map Icon
1333         4:Don't show Home Icon
1334         8:Show only current menu
1335         16:Don't use menu titles as button
1336         32:Show navit version
1337         64:Show time
1338         128:Show help
1339         256:Use background for menu headline
1340         512:Set osd_configuration and zoom to route when setting position
1341 */
1342
1343         w=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|(this->flags & 1 ? 0:flags_fill));
1344         w->bl=this->spacing;
1345         w->spx=this->spacing;
1346         w->background=this->background2;
1347         if ((this->flags & 6) == 6) {
1348                 w->bl=10;
1349                 w->br=10;
1350                 w->bt=6;
1351                 w->bb=6;
1352         }
1353         width=this->root.w-w->bl;
1354         if (! (this->flags & 2)) {
1355                 wm=gui_internal_button_new_with_callback(this, NULL,
1356                         image_new_s(this, "gui_map"), gravity_center|orientation_vertical,
1357                         gui_internal_cmd_return, NULL);
1358                 wm->speech=g_strdup(_("Back to map"));
1359                 gui_internal_widget_pack(this, wm);
1360                 gui_internal_widget_append(w, wm);
1361                 width-=wm->w;
1362         }
1363         if (! (this->flags & 4)) {
1364                 wh=gui_internal_button_new_with_callback(this, NULL,
1365                         image_new_s(this, "gui_home"), gravity_center|orientation_vertical,
1366                         gui_internal_cmd_main_menu, NULL);
1367                 wh->speech=g_strdup(_("Main Menu"));
1368                 gui_internal_widget_pack(this, wh);
1369                 gui_internal_widget_append(w, wh);
1370                 width-=wh->w;
1371         }
1372         if (!(this->flags & 6))
1373                 width-=w->spx;
1374         l=g_list_last(this->root.children);
1375         wcn=gui_internal_label_new(this,".. »");
1376         wcn->foreground=foreground;
1377         dots_len=wcn->w;
1378         gui_internal_widget_destroy(this, wcn);
1379         wcn=gui_internal_label_new(this,"»");
1380         wcn->foreground=foreground;
1381         sep_len=wcn->w;
1382         gui_internal_widget_destroy(this, wcn);
1383         while (l) {
1384                 if (g_list_previous(l) || !g_list_next(l)) {
1385                         wc=l->data;
1386                         wcn=gui_internal_label_new(this, wc->text);
1387                         wcn->foreground=foreground;
1388                         if (g_list_next(l))
1389                                 use_sep=1;
1390                         else
1391                                 use_sep=0;
1392                         dbg(1,"%d (%s) + %d + %d + %d > %d\n", wcn->w, wc->text, width_used, w->spx, use_sep ? sep_len : 0, width);
1393                         if (wcn->w + width_used + w->spx + (use_sep ? sep_len : 0) + (g_list_previous(l) ? dots_len : 0) > width) {
1394                                 incomplete=1;
1395                                 gui_internal_widget_destroy(this, wcn);
1396                                 break;
1397                         }
1398                         if (use_sep) {
1399                                 struct widget *wct=gui_internal_label_new(this, "»");
1400                                 wct->foreground=foreground;
1401                                 res=g_list_prepend(res, wct);
1402                                 width_used+=sep_len+w->spx;
1403                         }
1404                         width_used+=wcn->w;
1405                         if (!(this->flags & 16)) {
1406                                 wcn->func=gui_internal_cmd_return;
1407                                 wcn->data=wc;
1408                                 wcn->state |= STATE_SENSITIVE;
1409                         }
1410                         res=g_list_prepend(res, wcn);
1411                         if (this->flags & 8)
1412                                 break;
1413                 }
1414                 l=g_list_previous(l);
1415         }
1416         if (incomplete) {
1417                 if (! res) {
1418                         wcn=gui_internal_label_new_abbrev(this, wc->text, width-width_used-w->spx-dots_len);
1419                         wcn->foreground=foreground;
1420                         wcn->func=gui_internal_cmd_return;
1421                         wcn->data=wc;
1422                         wcn->state |= STATE_SENSITIVE;
1423                         res=g_list_prepend(res, wcn);
1424                         l=g_list_previous(l);
1425                         wc=l->data;
1426                 }
1427                 wcn=gui_internal_label_new(this, ".. »");
1428                 wcn->foreground=foreground;
1429                 wcn->func=gui_internal_cmd_return;
1430                 wcn->data=wc;
1431                 wcn->state |= STATE_SENSITIVE;
1432                 res=g_list_prepend(res, wcn);
1433         }
1434         l=res;
1435         while (l) {
1436                 gui_internal_widget_append(w, l->data);
1437                 l=g_list_next(l);
1438         }
1439         if (this->flags & 32) {
1440                 extern char *version;
1441                 char *version_text=g_strdup_printf("Navit %s",version);
1442                 wcn=gui_internal_label_new(this, version_text);
1443                 g_free(version_text);
1444                 wcn->flags=gravity_right_center|flags_expand;
1445                 gui_internal_widget_append(w, wcn);
1446         }
1447 #if 0
1448         if (dots)
1449                 gui_internal_widget_destroy(this, dots);
1450 #endif
1451         return w;
1452 }
1453
1454 static struct widget *
1455 gui_internal_time_help(struct gui_priv *this)
1456 {
1457         struct widget *w,*wc,*wcn;
1458         char timestr[64];
1459         struct tm *tm;
1460         time_t timep;
1461
1462         w=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
1463         w->bl=this->spacing;
1464         w->spx=this->spacing;
1465         w->spx=10;
1466         w->bl=10;
1467         w->br=10;
1468         w->bt=6;
1469         w->bb=6;
1470         if (this->flags & 64) {
1471                 wc=gui_internal_box_new(this, gravity_right_top|orientation_vertical|flags_fill);
1472                 wc->bl=10;
1473                 wc->br=20;
1474                 wc->bt=6;
1475                 wc->bb=6;
1476                 timep=time(NULL);
1477                 tm=localtime(&timep);
1478                 strftime(timestr, 64, "%H:%M %d.%m.%Y", tm);
1479                 wcn=gui_internal_label_new(this, timestr);
1480                 gui_internal_widget_append(wc, wcn);
1481                 gui_internal_widget_append(w, wc);
1482         }
1483         if (this->flags & 128) {
1484                 wcn=gui_internal_button_new_with_callback(this, _("Help"), image_new_l(this, "gui_help"), gravity_center|orientation_vertical|flags_fill, NULL, NULL);
1485                 gui_internal_widget_append(w, wcn);
1486         }
1487         return w;
1488 }
1489
1490
1491 /**
1492  * Applys the configuration values to this based on the settings
1493  * specified in the configuration file (this->config) and
1494  * the most approriate default profile based on screen resolution.
1495  *
1496  * This function should be run after this->root is setup and could
1497  * be rerun after the window is resized.
1498  *
1499  * @authors Steve Singer <ssinger_pg@sympatico.ca> (09/2008)
1500  */
1501 static void gui_internal_apply_config(struct gui_priv *this)
1502 {
1503   struct gui_config_settings *  current_config=0;
1504
1505   dbg(0,"w=%d h=%d\n", this->root.w, this->root.h);
1506   /**
1507    * Select default values from profile based on the screen.
1508    */
1509   if((this->root.w > 320 || this->root.h > 320) && this->root.w > 240 && this->root.h > 240)
1510   {
1511     if((this->root.w > 640 || this->root.h > 640) && this->root.w > 480 && this->root.h > 480 )
1512     {
1513       current_config = &config_profiles[LARGE_PROFILE];
1514     }
1515     else
1516     {
1517       current_config = &config_profiles[MEDIUM_PROFILE];
1518     }
1519   }
1520   else
1521   {
1522     current_config = &config_profiles[SMALL_PROFILE];
1523   }
1524
1525   /**
1526    * Apply override values from config file
1527    */
1528   if(this->config.font_size == -1 )
1529   {
1530     this->font_size = current_config->font_size;
1531   }
1532   else
1533   {
1534     this->font_size = this->config.font_size;
1535   }
1536
1537   if(this->config.icon_xs == -1 )
1538   {
1539       this->icon_xs = current_config->icon_xs;
1540   }
1541   else
1542   {
1543     this->icon_xs = this->config.icon_xs;
1544   }
1545
1546   if(this->config.icon_s == -1 )
1547   {
1548     this->icon_s = current_config->icon_s;
1549   }
1550   else
1551   {
1552     this->icon_s = this->config.icon_s;
1553   }
1554   if(this->config.icon_l == -1 )
1555   {
1556     this->icon_l = current_config->icon_l;
1557   }
1558   else
1559   {
1560     this->icon_l = this->config.icon_l;
1561   }
1562   if(this->config.spacing == -1 )
1563   {
1564     this->spacing = current_config->spacing;
1565   }
1566   else
1567   {
1568     this->spacing = current_config->spacing;
1569   }
1570
1571 }
1572
1573 static struct widget *
1574 gui_internal_button_label(struct gui_priv *this, char *label, int mode)
1575 {
1576         struct widget *wl,*wlb;
1577         struct widget *wb=gui_internal_menu_data(this)->button_bar;
1578         wlb=gui_internal_box_new(this, gravity_right_center|orientation_vertical);
1579         wl=gui_internal_label_new(this, label);
1580         wlb->border=1;
1581         wlb->foreground=this->text_foreground;
1582         wlb->bl=20;
1583         wlb->br=20;
1584         wlb->bb=6;
1585         wlb->bt=6;
1586         gui_internal_widget_append(wlb, wl);
1587         if (mode == 1)
1588                 gui_internal_widget_prepend(wb, wlb);
1589         if (mode == -1)
1590                 gui_internal_widget_append(wb, wlb);
1591
1592         return wlb;
1593 }
1594
1595
1596 static struct widget *
1597 gui_internal_menu(struct gui_priv *this, char *label)
1598 {
1599         struct widget *menu,*w,*w1,*topbox;
1600
1601         gui_internal_search_idle_end(this);
1602         topbox=gui_internal_box_new_with_label(this, 0, label);
1603         topbox->w=this->root.w;
1604         topbox->h=this->root.h;
1605         gui_internal_widget_append(&this->root, topbox);
1606         menu=gui_internal_box_new(this, gravity_left_center|orientation_vertical);
1607         menu->w=this->root.w;
1608         menu->h=this->root.h;
1609         menu->background=this->background;
1610         gui_internal_apply_config(this);
1611         this->font=graphics_font_new(this->gra,this->font_size,1);
1612         topbox->menu_data=g_new0(struct menu_data, 1);
1613         gui_internal_widget_append(topbox, menu);
1614         w=gui_internal_top_bar(this);
1615         gui_internal_widget_append(menu, w);
1616         w=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
1617         w->spx=4*this->spacing;
1618         w->w=menu->w;
1619         gui_internal_widget_append(menu, w);
1620         if (this->flags & 16) {
1621                 struct widget *wlb,*wb,*wm=w;
1622                 wm->flags=gravity_center|orientation_vertical|flags_expand|flags_fill;
1623                 w=gui_internal_box_new(this, gravity_center|orientation_horizontal|flags_expand|flags_fill);
1624                 dbg(0,"topbox->menu_data=%p\n", topbox->menu_data);
1625                 gui_internal_widget_append(wm, w);
1626                 wb=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
1627                 wb->bl=6;
1628                 wb->br=6;
1629                 wb->bb=6;
1630                 wb->bt=6;
1631                 wb->spx=6;
1632                 topbox->menu_data->button_bar=wb;
1633                 gui_internal_widget_append(wm, wb);
1634                 wlb=gui_internal_button_label(this,_("Back"),1);
1635                 wlb->func=gui_internal_back;
1636                 wlb->state |= STATE_SENSITIVE;
1637         }
1638         if (this->flags & 192) {
1639                 menu=gui_internal_box_new(this, gravity_left_center|orientation_vertical);
1640                 menu->w=this->root.w;
1641                 menu->h=this->root.h;
1642                 w1=gui_internal_time_help(this);
1643                 gui_internal_widget_append(menu, w1);
1644                 w1=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
1645                 gui_internal_widget_append(menu, w1);
1646                 gui_internal_widget_append(topbox, menu);
1647                 menu->background=NULL;
1648         }
1649         return w;
1650 }
1651
1652 static struct menu_data *
1653 gui_internal_menu_data(struct gui_priv *this)
1654 {
1655         GList *l;
1656         struct widget *menu;
1657
1658         l=g_list_last(this->root.children);
1659         menu=l->data;
1660         return menu->menu_data;
1661 }
1662
1663 static void
1664 gui_internal_menu_render(struct gui_priv *this)
1665 {
1666         GList *l;
1667         struct widget *menu;
1668
1669         l=g_list_last(this->root.children);
1670         menu=l->data;
1671         gui_internal_say(this, menu, 0);
1672         gui_internal_widget_pack(this, menu);
1673         gui_internal_widget_render(this, menu);
1674 }
1675
1676 static void
1677 gui_internal_cmd_set_destination(struct gui_priv *this, struct widget *wm, void *data)
1678 {
1679         struct widget *w=wm->data;
1680         dbg(0,"c=%d:0x%x,0x%x\n", w->c.pro, w->c.x, w->c.y);
1681         navit_set_destination(this->nav, &w->c, w->name, 1);
1682         if (this->flags & 512) {
1683                 struct attr follow;
1684                 follow.type=attr_follow;
1685                 follow.u.num=180;
1686                 navit_set_attr(this->nav, &this->osd_configuration);
1687                 navit_set_attr(this->nav, &follow);
1688                 navit_zoom_to_route(this->nav, 0);
1689         }
1690         gui_internal_prune_menu(this, NULL);
1691 }
1692
1693 static void
1694 gui_internal_cmd_set_position(struct gui_priv *this, struct widget *wm, void *data)
1695 {
1696         struct widget *w=wm->data;
1697         navit_set_position(this->nav, &w->c);
1698         gui_internal_prune_menu(this, NULL);
1699 }
1700
1701 static void
1702 gui_internal_cmd_add_bookmark_do(struct gui_priv *this, struct widget *widget)
1703 {
1704         GList *l;
1705         dbg(0,"text='%s'\n", widget->text);
1706         if (widget->text && strlen(widget->text))
1707                 navit_add_bookmark(this->nav, &widget->c, widget->text);
1708         g_free(widget->text);
1709         widget->text=NULL;
1710         l=g_list_previous(g_list_last(this->root.children));
1711         gui_internal_prune_menu(this, l->data);
1712 }
1713
1714 static void
1715 gui_internal_cmd_add_bookmark_clicked(struct gui_priv *this, struct widget *widget, void *data)
1716 {
1717         gui_internal_cmd_add_bookmark_do(this, widget->data);
1718 }
1719
1720 static void
1721 gui_internal_cmd_add_bookmark_changed(struct gui_priv *this, struct widget *wm, void *data)
1722 {
1723         int len;
1724         dbg(1,"enter\n");
1725         if (wm->text) {
1726                 len=strlen(wm->text);
1727                 dbg(1,"len=%d\n", len);
1728                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
1729                         wm->text[len-1]='\0';
1730                         gui_internal_cmd_add_bookmark_do(this, wm);
1731                 }
1732         }
1733 }
1734
1735
1736 static struct widget * gui_internal_keyboard(struct gui_priv *this, int mode);
1737
1738 static void
1739 gui_internal_cmd_add_bookmark(struct gui_priv *this, struct widget *wm, void *data)
1740 {
1741         struct widget *w,*wb,*wk,*wl,*we,*wnext,*wp=wm->data;
1742         wb=gui_internal_menu(this,_("Add Bookmark"));
1743         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
1744         gui_internal_widget_append(wb, w);
1745         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
1746         gui_internal_widget_append(w, we);
1747         gui_internal_widget_append(we, wk=gui_internal_label_new(this, wp->name ? wp->name : wp->text));
1748         wk->state |= STATE_EDIT|STATE_CLEAR;
1749         wk->background=this->background;
1750         wk->flags |= flags_expand|flags_fill;
1751         wk->func = gui_internal_cmd_add_bookmark_changed;
1752         wk->c=wm->c;
1753         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
1754         wnext->state |= STATE_SENSITIVE;
1755         wnext->func = gui_internal_cmd_add_bookmark_clicked;
1756         wnext->data=wk;
1757         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
1758         gui_internal_widget_append(w, wl);
1759         if (this->keyboard)
1760                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
1761         gui_internal_menu_render(this);
1762 }
1763
1764
1765 static void
1766 get_direction(char *buffer, int angle, int mode)
1767 {
1768         angle=angle%360;
1769         switch (mode) {
1770         case 0:
1771                 sprintf(buffer,"%d",angle);
1772                 break;
1773         case 1:
1774                 if (angle < 69 || angle > 291)
1775                         *buffer++='N';
1776                 if (angle > 111 && angle < 249)
1777                         *buffer++='S';
1778                 if (angle > 22 && angle < 158)
1779                         *buffer++='E';
1780                 if (angle > 202 && angle < 338)
1781                         *buffer++='W';
1782                 *buffer++='\0';
1783                 break;
1784         case 2:
1785                 angle=(angle+15)/30;
1786                 if (! angle)
1787                         angle=12;
1788                 sprintf(buffer,"%d H", angle);
1789                 break;
1790         }
1791 }
1792
1793 struct selector {
1794         char *icon;
1795         char *name;
1796         enum item_type *types;
1797 } selectors[]={
1798         {"bank","Bank",(enum item_type []){type_poi_bank,type_poi_bank,type_none}},
1799         {"fuel","Fuel",(enum item_type []){type_poi_fuel,type_poi_fuel,type_none}},
1800         {"hotel","Hotel",(enum item_type []) {
1801                 type_poi_hotel,type_poi_camp_rv,
1802                 type_poi_camping,type_poi_camping,
1803                 type_poi_resort,type_poi_resort,
1804                 type_poi_motel,type_poi_hostel,
1805                 type_none}},
1806         {"restaurant","Restaurant",(enum item_type []) {
1807                 type_poi_bar,type_poi_picnic,
1808                 type_poi_burgerking,type_poi_fastfood,
1809                 type_poi_restaurant,type_poi_restaurant,
1810                 type_none}},
1811         {"shopping","Shopping",(enum item_type []) {
1812                 type_poi_mall,type_poi_mall,
1813                 type_poi_shop_grocery,type_poi_shop_grocery,
1814                 type_none}},
1815         {"hospital","Service",(enum item_type []) {
1816                 type_poi_marina,type_poi_marina,
1817                 type_poi_hospital,type_poi_hospital,
1818                 type_poi_public_utilities,type_poi_public_utilities,
1819                 type_poi_police,type_poi_autoservice,
1820                 type_poi_information,type_poi_information,
1821                 type_poi_personal_service,type_poi_repair_service,
1822                 type_poi_restroom,type_poi_restroom,
1823                 type_none}},
1824         {"parking","Parking",(enum item_type []){type_poi_car_parking,type_poi_car_parking,type_none}},
1825         {"peak","Land Features",(enum item_type []){
1826                 type_poi_land_feature,type_poi_rock,
1827                 type_poi_dam,type_poi_dam,
1828                 type_none}},
1829         {"unknown","Other",(enum item_type []){
1830                 type_point_unspecified,type_point_unkn-1,
1831                 type_point_unkn+1,type_poi_land_feature-1,
1832                 type_poi_rock+1,type_poi_fuel-1,
1833                 type_poi_marina+1,type_poi_car_parking-1,
1834                 type_poi_car_parking+1,type_poi_bar-1,
1835                 type_poi_bank+1,type_poi_dam-1,
1836                 type_poi_dam+1,type_poi_information-1,
1837                 type_poi_information+1,type_poi_mall-1,
1838                 type_poi_mall+1,type_poi_personal_service-1,
1839                 type_poi_restaurant+1,type_poi_restroom-1,
1840                 type_poi_restroom+1,type_poi_shop_grocery-1,
1841                 type_poi_shop_grocery+1,type_poi_motel-1,
1842                 type_poi_hostel+1,type_selected_point,
1843                 type_none}},
1844         {"unknown","Unknown",(enum item_type []){
1845                 type_point_unkn,type_point_unkn,
1846                 type_none}},
1847 };
1848
1849 static void gui_internal_cmd_pois(struct gui_priv *this, struct widget *wm, void *data);
1850
1851 static struct widget *
1852 gui_internal_cmd_pois_selector(struct gui_priv *this, struct pcoord *c)
1853 {
1854         struct widget *wl,*wb;
1855         int i;
1856         wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
1857         for (i = 0 ; i < sizeof(selectors)/sizeof(struct selector) ; i++) {
1858         gui_internal_widget_append(wl, wb=gui_internal_button_new_with_callback(this, NULL,
1859                 image_new_xs(this, selectors[i].icon), gravity_left_center|orientation_vertical,
1860                 gui_internal_cmd_pois, &selectors[i]));
1861                 wb->c=*c;
1862                 wb->bt=10;
1863         }
1864         return wl;
1865 }
1866
1867 static struct widget *
1868 gui_internal_cmd_pois_item(struct gui_priv *this, struct coord *center, struct item *item, struct coord *c, int dist)
1869 {
1870         char distbuf[32];
1871         char dirbuf[32];
1872         char *type;
1873         struct attr attr;
1874         struct widget *wl,*wt;
1875         char *text;
1876
1877         wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
1878
1879         sprintf(distbuf,"%d", dist/1000);
1880         get_direction(dirbuf, transform_get_angle_delta(center, c, 0), 1);
1881         type=item_to_name(item->type);
1882         if (item_attr_get(item, attr_label, &attr)) {
1883                 wl->name=g_strdup_printf("%s %s",type,attr.u.str);
1884         } else {
1885                 attr.u.str="";
1886                 wl->name=g_strdup(type);
1887         }
1888         text=g_strdup_printf("%s %s %s %s", distbuf, dirbuf, type, attr.u.str);
1889         wt=gui_internal_label_new(this, text);
1890         wt->datai=dist;
1891         gui_internal_widget_append(wl, wt);
1892         g_free(text);
1893
1894         return wl;
1895 }
1896
1897 static gint
1898 gui_internal_cmd_pois_sort_num(gconstpointer a, gconstpointer b, gpointer user_data)
1899 {
1900         const struct widget *wa=a;
1901         const struct widget *wb=b;
1902         struct widget *wac=wa->children->data;
1903         struct widget *wbc=wb->children->data;
1904 #if 0
1905         int ia,ib;
1906         ia=atoi(wac->text);
1907         ib=atoi(wbc->text);
1908
1909         return ia-ib;
1910 #else
1911         return wac->datai-wbc->datai;
1912 #endif
1913 }
1914
1915 static int
1916 gui_internal_cmd_pois_item_selected(struct selector *sel, enum item_type type)
1917 {
1918         enum item_type *types;
1919         if (type >= type_line)
1920                 return 0;
1921         if (! sel || !sel->types)
1922                 return 1;
1923         types=sel->types;
1924         while (*types != type_none) {
1925                 if (type >= types[0] && type <= types[1]) {
1926                         return 1;
1927                 }
1928                 types+=2;
1929         }
1930         return 0;
1931 }
1932
1933 static void gui_internal_cmd_position(struct gui_priv *this, struct widget *wm, void *data);
1934
1935 static void
1936 gui_internal_cmd_pois(struct gui_priv *this, struct widget *wm, void *data)
1937 {
1938         struct map_selection *sel,*selm;
1939         struct coord c,center;
1940         struct mapset_handle *h;
1941         struct map *m;
1942         struct map_rect *mr;
1943         struct item *item;
1944         int idist,dist=20000;
1945         struct widget *wi,*w,*w2,*wb;
1946         enum projection pro=wm->c.pro;
1947         struct selector *isel=wm->data;
1948
1949         wb=gui_internal_menu(this, isel ? isel->name : _("POIs"));
1950         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
1951         gui_internal_widget_append(wb, w);
1952         if (! isel)
1953                 gui_internal_widget_append(w, gui_internal_cmd_pois_selector(this,&wm->c));
1954         w2=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
1955         gui_internal_widget_append(w, w2);
1956
1957         sel=map_selection_rect_new(&wm->c, dist, 18);
1958         center.x=wm->c.x;
1959         center.y=wm->c.y;
1960         h=mapset_open(navit_get_mapset(this->nav));
1961         while ((m=mapset_next(h, 1))) {
1962                 selm=map_selection_dup_pro(sel, pro, map_projection(m));
1963                 mr=map_rect_new(m, selm);
1964                 dbg(2,"mr=%p\n", mr);
1965                 if (mr) {
1966                         while ((item=map_rect_get_item(mr))) {
1967                                 if (gui_internal_cmd_pois_item_selected(isel, item->type) &&
1968                                     item_coord_get_pro(item, &c, 1, pro) &&
1969                                     coord_rect_contains(&sel->u.c_rect, &c) &&
1970                                     (idist=transform_distance(pro, &center, &c)) < dist) {
1971                                         gui_internal_widget_append(w2, wi=gui_internal_cmd_pois_item(this, &center, item, &c, idist));
1972                                         wi->func=gui_internal_cmd_position;
1973                                         wi->data=(void *)2;
1974                                         wi->item=*item;
1975                                         wi->state |= STATE_SENSITIVE;
1976                                         wi->c.x=c.x;
1977                                         wi->c.y=c.y;
1978                                         wi->c.pro=pro;
1979                                 }
1980                         }
1981                         map_rect_destroy(mr);
1982                 }
1983                 map_selection_destroy(selm);
1984         }
1985         map_selection_destroy(sel);
1986         mapset_close(h);
1987         w2->children=g_list_sort_with_data(w2->children,  gui_internal_cmd_pois_sort_num, (void *)1);
1988         gui_internal_menu_render(this);
1989 }
1990
1991 static void
1992 gui_internal_cmd_view_on_map(struct gui_priv *this, struct widget *wm, void *data)
1993 {
1994         struct widget *w=wm->data;
1995         int highlight=(w->data == (void *)2 || w->data == (void *)3 || w->data == (void *)4);
1996         if (highlight) {
1997                 graphics_clear_selection(this->gra, NULL);
1998                 graphics_add_selection(this->gra, &w->item, NULL);
1999         }
2000         navit_set_center(this->nav, &w->c, 1);
2001         gui_internal_prune_menu(this, NULL);
2002 }
2003
2004
2005 static void
2006 gui_internal_cmd_view_attribute_details(struct gui_priv *this, struct widget *wm, void *data)
2007 {
2008         struct widget *w,*wb;
2009         struct map_rect *mr;
2010         struct item *item;
2011         struct attr attr;
2012         char *text,*url;
2013         int i;
2014
2015         text=g_strdup_printf("Attribute %s",wm->name);
2016         wb=gui_internal_menu(this, text);
2017         g_free(text);
2018         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2019         gui_internal_widget_append(wb, w);
2020         mr=map_rect_new(wm->item.map, NULL);
2021         item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
2022         for (i = 0 ; i < wm->datai ; i++) {
2023                 item_attr_get(item, attr_any, &attr);
2024         }
2025         if (item_attr_get(item, attr_any, &attr)) {
2026                 url=NULL;
2027                 switch (attr.type) {
2028                 case attr_osm_nodeid:
2029                         url=g_strdup_printf("http://www.openstreetmap.org/browse/node/%Ld\n",*attr.u.num64);
2030                         break;
2031                 case attr_osm_wayid:
2032                         url=g_strdup_printf("http://www.openstreetmap.org/browse/way/%Ld\n",*attr.u.num64);
2033                         break;
2034                 case attr_osm_relationid:
2035                         url=g_strdup_printf("http://www.openstreetmap.org/browse/relation/%Ld\n",*attr.u.num64);
2036                         break;
2037                 default:
2038                         break;
2039                 }
2040                 if (url) {      
2041                         gui_internal_widget_append(w,
2042                                         wb=gui_internal_button_new_with_callback(this, _("View in Browser"),
2043                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2044                                                 gui_internal_cmd_view_in_browser, NULL));
2045                         wb->name=url;
2046                 }
2047         }
2048         map_rect_destroy(mr);
2049         gui_internal_menu_render(this);
2050 }
2051
2052 static void
2053 gui_internal_cmd_view_attributes(struct gui_priv *this, struct widget *wm, void *data)
2054 {
2055         struct widget *w,*wb;
2056         struct map_rect *mr;
2057         struct item *item;
2058         struct attr attr;
2059         char *text;
2060         int count=0;
2061
2062         dbg(0,"item=%p 0x%x 0x%x\n", wm->item.map,wm->item.id_hi, wm->item.id_lo);
2063         wb=gui_internal_menu(this, "Attributes");
2064         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2065         gui_internal_widget_append(wb, w);
2066         mr=map_rect_new(wm->item.map, NULL);
2067         item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
2068         dbg(0,"item=%p\n", item);
2069         if (item) {
2070                 while(item_attr_get(item, attr_any, &attr)) {
2071                         text=g_strdup_printf("%s:%s", attr_to_name(attr.type), attr_to_text(&attr, wm->item.map, 1));
2072                         gui_internal_widget_append(w,
2073                         wb=gui_internal_button_new_with_callback(this, text,
2074                                 NULL, gravity_left_center|orientation_horizontal|flags_fill,
2075                                 gui_internal_cmd_view_attribute_details, NULL));
2076                         wb->name=g_strdup(text);
2077                         wb->item=wm->item;
2078                         wb->datai=count++;
2079                         g_free(text);
2080                 }
2081         }
2082         map_rect_destroy(mr);
2083         gui_internal_menu_render(this);
2084 }
2085
2086 static void
2087 gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data)
2088 {
2089         struct map_rect *mr;
2090         struct item *item;
2091         struct attr attr;
2092         char *cmd=NULL;
2093
2094         if (!wm->name) {
2095                 dbg(0,"item=%p 0x%x 0x%x\n", wm->item.map,wm->item.id_hi, wm->item.id_lo);
2096                 mr=map_rect_new(wm->item.map, NULL);
2097                 item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
2098                 dbg(0,"item=%p\n", item);
2099                 if (item) {
2100                         while(item_attr_get(item, attr_url_local, &attr)) {
2101                                 if (! cmd)
2102                                         cmd=g_strdup_printf("navit-browser.sh '%s' &",attr.u.str);
2103                         }
2104                 }
2105                 map_rect_destroy(mr);
2106         } else {
2107                 cmd=g_strdup_printf("navit-browser.sh '%s' &",wm->name);
2108         }
2109         if (cmd) {
2110 #ifdef HAVE_SYSTEM
2111                 system(cmd);
2112 #else
2113                 dbg(0,"calling external cmd '%s' is not supported\n",cmd);
2114 #endif
2115                 g_free(cmd);
2116         }
2117 }
2118
2119 /* wm->data: 0 Nothing special
2120              1 Map Point
2121              2 Item
2122              3 Town
2123              4 County
2124              5 Street
2125 */
2126
2127
2128 static void
2129 gui_internal_cmd_position(struct gui_priv *this, struct widget *wm, void *data)
2130 {
2131         struct widget *wb,*w,*wc,*wbc;
2132         struct coord_geo g;
2133         struct coord c;
2134         char *coord,*name;
2135         int display_attributes=(wm->data == (void *)2);
2136         int display_view_on_map=(wm->data != (void *)1);
2137         int display_items=(wm->data == (void *)1);
2138         int display_streets=(wm->data == (void *)3);
2139         int display_house_numbers=(wm->data == (void *)5);
2140         if (wm->data == (void *)4) {
2141                 gui_internal_search_town_in_country(this, wm);
2142                 return;
2143         }
2144 #if 0
2145         switch ((int)wm->data) {
2146                 case 0:
2147 #endif
2148                         c.x=wm->c.x;
2149                         c.y=wm->c.y;
2150                         dbg(0,"x=0x%x y=0x%x\n", c.x, c.y);
2151                         transform_to_geo(wm->c.pro, &c, &g);
2152 #if 0
2153                         break;
2154                 case 1:
2155                         g=this->click;
2156                         break;
2157                 case 2:
2158                         g=this->vehicle;
2159                         break;
2160         }
2161 #endif
2162         name=wm->name ? wm->name : wm->text;
2163         wb=gui_internal_menu(this, name);
2164         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2165         gui_internal_widget_append(wb, w);
2166         coord=coordinates(&wm->c, ' ');
2167         gui_internal_widget_append(w, gui_internal_label_new(this, coord));
2168         g_free(coord);
2169         if (display_streets) {
2170                 gui_internal_widget_append(w,
2171                         wc=gui_internal_button_new_with_callback(this, _("Streets"),
2172                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2173                                 gui_internal_search_street_in_town, wm));
2174                 wc->item=wm->item;
2175                 wc->selection_id=wm->selection_id;
2176         }
2177         if (display_house_numbers) {
2178                 gui_internal_widget_append(w,
2179                         wc=gui_internal_button_new_with_callback(this, _("House numbers"),
2180                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2181                                 gui_internal_search_house_number_in_street, wm));
2182                 wc->item=wm->item;
2183                 wc->selection_id=wm->selection_id;
2184         }
2185         if (display_attributes) {
2186                 struct map_rect *mr;
2187                 struct item *item;
2188                 struct attr attr;
2189                 mr=map_rect_new(wm->item.map, NULL);
2190                 item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
2191                 if (item) {
2192                         if (item_attr_get(item, attr_description, &attr))
2193                                 gui_internal_widget_append(w, gui_internal_label_new(this, attr.u.str));
2194                         if (item_attr_get(item, attr_url_local, &attr)) {
2195                                 gui_internal_widget_append(w,
2196                                         wb=gui_internal_button_new_with_callback(this, _("View in Browser"),
2197                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2198                                                 gui_internal_cmd_view_in_browser, NULL));
2199                                 wb->item=wm->item;
2200                         }
2201                         gui_internal_widget_append(w,
2202                                 wb=gui_internal_button_new_with_callback(this, _("View Attributes"),
2203                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2204                                         gui_internal_cmd_view_attributes, NULL));
2205                         wb->item=wm->item;
2206                 }
2207                 map_rect_destroy(mr);
2208         }
2209         gui_internal_widget_append(w,
2210                 gui_internal_button_new_with_callback(this, _("Set as destination"),
2211                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2212                         gui_internal_cmd_set_destination, wm));
2213         gui_internal_widget_append(w,
2214                 gui_internal_button_new_with_callback(this, _("Set as position"),
2215                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2216                         gui_internal_cmd_set_position, wm));
2217         gui_internal_widget_append(w,
2218                 wbc=gui_internal_button_new_with_callback(this, _("Add as bookmark"),
2219                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2220                         gui_internal_cmd_add_bookmark, wm));
2221         wbc->c=wm->c;
2222         gui_internal_widget_append(w,
2223                 wbc=gui_internal_button_new_with_callback(this, _("POIs"),
2224                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2225                         gui_internal_cmd_pois, NULL));
2226         wbc->c=wm->c;
2227 #if 0
2228         gui_internal_widget_append(w,
2229                 gui_internal_button_new(this, "Add to tour",
2230                         image_new_o(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill));
2231         gui_internal_widget_append(w,
2232                 gui_internal_button_new(this, "Add as bookmark",
2233                         image_new_o(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill));
2234 #endif
2235         if (display_view_on_map) {
2236                 gui_internal_widget_append(w,
2237                         gui_internal_button_new_with_callback(this, _("View on map"),
2238                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2239                                 gui_internal_cmd_view_on_map, wm));
2240         }
2241         if (display_items) {
2242                 int dist=10;
2243                 struct mapset *ms;
2244                 struct mapset_handle *h;
2245                 struct map_rect *mr;
2246                 struct map *m;
2247                 struct item *item;
2248                 struct street_data *data;
2249                 struct map_selection sel;
2250                 struct transformation *trans;
2251                 enum projection pro;
2252                 struct attr attr;
2253                 char *label,*text;
2254
2255                 trans=navit_get_trans(this->nav);
2256                 pro=transform_get_projection(trans);
2257                 transform_from_geo(pro, &g, &c);
2258                 ms=navit_get_mapset(this->nav);
2259                 sel.next=NULL;
2260                 sel.u.c_rect.lu.x=c.x-dist;
2261                 sel.u.c_rect.lu.y=c.y+dist;
2262                 sel.u.c_rect.rl.x=c.x+dist;
2263                 sel.u.c_rect.rl.y=c.y-dist;
2264                 sel.order=18;
2265                 sel.range=item_range_all;
2266                 h=mapset_open(ms);
2267                 while ((m=mapset_next(h,1))) {
2268                         mr=map_rect_new(m, &sel);
2269                         if (! mr)
2270                                 continue;
2271                         while ((item=map_rect_get_item(mr))) {
2272                                 data=street_get_data(item);
2273                                 if (transform_within_dist_item(&c, item->type, data->c, data->count, dist)) {
2274                                         if (item_attr_get(item, attr_label, &attr)) {
2275                                                 label=map_convert_string(m, attr.u.str);
2276                                                 text=g_strdup_printf("%s %s", item_to_name(item->type), label);
2277                                                 map_convert_free(label);
2278                                         } else
2279                                                 text=g_strdup_printf("%s", item_to_name(item->type));
2280                                         gui_internal_widget_append(w,
2281                                                 wc=gui_internal_button_new_with_callback(this, text,
2282                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2283                                                 gui_internal_cmd_position, (void *)2));
2284                                         wc->c.x=data->c[0].x;
2285                                         wc->c.y=data->c[0].y;
2286                                         wc->c.pro=pro;
2287                                         wc->name=g_strdup(text);
2288                                         wc->item=*item;
2289                                         g_free(text);
2290                                 }
2291                                 street_data_free(data);
2292                         }
2293                         map_rect_destroy(mr);
2294                 }
2295                 mapset_close(h);
2296         }
2297
2298         gui_internal_menu_render(this);
2299 }
2300
2301 static void
2302 gui_internal_cmd_bookmarks(struct gui_priv *this, struct widget *wm, void *data)
2303 {
2304         struct attr attr,mattr;
2305         struct map_rect *mr=NULL;
2306         struct item *item;
2307         char *label_full,*l,*prefix,*pos;
2308         int len,plen,hassub;
2309         struct widget *wb,*w,*wbm;
2310         GHashTable *hash;
2311         struct coord c;
2312
2313
2314         wb=gui_internal_menu(this, wm->text ? wm->text : _("Bookmarks"));
2315         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2316         w->spy=this->spacing*3;
2317         gui_internal_widget_append(wb, w);
2318
2319         prefix=wm->prefix;
2320         if (! prefix)
2321                 prefix="";
2322         plen=strlen(prefix);
2323
2324         if(navit_get_attr(this->nav, attr_bookmark_map, &mattr, NULL) && mattr.u.map && (mr=map_rect_new(mattr.u.map, NULL))) {
2325                 hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
2326                 while ((item=map_rect_get_item(mr))) {
2327                         if (item->type != type_bookmark) continue;
2328                         if (!item_attr_get(item, attr_label, &attr)) continue;
2329                         label_full=attr.u.str;
2330                         if (!strncmp(label_full, prefix, plen)) {
2331                                 pos=strchr(label_full+plen, '/');
2332                                 if (pos) {
2333                                         hassub=1;
2334                                         len=pos-label_full;
2335                                 } else {
2336                                         hassub=0;
2337                                         len=strlen(label_full);
2338                                 }
2339                                 l=g_malloc(len-plen+1);
2340                                 strncpy(l, label_full+plen, len-plen);
2341                                 l[len-plen]='\0';
2342                                 if (!g_hash_table_lookup(hash, l)) {
2343                                         wbm=gui_internal_button_new_with_callback(this, l,
2344                                                 image_new_xs(this, hassub ? "gui_inactive" : "gui_active" ), gravity_left_center|orientation_horizontal|flags_fill,
2345                                                         hassub ? gui_internal_cmd_bookmarks : gui_internal_cmd_position, NULL);
2346                                         if (item_coord_get(item, &c, 1)) {
2347                                                 wbm->c.x=c.x;
2348                                                 wbm->c.y=c.y;
2349                                                 wbm->c.pro=map_projection(mattr.u.map);
2350                                                 wbm->name=g_strdup_printf(_("Bookmark %s"),label_full);
2351                                                 wbm->text=g_strdup(l);
2352                                                 gui_internal_widget_append(w, wbm);
2353                                                 g_hash_table_insert(hash, g_strdup(l), (void *)1);
2354                                                 wbm->prefix=g_malloc(len+2);
2355                                                 strncpy(wbm->prefix, label_full, len+1);
2356                                                 wbm->prefix[len+1]='\0';
2357                                         } else {
2358                                                 gui_internal_widget_destroy(this, wbm);
2359                                         }
2360                                 }
2361                                 g_free(l);
2362                         }
2363
2364                 }
2365                 g_hash_table_destroy(hash);
2366         }
2367         gui_internal_menu_render(this);
2368 }
2369
2370 static void gui_internal_keypress_do(struct gui_priv *this, char *key)
2371 {
2372         struct widget *wi,*menu;
2373         int len=0;
2374         char *text=NULL;
2375
2376         menu=g_list_last(this->root.children)->data;
2377         wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
2378         if (wi) {
2379                 if (*key == NAVIT_KEY_BACKSPACE) {
2380                         dbg(0,"backspace\n");
2381                         if (wi->text && wi->text[0]) {
2382                                 len=g_utf8_prev_char(wi->text+strlen(wi->text))-wi->text;
2383                                 wi->text[len]=' ';
2384                                 text=g_strdup_printf("%s ", wi->text);
2385                         }
2386                 } else {
2387                         if (wi->state & STATE_CLEAR) {
2388                                 dbg(0,"wi->state=0x%x\n", wi->state);
2389                                 g_free(wi->text);
2390                                 wi->text=NULL;
2391                                 wi->state &= ~STATE_CLEAR;
2392                                 dbg(0,"wi->state=0x%x\n", wi->state);
2393                         }
2394                         text=g_strdup_printf("%s%s", wi->text ? wi->text : "", key);
2395                 }
2396                 g_free(wi->text);
2397                 wi->text=text;
2398                 if (*key == NAVIT_KEY_BACKSPACE && wi->text) {
2399                         gui_internal_widget_render(this, wi);
2400                         wi->text[len]='\0';
2401                 }
2402                 if (wi->func) {
2403                         wi->reason=2;
2404                         wi->func(this, wi, wi->data);
2405                 }
2406                 gui_internal_widget_render(this, wi);
2407         }
2408 }
2409
2410
2411 static void
2412 gui_internal_cmd_keypress(struct gui_priv *this, struct widget *wm, void *data)
2413 {
2414         struct menu_data *md=gui_internal_menu_data(this);
2415         gui_internal_keypress_do(this, (char *) wm->data);
2416         if (md->keyboard_mode == 2)
2417                 gui_internal_keyboard_do(this, md->keyboard, 10);
2418         if (md->keyboard_mode == 26)
2419                 gui_internal_keyboard_do(this, md->keyboard, 34);
2420 }
2421
2422 static void
2423 gui_internal_search_idle_end(struct gui_priv *this)
2424 {
2425         if (this->idle) {
2426                 event_remove_idle(this->idle);
2427                 this->idle=NULL;
2428         }
2429         if (this->idle_cb) {
2430                 callback_destroy(this->idle_cb);
2431                 this->idle_cb=NULL;
2432         }
2433 }
2434
2435 static void
2436 gui_internal_search_idle(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
2437 {
2438         char *text=NULL,*name=NULL;
2439         struct search_list_result *res;
2440         struct widget *wc;
2441         struct item *item=NULL;
2442         GList *l;
2443         static char possible_keys[256]="";
2444
2445         res=search_list_get_result(this->sl);
2446         if (res) {
2447                 gchar* trunk_name = NULL;
2448
2449                 struct widget *menu=g_list_last(this->root.children)->data;
2450                 struct widget *wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
2451
2452                 if (! strcmp(wm_name,"Town"))
2453                         trunk_name = g_strrstr(res->town->name, wi->text);
2454                 if (! strcmp(wm_name,"Street"))
2455                         trunk_name = g_strrstr(name=res->street->name, wi->text);
2456
2457                 if (trunk_name) {
2458                         char next_char = trunk_name[strlen(wi->text)];
2459                         int i;
2460                         int len = strlen(possible_keys);
2461                         for(i = 0; (i<len) && (possible_keys[i] != next_char) ;i++) ;
2462                         if (i==len || !len) {
2463                                 possible_keys[len]=trunk_name[strlen(wi->text)];
2464                                 possible_keys[len+1]='\0';
2465
2466                         }
2467                         dbg(1,"%s %s possible_keys:%s \n", wi->text, res->town->name, possible_keys);
2468                 }
2469         }
2470
2471         if (! res) {
2472                 gui_internal_search_idle_end(this);
2473
2474                 struct menu_data *md=gui_internal_menu_data(this);
2475                 if (md && md->keyboard) {
2476                         GList *lk=md->keyboard->children;
2477                         graphics_draw_mode(this->gra, draw_mode_begin);
2478                         while (lk) {
2479                                 struct widget *child=lk->data;
2480                                 GList *lk2=child->children;
2481                                 while (lk2) {
2482                                         struct widget *child_=lk2->data;
2483                                         lk2=g_list_next(lk2);
2484                                         if (child_->data && strcmp("\b", child_->data)) { // FIXME don't disable special keys
2485                                                 if (strlen(possible_keys) == 0)
2486                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
2487                                                 else if (g_strrstr(possible_keys, child_->data)!=NULL ) {
2488                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
2489                                                 } else {
2490                                                         child_->state&= ~(STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SELECTED) ;
2491                                                 }
2492                                                 gui_internal_widget_render(this,child_);
2493                                         }
2494                                 }
2495                                 lk=g_list_next(lk);
2496                         }
2497                         gui_internal_widget_render(this,md->keyboard);
2498                         graphics_draw_mode(this->gra, draw_mode_end);
2499                 }
2500
2501                 possible_keys[0]='\0';
2502                 return;
2503         }
2504
2505         if (! strcmp(wm_name,"Country")) {
2506                 name=res->country->name;
2507                 item=&res->country->common.item;
2508                 text=g_strdup_printf("%s", res->country->name);
2509         }
2510         if (! strcmp(wm_name,"Town")) {
2511                 name=res->town->name;
2512                 item=&res->town->common.item;
2513                 if (res->town->name && res->town->district)
2514                         text=g_strdup_printf("%s%s%s (%s)", res->town->common.postal_mask ? res->town->common.postal_mask : "", res->town->common.postal_mask ? " ":"", res->town->name, res->town->district);
2515                 else
2516                         text=g_strdup_printf("%s%s%s", res->town->common.postal ? res->town->common.postal_mask : "", res->town->common.postal_mask ? " ":"", res->town->name);
2517         }
2518         if (! strcmp(wm_name,"Street")) {
2519                 name=res->street->name;
2520                 item=&res->street->common.item;
2521                 text=g_strdup_printf("%s %s", res->town->name, res->street->name);
2522         }
2523         dbg(1,"res->country->flag=%s\n", res->country->flag);
2524                 gui_internal_widget_append(search_list,
2525                                 wc=gui_internal_button_new_with_callback(this, text,
2526                                         image_new_xs(this, res->country->flag),
2527                                         gravity_left_center|orientation_horizontal|flags_fill,
2528                                         gui_internal_cmd_position, param));
2529                 wc->name=g_strdup(name);
2530                 if (res->c)
2531                         wc->c=*res->c;
2532                 wc->selection_id=res->id;
2533                 if (item)
2534                         wc->item=*item;
2535                 gui_internal_widget_pack(this, search_list);
2536                 l=g_list_last(this->root.children);
2537                 graphics_draw_mode(this->gra, draw_mode_begin);
2538                 gui_internal_widget_render(this, l->data);
2539                 graphics_draw_mode(this->gra, draw_mode_end);
2540         g_free(text);
2541 }
2542
2543 static void
2544 gui_internal_search_idle_start(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
2545 {
2546         this->idle_cb=callback_new_4(callback_cast(gui_internal_search_idle), this, wm_name, search_list, param);
2547         this->idle=event_add_idle(50,this->idle_cb);
2548         callback_call_0(this->idle_cb);
2549 }
2550
2551
2552 /**
2553  *
2554  * @param wm The widget that generated the event for the search changed,
2555  *        if this was generated by a key on the virtual keyboard then
2556  *        wm is the key button widget.
2557  */
2558 static void
2559 gui_internal_search_changed(struct gui_priv *this, struct widget *wm, void *data)
2560 {
2561         GList *l;
2562         struct widget *search_list=gui_internal_menu_data(this)->search_list;
2563         gui_internal_widget_children_destroy(this, search_list);
2564
2565         void *param=(void *)3;
2566         int minlen=1;
2567         if (! strcmp(wm->name,"Country")) 
2568                 param=(void *)4;
2569         if (! strcmp(wm->name,"Street")) 
2570                 param=(void *)5;
2571         dbg(0,"%s now '%s'\n", wm->name, wm->text);
2572
2573         gui_internal_search_idle_end(this);
2574         if (wm->text && g_utf8_strlen(wm->text, -1) >= minlen) {
2575                 struct attr search_attr;
2576
2577                 dbg(0,"process\n");
2578                 if (! strcmp(wm->name,"Country"))
2579                         search_attr.type=attr_country_all;
2580                 if (! strcmp(wm->name,"Town"))
2581                         search_attr.type=attr_town_or_district_name;
2582                 if (! strcmp(wm->name,"Street"))
2583                         search_attr.type=attr_street_name;
2584                 if (! strcmp(wm->name,"House number"))
2585                         search_attr.type=attr_house_number;
2586                 search_attr.u.str=wm->text;
2587                 search_list_search(this->sl, &search_attr, 1);
2588                 gui_internal_search_idle_start(this, wm->name, search_list, param);
2589         }
2590         l=g_list_last(this->root.children);
2591         gui_internal_widget_render(this, l->data);
2592 }
2593
2594 static struct widget *
2595 gui_internal_keyboard_key_data(struct gui_priv *this, struct widget *wkbd, char *text, void(*func)(struct gui_priv *priv, struct widget *widget, void *data), void *data, void (*data_free)(void *data), int w, int h)
2596 {
2597         struct widget *wk;
2598         gui_internal_widget_append(wkbd, wk=gui_internal_button_new_with_callback(this, text,
2599                 NULL, gravity_center|orientation_vertical, func, data));
2600         wk->data_free=data_free;
2601         wk->background=this->background;
2602         wk->bl=w/2;
2603         wk->br=0;
2604         wk->bt=h/3;
2605         wk->bb=0;
2606         return wk;
2607 }
2608
2609 static struct widget *
2610 gui_internal_keyboard_key(struct gui_priv *this, struct widget *wkbd, char *text, char *key, int w, int h)
2611 {
2612         return gui_internal_keyboard_key_data(this, wkbd, text, gui_internal_cmd_keypress, g_strdup(key), g_free,w,h);
2613 }
2614
2615 static void gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data);
2616
2617 // Some macros that make the keyboard layout easier to visualise in
2618 // the source code. The macros are #undef'd after this function.
2619 #define KEY(x) gui_internal_keyboard_key(this, wkbd, (x), (x), max_w, max_h)
2620 #define SPACER() gui_internal_keyboard_key_data(this, wkbd, "", NULL, NULL, NULL,max_w,max_h)
2621 static struct widget *
2622 gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode)
2623 {
2624         struct widget *wkbd,*wk;
2625         struct menu_data *md=gui_internal_menu_data(this);
2626         int i, max_w=this->root.w, max_h=this->root.h;
2627         int render=0;
2628
2629         if (wkbdb) {
2630                 this->current.x=-1;
2631                 this->current.y=-1;
2632                 gui_internal_highlight(this);
2633                 render=1;
2634                 gui_internal_widget_children_destroy(this, wkbdb);
2635         } else
2636                 wkbdb=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
2637         md->keyboard=wkbdb;
2638         md->keyboard_mode=mode;
2639         wkbd=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
2640         wkbd->background=this->background;
2641         wkbd->cols=8;
2642         wkbd->spx=3;
2643         wkbd->spy=3;
2644         max_w=max_w/9;
2645         max_h=max_h/6;
2646
2647         if (mode >= 0 && mode < 8) {
2648                 for (i = 0 ; i < 26 ; i++) {
2649                         char text[]={'A'+i,'\0'};
2650                         KEY(text);
2651                 }
2652                 gui_internal_keyboard_key(this, wkbd, "_"," ",max_w,max_h);
2653                 if (mode == 0) {
2654                         KEY("-");
2655                         KEY("'");
2656                         SPACER();
2657                 } else {
2658                         SPACER();
2659                         wk=gui_internal_keyboard_key_data(this, wkbd, "a", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2660                         wk->datai=mode+8;
2661                         wk=gui_internal_keyboard_key_data(this, wkbd, "1", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2662                         wk->datai=mode+16;
2663                 }
2664                 wk=gui_internal_keyboard_key_data(this, wkbd, "Ä",gui_internal_keyboard_change, wkbdb,NULL,max_w,max_h);
2665                 wk->datai=mode+24;
2666                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2667         }
2668         if (mode >= 8 && mode < 16) {
2669                 for (i = 0 ; i < 26 ; i++) {
2670                         char text[]={'a'+i,'\0'};
2671                         KEY(text);
2672                 }
2673                 gui_internal_keyboard_key(this, wkbd, "_"," ",max_w,max_h);
2674                 if (mode == 8) {
2675                         KEY("-");
2676                         KEY("'");
2677                         SPACER();
2678                 } else {
2679                         SPACER();
2680                         wk=gui_internal_keyboard_key_data(this, wkbd, "A", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2681                         wk->datai=mode-8;
2682                         wk=gui_internal_keyboard_key_data(this, wkbd, "1", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2683                         wk->datai=mode+8;
2684                 }
2685                 wk=gui_internal_keyboard_key_data(this, wkbd, "ä",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2686                 wk->datai=mode+24;
2687                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2688         }
2689         if (mode >= 16 && mode < 24) {
2690                 for (i = 0 ; i < 10 ; i++) {
2691                         char text[]={'0'+i,'\0'};
2692                         KEY(text);
2693                 }
2694                 KEY("."); KEY("°"); KEY("'"); KEY("\""); KEY("-"); KEY("+");
2695                 KEY("*"); KEY("/"); KEY("("); KEY(")"); KEY("="); KEY("?");
2696
2697                 for (i = 0 ; i < 5 ; i++) SPACER();
2698
2699                 if (mode == 16) {
2700                         KEY("-");
2701                         KEY("'");
2702                         SPACER();
2703                 } else {
2704                         SPACER();
2705                         wk=gui_internal_keyboard_key_data(this, wkbd, "A", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2706                         wk->datai=mode-16;
2707                         wk=gui_internal_keyboard_key_data(this, wkbd, "a", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2708                         wk->datai=mode-8;
2709                 }
2710                 wk=gui_internal_keyboard_key_data(this, wkbd, "Ä",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2711                 wk->datai=mode+8;
2712                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2713         }
2714         if (mode >= 24 && mode < 32) {
2715                 KEY("Ä"); KEY("Ë"); KEY("Ï"); KEY("Ö"); KEY("Ü"); KEY("Æ"); KEY("Ø"); KEY("Å");
2716                 KEY("Á"); KEY("É"); KEY("Í"); KEY("Ó"); KEY("Ú"); KEY("Š"); KEY("Č"); KEY("Ž");
2717                 KEY("À"); KEY("È"); KEY("Ì"); KEY("Ò"); KEY("Ù"); KEY("Ś"); KEY("Ć"); KEY("Ź");
2718                 KEY("Â"); KEY("Ê"); KEY("Î"); KEY("Ô"); KEY("Û"); SPACER();
2719
2720                 wk=gui_internal_keyboard_key_data(this, wkbd, "A",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2721                 wk->datai=mode-24;
2722
2723                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2724         }
2725         if (mode >= 32 && mode < 40) {
2726                 KEY("ä"); KEY("ë"); KEY("ï"); KEY("ö"); KEY("ü"); KEY("æ"); KEY("ø"); KEY("å");
2727                 KEY("á"); KEY("é"); KEY("í"); KEY("ó"); KEY("ú"); KEY("š"); KEY("č"); KEY("ž");
2728                 KEY("à"); KEY("è"); KEY("ì"); KEY("ò"); KEY("ù"); KEY("ś"); KEY("ć"); KEY("ź");
2729                 KEY("â"); KEY("ê"); KEY("î"); KEY("ô"); KEY("û"); KEY("ß");
2730
2731                 wk=gui_internal_keyboard_key_data(this, wkbd, "a",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2732                 wk->datai=mode-24;
2733
2734                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2735         }
2736         gui_internal_widget_append(wkbdb, wkbd);
2737         if (render) {
2738                 gui_internal_widget_pack(this, wkbdb);
2739                 gui_internal_widget_render(this, wkbdb);
2740         }
2741         return wkbdb;
2742 }
2743 #undef KEY
2744 #undef SPACER
2745
2746 static struct widget *
2747 gui_internal_keyboard(struct gui_priv *this, int mode)
2748 {
2749         if (! this->keyboard)
2750                 return NULL;
2751         return gui_internal_keyboard_do(this, NULL, mode);
2752 }
2753
2754 static void
2755 gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data)
2756 {
2757         gui_internal_keyboard_do(this, key->data, key->datai);
2758 }
2759
2760 static void
2761 gui_internal_search_list_set_default_country(struct gui_priv *this)
2762 {
2763         struct attr search_attr, country_name, country_iso2, *country_attr;
2764         struct item *item;
2765         struct country_search *cs;
2766         struct tracking *tracking;
2767         struct search_list_result *res;
2768
2769         country_attr=country_default();
2770         tracking=navit_get_tracking(this->nav);
2771         if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
2772                 country_attr=&search_attr;
2773         if (country_attr) {
2774                 cs=country_search_new(country_attr, 0);
2775                 item=country_search_get_item(cs);
2776                 if (item && item_attr_get(item, attr_country_name, &country_name)) {
2777                         search_attr.type=attr_country_all;
2778                         dbg(0,"country %s\n", country_name.u.str);
2779                         search_attr.u.str=country_name.u.str;
2780                         search_list_search(this->sl, &search_attr, 0);
2781                         while((res=search_list_get_result(this->sl)));
2782                         g_free(this->country_iso2);
2783                         if (item_attr_get(item, attr_country_iso2, &country_iso2))
2784                                 this->country_iso2=g_strdup(country_iso2.u.str);
2785                 }
2786                 country_search_destroy(cs);
2787         } else {
2788                 dbg(0,"warning: no default country found\n");
2789                 if (this->country_iso2) {
2790                     dbg(0,"attempting to use country '%s'\n",this->country_iso2);
2791                     search_attr.type=attr_country_iso2;
2792                     search_attr.u.str=this->country_iso2;
2793             search_list_search(this->sl, &search_attr, 0);
2794             while((res=search_list_get_result(this->sl)));
2795                 }
2796         }
2797 }
2798
2799 static void
2800 gui_internal_search_list_new(struct gui_priv *this)
2801 {
2802         struct mapset *ms=navit_get_mapset(this->nav);
2803         if (! this->sl) {
2804                 this->sl=search_list_new(ms);
2805                 gui_internal_search_list_set_default_country(this);
2806         }
2807 }
2808
2809 static void
2810 gui_internal_search_list_destroy(struct gui_priv *this)
2811 {
2812         if (this->sl) {
2813                 search_list_destroy(this->sl);
2814                 this->sl=NULL;
2815         }
2816 }
2817
2818
2819 static void
2820 gui_internal_search(struct gui_priv *this, char *what, char *type, int flags)
2821 {
2822         struct widget *wb,*wk,*w,*wr,*we,*wl,*wnext=NULL;
2823         char *country;
2824         gui_internal_search_list_new(this);
2825         wb=gui_internal_menu(this, what);
2826         w=gui_internal_box_new(this, gravity_center|orientation_vertical|flags_expand|flags_fill);
2827         gui_internal_widget_append(wb, w);
2828         wr=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2829         gui_internal_widget_append(w, wr);
2830         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2831         gui_internal_widget_append(wr, we);
2832
2833         if (!strcmp(type,"Country")) {
2834                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_town"));
2835                 wnext->func=gui_internal_search_town;
2836         } else if (!strcmp(type,"Town")) {
2837                 if (this->country_iso2) {
2838 #if HAVE_API_ANDROID
2839                         char country_iso2[strlen(this->country_iso2)+1];
2840                         strtolower(country_iso2, this->country_iso2);
2841                         country=g_strdup_printf("country_%s", country_iso2);
2842 #else
2843                         country=g_strdup_printf("country_%s", this->country_iso2);
2844 #endif
2845                 } else
2846                         country=strdup("gui_select_country");
2847                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, country)));
2848                 wb->state |= STATE_SENSITIVE;
2849                 if (flags)
2850                         wb->func = gui_internal_search_country;
2851                 else
2852                         wb->func = gui_internal_back;
2853                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_street"));
2854                 wnext->func=gui_internal_search_street;
2855                 g_free(country);
2856         } else if (!strcmp(type,"Street")) {
2857                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_town")));
2858                 wb->state |= STATE_SENSITIVE;
2859                 wb->func = gui_internal_back;
2860                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_house_number"));
2861                 wnext->func=gui_internal_search_house_number;
2862         } else if (!strcmp(type,"House number")) {
2863                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_street")));
2864                 wb->state |= STATE_SENSITIVE;
2865                 wb->func = gui_internal_back;
2866         }
2867         gui_internal_widget_append(we, wk=gui_internal_label_new(this, NULL));
2868         if (wnext) {
2869                 gui_internal_widget_append(we, wnext);
2870                 wnext->state |= STATE_SENSITIVE;
2871         }
2872         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2873         gui_internal_widget_append(wr, wl);
2874         gui_internal_menu_data(this)->search_list=wl;
2875         wk->state |= STATE_EDIT;
2876         wk->background=this->background;
2877         wk->flags |= flags_expand|flags_fill;
2878         wk->func = gui_internal_search_changed;
2879         wk->name=g_strdup(type);
2880         if (this->keyboard)
2881                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
2882         gui_internal_menu_render(this);
2883 }
2884
2885 static void
2886 gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data)
2887 {
2888         search_list_select(this->sl, attr_street_name, 0, 0);
2889         gui_internal_search(this,_("House number"),"House number",0);
2890 }
2891
2892 static void
2893 gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data)
2894 {
2895         dbg(0,"id %d\n", widget->selection_id);
2896         search_list_select(this->sl, attr_street_name, 0, 0);
2897         search_list_select(this->sl, attr_street_name, widget->selection_id, 1);
2898         gui_internal_search(this,_("House number"),"House number",0);
2899 }
2900
2901 static void
2902 gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data)
2903 {
2904         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
2905         gui_internal_search(this,_("Street"),"Street",0);
2906 }
2907
2908 static void
2909 gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data)
2910 {
2911         dbg(0,"id %d\n", widget->selection_id);
2912         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
2913         search_list_select(this->sl, attr_town_or_district_name, widget->selection_id, 1);
2914         gui_internal_search(this,_("Street"),"Street",0);
2915 }
2916
2917 static void
2918 gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data)
2919 {
2920         if (this->sl)
2921                 search_list_select(this->sl, attr_country_all, 0, 0);
2922         g_free(this->country_iso2);
2923         this->country_iso2=NULL;
2924         gui_internal_search(this,_("Town"),"Town",0);
2925 }
2926
2927 static void
2928 gui_internal_search_town_in_country(struct gui_priv *this, struct widget *widget)
2929 {
2930         struct search_list_common *slc;
2931         dbg(0,"id %d\n", widget->selection_id);
2932         search_list_select(this->sl, attr_country_all, 0, 0);
2933         slc=search_list_select(this->sl, attr_country_all, widget->selection_id, 1);
2934         if (slc) {
2935                 g_free(this->country_iso2);
2936                 this->country_iso2=((struct search_list_country *)slc)->iso2;
2937         }
2938         gui_internal_search(this,widget->name,"Town",0);
2939 }
2940
2941 static void
2942 gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data)
2943 {
2944         gui_internal_prune_menu_count(this, 1, 0);
2945         gui_internal_search(this,_("Country"),"Country",0);
2946 }
2947
2948 static void
2949 gui_internal_cmd_town(struct gui_priv *this, struct widget *wm, void *data)
2950 {
2951         if (this->sl)
2952                 search_list_select(this->sl, attr_country_all, 0, 0);
2953         gui_internal_search(this,_("Town"),"Town",1);
2954 }
2955
2956 static void
2957 gui_internal_cmd_layout(struct gui_priv *this, struct widget *wm, void *data)
2958 {
2959         struct attr attr;
2960         struct widget *w,*wb,*wl;
2961         struct attr_iter *iter;
2962
2963
2964         wb=gui_internal_menu(this, _("Layout"));
2965         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2966         w->spy=this->spacing*3;
2967         gui_internal_widget_append(wb, w);
2968         iter=navit_attr_iter_new();
2969         while(navit_get_attr(this->nav, attr_layout, &attr, iter)) {
2970                 wl=gui_internal_button_navit_attr_new(this, attr.u.layout->name, gravity_left_center|orientation_horizontal|flags_fill,
2971                         &attr, NULL);
2972                 gui_internal_widget_append(w, wl);
2973         }
2974         navit_attr_iter_destroy(iter);
2975         gui_internal_menu_render(this);
2976 }
2977
2978 static void
2979 gui_internal_cmd_fullscreen(struct gui_priv *this, struct widget *wm, void *data)
2980 {
2981         graphics_draw_mode(this->gra, draw_mode_end);
2982         if (this->fullscreen != 2)
2983                 this->fullscreen=!this->fullscreen;
2984         this->win->fullscreen(this->win, this->fullscreen != 0);
2985         graphics_draw_mode(this->gra, draw_mode_begin);
2986 }
2987
2988 static void
2989 gui_internal_cmd_2d(struct gui_priv *this, struct widget *wm, void *data)
2990 {
2991         struct transformation *trans=navit_get_trans(this->nav);
2992         transform_set_pitch(trans, 0);
2993         this->redraw=1;
2994         gui_internal_prune_menu(this, NULL);
2995 }
2996
2997 static void
2998 gui_internal_cmd_3d(struct gui_priv *this, struct widget *wm, void *data)
2999 {
3000         struct transformation *trans=navit_get_trans(this->nav);
3001         transform_set_pitch(trans, this->pitch);
3002         this->redraw=1;
3003         gui_internal_prune_menu(this, NULL);
3004 }
3005
3006 static void
3007 gui_internal_cmd_display(struct gui_priv *this, struct widget *wm, void *data)
3008 {
3009         struct widget *w;
3010         struct transformation *trans;
3011
3012         w=gui_internal_menu(this, _("Display"));
3013         gui_internal_widget_append(w,
3014                 gui_internal_button_new_with_callback(this, _("Layout"),
3015                         image_new_l(this, "gui_display"), gravity_center|orientation_vertical,
3016                         gui_internal_cmd_layout, NULL));
3017
3018         if(this->fullscreen != 2) {
3019                 if (this->fullscreen) {
3020                         gui_internal_widget_append(w,
3021                                         gui_internal_button_new_with_callback(this, _("Window Mode"),
3022                                                 image_new_l(this, "gui_leave_fullscreen"), gravity_center|orientation_vertical,
3023                                                 gui_internal_cmd_fullscreen, NULL));
3024                 } else {
3025                         gui_internal_widget_append(w,
3026                                         gui_internal_button_new_with_callback(this, _("Fullscreen"),
3027                                                 image_new_l(this, "gui_fullscreen"), gravity_center|orientation_vertical,
3028                                                 gui_internal_cmd_fullscreen, NULL));
3029                 }
3030         }
3031         trans=navit_get_trans(this->nav);
3032         if (transform_get_pitch(trans)) {
3033                 gui_internal_widget_append(w,
3034                         gui_internal_button_new_with_callback(this, _("2D"),
3035                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
3036                                 gui_internal_cmd_2d, NULL));
3037
3038         } else {
3039                 gui_internal_widget_append(w,
3040                         gui_internal_button_new_with_callback(this, _("3D"),
3041                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
3042                                 gui_internal_cmd_3d, NULL));
3043         }
3044         gui_internal_menu_render(this);
3045 }
3046
3047 static void
3048 gui_internal_cmd_quit(struct gui_priv *this, struct widget *wm, void *data)
3049 {
3050         struct attr navit;
3051         navit.type=attr_navit;
3052         navit.u.navit=this->nav;
3053         navit_destroy(navit.u.navit);
3054         config_remove_attr(config, &navit);
3055         event_main_loop_quit();
3056 }
3057
3058 static void
3059 gui_internal_cmd_abort_navigation(struct gui_priv *this, struct widget *wm, void *data)
3060 {
3061         navit_set_destination(this->nav, NULL, NULL, 0);
3062 }
3063
3064
3065 static void
3066 gui_internal_cmd_actions(struct gui_priv *this, struct widget *wm, void *data)
3067 {
3068         struct widget *w,*wc;
3069         char *coord;
3070
3071         w=gui_internal_menu(this, _("Actions"));
3072         gui_internal_widget_append(w,
3073                 gui_internal_button_new_with_callback(this, _("Bookmarks"),
3074                         image_new_l(this, "gui_bookmark"), gravity_center|orientation_vertical,
3075                         gui_internal_cmd_bookmarks, NULL));
3076         if (this->clickp_valid) {
3077                 coord=coordinates(&this->clickp, '\n');
3078                 gui_internal_widget_append(w,
3079                         wc=gui_internal_button_new_with_callback(this, coord,
3080                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
3081                                 gui_internal_cmd_position, (void *)1));
3082                 wc->name=g_strdup(_("Map Point"));
3083                 wc->c=this->clickp;
3084                 g_free(coord);
3085         }
3086         if (this->vehicle_valid) {
3087                 coord=coordinates(&this->vehiclep, '\n');
3088                 gui_internal_widget_append(w,
3089                         wc=gui_internal_button_new_with_callback(this, coord,
3090                                 image_new_l(this, "gui_vehicle"), gravity_center|orientation_vertical,
3091                                 gui_internal_cmd_position, NULL));
3092                 wc->name=g_strdup(_("Vehicle Position"));
3093                 wc->c=this->vehiclep;
3094                 g_free(coord);
3095         }
3096         gui_internal_widget_append(w,
3097                 gui_internal_button_new_with_callback(this, _("Town"),
3098                         image_new_l(this, "gui_town"), gravity_center|orientation_vertical,
3099                         gui_internal_cmd_town, NULL));
3100         gui_internal_widget_append(w,
3101                 gui_internal_button_new_with_callback(this, _("Quit"),
3102                         image_new_l(this, "gui_quit"), gravity_center|orientation_vertical,
3103                         gui_internal_cmd_quit, NULL));
3104
3105         if (navit_check_route(this->nav)) {
3106                 gui_internal_widget_append(w,
3107                                                                    gui_internal_button_new_with_callback(this, _("Stop\nNavigation"),
3108                                                                  image_new_l(this, "gui_stop"), gravity_center|orientation_vertical,
3109                                                                  gui_internal_cmd_abort_navigation, NULL));
3110         }
3111         gui_internal_menu_render(this);
3112 }
3113
3114 static void
3115 gui_internal_cmd_maps(struct gui_priv *this, struct widget *wm, void *wdata)
3116 {
3117         struct attr attr, on, off, description, type, data;
3118         struct widget *w,*wb,*wma;
3119         char *label;
3120         struct attr_iter *iter;
3121
3122
3123         wb=gui_internal_menu(this, _("Maps"));
3124         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3125         w->spy=this->spacing*3;
3126         gui_internal_widget_append(wb, w);
3127         iter=navit_attr_iter_new();
3128         on.type=off.type=attr_active;
3129         on.u.num=1;
3130         off.u.num=0;
3131         while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
3132                 if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
3133                         label=g_strdup(description.u.str);
3134                 } else {
3135                         if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
3136                                 type.u.str="";
3137                         if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
3138                                 data.u.str="";
3139                         label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
3140                 }
3141                 wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
3142                         attr.u.map, &on, &off, 1);
3143                 gui_internal_widget_append(w, wma);
3144                 g_free(label);
3145         }
3146         navit_attr_iter_destroy(iter);
3147         gui_internal_menu_render(this);
3148
3149 }
3150 static void
3151 gui_internal_cmd_set_active_vehicle(struct gui_priv *this, struct widget *wm, void *data)
3152 {
3153         struct attr vehicle = {attr_vehicle,{wm->data}};
3154         navit_set_attr(this->nav, &vehicle);
3155 }
3156
3157 static void
3158 gui_internal_cmd_show_satellite_status(struct gui_priv *this, struct widget *wm, void *data)
3159 {
3160         struct widget *w,*wb,*row;
3161         struct attr attr,sat_attr;
3162         struct vehicle *v=wm->data;
3163         char *str;
3164         int i;
3165         enum attr_type types[]={attr_sat_prn, attr_sat_elevation, attr_sat_azimuth, attr_sat_snr};
3166
3167         wb=gui_internal_menu(this, _("Show Satellite Status"));
3168         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_satellite_status;
3169         gui_internal_menu_data(this)->redisplay_widget=wm;
3170         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3171         gui_internal_widget_append(wb, w);
3172         w = gui_internal_widget_table_new(this,gravity_center | orientation_vertical | flags_expand | flags_fill, 0);
3173         row = gui_internal_widget_table_row_new(this,gravity_left_top);
3174         gui_internal_widget_append(row, gui_internal_label_new(this, _(" PRN ")));
3175         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Elevation ")));
3176         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Azimuth ")));
3177         gui_internal_widget_append(row, gui_internal_label_new(this, _(" SNR ")));
3178         gui_internal_widget_append(w,row);
3179         while (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
3180                 row = gui_internal_widget_table_row_new(this,gravity_left_top);
3181                 for (i = 0 ; i < sizeof(types)/sizeof(enum attr_type) ; i++) {
3182                         if (item_attr_get(attr.u.item, types[i], &sat_attr))
3183                                 str=g_strdup_printf("%d", sat_attr.u.num);
3184                         else
3185                                 str=g_strdup("");
3186                         gui_internal_widget_append(row, gui_internal_label_new(this, str));
3187                         g_free(str);
3188                 }
3189                 gui_internal_widget_append(w,row);
3190         }
3191         gui_internal_widget_append(wb, w);
3192         gui_internal_menu_render(this);
3193 }
3194
3195 static void
3196 gui_internal_cmd_show_nmea_data(struct gui_priv *this, struct widget *wm, void *data)
3197 {
3198         struct widget *w,*wb;
3199         struct attr attr;
3200         struct vehicle *v=wm->data;
3201         wb=gui_internal_menu(this, _("Show NMEA Data"));
3202         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_nmea_data;
3203         gui_internal_menu_data(this)->redisplay_widget=wm;
3204         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3205         gui_internal_widget_append(wb, w);
3206         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL))
3207                 gui_internal_widget_append(w, gui_internal_text_new(this, attr.u.str, gravity_left_center|orientation_vertical));
3208         gui_internal_menu_render(this);
3209 }
3210
3211 /**
3212  * A container to hold the selected vehicle and the desired profile in
3213  * one data item.
3214  */
3215 struct vehicle_and_profilename {
3216         struct vehicle *vehicle;
3217         char *profilename;
3218 };
3219
3220 /**
3221  * Figures out whether the given vehicle is the active vehicle.
3222  *
3223  * @return true if the vehicle is active, false otherwise.
3224  */
3225 static int
3226 gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle
3227         *vehicle)
3228 {
3229         struct attr active_vehicle;
3230
3231         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
3232         active_vehicle.u.vehicle=NULL;
3233
3234         return active_vehicle.u.vehicle == vehicle;
3235 }
3236
3237 static void
3238 save_vehicle_xml(struct vehicle *v)
3239 {
3240         struct attr attr;
3241         struct attr_iter *iter=vehicle_attr_iter_new();
3242         int childs=0;
3243         dbg(0,"enter\n");
3244         printf("<vehicle");
3245         while (vehicle_get_attr(v, attr_any_xml, &attr, iter)) {
3246                 if (attr_type_begin(attr.type) == attr_type_object_begin)
3247                         childs=1;
3248                 else
3249                         printf(" %s=\"%s\"",attr_to_name(attr.type),attr_to_text(&attr, NULL, 1));
3250         }
3251         if (childs) {
3252                 printf(">\n");
3253                 printf("</vehicle>\n");
3254         } else
3255                 printf(" />\n");
3256         vehicle_attr_iter_destroy(iter);
3257 }
3258
3259
3260 /**
3261  * Reacts to a button press that changes a vehicle's active profile.
3262  *
3263  * @see gui_internal_add_vehicle_profile
3264  */
3265 static void
3266 gui_internal_cmd_set_active_profile(struct gui_priv *this, struct
3267                 widget *wm, void *data)
3268 {
3269         struct vehicle_and_profilename *vapn = data;
3270         struct vehicle *v = vapn->vehicle;
3271         char *profilename = vapn->profilename;
3272         struct attr vehicle_name_attr;
3273         char *vehicle_name = NULL;
3274
3275         // Get the vehicle name
3276         vehicle_get_attr(v, attr_name, &vehicle_name_attr, NULL);
3277         vehicle_name = vehicle_name_attr.u.str;
3278
3279         dbg(0, "Changing vehicle %s to profile %s\n", vehicle_name,
3280                         profilename);
3281
3282         // Change the profile name
3283         struct attr profilename_attr = {attr_profilename, {profilename}};
3284         if(!vehicle_set_attr(v, &profilename_attr)) {
3285                 dbg(0, "Unable to set the vehicle's profile name\n");
3286         }
3287
3288     // Notify Navit that the routing should be re-done if this is the
3289     // active vehicle.
3290         if (gui_internal_is_active_vehicle(this, v)) {
3291                 struct attr vehicle;
3292                 vehicle.type=attr_vehicle;
3293                 vehicle.u.vehicle=v;
3294                 navit_set_attr(this->nav, &vehicle);
3295         }
3296         save_vehicle_xml(v);
3297 }
3298
3299 /**
3300  * Adds the vehicle profile to the GUI, allowing the user to pick a
3301  * profile for the currently selected vehicle.
3302  */
3303 static void
3304 gui_internal_add_vehicle_profile(struct gui_priv *this, struct widget
3305                 *parent, struct vehicle *v, struct vehicleprofile *profile)
3306 {
3307         // Just here to show up in the translation file, nice and close to
3308         // where the translations are actually used.
3309         struct attr profile_attr;
3310         struct attr *attr = NULL;
3311         char *name = NULL;
3312         char *active_profile = NULL;
3313         char *label = NULL;
3314         int active;
3315         struct vehicle_and_profilename *context = NULL;
3316
3317 #ifdef ONLY_FOR_TRANSLATION
3318         char *translations[] = {_n("car"), _n("bike"), _n("pedestrian")};
3319 #endif
3320
3321         // Figure out the profile name
3322         attr = attr_search(profile->attrs, NULL, attr_name);
3323         name = attr->u.str;
3324
3325         // Determine whether the profile is the active one
3326         vehicle_get_attr(v, attr_profilename, &profile_attr, NULL);
3327         active_profile = profile_attr.u.str;
3328         active = strcmp(name, active_profile) == 0;
3329
3330         dbg(0, "Adding vehicle profile %s, active=%s/%i\n", name,
3331                         active_profile, active);
3332
3333         // Build a translatable label.
3334         if(active) {
3335                 label = g_strdup_printf(_("Current profile: %s"), _(name));
3336         } else {
3337                 label = g_strdup_printf(_("Change profile to: %s"), _(name));
3338         }
3339
3340         // Create the context object (the vehicle and the desired profile)
3341         context = g_new0(struct vehicle_and_profilename, 1);
3342         context->vehicle = v;
3343         context->profilename = name;
3344
3345         // Add the button
3346         gui_internal_widget_append(parent,
3347                 gui_internal_button_new_with_callback(
3348                         this, label,
3349                         image_new_xs(this, active ? "gui_active" : "gui_inactive"),
3350                         gravity_left_center|orientation_horizontal|flags_fill,
3351                         gui_internal_cmd_set_active_profile, context));
3352
3353         free(label);
3354 }
3355
3356 static void
3357 gui_internal_cmd_vehicle_settings(struct gui_priv *this, struct widget *wm, void *data)
3358 {
3359         struct widget *w,*wb;
3360         struct attr attr;
3361         struct vehicle *v=wm->data;
3362     struct vehicleprofile *profile = NULL;
3363
3364         wb=gui_internal_menu(this, wm->text);
3365         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3366         gui_internal_widget_append(wb, w);
3367
3368     // Add the "Set as active" button if this isn't the active
3369     // vehicle.
3370         if (!gui_internal_is_active_vehicle(this, v)) {
3371                 gui_internal_widget_append(w,
3372                         gui_internal_button_new_with_callback(this, _("Set as active"),
3373                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3374                                 gui_internal_cmd_set_active_vehicle, wm->data));
3375         }
3376
3377         if (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
3378                 gui_internal_widget_append(w,
3379                         gui_internal_button_new_with_callback(this, _("Show Satellite status"),
3380                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3381                                 gui_internal_cmd_show_satellite_status, wm->data));
3382         }
3383         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL)) {
3384                 gui_internal_widget_append(w,
3385                         gui_internal_button_new_with_callback(this, _("Show NMEA data"),
3386                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3387                                 gui_internal_cmd_show_nmea_data, wm->data));
3388         }
3389
3390     // Add all the possible vehicle profiles to the menu
3391         GList *profiles = navit_get_vehicleprofiles(this->nav);
3392     while(profiles) {
3393         profile = (struct vehicleprofile *)profiles->data;
3394         gui_internal_add_vehicle_profile(this, w, v, profile);
3395                 profiles = g_list_next(profiles);
3396     }
3397
3398         callback_list_call_attr_2(this->cbl, attr_vehicle, w, wm->data);
3399         gui_internal_menu_render(this);
3400 }
3401
3402 static void
3403 gui_internal_cmd_vehicle(struct gui_priv *this, struct widget *wm, void *data)
3404 {
3405         struct attr attr,vattr;
3406         struct widget *w,*wb,*wl;
3407         struct attr_iter *iter;
3408         struct attr active_vehicle;
3409
3410
3411         wb=gui_internal_menu(this, _("Vehicle"));
3412         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3413         w->spy=this->spacing*3;
3414         gui_internal_widget_append(wb, w);
3415         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
3416                 active_vehicle.u.vehicle=NULL;
3417         iter=navit_attr_iter_new();
3418         while(navit_get_attr(this->nav, attr_vehicle, &attr, iter)) {
3419                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
3420                 wl=gui_internal_button_new_with_callback(this, vattr.u.str,
3421                         image_new_l(this, attr.u.vehicle == active_vehicle.u.vehicle ? "gui_active" : "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
3422                         gui_internal_cmd_vehicle_settings, attr.u.vehicle);
3423                 wl->text=g_strdup(vattr.u.str);
3424                 gui_internal_widget_append(w, wl);
3425         }
3426         navit_attr_iter_destroy(iter);
3427         gui_internal_menu_render(this);
3428 }
3429
3430
3431 static void
3432 gui_internal_cmd_rules(struct gui_priv *this, struct widget *wm, void *data)
3433 {
3434         struct widget *wb,*w;
3435         struct attr on,off;
3436         wb=gui_internal_menu(this, _("Rules"));
3437         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3438         w->spy=this->spacing*3;
3439         gui_internal_widget_append(wb, w);
3440         on.u.num=1;
3441         off.u.num=0;
3442         on.type=off.type=attr_tracking;
3443         gui_internal_widget_append(w,
3444                 gui_internal_button_navit_attr_new(this, _("Lock on road"), gravity_left_center|orientation_horizontal|flags_fill,
3445                         &on, &off));
3446         on.u.num=0;
3447         off.u.num=-1;
3448         on.type=off.type=attr_orientation;
3449         gui_internal_widget_append(w,
3450                 gui_internal_button_navit_attr_new(this, _("Northing"), gravity_left_center|orientation_horizontal|flags_fill,
3451                         &on, &off));
3452         on.u.num=1;
3453         off.u.num=0;
3454         on.type=off.type=attr_follow_cursor;
3455         gui_internal_widget_append(w,
3456                 gui_internal_button_navit_attr_new(this, _("Map follows Vehicle"), gravity_left_center|orientation_horizontal|flags_fill,
3457                         &on, &off));
3458         gui_internal_menu_render(this);
3459 }
3460
3461 static void
3462 gui_internal_cmd_settings(struct gui_priv *this, struct widget *wm, void *data)
3463 {
3464         struct widget *w;
3465
3466         w=gui_internal_menu(this, _("Settings"));
3467         gui_internal_widget_append(w,
3468                 gui_internal_button_new_with_callback(this, _("Display"),
3469                         image_new_l(this, "gui_display"), gravity_center|orientation_vertical,
3470                         gui_internal_cmd_display, NULL));
3471         gui_internal_widget_append(w,
3472                 gui_internal_button_new_with_callback(this, _("Maps"),
3473                         image_new_l(this, "gui_maps"), gravity_center|orientation_vertical,
3474                         gui_internal_cmd_maps, NULL));
3475         gui_internal_widget_append(w,
3476                 gui_internal_button_new_with_callback(this, _("Vehicle"),
3477                         image_new_l(this, "gui_vehicle"), gravity_center|orientation_vertical,
3478                         gui_internal_cmd_vehicle, NULL));
3479         gui_internal_widget_append(w,
3480                 gui_internal_button_new_with_callback(this, _("Rules"),
3481                         image_new_l(this, "gui_rules"), gravity_center|orientation_vertical,
3482                         gui_internal_cmd_rules, NULL));
3483         gui_internal_menu_render(this);
3484 }
3485
3486 //##############################################################################################################
3487 //# Description:
3488 //# Comment:
3489 //# Authors: Martin Schaller (04/2008)
3490 //##############################################################################################################
3491 static void gui_internal_motion(void *data, struct point *p)
3492 {
3493
3494         struct gui_priv *this=data;
3495         if (!this->root.children) {
3496                 navit_handle_motion(this->nav, p);
3497                 return;
3498         }
3499         if (!this->pressed)
3500                 return;
3501         this->current=*p;
3502         if(!this->motion_timeout_callback)
3503                 this->motion_timeout_callback=callback_new_1(callback_cast(gui_internal_highlight), this);
3504         if(!this->motion_timeout_event)
3505                 this->motion_timeout_event=event_add_timeout(100,0, this->motion_timeout_callback);
3506 }
3507
3508 static const char *
3509 find_attr(const char **names, const char **values, const char *name)
3510 {
3511         while (*names) {
3512                 if (!strcasecmp(*names, name))
3513                         return *values;
3514                 names+=xml_attr_distance;
3515                 values+=xml_attr_distance;
3516         }
3517         return NULL;
3518 }
3519
3520 static void
3521 gui_internal_html_command(struct gui_priv *this, struct widget *w, void *data)
3522 {
3523         struct attr attr;
3524         /* FIXME */
3525         if (w->command && navit_get_attr(this->nav, attr_gui, &attr, NULL)) {
3526                 command_evaluate(&attr, w->command);
3527         }
3528 }
3529
3530 static void
3531 gui_internal_html_start(void *dummy, const char *tag, const char **names, const char **values, void *data, void *error)
3532 {
3533         struct gui_priv *this=data;
3534         g_free(this->html_tag);
3535         g_free(this->html_command);
3536         this->html_tag=g_strdup(tag);
3537         if (!strcasecmp(tag,"img")) {
3538                 const char *src=find_attr(names, values, "src");
3539                 this->html_command=g_strdup(find_attr(names, values, "onclick"));
3540                 if (src)
3541                         this->html_curr=gui_internal_image_new(this, image_new_l(this, src));
3542         }
3543 }
3544
3545 static void
3546 gui_internal_html_end(void *dummy, const char *tag, void *data, void *error)
3547 {
3548         struct gui_priv *this=data;
3549         if (this->html_command && this->html_curr) {
3550                 struct widget *w=this->html_curr;
3551                 w->state |= STATE_SENSITIVE;
3552                 w->command=this->html_command;
3553                 w->func=gui_internal_html_command;
3554                 this->html_command=NULL;
3555         }
3556         if (!strcasecmp(this->html_tag, "img")) {
3557                 gui_internal_widget_append(this->html_container, this->html_curr);
3558         }
3559         this->html_curr=NULL;
3560 }
3561
3562 static void
3563 gui_internal_html_text(void *dummy, const char *text, int len, void *data, void *error)
3564 {
3565         struct gui_priv *this=data;
3566         if (!strcasecmp(this->html_tag, "h1")) {
3567                 if (!this->html_container) {
3568                         this->html_container=gui_internal_menu(this, gettext(text));
3569                         this->html_container->spx=this->spacing*10;
3570                 }
3571         }
3572         if (!strcasecmp(this->html_tag, "img")) {
3573                 struct widget *w;
3574                 w=gui_internal_box_new(this, gravity_center|orientation_vertical);
3575                 gui_internal_widget_append(w, this->html_curr);
3576                 gui_internal_widget_append(w, gui_internal_text_new(this, gettext(text), gravity_center|orientation_vertical));
3577                 this->html_curr=w;
3578         }
3579 }
3580
3581 static void
3582 gui_internal_html_menu(struct gui_priv *this, char *document)
3583 {
3584         graphics_draw_mode(this->gra, draw_mode_begin);
3585         this->html_container=NULL;
3586         this->html_curr=NULL;
3587         xml_parse_text(document, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
3588         gui_internal_menu_render(this);
3589         graphics_draw_mode(this->gra, draw_mode_end);
3590 }
3591
3592
3593 static void gui_internal_menu_root(struct gui_priv *this)
3594 {
3595         gui_internal_html_menu(this,
3596         "<html>"
3597         "<h1>"_n("Main menu")"</h1>"
3598         "<img src='gui_actions' onclick='actions()'>"_n("Actions")"</img>"
3599 #if 0
3600         "<img src='gui_map' onclick='show_map()'>"_n("Show</br>Map")"</img>"
3601 #endif
3602         "<img src='gui_settings' onclick='settings()'>"_n("Settings")"</img>"
3603         "<img src='gui_tools' onclick='tools()'>"_n("Tools")"</img>"
3604         "<img src='gui_settings' onclick='route()'>"_n("Route")"</img>"
3605         "</html>"
3606         );
3607 }
3608
3609 static void
3610 gui_internal_enter(struct gui_priv *this, int ignore)
3611 {
3612         struct graphics *gra=this->gra;
3613         this->ignore_button=ignore;
3614         this->clickp_valid=this->vehicle_valid=0;
3615
3616         navit_block(this->nav, 1);
3617         graphics_overlay_disable(gra, 1);
3618         this->root.p.x=0;
3619         this->root.p.y=0;
3620         this->root.background=this->background;
3621 }
3622
3623 static void
3624 gui_internal_leave(struct gui_priv *this)
3625 {
3626         graphics_draw_mode(this->gra, draw_mode_end);
3627 }
3628
3629 static void
3630 gui_internal_cmd_menu(struct gui_priv *this, struct point *p, int ignore)
3631 {
3632         struct transformation *trans;
3633         struct coord c;
3634         struct attr attr,attrp;
3635
3636         dbg(1,"enter\n");
3637         gui_internal_enter(this, ignore);
3638         trans=navit_get_trans(this->nav);
3639         if (p) {
3640                 transform_reverse(trans, p, &c);
3641                 dbg(0,"x=0x%x y=0x%x\n", c.x, c.y);
3642                 this->clickp.pro=transform_get_projection(trans);
3643                 this->clickp.x=c.x;
3644                 this->clickp.y=c.y;
3645                 this->clickp_valid=1;
3646         }
3647         if (navit_get_attr(this->nav, attr_vehicle, &attr, NULL) && attr.u.vehicle
3648                 && vehicle_get_attr(attr.u.vehicle, attr_position_coord_geo, &attrp, NULL)) {
3649                 this->vehiclep.pro=transform_get_projection(trans);
3650                 transform_from_geo(this->vehiclep.pro, attrp.u.coord_geo, &c);
3651                 this->vehiclep.x=c.x;
3652                 this->vehiclep.y=c.y;
3653                 this->vehicle_valid=1;
3654         }
3655         // draw menu
3656         gui_internal_menu_root(this);
3657 }
3658
3659 static void
3660 gui_internal_cmd_menu2(struct gui_priv *this)
3661 {
3662         gui_internal_cmd_menu(this, NULL, 1);
3663 }
3664
3665
3666 static void
3667 gui_internal_cmd_log_do(struct gui_priv *this, struct widget *widget)
3668 {
3669         if (widget->text && strlen(widget->text))
3670                 navit_textfile_debug_log(this->nav, "type=log_entry label=\"%s\"",widget->text);
3671         g_free(widget->text);
3672         widget->text=NULL;
3673         gui_internal_prune_menu(this, NULL);
3674         gui_internal_check_exit(this);
3675 }
3676
3677 static void
3678 gui_internal_cmd_log_clicked(struct gui_priv *this, struct widget *widget, void *data)
3679 {
3680         gui_internal_cmd_log_do(this, widget->data);
3681 }
3682
3683 static void
3684 gui_internal_cmd_log_changed(struct gui_priv *this, struct widget *wm, void *data)
3685 {
3686         int len;
3687         if (wm->text) {
3688                 len=strlen(wm->text);
3689                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
3690                         wm->text[len-1]='\0';
3691                         gui_internal_cmd_log_do(this, wm);
3692                 }
3693         }
3694 }
3695
3696
3697 static void
3698 gui_internal_cmd_log(struct gui_priv *this)
3699 {
3700         struct widget *w,*wb,*wk,*wl,*we,*wnext;
3701         gui_internal_enter(this, 1);
3702         wb=gui_internal_menu(this, "Log Message");
3703         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3704         gui_internal_widget_append(wb, w);
3705         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
3706         gui_internal_widget_append(w, we);
3707         gui_internal_widget_append(we, wk=gui_internal_label_new(this, _("Message")));
3708         wk->state |= STATE_EDIT|STATE_CLEAR;
3709         wk->background=this->background;
3710         wk->flags |= flags_expand|flags_fill;
3711         wk->func = gui_internal_cmd_log_changed;
3712         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
3713         wnext->state |= STATE_SENSITIVE;
3714         wnext->func = gui_internal_cmd_log_clicked;
3715         wnext->data=wk;
3716         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3717         gui_internal_widget_append(w, wl);
3718         if (this->keyboard)
3719                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
3720         gui_internal_menu_render(this);
3721         gui_internal_leave(this);
3722 }
3723
3724 static void
3725 gui_internal_check_exit(struct gui_priv *this)
3726 {
3727         struct graphics *gra=this->gra;
3728         if (! this->root.children) {
3729                 gui_internal_search_idle_end(this);
3730                 gui_internal_search_list_destroy(this);
3731                 graphics_overlay_disable(gra, 0);
3732                 if (!navit_block(this->nav, 0)) {
3733                         if (this->redraw)
3734                                 navit_draw(this->nav);
3735                         else
3736                                 navit_draw_displaylist(this->nav);
3737                 }
3738         }
3739 }
3740
3741 //##############################################################################################################
3742 //# Description: Function to handle mouse clicks and scroll wheel movement
3743 //# Comment:
3744 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
3745 //##############################################################################################################
3746 static void gui_internal_button(void *data, int pressed, int button, struct point *p)
3747 {
3748         struct gui_priv *this=data;
3749         struct graphics *gra=this->gra;
3750
3751         dbg(1,"enter %d %d\n", pressed, button);
3752         // if still on the map (not in the menu, yet):
3753         dbg(1,"children=%p ignore_button=%d\n",this->root.children,this->ignore_button);
3754         if (!this->root.children || this->ignore_button) {
3755                 this->ignore_button=0;
3756                 // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
3757                 if (!navit_handle_button(this->nav, pressed, button, p, NULL)) {
3758                         dbg(1,"navit has handled button\n");
3759                         return;
3760                 }
3761                 dbg(1,"menu_on_map_click=%d\n",this->menu_on_map_click);
3762                 if (this->menu_on_map_click && button == 1) 
3763                         gui_internal_cmd_menu(this, p, 0);
3764                 return;
3765         }
3766
3767
3768         // if already in the menu:
3769         if (pressed) {
3770                 this->pressed=1;
3771                 this->current=*p;
3772                 gui_internal_highlight(this);
3773         } else {
3774                 this->pressed=0;
3775                 this->current.x=-1;
3776                 this->current.y=-1;
3777                 graphics_draw_mode(gra, draw_mode_begin);
3778                 gui_internal_call_highlighted(this);
3779                 gui_internal_highlight(this);
3780                 graphics_draw_mode(gra, draw_mode_end);
3781                 gui_internal_check_exit(this);
3782         }
3783 }
3784
3785 static void
3786 gui_internal_setup_gc(struct gui_priv *this)
3787 {
3788         struct color cbh={0x9fff,0x9fff,0x9fff,0xffff};
3789         struct color cf={0xbfff,0xbfff,0xbfff,0xffff};
3790         struct graphics *gra=this->gra;
3791
3792         if (this->background)
3793                 return;
3794         this->background=graphics_gc_new(gra);
3795         this->background2=graphics_gc_new(gra);
3796         this->highlight_background=graphics_gc_new(gra);
3797         graphics_gc_set_foreground(this->highlight_background, &cbh);
3798         this->foreground=graphics_gc_new(gra);
3799         graphics_gc_set_foreground(this->foreground, &cf);
3800         this->text_background=graphics_gc_new(gra);
3801         this->text_foreground=graphics_gc_new(gra);
3802         graphics_gc_set_foreground(this->background, &this->background_color);
3803         graphics_gc_set_foreground(this->background2, &this->background2_color);
3804         graphics_gc_set_foreground(this->text_background, &this->text_background_color);
3805         graphics_gc_set_foreground(this->text_foreground, &this->text_foreground_color);
3806 }
3807
3808 //##############################################################################################################
3809 //# Description:
3810 //# Comment:
3811 //# Authors: Martin Schaller (04/2008)
3812 //##############################################################################################################
3813 static void gui_internal_resize(void *data, int w, int h)
3814 {
3815         struct gui_priv *this=data;
3816         int changed=0;
3817
3818         gui_internal_setup_gc(this);
3819
3820         if (this->root.w != w || this->root.h != h) {
3821                 this->root.w=w;
3822                 this->root.h=h;
3823                 changed=1;
3824         }
3825         dbg(1,"w=%d h=%d children=%p\n", w, h, this->root.children);
3826         navit_handle_resize(this->nav, w, h);
3827         if (this->root.children && changed) {
3828                 gui_internal_prune_menu(this, NULL);
3829                 gui_internal_menu_root(this);
3830         }
3831 }
3832
3833 static void
3834 gui_internal_keynav_point(struct widget *w, int dx, int dy, struct point *p)
3835 {
3836         p->x=w->p.x+w->w/2;
3837         p->y=w->p.y+w->h/2;
3838         if (dx < 0)
3839                 p->x=w->p.x;
3840         if (dx > 0)
3841                 p->x=w->p.x+w->w;
3842         if (dy < 0)
3843                 p->y=w->p.y;
3844         if (dy > 0)
3845                 p->y=w->p.y+w->h;
3846 }
3847
3848 static void
3849 gui_internal_keynav_find_closest(struct widget *wi, struct point *p, int dx, int dy, int *distance, struct widget **result)
3850 {
3851         GList *l=wi->children;
3852         if (wi->state & STATE_SENSITIVE) {
3853                 int dist1,dist2;
3854                 struct point wp;
3855                 gui_internal_keynav_point(wi, -dx, -dy, &wp);
3856                 if (dx) {
3857                         dist1=(wp.x-p->x)*dx;
3858                         dist2=wp.y-p->y;
3859                 } else if (dy) {
3860                         dist1=(wp.y-p->y)*dy;
3861                         dist2=wp.x-p->x;
3862                 } else {
3863                         dist2=wp.x-p->x;
3864                         dist1=wp.y-p->y;
3865                         if (dist1 < 0)
3866                                 dist1=-dist1;
3867                 }
3868                 dbg(1,"checking %d,%d %d %d against %d,%d-%d,%d result %d,%d\n", p->x, p->y, dx, dy, wi->p.x, wi->p.y, wi->p.x+wi->w, wi->p.y+wi->h, dist1, dist2);
3869                 if (dist1 >= 0) {
3870                         if (dist2 < 0)
3871                                 dist1-=dist2;
3872                         else
3873                                 dist1+=dist2;
3874                         if (dist1 < *distance) {
3875                                 *result=wi;
3876                                 *distance=dist1;
3877                         }
3878                 }
3879         }
3880         while (l) {
3881                 struct widget *child=l->data;
3882                 gui_internal_keynav_find_closest(child, p, dx, dy, distance, result);
3883                 l=g_list_next(l);
3884         }
3885 }
3886
3887 static void
3888 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy)
3889 {
3890         struct widget *result,*menu=g_list_last(this->root.children)->data;
3891         struct point p;
3892         int distance;
3893         if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
3894                 gui_internal_keynav_point(this->highlighted, dx, dy, &p);
3895         else {
3896                 p.x=0;
3897                 p.y=0;
3898                 distance=INT_MAX;
3899                 result=NULL;
3900                 gui_internal_keynav_find_closest(menu, &p, 0, 0, &distance, &result);
3901                 if (result) {
3902                         gui_internal_keynav_point(result, dx, dy, &p);
3903                         dbg(1,"result origin=%p p=%d,%d\n", result, p.x, p.y);
3904                 }
3905         }
3906         result=NULL;
3907         distance=INT_MAX;
3908         gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
3909         dbg(1,"result=%p\n", result);
3910         if (! result) {
3911                 if (dx < 0)
3912                         p.x=this->root.w;
3913                 if (dx > 0)
3914                         p.x=0;
3915                 if (dy < 0)
3916                         p.y=this->root.h;
3917                 if (dy > 0)
3918                         p.y=0;
3919                 result=NULL;
3920                 distance=INT_MAX;
3921                 gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
3922                 dbg(1,"wraparound result=%p\n", result);
3923         }
3924         gui_internal_highlight_do(this, result);
3925         if (result)
3926                 gui_internal_say(this, result, 1);
3927 }
3928
3929 //##############################################################################################################
3930 //# Description:
3931 //# Comment:
3932 //# Authors: Martin Schaller (04/2008)
3933 //##############################################################################################################
3934 static void gui_internal_keypress(void *data, char *key)
3935 {
3936         struct gui_priv *this=data;
3937         int w,h;
3938         struct point p;
3939         if (!this->root.children) {
3940                 transform_get_size(navit_get_trans(this->nav), &w, &h);
3941                 switch (*key) {
3942                 case NAVIT_KEY_UP:
3943                         p.x=w/2;
3944                         p.y=0;
3945                         navit_set_center_screen(this->nav, &p, 1);
3946                         break;
3947                 case NAVIT_KEY_DOWN:
3948                         p.x=w/2;
3949                         p.y=h;
3950                         navit_set_center_screen(this->nav, &p, 1);
3951                         break;
3952                 case NAVIT_KEY_LEFT:
3953                         p.x=0;
3954                         p.y=h/2;
3955                         navit_set_center_screen(this->nav, &p, 1);
3956                         break;
3957                 case NAVIT_KEY_RIGHT:
3958                         p.x=w;
3959                         p.y=h/2;
3960                         navit_set_center_screen(this->nav, &p, 1);
3961                         break;
3962                 case NAVIT_KEY_ZOOM_IN:
3963                         navit_zoom_in(this->nav, 2, NULL);
3964                         break;
3965                 case NAVIT_KEY_ZOOM_OUT:
3966                         navit_zoom_out(this->nav, 2, NULL);
3967                         break;
3968                 case NAVIT_KEY_RETURN:
3969                 case NAVIT_KEY_MENU:
3970                         gui_internal_cmd_menu(this, NULL, 0);
3971                         break;
3972                 }
3973                 return;
3974         }
3975         graphics_draw_mode(this->gra, draw_mode_begin);
3976         switch (*key) {
3977         case NAVIT_KEY_LEFT:
3978                 gui_internal_keynav_highlight_next(this,-1,0);
3979                 break;
3980         case NAVIT_KEY_RIGHT:
3981                 gui_internal_keynav_highlight_next(this,1,0);
3982                 break;
3983         case NAVIT_KEY_UP:
3984                 gui_internal_keynav_highlight_next(this,0,-1);
3985                 break;
3986         case NAVIT_KEY_DOWN:
3987                 gui_internal_keynav_highlight_next(this,0,1);
3988                 break;
3989         case NAVIT_KEY_BACK:
3990                 break;
3991         case NAVIT_KEY_RETURN:
3992                 if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
3993                         gui_internal_call_highlighted(this);
3994                 else
3995                         gui_internal_keypress_do(this, key);
3996                 break;
3997         default:
3998                 gui_internal_keypress_do(this, key);
3999         }
4000         graphics_draw_mode(this->gra, draw_mode_end);
4001         gui_internal_check_exit(this);
4002 }
4003
4004
4005 //##############################################################################################################
4006 //# Description:
4007 //# Comment:
4008 //# Authors: Martin Schaller (04/2008)
4009 //##############################################################################################################
4010 static int gui_internal_set_graphics(struct gui_priv *this, struct graphics *gra)
4011 {
4012         struct window *win;
4013         struct transformation *trans=navit_get_trans(this->nav);
4014
4015         win=graphics_get_data(gra, "window");
4016         if (! win)
4017                 return 1;
4018         navit_ignore_graphics_events(this->nav, 1);
4019         this->gra=gra;
4020         this->win=win;
4021         navit_ignore_graphics_events(this->nav, 1);
4022         transform_get_size(trans, &this->root.w, &this->root.h);
4023         this->resize_cb=callback_new_attr_1(callback_cast(gui_internal_resize), attr_resize, this);
4024         graphics_add_callback(gra, this->resize_cb);
4025         this->button_cb=callback_new_attr_1(callback_cast(gui_internal_button), attr_button, this);
4026         graphics_add_callback(gra, this->button_cb);
4027         this->motion_cb=callback_new_attr_1(callback_cast(gui_internal_motion), attr_motion, this);
4028         graphics_add_callback(gra, this->motion_cb);
4029         this->keypress_cb=callback_new_attr_1(callback_cast(gui_internal_keypress), attr_keypress, this);
4030         graphics_add_callback(gra, this->keypress_cb);
4031
4032         // set fullscreen if needed
4033         if (this->fullscreen)
4034                 this->win->fullscreen(this->win, this->fullscreen != 0);
4035         /* Was resize callback already issued? */
4036         if (navit_get_ready(this->nav) & 2)
4037                 gui_internal_setup_gc(this);
4038         return 0;
4039 }
4040
4041 static void gui_internal_disable_suspend(struct gui_priv *this)
4042 {
4043         if (this->win->disable_suspend)
4044                 this->win->disable_suspend(this->win);
4045 }
4046
4047 //##############################################################################################################
4048 //# Description:
4049 //# Comment:
4050 //# Authors: Martin Schaller (04/2008)
4051 //##############################################################################################################
4052 struct gui_methods gui_internal_methods = {
4053         NULL,
4054         NULL,
4055         gui_internal_set_graphics,
4056         NULL,
4057         NULL,
4058         NULL,
4059         gui_internal_disable_suspend,
4060 };
4061
4062 static void
4063 gui_internal_get_data(struct gui_priv *priv, char *command, struct attr **in, struct attr ***out)
4064 {
4065         struct attr private_data = (struct attr) { attr_private_data, {(void *)&priv->data}};
4066         if (out)
4067                 *out=attr_generic_add_attr(*out, &private_data);
4068 }
4069
4070 static void
4071 gui_internal_add_callback(struct gui_priv *priv, struct callback *cb)
4072 {
4073         callback_list_add(priv->cbl, cb);
4074 }
4075
4076 static void
4077 gui_internal_remove_callback(struct gui_priv *priv, struct callback *cb)
4078 {
4079         callback_list_remove(priv->cbl, cb);
4080 }
4081
4082
4083 static struct gui_internal_methods gui_internal_methods_ext = {
4084         gui_internal_add_callback,
4085         gui_internal_remove_callback,
4086         gui_internal_menu_render,
4087         image_new_xs,
4088         image_new_l,
4089 };
4090
4091
4092 static enum flags
4093 gui_internal_get_flags(struct widget *widget)
4094 {
4095         return widget->flags;
4096 }
4097
4098 static void
4099 gui_internal_set_flags(struct widget *widget, enum flags flags)
4100 {
4101         widget->flags=flags;
4102 }
4103
4104 static int
4105 gui_internal_get_state(struct widget *widget)
4106 {
4107         return widget->state;
4108 }
4109
4110 static void
4111 gui_internal_set_state(struct widget *widget, int state)
4112 {
4113         widget->state=state;
4114 }
4115
4116 static void
4117 gui_internal_set_func(struct widget *widget, void (*func)(struct gui_priv *priv, struct widget *widget, void *data))
4118 {
4119         widget->func=func;
4120 }
4121
4122 static void
4123 gui_internal_set_data(struct widget *widget, void *data)
4124 {
4125         widget->data=data;
4126 }
4127
4128 static void
4129 gui_internal_set_default_background(struct gui_priv *this, struct widget *widget)
4130 {
4131         widget->background=this->background;
4132 }
4133
4134 static struct gui_internal_widget_methods gui_internal_widget_methods = {
4135         gui_internal_widget_append,
4136         gui_internal_button_new,
4137         gui_internal_button_new_with_callback,
4138         gui_internal_box_new,
4139         gui_internal_label_new,
4140         gui_internal_image_new,
4141         gui_internal_keyboard,
4142         gui_internal_menu,
4143         gui_internal_get_flags,
4144         gui_internal_set_flags,
4145         gui_internal_get_state,
4146         gui_internal_set_state,
4147         gui_internal_set_func,
4148         gui_internal_set_data,
4149         gui_internal_set_default_background,
4150 };
4151
4152 static struct command_table commands[] = {
4153         {"actions",command_cast(gui_internal_cmd_actions)},
4154         {"fullscreen",command_cast(gui_internal_cmd_fullscreen)},
4155         {"get_data",command_cast(gui_internal_get_data)},
4156         {"log",command_cast(gui_internal_cmd_log)},
4157         {"menu",command_cast(gui_internal_cmd_menu2)},
4158         {"route",command_cast(gui_internal_cmd_route)},
4159         {"settings",command_cast(gui_internal_cmd_settings)},
4160         {"tools",command_cast(gui_internal_cmd_tools)},
4161 };
4162
4163
4164 //##############################################################################################################
4165 //# Description:
4166 //# Comment:
4167 //# Authors: Martin Schaller (04/2008)
4168 //##############################################################################################################
4169 static struct gui_priv * gui_internal_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs)
4170 {
4171         struct gui_priv *this;
4172         struct attr *attr;
4173         *meth=gui_internal_methods;
4174         this=g_new0(struct gui_priv, 1);
4175         this->nav=nav;
4176         if ((attr=attr_search(attrs, NULL, attr_menu_on_map_click)))
4177                 this->menu_on_map_click=attr->u.num;
4178         else
4179                 this->menu_on_map_click=1;
4180         if ((attr=attr_search(attrs, NULL, attr_callback_list))) {
4181                 command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this);
4182         }
4183
4184         if( (attr=attr_search(attrs,NULL,attr_font_size)))
4185         {
4186           this->config.font_size=attr->u.num;
4187         }
4188         else
4189         {
4190           this->config.font_size=-1;
4191         }
4192         if( (attr=attr_search(attrs,NULL,attr_icon_xs)))
4193         {
4194           this->config.icon_xs=attr->u.num;
4195         }
4196         else
4197         {
4198           this->config.icon_xs=-1;
4199         }
4200         if( (attr=attr_search(attrs,NULL,attr_icon_l)))
4201         {
4202           this->config.icon_l=attr->u.num;
4203         }
4204         else
4205         {
4206           this->config.icon_l=-1;
4207         }
4208         if( (attr=attr_search(attrs,NULL,attr_icon_s)))
4209         {
4210           this->config.icon_s=attr->u.num;
4211         }
4212         else
4213         {
4214           this->config.icon_s=-1;
4215         }
4216         if( (attr=attr_search(attrs,NULL,attr_spacing)))
4217         {
4218           this->config.spacing=attr->u.num;
4219         }
4220         else
4221         {
4222           this->config.spacing=-1;
4223         }
4224         if( (attr=attr_search(attrs,NULL,attr_gui_speech)))
4225         {
4226           this->speech=attr->u.num;
4227         }
4228         if( (attr=attr_search(attrs,NULL,attr_keyboard)))
4229           this->keyboard=attr->u.num;
4230         else
4231           this->keyboard=1;
4232
4233     if( (attr=attr_search(attrs,NULL,attr_fullscreen)))
4234       this->fullscreen=attr->u.num;
4235
4236         if( (attr=attr_search(attrs,NULL,attr_flags)))
4237               this->flags=attr->u.num;
4238         if( (attr=attr_search(attrs,NULL,attr_background_color)))
4239               this->background_color=*attr->u.color;
4240         else
4241               this->background_color=(struct color){0x0,0x0,0x0,0xffff};
4242         if( (attr=attr_search(attrs,NULL,attr_background_color2)))
4243                 this->background2_color=*attr->u.color;
4244         else
4245                 this->background2_color=(struct color){0x4141,0x4141,0x4141,0xffff};
4246         if( (attr=attr_search(attrs,NULL,attr_text_color)))
4247               this->text_foreground_color=*attr->u.color;
4248         else
4249               this->text_foreground_color=(struct color){0xffff,0xffff,0xffff,0xffff};
4250         if( (attr=attr_search(attrs,NULL,attr_columns)))
4251               this->cols=attr->u.num;
4252         if( (attr=attr_search(attrs,NULL,attr_osd_configuration)))
4253               this->osd_configuration=*attr;
4254
4255         if( (attr=attr_search(attrs,NULL,attr_pitch)))
4256               this->pitch=attr->u.num;
4257         else
4258                 this->pitch=20;
4259         this->data.priv=this;
4260         this->data.gui=&gui_internal_methods_ext;
4261         this->data.widget=&gui_internal_widget_methods;
4262         this->cbl=callback_list_new();
4263
4264         return this;
4265 }
4266
4267 //##############################################################################################################
4268 //# Description:
4269 //# Comment:
4270 //# Authors: Martin Schaller (04/2008)
4271 //##############################################################################################################
4272 void plugin_init(void)
4273 {
4274         plugin_register_gui_type("internal", gui_internal_new);
4275 }
4276
4277 /**
4278  * @brief Creates a new table widget.
4279  *
4280  * Creates and returns a new table widget.  This function will
4281  * setup next/previous buttons as children.
4282  *
4283  * @param this The graphics context.
4284  * @param flags widget sizing flags.
4285  * @returns The newly created widget
4286  */
4287 struct widget * gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons)
4288 {
4289         struct widget * widget = g_new0(struct widget,1);
4290         struct table_data * data = NULL;
4291         widget->type=widget_table;
4292         widget->flags=flags;
4293         widget->data = g_new0(struct table_data,1);
4294         widget->data_free=gui_internal_table_data_free;
4295         data = (struct table_data*)widget->data;
4296
4297
4298         if (buttons) {
4299         data->next_button = gui_internal_button_new_with_callback
4300                 (this,"Next",image_new_xs(this, "gui_active") ,
4301                  gravity_left_center  |orientation_vertical,
4302                  gui_internal_table_button_next,NULL);
4303         data->next_button->data=widget;
4304
4305
4306         data->prev_button =  gui_internal_button_new_with_callback
4307                 (this,"Prev",
4308                  image_new_xs(this, "gui_active")
4309                  ,gravity_right_center |orientation_vertical,
4310                  gui_internal_table_button_prev,NULL);
4311
4312         data->prev_button->data=widget;
4313
4314         data->this=this;
4315
4316         data->button_box=gui_internal_box_new(this,
4317                                               gravity_center|orientation_horizontal);
4318         data->button_box->children=g_list_append(data->button_box->children,
4319                                                  data->next_button);
4320         data->button_box->children=g_list_append(data->button_box->children,
4321                                                  data->prev_button);
4322         //data->button_box->background=this->background2;
4323         data->button_box->bl=this->spacing;
4324         widget->children=g_list_append(widget->children,data->button_box);
4325         gui_internal_widget_pack(this,data->button_box);
4326         }
4327
4328         return widget;
4329
4330 }
4331
4332 /**
4333  * @brief Clears all the rows from the table.
4334  * This function removes all rows from a table.
4335  * New rows can later be added to the table.
4336  */
4337 void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table)
4338 {
4339   GList * iter;
4340   struct table_data * table_data = (struct table_data* ) table->data;
4341
4342   iter = table->children;
4343   while(iter ) {
4344           if(iter->data != table_data->button_box) {
4345                   struct widget * child = (struct widget*)iter->data;
4346                   gui_internal_widget_destroy(this,child);
4347                   if(table->children == iter) {
4348                           table->children = g_list_remove(iter,iter->data);
4349                           iter=table->children;
4350                   }
4351                   else
4352                           iter = g_list_remove(iter,iter->data);
4353           }
4354           else {
4355                   iter = g_list_next(iter);
4356           }
4357
4358   }
4359   table_data->top_row=NULL;
4360   table_data->bottom_row=NULL;
4361   if(table_data->page_headers)
4362           g_list_free(table_data->page_headers);
4363   table_data->page_headers=NULL;
4364 }
4365
4366
4367 /**
4368  * Creates a new table_row widget.
4369  * @param this The graphics context
4370  * @param flags Sizing flags for the row
4371  * @returns The new table_row widget.
4372  */
4373 struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags)
4374 {
4375         struct widget * widget = g_new0(struct widget,1);
4376         widget->type=widget_table_row;
4377         widget->flags=flags;
4378         return widget;
4379 }
4380
4381
4382
4383 /**
4384  * @brief Computes the column dimensions for the table.
4385  *
4386  * @param w The table widget to compute dimensions for.
4387  *
4388  * This function examines all of the rows and columns for the table w
4389  * and returns a list (GList) of table_column_desc elements that
4390  * describe each column of the table.
4391  *
4392  * The caller is responsible for freeing the returned list.
4393  */
4394 static GList * gui_internal_compute_table_dimensions(struct gui_priv * this,struct widget * w)
4395 {
4396
4397         GList * column_desc = NULL;
4398         GList * current_desc=NULL;
4399         GList * cur_row = w->children;
4400         struct widget * cur_row_widget=NULL;
4401         GList * cur_column=NULL;
4402         struct widget * cell_w=NULL;
4403         struct table_column_desc * current_cell=NULL;
4404         struct table_data * table_data=NULL;
4405         int height=0;
4406         int width=0;
4407         int total_width=0;
4408         int column_count=0;
4409
4410         /**
4411          * Scroll through the the table and
4412          * 1. Compute the maximum width + height of each column across all rows.
4413          */
4414         table_data = (struct table_data*) w->data;
4415         for(cur_row=w->children;  cur_row ; cur_row = g_list_next(cur_row) )
4416         {
4417                 cur_row_widget = (struct widget*) cur_row->data;
4418                 current_desc = column_desc;
4419                 if(cur_row_widget == table_data->button_box)
4420                 {
4421                         continue;
4422                 }
4423                 column_count=0;
4424                 for(cur_column = cur_row_widget->children; cur_column;
4425                     cur_column=g_list_next(cur_column))
4426                 {
4427                         cell_w = (struct widget*) cur_column->data;
4428                         gui_internal_widget_pack(this,cell_w);
4429                         if(current_desc == 0)
4430                         {
4431                                 current_cell = g_new0(struct table_column_desc,1);
4432                                 column_desc = g_list_append(column_desc,current_cell);
4433                                 current_desc = g_list_last(column_desc);
4434                                 current_cell->height=cell_w->h;
4435                                 current_cell->width=cell_w->w;
4436                                 total_width+=cell_w->w;
4437
4438                         }
4439                         else
4440                         {
4441                                 current_cell = current_desc->data;
4442                                 height = cell_w->h;
4443                                 width = cell_w->w;
4444                                 if(current_cell->height < height )
4445                                 {
4446                                         current_cell->height = height;
4447                                 }
4448                                 if(current_cell->width < width)
4449                                 {
4450                                         total_width += (width-current_cell->width);
4451                                         current_cell->width = width;
4452
4453
4454
4455                                 }
4456                                 current_desc = g_list_next(current_desc);
4457                         }
4458                         column_count++;
4459
4460                 }/* column loop */
4461
4462         } /*row loop */
4463
4464
4465         /**
4466          * If the width of all columns is less than the width off
4467          * the table expand each cell proportionally.
4468          *
4469          */
4470         if(total_width+(this->spacing*column_count) < w->w ) {
4471                 for(current_desc=column_desc; current_desc; current_desc=g_list_next(current_desc)) {
4472                         current_cell = (struct table_column_desc*) current_desc->data;
4473                         current_cell->width= ( (current_cell->width+this->spacing)/(float)total_width) * w->w ;
4474                 }
4475         }
4476
4477         return column_desc;
4478 }
4479
4480
4481 /**
4482  * @brief Computes the height and width for the table.
4483  *
4484  * The height and widht are computed to display all cells in the table
4485  * at the requested height/width.
4486  *
4487  * @param this The graphics context
4488  * @param w The widget to pack.
4489  *
4490  */
4491 void gui_internal_table_pack(struct gui_priv * this, struct widget * w)
4492 {
4493
4494         int height=0;
4495         int width=0;
4496         int count=0;
4497         GList * column_data = gui_internal_compute_table_dimensions(this,w);
4498         GList * current=0;
4499         struct table_column_desc * cell_desc=0;
4500         struct table_data * table_data = (struct table_data*)w->data;
4501
4502         for(current = column_data; current; current=g_list_next(current))
4503         {
4504                 if(table_data->button_box == current->data )
4505                 {
4506                         continue;
4507                 }
4508                 cell_desc = (struct table_column_desc *) current->data;
4509                 width = width + cell_desc->width + this->spacing;
4510                 if(height < cell_desc->height)
4511                 {
4512                         height = cell_desc->height ;
4513                 }
4514         }
4515
4516
4517
4518         for(current=w->children; current; current=g_list_next(current))
4519         {
4520                 if(current->data!= table_data->button_box)
4521                 {
4522                         count++;
4523                 }
4524         }
4525         if (table_data->button_box)
4526                 gui_internal_widget_pack(this,table_data->button_box);
4527
4528
4529
4530         if(w->h + w->c.y   > this->root.h   )
4531         {
4532                 /**
4533                  * Do not allow the widget to exceed the screen.
4534                  *
4535                  */
4536                 w->h = this->root.h- w->c.y  - height;
4537         }
4538         w->w = width;
4539
4540         /**
4541          * Deallocate column descriptions.
4542          */
4543         current = column_data;
4544         while( (current = g_list_last(current)) )
4545         {
4546                 current = g_list_remove(current,current->data);
4547         }
4548
4549 }
4550
4551
4552
4553 /**
4554  * @brief Renders a table widget.
4555  *
4556  * @param this The graphics context
4557  * @param w The table widget to render.
4558  */
4559 void gui_internal_table_render(struct gui_priv * this, struct widget * w)
4560 {
4561
4562         int x;
4563         int y;
4564         GList * column_desc=NULL;
4565         GList * cur_row = NULL;
4566         GList * current_desc=NULL;
4567         struct table_data * table_data = (struct table_data*)w->data;
4568         int is_skipped=0;
4569         int is_first_page=1;
4570         struct table_column_desc * dim=NULL;
4571
4572         dbg_assert(table_data);
4573         column_desc = gui_internal_compute_table_dimensions(this,w);
4574         y=w->p.y;
4575
4576         /**
4577          * Skip rows that are on previous pages.
4578          */
4579         cur_row = w->children;
4580         if(table_data->top_row && table_data->top_row != w->children )
4581         {
4582                 cur_row = table_data->top_row;
4583                 is_first_page=0;
4584         }
4585
4586
4587         /**
4588          * Loop through each row.  Drawing each cell with the proper sizes,
4589          * at the proper positions.
4590          */
4591         for(table_data->top_row=cur_row; cur_row; cur_row = g_list_next(cur_row))
4592         {
4593                 GList * cur_column=NULL;
4594                 current_desc = column_desc;
4595                 struct widget * cur_row_widget = (struct widget*)cur_row->data;
4596                 int max_height=0;
4597                 x =w->p.x+this->spacing;
4598                 if(cur_row_widget == table_data->button_box )
4599                 {
4600                         continue;
4601                 }
4602                 dim = (struct table_column_desc*)current_desc->data;
4603
4604                 if( y + dim->height + (table_data->button_box ? table_data->button_box->h : 0) + this->spacing >= w->p.y + w->h )
4605                 {
4606                         /*
4607                          * No more drawing space left.
4608                          */
4609                         is_skipped=1;
4610                         break;
4611
4612                 }
4613                 for(cur_column = cur_row_widget->children; cur_column;
4614                     cur_column=g_list_next(cur_column))
4615                 {
4616                         struct  widget * cur_widget = (struct widget*) cur_column->data;
4617                         dim = (struct table_column_desc*)current_desc->data;
4618
4619                         cur_widget->p.x=x;
4620                         cur_widget->w=dim->width;
4621                         cur_widget->p.y=y;
4622                         cur_widget->h=dim->height;
4623                         x=x+cur_widget->w;
4624                         max_height = dim->height;
4625                         /* We pack the widget before rendering to ensure that the x and y
4626                          * coordinates get pushed down.
4627                          */
4628                         gui_internal_widget_pack(this,cur_widget);
4629                         gui_internal_widget_render(this,cur_widget);
4630
4631                         if(dim->height > max_height)
4632                         {
4633                                 max_height = dim->height;
4634                         }
4635                 }
4636                 y = y + max_height;
4637                 table_data->bottom_row=cur_row;
4638                 current_desc = g_list_next(current_desc);
4639         }
4640         if(table_data->button_box && (is_skipped || !is_first_page)  )
4641         {
4642                 table_data->button_box->p.y =w->p.y+w->h-table_data->button_box->h -
4643                         this->spacing;
4644                 if(table_data->button_box->p.y < y )
4645                 {
4646                         table_data->button_box->p.y=y;
4647                 }
4648                 table_data->button_box->p.x = w->p.x;
4649                 table_data->button_box->w = w->w;
4650                 //    table_data->button_box->h = w->h - y;
4651                 //    table_data->next_button->h=table_data->button_box->h;
4652                 //    table_data->prev_button->h=table_data->button_box->h;
4653                 //    table_data->next_button->c.y=table_data->button_box->c.y;
4654                 //    table_data->prev_button->c.y=table_data->button_box->c.y;
4655
4656                 gui_internal_widget_pack(this,table_data->button_box);
4657                 if(table_data->next_button->p.y > w->p.y + w->h + table_data->next_button->h)
4658                 {
4659
4660                         table_data->button_box->p.y = w->p.y + w->h -
4661                                 table_data->button_box->h;
4662                 }
4663                 if(is_skipped)
4664                 {
4665                         table_data->next_button->state|= STATE_SENSITIVE;
4666                 }
4667                 else
4668                 {
4669                         table_data->next_button->state&= ~STATE_SENSITIVE;
4670                 }
4671
4672                 if(table_data->top_row != w->children)
4673                 {
4674                         table_data->prev_button->state|= STATE_SENSITIVE;
4675                 }
4676                 else
4677                 {
4678                         table_data->prev_button->state&= ~STATE_SENSITIVE;
4679                 }
4680                 gui_internal_widget_render(this,table_data->button_box);
4681
4682
4683         }
4684
4685         /**
4686          * Deallocate column descriptions.
4687          */
4688         current_desc = column_desc;
4689         while( (current_desc = g_list_last(current_desc)) )
4690         {
4691                 current_desc = g_list_remove(current_desc,current_desc->data);
4692         }
4693 }
4694
4695
4696 /**
4697  * @brief Displays Route information
4698  *
4699  * @li The name of the active vehicle
4700  * @param wm The button that was pressed.
4701  * @param v Unused
4702  */
4703 static void
4704 gui_internal_cmd_route_description(struct gui_priv * this, struct widget * wm,void *v)
4705 {
4706
4707
4708         struct widget * menu;
4709         struct widget * row;
4710
4711
4712         if(! this->vehicle_cb)
4713         {
4714           /**
4715            * Register the callback on vehicle updates.
4716            */
4717           this->vehicle_cb = callback_new_attr_1(callback_cast(gui_internal_route_update),
4718                                                        attr_position_coord_geo,this);
4719           navit_add_callback(this->nav,this->vehicle_cb);
4720         }
4721
4722         this->route_data.route_table = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
4723         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
4724
4725         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
4726
4727
4728         menu=gui_internal_menu(this,_("Route Description"));
4729
4730         menu->free=gui_internal_route_screen_free;
4731         this->route_data.route_showing=1;
4732         this->route_data.route_table->spx = this->spacing;
4733
4734
4735         struct widget * box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
4736
4737         //      gui_internal_widget_append(box,gui_internal_box_new_with_label(this,"Test"));
4738         gui_internal_widget_append(box,this->route_data.route_table);
4739         box->w=menu->w;
4740         box->spx = this->spacing;
4741         this->route_data.route_table->w=box->w;
4742         gui_internal_widget_append(menu,box);
4743         gui_internal_populate_route_table(this,this->nav);
4744         gui_internal_menu_render(this);
4745
4746 }
4747
4748 static int
4749 line_intersection(struct coord* a1, struct coord *a2, struct coord * b1, struct coord *b2, struct coord *res)
4750 {
4751         int n, a, b;
4752         int adx=a2->x-a1->x;
4753         int ady=a2->y-a1->y;
4754         int bdx=b2->x-b1->x;
4755         int bdy=b2->y-b1->y;
4756         n = bdy * adx - bdx * ady;
4757         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
4758         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
4759         if (n < 0) {
4760                 n = -n;
4761                 a = -a;
4762                 b = -b;
4763         }
4764         if (a < 0 || b < 0)
4765                 return 0;
4766         if (a > n || b > n)
4767                 return 0;
4768         if (n == 0) {
4769                 dbg(0,"a=%d b=%d n=%d\n", a, b, n);
4770                 dbg(0,"a1=0x%x,0x%x ad %d,%d\n", a1->x, a1->y, adx, ady);
4771                 dbg(0,"b1=0x%x,0x%x bd %d,%d\n", b1->x, b1->y, bdx, bdy);
4772                 dbg_assert(n != 0);
4773         }
4774         res->x = a1->x + a * adx / n;
4775         res->y = a1->y + a * ady / n;
4776         return 1;
4777 }
4778
4779 struct heightline {
4780         struct heightline *next;
4781         int height;
4782         struct coord_rect bbox;
4783         int count;
4784         struct coord c[0];
4785 };
4786
4787 struct diagram_point {
4788         struct diagram_point *next;
4789         struct coord c;
4790 };
4791
4792 static struct heightline *
4793 item_get_heightline(struct item *item)
4794 {
4795         struct heightline *ret=NULL;
4796         struct street_data *sd;
4797         struct attr attr;
4798         int i,height;
4799
4800         if (item_attr_get(item, attr_label, &attr)) {
4801                 height=atoi(attr.u.str);
4802                 sd=street_get_data(item);
4803                 if (sd && sd->count > 1) {
4804                         ret=g_malloc(sizeof(struct heightline)+sd->count*sizeof(struct coord));
4805                         ret->bbox.lu=sd->c[0];
4806                         ret->bbox.rl=sd->c[0];
4807                         ret->count=sd->count;
4808                         ret->height=height;
4809                         for (i = 0 ; i < sd->count ; i++) {
4810                                 ret->c[i]=sd->c[i];
4811                                 coord_rect_extend(&ret->bbox, sd->c+i);
4812                         }
4813                 }
4814                 street_data_free(sd);
4815         }
4816         return ret;
4817 }
4818
4819
4820 /**
4821  * @brief Displays Route Height Profile
4822  *
4823  * @li The name of the active vehicle
4824  * @param wm The button that was pressed.
4825  * @param v Unused
4826  */
4827 static void gui_internal_cmd_route_height_profile(struct gui_priv * this, struct widget * wm,void *v)
4828 {
4829
4830
4831         struct widget * menu, *box;
4832
4833         struct map * map=NULL;
4834         struct map_rect * mr=NULL;
4835         struct route * route;
4836         struct item * item =NULL;
4837         struct mapset *ms;
4838         struct mapset_handle *msh;
4839         int x,i,first=1,dist=0;
4840         struct coord c,last,res;
4841         struct coord_rect rbbox,dbbox;
4842         struct map_selection sel;
4843         struct heightline *heightline,*heightlines=NULL;
4844         struct diagram_point *min,*diagram_point,*diagram_points=NULL;
4845         sel.next=NULL;
4846         sel.order=18;
4847         sel.range.min=type_height_line_1;
4848         sel.range.max=type_height_line_3;
4849
4850
4851         menu=gui_internal_menu(this,_("Height Profile"));
4852         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
4853         gui_internal_widget_append(menu, box);
4854         route = navit_get_route(this->nav);
4855         if (route)
4856                 map = route_get_map(route);
4857         if(map)
4858                 mr = map_rect_new(map,NULL);
4859         if(mr) {
4860                 while((item = map_rect_get_item(mr))) {
4861                         while (item_coord_get(item, &c, 1)) {
4862                                 if (first) {
4863                                         first=0;
4864                                         sel.u.c_rect.lu=c;
4865                                         sel.u.c_rect.rl=c;
4866                                 } else
4867                                         coord_rect_extend(&sel.u.c_rect, &c);
4868                         }
4869                 }
4870                 map_rect_destroy(mr);
4871                 ms=navit_get_mapset(this->nav);
4872                 if (!first && ms) {
4873                         msh=mapset_open(ms);
4874                         while ((map=mapset_next(msh, 1))) {
4875                                 mr=map_rect_new(map, &sel);
4876                                 if (mr) {
4877                                         while((item = map_rect_get_item(mr))) {
4878                                                 if (item->type >= sel.range.min && item->type <= sel.range.max) {
4879                                                         heightline=item_get_heightline(item);
4880                                                         if (heightline) {
4881                                                                 heightline->next=heightlines;
4882                                                                 heightlines=heightline;
4883                                                         }
4884                                                 }
4885                                         }
4886                                         map_rect_destroy(mr);
4887                                 }
4888                         }
4889                         mapset_close(msh);
4890                 }
4891         }
4892         map=NULL;
4893         mr=NULL;
4894         if (route)
4895                 map = route_get_map(route);
4896         if(map)
4897                 mr = map_rect_new(map,NULL);
4898         if(mr && heightlines) {
4899                 while((item = map_rect_get_item(mr))) {
4900                         first=1;
4901                         while (item_coord_get(item, &c, 1)) {
4902                                 if (first)
4903                                         first=0;
4904                                 else {
4905                                         heightline=heightlines;
4906                                         rbbox.lu=last;
4907                                         rbbox.rl=last;
4908                                         coord_rect_extend(&rbbox, &c);
4909                                         while (heightline) {
4910                                                 if (coord_rect_overlap(&rbbox, &heightline->bbox)) {
4911                                                         for (i = 0 ; i < heightline->count - 1; i++) {
4912                                                                 if (heightline->c[i].x != heightline->c[i+1].x || heightline->c[i].y != heightline->c[i+1].y) {
4913                                                                         if (line_intersection(heightline->c+i, heightline->c+i+1, &last, &c, &res)) {
4914                                                                                 diagram_point=g_new(struct diagram_point, 1);
4915                                                                                 diagram_point->c.x=dist+transform_distance(projection_mg, &last, &res);
4916                                                                                 diagram_point->c.y=heightline->height;
4917                                                                                 diagram_point->next=diagram_points;
4918                                                                                 diagram_points=diagram_point;
4919                                                                                 dbg(0,"%d %d\n", diagram_point->c.x, diagram_point->c.y);
4920                                                                         }
4921                                                                 }
4922                                                         }
4923                                                 }
4924                                                 heightline=heightline->next;
4925                                         }
4926                                         dist+=transform_distance(projection_mg, &last, &c);
4927                                 }
4928                                 last=c;
4929                         }
4930
4931                 }
4932                 map_rect_destroy(mr);
4933         }
4934
4935
4936         gui_internal_menu_render(this);
4937         first=1;
4938         diagram_point=diagram_points;
4939         while (diagram_point) {
4940                 if (first) {
4941                         dbbox.lu=diagram_point->c;
4942                         dbbox.rl=diagram_point->c;
4943                         first=0;
4944                 } else
4945                         coord_rect_extend(&dbbox, &diagram_point->c);
4946                 diagram_point=diagram_point->next;
4947         }
4948         dbg(0,"%d %d %d %d\n", dbbox.lu.x, dbbox.lu.y, dbbox.rl.x, dbbox.rl.y);
4949         if (dbbox.rl.x > dbbox.lu.x && dbbox.lu.x*100/(dbbox.rl.x-dbbox.lu.x) <= 25)
4950                 dbbox.lu.x=0;
4951         if (dbbox.lu.y > dbbox.rl.y && dbbox.rl.y*100/(dbbox.lu.y-dbbox.rl.y) <= 25)
4952                 dbbox.rl.y=0;
4953         dbg(0,"%d,%d %dx%d\n", box->p.x, box->p.y, box->w, box->h);
4954         x=dbbox.lu.x;
4955         first=1;
4956         for (;;) {
4957                 struct point p[2];
4958                 min=NULL;
4959                 diagram_point=diagram_points;
4960                 while (diagram_point) {
4961                         if (diagram_point->c.x >= x && (!min || min->c.x > diagram_point->c.x))
4962                                 min=diagram_point;
4963                         diagram_point=diagram_point->next;
4964                 }
4965                 if (! min)
4966                         break;
4967                 p[1].x=(min->c.x-dbbox.lu.x)*(box->w-10)/(dbbox.rl.x-dbbox.lu.x)+box->p.x+5;
4968                 p[1].y=(min->c.y-dbbox.rl.y)*(box->h-10)/(dbbox.lu.y-dbbox.rl.y)+box->p.y+5;
4969                 dbg(0,"%d,%d=%d,%d\n",min->c.x, min->c.y, p[1].x,p[1].y);
4970                 graphics_draw_circle(this->gra, this->foreground, &p[1], 2);
4971                 if (first)
4972                         first=0;
4973                 else
4974                         graphics_draw_lines(this->gra, this->foreground, p, 2);
4975                 p[0]=p[1];
4976                 x=min->c.x+1;
4977         }
4978
4979
4980 }
4981
4982 /**
4983  * @brief Displays Route information
4984  *
4985  * @li The name of the active vehicle
4986  * @param wm The button that was pressed.
4987  * @param v Unused
4988                 */
4989 void gui_internal_cmd_route(struct gui_priv * this, struct widget * wm,void *v)
4990 {
4991         struct widget *w;
4992
4993         graphics_draw_mode(this->gra, draw_mode_begin);
4994         w=gui_internal_menu(this, _("Route"));
4995         w->spx=this->spacing*10;
4996         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Description"),
4997                         image_new_l(this, "gui_actions"), gravity_center|orientation_vertical,
4998                         gui_internal_cmd_route_description, NULL));
4999         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Height Profile"),
5000                         image_new_l(this, "gui_actions"), gravity_center|orientation_vertical,
5001                         gui_internal_cmd_route_height_profile, NULL));
5002         gui_internal_menu_render(this);
5003         graphics_draw_mode(this->gra, draw_mode_end);
5004
5005 }
5006
5007 static void
5008 gui_internal_cmd_show_locale(struct gui_priv * this, struct widget * wm,void *v)
5009 {
5010         struct widget *menu,*wb,*w;
5011         char *text;
5012
5013         graphics_draw_mode(this->gra, draw_mode_begin);
5014         menu=gui_internal_menu(this, _("Show Locale"));
5015         menu->spx=this->spacing*10;
5016         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5017         gui_internal_widget_append(menu, wb);
5018         text=g_strdup_printf("LANG=%s",getenv("LANG"));
5019         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
5020         w->flags=gravity_left_center|orientation_horizontal|flags_fill;
5021         g_free(text);
5022 #ifdef HAVE_API_WIN32_BASE
5023         {
5024                 wchar_t wcountry[32],wlang[32];
5025                 char country[32],lang[32];
5026
5027                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
5028                 WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
5029                 text=g_strdup_printf("LOCALE_SABBREVLANGNAME=%s",lang);
5030                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
5031                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
5032                 g_free(text);
5033                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
5034                 WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
5035                 text=g_strdup_printf("LOCALE_SABBREVCTRYNAME=%s",country);
5036                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
5037                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
5038                 g_free(text);
5039         }
5040 #endif
5041
5042         gui_internal_menu_render(this);
5043         graphics_draw_mode(this->gra, draw_mode_end);
5044 }
5045
5046
5047 static void
5048 gui_internal_cmd_tools(struct gui_priv * this, struct widget * wm,void *v)
5049 {
5050         struct widget *w;
5051
5052         graphics_draw_mode(this->gra, draw_mode_begin);
5053         w=gui_internal_menu(this, _("Tools"));
5054         w->spx=this->spacing*10;
5055         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Show Locale"),
5056                         image_new_l(this, "gui_actions"), gravity_center|orientation_vertical,
5057                         gui_internal_cmd_show_locale, NULL));
5058         gui_internal_menu_render(this);
5059         graphics_draw_mode(this->gra, draw_mode_end);
5060 }
5061
5062 /**
5063  * @brief handles the 'next page' table event.
5064  * A callback function that is invoked when the 'next page' button is pressed
5065  * to advance the contents of a table widget.
5066  *
5067  * @param this The graphics context.
5068  * @param wm The button widget that was pressed.
5069  */
5070 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data)
5071 {
5072         struct widget * table_widget = (struct widget * ) wm->data;
5073         struct table_data * table_data = NULL;
5074         int found=0;
5075         GList * iterator;
5076
5077         if(table_widget)
5078         {
5079                 table_data = (struct table_data*) table_widget->data;
5080
5081         }
5082         if(table_data)
5083         {
5084                 /**
5085                  * Before advancing to the next page we need to ensure
5086                  * that the current top_row is in the list of previous top_rows
5087                  * so previous page can work.
5088                  *
5089                  */
5090                 for(iterator=table_data->page_headers; iterator != NULL;
5091                     iterator = g_list_next(iterator) )
5092                 {
5093                         if(iterator->data == table_data->top_row)
5094                         {
5095                                 found=1;
5096                                 break;
5097                         }
5098
5099                 }
5100                 if( ! found)
5101                 {
5102                         table_data->page_headers=g_list_append(table_data->page_headers,
5103                                                                table_data->top_row);
5104                 }
5105
5106                 table_data->top_row = g_list_next(table_data->bottom_row);
5107         }
5108         wm->state&= ~STATE_HIGHLIGHTED;
5109         gui_internal_menu_render(this);
5110 }
5111
5112
5113
5114 /**
5115  * @brief handles the 'previous page' table event.
5116  * A callback function that is invoked when the 'previous page' button is pressed
5117  * to go back in the contents of a table widget.
5118  *
5119  * @param this The graphics context.
5120  * @param wm The button widget that was pressed.
5121  */
5122 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data)
5123 {
5124         struct widget * table_widget = (struct widget * ) wm->data;
5125         struct table_data * table_data = NULL;
5126         GList * current_page_top=NULL;
5127
5128         GList * iterator;
5129         if(table_widget)
5130         {
5131                 table_data = (struct table_data*) table_widget->data;
5132                 if(table_data)
5133                 {
5134                         current_page_top = table_data->top_row;
5135                         for(iterator = table_data->page_headers; iterator != NULL;
5136                             iterator = g_list_next(iterator))
5137                         {
5138                                 if(current_page_top == iterator->data)
5139                                 {
5140                                         break;
5141                                 }
5142                                 table_data->top_row = (GList*) iterator->data;
5143                         }
5144                 }
5145         }
5146         wm->state&= ~STATE_HIGHLIGHTED;
5147         gui_internal_menu_render(this);
5148 }
5149
5150
5151 /**
5152  * @brief deallocates a table_data structure.
5153  *
5154  */
5155 void gui_internal_table_data_free(void * p)
5156 {
5157
5158
5159         /**
5160          * @note button_box and its children (next_button,prev_button)
5161          * have their memory managed by the table itself.
5162          */
5163         struct table_data * table_data =  (struct table_data*) p;
5164         g_list_free(table_data->page_headers);
5165         g_free(p);
5166
5167
5168 }
5169
5170
5171 /**
5172  * @brief Called when the route is updated.
5173  */
5174 void gui_internal_route_update(struct gui_priv * this, struct navit * navit, struct vehicle *v)
5175 {
5176
5177         if(this->route_data.route_showing) {
5178                 gui_internal_populate_route_table(this,navit);
5179                 graphics_draw_mode(this->gra, draw_mode_begin);
5180                 gui_internal_menu_render(this);
5181                 graphics_draw_mode(this->gra, draw_mode_end);
5182         }
5183
5184
5185 }
5186
5187
5188 /**
5189  * @brief Called when the route screen is closed (deallocated).
5190  *
5191  * The main purpose of this function is to remove the widgets from
5192  * references route_data because those widgets are about to be freed.
5193  */
5194 void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w)
5195 {
5196         if(this_) {
5197                 this_->route_data.route_showing=0;
5198                 this_->route_data.route_table=NULL;
5199                 g_free(w);
5200         }
5201
5202 }
5203
5204 /**
5205  * @brief Populates the route  table with route information
5206  *
5207  * @param this The gui context
5208  * @param navit The navit object
5209  */
5210 void gui_internal_populate_route_table(struct gui_priv * this,
5211                                        struct navit * navit)
5212 {
5213         struct map * map=NULL;
5214         struct map_rect * mr=NULL;
5215         struct navigation * nav = NULL;
5216         struct item * item =NULL;
5217         struct attr attr;
5218         struct widget * label = NULL;
5219         struct widget * row = NULL;
5220         nav = navit_get_navigation(navit);
5221         if(!nav) {
5222                 return;
5223         }
5224         map = navigation_get_map(nav);
5225         if(map)
5226           mr = map_rect_new(map,NULL);
5227         if(mr) {
5228                 gui_internal_widget_table_clear(this,this->route_data.route_table);
5229                 while((item = map_rect_get_item(mr))) {
5230                         if(item_attr_get(item,attr_navigation_long,&attr)) {
5231                           label = gui_internal_label_new(this,attr.u.str);
5232                           row = gui_internal_widget_table_row_new(this,
5233                                                                   gravity_left
5234                                                                   | flags_fill
5235                                                                   | orientation_horizontal);
5236                           row->children=g_list_append(row->children,label);
5237                           gui_internal_widget_append(this->route_data.route_table,row);
5238                         }
5239
5240                 }
5241
5242         }
5243 }