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