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