Fixed nasty scribble triggered by going into the menu 2 times caused by main html...
[profile/ivi/navit.git] / navit / navit / gui / internal / gui_internal.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2010 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 #include "config.h"
37 #ifdef HAVE_API_WIN32_BASE
38 #include <windows.h>
39 #endif
40 #ifndef _MSC_VER
41 #include <sys/time.h>
42 #else
43 #define snprintf sprintf_s
44 #endif /* _MSC_VER */
45 #include "item.h"
46 #include "file.h"
47 #include "navit.h"
48 #include "navit_nls.h"
49 #include "gui.h"
50 #include "coord.h"
51 #include "point.h"
52 #include "plugin.h"
53 #include "graphics.h"
54 #include "transform.h"
55 #include "color.h"
56 #include "map.h"
57 #include "layout.h"
58 #include "callback.h"
59 #include "vehicle.h"
60 #include "vehicleprofile.h"
61 #include "window.h"
62 #include "config_.h"
63 #include "keys.h"
64 #include "mapset.h"
65 #include "route.h"
66 #include "navit/search.h"
67 #include "track.h"
68 #include "country.h"
69 #include "config.h"
70 #include "event.h"
71 #include "navit_nls.h"
72 #include "navigation.h"
73 #include "gui_internal.h"
74 #include "command.h"
75 #include "xmlconfig.h"
76 #include "util.h"
77 #include "bookmarks.h"
78 #include "debug.h"
79 #include "fib.h"
80 #include "types.h"
81
82
83 extern char *version;
84
85 struct form {
86         char *onsubmit;
87 };
88
89
90 struct menu_data {
91         struct widget *search_list;
92         struct widget *keyboard;
93         struct widget *button_bar;
94         struct widget *menu;
95         int keyboard_mode;
96         void (*redisplay)(struct gui_priv *priv, struct widget *widget, void *data);
97         struct widget *redisplay_widget;
98         char *href;
99         struct attr refresh_callback_obj,refresh_callback;
100 };
101
102 //##############################################################################################################
103 //# Description:
104 //# Comment:
105 //# Authors: Martin Schaller (04/2008)
106 //##############################################################################################################
107 struct widget {
108         enum widget_type type;
109         struct graphics_gc *background,*text_background;
110         struct graphics_gc *foreground_frame;
111         struct graphics_gc *foreground;
112         char *text;
113         struct graphics_image *img;
114          /**
115           * A function to be invoked on actions.
116           * @li widget The widget that is receiving the button press.
117           *
118           */
119         void (*func)(struct gui_priv *priv, struct widget *widget, void *data);
120         int reason;
121         int datai;
122         void *data;
123         /**
124          * @brief A function to deallocate data
125          */
126         void (*data_free)(void *data);
127
128         /**
129          * @brief a function that will be called as the widget is being destroyed.
130          * This function can act as a destructor for the widget. It allows for
131          * on deallocation actions to be specified on a per widget basis.
132          * This function will call g_free on the widget (if required).
133          */
134         void (*wfree) (struct gui_priv *this_, struct widget * w);
135         char *prefix;
136         char *name;
137         char *speech;
138         char *command;
139         struct pcoord c;
140         struct item item;
141         int selection_id;
142         int state;
143         struct point p;
144         int wmin,hmin;
145         int w,h;
146         int textw,texth;
147         int font_idx;
148         int bl,br,bt,bb,spx,spy;
149         int border;
150         int packed;
151         /**
152          * The number of widgets to layout horizontally when doing
153          * a orientation_horizontal_vertical layout
154          */
155         int cols;
156         enum flags flags;
157         int flags2;
158         void *instance;
159         int (*set_attr)(void *, struct attr *);
160         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
161         void (*remove_cb)(void *, struct callback *cb);
162         struct callback *cb;
163         struct attr on;
164         struct attr off;
165         int deflt;
166         int is_on;
167         int redraw;
168         struct menu_data *menu_data;
169         struct form *form;
170         GList *children;
171         struct widget *parent;
172 };
173
174 /**
175  * @brief A structure to store configuration values.
176  *
177  * This structure stores configuration values for how gui elements in the internal GUI
178  * should be drawn.
179  */
180 struct gui_config_settings {
181
182   /**
183    * The base size (in fractions of a point) to use for text.
184    */
185   int font_size;
186   /**
187    * The size (in pixels) that xs style icons should be scaled to.
188    * This icon size is typically used in various lists and should be set to value which allows a list row to be easily cliked or dragged.
189    */
190   int icon_xs;
191   /**
192    * The size (in pixels) that s style icons (small) should be scaled to, used for the menu top row icons
193    */
194   int icon_s;
195   /**
196    * The size (in pixels) that l style icons should be scaled to, used for icons defined in the menu html
197    */
198   int icon_l;
199   /**
200    * The default amount of spacing (in pixels) to place between GUI elements.
201    */
202   int spacing;
203
204 };
205
206 /**
207  * Indexes into the config_profiles array.
208  */
209 const int LARGE_PROFILE=0;
210 const int MEDIUM_PROFILE=1;
211 const int SMALL_PROFILE=2;
212
213 /**
214  * The default config profiles.
215  *
216  * [0] =>  LARGE_PROFILE (screens 640 in one dimension)
217  * [1] =>  MEDIUM PROFILE (screens larger than 320 in one dimension
218  * [2] => Small profile (default)
219  */
220 static struct gui_config_settings config_profiles[]={
221       {545,32,48,96,10}
222     , {300,32,48,64,3}
223       ,{200,16,32,48,2}
224 };
225
226 struct route_data {
227   struct widget * route_table;
228   int route_showing;
229
230 };
231
232 //##############################################################################################################
233 //# Description:
234 //# Comment:
235 //# Authors: Martin Schaller (04/2008)
236 //##############################################################################################################
237 struct gui_priv {
238         struct navit *nav;
239         struct attr self;
240         struct window *win;
241         struct graphics *gra;
242         struct graphics_gc *background;
243         struct graphics_gc *background2;
244         struct graphics_gc *highlight_background;
245         struct graphics_gc *foreground;
246         struct graphics_gc *text_foreground;
247         struct graphics_gc *text_background;
248         struct color background_color, background2_color, text_foreground_color, text_background_color;
249         int spacing;
250         int font_size;
251         int fullscreen;
252         struct graphics_font *fonts[3];
253         /**
254          * The size (in pixels) that xs style icons should be scaled to.
255          * This icon size can be too small to click it on some devices.
256          */
257         int icon_xs;
258         /**
259          * The size (in pixels) that s style icons (small) should be scaled to
260          */
261         int icon_s;
262         /**
263          * The size (in pixels) that l style icons should be scaled to
264          */
265         int icon_l;
266         int pressed;
267         struct widget *widgets;
268         int widgets_count;
269         int redraw;
270         struct widget root;
271         struct widget *highlighted,*editable;
272         struct widget *highlighted_menu;
273         struct pcoord clickp, vehiclep;
274         struct attr *click_coord_geo, *position_coord_geo;
275         struct search_list *sl;
276         int ignore_button;
277         int menu_on_map_click;
278         char *on_map_click;
279         int signal_on_map_click;
280         char *country_iso2;
281         int speech;
282         int keyboard;
283         int keyboard_required;
284         /**
285          * The setting information read from the configuration file.
286          * values of -1 indicate no value was specified in the config file.
287          */
288         struct gui_config_settings config;
289         struct event_idle *idle;
290         struct callback *motion_cb,*button_cb,*resize_cb,*keypress_cb,*window_closed_cb,*idle_cb, *motion_timeout_callback;
291         struct event_timeout *motion_timeout_event;
292         struct point current;
293
294         struct callback * vehicle_cb;
295           /**
296            * Stores information about the route.
297            */
298         struct route_data route_data;
299
300         struct gui_internal_data data;
301         struct callback_list *cbl;
302         int flags;
303         int cols;
304         struct attr osd_configuration;
305         int pitch;
306         int flags_town,flags_street,flags_house_number;
307         int radius;
308         int mouse_button_clicked_on_map;
309 /* html */
310         char *html_text;
311         int html_depth;
312         struct widget *html_container;
313         int html_skip;
314         char *html_anchor;
315         char *href;
316         int html_anchor_found;
317         struct form *form;
318         struct html {
319                 int skip;
320                 enum html_tag {
321                         html_tag_none,
322                         html_tag_a,
323                         html_tag_h1,
324                         html_tag_html,
325                         html_tag_img,
326                         html_tag_script,
327                         html_tag_form,
328                         html_tag_input,
329                         html_tag_div,
330                 } tag;
331                 char *command;
332                 char *name;
333                 char *href;
334                 char *refresh_cond;
335                 struct widget *w;
336                 struct widget *container;
337         } html[10];
338
339 /* gestures */  
340
341         struct gesture_elem {
342                 int msec;
343                 struct point p;
344         } gesture_ring[GESTURE_RINGSIZE];
345         int gesture_ring_last, gesture_ring_first;
346
347
348         int results_map_population;
349 };
350
351
352
353
354
355 struct html_tag_map {
356         char *tag_name;
357         enum html_tag tag;
358 } html_tag_map[] = {
359         {"a",html_tag_a},
360         {"h1",html_tag_h1},
361         {"html",html_tag_html},
362         {"img",html_tag_img},
363         {"script",html_tag_script},
364         {"form",html_tag_form},
365         {"input",html_tag_input},
366         {"div",html_tag_div},
367 };
368
369
370 /**
371  * @brief A structure to store information about a table.
372  *
373  * The table_data widget stores pointers to extra information needed by the
374  * table widget.
375  *
376  * The table_data structure needs to be freed with data_free along with the widget.
377  *
378  */
379 struct table_data
380 {
381   /**
382    * A GList pointer into a widget->children list that indicates the row
383    * currently being rendered at the top of the table.
384    */
385   GList * top_row;
386   /**
387    * A Glist pointer into a widget->children list that indicates the row
388    * currently being rendered at the bottom of the table.
389    */
390   GList * bottom_row;
391
392   /**
393    * A container box that is the child of the table widget that contains+groups
394    * the next and previous button.
395    */
396   struct widget * button_box;
397
398   /**
399    * Button box should not be displayed if button_box_hide is not zero.
400    */
401   int button_box_hide;
402   
403   /**
404    * A button widget to handle 'next page' requests
405    */
406   struct widget * next_button;
407   /**
408    * A button widget to handle 'previous page' requests.
409    */
410   struct widget * prev_button;
411
412
413   /**
414    * a pointer to the gui context.
415    * This is needed by the free function to destory the buttons.
416    */
417   struct  gui_priv *  this;
418 };
419
420 /**
421  * A data structure that holds information about a column that makes up a table.
422  *
423  *
424  */
425 struct table_column_desc
426 {
427
428   /**
429    * The computed height of a cell in the table.
430    */
431   int height;
432
433   /**
434    * The computed width of a cell in the table.
435    */
436   int width;
437 };
438
439
440 static void gui_internal_widget_render(struct gui_priv *this, struct widget *w);
441 static void gui_internal_widget_pack(struct gui_priv *this, struct widget *w);
442 static struct widget * gui_internal_box_new(struct gui_priv *this, enum flags flags);
443 static void gui_internal_widget_append(struct widget *parent, struct widget *child);
444 static void gui_internal_widget_prepend(struct widget *parent, struct widget *child);
445 static void gui_internal_widget_insert_before(struct widget *parent, struct widget *sibling, struct widget *child);
446 static void gui_internal_widget_insert_sorted(struct widget *parent, struct widget *child, GCompareFunc func);
447 static void gui_internal_widget_destroy(struct gui_priv *this, struct widget *w);
448 static void gui_internal_apply_config(struct gui_priv *this);
449
450 static struct widget* gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons);
451 static struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags);
452 static void gui_internal_table_hide_rows(struct table_data * table_data);
453 static void gui_internal_table_render(struct gui_priv * this, struct widget * w);
454 static void gui_internal_table_pack(struct gui_priv * this, struct widget * w);
455 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data);
456 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data);
457 static void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table);
458 static int gui_internal_widget_table_is_empty(struct gui_priv *this,struct widget * table);
459 static GList * gui_internal_widget_table_next_row(GList * row);
460 static GList * gui_internal_widget_table_prev_row(GList * row);
461 static struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags);
462 static void gui_internal_table_data_free(void * d);
463 static void gui_internal_route_update(struct gui_priv * this, struct navit * navit,
464                                       struct vehicle * v);
465 static void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w);
466 static void gui_internal_populate_route_table(struct gui_priv * this,
467                                        struct navit * navit);
468 static void gui_internal_search_idle_end(struct gui_priv *this);
469 static void gui_internal_search(struct gui_priv *this, char *what, char *type, int flags);
470 static void gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data);
471 static void gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data);
472 static void gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data);
473 static void gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data);
474 static void gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data);
475 static void gui_internal_search_town_in_country(struct gui_priv *this, struct widget *wm);
476 static void gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data);
477 static void gui_internal_check_exit(struct gui_priv *this);
478 static void gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data);
479
480 static struct widget *gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode);
481 static struct menu_data * gui_internal_menu_data(struct gui_priv *this);
482
483 static int gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle *vehicle);
484 static void gui_internal_html_menu(struct gui_priv *this, const char *document, char *anchor);
485 static void gui_internal_html_load_href(struct gui_priv *this, char *href, int replace);
486 static void gui_internal_destroy(struct gui_priv *this);
487 static void gui_internal_enter(struct gui_priv *this, int ignore);
488 static void gui_internal_enter_setup(struct gui_priv *this);
489 static void gui_internal_html_main_menu(struct gui_priv *this);
490 static void gui_internal_menu_vehicle_settings(struct gui_priv *this, struct vehicle *v, char *name);
491
492
493 /*
494  * * Display image scaled to specific size
495  * * searches for scaleable and pre-scaled image
496  * * @param this Our gui context
497  * * @param name image name
498  * * @param w desired width of image
499  * * @param h desired height of image
500  * * @returns image_struct Ptr to scaled image struct or NULL if not scaled or found
501  * */
502 static struct graphics_image *
503 image_new_scaled(struct gui_priv *this, const char *name, int w, int h)
504 {
505         struct graphics_image *ret=NULL;
506         char *full_path=NULL;
507         full_path=graphics_icon_path(name);
508         ret=graphics_image_new_scaled(this->gra, full_path, w, h);
509         dbg(1,"Trying to load image '%s' (w=%d, h=%d): %s\n", name, w, h, ret ? "OK" : "NOT FOUND");
510         g_free(full_path);
511         if (!ret)
512                 dbg(0,"Failed to load image for '%s' (w=%d, h=%d)\n", name, w, h);
513         return ret;
514 }
515
516 #if 0
517 static struct graphics_image *
518 image_new_o(struct gui_priv *this, char *name)
519 {
520         return image_new_scaled(this, name, -1, -1);
521 }
522 #endif
523
524 /*
525  * * Display image scaled to xs (extra small) size
526  * * This image size can be too small to click it on some devices.
527  * * @param this Our gui context
528  * * @param name image name
529  * * @returns image_struct Ptr to scaled image struct or NULL if not scaled or found
530  * */
531 static struct graphics_image *
532 image_new_xs(struct gui_priv *this, const char *name)
533 {
534         return image_new_scaled(this, name, this->icon_xs, this->icon_xs);
535 }
536
537 /*
538  * * Display image scaled to s (small) size
539  * * @param this Our gui context
540  * * @param name image name
541  * * @returns image_struct Ptr to scaled image struct or NULL if not scaled or found
542  * */
543
544 static struct graphics_image *
545 image_new_s(struct gui_priv *this, const char *name)
546 {
547         return image_new_scaled(this, name, this->icon_s, this->icon_s);
548 }
549
550 /*
551  * * Display image scaled to l (large) size
552  * * @param this Our gui context
553  * * @param name image name
554  * * @returns image_struct Ptr to scaled image struct or NULL if not scaled or found
555  * */
556 static struct graphics_image *
557 image_new_l(struct gui_priv *this, const char *name)
558 {
559         return image_new_scaled(this, name, this->icon_l, this->icon_l);
560 }
561
562 static char *
563 coordinates_geo(const struct coord_geo *gc, char sep)
564 {
565         char latc='N',lngc='E';
566         int lat_deg,lat_min,lat_sec;
567         int lng_deg,lng_min,lng_sec;
568         struct coord_geo g=*gc;
569
570         if (g.lat < 0) {
571                 g.lat=-g.lat;
572                 latc='S';
573         }
574         if (g.lng < 0) {
575                 g.lng=-g.lng;
576                 lngc='W';
577         }
578         lat_deg=g.lat;
579         lat_min=fmod(g.lat*60,60);
580         lat_sec=fmod(g.lat*3600,60);
581         lng_deg=g.lng;
582         lng_min=fmod(g.lng*60,60);
583         lng_sec=fmod(g.lng*3600,60);
584         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);
585 }
586
587 static char *
588 coordinates(struct pcoord *pc, char sep)
589 {
590         struct coord_geo g;
591         struct coord c;
592         c.x=pc->x;
593         c.y=pc->y;
594         transform_to_geo(pc->pro, &c, &g);
595         return coordinates_geo(&g, sep);
596
597 }
598
599 static void
600 gui_internal_background_render(struct gui_priv *this, struct widget *w)
601 {
602         struct point pnt=w->p;
603         if (w->state & STATE_HIGHLIGHTED)
604                 graphics_draw_rectangle(this->gra, this->highlight_background, &pnt, w->w, w->h);
605         else {
606                 if (w->background)
607                         graphics_draw_rectangle(this->gra, w->background, &pnt, w->w, w->h);
608         }
609 }
610
611 static struct widget *
612 gui_internal_label_font_new(struct gui_priv *this, char *text, int font)
613 {
614         struct point p[4];
615         int w=0;
616         int h=0;
617
618         struct widget *widget=g_new0(struct widget, 1);
619         widget->type=widget_label;
620         widget->font_idx=font;
621         if (text) {
622                 widget->text=g_strdup(text);
623                 graphics_get_text_bbox(this->gra, this->fonts[font], text, 0x10000, 0x0, p, 0);
624                 w=p[2].x-p[0].x;
625                 h=p[0].y-p[2].y;
626         }
627         widget->h=h+this->spacing;
628         widget->texth=h;
629         widget->w=w+this->spacing;
630         widget->textw=w;
631         widget->flags=gravity_center;
632         widget->foreground=this->text_foreground;
633         widget->text_background=this->text_background;
634
635         return widget;
636 }
637
638 static struct widget *
639 gui_internal_label_new(struct gui_priv *this, char *text)
640 {
641         return gui_internal_label_font_new(this, text, 0);
642 }
643
644 static struct widget *
645 gui_internal_label_new_abbrev(struct gui_priv *this, char *text, int maxwidth)
646 {
647         struct widget *ret=NULL;
648         char *tmp=g_malloc(strlen(text)+3), *p;
649         p=text+strlen(text);
650         while ((p=g_utf8_find_prev_char(text, p)) >= text) {
651                 int i=p-text;
652                 strcpy(tmp, text);
653                 strcpy(tmp+i,"..");
654                 ret=gui_internal_label_new(this, tmp);
655                 if (ret->w < maxwidth)
656                         break;
657                 gui_internal_widget_destroy(this, ret);
658                 ret=NULL;
659         }
660         if(!ret)
661                 ret=gui_internal_label_new(this, "");
662         g_free(tmp);
663         return ret;
664 }
665
666 static struct widget *
667 gui_internal_image_new(struct gui_priv *this, struct graphics_image *image)
668 {
669         struct widget *widget=g_new0(struct widget, 1);
670         widget->type=widget_image;
671         widget->img=image;
672         if (image) {
673                 widget->w=image->width;
674                 widget->h=image->height;
675         }
676         return widget;
677 }
678
679 static void
680 gui_internal_image_render(struct gui_priv *this, struct widget *w)
681 {
682         struct point pnt;
683
684         gui_internal_background_render(this, w);
685         if (w->img) {
686                 pnt=w->p;
687                 pnt.x+=w->w/2-w->img->hot.x;
688                 pnt.y+=w->h/2-w->img->hot.y;
689                 graphics_draw_image(this->gra, this->foreground, &pnt, w->img);
690         }
691 }
692
693 static void
694 gui_internal_label_render(struct gui_priv *this, struct widget *w)
695 {
696         struct point pnt=w->p;
697         gui_internal_background_render(this, w);
698         if (w->state & STATE_EDIT)
699                 graphics_draw_rectangle(this->gra, this->highlight_background, &pnt, w->w, w->h);
700         if (w->text) {
701                 char *text;
702                 char *startext=(char*)g_alloca(strlen(w->text)+1);
703                 text=w->text;
704                 if (w->flags2 & 1) {
705                         int i;
706                         for (i = 0 ; i < strlen(text); i++)
707                                 startext[i]='*';
708                         startext[i]='\0';
709                         text=startext;
710                 }
711                 if (w->flags & gravity_right) {
712                         pnt.y+=w->h-this->spacing;
713                         pnt.x+=w->w-w->textw-this->spacing;
714                         graphics_draw_text(this->gra, w->foreground, w->text_background, this->fonts[w->font_idx], text, &pnt, 0x10000, 0x0);
715                 } else {
716                         pnt.y+=w->h-this->spacing;
717                         graphics_draw_text(this->gra, w->foreground, w->text_background, this->fonts[w->font_idx], text, &pnt, 0x10000, 0x0);
718                 }
719         }
720 }
721
722 /**
723  * @brief A text box is a widget that renders a text string containing newlines.
724  * The string will be broken up into label widgets at each newline with a vertical layout.
725  *
726  */
727 static struct widget *
728 gui_internal_text_font_new(struct gui_priv *this, char *text, int font, enum flags flags)
729 {
730         char *s=g_strdup(text),*s2,*tok;
731         struct widget *ret=gui_internal_box_new(this, flags);
732         s2=s;
733         while ((tok=strtok(s2,"\n"))) {
734                 gui_internal_widget_append(ret, gui_internal_label_font_new(this, tok, font));
735                 s2=NULL;
736         }
737         gui_internal_widget_pack(this,ret);
738         g_free(s);
739         return ret;
740 }
741
742 static struct widget *
743 gui_internal_text_new(struct gui_priv *this, char *text, enum flags flags)
744 {
745         return gui_internal_text_font_new(this, text, 0, flags);
746 }
747
748
749 static struct widget *
750 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)
751 {
752         struct widget *ret=NULL;
753         ret=gui_internal_box_new(this, flags);
754         if (ret) {
755                 if (image)
756                         gui_internal_widget_append(ret, gui_internal_image_new(this, image));
757                 if (text)
758                         gui_internal_widget_append(ret, gui_internal_text_font_new(this, text, font, gravity_center|orientation_vertical));
759                 ret->func=func;
760                 ret->data=data;
761                 if (func) {
762                         ret->state |= STATE_SENSITIVE;
763                         ret->speech=g_strdup(text);
764                 }
765         }
766         return ret;
767
768 }
769
770 static struct widget *
771 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)
772 {
773         return gui_internal_button_font_new_with_callback(this, text, 0, image, flags, func, data);
774 }
775
776 static int
777 gui_internal_button_attr_update(struct gui_priv *this, struct widget *w)
778 {
779         struct widget *wi;
780         int is_on=0;
781         struct attr curr;
782         GList *l;
783
784         if (w->get_attr(w->instance, w->on.type, &curr, NULL))
785                 is_on=curr.u.data == w->on.u.data;
786         else
787                 is_on=w->deflt;
788         if (is_on != w->is_on) {
789                 if (w->redraw)
790                         this->redraw=1;
791                 w->is_on=is_on;
792                 l=g_list_first(w->children);
793                 if (l) {
794                         wi=l->data;
795                         if (wi->img)
796                                 graphics_image_free(this->gra, wi->img);
797                         wi->img=image_new_xs(this, is_on ? "gui_active" : "gui_inactive");
798                 }
799                 if (w->is_on && w->off.type == attr_none)
800                         w->state &= ~STATE_SENSITIVE;
801                 else
802                         w->state |= STATE_SENSITIVE;
803                 return 1;
804         }
805         return 0;
806 }
807
808 static void
809 gui_internal_button_attr_callback(struct gui_priv *this, struct widget *w)
810 {
811         if (gui_internal_button_attr_update(this, w))
812                 gui_internal_widget_render(this, w);
813 }
814 static void
815 gui_internal_button_attr_pressed(struct gui_priv *this, struct widget *w, void *data)
816 {
817         if (w->is_on)
818                 w->set_attr(w->instance, &w->off);
819         else
820                 w->set_attr(w->instance, &w->on);
821         gui_internal_button_attr_update(this, w);
822
823 }
824
825 static struct widget *
826 gui_internal_button_navit_attr_new(struct gui_priv *this, char *text, enum flags flags, struct attr *on, struct attr *off)
827 {
828         struct graphics_image *image=NULL;
829         struct widget *ret;
830         if (!on && !off)
831                 return NULL;
832         image=image_new_xs(this, "gui_inactive");
833         ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
834         if (on)
835                 ret->on=*on;
836         if (off)
837                 ret->off=*off;
838         ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))navit_get_attr;
839         ret->set_attr=(int (*)(void *, struct attr *))navit_set_attr;
840         ret->remove_cb=(void (*)(void *, struct callback *))navit_remove_callback;
841         ret->instance=this->nav;
842         ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
843         navit_add_callback(this->nav, ret->cb);
844         gui_internal_button_attr_update(this, ret);
845         return ret;
846 }
847
848 static struct widget *
849 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)
850 {
851         struct graphics_image *image=NULL;
852         struct widget *ret;
853         image=image_new_xs(this, "gui_inactive");
854         if (!on && !off)
855                 return NULL;
856         ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
857         if (on)
858                 ret->on=*on;
859         if (off)
860                 ret->off=*off;
861         ret->deflt=deflt;
862         ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))map_get_attr;
863         ret->set_attr=(int (*)(void *, struct attr *))map_set_attr;
864         ret->remove_cb=(void (*)(void *, struct callback *))map_remove_callback;
865         ret->instance=map;
866         ret->redraw=1;
867         ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
868         map_add_callback(map, ret->cb);
869         gui_internal_button_attr_update(this, ret);
870         return ret;
871 }
872
873 static struct widget *
874 gui_internal_button_new(struct gui_priv *this, char *text, struct graphics_image *image, enum flags flags)
875 {
876         return gui_internal_button_new_with_callback(this, text, image, flags, NULL, NULL);
877 }
878
879 #if 0
880 //##############################################################################################################
881 //# Description:
882 //# Comment:
883 //# Authors: Martin Schaller (04/2008)
884 //##############################################################################################################
885 static void gui_internal_clear(struct gui_priv *this)
886 {
887         struct graphics *gra=this->gra;
888         struct point pnt;
889         pnt.x=0;
890         pnt.y=0;
891         graphics_draw_rectangle(gra, this->background, &pnt, this->root.w, this->root.h);
892 }
893 #endif
894
895 static struct widget *
896 gui_internal_find_widget(struct widget *wi, struct point *p, int flags)
897 {
898         struct widget *ret,*child;
899         GList *l=wi->children;
900
901         if (p) {
902                 if (wi->p.x > p->x )
903                         return NULL;
904                 if (wi->p.y > p->y )
905                         return NULL;
906                 if ( wi->p.x + wi->w < p->x)
907                         return NULL;
908                 if ( wi->p.y + wi->h < p->y)
909                         return NULL;
910         }
911         if (wi->state & flags)
912                 return wi;
913         while (l) {
914                 child=l->data;
915                 ret=gui_internal_find_widget(child, p, flags);
916                 if (ret) {
917                         return ret;
918                 }
919                 l=g_list_next(l);
920         }
921         return NULL;
922
923 }
924
925 static void
926 gui_internal_highlight_do(struct gui_priv *this, struct widget *found)
927 {
928         if (found == this->highlighted)
929                 return;
930
931         graphics_draw_mode(this->gra, draw_mode_begin);
932         if (this->highlighted) {
933                 this->highlighted->state &= ~STATE_HIGHLIGHTED;
934                 if (this->root.children && this->highlighted_menu == g_list_last(this->root.children)->data)
935                         gui_internal_widget_render(this, this->highlighted);
936                 this->highlighted=NULL;
937                 this->highlighted_menu=NULL;
938         }
939         if (found) {
940                 this->highlighted=found;
941                 this->highlighted_menu=g_list_last(this->root.children)->data;
942                 this->highlighted->state |= STATE_HIGHLIGHTED;
943                 gui_internal_widget_render(this, this->highlighted);
944                 dbg(1,"%d,%d %dx%d\n", found->p.x, found->p.y, found->w, found->h);
945         }
946         graphics_draw_mode(this->gra, draw_mode_end);
947 }
948 //##############################################################################################################
949 //# Description:
950 //# Comment:
951 //# Authors: Martin Schaller (04/2008)
952 //##############################################################################################################
953 static void gui_internal_highlight(struct gui_priv *this)
954 {
955         struct widget *menu,*found=NULL;
956         if (this->current.x > -1 && this->current.y > -1) {
957                 menu=g_list_last(this->root.children)->data;
958                 found=gui_internal_find_widget(menu, &this->current, STATE_SENSITIVE);
959                 if (!found) {
960                         found=gui_internal_find_widget(menu, &this->current, STATE_EDITABLE);
961                         if (found) {
962                                 if (this->editable && this->editable != found) {
963                                         this->editable->state &= ~ STATE_EDIT;
964                                         gui_internal_widget_render(this, this->editable);
965                                 }
966                                 found->state |= STATE_EDIT;
967                                 gui_internal_widget_render(this, found);
968                                 this->editable=found;
969                                 found=NULL;
970                         }
971                 }
972         }
973         gui_internal_highlight_do(this, found);
974         this->motion_timeout_event=NULL;
975 }
976
977
978 static void gui_internal_gesture_ring_clear(struct gui_priv *this)
979 {
980         this->gesture_ring_last=this->gesture_ring_first=0;
981 };
982
983
984 static struct gesture_elem * gui_internal_gesture_ring_get(struct gui_priv *this, int i)
985 {
986         int n=(this->gesture_ring_last-i)%GESTURE_RINGSIZE;
987         if(n==this->gesture_ring_first)
988                 return NULL;
989         return this->gesture_ring+n;
990 };
991
992 static void gui_internal_gesture_ring_add(struct gui_priv *this, struct point *p)
993 {
994         int msec;
995 #ifndef HAVE_API_WIN32_CE
996         struct timeval tv;
997         gettimeofday(&tv,NULL);
998         msec=tv.tv_sec*1000+tv.tv_usec/1000;
999 #else
1000         msec=GetTickCount();
1001 #endif
1002         this->gesture_ring_last++;
1003         this->gesture_ring_last%=GESTURE_RINGSIZE;
1004         if(this->gesture_ring_last==this->gesture_ring_first) {
1005                 this->gesture_ring_first++;
1006                 this->gesture_ring_first%=GESTURE_RINGSIZE;
1007         }
1008         this->gesture_ring[this->gesture_ring_last].p=*p;
1009         this->gesture_ring[this->gesture_ring_last].msec=msec;
1010         dbg(2,"msec=%d x=%d y=%d\n",msec,p->x,p->y);
1011 };
1012
1013
1014 /*
1015  * @brief Calculate movement vector and timing of the gesture.
1016  * @param in this gui context
1017  * @param in msec time in milliseconds to find gesture within
1018  * @param out p0 pointer to the point object, where gesture starting point coordinates should be placed. Can be NULL.
1019  * @param out dx pointer to variable to store horizontal movement of the gesture.
1020  * @param out dy pointer to variable to store vertical movement of the gesture.
1021  * @returns amount of time the actual movement took.
1022  */
1023 static int gui_internal_gesture_get_vector(struct gui_priv *this, int msec, struct point *p0, int *dx, int *dy)
1024 {
1025         struct gesture_elem *g;
1026         int x,y,dt;
1027         int i;
1028
1029         dt=0;
1030
1031         if(dx) *dx=0;
1032         if(dy) *dy=0;
1033         if(p0) {
1034                 p0->x=-1;
1035                 p0->y=-1;
1036         }
1037
1038         g=gui_internal_gesture_ring_get(this,0);
1039         if(!g)
1040                 return 0;
1041         x=g->p.x;
1042         y=g->p.y;
1043         if(p0) {
1044                 *p0=g->p;
1045         }
1046         msec=g->msec;
1047         dbg(2,"%d %d %d\n",g->msec, g->p.x, g->p.y);
1048         for(i=1;(g=gui_internal_gesture_ring_get(this,i))!=NULL;i++) {
1049                 if( msec-g->msec > 1000 )
1050                         break;
1051                 dt=msec-g->msec;
1052                 if(dx) *dx=x-g->p.x;
1053                 if(dy) *dy=y-g->p.y;
1054                 if(p0) {
1055                         *p0=g->p;
1056                 }
1057                 dbg(2,"%d %d %d\n",g->msec, g->p.x, g->p.y);
1058         }
1059         return dt;
1060 }
1061
1062 static int gui_internal_gesture_do(struct gui_priv *this)
1063 {
1064         int dt;
1065         int dx,dy;
1066         
1067         dt=gui_internal_gesture_get_vector(this, 1000, NULL, &dx, &dy);
1068
1069         if( abs(dx) > this->icon_s*3 && abs(dy) < this->icon_s ) {
1070                 struct widget *wt;
1071                 dbg(1,"horizontal dx=%d dy=%d\n",dx,dy);
1072
1073                 /* Prevent swiping if widget was scrolled beforehand */
1074                 if(this->pressed==2)
1075                         return 0;
1076
1077                 for(wt=this->highlighted;wt && wt->type!=widget_table;wt=wt->parent);
1078                 if(!wt || wt->type!=widget_table || !wt->data)
1079                         return 0;
1080                 if(this->highlighted) {
1081                         this->highlighted->state &= ~STATE_HIGHLIGHTED;
1082                         this->highlighted=NULL;
1083                 }
1084                 if(dx<0)
1085                         gui_internal_table_button_next(this,NULL,wt);
1086                 else
1087                         gui_internal_table_button_prev(this,NULL,wt);
1088                 return 1;
1089         } else if( abs(dy) > this->icon_s*3 && abs(dx) < this->icon_s ) {
1090                 dbg(1,"vertical dx=%d dy=%d\n",dx,dy);
1091         } else if (dt>300 && abs(dx) <this->icon_s && abs(dy) <this->icon_s ) {
1092                 dbg(1,"longtap dx=%d dy=%d\n",dx,dy);
1093         } else {
1094                 dbg(1,"none dx=%d dy=%d\n",dx,dy);
1095         }
1096         
1097         return 0;
1098
1099 };
1100
1101 static void gui_internal_motion_cb(struct gui_priv *this)
1102 {
1103         this->motion_timeout_event=NULL;
1104         gui_internal_gesture_ring_add(this, &(this->current));
1105
1106         /* Check for scrollable table below the highligted item if there's a movement with the button pressed */
1107         if (this->pressed && this->highlighted) {
1108                 struct widget *wt=NULL;
1109                 struct widget *wr=NULL;
1110                 int dx,dy;
1111                 
1112                 /* Guard against accidental scrolling when user is likely going to swipe */
1113                 gui_internal_gesture_get_vector(this, 1000, NULL, &dx, &dy);
1114                 if(abs(dx)>abs(dy) || abs(dy)<this->icon_s)
1115                         return;
1116         
1117                 if(this->highlighted)
1118                         for(wr=this->highlighted;wr && wr->type!=widget_table_row;wr=wr->parent);
1119                 if(wr)
1120                         wt=wr->parent;
1121
1122                 if(wt && wt->type==widget_table && (wt->state & STATE_SCROLLABLE)) {
1123                         struct table_data *td=wt->data;
1124                         GList *top=NULL;
1125                         GList *btm=NULL;
1126                         GList *ttop, *tbtm;
1127                         
1128                         
1129
1130                         if(!wr || !wr->h)
1131                                 return;
1132                         
1133                         if(this->current.y < wr->p.y  && wr!=td->top_row->data ) {
1134                                 int n=(wr->p.y-this->current.y)/wr->h+1;
1135
1136                                 btm=td->bottom_row;
1137                                 top=td->top_row;
1138
1139                                 while(n-->0 && (tbtm=gui_internal_widget_table_next_row(btm))!=NULL && (ttop=gui_internal_widget_table_next_row(top))!=NULL) {
1140                                         top=ttop;
1141                                         btm=tbtm;
1142                                         if(top->data==wr)
1143                                                 break;
1144                                 }
1145                                 this->pressed=2;
1146                         } else if (this->current.y > wr->p.y + wr->h ) {
1147                                 int y=wt->p.y+wt->h-wr->h;
1148                                 int n;
1149
1150                                 if(td->button_box && td->button_box->p.y!=0)
1151                                         y=td->button_box->p.y - td->button_box->h;
1152
1153                                 if(y>this->current.y)
1154                                         y=this->current.y;
1155
1156                                 n=(y - wr->p.y )/wr->h;
1157
1158                                 btm=td->bottom_row;
1159                                 top=td->top_row;
1160                                 
1161                                 while(n-->0 && (ttop=gui_internal_widget_table_prev_row(top))!=NULL && (tbtm=gui_internal_widget_table_prev_row(btm))!=NULL) {
1162                                         btm=tbtm;
1163                                         top=ttop;
1164                                         if(btm->data==wr)
1165                                                 break;
1166                                 }
1167                                 this->pressed=2;
1168                         }
1169                         if( top && btm && (td->top_row!=top || td->bottom_row!=btm) ) {
1170                                 gui_internal_table_hide_rows(wt->data);
1171                                 td->top_row=top;
1172                                 td->bottom_row=btm;
1173                                 graphics_draw_mode(this->gra, draw_mode_begin);
1174                                 gui_internal_widget_render(this,wt);
1175                                 graphics_draw_mode(this->gra, draw_mode_end);
1176                         }
1177
1178                         return;
1179                 }
1180         }
1181
1182         /* Else, just move highlight after pointer if there's nothing to scroll */
1183         gui_internal_highlight(this);
1184 }
1185
1186 static struct widget *
1187 gui_internal_box_new_with_label(struct gui_priv *this, enum flags flags, const char *label)
1188 {
1189         struct widget *widget=g_new0(struct widget, 1);
1190
1191         if (label)
1192                 widget->text=g_strdup(label);
1193         widget->type=widget_box;
1194         widget->flags=flags;
1195         return widget;
1196 }
1197
1198 static struct widget *
1199 gui_internal_box_new(struct gui_priv *this, enum flags flags)
1200 {
1201         return gui_internal_box_new_with_label(this, flags, NULL);
1202 }
1203
1204
1205 static void gui_internal_box_render(struct gui_priv *this, struct widget *w)
1206 {
1207         struct widget *wc;
1208         GList *l;
1209
1210         gui_internal_background_render(this, w);
1211 #if 0
1212         w->border=1;
1213         w->foreground=this->foreground;
1214 #endif
1215 #if 1
1216         if (w->foreground && w->border) {
1217         struct point pnt[5];
1218         pnt[0]=w->p;
1219         pnt[1].x=pnt[0].x+w->w;
1220         pnt[1].y=pnt[0].y;
1221         pnt[2].x=pnt[0].x+w->w;
1222         pnt[2].y=pnt[0].y+w->h;
1223         pnt[3].x=pnt[0].x;
1224         pnt[3].y=pnt[0].y+w->h;
1225         pnt[4]=pnt[0];
1226         graphics_gc_set_linewidth(w->foreground, w->border ? w->border : 1);
1227         graphics_draw_lines(this->gra, w->foreground, pnt, 5);
1228         graphics_gc_set_linewidth(w->foreground, 1);
1229         }
1230 #endif
1231
1232         l=w->children;
1233         while (l) {
1234                 wc=l->data;
1235                 gui_internal_widget_render(this, wc);
1236                 l=g_list_next(l);
1237         }
1238 }
1239
1240
1241 /**
1242  * @brief Compute the size and location for the widget.
1243  *
1244  *
1245  */
1246 static void gui_internal_box_pack(struct gui_priv *this, struct widget *w)
1247 {
1248         struct widget *wc;
1249         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;
1250         GList *l;
1251         int orientation=w->flags & 0xffff0000;
1252
1253         if (!cols)
1254                 cols=this->cols;
1255         if (!cols) {
1256                  if ( this->root.w > this->root.h )
1257                          cols=3;
1258                  else
1259                          cols=2;
1260                  width=0;
1261                  height=0;
1262         }
1263
1264
1265         /**
1266          * count the number of children
1267          */
1268         l=w->children;
1269         while (l) {
1270                 count++;
1271                 l=g_list_next(l);
1272         }
1273         if (orientation == orientation_horizontal_vertical && count <= cols)
1274                 orientation=orientation_horizontal;
1275         switch (orientation) {
1276         case orientation_horizontal:
1277                 /**
1278                  * For horizontal orientation:
1279                  * pack each child and find the largest height and
1280                  * compute the total width. x spacing (spx) is considered
1281                  *
1282                  * If any children want to be expanded
1283                  * we keep track of this
1284                  */
1285                 l=w->children;
1286                 while (l) {
1287                         wc=l->data;
1288                         gui_internal_widget_pack(this, wc);
1289                         if (height < wc->h)
1290                                 height=wc->h;
1291                         width+=wc->w;
1292                         if (wc->flags & flags_expand)
1293                                 expand+=wc->w ? wc->w : 1;
1294                         l=g_list_next(l);
1295                         if (l)
1296                                 width+=w->spx;
1297                 }
1298                 owidth=width;
1299                 if (expand && w->w) {
1300                         expandd=w->w-width+expand;
1301                         owidth=w->w;
1302                 } else
1303                         expandd=expand=1;
1304                 break;
1305         case orientation_vertical:
1306                 /**
1307                  * For vertical layouts:
1308                  * We pack each child and compute the largest width and
1309                  * the total height.  y spacing (spy) is considered
1310                  *
1311                  * If the expand flag is set then teh expansion amount
1312                  * is computed.
1313                  */
1314                 l=w->children;
1315                 while (l) {
1316                         wc=l->data;
1317                         gui_internal_widget_pack(this, wc);
1318                         if (width < wc->w)
1319                                 width=wc->w;
1320                         height+=wc->h;
1321                         if (wc->flags & flags_expand)
1322                                 expand+=wc->h ? wc->h : 1;
1323                         l=g_list_next(l);
1324                         if (l)
1325                                 height+=w->spy;
1326                 }
1327                 oheight=height;
1328                 if (expand && w->h) {
1329                         expandd=w->h-height+expand;
1330                         oheight=w->h;
1331                 } else
1332                         expandd=expand=1;
1333                 break;
1334         case orientation_horizontal_vertical:
1335                 /**
1336                  * For horizontal_vertical orientation
1337                  * pack the children.
1338                  * Compute the largest height and largest width.
1339                  * Layout the widgets based on having the
1340                  * number of columns specified by (cols)
1341                  */
1342                 count=0;
1343                 l=w->children;
1344                 while (l) {
1345                         wc=l->data;
1346                         gui_internal_widget_pack(this, wc);
1347                         if (height < wc->h)
1348                                 height=wc->h;
1349                         if (width < wc->w)
1350                                 width=wc->w;
1351                         l=g_list_next(l);
1352                         count++;
1353                 }
1354                 if (count < cols)
1355                         cols=count;
1356                 rows=(count+cols-1)/cols;
1357                 width*=cols;
1358                 height*=rows;
1359                 width+=w->spx*(cols-1);
1360                 height+=w->spy*(rows-1);
1361                 owidth=width;
1362                 oheight=height;
1363                 expandd=expand=1;
1364                 break;
1365         default:
1366                 /**
1367                  * No orientation was specified.
1368                  * width and height are both 0.
1369                  * The width & height of this widget
1370                  * will be used.
1371                  */
1372                 if(!w->w && !w->h)
1373                         dbg(0,"Warning width and height of a widget are 0");
1374                 break;
1375         }
1376         if (! w->w && ! w->h) {
1377                 w->w=w->bl+w->br+width;
1378                 w->h=w->bt+w->bb+height;
1379                 w->packed=1;
1380         }
1381 #if 0
1382         if (expand < 100)
1383                 expand=100;
1384 #endif
1385
1386         /**
1387          * At this stage the width and height of this
1388          * widget has been computed.
1389          * We now make a second pass assigning heights,
1390          * widths and coordinates to each child widget.
1391          */
1392
1393         if (w->flags & gravity_left)
1394                 x=w->p.x+w->bl;
1395         if (w->flags & gravity_xcenter)
1396                 x=w->p.x+w->w/2-owidth/2;
1397         if (w->flags & gravity_right)
1398                 x=w->p.x+w->w-w->br-owidth;
1399         if (w->flags & gravity_top)
1400                 y=w->p.y+w->bt;
1401         if (w->flags & gravity_ycenter)
1402                 y=w->p.y+w->h/2-oheight/2;
1403         if (w->flags & gravity_bottom)
1404                 y=w->p.y+w->h-w->bb-oheight;
1405         l=w->children;
1406         switch (orientation) {
1407         case orientation_horizontal:
1408                 l=w->children;
1409                 while (l) {
1410                         wc=l->data;
1411                         wc->p.x=x;
1412                         if (wc->flags & flags_fill)
1413                                 wc->h=w->h;
1414                         if (wc->flags & flags_expand) {
1415                                 if (! wc->w)
1416                                         wc->w=1;
1417                                 wc->w=wc->w*expandd/expand;
1418                         }
1419                         if (w->flags & gravity_top)
1420                                 wc->p.y=y;
1421                         if (w->flags & gravity_ycenter)
1422                                 wc->p.y=y-wc->h/2;
1423                         if (w->flags & gravity_bottom)
1424                                 wc->p.y=y-wc->h;
1425                         x+=wc->w+w->spx;
1426                         l=g_list_next(l);
1427                 }
1428                 break;
1429         case orientation_vertical:
1430                 l=w->children;
1431                 while (l) {
1432                         wc=l->data;
1433                         wc->p.y=y;
1434                         if (wc->flags & flags_fill)
1435                                 wc->w=w->w;
1436                         if (wc->flags & flags_expand) {
1437                                 if (! wc->h)
1438                                         wc->h=1;
1439                                 wc->h=wc->h*expandd/expand;
1440                         }
1441                         if (w->flags & gravity_left)
1442                                 wc->p.x=x;
1443                         if (w->flags & gravity_xcenter)
1444                                 wc->p.x=x-wc->w/2;
1445                         if (w->flags & gravity_right)
1446                                 wc->p.x=x-wc->w;
1447                         y+=wc->h+w->spy;
1448                         l=g_list_next(l);
1449                 }
1450                 break;
1451         case orientation_horizontal_vertical:
1452                 l=w->children;
1453                 x0=x;
1454                 count=0;
1455                 width/=cols;
1456                 height/=rows;
1457                 while (l) {
1458                         wc=l->data;
1459                         wc->p.x=x;
1460                         wc->p.y=y;
1461                         if (wc->flags & flags_fill) {
1462                                 wc->w=width;
1463                                 wc->h=height;
1464                         }
1465                         if (w->flags & gravity_left)
1466                                 wc->p.x=x;
1467                         if (w->flags & gravity_xcenter)
1468                                 wc->p.x=x+(width-wc->w)/2;
1469                         if (w->flags & gravity_right)
1470                                 wc->p.x=x+width-wc->w;
1471                         if (w->flags & gravity_top)
1472                                 wc->p.y=y;
1473                         if (w->flags & gravity_ycenter)
1474                                 wc->p.y=y+(height-wc->h)/2;
1475                         if (w->flags & gravity_bottom)
1476                                 wc->p.y=y-height-wc->h;
1477                         x+=width;
1478                         if (++count == cols) {
1479                                 count=0;
1480                                 x=x0;
1481                                 y+=height;
1482                         }
1483                         l=g_list_next(l);
1484                 }
1485                 break;
1486         default:
1487                 break;
1488         }
1489         /**
1490          * Call pack again on each child,
1491          * the child has now had its size and coordinates
1492          * set so they can repack their children.
1493          */
1494         l=w->children;
1495         while (l) {
1496                 wc=l->data;
1497                 gui_internal_widget_pack(this, wc);
1498                 l=g_list_next(l);
1499         }
1500 }
1501
1502 static void
1503 gui_internal_widget_reset_pack(struct gui_priv *this, struct widget *w)
1504 {
1505         struct widget *wc;
1506         GList *l;
1507
1508         l=w->children;
1509         while (l) {
1510                 wc=l->data;
1511                 gui_internal_widget_reset_pack(this, wc);
1512                 l=g_list_next(l);
1513         }
1514         if (w->packed) {
1515                 w->w=0;
1516                 w->h=0;
1517         }
1518 }
1519
1520 static void
1521 gui_internal_widget_append(struct widget *parent, struct widget *child)
1522 {
1523         if (! child)
1524                 return;
1525         if (! child->background)
1526                 child->background=parent->background;
1527         parent->children=g_list_append(parent->children, child);
1528         child->parent=parent;
1529 }
1530
1531 static void gui_internal_widget_prepend(struct widget *parent, struct widget *child)
1532 {
1533         if (! child->background)
1534                 child->background=parent->background;
1535         parent->children=g_list_prepend(parent->children, child);
1536         child->parent=parent;
1537 }
1538
1539 static void gui_internal_widget_insert_before(struct widget *parent, struct widget *sibling, struct widget *child)
1540 {
1541         GList *sib=NULL;
1542         if (! child->background)
1543                 child->background=parent->background;
1544
1545         if(sibling) 
1546                 sib=g_list_find(parent->children,sibling);
1547         parent->children=g_list_insert_before(parent->children, sib, child);
1548         child->parent=parent;
1549 }
1550
1551 static void gui_internal_widget_insert_sorted(struct widget *parent, struct widget *child, GCompareFunc func)
1552 {
1553         if (! child->background)
1554                 child->background=parent->background;
1555
1556         parent->children=g_list_insert_sorted(parent->children, child, func);
1557         child->parent=parent;
1558 }
1559
1560
1561 static void gui_internal_widget_children_destroy(struct gui_priv *this, struct widget *w)
1562 {
1563         GList *l;
1564         struct widget *wc;
1565
1566         l=w->children;
1567         while (l) {
1568                 wc=l->data;
1569                 gui_internal_widget_destroy(this, wc);
1570                 l=g_list_next(l);
1571         }
1572         g_list_free(w->children);
1573         w->children=NULL;
1574 }
1575
1576
1577 static void gui_internal_widget_destroy(struct gui_priv *this, struct widget *w)
1578 {
1579         gui_internal_widget_children_destroy(this, w);
1580         g_free(w->command);
1581         g_free(w->speech);
1582         g_free(w->text);
1583         if (w->img)
1584                 graphics_image_free(this->gra, w->img);
1585         if (w->prefix)
1586                 g_free(w->prefix);
1587         if (w->name)
1588                 g_free(w->name);
1589         if (w->data_free)
1590                 w->data_free(w->data);
1591         if (w->cb && w->remove_cb)
1592                 w->remove_cb(w->instance, w->cb);
1593         if (w==this->highlighted)
1594             this->highlighted=NULL;
1595         if(w->wfree)
1596                 w->wfree(this,w);
1597         else
1598                 g_free(w);
1599 }
1600
1601
1602 static void
1603 gui_internal_widget_render(struct gui_priv *this, struct widget *w)
1604 {
1605         if(w->p.x > this->root.w || w->p.y > this->root.h)
1606                 return;
1607
1608         switch (w->type) {
1609         case widget_box:
1610                 gui_internal_box_render(this, w);
1611                 break;
1612         case widget_label:
1613                 gui_internal_label_render(this, w);
1614                 break;
1615         case widget_image:
1616                 gui_internal_image_render(this, w);
1617                 break;
1618         case widget_table:
1619                 gui_internal_table_render(this,w);
1620                 break;
1621         default:
1622                 break;
1623         }
1624 }
1625
1626 static void
1627 gui_internal_widget_pack(struct gui_priv *this, struct widget *w)
1628 {
1629         switch (w->type) {
1630         case widget_box:
1631                 gui_internal_box_pack(this, w);
1632                 break;
1633         case widget_table:
1634           gui_internal_table_pack(this,w);
1635         default:
1636                 break;
1637         }
1638 }
1639
1640 //##############################################################################################################
1641 //# Description:
1642 //# Comment:
1643 //# Authors: Martin Schaller (04/2008)
1644 //##############################################################################################################
1645 static void gui_internal_call_highlighted(struct gui_priv *this)
1646 {
1647         if (! this->highlighted || ! this->highlighted->func)
1648                 return;
1649         this->highlighted->reason=1;
1650         this->highlighted->func(this, this->highlighted, this->highlighted->data);
1651 }
1652
1653 static void
1654 gui_internal_say(struct gui_priv *this, struct widget *w, int questionmark)
1655 {
1656         char *text=w->speech;
1657         if (! this->speech)
1658                 return;
1659         if (!text)
1660                 text=w->text;
1661         if (!text)
1662                 text=w->name;
1663         if (text) {
1664                 text=g_strdup_printf("%s%c", text, questionmark ? '?':'\0');
1665                 navit_say(this->nav, text);
1666                 g_free(text);
1667         }
1668 }
1669
1670 static void
1671 gui_internal_menu_destroy(struct gui_priv *this, struct widget *w)
1672 {
1673         struct menu_data *menu_data=w->menu_data;
1674         if (menu_data) {
1675                 if (menu_data->refresh_callback_obj.type) {
1676                         struct object_func *func;
1677                         func=object_func_lookup(menu_data->refresh_callback_obj.type);
1678                         if (func && func->remove_attr)
1679                                 func->remove_attr(menu_data->refresh_callback_obj.u.data, &menu_data->refresh_callback);
1680                 }
1681                 if (menu_data->refresh_callback.u.callback)
1682                         callback_destroy(menu_data->refresh_callback.u.callback);
1683
1684                 g_free(menu_data->href);
1685                 g_free(menu_data);
1686         }
1687         gui_internal_widget_destroy(this, w);
1688         this->root.children=g_list_remove(this->root.children, w);
1689 }
1690
1691 static void
1692 gui_internal_prune_menu_do(struct gui_priv *this, struct widget *w, int render)
1693 {
1694         GList *l;
1695         struct widget *wr,*wd;
1696         gui_internal_search_idle_end(this);
1697         while ((l = g_list_last(this->root.children))) {
1698                 wd=l->data;
1699                 if (wd == w) {
1700                         void (*redisplay)(struct gui_priv *priv, struct widget *widget, void *data);
1701                         if (!render)
1702                                 return;
1703                         gui_internal_say(this, w, 0);
1704                         redisplay=w->menu_data->redisplay;
1705                         wr=w->menu_data->redisplay_widget;
1706                         if (!w->menu_data->redisplay && !w->menu_data->href) {
1707                                 gui_internal_widget_render(this, w);
1708                                 return;
1709                         }
1710                         if (redisplay) {
1711                                 gui_internal_menu_destroy(this, w);
1712                                 redisplay(this, wr, wr->data);
1713                         } else {
1714                                 char *href=g_strdup(w->menu_data->href);
1715                                 gui_internal_menu_destroy(this, w);
1716                                 gui_internal_html_load_href(this, href, 0);
1717                                 g_free(href);
1718                         }
1719                         return;
1720                 }
1721                 gui_internal_menu_destroy(this, wd);
1722         }
1723 }
1724
1725 static void
1726 gui_internal_prune_menu(struct gui_priv *this, struct widget *w)
1727 {
1728         gui_internal_prune_menu_do(this, w, 1);
1729 }
1730
1731 static void
1732 gui_internal_prune_menu_count(struct gui_priv *this, int count, int render)
1733 {
1734         GList *l=g_list_last(this->root.children);
1735         struct widget *w=NULL;
1736         while (l && count-- > 0)
1737                 l=g_list_previous(l);
1738         if (l) {
1739                 w=l->data;
1740                 gui_internal_prune_menu_do(this, w, render);
1741         }
1742 }
1743
1744 static void
1745 gui_internal_back(struct gui_priv *this, struct widget *w, void *data)
1746 {
1747         gui_internal_prune_menu_count(this, 1, 1);
1748 }
1749
1750 static void
1751 gui_internal_cmd_return(struct gui_priv *this, struct widget *wm, void *data)
1752 {
1753         gui_internal_prune_menu(this, wm->data);
1754 }
1755
1756 static void
1757 gui_internal_cmd2_back(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
1758 {
1759         graphics_draw_mode(this->gra, draw_mode_begin);
1760         gui_internal_back(this, NULL, NULL);
1761         graphics_draw_mode(this->gra, draw_mode_end);
1762         gui_internal_check_exit(this);
1763 }
1764
1765 static void
1766 gui_internal_cmd2_back_to_map(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
1767 {
1768         gui_internal_prune_menu(this, NULL);
1769 }
1770
1771 static void
1772 gui_internal_cmd_main_menu(struct gui_priv *this, struct widget *wm, void *data)
1773 {
1774         struct widget *w=this->root.children->data;
1775         if (w && w->menu_data && w->menu_data->href && !strcmp(w->menu_data->href,"#Main Menu")) 
1776                 gui_internal_prune_menu(this, w);
1777         else
1778                 gui_internal_html_main_menu(this);
1779 }
1780
1781 static struct widget *
1782 gui_internal_top_bar(struct gui_priv *this)
1783 {
1784         struct widget *w,*wm,*wh,*wc,*wcn;
1785         int dots_len, sep_len;
1786         GList *res=NULL,*l;
1787         int width,width_used=0,use_sep=0,incomplete=0;
1788         struct graphics_gc *foreground=(this->flags & 256 ? this->background : this->text_foreground);
1789 /* flags
1790         1:Don't expand bar to screen width
1791         2:Don't show Map Icon
1792         4:Don't show Home Icon
1793         8:Show only current menu
1794         16:Don't use menu titles as button
1795         32:Show navit version
1796         64:Show time
1797         128:Show help
1798         256:Use background for menu headline
1799         512:Set osd_configuration and zoom to route when setting position
1800         1024:Don't show back button
1801         2048:No highlighting of keyboard
1802 */
1803
1804         w=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|(this->flags & 1 ? 0:flags_fill));
1805         w->bl=this->spacing;
1806         w->spx=this->spacing;
1807         w->background=this->background2;
1808         if ((this->flags & 6) == 6) {
1809                 w->bl=10;
1810                 w->br=10;
1811                 w->bt=6;
1812                 w->bb=6;
1813         }
1814         width=this->root.w-w->bl;
1815         if (! (this->flags & 2)) {
1816                 wm=gui_internal_button_new_with_callback(this, NULL,
1817                         image_new_s(this, "gui_map"), gravity_center|orientation_vertical,
1818                         gui_internal_cmd_return, NULL);
1819                 wm->speech=g_strdup(_("Back to map"));
1820                 gui_internal_widget_pack(this, wm);
1821                 gui_internal_widget_append(w, wm);
1822                 width-=wm->w;
1823         }
1824         if (! (this->flags & 4)) {
1825                 wh=gui_internal_button_new_with_callback(this, NULL,
1826                         image_new_s(this, "gui_home"), gravity_center|orientation_vertical,
1827                         gui_internal_cmd_main_menu, NULL);
1828                 wh->speech=g_strdup(_("Main Menu"));
1829                 gui_internal_widget_pack(this, wh);
1830                 gui_internal_widget_append(w, wh);
1831                 width-=wh->w;
1832         }
1833         if (!(this->flags & 6))
1834                 width-=w->spx;
1835         l=g_list_last(this->root.children);
1836         wcn=gui_internal_label_new(this,".. »");
1837         wcn->foreground=foreground;
1838         dots_len=wcn->w;
1839         gui_internal_widget_destroy(this, wcn);
1840         wcn=gui_internal_label_new(this,"»");
1841         wcn->foreground=foreground;
1842         sep_len=wcn->w;
1843         gui_internal_widget_destroy(this, wcn);
1844         while (l) {
1845                 if (g_list_previous(l) || !g_list_next(l)) {
1846                         wc=l->data;
1847                         wcn=gui_internal_label_new(this, wc->text);
1848                         wcn->foreground=foreground;
1849                         if (g_list_next(l))
1850                                 use_sep=1;
1851                         else
1852                                 use_sep=0;
1853                         dbg(1,"%d (%s) + %d + %d + %d > %d\n", wcn->w, wc->text, width_used, w->spx, use_sep ? sep_len : 0, width);
1854                         if (wcn->w + width_used + w->spx + (use_sep ? sep_len : 0) + (g_list_previous(l) ? dots_len : 0) > width) {
1855                                 incomplete=1;
1856                                 gui_internal_widget_destroy(this, wcn);
1857                                 break;
1858                         }
1859                         if (use_sep) {
1860                                 struct widget *wct=gui_internal_label_new(this, "»");
1861                                 wct->foreground=foreground;
1862                                 res=g_list_prepend(res, wct);
1863                                 width_used+=sep_len+w->spx;
1864                         }
1865                         width_used+=wcn->w;
1866                         if (!(this->flags & 16)) {
1867                                 wcn->func=gui_internal_cmd_return;
1868                                 wcn->data=wc;
1869                                 wcn->state |= STATE_SENSITIVE;
1870                         }
1871                         res=g_list_prepend(res, wcn);
1872                         if (this->flags & 8)
1873                                 break;
1874                 }
1875                 l=g_list_previous(l);
1876         }
1877         if (incomplete) {
1878                 if (! res) {
1879                         wcn=gui_internal_label_new_abbrev(this, wc->text, width-width_used-w->spx-dots_len);
1880                         wcn->foreground=foreground;
1881                         wcn->func=gui_internal_cmd_return;
1882                         wcn->data=wc;
1883                         wcn->state |= STATE_SENSITIVE;
1884                         res=g_list_prepend(res, wcn);
1885                         l=g_list_previous(l);
1886                         wc=l?l->data:NULL;
1887                 }
1888                 if(wc) {
1889                         wcn=gui_internal_label_new(this, ".. »");
1890                         wcn->foreground=foreground;
1891                         wcn->func=gui_internal_cmd_return;
1892                         wcn->data=wc;
1893                         wcn->state |= STATE_SENSITIVE;
1894                         res=g_list_prepend(res, wcn);
1895                 }
1896         }
1897         l=res;
1898         while (l) {
1899                 gui_internal_widget_append(w, l->data);
1900                 l=g_list_next(l);
1901         }
1902         if (this->flags & 32) {
1903                 char *version_text=g_strdup_printf("Navit %s",version);
1904                 wcn=gui_internal_label_new(this, version_text);
1905                 g_free(version_text);
1906                 wcn->flags=gravity_right_center|flags_expand;
1907                 gui_internal_widget_append(w, wcn);
1908         }
1909 #if 0
1910         if (dots)
1911                 gui_internal_widget_destroy(this, dots);
1912 #endif
1913         return w;
1914 }
1915
1916 static struct widget *
1917 gui_internal_time_help(struct gui_priv *this)
1918 {
1919         struct widget *w,*wc,*wcn;
1920         char timestr[64];
1921         struct tm *tm;
1922         time_t timep;
1923
1924         w=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
1925         w->bl=this->spacing;
1926         w->spx=this->spacing;
1927         w->spx=10;
1928         w->bl=10;
1929         w->br=10;
1930         w->bt=6;
1931         w->bb=6;
1932         if (this->flags & 64) {
1933                 wc=gui_internal_box_new(this, gravity_right_top|orientation_vertical|flags_fill);
1934                 wc->bl=10;
1935                 wc->br=20;
1936                 wc->bt=6;
1937                 wc->bb=6;
1938                 timep=time(NULL);
1939                 tm=localtime(&timep);
1940                 strftime(timestr, 64, "%H:%M %d.%m.%Y", tm);
1941                 wcn=gui_internal_label_new(this, timestr);
1942                 gui_internal_widget_append(wc, wcn);
1943                 gui_internal_widget_append(w, wc);
1944         }
1945         if (this->flags & 128) {
1946                 wcn=gui_internal_button_new_with_callback(this, _("Help"), image_new_l(this, "gui_help"), gravity_center|orientation_vertical|flags_fill, NULL, NULL);
1947                 gui_internal_widget_append(w, wcn);
1948         }
1949         return w;
1950 }
1951
1952
1953 /**
1954  * Applys the configuration values to this based on the settings
1955  * specified in the configuration file (this->config) and
1956  * the most approriate default profile based on screen resolution.
1957  *
1958  * This function should be run after this->root is setup and could
1959  * be rerun after the window is resized.
1960  *
1961  * @authors Steve Singer <ssinger_pg@sympatico.ca> (09/2008)
1962  */
1963 static void gui_internal_apply_config(struct gui_priv *this)
1964 {
1965   struct gui_config_settings *  current_config=0;
1966
1967   dbg(1,"w=%d h=%d\n", this->root.w, this->root.h);
1968   /**
1969    * Select default values from profile based on the screen.
1970    */
1971   if((this->root.w > 320 || this->root.h > 320) && this->root.w > 240 && this->root.h > 240)
1972   {
1973     if((this->root.w > 640 || this->root.h > 640) && this->root.w > 480 && this->root.h > 480 )
1974     {
1975       current_config = &config_profiles[LARGE_PROFILE];
1976     }
1977     else
1978     {
1979       current_config = &config_profiles[MEDIUM_PROFILE];
1980     }
1981   }
1982   else
1983   {
1984     current_config = &config_profiles[SMALL_PROFILE];
1985   }
1986
1987   /**
1988    * Apply override values from config file
1989    */
1990   if(this->config.font_size == -1 )
1991   {
1992     this->font_size = current_config->font_size;
1993   }
1994   else
1995   {
1996     this->font_size = this->config.font_size;
1997   }
1998
1999   if(this->config.icon_xs == -1 )
2000   {
2001       this->icon_xs = current_config->icon_xs;
2002   }
2003   else
2004   {
2005     this->icon_xs = this->config.icon_xs;
2006   }
2007
2008   if(this->config.icon_s == -1 )
2009   {
2010     this->icon_s = current_config->icon_s;
2011   }
2012   else
2013   {
2014     this->icon_s = this->config.icon_s;
2015   }
2016   if(this->config.icon_l == -1 )
2017   {
2018     this->icon_l = current_config->icon_l;
2019   }
2020   else
2021   {
2022     this->icon_l = this->config.icon_l;
2023   }
2024   if(this->config.spacing == -1 )
2025   {
2026     this->spacing = current_config->spacing;
2027   }
2028   else
2029   {
2030     this->spacing = current_config->spacing;
2031   }
2032
2033 }
2034
2035 static struct widget *
2036 gui_internal_button_label(struct gui_priv *this, char *label, int mode)
2037 {
2038         struct widget *wl,*wlb;
2039         struct widget *wb=gui_internal_menu_data(this)->button_bar;
2040         wlb=gui_internal_box_new(this, gravity_right_center|orientation_vertical);
2041         wl=gui_internal_label_new(this, label);
2042         wlb->border=1;
2043         wlb->foreground=this->text_foreground;
2044         wlb->bl=20;
2045         wlb->br=20;
2046         wlb->bb=6;
2047         wlb->bt=6;
2048         gui_internal_widget_append(wlb, wl);
2049         if (mode == 1)
2050                 gui_internal_widget_prepend(wb, wlb);
2051         if (mode == -1)
2052                 gui_internal_widget_append(wb, wlb);
2053
2054         return wlb;
2055 }
2056
2057
2058 static struct widget *
2059 gui_internal_menu(struct gui_priv *this, const char *label)
2060 {
2061         struct widget *menu,*w,*w1,*topbox;
2062
2063         gui_internal_search_idle_end(this);
2064         topbox=gui_internal_box_new_with_label(this, 0, label);
2065         topbox->w=this->root.w;
2066         topbox->h=this->root.h;
2067         gui_internal_widget_append(&this->root, topbox);
2068         menu=gui_internal_box_new(this, gravity_left_center|orientation_vertical);
2069         menu->w=this->root.w;
2070         menu->h=this->root.h;
2071         menu->background=this->background;
2072         gui_internal_apply_config(this);
2073         if (!this->fonts[0]) {
2074                 this->fonts[0]=graphics_font_new(this->gra,this->font_size,1);
2075                 this->fonts[1]=graphics_font_new(this->gra,this->font_size*66/100,1);
2076                 this->fonts[2]=graphics_font_new(this->gra,this->font_size*50/100,1);
2077         }
2078         topbox->menu_data=g_new0(struct menu_data, 1);
2079         gui_internal_widget_append(topbox, menu);
2080         w=gui_internal_top_bar(this);
2081         gui_internal_widget_append(menu, w);
2082         w=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
2083         w->spx=4*this->spacing;
2084         w->w=menu->w;
2085         gui_internal_widget_append(menu, w);
2086         if (this->flags & 16 && !(this->flags & 1024)) {
2087                 struct widget *wlb,*wb,*wm=w;
2088                 wm->flags=gravity_center|orientation_vertical|flags_expand|flags_fill;
2089                 w=gui_internal_box_new(this, gravity_center|orientation_horizontal|flags_expand|flags_fill);
2090                 dbg(0,"topbox->menu_data=%p\n", topbox->menu_data);
2091                 gui_internal_widget_append(wm, w);
2092                 wb=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
2093                 wb->bl=6;
2094                 wb->br=6;
2095                 wb->bb=6;
2096                 wb->bt=6;
2097                 wb->spx=6;
2098                 topbox->menu_data->button_bar=wb;
2099                 gui_internal_widget_append(wm, wb);
2100                 wlb=gui_internal_button_label(this,_("Back"),1);
2101                 wlb->func=gui_internal_back;
2102                 wlb->state |= STATE_SENSITIVE;
2103         }
2104         if (this->flags & 192) {
2105                 menu=gui_internal_box_new(this, gravity_left_center|orientation_vertical);
2106                 menu->w=this->root.w;
2107                 menu->h=this->root.h;
2108                 w1=gui_internal_time_help(this);
2109                 gui_internal_widget_append(menu, w1);
2110                 w1=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
2111                 gui_internal_widget_append(menu, w1);
2112                 gui_internal_widget_append(topbox, menu);
2113                 menu->background=NULL;
2114         }
2115         gui_internal_widget_pack(this, topbox);
2116         gui_internal_widget_reset_pack(this, topbox);
2117         topbox->w=this->root.w;
2118         topbox->h=this->root.h;
2119         menu->w=this->root.w;
2120         menu->h=this->root.h;
2121         return w;
2122 }
2123
2124 static struct menu_data *
2125 gui_internal_menu_data(struct gui_priv *this)
2126 {
2127         GList *l;
2128         struct widget *menu;
2129
2130         l=g_list_last(this->root.children);
2131         menu=l->data;
2132         return menu->menu_data;
2133 }
2134
2135 static void
2136 gui_internal_menu_reset_pack(struct gui_priv *this)
2137 {
2138         GList *l;
2139         struct widget *top_box;
2140
2141         l=g_list_last(this->root.children);
2142         top_box=l->data;
2143         gui_internal_widget_reset_pack(this, top_box);
2144 }
2145
2146 static void
2147 gui_internal_menu_render(struct gui_priv *this)
2148 {
2149         GList *l;
2150         struct widget *menu;
2151
2152         l=g_list_last(this->root.children);
2153         menu=l->data;
2154         gui_internal_say(this, menu, 0);
2155         gui_internal_widget_pack(this, menu);
2156         gui_internal_widget_render(this, menu);
2157 }
2158
2159 static void
2160 gui_internal_cmd_set_destination(struct gui_priv *this, struct widget *wm, void *data)
2161 {
2162         char *name=data;
2163         dbg(0,"c=%d:0x%x,0x%x\n", wm->c.pro, wm->c.x, wm->c.y);
2164         navit_set_destination(this->nav, &wm->c, name, 1);
2165         if (this->flags & 512) {
2166                 struct attr follow;
2167                 follow.type=attr_follow;
2168                 follow.u.num=180;
2169                 navit_set_attr(this->nav, &this->osd_configuration);
2170                 navit_set_attr(this->nav, &follow);
2171                 navit_zoom_to_route(this->nav, 0);
2172         }
2173         gui_internal_prune_menu(this, NULL);
2174 }
2175
2176 static void
2177 gui_internal_cmd_set_position(struct gui_priv *this, struct widget *wm, void *data)
2178 {
2179         struct attr v;
2180         if(data) {
2181                 v.type=attr_vehicle;
2182                 v.u.vehicle=NULL;
2183                 navit_set_attr(this->nav, &v);
2184         }
2185         navit_set_position(this->nav, &wm->c);
2186         gui_internal_prune_menu(this, NULL);
2187 }
2188
2189 static void
2190 gui_internal_cmd_add_bookmark_do(struct gui_priv *this, struct widget *widget)
2191 {
2192         GList *l;
2193         struct attr attr;
2194         dbg(0,"text='%s'\n", widget->text);
2195         if (widget->text && strlen(widget->text)){
2196                 navit_get_attr(this->nav, attr_bookmarks, &attr, NULL);
2197                 bookmarks_add_bookmark(attr.u.bookmarks, &widget->c, widget->text);
2198         }
2199         g_free(widget->text);
2200         widget->text=NULL;
2201         l=g_list_previous(g_list_last(this->root.children));
2202         gui_internal_prune_menu(this, l->data);
2203 }
2204
2205 static void
2206 gui_internal_cmd_add_bookmark_folder_do(struct gui_priv *this, struct widget *widget)
2207 {
2208         GList *l;
2209         struct attr attr;
2210         dbg(0,"text='%s'\n", widget->text);
2211         if (widget->text && strlen(widget->text)){
2212                 navit_get_attr(this->nav, attr_bookmarks, &attr, NULL);
2213                 bookmarks_add_bookmark(attr.u.bookmarks, NULL, widget->text);
2214         }
2215         g_free(widget->text);
2216         widget->text=NULL;
2217         l=g_list_previous(g_list_previous(g_list_last(this->root.children)));
2218         gui_internal_prune_menu(this, l->data);
2219 }
2220
2221 static void
2222 gui_internal_cmd_add_bookmark_clicked(struct gui_priv *this, struct widget *widget, void *data)
2223 {
2224         gui_internal_cmd_add_bookmark_do(this, widget->data);
2225 }
2226
2227 static void
2228 gui_internal_cmd_add_bookmark_folder_clicked(struct gui_priv *this, struct widget *widget, void *data)
2229 {
2230         gui_internal_cmd_add_bookmark_folder_do(this, widget->data);
2231 }
2232
2233 static void
2234 gui_internal_cmd_rename_bookmark_clicked(struct gui_priv *this, struct widget *widget,void *data)
2235 {
2236         struct widget *w=(struct widget*)widget->data;
2237         GList *l;
2238         struct attr attr;
2239         dbg(0,"text='%s'\n", w->text);
2240         if (w->text && strlen(w->text)){
2241                 navit_get_attr(this->nav, attr_bookmarks, &attr, NULL);
2242                 bookmarks_rename_bookmark(attr.u.bookmarks, w->name, w->text);
2243         }
2244         g_free(w->text);
2245         g_free(w->name);
2246         w->text=NULL;
2247         w->name=NULL;
2248         l=g_list_previous(g_list_previous(g_list_previous(g_list_last(this->root.children))));
2249         gui_internal_prune_menu(this, l->data);
2250 }
2251
2252 static void
2253 gui_internal_cmd_add_bookmark_changed(struct gui_priv *this, struct widget *wm, void *data)
2254 {
2255         int len;
2256         dbg(1,"enter\n");
2257         if (wm->text) {
2258                 len=strlen(wm->text);
2259                 dbg(1,"len=%d\n", len);
2260                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
2261                         wm->text[len-1]='\0';
2262                         gui_internal_cmd_add_bookmark_do(this, wm);
2263                 }
2264         }
2265 }
2266
2267
2268 static struct widget * gui_internal_keyboard(struct gui_priv *this, int mode);
2269
2270 static void
2271 gui_internal_cmd_add_bookmark2(struct gui_priv *this, struct widget *wm, void *data)
2272 {
2273         struct widget *w,*wb,*wk,*wl,*we,*wnext;
2274         char *name=data;
2275         wb=gui_internal_menu(this,_("Add Bookmark"));
2276         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2277         gui_internal_widget_append(wb, w);
2278         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2279         gui_internal_widget_append(w, we);
2280         gui_internal_widget_append(we, wk=gui_internal_label_new(this, name));
2281         wk->state |= STATE_EDIT|STATE_EDITABLE|STATE_CLEAR;
2282         wk->background=this->background;
2283         wk->flags |= flags_expand|flags_fill;
2284         wk->func = gui_internal_cmd_add_bookmark_changed;
2285         wk->c=wm->c;
2286         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
2287         wnext->state |= STATE_SENSITIVE;
2288         wnext->func = gui_internal_cmd_add_bookmark_clicked;
2289         wnext->data=wk;
2290         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2291         gui_internal_widget_append(w, wl);
2292         if (this->keyboard)
2293                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
2294         gui_internal_menu_render(this);
2295 }
2296
2297 static void
2298 gui_internal_cmd_add_bookmark_folder2(struct gui_priv *this, struct widget *wm, void *data)
2299 {
2300         struct widget *w,*wb,*wk,*wl,*we,*wnext;
2301         char *name=data;
2302         wb=gui_internal_menu(this,_("Add Bookmark folder"));
2303         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2304         gui_internal_widget_append(wb, w);
2305         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2306         gui_internal_widget_append(w, we);
2307         gui_internal_widget_append(we, wk=gui_internal_label_new(this, name));
2308         wk->state |= STATE_EDIT|STATE_EDITABLE|STATE_CLEAR;
2309         wk->background=this->background;
2310         wk->flags |= flags_expand|flags_fill;
2311         wk->func = gui_internal_cmd_add_bookmark_changed;
2312         wk->c=wm->c;
2313         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
2314         wnext->state |= STATE_SENSITIVE;
2315         wnext->func = gui_internal_cmd_add_bookmark_folder_clicked;
2316         wnext->data=wk;
2317         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2318         gui_internal_widget_append(w, wl);
2319         if (this->keyboard)
2320                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
2321         gui_internal_menu_render(this);
2322 }
2323
2324 static void
2325 gui_internal_cmd_rename_bookmark(struct gui_priv *this, struct widget *wm, void *data)
2326 {
2327         struct widget *w,*wb,*wk,*wl,*we,*wnext;
2328         char *name=wm->text;
2329         wb=gui_internal_menu(this,_("Rename"));
2330         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2331         gui_internal_widget_append(wb, w);
2332         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2333         gui_internal_widget_append(w, we);
2334         gui_internal_widget_append(we, wk=gui_internal_label_new(this, name));
2335         wk->state |= STATE_EDIT|STATE_EDITABLE|STATE_CLEAR;
2336         wk->background=this->background;
2337         wk->flags |= flags_expand|flags_fill;
2338         wk->func = gui_internal_cmd_add_bookmark_changed;
2339         wk->c=wm->c;
2340         wk->name=g_strdup(name);
2341         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
2342         wnext->state |= STATE_SENSITIVE;
2343         wnext->func = gui_internal_cmd_rename_bookmark_clicked;
2344         wnext->data=wk;
2345         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2346         gui_internal_widget_append(w, wl);
2347         if (this->keyboard)
2348                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
2349         gui_internal_menu_render(this);
2350 }
2351
2352 static void
2353 gui_internal_cmd_cut_bookmark(struct gui_priv *this, struct widget *wm, void *data)
2354 {
2355         struct attr mattr;
2356         GList *l;
2357         navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL);
2358         bookmarks_cut_bookmark(mattr.u.bookmarks,wm->text);
2359         l=g_list_previous(g_list_previous(g_list_last(this->root.children)));
2360         gui_internal_prune_menu(this, l->data);
2361 }
2362
2363 static void
2364 gui_internal_cmd_copy_bookmark(struct gui_priv *this, struct widget *wm, void *data)
2365 {
2366         struct attr mattr;
2367         GList *l;
2368         navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL);
2369         bookmarks_copy_bookmark(mattr.u.bookmarks,wm->text);
2370         l=g_list_previous(g_list_previous(g_list_last(this->root.children)));
2371         gui_internal_prune_menu(this, l->data);
2372 }
2373
2374 static void
2375 gui_internal_cmd_paste_bookmark(struct gui_priv *this, struct widget *wm, void *data)
2376 {
2377         struct attr mattr;
2378         GList *l;
2379         navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL);
2380         bookmarks_paste_bookmark(mattr.u.bookmarks);
2381         l=g_list_previous(g_list_last(this->root.children));
2382         gui_internal_prune_menu(this, l->data);
2383 }
2384
2385 static void
2386 gui_internal_cmd_delete_bookmark(struct gui_priv *this, struct widget *wm, void *data)
2387 {
2388         struct attr mattr;
2389         GList *l;
2390         navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL);
2391         bookmarks_delete_bookmark(mattr.u.bookmarks,wm->text);
2392         l=g_list_previous(g_list_previous(g_list_last(this->root.children)));
2393         gui_internal_prune_menu(this, l->data);
2394 }
2395
2396 static void
2397 gui_internal_cmd_delete_bookmark_folder(struct gui_priv *this, struct widget *wm, void *data)
2398 {
2399         struct attr mattr;
2400         GList *l;
2401         navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL);
2402         bookmarks_move_up(mattr.u.bookmarks);
2403         bookmarks_delete_bookmark(mattr.u.bookmarks,wm->prefix);
2404         l=g_list_first(this->root.children);
2405         gui_internal_prune_menu(this, l->data);
2406 }
2407
2408 static void
2409 gui_internal_cmd_load_bookmarks_as_waypoints(struct gui_priv *this, struct widget *wm, void *data)
2410 {
2411         struct attr mattr;
2412
2413         if(navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL) ) {
2414                 struct attr attr;
2415                 struct item *item;
2416                 struct coord c;
2417                 struct pcoord *pc;
2418                 enum projection pro=bookmarks_get_projection(mattr.u.bookmarks);
2419                 int i, bm_count;
2420
2421                 navit_set_destination(this->nav, NULL, NULL, 0);
2422
2423                 bm_count=bookmarks_get_bookmark_count(mattr.u.bookmarks);
2424                 pc=g_alloca(bm_count*sizeof(struct pcoord));
2425                 bookmarks_item_rewind(mattr.u.bookmarks);
2426                 i=0;
2427                 while ((item=bookmarks_get_item(mattr.u.bookmarks))) {
2428                         if (!item_attr_get(item, attr_label, &attr)) 
2429                                 continue;
2430                         if (item->type == type_bookmark) {
2431                                 if (item_coord_get(item, &c, 1)) {
2432                                         pc[i].x=c.x;
2433                                         pc[i].y=c.y;
2434                                         pc[i].pro=pro;
2435                                         i++;
2436                                 }
2437                         }
2438                 }
2439                 bm_count=i;
2440
2441                 if (bm_count>0){
2442                         navit_set_destinations(this->nav, pc, bm_count, wm->prefix, 1);
2443                         if (this->flags & 512) {
2444                                 struct attr follow;
2445                                 follow.type=attr_follow;
2446                                 follow.u.num=180;
2447                                 navit_set_attr(this->nav, &this->osd_configuration);
2448                                 navit_set_attr(this->nav, &follow);
2449                                 navit_zoom_to_route(this->nav, 0);
2450                         }
2451                 }
2452         }
2453
2454         gui_internal_prune_menu(this, NULL);
2455 }
2456
2457 static void
2458 gui_internal_cmd_replace_bookmarks_from_waypoints(struct gui_priv *this, struct widget *wm, void *data)
2459 {
2460         struct attr mattr;
2461
2462         if(navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL) ) {
2463                 struct attr attr;
2464                 char *desc=NULL;
2465                 struct pcoord *pc;
2466                 int i, bm_count;
2467
2468                 if (bookmarks_get_bookmark_count(mattr.u.bookmarks)>0){
2469                         struct item *item;
2470                         bookmarks_item_rewind(mattr.u.bookmarks);
2471
2472                         while ((item=bookmarks_get_item(mattr.u.bookmarks))) {
2473
2474                                 if (!item_attr_get(item, attr_label, &attr)) 
2475                                         continue;
2476
2477                                 if (item->type == type_bookmark) 
2478                                         bookmarks_delete_bookmark(mattr.u.bookmarks,  attr.u.str);
2479
2480                                 bookmarks_move_down(mattr.u.bookmarks,wm->prefix);
2481                         }
2482                 }
2483                 bookmarks_item_rewind(mattr.u.bookmarks);
2484
2485                 bm_count=navit_get_destination_count(this->nav);
2486                 pc=g_alloca(bm_count*sizeof(struct pcoord));
2487                 navit_get_destinations(this->nav, pc, bm_count);
2488
2489                 for (i=0; i<bm_count; i++){
2490                         desc=g_strdup_printf("%s WP%d", navit_get_destination_description(this->nav, i), i+1);
2491                         navit_get_attr(this->nav, attr_bookmarks, &attr, NULL);
2492                         bookmarks_add_bookmark(attr.u.bookmarks, &pc[i], desc);
2493                         bookmarks_move_down(mattr.u.bookmarks,wm->prefix);
2494                         g_free(desc);
2495                 }
2496         }
2497
2498         gui_internal_prune_menu(this, NULL);
2499 }
2500
2501 static void
2502 get_direction(char *buffer, int angle, int mode)
2503 {
2504         angle=angle%360;
2505         switch (mode) {
2506         case 0:
2507                 sprintf(buffer,"%d",angle);
2508                 break;
2509         case 1:
2510                 if (angle < 69 || angle > 291)
2511                         *buffer++='N';
2512                 if (angle > 111 && angle < 249)
2513                         *buffer++='S';
2514                 if (angle > 22 && angle < 158)
2515                         *buffer++='E';
2516                 if (angle > 202 && angle < 338)
2517                         *buffer++='W';
2518                 *buffer++='\0';
2519                 break;
2520         case 2:
2521                 angle=(angle+15)/30;
2522                 if (! angle)
2523                         angle=12;
2524                 sprintf(buffer,"%d H", angle);
2525                 break;
2526         }
2527 }
2528
2529 static void gui_internal_cmd_position(struct gui_priv *this, struct widget *wm, void *data);
2530
2531 struct selector {
2532         char *icon;
2533         char *name;
2534         enum item_type *types;
2535 };
2536 static enum item_type selectors_BankTypes[]={type_poi_bank,type_poi_bank, type_poi_atm,type_poi_atm, type_none};
2537 static enum item_type selectors_FuelTypes[]={type_poi_fuel,type_poi_fuel,type_none};
2538 static enum item_type selectors_HotelTypes[]={type_poi_hotel,type_poi_camp_rv,type_poi_camping,type_poi_camping,
2539     type_poi_resort,type_poi_resort,type_poi_motel,type_poi_hostel,type_none};
2540 static enum item_type selectors_RestaurantTypes[]={type_poi_bar,type_poi_picnic,type_poi_burgerking,type_poi_fastfood,
2541     type_poi_restaurant,type_poi_restaurant,type_poi_cafe,type_poi_cafe,type_poi_pub,type_poi_pub,type_none};
2542 static enum item_type selectors_ShoppingTypes[]={type_poi_mall,type_poi_mall,type_poi_shop_grocery,type_poi_shop_grocery,
2543     type_poi_shopping,type_poi_shopping,type_poi_shop_butcher,type_poi_shop_baker,type_poi_shop_fruit,
2544     type_poi_shop_fruit,type_poi_shop_beverages,type_poi_shop_beverages,type_none};
2545 static enum item_type selectors_ServiceTypes[]={type_poi_marina,type_poi_marina,type_poi_hospital,type_poi_hospital,
2546     type_poi_public_utilities,type_poi_public_utilities,type_poi_police,type_poi_autoservice,type_poi_information,
2547     type_poi_information,type_poi_pharmacy,type_poi_pharmacy,type_poi_personal_service,type_poi_repair_service,
2548     type_poi_restroom,type_poi_restroom,type_none};
2549 static enum item_type selectors_ParkingTypes[]={type_poi_car_parking,type_poi_car_parking,type_none};
2550 static enum item_type selectors_LandFeaturesTypes[]={type_poi_land_feature,type_poi_rock,type_poi_dam,type_poi_dam,
2551     type_poi_peak,type_poi_peak,type_none};
2552 static enum item_type selectors_OtherTypes[]={type_point_unspecified,type_poi_land_feature-1,type_poi_rock+1,type_poi_fuel-1,
2553     type_poi_marina+1,type_poi_shopping-1,type_poi_shopping+1,type_poi_car_parking-1,type_poi_car_parking+1,
2554     type_poi_bar-1,type_poi_bank+1,type_poi_dam-1,type_poi_dam+1,type_poi_information-1,type_poi_information+1,
2555     type_poi_mall-1,type_poi_mall+1,type_poi_personal_service-1,type_poi_pharmacy+1,type_poi_repair_service-1,
2556     type_poi_repair_service+1,type_poi_restaurant-1,type_poi_restaurant+1,type_poi_restroom-1,type_poi_restroom+1,
2557     type_poi_shop_grocery-1,type_poi_shop_grocery+1,type_poi_peak-1,type_poi_peak+1,type_poi_motel-1,type_poi_hostel+1,
2558     type_poi_shop_butcher-1,type_poi_shop_baker+1,type_poi_shop_fruit-1,type_poi_shop_fruit+1,type_poi_shop_beverages-1,
2559     type_poi_shop_beverages+1,type_poi_pub-1,type_poi_atm+1,type_line-1,type_none};
2560 /*static enum item_type selectors_UnknownTypes[]={type_point_unkn,type_point_unkn,type_none};*/
2561 struct selector selectors[]={
2562         {"bank","Bank",selectors_BankTypes},
2563         {"fuel","Fuel",selectors_FuelTypes},
2564         {"hotel","Hotel",selectors_HotelTypes},
2565         {"restaurant","Restaurant",selectors_RestaurantTypes},
2566         {"shopping","Shopping",selectors_ShoppingTypes},
2567         {"hospital","Service",selectors_ServiceTypes},
2568         {"parking","Parking",selectors_ParkingTypes},
2569         {"peak","Land Features",selectors_LandFeaturesTypes},
2570         {"unknown","Other",selectors_OtherTypes},
2571 /*      {"unknown","Unknown",selectors_UnknownTypes},*/
2572 };
2573
2574
2575 /*
2576  *  Get a utf-8 string, return the same prepared for case insensetive search. Result shoud be g_free()d after use.
2577  */
2578
2579 static char *
2580 removecase(char *s) 
2581 {
2582         char *r;
2583         r=g_utf8_casefold(s,-1);
2584         return r;
2585 }
2586
2587 /**
2588  * POI search/filtering parameters.
2589  *
2590  */
2591
2592 struct poi_param {
2593
2594                 /**
2595                  * =1 if selnb is defined, 0 otherwize.
2596                  */
2597                 unsigned char sel;
2598
2599                 /**
2600                  * Index to struct selector selectors[], shows what type of POIs is defined.
2601                  */             
2602                 unsigned char selnb;
2603                 /**
2604                  * Page number to display.
2605                  */             
2606                 unsigned char pagenb;
2607                 /**
2608                  * Radius (number of 10-kilometer intervals) to search for POIs.
2609                  */             
2610                 unsigned char dist;
2611                 /**
2612                  * Should filter phrase be compared to postal address of the POI.
2613                  * =1 - address filter, =0 - name filter
2614                  */             
2615                 unsigned char isAddressFilter;
2616                 /**
2617                  * Filter string, casefold()ed and divided into substrings at the spaces, which are replaced by ASCII 0*.
2618                  */             
2619                 char *filterstr; 
2620                 /**
2621                  * list of pointers to individual substrings of filterstr.
2622                  */             
2623                 GList *filter;
2624                 /**
2625                  * Number of POIs in this list
2626                  */
2627                 int count;
2628 };
2629
2630
2631 /**
2632  * @brief Free poi_param structure.
2633  *
2634  * @param p reference to the object to be freed.
2635  */
2636 static void
2637 gui_internal_poi_param_free(void *p) 
2638 {
2639         if(((struct poi_param *)p)->filterstr)
2640            g_free(((struct poi_param *)p)->filterstr);
2641         if(((struct poi_param *)p)->filter)
2642            g_list_free(((struct poi_param *)p)->filter);
2643         g_free(p);
2644 };
2645
2646 /**
2647  * @brief Clone poi_param structure.
2648  *
2649  * @param p reference to the object to be cloned.
2650  * @return  Cloned object reference.
2651  */
2652 static struct poi_param *
2653 gui_internal_poi_param_clone(struct poi_param *p) 
2654 {
2655         struct poi_param *r=g_new(struct poi_param,1);
2656         GList *l=p->filter;
2657         memcpy(r,p,sizeof(struct poi_param));
2658         r->filter=NULL;
2659         r->filterstr=NULL;
2660         if(p->filterstr) {
2661                 char *last=g_list_last(l)->data;
2662                 int len=(last - p->filterstr) + strlen(last)+1;
2663                 r->filterstr=g_memdup(p->filterstr,len);
2664         }
2665         while(l) {
2666                 r->filter=g_list_append(r->filter, r->filterstr + ((char*)(l->data) - p->filterstr) );
2667                 l=g_list_next(l);
2668         }
2669         return r;
2670 };
2671
2672 /**
2673  * @brief Set POIs filter data in poi_param structure.
2674  * @param param poi_param structure with unset filter data.
2675  * @param text filter text.
2676  */
2677 static void
2678 gui_internal_poi_param_set_filter(struct poi_param *param, char *text) 
2679 {
2680         char *s1, *s2;
2681         
2682         param->filterstr=removecase(text);
2683         s1=param->filterstr;
2684         do {
2685                 s2=g_utf8_strchr(s1,-1,' ');
2686                 if(s2)
2687                         *s2++=0;
2688                 param->filter=g_list_append(param->filter,s1);
2689                 if(s2) {
2690                         while(*s2==' ')
2691                                 s2++;
2692                 }
2693                 s1=s2;
2694         } while(s2 && *s2);
2695 }
2696
2697
2698 static void gui_internal_cmd_pois(struct gui_priv *this, struct widget *wm, void *data);
2699 static void gui_internal_cmd_pois_filter(struct gui_priv *this, struct widget *wm, void *data);
2700
2701
2702 static struct widget *
2703 gui_internal_cmd_pois_selector(struct gui_priv *this, struct pcoord *c, int pagenb)
2704 {
2705         struct widget *wl,*wb;
2706         int nitems,nrows;
2707         int i;
2708         //wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2709         wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal_vertical|flags_fill);
2710         wl->background=this->background;
2711         wl->w=this->root.w;
2712         wl->cols=this->root.w/this->icon_s;
2713         nitems=sizeof(selectors)/sizeof(struct selector);
2714         nrows=nitems/wl->cols + (nitems%wl->cols>0);
2715         wl->h=this->icon_l*nrows;
2716         for (i = 0 ; i < nitems ; i++) {
2717                 struct poi_param *p=g_new0(struct poi_param,1);
2718                 p->sel = 1;
2719                 p->selnb = i;
2720                 p->pagenb = pagenb;
2721                 p->dist = 0;
2722                 p->filter=NULL;
2723                 p->filterstr=NULL;
2724                 gui_internal_widget_append(wl, wb=gui_internal_button_new_with_callback(this, NULL,
2725                         image_new_s(this, selectors[i].icon), gravity_left_center|orientation_vertical,
2726                         gui_internal_cmd_pois, p));
2727                 wb->c=*c;
2728                 wb->data_free=gui_internal_poi_param_free;
2729                 wb->bt=10;
2730         }
2731
2732         gui_internal_widget_append(wl, wb=gui_internal_button_new_with_callback(this, NULL,
2733                         image_new_s(this, "gui_search"), gravity_left_center|orientation_vertical,
2734                         gui_internal_cmd_pois_filter, NULL));
2735         wb->c=*c;
2736         wb->bt=10;
2737         
2738         gui_internal_widget_pack(this,wl);
2739         return wl;
2740 }
2741
2742 /**
2743  * @brief Get icon for given POI type.
2744  *
2745  * @param this pointer to gui context
2746  * @param type POI type
2747  * @return  Pointer to graphics_image object, or NULL if no picture available. 
2748  */
2749 static struct graphics_image * gui_internal_poi_icon(struct gui_priv *this, enum item_type type)
2750 {
2751         struct attr layout;
2752         GList *layer;
2753         navit_get_attr(this->nav, attr_layout, &layout, NULL);
2754         layer=layout.u.layout->layers;
2755         while(layer) {
2756                 GList *itemgra=((struct layer *)layer->data)->itemgras;
2757                 while(itemgra) {
2758                         GList *types=((struct itemgra *)itemgra->data)->type;
2759                         while(types) {
2760                                 if((long)types->data==type) {
2761                                         GList *element=((struct itemgra *)itemgra->data)->elements;
2762                                         while(element) {
2763                                                 struct element * el=element->data;
2764                                                 if(el->type==element_icon) {
2765                                                         struct graphics_image *img;
2766                                                         char *icon=g_strdup(el->u.icon.src);
2767                                                         char *dot=g_strrstr(icon,".");
2768                                                         dbg(0,"%s %s\n", item_to_name(type),icon);
2769                                                         if(dot)
2770                                                                 *dot=0;
2771                                                         img=image_new_xs(this,icon);
2772                                                         g_free(icon);
2773                                                         if(img) 
2774                                                                 return img;
2775                                                 }
2776                                                 element=g_list_next(element);
2777                                         }
2778                                 }
2779                                 types=g_list_next(types);       
2780                         }
2781                         itemgra=g_list_next(itemgra);
2782                 }
2783                 layer=g_list_next(layer);
2784         }
2785         return NULL;
2786 }
2787
2788 /**
2789  * @brief Widget to display POI item.
2790  *
2791  * @param this pointer to gui context
2792  * @param center reference to the standing point where angle to be counted from
2793  * @param item POI item reference
2794  * @param c POI coordinates
2795  * @param dist precomputed distance to POI (or -1 if it's not to be displayed)
2796  * @param name POI name
2797  * @return  Pointer to new widget.
2798  */
2799 static struct widget *
2800 gui_internal_cmd_pois_item(struct gui_priv *this, struct coord *center, struct item *item, struct coord *c, int dist, char* name)
2801 {
2802         char distbuf[32]="";
2803         char dirbuf[32]="";
2804         char *type;
2805         struct widget *wl;
2806         char *text;
2807         struct graphics_image *icon;
2808
2809         if (dist > 10000)
2810                 sprintf(distbuf,"%d ", dist/1000);
2811         else if (dist>0)
2812                 sprintf(distbuf,"%d.%d ", dist/1000, (dist%1000)/100);
2813         if(c) {
2814                 int len;                
2815                 get_direction(dirbuf, transform_get_angle_delta(center, c, 0), 1);
2816                 len=strlen(dirbuf);
2817                 dirbuf[len]=' ';
2818                 dirbuf[len+1]=0;
2819         }
2820         
2821         type=item_to_name(item->type);
2822
2823         icon=gui_internal_poi_icon(this,item->type);
2824         if(!icon) {
2825                 icon=image_new_xs(this,"gui_inactive");
2826                 text=g_strdup_printf("%s%s%s %s", distbuf, dirbuf, type, name);
2827         } else if(strlen(name)>0)
2828                 text=g_strdup_printf("%s%s%s", distbuf, dirbuf, name);
2829         else 
2830                 text=g_strdup_printf("%s%s%s", distbuf, dirbuf, type);
2831                 
2832         wl=gui_internal_button_new_with_callback(this, text, icon, gravity_left_center|orientation_horizontal|flags_fill, NULL, NULL);
2833         wl->datai=dist;
2834         g_free(text);
2835         if (name[0]) {
2836                 wl->name=g_strdup_printf("%s %s",type,name);
2837         } else {
2838                 wl->name=g_strdup(type);
2839         }
2840         wl->func=gui_internal_cmd_position;
2841         wl->data=(void *)9;
2842         wl->item=*item;
2843         wl->state|= STATE_SENSITIVE;
2844         return wl;
2845 }
2846
2847 /**
2848  * @brief Get string representation of item address suitable for doing search and for display in POI list.
2849  *
2850  * @param item reference to item.
2851  * @return  Pointer to string representation of address. To be g_free()d after use.
2852  */
2853 char *
2854 gui_internal_compose_item_address_string(struct item *item)
2855 {
2856         char *s=g_strdup("");
2857         struct attr attr;
2858         if(item_attr_get(item, attr_house_number, &attr)) 
2859                 s=g_strjoin(" ",s,attr.u.str,NULL);
2860         if(item_attr_get(item, attr_street_name, &attr)) 
2861                 s=g_strjoin(" ",s,attr.u.str,NULL);
2862         if(item_attr_get(item, attr_street_name_systematic, &attr)) 
2863                 s=g_strjoin(" ",s,attr.u.str,NULL);
2864         if(item_attr_get(item, attr_district_name, &attr)) 
2865                 s=g_strjoin(" ",s,attr.u.str,NULL);
2866         if(item_attr_get(item, attr_town_name, &attr)) 
2867                 s=g_strjoin(" ",s,attr.u.str,NULL);
2868         if(item_attr_get(item, attr_county_name, &attr)) 
2869                 s=g_strjoin(" ",s,attr.u.str,NULL);
2870         if(item_attr_get(item, attr_country_name, &attr)) 
2871                 s=g_strjoin(" ",s,attr.u.str,NULL);
2872         
2873         if(item_attr_get(item, attr_address, &attr)) 
2874                 s=g_strjoin(" ",s,"|",attr.u.str,NULL);
2875         return s;
2876 }
2877
2878 static int
2879 gui_internal_cmd_pois_item_selected(struct poi_param *param, struct item *item)
2880 {
2881         enum item_type *types;
2882         struct selector *sel = param->sel? &selectors[param->selnb]: NULL;
2883         enum item_type type=item->type;
2884         struct attr attr;
2885         int match=0;
2886         if (type >= type_line && param->filter==NULL)
2887                 return 0;
2888         if (! sel || !sel->types) {
2889                 match=1;
2890         } else {
2891                 types=sel->types;
2892                 while (*types != type_none) {
2893                         if (item->type >= types[0] && item->type <= types[1]) {
2894                                 return 1;
2895                         }
2896                         types+=2;
2897                 }
2898         }
2899         if (param->filter) {
2900                 char *long_name, *s;
2901                 GList *f;
2902                 if (param->isAddressFilter) {
2903                         s=gui_internal_compose_item_address_string(item);
2904                 } else if (item_attr_get(item, attr_label, &attr)) {
2905                         s=g_strdup_printf("%s %s", item_to_name(item->type), attr.u.str);
2906                 } else {
2907                         s=g_strdup(item_to_name(item->type));
2908                 }
2909                 long_name=removecase(s);
2910                 g_free(s);
2911                 item_attr_rewind(item);
2912                 
2913                 for(s=long_name,f=param->filter;f && s;f=g_list_next(f)) {
2914                         s=strstr(s,f->data);
2915                         if(!s) 
2916                                 break;
2917                         s=g_utf8_strchr(s,-1,' ');
2918                 }
2919                 if(f)
2920                         match=0;
2921                 g_free(long_name);
2922         }
2923         return match;
2924 }
2925
2926 struct item_data {
2927         int dist;
2928         char *label;
2929         struct item item;
2930         struct coord c;
2931 };
2932
2933 /**
2934  * @brief Event handler for POIs list "more" element.
2935  *
2936  * @param this The graphics context.
2937  * @param wm called widget.
2938  * @param data event data.
2939  */
2940 static void
2941 gui_internal_cmd_pois_more(struct gui_priv *this, struct widget *wm, void *data) 
2942 {
2943         struct widget *w=g_new0(struct widget,1);
2944         w->data=wm->data;
2945         w->c=wm->c;
2946         w->w=wm->w;
2947         wm->data_free=NULL;
2948         gui_internal_back(this, NULL, NULL);
2949         gui_internal_cmd_pois(this, w, w->data);
2950         free(w);
2951 }
2952
2953
2954 /**
2955  * @brief Event to apply POIs text filter.
2956  *
2957  * @param this The graphics context.
2958  * @param wm called widget.
2959  * @param data event data (pointer to editor widget containg filter text).
2960  */
2961 static void
2962 gui_internal_cmd_pois_filter_do(struct gui_priv *this, struct widget *wm, void *data) 
2963 {
2964         struct widget *w=data;
2965         struct poi_param *param;
2966         
2967         if(!w->text)
2968                 return;
2969         
2970         if(w->data) {
2971                 param=gui_internal_poi_param_clone(w->data);
2972                 param->pagenb=0;
2973         } else {
2974                 param=g_new0(struct poi_param,1);
2975         }
2976         param->isAddressFilter=strcmp(wm->name,"AddressFilter")==0;
2977
2978         gui_internal_poi_param_set_filter(param, w->text);
2979
2980         gui_internal_cmd_pois(this,w,param);
2981         gui_internal_poi_param_free(param);
2982 }
2983
2984 /**
2985  * @brief POIs filter dialog.
2986  * Event to handle '\r' '\n' keys pressed.
2987  * 
2988  */
2989
2990 static void
2991 gui_internal_cmd_pois_filter_changed(struct gui_priv *this, struct widget *wm, void *data)
2992 {
2993         int len;
2994         if (wm->text) {
2995                 len=strlen(wm->text);
2996                 dbg(1,"len=%d\n", len);
2997                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
2998                         wm->text[len-1]='\0';
2999                         //gui_internal_cmd_pois_filter_do(this, wm, wm); // Doesnt clean filter editor from the screen. How to disable its redrawal after POI list is drawn?
3000                 }
3001         }
3002 }
3003
3004
3005 /**
3006  * @brief POIs filter dialog.
3007  *
3008  * @param this The graphics context.
3009  * @param wm called widget.
3010  * @param data event data.
3011  */
3012 static void
3013 gui_internal_cmd_pois_filter(struct gui_priv *this, struct widget *wm, void *data) 
3014 {
3015         struct widget *wb, *w, *wr, *wk, *we;
3016         int keyboard_mode=2;
3017         wb=gui_internal_menu(this,"Filter");
3018         w=gui_internal_box_new(this, gravity_center|orientation_vertical|flags_expand|flags_fill);
3019         gui_internal_widget_append(wb, w);
3020         wr=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3021         gui_internal_widget_append(w, wr);
3022         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
3023         gui_internal_widget_append(wr, we);
3024
3025         gui_internal_widget_append(we, wk=gui_internal_label_new(this, NULL));
3026         wk->state |= STATE_EDIT|STATE_EDITABLE;
3027         wk->func=gui_internal_cmd_pois_filter_changed;
3028         wk->background=this->background;
3029         wk->flags |= flags_expand|flags_fill;
3030         wk->name=g_strdup("POIsFilter");
3031         wk->c=wm->c;
3032         gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
3033         wb->state |= STATE_SENSITIVE;
3034         wb->func = gui_internal_cmd_pois_filter_do;
3035         wb->name=g_strdup("NameFilter");
3036         wb->data=wk;
3037         gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "post")));
3038         wb->state |= STATE_SENSITIVE;
3039         wb->name=g_strdup("AddressFilter");
3040         wb->func = gui_internal_cmd_pois_filter_do;
3041         wb->data=wk;
3042         
3043         if (this->keyboard)
3044                 gui_internal_widget_append(w, gui_internal_keyboard(this,keyboard_mode));
3045         gui_internal_menu_render(this);
3046
3047
3048 }
3049
3050 /**
3051  * @brief Do POI search specified by poi_param and display POIs found
3052  *
3053  * @param this The graphics context.
3054  * @param wm called widget.
3055  * @param data event data, reference to poi_param or NULL.
3056  */
3057 static void
3058 gui_internal_cmd_pois(struct gui_priv *this, struct widget *wm, void *data)
3059 {
3060         struct map_selection *sel,*selm;
3061         struct coord c,center;
3062         struct mapset_handle *h;
3063         struct map *m;
3064         struct map_rect *mr;
3065         struct item *item;
3066         struct widget *wi,*w,*w2,*wb, *wtable, *row;
3067         enum projection pro=wm->c.pro;
3068         struct poi_param *param;
3069         int param_free=0;
3070         int idist,dist;
3071         struct selector *isel;
3072         int pagenb;
3073         int prevdist;
3074         // Starting value and increment of count of items to be extracted
3075         const int pagesize = 50; 
3076         int maxitem, it = 0, i;
3077         struct item_data *items;
3078         struct fibheap* fh = fh_makekeyheap();
3079         int cnt = 0;
3080         struct table_data *td;
3081         struct widget *wl,*wt;
3082         char buffer[32];
3083         struct poi_param *paramnew;
3084
3085         if(data) {
3086           param = data;
3087         } else {
3088           param = g_new0(struct poi_param,1);
3089           param_free=1;
3090         }
3091         
3092         dist=10000*(param->dist+1);
3093         isel = param->sel? &selectors[param->selnb]: NULL;
3094         pagenb = param->pagenb;
3095         prevdist=param->dist*10000;
3096         maxitem = pagesize*(pagenb+1);
3097         items= g_new0( struct item_data, maxitem);
3098         
3099         
3100         dbg(0, "Params: sel = %i, selnb = %i, pagenb = %i, dist = %i, filterstr = %s, isAddressFilter= %d\n",
3101                 param->sel, param->selnb, param->pagenb, param->dist, param->filterstr, param->isAddressFilter);
3102
3103         wb=gui_internal_menu(this, isel ? isel->name : _("POIs"));
3104         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3105         gui_internal_widget_append(wb, w);
3106         if (!isel && !param->filter)
3107                 gui_internal_widget_append(w, gui_internal_cmd_pois_selector(this,&wm->c,pagenb));
3108         w2=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3109         gui_internal_widget_append(w, w2);
3110
3111         sel=map_selection_rect_new(&wm->c,dist*transform_scale(abs(wm->c.y)+dist*1.5),18);
3112         center.x=wm->c.x;
3113         center.y=wm->c.y;
3114         h=mapset_open(navit_get_mapset(this->nav));
3115         while ((m=mapset_next(h, 1))) {
3116                 selm=map_selection_dup_pro(sel, pro, map_projection(m));
3117                 mr=map_rect_new(m, selm);
3118                 dbg(2,"mr=%p\n", mr);
3119                 if (mr) {
3120                         while ((item=map_rect_get_item(mr))) {
3121                                 if (gui_internal_cmd_pois_item_selected(param, item) &&
3122                                     item_coord_get_pro(item, &c, 1, pro) &&
3123                                     coord_rect_contains(&sel->u.c_rect, &c)  &&
3124                                     (idist=transform_distance(pro, &center, &c)) < dist) {
3125                                         struct item_data *data;
3126                                         struct attr attr;
3127                                         char *label;
3128                                         
3129                                         if (item->type==type_house_number) {
3130                                                 label=gui_internal_compose_item_address_string(item);
3131                                         } else if (item_attr_get(item, attr_label, &attr)) {
3132                                                 label=g_strdup(attr.u.str);
3133                                                 // Buildings which label is equal to addr:housenumber value
3134                                                 // are duplicated by item_house_number. Don't include such 
3135                                                 // buildings into the list. This is true for OSM maps created with 
3136                                                 // maptool patched with #859 latest patch.
3137                                                 // FIXME: For non-OSM maps, we probably would better don't skip these items.
3138                                                 if(item->type==type_poly_building && item_attr_get(item, attr_house_number, &attr) ) {
3139                                                         if(strcmp(label,attr.u.str)==0) {
3140                                                                 g_free(label);
3141                                                                 continue;
3142                                                         }
3143                                                 }
3144
3145                                         } else {
3146                                                 label=g_strdup("");
3147                                         }
3148                                         
3149                                         if(it>=maxitem) {
3150                                                 data = fh_extractmin(fh);
3151                                                 g_free(data->label);
3152                                                 data->label=NULL;
3153                                         } else {
3154                                                 data = &items[it++];
3155                                         }
3156                                         data->label=label;
3157                                         data->item = *item;
3158                                         data->c = c;
3159                                         data->dist = idist;
3160                                         // Key expression is a workaround to fight
3161                                         // probable heap collisions when two objects
3162                                         // are at the same distance. But it destroys
3163                                         // right order of POIs 2048 km away from cener
3164                                         // and if table grows more than 1024 rows.
3165                                         fh_insertkey(fh, -((idist<<10) + cnt++), data);
3166                                         if (it == maxitem)
3167                                                 dist = (-fh_minkey(fh))>>10;
3168                                 }
3169                         }
3170                         map_rect_destroy(mr);
3171                 }
3172                 map_selection_destroy(selm);
3173         }
3174         map_selection_destroy(sel);
3175         mapset_close(h);
3176         
3177         wtable = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
3178         td=wtable->data;
3179
3180         gui_internal_widget_append(w2,wtable);
3181
3182         // Move items from heap to the table
3183         for(i=0;;i++) 
3184         {
3185                 int key = fh_minkey(fh);
3186                 struct item_data *data = fh_extractmin(fh);
3187                 if (data == NULL)
3188                 {
3189                         dbg(2, "Empty heap: maxitem = %i, it = %i, dist = %i\n", maxitem, it, dist);
3190                         break;
3191                 }
3192                 dbg(2, "dist1: %i, dist2: %i\n", data->dist, (-key)>>10);
3193                 if(i==(it-pagesize*pagenb) && data->dist>prevdist)
3194                         prevdist=data->dist;
3195                 wi=gui_internal_cmd_pois_item(this, &center, &data->item, &data->c, data->dist, data->label);
3196                 wi->c.x=data->c.x;
3197                 wi->c.y=data->c.y;
3198                 wi->c.pro=pro;
3199                 wi->background=this->background;
3200                 row = gui_internal_widget_table_row_new(this,
3201                                                           gravity_left
3202                                                           | flags_fill
3203                                                           | orientation_horizontal);
3204                 gui_internal_widget_append(row,wi);
3205                 row->datai=data->dist;
3206                 gui_internal_widget_prepend(wtable,row);
3207                 free(data->label);
3208         }
3209
3210         fh_deleteheap(fh);
3211         free(items);
3212
3213         // Add an entry for more POI
3214         row = gui_internal_widget_table_row_new(this,
3215                                                   gravity_left
3216                                                   | flags_fill
3217                                                   | orientation_horizontal);
3218         row->datai=100000000; // Really far away for Earth, but won't work for bigger planets.
3219         gui_internal_widget_append(wtable,row);
3220         wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
3221         gui_internal_widget_append(row,wl);
3222         if (it == maxitem) {
3223                 paramnew=gui_internal_poi_param_clone(param);
3224                 paramnew->pagenb++;
3225                 paramnew->count=it;
3226                 snprintf(buffer, sizeof(buffer), "Get more (up to %d items)...", (paramnew->pagenb+1)*pagesize);
3227                 wt=gui_internal_label_new(this, buffer);
3228                 gui_internal_widget_append(wl, wt);
3229                 wt->func=gui_internal_cmd_pois_more;
3230                 wt->data=paramnew;
3231                 wt->data_free=gui_internal_poi_param_free;
3232                 wt->state |= STATE_SENSITIVE;
3233                 wt->c = wm->c;
3234         } else {
3235                 static int dist[]={1,5,10,0};
3236                 wt=gui_internal_label_new(this, "Set distance to");
3237                 gui_internal_widget_append(wl, wt);
3238                 for(i=0;dist[i];i++) {
3239                         paramnew=gui_internal_poi_param_clone(param);
3240                         paramnew->dist+=dist[i];
3241                         paramnew->count=it;
3242                         snprintf(buffer, sizeof(buffer), " %i ", 10*(paramnew->dist+1));
3243                         wt=gui_internal_label_new(this, buffer);
3244                         gui_internal_widget_append(wl, wt);
3245                         wt->func=gui_internal_cmd_pois_more;
3246                         wt->data=paramnew;
3247                         wt->data_free=gui_internal_poi_param_free;
3248                         wt->state |= STATE_SENSITIVE;
3249                         wt->c = wm->c;
3250                 }
3251                 wt=gui_internal_label_new(this, "km.");
3252                 gui_internal_widget_append(wl, wt);
3253
3254         }
3255         // Rendering now is needed to have table_data->bottomrow filled in.
3256         gui_internal_menu_render(this);
3257         td=wtable->data;
3258         if(td->bottom_row!=NULL)
3259         {
3260 #if 0
3261                 while(((struct widget*)td->bottom_row->data)->datai<=prevdist
3262                                 && (td->next_button->state & STATE_SENSITIVE))
3263                 {
3264                         gui_internal_table_button_next(this, td->next_button, NULL);
3265                 }
3266 #else
3267                 int firstrow=g_list_index(wtable->children, td->top_row->data);
3268                 while(firstrow>=0) {
3269                         int currow=g_list_index(wtable->children, td->bottom_row->data) - firstrow;
3270                         if(currow<0) {
3271                                 dbg(0,"Can't find bottom row in children list. Stop paging.\n");
3272                                 break;
3273                         }
3274                         if(currow>=param->count)
3275                                 break;
3276                         if(!(td->next_button->state & STATE_SENSITIVE)) {
3277                                 dbg(0,"Reached last page but item %i not found. Stop paging.\n",param->count);
3278                                 break;
3279                         }
3280                         gui_internal_table_button_next(this, td->next_button, NULL);
3281                 }
3282 #endif
3283         }
3284         gui_internal_menu_render(this);
3285         if(param_free)
3286                 g_free(param);
3287 }
3288
3289 static void
3290 gui_internal_cmd_view_on_map(struct gui_priv *this, struct widget *wm, void *data)
3291 {
3292         if (wm->item.type != type_none) {
3293                 enum item_type type;
3294                 if (wm->item.type < type_line)
3295                         type=type_selected_point;
3296                 else if (wm->item.type < type_area)
3297                         type=type_selected_point;
3298                 else
3299                         type=type_selected_area;
3300                 graphics_clear_selection(this->gra, NULL);
3301                 graphics_add_selection(this->gra, &wm->item, type, NULL);
3302         }
3303         navit_set_center(this->nav, &wm->c, 1);
3304         gui_internal_prune_menu(this, NULL);
3305 }
3306
3307
3308 static void
3309 gui_internal_cmd_view_attribute_details(struct gui_priv *this, struct widget *wm, void *data)
3310 {
3311         struct widget *w,*wb;
3312         struct map_rect *mr;
3313         struct item *item;
3314         struct attr attr;
3315         char *text,*url;
3316         int i;
3317
3318         text=g_strdup_printf("Attribute %s",wm->name);
3319         wb=gui_internal_menu(this, text);
3320         g_free(text);
3321         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3322         gui_internal_widget_append(wb, w);
3323         mr=map_rect_new(wm->item.map, NULL);
3324         item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
3325         for (i = 0 ; i < wm->datai ; i++) {
3326                 item_attr_get(item, attr_any, &attr);
3327         }
3328         if (item_attr_get(item, attr_any, &attr)) {
3329                 url=NULL;
3330                 switch (attr.type) {
3331                 case attr_osm_nodeid:
3332                         url=g_strdup_printf("http://www.openstreetmap.org/browse/node/"LONGLONG_FMT"\n",*attr.u.num64);
3333                         break;
3334                 case attr_osm_wayid:
3335                         url=g_strdup_printf("http://www.openstreetmap.org/browse/way/"LONGLONG_FMT"\n",*attr.u.num64);
3336                         break;
3337                 case attr_osm_relationid:
3338                         url=g_strdup_printf("http://www.openstreetmap.org/browse/relation/"LONGLONG_FMT"\n",*attr.u.num64);
3339                         break;
3340                 default:
3341                         break;
3342                 }
3343                 if (url) {
3344                         gui_internal_widget_append(w,
3345                                         wb=gui_internal_button_new_with_callback(this, _("View in Browser"),
3346                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3347                                                 gui_internal_cmd_view_in_browser, NULL));
3348                         wb->name=url;
3349                 }
3350         }
3351         map_rect_destroy(mr);
3352         gui_internal_menu_render(this);
3353 }
3354
3355 static void
3356 gui_internal_cmd_view_attributes(struct gui_priv *this, struct widget *wm, void *data)
3357 {
3358         struct widget *w,*wb;
3359         struct map_rect *mr;
3360         struct item *item;
3361         struct attr attr;
3362         char *text;
3363         int count=0;
3364
3365         dbg(0,"item=%p 0x%x 0x%x\n", wm->item.map,wm->item.id_hi, wm->item.id_lo);
3366         wb=gui_internal_menu(this, "Attributes");
3367         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3368         gui_internal_widget_append(wb, w);
3369         mr=map_rect_new(wm->item.map, NULL);
3370         item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
3371         dbg(0,"item=%p\n", item);
3372         if (item) {
3373                 text=g_strdup_printf("%s:%s", _("Item type"), item_to_name(item->type));
3374                 gui_internal_widget_append(w,
3375                 wb=gui_internal_button_new(this, text,
3376                         NULL, gravity_left_center|orientation_horizontal|flags_fill));
3377                 wb->name=g_strdup(text);
3378                 wb->item=wm->item;
3379                 g_free(text);
3380                 while(item_attr_get(item, attr_any, &attr)) {
3381                         char *attrtxt;
3382                         text=g_strdup_printf("%s:%s", attr_to_name(attr.type), attrtxt=attr_to_text(&attr, wm->item.map, 1));
3383                         g_free(attrtxt);
3384                         gui_internal_widget_append(w,
3385                         wb=gui_internal_button_new_with_callback(this, text,
3386                                 NULL, gravity_left_center|orientation_horizontal|flags_fill,
3387                                 gui_internal_cmd_view_attribute_details, NULL));
3388                         wb->name=g_strdup(text);
3389                         wb->item=wm->item;
3390                         wb->datai=count++;
3391                         g_free(text);
3392                 }
3393         }
3394         map_rect_destroy(mr);
3395         gui_internal_menu_render(this);
3396 }
3397
3398 static void
3399 gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data)
3400 {
3401         struct map_rect *mr;
3402         struct item *item;
3403         struct attr attr;
3404         char *cmd=NULL;
3405
3406         if (!wm->name) {
3407                 dbg(0,"item=%p 0x%x 0x%x\n", wm->item.map,wm->item.id_hi, wm->item.id_lo);
3408                 mr=map_rect_new(wm->item.map, NULL);
3409                 item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
3410                 dbg(0,"item=%p\n", item);
3411                 if (item) {
3412                         while(item_attr_get(item, attr_url_local, &attr)) {
3413                                 if (! cmd)
3414                                         cmd=g_strdup_printf("navit-browser.sh '%s' &",attr.u.str);
3415                         }
3416                 }
3417                 map_rect_destroy(mr);
3418         } else {
3419                 cmd=g_strdup_printf("navit-browser.sh '%s' &",wm->name);
3420         }
3421         if (cmd) {
3422 #ifdef HAVE_SYSTEM
3423                 system(cmd);
3424 #else
3425                 dbg(0,"calling external cmd '%s' is not supported\n",cmd);
3426 #endif
3427                 g_free(cmd);
3428         }
3429 }
3430
3431
3432 /*
3433  * @brief Event to transfer search results to a map.
3434  *
3435  * @param this The graphics context.
3436  * @param wm called widget.
3437  * @param data event data (pointer to the table widget containing results, or NULL if results map is only have to be cleaned).
3438  */
3439 static void
3440 gui_internal_cmd_results_to_map(struct gui_priv *this, struct widget *wm, void *data)
3441 {
3442         struct widget *w;
3443         struct mapset *ms;      
3444         struct map *map;
3445         struct map_selection sel;
3446         struct map_rect *mr;
3447         struct item *item;
3448         GList *l;
3449         struct coord_rect r;
3450         struct attr a;
3451         int count;
3452         
3453         ms=navit_get_mapset(this->nav);
3454
3455         if(!ms)
3456                 return;
3457
3458         map=mapset_get_map_by_name(ms, "search_results");
3459         if(!map) {
3460                 struct attr *attrs[10], attrmap;
3461                 enum attr_type types[]={attr_position_longitude,attr_position_latitude,attr_label,attr_none};
3462                 int i;
3463                 
3464                 attrs[0]=g_new0(struct attr,1);
3465                 attrs[0]->type=attr_type;
3466                 attrs[0]->u.str="csv";
3467
3468                 attrs[1]=g_new0(struct attr,1);
3469                 attrs[1]->type=attr_name;
3470                 attrs[1]->u.str="search_results";
3471
3472                 attrs[2]=g_new0(struct attr,1);
3473                 attrs[2]->type=attr_charset;
3474                 attrs[2]->u.str="utf-8";
3475                 
3476                 attrs[3]=g_new0(struct attr,1);
3477                 attrs[3]->type=attr_item_type;
3478                 attrs[3]->u.num=type_found_item;
3479
3480                 attrs[4]=g_new0(struct attr,1);
3481                 attrs[4]->type=attr_attr_types;
3482                 attrs[4]->u.attr_types=types;
3483                 attrs[5]=NULL;
3484                 
3485                 attrmap.type=attr_map;
3486                 map=attrmap.u.map=map_new(NULL,attrs);
3487                 if(map)
3488                         mapset_add_attr(ms,&attrmap);
3489
3490                 for(i=0;attrs[i];i++)
3491                         g_free(attrs[i]);
3492
3493         }
3494
3495         if(!map)
3496                 return;
3497
3498         sel.next=NULL;
3499         sel.order=18;
3500         sel.range.min=type_none;
3501         sel.range.max=type_tec_common;
3502         
3503
3504         mr = map_rect_new(map, NULL);
3505
3506         if(!mr)
3507                 return;
3508                 
3509         /* Clean the map */
3510         while((item = map_rect_get_item(mr))!=NULL) {
3511                 item_type_set(item,type_none);
3512         }
3513
3514         this->results_map_population=0;
3515          
3516         /* Find the table to pupulate the map */
3517         for(w=data; w && w->type!=widget_table;w=w->parent);
3518         
3519         if(!w) {
3520                 map_rect_destroy(mr);
3521                 dbg(1,"Can't find the results table - only map clean up is done.\n");
3522                 return;
3523         }
3524
3525         /* Populate the map with search results*/
3526         for(l=w->children, count=0;l;l=g_list_next(l)) {
3527                 struct widget *wr=l->data;
3528                 if(wr->type==widget_table_row) {
3529                         struct widget *wi=wr->children->data;
3530                         struct item* it;
3531                         if(wi->name==NULL)
3532                                 continue;
3533                         dbg(0,"%s\n",wi->name);
3534                         it=map_rect_create_item(mr,type_found_item);
3535                         if(it) {
3536                                 struct coord c;
3537                                 struct attr a;
3538                                 c.x=wi->c.x;
3539                                 c.y=wi->c.y;
3540                                 item_coord_set(it, &c, 1, change_mode_modify);
3541                                 a.type=attr_label;
3542                                 a.u.str=wi->name;
3543                                 item_attr_set(it, &a, change_mode_modify);
3544                                 if(!count++)
3545                                         r.lu=r.rl=c;
3546                                 else
3547                                         coord_rect_extend(&r,&c);
3548                         }
3549                 }
3550         }
3551         map_rect_destroy(mr);
3552         if(!count)
3553                 return;
3554         a.type=attr_orientation;
3555         a.u.num=0;
3556         navit_set_attr(this->nav,&a);
3557         navit_zoom_to_rect(this->nav,&r);
3558         gui_internal_prune_menu(this, NULL);
3559         this->results_map_population=count;
3560 }
3561
3562 /*
3563  * @brief Event to remove search results from the a map.
3564  *
3565  * @param this The graphics context.
3566  * @param wm called widget.
3567  * @param data event data
3568  */
3569 static void
3570 gui_internal_cmd_results_map_clean(struct gui_priv *this, struct widget *wm, void *data)
3571 {
3572         gui_internal_cmd_results_to_map(this,wm,NULL);
3573         gui_internal_prune_menu(this, NULL);
3574         navit_draw(this->nav);
3575 }
3576
3577
3578 /* meaning of the bits in "flags":
3579  * 1: "Streets"
3580  * 2: "House numbers"
3581  * 4: "View in Browser", "View Attributes"
3582  * 8: "Set as dest."
3583  * 16: "Set as pos."
3584  * 32: "Add as bookm."
3585  * 64: "POIs"
3586  * 128: "View on Map"
3587  * 256: POIs around this point, "Drop search results from the map"
3588  * 512: "Cut/Copy... bookmark"
3589  * 1024: "Jump to attributes of top item within this->radius pixels of this point (implies flags|=256)"
3590  * 2048: "Show search results on the map"
3591  * TODO define constants for these values
3592  */
3593 static void
3594 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)
3595 {
3596         struct widget *wb,*w,*wtable,*row,*wc,*wbc,*wclosest=NULL;
3597         struct coord_geo g;
3598         struct pcoord pc;
3599         struct coord c;
3600         char *coord;
3601
3602         if (pc_in) {
3603                 pc=*pc_in;
3604                 c.x=pc.x;
3605                 c.y=pc.y;
3606                 dbg(0,"x=0x%x y=0x%x\n", c.x, c.y);
3607                 transform_to_geo(pc.pro, &c, &g);
3608         } else if (g_in) {
3609                 struct attr attr;
3610                 if (!navit_get_attr(this->nav, attr_projection, &attr, NULL))
3611                         return;
3612                 g=*g_in;
3613                 pc.pro=attr.u.projection;
3614                 transform_from_geo(pc.pro, &g, &c);
3615                 pc.x=c.x;
3616                 pc.y=c.y;
3617         } else
3618                 return;
3619
3620         wb=gui_internal_menu(this, name);
3621         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3622         gui_internal_widget_append(wb, w);
3623         coord=coordinates(&pc, ' ');
3624         gui_internal_widget_append(w, gui_internal_label_new(this, coord));
3625         g_free(coord);
3626         wtable = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
3627         gui_internal_widget_append(w,wtable);
3628         
3629         if ((flags & 1) && wm) {
3630                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3631                 gui_internal_widget_append(row,
3632                         wc=gui_internal_button_new_with_callback(this, _("Streets"),
3633                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3634                                 gui_internal_search_street_in_town, wm));
3635                 wc->item=wm->item;
3636                 wc->selection_id=wm->selection_id;
3637         }
3638         if ((flags & 2) && wm) {
3639                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3640                 gui_internal_widget_append(row,
3641                         wc=gui_internal_button_new_with_callback(this, _("House numbers"),
3642                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3643                                 gui_internal_search_house_number_in_street, wm));
3644                 wc->item=wm->item;
3645                 wc->selection_id=wm->selection_id;
3646         }
3647         if ((flags & 4) && wm) {
3648                 struct map_rect *mr;
3649                 struct item *item;
3650                 struct attr attr;
3651                 mr=map_rect_new(wm->item.map, NULL);
3652                 item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
3653                 if (item) {
3654                         if (item_attr_get(item, attr_description, &attr))
3655                                 gui_internal_widget_append(w, gui_internal_label_new(this, attr.u.str));
3656                         if (item_attr_get(item, attr_url_local, &attr)) {
3657                                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3658                                 gui_internal_widget_append(row,
3659                                         wb=gui_internal_button_new_with_callback(this, _("View in Browser"),
3660                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3661                                                 gui_internal_cmd_view_in_browser, NULL));
3662                                 wb->item=wm->item;
3663                         }
3664                         gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3665                         gui_internal_widget_append(row,
3666                                 wb=gui_internal_button_new_with_callback(this, _("View Attributes"),
3667                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3668                                         gui_internal_cmd_view_attributes, NULL));
3669                         wb->item=wm->item;
3670                 }
3671                 map_rect_destroy(mr);
3672         }
3673         if (flags & 8) {
3674                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3675                 gui_internal_widget_append(row,
3676                         wbc=gui_internal_button_new_with_callback(this, _("Set as destination"),
3677                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3678                                 gui_internal_cmd_set_destination, g_strdup(name)));
3679                 wbc->data_free=g_free_func;
3680                 wbc->c=pc;
3681         }
3682         if (flags & 16) {
3683                 const char *text;
3684                 struct attr vehicle, source;
3685                 int deactivate=0;
3686                 if (navit_get_attr(this->nav, attr_vehicle, &vehicle, NULL) && vehicle.u.vehicle && 
3687                                 !(vehicle_get_attr(vehicle.u.vehicle, attr_source, &source, NULL) && source.u.str && !strcmp("demo://",source.u.str))) 
3688                         deactivate=1;
3689
3690                 text=deactivate? _("Set as position (and deactivate vehicle)") : _("Set as position");
3691
3692                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3693                 gui_internal_widget_append(row,
3694                         wbc=gui_internal_button_new_with_callback(this, text,
3695                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3696                         gui_internal_cmd_set_position, (void*)deactivate));
3697                 wbc->c=pc;
3698         }
3699         if (flags & 32) {
3700                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3701                 gui_internal_widget_append(row,
3702                         wbc=gui_internal_button_new_with_callback(this, _("Add as bookmark"),
3703                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3704                         gui_internal_cmd_add_bookmark2, g_strdup(name)));
3705                 wbc->data_free=g_free_func;
3706                 wbc->c=pc;
3707         }
3708 #ifndef _MSC_VER
3709 //POIs are not operational under MSVC yet
3710         if (flags & 64) {
3711                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3712                 gui_internal_widget_append(row,
3713                         wbc=gui_internal_button_new_with_callback(this, _("POIs"),
3714                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3715                                 gui_internal_cmd_pois, NULL));
3716                 wbc->c=pc;
3717         }
3718 #endif /* _MSC_VER */
3719 #if 0
3720         gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3721         gui_internal_widget_append(row,
3722                 gui_internal_button_new(this, "Add to tour",
3723                         image_new_o(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill));
3724 #endif
3725         if (flags & 128) {
3726                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3727                 gui_internal_widget_append(row,
3728                         wbc=gui_internal_button_new_with_callback(this, _("View on map"),
3729                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3730                                 gui_internal_cmd_view_on_map, NULL));
3731                 wbc->c=pc;
3732                 if ((flags & 4) && wm)
3733                         wbc->item=wm->item;
3734                 else
3735                         wbc->item.type=type_none;
3736         }
3737         if(flags & 256 && this->results_map_population) {
3738                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3739                 gui_internal_widget_append(row,
3740                         wbc=gui_internal_button_new_with_callback(this, _("Remove search results from the map"),
3741                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3742                                 gui_internal_cmd_results_map_clean, NULL));
3743                 wbc->data=wm;
3744         }
3745         if(flags & 2048) {
3746                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3747                 gui_internal_widget_append(row,
3748                         wbc=gui_internal_button_new_with_callback(this, _("Show results on the map"),
3749                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3750                                 gui_internal_cmd_results_to_map, NULL));
3751                 wbc->data=wm;
3752         }
3753         if ((flags & 256) || (flags & 1024)) {
3754                 struct displaylist_handle *dlh;
3755                 struct displaylist *display;
3756                 struct attr attr;
3757                 struct point p;
3758                 struct transformation *trans;
3759                 
3760                 char *text, *label;
3761                 struct map_selection *sel;
3762                 GList *l, *ll;
3763                 
3764                 c.x=pc.x;
3765                 c.y=pc.y;
3766
3767                 trans=navit_get_trans(this->nav);
3768                 transform(trans,pc.pro,&c,&p,1,0,0,0);
3769                 display=navit_get_displaylist(this->nav);
3770                 dlh=graphics_displaylist_open(display);
3771                 sel=displaylist_get_selection(display);
3772                 l=displaylist_get_clicked_list(display, &p, this->radius);
3773                 for(ll=l;ll;ll=g_list_next(ll)) {
3774                         struct displayitem *di;
3775                         struct item *item;
3776                         struct map_rect *mr;
3777                         struct item *itemo;
3778
3779                         di=(struct displayitem*)ll->data;
3780                         item=graphics_displayitem_get_item(di);
3781                         
3782                         mr=map_rect_new(item->map, sel);
3783                         itemo=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
3784                                 
3785                         if (item_attr_get(itemo, attr_label, &attr)) {
3786                                 label=map_convert_string(itemo->map, attr.u.str);
3787                                 text=g_strdup_printf("%s %s", item_to_name(item->type), label);
3788                                 map_convert_free(label);
3789                         } else
3790                                 text=g_strdup_printf("%s", item_to_name(item->type));
3791                         gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3792                         gui_internal_widget_append(row, wc=gui_internal_cmd_pois_item(this, NULL, itemo, NULL, -1, text));
3793                         wc->c=pc;
3794                         g_free(wc->name);
3795                         wc->name=g_strdup(text);
3796                         wc->item=*itemo;
3797                         g_free(text);
3798                         map_rect_destroy(mr);
3799                         if(!wclosest)
3800                                 wclosest=wc;
3801
3802                 }
3803                 g_list_free(l);
3804                 map_selection_destroy(sel);
3805                 graphics_displaylist_close(dlh);
3806         }
3807         if (flags & 512) {
3808                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3809                 gui_internal_widget_append(row,
3810                         wbc=gui_internal_button_new_with_callback(this, _("Cut Bookmark"),
3811                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3812                                 gui_internal_cmd_cut_bookmark, NULL));
3813                 wbc->text=g_strdup(wm->text);
3814                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3815                 gui_internal_widget_append(row,
3816                         wbc=gui_internal_button_new_with_callback(this, _("Copy Bookmark"),
3817                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3818                                 gui_internal_cmd_copy_bookmark, NULL));
3819                 wbc->text=g_strdup(wm->text);
3820                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3821                 gui_internal_widget_append(row,
3822                         wbc=gui_internal_button_new_with_callback(this, _("Rename Bookmark"),
3823                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3824                                 gui_internal_cmd_rename_bookmark, NULL));
3825                 wbc->text=g_strdup(wm->text);
3826                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3827                 gui_internal_widget_append(row,
3828                         wbc=gui_internal_button_new_with_callback(this, _("Paste Bookmark"),
3829                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3830                                 gui_internal_cmd_paste_bookmark, NULL));
3831                 gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
3832                 gui_internal_widget_append(row,
3833                         wbc=gui_internal_button_new_with_callback(this, _("Delete Bookmark"),
3834                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3835                                 gui_internal_cmd_delete_bookmark, NULL));
3836                 wbc->text=g_strdup(wm->text);
3837         }
3838         
3839         gui_internal_menu_render(this);
3840
3841         if((flags & 1024) && wclosest) 
3842                         gui_internal_cmd_view_attributes(this,wclosest,wclosest->data);
3843 }
3844
3845
3846 /* wm->data:    0 Nothing special
3847                 1 Map Point
3848                 2 Item
3849                 3 Town
3850                 4 County
3851                 5 Street
3852                 6 House number
3853                 7 Bookmark
3854                 8 Former destination
3855                 9 Item from the POI list
3856 */
3857
3858 static void
3859 gui_internal_cmd_position(struct gui_priv *this, struct widget *wm, void *data)
3860 {
3861         int flags;
3862         switch ((long) wm->data) {
3863         case 0:
3864                 flags=8|16|32|64|128|256;
3865                 break;
3866         case 1:
3867                 flags=8|16|32|64|256;
3868                 break;
3869         case 2:
3870                 flags=4|8|16|32|64|128;
3871                 break;
3872         case 3:
3873                 flags=1|8|16|32|64|128|2048;
3874                 flags &= this->flags_town;
3875                 break;
3876         case 4:
3877                 gui_internal_search_town_in_country(this, wm);
3878                 return;
3879         case 5:
3880                 flags=2|8|16|32|64|128|2048;
3881                 flags &= this->flags_street;
3882                 break;
3883         case 6:
3884                 flags=8|16|32|64|128|2048;
3885                 flags &= this->flags_house_number;
3886                 break;
3887         case 7:
3888                 flags=8|16|64|128|512;
3889                 break;
3890         case 8:
3891                 flags=8|16|32|64|128;
3892                 break;
3893         case 9:
3894                 flags=4|8|16|32|64|128|2048;
3895                 break;
3896         default:
3897                 return;
3898         }
3899         switch (flags) {
3900         case 2:
3901                 gui_internal_search_house_number_in_street(this, wm, NULL);
3902                 return;
3903         case 8:
3904                 gui_internal_cmd_set_destination(this, wm, NULL);
3905                 return;
3906         }
3907         gui_internal_cmd_position_do(this, &wm->c, NULL, wm, wm->name ? wm->name : wm->text, flags);
3908 }
3909
3910 static void
3911 gui_internal_cmd2_position(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3912 {
3913         char *name=_("Position");
3914         int flags=-1;
3915
3916         dbg(1,"enter\n");
3917         if (!in || !in[0])
3918                 return;
3919         if (!ATTR_IS_COORD_GEO(in[0]->type))
3920                 return;
3921         if (in[1] && ATTR_IS_STRING(in[1]->type)) {
3922                 name=in[1]->u.str;
3923                 if (in[2] && ATTR_IS_INT(in[2]->type))
3924                         flags=in[2]->u.num;
3925         }
3926         dbg(1,"flags=0x%x\n",flags);
3927         gui_internal_cmd_position_do(this, NULL, in[0]->u.coord_geo, NULL, name, flags);
3928 }
3929
3930 static void
3931 gui_internal_cmd2_pois(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3932 {
3933         struct widget *w;
3934         struct poi_param *param;
3935         struct attr pro;
3936         struct coord c;
3937
3938         dbg(1,"enter\n");
3939         if (!in || !in[0])
3940                 return;
3941         if (!ATTR_IS_COORD_GEO(in[0]->type))
3942                 return;
3943         if (!navit_get_attr(this->nav, attr_projection, &pro, NULL))
3944                 return;
3945         w=g_new0(struct widget,1);
3946         param=g_new0(struct poi_param,1);
3947         if (in[1] && ATTR_IS_STRING(in[1]->type)) {
3948                 gui_internal_poi_param_set_filter(param, in[1]->u.str);
3949                 if (in[2] && ATTR_IS_INT(in[2]->type))
3950                         param->isAddressFilter=in[2]->u.num;
3951         }
3952         
3953         transform_from_geo(pro.u.projection,in[0]->u.coord_geo,&c);
3954         w->c.x=c.x;
3955         w->c.y=c.y;
3956         w->c.pro=pro.u.projection;
3957         gui_internal_cmd_pois(this, w, param);
3958         g_free(w);
3959         gui_internal_poi_param_free(param);
3960 }
3961
3962 static void
3963 gui_internal_cmd2_refresh(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
3964 {
3965         char *href=g_strdup(this->href);
3966         gui_internal_html_load_href(this, href, 1);
3967         g_free(href);
3968 }
3969
3970
3971 /**
3972   * The "Bookmarks" section of the OSD
3973   * 
3974   */
3975
3976 static void
3977 gui_internal_cmd_bookmarks(struct gui_priv *this, struct widget *wm, void *data)
3978 {
3979         struct attr attr,mattr;
3980         struct item *item;
3981         char *label_full,*prefix=0;
3982         int plen=0,hassub,found=0;
3983         struct widget *wb,*w,*wbm;
3984         struct coord c;
3985         struct widget *tbl, *row;
3986
3987         if (data)
3988                 prefix=g_strdup(data);
3989         else {
3990                 if (wm && wm->prefix)
3991                         prefix=g_strdup(wm->prefix);
3992         }
3993         if ( prefix )
3994                 plen=strlen(prefix);
3995
3996         gui_internal_prune_menu_count(this, 1, 0);
3997         wb=gui_internal_menu(this, _("Bookmarks"));
3998         wb->background=this->background;
3999         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4000         //w->spy=this->spacing*3;
4001         gui_internal_widget_append(wb, w);
4002
4003         if(navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL) ) {
4004                 if (!plen) {
4005                         bookmarks_move_root(mattr.u.bookmarks);
4006                 } else {
4007                         if (!strcmp(prefix,"..")) {
4008                                 bookmarks_move_up(mattr.u.bookmarks);
4009                                 g_free(prefix);
4010                                 prefix=g_strdup(bookmarks_item_cwd(mattr.u.bookmarks));
4011                                 if (prefix) {
4012                                         plen=strlen(prefix);
4013                                 } else {
4014                                         plen=0;
4015                                 }
4016                         } else {
4017                                 bookmarks_move_down(mattr.u.bookmarks,prefix);
4018                         }
4019                         
4020                           // "Back" button, when inside a bookmark folder
4021                           
4022                         if (plen) {
4023                                 wbm=gui_internal_button_new_with_callback(this, "..",
4024                                         image_new_xs(this, "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
4025                                                 gui_internal_cmd_bookmarks, NULL);
4026                                                 wbm->prefix=g_strdup("..");
4027                                 gui_internal_widget_append(w, wbm);
4028
4029                                 // load bookmark folder as Waypoints, if any
4030                                 if (bookmarks_get_bookmark_count(mattr.u.bookmarks) > 0){
4031                                         wbm=gui_internal_button_new_with_callback(this, _("Bookmarks as Waypoints"),
4032                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4033                                                         gui_internal_cmd_load_bookmarks_as_waypoints, NULL);
4034                                         wbm->prefix=g_strdup(prefix);
4035                                         gui_internal_widget_append(w, wbm);
4036                                 }
4037
4038                                 // save Waypoints in bookmark folder, if route exists
4039                                 if (navit_get_destination_count(this->nav) > 0){
4040                                         if (bookmarks_get_bookmark_count(mattr.u.bookmarks)==0){
4041                                                 wbm=gui_internal_button_new_with_callback(this, _("Save Waypoints"),
4042                                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4043                                                                         gui_internal_cmd_replace_bookmarks_from_waypoints, NULL);
4044                                         }else{
4045                                                 wbm=gui_internal_button_new_with_callback(this, _("Replace Waypoints"),
4046                                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4047                                                                         gui_internal_cmd_replace_bookmarks_from_waypoints, NULL);
4048                                         }
4049                                         wbm->prefix=g_strdup(prefix);
4050                                         gui_internal_widget_append(w, wbm);
4051                                 }
4052
4053                                 // delete empty folder
4054                                 if (bookmarks_get_bookmark_count(mattr.u.bookmarks)==0){
4055                                         gui_internal_widget_append(w,
4056                                                         wbm=gui_internal_button_new_with_callback(this, _("Delete Folder"),
4057                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4058                                                         gui_internal_cmd_delete_bookmark_folder, NULL));
4059                                         wbm->prefix=g_strdup(prefix);
4060                                 }
4061
4062                         }
4063                 }
4064                 
4065                 // Adds the Bookmark folders
4066                 wbm=gui_internal_button_new_with_callback(this, _("Add Bookmark folder"),
4067                             image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4068                                 gui_internal_cmd_add_bookmark_folder2, NULL);
4069                 gui_internal_widget_append(w, wbm);
4070
4071                 // Pastes the Bookmark
4072                 wbm=gui_internal_button_new_with_callback(this, _("Paste bookmark"),
4073                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4074                                 gui_internal_cmd_paste_bookmark, NULL);
4075                 gui_internal_widget_append(w, wbm);
4076
4077                 bookmarks_item_rewind(mattr.u.bookmarks);
4078                                 
4079                 tbl=gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
4080                 gui_internal_widget_append(w,tbl);
4081
4082                 while ((item=bookmarks_get_item(mattr.u.bookmarks))) {
4083                         if (!item_attr_get(item, attr_label, &attr)) continue;
4084                         label_full=attr.u.str;
4085                         dbg(0,"full_labled: %s\n",label_full);
4086                         
4087                         // hassub == 1 if the item type is a sub-folder
4088                         if (item->type == type_bookmark_folder) {
4089                                 hassub=1;
4090                         } else {
4091                                 hassub=0;
4092                         }
4093                         
4094                         row=gui_internal_widget_table_row_new(this,gravity_left| flags_fill| orientation_horizontal);
4095                         gui_internal_widget_append(tbl, row);
4096                         wbm=gui_internal_button_new_with_callback(this, label_full,
4097                                 image_new_xs(this, hassub ? "gui_inactive" : "gui_active" ), gravity_left_center|orientation_horizontal|flags_fill,
4098                                         hassub ? gui_internal_cmd_bookmarks : gui_internal_cmd_position, NULL);
4099
4100                         gui_internal_widget_append(row,wbm);
4101                         if (item_coord_get(item, &c, 1)) {
4102                                 wbm->c.x=c.x;
4103                                 wbm->c.y=c.y;
4104                                 wbm->c.pro=bookmarks_get_projection(mattr.u.bookmarks);
4105                                 wbm->name=g_strdup_printf(_("Bookmark %s"),label_full);
4106                                 wbm->text=g_strdup(label_full);
4107                                 if (!hassub) {
4108                                         wbm->data=(void*)7;//Mark us as a bookmark
4109                                 }
4110                                 wbm->prefix=g_strdup(label_full);
4111                         } else {
4112                                 gui_internal_widget_destroy(this, row);
4113                         }
4114                 }
4115         }
4116
4117         g_free(prefix);
4118
4119         if (found)
4120                 gui_internal_check_exit(this);
4121         else
4122                 gui_internal_menu_render(this);
4123 }
4124
4125 static void
4126 gui_internal_cmd2_bookmarks(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4127 {
4128         char *str=NULL;
4129         if (in && in[0] && ATTR_IS_STRING(in[0]->type)) {
4130                 str=in[0]->u.str;
4131         }
4132         gui_internal_cmd_bookmarks(this, NULL, str);
4133 }
4134
4135
4136 static void
4137 gui_internal_cmd_formerdests(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4138 {
4139         struct widget *wb,*w,*wbm,*tbl=NULL;
4140         struct map *formerdests;
4141         struct map_rect *mr_formerdests;
4142         struct item *item;
4143         struct attr attr;
4144         char *label_full;
4145         enum projection projection;
4146
4147         gui_internal_prune_menu_count(this, 1, 0);
4148         wb=gui_internal_menu(this, _("Former Destinations"));
4149         wb->background=this->background;
4150
4151         w=gui_internal_box_new(this,
4152                         gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4153         w->spy=this->spacing*2;
4154         gui_internal_widget_append(wb, w);
4155
4156         formerdests=read_former_destinations_from_file();
4157         mr_formerdests=map_rect_new(formerdests, NULL);
4158         projection = map_projection(formerdests);
4159         while ((item=map_rect_get_item(mr_formerdests))) {
4160                 struct coord c;
4161                 struct widget *row;
4162                 if (!item_attr_get(item, attr_label, &attr)) continue;
4163                 if(!tbl) {
4164                         tbl=gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand | orientation_vertical,1);
4165                         gui_internal_widget_append(w,tbl);
4166                 }
4167                 row=gui_internal_widget_table_row_new(this,gravity_left| flags_fill| orientation_vertical);
4168                 gui_internal_widget_prepend(tbl, row);
4169                 label_full=attr.u.str;
4170                 wbm=gui_internal_button_new_with_callback(this, label_full,
4171                                 image_new_xs(this, "gui_active"),
4172                                 gravity_left_center|orientation_horizontal|flags_fill,
4173                                 gui_internal_cmd_position, NULL);
4174                 gui_internal_widget_append(row,wbm);
4175                 if (item_coord_get(item, &c, 1)) {
4176                         wbm->c.x=c.x;
4177                         wbm->c.y=c.y;
4178                         wbm->c.pro=projection;
4179                         wbm->name=g_strdup(label_full);
4180                         wbm->text=g_strdup(label_full);
4181                         wbm->data=(void*)8; //Mark us as a former destination 
4182                         wbm->prefix=g_strdup(label_full);
4183                 }
4184         }
4185         if (!tbl){
4186                 wbm=gui_internal_text_new(this, _("- No former destinations available -"), 
4187                     gravity_left_center|orientation_horizontal|flags_fill);
4188                 gui_internal_widget_append(w, wbm);
4189         }
4190         gui_internal_menu_render(this);
4191         map_rect_destroy(mr_formerdests);
4192 }
4193
4194 static void
4195 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy);
4196
4197 static void gui_internal_keypress_do(struct gui_priv *this, char *key)
4198 {
4199         struct widget *wi,*menu,*search_list;
4200         int len=0;
4201         char *text=NULL;
4202
4203         menu=g_list_last(this->root.children)->data;
4204         wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
4205         if (wi) {
4206                 /* select first item of the searchlist */
4207                 if (*key == NAVIT_KEY_RETURN && (search_list=gui_internal_menu_data(this)->search_list)) {
4208                         GList *l=search_list->children;
4209                         if (l && l->data)
4210                                 gui_internal_highlight_do(this, l->data);
4211                         return; 
4212                 } else if (*key == NAVIT_KEY_BACKSPACE) {
4213                         dbg(0,"backspace\n");
4214                         if (wi->text && wi->text[0]) {
4215                                 len=g_utf8_prev_char(wi->text+strlen(wi->text))-wi->text;
4216                                 wi->text[len]=' ';
4217                                 text=g_strdup_printf("%s ", wi->text);
4218                         }
4219                 } else {
4220                         if (wi->state & STATE_CLEAR) {
4221                                 dbg(0,"wi->state=0x%x\n", wi->state);
4222                                 g_free(wi->text);
4223                                 wi->text=NULL;
4224                                 wi->state &= ~STATE_CLEAR;
4225                                 dbg(0,"wi->state=0x%x\n", wi->state);
4226                         }
4227                         text=g_strdup_printf("%s%s", wi->text ? wi->text : "", key);
4228                 }
4229                 g_free(wi->text);
4230                 wi->text=text;
4231                 if (*key == NAVIT_KEY_BACKSPACE && wi->text) {
4232                         gui_internal_widget_render(this, wi);
4233                         wi->text[len]='\0';
4234                 }
4235                 if (wi->func) {
4236                         wi->reason=2;
4237                         wi->func(this, wi, wi->data);
4238                 }
4239                 gui_internal_widget_render(this, wi);
4240         }
4241 }
4242
4243
4244 static void
4245 gui_internal_cmd_keypress(struct gui_priv *this, struct widget *wm, void *data)
4246 {
4247         struct menu_data *md=gui_internal_menu_data(this);
4248         gui_internal_keypress_do(this, (char *) wm->data);
4249         // Switch to lowercase after the first key is pressed
4250         if (md->keyboard_mode == 2) // Latin
4251                 gui_internal_keyboard_do(this, md->keyboard, 10);
4252         if (md->keyboard_mode == 26) // Umlaut
4253                 gui_internal_keyboard_do(this, md->keyboard, 34);
4254         if ((md->keyboard_mode & ~7) == 40) // Russian/Ukrainian/Belorussian
4255                 gui_internal_keyboard_do(this, md->keyboard, 48);
4256 }
4257
4258 static void
4259 gui_internal_search_idle_end(struct gui_priv *this)
4260 {
4261         if (this->idle) {
4262                 event_remove_idle(this->idle);
4263                 this->idle=NULL;
4264         }
4265         if (this->idle_cb) {
4266                 callback_destroy(this->idle_cb);
4267                 this->idle_cb=NULL;
4268         }
4269 }
4270
4271 static char *
4272 postal_str(struct search_list_result *res, int level)
4273 {
4274         char *ret=NULL;
4275         if (res->town->common.postal)
4276                 ret=res->town->common.postal;
4277         if (res->town->common.postal_mask)
4278                 ret=res->town->common.postal_mask;
4279         if (level == 1)
4280                 return ret;
4281         if (res->street->common.postal)
4282                 ret=res->street->common.postal;
4283         if (res->street->common.postal_mask)
4284                 ret=res->street->common.postal_mask;
4285         if (level == 2)
4286                 return ret;
4287         if (res->house_number->common.postal)
4288                 ret=res->house_number->common.postal;
4289         if (res->house_number->common.postal_mask)
4290                 ret=res->house_number->common.postal_mask;
4291         return ret;
4292 }
4293
4294 static char *
4295 district_str(struct search_list_result *res, int level)
4296 {
4297         char *ret=NULL;
4298         if (res->town->common.district_name)
4299                 ret=res->town->common.district_name;
4300         if (level == 1)
4301                 return ret;
4302         if (res->street->common.district_name)
4303                 ret=res->street->common.district_name;
4304         if (level == 2)
4305                 return ret;
4306         if (res->house_number->common.district_name)
4307                 ret=res->house_number->common.district_name;
4308         return ret;
4309 }
4310
4311 static char *
4312 town_str(struct search_list_result *res, int level, int flags)
4313 {
4314         char *town=res->town->common.town_name;
4315         char *district=district_str(res, level);
4316         char *postal=postal_str(res, level);
4317         char *postal_sep=" ";
4318         char *district_begin=" (";
4319         char *district_end=")";
4320         char *county_sep = ", Co. ";
4321         char *county = res->town->common.county_name;
4322         if (!postal)
4323                 postal_sep=postal="";
4324         if (!district || (flags & 1))
4325                 district_begin=district_end=district="";
4326         if (!county)
4327                 county_sep=county="";
4328
4329         return g_strdup_printf("%s%s%s%s%s%s%s%s", postal, postal_sep, town, district_begin, district, district_end, county_sep, county);
4330 }
4331
4332 static int gui_internal_search_cmp(gconstpointer _a, gconstpointer _b)
4333 {
4334   struct widget *a=(struct widget *)_a, *b=(struct widget *)_b;
4335   char *sa,*sb;
4336   int r;
4337   if(!b)
4338   if((!a || a->type!=widget_table_row || !a->text) && (!b || b->type!=widget_table_row || !b->text))
4339         return 0;
4340   if(!a || a->type!=widget_table_row || !a->text)
4341         return -1;
4342   if(!b || b->type!=widget_table_row || !b->text)
4343         return 1;
4344   r=a->datai-b->datai;
4345   if(r<0)
4346         return -1;
4347   if(r>0)
4348         return 1;
4349   sa=removecase(a->text);
4350   sb=removecase(b->text);
4351   r=strcmp(sa,sb);
4352   g_free(sa);
4353   g_free(sb);
4354   return r;
4355 }
4356
4357 static void
4358 gui_internal_search_idle(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
4359 {
4360         char *text=NULL,*text2=NULL,*name=NULL;
4361         struct search_list_result *res;
4362         struct widget *wc;
4363         struct item *item=NULL;
4364         GList *l;
4365         static char possible_keys[256]="";
4366         struct widget *wi=NULL;
4367         struct widget *wr;
4368
4369         res=search_list_get_result(this->sl);
4370         if (res) {
4371                 gchar* trunk_name = NULL;
4372
4373                 struct widget *menu=g_list_last(this->root.children)->data;
4374                 wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
4375
4376                 if (wi) {
4377                         if (! strcmp(wm_name,"Town"))
4378                                 trunk_name = g_strrstr(res->town->common.town_name, wi->text);
4379                         if (! strcmp(wm_name,"Street"))
4380                         {
4381                                 name=res->street->name;
4382                                 if (name)
4383                                         trunk_name = g_strrstr(name, wi->text);
4384                                 else
4385                                         trunk_name = NULL;
4386                         }
4387
4388                         if (trunk_name) {
4389                                 char next_char = trunk_name[strlen(wi->text)];
4390                                 int i;
4391                                 int len = strlen(possible_keys);
4392                                 for(i = 0; (i<len) && (possible_keys[i] != next_char) ;i++) ;
4393                                 if (i==len || !len) {
4394                                         possible_keys[len]=trunk_name[strlen(wi->text)];
4395                                         possible_keys[len+1]='\0';
4396                                 }
4397                                 dbg(1,"%s %s possible_keys:%s \n", wi->text, res->town->common.town_name, possible_keys);
4398                         }
4399                 } else {
4400                         dbg(0, "Unable to find widget");
4401                 }
4402         }
4403
4404         if (! res) {
4405                 struct menu_data *md;
4406                 gui_internal_search_idle_end(this);
4407
4408                 md=gui_internal_menu_data(this);
4409                 if (md && md->keyboard && !(this->flags & 2048)) {
4410                         GList *lk=md->keyboard->children;
4411                         graphics_draw_mode(this->gra, draw_mode_begin);
4412                         while (lk) {
4413                                 struct widget *child=lk->data;
4414                                 GList *lk2=child->children;
4415                                 while (lk2) {
4416                                         struct widget *child_=lk2->data;
4417                                         lk2=g_list_next(lk2);
4418                                         if (child_->data && strcmp("\b", child_->data)) { // FIXME don't disable special keys
4419                                                 if (strlen(possible_keys) == 0)
4420                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
4421                                                 else if (g_strrstr(possible_keys, child_->data)!=NULL ) {
4422                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
4423                                                 } else {
4424                                                         child_->state&= ~(STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SELECTED) ;
4425                                                 }
4426                                                 gui_internal_widget_render(this,child_);
4427                                         }
4428                                 }
4429                                 lk=g_list_next(lk);
4430                         }
4431                         gui_internal_widget_render(this,md->keyboard);
4432                         graphics_draw_mode(this->gra, draw_mode_end);
4433                 }
4434
4435                 possible_keys[0]='\0';
4436                 return;
4437         }
4438
4439         if (! strcmp(wm_name,"Country")) {
4440                 name=res->country->name;
4441                 item=&res->country->common.item;
4442                 text=g_strdup_printf("%s", res->country->name);
4443         }
4444         if (! strcmp(wm_name,"Town")) {
4445                 item=&res->town->common.item;
4446                 name=res->town->common.town_name;
4447                 text=town_str(res, 1, 0);
4448         }
4449         if (! strcmp(wm_name,"Street")) {
4450                 name=res->street->name;
4451                 item=&res->street->common.item;
4452                 text=g_strdup(res->street->name);
4453                 text2=town_str(res, 2, 1);
4454         }
4455         if (! strcmp(wm_name,"House number")) {
4456                 name=res->house_number->house_number;
4457                 text=g_strdup_printf("%s %s", res->street->name, name);
4458                 text2=town_str(res, 3, 0);
4459         }
4460         dbg(1,"res->country->flag=%s\n", res->country->flag);
4461         wr=gui_internal_widget_table_row_new(this, gravity_left|orientation_horizontal|flags_fill);
4462
4463                 if (!text2) {
4464                         wr->text=g_strdup(text);
4465                         gui_internal_widget_insert_sorted(search_list, wr, gui_internal_search_cmp);
4466                         gui_internal_widget_append(wr,
4467                                 wc=gui_internal_button_new_with_callback(this, text,
4468                                         image_new_xs(this, res->country->flag),
4469                                         gravity_left_center|orientation_horizontal|flags_fill,
4470                                         gui_internal_cmd_position, param));
4471                 } else {
4472                         struct widget *wb;
4473                         wr->text=g_strdup_printf("%s %s",text,text2);
4474
4475                         if (! strcmp(wm_name,"House number") && !res->street->name) {
4476                                 wr->datai=1000;
4477                         } else if(name) {
4478                                 if(!wi)
4479                                         dbg(0,"search text widget is NULL\n");
4480                                 if(wi && strlen(name)==strlen(wi->text)) {
4481                                         dbg(1,"xact %s %s\n",name, wi->text);
4482                                         wr->datai=-1000;
4483                                 } else {
4484                                         dbg(1,"not xact %s %s\n",name, wi->text);
4485                                         wr->datai=0;
4486                                 }
4487                         }
4488                         gui_internal_widget_insert_sorted(search_list, wr, gui_internal_search_cmp);
4489                         
4490                         wc=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
4491                         gui_internal_widget_append(wr, wc);
4492                         gui_internal_widget_append(wc, gui_internal_image_new(this, image_new_xs(this, res->country->flag)));
4493                         wb=gui_internal_box_new(this, gravity_left_center|orientation_vertical|flags_fill);
4494                         gui_internal_widget_append(wc, wb);
4495                         gui_internal_widget_append(wb, gui_internal_label_new(this, text));
4496                         gui_internal_widget_append(wb, gui_internal_label_font_new(this, text2, 1));
4497                         wc->func=gui_internal_cmd_position;
4498                         wc->data=param;
4499                         wc->state |= STATE_SENSITIVE;
4500                         wc->speech=g_strdup(text);
4501                 }
4502                 wc->name=g_strdup(name);
4503                 if (res->c)
4504                         wc->c=*res->c;
4505                 wc->selection_id=res->id;
4506                 if (item)
4507                         wc->item=*item;
4508                 gui_internal_widget_pack(this, search_list);
4509                 l=g_list_last(this->root.children);
4510                 graphics_draw_mode(this->gra, draw_mode_begin);
4511                 gui_internal_widget_render(this, l->data);
4512                 graphics_draw_mode(this->gra, draw_mode_end);
4513         g_free(text);
4514         g_free(text2);
4515 }
4516
4517 static void
4518 gui_internal_search_idle_start(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
4519 {
4520         this->idle_cb=callback_new_4(callback_cast(gui_internal_search_idle), this, wm_name, search_list, param);
4521         this->idle=event_add_idle(50,this->idle_cb);
4522         callback_call_0(this->idle_cb);
4523 }
4524
4525
4526 /**
4527  *
4528  * @param wm The widget that generated the event for the search changed,
4529  *        if this was generated by a key on the virtual keyboard then
4530  *        wm is the key button widget.
4531  */
4532 static void
4533 gui_internal_search_changed(struct gui_priv *this, struct widget *wm, void *data)
4534 {
4535         GList *l;
4536         struct widget *search_list=gui_internal_menu_data(this)->search_list;
4537         void *param=(void *)3;
4538         int minlen=1;
4539
4540         gui_internal_widget_table_clear(this, search_list);
4541
4542         if (! strcmp(wm->name,"Country"))
4543                 param=(void *)4;
4544         if (! strcmp(wm->name,"Street"))
4545                 param=(void *)5;
4546         if (! strcmp(wm->name,"House number"))
4547                 param=(void *)6;
4548         dbg(0,"%s now '%s'\n", wm->name, wm->text);
4549
4550         gui_internal_search_idle_end(this);
4551         if (wm->text && g_utf8_strlen(wm->text, -1) >= minlen) {
4552                 struct attr search_attr;
4553
4554                 dbg(0,"process\n");
4555                 if (! strcmp(wm->name,"Country"))
4556                         search_attr.type=attr_country_all;
4557                 if (! strcmp(wm->name,"Town"))
4558                         search_attr.type=attr_town_or_district_name;
4559                 if (! strcmp(wm->name,"Street"))
4560                         search_attr.type=attr_street_name;
4561                 if (! strcmp(wm->name,"House number"))
4562                         search_attr.type=attr_house_number;
4563                 search_attr.u.str=wm->text;
4564                 search_list_search(this->sl, &search_attr, 1);
4565                 gui_internal_search_idle_start(this, wm->name, search_list, param);
4566         }
4567         l=g_list_last(this->root.children);
4568         gui_internal_widget_render(this, l->data);
4569 }
4570
4571 static struct widget *
4572 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)
4573 {
4574         struct widget *wk;
4575         gui_internal_widget_append(wkbd, wk=gui_internal_button_font_new_with_callback(this, text, font,
4576                 NULL, gravity_center|orientation_vertical, func, data));
4577         wk->data_free=data_free;
4578         wk->background=this->background;
4579         wk->bl=0;
4580         wk->br=0;
4581         wk->bt=0;
4582         wk->bb=0;
4583         wk->w=w;
4584         wk->h=h;
4585         return wk;
4586 }
4587
4588 static struct widget *
4589 gui_internal_keyboard_key(struct gui_priv *this, struct widget *wkbd, char *text, char *key, int w, int h)
4590 {
4591         return gui_internal_keyboard_key_data(this, wkbd, text, 0, gui_internal_cmd_keypress, g_strdup(key), g_free_func,w,h);
4592 }
4593
4594 static void gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data);
4595
4596
4597 // A list of availiable keyboard modes.
4598 struct gui_internal_keyb_mode {
4599     char title[16]; // Label to be displayed on keys that switch to it
4600     int font; // Font size of label
4601     int case_mode; // Mode to switch to when case CHANGE() key is pressed.
4602     int umlaut_mode;  // Mode to switch to when UMLAUT() key is pressed.
4603 } gui_internal_keyb_modes[]= {
4604         /* 0*/ {"ABC", 2,  8, 24},
4605         /* 8*/ {"abc", 2,  0, 32},
4606         /*16*/ {"123", 2,  0, 24},
4607         /*24*/ {"ÄÖÜ", 2, 40, 0},
4608         /*32*/ {"äöü", 2, 32, 8},
4609         /*40*/ {"АБВ", 2, 48,  0},
4610         /*48*/ {"абв", 2, 40,  8}
4611 };
4612
4613
4614 // Some macros that make the keyboard layout easier to visualise in
4615 // the source code. The macros are #undef'd after this function.
4616 #define KEY(x) gui_internal_keyboard_key(this, wkbd, (x), (x), max_w, max_h)
4617 #define SPACER() gui_internal_keyboard_key_data(this, wkbd, "", 0, NULL, NULL, NULL,max_w,max_h)
4618 #define MODE(x) gui_internal_keyboard_key_data(this, wkbd, \
4619                 gui_internal_keyb_modes[(x)/8].title, \
4620                 gui_internal_keyb_modes[(x)/8].font, \
4621                 gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h) \
4622                         -> datai=(mode&7)+((x)&~7)
4623 #define SWCASE() MODE(gui_internal_keyb_modes[mode/8].case_mode)
4624 #define UMLAUT() MODE(gui_internal_keyb_modes[mode/8].umlaut_mode)
4625 static struct widget *
4626 gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode)
4627 {
4628         struct widget *wkbd,*wk;
4629         struct menu_data *md=gui_internal_menu_data(this);
4630         int i, max_w=this->root.w, max_h=this->root.h;
4631         int render=0;
4632         char *space="_";
4633         char *backspace="←";
4634         char *hide="▼";
4635         char *unhide="▲";
4636
4637         if (wkbdb) {
4638                 this->current.x=-1;
4639                 this->current.y=-1;
4640                 gui_internal_highlight(this);
4641                 if (md->keyboard_mode >= 1024)
4642                         render=2;
4643                 else
4644                         render=1;
4645                 gui_internal_widget_children_destroy(this, wkbdb);
4646         } else
4647                 wkbdb=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
4648         md->keyboard=wkbdb;
4649         md->keyboard_mode=mode;
4650         wkbd=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
4651         wkbd->background=this->background;
4652         wkbd->cols=8;
4653         wkbd->spx=0;
4654         wkbd->spy=0;
4655         max_w=max_w/8;
4656         max_h=max_h/8; // Allows 3 results in the list when searching for Towns
4657         wkbd->p.y=max_h*2;
4658         if(mode>=40&&mode<56) { // Russian/Ukrainian/Belarussian layout needs more space...
4659                 max_h=max_h*4/5;
4660                 max_w=max_w*8/9;
4661                 wkbd->cols=9;
4662         }
4663
4664         if (mode >= 0 && mode < 8) {
4665                 for (i = 0 ; i < 26 ; i++) {
4666                         char text[]={'A'+i,'\0'};
4667                         KEY(text);
4668                 }
4669                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4670                 if (mode == 0) {
4671                         KEY("-");
4672                         KEY("'");
4673                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4674                         wk->datai=mode+1024;
4675                 } else {
4676                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4677                         wk->datai=mode+1024;
4678                         SWCASE();
4679                         MODE(16);
4680                         
4681                 }
4682                 UMLAUT();
4683                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4684         }
4685         if (mode >= 8 && mode < 16) {
4686                 for (i = 0 ; i < 26 ; i++) {
4687                         char text[]={'a'+i,'\0'};
4688                         KEY(text);
4689                 }
4690                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4691                 if (mode == 8) {
4692                         KEY("-");
4693                         KEY("'");
4694                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4695                         wk->datai=mode+1024;
4696                 } else {
4697                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4698                         wk->datai=mode+1024;
4699                         SWCASE();
4700                         
4701                         MODE(16);
4702                 }
4703                 UMLAUT();
4704                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4705         }
4706         if (mode >= 16 && mode < 24) {
4707                 for (i = 0 ; i < 10 ; i++) {
4708                         char text[]={'0'+i,'\0'};
4709                         KEY(text);
4710                 }
4711                 KEY("."); KEY("°"); KEY("'"); KEY("\""); KEY("-"); KEY("+");
4712                 KEY("*"); KEY("/"); KEY("("); KEY(")"); KEY("="); KEY("?");
4713
4714                 
4715
4716                 if (mode == 16) {
4717                         for (i = 0 ; i < 5 ; i++) SPACER();
4718                         KEY("-");
4719                         KEY("'");
4720                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4721                         wk->datai=mode+1024;
4722                 } else {
4723                         for (i = 0 ; i < 3 ; i++) SPACER();
4724                         MODE(40);
4725                         MODE(48);
4726                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4727                         wk->datai=mode+1024;
4728                         MODE(0);
4729                         MODE(8);
4730                 }
4731                 UMLAUT();
4732                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4733         }
4734         if (mode >= 24 && mode < 32) {
4735                 KEY("Ä"); KEY("Ë"); KEY("Ï"); KEY("Ö"); KEY("Ü"); KEY("Æ"); KEY("Ø"); KEY("Å");
4736                 KEY("Á"); KEY("É"); KEY("Í"); KEY("Ó"); KEY("Ú"); KEY("Š"); KEY("Č"); KEY("Ž");
4737                 KEY("À"); KEY("È"); KEY("Ì"); KEY("Ò"); KEY("Ù"); KEY("Ś"); KEY("Ć"); KEY("Ź");
4738                 KEY("Â"); KEY("Ê"); KEY("Î"); KEY("Ô"); KEY("Û"); SPACER();
4739
4740                 UMLAUT();
4741
4742                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4743         }
4744         if (mode >= 32 && mode < 40) {
4745                 KEY("ä"); KEY("ë"); KEY("ï"); KEY("ö"); KEY("ü"); KEY("æ"); KEY("ø"); KEY("å");
4746                 KEY("á"); KEY("é"); KEY("í"); KEY("ó"); KEY("ú"); KEY("š"); KEY("č"); KEY("ž");
4747                 KEY("à"); KEY("è"); KEY("ì"); KEY("ò"); KEY("ù"); KEY("ś"); KEY("ć"); KEY("ź");
4748                 KEY("â"); KEY("ê"); KEY("î"); KEY("ô"); KEY("û"); KEY("ß");
4749
4750                 UMLAUT();
4751
4752                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4753         }
4754         if (mode >= 40 && mode < 48) {
4755                 KEY("А"); KEY("Б"); KEY("В"); KEY("Г"); KEY("Д"); KEY("Е"); KEY("Ж"); KEY("З"); KEY("И");
4756                 KEY("Й"); KEY("К"); KEY("Л"); KEY("М"); KEY("Н"); KEY("О"); KEY("П"); KEY("Р"); KEY("С");
4757                 KEY("Т"); KEY("У"); KEY("Ф"); KEY("Х"); KEY("Ц"); KEY("Ч"); KEY("Ш"); KEY("Щ"); KEY("Ъ"); 
4758                 KEY("Ы"); KEY("Ь"); KEY("Э"); KEY("Ю"); KEY("Я"); KEY("Ё"); KEY("І"); KEY("Ї"); KEY("Ў");
4759                 SPACER(); SPACER(); SPACER();
4760                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4761
4762                 wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4763                 wk->datai=mode+1024;
4764
4765                 SWCASE();
4766
4767                 MODE(16);
4768
4769                 SPACER();
4770
4771                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4772         }
4773         if (mode >= 48 && mode < 56) {
4774                 KEY("а"); KEY("б"); KEY("в"); KEY("г"); KEY("д"); KEY("е"); KEY("ж"); KEY("з"); KEY("и");
4775                 KEY("й"); KEY("к"); KEY("л"); KEY("м"); KEY("н"); KEY("о"); KEY("п"); KEY("р"); KEY("с");
4776                 KEY("т"); KEY("у"); KEY("ф"); KEY("х"); KEY("ц"); KEY("ч"); KEY("ш"); KEY("щ"); KEY("ъ");
4777                 KEY("ы"); KEY("ь"); KEY("э"); KEY("ю"); KEY("я"); KEY("ё"); KEY("і"); KEY("ї"); KEY("ў");
4778                 SPACER(); SPACER(); SPACER();
4779                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4780                 
4781                 wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4782                 wk->datai=mode+1024;
4783
4784                 SWCASE();
4785
4786                 MODE(16);
4787
4788                 SPACER();
4789
4790                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4791         }
4792
4793
4794         if(md->search_list && md->search_list->type==widget_table) {
4795                 struct table_data *td=(struct table_data*)(md->search_list->data);
4796                 td->button_box_hide=mode<1024;
4797         }
4798         
4799         if (mode >= 1024) {
4800                 char *text=NULL;
4801                 int font=0;
4802                 struct widget *wkl;
4803                 mode -= 1024;
4804                 text=gui_internal_keyb_modes[mode/8].title;
4805                 font=gui_internal_keyb_modes[mode/8].font;
4806                 wk=gui_internal_box_new(this, gravity_center|orientation_horizontal|flags_fill);
4807                 wk->func=gui_internal_keyboard_change;
4808                 wk->data=wkbdb;
4809                 wk->background=this->background;
4810                 wk->bl=0;
4811                 wk->br=0;
4812                 wk->bt=0;
4813                 wk->bb=0;
4814                 wk->w=max_w;
4815                 wk->h=max_h;
4816                 wk->datai=mode;
4817                 wk->state |= STATE_SENSITIVE;
4818                 gui_internal_widget_append(wk, wkl=gui_internal_label_new(this, unhide));
4819                 wkl->background=NULL;
4820                 gui_internal_widget_append(wk, wkl=gui_internal_label_font_new(this, text, font));
4821                 wkl->background=NULL;
4822                 gui_internal_widget_append(wkbd, wk);
4823                 if (render)
4824                         render=2;
4825         }
4826         gui_internal_widget_append(wkbdb, wkbd);
4827         if (render == 1) {
4828                 gui_internal_widget_pack(this, wkbdb);
4829                 gui_internal_widget_render(this, wkbdb);
4830         } else if (render == 2) {
4831                 gui_internal_menu_reset_pack(this);
4832                 gui_internal_menu_render(this);
4833         }
4834         return wkbdb;
4835 }
4836 #undef KEY
4837 #undef SPACER
4838 #undef SWCASE
4839 #undef UMLAUT
4840 #undef MODE
4841
4842 static struct widget *
4843 gui_internal_keyboard(struct gui_priv *this, int mode)
4844 {
4845         if (! this->keyboard)
4846                 return NULL;
4847         return gui_internal_keyboard_do(this, NULL, mode);
4848 }
4849
4850 static void
4851 gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data)
4852 {
4853         gui_internal_keyboard_do(this, key->data, key->datai);
4854 }
4855
4856 static void
4857 gui_internal_search_list_set_default_country(struct gui_priv *this)
4858 {
4859         struct attr search_attr, country_name, country_iso2, *country_attr;
4860         struct item *item;
4861         struct country_search *cs;
4862         struct tracking *tracking;
4863         struct search_list_result *res;
4864
4865         country_attr=country_default();
4866         tracking=navit_get_tracking(this->nav);
4867         if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
4868                 country_attr=&search_attr;
4869         if (country_attr) {
4870                 cs=country_search_new(country_attr, 0);
4871                 item=country_search_get_item(cs);
4872                 if (item && item_attr_get(item, attr_country_name, &country_name)) {
4873                         search_attr.type=attr_country_all;
4874                         dbg(0,"country %s\n", country_name.u.str);
4875                         search_attr.u.str=country_name.u.str;
4876                         search_list_search(this->sl, &search_attr, 0);
4877                         while((res=search_list_get_result(this->sl)));
4878                         if(this->country_iso2) {
4879                                 g_free(this->country_iso2);
4880                                 this->country_iso2=NULL;
4881                         }
4882                         if (item_attr_get(item, attr_country_iso2, &country_iso2))
4883                                 this->country_iso2=g_strdup(country_iso2.u.str);
4884                 }
4885                 country_search_destroy(cs);
4886         } else {
4887                 dbg(0,"warning: no default country found\n");
4888                 if (this->country_iso2) {
4889                     dbg(0,"attempting to use country '%s'\n",this->country_iso2);
4890                     search_attr.type=attr_country_iso2;
4891                     search_attr.u.str=this->country_iso2;
4892             search_list_search(this->sl, &search_attr, 0);
4893             while((res=search_list_get_result(this->sl)));
4894                 }
4895         }
4896 }
4897
4898 static void
4899 gui_internal_search_list_new(struct gui_priv *this)
4900 {
4901         struct mapset *ms=navit_get_mapset(this->nav);
4902         if (! this->sl) {
4903                 this->sl=search_list_new(ms);
4904                 gui_internal_search_list_set_default_country(this);
4905         }
4906 }
4907
4908 static void
4909 gui_internal_search_list_destroy(struct gui_priv *this)
4910 {
4911         if (this->sl) {
4912                 search_list_destroy(this->sl);
4913                 this->sl=NULL;
4914         }
4915 }
4916
4917
4918 static void
4919 gui_internal_search(struct gui_priv *this, char *what, char *type, int flags)
4920 {
4921         struct widget *wb,*wk,*w,*wr,*we,*wl,*wnext=NULL;
4922         char *country;
4923         int keyboard_mode=2;
4924         gui_internal_search_list_new(this);
4925         wb=gui_internal_menu(this, what);
4926         w=gui_internal_box_new(this, gravity_center|orientation_vertical|flags_expand|flags_fill);
4927         gui_internal_widget_append(wb, w);
4928         wr=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4929         gui_internal_widget_append(w, wr);
4930         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
4931         gui_internal_widget_append(wr, we);
4932         if (!strcmp(type,"Country")) {
4933                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_town"));
4934                 wnext->func=gui_internal_search_town;
4935         } else if (!strcmp(type,"Town")) {
4936                 if (this->country_iso2) {
4937 #if HAVE_API_ANDROID
4938                         char country_iso2[strlen(this->country_iso2)+1];
4939                         strtolower(country_iso2, this->country_iso2);
4940                         country=g_strdup_printf("country_%s", country_iso2);
4941 #else
4942                         country=g_strdup_printf("country_%s", this->country_iso2);
4943 #endif
4944                 } else
4945                         country=g_strdup("gui_select_country");
4946                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, country)));
4947                 wb->state |= STATE_SENSITIVE;
4948                 if (flags)
4949                         wb->func = gui_internal_search_country;
4950                 else
4951                         wb->func = gui_internal_back;
4952                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_street"));
4953                 wnext->func=gui_internal_search_street;
4954                 g_free(country);
4955         } else if (!strcmp(type,"Street")) {
4956                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_town")));
4957                 wb->state |= STATE_SENSITIVE;
4958                 wb->func = gui_internal_back;
4959                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_house_number"));
4960                 wnext->func=gui_internal_search_house_number;
4961         } else if (!strcmp(type,"House number")) {
4962                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_street")));
4963                 wb->state |= STATE_SENSITIVE;
4964                 wb->func = gui_internal_back;
4965                 keyboard_mode=18;
4966         }
4967         gui_internal_widget_append(we, wk=gui_internal_label_new(this, NULL));
4968         if (wnext) {
4969                 gui_internal_widget_append(we, wnext);
4970                 wnext->state |= STATE_SENSITIVE;
4971         }
4972         wl=gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);//gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
4973         gui_internal_widget_append(wr, wl);
4974         gui_internal_menu_data(this)->search_list=wl;
4975         wk->state |= STATE_EDIT|STATE_EDITABLE;
4976         wk->background=this->background;
4977         wk->flags |= flags_expand|flags_fill;
4978         wk->func = gui_internal_search_changed;
4979         wk->name=g_strdup(type);
4980         if (this->keyboard)
4981                 gui_internal_widget_append(w, gui_internal_keyboard(this,keyboard_mode));
4982         gui_internal_menu_render(this);
4983 }
4984
4985 static void
4986 gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data)
4987 {
4988         search_list_select(this->sl, attr_street_name, 0, 0);
4989         gui_internal_search(this,_("House number"),"House number",0);
4990 }
4991
4992 static void
4993 gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data)
4994 {
4995         dbg(0,"id %d\n", widget->selection_id);
4996         search_list_select(this->sl, attr_street_name, 0, 0);
4997         search_list_select(this->sl, attr_street_name, widget->selection_id, 1);
4998         gui_internal_search(this,_("House number"),"House number",0);
4999 }
5000
5001 static void
5002 gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data)
5003 {
5004         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
5005         gui_internal_search(this,_("Street"),"Street",0);
5006 }
5007
5008 static void
5009 gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data)
5010 {
5011         dbg(0,"id %d\n", widget->selection_id);
5012         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
5013         search_list_select(this->sl, attr_town_or_district_name, widget->selection_id, 1);
5014         gui_internal_search(this,_("Street"),"Street",0);
5015 }
5016
5017 static void
5018 gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data)
5019 {
5020         if (this->sl)
5021                 search_list_select(this->sl, attr_country_all, 0, 0);
5022         g_free(this->country_iso2);
5023         this->country_iso2=NULL;
5024         gui_internal_search(this,_("Town"),"Town",0);
5025 }
5026
5027 static void
5028 gui_internal_search_town_in_country(struct gui_priv *this, struct widget *widget)
5029 {
5030         struct search_list_common *slc;
5031         dbg(0,"id %d\n", widget->selection_id);
5032         search_list_select(this->sl, attr_country_all, 0, 0);
5033         slc=search_list_select(this->sl, attr_country_all, widget->selection_id, 1);
5034         if (slc) {
5035                 g_free(this->country_iso2);
5036                 this->country_iso2=g_strdup(((struct search_list_country *)slc)->iso2);
5037         }
5038         gui_internal_search(this,widget->name,"Town",0);
5039 }
5040
5041 static void
5042 gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data)
5043 {
5044         gui_internal_prune_menu_count(this, 1, 0);
5045         gui_internal_search(this,_("Country"),"Country",0);
5046 }
5047
5048 static void
5049 gui_internal_cmd2_town(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5050 {
5051         if (this->sl)
5052                 search_list_select(this->sl, attr_country_all, 0, 0);
5053         gui_internal_search(this,_("Town"),"Town",1);
5054 }
5055
5056 static void
5057 gui_internal_cmd2_setting_layout(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5058 {
5059         struct attr attr;
5060         struct widget *w,*wb,*wl,*row;
5061         struct attr_iter *iter;
5062
5063
5064         wb=gui_internal_menu(this, _("Layout"));
5065         w=gui_internal_widget_table_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill,1);
5066         gui_internal_widget_append(wb, w);
5067         iter=navit_attr_iter_new();
5068         while(navit_get_attr(this->nav, attr_layout, &attr, iter)) {
5069                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5070                 wl=gui_internal_button_navit_attr_new(this, attr.u.layout->name, gravity_left_center|orientation_horizontal|flags_fill,
5071                         &attr, NULL);
5072                 gui_internal_widget_append(row, wl);
5073         }
5074         navit_attr_iter_destroy(iter);
5075         gui_internal_menu_render(this);
5076 }
5077
5078 static char *
5079 gui_internal_cmd_match_expand(char *pattern, struct attr **in)
5080 {
5081         char p,*ret=g_strdup(pattern),*r=ret,*a;
5082         int len;
5083         while ((p=*pattern++)) {
5084                 switch (p) {
5085                 case '*':
5086                         *r='\0';
5087                         a=attr_to_text(*in++,NULL,0);
5088                         len=strlen(ret)+strlen(a)+strlen(pattern)+1;
5089                         r=g_malloc(len);
5090                         strcpy(r, ret);
5091                         strcat(r, a);
5092                         g_free(ret);
5093                         g_free(a);
5094                         ret=r;
5095                         r=ret+strlen(ret);
5096                         break;
5097                 case '\\':
5098                         p=*pattern++;
5099                 default:
5100                         *r++=p;
5101                 }       
5102         }
5103         *r++='\0';
5104         return ret;
5105 }
5106
5107 static int 
5108 gui_internal_match(const char *pattern, const char *string)
5109 {
5110         char p,s;
5111         while ((p=*pattern++)) {
5112                 switch (p) {
5113                 case '*':
5114                         while ((s=*string)) {
5115                                 if (gui_internal_match(pattern,string))
5116                                         return 1;
5117                                 string++;
5118                         }
5119                         break;
5120                 case '\\':
5121                         p=*pattern++;
5122                 default:
5123                         if (*string++ != p)
5124                                 return 0;
5125                 }
5126         }
5127         return 1;
5128 }
5129
5130 static int
5131 gui_internal_set(char *remove, char *add)
5132 {
5133         char *gui_file=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal.txt", NULL);
5134         char *gui_file_new=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal_new.txt", NULL);
5135         FILE *fo=fopen(gui_file_new,"w");
5136         FILE *fi=fopen(gui_file,"r");
5137         char *line=NULL;
5138         int ret;
5139         size_t size=0;
5140         if (fi != NULL){
5141                 while (getline(&line,&size,fi) > 0) {
5142                         int len=strlen(line);
5143                         if (len > 0 && line[len-1] == '\n')
5144                                 line[len-1]='\0';
5145                         dbg(1,"line=%s\n",line);
5146                         if (!gui_internal_match(remove, line))
5147                                 fprintf(fo,"%s\n",line);
5148                 }
5149                 if (line)
5150                         free(line);
5151                 fclose(fi);
5152         }
5153         fprintf(fo,"%s;\n",add);
5154         fclose(fo);
5155         ret=(rename(gui_file_new, gui_file)==0);
5156         g_free(gui_file_new);
5157         g_free(gui_file);
5158
5159         return ret;
5160 }
5161
5162 static void
5163 gui_internal_cmd2_set(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5164 {
5165         char *pattern,*command=NULL;
5166         if (!in || !in[0] || !ATTR_IS_STRING(in[0]->type)) {
5167                 dbg(0,"first parameter missing or wrong type\n");
5168                 return;
5169         }
5170         pattern=in[0]->u.str;
5171         dbg(0,"pattern %s\n",pattern);
5172         if (in[1]) {
5173                 command=gui_internal_cmd_match_expand(pattern, in+1);
5174                 dbg(0,"expand %s\n",command);
5175                 gui_internal_set(pattern, command);
5176                 command_evaluate(&this->self, command);
5177                 g_free(command);
5178         }
5179
5180 }
5181
5182 static void
5183 gui_internal_cmd2_quit(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5184 {
5185         struct attr navit;
5186         gui_internal_prune_menu(this, NULL);
5187         navit.type=attr_navit;
5188         navit.u.navit=this->nav;
5189         config_remove_attr(config, &navit);
5190         gui_internal_destroy(this);
5191         event_main_loop_quit();
5192 }
5193
5194 static void
5195 gui_internal_window_closed(struct gui_priv *this)
5196 {
5197         gui_internal_cmd2_quit(this, NULL, NULL, NULL, NULL);
5198 }
5199
5200 static void
5201 gui_internal_cmd2_abort_navigation(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5202 {
5203         navit_set_destination(this->nav, NULL, NULL, 0);
5204 }
5205
5206 static void
5207 gui_internal_cmd_map_download_do(struct gui_priv *this, struct widget *wm, void *data)
5208 {
5209         char *text=g_strdup_printf(_("Download %s"),wm->name);
5210         struct widget *w, *wb;
5211         struct map *map=data;
5212         double bllon,bllat,trlon,trlat;
5213
5214         wb=gui_internal_menu(this, text);
5215         g_free(text);
5216         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5217         w->spy=this->spacing*3;
5218         gui_internal_widget_append(wb, w);
5219         if (sscanf(wm->prefix,"%lf,%lf,%lf,%lf",&bllon,&bllat,&trlon,&trlat) == 4) {
5220                 struct coord_geo g;
5221                 struct map_selection sel;
5222                 struct map_rect *mr;
5223                 struct item *item;
5224
5225                 sel.next=NULL;
5226                 sel.order=255;
5227                 g.lng=bllon;
5228                 g.lat=trlat;
5229                 transform_from_geo(projection_mg, &g, &sel.u.c_rect.lu);
5230                 g.lng=trlon;
5231                 g.lat=bllat;
5232                 transform_from_geo(projection_mg, &g, &sel.u.c_rect.rl);
5233                 sel.range.min=type_none;
5234                 sel.range.max=type_last;
5235                 mr=map_rect_new(map, &sel);
5236                 while ((item=map_rect_get_item(mr))) {
5237                         dbg(0,"item\n");
5238                 }
5239                 map_rect_destroy(mr);
5240         }
5241         
5242         dbg(0,"bbox=%s\n",wm->prefix);
5243         gui_internal_menu_render(this);
5244 }
5245
5246 static void
5247 gui_internal_cmd_map_download(struct gui_priv *this, struct widget *wm, void *data)
5248 {
5249         struct attr on, off, download_enabled, download_disabled;
5250         struct widget *w,*wb,*wma;
5251         struct map *map=data;
5252         FILE *f;
5253         char *search,buffer[256];
5254         int found,sp_match=0;
5255
5256         dbg(1,"wm=%p prefix=%s\n",wm,wm->prefix);
5257
5258         search=wm->prefix;
5259         if (search) {
5260                 found=0;
5261                 while(search[sp_match] == ' ')
5262                         sp_match++;
5263                 sp_match++;
5264         } else {
5265                 found=1;
5266         }
5267         on.type=off.type=attr_active;
5268         on.u.num=1;
5269         off.u.num=0;
5270         wb=gui_internal_menu(this, wm->name?wm->name:_("Map Download"));
5271         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5272         w->spy=this->spacing*3;
5273         gui_internal_widget_append(wb, w);
5274         if (!search) {
5275                 wma=gui_internal_button_map_attr_new(this, _("Active"), gravity_left_center|orientation_horizontal|flags_fill, map, &on, &off, 1);
5276                 gui_internal_widget_append(w, wma);
5277         }
5278
5279         download_enabled.type=download_disabled.type=attr_update;
5280         download_enabled.u.num=1;
5281         download_disabled.u.num=0;
5282         wma=gui_internal_button_map_attr_new(this
5283                 , _("Download Enabled")
5284                 , gravity_left_center|orientation_horizontal|flags_fill
5285                 , map
5286                 , &download_enabled
5287                 , &download_disabled
5288                 , 0);
5289         gui_internal_widget_append(w, wma);
5290
5291
5292         f=fopen("maps/areas.tsv","r");
5293         while (f && fgets(buffer, sizeof(buffer), f)) {
5294                 char *nl,*description,*description_size,*bbox,*size=NULL;
5295                 int sp=0;
5296                 if ((nl=strchr(buffer,'\n')))
5297                         *nl='\0';
5298                 if ((nl=strchr(buffer,'\r')))
5299                         *nl='\0';
5300                 while(buffer[sp] == ' ')
5301                         sp++;
5302                 if ((bbox=strchr(buffer,'\t')))
5303                         *bbox++='\0';
5304                 if (bbox && (size=strchr(bbox,'\t'))) 
5305                         *size++='\0';
5306                 if (search && !strcmp(buffer, search)) {
5307                         wma=gui_internal_button_new_with_callback(this, _("Download completely"), NULL, 
5308                                 gravity_left_center|orientation_horizontal|flags_fill, gui_internal_cmd_map_download_do, map);
5309                         wma->name=g_strdup(buffer+sp);
5310                         wma->prefix=g_strdup(bbox);
5311                         gui_internal_widget_append(w, wma);
5312                         found=1;
5313                 } else if (sp < sp_match)
5314                         found=0;
5315                 if (sp == sp_match && found && buffer[sp]) {
5316                         description=g_strdup(buffer+sp);
5317                         if (size)
5318                                 description_size=g_strdup_printf("%s (%s)",description,size);
5319                         else
5320                                 description_size=g_strdup(description);
5321                         wma=gui_internal_button_new_with_callback(this, description_size, NULL, 
5322                                 gravity_left_center|orientation_horizontal|flags_fill, gui_internal_cmd_map_download, map);
5323                         g_free(description_size);
5324                         wma->prefix=g_strdup(buffer);
5325                         wma->name=description;
5326                         gui_internal_widget_append(w, wma);
5327                 }
5328         }
5329         
5330         gui_internal_menu_render(this);
5331 }
5332
5333 static void
5334 gui_internal_cmd2_setting_maps(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5335 {
5336         struct attr attr, on, off, description, type, data, url, active;
5337         struct widget *w,*wb,*row,*wma;
5338         char *label;
5339         struct attr_iter *iter;
5340
5341         wb=gui_internal_menu(this, _("Maps"));
5342         //w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5343         //w->spy=this->spacing*3;
5344         w = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
5345         gui_internal_widget_append(wb, w);
5346         iter=navit_attr_iter_new();
5347         on.type=off.type=attr_active;
5348         on.u.num=1;
5349         off.u.num=0;
5350         while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
5351                 if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
5352                         label=g_strdup(description.u.str);
5353                 } else {
5354                         if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
5355                                 type.u.str="";
5356                         if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
5357                                 data.u.str="";
5358                         label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
5359                 }
5360                 if (map_get_attr(attr.u.map, attr_url, &url, NULL)) {
5361                         if (!map_get_attr(attr.u.map, attr_active, &active, NULL))
5362                                 active.u.num=1;
5363                         wma=gui_internal_button_new_with_callback(this, label, image_new_xs(this, active.u.num ? "gui_active" : "gui_inactive"),
5364                         gravity_left_center|orientation_horizontal|flags_fill,
5365                         gui_internal_cmd_map_download, attr.u.map);
5366                 } else {
5367                         wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
5368                                 attr.u.map, &on, &off, 1);
5369                 }       
5370                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5371                 gui_internal_widget_append(row, wma);
5372                 g_free(label);
5373         }
5374         navit_attr_iter_destroy(iter);
5375         gui_internal_menu_render(this);
5376
5377 }
5378 static void
5379 gui_internal_cmd_set_active_vehicle(struct gui_priv *this, struct widget *wm, void *data)
5380 {
5381         struct attr vehicle = {attr_vehicle,{wm->data}};
5382         navit_set_attr(this->nav, &vehicle);
5383 }
5384
5385 static void
5386 gui_internal_cmd_show_satellite_status(struct gui_priv *this, struct widget *wm, void *data)
5387 {
5388         struct widget *w,*wb,*row;
5389         struct attr attr,sat_attr;
5390         struct vehicle *v=wm->data;
5391         char *str;
5392         int i;
5393         enum attr_type types[]={attr_sat_prn, attr_sat_elevation, attr_sat_azimuth, attr_sat_snr};
5394
5395         wb=gui_internal_menu(this, _("Show Satellite Status"));
5396         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_satellite_status;
5397         gui_internal_menu_data(this)->redisplay_widget=wm;
5398         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5399         gui_internal_widget_append(wb, w);
5400         w = gui_internal_widget_table_new(this,gravity_center | orientation_vertical | flags_expand | flags_fill, 0);
5401         row = gui_internal_widget_table_row_new(this,gravity_left_top);
5402         gui_internal_widget_append(row, gui_internal_label_new(this, " PRN "));
5403         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Elevation ")));
5404         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Azimuth ")));
5405         gui_internal_widget_append(row, gui_internal_label_new(this, " SNR "));
5406         gui_internal_widget_append(w,row);
5407         while (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
5408                 row = gui_internal_widget_table_row_new(this,gravity_left_top);
5409                 for (i = 0 ; i < sizeof(types)/sizeof(enum attr_type) ; i++) {
5410                         if (item_attr_get(attr.u.item, types[i], &sat_attr))
5411                                 str=g_strdup_printf("%ld", sat_attr.u.num);
5412                         else
5413                                 str=g_strdup("");
5414                         gui_internal_widget_append(row, gui_internal_label_new(this, str));
5415                         g_free(str);
5416                 }
5417                 gui_internal_widget_append(w,row);
5418         }
5419         gui_internal_widget_append(wb, w);
5420         gui_internal_menu_render(this);
5421 }
5422
5423 static void
5424 gui_internal_cmd_show_nmea_data(struct gui_priv *this, struct widget *wm, void *data)
5425 {
5426         struct widget *w,*wb;
5427         struct attr attr;
5428         struct vehicle *v=wm->data;
5429         wb=gui_internal_menu(this, _("Show NMEA Data"));
5430         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_nmea_data;
5431         gui_internal_menu_data(this)->redisplay_widget=wm;
5432         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5433         gui_internal_widget_append(wb, w);
5434         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL))
5435                 gui_internal_widget_append(w, gui_internal_text_new(this, attr.u.str, gravity_left_center|orientation_vertical));
5436         gui_internal_menu_render(this);
5437 }
5438
5439 /**
5440  * A container to hold the selected vehicle and the desired profile in
5441  * one data item.
5442  */
5443 struct vehicle_and_profilename {
5444         struct vehicle *vehicle;
5445         char *profilename;
5446 };
5447
5448 /**
5449  * Figures out whether the given vehicle is the active vehicle.
5450  *
5451  * @return true if the vehicle is active, false otherwise.
5452  */
5453 static int
5454 gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle
5455         *vehicle)
5456 {
5457         struct attr active_vehicle;
5458
5459         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
5460         active_vehicle.u.vehicle=NULL;
5461
5462         return active_vehicle.u.vehicle == vehicle;
5463 }
5464
5465 static void
5466 save_vehicle_xml(struct vehicle *v)
5467 {
5468         struct attr attr;
5469         struct attr_iter *iter=vehicle_attr_iter_new();
5470         int childs=0;
5471         dbg(0,"enter\n");
5472         printf("<vehicle");
5473         while (vehicle_get_attr(v, attr_any_xml, &attr, iter)) {
5474                 if (ATTR_IS_OBJECT(attr.type))
5475                         childs=1;
5476                 else    {
5477                         char *attrtxt;
5478                         printf(" %s=\"%s\"",attr_to_name(attr.type),attrtxt=attr_to_text(&attr, NULL, 1));
5479                         g_free(attrtxt);
5480                 }
5481         }
5482         if (childs) {
5483                 printf(">\n");
5484                 printf("</vehicle>\n");
5485         } else
5486                 printf(" />\n");
5487         vehicle_attr_iter_destroy(iter);
5488 }
5489
5490
5491 /**
5492  * Reacts to a button press that changes a vehicle's active profile.
5493  *
5494  * @see gui_internal_add_vehicle_profile
5495  */
5496 static void
5497 gui_internal_cmd_set_active_profile(struct gui_priv *this, struct
5498                 widget *wm, void *data)
5499 {
5500         struct vehicle_and_profilename *vapn = data;
5501         struct vehicle *v = vapn->vehicle;
5502         char *profilename = vapn->profilename;
5503         struct attr vehicle_name_attr;
5504         char *vehicle_name = NULL;
5505         struct attr profilename_attr;
5506
5507         // Get the vehicle name
5508         vehicle_get_attr(v, attr_name, &vehicle_name_attr, NULL);
5509         vehicle_name = vehicle_name_attr.u.str;
5510
5511         dbg(0, "Changing vehicle %s to profile %s\n", vehicle_name,
5512                         profilename);
5513
5514         // Change the profile name
5515         profilename_attr.type = attr_profilename;
5516         profilename_attr.u.str = profilename;
5517         if(!vehicle_set_attr(v, &profilename_attr)) {
5518                 dbg(0, "Unable to set the vehicle's profile name\n");
5519         }
5520
5521     // Notify Navit that the routing should be re-done if this is the
5522     // active vehicle.
5523         if (gui_internal_is_active_vehicle(this, v)) {
5524                 struct attr vehicle;
5525                 vehicle.type=attr_vehicle;
5526                 vehicle.u.vehicle=v;
5527                 navit_set_attr(this->nav, &vehicle);
5528         }
5529         save_vehicle_xml(v);
5530         
5531         gui_internal_prune_menu_count(this, 1, 0);
5532         gui_internal_menu_vehicle_settings(this, v, vehicle_name);
5533 }
5534
5535 /**
5536  * Adds the vehicle profile to the GUI, allowing the user to pick a
5537  * profile for the currently selected vehicle.
5538  */
5539 static void
5540 gui_internal_add_vehicle_profile(struct gui_priv *this, struct widget
5541                 *parent, struct vehicle *v, struct vehicleprofile *profile)
5542 {
5543         // Just here to show up in the translation file, nice and close to
5544         // where the translations are actually used.
5545         struct attr profile_attr;
5546         struct attr *attr = NULL;
5547         char *name = NULL;
5548         char *active_profile = NULL;
5549         char *label = NULL;
5550         int active;
5551         struct vehicle_and_profilename *context = NULL;
5552
5553 #ifdef ONLY_FOR_TRANSLATION
5554         char *translations[] = {_n("car"), _n("bike"), _n("pedestrian")};
5555 #endif
5556
5557         // Figure out the profile name
5558         attr = attr_search(profile->attrs, NULL, attr_name);
5559         if (!attr) {
5560                 dbg(0, "Adding vehicle profile failed. attr==NULL");
5561                 return;
5562         }
5563         name = attr->u.str;
5564
5565         // Determine whether the profile is the active one
5566         if (vehicle_get_attr(v, attr_profilename, &profile_attr, NULL))
5567                 active_profile = profile_attr.u.str;
5568         active = active_profile != NULL && !strcmp(name, active_profile);
5569
5570         dbg(0, "Adding vehicle profile %s, active=%s/%i\n", name,
5571                         active_profile, active);
5572
5573         // Build a translatable label.
5574         if(active) {
5575                 label = g_strdup_printf(_("Current profile: %s"), _(name));
5576         } else {
5577                 label = g_strdup_printf(_("Change profile to: %s"), _(name));
5578         }
5579
5580         // Create the context object (the vehicle and the desired profile)
5581         context = g_new0(struct vehicle_and_profilename, 1);
5582         context->vehicle = v;
5583         context->profilename = name;
5584
5585         // Add the button
5586         gui_internal_widget_append(parent,
5587                 gui_internal_button_new_with_callback(
5588                         this, label,
5589                         image_new_xs(this, active ? "gui_active" : "gui_inactive"),
5590                         gravity_left_center|orientation_horizontal|flags_fill,
5591                         gui_internal_cmd_set_active_profile, context));
5592
5593         free(label);
5594 }
5595
5596 static void
5597 gui_internal_menu_vehicle_settings(struct gui_priv *this, struct vehicle *v, char *name)
5598 {
5599         struct widget *w,*wb,*row;
5600         struct attr attr;
5601     struct vehicleprofile *profile = NULL;
5602         GList *profiles;
5603
5604         wb=gui_internal_menu(this, name);
5605         w=gui_internal_widget_table_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill,1);
5606         gui_internal_widget_append(wb, w);
5607
5608     // Add the "Set as active" button if this isn't the active
5609     // vehicle.
5610         if (!gui_internal_is_active_vehicle(this, v)) {
5611                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5612                 gui_internal_widget_append(row,
5613                         gui_internal_button_new_with_callback(this, _("Set as active"),
5614                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5615                                 gui_internal_cmd_set_active_vehicle, v));
5616         }
5617
5618         if (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
5619                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5620                 gui_internal_widget_append(row,
5621                         gui_internal_button_new_with_callback(this, _("Show Satellite status"),
5622                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5623                                 gui_internal_cmd_show_satellite_status, v));
5624         }
5625         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL)) {
5626                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5627                 gui_internal_widget_append(row,
5628                         gui_internal_button_new_with_callback(this, _("Show NMEA data"),
5629                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5630                                 gui_internal_cmd_show_nmea_data, v));
5631         }
5632
5633     // Add all the possible vehicle profiles to the menu
5634         profiles = navit_get_vehicleprofiles(this->nav);
5635     while(profiles) {
5636         profile = (struct vehicleprofile *)profiles->data;
5637         gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5638         gui_internal_add_vehicle_profile(this, row, v, profile);
5639                 profiles = g_list_next(profiles);
5640     }
5641
5642         callback_list_call_attr_2(this->cbl, attr_vehicle, w, v);
5643         gui_internal_menu_render(this);
5644 }
5645
5646 static void
5647 gui_internal_cmd_vehicle_settings(struct gui_priv *this, struct widget *wm, void *data)
5648 {
5649         gui_internal_menu_vehicle_settings(this, wm->data, wm->text);
5650 }
5651
5652 static void
5653 gui_internal_cmd2_setting_vehicle(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5654 {
5655         struct attr attr,attr2,vattr;
5656         struct widget *w,*wb,*wl;
5657         struct attr_iter *iter;
5658         struct attr active_vehicle;
5659     
5660         iter=navit_attr_iter_new();
5661         if (navit_get_attr(this->nav, attr_vehicle, &attr, iter) && !navit_get_attr(this->nav, attr_vehicle, &attr2, iter)) {
5662                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
5663                 navit_attr_iter_destroy(iter);
5664                 gui_internal_menu_vehicle_settings(this, attr.u.vehicle, vattr.u.str);
5665                 return;
5666         }
5667         navit_attr_iter_destroy(iter);
5668
5669         wb=gui_internal_menu(this, _("Vehicle"));
5670         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5671         w->spy=this->spacing*3;
5672         gui_internal_widget_append(wb, w);
5673         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
5674                 active_vehicle.u.vehicle=NULL;
5675         iter=navit_attr_iter_new();
5676         while(navit_get_attr(this->nav, attr_vehicle, &attr, iter)) {
5677                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
5678                 wl=gui_internal_button_new_with_callback(this, vattr.u.str,
5679                         image_new_xs(this, attr.u.vehicle == active_vehicle.u.vehicle ? "gui_active" : "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
5680                         gui_internal_cmd_vehicle_settings, attr.u.vehicle);
5681                 wl->text=g_strdup(vattr.u.str);
5682                 gui_internal_widget_append(w, wl);
5683         }
5684         navit_attr_iter_destroy(iter);
5685         gui_internal_menu_render(this);
5686 }
5687
5688
5689 static void
5690 gui_internal_cmd2_setting_rules(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5691 {
5692         struct widget *wb,*w;
5693         struct attr on,off;
5694         wb=gui_internal_menu(this, _("Rules"));
5695         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5696         w->spy=this->spacing*3;
5697         gui_internal_widget_append(wb, w);
5698         on.u.num=1;
5699         off.u.num=0;
5700         on.type=off.type=attr_tracking;
5701         gui_internal_widget_append(w,
5702                 gui_internal_button_navit_attr_new(this, _("Lock on road"), gravity_left_center|orientation_horizontal|flags_fill,
5703                         &on, &off));
5704         on.u.num=0;
5705         off.u.num=-1;
5706         on.type=off.type=attr_orientation;
5707         gui_internal_widget_append(w,
5708                 gui_internal_button_navit_attr_new(this, _("Northing"), gravity_left_center|orientation_horizontal|flags_fill,
5709                         &on, &off));
5710         on.u.num=1;
5711         off.u.num=0;
5712         on.type=off.type=attr_follow_cursor;
5713         gui_internal_widget_append(w,
5714                 gui_internal_button_navit_attr_new(this, _("Map follows Vehicle"), gravity_left_center|orientation_horizontal|flags_fill,
5715                         &on, &off));
5716         on.u.num=1;
5717         off.u.num=0;
5718         on.type=off.type=attr_waypoints_flag;
5719         gui_internal_widget_append(w,
5720                         gui_internal_button_navit_attr_new(this, _("Plan with Waypoints"), gravity_left_center|orientation_horizontal|flags_fill,
5721                                         &on, &off));
5722         gui_internal_menu_render(this);
5723 }
5724
5725 //##############################################################################################################
5726 //# Description:
5727 //# Comment:
5728 //# Authors: Martin Schaller (04/2008)
5729 //##############################################################################################################
5730 static void gui_internal_motion(void *data, struct point *p)
5731 {
5732
5733         struct gui_priv *this=data;
5734         if (!this->root.children) {
5735                 navit_handle_motion(this->nav, p);
5736                 return;
5737         }
5738         if (!this->pressed)
5739                 return;
5740         this->current=*p;
5741         if(!this->motion_timeout_callback)
5742                 this->motion_timeout_callback=callback_new_1(callback_cast(gui_internal_motion_cb), this);
5743         if(!this->motion_timeout_event)
5744                 this->motion_timeout_event=event_add_timeout(30,0, this->motion_timeout_callback);
5745 }
5746
5747 static const char *
5748 find_attr(const char **names, const char **values, const char *name)
5749 {
5750         while (*names) {
5751                 if (!g_strcasecmp(*names, name))
5752                         return *values;
5753                 names+=xml_attr_distance;
5754                 values+=xml_attr_distance;
5755         }
5756         return NULL;
5757 }
5758
5759 static char *
5760 find_attr_dup(const char **names, const char **values, const char *name)
5761 {
5762         return g_strdup(find_attr(names, values, name));
5763 }
5764
5765 static void
5766 gui_internal_evaluate(struct gui_priv *this, const char *command)
5767 {
5768         if (command)
5769                 command_evaluate(&this->self, command);
5770 }
5771
5772
5773 static void
5774 gui_internal_html_command(struct gui_priv *this, struct widget *w, void *data)
5775 {
5776         gui_internal_evaluate(this,w->command);
5777 }
5778
5779 static void
5780 gui_internal_html_submit_set(struct gui_priv *this, struct widget *w, struct form *form)
5781 {
5782         GList *l;
5783         if (w->form == form && w->name) {
5784                 struct attr *attr=attr_new_from_text(w->name, w->text?w->text:"");
5785                 if (attr)
5786                         gui_set_attr(this->self.u.gui, attr);
5787                 attr_free(attr);
5788         }
5789         l=w->children;
5790         while (l) {
5791                 w=l->data;
5792                 gui_internal_html_submit_set(this, w, form);
5793                 l=g_list_next(l);
5794         }
5795
5796 }
5797
5798 static void
5799 gui_internal_html_submit(struct gui_priv *this, struct widget *w, void *data)
5800 {
5801         struct widget *menu;
5802         GList *l;
5803
5804         dbg(1,"enter form %p %s\n",w->form,w->form->onsubmit);
5805         l=g_list_last(this->root.children);
5806         menu=l->data;
5807         graphics_draw_mode(this->gra, draw_mode_begin);
5808         gui_internal_highlight_do(this, NULL);
5809         gui_internal_menu_render(this);
5810         graphics_draw_mode(this->gra, draw_mode_end);
5811         gui_internal_html_submit_set(this, menu, w->form);
5812         gui_internal_evaluate(this,w->form->onsubmit);
5813 }
5814
5815 static void
5816 gui_internal_html_load_href(struct gui_priv *this, char *href, int replace)
5817 {
5818         if (replace)
5819                 gui_internal_prune_menu_count(this, 1, 0);
5820         if (href && href[0] == '#') {
5821                 dbg(1,"href=%s\n",href);
5822                 g_free(this->href);
5823                 this->href=g_strdup(href);
5824                 gui_internal_html_menu(this, this->html_text, href+1);
5825         }
5826 }
5827
5828 static void
5829 gui_internal_html_href(struct gui_priv *this, struct widget *w, void *data)
5830 {
5831         gui_internal_html_load_href(this, w->command, 0);
5832 }
5833
5834 struct div_flags_map {
5835         char *attr;
5836         char *val;
5837         enum flags flags;
5838 } div_flags_map[] = {
5839         {"gravity","none",gravity_none},
5840         {"gravity","left",gravity_left},
5841         {"gravity","xcenter",gravity_xcenter},
5842         {"gravity","right",gravity_right},
5843         {"gravity","top",gravity_top},
5844         {"gravity","ycenter",gravity_ycenter},
5845         {"gravity","bottom",gravity_bottom},
5846         {"gravity","left_top",gravity_left_top},
5847         {"gravity","top_center",gravity_top_center},
5848         {"gravity","right_top",gravity_right_top},
5849         {"gravity","left_center",gravity_left_center},
5850         {"gravity","center",gravity_center},
5851         {"gravity","right_center",gravity_right_center},
5852         {"gravity","left_bottom",gravity_left_bottom},
5853         {"gravity","bottom_center",gravity_bottom_center},
5854         {"gravity","right_bottom",gravity_right_bottom},
5855         {"expand","1",flags_expand},
5856         {"fill","1",flags_fill},
5857         {"orientation","horizontal",orientation_horizontal},
5858         {"orientation","vertical",orientation_vertical},
5859         {"orientation","horizontal_vertical",orientation_horizontal_vertical},
5860 };
5861
5862 static enum flags
5863 div_flag(const char **names, const char **values, char *name)
5864 {
5865         int i;
5866         enum flags ret=0;
5867         const char *value=find_attr(names, values, name);
5868         if (!value)
5869                 return ret;
5870         for (i = 0 ; i < sizeof(div_flags_map)/sizeof(struct div_flags_map); i++) {
5871                 if (!strcmp(div_flags_map[i].attr,name) && !strcmp(div_flags_map[i].val,value))
5872                         ret|=div_flags_map[i].flags;
5873         }
5874         return ret;
5875 }
5876
5877 static enum flags
5878 div_flags(const char **names, const char **values)
5879 {
5880         enum flags flags;
5881         flags = div_flag(names, values, "gravity");
5882         flags |= div_flag(names, values, "orientation");
5883         flags |= div_flag(names, values, "expand");
5884         flags |= div_flag(names, values, "fill");
5885         return flags;
5886 }
5887
5888 static struct widget *
5889 html_image(struct gui_priv *this, const char **names, const char **values)
5890 {
5891         const char *src, *size;
5892         struct graphics_image *img=NULL;
5893
5894         src=find_attr(names, values, "src");
5895         if (!src)
5896                 return NULL;
5897         size=find_attr(names, values, "size");
5898         if (!size)
5899                 size="l";
5900         if (!strcmp(size,"l"))
5901                 img=image_new_l(this, src);
5902         else if (!strcmp(size,"s"))
5903                 img=image_new_s(this, src);
5904         else if (!strcmp(size,"xs"))
5905                 img=image_new_xs(this, src);
5906         if (!img)
5907                 return NULL;
5908         return gui_internal_image_new(this, img);
5909 }
5910
5911 static void
5912 gui_internal_html_start(void *dummy, const char *tag_name, const char **names, const char **values, void *data, void *error)
5913 {
5914         struct gui_priv *this=data;
5915         int i;
5916         enum html_tag tag=html_tag_none;
5917         struct html *html=&this->html[this->html_depth];
5918         const char *cond, *type;
5919
5920         if (!g_strcasecmp(tag_name,"text"))
5921                 return;
5922         html->skip=0;
5923         html->command=NULL;
5924         html->name=NULL;
5925         html->href=NULL;
5926         html->refresh_cond=NULL;
5927         html->w=NULL;
5928         html->container=NULL;
5929         cond=find_attr(names, values, "cond");
5930
5931         if (cond && !this->html_skip) {
5932                 if (!command_evaluate_to_boolean(&this->self, cond, NULL))
5933                         html->skip=1;
5934         }
5935
5936         for (i=0 ; i < sizeof(html_tag_map)/sizeof(struct html_tag_map); i++) {
5937                 if (!g_strcasecmp(html_tag_map[i].tag_name, tag_name)) {
5938                         tag=html_tag_map[i].tag;
5939                         break;
5940                 }
5941         }
5942         html->tag=tag;
5943         if (!this->html_skip && !html->skip) {
5944                 switch (tag) {
5945                 case html_tag_a:
5946                         html->name=find_attr_dup(names, values, "name");
5947                         if (html->name) {
5948                                 html->skip=this->html_anchor ? strcmp(html->name,this->html_anchor) : 0;
5949                                 if (!html->skip)
5950                                         this->html_anchor_found=1;
5951                         }
5952                         html->command=find_attr_dup(names, values, "onclick");
5953                         html->href=find_attr_dup(names, values, "href");
5954                         html->refresh_cond=find_attr_dup(names, values, "refresh_cond");
5955                         break;
5956                 case html_tag_img:
5957                         html->command=find_attr_dup(names, values, "onclick");
5958                         html->w=html_image(this, names, values);
5959                         break;
5960                 case html_tag_form:
5961                         this->form=g_new0(struct form, 1);
5962                         this->form->onsubmit=find_attr_dup(names, values, "onsubmit");
5963                         break;
5964                 case html_tag_input:
5965                         type=find_attr_dup(names, values, "type");
5966                         if (!type)
5967                                 break;
5968                         if (!strcmp(type,"image")) {
5969                                 html->w=html_image(this, names, values);
5970                                 if (html->w) {
5971                                         html->w->state|=STATE_SENSITIVE;
5972                                         html->w->func=gui_internal_html_submit;
5973                                 }
5974                         }
5975                         if (!strcmp(type,"text") || !strcmp(type,"password")) {
5976                                 html->w=gui_internal_label_new(this, NULL);
5977                                 html->w->background=this->background;
5978                                 html->w->flags |= div_flags(names, values);
5979                                 html->w->state|=STATE_EDITABLE;
5980                                 if (!this->editable) {
5981                                         this->editable=html->w;
5982                                         html->w->state|=STATE_EDIT;
5983                                 }
5984                                 this->keyboard_required=1;
5985                                 if (!strcmp(type,"password"))
5986                                         html->w->flags2 |= 1;
5987                         }
5988                         if (html->w) {
5989                                 html->w->form=this->form;
5990                                 html->w->name=find_attr_dup(names, values, "name");
5991                         }
5992                         break;
5993                 case html_tag_div:
5994                         html->w=gui_internal_box_new(this, div_flags(names, values));
5995                         html->container=this->html_container;
5996                         this->html_container=html->w;
5997                         break;
5998                 default:
5999                         break;
6000                 }
6001         }
6002         this->html_skip+=html->skip;
6003         this->html_depth++;
6004 }
6005
6006 static void
6007 gui_internal_html_end(void *dummy, const char *tag_name, void *data, void *error)
6008 {
6009         struct gui_priv *this=data;
6010         struct html *html;
6011         struct html *parent=NULL;
6012
6013         if (!g_strcasecmp(tag_name,"text"))
6014                 return;
6015         this->html_depth--;
6016         html=&this->html[this->html_depth];
6017         if (this->html_depth > 0)
6018                 parent=&this->html[this->html_depth-1];
6019
6020
6021         if (!this->html_skip) {
6022                 if (html->command && html->w) {
6023                         html->w->state |= STATE_SENSITIVE;
6024                         html->w->command=html->command;
6025                         html->w->func=gui_internal_html_command;
6026                         html->command=NULL;
6027                 }
6028                 if (parent && (parent->href || parent->command) && html->w) {
6029                         html->w->state |= STATE_SENSITIVE;
6030                         if (parent->command) {
6031                                 html->w->command=g_strdup(parent->command);
6032                                 html->w->func=gui_internal_html_command;
6033                         } else {
6034                                 html->w->command=g_strdup(parent->href);
6035                                 html->w->func=gui_internal_html_href;
6036                         }
6037                 }
6038                 switch (html->tag) {
6039                 case html_tag_div:
6040                         this->html_container=html->container;
6041                 case html_tag_img:
6042                 case html_tag_input:
6043                         gui_internal_widget_append(this->html_container, html->w);
6044                         break;
6045                 case html_tag_form:
6046                         this->form=NULL;
6047                         break;
6048                 default:
6049                         break;
6050                 }
6051         }
6052         this->html_skip-=html->skip;
6053         g_free(html->command);
6054         g_free(html->name);
6055         g_free(html->href);
6056         g_free(html->refresh_cond);
6057 }
6058
6059 static void
6060 gui_internal_refresh_callback_called(struct gui_priv *this, struct menu_data *menu_data)
6061 {
6062         if (gui_internal_menu_data(this) == menu_data) {
6063                 char *href=g_strdup(menu_data->href);
6064                 gui_internal_html_load_href(this, href, 1);
6065                 g_free(href);
6066         }
6067 }
6068
6069 static void
6070 gui_internal_set_refresh_callback(struct gui_priv *this, char *cond)
6071 {
6072         dbg(0,"cond=%s\n",cond);
6073         if (cond) {
6074                 enum attr_type type;
6075                 struct object_func *func;
6076                 struct menu_data *menu_data=gui_internal_menu_data(this);
6077                 dbg(0,"navit=%p\n",this->nav);
6078                 type=command_evaluate_to_attr(&this->self, cond, NULL, &menu_data->refresh_callback_obj);
6079                 if (type == attr_none)
6080                         return;
6081                 func=object_func_lookup(menu_data->refresh_callback_obj.type);
6082                 if (!func || !func->add_attr)
6083                         return;
6084                 menu_data->refresh_callback.type=attr_callback;
6085                 menu_data->refresh_callback.u.callback=callback_new_attr_2(callback_cast(gui_internal_refresh_callback_called),type,this,menu_data);
6086                 func->add_attr(menu_data->refresh_callback_obj.u.data, &menu_data->refresh_callback);
6087         }
6088 }
6089
6090 static void
6091 gui_internal_html_text(void *dummy, const char *text, int len, void *data, void *error)
6092 {
6093         struct gui_priv *this=data;
6094         struct widget *w;
6095         int depth=this->html_depth-1;
6096         struct html *html=&this->html[depth];
6097         gchar *text_stripped;
6098
6099         if (this->html_skip)
6100                 return;
6101         while (isspace(text[0])) {
6102                 text++;
6103                 len--;
6104         }
6105         while (len > 0 && isspace(text[len-1]))
6106                 len--;
6107
6108         text_stripped = g_strndup(text, len);
6109         if (html->tag == html_tag_html && depth > 2) {
6110                 if (this->html[depth-1].tag == html_tag_script) {
6111                         html=&this->html[depth-2];
6112                 }
6113         }
6114         switch (html->tag) {
6115         case html_tag_a:
6116                 if (html->name && len) {
6117                         this->html_container=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
6118                         gui_internal_widget_append(gui_internal_menu(this, _(text_stripped)), this->html_container);
6119                         gui_internal_menu_data(this)->href=g_strdup(this->href);
6120                         gui_internal_set_refresh_callback(this, html->refresh_cond);
6121                         this->html_container->spx=this->spacing*10;
6122                 }
6123                 break;
6124         case html_tag_h1:
6125                 if (!this->html_container) {
6126                         this->html_container=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
6127                         gui_internal_widget_append(gui_internal_menu(this, _(text_stripped)), this->html_container);
6128                         this->html_container->spx=this->spacing*10;
6129                 }
6130                 break;
6131         case html_tag_img:
6132                 if (len) {
6133                         w=gui_internal_box_new(this, gravity_center|orientation_vertical);
6134                         gui_internal_widget_append(w, html->w);
6135                         gui_internal_widget_append(w, gui_internal_text_new(this, _(text_stripped), gravity_center|orientation_vertical));
6136                         html->w=w;
6137                 }
6138                 break;
6139         case html_tag_div:
6140                 if (len) {
6141                         gui_internal_widget_append(html->w, gui_internal_text_new(this, _(text_stripped), gravity_center|orientation_vertical));
6142                 }
6143                 break;
6144         case html_tag_script:
6145                 dbg(1,"execute %s\n",text_stripped);
6146                 gui_internal_evaluate(this,text_stripped);
6147                 break;
6148         default:
6149                 break;
6150         }
6151         g_free(text_stripped);
6152 }
6153
6154 static void
6155 gui_internal_html_menu(struct gui_priv *this, const char *document, char *anchor)
6156 {
6157         char *doc=g_strdup(document);
6158         graphics_draw_mode(this->gra, draw_mode_begin);
6159         this->html_container=NULL;
6160         this->html_depth=0;
6161         this->html_anchor=anchor;
6162         this->html_anchor_found=0;
6163         this->form=NULL;
6164         this->keyboard_required=0;
6165         this->editable=NULL;
6166         callback_list_call_attr_2(this->cbl,attr_gui,anchor,&doc);
6167         xml_parse_text(doc, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
6168         g_free(doc);
6169         if (this->keyboard_required && this->keyboard) {
6170                 this->html_container->flags=gravity_center|orientation_vertical|flags_expand|flags_fill;
6171                 gui_internal_widget_append(this->html_container, gui_internal_keyboard(this,2));
6172         }
6173         gui_internal_menu_render(this);
6174         graphics_draw_mode(this->gra, draw_mode_end);
6175 }
6176
6177
6178 static void
6179 gui_internal_enter(struct gui_priv *this, int ignore)
6180 {
6181         struct graphics *gra=this->gra;
6182         if (ignore != -1) 
6183                 this->ignore_button=ignore;
6184
6185         navit_block(this->nav, 1);
6186         graphics_overlay_disable(gra, 1);
6187         this->root.p.x=0;
6188         this->root.p.y=0;
6189         this->root.background=this->background;
6190 }
6191
6192 static void
6193 gui_internal_leave(struct gui_priv *this)
6194 {
6195         graphics_draw_mode(this->gra, draw_mode_end);
6196 }
6197
6198 static void
6199 gui_internal_set_click_coord(struct gui_priv *this, struct point *p)
6200 {
6201         struct coord c;
6202         struct coord_geo g;
6203         struct attr attr;
6204         struct transformation *trans;
6205         attr_free(this->click_coord_geo);
6206         this->click_coord_geo=NULL;
6207         if (p) {
6208                 trans=navit_get_trans(this->nav);
6209                 transform_reverse(trans, p, &c);
6210                 dbg(1,"x=0x%x y=0x%x\n", c.x, c.y);
6211                 this->clickp.pro=transform_get_projection(trans);
6212                 this->clickp.x=c.x;
6213                 this->clickp.y=c.y;
6214                 transform_to_geo(this->clickp.pro, &c, &g);
6215                 attr.u.coord_geo=&g;
6216                 attr.type=attr_click_coord_geo;
6217                 this->click_coord_geo=attr_dup(&attr);
6218         }
6219 }
6220
6221 static void
6222 gui_internal_set_position_coord(struct gui_priv *this)
6223 {
6224         struct transformation *trans;
6225         struct attr attr,attrp;
6226         struct coord c;
6227
6228         attr_free(this->position_coord_geo);
6229         this->position_coord_geo=NULL;
6230         if (navit_get_attr(this->nav, attr_vehicle, &attr, NULL) && attr.u.vehicle
6231                 && vehicle_get_attr(attr.u.vehicle, attr_position_coord_geo, &attrp, NULL)) {
6232                 trans=navit_get_trans(this->nav);
6233                 this->position_coord_geo=attr_dup(&attrp);
6234                 this->vehiclep.pro=transform_get_projection(trans);
6235                 transform_from_geo(this->vehiclep.pro, attrp.u.coord_geo, &c);
6236                 this->vehiclep.x=c.x;
6237                 this->vehiclep.y=c.y;
6238         }
6239 }
6240
6241 static void
6242 gui_internal_enter_setup(struct gui_priv *this)
6243 {
6244         if (!this->mouse_button_clicked_on_map)
6245                 gui_internal_set_position_coord(this);
6246 }
6247
6248 static void
6249 gui_internal_html_main_menu(struct gui_priv *this)
6250 {
6251         gui_internal_prune_menu(this, NULL);
6252         gui_internal_html_load_href(this, "#Main Menu", 0);
6253 }
6254
6255 static void
6256 gui_internal_cmd_menu(struct gui_priv *this, int ignore, char *href)
6257 {
6258         dbg(1,"enter\n");
6259         gui_internal_enter(this, ignore);
6260         gui_internal_enter_setup(this);
6261         // draw menu
6262         if (href)
6263                 gui_internal_html_load_href(this, href, 0);
6264         else
6265                 gui_internal_html_main_menu(this);
6266 }
6267
6268 static void
6269 gui_internal_cmd_menu2(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
6270 {
6271         char *href=NULL;
6272         int i=0, ignore=0, replace=0;
6273
6274         if (in && in[i] && ATTR_IS_INT(in[i]->type))
6275                 ignore=in[i++]->u.num;
6276
6277         if (in && in[i] && ATTR_IS_STRING(in[i]->type)) {
6278                 href=in[i++]->u.str;
6279                 if (in[i] && ATTR_IS_INT(in[i]->type))
6280                         replace=in[i++]->u.num;
6281         }
6282
6283         if (this->root.children) {
6284                 if (!href)
6285                         return;
6286                 gui_internal_html_load_href(this, href, replace);
6287                 return;
6288         }
6289         gui_internal_cmd_menu(this, ignore, href);
6290 }
6291
6292
6293 static void
6294 gui_internal_cmd_log_do(struct gui_priv *this, struct widget *widget)
6295 {
6296         if (widget->text && strlen(widget->text)) {
6297                 if (this->position_coord_geo)
6298                         navit_textfile_debug_log_at(this->nav, &this->vehiclep, "type=log_entry label=\"%s\"",widget->text);
6299                 else
6300                         navit_textfile_debug_log(this->nav, "type=log_entry label=\"%s\"",widget->text);
6301         }
6302         g_free(widget->text);
6303         widget->text=NULL;
6304         gui_internal_prune_menu(this, NULL);
6305         gui_internal_check_exit(this);
6306 }
6307
6308 static void
6309 gui_internal_cmd_log_clicked(struct gui_priv *this, struct widget *widget, void *data)
6310 {
6311         gui_internal_cmd_log_do(this, widget->data);
6312 }
6313
6314 static void
6315 gui_internal_cmd_log_changed(struct gui_priv *this, struct widget *wm, void *data)
6316 {
6317         int len;
6318         if (wm->text) {
6319                 len=strlen(wm->text);
6320                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
6321                         wm->text[len-1]='\0';
6322                         gui_internal_cmd_log_do(this, wm);
6323                 }
6324         }
6325 }
6326
6327
6328 static void
6329 gui_internal_cmd_log(struct gui_priv *this)
6330 {
6331         struct widget *w,*wb,*wk,*wl,*we,*wnext;
6332         gui_internal_enter(this, 1);
6333         gui_internal_set_click_coord(this, NULL);
6334         gui_internal_enter_setup(this);
6335         wb=gui_internal_menu(this, "Log Message");
6336         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
6337         gui_internal_widget_append(wb, w);
6338         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
6339         gui_internal_widget_append(w, we);
6340         gui_internal_widget_append(we, wk=gui_internal_label_new(this, _("Message")));
6341         wk->state |= STATE_EDIT|STATE_EDITABLE|STATE_CLEAR;
6342         wk->background=this->background;
6343         wk->flags |= flags_expand|flags_fill;
6344         wk->func = gui_internal_cmd_log_changed;
6345         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
6346         wnext->state |= STATE_SENSITIVE;
6347         wnext->func = gui_internal_cmd_log_clicked;
6348         wnext->data=wk;
6349         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
6350         gui_internal_widget_append(w, wl);
6351         if (this->keyboard)
6352                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
6353         gui_internal_menu_render(this);
6354         gui_internal_leave(this);
6355 }
6356
6357 static void
6358 gui_internal_check_exit(struct gui_priv *this)
6359 {
6360         struct graphics *gra=this->gra;
6361         if (! this->root.children) {
6362                 gui_internal_search_idle_end(this);
6363                 gui_internal_search_list_destroy(this);
6364                 graphics_overlay_disable(gra, 0);
6365                 if (!navit_block(this->nav, 0)) {
6366                         if (this->redraw)
6367                                 navit_draw(this->nav);
6368                         else
6369                                 navit_draw_displaylist(this->nav);
6370                 }
6371         }
6372 }
6373
6374 static int
6375 gui_internal_get_attr(struct gui_priv *this, enum attr_type type, struct attr *attr)
6376 {
6377         switch (type) {
6378         case attr_active:
6379                 attr->u.num=this->root.children != NULL;
6380                 break;
6381         case attr_click_coord_geo:
6382                 if (!this->click_coord_geo)
6383                         return 0;
6384                 *attr=*this->click_coord_geo;
6385                 break;
6386         case attr_position_coord_geo:
6387                 if (!this->position_coord_geo)
6388                         return 0;
6389                 *attr=*this->position_coord_geo;
6390                 break;
6391         case attr_pitch:
6392                 attr->u.num=this->pitch;
6393                 break;
6394         case attr_button:
6395                 attr->u.num=this->mouse_button_clicked_on_map;
6396                 break;
6397         default:
6398                 return 0;
6399         }
6400         attr->type=type;
6401         return 1;
6402 }
6403
6404 static int
6405 gui_internal_add_attr(struct gui_priv *this, struct attr *attr)
6406 {
6407         switch (attr->type) {
6408         case attr_xml_text:
6409                 g_free(this->html_text);
6410                 this->html_text=g_strdup(attr->u.str);
6411                 return 1;
6412         default:
6413                 return 0;
6414         }
6415 }
6416
6417 static int
6418 gui_internal_set_attr(struct gui_priv *this, struct attr *attr)
6419 {
6420         switch (attr->type) {
6421         case attr_fullscreen:
6422                 if ((this->fullscreen > 0) != (attr->u.num > 0)) {
6423                         graphics_draw_mode(this->gra, draw_mode_end);
6424                         this->win->fullscreen(this->win, attr->u.num > 0);
6425                         graphics_draw_mode(this->gra, draw_mode_begin);
6426                 }
6427                 this->fullscreen=attr->u.num;
6428                 return 1;
6429         case attr_menu_on_map_click:
6430                 this->menu_on_map_click=attr->u.num;
6431                 return 1;
6432         case attr_on_map_click:
6433                 g_free(this->on_map_click);
6434                 this->on_map_click=g_strdup(attr->u.str);
6435                 return 1;
6436         default:
6437                 dbg(0,"%s\n",attr_to_name(attr->type));
6438                 return 1;
6439         }
6440 }
6441
6442 static void gui_internal_dbus_signal(struct gui_priv *this, struct point *p)
6443 {
6444         struct displaylist_handle *dlh;
6445         struct displaylist *display;
6446         struct displayitem *di;
6447         struct attr cb,**attr_list=NULL;
6448         int valid=0;
6449
6450         display=navit_get_displaylist(this->nav);
6451         dlh=graphics_displaylist_open(display);
6452         while ((di=graphics_displaylist_next(dlh))) {
6453                 struct item *item=graphics_displayitem_get_item(di);
6454                 if (item_is_point(*item) && graphics_displayitem_get_displayed(di) &&
6455                         graphics_displayitem_within_dist(display, di, p, this->radius)) {
6456                         struct map_rect *mr=map_rect_new(item->map, NULL);
6457                         struct item *itemo=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
6458                         struct attr attr;
6459                         if (itemo && item_attr_get(itemo, attr_data, &attr))
6460                                 attr_list=attr_generic_add_attr(attr_list, &attr);
6461                         map_rect_destroy(mr);
6462                 }
6463         }
6464         graphics_displaylist_close(dlh);
6465         if (attr_list && navit_get_attr(this->nav, attr_callback_list, &cb, NULL))
6466                 callback_list_call_attr_4(cb.u.callback_list, attr_command, "dbus_send_signal", attr_list, NULL, &valid);
6467         attr_list_free(attr_list);
6468 }
6469
6470
6471
6472 //##############################################################################################################
6473 //# Description: Function to handle mouse clicks and scroll wheel movement
6474 //# Comment:
6475 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
6476 //##############################################################################################################
6477 static void gui_internal_button(void *data, int pressed, int button, struct point *p)
6478 {
6479         struct gui_priv *this=data;
6480         struct graphics *gra=this->gra;
6481
6482         dbg(1,"enter %d %d\n", pressed, button);
6483         // if still on the map (not in the menu, yet):
6484         dbg(1,"children=%p ignore_button=%d\n",this->root.children,this->ignore_button);
6485         if (!this->root.children || this->ignore_button) {
6486
6487                 this->ignore_button=0;
6488                 // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
6489                 if (!navit_handle_button(this->nav, pressed, button, p, NULL)) {
6490                         dbg(1,"navit has handled button\n");
6491                         return;
6492                 }
6493                 dbg(1,"menu_on_map_click=%d\n",this->menu_on_map_click);
6494                 if (button != 1)
6495                         return;
6496                 if (this->on_map_click || this->menu_on_map_click) {
6497                         this->mouse_button_clicked_on_map=1;
6498                         gui_internal_set_click_coord(this, p);
6499                         gui_internal_set_position_coord(this);
6500                         if (this->on_map_click)
6501                                 command_evaluate(&this->self, this->on_map_click);
6502                         else
6503                                 gui_internal_cmd_menu(this, 0, NULL);
6504                         this->mouse_button_clicked_on_map=0;
6505                 } else if (this->signal_on_map_click) {
6506                         gui_internal_dbus_signal(this, p);
6507                         return;
6508                 }
6509                 return;
6510         }
6511
6512
6513         /*
6514          * If already in the menu:
6515          */
6516
6517         if (pressed) {
6518                 this->pressed=1;
6519                 this->current=*p;
6520                 gui_internal_gesture_ring_clear(this);
6521                 gui_internal_gesture_ring_add(this, p);
6522                 gui_internal_highlight(this);
6523         } else {
6524                 int dx,dy;
6525                 gui_internal_gesture_ring_add(this, p);
6526                 gui_internal_gesture_get_vector(this, 300, NULL, &dx, &dy);
6527                 this->current.x=-1;
6528                 this->current.y=-1;
6529                 graphics_draw_mode(gra, draw_mode_begin);
6530                 if(!gui_internal_gesture_do(this) && this->pressed!=2 && abs(dx)<this->icon_s && abs(dy)<this->icon_s)
6531                         gui_internal_call_highlighted(this);
6532                 this->pressed=0;
6533                 if (!event_main_loop_has_quit()) {
6534                         gui_internal_highlight(this);
6535                         graphics_draw_mode(gra, draw_mode_end);
6536                         gui_internal_check_exit(this);
6537                 }
6538         }
6539 }
6540
6541 static void
6542 gui_internal_setup(struct gui_priv *this)
6543 {
6544         struct color cbh={0x9fff,0x9fff,0x9fff,0xffff};
6545         struct color cf={0xbfff,0xbfff,0xbfff,0xffff};
6546         struct graphics *gra=this->gra;
6547         unsigned char *buffer;
6548         char *gui_file;
6549         int size;
6550
6551         if (this->background)
6552                 return;
6553         this->background=graphics_gc_new(gra);
6554         this->background2=graphics_gc_new(gra);
6555         this->highlight_background=graphics_gc_new(gra);
6556         graphics_gc_set_foreground(this->highlight_background, &cbh);
6557         this->foreground=graphics_gc_new(gra);
6558         graphics_gc_set_foreground(this->foreground, &cf);
6559         this->text_background=graphics_gc_new(gra);
6560         this->text_foreground=graphics_gc_new(gra);
6561         graphics_gc_set_foreground(this->background, &this->background_color);
6562         graphics_gc_set_foreground(this->background2, &this->background2_color);
6563         graphics_gc_set_foreground(this->text_background, &this->text_background_color);
6564         graphics_gc_set_foreground(this->text_foreground, &this->text_foreground_color);
6565         gui_file=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal.txt", NULL);
6566         if (file_get_contents(gui_file,&buffer,&size)) {
6567                 char *command=g_malloc(size+1);
6568                 strncpy(command,(const char *)buffer,size);
6569                 command[size]=0;
6570                 command_evaluate(&this->self, command);
6571                 g_free(command);
6572                 g_free(buffer);
6573         }
6574         g_free(gui_file);
6575 }
6576
6577 //##############################################################################################################
6578 //# Description:
6579 //# Comment:
6580 //# Authors: Martin Schaller (04/2008)
6581 //##############################################################################################################
6582 static void gui_internal_resize(void *data, int w, int h)
6583 {
6584         struct gui_priv *this=data;
6585         int changed=0;
6586
6587         gui_internal_setup(this);
6588
6589         if (this->root.w != w || this->root.h != h) {
6590                 this->root.w=w;
6591                 this->root.h=h;
6592                 changed=1;
6593         }
6594         dbg(1,"w=%d h=%d children=%p\n", w, h, this->root.children);
6595         navit_handle_resize(this->nav, w, h);
6596         if (this->root.children) {
6597                 if (changed) {
6598                         gui_internal_html_main_menu(this);
6599                 } else {
6600                         gui_internal_menu_render(this);
6601                 }
6602         }
6603 }
6604
6605 static void
6606 gui_internal_keynav_point(struct widget *w, int dx, int dy, struct point *p)
6607 {
6608         p->x=w->p.x+w->w/2;
6609         p->y=w->p.y+w->h/2;
6610         if (dx < 0)
6611                 p->x=w->p.x;
6612         if (dx > 0)
6613                 p->x=w->p.x+w->w;
6614         if (dy < 0)
6615                 p->y=w->p.y;
6616         if (dy > 0)
6617                 p->y=w->p.y+w->h;
6618 }
6619
6620 static void
6621 gui_internal_keynav_find_closest(struct widget *wi, struct point *p, int dx, int dy, int *distance, struct widget **result)
6622 {
6623         GList *l=wi->children;
6624         // Skip hidden elements
6625         if (wi->p.x==0 && wi->p.y==0 && wi->w==0 && wi->h==0)
6626                 return;
6627         if ((wi->state & STATE_SENSITIVE) ) {
6628                 int dist1,dist2;
6629                 struct point wp;
6630                 gui_internal_keynav_point(wi, -dx, -dy, &wp);
6631                 if (dx) {
6632                         dist1=(wp.x-p->x)*dx;
6633                         dist2=wp.y-p->y;
6634                 } else if (dy) {
6635                         dist1=(wp.y-p->y)*dy;
6636                         dist2=wp.x-p->x;
6637                 } else {
6638                         dist2=wp.x-p->x;
6639                         dist1=wp.y-p->y;
6640                         if (dist1 < 0)
6641                                 dist1=-dist1;
6642                 }
6643                 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);
6644                 if (dist1 >= 0) {
6645                         if (dist2 < 0)
6646                                 dist1-=dist2;
6647                         else
6648                                 dist1+=dist2;
6649                         if (dist1 < *distance) {
6650                                 *result=wi;
6651                                 *distance=dist1;
6652                         }
6653                 }
6654         }
6655         while (l) {
6656                 struct widget *child=l->data;
6657                 gui_internal_keynav_find_closest(child, p, dx, dy, distance, result);
6658                 l=g_list_next(l);
6659         }
6660 }
6661
6662 static void
6663 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy)
6664 {
6665         struct widget *result,*menu=g_list_last(this->root.children)->data;
6666         struct point p;
6667         int distance;
6668         if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
6669                 gui_internal_keynav_point(this->highlighted, dx, dy, &p);
6670         else {
6671                 p.x=0;
6672                 p.y=0;
6673                 distance=INT_MAX;
6674                 result=NULL;
6675                 gui_internal_keynav_find_closest(menu, &p, 0, 0, &distance, &result);
6676                 if (result) {
6677                         gui_internal_keynav_point(result, dx, dy, &p);
6678                         dbg(1,"result origin=%p p=%d,%d\n", result, p.x, p.y);
6679                 }
6680         }
6681         result=NULL;
6682         distance=INT_MAX;
6683         gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
6684         dbg(1,"result=%p\n", result);
6685         if (! result) {
6686                 if (dx < 0)
6687                         p.x=this->root.w;
6688                 if (dx > 0)
6689                         p.x=0;
6690                 if (dy < 0)
6691                         p.y=this->root.h;
6692                 if (dy > 0)
6693                         p.y=0;
6694                 result=NULL;
6695                 distance=INT_MAX;
6696                 gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
6697                 dbg(1,"wraparound result=%p\n", result);
6698         }
6699         gui_internal_highlight_do(this, result);
6700         if (result)
6701                 gui_internal_say(this, result, 1);
6702 }
6703
6704 //##############################################################################################################
6705 //# Description:
6706 //# Comment:
6707 //# Authors: Martin Schaller (04/2008)
6708 //##############################################################################################################
6709 static void gui_internal_keypress(void *data, char *key)
6710 {
6711         struct gui_priv *this=data;
6712         int w,h;
6713         struct point p;
6714         if (!this->root.children) {
6715                 transform_get_size(navit_get_trans(this->nav), &w, &h);
6716                 switch (*key) {
6717                 case NAVIT_KEY_UP:
6718                         p.x=w/2;
6719                         p.y=0;
6720                         navit_set_center_screen(this->nav, &p, 1);
6721                         break;
6722                 case NAVIT_KEY_DOWN:
6723                         p.x=w/2;
6724                         p.y=h;
6725                         navit_set_center_screen(this->nav, &p, 1);
6726                         break;
6727                 case NAVIT_KEY_LEFT:
6728                         p.x=0;
6729                         p.y=h/2;
6730                         navit_set_center_screen(this->nav, &p, 1);
6731                         break;
6732                 case NAVIT_KEY_RIGHT:
6733                         p.x=w;
6734                         p.y=h/2;
6735                         navit_set_center_screen(this->nav, &p, 1);
6736                         break;
6737                 case NAVIT_KEY_ZOOM_IN:
6738                         navit_zoom_in(this->nav, 2, NULL);
6739                         break;
6740                 case NAVIT_KEY_ZOOM_OUT:
6741                         navit_zoom_out(this->nav, 2, NULL);
6742                         break;
6743                 case NAVIT_KEY_RETURN:
6744                 case NAVIT_KEY_MENU:
6745                         gui_internal_set_click_coord(this, NULL);
6746                         gui_internal_cmd_menu(this, 0, NULL);
6747                         break;
6748                 }
6749                 return;
6750         }
6751         graphics_draw_mode(this->gra, draw_mode_begin);
6752         switch (*key) {
6753         case NAVIT_KEY_LEFT:
6754                 gui_internal_keynav_highlight_next(this,-1,0);
6755                 break;
6756         case NAVIT_KEY_RIGHT:
6757                 gui_internal_keynav_highlight_next(this,1,0);
6758                 break;
6759         case NAVIT_KEY_UP:
6760                 gui_internal_keynav_highlight_next(this,0,-1);
6761                 break;
6762         case NAVIT_KEY_DOWN:
6763                 gui_internal_keynav_highlight_next(this,0,1);
6764                 break;
6765         case NAVIT_KEY_BACK:
6766                 if (g_list_length(this->root.children) > 1)
6767                         gui_internal_back(this, NULL, NULL);
6768                 else
6769                         gui_internal_prune_menu(this, NULL);
6770                 break;
6771         case NAVIT_KEY_RETURN:
6772                 if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
6773                         gui_internal_call_highlighted(this);
6774                 else
6775                         gui_internal_keypress_do(this, key);
6776                 break;
6777         default:
6778                 gui_internal_keypress_do(this, key);
6779         }
6780         if (!event_main_loop_has_quit()) {
6781                 graphics_draw_mode(this->gra, draw_mode_end);
6782                 gui_internal_check_exit(this);
6783         }
6784 }
6785
6786
6787 //##############################################################################################################
6788 //# Description:
6789 //# Comment:
6790 //# Authors: Martin Schaller (04/2008)
6791 //##############################################################################################################
6792 static int gui_internal_set_graphics(struct gui_priv *this, struct graphics *gra)
6793 {
6794         struct window *win;
6795         struct transformation *trans=navit_get_trans(this->nav);
6796
6797         win=graphics_get_data(gra, "window");
6798         if (! win)
6799                 return 1;
6800         navit_ignore_graphics_events(this->nav, 1);
6801         this->gra=gra;
6802         this->win=win;
6803         navit_ignore_graphics_events(this->nav, 1);
6804         transform_get_size(trans, &this->root.w, &this->root.h);
6805         this->resize_cb=callback_new_attr_1(callback_cast(gui_internal_resize), attr_resize, this);
6806         graphics_add_callback(gra, this->resize_cb);
6807         this->button_cb=callback_new_attr_1(callback_cast(gui_internal_button), attr_button, this);
6808         graphics_add_callback(gra, this->button_cb);
6809         this->motion_cb=callback_new_attr_1(callback_cast(gui_internal_motion), attr_motion, this);
6810         graphics_add_callback(gra, this->motion_cb);
6811         this->keypress_cb=callback_new_attr_1(callback_cast(gui_internal_keypress), attr_keypress, this);
6812         graphics_add_callback(gra, this->keypress_cb);
6813         this->window_closed_cb=callback_new_attr_1(callback_cast(gui_internal_window_closed), attr_window_closed, this);
6814         graphics_add_callback(gra, this->window_closed_cb);
6815
6816         // set fullscreen if needed
6817         if (this->fullscreen)
6818                 this->win->fullscreen(this->win, this->fullscreen != 0);
6819         /* Was resize callback already issued? */
6820         if (navit_get_ready(this->nav) & 2)
6821                 gui_internal_setup(this);
6822         return 0;
6823 }
6824
6825 static void gui_internal_disable_suspend(struct gui_priv *this)
6826 {
6827         if (this->win->disable_suspend)
6828                 this->win->disable_suspend(this->win);
6829 }
6830
6831 //##############################################################################################################
6832 //# Description:
6833 //# Comment:
6834 //# Authors: Martin Schaller (04/2008)
6835 //##############################################################################################################
6836 struct gui_methods gui_internal_methods = {
6837         NULL,
6838         NULL,
6839         gui_internal_set_graphics,
6840         NULL,
6841         NULL,
6842         NULL,
6843         gui_internal_disable_suspend,
6844         gui_internal_get_attr,
6845         gui_internal_add_attr,
6846         gui_internal_set_attr,
6847 };
6848
6849         static void
6850 gui_internal_get_data(struct gui_priv *priv, char *command, struct attr **in, struct attr ***out)
6851 {
6852         struct attr private_data = { attr_private_data, {(void *)&priv->data}};
6853         if (out)
6854                 *out=attr_generic_add_attr(*out, &private_data);
6855 }
6856
6857 static void
6858 gui_internal_add_callback(struct gui_priv *priv, struct callback *cb)
6859 {
6860         callback_list_add(priv->cbl, cb);
6861 }
6862
6863 static void
6864 gui_internal_remove_callback(struct gui_priv *priv, struct callback *cb)
6865 {
6866         callback_list_remove(priv->cbl, cb);
6867 }
6868
6869
6870 static struct gui_internal_methods gui_internal_methods_ext = {
6871         gui_internal_add_callback,
6872         gui_internal_remove_callback,
6873         gui_internal_menu_render,
6874         image_new_xs,
6875         image_new_l,
6876 };
6877
6878
6879 static enum flags
6880 gui_internal_get_flags(struct widget *widget)
6881 {
6882         return widget->flags;
6883 }
6884
6885 static void
6886 gui_internal_set_flags(struct widget *widget, enum flags flags)
6887 {
6888         widget->flags=flags;
6889 }
6890
6891 static int
6892 gui_internal_get_state(struct widget *widget)
6893 {
6894         return widget->state;
6895 }
6896
6897 static void
6898 gui_internal_set_state(struct widget *widget, int state)
6899 {
6900         widget->state=state;
6901 }
6902
6903 static void
6904 gui_internal_set_func(struct widget *widget, void (*func)(struct gui_priv *priv, struct widget *widget, void *data))
6905 {
6906         widget->func=func;
6907 }
6908
6909 static void
6910 gui_internal_set_data(struct widget *widget, void *data)
6911 {
6912         widget->data=data;
6913 }
6914
6915 static void
6916 gui_internal_set_default_background(struct gui_priv *this, struct widget *widget)
6917 {
6918         widget->background=this->background;
6919 }
6920
6921 static struct gui_internal_widget_methods gui_internal_widget_methods = {
6922         gui_internal_widget_append,
6923         gui_internal_button_new,
6924         gui_internal_button_new_with_callback,
6925         gui_internal_box_new,
6926         gui_internal_label_new,
6927         gui_internal_image_new,
6928         gui_internal_keyboard,
6929         gui_internal_menu,
6930         gui_internal_get_flags,
6931         gui_internal_set_flags,
6932         gui_internal_get_state,
6933         gui_internal_set_state,
6934         gui_internal_set_func,
6935         gui_internal_set_data,
6936         gui_internal_set_default_background,
6937 };
6938
6939 static void
6940 gui_internal_cmd_write(struct gui_priv * this, char *function, struct attr **in, struct attr ***out, int *valid)
6941 {
6942         char *str=NULL,*str2=NULL;
6943         dbg(1,"enter %s %p %p %p\n",function,in,out,valid);
6944         if (!in || !in[0])
6945                 return;
6946         dbg(1,"%s\n",attr_to_name(in[0]->type));
6947         if (ATTR_IS_STRING(in[0]->type)) {
6948                 str=in[0]->u.str;
6949         }
6950         if (ATTR_IS_COORD_GEO(in[0]->type)) {
6951                 str=str2=coordinates_geo(in[0]->u.coord_geo, '\n');
6952         }
6953         if (str) {
6954                 str=g_strdup_printf("<html>%s</html>\n",str);
6955                 xml_parse_text(str, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
6956         }
6957         g_free(str);
6958         g_free(str2);
6959 }
6960
6961
6962 /**
6963  * @brief Creates a new table widget.
6964  *
6965  * Creates and returns a new table widget.  This function will
6966  * setup next/previous buttons as children.
6967  *
6968  * @param this The graphics context.
6969  * @param flags widget sizing flags.
6970  * @returns The newly created widget
6971  */
6972 struct widget * gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons)
6973 {
6974         struct widget * widget = g_new0(struct widget,1);
6975         struct table_data * data = NULL;
6976         widget->type=widget_table;
6977         widget->flags=flags;
6978         widget->state=STATE_SCROLLABLE;
6979         widget->data=g_new0(struct table_data,1);
6980         widget->data_free=gui_internal_table_data_free;
6981
6982         // We have to set background here explicitly
6983         // because it will be used by inner elements created later in this 
6984         // function (navigation buttons). Else that elements won't have
6985         // any background.
6986         widget->background=this->background;
6987         data = (struct table_data*)widget->data;
6988
6989         if (buttons) {
6990                 data->next_button=gui_internal_box_new(this, gravity_center|orientation_horizontal);
6991                 gui_internal_widget_append(data->next_button, gui_internal_text_new(this,_("Next"),gravity_center|orientation_horizontal));
6992                 gui_internal_widget_append(data->next_button, gui_internal_image_new(this, image_new_xs(this, "gui_arrow_right")));
6993                 data->next_button->func=gui_internal_table_button_next;
6994                 data->next_button->data=widget;
6995
6996
6997                 data->prev_button =  gui_internal_button_new_with_callback
6998                         (this, _("Prev"),
6999                         image_new_xs(this, "gui_arrow_left"),
7000                         gravity_center |orientation_horizontal,
7001                         gui_internal_table_button_prev,NULL);
7002
7003                 data->prev_button->data=widget;
7004
7005                 data->this=this;
7006
7007                 data->button_box=gui_internal_box_new(this,
7008                                               gravity_center|orientation_horizontal);
7009                 gui_internal_widget_append(widget, data->button_box);
7010                 gui_internal_widget_append(data->button_box, data->prev_button);
7011                 gui_internal_widget_append(data->button_box, data->next_button);
7012
7013                 data->button_box->bl=this->spacing;
7014                 gui_internal_widget_pack(this,data->button_box);
7015         }
7016
7017         return widget;
7018
7019 }
7020
7021 /**
7022  * @brief Clears all the rows from the table.
7023  * This function removes all rows from a table.
7024  * New rows can later be added to the table.
7025  */
7026 void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table)
7027 {
7028   GList * iter;
7029   struct table_data * table_data = (struct table_data* ) table->data;
7030
7031   iter = table->children;
7032   while(iter ) {
7033           if(iter->data != table_data->button_box) {
7034                   struct widget * child = (struct widget*)iter->data;
7035                   gui_internal_widget_destroy(this,child);
7036                   if(table->children == iter) {
7037                           table->children = g_list_remove(iter,iter->data);
7038                           iter=table->children;
7039                   }
7040                   else
7041                           iter = g_list_remove(iter,iter->data);
7042           }
7043           else {
7044                   iter = g_list_next(iter);
7045           }
7046
7047   }
7048   table_data->top_row=NULL;
7049   table_data->bottom_row=NULL;
7050 }
7051
7052 /**
7053  * @brief Check if table has any data rows filled.
7054  * @param this The graphics context
7055  * @param table table widget
7056  * @returns 1 if the table is empty, 0 if there any data rows present.
7057  */
7058 static int gui_internal_widget_table_is_empty(struct gui_priv *this, struct widget * table)
7059 {
7060    GList *l;
7061    struct table_data *td=(struct table_data*) table->data;
7062
7063    for(l=table->children;l;l=g_list_next(l)) {
7064         if(l->data != td->button_box)
7065                 return 0;
7066    }
7067
7068    return 1;
7069 }
7070
7071 /**
7072  * @brief Move GList pointer to the next table row, skipping other table children (button box, for example).
7073  * @param row GList pointer into the children list 
7074  * @returns GList pointer to the next row in the children list, or NULL if there are no any rows left.
7075  */
7076 static GList * gui_internal_widget_table_next_row(GList * row)
7077 {
7078   while((row=g_list_next(row))!=NULL) {
7079         if(row->data && ((struct widget *)(row->data))->type == widget_table_row)
7080                 break;
7081    }
7082   return row;
7083 }
7084
7085 /**
7086  * @brief Move GList pointer to the previous table row, skipping other table children (button box, for example).
7087  * @param row GList pointer into the children list 
7088  * @returns GList pointer to the previous row in the children list, or NULL if there are no any rows left.
7089  */
7090 static GList * gui_internal_widget_table_prev_row(GList * row)
7091 {
7092   while((row=g_list_previous(row))!=NULL) {
7093         if(row->data && ((struct widget *)(row->data))->type == widget_table_row)
7094                 break;
7095    }
7096   return row;
7097 }
7098
7099
7100 /**
7101  * Creates a new table_row widget.
7102  * @param this The graphics context
7103  * @param flags Sizing flags for the row
7104  * @returns The new table_row widget.
7105  */
7106 struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags)
7107 {
7108         struct widget * widget = g_new0(struct widget,1);
7109         widget->type=widget_table_row;
7110         widget->flags=flags;
7111         return widget;
7112 }
7113
7114
7115
7116 /**
7117  * @brief Computes the column dimensions for the table.
7118  *
7119  * @param w The table widget to compute dimensions for.
7120  *
7121  * This function examines all of the rows and columns for the table w
7122  * and returns a list (GList) of table_column_desc elements that
7123  * describe each column of the table.
7124  *
7125  * The caller is responsible for freeing the returned list.
7126  */
7127 static GList * gui_internal_compute_table_dimensions(struct gui_priv * this,struct widget * w)
7128 {
7129
7130         GList * column_desc = NULL;
7131         GList * current_desc=NULL;
7132         GList * cur_row = w->children;
7133         struct widget * cur_row_widget=NULL;
7134         GList * cur_column=NULL;
7135         struct widget * cell_w=NULL;
7136         struct table_column_desc * current_cell=NULL;
7137         struct table_data * table_data=NULL;
7138         int height=0;
7139         int width=0;
7140         int total_width=0;
7141         int column_count=0;
7142
7143         /**
7144          * Scroll through the the table and
7145          * 1. Compute the maximum width + height of each column across all rows.
7146          */
7147         table_data = (struct table_data*) w->data;
7148         for(cur_row=w->children;  cur_row ; cur_row = g_list_next(cur_row) )
7149         {
7150                 cur_row_widget = (struct widget*) cur_row->data;
7151                 current_desc = column_desc;
7152                 if(cur_row_widget == table_data->button_box)
7153                 {
7154                         continue;
7155                 }
7156                 column_count=0;
7157                 for(cur_column = cur_row_widget->children; cur_column;
7158                     cur_column=g_list_next(cur_column))
7159                 {
7160                         cell_w = (struct widget*) cur_column->data;
7161                         gui_internal_widget_pack(this,cell_w);
7162                         if(current_desc == 0)
7163                         {
7164                                 current_cell = g_new0(struct table_column_desc,1);
7165                                 column_desc = g_list_append(column_desc,current_cell);
7166                                 current_desc = g_list_last(column_desc);
7167                                 current_cell->height=cell_w->h;
7168                                 current_cell->width=cell_w->w;
7169                                 total_width+=cell_w->w;
7170
7171                         }
7172                         else
7173                         {
7174                                 current_cell = current_desc->data;
7175                                 height = cell_w->h;
7176                                 width = cell_w->w;
7177                                 if(current_cell->height < height )
7178                                 {
7179                                         current_cell->height = height;
7180                                 }
7181                                 if(current_cell->width < width)
7182                                 {
7183                                         total_width += (width-current_cell->width);
7184                                         current_cell->width = width;
7185
7186
7187
7188                                 }
7189                                 current_desc = g_list_next(current_desc);
7190                         }
7191                         column_count++;
7192
7193                 }/* column loop */
7194
7195         } /*row loop */
7196
7197
7198         /**
7199          * If the width of all columns is less than the width off
7200          * the table expand each cell proportionally.
7201          *
7202          */
7203         if(total_width+(this->spacing*column_count) < w->w ) {
7204                 for(current_desc=column_desc; current_desc; current_desc=g_list_next(current_desc)) {
7205                         current_cell = (struct table_column_desc*) current_desc->data;
7206                         current_cell->width= ( (current_cell->width+this->spacing)/(float)total_width) * w->w ;
7207                 }
7208         }
7209
7210         return column_desc;
7211 }
7212
7213
7214 /**
7215  * @brief Computes the height and width for the table.
7216  *
7217  * The height and widht are computed to display all cells in the table
7218  * at the requested height/width.
7219  *
7220  * @param this The graphics context
7221  * @param w The widget to pack.
7222  *
7223  */
7224 void gui_internal_table_pack(struct gui_priv * this, struct widget * w)
7225 {
7226
7227         int height=0;
7228         int width=0;
7229         int count=0;
7230         GList * column_data = gui_internal_compute_table_dimensions(this,w);
7231         GList * current=0;
7232         struct table_column_desc * cell_desc=0;
7233         struct table_data * table_data = (struct table_data*)w->data;
7234
7235         for(current = column_data; current; current=g_list_next(current))
7236         {
7237                 if(table_data->button_box == current->data )
7238                 {
7239                         continue;
7240                 }
7241                 cell_desc = (struct table_column_desc *) current->data;
7242                 width = width + cell_desc->width + this->spacing;
7243                 if(height < cell_desc->height)
7244                 {
7245                         height = cell_desc->height ;
7246                 }
7247         }
7248
7249
7250
7251         for(current=w->children; current; current=g_list_next(current))
7252         {
7253                 if(current->data!= table_data->button_box)
7254                 {
7255                         count++;
7256                 }
7257         }
7258
7259         w->w = width;
7260         if(w->w + w->c.x > this->root.w)
7261         {
7262                 w->w = this->root.w - w->c.x;
7263         }
7264
7265
7266         if(w->h + w->c.y   > this->root.h   )
7267         {
7268                 /**
7269                  * Do not allow the widget to exceed the screen.
7270                  *
7271                  */
7272                 w->h = this->root.h- w->c.y  - height;
7273         }
7274
7275         if (table_data->button_box) 
7276         {
7277                 gui_internal_widget_pack(this,table_data->button_box);
7278         }
7279
7280
7281         /**
7282          * Deallocate column descriptions.
7283          */
7284         g_list_foreach(column_data,(GFunc)g_free,NULL);
7285         g_list_free(column_data);
7286 }
7287
7288
7289
7290
7291 /**
7292  * @brief Invalidates coordinates for previosly rendered table widget rows.
7293  *
7294  * @param table_data Data from the table object.
7295  */
7296 static void gui_internal_table_hide_rows(struct table_data * table_data)
7297 {
7298         GList*cur_row;
7299         for(cur_row=table_data->top_row; cur_row ; cur_row = g_list_next(cur_row))
7300         {
7301                 struct widget * cur_row_widget = (struct widget*)cur_row->data;
7302                 if (cur_row_widget->type!=widget_table_row)
7303                         continue;
7304                 cur_row_widget->p.x=0;
7305                 cur_row_widget->p.y=0;
7306                 cur_row_widget->w=0;
7307                 cur_row_widget->h=0;
7308                 if(cur_row==table_data->bottom_row)
7309                         break;
7310         }
7311 }
7312
7313
7314 /**
7315  * @brief Renders a table widget.
7316  *
7317  * @param this The graphics context
7318  * @param w The table widget to render.
7319  */
7320 void gui_internal_table_render(struct gui_priv * this, struct widget * w)
7321 {
7322
7323         int x;
7324         int y;
7325         GList * column_desc=NULL;
7326         GList * cur_row = NULL;
7327         GList * current_desc=NULL;
7328         struct table_data * table_data = (struct table_data*)w->data;
7329         int is_skipped=0;
7330         int is_first_page=1;
7331         struct table_column_desc * dim=NULL;
7332
7333         dbg_assert(table_data);
7334         column_desc = gui_internal_compute_table_dimensions(this,w);
7335         if(!column_desc)
7336                 return;
7337         y=w->p.y;
7338         gui_internal_table_hide_rows(table_data);
7339         /**
7340          * Skip rows that are on previous pages.
7341          */
7342         cur_row = w->children;
7343         if(table_data->top_row && table_data->top_row != w->children && !table_data->button_box_hide)
7344         {
7345                 cur_row = table_data->top_row;
7346                 is_first_page=0;
7347         } else {
7348                 table_data->top_row=NULL;
7349         }
7350         /**
7351          * Loop through each row.  Drawing each cell with the proper sizes,
7352          * at the proper positions.
7353          */
7354         for(table_data->top_row=cur_row; cur_row; cur_row = g_list_next(cur_row))
7355         {
7356                 int max_height=0, bbox_height=0;
7357                 struct widget * cur_row_widget;
7358                 GList * cur_column=NULL;
7359                 current_desc = column_desc;
7360                 cur_row_widget = (struct widget*)cur_row->data;
7361                 x =w->p.x+this->spacing;
7362                 if(cur_row_widget == table_data->button_box )
7363                 {
7364                         continue;
7365                 }
7366                 dim = (struct table_column_desc*)current_desc->data;
7367
7368                 if (table_data->button_box && !table_data->button_box_hide)
7369                         bbox_height=table_data->button_box->h;
7370
7371                 if( y + dim->height + bbox_height + this->spacing >= w->p.y + w->h )
7372                 {
7373                         /*
7374                          * No more drawing space left.
7375                          */
7376                         is_skipped=1;
7377                         break;
7378                 }
7379                 for(cur_column = cur_row_widget->children; cur_column;
7380                     cur_column=g_list_next(cur_column))
7381                 {
7382                         struct  widget * cur_widget = (struct widget*) cur_column->data;
7383                         dim = (struct table_column_desc*)current_desc->data;
7384
7385                         cur_widget->p.x=x;
7386                         cur_widget->w=dim->width;
7387                         cur_widget->p.y=y;
7388                         cur_widget->h=dim->height;
7389                         x=x+cur_widget->w;
7390                         max_height = dim->height;
7391                         /* We pack the widget before rendering to ensure that the x and y
7392                          * coordinates get pushed down.
7393                          */
7394                         gui_internal_widget_pack(this,cur_widget);
7395                         gui_internal_widget_render(this,cur_widget);
7396
7397                         if(dim->height > max_height)
7398                         {
7399                                 max_height = dim->height;
7400                         }
7401                 }
7402                 
7403                 /* Row object should have its coordinates in actual
7404                  * state to be able to pass mouse clicks to Column objects
7405                  */
7406                 cur_row_widget->p.x=w->p.x;
7407                 cur_row_widget->w=w->w;
7408                 cur_row_widget->p.y=y;
7409                 cur_row_widget->h=max_height;
7410                 y = y + max_height;
7411                 table_data->bottom_row=cur_row;
7412                 current_desc = g_list_next(current_desc);
7413         }
7414         if(table_data->button_box && (is_skipped || !is_first_page) && !table_data->button_box_hide )
7415         {
7416                 table_data->button_box->p.y =w->p.y+w->h-table_data->button_box->h -
7417                         this->spacing;
7418                 if(table_data->button_box->p.y < y )
7419                 {
7420                         table_data->button_box->p.y=y;
7421                 }
7422                 table_data->button_box->p.x = w->p.x;
7423                 table_data->button_box->w = w->w;
7424                 //    table_data->button_box->h = w->h - y;
7425                 //    table_data->next_button->h=table_data->button_box->h;
7426                 //    table_data->prev_button->h=table_data->button_box->h;
7427                 //    table_data->next_button->c.y=table_data->button_box->c.y;
7428                 //    table_data->prev_button->c.y=table_data->button_box->c.y;
7429                 gui_internal_widget_pack(this,table_data->button_box);
7430                 if(table_data->next_button->p.y > w->p.y + w->h + table_data->next_button->h)
7431                 {
7432                         table_data->button_box->p.y = w->p.y + w->h -
7433                                 table_data->button_box->h;
7434                 }
7435                 if(is_skipped)
7436                 {
7437                         table_data->next_button->state|= STATE_SENSITIVE;
7438                 }
7439                 else
7440                 {
7441                         table_data->next_button->state&= ~STATE_SENSITIVE;
7442                 }
7443
7444                 if(table_data->top_row != w->children)
7445                 {
7446                         table_data->prev_button->state|= STATE_SENSITIVE;
7447                 }
7448                 else
7449                 {
7450                         table_data->prev_button->state&= ~STATE_SENSITIVE;
7451                 }
7452                 gui_internal_widget_render(this,table_data->button_box);
7453         }
7454
7455         /**
7456          * Deallocate column descriptions.
7457          */
7458         g_list_foreach(column_desc,(GFunc)g_free,NULL);
7459         g_list_free(column_desc);
7460 }
7461
7462
7463 /**
7464  * @brief Displays Route information
7465  *
7466  * @li The name of the active vehicle
7467  * @param wm The button that was pressed.
7468  * @param v Unused
7469  */
7470 static void
7471 gui_internal_cmd2_route_description(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7472 {
7473
7474
7475         struct widget * menu;
7476         struct widget * row;
7477         struct widget * box;
7478
7479
7480         if(! this->vehicle_cb)
7481         {
7482           /**
7483            * Register the callback on vehicle updates.
7484            */
7485           this->vehicle_cb = callback_new_attr_1(callback_cast(gui_internal_route_update),
7486                                                        attr_position_coord_geo,this);
7487           navit_add_callback(this->nav,this->vehicle_cb);
7488         }
7489
7490         this->route_data.route_table = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
7491
7492         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
7493
7494
7495         menu=gui_internal_menu(this,_("Route Description"));
7496
7497         menu->wfree=gui_internal_route_screen_free;
7498         this->route_data.route_showing=1;
7499         this->route_data.route_table->spx = this->spacing;
7500
7501
7502         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
7503
7504         //      gui_internal_widget_append(box,gui_internal_box_new_with_label(this,"Test"));
7505         gui_internal_widget_append(box,this->route_data.route_table);
7506         box->w=menu->w;
7507         box->spx = this->spacing;
7508         this->route_data.route_table->w=box->w;
7509         gui_internal_widget_append(menu,box);
7510         gui_internal_populate_route_table(this,this->nav);
7511         gui_internal_menu_render(this);
7512
7513 }
7514
7515 static int
7516 line_intersection(struct coord* a1, struct coord *a2, struct coord * b1, struct coord *b2, struct coord *res)
7517 {
7518         int n, a, b;
7519         int adx=a2->x-a1->x;
7520         int ady=a2->y-a1->y;
7521         int bdx=b2->x-b1->x;
7522         int bdy=b2->y-b1->y;
7523         n = bdy * adx - bdx * ady;
7524         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
7525         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
7526         if (n < 0) {
7527                 n = -n;
7528                 a = -a;
7529                 b = -b;
7530         }
7531         if (a < 0 || b < 0)
7532                 return 0;
7533         if (a > n || b > n)
7534                 return 0;
7535         if (n == 0) {
7536                 dbg(0,"a=%d b=%d n=%d\n", a, b, n);
7537                 dbg(0,"a1=0x%x,0x%x ad %d,%d\n", a1->x, a1->y, adx, ady);
7538                 dbg(0,"b1=0x%x,0x%x bd %d,%d\n", b1->x, b1->y, bdx, bdy);
7539                 dbg_assert(n != 0);
7540         }
7541         res->x = a1->x + a * adx / n;
7542         res->y = a1->y + a * ady / n;
7543         return 1;
7544 }
7545
7546 struct heightline {
7547         struct heightline *next;
7548         int height;
7549         struct coord_rect bbox;
7550         int count;
7551         struct coord c[0];
7552 };
7553
7554 struct diagram_point {
7555         struct diagram_point *next;
7556         struct coord c;
7557 };
7558
7559 static struct heightline *
7560 item_get_heightline(struct item *item)
7561 {
7562         struct heightline *ret=NULL;
7563         struct street_data *sd;
7564         struct attr attr;
7565         int i,height;
7566
7567         if (item_attr_get(item, attr_label, &attr)) {
7568                 height=atoi(attr.u.str);
7569                 sd=street_get_data(item);
7570                 if (sd && sd->count > 1) {
7571                         ret=g_malloc(sizeof(struct heightline)+sd->count*sizeof(struct coord));
7572                         ret->bbox.lu=sd->c[0];
7573                         ret->bbox.rl=sd->c[0];
7574                         ret->count=sd->count;
7575                         ret->height=height;
7576                         for (i = 0 ; i < sd->count ; i++) {
7577                                 ret->c[i]=sd->c[i];
7578                                 coord_rect_extend(&ret->bbox, sd->c+i);
7579                         }
7580                 }
7581                 street_data_free(sd);
7582         }
7583         return ret;
7584 }
7585
7586
7587 /**
7588  * @brief Displays Route Height Profile
7589  *
7590  * @li The name of the active vehicle
7591  * @param wm The button that was pressed.
7592  * @param v Unused
7593  */
7594 static void
7595 gui_internal_cmd2_route_height_profile(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7596 {
7597
7598
7599         struct widget * menu, *box;
7600
7601         struct map * map=NULL;
7602         struct map_rect * mr=NULL;
7603         struct route * route;
7604         struct item * item =NULL;
7605         struct mapset *ms;
7606         struct mapset_handle *msh;
7607         int x,i,first=1,dist=0;
7608         struct coord c,last,res;
7609         struct coord_rect rbbox,dbbox;
7610         struct map_selection sel;
7611         struct heightline *heightline,*heightlines=NULL;
7612         struct diagram_point *min,*diagram_point,*diagram_points=NULL;
7613         sel.next=NULL;
7614         sel.order=18;
7615         sel.range.min=type_height_line_1;
7616         sel.range.max=type_height_line_3;
7617
7618
7619         menu=gui_internal_menu(this,_("Height Profile"));
7620         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
7621         gui_internal_widget_append(menu, box);
7622         route = navit_get_route(this->nav);
7623         if (route)
7624                 map = route_get_map(route);
7625         if(map)
7626                 mr = map_rect_new(map,NULL);
7627         if(mr) {
7628                 while((item = map_rect_get_item(mr))) {
7629                         while (item_coord_get(item, &c, 1)) {
7630                                 if (first) {
7631                                         first=0;
7632                                         sel.u.c_rect.lu=c;
7633                                         sel.u.c_rect.rl=c;
7634                                 } else
7635                                         coord_rect_extend(&sel.u.c_rect, &c);
7636                         }
7637                 }
7638                 map_rect_destroy(mr);
7639                 ms=navit_get_mapset(this->nav);
7640                 if (!first && ms) {
7641                         msh=mapset_open(ms);
7642                         while ((map=mapset_next(msh, 1))) {
7643                                 mr=map_rect_new(map, &sel);
7644                                 if (mr) {
7645                                         while((item = map_rect_get_item(mr))) {
7646                                                 if (item->type >= sel.range.min && item->type <= sel.range.max) {
7647                                                         heightline=item_get_heightline(item);
7648                                                         if (heightline) {
7649                                                                 heightline->next=heightlines;
7650                                                                 heightlines=heightline;
7651                                                         }
7652                                                 }
7653                                         }
7654                                         map_rect_destroy(mr);
7655                                 }
7656                         }
7657                         mapset_close(msh);
7658                 }
7659         }
7660         map=NULL;
7661         mr=NULL;
7662         if (route)
7663                 map = route_get_map(route);
7664         if(map)
7665                 mr = map_rect_new(map,NULL);
7666         if(mr && heightlines) {
7667                 while((item = map_rect_get_item(mr))) {
7668                         first=1;
7669                         while (item_coord_get(item, &c, 1)) {
7670                                 if (first)
7671                                         first=0;
7672                                 else {
7673                                         heightline=heightlines;
7674                                         rbbox.lu=last;
7675                                         rbbox.rl=last;
7676                                         coord_rect_extend(&rbbox, &c);
7677                                         while (heightline) {
7678                                                 if (coord_rect_overlap(&rbbox, &heightline->bbox)) {
7679                                                         for (i = 0 ; i < heightline->count - 1; i++) {
7680                                                                 if (heightline->c[i].x != heightline->c[i+1].x || heightline->c[i].y != heightline->c[i+1].y) {
7681                                                                         if (line_intersection(heightline->c+i, heightline->c+i+1, &last, &c, &res)) {
7682                                                                                 diagram_point=g_new(struct diagram_point, 1);
7683                                                                                 diagram_point->c.x=dist+transform_distance(projection_mg, &last, &res);
7684                                                                                 diagram_point->c.y=heightline->height;
7685                                                                                 diagram_point->next=diagram_points;
7686                                                                                 diagram_points=diagram_point;
7687                                                                                 dbg(0,"%d %d\n", diagram_point->c.x, diagram_point->c.y);
7688                                                                         }
7689                                                                 }
7690                                                         }
7691                                                 }
7692                                                 heightline=heightline->next;
7693                                         }
7694                                         dist+=transform_distance(projection_mg, &last, &c);
7695                                 }
7696                                 last=c;
7697                         }
7698
7699                 }
7700                 map_rect_destroy(mr);
7701         }
7702
7703
7704         gui_internal_menu_render(this);
7705         first=1;
7706         diagram_point=diagram_points;
7707         while (diagram_point) {
7708                 if (first) {
7709                         dbbox.lu=diagram_point->c;
7710                         dbbox.rl=diagram_point->c;
7711                         first=0;
7712                 } else
7713                         coord_rect_extend(&dbbox, &diagram_point->c);
7714                 diagram_point=diagram_point->next;
7715         }
7716         dbg(0,"%d %d %d %d\n", dbbox.lu.x, dbbox.lu.y, dbbox.rl.x, dbbox.rl.y);
7717         if (dbbox.rl.x > dbbox.lu.x && dbbox.lu.x*100/(dbbox.rl.x-dbbox.lu.x) <= 25)
7718                 dbbox.lu.x=0;
7719         if (dbbox.lu.y > dbbox.rl.y && dbbox.rl.y*100/(dbbox.lu.y-dbbox.rl.y) <= 25)
7720                 dbbox.rl.y=0;
7721         dbg(0,"%d,%d %dx%d\n", box->p.x, box->p.y, box->w, box->h);
7722         x=dbbox.lu.x;
7723         first=1;
7724         for (;;) {
7725                 struct point p[2];
7726                 min=NULL;
7727                 diagram_point=diagram_points;
7728                 while (diagram_point) {
7729                         if (diagram_point->c.x >= x && (!min || min->c.x > diagram_point->c.x))
7730                                 min=diagram_point;
7731                         diagram_point=diagram_point->next;
7732                 }
7733                 if (! min)
7734                         break;
7735                 p[1].x=(min->c.x-dbbox.lu.x)*(box->w-10)/(dbbox.rl.x-dbbox.lu.x)+box->p.x+5;
7736                 p[1].y=(min->c.y-dbbox.rl.y)*(box->h-10)/(dbbox.lu.y-dbbox.rl.y)+box->p.y+5;
7737                 dbg(0,"%d,%d=%d,%d\n",min->c.x, min->c.y, p[1].x,p[1].y);
7738                 graphics_draw_circle(this->gra, this->foreground, &p[1], 2);
7739                 if (first)
7740                         first=0;
7741                 else
7742                         graphics_draw_lines(this->gra, this->foreground, p, 2);
7743                 p[0]=p[1];
7744                 x=min->c.x+1;
7745         }
7746
7747
7748 }
7749
7750
7751 static void
7752 gui_internal_cmd2_locale(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7753 {
7754         struct widget *menu,*wb,*w;
7755         char *text;
7756
7757         graphics_draw_mode(this->gra, draw_mode_begin);
7758         menu=gui_internal_menu(this, _("Show Locale"));
7759         menu->spx=this->spacing*10;
7760         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
7761         gui_internal_widget_append(menu, wb);
7762         text=g_strdup_printf("LANG=%1$s (1=%3$s 2=%2$s)",getenv("LANG"),"2","1");
7763         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7764         w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7765         g_free(text);
7766 #ifdef HAVE_API_WIN32_BASE
7767         {
7768                 char country[32],lang[32];
7769 #ifdef HAVE_API_WIN32_CE
7770                 wchar_t wcountry[32],wlang[32];
7771
7772                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
7773                 WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
7774 #else
7775                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, lang, sizeof(lang));
7776 #endif
7777                 text=g_strdup_printf("LOCALE_SABBREVLANGNAME=%s",lang);
7778                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7779                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7780                 g_free(text);
7781 #ifdef HAVE_API_WIN32_CE
7782                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
7783                 WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
7784 #else
7785                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, country, sizeof(country));
7786 #endif
7787                 text=g_strdup_printf("LOCALE_SABBREVCTRYNAME=%s",country);
7788                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7789                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7790                 g_free(text);
7791         }
7792 #endif
7793
7794         gui_internal_menu_render(this);
7795         graphics_draw_mode(this->gra, draw_mode_end);
7796 }
7797
7798 static void
7799 gui_internal_cmd2_about(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7800 {
7801         struct widget *menu,*wb,*w;
7802         char *text;
7803
7804         graphics_draw_mode(this->gra, draw_mode_begin);
7805         menu=gui_internal_menu(this, _("About Navit"));
7806         menu->spx=this->spacing*10;
7807         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand);
7808         gui_internal_widget_append(menu, wb);
7809
7810         //Icon
7811         gui_internal_widget_append(wb, w=gui_internal_image_new(this, image_new_xs(this, "navit")));
7812         w->flags=gravity_top_center|orientation_horizontal|flags_fill;
7813
7814         //app name
7815         text=g_strdup_printf("%s",PACKAGE_NAME);
7816         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7817         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7818         g_free(text);
7819
7820         //Version
7821         text=g_strdup_printf("%s",version);
7822         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7823         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7824         g_free(text);
7825
7826         //Site
7827         text=g_strdup_printf("http://www.navit-project.org/");
7828         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7829         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7830         g_free(text);
7831
7832         //Authors
7833         text=g_strdup_printf("%s:",_("By"));
7834         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7835         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7836         g_free(text);
7837         text=g_strdup_printf("Martin Schaller");
7838         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7839         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7840         g_free(text);
7841         text=g_strdup_printf("Michael Farmbauer");
7842         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7843         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7844         g_free(text);
7845         text=g_strdup_printf("Alexander Atanasov");
7846         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7847         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7848         g_free(text);
7849         text=g_strdup_printf("Pierre Grandin");
7850         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7851         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7852         g_free(text);
7853
7854         //Contributors
7855         text=g_strdup_printf("%s",_("And all the Navit Team"));
7856         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7857         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7858         g_free(text);
7859         text=g_strdup_printf("%s",_("members and contributors."));
7860         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7861         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7862         g_free(text);
7863
7864         gui_internal_menu_render(this);
7865         graphics_draw_mode(this->gra, draw_mode_end);
7866 }
7867
7868 /**
7869  * @brief handles the 'next page' table event.
7870  * A callback function that is invoked when the 'next page' button is pressed
7871  * to advance the contents of a table widget.
7872  *
7873  * @param this The graphics context.
7874  * @param wm The button widget that was pressed.
7875  */
7876 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data)
7877 {
7878         struct widget * table_widget=NULL;
7879         struct table_data * table_data = NULL;
7880
7881         if(wm)
7882                 table_widget = (struct widget * ) wm->data;
7883         else 
7884                 table_widget = data;
7885
7886         if(table_widget && table_widget->type==widget_table)
7887                 table_data = (struct table_data*) table_widget->data;
7888
7889         if(table_data)
7890         {
7891                 GList *l=g_list_next(table_data->bottom_row);
7892                 if(l)   {
7893                         gui_internal_table_hide_rows(table_data);
7894                         table_data->top_row=l;
7895                 }
7896         }
7897
7898         if(wm)
7899                 wm->state&= ~STATE_HIGHLIGHTED;
7900
7901         gui_internal_menu_render(this);
7902 }
7903
7904
7905
7906 /**
7907  * @brief handles the 'previous page' table event.
7908  * A callback function that is invoked when the 'previous page' button is pressed
7909  * to go back in the contents of a table widget.
7910  *
7911  * @param this The graphics context.
7912  * @param wm The button widget that was pressed.
7913  */
7914 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data)
7915 {
7916         struct widget * table_widget = NULL;
7917         struct table_data * table_data = NULL;
7918         
7919         if(wm)
7920                 table_widget=(struct widget * ) wm->data;
7921         else 
7922                 table_widget=(struct widget * ) data;
7923
7924         if(table_widget && table_widget->type==widget_table)
7925                 table_data = (struct table_data*) table_widget->data;
7926
7927         if(table_data)  {
7928                 int bottomy=table_widget->p.y+table_widget->h;
7929                 int n;
7930                 GList *top=table_data->top_row;
7931                 struct widget *w=(struct widget*)top->data;
7932                 if(table_data->button_box->p.y!=0) {
7933                     bottomy=table_data->button_box->p.y;
7934                 }
7935                 n=(bottomy-w->p.y)/w->h;
7936                 while(n-- > 0 && (top=g_list_previous(top))!=NULL);
7937                 gui_internal_table_hide_rows(table_data);
7938                 table_data->top_row=top;
7939         }
7940         if(wm)
7941                 wm->state&= ~STATE_HIGHLIGHTED;
7942         gui_internal_menu_render(this);
7943 }
7944
7945
7946 /**
7947  * @brief deallocates a table_data structure.
7948  *
7949  */
7950 void gui_internal_table_data_free(void * p)
7951 {
7952
7953
7954         /**
7955          * @note button_box and its children (next_button,prev_button)
7956          * have their memory managed by the table itself.
7957          */
7958         g_free(p);
7959
7960
7961 }
7962
7963
7964 /**
7965  * @brief Called when the route is updated.
7966  */
7967 void gui_internal_route_update(struct gui_priv * this, struct navit * navit, struct vehicle *v)
7968 {
7969
7970         if(this->route_data.route_showing) {
7971                 gui_internal_populate_route_table(this,navit);
7972                 graphics_draw_mode(this->gra, draw_mode_begin);
7973                 gui_internal_menu_render(this);
7974                 graphics_draw_mode(this->gra, draw_mode_end);
7975         }
7976
7977
7978 }
7979
7980
7981 /**
7982  * @brief Called when the route screen is closed (deallocated).
7983  *
7984  * The main purpose of this function is to remove the widgets from
7985  * references route_data because those widgets are about to be freed.
7986  */
7987 void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w)
7988 {
7989         if(this_) {
7990                 this_->route_data.route_showing=0;
7991                 this_->route_data.route_table=NULL;
7992                 g_free(w);
7993         }
7994
7995 }
7996
7997 /**
7998  * @brief Populates the route  table with route information
7999  *
8000  * @param this The gui context
8001  * @param navit The navit object
8002  */
8003 void gui_internal_populate_route_table(struct gui_priv * this,
8004                                        struct navit * navit)
8005 {
8006         struct map * map=NULL;
8007         struct map_rect * mr=NULL;
8008         struct navigation * nav = NULL;
8009         struct item * item =NULL;
8010         struct attr attr;
8011         struct widget * label = NULL;
8012         struct widget * row = NULL;
8013         struct coord c;
8014         nav = navit_get_navigation(navit);
8015         if(!nav) {
8016                 return;
8017         }
8018         map = navigation_get_map(nav);
8019         if(map)
8020           mr = map_rect_new(map,NULL);
8021         if(mr) {
8022                 gui_internal_widget_table_clear(this,this->route_data.route_table);
8023                 while((item = map_rect_get_item(mr))) {
8024                         if(item_attr_get(item,attr_navigation_long,&attr)) {
8025                           row = gui_internal_widget_table_row_new(this,
8026                                                                   gravity_left
8027                                                                   | flags_fill
8028                                                                   | orientation_horizontal);
8029                           gui_internal_widget_append(this->route_data.route_table,row);
8030
8031                           label = gui_internal_label_new(this,attr.u.str);
8032                           gui_internal_widget_append(row,label);
8033
8034                           label->item=*item;
8035                           item_coord_get(item, &c, 1);
8036                           label->c.x=c.x;
8037                           label->c.y=c.y;
8038                           label->c.pro=map_projection(map);
8039                           label->func=gui_internal_cmd_position;
8040                           label->state|=STATE_SENSITIVE;
8041                           label->data=(void*)2;   
8042                         }
8043
8044                 }
8045
8046         }
8047 }
8048
8049 static struct command_table commands[] = {
8050         {"abort_navigation",command_cast(gui_internal_cmd2_abort_navigation)},
8051         {"back",command_cast(gui_internal_cmd2_back)},
8052         {"back_to_map",command_cast(gui_internal_cmd2_back_to_map)},
8053         {"bookmarks",command_cast(gui_internal_cmd2_bookmarks)},
8054         {"formerdests",command_cast(gui_internal_cmd_formerdests)},
8055         {"get_data",command_cast(gui_internal_get_data)},
8056         {"locale",command_cast(gui_internal_cmd2_locale)},
8057         {"log",command_cast(gui_internal_cmd_log)},
8058         {"menu",command_cast(gui_internal_cmd_menu2)},
8059         {"position",command_cast(gui_internal_cmd2_position)},
8060         {"pois",command_cast(gui_internal_cmd2_pois)},
8061         {"refresh",command_cast(gui_internal_cmd2_refresh)},
8062         {"route_description",command_cast(gui_internal_cmd2_route_description)},
8063         {"route_height_profile",command_cast(gui_internal_cmd2_route_height_profile)},
8064         {"set",command_cast(gui_internal_cmd2_set)},
8065         {"setting_layout",command_cast(gui_internal_cmd2_setting_layout)},
8066         {"setting_maps",command_cast(gui_internal_cmd2_setting_maps)},
8067         {"setting_rules",command_cast(gui_internal_cmd2_setting_rules)},
8068         {"setting_vehicle",command_cast(gui_internal_cmd2_setting_vehicle)},
8069         {"town",command_cast(gui_internal_cmd2_town)},
8070         {"quit",command_cast(gui_internal_cmd2_quit)},
8071         {"write",command_cast(gui_internal_cmd_write)},
8072         {"about",command_cast(gui_internal_cmd2_about)},
8073
8074 };
8075
8076
8077 //##############################################################################################################
8078 //# Description:
8079 //# Comment:
8080 //# Authors: Martin Schaller (04/2008)
8081 //##############################################################################################################
8082 static struct gui_priv * gui_internal_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs, struct gui *gui)
8083 {
8084         struct color color_white={0xffff,0xffff,0xffff,0xffff};
8085         struct color color_black={0x0,0x0,0x0,0xffff};
8086         struct color back2_color={0x4141,0x4141,0x4141,0xffff};
8087
8088         struct gui_priv *this;
8089         struct attr *attr;
8090         *meth=gui_internal_methods;
8091         this=g_new0(struct gui_priv, 1);
8092         this->nav=nav;
8093
8094         this->self.type=attr_gui;
8095         this->self.u.gui=gui;
8096
8097         if ((attr=attr_search(attrs, NULL, attr_menu_on_map_click)))
8098                 this->menu_on_map_click=attr->u.num;
8099         else
8100                 this->menu_on_map_click=1;
8101
8102         if ((attr=attr_search(attrs, NULL, attr_on_map_click)))
8103                 this->on_map_click=g_strdup(attr->u.str);
8104
8105         if ((attr=attr_search(attrs, NULL, attr_signal_on_map_click)))
8106                 this->signal_on_map_click=attr->u.num;
8107
8108         if ((attr=attr_search(attrs, NULL, attr_callback_list))) {
8109                 command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this);
8110         }
8111
8112         if( (attr=attr_search(attrs,NULL,attr_font_size)))
8113         {
8114           this->config.font_size=attr->u.num;
8115         }
8116         else
8117         {
8118           this->config.font_size=-1;
8119         }
8120         if( (attr=attr_search(attrs,NULL,attr_icon_xs)))
8121         {
8122           this->config.icon_xs=attr->u.num;
8123         }
8124         else
8125         {
8126           this->config.icon_xs=-1;
8127         }
8128         if( (attr=attr_search(attrs,NULL,attr_icon_l)))
8129         {
8130           this->config.icon_l=attr->u.num;
8131         }
8132         else
8133         {
8134           this->config.icon_l=-1;
8135         }
8136         if( (attr=attr_search(attrs,NULL,attr_icon_s)))
8137         {
8138           this->config.icon_s=attr->u.num;
8139         }
8140         else
8141         {
8142           this->config.icon_s=-1;
8143         }
8144         if( (attr=attr_search(attrs,NULL,attr_spacing)))
8145         {
8146           this->config.spacing=attr->u.num;
8147         }
8148         else
8149         {
8150           this->config.spacing=-1;
8151         }
8152         if( (attr=attr_search(attrs,NULL,attr_gui_speech)))
8153         {
8154           this->speech=attr->u.num;
8155         }
8156         if( (attr=attr_search(attrs,NULL,attr_keyboard)))
8157           this->keyboard=attr->u.num;
8158         else
8159           this->keyboard=1;
8160
8161     if( (attr=attr_search(attrs,NULL,attr_fullscreen)))
8162       this->fullscreen=attr->u.num;
8163
8164         if( (attr=attr_search(attrs,NULL,attr_flags)))
8165               this->flags=attr->u.num;
8166         if( (attr=attr_search(attrs,NULL,attr_background_color)))
8167               this->background_color=*attr->u.color;
8168         else
8169               this->background_color=color_black;
8170         if( (attr=attr_search(attrs,NULL,attr_background_color2)))
8171                 this->background2_color=*attr->u.color;
8172         else
8173                 this->background2_color=back2_color;
8174         if( (attr=attr_search(attrs,NULL,attr_text_color)))
8175               this->text_foreground_color=*attr->u.color;
8176         else
8177               this->text_foreground_color=color_white;
8178         this->text_background_color=color_black;
8179         if( (attr=attr_search(attrs,NULL,attr_columns)))
8180               this->cols=attr->u.num;
8181         if( (attr=attr_search(attrs,NULL,attr_osd_configuration)))
8182               this->osd_configuration=*attr;
8183
8184         if( (attr=attr_search(attrs,NULL,attr_pitch)))
8185               this->pitch=attr->u.num;
8186         else
8187                 this->pitch=20;
8188         if( (attr=attr_search(attrs,NULL,attr_flags_town)))
8189                 this->flags_town=attr->u.num;
8190         else
8191                 this->flags_town=-1;
8192         if( (attr=attr_search(attrs,NULL,attr_flags_street)))
8193                 this->flags_street=attr->u.num;
8194         else
8195                 this->flags_street=-1;
8196         if( (attr=attr_search(attrs,NULL,attr_flags_house_number)))
8197                 this->flags_house_number=attr->u.num;
8198         else
8199                 this->flags_house_number=-1;
8200         if( (attr=attr_search(attrs,NULL,attr_radius)))
8201                 this->radius=attr->u.num;
8202         else
8203                 this->radius=10;
8204         this->data.priv=this;
8205         this->data.gui=&gui_internal_methods_ext;
8206         this->data.widget=&gui_internal_widget_methods;
8207         this->cbl=callback_list_new();
8208
8209         return this;
8210 }
8211
8212 //##############################################################################################################
8213 //# Description:
8214 //# Comment:
8215 //# Authors: Martin Schaller (04/2008)
8216 //##############################################################################################################
8217 void plugin_init(void)
8218 {
8219         plugin_register_gui_type("internal", gui_internal_new);
8220 }
8221
8222 static void
8223 gui_internal_destroy(struct gui_priv *this)
8224 {
8225
8226         graphics_font_destroy(this->fonts[0]);
8227         graphics_font_destroy(this->fonts[1]);
8228         graphics_font_destroy(this->fonts[2]);
8229         attr_free(this->click_coord_geo);
8230         attr_free(this->position_coord_geo);
8231         g_free(this->country_iso2);
8232         g_free(this->href);
8233         g_free(this->html_text);
8234         g_free(this->on_map_click);
8235         g_free(this);
8236 }