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