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