Simple fixes to get navit compiling with MSVC
[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 (*free) (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->free)
1596                 w->free(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
3963 /**
3964   * The "Bookmarks" section of the OSD
3965   * 
3966   */
3967
3968 static void
3969 gui_internal_cmd_bookmarks(struct gui_priv *this, struct widget *wm, void *data)
3970 {
3971         struct attr attr,mattr;
3972         struct item *item;
3973         char *label_full,*prefix=0;
3974         int plen=0,hassub,found=0;
3975         struct widget *wb,*w,*wbm;
3976         struct coord c;
3977         struct widget *tbl, *row;
3978
3979         if (data)
3980                 prefix=g_strdup(data);
3981         else {
3982                 if (wm && wm->prefix)
3983                         prefix=g_strdup(wm->prefix);
3984         }
3985         if ( prefix )
3986                 plen=strlen(prefix);
3987
3988         gui_internal_prune_menu_count(this, 1, 0);
3989         wb=gui_internal_menu(this, _("Bookmarks"));
3990         wb->background=this->background;
3991         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3992         //w->spy=this->spacing*3;
3993         gui_internal_widget_append(wb, w);
3994
3995         if(navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL) ) {
3996                 if (!plen) {
3997                         bookmarks_move_root(mattr.u.bookmarks);
3998                 } else {
3999                         if (!strcmp(prefix,"..")) {
4000                                 bookmarks_move_up(mattr.u.bookmarks);
4001                                 g_free(prefix);
4002                                 prefix=g_strdup(bookmarks_item_cwd(mattr.u.bookmarks));
4003                                 if (prefix) {
4004                                         plen=strlen(prefix);
4005                                 } else {
4006                                         plen=0;
4007                                 }
4008                         } else {
4009                                 bookmarks_move_down(mattr.u.bookmarks,prefix);
4010                         }
4011                         
4012                           // "Back" button, when inside a bookmark folder
4013                           
4014                         if (plen) {
4015                                 wbm=gui_internal_button_new_with_callback(this, "..",
4016                                         image_new_xs(this, "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
4017                                                 gui_internal_cmd_bookmarks, NULL);
4018                                                 wbm->prefix=g_strdup("..");
4019                                 gui_internal_widget_append(w, wbm);
4020
4021                                 // load bookmark folder as Waypoints, if any
4022                                 if (bookmarks_get_bookmark_count(mattr.u.bookmarks) > 0){
4023                                         wbm=gui_internal_button_new_with_callback(this, _("Bookmarks as Waypoints"),
4024                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4025                                                         gui_internal_cmd_load_bookmarks_as_waypoints, NULL);
4026                                         wbm->prefix=g_strdup(prefix);
4027                                         gui_internal_widget_append(w, wbm);
4028                                 }
4029
4030                                 // save Waypoints in bookmark folder, if route exists
4031                                 if (navit_get_destination_count(this->nav) > 0){
4032                                         if (bookmarks_get_bookmark_count(mattr.u.bookmarks)==0){
4033                                                 wbm=gui_internal_button_new_with_callback(this, _("Save Waypoints"),
4034                                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4035                                                                         gui_internal_cmd_replace_bookmarks_from_waypoints, NULL);
4036                                         }else{
4037                                                 wbm=gui_internal_button_new_with_callback(this, _("Replace Waypoints"),
4038                                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4039                                                                         gui_internal_cmd_replace_bookmarks_from_waypoints, NULL);
4040                                         }
4041                                         wbm->prefix=g_strdup(prefix);
4042                                         gui_internal_widget_append(w, wbm);
4043                                 }
4044
4045                                 // delete empty folder
4046                                 if (bookmarks_get_bookmark_count(mattr.u.bookmarks)==0){
4047                                         gui_internal_widget_append(w,
4048                                                         wbm=gui_internal_button_new_with_callback(this, _("Delete Folder"),
4049                                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4050                                                         gui_internal_cmd_delete_bookmark_folder, NULL));
4051                                         wbm->prefix=g_strdup(prefix);
4052                                 }
4053
4054                         }
4055                 }
4056                 
4057                 // Adds the Bookmark folders
4058                 wbm=gui_internal_button_new_with_callback(this, _("Add Bookmark folder"),
4059                             image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4060                                 gui_internal_cmd_add_bookmark_folder2, NULL);
4061                 gui_internal_widget_append(w, wbm);
4062
4063                 // Pastes the Bookmark
4064                 wbm=gui_internal_button_new_with_callback(this, _("Paste bookmark"),
4065                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
4066                                 gui_internal_cmd_paste_bookmark, NULL);
4067                 gui_internal_widget_append(w, wbm);
4068
4069                 bookmarks_item_rewind(mattr.u.bookmarks);
4070                                 
4071                 tbl=gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
4072                 gui_internal_widget_append(w,tbl);
4073
4074                 while ((item=bookmarks_get_item(mattr.u.bookmarks))) {
4075                         if (!item_attr_get(item, attr_label, &attr)) continue;
4076                         label_full=attr.u.str;
4077                         dbg(0,"full_labled: %s\n",label_full);
4078                         
4079                         // hassub == 1 if the item type is a sub-folder
4080                         if (item->type == type_bookmark_folder) {
4081                                 hassub=1;
4082                         } else {
4083                                 hassub=0;
4084                         }
4085                         
4086                         row=gui_internal_widget_table_row_new(this,gravity_left| flags_fill| orientation_horizontal);
4087                         gui_internal_widget_append(tbl, row);
4088                         wbm=gui_internal_button_new_with_callback(this, label_full,
4089                                 image_new_xs(this, hassub ? "gui_inactive" : "gui_active" ), gravity_left_center|orientation_horizontal|flags_fill,
4090                                         hassub ? gui_internal_cmd_bookmarks : gui_internal_cmd_position, NULL);
4091
4092                         gui_internal_widget_append(row,wbm);
4093                         if (item_coord_get(item, &c, 1)) {
4094                                 wbm->c.x=c.x;
4095                                 wbm->c.y=c.y;
4096                                 wbm->c.pro=bookmarks_get_projection(mattr.u.bookmarks);
4097                                 wbm->name=g_strdup_printf(_("Bookmark %s"),label_full);
4098                                 wbm->text=g_strdup(label_full);
4099                                 if (!hassub) {
4100                                         wbm->data=(void*)7;//Mark us as a bookmark
4101                                 }
4102                                 wbm->prefix=g_strdup(label_full);
4103                         } else {
4104                                 gui_internal_widget_destroy(this, row);
4105                         }
4106                 }
4107         }
4108
4109         g_free(prefix);
4110
4111         if (found)
4112                 gui_internal_check_exit(this);
4113         else
4114                 gui_internal_menu_render(this);
4115 }
4116
4117 static void
4118 gui_internal_cmd2_bookmarks(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4119 {
4120         char *str=NULL;
4121         if (in && in[0] && ATTR_IS_STRING(in[0]->type)) {
4122                 str=in[0]->u.str;
4123         }
4124         gui_internal_cmd_bookmarks(this, NULL, str);
4125 }
4126
4127
4128 static void
4129 gui_internal_cmd_formerdests(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4130 {
4131         struct widget *wb,*w,*wbm,*tbl=NULL;
4132         struct map *formerdests;
4133         struct map_rect *mr_formerdests;
4134         struct item *item;
4135         struct attr attr;
4136         char *label_full;
4137         enum projection projection;
4138
4139         gui_internal_prune_menu_count(this, 1, 0);
4140         wb=gui_internal_menu(this, _("Former Destinations"));
4141         wb->background=this->background;
4142
4143         w=gui_internal_box_new(this,
4144                         gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4145         w->spy=this->spacing*2;
4146         gui_internal_widget_append(wb, w);
4147
4148         formerdests=read_former_destinations_from_file();
4149         mr_formerdests=map_rect_new(formerdests, NULL);
4150         projection = map_projection(formerdests);
4151         while ((item=map_rect_get_item(mr_formerdests))) {
4152                 struct coord c;
4153                 struct widget *row;
4154                 if (!item_attr_get(item, attr_label, &attr)) continue;
4155                 if(!tbl) {
4156                         tbl=gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand | orientation_vertical,1);
4157                         gui_internal_widget_append(w,tbl);
4158                 }
4159                 row=gui_internal_widget_table_row_new(this,gravity_left| flags_fill| orientation_vertical);
4160                 gui_internal_widget_prepend(tbl, row);
4161                 label_full=attr.u.str;
4162                 wbm=gui_internal_button_new_with_callback(this, label_full,
4163                                 image_new_xs(this, "gui_active"),
4164                                 gravity_left_center|orientation_horizontal|flags_fill,
4165                                 gui_internal_cmd_position, NULL);
4166                 gui_internal_widget_append(row,wbm);
4167                 if (item_coord_get(item, &c, 1)) {
4168                         wbm->c.x=c.x;
4169                         wbm->c.y=c.y;
4170                         wbm->c.pro=projection;
4171                         wbm->name=g_strdup(label_full);
4172                         wbm->text=g_strdup(label_full);
4173                         wbm->data=(void*)8; //Mark us as a former destination 
4174                         wbm->prefix=g_strdup(label_full);
4175                 }
4176         }
4177         if (!tbl){
4178                 wbm=gui_internal_text_new(this, _("- No former destinations available -"), 
4179                     gravity_left_center|orientation_horizontal|flags_fill);
4180                 gui_internal_widget_append(w, wbm);
4181         }
4182         gui_internal_menu_render(this);
4183         map_rect_destroy(mr_formerdests);
4184 }
4185
4186 static void
4187 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy);
4188
4189 static void gui_internal_keypress_do(struct gui_priv *this, char *key)
4190 {
4191         struct widget *wi,*menu,*search_list;
4192         int len=0;
4193         char *text=NULL;
4194
4195         menu=g_list_last(this->root.children)->data;
4196         wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
4197         if (wi) {
4198                 /* select first item of the searchlist */
4199                 if (*key == NAVIT_KEY_RETURN && (search_list=gui_internal_menu_data(this)->search_list)) {
4200                         GList *l=search_list->children;
4201                         if (l && l->data)
4202                                 gui_internal_highlight_do(this, l->data);
4203                         return; 
4204                 } else if (*key == NAVIT_KEY_BACKSPACE) {
4205                         dbg(0,"backspace\n");
4206                         if (wi->text && wi->text[0]) {
4207                                 len=g_utf8_prev_char(wi->text+strlen(wi->text))-wi->text;
4208                                 wi->text[len]=' ';
4209                                 text=g_strdup_printf("%s ", wi->text);
4210                         }
4211                 } else {
4212                         if (wi->state & STATE_CLEAR) {
4213                                 dbg(0,"wi->state=0x%x\n", wi->state);
4214                                 g_free(wi->text);
4215                                 wi->text=NULL;
4216                                 wi->state &= ~STATE_CLEAR;
4217                                 dbg(0,"wi->state=0x%x\n", wi->state);
4218                         }
4219                         text=g_strdup_printf("%s%s", wi->text ? wi->text : "", key);
4220                 }
4221                 g_free(wi->text);
4222                 wi->text=text;
4223                 if (*key == NAVIT_KEY_BACKSPACE && wi->text) {
4224                         gui_internal_widget_render(this, wi);
4225                         wi->text[len]='\0';
4226                 }
4227                 if (wi->func) {
4228                         wi->reason=2;
4229                         wi->func(this, wi, wi->data);
4230                 }
4231                 gui_internal_widget_render(this, wi);
4232         }
4233 }
4234
4235
4236 static void
4237 gui_internal_cmd_keypress(struct gui_priv *this, struct widget *wm, void *data)
4238 {
4239         struct menu_data *md=gui_internal_menu_data(this);
4240         gui_internal_keypress_do(this, (char *) wm->data);
4241         // Switch to lowercase after the first key is pressed
4242         if (md->keyboard_mode == 2) // Latin
4243                 gui_internal_keyboard_do(this, md->keyboard, 10);
4244         if (md->keyboard_mode == 26) // Umlaut
4245                 gui_internal_keyboard_do(this, md->keyboard, 34);
4246         if ((md->keyboard_mode & ~7) == 40) // Russian/Ukrainian/Belorussian
4247                 gui_internal_keyboard_do(this, md->keyboard, 48);
4248 }
4249
4250 static void
4251 gui_internal_search_idle_end(struct gui_priv *this)
4252 {
4253         if (this->idle) {
4254                 event_remove_idle(this->idle);
4255                 this->idle=NULL;
4256         }
4257         if (this->idle_cb) {
4258                 callback_destroy(this->idle_cb);
4259                 this->idle_cb=NULL;
4260         }
4261 }
4262
4263 static char *
4264 postal_str(struct search_list_result *res, int level)
4265 {
4266         char *ret=NULL;
4267         if (res->town->common.postal)
4268                 ret=res->town->common.postal;
4269         if (res->town->common.postal_mask)
4270                 ret=res->town->common.postal_mask;
4271         if (level == 1)
4272                 return ret;
4273         if (res->street->common.postal)
4274                 ret=res->street->common.postal;
4275         if (res->street->common.postal_mask)
4276                 ret=res->street->common.postal_mask;
4277         if (level == 2)
4278                 return ret;
4279         if (res->house_number->common.postal)
4280                 ret=res->house_number->common.postal;
4281         if (res->house_number->common.postal_mask)
4282                 ret=res->house_number->common.postal_mask;
4283         return ret;
4284 }
4285
4286 static char *
4287 district_str(struct search_list_result *res, int level)
4288 {
4289         char *ret=NULL;
4290         if (res->town->common.district_name)
4291                 ret=res->town->common.district_name;
4292         if (level == 1)
4293                 return ret;
4294         if (res->street->common.district_name)
4295                 ret=res->street->common.district_name;
4296         if (level == 2)
4297                 return ret;
4298         if (res->house_number->common.district_name)
4299                 ret=res->house_number->common.district_name;
4300         return ret;
4301 }
4302
4303 static char *
4304 town_str(struct search_list_result *res, int level, int flags)
4305 {
4306         char *town=res->town->common.town_name;
4307         char *district=district_str(res, level);
4308         char *postal=postal_str(res, level);
4309         char *postal_sep=" ";
4310         char *district_begin=" (";
4311         char *district_end=")";
4312         char *county_sep = ", Co. ";
4313         char *county = res->town->common.county_name;
4314         if (!postal)
4315                 postal_sep=postal="";
4316         if (!district || (flags & 1))
4317                 district_begin=district_end=district="";
4318         if (!county)
4319                 county_sep=county="";
4320
4321         return g_strdup_printf("%s%s%s%s%s%s%s%s", postal, postal_sep, town, district_begin, district, district_end, county_sep, county);
4322 }
4323
4324 static int gui_internal_search_cmp(gconstpointer _a, gconstpointer _b)
4325 {
4326   struct widget *a=(struct widget *)_a, *b=(struct widget *)_b;
4327   char *sa,*sb;
4328   int r;
4329   if(!b)
4330   if((!a || a->type!=widget_table_row || !a->text) && (!b || b->type!=widget_table_row || !b->text))
4331         return 0;
4332   if(!a || a->type!=widget_table_row || !a->text)
4333         return -1;
4334   if(!b || b->type!=widget_table_row || !b->text)
4335         return 1;
4336   r=a->datai-b->datai;
4337   if(r<0)
4338         return -1;
4339   if(r>0)
4340         return 1;
4341   sa=removecase(a->text);
4342   sb=removecase(b->text);
4343   r=strcmp(sa,sb);
4344   g_free(sa);
4345   g_free(sb);
4346   return r;
4347 }
4348
4349 static void
4350 gui_internal_search_idle(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
4351 {
4352         char *text=NULL,*text2=NULL,*name=NULL;
4353         struct search_list_result *res;
4354         struct widget *wc;
4355         struct item *item=NULL;
4356         GList *l;
4357         static char possible_keys[256]="";
4358         struct widget *wi=NULL;
4359         struct widget *wr;
4360
4361         res=search_list_get_result(this->sl);
4362         if (res) {
4363                 gchar* trunk_name = NULL;
4364
4365                 struct widget *menu=g_list_last(this->root.children)->data;
4366                 wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
4367
4368                 if (wi) {
4369                         if (! strcmp(wm_name,"Town"))
4370                                 trunk_name = g_strrstr(res->town->common.town_name, wi->text);
4371                         if (! strcmp(wm_name,"Street"))
4372                         {
4373                                 name=res->street->name;
4374                                 if (name)
4375                                         trunk_name = g_strrstr(name, wi->text);
4376                                 else
4377                                         trunk_name = NULL;
4378                         }
4379
4380                         if (trunk_name) {
4381                                 char next_char = trunk_name[strlen(wi->text)];
4382                                 int i;
4383                                 int len = strlen(possible_keys);
4384                                 for(i = 0; (i<len) && (possible_keys[i] != next_char) ;i++) ;
4385                                 if (i==len || !len) {
4386                                         possible_keys[len]=trunk_name[strlen(wi->text)];
4387                                         possible_keys[len+1]='\0';
4388                                 }
4389                                 dbg(1,"%s %s possible_keys:%s \n", wi->text, res->town->common.town_name, possible_keys);
4390                         }
4391                 } else {
4392                         dbg(0, "Unable to find widget");
4393                 }
4394         }
4395
4396         if (! res) {
4397                 struct menu_data *md;
4398                 gui_internal_search_idle_end(this);
4399
4400                 md=gui_internal_menu_data(this);
4401                 if (md && md->keyboard && !(this->flags & 2048)) {
4402                         GList *lk=md->keyboard->children;
4403                         graphics_draw_mode(this->gra, draw_mode_begin);
4404                         while (lk) {
4405                                 struct widget *child=lk->data;
4406                                 GList *lk2=child->children;
4407                                 while (lk2) {
4408                                         struct widget *child_=lk2->data;
4409                                         lk2=g_list_next(lk2);
4410                                         if (child_->data && strcmp("\b", child_->data)) { // FIXME don't disable special keys
4411                                                 if (strlen(possible_keys) == 0)
4412                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
4413                                                 else if (g_strrstr(possible_keys, child_->data)!=NULL ) {
4414                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
4415                                                 } else {
4416                                                         child_->state&= ~(STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SELECTED) ;
4417                                                 }
4418                                                 gui_internal_widget_render(this,child_);
4419                                         }
4420                                 }
4421                                 lk=g_list_next(lk);
4422                         }
4423                         gui_internal_widget_render(this,md->keyboard);
4424                         graphics_draw_mode(this->gra, draw_mode_end);
4425                 }
4426
4427                 possible_keys[0]='\0';
4428                 return;
4429         }
4430
4431         if (! strcmp(wm_name,"Country")) {
4432                 name=res->country->name;
4433                 item=&res->country->common.item;
4434                 text=g_strdup_printf("%s", res->country->name);
4435         }
4436         if (! strcmp(wm_name,"Town")) {
4437                 item=&res->town->common.item;
4438                 name=res->town->common.town_name;
4439                 text=town_str(res, 1, 0);
4440         }
4441         if (! strcmp(wm_name,"Street")) {
4442                 name=res->street->name;
4443                 item=&res->street->common.item;
4444                 text=g_strdup(res->street->name);
4445                 text2=town_str(res, 2, 1);
4446         }
4447         if (! strcmp(wm_name,"House number")) {
4448                 name=res->house_number->house_number;
4449                 text=g_strdup_printf("%s %s", res->street->name, name);
4450                 text2=town_str(res, 3, 0);
4451         }
4452         dbg(1,"res->country->flag=%s\n", res->country->flag);
4453         wr=gui_internal_widget_table_row_new(this, gravity_left|orientation_horizontal|flags_fill);
4454
4455                 if (!text2) {
4456                         wr->text=g_strdup(text);
4457                         gui_internal_widget_insert_sorted(search_list, wr, gui_internal_search_cmp);
4458                         gui_internal_widget_append(wr,
4459                                 wc=gui_internal_button_new_with_callback(this, text,
4460                                         image_new_xs(this, res->country->flag),
4461                                         gravity_left_center|orientation_horizontal|flags_fill,
4462                                         gui_internal_cmd_position, param));
4463                 } else {
4464                         struct widget *wb;
4465                         wr->text=g_strdup_printf("%s %s",text,text2);
4466
4467                         if (! strcmp(wm_name,"House number") && !res->street->name) {
4468                                 wr->datai=1000;
4469                         } else if(name) {
4470                                 if(!wi)
4471                                         dbg(0,"search text widget is NULL\n");
4472                                 if(wi && strlen(name)==strlen(wi->text)) {
4473                                         dbg(1,"xact %s %s\n",name, wi->text);
4474                                         wr->datai=-1000;
4475                                 } else {
4476                                         dbg(1,"not xact %s %s\n",name, wi->text);
4477                                         wr->datai=0;
4478                                 }
4479                         }
4480                         gui_internal_widget_insert_sorted(search_list, wr, gui_internal_search_cmp);
4481                         
4482                         wc=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
4483                         gui_internal_widget_append(wr, wc);
4484                         gui_internal_widget_append(wc, gui_internal_image_new(this, image_new_xs(this, res->country->flag)));
4485                         wb=gui_internal_box_new(this, gravity_left_center|orientation_vertical|flags_fill);
4486                         gui_internal_widget_append(wc, wb);
4487                         gui_internal_widget_append(wb, gui_internal_label_new(this, text));
4488                         gui_internal_widget_append(wb, gui_internal_label_font_new(this, text2, 1));
4489                         wc->func=gui_internal_cmd_position;
4490                         wc->data=param;
4491                         wc->state |= STATE_SENSITIVE;
4492                         wc->speech=g_strdup(text);
4493                 }
4494                 wc->name=g_strdup(name);
4495                 if (res->c)
4496                         wc->c=*res->c;
4497                 wc->selection_id=res->id;
4498                 if (item)
4499                         wc->item=*item;
4500                 gui_internal_widget_pack(this, search_list);
4501                 l=g_list_last(this->root.children);
4502                 graphics_draw_mode(this->gra, draw_mode_begin);
4503                 gui_internal_widget_render(this, l->data);
4504                 graphics_draw_mode(this->gra, draw_mode_end);
4505         g_free(text);
4506         g_free(text2);
4507 }
4508
4509 static void
4510 gui_internal_search_idle_start(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
4511 {
4512         this->idle_cb=callback_new_4(callback_cast(gui_internal_search_idle), this, wm_name, search_list, param);
4513         this->idle=event_add_idle(50,this->idle_cb);
4514         callback_call_0(this->idle_cb);
4515 }
4516
4517
4518 /**
4519  *
4520  * @param wm The widget that generated the event for the search changed,
4521  *        if this was generated by a key on the virtual keyboard then
4522  *        wm is the key button widget.
4523  */
4524 static void
4525 gui_internal_search_changed(struct gui_priv *this, struct widget *wm, void *data)
4526 {
4527         GList *l;
4528         struct widget *search_list=gui_internal_menu_data(this)->search_list;
4529         void *param=(void *)3;
4530         int minlen=1;
4531
4532         gui_internal_widget_table_clear(this, search_list);
4533
4534         if (! strcmp(wm->name,"Country"))
4535                 param=(void *)4;
4536         if (! strcmp(wm->name,"Street"))
4537                 param=(void *)5;
4538         if (! strcmp(wm->name,"House number"))
4539                 param=(void *)6;
4540         dbg(0,"%s now '%s'\n", wm->name, wm->text);
4541
4542         gui_internal_search_idle_end(this);
4543         if (wm->text && g_utf8_strlen(wm->text, -1) >= minlen) {
4544                 struct attr search_attr;
4545
4546                 dbg(0,"process\n");
4547                 if (! strcmp(wm->name,"Country"))
4548                         search_attr.type=attr_country_all;
4549                 if (! strcmp(wm->name,"Town"))
4550                         search_attr.type=attr_town_or_district_name;
4551                 if (! strcmp(wm->name,"Street"))
4552                         search_attr.type=attr_street_name;
4553                 if (! strcmp(wm->name,"House number"))
4554                         search_attr.type=attr_house_number;
4555                 search_attr.u.str=wm->text;
4556                 search_list_search(this->sl, &search_attr, 1);
4557                 gui_internal_search_idle_start(this, wm->name, search_list, param);
4558         }
4559         l=g_list_last(this->root.children);
4560         gui_internal_widget_render(this, l->data);
4561 }
4562
4563 static struct widget *
4564 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)
4565 {
4566         struct widget *wk;
4567         gui_internal_widget_append(wkbd, wk=gui_internal_button_font_new_with_callback(this, text, font,
4568                 NULL, gravity_center|orientation_vertical, func, data));
4569         wk->data_free=data_free;
4570         wk->background=this->background;
4571         wk->bl=0;
4572         wk->br=0;
4573         wk->bt=0;
4574         wk->bb=0;
4575         wk->w=w;
4576         wk->h=h;
4577         return wk;
4578 }
4579
4580 static struct widget *
4581 gui_internal_keyboard_key(struct gui_priv *this, struct widget *wkbd, char *text, char *key, int w, int h)
4582 {
4583         return gui_internal_keyboard_key_data(this, wkbd, text, 0, gui_internal_cmd_keypress, g_strdup(key), g_free_func,w,h);
4584 }
4585
4586 static void gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data);
4587
4588
4589 // A list of availiable keyboard modes.
4590 struct gui_internal_keyb_mode {
4591     char title[16]; // Label to be displayed on keys that switch to it
4592     int font; // Font size of label
4593     int case_mode; // Mode to switch to when case CHANGE() key is pressed.
4594     int umlaut_mode;  // Mode to switch to when UMLAUT() key is pressed.
4595 } gui_internal_keyb_modes[]= {
4596         /* 0*/ {"ABC", 2,  8, 24},
4597         /* 8*/ {"abc", 2,  0, 32},
4598         /*16*/ {"123", 2,  0, 24},
4599         /*24*/ {"ÄÖÜ", 2, 40, 0},
4600         /*32*/ {"äöü", 2, 32, 8},
4601         /*40*/ {"АБВ", 2, 48,  0},
4602         /*48*/ {"абв", 2, 40,  8}
4603 };
4604
4605
4606 // Some macros that make the keyboard layout easier to visualise in
4607 // the source code. The macros are #undef'd after this function.
4608 #define KEY(x) gui_internal_keyboard_key(this, wkbd, (x), (x), max_w, max_h)
4609 #define SPACER() gui_internal_keyboard_key_data(this, wkbd, "", 0, NULL, NULL, NULL,max_w,max_h)
4610 #define MODE(x) gui_internal_keyboard_key_data(this, wkbd, \
4611                 gui_internal_keyb_modes[(x)/8].title, \
4612                 gui_internal_keyb_modes[(x)/8].font, \
4613                 gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h) \
4614                         -> datai=(mode&7)+((x)&~7)
4615 #define SWCASE() MODE(gui_internal_keyb_modes[mode/8].case_mode)
4616 #define UMLAUT() MODE(gui_internal_keyb_modes[mode/8].umlaut_mode)
4617 static struct widget *
4618 gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode)
4619 {
4620         struct widget *wkbd,*wk;
4621         struct menu_data *md=gui_internal_menu_data(this);
4622         int i, max_w=this->root.w, max_h=this->root.h;
4623         int render=0;
4624         char *space="_";
4625         char *backspace="←";
4626         char *hide="▼";
4627         char *unhide="▲";
4628
4629         if (wkbdb) {
4630                 this->current.x=-1;
4631                 this->current.y=-1;
4632                 gui_internal_highlight(this);
4633                 if (md->keyboard_mode >= 1024)
4634                         render=2;
4635                 else
4636                         render=1;
4637                 gui_internal_widget_children_destroy(this, wkbdb);
4638         } else
4639                 wkbdb=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
4640         md->keyboard=wkbdb;
4641         md->keyboard_mode=mode;
4642         wkbd=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
4643         wkbd->background=this->background;
4644         wkbd->cols=8;
4645         wkbd->spx=0;
4646         wkbd->spy=0;
4647         max_w=max_w/8;
4648         max_h=max_h/8; // Allows 3 results in the list when searching for Towns
4649         wkbd->p.y=max_h*2;
4650         if(mode>=40&&mode<56) { // Russian/Ukrainian/Belarussian layout needs more space...
4651                 max_h=max_h*4/5;
4652                 max_w=max_w*8/9;
4653                 wkbd->cols=9;
4654         }
4655
4656         if (mode >= 0 && mode < 8) {
4657                 for (i = 0 ; i < 26 ; i++) {
4658                         char text[]={'A'+i,'\0'};
4659                         KEY(text);
4660                 }
4661                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4662                 if (mode == 0) {
4663                         KEY("-");
4664                         KEY("'");
4665                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4666                         wk->datai=mode+1024;
4667                 } else {
4668                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4669                         wk->datai=mode+1024;
4670                         SWCASE();
4671                         MODE(16);
4672                         
4673                 }
4674                 UMLAUT();
4675                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4676         }
4677         if (mode >= 8 && mode < 16) {
4678                 for (i = 0 ; i < 26 ; i++) {
4679                         char text[]={'a'+i,'\0'};
4680                         KEY(text);
4681                 }
4682                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4683                 if (mode == 8) {
4684                         KEY("-");
4685                         KEY("'");
4686                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4687                         wk->datai=mode+1024;
4688                 } else {
4689                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4690                         wk->datai=mode+1024;
4691                         SWCASE();
4692                         
4693                         MODE(16);
4694                 }
4695                 UMLAUT();
4696                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4697         }
4698         if (mode >= 16 && mode < 24) {
4699                 for (i = 0 ; i < 10 ; i++) {
4700                         char text[]={'0'+i,'\0'};
4701                         KEY(text);
4702                 }
4703                 KEY("."); KEY("°"); KEY("'"); KEY("\""); KEY("-"); KEY("+");
4704                 KEY("*"); KEY("/"); KEY("("); KEY(")"); KEY("="); KEY("?");
4705
4706                 
4707
4708                 if (mode == 16) {
4709                         for (i = 0 ; i < 5 ; i++) SPACER();
4710                         KEY("-");
4711                         KEY("'");
4712                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4713                         wk->datai=mode+1024;
4714                 } else {
4715                         for (i = 0 ; i < 3 ; i++) SPACER();
4716                         MODE(40);
4717                         MODE(48);
4718                         wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4719                         wk->datai=mode+1024;
4720                         MODE(0);
4721                         MODE(8);
4722                 }
4723                 UMLAUT();
4724                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4725         }
4726         if (mode >= 24 && mode < 32) {
4727                 KEY("Ä"); KEY("Ë"); KEY("Ï"); KEY("Ö"); KEY("Ü"); KEY("Æ"); KEY("Ø"); KEY("Å");
4728                 KEY("Á"); KEY("É"); KEY("Í"); KEY("Ó"); KEY("Ú"); KEY("Š"); KEY("Č"); KEY("Ž");
4729                 KEY("À"); KEY("È"); KEY("Ì"); KEY("Ò"); KEY("Ù"); KEY("Ś"); KEY("Ć"); KEY("Ź");
4730                 KEY("Â"); KEY("Ê"); KEY("Î"); KEY("Ô"); KEY("Û"); SPACER();
4731
4732                 UMLAUT();
4733
4734                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4735         }
4736         if (mode >= 32 && mode < 40) {
4737                 KEY("ä"); KEY("ë"); KEY("ï"); KEY("ö"); KEY("ü"); KEY("æ"); KEY("ø"); KEY("å");
4738                 KEY("á"); KEY("é"); KEY("í"); KEY("ó"); KEY("ú"); KEY("š"); KEY("č"); KEY("ž");
4739                 KEY("à"); KEY("è"); KEY("ì"); KEY("ò"); KEY("ù"); KEY("ś"); KEY("ć"); KEY("ź");
4740                 KEY("â"); KEY("ê"); KEY("î"); KEY("ô"); KEY("û"); KEY("ß");
4741
4742                 UMLAUT();
4743
4744                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4745         }
4746         if (mode >= 40 && mode < 48) {
4747                 KEY("А"); KEY("Б"); KEY("В"); KEY("Г"); KEY("Д"); KEY("Е"); KEY("Ж"); KEY("З"); KEY("И");
4748                 KEY("Й"); KEY("К"); KEY("Л"); KEY("М"); KEY("Н"); KEY("О"); KEY("П"); KEY("Р"); KEY("С");
4749                 KEY("Т"); KEY("У"); KEY("Ф"); KEY("Х"); KEY("Ц"); KEY("Ч"); KEY("Ш"); KEY("Щ"); KEY("Ъ"); 
4750                 KEY("Ы"); KEY("Ь"); KEY("Э"); KEY("Ю"); KEY("Я"); KEY("Ё"); KEY("І"); KEY("Ї"); KEY("Ў");
4751                 SPACER(); SPACER(); SPACER();
4752                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4753
4754                 wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4755                 wk->datai=mode+1024;
4756
4757                 SWCASE();
4758
4759                 MODE(16);
4760
4761                 SPACER();
4762
4763                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4764         }
4765         if (mode >= 48 && mode < 56) {
4766                 KEY("а"); KEY("б"); KEY("в"); KEY("г"); KEY("д"); KEY("е"); KEY("ж"); KEY("з"); KEY("и");
4767                 KEY("й"); KEY("к"); KEY("л"); KEY("м"); KEY("н"); KEY("о"); KEY("п"); KEY("р"); KEY("с");
4768                 KEY("т"); KEY("у"); KEY("ф"); KEY("х"); KEY("ц"); KEY("ч"); KEY("ш"); KEY("щ"); KEY("ъ");
4769                 KEY("ы"); KEY("ь"); KEY("э"); KEY("ю"); KEY("я"); KEY("ё"); KEY("і"); KEY("ї"); KEY("ў");
4770                 SPACER(); SPACER(); SPACER();
4771                 gui_internal_keyboard_key(this, wkbd, space," ",max_w,max_h);
4772                 
4773                 wk=gui_internal_keyboard_key_data(this, wkbd, hide, 0, gui_internal_keyboard_change, wkbdb, NULL,max_w,max_h);
4774                 wk->datai=mode+1024;
4775
4776                 SWCASE();
4777
4778                 MODE(16);
4779
4780                 SPACER();
4781
4782                 gui_internal_keyboard_key(this, wkbd, backspace,"\b",max_w,max_h);
4783         }
4784
4785
4786         if(md->search_list && md->search_list->type==widget_table) {
4787                 struct table_data *td=(struct table_data*)(md->search_list->data);
4788                 td->button_box_hide=mode<1024;
4789         }
4790         
4791         if (mode >= 1024) {
4792                 char *text=NULL;
4793                 int font=0;
4794                 struct widget *wkl;
4795                 mode -= 1024;
4796                 text=gui_internal_keyb_modes[mode/8].title;
4797                 font=gui_internal_keyb_modes[mode/8].font;
4798                 wk=gui_internal_box_new(this, gravity_center|orientation_horizontal|flags_fill);
4799                 wk->func=gui_internal_keyboard_change;
4800                 wk->data=wkbdb;
4801                 wk->background=this->background;
4802                 wk->bl=0;
4803                 wk->br=0;
4804                 wk->bt=0;
4805                 wk->bb=0;
4806                 wk->w=max_w;
4807                 wk->h=max_h;
4808                 wk->datai=mode;
4809                 wk->state |= STATE_SENSITIVE;
4810                 gui_internal_widget_append(wk, wkl=gui_internal_label_new(this, unhide));
4811                 wkl->background=NULL;
4812                 gui_internal_widget_append(wk, wkl=gui_internal_label_font_new(this, text, font));
4813                 wkl->background=NULL;
4814                 gui_internal_widget_append(wkbd, wk);
4815                 if (render)
4816                         render=2;
4817         }
4818         gui_internal_widget_append(wkbdb, wkbd);
4819         if (render == 1) {
4820                 gui_internal_widget_pack(this, wkbdb);
4821                 gui_internal_widget_render(this, wkbdb);
4822         } else if (render == 2) {
4823                 gui_internal_menu_reset_pack(this);
4824                 gui_internal_menu_render(this);
4825         }
4826         return wkbdb;
4827 }
4828 #undef KEY
4829 #undef SPACER
4830 #undef SWCASE
4831 #undef UMLAUT
4832 #undef MODE
4833
4834 static struct widget *
4835 gui_internal_keyboard(struct gui_priv *this, int mode)
4836 {
4837         if (! this->keyboard)
4838                 return NULL;
4839         return gui_internal_keyboard_do(this, NULL, mode);
4840 }
4841
4842 static void
4843 gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data)
4844 {
4845         gui_internal_keyboard_do(this, key->data, key->datai);
4846 }
4847
4848 static void
4849 gui_internal_search_list_set_default_country(struct gui_priv *this)
4850 {
4851         struct attr search_attr, country_name, country_iso2, *country_attr;
4852         struct item *item;
4853         struct country_search *cs;
4854         struct tracking *tracking;
4855         struct search_list_result *res;
4856
4857         country_attr=country_default();
4858         tracking=navit_get_tracking(this->nav);
4859         if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
4860                 country_attr=&search_attr;
4861         if (country_attr) {
4862                 cs=country_search_new(country_attr, 0);
4863                 item=country_search_get_item(cs);
4864                 if (item && item_attr_get(item, attr_country_name, &country_name)) {
4865                         search_attr.type=attr_country_all;
4866                         dbg(0,"country %s\n", country_name.u.str);
4867                         search_attr.u.str=country_name.u.str;
4868                         search_list_search(this->sl, &search_attr, 0);
4869                         while((res=search_list_get_result(this->sl)));
4870                         if(this->country_iso2) {
4871                                 g_free(this->country_iso2);
4872                                 this->country_iso2=NULL;
4873                         }
4874                         if (item_attr_get(item, attr_country_iso2, &country_iso2))
4875                                 this->country_iso2=g_strdup(country_iso2.u.str);
4876                 }
4877                 country_search_destroy(cs);
4878         } else {
4879                 dbg(0,"warning: no default country found\n");
4880                 if (this->country_iso2) {
4881                     dbg(0,"attempting to use country '%s'\n",this->country_iso2);
4882                     search_attr.type=attr_country_iso2;
4883                     search_attr.u.str=this->country_iso2;
4884             search_list_search(this->sl, &search_attr, 0);
4885             while((res=search_list_get_result(this->sl)));
4886                 }
4887         }
4888 }
4889
4890 static void
4891 gui_internal_search_list_new(struct gui_priv *this)
4892 {
4893         struct mapset *ms=navit_get_mapset(this->nav);
4894         if (! this->sl) {
4895                 this->sl=search_list_new(ms);
4896                 gui_internal_search_list_set_default_country(this);
4897         }
4898 }
4899
4900 static void
4901 gui_internal_search_list_destroy(struct gui_priv *this)
4902 {
4903         if (this->sl) {
4904                 search_list_destroy(this->sl);
4905                 this->sl=NULL;
4906         }
4907 }
4908
4909
4910 static void
4911 gui_internal_search(struct gui_priv *this, char *what, char *type, int flags)
4912 {
4913         struct widget *wb,*wk,*w,*wr,*we,*wl,*wnext=NULL;
4914         char *country;
4915         int keyboard_mode=2;
4916         gui_internal_search_list_new(this);
4917         wb=gui_internal_menu(this, what);
4918         w=gui_internal_box_new(this, gravity_center|orientation_vertical|flags_expand|flags_fill);
4919         gui_internal_widget_append(wb, w);
4920         wr=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4921         gui_internal_widget_append(w, wr);
4922         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
4923         gui_internal_widget_append(wr, we);
4924         if (!strcmp(type,"Country")) {
4925                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_town"));
4926                 wnext->func=gui_internal_search_town;
4927         } else if (!strcmp(type,"Town")) {
4928                 if (this->country_iso2) {
4929 #if HAVE_API_ANDROID
4930                         char country_iso2[strlen(this->country_iso2)+1];
4931                         strtolower(country_iso2, this->country_iso2);
4932                         country=g_strdup_printf("country_%s", country_iso2);
4933 #else
4934                         country=g_strdup_printf("country_%s", this->country_iso2);
4935 #endif
4936                 } else
4937                         country=g_strdup("gui_select_country");
4938                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, country)));
4939                 wb->state |= STATE_SENSITIVE;
4940                 if (flags)
4941                         wb->func = gui_internal_search_country;
4942                 else
4943                         wb->func = gui_internal_back;
4944                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_street"));
4945                 wnext->func=gui_internal_search_street;
4946                 g_free(country);
4947         } else if (!strcmp(type,"Street")) {
4948                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_town")));
4949                 wb->state |= STATE_SENSITIVE;
4950                 wb->func = gui_internal_back;
4951                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_house_number"));
4952                 wnext->func=gui_internal_search_house_number;
4953         } else if (!strcmp(type,"House number")) {
4954                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_street")));
4955                 wb->state |= STATE_SENSITIVE;
4956                 wb->func = gui_internal_back;
4957                 keyboard_mode=18;
4958         }
4959         gui_internal_widget_append(we, wk=gui_internal_label_new(this, NULL));
4960         if (wnext) {
4961                 gui_internal_widget_append(we, wnext);
4962                 wnext->state |= STATE_SENSITIVE;
4963         }
4964         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);
4965         gui_internal_widget_append(wr, wl);
4966         gui_internal_menu_data(this)->search_list=wl;
4967         wk->state |= STATE_EDIT|STATE_EDITABLE;
4968         wk->background=this->background;
4969         wk->flags |= flags_expand|flags_fill;
4970         wk->func = gui_internal_search_changed;
4971         wk->name=g_strdup(type);
4972         if (this->keyboard)
4973                 gui_internal_widget_append(w, gui_internal_keyboard(this,keyboard_mode));
4974         gui_internal_menu_render(this);
4975 }
4976
4977 static void
4978 gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data)
4979 {
4980         search_list_select(this->sl, attr_street_name, 0, 0);
4981         gui_internal_search(this,_("House number"),"House number",0);
4982 }
4983
4984 static void
4985 gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data)
4986 {
4987         dbg(0,"id %d\n", widget->selection_id);
4988         search_list_select(this->sl, attr_street_name, 0, 0);
4989         search_list_select(this->sl, attr_street_name, widget->selection_id, 1);
4990         gui_internal_search(this,_("House number"),"House number",0);
4991 }
4992
4993 static void
4994 gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data)
4995 {
4996         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
4997         gui_internal_search(this,_("Street"),"Street",0);
4998 }
4999
5000 static void
5001 gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data)
5002 {
5003         dbg(0,"id %d\n", widget->selection_id);
5004         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
5005         search_list_select(this->sl, attr_town_or_district_name, widget->selection_id, 1);
5006         gui_internal_search(this,_("Street"),"Street",0);
5007 }
5008
5009 static void
5010 gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data)
5011 {
5012         if (this->sl)
5013                 search_list_select(this->sl, attr_country_all, 0, 0);
5014         g_free(this->country_iso2);
5015         this->country_iso2=NULL;
5016         gui_internal_search(this,_("Town"),"Town",0);
5017 }
5018
5019 static void
5020 gui_internal_search_town_in_country(struct gui_priv *this, struct widget *widget)
5021 {
5022         struct search_list_common *slc;
5023         dbg(0,"id %d\n", widget->selection_id);
5024         search_list_select(this->sl, attr_country_all, 0, 0);
5025         slc=search_list_select(this->sl, attr_country_all, widget->selection_id, 1);
5026         if (slc) {
5027                 g_free(this->country_iso2);
5028                 this->country_iso2=g_strdup(((struct search_list_country *)slc)->iso2);
5029         }
5030         gui_internal_search(this,widget->name,"Town",0);
5031 }
5032
5033 static void
5034 gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data)
5035 {
5036         gui_internal_prune_menu_count(this, 1, 0);
5037         gui_internal_search(this,_("Country"),"Country",0);
5038 }
5039
5040 static void
5041 gui_internal_cmd2_town(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5042 {
5043         if (this->sl)
5044                 search_list_select(this->sl, attr_country_all, 0, 0);
5045         gui_internal_search(this,_("Town"),"Town",1);
5046 }
5047
5048 static void
5049 gui_internal_cmd2_setting_layout(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5050 {
5051         struct attr attr;
5052         struct widget *w,*wb,*wl,*row;
5053         struct attr_iter *iter;
5054
5055
5056         wb=gui_internal_menu(this, _("Layout"));
5057         w=gui_internal_widget_table_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill,1);
5058         gui_internal_widget_append(wb, w);
5059         iter=navit_attr_iter_new();
5060         while(navit_get_attr(this->nav, attr_layout, &attr, iter)) {
5061                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5062                 wl=gui_internal_button_navit_attr_new(this, attr.u.layout->name, gravity_left_center|orientation_horizontal|flags_fill,
5063                         &attr, NULL);
5064                 gui_internal_widget_append(row, wl);
5065         }
5066         navit_attr_iter_destroy(iter);
5067         gui_internal_menu_render(this);
5068 }
5069
5070 static char *
5071 gui_internal_cmd_match_expand(char *pattern, struct attr **in)
5072 {
5073         char p,*ret=g_strdup(pattern),*r=ret,*a;
5074         int len;
5075         while ((p=*pattern++)) {
5076                 switch (p) {
5077                 case '*':
5078                         *r='\0';
5079                         a=attr_to_text(*in++,NULL,0);
5080                         len=strlen(ret)+strlen(a)+strlen(pattern)+1;
5081                         r=g_malloc(len);
5082                         strcpy(r, ret);
5083                         strcat(r, a);
5084                         g_free(ret);
5085                         g_free(a);
5086                         ret=r;
5087                         r=ret+strlen(ret);
5088                         break;
5089                 case '\\':
5090                         p=*pattern++;
5091                 default:
5092                         *r++=p;
5093                 }       
5094         }
5095         *r++='\0';
5096         return ret;
5097 }
5098
5099 static int 
5100 gui_internal_match(const char *pattern, const char *string)
5101 {
5102         char p,s;
5103         while ((p=*pattern++)) {
5104                 switch (p) {
5105                 case '*':
5106                         while ((s=*string)) {
5107                                 if (gui_internal_match(pattern,string))
5108                                         return 1;
5109                                 string++;
5110                         }
5111                         break;
5112                 case '\\':
5113                         p=*pattern++;
5114                 default:
5115                         if (*string++ != p)
5116                                 return 0;
5117                 }
5118         }
5119         return 1;
5120 }
5121
5122 static int
5123 gui_internal_set(char *remove, char *add)
5124 {
5125         char *gui_file=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal.txt", NULL);
5126         char *gui_file_new=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal_new.txt", NULL);
5127         FILE *fo=fopen(gui_file_new,"w");
5128         FILE *fi=fopen(gui_file,"r");
5129         char *line=NULL;
5130         int ret;
5131         size_t size=0;
5132         if (fi != NULL){
5133                 while (getline(&line,&size,fi) > 0) {
5134                         int len=strlen(line);
5135                         if (len > 0 && line[len-1] == '\n')
5136                                 line[len-1]='\0';
5137                         dbg(1,"line=%s\n",line);
5138                         if (!gui_internal_match(remove, line))
5139                                 fprintf(fo,"%s\n",line);
5140                 }
5141                 if (line)
5142                         free(line);
5143                 fclose(fi);
5144         }
5145         fprintf(fo,"%s;\n",add);
5146         fclose(fo);
5147         ret=(rename(gui_file_new, gui_file)==0);
5148         g_free(gui_file_new);
5149         g_free(gui_file);
5150
5151         return ret;
5152 }
5153
5154 static void
5155 gui_internal_cmd2_set(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5156 {
5157         char *pattern,*command=NULL;
5158         if (!in || !in[0] || !ATTR_IS_STRING(in[0]->type)) {
5159                 dbg(0,"first parameter missing or wrong type\n");
5160                 return;
5161         }
5162         pattern=in[0]->u.str;
5163         dbg(0,"pattern %s\n",pattern);
5164         if (in[1]) {
5165                 command=gui_internal_cmd_match_expand(pattern, in+1);
5166                 dbg(0,"expand %s\n",command);
5167                 gui_internal_set(pattern, command);
5168                 command_evaluate(&this->self, command);
5169                 g_free(command);
5170         }
5171
5172 }
5173
5174 static void
5175 gui_internal_cmd2_quit(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5176 {
5177         struct attr navit;
5178         gui_internal_prune_menu(this, NULL);
5179         navit.type=attr_navit;
5180         navit.u.navit=this->nav;
5181         config_remove_attr(config, &navit);
5182         gui_internal_destroy(this);
5183         event_main_loop_quit();
5184 }
5185
5186 static void
5187 gui_internal_window_closed(struct gui_priv *this)
5188 {
5189         gui_internal_cmd2_quit(this, NULL, NULL, NULL, NULL);
5190 }
5191
5192 static void
5193 gui_internal_cmd2_abort_navigation(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5194 {
5195         navit_set_destination(this->nav, NULL, NULL, 0);
5196 }
5197
5198 static void
5199 gui_internal_cmd_map_download_do(struct gui_priv *this, struct widget *wm, void *data)
5200 {
5201         char *text=g_strdup_printf(_("Download %s"),wm->name);
5202         struct widget *w, *wb;
5203         struct map *map=data;
5204         double bllon,bllat,trlon,trlat;
5205
5206         wb=gui_internal_menu(this, text);
5207         g_free(text);
5208         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5209         w->spy=this->spacing*3;
5210         gui_internal_widget_append(wb, w);
5211         if (sscanf(wm->prefix,"%lf,%lf,%lf,%lf",&bllon,&bllat,&trlon,&trlat) == 4) {
5212                 struct coord_geo g;
5213                 struct map_selection sel;
5214                 struct map_rect *mr;
5215                 struct item *item;
5216
5217                 sel.next=NULL;
5218                 sel.order=255;
5219                 g.lng=bllon;
5220                 g.lat=trlat;
5221                 transform_from_geo(projection_mg, &g, &sel.u.c_rect.lu);
5222                 g.lng=trlon;
5223                 g.lat=bllat;
5224                 transform_from_geo(projection_mg, &g, &sel.u.c_rect.rl);
5225                 sel.range.min=type_none;
5226                 sel.range.max=type_last;
5227                 mr=map_rect_new(map, &sel);
5228                 while ((item=map_rect_get_item(mr))) {
5229                         dbg(0,"item\n");
5230                 }
5231                 map_rect_destroy(mr);
5232         }
5233         
5234         dbg(0,"bbox=%s\n",wm->prefix);
5235         gui_internal_menu_render(this);
5236 }
5237
5238 static void
5239 gui_internal_cmd_map_download(struct gui_priv *this, struct widget *wm, void *data)
5240 {
5241         struct attr on, off, download_enabled, download_disabled;
5242         struct widget *w,*wb,*wma;
5243         struct map *map=data;
5244         FILE *f;
5245         char *search,buffer[256];
5246         int found,sp_match=0;
5247
5248         dbg(1,"wm=%p prefix=%s\n",wm,wm->prefix);
5249
5250         search=wm->prefix;
5251         if (search) {
5252                 found=0;
5253                 while(search[sp_match] == ' ')
5254                         sp_match++;
5255                 sp_match++;
5256         } else {
5257                 found=1;
5258         }
5259         on.type=off.type=attr_active;
5260         on.u.num=1;
5261         off.u.num=0;
5262         wb=gui_internal_menu(this, wm->name?wm->name:_("Map Download"));
5263         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5264         w->spy=this->spacing*3;
5265         gui_internal_widget_append(wb, w);
5266         if (!search) {
5267                 wma=gui_internal_button_map_attr_new(this, _("Active"), gravity_left_center|orientation_horizontal|flags_fill, map, &on, &off, 1);
5268                 gui_internal_widget_append(w, wma);
5269         }
5270
5271         download_enabled.type=download_disabled.type=attr_update;
5272         download_enabled.u.num=1;
5273         download_disabled.u.num=0;
5274         wma=gui_internal_button_map_attr_new(this
5275                 , _("Download Enabled")
5276                 , gravity_left_center|orientation_horizontal|flags_fill
5277                 , map
5278                 , &download_enabled
5279                 , &download_disabled
5280                 , 0);
5281         gui_internal_widget_append(w, wma);
5282
5283
5284         f=fopen("maps/areas.tsv","r");
5285         while (f && fgets(buffer, sizeof(buffer), f)) {
5286                 char *nl,*description,*description_size,*bbox,*size=NULL;
5287                 int sp=0;
5288                 if ((nl=strchr(buffer,'\n')))
5289                         *nl='\0';
5290                 if ((nl=strchr(buffer,'\r')))
5291                         *nl='\0';
5292                 while(buffer[sp] == ' ')
5293                         sp++;
5294                 if ((bbox=strchr(buffer,'\t')))
5295                         *bbox++='\0';
5296                 if (bbox && (size=strchr(bbox,'\t'))) 
5297                         *size++='\0';
5298                 if (search && !strcmp(buffer, search)) {
5299                         wma=gui_internal_button_new_with_callback(this, _("Download completely"), NULL, 
5300                                 gravity_left_center|orientation_horizontal|flags_fill, gui_internal_cmd_map_download_do, map);
5301                         wma->name=g_strdup(buffer+sp);
5302                         wma->prefix=g_strdup(bbox);
5303                         gui_internal_widget_append(w, wma);
5304                         found=1;
5305                 } else if (sp < sp_match)
5306                         found=0;
5307                 if (sp == sp_match && found && buffer[sp]) {
5308                         description=g_strdup(buffer+sp);
5309                         if (size)
5310                                 description_size=g_strdup_printf("%s (%s)",description,size);
5311                         else
5312                                 description_size=g_strdup(description);
5313                         wma=gui_internal_button_new_with_callback(this, description_size, NULL, 
5314                                 gravity_left_center|orientation_horizontal|flags_fill, gui_internal_cmd_map_download, map);
5315                         g_free(description_size);
5316                         wma->prefix=g_strdup(buffer);
5317                         wma->name=description;
5318                         gui_internal_widget_append(w, wma);
5319                 }
5320         }
5321         
5322         gui_internal_menu_render(this);
5323 }
5324
5325 static void
5326 gui_internal_cmd2_setting_maps(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5327 {
5328         struct attr attr, on, off, description, type, data, url, active;
5329         struct widget *w,*wb,*row,*wma;
5330         char *label;
5331         struct attr_iter *iter;
5332
5333         wb=gui_internal_menu(this, _("Maps"));
5334         //w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5335         //w->spy=this->spacing*3;
5336         w = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
5337         gui_internal_widget_append(wb, w);
5338         iter=navit_attr_iter_new();
5339         on.type=off.type=attr_active;
5340         on.u.num=1;
5341         off.u.num=0;
5342         while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
5343                 if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
5344                         label=g_strdup(description.u.str);
5345                 } else {
5346                         if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
5347                                 type.u.str="";
5348                         if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
5349                                 data.u.str="";
5350                         label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
5351                 }
5352                 if (map_get_attr(attr.u.map, attr_url, &url, NULL)) {
5353                         if (!map_get_attr(attr.u.map, attr_active, &active, NULL))
5354                                 active.u.num=1;
5355                         wma=gui_internal_button_new_with_callback(this, label, image_new_xs(this, active.u.num ? "gui_active" : "gui_inactive"),
5356                         gravity_left_center|orientation_horizontal|flags_fill,
5357                         gui_internal_cmd_map_download, attr.u.map);
5358                 } else {
5359                         wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
5360                                 attr.u.map, &on, &off, 1);
5361                 }       
5362                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5363                 gui_internal_widget_append(row, wma);
5364                 g_free(label);
5365         }
5366         navit_attr_iter_destroy(iter);
5367         gui_internal_menu_render(this);
5368
5369 }
5370 static void
5371 gui_internal_cmd_set_active_vehicle(struct gui_priv *this, struct widget *wm, void *data)
5372 {
5373         struct attr vehicle = {attr_vehicle,{wm->data}};
5374         navit_set_attr(this->nav, &vehicle);
5375 }
5376
5377 static void
5378 gui_internal_cmd_show_satellite_status(struct gui_priv *this, struct widget *wm, void *data)
5379 {
5380         struct widget *w,*wb,*row;
5381         struct attr attr,sat_attr;
5382         struct vehicle *v=wm->data;
5383         char *str;
5384         int i;
5385         enum attr_type types[]={attr_sat_prn, attr_sat_elevation, attr_sat_azimuth, attr_sat_snr};
5386
5387         wb=gui_internal_menu(this, _("Show Satellite Status"));
5388         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_satellite_status;
5389         gui_internal_menu_data(this)->redisplay_widget=wm;
5390         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5391         gui_internal_widget_append(wb, w);
5392         w = gui_internal_widget_table_new(this,gravity_center | orientation_vertical | flags_expand | flags_fill, 0);
5393         row = gui_internal_widget_table_row_new(this,gravity_left_top);
5394         gui_internal_widget_append(row, gui_internal_label_new(this, " PRN "));
5395         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Elevation ")));
5396         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Azimuth ")));
5397         gui_internal_widget_append(row, gui_internal_label_new(this, " SNR "));
5398         gui_internal_widget_append(w,row);
5399         while (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
5400                 row = gui_internal_widget_table_row_new(this,gravity_left_top);
5401                 for (i = 0 ; i < sizeof(types)/sizeof(enum attr_type) ; i++) {
5402                         if (item_attr_get(attr.u.item, types[i], &sat_attr))
5403                                 str=g_strdup_printf("%ld", sat_attr.u.num);
5404                         else
5405                                 str=g_strdup("");
5406                         gui_internal_widget_append(row, gui_internal_label_new(this, str));
5407                         g_free(str);
5408                 }
5409                 gui_internal_widget_append(w,row);
5410         }
5411         gui_internal_widget_append(wb, w);
5412         gui_internal_menu_render(this);
5413 }
5414
5415 static void
5416 gui_internal_cmd_show_nmea_data(struct gui_priv *this, struct widget *wm, void *data)
5417 {
5418         struct widget *w,*wb;
5419         struct attr attr;
5420         struct vehicle *v=wm->data;
5421         wb=gui_internal_menu(this, _("Show NMEA Data"));
5422         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_nmea_data;
5423         gui_internal_menu_data(this)->redisplay_widget=wm;
5424         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5425         gui_internal_widget_append(wb, w);
5426         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL))
5427                 gui_internal_widget_append(w, gui_internal_text_new(this, attr.u.str, gravity_left_center|orientation_vertical));
5428         gui_internal_menu_render(this);
5429 }
5430
5431 /**
5432  * A container to hold the selected vehicle and the desired profile in
5433  * one data item.
5434  */
5435 struct vehicle_and_profilename {
5436         struct vehicle *vehicle;
5437         char *profilename;
5438 };
5439
5440 /**
5441  * Figures out whether the given vehicle is the active vehicle.
5442  *
5443  * @return true if the vehicle is active, false otherwise.
5444  */
5445 static int
5446 gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle
5447         *vehicle)
5448 {
5449         struct attr active_vehicle;
5450
5451         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
5452         active_vehicle.u.vehicle=NULL;
5453
5454         return active_vehicle.u.vehicle == vehicle;
5455 }
5456
5457 static void
5458 save_vehicle_xml(struct vehicle *v)
5459 {
5460         struct attr attr;
5461         struct attr_iter *iter=vehicle_attr_iter_new();
5462         int childs=0;
5463         dbg(0,"enter\n");
5464         printf("<vehicle");
5465         while (vehicle_get_attr(v, attr_any_xml, &attr, iter)) {
5466                 if (ATTR_IS_OBJECT(attr.type))
5467                         childs=1;
5468                 else    {
5469                         char *attrtxt;
5470                         printf(" %s=\"%s\"",attr_to_name(attr.type),attrtxt=attr_to_text(&attr, NULL, 1));
5471                         g_free(attrtxt);
5472                 }
5473         }
5474         if (childs) {
5475                 printf(">\n");
5476                 printf("</vehicle>\n");
5477         } else
5478                 printf(" />\n");
5479         vehicle_attr_iter_destroy(iter);
5480 }
5481
5482
5483 /**
5484  * Reacts to a button press that changes a vehicle's active profile.
5485  *
5486  * @see gui_internal_add_vehicle_profile
5487  */
5488 static void
5489 gui_internal_cmd_set_active_profile(struct gui_priv *this, struct
5490                 widget *wm, void *data)
5491 {
5492         struct vehicle_and_profilename *vapn = data;
5493         struct vehicle *v = vapn->vehicle;
5494         char *profilename = vapn->profilename;
5495         struct attr vehicle_name_attr;
5496         char *vehicle_name = NULL;
5497         struct attr profilename_attr;
5498
5499         // Get the vehicle name
5500         vehicle_get_attr(v, attr_name, &vehicle_name_attr, NULL);
5501         vehicle_name = vehicle_name_attr.u.str;
5502
5503         dbg(0, "Changing vehicle %s to profile %s\n", vehicle_name,
5504                         profilename);
5505
5506         // Change the profile name
5507         profilename_attr.type = attr_profilename;
5508         profilename_attr.u.str = profilename;
5509         if(!vehicle_set_attr(v, &profilename_attr)) {
5510                 dbg(0, "Unable to set the vehicle's profile name\n");
5511         }
5512
5513     // Notify Navit that the routing should be re-done if this is the
5514     // active vehicle.
5515         if (gui_internal_is_active_vehicle(this, v)) {
5516                 struct attr vehicle;
5517                 vehicle.type=attr_vehicle;
5518                 vehicle.u.vehicle=v;
5519                 navit_set_attr(this->nav, &vehicle);
5520         }
5521         save_vehicle_xml(v);
5522         
5523         gui_internal_prune_menu_count(this, 1, 0);
5524         gui_internal_menu_vehicle_settings(this, v, vehicle_name);
5525 }
5526
5527 /**
5528  * Adds the vehicle profile to the GUI, allowing the user to pick a
5529  * profile for the currently selected vehicle.
5530  */
5531 static void
5532 gui_internal_add_vehicle_profile(struct gui_priv *this, struct widget
5533                 *parent, struct vehicle *v, struct vehicleprofile *profile)
5534 {
5535         // Just here to show up in the translation file, nice and close to
5536         // where the translations are actually used.
5537         struct attr profile_attr;
5538         struct attr *attr = NULL;
5539         char *name = NULL;
5540         char *active_profile = NULL;
5541         char *label = NULL;
5542         int active;
5543         struct vehicle_and_profilename *context = NULL;
5544
5545 #ifdef ONLY_FOR_TRANSLATION
5546         char *translations[] = {_n("car"), _n("bike"), _n("pedestrian")};
5547 #endif
5548
5549         // Figure out the profile name
5550         attr = attr_search(profile->attrs, NULL, attr_name);
5551         if (!attr) {
5552                 dbg(0, "Adding vehicle profile failed. attr==NULL");
5553                 return;
5554         }
5555         name = attr->u.str;
5556
5557         // Determine whether the profile is the active one
5558         if (vehicle_get_attr(v, attr_profilename, &profile_attr, NULL))
5559                 active_profile = profile_attr.u.str;
5560         active = active_profile != NULL && !strcmp(name, active_profile);
5561
5562         dbg(0, "Adding vehicle profile %s, active=%s/%i\n", name,
5563                         active_profile, active);
5564
5565         // Build a translatable label.
5566         if(active) {
5567                 label = g_strdup_printf(_("Current profile: %s"), _(name));
5568         } else {
5569                 label = g_strdup_printf(_("Change profile to: %s"), _(name));
5570         }
5571
5572         // Create the context object (the vehicle and the desired profile)
5573         context = g_new0(struct vehicle_and_profilename, 1);
5574         context->vehicle = v;
5575         context->profilename = name;
5576
5577         // Add the button
5578         gui_internal_widget_append(parent,
5579                 gui_internal_button_new_with_callback(
5580                         this, label,
5581                         image_new_xs(this, active ? "gui_active" : "gui_inactive"),
5582                         gravity_left_center|orientation_horizontal|flags_fill,
5583                         gui_internal_cmd_set_active_profile, context));
5584
5585         free(label);
5586 }
5587
5588 static void
5589 gui_internal_menu_vehicle_settings(struct gui_priv *this, struct vehicle *v, char *name)
5590 {
5591         struct widget *w,*wb,*row;
5592         struct attr attr;
5593     struct vehicleprofile *profile = NULL;
5594         GList *profiles;
5595
5596         wb=gui_internal_menu(this, name);
5597         w=gui_internal_widget_table_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill,1);
5598         gui_internal_widget_append(wb, w);
5599
5600     // Add the "Set as active" button if this isn't the active
5601     // vehicle.
5602         if (!gui_internal_is_active_vehicle(this, v)) {
5603                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5604                 gui_internal_widget_append(row,
5605                         gui_internal_button_new_with_callback(this, _("Set as active"),
5606                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5607                                 gui_internal_cmd_set_active_vehicle, v));
5608         }
5609
5610         if (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
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, _("Show Satellite status"),
5614                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5615                                 gui_internal_cmd_show_satellite_status, v));
5616         }
5617         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL)) {
5618                 gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5619                 gui_internal_widget_append(row,
5620                         gui_internal_button_new_with_callback(this, _("Show NMEA data"),
5621                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5622                                 gui_internal_cmd_show_nmea_data, v));
5623         }
5624
5625     // Add all the possible vehicle profiles to the menu
5626         profiles = navit_get_vehicleprofiles(this->nav);
5627     while(profiles) {
5628         profile = (struct vehicleprofile *)profiles->data;
5629         gui_internal_widget_append(w, row=gui_internal_widget_table_row_new(this,gravity_left|orientation_horizontal|flags_fill));
5630         gui_internal_add_vehicle_profile(this, row, v, profile);
5631                 profiles = g_list_next(profiles);
5632     }
5633
5634         callback_list_call_attr_2(this->cbl, attr_vehicle, w, v);
5635         gui_internal_menu_render(this);
5636 }
5637
5638 static void
5639 gui_internal_cmd_vehicle_settings(struct gui_priv *this, struct widget *wm, void *data)
5640 {
5641         gui_internal_menu_vehicle_settings(this, wm->data, wm->text);
5642 }
5643
5644 static void
5645 gui_internal_cmd2_setting_vehicle(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5646 {
5647         struct attr attr,attr2,vattr;
5648         struct widget *w,*wb,*wl;
5649         struct attr_iter *iter;
5650         struct attr active_vehicle;
5651     
5652         iter=navit_attr_iter_new();
5653         if (navit_get_attr(this->nav, attr_vehicle, &attr, iter) && !navit_get_attr(this->nav, attr_vehicle, &attr2, iter)) {
5654                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
5655                 navit_attr_iter_destroy(iter);
5656                 gui_internal_menu_vehicle_settings(this, attr.u.vehicle, vattr.u.str);
5657                 return;
5658         }
5659         navit_attr_iter_destroy(iter);
5660
5661         wb=gui_internal_menu(this, _("Vehicle"));
5662         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5663         w->spy=this->spacing*3;
5664         gui_internal_widget_append(wb, w);
5665         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
5666                 active_vehicle.u.vehicle=NULL;
5667         iter=navit_attr_iter_new();
5668         while(navit_get_attr(this->nav, attr_vehicle, &attr, iter)) {
5669                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
5670                 wl=gui_internal_button_new_with_callback(this, vattr.u.str,
5671                         image_new_xs(this, attr.u.vehicle == active_vehicle.u.vehicle ? "gui_active" : "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
5672                         gui_internal_cmd_vehicle_settings, attr.u.vehicle);
5673                 wl->text=g_strdup(vattr.u.str);
5674                 gui_internal_widget_append(w, wl);
5675         }
5676         navit_attr_iter_destroy(iter);
5677         gui_internal_menu_render(this);
5678 }
5679
5680
5681 static void
5682 gui_internal_cmd2_setting_rules(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5683 {
5684         struct widget *wb,*w;
5685         struct attr on,off;
5686         wb=gui_internal_menu(this, _("Rules"));
5687         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5688         w->spy=this->spacing*3;
5689         gui_internal_widget_append(wb, w);
5690         on.u.num=1;
5691         off.u.num=0;
5692         on.type=off.type=attr_tracking;
5693         gui_internal_widget_append(w,
5694                 gui_internal_button_navit_attr_new(this, _("Lock on road"), gravity_left_center|orientation_horizontal|flags_fill,
5695                         &on, &off));
5696         on.u.num=0;
5697         off.u.num=-1;
5698         on.type=off.type=attr_orientation;
5699         gui_internal_widget_append(w,
5700                 gui_internal_button_navit_attr_new(this, _("Northing"), gravity_left_center|orientation_horizontal|flags_fill,
5701                         &on, &off));
5702         on.u.num=1;
5703         off.u.num=0;
5704         on.type=off.type=attr_follow_cursor;
5705         gui_internal_widget_append(w,
5706                 gui_internal_button_navit_attr_new(this, _("Map follows Vehicle"), gravity_left_center|orientation_horizontal|flags_fill,
5707                         &on, &off));
5708         on.u.num=1;
5709         off.u.num=0;
5710         on.type=off.type=attr_waypoints_flag;
5711         gui_internal_widget_append(w,
5712                         gui_internal_button_navit_attr_new(this, _("Plan with Waypoints"), gravity_left_center|orientation_horizontal|flags_fill,
5713                                         &on, &off));
5714         gui_internal_menu_render(this);
5715 }
5716
5717 //##############################################################################################################
5718 //# Description:
5719 //# Comment:
5720 //# Authors: Martin Schaller (04/2008)
5721 //##############################################################################################################
5722 static void gui_internal_motion(void *data, struct point *p)
5723 {
5724
5725         struct gui_priv *this=data;
5726         if (!this->root.children) {
5727                 navit_handle_motion(this->nav, p);
5728                 return;
5729         }
5730         if (!this->pressed)
5731                 return;
5732         this->current=*p;
5733         if(!this->motion_timeout_callback)
5734                 this->motion_timeout_callback=callback_new_1(callback_cast(gui_internal_motion_cb), this);
5735         if(!this->motion_timeout_event)
5736                 this->motion_timeout_event=event_add_timeout(30,0, this->motion_timeout_callback);
5737 }
5738
5739 static const char *
5740 find_attr(const char **names, const char **values, const char *name)
5741 {
5742         while (*names) {
5743                 if (!g_strcasecmp(*names, name))
5744                         return *values;
5745                 names+=xml_attr_distance;
5746                 values+=xml_attr_distance;
5747         }
5748         return NULL;
5749 }
5750
5751 static char *
5752 find_attr_dup(const char **names, const char **values, const char *name)
5753 {
5754         return g_strdup(find_attr(names, values, name));
5755 }
5756
5757 static void
5758 gui_internal_evaluate(struct gui_priv *this, const char *command)
5759 {
5760         if (command)
5761                 command_evaluate(&this->self, command);
5762 }
5763
5764
5765 static void
5766 gui_internal_html_command(struct gui_priv *this, struct widget *w, void *data)
5767 {
5768         gui_internal_evaluate(this,w->command);
5769 }
5770
5771 static void
5772 gui_internal_html_submit_set(struct gui_priv *this, struct widget *w, struct form *form)
5773 {
5774         GList *l;
5775         if (w->form == form && w->name) {
5776                 struct attr *attr=attr_new_from_text(w->name, w->text?w->text:"");
5777                 if (attr)
5778                         gui_set_attr(this->self.u.gui, attr);
5779                 attr_free(attr);
5780         }
5781         l=w->children;
5782         while (l) {
5783                 w=l->data;
5784                 gui_internal_html_submit_set(this, w, form);
5785                 l=g_list_next(l);
5786         }
5787
5788 }
5789
5790 static void
5791 gui_internal_html_submit(struct gui_priv *this, struct widget *w, void *data)
5792 {
5793         struct widget *menu;
5794         GList *l;
5795
5796         dbg(1,"enter form %p %s\n",w->form,w->form->onsubmit);
5797         l=g_list_last(this->root.children);
5798         menu=l->data;
5799         graphics_draw_mode(this->gra, draw_mode_begin);
5800         gui_internal_highlight_do(this, NULL);
5801         gui_internal_menu_render(this);
5802         graphics_draw_mode(this->gra, draw_mode_end);
5803         gui_internal_html_submit_set(this, menu, w->form);
5804         gui_internal_evaluate(this,w->form->onsubmit);
5805 }
5806
5807 static void
5808 gui_internal_html_load_href(struct gui_priv *this, char *href, int replace)
5809 {
5810         if (replace)
5811                 gui_internal_prune_menu_count(this, 1, 0);
5812         if (href && href[0] == '#') {
5813                 dbg(1,"href=%s\n",href);
5814                 g_free(this->href);
5815                 this->href=g_strdup(href);
5816                 gui_internal_html_menu(this, this->html_text, href+1);
5817         }
5818 }
5819
5820 static void
5821 gui_internal_html_href(struct gui_priv *this, struct widget *w, void *data)
5822 {
5823         gui_internal_html_load_href(this, w->command, 0);
5824 }
5825
5826 struct div_flags_map {
5827         char *attr;
5828         char *val;
5829         enum flags flags;
5830 } div_flags_map[] = {
5831         {"gravity","none",gravity_none},
5832         {"gravity","left",gravity_left},
5833         {"gravity","xcenter",gravity_xcenter},
5834         {"gravity","right",gravity_right},
5835         {"gravity","top",gravity_top},
5836         {"gravity","ycenter",gravity_ycenter},
5837         {"gravity","bottom",gravity_bottom},
5838         {"gravity","left_top",gravity_left_top},
5839         {"gravity","top_center",gravity_top_center},
5840         {"gravity","right_top",gravity_right_top},
5841         {"gravity","left_center",gravity_left_center},
5842         {"gravity","center",gravity_center},
5843         {"gravity","right_center",gravity_right_center},
5844         {"gravity","left_bottom",gravity_left_bottom},
5845         {"gravity","bottom_center",gravity_bottom_center},
5846         {"gravity","right_bottom",gravity_right_bottom},
5847         {"expand","1",flags_expand},
5848         {"fill","1",flags_fill},
5849         {"orientation","horizontal",orientation_horizontal},
5850         {"orientation","vertical",orientation_vertical},
5851         {"orientation","horizontal_vertical",orientation_horizontal_vertical},
5852 };
5853
5854 static enum flags
5855 div_flag(const char **names, const char **values, char *name)
5856 {
5857         int i;
5858         enum flags ret=0;
5859         const char *value=find_attr(names, values, name);
5860         if (!value)
5861                 return ret;
5862         for (i = 0 ; i < sizeof(div_flags_map)/sizeof(struct div_flags_map); i++) {
5863                 if (!strcmp(div_flags_map[i].attr,name) && !strcmp(div_flags_map[i].val,value))
5864                         ret|=div_flags_map[i].flags;
5865         }
5866         return ret;
5867 }
5868
5869 static enum flags
5870 div_flags(const char **names, const char **values)
5871 {
5872         enum flags flags;
5873         flags = div_flag(names, values, "gravity");
5874         flags |= div_flag(names, values, "orientation");
5875         flags |= div_flag(names, values, "expand");
5876         flags |= div_flag(names, values, "fill");
5877         return flags;
5878 }
5879
5880 static struct widget *
5881 html_image(struct gui_priv *this, const char **names, const char **values)
5882 {
5883         const char *src, *size;
5884         struct graphics_image *img=NULL;
5885
5886         src=find_attr(names, values, "src");
5887         if (!src)
5888                 return NULL;
5889         size=find_attr(names, values, "size");
5890         if (!size)
5891                 size="l";
5892         if (!strcmp(size,"l"))
5893                 img=image_new_l(this, src);
5894         else if (!strcmp(size,"s"))
5895                 img=image_new_s(this, src);
5896         else if (!strcmp(size,"xs"))
5897                 img=image_new_xs(this, src);
5898         if (!img)
5899                 return NULL;
5900         return gui_internal_image_new(this, img);
5901 }
5902
5903 static void
5904 gui_internal_html_start(void *dummy, const char *tag_name, const char **names, const char **values, void *data, void *error)
5905 {
5906         struct gui_priv *this=data;
5907         int i;
5908         enum html_tag tag=html_tag_none;
5909         struct html *html=&this->html[this->html_depth];
5910         const char *cond, *type;
5911
5912         if (!g_strcasecmp(tag_name,"text"))
5913                 return;
5914         html->command=NULL;
5915         html->name=NULL;
5916         html->href=NULL;
5917         html->skip=0;
5918         cond=find_attr(names, values, "cond");
5919
5920         if (cond && !this->html_skip) {
5921                 if (!command_evaluate_to_boolean(&this->self, cond, NULL))
5922                         html->skip=1;
5923         }
5924
5925         for (i=0 ; i < sizeof(html_tag_map)/sizeof(struct html_tag_map); i++) {
5926                 if (!g_strcasecmp(html_tag_map[i].tag_name, tag_name)) {
5927                         tag=html_tag_map[i].tag;
5928                         break;
5929                 }
5930         }
5931         html->tag=tag;
5932         if (!this->html_skip && !html->skip) {
5933                 switch (tag) {
5934                 case html_tag_a:
5935                         html->name=find_attr_dup(names, values, "name");
5936                         if (html->name) {
5937                                 html->skip=this->html_anchor ? strcmp(html->name,this->html_anchor) : 0;
5938                                 if (!html->skip)
5939                                         this->html_anchor_found=1;
5940                         }
5941                         html->command=find_attr_dup(names, values, "onclick");
5942                         html->href=find_attr_dup(names, values, "href");
5943                         html->refresh_cond=find_attr_dup(names, values, "refresh_cond");
5944                         break;
5945                 case html_tag_img:
5946                         html->command=find_attr_dup(names, values, "onclick");
5947                         html->w=html_image(this, names, values);
5948                         break;
5949                 case html_tag_form:
5950                         this->form=g_new0(struct form, 1);
5951                         this->form->onsubmit=find_attr_dup(names, values, "onsubmit");
5952                         break;
5953                 case html_tag_input:
5954                         type=find_attr_dup(names, values, "type");
5955                         if (!type)
5956                                 break;
5957                         if (!strcmp(type,"image")) {
5958                                 html->w=html_image(this, names, values);
5959                                 if (html->w) {
5960                                         html->w->state|=STATE_SENSITIVE;
5961                                         html->w->func=gui_internal_html_submit;
5962                                 }
5963                         }
5964                         if (!strcmp(type,"text") || !strcmp(type,"password")) {
5965                                 html->w=gui_internal_label_new(this, NULL);
5966                                 html->w->background=this->background;
5967                                 html->w->flags |= div_flags(names, values);
5968                                 html->w->state|=STATE_EDITABLE;
5969                                 if (!this->editable) {
5970                                         this->editable=html->w;
5971                                         html->w->state|=STATE_EDIT;
5972                                 }
5973                                 this->keyboard_required=1;
5974                                 if (!strcmp(type,"password"))
5975                                         html->w->flags2 |= 1;
5976                         }
5977                         if (html->w) {
5978                                 html->w->form=this->form;
5979                                 html->w->name=find_attr_dup(names, values, "name");
5980                         }
5981                         break;
5982                 case html_tag_div:
5983                         html->w=gui_internal_box_new(this, div_flags(names, values));
5984                         html->container=this->html_container;
5985                         this->html_container=html->w;
5986                         break;
5987                 default:
5988                         break;
5989                 }
5990         }
5991         this->html_skip+=html->skip;
5992         this->html_depth++;
5993 }
5994
5995 static void
5996 gui_internal_html_end(void *dummy, const char *tag_name, void *data, void *error)
5997 {
5998         struct gui_priv *this=data;
5999         struct html *html;
6000         struct html *parent=NULL;
6001
6002         if (!g_strcasecmp(tag_name,"text"))
6003                 return;
6004         this->html_depth--;
6005         html=&this->html[this->html_depth];
6006         if (this->html_depth > 0)
6007                 parent=&this->html[this->html_depth-1];
6008
6009
6010         if (!this->html_skip) {
6011                 if (html->command && html->w) {
6012                         html->w->state |= STATE_SENSITIVE;
6013                         html->w->command=html->command;
6014                         html->w->func=gui_internal_html_command;
6015                         html->command=NULL;
6016                 }
6017                 if (parent && (parent->href || parent->command) && html->w) {
6018                         html->w->state |= STATE_SENSITIVE;
6019                         if (parent->command) {
6020                                 html->w->command=g_strdup(parent->command);
6021                                 html->w->func=gui_internal_html_command;
6022                         } else {
6023                                 html->w->command=g_strdup(parent->href);
6024                                 html->w->func=gui_internal_html_href;
6025                         }
6026                 }
6027                 switch (html->tag) {
6028                 case html_tag_div:
6029                         this->html_container=html->container;
6030                 case html_tag_img:
6031                 case html_tag_input:
6032                         gui_internal_widget_append(this->html_container, html->w);
6033                         break;
6034                 case html_tag_form:
6035                         this->form=NULL;
6036                         break;
6037                 default:
6038                         break;
6039                 }
6040         }
6041         this->html_skip-=html->skip;
6042         g_free(html->command);
6043         g_free(html->name);
6044         g_free(html->href);
6045         g_free(html->refresh_cond);
6046 }
6047
6048 static void
6049 gui_internal_refresh_callback_called(struct gui_priv *this, struct menu_data *menu_data)
6050 {
6051         if (gui_internal_menu_data(this) == menu_data) {
6052                 char *href=g_strdup(menu_data->href);
6053                 gui_internal_html_load_href(this, href, 1);
6054                 g_free(href);
6055         }
6056 }
6057
6058 static void
6059 gui_internal_set_refresh_callback(struct gui_priv *this, char *cond)
6060 {
6061         dbg(0,"cond=%s\n",cond);
6062         if (cond) {
6063                 enum attr_type type;
6064                 struct object_func *func;
6065                 struct menu_data *menu_data=gui_internal_menu_data(this);
6066                 dbg(0,"navit=%p\n",this->nav);
6067                 type=command_evaluate_to_attr(&this->self, cond, NULL, &menu_data->refresh_callback_obj);
6068                 if (type == attr_none)
6069                         return;
6070                 func=object_func_lookup(menu_data->refresh_callback_obj.type);
6071                 if (!func || !func->add_attr)
6072                         return;
6073                 menu_data->refresh_callback.type=attr_callback;
6074                 menu_data->refresh_callback.u.callback=callback_new_attr_2(callback_cast(gui_internal_refresh_callback_called),type,this,menu_data);
6075                 func->add_attr(menu_data->refresh_callback_obj.u.data, &menu_data->refresh_callback);
6076         }
6077 }
6078
6079 static void
6080 gui_internal_html_text(void *dummy, const char *text, int len, void *data, void *error)
6081 {
6082         struct gui_priv *this=data;
6083         struct widget *w;
6084         int depth=this->html_depth-1;
6085         struct html *html=&this->html[depth];
6086         gchar *text_stripped;
6087
6088         if (this->html_skip)
6089                 return;
6090         while (isspace(text[0])) {
6091                 text++;
6092                 len--;
6093         }
6094         while (len > 0 && isspace(text[len-1]))
6095                 len--;
6096
6097         text_stripped = g_strndup(text, len);
6098         if (html->tag == html_tag_html && depth > 2) {
6099                 if (this->html[depth-1].tag == html_tag_script) {
6100                         html=&this->html[depth-2];
6101                 }
6102         }
6103         switch (html->tag) {
6104         case html_tag_a:
6105                 if (html->name && len) {
6106                         this->html_container=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
6107                         gui_internal_widget_append(gui_internal_menu(this, _(text_stripped)), this->html_container);
6108                         gui_internal_menu_data(this)->href=g_strdup(this->href);
6109                         gui_internal_set_refresh_callback(this, html->refresh_cond);
6110                         this->html_container->spx=this->spacing*10;
6111                 }
6112                 break;
6113         case html_tag_h1:
6114                 if (!this->html_container) {
6115                         this->html_container=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
6116                         gui_internal_widget_append(gui_internal_menu(this, _(text_stripped)), this->html_container);
6117                         this->html_container->spx=this->spacing*10;
6118                 }
6119                 break;
6120         case html_tag_img:
6121                 if (len) {
6122                         w=gui_internal_box_new(this, gravity_center|orientation_vertical);
6123                         gui_internal_widget_append(w, html->w);
6124                         gui_internal_widget_append(w, gui_internal_text_new(this, _(text_stripped), gravity_center|orientation_vertical));
6125                         html->w=w;
6126                 }
6127                 break;
6128         case html_tag_div:
6129                 if (len) {
6130                         gui_internal_widget_append(html->w, gui_internal_text_new(this, _(text_stripped), gravity_center|orientation_vertical));
6131                 }
6132                 break;
6133         case html_tag_script:
6134                 dbg(1,"execute %s\n",text_stripped);
6135                 gui_internal_evaluate(this,text_stripped);
6136                 break;
6137         default:
6138                 break;
6139         }
6140         g_free(text_stripped);
6141 }
6142
6143 static void
6144 gui_internal_html_menu(struct gui_priv *this, const char *document, char *anchor)
6145 {
6146         char *doc=g_strdup(document);
6147         graphics_draw_mode(this->gra, draw_mode_begin);
6148         this->html_container=NULL;
6149         this->html_depth=0;
6150         this->html_anchor=anchor;
6151         this->html_anchor_found=0;
6152         this->form=NULL;
6153         this->keyboard_required=0;
6154         this->editable=NULL;
6155         callback_list_call_attr_2(this->cbl,attr_gui,anchor,&doc);
6156         xml_parse_text(doc, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
6157         g_free(doc);
6158         if (this->keyboard_required && this->keyboard) {
6159                 this->html_container->flags=gravity_center|orientation_vertical|flags_expand|flags_fill;
6160                 gui_internal_widget_append(this->html_container, gui_internal_keyboard(this,2));
6161         }
6162         gui_internal_menu_render(this);
6163         graphics_draw_mode(this->gra, draw_mode_end);
6164 }
6165
6166
6167 static void
6168 gui_internal_enter(struct gui_priv *this, int ignore)
6169 {
6170         struct graphics *gra=this->gra;
6171         if (ignore != -1) 
6172                 this->ignore_button=ignore;
6173
6174         navit_block(this->nav, 1);
6175         graphics_overlay_disable(gra, 1);
6176         this->root.p.x=0;
6177         this->root.p.y=0;
6178         this->root.background=this->background;
6179 }
6180
6181 static void
6182 gui_internal_leave(struct gui_priv *this)
6183 {
6184         graphics_draw_mode(this->gra, draw_mode_end);
6185 }
6186
6187 static void
6188 gui_internal_set_click_coord(struct gui_priv *this, struct point *p)
6189 {
6190         struct coord c;
6191         struct coord_geo g;
6192         struct attr attr;
6193         struct transformation *trans;
6194         attr_free(this->click_coord_geo);
6195         this->click_coord_geo=NULL;
6196         if (p) {
6197                 trans=navit_get_trans(this->nav);
6198                 transform_reverse(trans, p, &c);
6199                 dbg(1,"x=0x%x y=0x%x\n", c.x, c.y);
6200                 this->clickp.pro=transform_get_projection(trans);
6201                 this->clickp.x=c.x;
6202                 this->clickp.y=c.y;
6203                 transform_to_geo(this->clickp.pro, &c, &g);
6204                 attr.u.coord_geo=&g;
6205                 attr.type=attr_click_coord_geo;
6206                 this->click_coord_geo=attr_dup(&attr);
6207         }
6208 }
6209
6210 static void
6211 gui_internal_set_position_coord(struct gui_priv *this)
6212 {
6213         struct transformation *trans;
6214         struct attr attr,attrp;
6215         struct coord c;
6216
6217         attr_free(this->position_coord_geo);
6218         this->position_coord_geo=NULL;
6219         if (navit_get_attr(this->nav, attr_vehicle, &attr, NULL) && attr.u.vehicle
6220                 && vehicle_get_attr(attr.u.vehicle, attr_position_coord_geo, &attrp, NULL)) {
6221                 trans=navit_get_trans(this->nav);
6222                 this->position_coord_geo=attr_dup(&attrp);
6223                 this->vehiclep.pro=transform_get_projection(trans);
6224                 transform_from_geo(this->vehiclep.pro, attrp.u.coord_geo, &c);
6225                 this->vehiclep.x=c.x;
6226                 this->vehiclep.y=c.y;
6227         }
6228 }
6229
6230 static void
6231 gui_internal_enter_setup(struct gui_priv *this)
6232 {
6233         if (!this->mouse_button_clicked_on_map)
6234                 gui_internal_set_position_coord(this);
6235 }
6236
6237 static void
6238 gui_internal_html_main_menu(struct gui_priv *this)
6239 {
6240         gui_internal_prune_menu(this, NULL);
6241         gui_internal_html_load_href(this, "#Main Menu", 0);
6242 }
6243
6244 static void
6245 gui_internal_cmd_menu(struct gui_priv *this, int ignore, char *href)
6246 {
6247         dbg(1,"enter\n");
6248         gui_internal_enter(this, ignore);
6249         gui_internal_enter_setup(this);
6250         // draw menu
6251         if (href)
6252                 gui_internal_html_load_href(this, href, 0);
6253         else
6254                 gui_internal_html_main_menu(this);
6255 }
6256
6257 static void
6258 gui_internal_cmd_menu2(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
6259 {
6260         char *href=NULL;
6261         int i=0, ignore=0, replace=0;
6262
6263         if (in && in[i] && ATTR_IS_INT(in[i]->type))
6264                 ignore=in[i++]->u.num;
6265
6266         if (in && in[i] && ATTR_IS_STRING(in[i]->type)) {
6267                 href=in[i++]->u.str;
6268                 if (in[i] && ATTR_IS_INT(in[i]->type))
6269                         replace=in[i++]->u.num;
6270         }
6271
6272         if (this->root.children) {
6273                 if (!href)
6274                         return;
6275                 gui_internal_html_load_href(this, href, replace);
6276                 return;
6277         }
6278         gui_internal_cmd_menu(this, ignore, href);
6279 }
6280
6281
6282 static void
6283 gui_internal_cmd_log_do(struct gui_priv *this, struct widget *widget)
6284 {
6285         if (widget->text && strlen(widget->text)) {
6286                 if (this->position_coord_geo)
6287                         navit_textfile_debug_log_at(this->nav, &this->vehiclep, "type=log_entry label=\"%s\"",widget->text);
6288                 else
6289                         navit_textfile_debug_log(this->nav, "type=log_entry label=\"%s\"",widget->text);
6290         }
6291         g_free(widget->text);
6292         widget->text=NULL;
6293         gui_internal_prune_menu(this, NULL);
6294         gui_internal_check_exit(this);
6295 }
6296
6297 static void
6298 gui_internal_cmd_log_clicked(struct gui_priv *this, struct widget *widget, void *data)
6299 {
6300         gui_internal_cmd_log_do(this, widget->data);
6301 }
6302
6303 static void
6304 gui_internal_cmd_log_changed(struct gui_priv *this, struct widget *wm, void *data)
6305 {
6306         int len;
6307         if (wm->text) {
6308                 len=strlen(wm->text);
6309                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
6310                         wm->text[len-1]='\0';
6311                         gui_internal_cmd_log_do(this, wm);
6312                 }
6313         }
6314 }
6315
6316
6317 static void
6318 gui_internal_cmd_log(struct gui_priv *this)
6319 {
6320         struct widget *w,*wb,*wk,*wl,*we,*wnext;
6321         gui_internal_enter(this, 1);
6322         gui_internal_set_click_coord(this, NULL);
6323         gui_internal_enter_setup(this);
6324         wb=gui_internal_menu(this, "Log Message");
6325         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
6326         gui_internal_widget_append(wb, w);
6327         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
6328         gui_internal_widget_append(w, we);
6329         gui_internal_widget_append(we, wk=gui_internal_label_new(this, _("Message")));
6330         wk->state |= STATE_EDIT|STATE_EDITABLE|STATE_CLEAR;
6331         wk->background=this->background;
6332         wk->flags |= flags_expand|flags_fill;
6333         wk->func = gui_internal_cmd_log_changed;
6334         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
6335         wnext->state |= STATE_SENSITIVE;
6336         wnext->func = gui_internal_cmd_log_clicked;
6337         wnext->data=wk;
6338         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
6339         gui_internal_widget_append(w, wl);
6340         if (this->keyboard)
6341                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
6342         gui_internal_menu_render(this);
6343         gui_internal_leave(this);
6344 }
6345
6346 static void
6347 gui_internal_check_exit(struct gui_priv *this)
6348 {
6349         struct graphics *gra=this->gra;
6350         if (! this->root.children) {
6351                 gui_internal_search_idle_end(this);
6352                 gui_internal_search_list_destroy(this);
6353                 graphics_overlay_disable(gra, 0);
6354                 if (!navit_block(this->nav, 0)) {
6355                         if (this->redraw)
6356                                 navit_draw(this->nav);
6357                         else
6358                                 navit_draw_displaylist(this->nav);
6359                 }
6360         }
6361 }
6362
6363 static int
6364 gui_internal_get_attr(struct gui_priv *this, enum attr_type type, struct attr *attr)
6365 {
6366         switch (type) {
6367         case attr_active:
6368                 attr->u.num=this->root.children != NULL;
6369                 break;
6370         case attr_click_coord_geo:
6371                 if (!this->click_coord_geo)
6372                         return 0;
6373                 *attr=*this->click_coord_geo;
6374                 break;
6375         case attr_position_coord_geo:
6376                 if (!this->position_coord_geo)
6377                         return 0;
6378                 *attr=*this->position_coord_geo;
6379                 break;
6380         case attr_pitch:
6381                 attr->u.num=this->pitch;
6382                 break;
6383         case attr_button:
6384                 attr->u.num=this->mouse_button_clicked_on_map;
6385                 break;
6386         default:
6387                 return 0;
6388         }
6389         attr->type=type;
6390         return 1;
6391 }
6392
6393 static int
6394 gui_internal_add_attr(struct gui_priv *this, struct attr *attr)
6395 {
6396         switch (attr->type) {
6397         case attr_xml_text:
6398                 g_free(this->html_text);
6399                 this->html_text=g_strdup(attr->u.str);
6400                 return 1;
6401         default:
6402                 return 0;
6403         }
6404 }
6405
6406 static int
6407 gui_internal_set_attr(struct gui_priv *this, struct attr *attr)
6408 {
6409         switch (attr->type) {
6410         case attr_fullscreen:
6411                 if ((this->fullscreen > 0) != (attr->u.num > 0)) {
6412                         graphics_draw_mode(this->gra, draw_mode_end);
6413                         this->win->fullscreen(this->win, attr->u.num > 0);
6414                         graphics_draw_mode(this->gra, draw_mode_begin);
6415                 }
6416                 this->fullscreen=attr->u.num;
6417                 return 1;
6418         case attr_menu_on_map_click:
6419                 this->menu_on_map_click=attr->u.num;
6420                 return 1;
6421         case attr_on_map_click:
6422                 g_free(this->on_map_click);
6423                 this->on_map_click=g_strdup(attr->u.str);
6424                 return 1;
6425         default:
6426                 dbg(0,"%s\n",attr_to_name(attr->type));
6427                 return 1;
6428         }
6429 }
6430
6431 static void gui_internal_dbus_signal(struct gui_priv *this, struct point *p)
6432 {
6433         struct displaylist_handle *dlh;
6434         struct displaylist *display;
6435         struct displayitem *di;
6436         struct attr cb,**attr_list=NULL;
6437         int valid=0;
6438
6439         display=navit_get_displaylist(this->nav);
6440         dlh=graphics_displaylist_open(display);
6441         while ((di=graphics_displaylist_next(dlh))) {
6442                 struct item *item=graphics_displayitem_get_item(di);
6443                 if (item_is_point(*item) && graphics_displayitem_get_displayed(di) &&
6444                         graphics_displayitem_within_dist(display, di, p, this->radius)) {
6445                         struct map_rect *mr=map_rect_new(item->map, NULL);
6446                         struct item *itemo=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
6447                         struct attr attr;
6448                         if (itemo && item_attr_get(itemo, attr_data, &attr))
6449                                 attr_list=attr_generic_add_attr(attr_list, &attr);
6450                         map_rect_destroy(mr);
6451                 }
6452         }
6453         graphics_displaylist_close(dlh);
6454         if (attr_list && navit_get_attr(this->nav, attr_callback_list, &cb, NULL))
6455                 callback_list_call_attr_4(cb.u.callback_list, attr_command, "dbus_send_signal", attr_list, NULL, &valid);
6456         attr_list_free(attr_list);
6457 }
6458
6459
6460
6461 //##############################################################################################################
6462 //# Description: Function to handle mouse clicks and scroll wheel movement
6463 //# Comment:
6464 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
6465 //##############################################################################################################
6466 static void gui_internal_button(void *data, int pressed, int button, struct point *p)
6467 {
6468         struct gui_priv *this=data;
6469         struct graphics *gra=this->gra;
6470
6471         dbg(1,"enter %d %d\n", pressed, button);
6472         // if still on the map (not in the menu, yet):
6473         dbg(1,"children=%p ignore_button=%d\n",this->root.children,this->ignore_button);
6474         if (!this->root.children || this->ignore_button) {
6475
6476                 this->ignore_button=0;
6477                 // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
6478                 if (!navit_handle_button(this->nav, pressed, button, p, NULL)) {
6479                         dbg(1,"navit has handled button\n");
6480                         return;
6481                 }
6482                 dbg(1,"menu_on_map_click=%d\n",this->menu_on_map_click);
6483                 if (button != 1)
6484                         return;
6485                 if (this->on_map_click || this->menu_on_map_click) {
6486                         this->mouse_button_clicked_on_map=1;
6487                         gui_internal_set_click_coord(this, p);
6488                         gui_internal_set_position_coord(this);
6489                         if (this->on_map_click)
6490                                 command_evaluate(&this->self, this->on_map_click);
6491                         else
6492                                 gui_internal_cmd_menu(this, 0, NULL);
6493                         this->mouse_button_clicked_on_map=0;
6494                 } else if (this->signal_on_map_click) {
6495                         gui_internal_dbus_signal(this, p);
6496                         return;
6497                 }
6498                 return;
6499         }
6500
6501
6502         /*
6503          * If already in the menu:
6504          */
6505
6506         if (pressed) {
6507                 this->pressed=1;
6508                 this->current=*p;
6509                 gui_internal_gesture_ring_clear(this);
6510                 gui_internal_gesture_ring_add(this, p);
6511                 gui_internal_highlight(this);
6512         } else {
6513                 int dx,dy;
6514                 gui_internal_gesture_ring_add(this, p);
6515                 gui_internal_gesture_get_vector(this, 300, NULL, &dx, &dy);
6516                 this->current.x=-1;
6517                 this->current.y=-1;
6518                 graphics_draw_mode(gra, draw_mode_begin);
6519                 if(!gui_internal_gesture_do(this) && this->pressed!=2 && abs(dx)<this->icon_s && abs(dy)<this->icon_s)
6520                         gui_internal_call_highlighted(this);
6521                 this->pressed=0;
6522                 if (!event_main_loop_has_quit()) {
6523                         gui_internal_highlight(this);
6524                         graphics_draw_mode(gra, draw_mode_end);
6525                         gui_internal_check_exit(this);
6526                 }
6527         }
6528 }
6529
6530 static void
6531 gui_internal_setup(struct gui_priv *this)
6532 {
6533         struct color cbh={0x9fff,0x9fff,0x9fff,0xffff};
6534         struct color cf={0xbfff,0xbfff,0xbfff,0xffff};
6535         struct graphics *gra=this->gra;
6536         unsigned char *buffer;
6537         char *gui_file;
6538         int size;
6539
6540         if (this->background)
6541                 return;
6542         this->background=graphics_gc_new(gra);
6543         this->background2=graphics_gc_new(gra);
6544         this->highlight_background=graphics_gc_new(gra);
6545         graphics_gc_set_foreground(this->highlight_background, &cbh);
6546         this->foreground=graphics_gc_new(gra);
6547         graphics_gc_set_foreground(this->foreground, &cf);
6548         this->text_background=graphics_gc_new(gra);
6549         this->text_foreground=graphics_gc_new(gra);
6550         graphics_gc_set_foreground(this->background, &this->background_color);
6551         graphics_gc_set_foreground(this->background2, &this->background2_color);
6552         graphics_gc_set_foreground(this->text_background, &this->text_background_color);
6553         graphics_gc_set_foreground(this->text_foreground, &this->text_foreground_color);
6554         gui_file=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal.txt", NULL);
6555         if (file_get_contents(gui_file,&buffer,&size)) {
6556                 char *command=g_malloc(size+1);
6557                 strncpy(command,(const char *)buffer,size);
6558                 command[size]=0;
6559                 command_evaluate(&this->self, command);
6560                 g_free(command);
6561                 g_free(buffer);
6562         }
6563         g_free(gui_file);
6564 }
6565
6566 //##############################################################################################################
6567 //# Description:
6568 //# Comment:
6569 //# Authors: Martin Schaller (04/2008)
6570 //##############################################################################################################
6571 static void gui_internal_resize(void *data, int w, int h)
6572 {
6573         struct gui_priv *this=data;
6574         int changed=0;
6575
6576         gui_internal_setup(this);
6577
6578         if (this->root.w != w || this->root.h != h) {
6579                 this->root.w=w;
6580                 this->root.h=h;
6581                 changed=1;
6582         }
6583         dbg(1,"w=%d h=%d children=%p\n", w, h, this->root.children);
6584         navit_handle_resize(this->nav, w, h);
6585         if (this->root.children) {
6586                 if (changed) {
6587                         gui_internal_html_main_menu(this);
6588                 } else {
6589                         gui_internal_menu_render(this);
6590                 }
6591         }
6592 }
6593
6594 static void
6595 gui_internal_keynav_point(struct widget *w, int dx, int dy, struct point *p)
6596 {
6597         p->x=w->p.x+w->w/2;
6598         p->y=w->p.y+w->h/2;
6599         if (dx < 0)
6600                 p->x=w->p.x;
6601         if (dx > 0)
6602                 p->x=w->p.x+w->w;
6603         if (dy < 0)
6604                 p->y=w->p.y;
6605         if (dy > 0)
6606                 p->y=w->p.y+w->h;
6607 }
6608
6609 static void
6610 gui_internal_keynav_find_closest(struct widget *wi, struct point *p, int dx, int dy, int *distance, struct widget **result)
6611 {
6612         GList *l=wi->children;
6613         // Skip hidden elements
6614         if (wi->p.x==0 && wi->p.y==0 && wi->w==0 && wi->h==0)
6615                 return;
6616         if ((wi->state & STATE_SENSITIVE) ) {
6617                 int dist1,dist2;
6618                 struct point wp;
6619                 gui_internal_keynav_point(wi, -dx, -dy, &wp);
6620                 if (dx) {
6621                         dist1=(wp.x-p->x)*dx;
6622                         dist2=wp.y-p->y;
6623                 } else if (dy) {
6624                         dist1=(wp.y-p->y)*dy;
6625                         dist2=wp.x-p->x;
6626                 } else {
6627                         dist2=wp.x-p->x;
6628                         dist1=wp.y-p->y;
6629                         if (dist1 < 0)
6630                                 dist1=-dist1;
6631                 }
6632                 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);
6633                 if (dist1 >= 0) {
6634                         if (dist2 < 0)
6635                                 dist1-=dist2;
6636                         else
6637                                 dist1+=dist2;
6638                         if (dist1 < *distance) {
6639                                 *result=wi;
6640                                 *distance=dist1;
6641                         }
6642                 }
6643         }
6644         while (l) {
6645                 struct widget *child=l->data;
6646                 gui_internal_keynav_find_closest(child, p, dx, dy, distance, result);
6647                 l=g_list_next(l);
6648         }
6649 }
6650
6651 static void
6652 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy)
6653 {
6654         struct widget *result,*menu=g_list_last(this->root.children)->data;
6655         struct point p;
6656         int distance;
6657         if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
6658                 gui_internal_keynav_point(this->highlighted, dx, dy, &p);
6659         else {
6660                 p.x=0;
6661                 p.y=0;
6662                 distance=INT_MAX;
6663                 result=NULL;
6664                 gui_internal_keynav_find_closest(menu, &p, 0, 0, &distance, &result);
6665                 if (result) {
6666                         gui_internal_keynav_point(result, dx, dy, &p);
6667                         dbg(1,"result origin=%p p=%d,%d\n", result, p.x, p.y);
6668                 }
6669         }
6670         result=NULL;
6671         distance=INT_MAX;
6672         gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
6673         dbg(1,"result=%p\n", result);
6674         if (! result) {
6675                 if (dx < 0)
6676                         p.x=this->root.w;
6677                 if (dx > 0)
6678                         p.x=0;
6679                 if (dy < 0)
6680                         p.y=this->root.h;
6681                 if (dy > 0)
6682                         p.y=0;
6683                 result=NULL;
6684                 distance=INT_MAX;
6685                 gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
6686                 dbg(1,"wraparound result=%p\n", result);
6687         }
6688         gui_internal_highlight_do(this, result);
6689         if (result)
6690                 gui_internal_say(this, result, 1);
6691 }
6692
6693 //##############################################################################################################
6694 //# Description:
6695 //# Comment:
6696 //# Authors: Martin Schaller (04/2008)
6697 //##############################################################################################################
6698 static void gui_internal_keypress(void *data, char *key)
6699 {
6700         struct gui_priv *this=data;
6701         int w,h;
6702         struct point p;
6703         if (!this->root.children) {
6704                 transform_get_size(navit_get_trans(this->nav), &w, &h);
6705                 switch (*key) {
6706                 case NAVIT_KEY_UP:
6707                         p.x=w/2;
6708                         p.y=0;
6709                         navit_set_center_screen(this->nav, &p, 1);
6710                         break;
6711                 case NAVIT_KEY_DOWN:
6712                         p.x=w/2;
6713                         p.y=h;
6714                         navit_set_center_screen(this->nav, &p, 1);
6715                         break;
6716                 case NAVIT_KEY_LEFT:
6717                         p.x=0;
6718                         p.y=h/2;
6719                         navit_set_center_screen(this->nav, &p, 1);
6720                         break;
6721                 case NAVIT_KEY_RIGHT:
6722                         p.x=w;
6723                         p.y=h/2;
6724                         navit_set_center_screen(this->nav, &p, 1);
6725                         break;
6726                 case NAVIT_KEY_ZOOM_IN:
6727                         navit_zoom_in(this->nav, 2, NULL);
6728                         break;
6729                 case NAVIT_KEY_ZOOM_OUT:
6730                         navit_zoom_out(this->nav, 2, NULL);
6731                         break;
6732                 case NAVIT_KEY_RETURN:
6733                 case NAVIT_KEY_MENU:
6734                         gui_internal_set_click_coord(this, NULL);
6735                         gui_internal_cmd_menu(this, 0, NULL);
6736                         break;
6737                 }
6738                 return;
6739         }
6740         graphics_draw_mode(this->gra, draw_mode_begin);
6741         switch (*key) {
6742         case NAVIT_KEY_LEFT:
6743                 gui_internal_keynav_highlight_next(this,-1,0);
6744                 break;
6745         case NAVIT_KEY_RIGHT:
6746                 gui_internal_keynav_highlight_next(this,1,0);
6747                 break;
6748         case NAVIT_KEY_UP:
6749                 gui_internal_keynav_highlight_next(this,0,-1);
6750                 break;
6751         case NAVIT_KEY_DOWN:
6752                 gui_internal_keynav_highlight_next(this,0,1);
6753                 break;
6754         case NAVIT_KEY_BACK:
6755                 if (g_list_length(this->root.children) > 1)
6756                         gui_internal_back(this, NULL, NULL);
6757                 else
6758                         gui_internal_prune_menu(this, NULL);
6759                 break;
6760         case NAVIT_KEY_RETURN:
6761                 if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
6762                         gui_internal_call_highlighted(this);
6763                 else
6764                         gui_internal_keypress_do(this, key);
6765                 break;
6766         default:
6767                 gui_internal_keypress_do(this, key);
6768         }
6769         if (!event_main_loop_has_quit()) {
6770                 graphics_draw_mode(this->gra, draw_mode_end);
6771                 gui_internal_check_exit(this);
6772         }
6773 }
6774
6775
6776 //##############################################################################################################
6777 //# Description:
6778 //# Comment:
6779 //# Authors: Martin Schaller (04/2008)
6780 //##############################################################################################################
6781 static int gui_internal_set_graphics(struct gui_priv *this, struct graphics *gra)
6782 {
6783         struct window *win;
6784         struct transformation *trans=navit_get_trans(this->nav);
6785
6786         win=graphics_get_data(gra, "window");
6787         if (! win)
6788                 return 1;
6789         navit_ignore_graphics_events(this->nav, 1);
6790         this->gra=gra;
6791         this->win=win;
6792         navit_ignore_graphics_events(this->nav, 1);
6793         transform_get_size(trans, &this->root.w, &this->root.h);
6794         this->resize_cb=callback_new_attr_1(callback_cast(gui_internal_resize), attr_resize, this);
6795         graphics_add_callback(gra, this->resize_cb);
6796         this->button_cb=callback_new_attr_1(callback_cast(gui_internal_button), attr_button, this);
6797         graphics_add_callback(gra, this->button_cb);
6798         this->motion_cb=callback_new_attr_1(callback_cast(gui_internal_motion), attr_motion, this);
6799         graphics_add_callback(gra, this->motion_cb);
6800         this->keypress_cb=callback_new_attr_1(callback_cast(gui_internal_keypress), attr_keypress, this);
6801         graphics_add_callback(gra, this->keypress_cb);
6802         this->window_closed_cb=callback_new_attr_1(callback_cast(gui_internal_window_closed), attr_window_closed, this);
6803         graphics_add_callback(gra, this->window_closed_cb);
6804
6805         // set fullscreen if needed
6806         if (this->fullscreen)
6807                 this->win->fullscreen(this->win, this->fullscreen != 0);
6808         /* Was resize callback already issued? */
6809         if (navit_get_ready(this->nav) & 2)
6810                 gui_internal_setup(this);
6811         return 0;
6812 }
6813
6814 static void gui_internal_disable_suspend(struct gui_priv *this)
6815 {
6816         if (this->win->disable_suspend)
6817                 this->win->disable_suspend(this->win);
6818 }
6819
6820 //##############################################################################################################
6821 //# Description:
6822 //# Comment:
6823 //# Authors: Martin Schaller (04/2008)
6824 //##############################################################################################################
6825 struct gui_methods gui_internal_methods = {
6826         NULL,
6827         NULL,
6828         gui_internal_set_graphics,
6829         NULL,
6830         NULL,
6831         NULL,
6832         gui_internal_disable_suspend,
6833         gui_internal_get_attr,
6834         gui_internal_add_attr,
6835         gui_internal_set_attr,
6836 };
6837
6838         static void
6839 gui_internal_get_data(struct gui_priv *priv, char *command, struct attr **in, struct attr ***out)
6840 {
6841         struct attr private_data = { attr_private_data, {(void *)&priv->data}};
6842         if (out)
6843                 *out=attr_generic_add_attr(*out, &private_data);
6844 }
6845
6846 static void
6847 gui_internal_add_callback(struct gui_priv *priv, struct callback *cb)
6848 {
6849         callback_list_add(priv->cbl, cb);
6850 }
6851
6852 static void
6853 gui_internal_remove_callback(struct gui_priv *priv, struct callback *cb)
6854 {
6855         callback_list_remove(priv->cbl, cb);
6856 }
6857
6858
6859 static struct gui_internal_methods gui_internal_methods_ext = {
6860         gui_internal_add_callback,
6861         gui_internal_remove_callback,
6862         gui_internal_menu_render,
6863         image_new_xs,
6864         image_new_l,
6865 };
6866
6867
6868 static enum flags
6869 gui_internal_get_flags(struct widget *widget)
6870 {
6871         return widget->flags;
6872 }
6873
6874 static void
6875 gui_internal_set_flags(struct widget *widget, enum flags flags)
6876 {
6877         widget->flags=flags;
6878 }
6879
6880 static int
6881 gui_internal_get_state(struct widget *widget)
6882 {
6883         return widget->state;
6884 }
6885
6886 static void
6887 gui_internal_set_state(struct widget *widget, int state)
6888 {
6889         widget->state=state;
6890 }
6891
6892 static void
6893 gui_internal_set_func(struct widget *widget, void (*func)(struct gui_priv *priv, struct widget *widget, void *data))
6894 {
6895         widget->func=func;
6896 }
6897
6898 static void
6899 gui_internal_set_data(struct widget *widget, void *data)
6900 {
6901         widget->data=data;
6902 }
6903
6904 static void
6905 gui_internal_set_default_background(struct gui_priv *this, struct widget *widget)
6906 {
6907         widget->background=this->background;
6908 }
6909
6910 static struct gui_internal_widget_methods gui_internal_widget_methods = {
6911         gui_internal_widget_append,
6912         gui_internal_button_new,
6913         gui_internal_button_new_with_callback,
6914         gui_internal_box_new,
6915         gui_internal_label_new,
6916         gui_internal_image_new,
6917         gui_internal_keyboard,
6918         gui_internal_menu,
6919         gui_internal_get_flags,
6920         gui_internal_set_flags,
6921         gui_internal_get_state,
6922         gui_internal_set_state,
6923         gui_internal_set_func,
6924         gui_internal_set_data,
6925         gui_internal_set_default_background,
6926 };
6927
6928 static void
6929 gui_internal_cmd_write(struct gui_priv * this, char *function, struct attr **in, struct attr ***out, int *valid)
6930 {
6931         char *str=NULL,*str2=NULL;
6932         dbg(1,"enter %s %p %p %p\n",function,in,out,valid);
6933         if (!in || !in[0])
6934                 return;
6935         dbg(1,"%s\n",attr_to_name(in[0]->type));
6936         if (ATTR_IS_STRING(in[0]->type)) {
6937                 str=in[0]->u.str;
6938         }
6939         if (ATTR_IS_COORD_GEO(in[0]->type)) {
6940                 str=str2=coordinates_geo(in[0]->u.coord_geo, '\n');
6941         }
6942         if (str) {
6943                 str=g_strdup_printf("<html>%s</html>\n",str);
6944                 xml_parse_text(str, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
6945         }
6946         g_free(str);
6947         g_free(str2);
6948 }
6949
6950
6951 /**
6952  * @brief Creates a new table widget.
6953  *
6954  * Creates and returns a new table widget.  This function will
6955  * setup next/previous buttons as children.
6956  *
6957  * @param this The graphics context.
6958  * @param flags widget sizing flags.
6959  * @returns The newly created widget
6960  */
6961 struct widget * gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons)
6962 {
6963         struct widget * widget = g_new0(struct widget,1);
6964         struct table_data * data = NULL;
6965         widget->type=widget_table;
6966         widget->flags=flags;
6967         widget->state=STATE_SCROLLABLE;
6968         widget->data=g_new0(struct table_data,1);
6969         widget->data_free=gui_internal_table_data_free;
6970
6971         // We have to set background here explicitly
6972         // because it will be used by inner elements created later in this 
6973         // function (navigation buttons). Else that elements won't have
6974         // any background.
6975         widget->background=this->background;
6976         data = (struct table_data*)widget->data;
6977
6978         if (buttons) {
6979                 data->next_button=gui_internal_box_new(this, gravity_center|orientation_horizontal);
6980                 gui_internal_widget_append(data->next_button, gui_internal_text_new(this,_("Next"),gravity_center|orientation_horizontal));
6981                 gui_internal_widget_append(data->next_button, gui_internal_image_new(this, image_new_xs(this, "gui_arrow_right")));
6982                 data->next_button->func=gui_internal_table_button_next;
6983                 data->next_button->data=widget;
6984
6985
6986                 data->prev_button =  gui_internal_button_new_with_callback
6987                         (this, _("Prev"),
6988                         image_new_xs(this, "gui_arrow_left"),
6989                         gravity_center |orientation_horizontal,
6990                         gui_internal_table_button_prev,NULL);
6991
6992                 data->prev_button->data=widget;
6993
6994                 data->this=this;
6995
6996                 data->button_box=gui_internal_box_new(this,
6997                                               gravity_center|orientation_horizontal);
6998                 gui_internal_widget_append(widget, data->button_box);
6999                 gui_internal_widget_append(data->button_box, data->prev_button);
7000                 gui_internal_widget_append(data->button_box, data->next_button);
7001
7002                 data->button_box->bl=this->spacing;
7003                 gui_internal_widget_pack(this,data->button_box);
7004         }
7005
7006         return widget;
7007
7008 }
7009
7010 /**
7011  * @brief Clears all the rows from the table.
7012  * This function removes all rows from a table.
7013  * New rows can later be added to the table.
7014  */
7015 void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table)
7016 {
7017   GList * iter;
7018   struct table_data * table_data = (struct table_data* ) table->data;
7019
7020   iter = table->children;
7021   while(iter ) {
7022           if(iter->data != table_data->button_box) {
7023                   struct widget * child = (struct widget*)iter->data;
7024                   gui_internal_widget_destroy(this,child);
7025                   if(table->children == iter) {
7026                           table->children = g_list_remove(iter,iter->data);
7027                           iter=table->children;
7028                   }
7029                   else
7030                           iter = g_list_remove(iter,iter->data);
7031           }
7032           else {
7033                   iter = g_list_next(iter);
7034           }
7035
7036   }
7037   table_data->top_row=NULL;
7038   table_data->bottom_row=NULL;
7039 }
7040
7041 /**
7042  * @brief Check if table has any data rows filled.
7043  * @param this The graphics context
7044  * @param table table widget
7045  * @returns 1 if the table is empty, 0 if there any data rows present.
7046  */
7047 static int gui_internal_widget_table_is_empty(struct gui_priv *this, struct widget * table)
7048 {
7049    GList *l;
7050    struct table_data *td=(struct table_data*) table->data;
7051
7052    for(l=table->children;l;l=g_list_next(l)) {
7053         if(l->data != td->button_box)
7054                 return 0;
7055    }
7056
7057    return 1;
7058 }
7059
7060 /**
7061  * @brief Move GList pointer to the next table row, skipping other table children (button box, for example).
7062  * @param row GList pointer into the children list 
7063  * @returns GList pointer to the next row in the children list, or NULL if there are no any rows left.
7064  */
7065 static GList * gui_internal_widget_table_next_row(GList * row)
7066 {
7067   while((row=g_list_next(row))!=NULL) {
7068         if(row->data && ((struct widget *)(row->data))->type == widget_table_row)
7069                 break;
7070    }
7071   return row;
7072 }
7073
7074 /**
7075  * @brief Move GList pointer to the previous table row, skipping other table children (button box, for example).
7076  * @param row GList pointer into the children list 
7077  * @returns GList pointer to the previous row in the children list, or NULL if there are no any rows left.
7078  */
7079 static GList * gui_internal_widget_table_prev_row(GList * row)
7080 {
7081   while((row=g_list_previous(row))!=NULL) {
7082         if(row->data && ((struct widget *)(row->data))->type == widget_table_row)
7083                 break;
7084    }
7085   return row;
7086 }
7087
7088
7089 /**
7090  * Creates a new table_row widget.
7091  * @param this The graphics context
7092  * @param flags Sizing flags for the row
7093  * @returns The new table_row widget.
7094  */
7095 struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags)
7096 {
7097         struct widget * widget = g_new0(struct widget,1);
7098         widget->type=widget_table_row;
7099         widget->flags=flags;
7100         return widget;
7101 }
7102
7103
7104
7105 /**
7106  * @brief Computes the column dimensions for the table.
7107  *
7108  * @param w The table widget to compute dimensions for.
7109  *
7110  * This function examines all of the rows and columns for the table w
7111  * and returns a list (GList) of table_column_desc elements that
7112  * describe each column of the table.
7113  *
7114  * The caller is responsible for freeing the returned list.
7115  */
7116 static GList * gui_internal_compute_table_dimensions(struct gui_priv * this,struct widget * w)
7117 {
7118
7119         GList * column_desc = NULL;
7120         GList * current_desc=NULL;
7121         GList * cur_row = w->children;
7122         struct widget * cur_row_widget=NULL;
7123         GList * cur_column=NULL;
7124         struct widget * cell_w=NULL;
7125         struct table_column_desc * current_cell=NULL;
7126         struct table_data * table_data=NULL;
7127         int height=0;
7128         int width=0;
7129         int total_width=0;
7130         int column_count=0;
7131
7132         /**
7133          * Scroll through the the table and
7134          * 1. Compute the maximum width + height of each column across all rows.
7135          */
7136         table_data = (struct table_data*) w->data;
7137         for(cur_row=w->children;  cur_row ; cur_row = g_list_next(cur_row) )
7138         {
7139                 cur_row_widget = (struct widget*) cur_row->data;
7140                 current_desc = column_desc;
7141                 if(cur_row_widget == table_data->button_box)
7142                 {
7143                         continue;
7144                 }
7145                 column_count=0;
7146                 for(cur_column = cur_row_widget->children; cur_column;
7147                     cur_column=g_list_next(cur_column))
7148                 {
7149                         cell_w = (struct widget*) cur_column->data;
7150                         gui_internal_widget_pack(this,cell_w);
7151                         if(current_desc == 0)
7152                         {
7153                                 current_cell = g_new0(struct table_column_desc,1);
7154                                 column_desc = g_list_append(column_desc,current_cell);
7155                                 current_desc = g_list_last(column_desc);
7156                                 current_cell->height=cell_w->h;
7157                                 current_cell->width=cell_w->w;
7158                                 total_width+=cell_w->w;
7159
7160                         }
7161                         else
7162                         {
7163                                 current_cell = current_desc->data;
7164                                 height = cell_w->h;
7165                                 width = cell_w->w;
7166                                 if(current_cell->height < height )
7167                                 {
7168                                         current_cell->height = height;
7169                                 }
7170                                 if(current_cell->width < width)
7171                                 {
7172                                         total_width += (width-current_cell->width);
7173                                         current_cell->width = width;
7174
7175
7176
7177                                 }
7178                                 current_desc = g_list_next(current_desc);
7179                         }
7180                         column_count++;
7181
7182                 }/* column loop */
7183
7184         } /*row loop */
7185
7186
7187         /**
7188          * If the width of all columns is less than the width off
7189          * the table expand each cell proportionally.
7190          *
7191          */
7192         if(total_width+(this->spacing*column_count) < w->w ) {
7193                 for(current_desc=column_desc; current_desc; current_desc=g_list_next(current_desc)) {
7194                         current_cell = (struct table_column_desc*) current_desc->data;
7195                         current_cell->width= ( (current_cell->width+this->spacing)/(float)total_width) * w->w ;
7196                 }
7197         }
7198
7199         return column_desc;
7200 }
7201
7202
7203 /**
7204  * @brief Computes the height and width for the table.
7205  *
7206  * The height and widht are computed to display all cells in the table
7207  * at the requested height/width.
7208  *
7209  * @param this The graphics context
7210  * @param w The widget to pack.
7211  *
7212  */
7213 void gui_internal_table_pack(struct gui_priv * this, struct widget * w)
7214 {
7215
7216         int height=0;
7217         int width=0;
7218         int count=0;
7219         GList * column_data = gui_internal_compute_table_dimensions(this,w);
7220         GList * current=0;
7221         struct table_column_desc * cell_desc=0;
7222         struct table_data * table_data = (struct table_data*)w->data;
7223
7224         for(current = column_data; current; current=g_list_next(current))
7225         {
7226                 if(table_data->button_box == current->data )
7227                 {
7228                         continue;
7229                 }
7230                 cell_desc = (struct table_column_desc *) current->data;
7231                 width = width + cell_desc->width + this->spacing;
7232                 if(height < cell_desc->height)
7233                 {
7234                         height = cell_desc->height ;
7235                 }
7236         }
7237
7238
7239
7240         for(current=w->children; current; current=g_list_next(current))
7241         {
7242                 if(current->data!= table_data->button_box)
7243                 {
7244                         count++;
7245                 }
7246         }
7247
7248         w->w = width;
7249         if(w->w + w->c.x > this->root.w)
7250         {
7251                 w->w = this->root.w - w->c.x;
7252         }
7253
7254
7255         if(w->h + w->c.y   > this->root.h   )
7256         {
7257                 /**
7258                  * Do not allow the widget to exceed the screen.
7259                  *
7260                  */
7261                 w->h = this->root.h- w->c.y  - height;
7262         }
7263
7264         if (table_data->button_box) 
7265         {
7266                 gui_internal_widget_pack(this,table_data->button_box);
7267         }
7268
7269
7270         /**
7271          * Deallocate column descriptions.
7272          */
7273         g_list_foreach(column_data,(GFunc)g_free,NULL);
7274         g_list_free(column_data);
7275 }
7276
7277
7278
7279
7280 /**
7281  * @brief Invalidates coordinates for previosly rendered table widget rows.
7282  *
7283  * @param table_data Data from the table object.
7284  */
7285 static void gui_internal_table_hide_rows(struct table_data * table_data)
7286 {
7287         GList*cur_row;
7288         for(cur_row=table_data->top_row; cur_row ; cur_row = g_list_next(cur_row))
7289         {
7290                 struct widget * cur_row_widget = (struct widget*)cur_row->data;
7291                 if (cur_row_widget->type!=widget_table_row)
7292                         continue;
7293                 cur_row_widget->p.x=0;
7294                 cur_row_widget->p.y=0;
7295                 cur_row_widget->w=0;
7296                 cur_row_widget->h=0;
7297                 if(cur_row==table_data->bottom_row)
7298                         break;
7299         }
7300 }
7301
7302
7303 /**
7304  * @brief Renders a table widget.
7305  *
7306  * @param this The graphics context
7307  * @param w The table widget to render.
7308  */
7309 void gui_internal_table_render(struct gui_priv * this, struct widget * w)
7310 {
7311
7312         int x;
7313         int y;
7314         GList * column_desc=NULL;
7315         GList * cur_row = NULL;
7316         GList * current_desc=NULL;
7317         struct table_data * table_data = (struct table_data*)w->data;
7318         int is_skipped=0;
7319         int is_first_page=1;
7320         struct table_column_desc * dim=NULL;
7321
7322         dbg_assert(table_data);
7323         column_desc = gui_internal_compute_table_dimensions(this,w);
7324         if(!column_desc)
7325                 return;
7326         y=w->p.y;
7327         gui_internal_table_hide_rows(table_data);
7328         /**
7329          * Skip rows that are on previous pages.
7330          */
7331         cur_row = w->children;
7332         if(table_data->top_row && table_data->top_row != w->children && !table_data->button_box_hide)
7333         {
7334                 cur_row = table_data->top_row;
7335                 is_first_page=0;
7336         } else {
7337                 table_data->top_row=NULL;
7338         }
7339         /**
7340          * Loop through each row.  Drawing each cell with the proper sizes,
7341          * at the proper positions.
7342          */
7343         for(table_data->top_row=cur_row; cur_row; cur_row = g_list_next(cur_row))
7344         {
7345                 int max_height=0, bbox_height=0;
7346                 struct widget * cur_row_widget;
7347                 GList * cur_column=NULL;
7348                 current_desc = column_desc;
7349                 cur_row_widget = (struct widget*)cur_row->data;
7350                 x =w->p.x+this->spacing;
7351                 if(cur_row_widget == table_data->button_box )
7352                 {
7353                         continue;
7354                 }
7355                 dim = (struct table_column_desc*)current_desc->data;
7356
7357                 if (table_data->button_box && !table_data->button_box_hide)
7358                         bbox_height=table_data->button_box->h;
7359
7360                 if( y + dim->height + bbox_height + this->spacing >= w->p.y + w->h )
7361                 {
7362                         /*
7363                          * No more drawing space left.
7364                          */
7365                         is_skipped=1;
7366                         break;
7367                 }
7368                 for(cur_column = cur_row_widget->children; cur_column;
7369                     cur_column=g_list_next(cur_column))
7370                 {
7371                         struct  widget * cur_widget = (struct widget*) cur_column->data;
7372                         dim = (struct table_column_desc*)current_desc->data;
7373
7374                         cur_widget->p.x=x;
7375                         cur_widget->w=dim->width;
7376                         cur_widget->p.y=y;
7377                         cur_widget->h=dim->height;
7378                         x=x+cur_widget->w;
7379                         max_height = dim->height;
7380                         /* We pack the widget before rendering to ensure that the x and y
7381                          * coordinates get pushed down.
7382                          */
7383                         gui_internal_widget_pack(this,cur_widget);
7384                         gui_internal_widget_render(this,cur_widget);
7385
7386                         if(dim->height > max_height)
7387                         {
7388                                 max_height = dim->height;
7389                         }
7390                 }
7391                 
7392                 /* Row object should have its coordinates in actual
7393                  * state to be able to pass mouse clicks to Column objects
7394                  */
7395                 cur_row_widget->p.x=w->p.x;
7396                 cur_row_widget->w=w->w;
7397                 cur_row_widget->p.y=y;
7398                 cur_row_widget->h=max_height;
7399                 y = y + max_height;
7400                 table_data->bottom_row=cur_row;
7401                 current_desc = g_list_next(current_desc);
7402         }
7403         if(table_data->button_box && (is_skipped || !is_first_page) && !table_data->button_box_hide )
7404         {
7405                 table_data->button_box->p.y =w->p.y+w->h-table_data->button_box->h -
7406                         this->spacing;
7407                 if(table_data->button_box->p.y < y )
7408                 {
7409                         table_data->button_box->p.y=y;
7410                 }
7411                 table_data->button_box->p.x = w->p.x;
7412                 table_data->button_box->w = w->w;
7413                 //    table_data->button_box->h = w->h - y;
7414                 //    table_data->next_button->h=table_data->button_box->h;
7415                 //    table_data->prev_button->h=table_data->button_box->h;
7416                 //    table_data->next_button->c.y=table_data->button_box->c.y;
7417                 //    table_data->prev_button->c.y=table_data->button_box->c.y;
7418                 gui_internal_widget_pack(this,table_data->button_box);
7419                 if(table_data->next_button->p.y > w->p.y + w->h + table_data->next_button->h)
7420                 {
7421                         table_data->button_box->p.y = w->p.y + w->h -
7422                                 table_data->button_box->h;
7423                 }
7424                 if(is_skipped)
7425                 {
7426                         table_data->next_button->state|= STATE_SENSITIVE;
7427                 }
7428                 else
7429                 {
7430                         table_data->next_button->state&= ~STATE_SENSITIVE;
7431                 }
7432
7433                 if(table_data->top_row != w->children)
7434                 {
7435                         table_data->prev_button->state|= STATE_SENSITIVE;
7436                 }
7437                 else
7438                 {
7439                         table_data->prev_button->state&= ~STATE_SENSITIVE;
7440                 }
7441                 gui_internal_widget_render(this,table_data->button_box);
7442         }
7443
7444         /**
7445          * Deallocate column descriptions.
7446          */
7447         g_list_foreach(column_desc,(GFunc)g_free,NULL);
7448         g_list_free(column_desc);
7449 }
7450
7451
7452 /**
7453  * @brief Displays Route information
7454  *
7455  * @li The name of the active vehicle
7456  * @param wm The button that was pressed.
7457  * @param v Unused
7458  */
7459 static void
7460 gui_internal_cmd2_route_description(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7461 {
7462
7463
7464         struct widget * menu;
7465         struct widget * row;
7466         struct widget * box;
7467
7468
7469         if(! this->vehicle_cb)
7470         {
7471           /**
7472            * Register the callback on vehicle updates.
7473            */
7474           this->vehicle_cb = callback_new_attr_1(callback_cast(gui_internal_route_update),
7475                                                        attr_position_coord_geo,this);
7476           navit_add_callback(this->nav,this->vehicle_cb);
7477         }
7478
7479         this->route_data.route_table = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
7480
7481         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
7482
7483
7484         menu=gui_internal_menu(this,_("Route Description"));
7485
7486         menu->free=gui_internal_route_screen_free;
7487         this->route_data.route_showing=1;
7488         this->route_data.route_table->spx = this->spacing;
7489
7490
7491         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
7492
7493         //      gui_internal_widget_append(box,gui_internal_box_new_with_label(this,"Test"));
7494         gui_internal_widget_append(box,this->route_data.route_table);
7495         box->w=menu->w;
7496         box->spx = this->spacing;
7497         this->route_data.route_table->w=box->w;
7498         gui_internal_widget_append(menu,box);
7499         gui_internal_populate_route_table(this,this->nav);
7500         gui_internal_menu_render(this);
7501
7502 }
7503
7504 static int
7505 line_intersection(struct coord* a1, struct coord *a2, struct coord * b1, struct coord *b2, struct coord *res)
7506 {
7507         int n, a, b;
7508         int adx=a2->x-a1->x;
7509         int ady=a2->y-a1->y;
7510         int bdx=b2->x-b1->x;
7511         int bdy=b2->y-b1->y;
7512         n = bdy * adx - bdx * ady;
7513         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
7514         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
7515         if (n < 0) {
7516                 n = -n;
7517                 a = -a;
7518                 b = -b;
7519         }
7520         if (a < 0 || b < 0)
7521                 return 0;
7522         if (a > n || b > n)
7523                 return 0;
7524         if (n == 0) {
7525                 dbg(0,"a=%d b=%d n=%d\n", a, b, n);
7526                 dbg(0,"a1=0x%x,0x%x ad %d,%d\n", a1->x, a1->y, adx, ady);
7527                 dbg(0,"b1=0x%x,0x%x bd %d,%d\n", b1->x, b1->y, bdx, bdy);
7528                 dbg_assert(n != 0);
7529         }
7530         res->x = a1->x + a * adx / n;
7531         res->y = a1->y + a * ady / n;
7532         return 1;
7533 }
7534
7535 struct heightline {
7536         struct heightline *next;
7537         int height;
7538         struct coord_rect bbox;
7539         int count;
7540         struct coord c[0];
7541 };
7542
7543 struct diagram_point {
7544         struct diagram_point *next;
7545         struct coord c;
7546 };
7547
7548 static struct heightline *
7549 item_get_heightline(struct item *item)
7550 {
7551         struct heightline *ret=NULL;
7552         struct street_data *sd;
7553         struct attr attr;
7554         int i,height;
7555
7556         if (item_attr_get(item, attr_label, &attr)) {
7557                 height=atoi(attr.u.str);
7558                 sd=street_get_data(item);
7559                 if (sd && sd->count > 1) {
7560                         ret=g_malloc(sizeof(struct heightline)+sd->count*sizeof(struct coord));
7561                         ret->bbox.lu=sd->c[0];
7562                         ret->bbox.rl=sd->c[0];
7563                         ret->count=sd->count;
7564                         ret->height=height;
7565                         for (i = 0 ; i < sd->count ; i++) {
7566                                 ret->c[i]=sd->c[i];
7567                                 coord_rect_extend(&ret->bbox, sd->c+i);
7568                         }
7569                 }
7570                 street_data_free(sd);
7571         }
7572         return ret;
7573 }
7574
7575
7576 /**
7577  * @brief Displays Route Height Profile
7578  *
7579  * @li The name of the active vehicle
7580  * @param wm The button that was pressed.
7581  * @param v Unused
7582  */
7583 static void
7584 gui_internal_cmd2_route_height_profile(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7585 {
7586
7587
7588         struct widget * menu, *box;
7589
7590         struct map * map=NULL;
7591         struct map_rect * mr=NULL;
7592         struct route * route;
7593         struct item * item =NULL;
7594         struct mapset *ms;
7595         struct mapset_handle *msh;
7596         int x,i,first=1,dist=0;
7597         struct coord c,last,res;
7598         struct coord_rect rbbox,dbbox;
7599         struct map_selection sel;
7600         struct heightline *heightline,*heightlines=NULL;
7601         struct diagram_point *min,*diagram_point,*diagram_points=NULL;
7602         sel.next=NULL;
7603         sel.order=18;
7604         sel.range.min=type_height_line_1;
7605         sel.range.max=type_height_line_3;
7606
7607
7608         menu=gui_internal_menu(this,_("Height Profile"));
7609         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
7610         gui_internal_widget_append(menu, box);
7611         route = navit_get_route(this->nav);
7612         if (route)
7613                 map = route_get_map(route);
7614         if(map)
7615                 mr = map_rect_new(map,NULL);
7616         if(mr) {
7617                 while((item = map_rect_get_item(mr))) {
7618                         while (item_coord_get(item, &c, 1)) {
7619                                 if (first) {
7620                                         first=0;
7621                                         sel.u.c_rect.lu=c;
7622                                         sel.u.c_rect.rl=c;
7623                                 } else
7624                                         coord_rect_extend(&sel.u.c_rect, &c);
7625                         }
7626                 }
7627                 map_rect_destroy(mr);
7628                 ms=navit_get_mapset(this->nav);
7629                 if (!first && ms) {
7630                         msh=mapset_open(ms);
7631                         while ((map=mapset_next(msh, 1))) {
7632                                 mr=map_rect_new(map, &sel);
7633                                 if (mr) {
7634                                         while((item = map_rect_get_item(mr))) {
7635                                                 if (item->type >= sel.range.min && item->type <= sel.range.max) {
7636                                                         heightline=item_get_heightline(item);
7637                                                         if (heightline) {
7638                                                                 heightline->next=heightlines;
7639                                                                 heightlines=heightline;
7640                                                         }
7641                                                 }
7642                                         }
7643                                         map_rect_destroy(mr);
7644                                 }
7645                         }
7646                         mapset_close(msh);
7647                 }
7648         }
7649         map=NULL;
7650         mr=NULL;
7651         if (route)
7652                 map = route_get_map(route);
7653         if(map)
7654                 mr = map_rect_new(map,NULL);
7655         if(mr && heightlines) {
7656                 while((item = map_rect_get_item(mr))) {
7657                         first=1;
7658                         while (item_coord_get(item, &c, 1)) {
7659                                 if (first)
7660                                         first=0;
7661                                 else {
7662                                         heightline=heightlines;
7663                                         rbbox.lu=last;
7664                                         rbbox.rl=last;
7665                                         coord_rect_extend(&rbbox, &c);
7666                                         while (heightline) {
7667                                                 if (coord_rect_overlap(&rbbox, &heightline->bbox)) {
7668                                                         for (i = 0 ; i < heightline->count - 1; i++) {
7669                                                                 if (heightline->c[i].x != heightline->c[i+1].x || heightline->c[i].y != heightline->c[i+1].y) {
7670                                                                         if (line_intersection(heightline->c+i, heightline->c+i+1, &last, &c, &res)) {
7671                                                                                 diagram_point=g_new(struct diagram_point, 1);
7672                                                                                 diagram_point->c.x=dist+transform_distance(projection_mg, &last, &res);
7673                                                                                 diagram_point->c.y=heightline->height;
7674                                                                                 diagram_point->next=diagram_points;
7675                                                                                 diagram_points=diagram_point;
7676                                                                                 dbg(0,"%d %d\n", diagram_point->c.x, diagram_point->c.y);
7677                                                                         }
7678                                                                 }
7679                                                         }
7680                                                 }
7681                                                 heightline=heightline->next;
7682                                         }
7683                                         dist+=transform_distance(projection_mg, &last, &c);
7684                                 }
7685                                 last=c;
7686                         }
7687
7688                 }
7689                 map_rect_destroy(mr);
7690         }
7691
7692
7693         gui_internal_menu_render(this);
7694         first=1;
7695         diagram_point=diagram_points;
7696         while (diagram_point) {
7697                 if (first) {
7698                         dbbox.lu=diagram_point->c;
7699                         dbbox.rl=diagram_point->c;
7700                         first=0;
7701                 } else
7702                         coord_rect_extend(&dbbox, &diagram_point->c);
7703                 diagram_point=diagram_point->next;
7704         }
7705         dbg(0,"%d %d %d %d\n", dbbox.lu.x, dbbox.lu.y, dbbox.rl.x, dbbox.rl.y);
7706         if (dbbox.rl.x > dbbox.lu.x && dbbox.lu.x*100/(dbbox.rl.x-dbbox.lu.x) <= 25)
7707                 dbbox.lu.x=0;
7708         if (dbbox.lu.y > dbbox.rl.y && dbbox.rl.y*100/(dbbox.lu.y-dbbox.rl.y) <= 25)
7709                 dbbox.rl.y=0;
7710         dbg(0,"%d,%d %dx%d\n", box->p.x, box->p.y, box->w, box->h);
7711         x=dbbox.lu.x;
7712         first=1;
7713         for (;;) {
7714                 struct point p[2];
7715                 min=NULL;
7716                 diagram_point=diagram_points;
7717                 while (diagram_point) {
7718                         if (diagram_point->c.x >= x && (!min || min->c.x > diagram_point->c.x))
7719                                 min=diagram_point;
7720                         diagram_point=diagram_point->next;
7721                 }
7722                 if (! min)
7723                         break;
7724                 p[1].x=(min->c.x-dbbox.lu.x)*(box->w-10)/(dbbox.rl.x-dbbox.lu.x)+box->p.x+5;
7725                 p[1].y=(min->c.y-dbbox.rl.y)*(box->h-10)/(dbbox.lu.y-dbbox.rl.y)+box->p.y+5;
7726                 dbg(0,"%d,%d=%d,%d\n",min->c.x, min->c.y, p[1].x,p[1].y);
7727                 graphics_draw_circle(this->gra, this->foreground, &p[1], 2);
7728                 if (first)
7729                         first=0;
7730                 else
7731                         graphics_draw_lines(this->gra, this->foreground, p, 2);
7732                 p[0]=p[1];
7733                 x=min->c.x+1;
7734         }
7735
7736
7737 }
7738
7739
7740 static void
7741 gui_internal_cmd2_locale(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7742 {
7743         struct widget *menu,*wb,*w;
7744         char *text;
7745
7746         graphics_draw_mode(this->gra, draw_mode_begin);
7747         menu=gui_internal_menu(this, _("Show Locale"));
7748         menu->spx=this->spacing*10;
7749         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
7750         gui_internal_widget_append(menu, wb);
7751         text=g_strdup_printf("LANG=%1$s (1=%3$s 2=%2$s)",getenv("LANG"),"2","1");
7752         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7753         w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7754         g_free(text);
7755 #ifdef HAVE_API_WIN32_BASE
7756         {
7757                 char country[32],lang[32];
7758 #ifdef HAVE_API_WIN32_CE
7759                 wchar_t wcountry[32],wlang[32];
7760
7761                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
7762                 WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
7763 #else
7764                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, lang, sizeof(lang));
7765 #endif
7766                 text=g_strdup_printf("LOCALE_SABBREVLANGNAME=%s",lang);
7767                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7768                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7769                 g_free(text);
7770 #ifdef HAVE_API_WIN32_CE
7771                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
7772                 WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
7773 #else
7774                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, country, sizeof(country));
7775 #endif
7776                 text=g_strdup_printf("LOCALE_SABBREVCTRYNAME=%s",country);
7777                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7778                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7779                 g_free(text);
7780         }
7781 #endif
7782
7783         gui_internal_menu_render(this);
7784         graphics_draw_mode(this->gra, draw_mode_end);
7785 }
7786
7787 static void
7788 gui_internal_cmd2_about(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7789 {
7790         struct widget *menu,*wb,*w;
7791         char *text;
7792
7793         graphics_draw_mode(this->gra, draw_mode_begin);
7794         menu=gui_internal_menu(this, _("About Navit"));
7795         menu->spx=this->spacing*10;
7796         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand);
7797         gui_internal_widget_append(menu, wb);
7798
7799         //Icon
7800         gui_internal_widget_append(wb, w=gui_internal_image_new(this, image_new_xs(this, "navit")));
7801         w->flags=gravity_top_center|orientation_horizontal|flags_fill;
7802
7803         //app name
7804         text=g_strdup_printf("%s",PACKAGE_NAME);
7805         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7806         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7807         g_free(text);
7808
7809         //Version
7810         text=g_strdup_printf("%s",version);
7811         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7812         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7813         g_free(text);
7814
7815         //Site
7816         text=g_strdup_printf("http://www.navit-project.org/");
7817         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7818         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7819         g_free(text);
7820
7821         //Authors
7822         text=g_strdup_printf("%s:",_("By"));
7823         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7824         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7825         g_free(text);
7826         text=g_strdup_printf("Martin Schaller");
7827         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7828         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7829         g_free(text);
7830         text=g_strdup_printf("Michael Farmbauer");
7831         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7832         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7833         g_free(text);
7834         text=g_strdup_printf("Alexander Atanasov");
7835         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7836         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7837         g_free(text);
7838         text=g_strdup_printf("Pierre Grandin");
7839         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7840         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7841         g_free(text);
7842
7843         //Contributors
7844         text=g_strdup_printf("%s",_("And all the Navit Team"));
7845         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7846         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7847         g_free(text);
7848         text=g_strdup_printf("%s",_("members and contributors."));
7849         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7850         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7851         g_free(text);
7852
7853         gui_internal_menu_render(this);
7854         graphics_draw_mode(this->gra, draw_mode_end);
7855 }
7856
7857 /**
7858  * @brief handles the 'next page' table event.
7859  * A callback function that is invoked when the 'next page' button is pressed
7860  * to advance the contents of a table widget.
7861  *
7862  * @param this The graphics context.
7863  * @param wm The button widget that was pressed.
7864  */
7865 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data)
7866 {
7867         struct widget * table_widget=NULL;
7868         struct table_data * table_data = NULL;
7869
7870         if(wm)
7871                 table_widget = (struct widget * ) wm->data;
7872         else 
7873                 table_widget = data;
7874
7875         if(table_widget && table_widget->type==widget_table)
7876                 table_data = (struct table_data*) table_widget->data;
7877
7878         if(table_data)
7879         {
7880                 GList *l=g_list_next(table_data->bottom_row);
7881                 if(l)   {
7882                         gui_internal_table_hide_rows(table_data);
7883                         table_data->top_row=l;
7884                 }
7885         }
7886
7887         if(wm)
7888                 wm->state&= ~STATE_HIGHLIGHTED;
7889
7890         gui_internal_menu_render(this);
7891 }
7892
7893
7894
7895 /**
7896  * @brief handles the 'previous page' table event.
7897  * A callback function that is invoked when the 'previous page' button is pressed
7898  * to go back in the contents of a table widget.
7899  *
7900  * @param this The graphics context.
7901  * @param wm The button widget that was pressed.
7902  */
7903 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data)
7904 {
7905         struct widget * table_widget = NULL;
7906         struct table_data * table_data = NULL;
7907         
7908         if(wm)
7909                 table_widget=(struct widget * ) wm->data;
7910         else 
7911                 table_widget=(struct widget * ) data;
7912
7913         if(table_widget && table_widget->type==widget_table)
7914                 table_data = (struct table_data*) table_widget->data;
7915
7916         if(table_data)  {
7917                 int bottomy=table_widget->p.y+table_widget->h;
7918                 int n;
7919                 GList *top=table_data->top_row;
7920                 struct widget *w=(struct widget*)top->data;
7921                 if(table_data->button_box->p.y!=0) {
7922                     bottomy=table_data->button_box->p.y;
7923                 }
7924                 n=(bottomy-w->p.y)/w->h;
7925                 while(n-- > 0 && (top=g_list_previous(top))!=NULL);
7926                 gui_internal_table_hide_rows(table_data);
7927                 table_data->top_row=top;
7928         }
7929         if(wm)
7930                 wm->state&= ~STATE_HIGHLIGHTED;
7931         gui_internal_menu_render(this);
7932 }
7933
7934
7935 /**
7936  * @brief deallocates a table_data structure.
7937  *
7938  */
7939 void gui_internal_table_data_free(void * p)
7940 {
7941
7942
7943         /**
7944          * @note button_box and its children (next_button,prev_button)
7945          * have their memory managed by the table itself.
7946          */
7947         g_free(p);
7948
7949
7950 }
7951
7952
7953 /**
7954  * @brief Called when the route is updated.
7955  */
7956 void gui_internal_route_update(struct gui_priv * this, struct navit * navit, struct vehicle *v)
7957 {
7958
7959         if(this->route_data.route_showing) {
7960                 gui_internal_populate_route_table(this,navit);
7961                 graphics_draw_mode(this->gra, draw_mode_begin);
7962                 gui_internal_menu_render(this);
7963                 graphics_draw_mode(this->gra, draw_mode_end);
7964         }
7965
7966
7967 }
7968
7969
7970 /**
7971  * @brief Called when the route screen is closed (deallocated).
7972  *
7973  * The main purpose of this function is to remove the widgets from
7974  * references route_data because those widgets are about to be freed.
7975  */
7976 void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w)
7977 {
7978         if(this_) {
7979                 this_->route_data.route_showing=0;
7980                 this_->route_data.route_table=NULL;
7981                 g_free(w);
7982         }
7983
7984 }
7985
7986 /**
7987  * @brief Populates the route  table with route information
7988  *
7989  * @param this The gui context
7990  * @param navit The navit object
7991  */
7992 void gui_internal_populate_route_table(struct gui_priv * this,
7993                                        struct navit * navit)
7994 {
7995         struct map * map=NULL;
7996         struct map_rect * mr=NULL;
7997         struct navigation * nav = NULL;
7998         struct item * item =NULL;
7999         struct attr attr;
8000         struct widget * label = NULL;
8001         struct widget * row = NULL;
8002         struct coord c;
8003         nav = navit_get_navigation(navit);
8004         if(!nav) {
8005                 return;
8006         }
8007         map = navigation_get_map(nav);
8008         if(map)
8009           mr = map_rect_new(map,NULL);
8010         if(mr) {
8011                 gui_internal_widget_table_clear(this,this->route_data.route_table);
8012                 while((item = map_rect_get_item(mr))) {
8013                         if(item_attr_get(item,attr_navigation_long,&attr)) {
8014                           row = gui_internal_widget_table_row_new(this,
8015                                                                   gravity_left
8016                                                                   | flags_fill
8017                                                                   | orientation_horizontal);
8018                           gui_internal_widget_append(this->route_data.route_table,row);
8019
8020                           label = gui_internal_label_new(this,attr.u.str);
8021                           gui_internal_widget_append(row,label);
8022
8023                           label->item=*item;
8024                           item_coord_get(item, &c, 1);
8025                           label->c.x=c.x;
8026                           label->c.y=c.y;
8027                           label->c.pro=map_projection(map);
8028                           label->func=gui_internal_cmd_position;
8029                           label->state|=STATE_SENSITIVE;
8030                           label->data=(void*)2;   
8031                         }
8032
8033                 }
8034
8035         }
8036 }
8037
8038 static struct command_table commands[] = {
8039         {"abort_navigation",command_cast(gui_internal_cmd2_abort_navigation)},
8040         {"back",command_cast(gui_internal_cmd2_back)},
8041         {"back_to_map",command_cast(gui_internal_cmd2_back_to_map)},
8042         {"bookmarks",command_cast(gui_internal_cmd2_bookmarks)},
8043         {"formerdests",command_cast(gui_internal_cmd_formerdests)},
8044         {"get_data",command_cast(gui_internal_get_data)},
8045         {"locale",command_cast(gui_internal_cmd2_locale)},
8046         {"log",command_cast(gui_internal_cmd_log)},
8047         {"menu",command_cast(gui_internal_cmd_menu2)},
8048         {"position",command_cast(gui_internal_cmd2_position)},
8049         {"pois",command_cast(gui_internal_cmd2_pois)},
8050         {"route_description",command_cast(gui_internal_cmd2_route_description)},
8051         {"route_height_profile",command_cast(gui_internal_cmd2_route_height_profile)},
8052         {"set",command_cast(gui_internal_cmd2_set)},
8053         {"setting_layout",command_cast(gui_internal_cmd2_setting_layout)},
8054         {"setting_maps",command_cast(gui_internal_cmd2_setting_maps)},
8055         {"setting_rules",command_cast(gui_internal_cmd2_setting_rules)},
8056         {"setting_vehicle",command_cast(gui_internal_cmd2_setting_vehicle)},
8057         {"town",command_cast(gui_internal_cmd2_town)},
8058         {"quit",command_cast(gui_internal_cmd2_quit)},
8059         {"write",command_cast(gui_internal_cmd_write)},
8060         {"about",command_cast(gui_internal_cmd2_about)},
8061
8062 };
8063
8064
8065 //##############################################################################################################
8066 //# Description:
8067 //# Comment:
8068 //# Authors: Martin Schaller (04/2008)
8069 //##############################################################################################################
8070 static struct gui_priv * gui_internal_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs, struct gui *gui)
8071 {
8072         struct color color_white={0xffff,0xffff,0xffff,0xffff};
8073         struct color color_black={0x0,0x0,0x0,0xffff};
8074         struct color back2_color={0x4141,0x4141,0x4141,0xffff};
8075
8076         struct gui_priv *this;
8077         struct attr *attr;
8078         *meth=gui_internal_methods;
8079         this=g_new0(struct gui_priv, 1);
8080         this->nav=nav;
8081
8082         this->self.type=attr_gui;
8083         this->self.u.gui=gui;
8084
8085         if ((attr=attr_search(attrs, NULL, attr_menu_on_map_click)))
8086                 this->menu_on_map_click=attr->u.num;
8087         else
8088                 this->menu_on_map_click=1;
8089
8090         if ((attr=attr_search(attrs, NULL, attr_on_map_click)))
8091                 this->on_map_click=g_strdup(attr->u.str);
8092
8093         if ((attr=attr_search(attrs, NULL, attr_signal_on_map_click)))
8094                 this->signal_on_map_click=attr->u.num;
8095
8096         if ((attr=attr_search(attrs, NULL, attr_callback_list))) {
8097                 command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this);
8098         }
8099
8100         if( (attr=attr_search(attrs,NULL,attr_font_size)))
8101         {
8102           this->config.font_size=attr->u.num;
8103         }
8104         else
8105         {
8106           this->config.font_size=-1;
8107         }
8108         if( (attr=attr_search(attrs,NULL,attr_icon_xs)))
8109         {
8110           this->config.icon_xs=attr->u.num;
8111         }
8112         else
8113         {
8114           this->config.icon_xs=-1;
8115         }
8116         if( (attr=attr_search(attrs,NULL,attr_icon_l)))
8117         {
8118           this->config.icon_l=attr->u.num;
8119         }
8120         else
8121         {
8122           this->config.icon_l=-1;
8123         }
8124         if( (attr=attr_search(attrs,NULL,attr_icon_s)))
8125         {
8126           this->config.icon_s=attr->u.num;
8127         }
8128         else
8129         {
8130           this->config.icon_s=-1;
8131         }
8132         if( (attr=attr_search(attrs,NULL,attr_spacing)))
8133         {
8134           this->config.spacing=attr->u.num;
8135         }
8136         else
8137         {
8138           this->config.spacing=-1;
8139         }
8140         if( (attr=attr_search(attrs,NULL,attr_gui_speech)))
8141         {
8142           this->speech=attr->u.num;
8143         }
8144         if( (attr=attr_search(attrs,NULL,attr_keyboard)))
8145           this->keyboard=attr->u.num;
8146         else
8147           this->keyboard=1;
8148
8149     if( (attr=attr_search(attrs,NULL,attr_fullscreen)))
8150       this->fullscreen=attr->u.num;
8151
8152         if( (attr=attr_search(attrs,NULL,attr_flags)))
8153               this->flags=attr->u.num;
8154         if( (attr=attr_search(attrs,NULL,attr_background_color)))
8155               this->background_color=*attr->u.color;
8156         else
8157               this->background_color=color_black;
8158         if( (attr=attr_search(attrs,NULL,attr_background_color2)))
8159                 this->background2_color=*attr->u.color;
8160         else
8161                 this->background2_color=back2_color;
8162         if( (attr=attr_search(attrs,NULL,attr_text_color)))
8163               this->text_foreground_color=*attr->u.color;
8164         else
8165               this->text_foreground_color=color_white;
8166         this->text_background_color=color_black;
8167         if( (attr=attr_search(attrs,NULL,attr_columns)))
8168               this->cols=attr->u.num;
8169         if( (attr=attr_search(attrs,NULL,attr_osd_configuration)))
8170               this->osd_configuration=*attr;
8171
8172         if( (attr=attr_search(attrs,NULL,attr_pitch)))
8173               this->pitch=attr->u.num;
8174         else
8175                 this->pitch=20;
8176         if( (attr=attr_search(attrs,NULL,attr_flags_town)))
8177                 this->flags_town=attr->u.num;
8178         else
8179                 this->flags_town=-1;
8180         if( (attr=attr_search(attrs,NULL,attr_flags_street)))
8181                 this->flags_street=attr->u.num;
8182         else
8183                 this->flags_street=-1;
8184         if( (attr=attr_search(attrs,NULL,attr_flags_house_number)))
8185                 this->flags_house_number=attr->u.num;
8186         else
8187                 this->flags_house_number=-1;
8188         if( (attr=attr_search(attrs,NULL,attr_radius)))
8189                 this->radius=attr->u.num;
8190         else
8191                 this->radius=10;
8192         this->data.priv=this;
8193         this->data.gui=&gui_internal_methods_ext;
8194         this->data.widget=&gui_internal_widget_methods;
8195         this->cbl=callback_list_new();
8196
8197         return this;
8198 }
8199
8200 //##############################################################################################################
8201 //# Description:
8202 //# Comment:
8203 //# Authors: Martin Schaller (04/2008)
8204 //##############################################################################################################
8205 void plugin_init(void)
8206 {
8207         plugin_register_gui_type("internal", gui_internal_new);
8208 }
8209
8210 static void
8211 gui_internal_destroy(struct gui_priv *this)
8212 {
8213
8214         graphics_font_destroy(this->fonts[0]);
8215         graphics_font_destroy(this->fonts[1]);
8216         graphics_font_destroy(this->fonts[2]);
8217         attr_free(this->click_coord_geo);
8218         attr_free(this->position_coord_geo);
8219         g_free(this->country_iso2);
8220         g_free(this->href);
8221         g_free(this->html_text);
8222         g_free(this->on_map_click);
8223         g_free(this);
8224 }