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