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