Recreate the navit git/gerrit project that vanished
[profile/ivi/navit.git] / 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         navit_destroy(navit.u.navit);
4609         config_remove_attr(config, &navit);
4610         gui_internal_destroy(this);
4611         event_main_loop_quit();
4612 }
4613
4614 static void
4615 gui_internal_window_closed(struct gui_priv *this)
4616 {
4617         gui_internal_cmd2_quit(this, NULL, NULL, NULL, NULL);
4618 }
4619
4620 static void
4621 gui_internal_cmd2_abort_navigation(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4622 {
4623         navit_set_destination(this->nav, NULL, NULL, 0);
4624 }
4625
4626 static void
4627 gui_internal_cmd_map_download_do(struct gui_priv *this, struct widget *wm, void *data)
4628 {
4629         char *text=g_strdup_printf(_("Download %s"),wm->name);
4630         struct widget *w, *wb;
4631         struct map *map=data;
4632         double bllon,bllat,trlon,trlat;
4633
4634         wb=gui_internal_menu(this, text);
4635         g_free(text);
4636         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4637         w->spy=this->spacing*3;
4638         gui_internal_widget_append(wb, w);
4639         if (sscanf(wm->prefix,"%lf,%lf,%lf,%lf",&bllon,&bllat,&trlon,&trlat) == 4) {
4640                 struct coord_geo g;
4641                 struct map_selection sel;
4642                 struct map_rect *mr;
4643                 struct item *item;
4644
4645                 sel.next=NULL;
4646                 sel.order=255;
4647                 g.lng=bllon;
4648                 g.lat=trlat;
4649                 transform_from_geo(projection_mg, &g, &sel.u.c_rect.lu);
4650                 g.lng=trlon;
4651                 g.lat=bllat;
4652                 transform_from_geo(projection_mg, &g, &sel.u.c_rect.rl);
4653                 sel.range.min=type_none;
4654                 sel.range.max=type_last;
4655                 mr=map_rect_new(map, &sel);
4656                 while ((item=map_rect_get_item(mr))) {
4657                         dbg(0,"item\n");
4658                 }
4659                 map_rect_destroy(mr);
4660         }
4661         
4662         dbg(0,"bbox=%s\n",wm->prefix);
4663         gui_internal_menu_render(this);
4664 }
4665
4666 static void
4667 gui_internal_cmd_map_download(struct gui_priv *this, struct widget *wm, void *data)
4668 {
4669         struct attr on, off, download_enabled, download_disabled;
4670         struct widget *w,*wb,*wma;
4671         struct map *map=data;
4672         FILE *f;
4673         char *search,buffer[256];
4674         int found,sp_match=0;
4675
4676         dbg(1,"wm=%p prefix=%s\n",wm,wm->prefix);
4677
4678         search=wm->prefix;
4679         if (search) {
4680                 found=0;
4681                 while(search[sp_match] == ' ')
4682                         sp_match++;
4683                 sp_match++;
4684         } else {
4685                 found=1;
4686         }
4687         on.type=off.type=attr_active;
4688         on.u.num=1;
4689         off.u.num=0;
4690         wb=gui_internal_menu(this, wm->name?wm->name:_("Map Download"));
4691         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4692         w->spy=this->spacing*3;
4693         gui_internal_widget_append(wb, w);
4694         if (!search) {
4695                 wma=gui_internal_button_map_attr_new(this, _("Active"), gravity_left_center|orientation_horizontal|flags_fill, map, &on, &off, 1);
4696                 gui_internal_widget_append(w, wma);
4697         }
4698
4699         download_enabled.type=download_disabled.type=attr_update;
4700         download_enabled.u.num=1;
4701         download_disabled.u.num=0;
4702         wma=gui_internal_button_map_attr_new(this
4703                 , _("Download Enabled")
4704                 , gravity_left_center|orientation_horizontal|flags_fill
4705                 , map
4706                 , &download_enabled
4707                 , &download_disabled
4708                 , 0);
4709         gui_internal_widget_append(w, wma);
4710
4711
4712         f=fopen("maps/areas.tsv","r");
4713         while (f && fgets(buffer, sizeof(buffer), f)) {
4714                 char *nl,*description,*description_size,*bbox,*size=NULL;
4715                 int sp=0;
4716                 if ((nl=strchr(buffer,'\n')))
4717                         *nl='\0';
4718                 if ((nl=strchr(buffer,'\r')))
4719                         *nl='\0';
4720                 while(buffer[sp] == ' ')
4721                         sp++;
4722                 if ((bbox=strchr(buffer,'\t')))
4723                         *bbox++='\0';
4724                 if (bbox && (size=strchr(bbox,'\t'))) 
4725                         *size++='\0';
4726                 if (search && !strcmp(buffer, search)) {
4727                         wma=gui_internal_button_new_with_callback(this, _("Download completely"), NULL, 
4728                                 gravity_left_center|orientation_horizontal|flags_fill, gui_internal_cmd_map_download_do, map);
4729                         wma->name=g_strdup(buffer+sp);
4730                         wma->prefix=g_strdup(bbox);
4731                         gui_internal_widget_append(w, wma);
4732                         found=1;
4733                 } else if (sp < sp_match)
4734                         found=0;
4735                 if (sp == sp_match && found && buffer[sp]) {
4736                         description=g_strdup(buffer+sp);
4737                         if (size)
4738                                 description_size=g_strdup_printf("%s (%s)",description,size);
4739                         else
4740                                 description_size=g_strdup(description);
4741                         wma=gui_internal_button_new_with_callback(this, description_size, NULL, 
4742                                 gravity_left_center|orientation_horizontal|flags_fill, gui_internal_cmd_map_download, map);
4743                         g_free(description_size);
4744                         wma->prefix=g_strdup(buffer);
4745                         wma->name=description;
4746                         gui_internal_widget_append(w, wma);
4747                 }
4748         }
4749         
4750         gui_internal_menu_render(this);
4751 }
4752
4753 static void
4754 gui_internal_cmd2_setting_maps(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
4755 {
4756         struct attr attr, on, off, description, type, data, url, active;
4757         struct widget *w,*wb,*wma;
4758         char *label;
4759         struct attr_iter *iter;
4760
4761         wb=gui_internal_menu(this, _("Maps"));
4762         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4763         w->spy=this->spacing*3;
4764         gui_internal_widget_append(wb, w);
4765         iter=navit_attr_iter_new();
4766         on.type=off.type=attr_active;
4767         on.u.num=1;
4768         off.u.num=0;
4769         while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
4770                 if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
4771                         label=g_strdup(description.u.str);
4772                 } else {
4773                         if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
4774                                 type.u.str="";
4775                         if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
4776                                 data.u.str="";
4777                         label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
4778                 }
4779                 if (map_get_attr(attr.u.map, attr_url, &url, NULL)) {
4780                         if (!map_get_attr(attr.u.map, attr_active, &active, NULL))
4781                                 active.u.num=1;
4782                         wma=gui_internal_button_new_with_callback(this, label, image_new_xs(this, active.u.num ? "gui_active" : "gui_inactive"),
4783                         gravity_left_center|orientation_horizontal|flags_fill,
4784                         gui_internal_cmd_map_download, attr.u.map);
4785                 } else {
4786                         wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
4787                                 attr.u.map, &on, &off, 1);
4788                 }       
4789                 gui_internal_widget_append(w, wma);
4790                 g_free(label);
4791         }
4792         navit_attr_iter_destroy(iter);
4793         gui_internal_menu_render(this);
4794
4795 }
4796 static void
4797 gui_internal_cmd_set_active_vehicle(struct gui_priv *this, struct widget *wm, void *data)
4798 {
4799         struct attr vehicle = {attr_vehicle,{wm->data}};
4800         navit_set_attr(this->nav, &vehicle);
4801 }
4802
4803 static void
4804 gui_internal_cmd_show_satellite_status(struct gui_priv *this, struct widget *wm, void *data)
4805 {
4806         struct widget *w,*wb,*row;
4807         struct attr attr,sat_attr;
4808         struct vehicle *v=wm->data;
4809         char *str;
4810         int i;
4811         enum attr_type types[]={attr_sat_prn, attr_sat_elevation, attr_sat_azimuth, attr_sat_snr};
4812
4813         wb=gui_internal_menu(this, _("Show Satellite Status"));
4814         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_satellite_status;
4815         gui_internal_menu_data(this)->redisplay_widget=wm;
4816         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4817         gui_internal_widget_append(wb, w);
4818         w = gui_internal_widget_table_new(this,gravity_center | orientation_vertical | flags_expand | flags_fill, 0);
4819         row = gui_internal_widget_table_row_new(this,gravity_left_top);
4820         gui_internal_widget_append(row, gui_internal_label_new(this, " PRN "));
4821         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Elevation ")));
4822         gui_internal_widget_append(row, gui_internal_label_new(this, _(" Azimuth ")));
4823         gui_internal_widget_append(row, gui_internal_label_new(this, " SNR "));
4824         gui_internal_widget_append(w,row);
4825         while (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
4826                 row = gui_internal_widget_table_row_new(this,gravity_left_top);
4827                 for (i = 0 ; i < sizeof(types)/sizeof(enum attr_type) ; i++) {
4828                         if (item_attr_get(attr.u.item, types[i], &sat_attr))
4829                                 str=g_strdup_printf("%ld", sat_attr.u.num);
4830                         else
4831                                 str=g_strdup("");
4832                         gui_internal_widget_append(row, gui_internal_label_new(this, str));
4833                         g_free(str);
4834                 }
4835                 gui_internal_widget_append(w,row);
4836         }
4837         gui_internal_widget_append(wb, w);
4838         gui_internal_menu_render(this);
4839 }
4840
4841 static void
4842 gui_internal_cmd_show_nmea_data(struct gui_priv *this, struct widget *wm, void *data)
4843 {
4844         struct widget *w,*wb;
4845         struct attr attr;
4846         struct vehicle *v=wm->data;
4847         wb=gui_internal_menu(this, _("Show NMEA Data"));
4848         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_nmea_data;
4849         gui_internal_menu_data(this)->redisplay_widget=wm;
4850         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
4851         gui_internal_widget_append(wb, w);
4852         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL))
4853                 gui_internal_widget_append(w, gui_internal_text_new(this, attr.u.str, gravity_left_center|orientation_vertical));
4854         gui_internal_menu_render(this);
4855 }
4856
4857 /**
4858  * A container to hold the selected vehicle and the desired profile in
4859  * one data item.
4860  */
4861 struct vehicle_and_profilename {
4862         struct vehicle *vehicle;
4863         char *profilename;
4864 };
4865
4866 /**
4867  * Figures out whether the given vehicle is the active vehicle.
4868  *
4869  * @return true if the vehicle is active, false otherwise.
4870  */
4871 static int
4872 gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle
4873         *vehicle)
4874 {
4875         struct attr active_vehicle;
4876
4877         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
4878         active_vehicle.u.vehicle=NULL;
4879
4880         return active_vehicle.u.vehicle == vehicle;
4881 }
4882
4883 static void
4884 save_vehicle_xml(struct vehicle *v)
4885 {
4886         struct attr attr;
4887         struct attr_iter *iter=vehicle_attr_iter_new();
4888         int childs=0;
4889         dbg(0,"enter\n");
4890         printf("<vehicle");
4891         while (vehicle_get_attr(v, attr_any_xml, &attr, iter)) {
4892                 if (ATTR_IS_OBJECT(attr.type))
4893                         childs=1;
4894                 else
4895                         printf(" %s=\"%s\"",attr_to_name(attr.type),attr_to_text(&attr, NULL, 1));
4896         }
4897         if (childs) {
4898                 printf(">\n");
4899                 printf("</vehicle>\n");
4900         } else
4901                 printf(" />\n");
4902         vehicle_attr_iter_destroy(iter);
4903 }
4904
4905
4906 /**
4907  * Reacts to a button press that changes a vehicle's active profile.
4908  *
4909  * @see gui_internal_add_vehicle_profile
4910  */
4911 static void
4912 gui_internal_cmd_set_active_profile(struct gui_priv *this, struct
4913                 widget *wm, void *data)
4914 {
4915         struct vehicle_and_profilename *vapn = data;
4916         struct vehicle *v = vapn->vehicle;
4917         char *profilename = vapn->profilename;
4918         struct attr vehicle_name_attr;
4919         char *vehicle_name = NULL;
4920         struct attr profilename_attr;
4921
4922         // Get the vehicle name
4923         vehicle_get_attr(v, attr_name, &vehicle_name_attr, NULL);
4924         vehicle_name = vehicle_name_attr.u.str;
4925
4926         dbg(0, "Changing vehicle %s to profile %s\n", vehicle_name,
4927                         profilename);
4928
4929         // Change the profile name
4930         profilename_attr.type = attr_profilename;
4931         profilename_attr.u.str = profilename;
4932         if(!vehicle_set_attr(v, &profilename_attr)) {
4933                 dbg(0, "Unable to set the vehicle's profile name\n");
4934         }
4935
4936     // Notify Navit that the routing should be re-done if this is the
4937     // active vehicle.
4938         if (gui_internal_is_active_vehicle(this, v)) {
4939                 struct attr vehicle;
4940                 vehicle.type=attr_vehicle;
4941                 vehicle.u.vehicle=v;
4942                 navit_set_attr(this->nav, &vehicle);
4943         }
4944         save_vehicle_xml(v);
4945 }
4946
4947 /**
4948  * Adds the vehicle profile to the GUI, allowing the user to pick a
4949  * profile for the currently selected vehicle.
4950  */
4951 static void
4952 gui_internal_add_vehicle_profile(struct gui_priv *this, struct widget
4953                 *parent, struct vehicle *v, struct vehicleprofile *profile)
4954 {
4955         // Just here to show up in the translation file, nice and close to
4956         // where the translations are actually used.
4957         struct attr profile_attr;
4958         struct attr *attr = NULL;
4959         char *name = NULL;
4960         char *active_profile = NULL;
4961         char *label = NULL;
4962         int active;
4963         struct vehicle_and_profilename *context = NULL;
4964
4965 #ifdef ONLY_FOR_TRANSLATION
4966         char *translations[] = {_n("car"), _n("bike"), _n("pedestrian")};
4967 #endif
4968
4969         // Figure out the profile name
4970         attr = attr_search(profile->attrs, NULL, attr_name);
4971         if (!attr) {
4972                 dbg(0, "Adding vehicle profile failed. attr==NULL");
4973                 return;
4974         }
4975         name = attr->u.str;
4976
4977         // Determine whether the profile is the active one
4978         if (vehicle_get_attr(v, attr_profilename, &profile_attr, NULL))
4979                 active_profile = profile_attr.u.str;
4980         active = active_profile != NULL && !strcmp(name, active_profile);
4981
4982         dbg(0, "Adding vehicle profile %s, active=%s/%i\n", name,
4983                         active_profile, active);
4984
4985         // Build a translatable label.
4986         if(active) {
4987                 label = g_strdup_printf(_("Current profile: %s"), _(name));
4988         } else {
4989                 label = g_strdup_printf(_("Change profile to: %s"), _(name));
4990         }
4991
4992         // Create the context object (the vehicle and the desired profile)
4993         context = g_new0(struct vehicle_and_profilename, 1);
4994         context->vehicle = v;
4995         context->profilename = name;
4996
4997         // Add the button
4998         gui_internal_widget_append(parent,
4999                 gui_internal_button_new_with_callback(
5000                         this, label,
5001                         image_new_xs(this, active ? "gui_active" : "gui_inactive"),
5002                         gravity_left_center|orientation_horizontal|flags_fill,
5003                         gui_internal_cmd_set_active_profile, context));
5004
5005         free(label);
5006 }
5007
5008 static void
5009 gui_internal_cmd_vehicle_settings(struct gui_priv *this, struct widget *wm, void *data)
5010 {
5011         struct widget *w,*wb;
5012         struct attr attr;
5013         struct vehicle *v=wm->data;
5014     struct vehicleprofile *profile = NULL;
5015         GList *profiles;
5016
5017         wb=gui_internal_menu(this, wm->text);
5018         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5019         gui_internal_widget_append(wb, w);
5020
5021     // Add the "Set as active" button if this isn't the active
5022     // vehicle.
5023         if (!gui_internal_is_active_vehicle(this, v)) {
5024                 gui_internal_widget_append(w,
5025                         gui_internal_button_new_with_callback(this, _("Set as active"),
5026                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5027                                 gui_internal_cmd_set_active_vehicle, wm->data));
5028         }
5029
5030         if (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
5031                 gui_internal_widget_append(w,
5032                         gui_internal_button_new_with_callback(this, _("Show Satellite status"),
5033                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5034                                 gui_internal_cmd_show_satellite_status, wm->data));
5035         }
5036         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL)) {
5037                 gui_internal_widget_append(w,
5038                         gui_internal_button_new_with_callback(this, _("Show NMEA data"),
5039                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
5040                                 gui_internal_cmd_show_nmea_data, wm->data));
5041         }
5042
5043     // Add all the possible vehicle profiles to the menu
5044         profiles = navit_get_vehicleprofiles(this->nav);
5045     while(profiles) {
5046         profile = (struct vehicleprofile *)profiles->data;
5047         gui_internal_add_vehicle_profile(this, w, v, profile);
5048                 profiles = g_list_next(profiles);
5049     }
5050
5051         callback_list_call_attr_2(this->cbl, attr_vehicle, w, wm->data);
5052         gui_internal_menu_render(this);
5053 }
5054
5055 static void
5056 gui_internal_cmd2_setting_vehicle(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5057 {
5058         struct attr attr,vattr;
5059         struct widget *w,*wb,*wl;
5060         struct attr_iter *iter;
5061         struct attr active_vehicle;
5062
5063
5064         wb=gui_internal_menu(this, _("Vehicle"));
5065         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5066         w->spy=this->spacing*3;
5067         gui_internal_widget_append(wb, w);
5068         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
5069                 active_vehicle.u.vehicle=NULL;
5070         iter=navit_attr_iter_new();
5071         while(navit_get_attr(this->nav, attr_vehicle, &attr, iter)) {
5072                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
5073                 wl=gui_internal_button_new_with_callback(this, vattr.u.str,
5074                         image_new_l(this, attr.u.vehicle == active_vehicle.u.vehicle ? "gui_active" : "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
5075                         gui_internal_cmd_vehicle_settings, attr.u.vehicle);
5076                 wl->text=g_strdup(vattr.u.str);
5077                 gui_internal_widget_append(w, wl);
5078         }
5079         navit_attr_iter_destroy(iter);
5080         gui_internal_menu_render(this);
5081 }
5082
5083
5084 static void
5085 gui_internal_cmd2_setting_rules(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5086 {
5087         struct widget *wb,*w;
5088         struct attr on,off;
5089         wb=gui_internal_menu(this, _("Rules"));
5090         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
5091         w->spy=this->spacing*3;
5092         gui_internal_widget_append(wb, w);
5093         on.u.num=1;
5094         off.u.num=0;
5095         on.type=off.type=attr_tracking;
5096         gui_internal_widget_append(w,
5097                 gui_internal_button_navit_attr_new(this, _("Lock on road"), gravity_left_center|orientation_horizontal|flags_fill,
5098                         &on, &off));
5099         on.u.num=0;
5100         off.u.num=-1;
5101         on.type=off.type=attr_orientation;
5102         gui_internal_widget_append(w,
5103                 gui_internal_button_navit_attr_new(this, _("Northing"), gravity_left_center|orientation_horizontal|flags_fill,
5104                         &on, &off));
5105         on.u.num=1;
5106         off.u.num=0;
5107         on.type=off.type=attr_follow_cursor;
5108         gui_internal_widget_append(w,
5109                 gui_internal_button_navit_attr_new(this, _("Map follows Vehicle"), gravity_left_center|orientation_horizontal|flags_fill,
5110                         &on, &off));
5111         gui_internal_menu_render(this);
5112 }
5113
5114 //##############################################################################################################
5115 //# Description:
5116 //# Comment:
5117 //# Authors: Martin Schaller (04/2008)
5118 //##############################################################################################################
5119 static void gui_internal_motion(void *data, struct point *p)
5120 {
5121
5122         struct gui_priv *this=data;
5123         if (!this->root.children) {
5124                 navit_handle_motion(this->nav, p);
5125                 return;
5126         }
5127         if (!this->pressed)
5128                 return;
5129         this->current=*p;
5130         if(!this->motion_timeout_callback)
5131                 this->motion_timeout_callback=callback_new_1(callback_cast(gui_internal_highlight), this);
5132         if(!this->motion_timeout_event)
5133                 this->motion_timeout_event=event_add_timeout(100,0, this->motion_timeout_callback);
5134 }
5135
5136 static const char *
5137 find_attr(const char **names, const char **values, const char *name)
5138 {
5139         while (*names) {
5140                 if (!g_strcasecmp(*names, name))
5141                         return *values;
5142                 names+=xml_attr_distance;
5143                 values+=xml_attr_distance;
5144         }
5145         return NULL;
5146 }
5147
5148 static char *
5149 find_attr_dup(const char **names, const char **values, const char *name)
5150 {
5151         return g_strdup(find_attr(names, values, name));
5152 }
5153
5154 static void
5155 gui_internal_evaluate(struct gui_priv *this, const char *command)
5156 {
5157         if (command)
5158                 command_evaluate(&this->self, command);
5159 }
5160
5161
5162 static void
5163 gui_internal_html_command(struct gui_priv *this, struct widget *w, void *data)
5164 {
5165         gui_internal_evaluate(this,w->command);
5166 }
5167
5168 static void
5169 gui_internal_html_submit_set(struct gui_priv *this, struct widget *w, struct form *form)
5170 {
5171         GList *l;
5172         if (w->form == form && w->name) {
5173                 struct attr *attr=attr_new_from_text(w->name, w->text?w->text:"");
5174                 if (attr)
5175                         gui_set_attr(this->self.u.gui, attr);
5176                 attr_free(attr);
5177         }
5178         l=w->children;
5179         while (l) {
5180                 w=l->data;
5181                 gui_internal_html_submit_set(this, w, form);
5182                 l=g_list_next(l);
5183         }
5184
5185 }
5186
5187 static void
5188 gui_internal_html_submit(struct gui_priv *this, struct widget *w, void *data)
5189 {
5190         struct widget *menu;
5191         GList *l;
5192
5193         dbg(1,"enter form %p %s\n",w->form,w->form->onsubmit);
5194         l=g_list_last(this->root.children);
5195         menu=l->data;
5196         graphics_draw_mode(this->gra, draw_mode_begin);
5197         gui_internal_highlight_do(this, NULL);
5198         gui_internal_menu_render(this);
5199         graphics_draw_mode(this->gra, draw_mode_end);
5200         gui_internal_html_submit_set(this, menu, w->form);
5201         gui_internal_evaluate(this,w->form->onsubmit);
5202 }
5203
5204 static void
5205 gui_internal_html_load_href(struct gui_priv *this, char *href, int replace)
5206 {
5207         if (replace)
5208                 gui_internal_prune_menu_count(this, 1, 0);
5209         if (href && href[0] == '#') {
5210                 dbg(1,"href=%s\n",href);
5211                 g_free(this->href);
5212                 this->href=g_strdup(href);
5213                 gui_internal_html_menu(this, this->html_text, href+1);
5214         }
5215 }
5216
5217 static void
5218 gui_internal_html_href(struct gui_priv *this, struct widget *w, void *data)
5219 {
5220         gui_internal_html_load_href(this, w->command, 0);
5221 }
5222
5223 struct div_flags_map {
5224         char *attr;
5225         char *val;
5226         enum flags flags;
5227 } div_flags_map[] = {
5228         {"gravity","none",gravity_none},
5229         {"gravity","left",gravity_left},
5230         {"gravity","xcenter",gravity_xcenter},
5231         {"gravity","right",gravity_right},
5232         {"gravity","top",gravity_top},
5233         {"gravity","ycenter",gravity_ycenter},
5234         {"gravity","bottom",gravity_bottom},
5235         {"gravity","left_top",gravity_left_top},
5236         {"gravity","top_center",gravity_top_center},
5237         {"gravity","right_top",gravity_right_top},
5238         {"gravity","left_center",gravity_left_center},
5239         {"gravity","center",gravity_center},
5240         {"gravity","right_center",gravity_right_center},
5241         {"gravity","left_bottom",gravity_left_bottom},
5242         {"gravity","bottom_center",gravity_bottom_center},
5243         {"gravity","right_bottom",gravity_right_bottom},
5244         {"expand","1",flags_expand},
5245         {"fill","1",flags_fill},
5246         {"orientation","horizontal",orientation_horizontal},
5247         {"orientation","vertical",orientation_vertical},
5248         {"orientation","horizontal_vertical",orientation_horizontal_vertical},
5249 };
5250
5251 static enum flags
5252 div_flag(const char **names, const char **values, char *name)
5253 {
5254         int i;
5255         enum flags ret=0;
5256         const char *value=find_attr(names, values, name);
5257         if (!value)
5258                 return ret;
5259         for (i = 0 ; i < sizeof(div_flags_map)/sizeof(struct div_flags_map); i++) {
5260                 if (!strcmp(div_flags_map[i].attr,name) && !strcmp(div_flags_map[i].val,value))
5261                         ret|=div_flags_map[i].flags;
5262         }
5263         return ret;
5264 }
5265
5266 static enum flags
5267 div_flags(const char **names, const char **values)
5268 {
5269         enum flags flags;
5270         flags = div_flag(names, values, "gravity");
5271         flags |= div_flag(names, values, "orientation");
5272         flags |= div_flag(names, values, "expand");
5273         flags |= div_flag(names, values, "fill");
5274         return flags;
5275 }
5276
5277 static struct widget *
5278 html_image(struct gui_priv *this, const char **names, const char **values)
5279 {
5280         const char *src, *size;
5281         struct graphics_image *img=NULL;
5282
5283         src=find_attr(names, values, "src");
5284         if (!src)
5285                 return NULL;
5286         size=find_attr(names, values, "size");
5287         if (!size)
5288                 size="l";
5289         if (!strcmp(size,"l"))
5290                 img=image_new_l(this, src);
5291         else if (!strcmp(size,"s"))
5292                 img=image_new_s(this, src);
5293         else if (!strcmp(size,"xs"))
5294                 img=image_new_xs(this, src);
5295         if (!img)
5296                 return NULL;
5297         return gui_internal_image_new(this, img);
5298 }
5299
5300 static void
5301 gui_internal_html_start(void *dummy, const char *tag_name, const char **names, const char **values, void *data, void *error)
5302 {
5303         struct gui_priv *this=data;
5304         int i;
5305         enum html_tag tag=html_tag_none;
5306         struct html *html=&this->html[this->html_depth];
5307         const char *cond, *type;
5308
5309         if (!g_strcasecmp(tag_name,"text"))
5310                 return;
5311         html->command=NULL;
5312         html->name=NULL;
5313         html->href=NULL;
5314         html->skip=0;
5315         cond=find_attr(names, values, "cond");
5316
5317         if (cond && !this->html_skip) {
5318                 if (!command_evaluate_to_boolean(&this->self, cond, NULL))
5319                         html->skip=1;
5320         }
5321
5322         for (i=0 ; i < sizeof(html_tag_map)/sizeof(struct html_tag_map); i++) {
5323                 if (!g_strcasecmp(html_tag_map[i].tag_name, tag_name)) {
5324                         tag=html_tag_map[i].tag;
5325                         break;
5326                 }
5327         }
5328         html->tag=tag;
5329         if (!this->html_skip && !html->skip) {
5330                 switch (tag) {
5331                 case html_tag_a:
5332                         html->name=find_attr_dup(names, values, "name");
5333                         if (html->name) {
5334                                 html->skip=this->html_anchor ? strcmp(html->name,this->html_anchor) : 0;
5335                                 if (!html->skip)
5336                                         this->html_anchor_found=1;
5337                         }
5338                         html->command=find_attr_dup(names, values, "onclick");
5339                         html->href=find_attr_dup(names, values, "href");
5340                         html->refresh_cond=find_attr_dup(names, values, "refresh_cond");
5341                         break;
5342                 case html_tag_img:
5343                         html->command=find_attr_dup(names, values, "onclick");
5344                         html->w=html_image(this, names, values);
5345                         break;
5346                 case html_tag_form:
5347                         this->form=g_new0(struct form, 1);
5348                         this->form->onsubmit=find_attr_dup(names, values, "onsubmit");
5349                         break;
5350                 case html_tag_input:
5351                         type=find_attr_dup(names, values, "type");
5352                         if (!type)
5353                                 break;
5354                         if (!strcmp(type,"image")) {
5355                                 html->w=html_image(this, names, values);
5356                                 if (html->w) {
5357                                         html->w->state|=STATE_SENSITIVE;
5358                                         html->w->func=gui_internal_html_submit;
5359                                 }
5360                         }
5361                         if (!strcmp(type,"text") || !strcmp(type,"password")) {
5362                                 html->w=gui_internal_label_new(this, NULL);
5363                                 html->w->background=this->background;
5364                                 html->w->flags |= div_flags(names, values);
5365                                 html->w->state|=STATE_EDITABLE;
5366                                 if (!this->editable) {
5367                                         this->editable=html->w;
5368                                         html->w->state|=STATE_EDIT;
5369                                 }
5370                                 this->keyboard_required=1;
5371                                 if (!strcmp(type,"password"))
5372                                         html->w->flags2 |= 1;
5373                         }
5374                         if (html->w) {
5375                                 html->w->form=this->form;
5376                                 html->w->name=find_attr_dup(names, values, "name");
5377                         }
5378                         break;
5379                 case html_tag_div:
5380                         html->w=gui_internal_box_new(this, div_flags(names, values));
5381                         html->container=this->html_container;
5382                         this->html_container=html->w;
5383                         break;
5384                 default:
5385                         break;
5386                 }
5387         }
5388         this->html_skip+=html->skip;
5389         this->html_depth++;
5390 }
5391
5392 static void
5393 gui_internal_html_end(void *dummy, const char *tag_name, void *data, void *error)
5394 {
5395         struct gui_priv *this=data;
5396         struct html *html;
5397         struct html *parent=NULL;
5398
5399         if (!g_strcasecmp(tag_name,"text"))
5400                 return;
5401         this->html_depth--;
5402         html=&this->html[this->html_depth];
5403         if (this->html_depth > 0)
5404                 parent=&this->html[this->html_depth-1];
5405
5406
5407         if (!this->html_skip) {
5408                 if (html->command && html->w) {
5409                         html->w->state |= STATE_SENSITIVE;
5410                         html->w->command=html->command;
5411                         html->w->func=gui_internal_html_command;
5412                         html->command=NULL;
5413                 }
5414                 if (parent && (parent->href || parent->command) && html->w) {
5415                         html->w->state |= STATE_SENSITIVE;
5416                         if (parent->command) {
5417                                 html->w->command=g_strdup(parent->command);
5418                                 html->w->func=gui_internal_html_command;
5419                         } else {
5420                                 html->w->command=g_strdup(parent->href);
5421                                 html->w->func=gui_internal_html_href;
5422                         }
5423                 }
5424                 switch (html->tag) {
5425                 case html_tag_div:
5426                         this->html_container=html->container;
5427                 case html_tag_img:
5428                 case html_tag_input:
5429                         gui_internal_widget_append(this->html_container, html->w);
5430                         break;
5431                 case html_tag_form:
5432                         this->form=NULL;
5433                         break;
5434                 default:
5435                         break;
5436                 }
5437         }
5438         this->html_skip-=html->skip;
5439         g_free(html->command);
5440         g_free(html->name);
5441         g_free(html->href);
5442         g_free(html->refresh_cond);
5443 }
5444
5445 static void
5446 gui_internal_refresh_callback_called(struct gui_priv *this, struct menu_data *menu_data)
5447 {
5448         if (gui_internal_menu_data(this) == menu_data) {
5449                 char *href=g_strdup(menu_data->href);
5450                 gui_internal_html_load_href(this, href, 1);
5451                 g_free(href);
5452         }
5453 }
5454
5455 static void
5456 gui_internal_set_refresh_callback(struct gui_priv *this, char *cond)
5457 {
5458         dbg(0,"cond=%s\n",cond);
5459         if (cond) {
5460                 enum attr_type type;
5461                 struct object_func *func;
5462                 struct menu_data *menu_data=gui_internal_menu_data(this);
5463                 dbg(0,"navit=%p\n",this->nav);
5464                 type=command_evaluate_to_attr(&this->self, cond, NULL, &menu_data->refresh_callback_obj);
5465                 if (type == attr_none)
5466                         return;
5467                 func=object_func_lookup(menu_data->refresh_callback_obj.type);
5468                 if (!func || !func->add_attr)
5469                         return;
5470                 menu_data->refresh_callback.type=attr_callback;
5471                 menu_data->refresh_callback.u.callback=callback_new_attr_2(callback_cast(gui_internal_refresh_callback_called),type,this,menu_data);
5472                 func->add_attr(menu_data->refresh_callback_obj.u.data, &menu_data->refresh_callback);
5473         }
5474 }
5475
5476 static void
5477 gui_internal_html_text(void *dummy, const char *text, int len, void *data, void *error)
5478 {
5479         struct gui_priv *this=data;
5480         struct widget *w;
5481         int depth=this->html_depth-1;
5482         struct html *html=&this->html[depth];
5483         gchar *text_stripped;
5484
5485         if (this->html_skip)
5486                 return;
5487         while (isspace(text[0])) {
5488                 text++;
5489                 len--;
5490         }
5491         while (len > 0 && isspace(text[len-1]))
5492                 len--;
5493
5494         text_stripped = g_strndup(text, len);
5495         if (html->tag == html_tag_html && depth > 2) {
5496                 if (this->html[depth-1].tag == html_tag_script) {
5497                         html=&this->html[depth-2];
5498                 }
5499         }
5500         switch (html->tag) {
5501         case html_tag_a:
5502                 if (html->name && len) {
5503                         this->html_container=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
5504                         gui_internal_widget_append(gui_internal_menu(this, _(text_stripped)), this->html_container);
5505                         gui_internal_menu_data(this)->href=g_strdup(this->href);
5506                         gui_internal_set_refresh_callback(this, html->refresh_cond);
5507                         this->html_container->spx=this->spacing*10;
5508                 }
5509                 break;
5510         case html_tag_h1:
5511                 if (!this->html_container) {
5512                         this->html_container=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
5513                         gui_internal_widget_append(gui_internal_menu(this, _(text_stripped)), this->html_container);
5514                         this->html_container->spx=this->spacing*10;
5515                 }
5516                 break;
5517         case html_tag_img:
5518                 if (len) {
5519                         w=gui_internal_box_new(this, gravity_center|orientation_vertical);
5520                         gui_internal_widget_append(w, html->w);
5521                         gui_internal_widget_append(w, gui_internal_text_new(this, _(text_stripped), gravity_center|orientation_vertical));
5522                         html->w=w;
5523                 }
5524                 break;
5525         case html_tag_div:
5526                 if (len) {
5527                         gui_internal_widget_append(html->w, gui_internal_text_new(this, _(text_stripped), gravity_center|orientation_vertical));
5528                 }
5529                 break;
5530         case html_tag_script:
5531                 dbg(1,"execute %s\n",text_stripped);
5532                 gui_internal_evaluate(this,text_stripped);
5533                 break;
5534         default:
5535                 break;
5536         }
5537         g_free(text_stripped);
5538 }
5539
5540 static void
5541 gui_internal_html_menu(struct gui_priv *this, const char *document, char *anchor)
5542 {
5543         char *doc=g_strdup(document);
5544         graphics_draw_mode(this->gra, draw_mode_begin);
5545         this->html_container=NULL;
5546         this->html_depth=0;
5547         this->html_anchor=anchor;
5548         this->html_anchor_found=0;
5549         this->form=NULL;
5550         this->keyboard_required=0;
5551         this->editable=NULL;
5552         callback_list_call_attr_2(this->cbl,attr_gui,anchor,&doc);
5553         xml_parse_text(doc, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
5554         g_free(doc);
5555         if (this->keyboard_required && this->keyboard) {
5556                 this->html_container->flags=gravity_center|orientation_vertical|flags_expand|flags_fill;
5557                 gui_internal_widget_append(this->html_container, gui_internal_keyboard(this,2));
5558         }
5559         gui_internal_menu_render(this);
5560         graphics_draw_mode(this->gra, draw_mode_end);
5561 }
5562
5563
5564 static void
5565 gui_internal_enter(struct gui_priv *this, int ignore)
5566 {
5567         struct graphics *gra=this->gra;
5568         this->ignore_button=ignore;
5569         this->clickp_valid=this->vehicle_valid=0;
5570
5571         navit_block(this->nav, 1);
5572         graphics_overlay_disable(gra, 1);
5573         this->root.p.x=0;
5574         this->root.p.y=0;
5575         this->root.background=this->background;
5576 }
5577
5578 static void
5579 gui_internal_leave(struct gui_priv *this)
5580 {
5581         graphics_draw_mode(this->gra, draw_mode_end);
5582 }
5583
5584 static void
5585 gui_internal_enter_setup(struct gui_priv *this, struct point *p)
5586 {
5587         struct transformation *trans;
5588         struct coord c;
5589         struct coord_geo g;
5590         struct attr attr,attrp;
5591
5592         trans=navit_get_trans(this->nav);
5593         attr_free(this->click_coord_geo);
5594         this->click_coord_geo=NULL;
5595         attr_free(this->position_coord_geo);
5596         this->position_coord_geo=NULL;
5597         if (p) {
5598                 transform_reverse(trans, p, &c);
5599                 dbg(1,"x=0x%x y=0x%x\n", c.x, c.y);
5600                 this->clickp.pro=transform_get_projection(trans);
5601                 this->clickp.x=c.x;
5602                 this->clickp.y=c.y;
5603                 transform_to_geo(this->clickp.pro, &c, &g);
5604                 attr.u.coord_geo=&g;
5605                 attr.type=attr_click_coord_geo;
5606                 this->click_coord_geo=attr_dup(&attr);
5607         }
5608         if (navit_get_attr(this->nav, attr_vehicle, &attr, NULL) && attr.u.vehicle
5609                 && vehicle_get_attr(attr.u.vehicle, attr_position_coord_geo, &attrp, NULL)) {
5610                 this->position_coord_geo=attr_dup(&attrp);
5611                 this->vehiclep.pro=transform_get_projection(trans);
5612                 transform_from_geo(this->vehiclep.pro, attrp.u.coord_geo, &c);
5613                 this->vehiclep.x=c.x;
5614                 this->vehiclep.y=c.y;
5615                 this->vehicle_valid=1;
5616         }
5617 }
5618
5619 static void
5620 gui_internal_cmd_menu(struct gui_priv *this, struct point *p, int ignore, char *href)
5621 {
5622         dbg(1,"enter\n");
5623         gui_internal_enter(this, ignore);
5624         gui_internal_enter_setup(this, p);
5625         // draw menu
5626         gui_internal_html_load_href(this, href?href:"#Main Menu", 0);
5627 }
5628
5629 static void
5630 gui_internal_cmd_menu2(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
5631 {
5632         char *href=NULL;
5633         int i=0, ignore=0, replace=0;
5634
5635         if (in && in[i] && ATTR_IS_INT(in[i]->type))
5636                 ignore=in[i++]->u.num;
5637
5638         if (in && in[i] && ATTR_IS_STRING(in[i]->type)) {
5639                 href=in[i++]->u.str;
5640                 if (in[i] && ATTR_IS_INT(in[i]->type))
5641                         replace=in[i++]->u.num;
5642         }
5643
5644         if (this->root.children) {
5645                 if (!href)
5646                         return;
5647                 gui_internal_html_load_href(this, href, replace);
5648                 return;
5649         }
5650
5651         gui_internal_cmd_menu(this, NULL, ignore, href);
5652 }
5653
5654
5655 static void
5656 gui_internal_cmd_log_do(struct gui_priv *this, struct widget *widget)
5657 {
5658         if (widget->text && strlen(widget->text)) {
5659                 if (this->vehicle_valid)
5660                         navit_textfile_debug_log_at(this->nav, &this->vehiclep, "type=log_entry label=\"%s\"",widget->text);
5661                 else
5662                         navit_textfile_debug_log(this->nav, "type=log_entry label=\"%s\"",widget->text);
5663         }
5664         g_free(widget->text);
5665         widget->text=NULL;
5666         gui_internal_prune_menu(this, NULL);
5667         gui_internal_check_exit(this);
5668 }
5669
5670 static void
5671 gui_internal_cmd_log_clicked(struct gui_priv *this, struct widget *widget, void *data)
5672 {
5673         gui_internal_cmd_log_do(this, widget->data);
5674 }
5675
5676 static void
5677 gui_internal_cmd_log_changed(struct gui_priv *this, struct widget *wm, void *data)
5678 {
5679         int len;
5680         if (wm->text) {
5681                 len=strlen(wm->text);
5682                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
5683                         wm->text[len-1]='\0';
5684                         gui_internal_cmd_log_do(this, wm);
5685                 }
5686         }
5687 }
5688
5689
5690 static void
5691 gui_internal_cmd_log(struct gui_priv *this)
5692 {
5693         struct widget *w,*wb,*wk,*wl,*we,*wnext;
5694         gui_internal_enter(this, 1);
5695         gui_internal_enter_setup(this, NULL);
5696         wb=gui_internal_menu(this, "Log Message");
5697         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
5698         gui_internal_widget_append(wb, w);
5699         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
5700         gui_internal_widget_append(w, we);
5701         gui_internal_widget_append(we, wk=gui_internal_label_new(this, _("Message")));
5702         wk->state |= STATE_EDIT|STATE_EDITABLE|STATE_CLEAR;
5703         wk->background=this->background;
5704         wk->flags |= flags_expand|flags_fill;
5705         wk->func = gui_internal_cmd_log_changed;
5706         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
5707         wnext->state |= STATE_SENSITIVE;
5708         wnext->func = gui_internal_cmd_log_clicked;
5709         wnext->data=wk;
5710         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
5711         gui_internal_widget_append(w, wl);
5712         if (this->keyboard)
5713                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
5714         gui_internal_menu_render(this);
5715         gui_internal_leave(this);
5716 }
5717
5718 static void
5719 gui_internal_check_exit(struct gui_priv *this)
5720 {
5721         struct graphics *gra=this->gra;
5722         if (! this->root.children) {
5723                 gui_internal_search_idle_end(this);
5724                 gui_internal_search_list_destroy(this);
5725                 graphics_overlay_disable(gra, 0);
5726                 if (!navit_block(this->nav, 0)) {
5727                         if (this->redraw)
5728                                 navit_draw(this->nav);
5729                         else
5730                                 navit_draw_displaylist(this->nav);
5731                 }
5732         }
5733 }
5734
5735 static int
5736 gui_internal_get_attr(struct gui_priv *this, enum attr_type type, struct attr *attr)
5737 {
5738         switch (type) {
5739         case attr_active:
5740                 attr->u.num=this->root.children != NULL;
5741                 break;
5742         case attr_click_coord_geo:
5743                 if (!this->click_coord_geo)
5744                         return 0;
5745                 *attr=*this->click_coord_geo;
5746                 break;
5747         case attr_position_coord_geo:
5748                 if (!this->position_coord_geo)
5749                         return 0;
5750                 *attr=*this->position_coord_geo;
5751                 break;
5752         case attr_pitch:
5753                 attr->u.num=this->pitch;
5754                 break;
5755         case attr_button:
5756                 attr->u.num=this->mouse_button_clicked_on_map;
5757                 break;
5758         default:
5759                 return 0;
5760         }
5761         attr->type=type;
5762         return 1;
5763 }
5764
5765 static int
5766 gui_internal_add_attr(struct gui_priv *this, struct attr *attr)
5767 {
5768         switch (attr->type) {
5769         case attr_xml_text:
5770                 g_free(this->html_text);
5771                 this->html_text=g_strdup(attr->u.str);
5772                 return 1;
5773         default:
5774                 return 0;
5775         }
5776 }
5777
5778 static int
5779 gui_internal_set_attr(struct gui_priv *this, struct attr *attr)
5780 {
5781         switch (attr->type) {
5782         case attr_fullscreen:
5783                 if ((this->fullscreen > 0) != (attr->u.num > 0)) {
5784                         graphics_draw_mode(this->gra, draw_mode_end);
5785                         this->win->fullscreen(this->win, attr->u.num > 0);
5786                         graphics_draw_mode(this->gra, draw_mode_begin);
5787                 }
5788                 this->fullscreen=attr->u.num;
5789                 return 1;
5790         case attr_menu_on_map_click:
5791                 this->menu_on_map_click=attr->u.num;
5792                 return 1;
5793         default:
5794                 dbg(0,"%s\n",attr_to_name(attr->type));
5795                 return 1;
5796         }
5797 }
5798
5799 static void gui_internal_dbus_signal(struct gui_priv *this, struct point *p)
5800 {
5801         struct displaylist_handle *dlh;
5802         struct displaylist *display;
5803         struct displayitem *di;
5804         struct attr cb,**attr_list=NULL;
5805         int valid=0;
5806
5807         display=navit_get_displaylist(this->nav);
5808         dlh=graphics_displaylist_open(display);
5809         while ((di=graphics_displaylist_next(dlh))) {
5810                 struct item *item=graphics_displayitem_get_item(di);
5811                 if (item_is_point(*item) && graphics_displayitem_get_displayed(di) &&
5812                         graphics_displayitem_within_dist(display, di, p, this->radius)) {
5813                         struct map_rect *mr=map_rect_new(item->map, NULL);
5814                         struct item *itemo=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
5815                         struct attr attr;
5816                         if (itemo && item_attr_get(itemo, attr_data, &attr))
5817                                 attr_list=attr_generic_add_attr(attr_list, &attr);
5818                         map_rect_destroy(mr);
5819                 }
5820         }
5821         graphics_displaylist_close(dlh);
5822         if (attr_list && navit_get_attr(this->nav, attr_callback_list, &cb, NULL))
5823                 callback_list_call_attr_4(cb.u.callback_list, attr_command, "dbus_send_signal", attr_list, NULL, &valid);
5824         attr_list_free(attr_list);
5825 }
5826
5827
5828
5829 //##############################################################################################################
5830 //# Description: Function to handle mouse clicks and scroll wheel movement
5831 //# Comment:
5832 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
5833 //##############################################################################################################
5834 static void gui_internal_button(void *data, int pressed, int button, struct point *p)
5835 {
5836         struct gui_priv *this=data;
5837         struct graphics *gra=this->gra;
5838
5839         dbg(1,"enter %d %d\n", pressed, button);
5840         // if still on the map (not in the menu, yet):
5841         dbg(1,"children=%p ignore_button=%d\n",this->root.children,this->ignore_button);
5842         if (!this->root.children || this->ignore_button) {
5843
5844                 this->ignore_button=0;
5845                 // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
5846                 if (!navit_handle_button(this->nav, pressed, button, p, NULL)) {
5847                         dbg(1,"navit has handled button\n");
5848                         return;
5849                 }
5850                 dbg(1,"menu_on_map_click=%d\n",this->menu_on_map_click);
5851                 if (button != 1)
5852                         return;
5853                 if (this->menu_on_map_click) {
5854                         this->mouse_button_clicked_on_map=1;
5855                         gui_internal_cmd_menu(this, p, 0, NULL);
5856                         this->mouse_button_clicked_on_map=0;
5857                         return;
5858                 }
5859                 if (this->signal_on_map_click) {
5860                         gui_internal_dbus_signal(this, p);
5861                         return;
5862                 }
5863                 return;
5864         }
5865
5866
5867         // if already in the menu:
5868         if (pressed) {
5869                 this->pressed=1;
5870                 this->current=*p;
5871                 gui_internal_highlight(this);
5872         } else {
5873                 this->pressed=0;
5874                 this->current.x=-1;
5875                 this->current.y=-1;
5876                 graphics_draw_mode(gra, draw_mode_begin);
5877                 gui_internal_call_highlighted(this);
5878                 if (!event_main_loop_has_quit()) {
5879                         gui_internal_highlight(this);
5880                         graphics_draw_mode(gra, draw_mode_end);
5881                         gui_internal_check_exit(this);
5882                 }
5883         }
5884 }
5885
5886 static void
5887 gui_internal_setup(struct gui_priv *this)
5888 {
5889         struct color cbh={0x9fff,0x9fff,0x9fff,0xffff};
5890         struct color cf={0xbfff,0xbfff,0xbfff,0xffff};
5891         struct graphics *gra=this->gra;
5892         unsigned char *buffer;
5893         char *gui_file;
5894         int size;
5895
5896         if (this->background)
5897                 return;
5898         this->background=graphics_gc_new(gra);
5899         this->background2=graphics_gc_new(gra);
5900         this->highlight_background=graphics_gc_new(gra);
5901         graphics_gc_set_foreground(this->highlight_background, &cbh);
5902         this->foreground=graphics_gc_new(gra);
5903         graphics_gc_set_foreground(this->foreground, &cf);
5904         this->text_background=graphics_gc_new(gra);
5905         this->text_foreground=graphics_gc_new(gra);
5906         graphics_gc_set_foreground(this->background, &this->background_color);
5907         graphics_gc_set_foreground(this->background2, &this->background2_color);
5908         graphics_gc_set_foreground(this->text_background, &this->text_background_color);
5909         graphics_gc_set_foreground(this->text_foreground, &this->text_foreground_color);
5910         gui_file=g_strjoin(NULL, navit_get_user_data_directory(TRUE), "/gui_internal.txt", NULL);
5911         if (file_get_contents(gui_file,&buffer,&size)) {
5912                 char *command=g_malloc(size+1);
5913                 strncpy(command,(const char *)buffer,size);
5914                 command[size]=0;
5915                 command_evaluate(&this->self, command);
5916                 g_free(command);
5917                 g_free(buffer);
5918         }
5919         g_free(gui_file);
5920 }
5921
5922 //##############################################################################################################
5923 //# Description:
5924 //# Comment:
5925 //# Authors: Martin Schaller (04/2008)
5926 //##############################################################################################################
5927 static void gui_internal_resize(void *data, int w, int h)
5928 {
5929         struct gui_priv *this=data;
5930         int changed=0;
5931
5932         gui_internal_setup(this);
5933
5934         if (this->root.w != w || this->root.h != h) {
5935                 this->root.w=w;
5936                 this->root.h=h;
5937                 changed=1;
5938         }
5939         dbg(1,"w=%d h=%d children=%p\n", w, h, this->root.children);
5940         navit_handle_resize(this->nav, w, h);
5941         if (this->root.children) {
5942                 if (changed) {
5943                         gui_internal_prune_menu(this, NULL);
5944                         gui_internal_html_load_href(this, "#Main Menu", 0);
5945                 } else {
5946                         gui_internal_menu_render(this);
5947                 }
5948         }
5949 }
5950
5951 static void
5952 gui_internal_keynav_point(struct widget *w, int dx, int dy, struct point *p)
5953 {
5954         p->x=w->p.x+w->w/2;
5955         p->y=w->p.y+w->h/2;
5956         if (dx < 0)
5957                 p->x=w->p.x;
5958         if (dx > 0)
5959                 p->x=w->p.x+w->w;
5960         if (dy < 0)
5961                 p->y=w->p.y;
5962         if (dy > 0)
5963                 p->y=w->p.y+w->h;
5964 }
5965
5966 static void
5967 gui_internal_keynav_find_closest(struct widget *wi, struct point *p, int dx, int dy, int *distance, struct widget **result)
5968 {
5969         GList *l=wi->children;
5970         // Skip hidden elements
5971         if (wi->p.x==0 && wi->p.y==0 && wi->w==0 && wi->h==0)
5972                 return;
5973         if ((wi->state & STATE_SENSITIVE) ) {
5974                 int dist1,dist2;
5975                 struct point wp;
5976                 gui_internal_keynav_point(wi, -dx, -dy, &wp);
5977                 if (dx) {
5978                         dist1=(wp.x-p->x)*dx;
5979                         dist2=wp.y-p->y;
5980                 } else if (dy) {
5981                         dist1=(wp.y-p->y)*dy;
5982                         dist2=wp.x-p->x;
5983                 } else {
5984                         dist2=wp.x-p->x;
5985                         dist1=wp.y-p->y;
5986                         if (dist1 < 0)
5987                                 dist1=-dist1;
5988                 }
5989                 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);
5990                 if (dist1 >= 0) {
5991                         if (dist2 < 0)
5992                                 dist1-=dist2;
5993                         else
5994                                 dist1+=dist2;
5995                         if (dist1 < *distance) {
5996                                 *result=wi;
5997                                 *distance=dist1;
5998                         }
5999                 }
6000         }
6001         while (l) {
6002                 struct widget *child=l->data;
6003                 gui_internal_keynav_find_closest(child, p, dx, dy, distance, result);
6004                 l=g_list_next(l);
6005         }
6006 }
6007
6008 static void
6009 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy)
6010 {
6011         struct widget *result,*menu=g_list_last(this->root.children)->data;
6012         struct point p;
6013         int distance;
6014         if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
6015                 gui_internal_keynav_point(this->highlighted, dx, dy, &p);
6016         else {
6017                 p.x=0;
6018                 p.y=0;
6019                 distance=INT_MAX;
6020                 result=NULL;
6021                 gui_internal_keynav_find_closest(menu, &p, 0, 0, &distance, &result);
6022                 if (result) {
6023                         gui_internal_keynav_point(result, dx, dy, &p);
6024                         dbg(1,"result origin=%p p=%d,%d\n", result, p.x, p.y);
6025                 }
6026         }
6027         result=NULL;
6028         distance=INT_MAX;
6029         gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
6030         dbg(1,"result=%p\n", result);
6031         if (! result) {
6032                 if (dx < 0)
6033                         p.x=this->root.w;
6034                 if (dx > 0)
6035                         p.x=0;
6036                 if (dy < 0)
6037                         p.y=this->root.h;
6038                 if (dy > 0)
6039                         p.y=0;
6040                 result=NULL;
6041                 distance=INT_MAX;
6042                 gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
6043                 dbg(1,"wraparound result=%p\n", result);
6044         }
6045         gui_internal_highlight_do(this, result);
6046         if (result)
6047                 gui_internal_say(this, result, 1);
6048 }
6049
6050 //##############################################################################################################
6051 //# Description:
6052 //# Comment:
6053 //# Authors: Martin Schaller (04/2008)
6054 //##############################################################################################################
6055 static void gui_internal_keypress(void *data, char *key)
6056 {
6057         struct gui_priv *this=data;
6058         int w,h;
6059         struct point p;
6060         if (!this->root.children) {
6061                 transform_get_size(navit_get_trans(this->nav), &w, &h);
6062                 switch (*key) {
6063                 case NAVIT_KEY_UP:
6064                         p.x=w/2;
6065                         p.y=0;
6066                         navit_set_center_screen(this->nav, &p, 1);
6067                         break;
6068                 case NAVIT_KEY_DOWN:
6069                         p.x=w/2;
6070                         p.y=h;
6071                         navit_set_center_screen(this->nav, &p, 1);
6072                         break;
6073                 case NAVIT_KEY_LEFT:
6074                         p.x=0;
6075                         p.y=h/2;
6076                         navit_set_center_screen(this->nav, &p, 1);
6077                         break;
6078                 case NAVIT_KEY_RIGHT:
6079                         p.x=w;
6080                         p.y=h/2;
6081                         navit_set_center_screen(this->nav, &p, 1);
6082                         break;
6083                 case NAVIT_KEY_ZOOM_IN:
6084                         navit_zoom_in(this->nav, 2, NULL);
6085                         break;
6086                 case NAVIT_KEY_ZOOM_OUT:
6087                         navit_zoom_out(this->nav, 2, NULL);
6088                         break;
6089                 case NAVIT_KEY_RETURN:
6090                 case NAVIT_KEY_MENU:
6091                         gui_internal_cmd_menu(this, NULL, 0, NULL);
6092                         break;
6093                 }
6094                 return;
6095         }
6096         graphics_draw_mode(this->gra, draw_mode_begin);
6097         switch (*key) {
6098         case NAVIT_KEY_LEFT:
6099                 gui_internal_keynav_highlight_next(this,-1,0);
6100                 break;
6101         case NAVIT_KEY_RIGHT:
6102                 gui_internal_keynav_highlight_next(this,1,0);
6103                 break;
6104         case NAVIT_KEY_UP:
6105                 gui_internal_keynav_highlight_next(this,0,-1);
6106                 break;
6107         case NAVIT_KEY_DOWN:
6108                 gui_internal_keynav_highlight_next(this,0,1);
6109                 break;
6110         case NAVIT_KEY_BACK:
6111                 if (g_list_length(this->root.children) > 1)
6112                         gui_internal_back(this, NULL, NULL);
6113                 else
6114                         gui_internal_prune_menu(this, NULL);
6115                 break;
6116         case NAVIT_KEY_RETURN:
6117                 if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
6118                         gui_internal_call_highlighted(this);
6119                 else
6120                         gui_internal_keypress_do(this, key);
6121                 break;
6122         default:
6123                 gui_internal_keypress_do(this, key);
6124         }
6125         if (!event_main_loop_has_quit()) {
6126                 graphics_draw_mode(this->gra, draw_mode_end);
6127                 gui_internal_check_exit(this);
6128         }
6129 }
6130
6131
6132 //##############################################################################################################
6133 //# Description:
6134 //# Comment:
6135 //# Authors: Martin Schaller (04/2008)
6136 //##############################################################################################################
6137 static int gui_internal_set_graphics(struct gui_priv *this, struct graphics *gra)
6138 {
6139         struct window *win;
6140         struct transformation *trans=navit_get_trans(this->nav);
6141
6142         win=graphics_get_data(gra, "window");
6143         if (! win)
6144                 return 1;
6145         navit_ignore_graphics_events(this->nav, 1);
6146         this->gra=gra;
6147         this->win=win;
6148         navit_ignore_graphics_events(this->nav, 1);
6149         transform_get_size(trans, &this->root.w, &this->root.h);
6150         this->resize_cb=callback_new_attr_1(callback_cast(gui_internal_resize), attr_resize, this);
6151         graphics_add_callback(gra, this->resize_cb);
6152         this->button_cb=callback_new_attr_1(callback_cast(gui_internal_button), attr_button, this);
6153         graphics_add_callback(gra, this->button_cb);
6154         this->motion_cb=callback_new_attr_1(callback_cast(gui_internal_motion), attr_motion, this);
6155         graphics_add_callback(gra, this->motion_cb);
6156         this->keypress_cb=callback_new_attr_1(callback_cast(gui_internal_keypress), attr_keypress, this);
6157         graphics_add_callback(gra, this->keypress_cb);
6158         this->window_closed_cb=callback_new_attr_1(callback_cast(gui_internal_window_closed), attr_window_closed, this);
6159         graphics_add_callback(gra, this->window_closed_cb);
6160
6161         // set fullscreen if needed
6162         if (this->fullscreen)
6163                 this->win->fullscreen(this->win, this->fullscreen != 0);
6164         /* Was resize callback already issued? */
6165         if (navit_get_ready(this->nav) & 2)
6166                 gui_internal_setup(this);
6167         return 0;
6168 }
6169
6170 static void gui_internal_disable_suspend(struct gui_priv *this)
6171 {
6172         if (this->win->disable_suspend)
6173                 this->win->disable_suspend(this->win);
6174 }
6175
6176 //##############################################################################################################
6177 //# Description:
6178 //# Comment:
6179 //# Authors: Martin Schaller (04/2008)
6180 //##############################################################################################################
6181 struct gui_methods gui_internal_methods = {
6182         NULL,
6183         NULL,
6184         gui_internal_set_graphics,
6185         NULL,
6186         NULL,
6187         NULL,
6188         gui_internal_disable_suspend,
6189         gui_internal_get_attr,
6190         gui_internal_add_attr,
6191         gui_internal_set_attr,
6192 };
6193
6194         static void
6195 gui_internal_get_data(struct gui_priv *priv, char *command, struct attr **in, struct attr ***out)
6196 {
6197         struct attr private_data = { attr_private_data, {(void *)&priv->data}};
6198         if (out)
6199                 *out=attr_generic_add_attr(*out, &private_data);
6200 }
6201
6202 static void
6203 gui_internal_add_callback(struct gui_priv *priv, struct callback *cb)
6204 {
6205         callback_list_add(priv->cbl, cb);
6206 }
6207
6208 static void
6209 gui_internal_remove_callback(struct gui_priv *priv, struct callback *cb)
6210 {
6211         callback_list_remove(priv->cbl, cb);
6212 }
6213
6214
6215 static struct gui_internal_methods gui_internal_methods_ext = {
6216         gui_internal_add_callback,
6217         gui_internal_remove_callback,
6218         gui_internal_menu_render,
6219         image_new_xs,
6220         image_new_l,
6221 };
6222
6223
6224 static enum flags
6225 gui_internal_get_flags(struct widget *widget)
6226 {
6227         return widget->flags;
6228 }
6229
6230 static void
6231 gui_internal_set_flags(struct widget *widget, enum flags flags)
6232 {
6233         widget->flags=flags;
6234 }
6235
6236 static int
6237 gui_internal_get_state(struct widget *widget)
6238 {
6239         return widget->state;
6240 }
6241
6242 static void
6243 gui_internal_set_state(struct widget *widget, int state)
6244 {
6245         widget->state=state;
6246 }
6247
6248 static void
6249 gui_internal_set_func(struct widget *widget, void (*func)(struct gui_priv *priv, struct widget *widget, void *data))
6250 {
6251         widget->func=func;
6252 }
6253
6254 static void
6255 gui_internal_set_data(struct widget *widget, void *data)
6256 {
6257         widget->data=data;
6258 }
6259
6260 static void
6261 gui_internal_set_default_background(struct gui_priv *this, struct widget *widget)
6262 {
6263         widget->background=this->background;
6264 }
6265
6266 static struct gui_internal_widget_methods gui_internal_widget_methods = {
6267         gui_internal_widget_append,
6268         gui_internal_button_new,
6269         gui_internal_button_new_with_callback,
6270         gui_internal_box_new,
6271         gui_internal_label_new,
6272         gui_internal_image_new,
6273         gui_internal_keyboard,
6274         gui_internal_menu,
6275         gui_internal_get_flags,
6276         gui_internal_set_flags,
6277         gui_internal_get_state,
6278         gui_internal_set_state,
6279         gui_internal_set_func,
6280         gui_internal_set_data,
6281         gui_internal_set_default_background,
6282 };
6283
6284 static void
6285 gui_internal_cmd_write(struct gui_priv * this, char *function, struct attr **in, struct attr ***out, int *valid)
6286 {
6287         char *str=NULL,*str2=NULL;
6288         dbg(1,"enter %s %p %p %p\n",function,in,out,valid);
6289         if (!in || !in[0])
6290                 return;
6291         dbg(1,"%s\n",attr_to_name(in[0]->type));
6292         if (ATTR_IS_STRING(in[0]->type)) {
6293                 str=in[0]->u.str;
6294         }
6295         if (ATTR_IS_COORD_GEO(in[0]->type)) {
6296                 str=str2=coordinates_geo(in[0]->u.coord_geo, '\n');
6297         }
6298         if (str) {
6299                 str=g_strdup_printf("<html>%s</html>\n",str);
6300                 xml_parse_text(str, this, gui_internal_html_start, gui_internal_html_end, gui_internal_html_text);
6301         }
6302         g_free(str);
6303         g_free(str2);
6304 }
6305
6306
6307 /**
6308  * @brief Creates a new table widget.
6309  *
6310  * Creates and returns a new table widget.  This function will
6311  * setup next/previous buttons as children.
6312  *
6313  * @param this The graphics context.
6314  * @param flags widget sizing flags.
6315  * @returns The newly created widget
6316  */
6317 struct widget * gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons)
6318 {
6319         struct widget * widget = g_new0(struct widget,1);
6320         struct table_data * data = NULL;
6321         widget->type=widget_table;
6322         widget->flags=flags;
6323         widget->data = g_new0(struct table_data,1);
6324         widget->data_free=gui_internal_table_data_free;
6325
6326         // We have to set background here explicitly
6327         // because it will be used by inner elements created later in this 
6328         // function (navigation buttons). Else that elements won't have
6329         // any background.
6330         widget->background=this->background;
6331         data = (struct table_data*)widget->data;
6332
6333         if (buttons) {
6334                 data->next_button=gui_internal_box_new(this, gravity_center|orientation_horizontal);
6335                 gui_internal_widget_append(data->next_button, gui_internal_text_new(this,_("Next"),gravity_center|orientation_horizontal));
6336                 gui_internal_widget_append(data->next_button, gui_internal_image_new(this, image_new_xs(this, "gui_arrow_right")));
6337                 data->next_button->func=gui_internal_table_button_next;
6338                 data->next_button->data=widget;
6339
6340
6341                 data->prev_button =  gui_internal_button_new_with_callback
6342                         (this, _("Prev"),
6343                         image_new_xs(this, "gui_arrow_left"),
6344                         gravity_center |orientation_horizontal,
6345                         gui_internal_table_button_prev,NULL);
6346
6347                 data->prev_button->data=widget;
6348
6349                 data->this=this;
6350
6351                 data->button_box=gui_internal_box_new(this,
6352                                               gravity_center|orientation_horizontal);
6353                 gui_internal_widget_append(widget, data->button_box);
6354                 gui_internal_widget_append(data->button_box, data->prev_button);
6355                 gui_internal_widget_append(data->button_box, data->next_button);
6356
6357                 data->button_box->bl=this->spacing;
6358                 gui_internal_widget_pack(this,data->button_box);
6359         }
6360
6361         return widget;
6362
6363 }
6364
6365 /**
6366  * @brief Clears all the rows from the table.
6367  * This function removes all rows from a table.
6368  * New rows can later be added to the table.
6369  */
6370 void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table)
6371 {
6372   GList * iter;
6373   struct table_data * table_data = (struct table_data* ) table->data;
6374
6375   iter = table->children;
6376   while(iter ) {
6377           if(iter->data != table_data->button_box) {
6378                   struct widget * child = (struct widget*)iter->data;
6379                   gui_internal_widget_destroy(this,child);
6380                   if(table->children == iter) {
6381                           table->children = g_list_remove(iter,iter->data);
6382                           iter=table->children;
6383                   }
6384                   else
6385                           iter = g_list_remove(iter,iter->data);
6386           }
6387           else {
6388                   iter = g_list_next(iter);
6389           }
6390
6391   }
6392   table_data->top_row=NULL;
6393   table_data->bottom_row=NULL;
6394   if(table_data->page_headers)
6395           g_list_free(table_data->page_headers);
6396   table_data->page_headers=NULL;
6397 }
6398
6399 /**
6400  * @brief Check if table has any data rows filled.
6401  * @param this The graphics context
6402  * @param table table widget
6403  * @returns 1 if the table is empty, 0 if there any data rows present.
6404  */
6405 static int gui_internal_widget_table_is_empty(struct gui_priv *this, struct widget * table)
6406 {
6407    GList *l;
6408    struct table_data *td=(struct table_data*) table->data;
6409
6410    for(l=table->children;l;l=g_list_next(l)) {
6411         if(l->data != td->button_box)
6412                 return 0;
6413    }
6414
6415    return 1;
6416 }
6417
6418 /**
6419  * Creates a new table_row widget.
6420  * @param this The graphics context
6421  * @param flags Sizing flags for the row
6422  * @returns The new table_row widget.
6423  */
6424 struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags)
6425 {
6426         struct widget * widget = g_new0(struct widget,1);
6427         widget->type=widget_table_row;
6428         widget->flags=flags;
6429         return widget;
6430 }
6431
6432
6433
6434 /**
6435  * @brief Computes the column dimensions for the table.
6436  *
6437  * @param w The table widget to compute dimensions for.
6438  *
6439  * This function examines all of the rows and columns for the table w
6440  * and returns a list (GList) of table_column_desc elements that
6441  * describe each column of the table.
6442  *
6443  * The caller is responsible for freeing the returned list.
6444  */
6445 static GList * gui_internal_compute_table_dimensions(struct gui_priv * this,struct widget * w)
6446 {
6447
6448         GList * column_desc = NULL;
6449         GList * current_desc=NULL;
6450         GList * cur_row = w->children;
6451         struct widget * cur_row_widget=NULL;
6452         GList * cur_column=NULL;
6453         struct widget * cell_w=NULL;
6454         struct table_column_desc * current_cell=NULL;
6455         struct table_data * table_data=NULL;
6456         int height=0;
6457         int width=0;
6458         int total_width=0;
6459         int column_count=0;
6460
6461         /**
6462          * Scroll through the the table and
6463          * 1. Compute the maximum width + height of each column across all rows.
6464          */
6465         table_data = (struct table_data*) w->data;
6466         for(cur_row=w->children;  cur_row ; cur_row = g_list_next(cur_row) )
6467         {
6468                 cur_row_widget = (struct widget*) cur_row->data;
6469                 current_desc = column_desc;
6470                 if(cur_row_widget == table_data->button_box)
6471                 {
6472                         continue;
6473                 }
6474                 column_count=0;
6475                 for(cur_column = cur_row_widget->children; cur_column;
6476                     cur_column=g_list_next(cur_column))
6477                 {
6478                         cell_w = (struct widget*) cur_column->data;
6479                         gui_internal_widget_pack(this,cell_w);
6480                         if(current_desc == 0)
6481                         {
6482                                 current_cell = g_new0(struct table_column_desc,1);
6483                                 column_desc = g_list_append(column_desc,current_cell);
6484                                 current_desc = g_list_last(column_desc);
6485                                 current_cell->height=cell_w->h;
6486                                 current_cell->width=cell_w->w;
6487                                 total_width+=cell_w->w;
6488
6489                         }
6490                         else
6491                         {
6492                                 current_cell = current_desc->data;
6493                                 height = cell_w->h;
6494                                 width = cell_w->w;
6495                                 if(current_cell->height < height )
6496                                 {
6497                                         current_cell->height = height;
6498                                 }
6499                                 if(current_cell->width < width)
6500                                 {
6501                                         total_width += (width-current_cell->width);
6502                                         current_cell->width = width;
6503
6504
6505
6506                                 }
6507                                 current_desc = g_list_next(current_desc);
6508                         }
6509                         column_count++;
6510
6511                 }/* column loop */
6512
6513         } /*row loop */
6514
6515
6516         /**
6517          * If the width of all columns is less than the width off
6518          * the table expand each cell proportionally.
6519          *
6520          */
6521         if(total_width+(this->spacing*column_count) < w->w ) {
6522                 for(current_desc=column_desc; current_desc; current_desc=g_list_next(current_desc)) {
6523                         current_cell = (struct table_column_desc*) current_desc->data;
6524                         current_cell->width= ( (current_cell->width+this->spacing)/(float)total_width) * w->w ;
6525                 }
6526         }
6527
6528         return column_desc;
6529 }
6530
6531
6532 /**
6533  * @brief Computes the height and width for the table.
6534  *
6535  * The height and widht are computed to display all cells in the table
6536  * at the requested height/width.
6537  *
6538  * @param this The graphics context
6539  * @param w The widget to pack.
6540  *
6541  */
6542 void gui_internal_table_pack(struct gui_priv * this, struct widget * w)
6543 {
6544
6545         int height=0;
6546         int width=0;
6547         int count=0;
6548         GList * column_data = gui_internal_compute_table_dimensions(this,w);
6549         GList * current=0;
6550         struct table_column_desc * cell_desc=0;
6551         struct table_data * table_data = (struct table_data*)w->data;
6552
6553         for(current = column_data; current; current=g_list_next(current))
6554         {
6555                 if(table_data->button_box == current->data )
6556                 {
6557                         continue;
6558                 }
6559                 cell_desc = (struct table_column_desc *) current->data;
6560                 width = width + cell_desc->width + this->spacing;
6561                 if(height < cell_desc->height)
6562                 {
6563                         height = cell_desc->height ;
6564                 }
6565         }
6566
6567
6568
6569         for(current=w->children; current; current=g_list_next(current))
6570         {
6571                 if(current->data!= table_data->button_box)
6572                 {
6573                         count++;
6574                 }
6575         }
6576
6577         w->w = width;
6578         if(w->w + w->c.x > this->root.w)
6579         {
6580                 w->w = this->root.w - w->c.x;
6581         }
6582
6583
6584         if(w->h + w->c.y   > this->root.h   )
6585         {
6586                 /**
6587                  * Do not allow the widget to exceed the screen.
6588                  *
6589                  */
6590                 w->h = this->root.h- w->c.y  - height;
6591         }
6592
6593         if (table_data->button_box) 
6594         {
6595                 gui_internal_widget_pack(this,table_data->button_box);
6596         }
6597
6598
6599         /**
6600          * Deallocate column descriptions.
6601          */
6602         current = column_data;
6603         while( (current = g_list_last(current)) )
6604         {
6605                 current = g_list_remove(current,current->data);
6606         }
6607
6608 }
6609
6610 /**
6611  * @brief Invalidates coordinates for previosly rendered table widget rows.
6612  *
6613  * @param table_data Data from the table object.
6614  */
6615 void gui_internal_table_hide_rows(struct table_data * table_data)
6616 {
6617         GList*cur_row;
6618         for(cur_row=table_data->top_row; cur_row ; cur_row = g_list_next(cur_row))
6619         {
6620                 struct widget * cur_row_widget = (struct widget*)cur_row->data;
6621                 if (cur_row_widget->type!=widget_table_row)
6622                         continue;
6623                 cur_row_widget->p.x=0;
6624                 cur_row_widget->p.y=0;
6625                 cur_row_widget->w=0;
6626                 cur_row_widget->h=0;
6627                 if(cur_row==table_data->bottom_row)
6628                         break;
6629         }
6630 }
6631
6632 /**
6633  * @brief Renders a table widget.
6634  *
6635  * @param this The graphics context
6636  * @param w The table widget to render.
6637  */
6638 void gui_internal_table_render(struct gui_priv * this, struct widget * w)
6639 {
6640
6641         int x;
6642         int y;
6643         GList * column_desc=NULL;
6644         GList * cur_row = NULL;
6645         GList * current_desc=NULL;
6646         struct table_data * table_data = (struct table_data*)w->data;
6647         int is_skipped=0;
6648         int is_first_page=1;
6649         struct table_column_desc * dim=NULL;
6650
6651         dbg_assert(table_data);
6652         column_desc = gui_internal_compute_table_dimensions(this,w);
6653         if(!column_desc)
6654                 return;
6655         y=w->p.y;
6656         gui_internal_table_hide_rows(table_data);
6657         /**
6658          * Skip rows that are on previous pages.
6659          */
6660         cur_row = w->children;
6661         if(table_data->top_row && table_data->top_row != w->children && !table_data->button_box_hide)
6662         {
6663                 cur_row = table_data->top_row;
6664                 is_first_page=0;
6665         } else {
6666                 table_data->top_row=NULL;
6667         }
6668         /**
6669          * Loop through each row.  Drawing each cell with the proper sizes,
6670          * at the proper positions.
6671          */
6672         for(table_data->top_row=cur_row; cur_row; cur_row = g_list_next(cur_row))
6673         {
6674                 int max_height=0, bbox_height=0;
6675                 struct widget * cur_row_widget;
6676                 GList * cur_column=NULL;
6677                 current_desc = column_desc;
6678                 cur_row_widget = (struct widget*)cur_row->data;
6679                 x =w->p.x+this->spacing;
6680                 if(cur_row_widget == table_data->button_box )
6681                 {
6682                         continue;
6683                 }
6684                 dim = (struct table_column_desc*)current_desc->data;
6685
6686                 if (table_data->button_box && !table_data->button_box_hide)
6687                         bbox_height=table_data->button_box->h;
6688
6689                 if( y + dim->height + bbox_height + this->spacing >= w->p.y + w->h )
6690                 {
6691                         /*
6692                          * No more drawing space left.
6693                          */
6694                         is_skipped=1;
6695                         break;
6696                 }
6697                 for(cur_column = cur_row_widget->children; cur_column;
6698                     cur_column=g_list_next(cur_column))
6699                 {
6700                         struct  widget * cur_widget = (struct widget*) cur_column->data;
6701                         dim = (struct table_column_desc*)current_desc->data;
6702
6703                         cur_widget->p.x=x;
6704                         cur_widget->w=dim->width;
6705                         cur_widget->p.y=y;
6706                         cur_widget->h=dim->height;
6707                         x=x+cur_widget->w;
6708                         max_height = dim->height;
6709                         /* We pack the widget before rendering to ensure that the x and y
6710                          * coordinates get pushed down.
6711                          */
6712                         gui_internal_widget_pack(this,cur_widget);
6713                         gui_internal_widget_render(this,cur_widget);
6714
6715                         if(dim->height > max_height)
6716                         {
6717                                 max_height = dim->height;
6718                         }
6719                 }
6720                 
6721                 /* Row object should have its coordinates in actual
6722                  * state to be able to pass mouse clicks to Column objects
6723                  */
6724                 cur_row_widget->p.x=w->p.x;
6725                 cur_row_widget->w=w->w;
6726                 cur_row_widget->p.y=y;
6727                 cur_row_widget->h=max_height;
6728                 y = y + max_height;
6729                 table_data->bottom_row=cur_row;
6730                 current_desc = g_list_next(current_desc);
6731         }
6732         if(table_data->button_box && (is_skipped || !is_first_page) && !table_data->button_box_hide )
6733         {
6734                 table_data->button_box->p.y =w->p.y+w->h-table_data->button_box->h -
6735                         this->spacing;
6736                 if(table_data->button_box->p.y < y )
6737                 {
6738                         table_data->button_box->p.y=y;
6739                 }
6740                 table_data->button_box->p.x = w->p.x;
6741                 table_data->button_box->w = w->w;
6742                 //    table_data->button_box->h = w->h - y;
6743                 //    table_data->next_button->h=table_data->button_box->h;
6744                 //    table_data->prev_button->h=table_data->button_box->h;
6745                 //    table_data->next_button->c.y=table_data->button_box->c.y;
6746                 //    table_data->prev_button->c.y=table_data->button_box->c.y;
6747                 gui_internal_widget_pack(this,table_data->button_box);
6748                 if(table_data->next_button->p.y > w->p.y + w->h + table_data->next_button->h)
6749                 {
6750                         table_data->button_box->p.y = w->p.y + w->h -
6751                                 table_data->button_box->h;
6752                 }
6753                 if(is_skipped)
6754                 {
6755                         table_data->next_button->state|= STATE_SENSITIVE;
6756                 }
6757                 else
6758                 {
6759                         table_data->next_button->state&= ~STATE_SENSITIVE;
6760                 }
6761
6762                 if(table_data->top_row != w->children)
6763                 {
6764                         table_data->prev_button->state|= STATE_SENSITIVE;
6765                 }
6766                 else
6767                 {
6768                         table_data->prev_button->state&= ~STATE_SENSITIVE;
6769                 }
6770                 gui_internal_widget_render(this,table_data->button_box);
6771         }
6772
6773         /**
6774          * Deallocate column descriptions.
6775          */
6776         current_desc = column_desc;
6777         while( (current_desc = g_list_last(current_desc)) )
6778         {
6779                 current_desc = g_list_remove(current_desc,current_desc->data);
6780         }
6781 }
6782
6783
6784 /**
6785  * @brief Displays Route information
6786  *
6787  * @li The name of the active vehicle
6788  * @param wm The button that was pressed.
6789  * @param v Unused
6790  */
6791 static void
6792 gui_internal_cmd2_route_description(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
6793 {
6794
6795
6796         struct widget * menu;
6797         struct widget * row;
6798         struct widget * box;
6799
6800
6801         if(! this->vehicle_cb)
6802         {
6803           /**
6804            * Register the callback on vehicle updates.
6805            */
6806           this->vehicle_cb = callback_new_attr_1(callback_cast(gui_internal_route_update),
6807                                                        attr_position_coord_geo,this);
6808           navit_add_callback(this->nav,this->vehicle_cb);
6809         }
6810
6811         this->route_data.route_table = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
6812
6813         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
6814
6815
6816         menu=gui_internal_menu(this,_("Route Description"));
6817
6818         menu->free=gui_internal_route_screen_free;
6819         this->route_data.route_showing=1;
6820         this->route_data.route_table->spx = this->spacing;
6821
6822
6823         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
6824
6825         //      gui_internal_widget_append(box,gui_internal_box_new_with_label(this,"Test"));
6826         gui_internal_widget_append(box,this->route_data.route_table);
6827         box->w=menu->w;
6828         box->spx = this->spacing;
6829         this->route_data.route_table->w=box->w;
6830         gui_internal_widget_append(menu,box);
6831         gui_internal_populate_route_table(this,this->nav);
6832         gui_internal_menu_render(this);
6833
6834 }
6835
6836 static int
6837 line_intersection(struct coord* a1, struct coord *a2, struct coord * b1, struct coord *b2, struct coord *res)
6838 {
6839         int n, a, b;
6840         int adx=a2->x-a1->x;
6841         int ady=a2->y-a1->y;
6842         int bdx=b2->x-b1->x;
6843         int bdy=b2->y-b1->y;
6844         n = bdy * adx - bdx * ady;
6845         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
6846         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
6847         if (n < 0) {
6848                 n = -n;
6849                 a = -a;
6850                 b = -b;
6851         }
6852         if (a < 0 || b < 0)
6853                 return 0;
6854         if (a > n || b > n)
6855                 return 0;
6856         if (n == 0) {
6857                 dbg(0,"a=%d b=%d n=%d\n", a, b, n);
6858                 dbg(0,"a1=0x%x,0x%x ad %d,%d\n", a1->x, a1->y, adx, ady);
6859                 dbg(0,"b1=0x%x,0x%x bd %d,%d\n", b1->x, b1->y, bdx, bdy);
6860                 dbg_assert(n != 0);
6861         }
6862         res->x = a1->x + a * adx / n;
6863         res->y = a1->y + a * ady / n;
6864         return 1;
6865 }
6866
6867 struct heightline {
6868         struct heightline *next;
6869         int height;
6870         struct coord_rect bbox;
6871         int count;
6872         struct coord c[0];
6873 };
6874
6875 struct diagram_point {
6876         struct diagram_point *next;
6877         struct coord c;
6878 };
6879
6880 static struct heightline *
6881 item_get_heightline(struct item *item)
6882 {
6883         struct heightline *ret=NULL;
6884         struct street_data *sd;
6885         struct attr attr;
6886         int i,height;
6887
6888         if (item_attr_get(item, attr_label, &attr)) {
6889                 height=atoi(attr.u.str);
6890                 sd=street_get_data(item);
6891                 if (sd && sd->count > 1) {
6892                         ret=g_malloc(sizeof(struct heightline)+sd->count*sizeof(struct coord));
6893                         ret->bbox.lu=sd->c[0];
6894                         ret->bbox.rl=sd->c[0];
6895                         ret->count=sd->count;
6896                         ret->height=height;
6897                         for (i = 0 ; i < sd->count ; i++) {
6898                                 ret->c[i]=sd->c[i];
6899                                 coord_rect_extend(&ret->bbox, sd->c+i);
6900                         }
6901                 }
6902                 street_data_free(sd);
6903         }
6904         return ret;
6905 }
6906
6907
6908 /**
6909  * @brief Displays Route Height Profile
6910  *
6911  * @li The name of the active vehicle
6912  * @param wm The button that was pressed.
6913  * @param v Unused
6914  */
6915 static void
6916 gui_internal_cmd2_route_height_profile(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
6917 {
6918
6919
6920         struct widget * menu, *box;
6921
6922         struct map * map=NULL;
6923         struct map_rect * mr=NULL;
6924         struct route * route;
6925         struct item * item =NULL;
6926         struct mapset *ms;
6927         struct mapset_handle *msh;
6928         int x,i,first=1,dist=0;
6929         struct coord c,last,res;
6930         struct coord_rect rbbox,dbbox;
6931         struct map_selection sel;
6932         struct heightline *heightline,*heightlines=NULL;
6933         struct diagram_point *min,*diagram_point,*diagram_points=NULL;
6934         sel.next=NULL;
6935         sel.order=18;
6936         sel.range.min=type_height_line_1;
6937         sel.range.max=type_height_line_3;
6938
6939
6940         menu=gui_internal_menu(this,_("Height Profile"));
6941         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
6942         gui_internal_widget_append(menu, box);
6943         route = navit_get_route(this->nav);
6944         if (route)
6945                 map = route_get_map(route);
6946         if(map)
6947                 mr = map_rect_new(map,NULL);
6948         if(mr) {
6949                 while((item = map_rect_get_item(mr))) {
6950                         while (item_coord_get(item, &c, 1)) {
6951                                 if (first) {
6952                                         first=0;
6953                                         sel.u.c_rect.lu=c;
6954                                         sel.u.c_rect.rl=c;
6955                                 } else
6956                                         coord_rect_extend(&sel.u.c_rect, &c);
6957                         }
6958                 }
6959                 map_rect_destroy(mr);
6960                 ms=navit_get_mapset(this->nav);
6961                 if (!first && ms) {
6962                         msh=mapset_open(ms);
6963                         while ((map=mapset_next(msh, 1))) {
6964                                 mr=map_rect_new(map, &sel);
6965                                 if (mr) {
6966                                         while((item = map_rect_get_item(mr))) {
6967                                                 if (item->type >= sel.range.min && item->type <= sel.range.max) {
6968                                                         heightline=item_get_heightline(item);
6969                                                         if (heightline) {
6970                                                                 heightline->next=heightlines;
6971                                                                 heightlines=heightline;
6972                                                         }
6973                                                 }
6974                                         }
6975                                         map_rect_destroy(mr);
6976                                 }
6977                         }
6978                         mapset_close(msh);
6979                 }
6980         }
6981         map=NULL;
6982         mr=NULL;
6983         if (route)
6984                 map = route_get_map(route);
6985         if(map)
6986                 mr = map_rect_new(map,NULL);
6987         if(mr && heightlines) {
6988                 while((item = map_rect_get_item(mr))) {
6989                         first=1;
6990                         while (item_coord_get(item, &c, 1)) {
6991                                 if (first)
6992                                         first=0;
6993                                 else {
6994                                         heightline=heightlines;
6995                                         rbbox.lu=last;
6996                                         rbbox.rl=last;
6997                                         coord_rect_extend(&rbbox, &c);
6998                                         while (heightline) {
6999                                                 if (coord_rect_overlap(&rbbox, &heightline->bbox)) {
7000                                                         for (i = 0 ; i < heightline->count - 1; i++) {
7001                                                                 if (heightline->c[i].x != heightline->c[i+1].x || heightline->c[i].y != heightline->c[i+1].y) {
7002                                                                         if (line_intersection(heightline->c+i, heightline->c+i+1, &last, &c, &res)) {
7003                                                                                 diagram_point=g_new(struct diagram_point, 1);
7004                                                                                 diagram_point->c.x=dist+transform_distance(projection_mg, &last, &res);
7005                                                                                 diagram_point->c.y=heightline->height;
7006                                                                                 diagram_point->next=diagram_points;
7007                                                                                 diagram_points=diagram_point;
7008                                                                                 dbg(0,"%d %d\n", diagram_point->c.x, diagram_point->c.y);
7009                                                                         }
7010                                                                 }
7011                                                         }
7012                                                 }
7013                                                 heightline=heightline->next;
7014                                         }
7015                                         dist+=transform_distance(projection_mg, &last, &c);
7016                                 }
7017                                 last=c;
7018                         }
7019
7020                 }
7021                 map_rect_destroy(mr);
7022         }
7023
7024
7025         gui_internal_menu_render(this);
7026         first=1;
7027         diagram_point=diagram_points;
7028         while (diagram_point) {
7029                 if (first) {
7030                         dbbox.lu=diagram_point->c;
7031                         dbbox.rl=diagram_point->c;
7032                         first=0;
7033                 } else
7034                         coord_rect_extend(&dbbox, &diagram_point->c);
7035                 diagram_point=diagram_point->next;
7036         }
7037         dbg(0,"%d %d %d %d\n", dbbox.lu.x, dbbox.lu.y, dbbox.rl.x, dbbox.rl.y);
7038         if (dbbox.rl.x > dbbox.lu.x && dbbox.lu.x*100/(dbbox.rl.x-dbbox.lu.x) <= 25)
7039                 dbbox.lu.x=0;
7040         if (dbbox.lu.y > dbbox.rl.y && dbbox.rl.y*100/(dbbox.lu.y-dbbox.rl.y) <= 25)
7041                 dbbox.rl.y=0;
7042         dbg(0,"%d,%d %dx%d\n", box->p.x, box->p.y, box->w, box->h);
7043         x=dbbox.lu.x;
7044         first=1;
7045         for (;;) {
7046                 struct point p[2];
7047                 min=NULL;
7048                 diagram_point=diagram_points;
7049                 while (diagram_point) {
7050                         if (diagram_point->c.x >= x && (!min || min->c.x > diagram_point->c.x))
7051                                 min=diagram_point;
7052                         diagram_point=diagram_point->next;
7053                 }
7054                 if (! min)
7055                         break;
7056                 p[1].x=(min->c.x-dbbox.lu.x)*(box->w-10)/(dbbox.rl.x-dbbox.lu.x)+box->p.x+5;
7057                 p[1].y=(min->c.y-dbbox.rl.y)*(box->h-10)/(dbbox.lu.y-dbbox.rl.y)+box->p.y+5;
7058                 dbg(0,"%d,%d=%d,%d\n",min->c.x, min->c.y, p[1].x,p[1].y);
7059                 graphics_draw_circle(this->gra, this->foreground, &p[1], 2);
7060                 if (first)
7061                         first=0;
7062                 else
7063                         graphics_draw_lines(this->gra, this->foreground, p, 2);
7064                 p[0]=p[1];
7065                 x=min->c.x+1;
7066         }
7067
7068
7069 }
7070
7071 static void
7072 gui_internal_cmd2_locale(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7073 {
7074         struct widget *menu,*wb,*w;
7075         char *text;
7076
7077         graphics_draw_mode(this->gra, draw_mode_begin);
7078         menu=gui_internal_menu(this, _("Show Locale"));
7079         menu->spx=this->spacing*10;
7080         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
7081         gui_internal_widget_append(menu, wb);
7082         text=g_strdup_printf("LANG=%1$s (1=%3$s 2=%2$s)",getenv("LANG"),"2","1");
7083         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7084         w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7085         g_free(text);
7086 #ifdef HAVE_API_WIN32_BASE
7087         {
7088                 char country[32],lang[32];
7089 #ifdef HAVE_API_WIN32_CE
7090                 wchar_t wcountry[32],wlang[32];
7091
7092                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
7093                 WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
7094 #else
7095                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, lang, sizeof(lang));
7096 #endif
7097                 text=g_strdup_printf("LOCALE_SABBREVLANGNAME=%s",lang);
7098                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7099                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7100                 g_free(text);
7101 #ifdef HAVE_API_WIN32_CE
7102                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
7103                 WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
7104 #else
7105                 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, country, sizeof(country));
7106 #endif
7107                 text=g_strdup_printf("LOCALE_SABBREVCTRYNAME=%s",country);
7108                 gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7109                 w->flags=gravity_left_center|orientation_horizontal|flags_fill;
7110                 g_free(text);
7111         }
7112 #endif
7113
7114         gui_internal_menu_render(this);
7115         graphics_draw_mode(this->gra, draw_mode_end);
7116 }
7117
7118 static void
7119 gui_internal_cmd2_about(struct gui_priv *this, char *function, struct attr **in, struct attr ***out, int *valid)
7120 {
7121         struct widget *menu,*wb,*w;
7122         char *text;
7123
7124         graphics_draw_mode(this->gra, draw_mode_begin);
7125         menu=gui_internal_menu(this, _("About Navit"));
7126         menu->spx=this->spacing*10;
7127         wb=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand);
7128         gui_internal_widget_append(menu, wb);
7129
7130         //Icon
7131         gui_internal_widget_append(wb, w=gui_internal_image_new(this, image_new_xs(this, "navit")));
7132         w->flags=gravity_top_center|orientation_horizontal|flags_fill;
7133
7134         //app name
7135         text=g_strdup_printf("%s",PACKAGE_NAME);
7136         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7137         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7138         g_free(text);
7139
7140         //Version
7141         text=g_strdup_printf("%s",version);
7142         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7143         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7144         g_free(text);
7145
7146         //Site
7147         text=g_strdup_printf("http://www.navit-project.org/");
7148         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7149         w->flags=gravity_top_center|orientation_horizontal|flags_expand;
7150         g_free(text);
7151
7152         //Authors
7153         text=g_strdup_printf("%s:",_("By"));
7154         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7155         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7156         g_free(text);
7157         text=g_strdup_printf("Martin Schaller");
7158         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7159         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7160         g_free(text);
7161         text=g_strdup_printf("Michael Farmbauer");
7162         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7163         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7164         g_free(text);
7165         text=g_strdup_printf("Alexander Atanasov");
7166         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7167         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7168         g_free(text);
7169         text=g_strdup_printf("Pierre Grandin");
7170         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7171         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7172         g_free(text);
7173
7174         //Contributors
7175         text=g_strdup_printf("%s",_("And all the Navit Team"));
7176         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7177         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7178         g_free(text);
7179         text=g_strdup_printf("%s",_("members and contributors."));
7180         gui_internal_widget_append(wb, w=gui_internal_label_new(this, text));
7181         w->flags=gravity_bottom_center|orientation_horizontal|flags_fill;
7182         g_free(text);
7183
7184         gui_internal_menu_render(this);
7185         graphics_draw_mode(this->gra, draw_mode_end);
7186 }
7187
7188 /**
7189  * @brief handles the 'next page' table event.
7190  * A callback function that is invoked when the 'next page' button is pressed
7191  * to advance the contents of a table widget.
7192  *
7193  * @param this The graphics context.
7194  * @param wm The button widget that was pressed.
7195  */
7196 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data)
7197 {
7198         struct widget * table_widget = (struct widget * ) wm->data;
7199         struct table_data * table_data = NULL;
7200         int found=0;
7201         GList * iterator;
7202
7203         if(table_widget)
7204         {
7205                 table_data = (struct table_data*) table_widget->data;
7206
7207         }
7208         if(table_data)
7209         {
7210                 /**
7211                  * Before advancing to the next page we need to ensure
7212                  * that the current top_row is in the list of previous top_rows
7213                  * so previous page can work.
7214                  *
7215                  */
7216                 for(iterator=table_data->page_headers; iterator != NULL;
7217                     iterator = g_list_next(iterator) )
7218                 {
7219                         if(iterator->data == table_data->top_row)
7220                         {
7221                                 found=1;
7222                                 break;
7223                         }
7224
7225                 }
7226                 if( ! found)
7227                 {
7228                         table_data->page_headers=g_list_append(table_data->page_headers,
7229                                                                table_data->top_row);
7230                 }
7231                 /**
7232                  * Invalidate row coordinates for all rows which were previously rendered
7233                  
7234                 for(iterator=table_data->top_row; iterator && table_data->rows_on_page; iterator = g_list_next(iterator))
7235                 {
7236                         struct widget * cur_row_widget = (struct widget*)iterator->data;
7237                         cur_row_widget->p.x=-1;
7238                         cur_row_widget->p.y=-1;
7239                         cur_row_widget->w=0;
7240                         cur_row_widget->h=0;
7241                         table_data->rows_on_page--;
7242                 }
7243                 */
7244                 gui_internal_table_hide_rows(table_data);
7245                 table_data->top_row=g_list_next(table_data->bottom_row);
7246         }
7247         wm->state&= ~STATE_HIGHLIGHTED;
7248         gui_internal_menu_render(this);
7249 }
7250
7251
7252
7253 /**
7254  * @brief handles the 'previous page' table event.
7255  * A callback function that is invoked when the 'previous page' button is pressed
7256  * to go back in the contents of a table widget.
7257  *
7258  * @param this The graphics context.
7259  * @param wm The button widget that was pressed.
7260  */
7261 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data)
7262 {
7263         struct widget * table_widget = (struct widget * ) wm->data;
7264         struct table_data * table_data = NULL;
7265         GList * current_page_top=NULL;
7266
7267         GList * iterator;
7268         if(table_widget)
7269         {
7270                 table_data = (struct table_data*) table_widget->data;
7271                 if(table_data)
7272                 {
7273                         gui_internal_table_hide_rows(table_data);
7274                         current_page_top = table_data->top_row;
7275                         for(iterator = table_data->page_headers; iterator != NULL;
7276                             iterator = g_list_next(iterator))
7277                         {
7278                                 if(current_page_top == iterator->data)
7279                                 {
7280                                         break;
7281                                 }
7282                                 table_data->top_row = (GList*) iterator->data;
7283                         }
7284                 }
7285         }
7286         wm->state&= ~STATE_HIGHLIGHTED;
7287         gui_internal_menu_render(this);
7288 }
7289
7290
7291 /**
7292  * @brief deallocates a table_data structure.
7293  *
7294  */
7295 void gui_internal_table_data_free(void * p)
7296 {
7297
7298
7299         /**
7300          * @note button_box and its children (next_button,prev_button)
7301          * have their memory managed by the table itself.
7302          */
7303         struct table_data * table_data =  (struct table_data*) p;
7304         g_list_free(table_data->page_headers);
7305         g_free(p);
7306
7307
7308 }
7309
7310
7311 /**
7312  * @brief Called when the route is updated.
7313  */
7314 void gui_internal_route_update(struct gui_priv * this, struct navit * navit, struct vehicle *v)
7315 {
7316
7317         if(this->route_data.route_showing) {
7318                 gui_internal_populate_route_table(this,navit);
7319                 graphics_draw_mode(this->gra, draw_mode_begin);
7320                 gui_internal_menu_render(this);
7321                 graphics_draw_mode(this->gra, draw_mode_end);
7322         }
7323
7324
7325 }
7326
7327
7328 /**
7329  * @brief Called when the route screen is closed (deallocated).
7330  *
7331  * The main purpose of this function is to remove the widgets from
7332  * references route_data because those widgets are about to be freed.
7333  */
7334 void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w)
7335 {
7336         if(this_) {
7337                 this_->route_data.route_showing=0;
7338                 this_->route_data.route_table=NULL;
7339                 g_free(w);
7340         }
7341
7342 }
7343
7344 /**
7345  * @brief Populates the route  table with route information
7346  *
7347  * @param this The gui context
7348  * @param navit The navit object
7349  */
7350 void gui_internal_populate_route_table(struct gui_priv * this,
7351                                        struct navit * navit)
7352 {
7353         struct map * map=NULL;
7354         struct map_rect * mr=NULL;
7355         struct navigation * nav = NULL;
7356         struct item * item =NULL;
7357         struct attr attr;
7358         struct widget * label = NULL;
7359         struct widget * row = NULL;
7360         struct coord c;
7361         nav = navit_get_navigation(navit);
7362         if(!nav) {
7363                 return;
7364         }
7365         map = navigation_get_map(nav);
7366         if(map)
7367           mr = map_rect_new(map,NULL);
7368         if(mr) {
7369                 gui_internal_widget_table_clear(this,this->route_data.route_table);
7370                 while((item = map_rect_get_item(mr))) {
7371                         if(item_attr_get(item,attr_navigation_long,&attr)) {
7372                           row = gui_internal_widget_table_row_new(this,
7373                                                                   gravity_left
7374                                                                   | flags_fill
7375                                                                   | orientation_horizontal);
7376                           gui_internal_widget_append(this->route_data.route_table,row);
7377
7378                           label = gui_internal_label_new(this,attr.u.str);
7379                           gui_internal_widget_append(row,label);
7380
7381                           label->item=*item;
7382                           item_coord_get(item, &c, 1);
7383                           label->c.x=c.x;
7384                           label->c.y=c.y;
7385                           label->c.pro=map_projection(map);
7386                           label->func=gui_internal_cmd_position;
7387                           label->state|=STATE_SENSITIVE;
7388                           label->data=(void*)2;   
7389                         }
7390
7391                 }
7392
7393         }
7394 }
7395
7396 static struct command_table commands[] = {
7397         {"abort_navigation",command_cast(gui_internal_cmd2_abort_navigation)},
7398         {"back",command_cast(gui_internal_cmd2_back)},
7399         {"back_to_map",command_cast(gui_internal_cmd2_back_to_map)},
7400         {"bookmarks",command_cast(gui_internal_cmd2_bookmarks)},
7401         {"formerdests",command_cast(gui_internal_cmd_formerdests)},
7402         {"get_data",command_cast(gui_internal_get_data)},
7403         {"locale",command_cast(gui_internal_cmd2_locale)},
7404         {"log",command_cast(gui_internal_cmd_log)},
7405         {"menu",command_cast(gui_internal_cmd_menu2)},
7406         {"position",command_cast(gui_internal_cmd2_position)},
7407         {"pois",command_cast(gui_internal_cmd2_pois)},
7408         {"route_description",command_cast(gui_internal_cmd2_route_description)},
7409         {"route_height_profile",command_cast(gui_internal_cmd2_route_height_profile)},
7410         {"set",command_cast(gui_internal_cmd2_set)},
7411         {"setting_layout",command_cast(gui_internal_cmd2_setting_layout)},
7412         {"setting_maps",command_cast(gui_internal_cmd2_setting_maps)},
7413         {"setting_rules",command_cast(gui_internal_cmd2_setting_rules)},
7414         {"setting_vehicle",command_cast(gui_internal_cmd2_setting_vehicle)},
7415         {"town",command_cast(gui_internal_cmd2_town)},
7416         {"quit",command_cast(gui_internal_cmd2_quit)},
7417         {"write",command_cast(gui_internal_cmd_write)},
7418         {"about",command_cast(gui_internal_cmd2_about)},
7419
7420 };
7421
7422
7423 //##############################################################################################################
7424 //# Description:
7425 //# Comment:
7426 //# Authors: Martin Schaller (04/2008)
7427 //##############################################################################################################
7428 static struct gui_priv * gui_internal_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs, struct gui *gui)
7429 {
7430         struct color color_white={0xffff,0xffff,0xffff,0xffff};
7431         struct color color_black={0x0,0x0,0x0,0xffff};
7432         struct color back2_color={0x4141,0x4141,0x4141,0xffff};
7433
7434         struct gui_priv *this;
7435         struct attr *attr;
7436         *meth=gui_internal_methods;
7437         this=g_new0(struct gui_priv, 1);
7438         this->nav=nav;
7439
7440         this->self.type=attr_gui;
7441         this->self.u.gui=gui;
7442
7443         if ((attr=attr_search(attrs, NULL, attr_menu_on_map_click)))
7444                 this->menu_on_map_click=attr->u.num;
7445         else
7446                 this->menu_on_map_click=1;
7447         if ((attr=attr_search(attrs, NULL, attr_signal_on_map_click)))
7448                 this->signal_on_map_click=attr->u.num;
7449
7450         if ((attr=attr_search(attrs, NULL, attr_callback_list))) {
7451                 command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this);
7452         }
7453
7454         if( (attr=attr_search(attrs,NULL,attr_font_size)))
7455         {
7456           this->config.font_size=attr->u.num;
7457         }
7458         else
7459         {
7460           this->config.font_size=-1;
7461         }
7462         if( (attr=attr_search(attrs,NULL,attr_icon_xs)))
7463         {
7464           this->config.icon_xs=attr->u.num;
7465         }
7466         else
7467         {
7468           this->config.icon_xs=-1;
7469         }
7470         if( (attr=attr_search(attrs,NULL,attr_icon_l)))
7471         {
7472           this->config.icon_l=attr->u.num;
7473         }
7474         else
7475         {
7476           this->config.icon_l=-1;
7477         }
7478         if( (attr=attr_search(attrs,NULL,attr_icon_s)))
7479         {
7480           this->config.icon_s=attr->u.num;
7481         }
7482         else
7483         {
7484           this->config.icon_s=-1;
7485         }
7486         if( (attr=attr_search(attrs,NULL,attr_spacing)))
7487         {
7488           this->config.spacing=attr->u.num;
7489         }
7490         else
7491         {
7492           this->config.spacing=-1;
7493         }
7494         if( (attr=attr_search(attrs,NULL,attr_gui_speech)))
7495         {
7496           this->speech=attr->u.num;
7497         }
7498         if( (attr=attr_search(attrs,NULL,attr_keyboard)))
7499           this->keyboard=attr->u.num;
7500         else
7501           this->keyboard=1;
7502
7503     if( (attr=attr_search(attrs,NULL,attr_fullscreen)))
7504       this->fullscreen=attr->u.num;
7505
7506         if( (attr=attr_search(attrs,NULL,attr_flags)))
7507               this->flags=attr->u.num;
7508         if( (attr=attr_search(attrs,NULL,attr_background_color)))
7509               this->background_color=*attr->u.color;
7510         else
7511               this->background_color=color_black;
7512         if( (attr=attr_search(attrs,NULL,attr_background_color2)))
7513                 this->background2_color=*attr->u.color;
7514         else
7515                 this->background2_color=back2_color;
7516         if( (attr=attr_search(attrs,NULL,attr_text_color)))
7517               this->text_foreground_color=*attr->u.color;
7518         else
7519               this->text_foreground_color=color_white;
7520         this->text_background_color=color_black;
7521         if( (attr=attr_search(attrs,NULL,attr_columns)))
7522               this->cols=attr->u.num;
7523         if( (attr=attr_search(attrs,NULL,attr_osd_configuration)))
7524               this->osd_configuration=*attr;
7525
7526         if( (attr=attr_search(attrs,NULL,attr_pitch)))
7527               this->pitch=attr->u.num;
7528         else
7529                 this->pitch=20;
7530         if( (attr=attr_search(attrs,NULL,attr_flags_town)))
7531                 this->flags_town=attr->u.num;
7532         else
7533                 this->flags_town=-1;
7534         if( (attr=attr_search(attrs,NULL,attr_flags_street)))
7535                 this->flags_street=attr->u.num;
7536         else
7537                 this->flags_street=-1;
7538         if( (attr=attr_search(attrs,NULL,attr_flags_house_number)))
7539                 this->flags_house_number=attr->u.num;
7540         else
7541                 this->flags_house_number=-1;
7542         if( (attr=attr_search(attrs,NULL,attr_radius)))
7543                 this->radius=attr->u.num;
7544         else
7545                 this->radius=10;
7546         this->data.priv=this;
7547         this->data.gui=&gui_internal_methods_ext;
7548         this->data.widget=&gui_internal_widget_methods;
7549         this->cbl=callback_list_new();
7550
7551         return this;
7552 }
7553
7554 //##############################################################################################################
7555 //# Description:
7556 //# Comment:
7557 //# Authors: Martin Schaller (04/2008)
7558 //##############################################################################################################
7559 void plugin_init(void)
7560 {
7561         plugin_register_gui_type("internal", gui_internal_new);
7562 }
7563
7564 static void
7565 gui_internal_destroy(struct gui_priv *this)
7566 {
7567         g_free(this->country_iso2);
7568         g_free(this->href);
7569         g_free(this->html_text);
7570         g_free(this);
7571 }