Fix:Core:Improved searching
[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;
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;
2382                 flags &= this->flags_street;
2383                 break;
2384         case 6:
2385                 flags=8|16|32|64;
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         dbg(1,"res->country->flag=%s\n", res->country->flag);
2671                 gui_internal_widget_append(search_list,
2672                                 wc=gui_internal_button_new_with_callback(this, text,
2673                                         image_new_xs(this, res->country->flag),
2674                                         gravity_left_center|orientation_horizontal|flags_fill,
2675                                         gui_internal_cmd_position, param));
2676                 wc->name=g_strdup(name);
2677                 if (res->c)
2678                         wc->c=*res->c;
2679                 wc->selection_id=res->id;
2680                 if (item)
2681                         wc->item=*item;
2682                 gui_internal_widget_pack(this, search_list);
2683                 l=g_list_last(this->root.children);
2684                 graphics_draw_mode(this->gra, draw_mode_begin);
2685                 gui_internal_widget_render(this, l->data);
2686                 graphics_draw_mode(this->gra, draw_mode_end);
2687         g_free(text);
2688 }
2689
2690 static void
2691 gui_internal_search_idle_start(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
2692 {
2693         this->idle_cb=callback_new_4(callback_cast(gui_internal_search_idle), this, wm_name, search_list, param);
2694         this->idle=event_add_idle(50,this->idle_cb);
2695         callback_call_0(this->idle_cb);
2696 }
2697
2698
2699 /**
2700  *
2701  * @param wm The widget that generated the event for the search changed,
2702  *        if this was generated by a key on the virtual keyboard then
2703  *        wm is the key button widget.
2704  */
2705 static void
2706 gui_internal_search_changed(struct gui_priv *this, struct widget *wm, void *data)
2707 {
2708         GList *l;
2709         struct widget *search_list=gui_internal_menu_data(this)->search_list;
2710         gui_internal_widget_children_destroy(this, search_list);
2711
2712         void *param=(void *)3;
2713         int minlen=1;
2714         if (! strcmp(wm->name,"Country")) 
2715                 param=(void *)4;
2716         if (! strcmp(wm->name,"Street")) 
2717                 param=(void *)5;
2718         if (! strcmp(wm->name,"House number")) 
2719                 param=(void *)6;
2720         dbg(0,"%s now '%s'\n", wm->name, wm->text);
2721
2722         gui_internal_search_idle_end(this);
2723         if (wm->text && g_utf8_strlen(wm->text, -1) >= minlen) {
2724                 struct attr search_attr;
2725
2726                 dbg(0,"process\n");
2727                 if (! strcmp(wm->name,"Country"))
2728                         search_attr.type=attr_country_all;
2729                 if (! strcmp(wm->name,"Town"))
2730                         search_attr.type=attr_town_or_district_name;
2731                 if (! strcmp(wm->name,"Street"))
2732                         search_attr.type=attr_street_name;
2733                 if (! strcmp(wm->name,"House number"))
2734                         search_attr.type=attr_house_number;
2735                 search_attr.u.str=wm->text;
2736                 search_list_search(this->sl, &search_attr, 1);
2737                 gui_internal_search_idle_start(this, wm->name, search_list, param);
2738         }
2739         l=g_list_last(this->root.children);
2740         gui_internal_widget_render(this, l->data);
2741 }
2742
2743 static struct widget *
2744 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)
2745 {
2746         struct widget *wk;
2747         gui_internal_widget_append(wkbd, wk=gui_internal_button_new_with_callback(this, text,
2748                 NULL, gravity_center|orientation_vertical, func, data));
2749         wk->data_free=data_free;
2750         wk->background=this->background;
2751         wk->bl=w/2;
2752         wk->br=0;
2753         wk->bt=h/3;
2754         wk->bb=0;
2755         return wk;
2756 }
2757
2758 static struct widget *
2759 gui_internal_keyboard_key(struct gui_priv *this, struct widget *wkbd, char *text, char *key, int w, int h)
2760 {
2761         return gui_internal_keyboard_key_data(this, wkbd, text, gui_internal_cmd_keypress, g_strdup(key), g_free,w,h);
2762 }
2763
2764 static void gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data);
2765
2766 // Some macros that make the keyboard layout easier to visualise in
2767 // the source code. The macros are #undef'd after this function.
2768 #define KEY(x) gui_internal_keyboard_key(this, wkbd, (x), (x), max_w, max_h)
2769 #define SPACER() gui_internal_keyboard_key_data(this, wkbd, "", NULL, NULL, NULL,max_w,max_h)
2770 static struct widget *
2771 gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode)
2772 {
2773         struct widget *wkbd,*wk;
2774         struct menu_data *md=gui_internal_menu_data(this);
2775         int i, max_w=this->root.w, max_h=this->root.h;
2776         int render=0;
2777
2778         if (wkbdb) {
2779                 this->current.x=-1;
2780                 this->current.y=-1;
2781                 gui_internal_highlight(this);
2782                 render=1;
2783                 gui_internal_widget_children_destroy(this, wkbdb);
2784         } else
2785                 wkbdb=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
2786         md->keyboard=wkbdb;
2787         md->keyboard_mode=mode;
2788         wkbd=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
2789         wkbd->background=this->background;
2790         wkbd->cols=8;
2791         wkbd->spx=3;
2792         wkbd->spy=3;
2793         max_w=max_w/9;
2794         max_h=max_h/6;
2795
2796         if (mode >= 0 && mode < 8) {
2797                 for (i = 0 ; i < 26 ; i++) {
2798                         char text[]={'A'+i,'\0'};
2799                         KEY(text);
2800                 }
2801                 gui_internal_keyboard_key(this, wkbd, "_"," ",max_w,max_h);
2802                 if (mode == 0) {
2803                         KEY("-");
2804                         KEY("'");
2805                         SPACER();
2806                 } else {
2807                         SPACER();
2808                         wk=gui_internal_keyboard_key_data(this, wkbd, "a", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2809                         wk->datai=mode+8;
2810                         wk=gui_internal_keyboard_key_data(this, wkbd, "1", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2811                         wk->datai=mode+16;
2812                 }
2813                 wk=gui_internal_keyboard_key_data(this, wkbd, "Ä",gui_internal_keyboard_change, wkbdb,NULL,max_w,max_h);
2814                 wk->datai=mode+24;
2815                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2816         }
2817         if (mode >= 8 && mode < 16) {
2818                 for (i = 0 ; i < 26 ; i++) {
2819                         char text[]={'a'+i,'\0'};
2820                         KEY(text);
2821                 }
2822                 gui_internal_keyboard_key(this, wkbd, "_"," ",max_w,max_h);
2823                 if (mode == 8) {
2824                         KEY("-");
2825                         KEY("'");
2826                         SPACER();
2827                 } else {
2828                         SPACER();
2829                         wk=gui_internal_keyboard_key_data(this, wkbd, "A", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2830                         wk->datai=mode-8;
2831                         wk=gui_internal_keyboard_key_data(this, wkbd, "1", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2832                         wk->datai=mode+8;
2833                 }
2834                 wk=gui_internal_keyboard_key_data(this, wkbd, "ä",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2835                 wk->datai=mode+24;
2836                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2837         }
2838         if (mode >= 16 && mode < 24) {
2839                 for (i = 0 ; i < 10 ; i++) {
2840                         char text[]={'0'+i,'\0'};
2841                         KEY(text);
2842                 }
2843                 KEY("."); KEY("°"); KEY("'"); KEY("\""); KEY("-"); KEY("+");
2844                 KEY("*"); KEY("/"); KEY("("); KEY(")"); KEY("="); KEY("?");
2845
2846                 for (i = 0 ; i < 5 ; i++) SPACER();
2847
2848                 if (mode == 16) {
2849                         KEY("-");
2850                         KEY("'");
2851                         SPACER();
2852                 } else {
2853                         SPACER();
2854                         wk=gui_internal_keyboard_key_data(this, wkbd, "A", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2855                         wk->datai=mode-16;
2856                         wk=gui_internal_keyboard_key_data(this, wkbd, "a", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2857                         wk->datai=mode-8;
2858                 }
2859                 wk=gui_internal_keyboard_key_data(this, wkbd, "Ä",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2860                 wk->datai=mode+8;
2861                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2862         }
2863         if (mode >= 24 && mode < 32) {
2864                 KEY("Ä"); KEY("Ë"); KEY("Ï"); KEY("Ö"); KEY("Ü"); KEY("Æ"); KEY("Ø"); KEY("Å");
2865                 KEY("Á"); KEY("É"); KEY("Í"); KEY("Ó"); KEY("Ú"); KEY("Š"); KEY("Č"); KEY("Ž");
2866                 KEY("À"); KEY("È"); KEY("Ì"); KEY("Ò"); KEY("Ù"); KEY("Ś"); KEY("Ć"); KEY("Ź");
2867                 KEY("Â"); KEY("Ê"); KEY("Î"); KEY("Ô"); KEY("Û"); SPACER();
2868
2869                 wk=gui_internal_keyboard_key_data(this, wkbd, "A",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2870                 wk->datai=mode-24;
2871
2872                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2873         }
2874         if (mode >= 32 && mode < 40) {
2875                 KEY("ä"); KEY("ë"); KEY("ï"); KEY("ö"); KEY("ü"); KEY("æ"); KEY("ø"); KEY("å");
2876                 KEY("á"); KEY("é"); KEY("í"); KEY("ó"); KEY("ú"); KEY("š"); KEY("č"); KEY("ž");
2877                 KEY("à"); KEY("è"); KEY("ì"); KEY("ò"); KEY("ù"); KEY("ś"); KEY("ć"); KEY("ź");
2878                 KEY("â"); KEY("ê"); KEY("î"); KEY("ô"); KEY("û"); KEY("ß");
2879
2880                 wk=gui_internal_keyboard_key_data(this, wkbd, "a",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2881                 wk->datai=mode-24;
2882
2883                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2884         }
2885         gui_internal_widget_append(wkbdb, wkbd);
2886         if (render) {
2887                 gui_internal_widget_pack(this, wkbdb);
2888                 gui_internal_widget_render(this, wkbdb);
2889         }
2890         return wkbdb;
2891 }
2892 #undef KEY
2893 #undef SPACER
2894
2895 static struct widget *
2896 gui_internal_keyboard(struct gui_priv *this, int mode)
2897 {
2898         if (! this->keyboard)
2899                 return NULL;
2900         return gui_internal_keyboard_do(this, NULL, mode);
2901 }
2902
2903 static void
2904 gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data)
2905 {
2906         gui_internal_keyboard_do(this, key->data, key->datai);
2907 }
2908
2909 static void
2910 gui_internal_search_list_set_default_country(struct gui_priv *this)
2911 {
2912         struct attr search_attr, country_name, country_iso2, *country_attr;
2913         struct item *item;
2914         struct country_search *cs;
2915         struct tracking *tracking;
2916         struct search_list_result *res;
2917
2918         country_attr=country_default();
2919         tracking=navit_get_tracking(this->nav);
2920         if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
2921                 country_attr=&search_attr;
2922         if (country_attr) {
2923                 cs=country_search_new(country_attr, 0);
2924                 item=country_search_get_item(cs);
2925                 if (item && item_attr_get(item, attr_country_name, &country_name)) {
2926                         search_attr.type=attr_country_all;
2927                         dbg(0,"country %s\n", country_name.u.str);
2928                         search_attr.u.str=country_name.u.str;
2929                         search_list_search(this->sl, &search_attr, 0);
2930                         while((res=search_list_get_result(this->sl)));
2931                         g_free(this->country_iso2);
2932                         if (item_attr_get(item, attr_country_iso2, &country_iso2))
2933                                 this->country_iso2=g_strdup(country_iso2.u.str);
2934                 }
2935                 country_search_destroy(cs);
2936         } else {
2937                 dbg(0,"warning: no default country found\n");
2938                 if (this->country_iso2) {
2939                     dbg(0,"attempting to use country '%s'\n",this->country_iso2);
2940                     search_attr.type=attr_country_iso2;
2941                     search_attr.u.str=this->country_iso2;
2942             search_list_search(this->sl, &search_attr, 0);
2943             while((res=search_list_get_result(this->sl)));
2944                 }
2945         }
2946 }
2947
2948 static void
2949 gui_internal_search_list_new(struct gui_priv *this)
2950 {
2951         struct mapset *ms=navit_get_mapset(this->nav);
2952         if (! this->sl) {
2953                 this->sl=search_list_new(ms);
2954                 gui_internal_search_list_set_default_country(this);
2955         }
2956 }
2957
2958 static void
2959 gui_internal_search_list_destroy(struct gui_priv *this)
2960 {
2961         if (this->sl) {
2962                 search_list_destroy(this->sl);
2963                 this->sl=NULL;
2964         }
2965 }
2966
2967
2968 static void
2969 gui_internal_search(struct gui_priv *this, char *what, char *type, int flags)
2970 {
2971         struct widget *wb,*wk,*w,*wr,*we,*wl,*wnext=NULL;
2972         char *country;
2973         gui_internal_search_list_new(this);
2974         wb=gui_internal_menu(this, what);
2975         w=gui_internal_box_new(this, gravity_center|orientation_vertical|flags_expand|flags_fill);
2976         gui_internal_widget_append(wb, w);
2977         wr=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2978         gui_internal_widget_append(w, wr);
2979         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2980         gui_internal_widget_append(wr, we);
2981
2982         if (!strcmp(type,"Country")) {
2983                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_town"));
2984                 wnext->func=gui_internal_search_town;
2985         } else if (!strcmp(type,"Town")) {
2986                 if (this->country_iso2) {
2987 #if HAVE_API_ANDROID
2988                         char country_iso2[strlen(this->country_iso2)+1];
2989                         strtolower(country_iso2, this->country_iso2);
2990                         country=g_strdup_printf("country_%s", country_iso2);
2991 #else
2992                         country=g_strdup_printf("country_%s", this->country_iso2);
2993 #endif
2994                 } else
2995                         country=strdup("gui_select_country");
2996                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, country)));
2997                 wb->state |= STATE_SENSITIVE;
2998                 if (flags)
2999                         wb->func = gui_internal_search_country;
3000                 else
3001                         wb->func = gui_internal_back;
3002                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_street"));
3003                 wnext->func=gui_internal_search_street;
3004                 g_free(country);
3005         } else if (!strcmp(type,"Street")) {
3006                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_town")));
3007                 wb->state |= STATE_SENSITIVE;
3008                 wb->func = gui_internal_back;
3009                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_house_number"));
3010                 wnext->func=gui_internal_search_house_number;
3011         } else if (!strcmp(type,"House number")) {
3012                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_street")));
3013                 wb->state |= STATE_SENSITIVE;
3014                 wb->func = gui_internal_back;
3015         }
3016         gui_internal_widget_append(we, wk=gui_internal_label_new(this, NULL));
3017         if (wnext) {
3018                 gui_internal_widget_append(we, wnext);
3019                 wnext->state |= STATE_SENSITIVE;
3020         }
3021         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3022         gui_internal_widget_append(wr, wl);
3023         gui_internal_menu_data(this)->search_list=wl;
3024         wk->state |= STATE_EDIT;
3025         wk->background=this->background;
3026         wk->flags |= flags_expand|flags_fill;
3027         wk->func = gui_internal_search_changed;
3028         wk->name=g_strdup(type);
3029         if (this->keyboard)
3030                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
3031         gui_internal_menu_render(this);
3032 }
3033
3034 static void
3035 gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data)
3036 {
3037         search_list_select(this->sl, attr_street_name, 0, 0);
3038         gui_internal_search(this,_("House number"),"House number",0);
3039 }
3040
3041 static void
3042 gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data)
3043 {
3044         dbg(0,"id %d\n", widget->selection_id);
3045         search_list_select(this->sl, attr_street_name, 0, 0);
3046         search_list_select(this->sl, attr_street_name, widget->selection_id, 1);
3047         gui_internal_search(this,_("House number"),"House number",0);
3048 }
3049
3050 static void
3051 gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data)
3052 {
3053         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
3054         gui_internal_search(this,_("Street"),"Street",0);
3055 }
3056
3057 static void
3058 gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data)
3059 {
3060         dbg(0,"id %d\n", widget->selection_id);
3061         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
3062         search_list_select(this->sl, attr_town_or_district_name, widget->selection_id, 1);
3063         gui_internal_search(this,_("Street"),"Street",0);
3064 }
3065
3066 static void
3067 gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data)
3068 {
3069         if (this->sl)
3070                 search_list_select(this->sl, attr_country_all, 0, 0);
3071         g_free(this->country_iso2);
3072         this->country_iso2=NULL;
3073         gui_internal_search(this,_("Town"),"Town",0);
3074 }
3075
3076 static void
3077 gui_internal_search_town_in_country(struct gui_priv *this, struct widget *widget)
3078 {
3079         struct search_list_common *slc;
3080         dbg(0,"id %d\n", widget->selection_id);
3081         search_list_select(this->sl, attr_country_all, 0, 0);
3082         slc=search_list_select(this->sl, attr_country_all, widget->selection_id, 1);
3083         if (slc) {
3084                 g_free(this->country_iso2);
3085                 this->country_iso2=((struct search_list_country *)slc)->iso2;
3086         }
3087         gui_internal_search(this,widget->name,"Town",0);
3088 }
3089
3090 static void
3091 gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data)
3092 {
3093         gui_internal_prune_menu_count(this, 1, 0);
3094         gui_internal_search(this,_("Country"),"Country",0);
3095 }
3096
3097 static void
3098 gui_internal_cmd2_town(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3099 {
3100         if (this->sl)
3101                 search_list_select(this->sl, attr_country_all, 0, 0);
3102         gui_internal_search(this,_("Town"),"Town",1);
3103 }
3104
3105 static void
3106 gui_internal_cmd2_setting_layout(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3107 {
3108         struct attr attr;
3109         struct widget *w,*wb,*wl;
3110         struct attr_iter *iter;
3111
3112
3113         wb=gui_internal_menu(this, _("Layout"));
3114         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3115         w->spy=this->spacing*3;
3116         gui_internal_widget_append(wb, w);
3117         iter=navit_attr_iter_new();
3118         while(navit_get_attr(this->nav, attr_layout, &attr, iter)) {
3119                 wl=gui_internal_button_navit_attr_new(this, attr.u.layout->name, gravity_left_center|orientation_horizontal|flags_fill,
3120                         &attr, NULL);
3121                 gui_internal_widget_append(w, wl);
3122         }
3123         navit_attr_iter_destroy(iter);
3124         gui_internal_menu_render(this);
3125 }
3126
3127 static void
3128 gui_internal_cmd2_quit(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3129 {
3130         struct attr navit;
3131         navit.type=attr_navit;
3132         navit.u.navit=this->nav;
3133         navit_destroy(navit.u.navit);
3134         config_remove_attr(config, &navit);
3135         event_main_loop_quit();
3136 }
3137
3138 static void
3139 gui_internal_window_closed(struct gui_priv *this)
3140 {
3141         gui_internal_cmd2_quit(this, NULL, NULL, NULL, NULL);
3142 }
3143
3144 static void
3145 gui_internal_cmd2_abort_navigation(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3146 {
3147         navit_set_destination(this->nav, NULL, NULL, 0);
3148 }
3149
3150
3151 static void
3152 gui_internal_cmd2_setting_maps(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3153 {
3154         struct attr attr, on, off, description, type, data;
3155         struct widget *w,*wb,*wma;
3156         char *label;
3157         struct attr_iter *iter;
3158
3159
3160         wb=gui_internal_menu(this, _("Maps"));
3161         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3162         w->spy=this->spacing*3;
3163         gui_internal_widget_append(wb, w);
3164         iter=navit_attr_iter_new();
3165         on.type=off.type=attr_active;
3166         on.u.num=1;
3167         off.u.num=0;
3168         while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
3169                 if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
3170                         label=g_strdup(description.u.str);
3171                 } else {
3172                         if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
3173                                 type.u.str="";
3174                         if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
3175                                 data.u.str="";
3176                         label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
3177                 }
3178                 wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
3179                         attr.u.map, &on, &off, 1);
3180                 gui_internal_widget_append(w, wma);
3181                 g_free(label);
3182         }
3183         navit_attr_iter_destroy(iter);
3184         gui_internal_menu_render(this);
3185
3186 }
3187 static void
3188 gui_internal_cmd_set_active_vehicle(struct gui_priv *this, struct widget *wm, void *data)
3189 {
3190         struct attr vehicle = {attr_vehicle,{wm->data}};
3191         navit_set_attr(this->nav, &vehicle);
3192 }
3193
3194 static void
3195 gui_internal_cmd_show_satellite_status(struct gui_priv *this, struct widget *wm, void *data)
3196 {
3197         struct widget *w,*wb,*row;
3198         struct attr attr,sat_attr;
3199         struct vehicle *v=wm->data;
3200         char *str;
3201         int i;
3202         enum attr_type types[]={attr_sat_prn, attr_sat_elevation, attr_sat_azimuth, attr_sat_snr};
3203
3204         wb=gui_internal_menu(this, _("Show Satellite Status"));
3205         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_satellite_status;
3206         gui_internal_menu_data(this)->redisplay_widget=wm;
3207         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3208         gui_internal_widget_append(wb, w);
3209         w = gui_internal_widget_table_new(this,gravity_center | orientation_vertical | flags_expand | flags_fill, 0);
3210         row = gui_internal_widget_table_row_new(this,gravity_left_top);
3211         gui_internal_widget_append(row, gui_internal_label_new(this, _(" PRN ")));
3212         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Elevation ")));
3213         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Azimuth ")));
3214         gui_internal_widget_append(row, gui_internal_label_new(this, _(" SNR ")));
3215         gui_internal_widget_append(w,row);
3216         while (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
3217                 row = gui_internal_widget_table_row_new(this,gravity_left_top);
3218                 for (i = 0 ; i < sizeof(types)/sizeof(enum attr_type) ; i++) {
3219                         if (item_attr_get(attr.u.item, types[i], &sat_attr))
3220                                 str=g_strdup_printf("%d", sat_attr.u.num);
3221                         else
3222                                 str=g_strdup("");
3223                         gui_internal_widget_append(row, gui_internal_label_new(this, str));
3224                         g_free(str);
3225                 }
3226                 gui_internal_widget_append(w,row);
3227         }
3228         gui_internal_widget_append(wb, w);
3229         gui_internal_menu_render(this);
3230 }
3231
3232 static void
3233 gui_internal_cmd_show_nmea_data(struct gui_priv *this, struct widget *wm, void *data)
3234 {
3235         struct widget *w,*wb;
3236         struct attr attr;
3237         struct vehicle *v=wm->data;
3238         wb=gui_internal_menu(this, _("Show NMEA Data"));
3239         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_nmea_data;
3240         gui_internal_menu_data(this)->redisplay_widget=wm;
3241         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3242         gui_internal_widget_append(wb, w);
3243         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL))
3244                 gui_internal_widget_append(w, gui_internal_text_new(this, attr.u.str, gravity_left_center|orientation_vertical));
3245         gui_internal_menu_render(this);
3246 }
3247
3248 /**
3249  * A container to hold the selected vehicle and the desired profile in
3250  * one data item.
3251  */
3252 struct vehicle_and_profilename {
3253         struct vehicle *vehicle;
3254         char *profilename;
3255 };
3256
3257 /**
3258  * Figures out whether the given vehicle is the active vehicle.
3259  *
3260  * @return true if the vehicle is active, false otherwise.
3261  */
3262 static int
3263 gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle
3264         *vehicle)
3265 {
3266         struct attr active_vehicle;
3267
3268         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
3269         active_vehicle.u.vehicle=NULL;
3270
3271         return active_vehicle.u.vehicle == vehicle;
3272 }
3273
3274 static void
3275 save_vehicle_xml(struct vehicle *v)
3276 {
3277         struct attr attr;
3278         struct attr_iter *iter=vehicle_attr_iter_new();
3279         int childs=0;
3280         dbg(0,"enter\n");
3281         printf("<vehicle");
3282         while (vehicle_get_attr(v, attr_any_xml, &attr, iter)) {
3283                 if (ATTR_IS_OBJECT(attr.type))
3284                         childs=1;
3285                 else
3286                         printf(" %s=\"%s\"",attr_to_name(attr.type),attr_to_text(&attr, NULL, 1));
3287         }
3288         if (childs) {
3289                 printf(">\n");
3290                 printf("</vehicle>\n");
3291         } else
3292                 printf(" />\n");
3293         vehicle_attr_iter_destroy(iter);
3294 }
3295
3296
3297 /**
3298  * Reacts to a button press that changes a vehicle's active profile.
3299  *
3300  * @see gui_internal_add_vehicle_profile
3301  */
3302 static void
3303 gui_internal_cmd_set_active_profile(struct gui_priv *this, struct
3304                 widget *wm, void *data)
3305 {
3306         struct vehicle_and_profilename *vapn = data;
3307         struct vehicle *v = vapn->vehicle;
3308         char *profilename = vapn->profilename;
3309         struct attr vehicle_name_attr;
3310         char *vehicle_name = NULL;
3311
3312         // Get the vehicle name
3313         vehicle_get_attr(v, attr_name, &vehicle_name_attr, NULL);
3314         vehicle_name = vehicle_name_attr.u.str;
3315
3316         dbg(0, "Changing vehicle %s to profile %s\n", vehicle_name,
3317                         profilename);
3318
3319         // Change the profile name
3320         struct attr profilename_attr = {attr_profilename, {profilename}};
3321         if(!vehicle_set_attr(v, &profilename_attr)) {
3322                 dbg(0, "Unable to set the vehicle's profile name\n");
3323         }
3324
3325     // Notify Navit that the routing should be re-done if this is the
3326     // active vehicle.
3327         if (gui_internal_is_active_vehicle(this, v)) {
3328                 struct attr vehicle;
3329                 vehicle.type=attr_vehicle;
3330                 vehicle.u.vehicle=v;
3331                 navit_set_attr(this->nav, &vehicle);
3332         }
3333         save_vehicle_xml(v);
3334 }
3335
3336 /**
3337  * Adds the vehicle profile to the GUI, allowing the user to pick a
3338  * profile for the currently selected vehicle.
3339  */
3340 static void
3341 gui_internal_add_vehicle_profile(struct gui_priv *this, struct widget
3342                 *parent, struct vehicle *v, struct vehicleprofile *profile)
3343 {
3344         // Just here to show up in the translation file, nice and close to
3345         // where the translations are actually used.
3346         struct attr profile_attr;
3347         struct attr *attr = NULL;
3348         char *name = NULL;
3349         char *active_profile = NULL;
3350         char *label = NULL;
3351         int active;
3352         struct vehicle_and_profilename *context = NULL;
3353
3354 #ifdef ONLY_FOR_TRANSLATION
3355         char *translations[] = {_n("car"), _n("bike"), _n("pedestrian")};
3356 #endif
3357
3358         // Figure out the profile name
3359         attr = attr_search(profile->attrs, NULL, attr_name);
3360         name = attr->u.str;
3361
3362         // Determine whether the profile is the active one
3363         vehicle_get_attr(v, attr_profilename, &profile_attr, NULL);
3364         active_profile = profile_attr.u.str;
3365         active = strcmp(name, active_profile) == 0;
3366
3367         dbg(0, "Adding vehicle profile %s, active=%s/%i\n", name,
3368                         active_profile, active);
3369
3370         // Build a translatable label.
3371         if(active) {
3372                 label = g_strdup_printf(_("Current profile: %s"), _(name));
3373         } else {
3374                 label = g_strdup_printf(_("Change profile to: %s"), _(name));
3375         }
3376
3377         // Create the context object (the vehicle and the desired profile)
3378         context = g_new0(struct vehicle_and_profilename, 1);
3379         context->vehicle = v;
3380         context->profilename = name;
3381
3382         // Add the button
3383         gui_internal_widget_append(parent,
3384                 gui_internal_button_new_with_callback(
3385                         this, label,
3386                         image_new_xs(this, active ? "gui_active" : "gui_inactive"),
3387                         gravity_left_center|orientation_horizontal|flags_fill,
3388                         gui_internal_cmd_set_active_profile, context));
3389
3390         free(label);
3391 }
3392
3393 static void
3394 gui_internal_cmd_vehicle_settings(struct gui_priv *this, struct widget *wm, void *data)
3395 {
3396         struct widget *w,*wb;
3397         struct attr attr;
3398         struct vehicle *v=wm->data;
3399     struct vehicleprofile *profile = NULL;
3400
3401         wb=gui_internal_menu(this, wm->text);
3402         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3403         gui_internal_widget_append(wb, w);
3404
3405     // Add the "Set as active" button if this isn't the active
3406     // vehicle.
3407         if (!gui_internal_is_active_vehicle(this, v)) {
3408                 gui_internal_widget_append(w,
3409                         gui_internal_button_new_with_callback(this, _("Set as active"),
3410                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3411                                 gui_internal_cmd_set_active_vehicle, wm->data));
3412         }
3413
3414         if (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
3415                 gui_internal_widget_append(w,
3416                         gui_internal_button_new_with_callback(this, _("Show Satellite status"),
3417                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3418                                 gui_internal_cmd_show_satellite_status, wm->data));
3419         }
3420         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL)) {
3421                 gui_internal_widget_append(w,
3422                         gui_internal_button_new_with_callback(this, _("Show NMEA data"),
3423                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3424                                 gui_internal_cmd_show_nmea_data, wm->data));
3425         }
3426
3427     // Add all the possible vehicle profiles to the menu
3428         GList *profiles = navit_get_vehicleprofiles(this->nav);
3429     while(profiles) {
3430         profile = (struct vehicleprofile *)profiles->data;
3431         gui_internal_add_vehicle_profile(this, w, v, profile);
3432                 profiles = g_list_next(profiles);
3433     }
3434
3435         callback_list_call_attr_2(this->cbl, attr_vehicle, w, wm->data);
3436         gui_internal_menu_render(this);
3437 }
3438
3439 static void
3440 gui_internal_cmd2_setting_vehicle(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3441 {
3442         struct attr attr,vattr;
3443         struct widget *w,*wb,*wl;
3444         struct attr_iter *iter;
3445         struct attr active_vehicle;
3446
3447
3448         wb=gui_internal_menu(this, _("Vehicle"));
3449         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3450         w->spy=this->spacing*3;
3451         gui_internal_widget_append(wb, w);
3452         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
3453                 active_vehicle.u.vehicle=NULL;
3454         iter=navit_attr_iter_new();
3455         while(navit_get_attr(this->nav, attr_vehicle, &attr, iter)) {
3456                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
3457                 wl=gui_internal_button_new_with_callback(this, vattr.u.str,
3458                         image_new_l(this, attr.u.vehicle == active_vehicle.u.vehicle ? "gui_active" : "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
3459                         gui_internal_cmd_vehicle_settings, attr.u.vehicle);
3460                 wl->text=g_strdup(vattr.u.str);
3461                 gui_internal_widget_append(w, wl);
3462         }
3463         navit_attr_iter_destroy(iter);
3464         gui_internal_menu_render(this);
3465 }
3466
3467
3468 static void
3469 gui_internal_cmd2_setting_rules(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3470 {
3471         struct widget *wb,*w;
3472         struct attr on,off;
3473         wb=gui_internal_menu(this, _("Rules"));
3474         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3475         w->spy=this->spacing*3;
3476         gui_internal_widget_append(wb, w);
3477         on.u.num=1;
3478         off.u.num=0;
3479         on.type=off.type=attr_tracking;
3480         gui_internal_widget_append(w,
3481                 gui_internal_button_navit_attr_new(this, _("Lock on road"), gravity_left_center|orientation_horizontal|flags_fill,
3482                         &on, &off));
3483         on.u.num=0;
3484         off.u.num=-1;
3485         on.type=off.type=attr_orientation;
3486         gui_internal_widget_append(w,
3487                 gui_internal_button_navit_attr_new(this, _("Northing"), gravity_left_center|orientation_horizontal|flags_fill,
3488                         &on, &off));
3489         on.u.num=1;
3490         off.u.num=0;
3491         on.type=off.type=attr_follow_cursor;
3492         gui_internal_widget_append(w,
3493                 gui_internal_button_navit_attr_new(this, _("Map follows Vehicle"), gravity_left_center|orientation_horizontal|flags_fill,
3494                         &on, &off));
3495         gui_internal_menu_render(this);
3496 }
3497
3498 //##############################################################################################################
3499 //# Description:
3500 //# Comment:
3501 //# Authors: Martin Schaller (04/2008)
3502 //##############################################################################################################
3503 static void gui_internal_motion(void *data, struct point *p)
3504 {
3505
3506         struct gui_priv *this=data;
3507         if (!this->root.children) {
3508                 navit_handle_motion(this->nav, p);
3509                 return;
3510         }
3511         if (!this->pressed)
3512                 return;
3513         this->current=*p;
3514         if(!this->motion_timeout_callback)
3515                 this->motion_timeout_callback=callback_new_1(callback_cast(gui_internal_highlight), this);
3516         if(!this->motion_timeout_event)
3517                 this->motion_timeout_event=event_add_timeout(100,0, this->motion_timeout_callback);
3518 }
3519
3520 static const char *
3521 find_attr(const char **names, const char **values, const char *name)
3522 {
3523         while (*names) {
3524                 if (!strcasecmp(*names, name))
3525                         return *values;
3526                 names+=xml_attr_distance;
3527                 values+=xml_attr_distance;
3528         }
3529         return NULL;
3530 }
3531
3532 static char *
3533 find_attr_dup(const char **names, const char **values, const char *name)
3534 {
3535         return g_strdup(find_attr(names, values, name));
3536 }
3537
3538 static void
3539 gui_internal_evaluate(struct gui_priv *this, const char *command)
3540 {
3541         if (command)
3542                 command_evaluate(&this->self, command);
3543 }
3544
3545
3546 static void
3547 gui_internal_html_command(struct gui_priv *this, struct widget *w, void *data)
3548 {
3549         gui_internal_evaluate(this,w->command);
3550 }
3551
3552 static void
3553 gui_internal_html_href(struct gui_priv *this, struct widget *w, void *data)
3554 {
3555         if (w->command && w->command[0] == '#') {
3556                 dbg(1,"href=%s\n",w->command);
3557                 gui_internal_html_menu(this, this->html_text, w->command+1);
3558         }
3559 }
3560
3561
3562 static void
3563 gui_internal_html_start(void *dummy, const char *tag_name, const char **names, const char **values, void *data, void *error)
3564 {
3565         struct gui_priv *this=data;
3566         int i;
3567         enum html_tag tag=html_tag_none;
3568         struct html *html=&this->html[this->html_depth];
3569         const char *src, *cond;
3570
3571         if (!strcasecmp(tag_name,"text"))
3572                 return;
3573         html->command=NULL;
3574         html->name=NULL;
3575         html->href=NULL;
3576         html->skip=0;
3577         cond=find_attr(names, values, "cond");
3578         
3579         if (cond && !this->html_skip) {
3580                 if (!command_evaluate_to_boolean(&this->self, cond, NULL)) 
3581                         html->skip=1;
3582         }
3583
3584         for (i=0 ; i < sizeof(html_tag_map)/sizeof(struct html_tag_map); i++) {
3585                 if (!strcasecmp(html_tag_map[i].tag_name, tag_name)) {
3586                         tag=html_tag_map[i].tag;
3587                         break;
3588                 }
3589         }
3590         html->tag=tag;
3591         if (!this->html_skip && !html->skip) {
3592                 switch (tag) {
3593                 case html_tag_a:
3594                         html->name=find_attr_dup(names, values, "name");
3595                         if (html->name) {
3596                                 html->skip=this->html_anchor ? strcmp(html->name,this->html_anchor) : 0;
3597                                 if (!html->skip)
3598                                         this->html_anchor_found=1;
3599                         }
3600                         html->href=find_attr_dup(names, values, "href");
3601                         break;
3602                 case html_tag_img:
3603                         html->command=find_attr_dup(names, values, "onclick");
3604                         src=find_attr(names, values, "src");
3605                         if (src)
3606                                 html->w=gui_internal_image_new(this, image_new_l(this, src));
3607                         break;
3608                 default:
3609                         break;
3610                 }
3611         }
3612         this->html_skip+=html->skip;
3613         this->html_depth++;
3614 }
3615
3616 static void
3617 gui_internal_html_end(void *dummy, const char *tag_name, void *data, void *error)
3618 {
3619         struct gui_priv *this=data;
3620         struct html *html;
3621         struct html *parent=NULL;
3622
3623         if (!strcasecmp(tag_name,"text"))
3624                 return;
3625         this->html_depth--;
3626         html=&this->html[this->html_depth];
3627         if (this->html_depth > 0)
3628                 parent=&this->html[this->html_depth-1];
3629         
3630
3631         if (!this->html_skip) { 
3632                 if (html->command && html->w) {
3633                         html->w->state |= STATE_SENSITIVE;
3634                         html->w->command=html->command;
3635                         html->w->func=gui_internal_html_command;
3636                         html->command=NULL;
3637                 }
3638                 if (parent && parent->href && html->w) {
3639                         html->w->state |= STATE_SENSITIVE;
3640                         html->w->command=g_strdup(parent->href);
3641                         html->w->func=gui_internal_html_href;
3642                 }
3643                 switch (html->tag) {
3644                 case html_tag_img:
3645                         gui_internal_widget_append(this->html_container, html->w);
3646                         break;
3647                 default:
3648                         break;
3649                 }
3650         }
3651         this->html_skip-=html->skip;
3652         g_free(html->command);
3653         g_free(html->name);
3654         g_free(html->href);
3655 }
3656
3657 static void
3658 gui_internal_html_text(void *dummy, const char *text, int len, void *data, void *error)
3659 {
3660         struct gui_priv *this=data;
3661         struct widget *w;
3662         int depth=this->html_depth-1;
3663         struct html *html=&this->html[depth];
3664         char *text_stripped;
3665
3666         if (this->html_skip)
3667                 return;
3668         while (isspace(text[0])) {
3669                 text++;
3670                 len--;
3671         }
3672         while (len > 0 && isspace(text[len-1]))
3673                 len--;
3674         text_stripped=g_malloc(len+1);
3675         strncpy(text_stripped, text, len);
3676         text_stripped[len]='\0';
3677         if (html->tag == html_tag_html && depth > 2) {
3678                 if (this->html[depth-1].tag == html_tag_script) {
3679                         html=&this->html[depth-2];
3680                 }
3681         }
3682         switch (html->tag) {
3683         case html_tag_a:
3684                 if (html->name && len) {
3685                         this->html_container=gui_internal_menu(this, gettext(text_stripped));
3686                         this->html_container->spx=this->spacing*10;
3687                 }
3688                 break;
3689         case html_tag_h1:
3690                 if (!this->html_container) {
3691                         this->html_container=gui_internal_menu(this, gettext(text_stripped));
3692                         this->html_container->spx=this->spacing*10;
3693                 }
3694                 break;
3695         case html_tag_img:
3696                 if (len) {
3697                         w=gui_internal_box_new(this, gravity_center|orientation_vertical);
3698                         gui_internal_widget_append(w, html->w);
3699                         gui_internal_widget_append(w, gui_internal_text_new(this, gettext(text_stripped), gravity_center|orientation_vertical));
3700                         html->w=w;
3701                 }
3702                 break;
3703         case html_tag_script:
3704                 dbg(1,"execute %s\n",text_stripped);
3705                 gui_internal_evaluate(this,text_stripped);
3706                 break;
3707         default:
3708                 break;
3709         }
3710 }
3711
3712 static void
3713 gui_internal_html_menu(struct gui_priv *this, const char *document, char *anchor)
3714 {
3715         graphics_draw_mode(this->gra, draw_mode_begin);
3716         this->html_container=NULL;
3717         this->html_depth=0;
3718         this->html_anchor=anchor;
3719         this->html_anchor_found=0;
3720         xml_parse_text(document, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
3721         gui_internal_menu_render(this);
3722         graphics_draw_mode(this->gra, draw_mode_end);
3723 }
3724
3725
3726 static void gui_internal_menu_root(struct gui_priv *this)
3727 {
3728         gui_internal_html_menu(this, this->html_text, "Main Menu");
3729 }
3730
3731 static void
3732 gui_internal_enter(struct gui_priv *this, int ignore)
3733 {
3734         struct graphics *gra=this->gra;
3735         this->ignore_button=ignore;
3736         this->clickp_valid=this->vehicle_valid=0;
3737
3738         navit_block(this->nav, 1);
3739         graphics_overlay_disable(gra, 1);
3740         this->root.p.x=0;
3741         this->root.p.y=0;
3742         this->root.background=this->background;
3743 }
3744
3745 static void
3746 gui_internal_leave(struct gui_priv *this)
3747 {
3748         graphics_draw_mode(this->gra, draw_mode_end);
3749 }
3750
3751 static void
3752 gui_internal_cmd_menu(struct gui_priv *this, struct point *p, int ignore)
3753 {
3754         struct transformation *trans;
3755         struct coord c;
3756         struct coord_geo g;
3757         struct attr attr,attrp;
3758
3759         dbg(1,"enter\n");
3760         gui_internal_enter(this, ignore);
3761         trans=navit_get_trans(this->nav);
3762         attr_free(this->click_coord_geo);
3763         this->click_coord_geo=NULL;
3764         attr_free(this->position_coord_geo);
3765         this->position_coord_geo=NULL;
3766         if (p) {
3767                 transform_reverse(trans, p, &c);
3768                 dbg(1,"x=0x%x y=0x%x\n", c.x, c.y);
3769                 this->clickp.pro=transform_get_projection(trans);
3770                 this->clickp.x=c.x;
3771                 this->clickp.y=c.y;
3772                 transform_to_geo(this->clickp.pro, &c, &g);
3773                 attr.u.coord_geo=&g;
3774                 attr.type=attr_click_coord_geo;
3775                 this->click_coord_geo=attr_dup(&attr);
3776         }
3777         if (navit_get_attr(this->nav, attr_vehicle, &attr, NULL) && attr.u.vehicle
3778                 && vehicle_get_attr(attr.u.vehicle, attr_position_coord_geo, &attrp, NULL)) {
3779                 this->position_coord_geo=attr_dup(&attrp);
3780                 this->vehiclep.pro=transform_get_projection(trans);
3781                 transform_from_geo(this->vehiclep.pro, attrp.u.coord_geo, &c);
3782                 this->vehiclep.x=c.x;
3783                 this->vehiclep.y=c.y;
3784                 this->vehicle_valid=1;
3785         }
3786         // draw menu
3787         gui_internal_menu_root(this);
3788 }
3789
3790 static void
3791 gui_internal_cmd_menu2(struct gui_priv *this)
3792 {
3793         gui_internal_cmd_menu(this, NULL, 1);
3794 }
3795
3796
3797 static void
3798 gui_internal_cmd_log_do(struct gui_priv *this, struct widget *widget)
3799 {
3800         if (widget->text && strlen(widget->text))
3801                 navit_textfile_debug_log(this->nav, "type=log_entry label=\"%s\"",widget->text);
3802         g_free(widget->text);
3803         widget->text=NULL;
3804         gui_internal_prune_menu(this, NULL);
3805         gui_internal_check_exit(this);
3806 }
3807
3808 static void
3809 gui_internal_cmd_log_clicked(struct gui_priv *this, struct widget *widget, void *data)
3810 {
3811         gui_internal_cmd_log_do(this, widget->data);
3812 }
3813
3814 static void
3815 gui_internal_cmd_log_changed(struct gui_priv *this, struct widget *wm, void *data)
3816 {
3817         int len;
3818         if (wm->text) {
3819                 len=strlen(wm->text);
3820                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
3821                         wm->text[len-1]='\0';
3822                         gui_internal_cmd_log_do(this, wm);
3823                 }
3824         }
3825 }
3826
3827
3828 static void
3829 gui_internal_cmd_log(struct gui_priv *this)
3830 {
3831         struct widget *w,*wb,*wk,*wl,*we,*wnext;
3832         gui_internal_enter(this, 1);
3833         wb=gui_internal_menu(this, "Log Message");
3834         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3835         gui_internal_widget_append(wb, w);
3836         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
3837         gui_internal_widget_append(w, we);
3838         gui_internal_widget_append(we, wk=gui_internal_label_new(this, _("Message")));
3839         wk->state |= STATE_EDIT|STATE_CLEAR;
3840         wk->background=this->background;
3841         wk->flags |= flags_expand|flags_fill;
3842         wk->func = gui_internal_cmd_log_changed;
3843         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
3844         wnext->state |= STATE_SENSITIVE;
3845         wnext->func = gui_internal_cmd_log_clicked;
3846         wnext->data=wk;
3847         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3848         gui_internal_widget_append(w, wl);
3849         if (this->keyboard)
3850                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
3851         gui_internal_menu_render(this);
3852         gui_internal_leave(this);
3853 }
3854
3855 static void
3856 gui_internal_check_exit(struct gui_priv *this)
3857 {
3858         struct graphics *gra=this->gra;
3859         if (! this->root.children) {
3860                 gui_internal_search_idle_end(this);
3861                 gui_internal_search_list_destroy(this);
3862                 graphics_overlay_disable(gra, 0);
3863                 if (!navit_block(this->nav, 0)) {
3864                         if (this->redraw)
3865                                 navit_draw(this->nav);
3866                         else
3867                                 navit_draw_displaylist(this->nav);
3868                 }
3869         }
3870 }
3871
3872 static int
3873 gui_internal_get_attr(struct gui_priv *this, enum attr_type type, struct attr *attr)
3874 {
3875         switch (type) {
3876         case attr_active:
3877                 attr->u.num=this->root.children != NULL;
3878                 break;
3879         case attr_click_coord_geo:
3880                 if (!this->click_coord_geo)
3881                         return 0;
3882                 *attr=*this->click_coord_geo;
3883                 break;
3884         case attr_position_coord_geo:
3885                 if (!this->position_coord_geo)
3886                         return 0;
3887                 *attr=*this->position_coord_geo;
3888                 break;
3889         case attr_pitch:
3890                 attr->u.num=this->pitch;
3891                 break;
3892         default:
3893                 return 0;
3894         }
3895         attr->type=type;
3896         return 1;
3897 }
3898
3899 static int
3900 gui_internal_add_attr(struct gui_priv *this, struct attr *attr)
3901 {
3902         switch (attr->type) {
3903         case attr_xml_text:
3904                 g_free(this->html_text);
3905                 this->html_text=g_strdup(attr->u.str);
3906                 return 1;
3907         default:
3908                 return 0;
3909         }
3910 }
3911
3912 static int
3913 gui_internal_set_attr(struct gui_priv *this, struct attr *attr)
3914 {
3915         switch (attr->type) {
3916         case attr_fullscreen:
3917                 if ((this->fullscreen > 0) != (attr->u.num > 0)) {
3918                         graphics_draw_mode(this->gra, draw_mode_end);
3919                         this->win->fullscreen(this->win, attr->u.num > 0);
3920                         graphics_draw_mode(this->gra, draw_mode_begin);
3921                 }
3922                 this->fullscreen=attr->u.num;
3923                 return 1;
3924         default:
3925                 dbg(0,"%s\n",attr_to_name(attr->type));
3926                 return 0;
3927         }
3928 }
3929
3930 static void gui_internal_dbus_signal(struct gui_priv *this, struct point *p)
3931 {
3932         struct displaylist_handle *dlh;
3933         struct displaylist *display;
3934         struct displayitem *di;
3935
3936         display=navit_get_displaylist(this->nav);
3937         dlh=graphics_displaylist_open(display);
3938         while ((di=graphics_displaylist_next(dlh))) {
3939                 struct item *item=graphics_displayitem_get_item(di);
3940                 if (item_is_point(*item) && graphics_displayitem_get_displayed(di) &&
3941                         graphics_displayitem_within_dist(display, di, p, 10)) {
3942                         struct map_rect *mr=map_rect_new(item->map, NULL);
3943                         struct item *itemo=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
3944                         struct attr attr;
3945                         if (item_attr_get(itemo, attr_data, &attr)) {
3946                                 struct attr cb,*attr_list[2];
3947                                 int valid=0;
3948                                 attr.type=attr_data;
3949                                 attr_list[0]=&attr;
3950                                 attr_list[1]=NULL;
3951                                 if (navit_get_attr(this->nav, attr_callback_list, &cb, NULL)) 
3952                                         callback_list_call_attr_4(cb.u.callback_list, attr_command, "dbus_send_signal", attr_list, NULL, &valid);
3953                         }
3954                         map_rect_destroy(mr);
3955                 }
3956         }
3957         graphics_displaylist_close(dlh);
3958 }
3959
3960
3961 //##############################################################################################################
3962 //# Description: Function to handle mouse clicks and scroll wheel movement
3963 //# Comment:
3964 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
3965 //##############################################################################################################
3966 static void gui_internal_button(void *data, int pressed, int button, struct point *p)
3967 {
3968         struct gui_priv *this=data;
3969         struct graphics *gra=this->gra;
3970
3971         dbg(1,"enter %d %d\n", pressed, button);
3972         // if still on the map (not in the menu, yet):
3973         dbg(1,"children=%p ignore_button=%d\n",this->root.children,this->ignore_button);
3974         if (!this->root.children || this->ignore_button) {
3975
3976                 this->ignore_button=0;
3977                 // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
3978                 if (!navit_handle_button(this->nav, pressed, button, p, NULL)) {
3979                         dbg(1,"navit has handled button\n");
3980                         return;
3981                 }
3982                 dbg(1,"menu_on_map_click=%d\n",this->menu_on_map_click);
3983                 if (button != 1)
3984                         return;
3985                 if (this->menu_on_map_click) {
3986                         gui_internal_cmd_menu(this, p, 0);
3987                         return;
3988                 }
3989                 if (this->signal_on_map_click) {
3990                         gui_internal_dbus_signal(this, p);
3991                         return;
3992                 }
3993                 return;
3994         }
3995
3996
3997         // if already in the menu:
3998         if (pressed) {
3999                 this->pressed=1;
4000                 this->current=*p;
4001                 gui_internal_highlight(this);
4002         } else {
4003                 this->pressed=0;
4004                 this->current.x=-1;
4005                 this->current.y=-1;
4006                 graphics_draw_mode(gra, draw_mode_begin);
4007                 gui_internal_call_highlighted(this);
4008                 gui_internal_highlight(this);
4009                 graphics_draw_mode(gra, draw_mode_end);
4010                 gui_internal_check_exit(this);
4011         }
4012 }
4013
4014 static void
4015 gui_internal_setup_gc(struct gui_priv *this)
4016 {
4017         struct color cbh={0x9fff,0x9fff,0x9fff,0xffff};
4018         struct color cf={0xbfff,0xbfff,0xbfff,0xffff};
4019         struct graphics *gra=this->gra;
4020
4021         if (this->background)
4022                 return;
4023         this->background=graphics_gc_new(gra);
4024         this->background2=graphics_gc_new(gra);
4025         this->highlight_background=graphics_gc_new(gra);
4026         graphics_gc_set_foreground(this->highlight_background, &cbh);
4027         this->foreground=graphics_gc_new(gra);
4028         graphics_gc_set_foreground(this->foreground, &cf);
4029         this->text_background=graphics_gc_new(gra);
4030         this->text_foreground=graphics_gc_new(gra);
4031         graphics_gc_set_foreground(this->background, &this->background_color);
4032         graphics_gc_set_foreground(this->background2, &this->background2_color);
4033         graphics_gc_set_foreground(this->text_background, &this->text_background_color);
4034         graphics_gc_set_foreground(this->text_foreground, &this->text_foreground_color);
4035 }
4036
4037 //##############################################################################################################
4038 //# Description:
4039 //# Comment:
4040 //# Authors: Martin Schaller (04/2008)
4041 //##############################################################################################################
4042 static void gui_internal_resize(void *data, int w, int h)
4043 {
4044         struct gui_priv *this=data;
4045         int changed=0;
4046
4047         gui_internal_setup_gc(this);
4048
4049         if (this->root.w != w || this->root.h != h) {
4050                 this->root.w=w;
4051                 this->root.h=h;
4052                 changed=1;
4053         }
4054         dbg(1,"w=%d h=%d children=%p\n", w, h, this->root.children);
4055         navit_handle_resize(this->nav, w, h);
4056         if (this->root.children && changed) {
4057                 gui_internal_prune_menu(this, NULL);
4058                 gui_internal_menu_root(this);
4059         }
4060 }
4061
4062 static void
4063 gui_internal_keynav_point(struct widget *w, int dx, int dy, struct point *p)
4064 {
4065         p->x=w->p.x+w->w/2;
4066         p->y=w->p.y+w->h/2;
4067         if (dx < 0)
4068                 p->x=w->p.x;
4069         if (dx > 0)
4070                 p->x=w->p.x+w->w;
4071         if (dy < 0)
4072                 p->y=w->p.y;
4073         if (dy > 0)
4074                 p->y=w->p.y+w->h;
4075 }
4076
4077 static void
4078 gui_internal_keynav_find_closest(struct widget *wi, struct point *p, int dx, int dy, int *distance, struct widget **result)
4079 {
4080         GList *l=wi->children;
4081         if (wi->state & STATE_SENSITIVE) {
4082                 int dist1,dist2;
4083                 struct point wp;
4084                 gui_internal_keynav_point(wi, -dx, -dy, &wp);
4085                 if (dx) {
4086                         dist1=(wp.x-p->x)*dx;
4087                         dist2=wp.y-p->y;
4088                 } else if (dy) {
4089                         dist1=(wp.y-p->y)*dy;
4090                         dist2=wp.x-p->x;
4091                 } else {
4092                         dist2=wp.x-p->x;
4093                         dist1=wp.y-p->y;
4094                         if (dist1 < 0)
4095                                 dist1=-dist1;
4096                 }
4097                 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);
4098                 if (dist1 >= 0) {
4099                         if (dist2 < 0)
4100                                 dist1-=dist2;
4101                         else
4102                                 dist1+=dist2;
4103                         if (dist1 < *distance) {
4104                                 *result=wi;
4105                                 *distance=dist1;
4106                         }
4107                 }
4108         }
4109         while (l) {
4110                 struct widget *child=l->data;
4111                 gui_internal_keynav_find_closest(child, p, dx, dy, distance, result);
4112                 l=g_list_next(l);
4113         }
4114 }
4115
4116 static void
4117 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy)
4118 {
4119         struct widget *result,*menu=g_list_last(this->root.children)->data;
4120         struct point p;
4121         int distance;
4122         if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
4123                 gui_internal_keynav_point(this->highlighted, dx, dy, &p);
4124         else {
4125                 p.x=0;
4126                 p.y=0;
4127                 distance=INT_MAX;
4128                 result=NULL;
4129                 gui_internal_keynav_find_closest(menu, &p, 0, 0, &distance, &result);
4130                 if (result) {
4131                         gui_internal_keynav_point(result, dx, dy, &p);
4132                         dbg(1,"result origin=%p p=%d,%d\n", result, p.x, p.y);
4133                 }
4134         }
4135         result=NULL;
4136         distance=INT_MAX;
4137         gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
4138         dbg(1,"result=%p\n", result);
4139         if (! result) {
4140                 if (dx < 0)
4141                         p.x=this->root.w;
4142                 if (dx > 0)
4143                         p.x=0;
4144                 if (dy < 0)
4145                         p.y=this->root.h;
4146                 if (dy > 0)
4147                         p.y=0;
4148                 result=NULL;
4149                 distance=INT_MAX;
4150                 gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
4151                 dbg(1,"wraparound result=%p\n", result);
4152         }
4153         gui_internal_highlight_do(this, result);
4154         if (result)
4155                 gui_internal_say(this, result, 1);
4156 }
4157
4158 //##############################################################################################################
4159 //# Description:
4160 //# Comment:
4161 //# Authors: Martin Schaller (04/2008)
4162 //##############################################################################################################
4163 static void gui_internal_keypress(void *data, char *key)
4164 {
4165         struct gui_priv *this=data;
4166         int w,h;
4167         struct point p;
4168         if (!this->root.children) {
4169                 transform_get_size(navit_get_trans(this->nav), &w, &h);
4170                 switch (*key) {
4171                 case NAVIT_KEY_UP:
4172                         p.x=w/2;
4173                         p.y=0;
4174                         navit_set_center_screen(this->nav, &p, 1);
4175                         break;
4176                 case NAVIT_KEY_DOWN:
4177                         p.x=w/2;
4178                         p.y=h;
4179                         navit_set_center_screen(this->nav, &p, 1);
4180                         break;
4181                 case NAVIT_KEY_LEFT:
4182                         p.x=0;
4183                         p.y=h/2;
4184                         navit_set_center_screen(this->nav, &p, 1);
4185                         break;
4186                 case NAVIT_KEY_RIGHT:
4187                         p.x=w;
4188                         p.y=h/2;
4189                         navit_set_center_screen(this->nav, &p, 1);
4190                         break;
4191                 case NAVIT_KEY_ZOOM_IN:
4192                         navit_zoom_in(this->nav, 2, NULL);
4193                         break;
4194                 case NAVIT_KEY_ZOOM_OUT:
4195                         navit_zoom_out(this->nav, 2, NULL);
4196                         break;
4197                 case NAVIT_KEY_RETURN:
4198                 case NAVIT_KEY_MENU:
4199                         gui_internal_cmd_menu(this, NULL, 0);
4200                         break;
4201                 }
4202                 return;
4203         }
4204         graphics_draw_mode(this->gra, draw_mode_begin);
4205         switch (*key) {
4206         case NAVIT_KEY_LEFT:
4207                 gui_internal_keynav_highlight_next(this,-1,0);
4208                 break;
4209         case NAVIT_KEY_RIGHT:
4210                 gui_internal_keynav_highlight_next(this,1,0);
4211                 break;
4212         case NAVIT_KEY_UP:
4213                 gui_internal_keynav_highlight_next(this,0,-1);
4214                 break;
4215         case NAVIT_KEY_DOWN:
4216                 gui_internal_keynav_highlight_next(this,0,1);
4217                 break;
4218         case NAVIT_KEY_BACK:
4219                 break;
4220         case NAVIT_KEY_RETURN:
4221                 if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
4222                         gui_internal_call_highlighted(this);
4223                 else
4224                         gui_internal_keypress_do(this, key);
4225                 break;
4226         default:
4227                 gui_internal_keypress_do(this, key);
4228         }
4229         graphics_draw_mode(this->gra, draw_mode_end);
4230         gui_internal_check_exit(this);
4231 }
4232
4233
4234 //##############################################################################################################
4235 //# Description:
4236 //# Comment:
4237 //# Authors: Martin Schaller (04/2008)
4238 //##############################################################################################################
4239 static int gui_internal_set_graphics(struct gui_priv *this, struct graphics *gra)
4240 {
4241         struct window *win;
4242         struct transformation *trans=navit_get_trans(this->nav);
4243
4244         win=graphics_get_data(gra, "window");
4245         if (! win)
4246                 return 1;
4247         navit_ignore_graphics_events(this->nav, 1);
4248         this->gra=gra;
4249         this->win=win;
4250         navit_ignore_graphics_events(this->nav, 1);
4251         transform_get_size(trans, &this->root.w, &this->root.h);
4252         this->resize_cb=callback_new_attr_1(callback_cast(gui_internal_resize), attr_resize, this);
4253         graphics_add_callback(gra, this->resize_cb);
4254         this->button_cb=callback_new_attr_1(callback_cast(gui_internal_button), attr_button, this);
4255         graphics_add_callback(gra, this->button_cb);
4256         this->motion_cb=callback_new_attr_1(callback_cast(gui_internal_motion), attr_motion, this);
4257         graphics_add_callback(gra, this->motion_cb);
4258         this->keypress_cb=callback_new_attr_1(callback_cast(gui_internal_keypress), attr_keypress, this);
4259         graphics_add_callback(gra, this->keypress_cb);
4260         this->window_closed_cb=callback_new_attr_1(callback_cast(gui_internal_window_closed), attr_window_closed, this);
4261         graphics_add_callback(gra, this->window_closed_cb);
4262
4263         // set fullscreen if needed
4264         if (this->fullscreen)
4265                 this->win->fullscreen(this->win, this->fullscreen != 0);
4266         /* Was resize callback already issued? */
4267         if (navit_get_ready(this->nav) & 2)
4268                 gui_internal_setup_gc(this);
4269         return 0;
4270 }
4271
4272 static void gui_internal_disable_suspend(struct gui_priv *this)
4273 {
4274         if (this->win->disable_suspend)
4275                 this->win->disable_suspend(this->win);
4276 }
4277
4278 //##############################################################################################################
4279 //# Description:
4280 //# Comment:
4281 //# Authors: Martin Schaller (04/2008)
4282 //##############################################################################################################
4283 struct gui_methods gui_internal_methods = {
4284         NULL,
4285         NULL,
4286         gui_internal_set_graphics,
4287         NULL,
4288         NULL,
4289         NULL,
4290         gui_internal_disable_suspend,
4291         gui_internal_get_attr,
4292         gui_internal_add_attr,
4293         gui_internal_set_attr,
4294 };
4295
4296 static void
4297 gui_internal_get_data(struct gui_priv *priv, char *command, struct attr **in, struct attr ***out)
4298 {
4299         struct attr private_data = (struct attr) { attr_private_data, {(void *)&priv->data}};
4300         if (out)
4301                 *out=attr_generic_add_attr(*out, &private_data);
4302 }
4303
4304 static void
4305 gui_internal_add_callback(struct gui_priv *priv, struct callback *cb)
4306 {
4307         callback_list_add(priv->cbl, cb);
4308 }
4309
4310 static void
4311 gui_internal_remove_callback(struct gui_priv *priv, struct callback *cb)
4312 {
4313         callback_list_remove(priv->cbl, cb);
4314 }
4315
4316
4317 static struct gui_internal_methods gui_internal_methods_ext = {
4318         gui_internal_add_callback,
4319         gui_internal_remove_callback,
4320         gui_internal_menu_render,
4321         image_new_xs,
4322         image_new_l,
4323 };
4324
4325
4326 static enum flags
4327 gui_internal_get_flags(struct widget *widget)
4328 {
4329         return widget->flags;
4330 }
4331
4332 static void
4333 gui_internal_set_flags(struct widget *widget, enum flags flags)
4334 {
4335         widget->flags=flags;
4336 }
4337
4338 static int
4339 gui_internal_get_state(struct widget *widget)
4340 {
4341         return widget->state;
4342 }
4343
4344 static void
4345 gui_internal_set_state(struct widget *widget, int state)
4346 {
4347         widget->state=state;
4348 }
4349
4350 static void
4351 gui_internal_set_func(struct widget *widget, void (*func)(struct gui_priv *priv, struct widget *widget, void *data))
4352 {
4353         widget->func=func;
4354 }
4355
4356 static void
4357 gui_internal_set_data(struct widget *widget, void *data)
4358 {
4359         widget->data=data;
4360 }
4361
4362 static void
4363 gui_internal_set_default_background(struct gui_priv *this, struct widget *widget)
4364 {
4365         widget->background=this->background;
4366 }
4367
4368 static struct gui_internal_widget_methods gui_internal_widget_methods = {
4369         gui_internal_widget_append,
4370         gui_internal_button_new,
4371         gui_internal_button_new_with_callback,
4372         gui_internal_box_new,
4373         gui_internal_label_new,
4374         gui_internal_image_new,
4375         gui_internal_keyboard,
4376         gui_internal_menu,
4377         gui_internal_get_flags,
4378         gui_internal_set_flags,
4379         gui_internal_get_state,
4380         gui_internal_set_state,
4381         gui_internal_set_func,
4382         gui_internal_set_data,
4383         gui_internal_set_default_background,
4384 };
4385
4386 static void
4387 gui_internal_cmd_write(struct gui_priv * this, char *function, struct attr **in, struct attr ***out, int *valid)
4388 {
4389         char *str=NULL,*str2=NULL;
4390         dbg(1,"enter %s %p %p %p\n",function,in,out,valid);
4391         if (!in || !in[0])
4392                 return;
4393         dbg(1,"%s\n",attr_to_name(in[0]->type));        
4394         if (ATTR_IS_STRING(in[0]->type)) {
4395                 str=in[0]->u.str;
4396         }
4397         if (ATTR_IS_COORD_GEO(in[0]->type)) {
4398                 str=str2=coordinates_geo(in[0]->u.coord_geo, '\n');
4399         }
4400         if (str) {
4401                 str=g_strdup_printf("<html>%s</html>\n",str);
4402                 xml_parse_text(str, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
4403         }
4404         g_free(str);
4405         g_free(str2);
4406 }
4407
4408
4409 /**
4410  * @brief Creates a new table widget.
4411  *
4412  * Creates and returns a new table widget.  This function will
4413  * setup next/previous buttons as children.
4414  *
4415  * @param this The graphics context.
4416  * @param flags widget sizing flags.
4417  * @returns The newly created widget
4418  */
4419 struct widget * gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons)
4420 {
4421         struct widget * widget = g_new0(struct widget,1);
4422         struct table_data * data = NULL;
4423         widget->type=widget_table;
4424         widget->flags=flags;
4425         widget->data = g_new0(struct table_data,1);
4426         widget->data_free=gui_internal_table_data_free;
4427         data = (struct table_data*)widget->data;
4428
4429
4430         if (buttons) {
4431         data->next_button = gui_internal_button_new_with_callback
4432                 (this,"Next",image_new_xs(this, "gui_active") ,
4433                  gravity_left_center  |orientation_vertical,
4434                  gui_internal_table_button_next,NULL);
4435         data->next_button->data=widget;
4436
4437
4438         data->prev_button =  gui_internal_button_new_with_callback
4439                 (this,"Prev",
4440                  image_new_xs(this, "gui_active")
4441                  ,gravity_right_center |orientation_vertical,
4442                  gui_internal_table_button_prev,NULL);
4443
4444         data->prev_button->data=widget;
4445
4446         data->this=this;
4447
4448         data->button_box=gui_internal_box_new(this,
4449                                               gravity_center|orientation_horizontal);
4450         data->button_box->children=g_list_append(data->button_box->children,
4451                                                  data->next_button);
4452         data->button_box->children=g_list_append(data->button_box->children,
4453                                                  data->prev_button);
4454         //data->button_box->background=this->background2;
4455         data->button_box->bl=this->spacing;
4456         widget->children=g_list_append(widget->children,data->button_box);
4457         gui_internal_widget_pack(this,data->button_box);
4458         }
4459
4460         return widget;
4461
4462 }
4463
4464 /**
4465  * @brief Clears all the rows from the table.
4466  * This function removes all rows from a table.
4467  * New rows can later be added to the table.
4468  */
4469 void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table)
4470 {
4471   GList * iter;
4472   struct table_data * table_data = (struct table_data* ) table->data;
4473
4474   iter = table->children;
4475   while(iter ) {
4476           if(iter->data != table_data->button_box) {
4477                   struct widget * child = (struct widget*)iter->data;
4478                   gui_internal_widget_destroy(this,child);
4479                   if(table->children == iter) {
4480                           table->children = g_list_remove(iter,iter->data);
4481                           iter=table->children;
4482                   }
4483                   else
4484                           iter = g_list_remove(iter,iter->data);
4485           }
4486           else {
4487                   iter = g_list_next(iter);
4488           }
4489
4490   }
4491   table_data->top_row=NULL;
4492   table_data->bottom_row=NULL;
4493   if(table_data->page_headers)
4494           g_list_free(table_data->page_headers);
4495   table_data->page_headers=NULL;
4496 }
4497
4498
4499 /**
4500  * Creates a new table_row widget.
4501  * @param this The graphics context
4502  * @param flags Sizing flags for the row
4503  * @returns The new table_row widget.
4504  */
4505 struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags)
4506 {
4507         struct widget * widget = g_new0(struct widget,1);
4508         widget->type=widget_table_row;
4509         widget->flags=flags;
4510         return widget;
4511 }
4512
4513
4514
4515 /**
4516  * @brief Computes the column dimensions for the table.
4517  *
4518  * @param w The table widget to compute dimensions for.
4519  *
4520  * This function examines all of the rows and columns for the table w
4521  * and returns a list (GList) of table_column_desc elements that
4522  * describe each column of the table.
4523  *
4524  * The caller is responsible for freeing the returned list.
4525  */
4526 static GList * gui_internal_compute_table_dimensions(struct gui_priv * this,struct widget * w)
4527 {
4528
4529         GList * column_desc = NULL;
4530         GList * current_desc=NULL;
4531         GList * cur_row = w->children;
4532         struct widget * cur_row_widget=NULL;
4533         GList * cur_column=NULL;
4534         struct widget * cell_w=NULL;
4535         struct table_column_desc * current_cell=NULL;
4536         struct table_data * table_data=NULL;
4537         int height=0;
4538         int width=0;
4539         int total_width=0;
4540         int column_count=0;
4541
4542         /**
4543          * Scroll through the the table and
4544          * 1. Compute the maximum width + height of each column across all rows.
4545          */
4546         table_data = (struct table_data*) w->data;
4547         for(cur_row=w->children;  cur_row ; cur_row = g_list_next(cur_row) )
4548         {
4549                 cur_row_widget = (struct widget*) cur_row->data;
4550                 current_desc = column_desc;
4551                 if(cur_row_widget == table_data->button_box)
4552                 {
4553                         continue;
4554                 }
4555                 column_count=0;
4556                 for(cur_column = cur_row_widget->children; cur_column;
4557                     cur_column=g_list_next(cur_column))
4558                 {
4559                         cell_w = (struct widget*) cur_column->data;
4560                         gui_internal_widget_pack(this,cell_w);
4561                         if(current_desc == 0)
4562                         {
4563                                 current_cell = g_new0(struct table_column_desc,1);
4564                                 column_desc = g_list_append(column_desc,current_cell);
4565                                 current_desc = g_list_last(column_desc);
4566                                 current_cell->height=cell_w->h;
4567                                 current_cell->width=cell_w->w;
4568                                 total_width+=cell_w->w;
4569
4570                         }
4571                         else
4572                         {
4573                                 current_cell = current_desc->data;
4574                                 height = cell_w->h;
4575                                 width = cell_w->w;
4576                                 if(current_cell->height < height )
4577                                 {
4578                                         current_cell->height = height;
4579                                 }
4580                                 if(current_cell->width < width)
4581                                 {
4582                                         total_width += (width-current_cell->width);
4583                                         current_cell->width = width;
4584
4585
4586
4587                                 }
4588                                 current_desc = g_list_next(current_desc);
4589                         }
4590                         column_count++;
4591
4592                 }/* column loop */
4593
4594         } /*row loop */
4595
4596
4597         /**
4598          * If the width of all columns is less than the width off
4599          * the table expand each cell proportionally.
4600          *
4601          */
4602         if(total_width+(this->spacing*column_count) < w->w ) {
4603                 for(current_desc=column_desc; current_desc; current_desc=g_list_next(current_desc)) {
4604                         current_cell = (struct table_column_desc*) current_desc->data;
4605                         current_cell->width= ( (current_cell->width+this->spacing)/(float)total_width) * w->w ;
4606                 }
4607         }
4608
4609         return column_desc;
4610 }
4611
4612
4613 /**
4614  * @brief Computes the height and width for the table.
4615  *
4616  * The height and widht are computed to display all cells in the table
4617  * at the requested height/width.
4618  *
4619  * @param this The graphics context
4620  * @param w The widget to pack.
4621  *
4622  */
4623 void gui_internal_table_pack(struct gui_priv * this, struct widget * w)
4624 {
4625
4626         int height=0;
4627         int width=0;
4628         int count=0;
4629         GList * column_data = gui_internal_compute_table_dimensions(this,w);
4630         GList * current=0;
4631         struct table_column_desc * cell_desc=0;
4632         struct table_data * table_data = (struct table_data*)w->data;
4633
4634         for(current = column_data; current; current=g_list_next(current))
4635         {
4636                 if(table_data->button_box == current->data )
4637                 {
4638                         continue;
4639                 }
4640                 cell_desc = (struct table_column_desc *) current->data;
4641                 width = width + cell_desc->width + this->spacing;
4642                 if(height < cell_desc->height)
4643                 {
4644                         height = cell_desc->height ;
4645                 }
4646         }
4647
4648
4649
4650         for(current=w->children; current; current=g_list_next(current))
4651         {
4652                 if(current->data!= table_data->button_box)
4653                 {
4654                         count++;
4655                 }
4656         }
4657         if (table_data->button_box)
4658                 gui_internal_widget_pack(this,table_data->button_box);
4659
4660
4661
4662         if(w->h + w->c.y   > this->root.h   )
4663         {
4664                 /**
4665                  * Do not allow the widget to exceed the screen.
4666                  *
4667                  */
4668                 w->h = this->root.h- w->c.y  - height;
4669         }
4670         w->w = width;
4671
4672         /**
4673          * Deallocate column descriptions.
4674          */
4675         current = column_data;
4676         while( (current = g_list_last(current)) )
4677         {
4678                 current = g_list_remove(current,current->data);
4679         }
4680
4681 }
4682
4683
4684
4685 /**
4686  * @brief Renders a table widget.
4687  *
4688  * @param this The graphics context
4689  * @param w The table widget to render.
4690  */
4691 void gui_internal_table_render(struct gui_priv * this, struct widget * w)
4692 {
4693
4694         int x;
4695         int y;
4696         GList * column_desc=NULL;
4697         GList * cur_row = NULL;
4698         GList * current_desc=NULL;
4699         struct table_data * table_data = (struct table_data*)w->data;
4700         int is_skipped=0;
4701         int is_first_page=1;
4702         struct table_column_desc * dim=NULL;
4703
4704         dbg_assert(table_data);
4705         column_desc = gui_internal_compute_table_dimensions(this,w);
4706         y=w->p.y;
4707
4708         /**
4709          * Skip rows that are on previous pages.
4710          */
4711         cur_row = w->children;
4712         if(table_data->top_row && table_data->top_row != w->children )
4713         {
4714                 cur_row = table_data->top_row;
4715                 is_first_page=0;
4716         }
4717
4718
4719         /**
4720          * Loop through each row.  Drawing each cell with the proper sizes,
4721          * at the proper positions.
4722          */
4723         for(table_data->top_row=cur_row; cur_row; cur_row = g_list_next(cur_row))
4724         {
4725                 GList * cur_column=NULL;
4726                 current_desc = column_desc;
4727                 struct widget * cur_row_widget = (struct widget*)cur_row->data;
4728                 int max_height=0;
4729                 x =w->p.x+this->spacing;
4730                 if(cur_row_widget == table_data->button_box )
4731                 {
4732                         continue;
4733                 }
4734                 dim = (struct table_column_desc*)current_desc->data;
4735
4736                 if( y + dim->height + (table_data->button_box ? table_data->button_box->h : 0) + this->spacing >= w->p.y + w->h )
4737                 {
4738                         /*
4739                          * No more drawing space left.
4740                          */
4741                         is_skipped=1;
4742                         break;
4743
4744                 }
4745                 for(cur_column = cur_row_widget->children; cur_column;
4746                     cur_column=g_list_next(cur_column))
4747                 {
4748                         struct  widget * cur_widget = (struct widget*) cur_column->data;
4749                         dim = (struct table_column_desc*)current_desc->data;
4750
4751                         cur_widget->p.x=x;
4752                         cur_widget->w=dim->width;
4753                         cur_widget->p.y=y;
4754                         cur_widget->h=dim->height;
4755                         x=x+cur_widget->w;
4756                         max_height = dim->height;
4757                         /* We pack the widget before rendering to ensure that the x and y
4758                          * coordinates get pushed down.
4759                          */
4760                         gui_internal_widget_pack(this,cur_widget);
4761                         gui_internal_widget_render(this,cur_widget);
4762
4763                         if(dim->height > max_height)
4764                         {
4765                                 max_height = dim->height;
4766                         }
4767                 }
4768                 y = y + max_height;
4769                 table_data->bottom_row=cur_row;
4770                 current_desc = g_list_next(current_desc);
4771         }
4772         if(table_data->button_box && (is_skipped || !is_first_page)  )
4773         {
4774                 table_data->button_box->p.y =w->p.y+w->h-table_data->button_box->h -
4775                         this->spacing;
4776                 if(table_data->button_box->p.y < y )
4777                 {
4778                         table_data->button_box->p.y=y;
4779                 }
4780                 table_data->button_box->p.x = w->p.x;
4781                 table_data->button_box->w = w->w;
4782                 //    table_data->button_box->h = w->h - y;
4783                 //    table_data->next_button->h=table_data->button_box->h;
4784                 //    table_data->prev_button->h=table_data->button_box->h;
4785                 //    table_data->next_button->c.y=table_data->button_box->c.y;
4786                 //    table_data->prev_button->c.y=table_data->button_box->c.y;
4787
4788                 gui_internal_widget_pack(this,table_data->button_box);
4789                 if(table_data->next_button->p.y > w->p.y + w->h + table_data->next_button->h)
4790                 {
4791
4792                         table_data->button_box->p.y = w->p.y + w->h -
4793                                 table_data->button_box->h;
4794                 }
4795                 if(is_skipped)
4796                 {
4797                         table_data->next_button->state|= STATE_SENSITIVE;
4798                 }
4799                 else
4800                 {
4801                         table_data->next_button->state&= ~STATE_SENSITIVE;
4802                 }
4803
4804                 if(table_data->top_row != w->children)
4805                 {
4806                         table_data->prev_button->state|= STATE_SENSITIVE;
4807                 }
4808                 else
4809                 {
4810                         table_data->prev_button->state&= ~STATE_SENSITIVE;
4811                 }
4812                 gui_internal_widget_render(this,table_data->button_box);
4813
4814
4815         }
4816
4817         /**
4818          * Deallocate column descriptions.
4819          */
4820         current_desc = column_desc;
4821         while( (current_desc = g_list_last(current_desc)) )
4822         {
4823                 current_desc = g_list_remove(current_desc,current_desc->data);
4824         }
4825 }
4826
4827
4828 /**
4829  * @brief Displays Route information
4830  *
4831  * @li The name of the active vehicle
4832  * @param wm The button that was pressed.
4833  * @param v Unused
4834  */
4835 static void
4836 gui_internal_cmd2_route_description(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4837 {
4838
4839
4840         struct widget * menu;
4841         struct widget * row;
4842
4843
4844         if(! this->vehicle_cb)
4845         {
4846           /**
4847            * Register the callback on vehicle updates.
4848            */
4849           this->vehicle_cb = callback_new_attr_1(callback_cast(gui_internal_route_update),
4850                                                        attr_position_coord_geo,this);
4851           navit_add_callback(this->nav,this->vehicle_cb);
4852         }
4853
4854         this->route_data.route_table = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
4855         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
4856
4857         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
4858
4859
4860         menu=gui_internal_menu(this,_("Route Description"));
4861
4862         menu->free=gui_internal_route_screen_free;
4863         this->route_data.route_showing=1;
4864         this->route_data.route_table->spx = this->spacing;
4865
4866
4867         struct widget * box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
4868
4869         //      gui_internal_widget_append(box,gui_internal_box_new_with_label(this,"Test"));
4870         gui_internal_widget_append(box,this->route_data.route_table);
4871         box->w=menu->w;
4872         box->spx = this->spacing;
4873         this->route_data.route_table->w=box->w;
4874         gui_internal_widget_append(menu,box);
4875         gui_internal_populate_route_table(this,this->nav);
4876         gui_internal_menu_render(this);
4877
4878 }
4879
4880 static int
4881 line_intersection(struct coord* a1, struct coord *a2, struct coord * b1, struct coord *b2, struct coord *res)
4882 {
4883         int n, a, b;
4884         int adx=a2->x-a1->x;
4885         int ady=a2->y-a1->y;
4886         int bdx=b2->x-b1->x;
4887         int bdy=b2->y-b1->y;
4888         n = bdy * adx - bdx * ady;
4889         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
4890         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
4891         if (n < 0) {
4892                 n = -n;
4893                 a = -a;
4894                 b = -b;
4895         }
4896         if (a < 0 || b < 0)
4897                 return 0;
4898         if (a > n || b > n)
4899                 return 0;
4900         if (n == 0) {
4901                 dbg(0,"a=%d b=%d n=%d\n", a, b, n);
4902                 dbg(0,"a1=0x%x,0x%x ad %d,%d\n", a1->x, a1->y, adx, ady);
4903                 dbg(0,"b1=0x%x,0x%x bd %d,%d\n", b1->x, b1->y, bdx, bdy);
4904                 dbg_assert(n != 0);
4905         }
4906         res->x = a1->x + a * adx / n;
4907         res->y = a1->y + a * ady / n;
4908         return 1;
4909 }
4910
4911 struct heightline {
4912         struct heightline *next;
4913         int height;
4914         struct coord_rect bbox;
4915         int count;
4916         struct coord c[0];
4917 };
4918
4919 struct diagram_point {
4920         struct diagram_point *next;
4921         struct coord c;
4922 };
4923
4924 static struct heightline *
4925 item_get_heightline(struct item *item)
4926 {
4927         struct heightline *ret=NULL;
4928         struct street_data *sd;
4929         struct attr attr;
4930         int i,height;
4931
4932         if (item_attr_get(item, attr_label, &attr)) {
4933                 height=atoi(attr.u.str);
4934                 sd=street_get_data(item);
4935                 if (sd && sd->count > 1) {
4936                         ret=g_malloc(sizeof(struct heightline)+sd->count*sizeof(struct coord));
4937                         ret->bbox.lu=sd->c[0];
4938                         ret->bbox.rl=sd->c[0];
4939                         ret->count=sd->count;
4940                         ret->height=height;
4941                         for (i = 0 ; i < sd->count ; i++) {
4942                                 ret->c[i]=sd->c[i];
4943                                 coord_rect_extend(&ret->bbox, sd->c+i);
4944                         }
4945                 }
4946                 street_data_free(sd);
4947         }
4948         return ret;
4949 }
4950
4951
4952 /**
4953  * @brief Displays Route Height Profile
4954  *
4955  * @li The name of the active vehicle
4956  * @param wm The button that was pressed.
4957  * @param v Unused
4958  */
4959 static void
4960 gui_internal_cmd2_route_height_profile(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4961 {
4962
4963
4964         struct widget * menu, *box;
4965
4966         struct map * map=NULL;
4967         struct map_rect * mr=NULL;
4968         struct route * route;
4969         struct item * item =NULL;
4970         struct mapset *ms;
4971         struct mapset_handle *msh;
4972         int x,i,first=1,dist=0;
4973         struct coord c,last,res;
4974         struct coord_rect rbbox,dbbox;
4975         struct map_selection sel;
4976         struct heightline *heightline,*heightlines=NULL;
4977         struct diagram_point *min,*diagram_point,*diagram_points=NULL;
4978         sel.next=NULL;
4979         sel.order=18;
4980         sel.range.min=type_height_line_1;
4981         sel.range.max=type_height_line_3;
4982
4983
4984         menu=gui_internal_menu(this,_("Height Profile"));
4985         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
4986         gui_internal_widget_append(menu, box);
4987         route = navit_get_route(this->nav);
4988         if (route)
4989                 map = route_get_map(route);
4990         if(map)
4991                 mr = map_rect_new(map,NULL);
4992         if(mr) {
4993                 while((item = map_rect_get_item(mr))) {
4994                         while (item_coord_get(item, &c, 1)) {
4995                                 if (first) {
4996                                         first=0;
4997                                         sel.u.c_rect.lu=c;
4998                                         sel.u.c_rect.rl=c;
4999                                 } else
5000                                         coord_rect_extend(&sel.u.c_rect, &c);
5001                         }
5002                 }
5003                 map_rect_destroy(mr);
5004                 ms=navit_get_mapset(this->nav);
5005                 if (!first && ms) {
5006                         msh=mapset_open(ms);
5007                         while ((map=mapset_next(msh, 1))) {
5008                                 mr=map_rect_new(map, &sel);
5009                                 if (mr) {
5010                                         while((item = map_rect_get_item(mr))) {
5011                                                 if (item->type >= sel.range.min && item->type <= sel.range.max) {
5012                                                         heightline=item_get_heightline(item);
5013                                                         if (heightline) {
5014                                                                 heightline->next=heightlines;
5015                                                                 heightlines=heightline;
5016                                                         }
5017                                                 }
5018                                         }
5019                                         map_rect_destroy(mr);
5020                                 }
5021                         }
5022                         mapset_close(msh);
5023                 }
5024         }
5025         map=NULL;
5026         mr=NULL;
5027         if (route)
5028                 map = route_get_map(route);
5029         if(map)
5030                 mr = map_rect_new(map,NULL);
5031         if(mr && heightlines) {
5032                 while((item = map_rect_get_item(mr))) {
5033                         first=1;
5034                         while (item_coord_get(item, &c, 1)) {
5035                                 if (first)
5036                                         first=0;
5037                                 else {
5038                                         heightline=heightlines;
5039                                         rbbox.lu=last;
5040                                         rbbox.rl=last;
5041                                         coord_rect_extend(&rbbox, &c);
5042                                         while (heightline) {
5043                                                 if (coord_rect_overlap(&rbbox, &heightline->bbox)) {
5044                                                         for (i = 0 ; i < heightline->count - 1; i++) {
5045                                                                 if (heightline->c[i].x != heightline->c[i+1].x || heightline->c[i].y != heightline->c[i+1].y) {
5046                                                                         if (line_intersection(heightline->c+i, heightline->c+i+1, &last, &c, &res)) {
5047                                                                                 diagram_point=g_new(struct diagram_point, 1);
5048                                                                                 diagram_point->c.x=dist+transform_distance(projection_mg, &last, &res);
5049                                                                                 diagram_point->c.y=heightline->height;
5050                                                                                 diagram_point->next=diagram_points;
5051                                                                                 diagram_points=diagram_point;
5052                                                                                 dbg(0,"%d %d\n", diagram_point->c.x, diagram_point->c.y);
5053                                                                         }
5054                                                                 }
5055                                                         }
5056                                                 }
5057                                                 heightline=heightline->next;
5058                                         }
5059                                         dist+=transform_distance(projection_mg, &last, &c);
5060                                 }
5061                                 last=c;
5062                         }
5063
5064                 }
5065                 map_rect_destroy(mr);
5066         }
5067
5068
5069         gui_internal_menu_render(this);
5070         first=1;
5071         diagram_point=diagram_points;
5072         while (diagram_point) {
5073                 if (first) {
5074                         dbbox.lu=diagram_point->c;
5075                         dbbox.rl=diagram_point->c;
5076                         first=0;
5077                 } else
5078                         coord_rect_extend(&dbbox, &diagram_point->c);
5079                 diagram_point=diagram_point->next;
5080         }
5081         dbg(0,"%d %d %d %d\n", dbbox.lu.x, dbbox.lu.y, dbbox.rl.x, dbbox.rl.y);
5082         if (dbbox.rl.x > dbbox.lu.x && dbbox.lu.x*100/(dbbox.rl.x-dbbox.lu.x) <= 25)
5083                 dbbox.lu.x=0;
5084         if (dbbox.lu.y > dbbox.rl.y && dbbox.rl.y*100/(dbbox.lu.y-dbbox.rl.y) <= 25)
5085                 dbbox.rl.y=0;
5086         dbg(0,"%d,%d %dx%d\n", box->p.x, box->p.y, box->w, box->h);
5087         x=dbbox.lu.x;
5088         first=1;
5089         for (;;) {
5090                 struct point p[2];
5091                 min=NULL;
5092                 diagram_point=diagram_points;
5093                 while (diagram_point) {
5094                         if (diagram_point->c.x >= x && (!min || min->c.x > diagram_point->c.x))
5095                                 min=diagram_point;
5096                         diagram_point=diagram_point->next;
5097                 }
5098                 if (! min)
5099                         break;
5100                 p[1].x=(min->c.x-dbbox.lu.x)*(box->w-10)/(dbbox.rl.x-dbbox.lu.x)+box->p.x+5;
5101                 p[1].y=(min->c.y-dbbox.rl.y)*(box->h-10)/(dbbox.lu.y-dbbox.rl.y)+box->p.y+5;
5102                 dbg(0,"%d,%d=%d,%d\n",min->c.x, min->c.y, p[1].x,p[1].y);
5103                 graphics_draw_circle(this->gra, this->foreground, &p[1], 2);
5104                 if (first)
5105                         first=0;
5106                 else
5107                         graphics_draw_lines(this->gra, this->foreground, p, 2);
5108                 p[0]=p[1];
5109                 x=min->c.x+1;
5110         }
5111
5112
5113 }
5114
5115 static void
5116 gui_internal_cmd2_locale(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5117 {
5118         struct widget *menu,*wb,*w;
5119         char *text;
5120
5121         graphics_draw_mode(this->gra, draw_mode_begin);
5122         menu=gui_internal_menu(this, _("Show Locale"));
5123         menu->spx=this->spacing*10;
5124         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5125         gui_internal_widget_append(menu, wb);
5126         text=g_strdup_printf("LANG=%s",getenv("LANG"));
5127         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
5128         w->flags=gravity_left_center|orientation_horizontal|flags_fill;
5129         g_free(text);
5130 #ifdef HAVE_API_WIN32_BASE
5131         {
5132                 wchar_t wcountry[32],wlang[32];
5133                 char country[32],lang[32];
5134
5135                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
5136                 WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
5137                 text=g_strdup_printf("LOCALE_SABBREVLANGNAME=%s",lang);
5138                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
5139                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
5140                 g_free(text);
5141                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
5142                 WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
5143                 text=g_strdup_printf("LOCALE_SABBREVCTRYNAME=%s",country);
5144                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
5145                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
5146                 g_free(text);
5147         }
5148 #endif
5149
5150         gui_internal_menu_render(this);
5151         graphics_draw_mode(this->gra, draw_mode_end);
5152 }
5153
5154
5155 /**
5156  * @brief handles the 'next page' table event.
5157  * A callback function that is invoked when the 'next page' button is pressed
5158  * to advance the contents of a table widget.
5159  *
5160  * @param this The graphics context.
5161  * @param wm The button widget that was pressed.
5162  */
5163 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data)
5164 {
5165         struct widget * table_widget = (struct widget * ) wm->data;
5166         struct table_data * table_data = NULL;
5167         int found=0;
5168         GList * iterator;
5169
5170         if(table_widget)
5171         {
5172                 table_data = (struct table_data*) table_widget->data;
5173
5174         }
5175         if(table_data)
5176         {
5177                 /**
5178                  * Before advancing to the next page we need to ensure
5179                  * that the current top_row is in the list of previous top_rows
5180                  * so previous page can work.
5181                  *
5182                  */
5183                 for(iterator=table_data->page_headers; iterator != NULL;
5184                     iterator = g_list_next(iterator) )
5185                 {
5186                         if(iterator->data == table_data->top_row)
5187                         {
5188                                 found=1;
5189                                 break;
5190                         }
5191
5192                 }
5193                 if( ! found)
5194                 {
5195                         table_data->page_headers=g_list_append(table_data->page_headers,
5196                                                                table_data->top_row);
5197                 }
5198
5199                 table_data->top_row = g_list_next(table_data->bottom_row);
5200         }
5201         wm->state&= ~STATE_HIGHLIGHTED;
5202         gui_internal_menu_render(this);
5203 }
5204
5205
5206
5207 /**
5208  * @brief handles the 'previous page' table event.
5209  * A callback function that is invoked when the 'previous page' button is pressed
5210  * to go back in the contents of a table widget.
5211  *
5212  * @param this The graphics context.
5213  * @param wm The button widget that was pressed.
5214  */
5215 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data)
5216 {
5217         struct widget * table_widget = (struct widget * ) wm->data;
5218         struct table_data * table_data = NULL;
5219         GList * current_page_top=NULL;
5220
5221         GList * iterator;
5222         if(table_widget)
5223         {
5224                 table_data = (struct table_data*) table_widget->data;
5225                 if(table_data)
5226                 {
5227                         current_page_top = table_data->top_row;
5228                         for(iterator = table_data->page_headers; iterator != NULL;
5229                             iterator = g_list_next(iterator))
5230                         {
5231                                 if(current_page_top == iterator->data)
5232                                 {
5233                                         break;
5234                                 }
5235                                 table_data->top_row = (GList*) iterator->data;
5236                         }
5237                 }
5238         }
5239         wm->state&= ~STATE_HIGHLIGHTED;
5240         gui_internal_menu_render(this);
5241 }
5242
5243
5244 /**
5245  * @brief deallocates a table_data structure.
5246  *
5247  */
5248 void gui_internal_table_data_free(void * p)
5249 {
5250
5251
5252         /**
5253          * @note button_box and its children (next_button,prev_button)
5254          * have their memory managed by the table itself.
5255          */
5256         struct table_data * table_data =  (struct table_data*) p;
5257         g_list_free(table_data->page_headers);
5258         g_free(p);
5259
5260
5261 }
5262
5263
5264 /**
5265  * @brief Called when the route is updated.
5266  */
5267 void gui_internal_route_update(struct gui_priv * this, struct navit * navit, struct vehicle *v)
5268 {
5269
5270         if(this->route_data.route_showing) {
5271                 gui_internal_populate_route_table(this,navit);
5272                 graphics_draw_mode(this->gra, draw_mode_begin);
5273                 gui_internal_menu_render(this);
5274                 graphics_draw_mode(this->gra, draw_mode_end);
5275         }
5276
5277
5278 }
5279
5280
5281 /**
5282  * @brief Called when the route screen is closed (deallocated).
5283  *
5284  * The main purpose of this function is to remove the widgets from
5285  * references route_data because those widgets are about to be freed.
5286  */
5287 void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w)
5288 {
5289         if(this_) {
5290                 this_->route_data.route_showing=0;
5291                 this_->route_data.route_table=NULL;
5292                 g_free(w);
5293         }
5294
5295 }
5296
5297 /**
5298  * @brief Populates the route  table with route information
5299  *
5300  * @param this The gui context
5301  * @param navit The navit object
5302  */
5303 void gui_internal_populate_route_table(struct gui_priv * this,
5304                                        struct navit * navit)
5305 {
5306         struct map * map=NULL;
5307         struct map_rect * mr=NULL;
5308         struct navigation * nav = NULL;
5309         struct item * item =NULL;
5310         struct attr attr;
5311         struct widget * label = NULL;
5312         struct widget * row = NULL;
5313         nav = navit_get_navigation(navit);
5314         if(!nav) {
5315                 return;
5316         }
5317         map = navigation_get_map(nav);
5318         if(map)
5319           mr = map_rect_new(map,NULL);
5320         if(mr) {
5321                 gui_internal_widget_table_clear(this,this->route_data.route_table);
5322                 while((item = map_rect_get_item(mr))) {
5323                         if(item_attr_get(item,attr_navigation_long,&attr)) {
5324                           label = gui_internal_label_new(this,attr.u.str);
5325                           row = gui_internal_widget_table_row_new(this,
5326                                                                   gravity_left
5327                                                                   | flags_fill
5328                                                                   | orientation_horizontal);
5329                           row->children=g_list_append(row->children,label);
5330                           gui_internal_widget_append(this->route_data.route_table,row);
5331                         }
5332
5333                 }
5334
5335         }
5336 }
5337
5338 static struct command_table commands[] = {
5339         {"abort_navigation",command_cast(gui_internal_cmd2_abort_navigation)},
5340         {"back",command_cast(gui_internal_cmd2_back)},
5341         {"back_to_map",command_cast(gui_internal_cmd2_back_to_map)},
5342         {"bookmarks",command_cast(gui_internal_cmd2_bookmarks)},
5343         {"get_data",command_cast(gui_internal_get_data)},
5344         {"locale",command_cast(gui_internal_cmd2_locale)},
5345         {"log",command_cast(gui_internal_cmd_log)},
5346         {"menu",command_cast(gui_internal_cmd_menu2)},
5347         {"position",command_cast(gui_internal_cmd2_position)},
5348         {"route_description",command_cast(gui_internal_cmd2_route_description)},
5349         {"route_height_profile",command_cast(gui_internal_cmd2_route_height_profile)},
5350         {"setting_layout",command_cast(gui_internal_cmd2_setting_layout)},
5351         {"setting_maps",command_cast(gui_internal_cmd2_setting_maps)},
5352         {"setting_rules",command_cast(gui_internal_cmd2_setting_rules)},
5353         {"setting_vehicle",command_cast(gui_internal_cmd2_setting_vehicle)},
5354         {"town",command_cast(gui_internal_cmd2_town)},
5355         {"quit",command_cast(gui_internal_cmd2_quit)},
5356         {"write",command_cast(gui_internal_cmd_write)},
5357 };
5358
5359
5360 //##############################################################################################################
5361 //# Description:
5362 //# Comment:
5363 //# Authors: Martin Schaller (04/2008)
5364 //##############################################################################################################
5365 static struct gui_priv * gui_internal_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs, struct gui *gui)
5366 {
5367         struct gui_priv *this;
5368         struct attr *attr;
5369         *meth=gui_internal_methods;
5370         this=g_new0(struct gui_priv, 1);
5371         this->nav=nav;
5372
5373         this->self.type=attr_gui;
5374         this->self.u.gui=gui;   
5375
5376         if ((attr=attr_search(attrs, NULL, attr_menu_on_map_click)))
5377                 this->menu_on_map_click=attr->u.num;
5378         else
5379                 this->menu_on_map_click=1;
5380         if ((attr=attr_search(attrs, NULL, attr_signal_on_map_click)))
5381                 this->signal_on_map_click=attr->u.num;
5382
5383         if ((attr=attr_search(attrs, NULL, attr_callback_list))) {
5384                 command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this);
5385         }
5386
5387         if( (attr=attr_search(attrs,NULL,attr_font_size)))
5388         {
5389           this->config.font_size=attr->u.num;
5390         }
5391         else
5392         {
5393           this->config.font_size=-1;
5394         }
5395         if( (attr=attr_search(attrs,NULL,attr_icon_xs)))
5396         {
5397           this->config.icon_xs=attr->u.num;
5398         }
5399         else
5400         {
5401           this->config.icon_xs=-1;
5402         }
5403         if( (attr=attr_search(attrs,NULL,attr_icon_l)))
5404         {
5405           this->config.icon_l=attr->u.num;
5406         }
5407         else
5408         {
5409           this->config.icon_l=-1;
5410         }
5411         if( (attr=attr_search(attrs,NULL,attr_icon_s)))
5412         {
5413           this->config.icon_s=attr->u.num;
5414         }
5415         else
5416         {
5417           this->config.icon_s=-1;
5418         }
5419         if( (attr=attr_search(attrs,NULL,attr_spacing)))
5420         {
5421           this->config.spacing=attr->u.num;
5422         }
5423         else
5424         {
5425           this->config.spacing=-1;
5426         }
5427         if( (attr=attr_search(attrs,NULL,attr_gui_speech)))
5428         {
5429           this->speech=attr->u.num;
5430         }
5431         if( (attr=attr_search(attrs,NULL,attr_keyboard)))
5432           this->keyboard=attr->u.num;
5433         else
5434           this->keyboard=1;
5435
5436     if( (attr=attr_search(attrs,NULL,attr_fullscreen)))
5437       this->fullscreen=attr->u.num;
5438
5439         if( (attr=attr_search(attrs,NULL,attr_flags)))
5440               this->flags=attr->u.num;
5441         if( (attr=attr_search(attrs,NULL,attr_background_color)))
5442               this->background_color=*attr->u.color;
5443         else
5444               this->background_color=COLOR_BLACK;
5445         if( (attr=attr_search(attrs,NULL,attr_background_color2)))
5446                 this->background2_color=*attr->u.color;
5447         else
5448                 this->background2_color=(struct color){0x4141,0x4141,0x4141,0xffff};
5449         if( (attr=attr_search(attrs,NULL,attr_text_color)))
5450               this->text_foreground_color=*attr->u.color;
5451         else
5452               this->text_foreground_color=COLOR_WHITE;
5453         this->text_background_color=COLOR_BLACK;
5454         if( (attr=attr_search(attrs,NULL,attr_columns)))
5455               this->cols=attr->u.num;
5456         if( (attr=attr_search(attrs,NULL,attr_osd_configuration)))
5457               this->osd_configuration=*attr;
5458
5459         if( (attr=attr_search(attrs,NULL,attr_pitch)))
5460               this->pitch=attr->u.num;
5461         else
5462                 this->pitch=20;
5463         if( (attr=attr_search(attrs,NULL,attr_flags_town)))
5464                 this->flags_town=attr->u.num;
5465         else
5466                 this->flags_town=-1;
5467         if( (attr=attr_search(attrs,NULL,attr_flags_street)))
5468                 this->flags_street=attr->u.num;
5469         else
5470                 this->flags_street=-1;
5471         if( (attr=attr_search(attrs,NULL,attr_flags_house_number)))
5472                 this->flags_house_number=attr->u.num;
5473         else
5474                 this->flags_house_number=-1;
5475         this->data.priv=this;
5476         this->data.gui=&gui_internal_methods_ext;
5477         this->data.widget=&gui_internal_widget_methods;
5478         this->cbl=callback_list_new();
5479
5480         return this;
5481 }
5482
5483 //##############################################################################################################
5484 //# Description:
5485 //# Comment:
5486 //# Authors: Martin Schaller (04/2008)
5487 //##############################################################################################################
5488 void plugin_init(void)
5489 {
5490         plugin_register_gui_type("internal", gui_internal_new);
5491 }
5492