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