Add:Core:Beginning of work on writing navit.xml
[navit-package] / navit / gui / internal / gui_internal.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 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 #include "item.h"
38 #include "file.h"
39 #include "navit.h"
40 #include "navit_nls.h"
41 #include "debug.h"
42 #include "gui.h"
43 #include "coord.h"
44 #include "point.h"
45 #include "plugin.h"
46 #include "graphics.h"
47 #include "transform.h"
48 #include "color.h"
49 #include "map.h"
50 #include "layout.h"
51 #include "callback.h"
52 #include "vehicle.h"
53 #include "vehicleprofile.h"
54 #include "window.h"
55 #include "main.h"
56 #include "keys.h"
57 #include "mapset.h"
58 #include "route.h"
59 #include "search.h"
60 #include "track.h"
61 #include "country.h"
62 #include "config.h"
63 #include "event.h"
64 #include "navit_nls.h"
65 #include "navigation.h"
66 #include "gui_internal.h"
67 #include "command.h"
68 #include "util.h"
69
70 struct menu_data {
71         struct widget *search_list;
72         struct widget *keyboard;
73         struct widget *button_bar;
74         int keyboard_mode;
75         void (*redisplay)(struct gui_priv *priv, struct widget *widget, void *data);
76         struct widget *redisplay_widget;
77 };
78
79 //##############################################################################################################
80 //# Description:
81 //# Comment:
82 //# Authors: Martin Schaller (04/2008)
83 //##############################################################################################################
84 struct widget {
85         enum widget_type type;
86         struct graphics_gc *background,*text_background;
87         struct graphics_gc *foreground_frame;
88         struct graphics_gc *foreground;
89         char *text;
90         struct graphics_image *img;
91          /**
92           * A function to be invoked on actions.
93           * @li widget The widget that is receiving the button press.
94           *
95           */
96         void (*func)(struct gui_priv *priv, struct widget *widget, void *data);
97         int reason;
98         int datai;
99         void *data;
100         /**
101          * @brief A function to deallocate data
102          */
103         void (*data_free)(void *data);
104
105         /**
106          * @brief a function that will be called as the widget is being destroyed.
107          * This function can act as a destructor for the widget. It allows for
108          * on deallocation actions to be specified on a per widget basis.
109          * This function will call g_free on the widget (if required).
110          */
111         void (*free) (struct gui_priv *this_, struct widget * w);
112         char *prefix;
113         char *name;
114         char *speech;
115         struct pcoord c;
116         struct item item;
117         int selection_id;
118         int state;
119         struct point p;
120         int wmin,hmin;
121         int w,h;
122         int textw,texth;
123         int bl,br,bt,bb,spx,spy;
124         int border;
125         /**
126          * The number of widgets to layout horizontally when doing
127          * a orientation_horizontal_vertical layout
128          */
129         int cols;
130         enum flags flags;
131         void *instance;
132         int (*set_attr)(void *, struct attr *);
133         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
134         void (*remove_cb)(void *, struct callback *cb);
135         struct callback *cb;
136         struct attr on;
137         struct attr off;
138         int deflt;
139         int is_on;
140         int redraw;
141         struct menu_data *menu_data;
142         GList *children;
143 };
144
145 /**
146  * @brief A structure to store configuration values.
147  *
148  * This structure stores configuration values for how gui elements in the internal GUI
149  * should be drawn.
150  */
151 struct gui_config_settings {
152
153   /**
154    * The base size (in fractions of a point) to use for text.
155    */
156   int font_size;
157   /**
158    * The size (in pixels) that xs style icons should be scaled to.
159    */
160   int icon_xs;
161   /**
162    * The size (in pixels) that s style icons (small) should be scaled to
163    */
164   int icon_s;
165   /**
166    * The size (in pixels) that l style icons should be scaled to
167    */
168   int icon_l;
169   /**
170    * The default amount of spacing (in pixels) to place between GUI elements.
171    */
172   int spacing;
173
174 };
175
176 /**
177  * Indexes into the config_profiles array.
178  */
179 const int LARGE_PROFILE=0;
180 const int MEDIUM_PROFILE=1;
181 const int SMALL_PROFILE=2;
182
183 /**
184  * The default config profiles.
185  *
186  * [0] =>  LARGE_PROFILE (screens 640 in one dimension)
187  * [1] =>  MEDIUM PROFILE (screens larger than 320 in one dimension
188  * [2] => Small profile (default)
189  */
190 static struct gui_config_settings config_profiles[]={
191       {545,32,48,96,10}
192     , {300,32,48,64,3}
193       ,{200,16,32,48,2}
194 };
195
196 struct route_data {
197   struct widget * route_table;
198   int route_showing;
199
200 };
201
202 //##############################################################################################################
203 //# Description:
204 //# Comment:
205 //# Authors: Martin Schaller (04/2008)
206 //##############################################################################################################
207 struct gui_priv {
208         struct navit *nav;
209         struct window *win;
210         struct graphics *gra;
211         struct graphics_gc *background;
212         struct graphics_gc *background2;
213         struct graphics_gc *highlight_background;
214         struct graphics_gc *foreground;
215         struct graphics_gc *text_foreground;
216         struct graphics_gc *text_background;
217         struct color background_color, background2_color, text_foreground_color, text_background_color;
218         int spacing;
219         int font_size;
220         int fullscreen;
221         struct graphics_font *font;
222         int icon_xs, icon_s, icon_l;
223         int pressed;
224         struct widget *widgets;
225         int widgets_count;
226         int redraw;
227         struct widget root;
228         struct widget *highlighted;
229         struct widget *highlighted_menu;
230         int clickp_valid, vehicle_valid;
231         struct pcoord clickp, vehiclep;
232         struct search_list *sl;
233         int ignore_button;
234         int menu_on_map_click;
235         char *country_iso2;
236         int speech;
237         int keyboard;
238         /**
239          * The setting information read from the configuration file.
240          * values of -1 indicate no value was specified in the config file.
241          */
242         struct gui_config_settings config;
243         struct event_idle *idle;
244         struct callback *motion_cb,*button_cb,*resize_cb,*keypress_cb,*idle_cb, *motion_timeout_callback;
245         struct event_timeout *motion_timeout_event;
246         struct point current;
247
248         struct callback * vehicle_cb;
249           /**
250            * Stores information about the route.
251            */
252         struct route_data route_data;
253
254         struct gui_internal_data data;
255         struct callback_list *cbl;
256         int flags;
257         int cols;
258         struct attr osd_configuration;
259         int pitch;
260 };
261
262
263 /**
264  * @brief A structure to store information about a table.
265  *
266  * The table_data widget stores pointers to extra information needed by the
267  * table widget.
268  *
269  * The table_data structure needs to be freed with data_free along with the widget.
270  *
271  */
272 struct table_data
273 {
274   /**
275    * A GList pointer into a widget->children list that indicates the row
276    * currently being rendered at the top of the table.
277    */
278   GList * top_row;
279   /**
280    * A Glist pointer into a widget->children list that indicates the row
281    * currently being rendered at the bottom of the table.
282    */
283   GList * bottom_row;
284
285   /**
286    * A list of table_row widgets that mark the
287    * top rows for each page of the table.
288    * This is needed for the 'previous page' function of the table.
289    */
290   GList * page_headers;
291
292   /**
293    * A container box that is the child of the table widget that contains+groups
294    * the next and previous button.
295    */
296   struct widget * button_box;
297
298   /**
299    * A button widget to handle 'next page' requests
300    */
301   struct widget * next_button;
302   /**
303    * A button widget to handle 'previous page' requests.
304    */
305   struct widget * prev_button;
306
307
308   /**
309    * a pointer to the gui context.
310    * This is needed by the free function to destory the buttons.
311    */
312   struct  gui_priv *  this;
313 };
314
315 /**
316  * A data structure that holds information about a column that makes up a table.
317  *
318  *
319  */
320 struct table_column_desc
321 {
322
323   /**
324    * The computed height of a cell in the table.
325    */
326   int height;
327
328   /**
329    * The computed width of a cell in the table.
330    */
331   int width;
332 };
333
334
335 static void gui_internal_widget_render(struct gui_priv *this, struct widget *w);
336 static void gui_internal_widget_pack(struct gui_priv *this, struct widget *w);
337 static struct widget * gui_internal_box_new(struct gui_priv *this, enum flags flags);
338 static void gui_internal_widget_append(struct widget *parent, struct widget *child);
339 static void gui_internal_widget_destroy(struct gui_priv *this, struct widget *w);
340 static void gui_internal_apply_config(struct gui_priv *this);
341
342 static struct widget* gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons);
343 static struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags);
344 static void gui_internal_table_render(struct gui_priv * this, struct widget * w);
345 static void gui_internal_cmd_route(struct gui_priv * this, struct widget * w,void *);
346 static void gui_internal_table_pack(struct gui_priv * this, struct widget * w);
347 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data);
348 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data);
349 static void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table);
350 static struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags);
351 static void gui_internal_table_data_free(void * d);
352 static void gui_internal_route_update(struct gui_priv * this, struct navit * navit,
353                                       struct vehicle * v);
354 static void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w);
355 static void gui_internal_populate_route_table(struct gui_priv * this,
356                                        struct navit * navit);
357 static void gui_internal_search_idle_end(struct gui_priv *this);
358 static void gui_internal_search(struct gui_priv *this, char *what, char *type, int flags);
359 static void gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data);
360 static void gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data);
361 static void gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data);
362 static void gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data);
363 static void gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data);
364 static void gui_internal_search_town_in_country(struct gui_priv *this, struct widget *wm);
365 static void gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data);
366 static void gui_internal_check_exit(struct gui_priv *this);
367
368 static struct widget *gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode);
369 static struct menu_data * gui_internal_menu_data(struct gui_priv *this);
370
371 static int gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle *vehicle);
372
373 /*
374  * * Display image scaled to specific size
375  * * searches for scaleable and pre-scaled image
376  * * @param this Our gui context
377  * * @param name image name
378  * * @param w desired width of image
379  * * @param h desired height of image
380  * * @returns image_struct Ptr to scaled image struct or NULL if not scaled or found
381  * */
382 static struct graphics_image *
383 image_new_scaled(struct gui_priv *this, char *name, int w, int h)
384 {
385         struct graphics_image *ret=NULL;
386         char *full_name=NULL;
387         char *full_path=NULL;
388         int i;
389
390         for (i = 1 ; i < 6 ; i++) {
391                 full_name=NULL;
392                 switch (i) {
393                 case 3:
394                         full_name=g_strdup_printf("%s.svg", name);
395                         break;
396                 case 2:
397                         full_name=g_strdup_printf("%s.svgz", name);
398                         break;
399                 case 1:
400                         if (w != -1 && h != -1) {
401                                 full_name=g_strdup_printf("%s_%d_%d.png", name, w, h);
402                         }
403                         break;
404                 case 4:
405                         full_name=g_strdup_printf("%s.png", name);
406                         break;
407                 case 5:
408                         full_name=g_strdup_printf("%s.xpm", name);
409                         break;
410                 }
411                 dbg(1,"trying '%s'\n", full_name);
412                 if (! full_name)
413                         continue;
414 #if 0
415                 /* needs to be checked in the driver */
416                 if (!file_exists(full_name)) {
417                         g_free(full_name);
418                         continue;
419                 }
420 #endif
421                 full_path=graphics_icon_path(full_name);
422                 ret=graphics_image_new_scaled(this->gra, full_path, w, h);
423                 dbg(1,"ret=%p\n", ret);
424                 g_free(full_path);
425                 g_free(full_name);
426                 if (ret)
427                         return ret;
428         }
429         dbg(0,"failed to load %s with %d,%d\n", name, w, h);
430         return NULL;
431 }
432
433 #if 0
434 static struct graphics_image *
435 image_new_o(struct gui_priv *this, char *name)
436 {
437         return image_new_scaled(this, name, -1, -1);
438 }
439 #endif
440
441 static struct graphics_image *
442 image_new_xs(struct gui_priv *this, char *name)
443 {
444         return image_new_scaled(this, name, this->icon_xs, this->icon_xs);
445 }
446
447
448 static struct graphics_image *
449 image_new_s(struct gui_priv *this, char *name)
450 {
451         return image_new_scaled(this, name, this->icon_s, this->icon_s);
452 }
453
454 static struct graphics_image *
455 image_new_l(struct gui_priv *this, char *name)
456 {
457         return image_new_scaled(this, name, this->icon_l, this->icon_l);
458 }
459
460 static char *
461 coordinates(struct pcoord *pc, char sep)
462 {
463         char latc='N',lngc='E';
464         int lat_deg,lat_min,lat_sec;
465         int lng_deg,lng_min,lng_sec;
466         struct coord_geo g;
467         struct coord c;
468         c.x=pc->x;
469         c.y=pc->y;
470         transform_to_geo(pc->pro, &c, &g);
471
472         if (g.lat < 0) {
473                 g.lat=-g.lat;
474                 latc='S';
475         }
476         if (g.lng < 0) {
477                 g.lng=-g.lng;
478                 lngc='W';
479         }
480         lat_deg=g.lat;
481         lat_min=fmod(g.lat*60,60);
482         lat_sec=fmod(g.lat*3600,60);
483         lng_deg=g.lng;
484         lng_min=fmod(g.lng*60,60);
485         lng_sec=fmod(g.lng*3600,60);
486         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);
487 }
488
489 static void
490 gui_internal_background_render(struct gui_priv *this, struct widget *w)
491 {
492         struct point pnt=w->p;
493         if (w->state & STATE_HIGHLIGHTED)
494                 graphics_draw_rectangle(this->gra, this->highlight_background, &pnt, w->w, w->h);
495         else {
496                 if (w->background)
497                         graphics_draw_rectangle(this->gra, w->background, &pnt, w->w, w->h);
498         }
499 }
500 static struct widget *
501 gui_internal_label_new(struct gui_priv *this, char *text)
502 {
503         struct point p[4];
504         int w=0;
505         int h=0;
506
507         struct widget *widget=g_new0(struct widget, 1);
508         widget->type=widget_label;
509         if (text) {
510                 widget->text=g_strdup(text);
511                 graphics_get_text_bbox(this->gra, this->font, text, 0x10000, 0x0, p, 0);
512                 w=p[2].x-p[0].x;
513                 h=p[0].y-p[2].y;
514         }
515         widget->h=h+this->spacing;
516         widget->texth=h;
517         widget->w=w+this->spacing;
518         widget->textw=w;
519         widget->flags=gravity_center;
520         widget->foreground=this->text_foreground;
521         widget->text_background=this->text_background;
522
523         return widget;
524 }
525
526 static struct widget *
527 gui_internal_label_new_abbrev(struct gui_priv *this, char *text, int maxwidth)
528 {
529         struct widget *ret=NULL;
530         char *tmp=g_malloc(strlen(text)+3);
531         int i;
532
533         i=strlen(text)-1;
534         while (i >= 0) {
535                 strcpy(tmp, text);
536                 strcpy(tmp+i,"..");
537                 ret=gui_internal_label_new(this, tmp);
538                 if (ret->w < maxwidth)
539                         break;
540                 gui_internal_widget_destroy(this, ret);
541                 ret=NULL;
542                 i--;
543         }
544         g_free(tmp);
545         return ret;
546 }
547
548 static struct widget *
549 gui_internal_image_new(struct gui_priv *this, struct graphics_image *image)
550 {
551         struct widget *widget=g_new0(struct widget, 1);
552         widget->type=widget_image;
553         widget->img=image;
554         if (image) {
555                 widget->w=image->width;
556                 widget->h=image->height;
557         }
558         return widget;
559 }
560
561 static void
562 gui_internal_image_render(struct gui_priv *this, struct widget *w)
563 {
564         struct point pnt;
565
566         gui_internal_background_render(this, w);
567         if (w->img) {
568                 pnt=w->p;
569                 pnt.x+=w->w/2-w->img->hot.x;
570                 pnt.y+=w->h/2-w->img->hot.y;
571                 graphics_draw_image(this->gra, this->foreground, &pnt, w->img);
572         }
573 }
574
575 static void
576 gui_internal_label_render(struct gui_priv *this, struct widget *w)
577 {
578         struct point pnt=w->p;
579         gui_internal_background_render(this, w);
580         if (w->text) {
581                 if (w->flags & gravity_right) {
582                         pnt.y+=w->h-this->spacing;
583                         pnt.x+=w->w-w->textw-this->spacing;
584                         graphics_draw_text(this->gra, w->foreground, w->text_background, this->font, w->text, &pnt, 0x10000, 0x0);
585                 } else {
586                         pnt.y+=w->h-this->spacing;
587                         graphics_draw_text(this->gra, w->foreground, w->text_background, this->font, w->text, &pnt, 0x10000, 0x0);
588                 }
589         }
590 }
591
592 /**
593  * @brief A text box is a widget that renders a text string containing newlines.
594  * The string will be broken up into label widgets at each newline with a vertical layout.
595  *
596  */
597 static struct widget *
598 gui_internal_text_new(struct gui_priv *this, char *text, enum flags flags)
599 {
600         char *s=g_strdup(text),*s2,*tok;
601         struct widget *ret=gui_internal_box_new(this, flags);
602         s2=s;
603         while ((tok=strtok(s2,"\n"))) {
604                 gui_internal_widget_append(ret, gui_internal_label_new(this, tok));
605                 s2=NULL;
606         }
607         gui_internal_widget_pack(this,ret);
608         return ret;
609 }
610
611
612 static struct widget *
613 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)
614 {
615         struct widget *ret=NULL;
616         ret=gui_internal_box_new(this, flags);
617         if (ret) {
618                 if (image)
619                         gui_internal_widget_append(ret, gui_internal_image_new(this, image));
620                 if (text)
621                         gui_internal_widget_append(ret, gui_internal_text_new(this, text, gravity_center|orientation_vertical));
622                 ret->func=func;
623                 ret->data=data;
624                 if (func) {
625                         ret->state |= STATE_SENSITIVE;
626                         ret->speech=g_strdup(text);
627                 }
628         }
629         return ret;
630
631 }
632
633 static int
634 gui_internal_button_attr_update(struct gui_priv *this, struct widget *w)
635 {
636         struct widget *wi;
637         int is_on=0;
638         struct attr curr;
639         GList *l;
640
641         if (w->get_attr(w->instance, w->on.type, &curr, NULL))
642                 is_on=curr.u.data == w->on.u.data;
643         else
644                 is_on=w->deflt;
645         if (is_on != w->is_on) {
646                 if (w->redraw)
647                         this->redraw=1;
648                 w->is_on=is_on;
649                 l=g_list_first(w->children);
650                 if (l) {
651                         wi=l->data;
652                         if (wi->img)
653                                 graphics_image_free(this->gra, wi->img);
654                         wi->img=image_new_xs(this, is_on ? "gui_active" : "gui_inactive");
655                 }
656                 if (w->is_on && w->off.type == attr_none)
657                         w->state &= ~STATE_SENSITIVE;
658                 else
659                         w->state |= STATE_SENSITIVE;
660                 return 1;
661         }
662         return 0;
663 }
664
665 static void
666 gui_internal_button_attr_callback(struct gui_priv *this, struct widget *w)
667 {
668         if (gui_internal_button_attr_update(this, w))
669                 gui_internal_widget_render(this, w);
670 }
671 static void
672 gui_internal_button_attr_pressed(struct gui_priv *this, struct widget *w, void *data)
673 {
674         if (w->is_on)
675                 w->set_attr(w->instance, &w->off);
676         else
677                 w->set_attr(w->instance, &w->on);
678         gui_internal_button_attr_update(this, w);
679
680 }
681
682 static struct widget *
683 gui_internal_button_navit_attr_new(struct gui_priv *this, char *text, enum flags flags, struct attr *on, struct attr *off)
684 {
685         struct graphics_image *image=NULL;
686         struct widget *ret;
687         if (!on && !off)
688                 return NULL;
689         image=image_new_xs(this, "gui_inactive");
690         ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
691         if (on)
692                 ret->on=*on;
693         if (off)
694                 ret->off=*off;
695         ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))navit_get_attr;
696         ret->set_attr=(int (*)(void *, struct attr *))navit_set_attr;
697         ret->remove_cb=(void (*)(void *, struct callback *))navit_remove_callback;
698         ret->instance=this->nav;
699         ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
700         navit_add_callback(this->nav, ret->cb);
701         gui_internal_button_attr_update(this, ret);
702         return ret;
703 }
704
705 static struct widget *
706 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)
707 {
708         struct graphics_image *image=NULL;
709         struct widget *ret;
710         image=image_new_xs(this, "gui_inactive");
711         if (!on && !off)
712                 return NULL;
713         ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
714         if (on)
715                 ret->on=*on;
716         if (off)
717                 ret->off=*off;
718         ret->deflt=deflt;
719         ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))map_get_attr;
720         ret->set_attr=(int (*)(void *, struct attr *))map_set_attr;
721         ret->remove_cb=(void (*)(void *, struct callback *))map_remove_callback;
722         ret->instance=map;
723         ret->redraw=1;
724         ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
725         map_add_callback(map, ret->cb);
726         gui_internal_button_attr_update(this, ret);
727         return ret;
728 }
729
730 static struct widget *
731 gui_internal_button_new(struct gui_priv *this, char *text, struct graphics_image *image, enum flags flags)
732 {
733         return gui_internal_button_new_with_callback(this, text, image, flags, NULL, NULL);
734 }
735
736 #if 0
737 //##############################################################################################################
738 //# Description:
739 //# Comment:
740 //# Authors: Martin Schaller (04/2008)
741 //##############################################################################################################
742 static void gui_internal_clear(struct gui_priv *this)
743 {
744         struct graphics *gra=this->gra;
745         struct point pnt;
746         pnt.x=0;
747         pnt.y=0;
748         graphics_draw_rectangle(gra, this->background, &pnt, this->root.w, this->root.h);
749 }
750 #endif
751
752 static struct widget *
753 gui_internal_find_widget(struct widget *wi, struct point *p, int flags)
754 {
755         struct widget *ret,*child;
756         GList *l=wi->children;
757
758         if (p) {
759                 if (wi->p.x > p->x )
760                         return NULL;
761                 if (wi->p.y > p->y )
762                         return NULL;
763                 if ( wi->p.x + wi->w < p->x)
764                         return NULL;
765                 if ( wi->p.y + wi->h < p->y)
766                         return NULL;
767         }
768         if (wi->state & flags)
769                 return wi;
770         while (l) {
771                 child=l->data;
772                 ret=gui_internal_find_widget(child, p, flags);
773                 if (ret) {
774                         return ret;
775                 }
776                 l=g_list_next(l);
777         }
778         return NULL;
779
780 }
781
782 static void
783 gui_internal_highlight_do(struct gui_priv *this, struct widget *found)
784 {
785         if (found == this->highlighted)
786                 return;
787
788         graphics_draw_mode(this->gra, draw_mode_begin);
789         if (this->highlighted) {
790                 this->highlighted->state &= ~STATE_HIGHLIGHTED;
791                 if (this->root.children && this->highlighted_menu == g_list_last(this->root.children)->data)
792                         gui_internal_widget_render(this, this->highlighted);
793                 this->highlighted=NULL;
794                 this->highlighted_menu=NULL;
795         }
796         if (found) {
797                 this->highlighted=found;
798                 this->highlighted_menu=g_list_last(this->root.children)->data;
799                 this->highlighted->state |= STATE_HIGHLIGHTED;
800                 gui_internal_widget_render(this, this->highlighted);
801                 dbg(1,"%d,%d %dx%d\n", found->p.x, found->p.y, found->w, found->h);
802         }
803         graphics_draw_mode(this->gra, draw_mode_end);
804 }
805 //##############################################################################################################
806 //# Description:
807 //# Comment:
808 //# Authors: Martin Schaller (04/2008)
809 //##############################################################################################################
810 static void gui_internal_highlight(struct gui_priv *this)
811 {
812         struct widget *menu,*found=NULL;
813         if (this->current.x > -1 && this->current.y > -1) {
814                 menu=g_list_last(this->root.children)->data;
815                 found=gui_internal_find_widget(menu, &this->current, STATE_SENSITIVE);
816         }
817         gui_internal_highlight_do(this, found);
818         this->motion_timeout_event=NULL;
819 }
820
821 static struct widget *
822 gui_internal_box_new_with_label(struct gui_priv *this, enum flags flags, char *label)
823 {
824         struct widget *widget=g_new0(struct widget, 1);
825
826         if (label)
827                 widget->text=g_strdup(label);
828         widget->type=widget_box;
829         widget->flags=flags;
830         return widget;
831 }
832
833 static struct widget *
834 gui_internal_box_new(struct gui_priv *this, enum flags flags)
835 {
836         return gui_internal_box_new_with_label(this, flags, NULL);
837 }
838
839
840 static void gui_internal_box_render(struct gui_priv *this, struct widget *w)
841 {
842         struct widget *wc;
843         GList *l;
844
845         gui_internal_background_render(this, w);
846 #if 1
847         if (w->foreground && w->border) {
848         struct point pnt[5];
849         pnt[0]=w->p;
850         pnt[1].x=pnt[0].x+w->w;
851         pnt[1].y=pnt[0].y;
852         pnt[2].x=pnt[0].x+w->w;
853         pnt[2].y=pnt[0].y+w->h;
854         pnt[3].x=pnt[0].x;
855         pnt[3].y=pnt[0].y+w->h;
856         pnt[4]=pnt[0];
857         graphics_gc_set_linewidth(w->foreground, w->border ? w->border : 1);
858         graphics_draw_lines(this->gra, w->foreground, pnt, 5);
859         graphics_gc_set_linewidth(w->foreground, 1);
860         }
861 #endif
862
863         l=w->children;
864         while (l) {
865                 wc=l->data;
866                 gui_internal_widget_render(this, wc);
867                 l=g_list_next(l);
868         }
869 }
870
871
872 /**
873  * @brief Compute the size and location for the widget.
874  *
875  *
876  */
877 static void gui_internal_box_pack(struct gui_priv *this, struct widget *w)
878 {
879         struct widget *wc;
880         int x0,x=0,y=0,width=0,height=0,owidth=0,oheight=0,expand=0,count=0,rows=0,cols=w->cols ? w->cols : 0;
881         GList *l;
882         int orientation=w->flags & 0xffff0000;
883
884         if (!cols)
885                 cols=this->cols;
886         if (!cols) {
887                  if ( this->root.w > this->root.h )
888                          cols=3;
889                  else
890                          cols=2;
891                  width=0;
892                  height=0;
893         }
894
895
896         /**
897          * count the number of children
898          */
899         l=w->children;
900         while (l) {
901                 count++;
902                 l=g_list_next(l);
903         }
904         if (orientation == orientation_horizontal_vertical && count <= cols)
905                 orientation=orientation_horizontal;
906         switch (orientation) {
907         case orientation_horizontal:
908                 /**
909                  * For horizontal orientation:
910                  * pack each child and find the largest height and
911                  * compute the total width. x spacing (spx) is considered
912                  *
913                  * If any children want to be expanded
914                  * we keep track of this
915                  */
916                 l=w->children;
917                 while (l) {
918                         wc=l->data;
919                         gui_internal_widget_pack(this, wc);
920                         if (height < wc->h)
921                                 height=wc->h;
922                         width+=wc->w;
923                         if (wc->flags & flags_expand)
924                                 expand+=wc->w ? wc->w : 1;
925                         l=g_list_next(l);
926                         if (l)
927                                 width+=w->spx;
928                 }
929                 owidth=width;
930                 if (expand && w->w) {
931                         expand=100*(w->w-width+expand)/expand;
932                         owidth=w->w;
933                 } else
934                         expand=100;
935                 break;
936         case orientation_vertical:
937                 /**
938                  * For vertical layouts:
939                  * We pack each child and compute the largest width and
940                  * the total height.  y spacing (spy) is considered
941                  *
942                  * If the expand flag is set then teh expansion amount
943                  * is computed.
944                  */
945                 l=w->children;
946                 while (l) {
947                         wc=l->data;
948                         gui_internal_widget_pack(this, wc);
949                         if (width < wc->w)
950                                 width=wc->w;
951                         height+=wc->h;
952                         if (wc->flags & flags_expand)
953                                 expand+=wc->h ? wc->h : 1;
954                         l=g_list_next(l);
955                         if (l)
956                                 height+=w->spy;
957                 }
958                 oheight=height;
959                 if (expand && w->h) {
960                         expand=100*(w->h-height+expand)/expand;
961                         oheight=w->h;
962                 } else
963                         expand=100;
964                 break;
965         case orientation_horizontal_vertical:
966                 /**
967                  * For horizontal_vertical orientation
968                  * pack the children.
969                  * Compute the largest height and largest width.
970                  * Layout the widgets based on having the
971                  * number of columns specified by (cols)
972                  */
973                 count=0;
974                 l=w->children;
975                 while (l) {
976                         wc=l->data;
977                         gui_internal_widget_pack(this, wc);
978                         if (height < wc->h)
979                                 height=wc->h;
980                         if (width < wc->w)
981                                 width=wc->w;
982                         l=g_list_next(l);
983                         count++;
984                 }
985                 if (count < cols)
986                         cols=count;
987                 rows=(count+cols-1)/cols;
988                 width*=cols;
989                 height*=rows;
990                 width+=w->spx*(cols-1);
991                 height+=w->spy*(rows-1);
992                 owidth=width;
993                 oheight=height;
994                 expand=100;
995                 break;
996         default:
997                 /**
998                  * No orientation was specified.
999                  * width and height are both 0.
1000                  * The width & height of this widget
1001                  * will be used.
1002                  */
1003                 if(!w->w && !w->h)
1004                         dbg(0,"Warning width and height of a widget are 0");
1005                 break;
1006         }
1007         if (! w->w && ! w->h) {
1008                 w->w=w->bl+w->br+width;
1009                 w->h=w->bt+w->bb+height;
1010         }
1011
1012         /**
1013          * At this stage the width and height of this
1014          * widget has been computed.
1015          * We now make a second pass assigning heights,
1016          * widths and coordinates to each child widget.
1017          */
1018
1019         if (w->flags & gravity_left)
1020                 x=w->p.x+w->bl;
1021         if (w->flags & gravity_xcenter)
1022                 x=w->p.x+w->w/2-owidth/2;
1023         if (w->flags & gravity_right)
1024                 x=w->p.x+w->w-w->br-owidth;
1025         if (w->flags & gravity_top)
1026                 y=w->p.y+w->bt;
1027         if (w->flags & gravity_ycenter)
1028                 y=w->p.y+w->h/2-oheight/2;
1029         if (w->flags & gravity_bottom)
1030                 y=w->p.y+w->h-w->bb-oheight;
1031         l=w->children;
1032         switch (orientation) {
1033         case orientation_horizontal:
1034                 l=w->children;
1035                 while (l) {
1036                         wc=l->data;
1037                         wc->p.x=x;
1038                         if (wc->flags & flags_fill)
1039                                 wc->h=w->h;
1040                         if (wc->flags & flags_expand) {
1041                                 if (! wc->w)
1042                                         wc->w=1;
1043                                 wc->w=wc->w*expand/100;
1044                         }
1045                         if (w->flags & gravity_top)
1046                                 wc->p.y=y;
1047                         if (w->flags & gravity_ycenter)
1048                                 wc->p.y=y-wc->h/2;
1049                         if (w->flags & gravity_bottom)
1050                                 wc->p.y=y-wc->h;
1051                         x+=wc->w+w->spx;
1052                         l=g_list_next(l);
1053                 }
1054                 break;
1055         case orientation_vertical:
1056                 l=w->children;
1057                 while (l) {
1058                         wc=l->data;
1059                         wc->p.y=y;
1060                         if (wc->flags & flags_fill)
1061                                 wc->w=w->w;
1062                         if (wc->flags & flags_expand) {
1063                                 if (! wc->h)
1064                                         wc->h=1;
1065                                 wc->h=wc->h*expand/100;
1066                         }
1067                         if (w->flags & gravity_left)
1068                                 wc->p.x=x;
1069                         if (w->flags & gravity_xcenter)
1070                                 wc->p.x=x-wc->w/2;
1071                         if (w->flags & gravity_right)
1072                                 wc->p.x=x-wc->w;
1073                         y+=wc->h+w->spy;
1074                         l=g_list_next(l);
1075                 }
1076                 break;
1077         case orientation_horizontal_vertical:
1078                 l=w->children;
1079                 x0=x;
1080                 count=0;
1081                 width/=cols;
1082                 height/=rows;
1083                 while (l) {
1084                         wc=l->data;
1085                         wc->p.x=x;
1086                         wc->p.y=y;
1087                         if (wc->flags & flags_fill) {
1088                                 wc->w=width;
1089                                 wc->h=height;
1090                         }
1091                         if (w->flags & gravity_left)
1092                                 wc->p.x=x;
1093                         if (w->flags & gravity_xcenter)
1094                                 wc->p.x=x+(width-wc->w)/2;
1095                         if (w->flags & gravity_right)
1096                                 wc->p.x=x+width-wc->w;
1097                         if (w->flags & gravity_top)
1098                                 wc->p.y=y;
1099                         if (w->flags & gravity_ycenter)
1100                                 wc->p.y=y+(height-wc->h)/2;
1101                         if (w->flags & gravity_bottom)
1102                                 wc->p.y=y-height-wc->h;
1103                         x+=width;
1104                         if (++count == cols) {
1105                                 count=0;
1106                                 x=x0;
1107                                 y+=height;
1108                         }
1109                         l=g_list_next(l);
1110                 }
1111                 break;
1112         default:
1113                 break;
1114         }
1115         /**
1116          * Call pack again on each child,
1117          * the child has now had its size and coordinates
1118          * set so they can repack their children.
1119          */
1120         l=w->children;
1121         while (l) {
1122                 wc=l->data;
1123                 gui_internal_widget_pack(this, wc);
1124                 l=g_list_next(l);
1125         }
1126 }
1127
1128 static void
1129 gui_internal_widget_append(struct widget *parent, struct widget *child)
1130 {
1131         if (! child)
1132                 return;
1133         if (! child->background)
1134                 child->background=parent->background;
1135         parent->children=g_list_append(parent->children, child);
1136 }
1137
1138 static void gui_internal_widget_prepend(struct widget *parent, struct widget *child)
1139 {
1140         if (! child->background)
1141                 child->background=parent->background;
1142         parent->children=g_list_prepend(parent->children, child);
1143 }
1144
1145 static void gui_internal_widget_children_destroy(struct gui_priv *this, struct widget *w)
1146 {
1147         GList *l;
1148         struct widget *wc;
1149
1150         l=w->children;
1151         while (l) {
1152                 wc=l->data;
1153                 gui_internal_widget_destroy(this, wc);
1154                 l=g_list_next(l);
1155         }
1156         g_list_free(w->children);
1157         w->children=NULL;
1158 }
1159
1160 static void gui_internal_widget_destroy(struct gui_priv *this, struct widget *w)
1161 {
1162         gui_internal_widget_children_destroy(this, w);
1163         g_free(w->speech);
1164         g_free(w->text);
1165         if (w->img)
1166                 graphics_image_free(this->gra, w->img);
1167         if (w->prefix)
1168                 g_free(w->prefix);
1169         if (w->name)
1170                 g_free(w->name);
1171         if (w->data_free)
1172                 w->data_free(w->data);
1173         if (w->cb && w->remove_cb)
1174                 w->remove_cb(w->instance, w->cb);
1175         if(w->free)
1176                 w->free(this,w);
1177         else
1178                 g_free(w);
1179 }
1180
1181
1182 static void
1183 gui_internal_widget_render(struct gui_priv *this, struct widget *w)
1184 {
1185         if(w->p.x > this->root.w || w->p.y > this->root.h)
1186                 return;
1187
1188         switch (w->type) {
1189         case widget_box:
1190                 gui_internal_box_render(this, w);
1191                 break;
1192         case widget_label:
1193                 gui_internal_label_render(this, w);
1194                 break;
1195         case widget_image:
1196                 gui_internal_image_render(this, w);
1197                 break;
1198         case widget_table:
1199                 gui_internal_table_render(this,w);
1200                 break;
1201         default:
1202                 break;
1203         }
1204 }
1205
1206 static void
1207 gui_internal_widget_pack(struct gui_priv *this, struct widget *w)
1208 {
1209         switch (w->type) {
1210         case widget_box:
1211                 gui_internal_box_pack(this, w);
1212                 break;
1213         case widget_table:
1214           gui_internal_table_pack(this,w);
1215         default:
1216                 break;
1217         }
1218 }
1219
1220 //##############################################################################################################
1221 //# Description:
1222 //# Comment:
1223 //# Authors: Martin Schaller (04/2008)
1224 //##############################################################################################################
1225 static void gui_internal_call_highlighted(struct gui_priv *this)
1226 {
1227         if (! this->highlighted || ! this->highlighted->func)
1228                 return;
1229         this->highlighted->reason=1;
1230         this->highlighted->func(this, this->highlighted, this->highlighted->data);
1231 }
1232
1233 static void
1234 gui_internal_say(struct gui_priv *this, struct widget *w, int questionmark)
1235 {
1236         char *text=w->speech;
1237         if (! this->speech)
1238                 return;
1239         if (!text)
1240                 text=w->text;
1241         if (!text)
1242                 text=w->name;
1243         if (text) {
1244                 text=g_strdup_printf("%s%c", text, questionmark ? '?':'\0');
1245                 navit_say(this->nav, text);
1246                 g_free(text);
1247         }
1248 }
1249
1250 static void
1251 gui_internal_prune_menu(struct gui_priv *this, struct widget *w)
1252 {
1253         GList *l;
1254         struct widget *wr;
1255         gui_internal_search_idle_end(this);
1256         while ((l = g_list_last(this->root.children))) {
1257                 if (l->data == w) {
1258                         void (*redisplay)(struct gui_priv *priv, struct widget *widget, void *data);
1259                         gui_internal_say(this, w, 0);
1260                         redisplay=w->menu_data->redisplay;
1261                         wr=w->menu_data->redisplay_widget;
1262                         if (!w->menu_data->redisplay) {
1263                                 gui_internal_widget_render(this, w);
1264                                 return;
1265                         }
1266                         gui_internal_widget_destroy(this, l->data);
1267                         this->root.children=g_list_remove(this->root.children, l->data);
1268                         redisplay(this, wr, wr->data);
1269                         return;
1270                 }
1271                 gui_internal_widget_destroy(this, l->data);
1272                 this->root.children=g_list_remove(this->root.children, l->data);
1273         }
1274 }
1275
1276 static void
1277 gui_internal_prune_menu_count(struct gui_priv *this, int count, int render)
1278 {
1279         GList *l;
1280         gui_internal_search_idle_end(this);
1281         while ((l = g_list_last(this->root.children)) && count-- > 0) {
1282                 gui_internal_widget_destroy(this, l->data);
1283                 this->root.children=g_list_remove(this->root.children, l->data);
1284         }
1285         if (l && render) {
1286                 gui_internal_say(this, l->data, 0);
1287                 gui_internal_widget_render(this, l->data);
1288         }
1289 }
1290
1291 static void
1292 gui_internal_back(struct gui_priv *this, struct widget *w, void *data)
1293 {
1294         gui_internal_prune_menu_count(this, 1, 1);
1295 }
1296
1297 static void
1298 gui_internal_cmd_return(struct gui_priv *this, struct widget *wm, void *data)
1299 {
1300         gui_internal_prune_menu(this, wm->data);
1301 }
1302
1303 static void
1304 gui_internal_cmd_main_menu(struct gui_priv *this, struct widget *wm, void *data)
1305 {
1306         gui_internal_prune_menu(this, this->root.children->data);
1307 }
1308
1309 static struct widget *
1310 gui_internal_top_bar(struct gui_priv *this)
1311 {
1312         struct widget *w,*wm,*wh,*wc,*wcn;
1313         int dots_len, sep_len;
1314         GList *res=NULL,*l;
1315         int width,width_used=0,use_sep=0,incomplete=0;
1316         struct graphics_gc *foreground=(this->flags & 256 ? this->background : this->text_foreground);
1317 /* flags
1318         1:Don't expand bar to screen width
1319         2:Don't show Map Icon
1320         4:Don't show Home Icon
1321         8:Show only current menu
1322         16:Don't use menu titles as button
1323         32:Show navit version
1324         64:Show time
1325         128:Show help
1326         256:Use background for menu headline
1327         512:Set osd_configuration and zoom to route when setting position
1328 */
1329
1330         w=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|(this->flags & 1 ? 0:flags_fill));
1331         w->bl=this->spacing;
1332         w->spx=this->spacing;
1333         w->background=this->background2;
1334         if ((this->flags & 6) == 6) {
1335                 w->bl=10;
1336                 w->br=10;
1337                 w->bt=6;
1338                 w->bb=6;
1339         }
1340         width=this->root.w-w->bl;
1341         if (! (this->flags & 2)) {
1342                 wm=gui_internal_button_new_with_callback(this, NULL,
1343                         image_new_s(this, "gui_map"), gravity_center|orientation_vertical,
1344                         gui_internal_cmd_return, NULL);
1345                 wm->speech=g_strdup(_("Back to map"));
1346                 gui_internal_widget_pack(this, wm);
1347                 gui_internal_widget_append(w, wm);
1348                 width-=wm->w;
1349         }
1350         if (! (this->flags & 4)) {
1351                 wh=gui_internal_button_new_with_callback(this, NULL,
1352                         image_new_s(this, "gui_home"), gravity_center|orientation_vertical,
1353                         gui_internal_cmd_main_menu, NULL);
1354                 wh->speech=g_strdup(_("Main Menu"));
1355                 gui_internal_widget_pack(this, wh);
1356                 gui_internal_widget_append(w, wh);
1357                 width-=wh->w;
1358         }
1359         if (!(this->flags & 6))
1360                 width-=w->spx;
1361         l=g_list_last(this->root.children);
1362         wcn=gui_internal_label_new(this,".. »");
1363         wcn->foreground=foreground;
1364         dots_len=wcn->w;
1365         gui_internal_widget_destroy(this, wcn);
1366         wcn=gui_internal_label_new(this,"»");
1367         wcn->foreground=foreground;
1368         sep_len=wcn->w;
1369         gui_internal_widget_destroy(this, wcn);
1370         while (l) {
1371                 if (g_list_previous(l) || !g_list_next(l)) {
1372                         wc=l->data;
1373                         wcn=gui_internal_label_new(this, wc->text);
1374                         wcn->foreground=foreground;
1375                         if (g_list_next(l))
1376                                 use_sep=1;
1377                         else
1378                                 use_sep=0;
1379                         dbg(1,"%d (%s) + %d + %d + %d > %d\n", wcn->w, wc->text, width_used, w->spx, use_sep ? sep_len : 0, width);
1380                         if (wcn->w + width_used + w->spx + (use_sep ? sep_len : 0) + (g_list_previous(l) ? dots_len : 0) > width) {
1381                                 incomplete=1;
1382                                 gui_internal_widget_destroy(this, wcn);
1383                                 break;
1384                         }
1385                         if (use_sep) {
1386                                 struct widget *wct=gui_internal_label_new(this, "»");
1387                                 wct->foreground=foreground;
1388                                 res=g_list_prepend(res, wct);
1389                                 width_used+=sep_len+w->spx;
1390                         }
1391                         width_used+=wcn->w;
1392                         if (!(this->flags & 16)) {
1393                                 wcn->func=gui_internal_cmd_return;
1394                                 wcn->data=wc;
1395                                 wcn->state |= STATE_SENSITIVE;
1396                         }
1397                         res=g_list_prepend(res, wcn);
1398                         if (this->flags & 8)
1399                                 break;
1400                 }
1401                 l=g_list_previous(l);
1402         }
1403         if (incomplete) {
1404                 if (! res) {
1405                         wcn=gui_internal_label_new_abbrev(this, wc->text, width-width_used-w->spx-dots_len);
1406                         wcn->foreground=foreground;
1407                         wcn->func=gui_internal_cmd_return;
1408                         wcn->data=wc;
1409                         wcn->state |= STATE_SENSITIVE;
1410                         res=g_list_prepend(res, wcn);
1411                         l=g_list_previous(l);
1412                         wc=l->data;
1413                 }
1414                 wcn=gui_internal_label_new(this, ".. »");
1415                 wcn->foreground=foreground;
1416                 wcn->func=gui_internal_cmd_return;
1417                 wcn->data=wc;
1418                 wcn->state |= STATE_SENSITIVE;
1419                 res=g_list_prepend(res, wcn);
1420         }
1421         l=res;
1422         while (l) {
1423                 gui_internal_widget_append(w, l->data);
1424                 l=g_list_next(l);
1425         }
1426         if (this->flags & 32) {
1427                 extern char *version;
1428                 char *version_text=g_strdup_printf("Navit %s",version);
1429                 wcn=gui_internal_label_new(this, version_text);
1430                 g_free(version_text);
1431                 wcn->flags=gravity_right_center|flags_expand;
1432                 gui_internal_widget_append(w, wcn);
1433         }
1434 #if 0
1435         if (dots)
1436                 gui_internal_widget_destroy(this, dots);
1437 #endif
1438         return w;
1439 }
1440
1441 static struct widget *
1442 gui_internal_time_help(struct gui_priv *this)
1443 {
1444         struct widget *w,*wc,*wcn;
1445         char timestr[64];
1446         struct tm *tm;
1447         time_t timep;
1448
1449         w=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
1450         w->bl=this->spacing;
1451         w->spx=this->spacing;
1452         w->spx=10;
1453         w->bl=10;
1454         w->br=10;
1455         w->bt=6;
1456         w->bb=6;
1457         if (this->flags & 64) {
1458                 wc=gui_internal_box_new(this, gravity_right_top|orientation_vertical|flags_fill);
1459                 wc->bl=10;
1460                 wc->br=20;
1461                 wc->bt=6;
1462                 wc->bb=6;
1463                 timep=time(NULL);
1464                 tm=localtime(&timep);
1465                 strftime(timestr, 64, "%H:%M %d.%m.%Y", tm);
1466                 wcn=gui_internal_label_new(this, timestr);
1467                 gui_internal_widget_append(wc, wcn);
1468                 gui_internal_widget_append(w, wc);
1469         }
1470         if (this->flags & 128) {
1471                 wcn=gui_internal_button_new_with_callback(this, _("Help"), image_new_l(this, "gui_help"), gravity_center|orientation_vertical|flags_fill, NULL, NULL);
1472                 gui_internal_widget_append(w, wcn);
1473         }
1474         return w;
1475 }
1476
1477
1478 /**
1479  * Applys the configuration values to this based on the settings
1480  * specified in the configuration file (this->config) and
1481  * the most approriate default profile based on screen resolution.
1482  *
1483  * This function should be run after this->root is setup and could
1484  * be rerun after the window is resized.
1485  *
1486  * @authors Steve Singer <ssinger_pg@sympatico.ca> (09/2008)
1487  */
1488 static void gui_internal_apply_config(struct gui_priv *this)
1489 {
1490   struct gui_config_settings *  current_config=0;
1491
1492   dbg(0,"w=%d h=%d\n", this->root.w, this->root.h);
1493   /**
1494    * Select default values from profile based on the screen.
1495    */
1496   if((this->root.w > 320 || this->root.h > 320) && this->root.w > 240 && this->root.h > 240)
1497   {
1498     if((this->root.w > 640 || this->root.h > 640) && this->root.w > 480 && this->root.h > 480 )
1499     {
1500       current_config = &config_profiles[LARGE_PROFILE];
1501     }
1502     else
1503     {
1504       current_config = &config_profiles[MEDIUM_PROFILE];
1505     }
1506   }
1507   else
1508   {
1509     current_config = &config_profiles[SMALL_PROFILE];
1510   }
1511
1512   /**
1513    * Apply override values from config file
1514    */
1515   if(this->config.font_size == -1 )
1516   {
1517     this->font_size = current_config->font_size;
1518   }
1519   else
1520   {
1521     this->font_size = this->config.font_size;
1522   }
1523
1524   if(this->config.icon_xs == -1 )
1525   {
1526       this->icon_xs = current_config->icon_xs;
1527   }
1528   else
1529   {
1530     this->icon_xs = this->config.icon_xs;
1531   }
1532
1533   if(this->config.icon_s == -1 )
1534   {
1535     this->icon_s = current_config->icon_s;
1536   }
1537   else
1538   {
1539     this->icon_s = this->config.icon_s;
1540   }
1541   if(this->config.icon_l == -1 )
1542   {
1543     this->icon_l = current_config->icon_l;
1544   }
1545   else
1546   {
1547     this->icon_l = this->config.icon_l;
1548   }
1549   if(this->config.spacing == -1 )
1550   {
1551     this->spacing = current_config->spacing;
1552   }
1553   else
1554   {
1555     this->spacing = current_config->spacing;
1556   }
1557
1558 }
1559
1560 static struct widget *
1561 gui_internal_button_label(struct gui_priv *this, char *label, int mode)
1562 {
1563         struct widget *wl,*wlb;
1564         struct widget *wb=gui_internal_menu_data(this)->button_bar;
1565         wlb=gui_internal_box_new(this, gravity_right_center|orientation_vertical);
1566         wl=gui_internal_label_new(this, label);
1567         wlb->border=1;
1568         wlb->foreground=this->text_foreground;
1569         wlb->bl=20;
1570         wlb->br=20;
1571         wlb->bb=6;
1572         wlb->bt=6;
1573         gui_internal_widget_append(wlb, wl);
1574         if (mode == 1)
1575                 gui_internal_widget_prepend(wb, wlb);
1576         if (mode == -1)
1577                 gui_internal_widget_append(wb, wlb);
1578
1579         return wlb;
1580 }
1581
1582
1583 static struct widget *
1584 gui_internal_menu(struct gui_priv *this, char *label)
1585 {
1586         struct widget *menu,*w,*w1,*topbox;
1587
1588         gui_internal_search_idle_end(this);
1589         topbox=gui_internal_box_new_with_label(this, 0, label);
1590         topbox->w=this->root.w;
1591         topbox->h=this->root.h;
1592         gui_internal_widget_append(&this->root, topbox);
1593         menu=gui_internal_box_new(this, gravity_left_center|orientation_vertical);
1594         menu->w=this->root.w;
1595         menu->h=this->root.h;
1596         menu->background=this->background;
1597         gui_internal_apply_config(this);
1598         this->font=graphics_font_new(this->gra,this->font_size,1);
1599         topbox->menu_data=g_new0(struct menu_data, 1);
1600         gui_internal_widget_append(topbox, menu);
1601         w=gui_internal_top_bar(this);
1602         gui_internal_widget_append(menu, w);
1603         w=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
1604         w->spx=4*this->spacing;
1605         w->w=menu->w;
1606         gui_internal_widget_append(menu, w);
1607         if (this->flags & 16) {
1608                 struct widget *wlb,*wb,*wm=w;
1609                 wm->flags=gravity_center|orientation_vertical|flags_expand|flags_fill;
1610                 w=gui_internal_box_new(this, gravity_center|orientation_horizontal|flags_expand|flags_fill);
1611                 dbg(0,"topbox->menu_data=%p\n", topbox->menu_data);
1612                 gui_internal_widget_append(wm, w);
1613                 wb=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
1614                 wb->bl=6;
1615                 wb->br=6;
1616                 wb->bb=6;
1617                 wb->bt=6;
1618                 wb->spx=6;
1619                 topbox->menu_data->button_bar=wb;
1620                 gui_internal_widget_append(wm, wb);
1621                 wlb=gui_internal_button_label(this,_("Back"),1);
1622                 wlb->func=gui_internal_back;
1623                 wlb->state |= STATE_SENSITIVE;
1624         }
1625         if (this->flags & 192) {
1626                 menu=gui_internal_box_new(this, gravity_left_center|orientation_vertical);
1627                 menu->w=this->root.w;
1628                 menu->h=this->root.h;
1629                 w1=gui_internal_time_help(this);
1630                 gui_internal_widget_append(menu, w1);
1631                 w1=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_expand|flags_fill);
1632                 gui_internal_widget_append(menu, w1);
1633                 gui_internal_widget_append(topbox, menu);
1634                 menu->background=NULL;
1635         }
1636         return w;
1637 }
1638
1639 static struct menu_data *
1640 gui_internal_menu_data(struct gui_priv *this)
1641 {
1642         GList *l;
1643         struct widget *menu;
1644
1645         l=g_list_last(this->root.children);
1646         menu=l->data;
1647         return menu->menu_data;
1648 }
1649
1650 static void
1651 gui_internal_menu_render(struct gui_priv *this)
1652 {
1653         GList *l;
1654         struct widget *menu;
1655
1656         l=g_list_last(this->root.children);
1657         menu=l->data;
1658         gui_internal_say(this, menu, 0);
1659         gui_internal_widget_pack(this, menu);
1660         gui_internal_widget_render(this, menu);
1661 }
1662
1663 static void
1664 gui_internal_cmd_set_destination(struct gui_priv *this, struct widget *wm, void *data)
1665 {
1666         struct widget *w=wm->data;
1667         dbg(0,"c=%d:0x%x,0x%x\n", w->c.pro, w->c.x, w->c.y);
1668         navit_set_destination(this->nav, &w->c, w->name, 1);
1669         if (this->flags & 512) {
1670                 struct attr follow;
1671                 follow.type=attr_follow;
1672                 follow.u.num=180;
1673                 navit_set_attr(this->nav, &this->osd_configuration);
1674                 navit_set_attr(this->nav, &follow);
1675                 navit_zoom_to_route(this->nav, 0);
1676         }
1677         gui_internal_prune_menu(this, NULL);
1678 }
1679
1680 static void
1681 gui_internal_cmd_set_position(struct gui_priv *this, struct widget *wm, void *data)
1682 {
1683         struct widget *w=wm->data;
1684         navit_set_position(this->nav, &w->c);
1685         gui_internal_prune_menu(this, NULL);
1686 }
1687
1688 static void
1689 gui_internal_cmd_add_bookmark_do(struct gui_priv *this, struct widget *widget)
1690 {
1691         GList *l;
1692         dbg(0,"text='%s'\n", widget->text);
1693         if (widget->text && strlen(widget->text))
1694                 navit_add_bookmark(this->nav, &widget->c, widget->text);
1695         g_free(widget->text);
1696         widget->text=NULL;
1697         l=g_list_previous(g_list_last(this->root.children));
1698         gui_internal_prune_menu(this, l->data);
1699 }
1700
1701 static void
1702 gui_internal_cmd_add_bookmark_clicked(struct gui_priv *this, struct widget *widget, void *data)
1703 {
1704         gui_internal_cmd_add_bookmark_do(this, widget->data);
1705 }
1706
1707 static void
1708 gui_internal_cmd_add_bookmark_changed(struct gui_priv *this, struct widget *wm, void *data)
1709 {
1710         int len;
1711         dbg(1,"enter\n");
1712         if (wm->text) {
1713                 len=strlen(wm->text);
1714                 dbg(1,"len=%d\n", len);
1715                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
1716                         wm->text[len-1]='\0';
1717                         gui_internal_cmd_add_bookmark_do(this, wm);
1718                 }
1719         }
1720 }
1721
1722
1723 static struct widget * gui_internal_keyboard(struct gui_priv *this, int mode);
1724
1725 static void
1726 gui_internal_cmd_add_bookmark(struct gui_priv *this, struct widget *wm, void *data)
1727 {
1728         struct widget *w,*wb,*wk,*wl,*we,*wnext,*wp=wm->data;
1729         wb=gui_internal_menu(this, "Add Bookmark");
1730         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
1731         gui_internal_widget_append(wb, w);
1732         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
1733         gui_internal_widget_append(w, we);
1734         gui_internal_widget_append(we, wk=gui_internal_label_new(this, wp->name ? wp->name : wp->text));
1735         wk->state |= STATE_EDIT|STATE_CLEAR;
1736         wk->background=this->background;
1737         wk->flags |= flags_expand|flags_fill;
1738         wk->func = gui_internal_cmd_add_bookmark_changed;
1739         wk->c=wm->c;
1740         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
1741         wnext->state |= STATE_SENSITIVE;
1742         wnext->func = gui_internal_cmd_add_bookmark_clicked;
1743         wnext->data=wk;
1744         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
1745         gui_internal_widget_append(w, wl);
1746         if (this->keyboard)
1747                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
1748         gui_internal_menu_render(this);
1749 }
1750
1751
1752 static void
1753 get_direction(char *buffer, int angle, int mode)
1754 {
1755         angle=angle%360;
1756         switch (mode) {
1757         case 0:
1758                 sprintf(buffer,"%d",angle);
1759                 break;
1760         case 1:
1761                 if (angle < 69 || angle > 291)
1762                         *buffer++='N';
1763                 if (angle > 111 && angle < 249)
1764                         *buffer++='S';
1765                 if (angle > 22 && angle < 158)
1766                         *buffer++='E';
1767                 if (angle > 202 && angle < 338)
1768                         *buffer++='W';
1769                 *buffer++='\0';
1770                 break;
1771         case 2:
1772                 angle=(angle+15)/30;
1773                 if (! angle)
1774                         angle=12;
1775                 sprintf(buffer,"%d H", angle);
1776                 break;
1777         }
1778 }
1779
1780 struct selector {
1781         char *icon;
1782         char *name;
1783         enum item_type *types;
1784 } selectors[]={
1785         {"bank","Bank",(enum item_type []){type_poi_bank,type_poi_bank,type_none}},
1786         {"fuel","Fuel",(enum item_type []){type_poi_fuel,type_poi_fuel,type_none}},
1787         {"hotel","Hotel",(enum item_type []) {
1788                 type_poi_hotel,type_poi_camp_rv,
1789                 type_poi_camping,type_poi_camping,
1790                 type_poi_resort,type_poi_resort,
1791                 type_poi_motel,type_poi_hostel,
1792                 type_none}},
1793         {"restaurant","Restaurant",(enum item_type []) {
1794                 type_poi_bar,type_poi_picnic,
1795                 type_poi_burgerking,type_poi_fastfood,
1796                 type_poi_restaurant,type_poi_restaurant,
1797                 type_none}},
1798         {"shopping","Shopping",(enum item_type []) {
1799                 type_poi_mall,type_poi_mall,
1800                 type_poi_shop_grocery,type_poi_shop_grocery,
1801                 type_none}},
1802         {"hospital","Service",(enum item_type []) {
1803                 type_poi_marina,type_poi_marina,
1804                 type_poi_hospital,type_poi_hospital,
1805                 type_poi_public_utilities,type_poi_public_utilities,
1806                 type_poi_police,type_poi_autoservice,
1807                 type_poi_information,type_poi_information,
1808                 type_poi_personal_service,type_poi_repair_service,
1809                 type_poi_rest_room,type_poi_rest_room,
1810                 type_poi_restroom,type_poi_restroom,
1811                 type_none}},
1812         {"parking","Parking",(enum item_type []){type_poi_car_parking,type_poi_car_parking,type_none}},
1813         {"peak","Land Features",(enum item_type []){
1814                 type_poi_land_feature,type_poi_rock,
1815                 type_poi_dam,type_poi_dam,
1816                 type_none}},
1817         {"unknown","Other",(enum item_type []){
1818                 type_point_unspecified,type_poi_land_feature-1,
1819                 type_poi_rock+1,type_poi_fuel-1,
1820                 type_poi_marina+1,type_poi_car_parking-1,
1821                 type_poi_car_parking+1,type_poi_bar-1,
1822                 type_poi_bank+1,type_poi_dam-1,
1823                 type_poi_dam+1,type_poi_information-1,
1824                 type_poi_information+1,type_poi_mall-1,
1825                 type_poi_mall+1,type_poi_personal_service-1,
1826                 type_poi_restaurant+1,type_poi_restroom-1,
1827                 type_poi_restroom+1,type_poi_shop_grocery-1,
1828                 type_poi_shop_grocery+1,type_poi_motel-1,
1829                 type_poi_hostel+1,type_selected_point,
1830                 type_none}},
1831 };
1832
1833 static void gui_internal_cmd_pois(struct gui_priv *this, struct widget *wm, void *data);
1834
1835 static struct widget *
1836 gui_internal_cmd_pois_selector(struct gui_priv *this, struct pcoord *c)
1837 {
1838         struct widget *wl,*wb;
1839         int i;
1840         wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
1841         for (i = 0 ; i < sizeof(selectors)/sizeof(struct selector) ; i++) {
1842         gui_internal_widget_append(wl, wb=gui_internal_button_new_with_callback(this, NULL,
1843                 image_new_xs(this, selectors[i].icon), gravity_left_center|orientation_vertical,
1844                 gui_internal_cmd_pois, &selectors[i]));
1845                 wb->c=*c;
1846                 wb->bt=10;
1847         }
1848         return wl;
1849 }
1850
1851 static struct widget *
1852 gui_internal_cmd_pois_item(struct gui_priv *this, struct coord *center, struct item *item, struct coord *c, int dist)
1853 {
1854         char distbuf[32];
1855         char dirbuf[32];
1856         char *type;
1857         struct attr attr;
1858         struct widget *wl;
1859         char *text;
1860
1861         wl=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
1862
1863         sprintf(distbuf,"%d", dist/1000);
1864         get_direction(dirbuf, transform_get_angle_delta(center, c, 0), 1);
1865         type=item_to_name(item->type);
1866         if (item_attr_get(item, attr_label, &attr)) {
1867                 wl->name=g_strdup_printf("%s %s",type,attr.u.str);
1868         } else {
1869                 attr.u.str="";
1870                 wl->name=g_strdup(type);
1871         }
1872         text=g_strdup_printf("%s %s %s %s", distbuf, dirbuf, type, attr.u.str);
1873         gui_internal_widget_append(wl, gui_internal_label_new(this, text));
1874         g_free(text);
1875
1876         return wl;
1877 }
1878
1879 static gint
1880 gui_internal_cmd_pois_sort_num(gconstpointer a, gconstpointer b, gpointer user_data)
1881 {
1882         const struct widget *wa=a;
1883         const struct widget *wb=b;
1884         struct widget *wac=wa->children->data;
1885         struct widget *wbc=wb->children->data;
1886         int ia,ib;
1887         ia=atoi(wac->text);
1888         ib=atoi(wbc->text);
1889
1890         return ia-ib;
1891 }
1892
1893 static int
1894 gui_internal_cmd_pois_item_selected(struct selector *sel, enum item_type type)
1895 {
1896         enum item_type *types;
1897         if (type >= type_line)
1898                 return 0;
1899         if (! sel || !sel->types)
1900                 return 1;
1901         types=sel->types;
1902         while (*types != type_none) {
1903                 if (type >= types[0] && type <= types[1]) {
1904                         return 1;
1905                 }
1906                 types+=2;
1907         }
1908         return 0;
1909 }
1910
1911 static void gui_internal_cmd_position(struct gui_priv *this, struct widget *wm, void *data);
1912
1913 static void
1914 gui_internal_cmd_pois(struct gui_priv *this, struct widget *wm, void *data)
1915 {
1916         struct map_selection *sel,*selm;
1917         struct coord c,center;
1918         struct mapset_handle *h;
1919         struct map *m;
1920         struct map_rect *mr;
1921         struct item *item;
1922         int idist,dist=20000;
1923         struct widget *wi,*w,*w2,*wb;
1924         enum projection pro=wm->c.pro;
1925         struct selector *isel=wm->data;
1926
1927         wb=gui_internal_menu(this, isel ? isel->name : _("POIs"));
1928         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
1929         gui_internal_widget_append(wb, w);
1930         if (! isel)
1931                 gui_internal_widget_append(w, gui_internal_cmd_pois_selector(this,&wm->c));
1932         w2=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
1933         gui_internal_widget_append(w, w2);
1934
1935         sel=map_selection_rect_new(&wm->c, dist, 18);
1936         center.x=wm->c.x;
1937         center.y=wm->c.y;
1938         h=mapset_open(navit_get_mapset(this->nav));
1939         while ((m=mapset_next(h, 1))) {
1940                 selm=map_selection_dup_pro(sel, pro, map_projection(m));
1941                 mr=map_rect_new(m, selm);
1942                 dbg(2,"mr=%p\n", mr);
1943                 if (mr) {
1944                         while ((item=map_rect_get_item(mr))) {
1945                                 if (gui_internal_cmd_pois_item_selected(isel, item->type) &&
1946                                     item_coord_get_pro(item, &c, 1, pro) &&
1947                                     coord_rect_contains(&sel->u.c_rect, &c) &&
1948                                     (idist=transform_distance(pro, &center, &c)) < dist) {
1949                                         gui_internal_widget_append(w2, wi=gui_internal_cmd_pois_item(this, &center, item, &c, idist));
1950                                         wi->func=gui_internal_cmd_position;
1951                                         wi->data=(void *)2;
1952                                         wi->item=*item;
1953                                         wi->state |= STATE_SENSITIVE;
1954                                         wi->c.x=c.x;
1955                                         wi->c.y=c.y;
1956                                         wi->c.pro=pro;
1957                                 }
1958                         }
1959                         map_rect_destroy(mr);
1960                 }
1961                 map_selection_destroy(selm);
1962         }
1963         map_selection_destroy(sel);
1964         mapset_close(h);
1965         w2->children=g_list_sort_with_data(w2->children,  gui_internal_cmd_pois_sort_num, (void *)1);
1966         gui_internal_menu_render(this);
1967 }
1968
1969 static void
1970 gui_internal_cmd_view_on_map(struct gui_priv *this, struct widget *wm, void *data)
1971 {
1972         struct widget *w=wm->data;
1973         int highlight=(w->data == (void *)2 || w->data == (void *)3 || w->data == (void *)4);
1974         if (highlight) {
1975                 graphics_clear_selection(this->gra, NULL);
1976                 graphics_add_selection(this->gra, &w->item, NULL);
1977         }
1978         navit_set_center(this->nav, &w->c, 1);
1979         gui_internal_prune_menu(this, NULL);
1980 }
1981
1982
1983 static void
1984 gui_internal_cmd_view_attributes(struct gui_priv *this, struct widget *wm, void *data)
1985 {
1986         struct widget *w,*wb;
1987         struct map_rect *mr;
1988         struct item *item;
1989         struct attr attr;
1990         char *text;
1991
1992         dbg(0,"item=%p 0x%x 0x%x\n", wm->item.map,wm->item.id_hi, wm->item.id_lo);
1993         wb=gui_internal_menu(this, "Attributes");
1994         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
1995         gui_internal_widget_append(wb, w);
1996         mr=map_rect_new(wm->item.map, NULL);
1997         item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
1998         dbg(0,"item=%p\n", item);
1999         if (item) {
2000                 while(item_attr_get(item, attr_any, &attr)) {
2001                         text=g_strdup_printf("%s:%s", attr_to_name(attr.type), attr_to_text(&attr, wm->item.map, 1));
2002                         gui_internal_widget_append(w,
2003                         wb=gui_internal_button_new_with_callback(this, text,
2004                                 NULL, gravity_left_center|orientation_horizontal|flags_fill,
2005                                 gui_internal_cmd_view_attributes, NULL));
2006                         g_free(text);
2007                 }
2008         }
2009         map_rect_destroy(mr);
2010         gui_internal_menu_render(this);
2011 }
2012
2013 static void
2014 gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data)
2015 {
2016         struct map_rect *mr;
2017         struct item *item;
2018         struct attr attr;
2019         char *cmd=NULL;
2020
2021         dbg(0,"item=%p 0x%x 0x%x\n", wm->item.map,wm->item.id_hi, wm->item.id_lo);
2022         mr=map_rect_new(wm->item.map, NULL);
2023         item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
2024         dbg(0,"item=%p\n", item);
2025         if (item) {
2026                 while(item_attr_get(item, attr_url_local, &attr)) {
2027                         if (! cmd)
2028                                 cmd=g_strdup_printf("navit-browser.sh '%s' &",attr.u.str);
2029                 }
2030         }
2031         map_rect_destroy(mr);
2032         if (cmd) {
2033 #ifdef HAVE_SYSTEM
2034                 system(cmd);
2035 #else
2036                 dbg(0,"calling external cmd '%s' is not supported\n",cmd);
2037 #endif
2038                 g_free(cmd);
2039         }
2040 }
2041
2042 /* wm->data: 0 Nothing special
2043              1 Map Point
2044              2 Item
2045              3 Town
2046 */
2047
2048
2049 static void
2050 gui_internal_cmd_position(struct gui_priv *this, struct widget *wm, void *data)
2051 {
2052         struct widget *wb,*w,*wc,*wbc;
2053         struct coord_geo g;
2054         struct coord c;
2055         char *coord,*name;
2056         int display_attributes=(wm->data == (void *)2);
2057         int display_view_on_map=(wm->data != (void *)1);
2058         int display_items=(wm->data == (void *)1);
2059         int display_streets=(wm->data == (void *)3);
2060         int display_house_numbers=1;
2061         if (wm->data == (void *)4) {
2062                 gui_internal_search_town_in_country(this, wm);
2063                 return;
2064         }
2065 #if 0
2066         switch ((int)wm->data) {
2067                 case 0:
2068 #endif
2069                         c.x=wm->c.x;
2070                         c.y=wm->c.y;
2071                         dbg(0,"x=0x%x y=0x%x\n", c.x, c.y);
2072                         transform_to_geo(wm->c.pro, &c, &g);
2073 #if 0
2074                         break;
2075                 case 1:
2076                         g=this->click;
2077                         break;
2078                 case 2:
2079                         g=this->vehicle;
2080                         break;
2081         }
2082 #endif
2083         name=wm->name ? wm->name : wm->text;
2084         wb=gui_internal_menu(this, name);
2085         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2086         gui_internal_widget_append(wb, w);
2087         coord=coordinates(&wm->c, ' ');
2088         gui_internal_widget_append(w, gui_internal_label_new(this, coord));
2089         g_free(coord);
2090         if (display_streets) {
2091                 gui_internal_widget_append(w,
2092                         wc=gui_internal_button_new_with_callback(this, _("Streets"),
2093                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2094                                 gui_internal_search_street_in_town, wm));
2095                 wc->item=wm->item;
2096                 wc->selection_id=wm->selection_id;
2097         }
2098         if (display_house_numbers) {
2099                 gui_internal_widget_append(w,
2100                         wc=gui_internal_button_new_with_callback(this, _("House numbers"),
2101                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2102                                 gui_internal_search_house_number_in_street, wm));
2103                 wc->item=wm->item;
2104                 wc->selection_id=wm->selection_id;
2105         }
2106         if (display_attributes) {
2107                 struct map_rect *mr;
2108                 struct item *item;
2109                 struct attr attr;
2110                 mr=map_rect_new(wm->item.map, NULL);
2111                 item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
2112                 if (item) {
2113                         if (item_attr_get(item, attr_description, &attr))
2114                                 gui_internal_widget_append(w, gui_internal_label_new(this, attr.u.str));
2115                         if (item_attr_get(item, attr_url_local, &attr)) {
2116                                 gui_internal_widget_append(w,
2117                                         wb=gui_internal_button_new_with_callback(this, _("View in Browser"),
2118                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2119                                                 gui_internal_cmd_view_in_browser, NULL));
2120                                 wb->item=wm->item;
2121                         }
2122                         gui_internal_widget_append(w,
2123                                 wb=gui_internal_button_new_with_callback(this, _("View Attributes"),
2124                                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2125                                         gui_internal_cmd_view_attributes, NULL));
2126                         wb->item=wm->item;
2127                 }
2128                 map_rect_destroy(mr);
2129         }
2130         gui_internal_widget_append(w,
2131                 gui_internal_button_new_with_callback(this, _("Set as destination"),
2132                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2133                         gui_internal_cmd_set_destination, wm));
2134         gui_internal_widget_append(w,
2135                 gui_internal_button_new_with_callback(this, _("Set as position"),
2136                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2137                         gui_internal_cmd_set_position, wm));
2138         gui_internal_widget_append(w,
2139                 wbc=gui_internal_button_new_with_callback(this, _("Add as bookmark"),
2140                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2141                         gui_internal_cmd_add_bookmark, wm));
2142         wbc->c=wm->c;
2143         gui_internal_widget_append(w,
2144                 wbc=gui_internal_button_new_with_callback(this, _("POIs"),
2145                         image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2146                         gui_internal_cmd_pois, NULL));
2147         wbc->c=wm->c;
2148 #if 0
2149         gui_internal_widget_append(w,
2150                 gui_internal_button_new(this, "Add to tour",
2151                         image_new_o(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill));
2152         gui_internal_widget_append(w,
2153                 gui_internal_button_new(this, "Add as bookmark",
2154                         image_new_o(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill));
2155 #endif
2156         if (display_view_on_map) {
2157                 gui_internal_widget_append(w,
2158                         gui_internal_button_new_with_callback(this, _("View on map"),
2159                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2160                                 gui_internal_cmd_view_on_map, wm));
2161         }
2162         if (display_items) {
2163                 int dist=10;
2164                 struct mapset *ms;
2165                 struct mapset_handle *h;
2166                 struct map_rect *mr;
2167                 struct map *m;
2168                 struct item *item;
2169                 struct street_data *data;
2170                 struct map_selection sel;
2171                 struct transformation *trans;
2172                 enum projection pro;
2173                 struct attr attr;
2174                 char *label,*text;
2175
2176                 trans=navit_get_trans(this->nav);
2177                 pro=transform_get_projection(trans);
2178                 transform_from_geo(pro, &g, &c);
2179                 ms=navit_get_mapset(this->nav);
2180                 sel.next=NULL;
2181                 sel.u.c_rect.lu.x=c.x-dist;
2182                 sel.u.c_rect.lu.y=c.y+dist;
2183                 sel.u.c_rect.rl.x=c.x+dist;
2184                 sel.u.c_rect.rl.y=c.y-dist;
2185                 sel.order=18;
2186                 sel.range=item_range_all;
2187                 h=mapset_open(ms);
2188                 while ((m=mapset_next(h,1))) {
2189                         mr=map_rect_new(m, &sel);
2190                         if (! mr)
2191                                 continue;
2192                         while ((item=map_rect_get_item(mr))) {
2193                                 data=street_get_data(item);
2194                                 if (transform_within_dist_item(&c, item->type, data->c, data->count, dist)) {
2195                                         if (item_attr_get(item, attr_label, &attr)) {
2196                                                 label=map_convert_string(m, attr.u.str);
2197                                                 text=g_strdup_printf("%s %s", item_to_name(item->type), label);
2198                                                 map_convert_free(label);
2199                                         } else
2200                                                 text=g_strdup_printf("%s", item_to_name(item->type));
2201                                         gui_internal_widget_append(w,
2202                                                 wc=gui_internal_button_new_with_callback(this, text,
2203                                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
2204                                                 gui_internal_cmd_position, (void *)2));
2205                                         wc->c.x=data->c[0].x;
2206                                         wc->c.y=data->c[0].y;
2207                                         wc->c.pro=pro;
2208                                         wc->name=g_strdup(text);
2209                                         wc->item=*item;
2210                                         g_free(text);
2211                                 }
2212                                 street_data_free(data);
2213                         }
2214                         map_rect_destroy(mr);
2215                 }
2216                 mapset_close(h);
2217         }
2218
2219         gui_internal_menu_render(this);
2220 }
2221
2222 static void
2223 gui_internal_cmd_bookmarks(struct gui_priv *this, struct widget *wm, void *data)
2224 {
2225         struct attr attr,mattr;
2226         struct map_rect *mr=NULL;
2227         struct item *item;
2228         char *label_full,*l,*prefix,*pos;
2229         int len,plen,hassub;
2230         struct widget *wb,*w,*wbm;
2231         GHashTable *hash;
2232         struct coord c;
2233
2234
2235         wb=gui_internal_menu(this, wm->text ? wm->text : _("Bookmarks"));
2236         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2237         w->spy=this->spacing*3;
2238         gui_internal_widget_append(wb, w);
2239
2240         prefix=wm->prefix;
2241         if (! prefix)
2242                 prefix="";
2243         plen=strlen(prefix);
2244
2245         if(navit_get_attr(this->nav, attr_bookmark_map, &mattr, NULL) && mattr.u.map && (mr=map_rect_new(mattr.u.map, NULL))) {
2246                 hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
2247                 while ((item=map_rect_get_item(mr))) {
2248                         if (item->type != type_bookmark) continue;
2249                         if (!item_attr_get(item, attr_label, &attr)) continue;
2250                         label_full=attr.u.str;
2251                         if (!strncmp(label_full, prefix, plen)) {
2252                                 pos=strchr(label_full+plen, '/');
2253                                 if (pos) {
2254                                         hassub=1;
2255                                         len=pos-label_full;
2256                                 } else {
2257                                         hassub=0;
2258                                         len=strlen(label_full);
2259                                 }
2260                                 l=g_malloc(len-plen+1);
2261                                 strncpy(l, label_full+plen, len-plen);
2262                                 l[len-plen]='\0';
2263                                 if (!g_hash_table_lookup(hash, l)) {
2264                                         wbm=gui_internal_button_new_with_callback(this, l,
2265                                                 image_new_xs(this, hassub ? "gui_inactive" : "gui_active" ), gravity_left_center|orientation_horizontal|flags_fill,
2266                                                         hassub ? gui_internal_cmd_bookmarks : gui_internal_cmd_position, NULL);
2267                                         if (item_coord_get(item, &c, 1)) {
2268                                                 wbm->c.x=c.x;
2269                                                 wbm->c.y=c.y;
2270                                                 wbm->c.pro=map_projection(mattr.u.map);
2271                                                 wbm->name=g_strdup_printf(_("Bookmark %s"),label_full);
2272                                                 wbm->text=g_strdup(l);
2273                                                 gui_internal_widget_append(w, wbm);
2274                                                 g_hash_table_insert(hash, g_strdup(l), (void *)1);
2275                                                 wbm->prefix=g_malloc(len+2);
2276                                                 strncpy(wbm->prefix, label_full, len+1);
2277                                                 wbm->prefix[len+1]='\0';
2278                                         } else {
2279                                                 gui_internal_widget_destroy(this, wbm);
2280                                         }
2281                                 }
2282                                 g_free(l);
2283                         }
2284
2285                 }
2286                 g_hash_table_destroy(hash);
2287         }
2288         gui_internal_menu_render(this);
2289 }
2290
2291 static void gui_internal_keypress_do(struct gui_priv *this, char *key)
2292 {
2293         struct widget *wi,*menu;
2294         int len=0;
2295         char *text=NULL;
2296
2297         menu=g_list_last(this->root.children)->data;
2298         wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
2299         if (wi) {
2300                 if (*key == NAVIT_KEY_BACKSPACE) {
2301                         dbg(0,"backspace\n");
2302                         if (wi->text && wi->text[0]) {
2303                                 len=g_utf8_prev_char(wi->text+strlen(wi->text))-wi->text;
2304                                 wi->text[len]=' ';
2305                                 text=g_strdup_printf("%s ", wi->text);
2306                         }
2307                 } else {
2308                         if (wi->state & STATE_CLEAR) {
2309                                 dbg(0,"wi->state=0x%x\n", wi->state);
2310                                 g_free(wi->text);
2311                                 wi->text=NULL;
2312                                 wi->state &= ~STATE_CLEAR;
2313                                 dbg(0,"wi->state=0x%x\n", wi->state);
2314                         }
2315                         text=g_strdup_printf("%s%s", wi->text ? wi->text : "", key);
2316                 }
2317                 g_free(wi->text);
2318                 wi->text=text;
2319                 if (*key == NAVIT_KEY_BACKSPACE && wi->text) {
2320                         gui_internal_widget_render(this, wi);
2321                         wi->text[len]='\0';
2322                 }
2323                 if (wi->func) {
2324                         wi->reason=2;
2325                         wi->func(this, wi, wi->data);
2326                 }
2327                 gui_internal_widget_render(this, wi);
2328         }
2329 }
2330
2331
2332 static void
2333 gui_internal_cmd_keypress(struct gui_priv *this, struct widget *wm, void *data)
2334 {
2335         struct menu_data *md=gui_internal_menu_data(this);
2336         gui_internal_keypress_do(this, (char *) wm->data);
2337         if (md->keyboard_mode == 2)
2338                 gui_internal_keyboard_do(this, md->keyboard, 10);
2339         if (md->keyboard_mode == 26)
2340                 gui_internal_keyboard_do(this, md->keyboard, 34);
2341 }
2342
2343 static void
2344 gui_internal_search_idle_end(struct gui_priv *this)
2345 {
2346         if (this->idle) {
2347                 event_remove_idle(this->idle);
2348                 this->idle=NULL;
2349         }
2350         if (this->idle_cb) {
2351                 callback_destroy(this->idle_cb);
2352                 this->idle_cb=NULL;
2353         }
2354 }
2355
2356 static void
2357 gui_internal_search_idle(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
2358 {
2359         char *text=NULL,*name=NULL;
2360         struct search_list_result *res;
2361         struct widget *wc;
2362         struct item *item=NULL;
2363         GList *l;
2364         static char possible_keys[256]="";
2365
2366         res=search_list_get_result(this->sl);
2367         if (res) {
2368                 gchar* trunk_name = NULL;
2369
2370                 struct widget *menu=g_list_last(this->root.children)->data;
2371                 struct widget *wi=gui_internal_find_widget(menu, NULL, STATE_EDIT);
2372
2373                 if (! strcmp(wm_name,"Town"))
2374                         trunk_name = g_strrstr(res->town->name, wi->text);
2375                 if (! strcmp(wm_name,"Street"))
2376                         trunk_name = g_strrstr(name=res->street->name, wi->text);
2377
2378                 if (trunk_name) {
2379                         char next_char = trunk_name[strlen(wi->text)];
2380                         int i;
2381                         int len = strlen(possible_keys);
2382                         for(i = 0; (i<len) && (possible_keys[i] != next_char) ;i++) ;
2383                         if (i==len || !len) {
2384                                 possible_keys[len]=trunk_name[strlen(wi->text)];
2385                                 possible_keys[len+1]='\0';
2386
2387                         }
2388                         dbg(1,"%s %s possible_keys:%s \n", wi->text, res->town->name, possible_keys);
2389                 }
2390         }
2391
2392         if (! res) {
2393                 gui_internal_search_idle_end(this);
2394
2395                 struct menu_data *md=gui_internal_menu_data(this);
2396                 if (md && md->keyboard) {
2397                         GList *lk=md->keyboard->children;
2398                         graphics_draw_mode(this->gra, draw_mode_begin);
2399                         while (lk) {
2400                                 struct widget *child=lk->data;
2401                                 GList *lk2=child->children;
2402                                 while (lk2) {
2403                                         struct widget *child_=lk2->data;
2404                                         lk2=g_list_next(lk2);
2405                                         if (child_->data && strcmp("\b", child_->data)) { // FIXME don't disable special keys
2406                                                 if (strlen(possible_keys) == 0)
2407                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
2408                                                 else if (g_strrstr(possible_keys, child_->data)!=NULL ) {
2409                                                         child_->state|= STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SENSITIVE|STATE_CLEAR ;
2410                                                 } else {
2411                                                         child_->state&= ~(STATE_HIGHLIGHTED|STATE_VISIBLE|STATE_SELECTED) ;
2412                                                 }
2413                                                 gui_internal_widget_render(this,child_);
2414                                         }
2415                                 }
2416                                 lk=g_list_next(lk);
2417                         }
2418                         gui_internal_widget_render(this,md->keyboard);
2419                         graphics_draw_mode(this->gra, draw_mode_end);
2420                 }
2421
2422                 possible_keys[0]='\0';
2423                 return;
2424         }
2425
2426         if (! strcmp(wm_name,"Country")) {
2427                 name=res->country->name;
2428                 item=&res->country->common.item;
2429                 text=g_strdup_printf("%s", res->country->name);
2430         }
2431         if (! strcmp(wm_name,"Town")) {
2432                 name=res->town->name;
2433                 item=&res->town->common.item;
2434                 if (res->town->name && res->town->district)
2435                         text=g_strdup_printf("%s%s%s (%s)", res->town->common.postal_mask ? res->town->common.postal_mask : "", res->town->common.postal_mask ? " ":"", res->town->name, res->town->district);
2436                 else
2437                         text=g_strdup_printf("%s%s%s", res->town->common.postal ? res->town->common.postal_mask : "", res->town->common.postal_mask ? " ":"", res->town->name);
2438         }
2439         if (! strcmp(wm_name,"Street")) {
2440                 name=res->street->name;
2441                 item=&res->street->common.item;
2442                 text=g_strdup_printf("%s %s", res->town->name, res->street->name);
2443         }
2444         dbg(1,"res->country->flag=%s\n", res->country->flag);
2445                 gui_internal_widget_append(search_list,
2446                                 wc=gui_internal_button_new_with_callback(this, text,
2447                                         image_new_xs(this, res->country->flag),
2448                                         gravity_left_center|orientation_horizontal|flags_fill,
2449                                         gui_internal_cmd_position, param));
2450                 wc->name=g_strdup(name);
2451                 if (res->c)
2452                         wc->c=*res->c;
2453                 wc->selection_id=res->id;
2454                 if (item)
2455                         wc->item=*item;
2456                 gui_internal_widget_pack(this, search_list);
2457                 l=g_list_last(this->root.children);
2458                 graphics_draw_mode(this->gra, draw_mode_begin);
2459                 gui_internal_widget_render(this, l->data);
2460                 graphics_draw_mode(this->gra, draw_mode_end);
2461         g_free(text);
2462 }
2463
2464 static void
2465 gui_internal_search_idle_start(struct gui_priv *this, char *wm_name, struct widget *search_list, void *param)
2466 {
2467         this->idle_cb=callback_new_4(callback_cast(gui_internal_search_idle), this, wm_name, search_list, param);
2468         this->idle=event_add_idle(50,this->idle_cb);
2469         callback_call_0(this->idle_cb);
2470 }
2471
2472
2473 /**
2474  *
2475  * @param wm The widget that generated the event for the search changed,
2476  *        if this was generated by a key on the virtual keyboard then
2477  *        wm is the key button widget.
2478  */
2479 static void
2480 gui_internal_search_changed(struct gui_priv *this, struct widget *wm, void *data)
2481 {
2482         GList *l;
2483         struct widget *search_list=gui_internal_menu_data(this)->search_list;
2484         gui_internal_widget_children_destroy(this, search_list);
2485
2486         void *param=(void *)3;
2487         int minlen=1;
2488         if (! strcmp(wm->name,"Country")) {
2489                 param=(void *)4;
2490                 minlen=1;
2491         }
2492         dbg(0,"%s now '%s'\n", wm->name, wm->text);
2493
2494         gui_internal_search_idle_end(this);
2495         if (wm->text && g_utf8_strlen(wm->text, -1) >= minlen) {
2496                 struct attr search_attr;
2497
2498                 dbg(0,"process\n");
2499                 if (! strcmp(wm->name,"Country"))
2500                         search_attr.type=attr_country_all;
2501                 if (! strcmp(wm->name,"Town"))
2502                         search_attr.type=attr_town_or_district_name;
2503                 if (! strcmp(wm->name,"Street"))
2504                         search_attr.type=attr_street_name;
2505                 if (! strcmp(wm->name,"House number"))
2506                         search_attr.type=attr_house_number;
2507                 search_attr.u.str=wm->text;
2508                 search_list_search(this->sl, &search_attr, 1);
2509                 gui_internal_search_idle_start(this, wm->name, search_list, param);
2510         }
2511         l=g_list_last(this->root.children);
2512         gui_internal_widget_render(this, l->data);
2513 }
2514
2515 static struct widget *
2516 gui_internal_keyboard_key_data(struct gui_priv *this, struct widget *wkbd, char *text, void(*func)(struct gui_priv *priv, struct widget *widget, void *data), void *data, void (*data_free)(void *data), int w, int h)
2517 {
2518         struct widget *wk;
2519         gui_internal_widget_append(wkbd, wk=gui_internal_button_new_with_callback(this, text,
2520                 NULL, gravity_center|orientation_vertical, func, data));
2521         wk->data_free=data_free;
2522         wk->background=this->background;
2523         wk->bl=w/2;
2524         wk->br=0;
2525         wk->bt=h/3;
2526         wk->bb=0;
2527         return wk;
2528 }
2529
2530 static struct widget *
2531 gui_internal_keyboard_key(struct gui_priv *this, struct widget *wkbd, char *text, char *key, int w, int h)
2532 {
2533         return gui_internal_keyboard_key_data(this, wkbd, text, gui_internal_cmd_keypress, g_strdup(key), g_free,w,h);
2534 }
2535
2536 static void gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data);
2537
2538 // Some macros that make the keyboard layout easier to visualise in
2539 // the source code. The macros are #undef'd after this function.
2540 #define KEY(x) gui_internal_keyboard_key(this, wkbd, (x), (x), max_w, max_h)
2541 #define SPACER() gui_internal_keyboard_key_data(this, wkbd, "", NULL, NULL, NULL,max_w,max_h)
2542 static struct widget *
2543 gui_internal_keyboard_do(struct gui_priv *this, struct widget *wkbdb, int mode)
2544 {
2545         struct widget *wkbd,*wk;
2546         struct menu_data *md=gui_internal_menu_data(this);
2547         int i, max_w=this->root.w, max_h=this->root.h;
2548         int render=0;
2549
2550         if (wkbdb) {
2551                 this->current.x=-1;
2552                 this->current.y=-1;
2553                 gui_internal_highlight(this);
2554                 render=1;
2555                 gui_internal_widget_children_destroy(this, wkbdb);
2556         } else
2557                 wkbdb=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
2558         md->keyboard=wkbdb;
2559         md->keyboard_mode=mode;
2560         wkbd=gui_internal_box_new(this, gravity_center|orientation_horizontal_vertical|flags_fill);
2561         wkbd->background=this->background;
2562         wkbd->cols=8;
2563         wkbd->spx=3;
2564         wkbd->spy=3;
2565         max_w=max_w/9;
2566         max_h=max_h/6;
2567
2568         if (mode >= 0 && mode < 8) {
2569                 for (i = 0 ; i < 26 ; i++) {
2570                         char text[]={'A'+i,'\0'};
2571                         KEY(text);
2572                 }
2573                 gui_internal_keyboard_key(this, wkbd, "_"," ",max_w,max_h);
2574                 if (mode == 0) {
2575                         KEY("-");
2576                         KEY("'");
2577                         SPACER();
2578                 } else {
2579                         SPACER();
2580                         wk=gui_internal_keyboard_key_data(this, wkbd, "a", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2581                         wk->datai=mode+8;
2582                         wk=gui_internal_keyboard_key_data(this, wkbd, "1", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2583                         wk->datai=mode+16;
2584                 }
2585                 wk=gui_internal_keyboard_key_data(this, wkbd, "Ä",gui_internal_keyboard_change, wkbdb,NULL,max_w,max_h);
2586                 wk->datai=mode+24;
2587                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2588         }
2589         if (mode >= 8 && mode < 16) {
2590                 for (i = 0 ; i < 26 ; i++) {
2591                         char text[]={'a'+i,'\0'};
2592                         KEY(text);
2593                 }
2594                 gui_internal_keyboard_key(this, wkbd, "_"," ",max_w,max_h);
2595                 if (mode == 8) {
2596                         KEY("-");
2597                         KEY("'");
2598                         SPACER();
2599                 } else {
2600                         SPACER();
2601                         wk=gui_internal_keyboard_key_data(this, wkbd, "A", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2602                         wk->datai=mode-8;
2603                         wk=gui_internal_keyboard_key_data(this, wkbd, "1", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2604                         wk->datai=mode+8;
2605                 }
2606                 wk=gui_internal_keyboard_key_data(this, wkbd, "ä",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2607                 wk->datai=mode+24;
2608                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2609         }
2610         if (mode >= 16 && mode < 24) {
2611                 for (i = 0 ; i < 10 ; i++) {
2612                         char text[]={'0'+i,'\0'};
2613                         KEY(text);
2614                 }
2615                 KEY("."); KEY("°"); KEY("'"); KEY("\""); KEY("-"); KEY("+");
2616                 KEY("*"); KEY("/"); KEY("("); KEY(")"); KEY("="); KEY("?");
2617
2618                 for (i = 0 ; i < 5 ; i++) SPACER();
2619
2620                 if (mode == 16) {
2621                         KEY("-");
2622                         KEY("'");
2623                         SPACER();
2624                 } else {
2625                         SPACER();
2626                         wk=gui_internal_keyboard_key_data(this, wkbd, "A", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2627                         wk->datai=mode-16;
2628                         wk=gui_internal_keyboard_key_data(this, wkbd, "a", gui_internal_keyboard_change, wkbd, NULL,max_w,max_h);
2629                         wk->datai=mode-8;
2630                 }
2631                 wk=gui_internal_keyboard_key_data(this, wkbd, "Ä",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2632                 wk->datai=mode+8;
2633                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2634         }
2635         if (mode >= 24 && mode < 32) {
2636                 KEY("Ä"); KEY("Ë"); KEY("Ï"); KEY("Ö"); KEY("Ü"); KEY("Æ"); KEY("Ø"); KEY("Å");
2637                 KEY("Á"); KEY("É"); KEY("Í"); KEY("Ó"); KEY("Ú"); KEY("Š"); KEY("Č"); KEY("Ž");
2638                 KEY("À"); KEY("È"); KEY("Ì"); KEY("Ò"); KEY("Ù"); KEY("Ś"); KEY("Ć"); KEY("Ź");
2639                 KEY("Â"); KEY("Ê"); KEY("Î"); KEY("Ô"); KEY("Û"); SPACER();
2640
2641                 wk=gui_internal_keyboard_key_data(this, wkbd, "A",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2642                 wk->datai=mode-24;
2643
2644                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2645         }
2646         if (mode >= 32 && mode < 40) {
2647                 KEY("ä"); KEY("ë"); KEY("ï"); KEY("ö"); KEY("ü"); KEY("æ"); KEY("ø"); KEY("å");
2648                 KEY("á"); KEY("é"); KEY("í"); KEY("ó"); KEY("ú"); KEY("š"); KEY("č"); KEY("ž");
2649                 KEY("à"); KEY("è"); KEY("ì"); KEY("ò"); KEY("ù"); KEY("ś"); KEY("ć"); KEY("ź");
2650                 KEY("â"); KEY("ê"); KEY("î"); KEY("ô"); KEY("û"); KEY("ß");
2651
2652                 wk=gui_internal_keyboard_key_data(this, wkbd, "a",gui_internal_keyboard_change,wkbdb,NULL,max_w,max_h);
2653                 wk->datai=mode-24;
2654
2655                 gui_internal_keyboard_key(this, wkbd, "<-","\b",max_w,max_h);
2656         }
2657         gui_internal_widget_append(wkbdb, wkbd);
2658         if (render) {
2659                 gui_internal_widget_pack(this, wkbdb);
2660                 gui_internal_widget_render(this, wkbdb);
2661         }
2662         return wkbdb;
2663 }
2664 #undef KEY
2665 #undef SPACER
2666
2667 static struct widget *
2668 gui_internal_keyboard(struct gui_priv *this, int mode)
2669 {
2670         if (! this->keyboard)
2671                 return NULL;
2672         return gui_internal_keyboard_do(this, NULL, mode);
2673 }
2674
2675 static void
2676 gui_internal_keyboard_change(struct gui_priv *this, struct widget *key, void *data)
2677 {
2678         gui_internal_keyboard_do(this, key->data, key->datai);
2679 }
2680
2681 static void
2682 gui_internal_search_list_set_default_country(struct gui_priv *this)
2683 {
2684         struct attr search_attr, country_name, country_iso2, *country_attr;
2685         struct item *item;
2686         struct country_search *cs;
2687         struct tracking *tracking;
2688         struct search_list_result *res;
2689
2690         country_attr=country_default();
2691         tracking=navit_get_tracking(this->nav);
2692         if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
2693                 country_attr=&search_attr;
2694         if (country_attr) {
2695                 cs=country_search_new(country_attr, 0);
2696                 item=country_search_get_item(cs);
2697                 if (item && item_attr_get(item, attr_country_name, &country_name)) {
2698                         search_attr.type=attr_country_all;
2699                         dbg(0,"country %s\n", country_name.u.str);
2700                         search_attr.u.str=country_name.u.str;
2701                         search_list_search(this->sl, &search_attr, 0);
2702                         while((res=search_list_get_result(this->sl)));
2703                         g_free(this->country_iso2);
2704                         if (item_attr_get(item, attr_country_iso2, &country_iso2))
2705                                 this->country_iso2=g_strdup(country_iso2.u.str);
2706                 }
2707                 country_search_destroy(cs);
2708         } else {
2709                 dbg(0,"warning: no default country found\n");
2710                 if (this->country_iso2) {
2711                     dbg(0,"attempting to use country '%s'\n",this->country_iso2);
2712                     search_attr.type=attr_country_iso2;
2713                     search_attr.u.str=this->country_iso2;
2714             search_list_search(this->sl, &search_attr, 0);
2715             while((res=search_list_get_result(this->sl)));
2716                 }
2717         }
2718 }
2719
2720 static void
2721 gui_internal_search_list_new(struct gui_priv *this)
2722 {
2723         struct mapset *ms=navit_get_mapset(this->nav);
2724         if (! this->sl) {
2725                 this->sl=search_list_new(ms);
2726                 gui_internal_search_list_set_default_country(this);
2727         }
2728 }
2729
2730 static void
2731 gui_internal_search_list_destroy(struct gui_priv *this)
2732 {
2733         if (this->sl) {
2734                 search_list_destroy(this->sl);
2735                 this->sl=NULL;
2736         }
2737 }
2738
2739
2740 static void
2741 gui_internal_search(struct gui_priv *this, char *what, char *type, int flags)
2742 {
2743         struct widget *wb,*wk,*w,*wr,*we,*wl,*wnext=NULL;
2744         char *country;
2745         gui_internal_search_list_new(this);
2746         wb=gui_internal_menu(this, what);
2747         w=gui_internal_box_new(this, gravity_center|orientation_vertical|flags_expand|flags_fill);
2748         gui_internal_widget_append(wb, w);
2749         wr=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2750         gui_internal_widget_append(w, wr);
2751         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
2752         gui_internal_widget_append(wr, we);
2753
2754         if (!strcmp(type,"Country")) {
2755                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_town"));
2756                 wnext->func=gui_internal_search_town;
2757         } else if (!strcmp(type,"Town")) {
2758                 if (this->country_iso2) {
2759 #if HAVE_API_ANDROID
2760                         char country_iso2[strlen(this->country_iso2)+1];
2761                         strtolower(country_iso2, this->country_iso2);
2762                         country=g_strdup_printf("country_%s", country_iso2);
2763 #else
2764                         country=g_strdup_printf("country_%s", this->country_iso2);
2765 #endif
2766                 } else
2767                         country=strdup("gui_select_country");
2768                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, country)));
2769                 wb->state |= STATE_SENSITIVE;
2770                 if (flags)
2771                         wb->func = gui_internal_search_country;
2772                 else
2773                         wb->func = gui_internal_back;
2774                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_street"));
2775                 wnext->func=gui_internal_search_street;
2776                 g_free(country);
2777         } else if (!strcmp(type,"Street")) {
2778                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_town")));
2779                 wb->state |= STATE_SENSITIVE;
2780                 wb->func = gui_internal_back;
2781                 wnext=gui_internal_image_new(this, image_new_xs(this, "gui_select_house_number"));
2782                 wnext->func=gui_internal_search_house_number;
2783         } else if (!strcmp(type,"House number")) {
2784                 gui_internal_widget_append(we, wb=gui_internal_image_new(this, image_new_xs(this, "gui_select_street")));
2785                 wb->state |= STATE_SENSITIVE;
2786                 wb->func = gui_internal_back;
2787         }
2788         gui_internal_widget_append(we, wk=gui_internal_label_new(this, NULL));
2789         if (wnext) {
2790                 gui_internal_widget_append(we, wnext);
2791                 wnext->state |= STATE_SENSITIVE;
2792         }
2793         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
2794         gui_internal_widget_append(wr, wl);
2795         gui_internal_menu_data(this)->search_list=wl;
2796         wk->state |= STATE_EDIT;
2797         wk->background=this->background;
2798         wk->flags |= flags_expand|flags_fill;
2799         wk->func = gui_internal_search_changed;
2800         wk->name=g_strdup(type);
2801         if (this->keyboard)
2802                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
2803         gui_internal_menu_render(this);
2804 }
2805
2806 static void
2807 gui_internal_search_house_number(struct gui_priv *this, struct widget *widget, void *data)
2808 {
2809         search_list_select(this->sl, attr_street_name, 0, 0);
2810         gui_internal_search(this,_("House number"),"House number",0);
2811 }
2812
2813 static void
2814 gui_internal_search_house_number_in_street(struct gui_priv *this, struct widget *widget, void *data)
2815 {
2816         dbg(0,"id %d\n", widget->selection_id);
2817         search_list_select(this->sl, attr_street_name, 0, 0);
2818         search_list_select(this->sl, attr_street_name, widget->selection_id, 1);
2819         gui_internal_search(this,_("House number"),"House number",0);
2820 }
2821
2822 static void
2823 gui_internal_search_street(struct gui_priv *this, struct widget *widget, void *data)
2824 {
2825         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
2826         gui_internal_search(this,_("Street"),"Street",0);
2827 }
2828
2829 static void
2830 gui_internal_search_street_in_town(struct gui_priv *this, struct widget *widget, void *data)
2831 {
2832         dbg(0,"id %d\n", widget->selection_id);
2833         search_list_select(this->sl, attr_town_or_district_name, 0, 0);
2834         search_list_select(this->sl, attr_town_or_district_name, widget->selection_id, 1);
2835         gui_internal_search(this,_("Street"),"Street",0);
2836 }
2837
2838 static void
2839 gui_internal_search_town(struct gui_priv *this, struct widget *wm, void *data)
2840 {
2841         if (this->sl)
2842                 search_list_select(this->sl, attr_country_all, 0, 0);
2843         g_free(this->country_iso2);
2844         this->country_iso2=NULL;
2845         gui_internal_search(this,_("Town"),"Town",0);
2846 }
2847
2848 static void
2849 gui_internal_search_town_in_country(struct gui_priv *this, struct widget *widget)
2850 {
2851         struct search_list_common *slc;
2852         dbg(0,"id %d\n", widget->selection_id);
2853         search_list_select(this->sl, attr_country_all, 0, 0);
2854         slc=search_list_select(this->sl, attr_country_all, widget->selection_id, 1);
2855         if (slc) {
2856                 g_free(this->country_iso2);
2857                 this->country_iso2=((struct search_list_country *)slc)->iso2;
2858         }
2859         gui_internal_search(this,widget->name,"Town",0);
2860 }
2861
2862 static void
2863 gui_internal_search_country(struct gui_priv *this, struct widget *widget, void *data)
2864 {
2865         gui_internal_prune_menu_count(this, 1, 0);
2866         gui_internal_search(this,_("Country"),"Country",0);
2867 }
2868
2869 static void
2870 gui_internal_cmd_town(struct gui_priv *this, struct widget *wm, void *data)
2871 {
2872         if (this->sl)
2873                 search_list_select(this->sl, attr_country_all, 0, 0);
2874         gui_internal_search(this,_("Town"),"Town",1);
2875 }
2876
2877 static void
2878 gui_internal_cmd_layout(struct gui_priv *this, struct widget *wm, void *data)
2879 {
2880         struct attr attr;
2881         struct widget *w,*wb,*wl;
2882         struct attr_iter *iter;
2883
2884
2885         wb=gui_internal_menu(this, _("Layout"));
2886         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
2887         w->spy=this->spacing*3;
2888         gui_internal_widget_append(wb, w);
2889         iter=navit_attr_iter_new();
2890         while(navit_get_attr(this->nav, attr_layout, &attr, iter)) {
2891                 wl=gui_internal_button_navit_attr_new(this, attr.u.layout->name, gravity_left_center|orientation_horizontal|flags_fill,
2892                         &attr, NULL);
2893                 gui_internal_widget_append(w, wl);
2894         }
2895         navit_attr_iter_destroy(iter);
2896         gui_internal_menu_render(this);
2897 }
2898
2899 static void
2900 gui_internal_cmd_fullscreen(struct gui_priv *this, struct widget *wm, void *data)
2901 {
2902         graphics_draw_mode(this->gra, draw_mode_end);
2903         if (this->fullscreen != 2)
2904                 this->fullscreen=!this->fullscreen;
2905         this->win->fullscreen(this->win, this->fullscreen != 0);
2906         graphics_draw_mode(this->gra, draw_mode_begin);
2907 }
2908
2909 static void
2910 gui_internal_cmd_2d(struct gui_priv *this, struct widget *wm, void *data)
2911 {
2912         struct transformation *trans=navit_get_trans(this->nav);
2913         transform_set_pitch(trans, 0);
2914         this->redraw=1;
2915         gui_internal_prune_menu(this, NULL);
2916 }
2917
2918 static void
2919 gui_internal_cmd_3d(struct gui_priv *this, struct widget *wm, void *data)
2920 {
2921         struct transformation *trans=navit_get_trans(this->nav);
2922         transform_set_pitch(trans, this->pitch);
2923         this->redraw=1;
2924         gui_internal_prune_menu(this, NULL);
2925 }
2926
2927 static void
2928 gui_internal_cmd_display(struct gui_priv *this, struct widget *wm, void *data)
2929 {
2930         struct widget *w;
2931         struct transformation *trans;
2932
2933         w=gui_internal_menu(this, _("Display"));
2934         gui_internal_widget_append(w,
2935                 gui_internal_button_new_with_callback(this, _("Layout"),
2936                         image_new_l(this, "gui_display"), gravity_center|orientation_vertical,
2937                         gui_internal_cmd_layout, NULL));
2938
2939         if(this->fullscreen != 2) {
2940                 if (this->fullscreen) {
2941                         gui_internal_widget_append(w,
2942                                         gui_internal_button_new_with_callback(this, _("Window Mode"),
2943                                                 image_new_l(this, "gui_leave_fullscreen"), gravity_center|orientation_vertical,
2944                                                 gui_internal_cmd_fullscreen, NULL));
2945                 } else {
2946                         gui_internal_widget_append(w,
2947                                         gui_internal_button_new_with_callback(this, _("Fullscreen"),
2948                                                 image_new_l(this, "gui_fullscreen"), gravity_center|orientation_vertical,
2949                                                 gui_internal_cmd_fullscreen, NULL));
2950                 }
2951         }
2952         trans=navit_get_trans(this->nav);
2953         if (transform_get_pitch(trans)) {
2954                 gui_internal_widget_append(w,
2955                         gui_internal_button_new_with_callback(this, _("2D"),
2956                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
2957                                 gui_internal_cmd_2d, NULL));
2958
2959         } else {
2960                 gui_internal_widget_append(w,
2961                         gui_internal_button_new_with_callback(this, _("3D"),
2962                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
2963                                 gui_internal_cmd_3d, NULL));
2964         }
2965         gui_internal_menu_render(this);
2966 }
2967
2968 static void
2969 gui_internal_cmd_quit(struct gui_priv *this, struct widget *wm, void *data)
2970 {
2971         struct navit *nav=this->nav;
2972         navit_destroy(nav);
2973         main_remove_navit(nav);
2974 }
2975
2976 static void
2977 gui_internal_cmd_abort_navigation(struct gui_priv *this, struct widget *wm, void *data)
2978 {
2979         navit_set_destination(this->nav, NULL, NULL, 0);
2980 }
2981
2982
2983 static void
2984 gui_internal_cmd_actions(struct gui_priv *this, struct widget *wm, void *data)
2985 {
2986         struct widget *w,*wc;
2987         char *coord;
2988
2989         w=gui_internal_menu(this, _("Actions"));
2990         gui_internal_widget_append(w,
2991                 gui_internal_button_new_with_callback(this, _("Bookmarks"),
2992                         image_new_l(this, "gui_bookmark"), gravity_center|orientation_vertical,
2993                         gui_internal_cmd_bookmarks, NULL));
2994         if (this->clickp_valid) {
2995                 coord=coordinates(&this->clickp, '\n');
2996                 gui_internal_widget_append(w,
2997                         wc=gui_internal_button_new_with_callback(this, coord,
2998                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
2999                                 gui_internal_cmd_position, (void *)1));
3000                 wc->name=g_strdup(_("Map Point"));
3001                 wc->c=this->clickp;
3002                 g_free(coord);
3003         }
3004         if (this->vehicle_valid) {
3005                 coord=coordinates(&this->vehiclep, '\n');
3006                 gui_internal_widget_append(w,
3007                         wc=gui_internal_button_new_with_callback(this, coord,
3008                                 image_new_l(this, "gui_vehicle"), gravity_center|orientation_vertical,
3009                                 gui_internal_cmd_position, NULL));
3010                 wc->name=g_strdup(_("Vehicle Position"));
3011                 wc->c=this->vehiclep;
3012                 g_free(coord);
3013         }
3014         gui_internal_widget_append(w,
3015                 gui_internal_button_new_with_callback(this, _("Town"),
3016                         image_new_l(this, "gui_town"), gravity_center|orientation_vertical,
3017                         gui_internal_cmd_town, NULL));
3018         gui_internal_widget_append(w,
3019                 gui_internal_button_new_with_callback(this, _("Quit"),
3020                         image_new_l(this, "gui_quit"), gravity_center|orientation_vertical,
3021                         gui_internal_cmd_quit, NULL));
3022
3023         if (navit_check_route(this->nav)) {
3024                 gui_internal_widget_append(w,
3025                                                                    gui_internal_button_new_with_callback(this, _("Stop\nNavigation"),
3026                                                                  image_new_l(this, "gui_stop"), gravity_center|orientation_vertical,
3027                                                                  gui_internal_cmd_abort_navigation, NULL));
3028         }
3029         gui_internal_menu_render(this);
3030 }
3031
3032 static void
3033 gui_internal_cmd_maps(struct gui_priv *this, struct widget *wm, void *wdata)
3034 {
3035         struct attr attr, on, off, description, type, data;
3036         struct widget *w,*wb,*wma;
3037         char *label;
3038         struct attr_iter *iter;
3039
3040
3041         wb=gui_internal_menu(this, _("Maps"));
3042         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3043         w->spy=this->spacing*3;
3044         gui_internal_widget_append(wb, w);
3045         iter=navit_attr_iter_new();
3046         on.type=off.type=attr_active;
3047         on.u.num=1;
3048         off.u.num=0;
3049         while(navit_get_attr(this->nav, attr_map, &attr, iter)) {
3050                 if (map_get_attr(attr.u.map, attr_description, &description, NULL)) {
3051                         label=g_strdup(description.u.str);
3052                 } else {
3053                         if (!map_get_attr(attr.u.map, attr_type, &type, NULL))
3054                                 type.u.str="";
3055                         if (!map_get_attr(attr.u.map, attr_data, &data, NULL))
3056                                 data.u.str="";
3057                         label=g_strdup_printf("%s:%s", type.u.str, data.u.str);
3058                 }
3059                 wma=gui_internal_button_map_attr_new(this, label, gravity_left_center|orientation_horizontal|flags_fill,
3060                         attr.u.map, &on, &off, 1);
3061                 gui_internal_widget_append(w, wma);
3062                 g_free(label);
3063         }
3064         navit_attr_iter_destroy(iter);
3065         gui_internal_menu_render(this);
3066
3067 }
3068 static void
3069 gui_internal_cmd_set_active_vehicle(struct gui_priv *this, struct widget *wm, void *data)
3070 {
3071         struct attr vehicle = {attr_vehicle,{wm->data}};
3072         navit_set_attr(this->nav, &vehicle);
3073 }
3074
3075 static void
3076 gui_internal_cmd_show_satellite_status(struct gui_priv *this, struct widget *wm, void *data)
3077 {
3078         struct widget *w,*wb,*row;
3079         struct attr attr,sat_attr;
3080         struct vehicle *v=wm->data;
3081         char *str;
3082         int i;
3083         enum attr_type types[]={attr_sat_prn, attr_sat_elevation, attr_sat_azimuth, attr_sat_snr};
3084
3085         wb=gui_internal_menu(this, _("Show Satellite Status"));
3086         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_satellite_status;
3087         gui_internal_menu_data(this)->redisplay_widget=wm;
3088         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3089         gui_internal_widget_append(wb, w);
3090         w = gui_internal_widget_table_new(this,gravity_center | orientation_vertical | flags_expand | flags_fill, 0);
3091         row = gui_internal_widget_table_row_new(this,gravity_left_top);
3092         gui_internal_widget_append(row, gui_internal_label_new(this, " PRN "));
3093         gui_internal_widget_append(row, gui_internal_label_new(this, " Elevation "));
3094         gui_internal_widget_append(row, gui_internal_label_new(this, " Azimuth "));
3095         gui_internal_widget_append(row, gui_internal_label_new(this, " SNR "));
3096         gui_internal_widget_append(w,row);
3097         while (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
3098                 row = gui_internal_widget_table_row_new(this,gravity_left_top);
3099                 for (i = 0 ; i < sizeof(types)/sizeof(enum attr_type) ; i++) {
3100                         if (item_attr_get(attr.u.item, types[i], &sat_attr))
3101                                 str=g_strdup_printf("%d", sat_attr.u.num);
3102                         else
3103                                 str=g_strdup("");
3104                         gui_internal_widget_append(row, gui_internal_label_new(this, str));
3105                         g_free(str);
3106                 }
3107                 gui_internal_widget_append(w,row);
3108         }
3109         gui_internal_widget_append(wb, w);
3110         gui_internal_menu_render(this);
3111 }
3112
3113 static void
3114 gui_internal_cmd_show_nmea_data(struct gui_priv *this, struct widget *wm, void *data)
3115 {
3116         struct widget *w,*wb;
3117         struct attr attr;
3118         struct vehicle *v=wm->data;
3119         wb=gui_internal_menu(this, _("Show NMEA Data"));
3120         gui_internal_menu_data(this)->redisplay=gui_internal_cmd_show_nmea_data;
3121         gui_internal_menu_data(this)->redisplay_widget=wm;
3122         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3123         gui_internal_widget_append(wb, w);
3124         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL))
3125                 gui_internal_widget_append(w, gui_internal_text_new(this, attr.u.str, gravity_left_center|orientation_vertical));
3126         gui_internal_menu_render(this);
3127 }
3128
3129 /**
3130  * A container to hold the selected vehicle and the desired profile in
3131  * one data item.
3132  */
3133 struct vehicle_and_profilename {
3134         struct vehicle *vehicle;
3135         char *profilename;
3136 };
3137
3138 /**
3139  * Figures out whether the given vehicle is the active vehicle.
3140  *
3141  * @return true if the vehicle is active, false otherwise.
3142  */
3143 static int
3144 gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle
3145         *vehicle)
3146 {
3147         struct attr active_vehicle;
3148
3149         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
3150         active_vehicle.u.vehicle=NULL;
3151
3152         return active_vehicle.u.vehicle == vehicle;
3153 }
3154
3155 static void
3156 save_vehicle_xml(struct vehicle *v)
3157 {
3158         struct attr attr;
3159         struct attr_iter *iter=vehicle_attr_iter_new();
3160         int childs=0;
3161         dbg(0,"enter\n");
3162         printf("<vehicle");
3163         while (vehicle_get_attr(v, attr_any_xml, &attr, iter)) {
3164                 if (attr_type_begin(attr.type) == attr_type_object_begin)
3165                         childs=1;
3166                 else
3167                         printf(" %s=\"%s\"",attr_to_name(attr.type),attr_to_text(&attr, NULL, 1));
3168         }
3169         if (childs) {
3170                 printf(">\n");
3171                 printf("</vehicle>\n");
3172         } else
3173                 printf(" />\n");
3174         vehicle_attr_iter_destroy(iter);
3175 }
3176
3177
3178 /**
3179  * Reacts to a button press that changes a vehicle's active profile.
3180  *
3181  * @see gui_internal_add_vehicle_profile
3182  */
3183 static void
3184 gui_internal_cmd_set_active_profile(struct gui_priv *this, struct
3185                 widget *wm, void *data)
3186 {
3187         struct vehicle_and_profilename *vapn = data;
3188         struct vehicle *v = vapn->vehicle;
3189         char *profilename = vapn->profilename;
3190         struct attr vehicle_name_attr;
3191         char *vehicle_name = NULL;
3192
3193         // Get the vehicle name
3194         vehicle_get_attr(v, attr_name, &vehicle_name_attr, NULL);
3195         vehicle_name = vehicle_name_attr.u.str;
3196
3197         dbg(0, "Changing vehicle %s to profile %s\n", vehicle_name,
3198                         profilename);
3199
3200         // Change the profile name
3201         struct attr profilename_attr = {attr_profilename, {profilename}};
3202         if(!vehicle_set_attr(v, &profilename_attr, NULL)) {
3203                 dbg(0, "Unable to set the vehicle's profile name\n");
3204         }
3205
3206     // Notify Navit that the routing should be re-done if this is the
3207     // active vehicle.
3208     if(gui_internal_is_active_vehicle(this, v)) {
3209         struct attr vehicle = {attr_vehicle, {v}};
3210         navit_set_attr(this->nav, &vehicle);
3211     }
3212         save_vehicle_xml(v);
3213
3214                         
3215 }
3216
3217 /**
3218  * Adds the vehicle profile to the GUI, allowing the user to pick a
3219  * profile for the currently selected vehicle.
3220  */
3221 static void
3222 gui_internal_add_vehicle_profile(struct gui_priv *this, struct widget
3223                 *parent, struct vehicle *v, struct vehicleprofile *profile)
3224 {
3225         // Just here to show up in the translation file, nice and close to
3226         // where the translations are actually used.
3227         struct attr profile_attr;
3228         struct attr *attr = NULL;
3229         char *name = NULL;
3230         char *active_profile = NULL;
3231         char *label = NULL;
3232         int active;
3233         struct vehicle_and_profilename *context = NULL;
3234
3235         static char *__profile_translations[] = {
3236                 _n("car"), _n("bike"), _n("pedestrian")
3237         };
3238
3239         // Figure out the profile name
3240         attr = attr_search(profile->attrs, NULL, attr_name);
3241         name = attr->u.str;
3242
3243         // Determine whether the profile is the active one
3244         vehicle_get_attr(v, attr_profilename, &profile_attr, NULL);
3245         active_profile = profile_attr.u.str;
3246         active = strcmp(name, active_profile) == 0;
3247
3248         dbg(0, "Adding vehicle profile %s, active=%s/%i\n", name,
3249                         active_profile, active);
3250
3251         // Build a translatable label.
3252         if(active) {
3253                 label = g_strdup_printf(_("Current profile: %s"), _(name));
3254         } else {
3255                 label = g_strdup_printf(_("Change profile to: %s"), _(name));
3256         }
3257
3258         // Create the context object (the vehicle and the desired profile)
3259         context = g_new0(struct vehicle_and_profilename, 1);
3260         context->vehicle = v;
3261         context->profilename = name;
3262
3263         // Add the button
3264         gui_internal_widget_append(parent,
3265                 gui_internal_button_new_with_callback(
3266                         this, label,
3267                         image_new_xs(this, active ? "gui_active" : "gui_inactive"),
3268                         gravity_left_center|orientation_horizontal|flags_fill,
3269                         gui_internal_cmd_set_active_profile, context));
3270
3271         free(label);
3272 }
3273
3274 static void
3275 gui_internal_cmd_vehicle_settings(struct gui_priv *this, struct widget *wm, void *data)
3276 {
3277         struct widget *w,*wb;
3278         struct attr attr;
3279         struct vehicle *v=wm->data;
3280     struct vehicleprofile *profile = NULL;
3281
3282         wb=gui_internal_menu(this, wm->text);
3283         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3284         gui_internal_widget_append(wb, w);
3285
3286     // Add the "Set as active" button if this isn't the active
3287     // vehicle.
3288         if (!gui_internal_is_active_vehicle(this, v)) {
3289                 gui_internal_widget_append(w,
3290                         gui_internal_button_new_with_callback(this, _("Set as active"),
3291                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3292                                 gui_internal_cmd_set_active_vehicle, wm->data));
3293         }
3294
3295         if (vehicle_get_attr(v, attr_position_sat_item, &attr, NULL)) {
3296                 gui_internal_widget_append(w,
3297                         gui_internal_button_new_with_callback(this, _("Show Satellite status"),
3298                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3299                                 gui_internal_cmd_show_satellite_status, wm->data));
3300         }
3301         if (vehicle_get_attr(v, attr_position_nmea, &attr, NULL)) {
3302                 gui_internal_widget_append(w,
3303                         gui_internal_button_new_with_callback(this, _("Show NMEA data"),
3304                                 image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
3305                                 gui_internal_cmd_show_nmea_data, wm->data));
3306         }
3307
3308     // Add all the possible vehicle profiles to the menu
3309         GList *profiles = navit_get_vehicleprofiles(this->nav);
3310     while(profiles) {
3311         profile = (struct vehicleprofile *)profiles->data;
3312         gui_internal_add_vehicle_profile(this, w, v, profile);
3313                 profiles = g_list_next(profiles);
3314     }
3315
3316         callback_list_call_attr_2(this->cbl, attr_vehicle, w, wm->data);
3317         gui_internal_menu_render(this);
3318 }
3319
3320 static void
3321 gui_internal_cmd_vehicle(struct gui_priv *this, struct widget *wm, void *data)
3322 {
3323         struct attr attr,vattr;
3324         struct widget *w,*wb,*wl;
3325         struct attr_iter *iter;
3326         struct attr active_vehicle;
3327
3328
3329         wb=gui_internal_menu(this, _("Vehicle"));
3330         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3331         w->spy=this->spacing*3;
3332         gui_internal_widget_append(wb, w);
3333         if (!navit_get_attr(this->nav, attr_vehicle, &active_vehicle, NULL))
3334                 active_vehicle.u.vehicle=NULL;
3335         iter=navit_attr_iter_new();
3336         while(navit_get_attr(this->nav, attr_vehicle, &attr, iter)) {
3337                 vehicle_get_attr(attr.u.vehicle, attr_name, &vattr, NULL);
3338                 wl=gui_internal_button_new_with_callback(this, vattr.u.str,
3339                         image_new_l(this, attr.u.vehicle == active_vehicle.u.vehicle ? "gui_active" : "gui_inactive"), gravity_left_center|orientation_horizontal|flags_fill,
3340                         gui_internal_cmd_vehicle_settings, attr.u.vehicle);
3341                 wl->text=g_strdup(vattr.u.str);
3342                 gui_internal_widget_append(w, wl);
3343         }
3344         navit_attr_iter_destroy(iter);
3345         gui_internal_menu_render(this);
3346 }
3347
3348
3349 static void
3350 gui_internal_cmd_rules(struct gui_priv *this, struct widget *wm, void *data)
3351 {
3352         struct widget *wb,*w;
3353         struct attr on,off;
3354         wb=gui_internal_menu(this, _("Rules"));
3355         w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
3356         w->spy=this->spacing*3;
3357         gui_internal_widget_append(wb, w);
3358         on.u.num=1;
3359         off.u.num=0;
3360         on.type=off.type=attr_tracking;
3361         gui_internal_widget_append(w,
3362                 gui_internal_button_navit_attr_new(this, _("Lock on road"), gravity_left_center|orientation_horizontal|flags_fill,
3363                         &on, &off));
3364         on.u.num=0;
3365         off.u.num=-1;
3366         on.type=off.type=attr_orientation;
3367         gui_internal_widget_append(w,
3368                 gui_internal_button_navit_attr_new(this, _("Northing"), gravity_left_center|orientation_horizontal|flags_fill,
3369                         &on, &off));
3370         on.u.num=1;
3371         off.u.num=0;
3372         on.type=off.type=attr_follow_cursor;
3373         gui_internal_widget_append(w,
3374                 gui_internal_button_navit_attr_new(this, _("Map follows Vehicle"), gravity_left_center|orientation_horizontal|flags_fill,
3375                         &on, &off));
3376         gui_internal_menu_render(this);
3377 }
3378
3379 static void
3380 gui_internal_cmd_settings(struct gui_priv *this, struct widget *wm, void *data)
3381 {
3382         struct widget *w;
3383
3384         w=gui_internal_menu(this, _("Settings"));
3385         gui_internal_widget_append(w,
3386                 gui_internal_button_new_with_callback(this, _("Display"),
3387                         image_new_l(this, "gui_display"), gravity_center|orientation_vertical,
3388                         gui_internal_cmd_display, NULL));
3389         gui_internal_widget_append(w,
3390                 gui_internal_button_new_with_callback(this, _("Maps"),
3391                         image_new_l(this, "gui_maps"), gravity_center|orientation_vertical,
3392                         gui_internal_cmd_maps, NULL));
3393         gui_internal_widget_append(w,
3394                 gui_internal_button_new_with_callback(this, _("Vehicle"),
3395                         image_new_l(this, "gui_vehicle"), gravity_center|orientation_vertical,
3396                         gui_internal_cmd_vehicle, NULL));
3397         gui_internal_widget_append(w,
3398                 gui_internal_button_new_with_callback(this, _("Rules"),
3399                         image_new_l(this, "gui_rules"), gravity_center|orientation_vertical,
3400                         gui_internal_cmd_rules, NULL));
3401         gui_internal_menu_render(this);
3402 }
3403
3404 //##############################################################################################################
3405 //# Description:
3406 //# Comment:
3407 //# Authors: Martin Schaller (04/2008)
3408 //##############################################################################################################
3409 static void gui_internal_motion(void *data, struct point *p)
3410 {
3411
3412         struct gui_priv *this=data;
3413         if (!this->root.children) {
3414                 navit_handle_motion(this->nav, p);
3415                 return;
3416         }
3417         if (!this->pressed)
3418                 return;
3419         this->current=*p;
3420         if(!this->motion_timeout_callback)
3421                 this->motion_timeout_callback=callback_new_1(callback_cast(gui_internal_highlight), this);
3422         if(!this->motion_timeout_event)
3423                 this->motion_timeout_event=event_add_timeout(100,0, this->motion_timeout_callback);
3424 }
3425
3426
3427 static void gui_internal_menu_root(struct gui_priv *this)
3428 {
3429         struct widget *w;
3430
3431         graphics_draw_mode(this->gra, draw_mode_begin);
3432         w=gui_internal_menu(this, _("Main menu"));
3433         w->spx=this->spacing*10;
3434         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Actions"),
3435                         image_new_l(this, "gui_actions"), gravity_center|orientation_vertical,
3436                         gui_internal_cmd_actions, NULL));
3437         if (this->flags & 2) {
3438                 gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Show\nMap"),
3439                                 image_new_l(this, "gui_map"), gravity_center|orientation_vertical,
3440                                 gui_internal_cmd_settings, NULL));
3441         }
3442         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Settings"),
3443                         image_new_l(this, "gui_settings"), gravity_center|orientation_vertical,
3444                         gui_internal_cmd_settings, NULL));
3445         gui_internal_widget_append(w, gui_internal_button_new(this, _("Tools"),
3446                         image_new_l(this, "gui_tools"), gravity_center|orientation_vertical));
3447
3448         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, "Route",
3449                         image_new_l(this, "gui_settings"), gravity_center|orientation_vertical,
3450                         gui_internal_cmd_route, NULL));
3451
3452
3453         callback_list_call_attr_1(this->cbl, attr_gui, w);
3454
3455         gui_internal_menu_render(this);
3456         graphics_draw_mode(this->gra, draw_mode_end);
3457 }
3458
3459 static void
3460 gui_internal_enter(struct gui_priv *this, int ignore)
3461 {
3462         struct graphics *gra=this->gra;
3463         this->ignore_button=ignore;
3464         this->clickp_valid=this->vehicle_valid=0;
3465
3466         navit_block(this->nav, 1);
3467         graphics_overlay_disable(gra, 1);
3468         this->root.p.x=0;
3469         this->root.p.y=0;
3470         this->root.background=this->background;
3471 }
3472
3473 static void
3474 gui_internal_leave(struct gui_priv *this)
3475 {
3476         graphics_draw_mode(this->gra, draw_mode_end);
3477 }
3478
3479 static void
3480 gui_internal_cmd_menu(struct gui_priv *this, struct point *p, int ignore)
3481 {
3482         struct transformation *trans;
3483         struct coord c;
3484         struct attr attr,attrp;
3485
3486         dbg(1,"enter\n");
3487         gui_internal_enter(this, ignore);
3488         trans=navit_get_trans(this->nav);
3489         if (p) {
3490                 transform_reverse(trans, p, &c);
3491                 dbg(0,"x=0x%x y=0x%x\n", c.x, c.y);
3492                 this->clickp.pro=transform_get_projection(trans);
3493                 this->clickp.x=c.x;
3494                 this->clickp.y=c.y;
3495                 this->clickp_valid=1;
3496         }
3497         if (navit_get_attr(this->nav, attr_vehicle, &attr, NULL) && attr.u.vehicle
3498                 && vehicle_get_attr(attr.u.vehicle, attr_position_coord_geo, &attrp, NULL)) {
3499                 this->vehiclep.pro=transform_get_projection(trans);
3500                 transform_from_geo(this->vehiclep.pro, attrp.u.coord_geo, &c);
3501                 this->vehiclep.x=c.x;
3502                 this->vehiclep.y=c.y;
3503                 this->vehicle_valid=1;
3504         }
3505         // draw menu
3506         gui_internal_menu_root(this);
3507 }
3508
3509 static void
3510 gui_internal_cmd_menu2(struct gui_priv *this)
3511 {
3512         gui_internal_cmd_menu(this, NULL, 1);
3513 }
3514
3515
3516 static void
3517 gui_internal_cmd_log_do(struct gui_priv *this, struct widget *widget)
3518 {
3519         if (widget->text && strlen(widget->text))
3520                 navit_textfile_debug_log(this->nav, "type=log_entry label=\"%s\"",widget->text);
3521         g_free(widget->text);
3522         widget->text=NULL;
3523         gui_internal_prune_menu(this, NULL);
3524         gui_internal_check_exit(this);
3525 }
3526
3527 static void
3528 gui_internal_cmd_log_clicked(struct gui_priv *this, struct widget *widget, void *data)
3529 {
3530         gui_internal_cmd_log_do(this, widget->data);
3531 }
3532
3533 static void
3534 gui_internal_cmd_log_changed(struct gui_priv *this, struct widget *wm, void *data)
3535 {
3536         int len;
3537         if (wm->text) {
3538                 len=strlen(wm->text);
3539                 if (len && (wm->text[len-1] == '\n' || wm->text[len-1] == '\r')) {
3540                         wm->text[len-1]='\0';
3541                         gui_internal_cmd_log_do(this, wm);
3542                 }
3543         }
3544 }
3545
3546
3547 static void
3548 gui_internal_cmd_log(struct gui_priv *this)
3549 {
3550         struct widget *w,*wb,*wk,*wl,*we,*wnext;
3551         gui_internal_enter(this, 1);
3552         wb=gui_internal_menu(this, "Log Message");
3553         w=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3554         gui_internal_widget_append(wb, w);
3555         we=gui_internal_box_new(this, gravity_left_center|orientation_horizontal|flags_fill);
3556         gui_internal_widget_append(w, we);
3557         gui_internal_widget_append(we, wk=gui_internal_label_new(this, _("Message")));
3558         wk->state |= STATE_EDIT|STATE_CLEAR;
3559         wk->background=this->background;
3560         wk->flags |= flags_expand|flags_fill;
3561         wk->func = gui_internal_cmd_log_changed;
3562         gui_internal_widget_append(we, wnext=gui_internal_image_new(this, image_new_xs(this, "gui_active")));
3563         wnext->state |= STATE_SENSITIVE;
3564         wnext->func = gui_internal_cmd_log_clicked;
3565         wnext->data=wk;
3566         wl=gui_internal_box_new(this, gravity_left_top|orientation_vertical|flags_expand|flags_fill);
3567         gui_internal_widget_append(w, wl);
3568         if (this->keyboard)
3569                 gui_internal_widget_append(w, gui_internal_keyboard(this,2));
3570         gui_internal_menu_render(this);
3571         gui_internal_leave(this);
3572 }
3573
3574 static void
3575 gui_internal_check_exit(struct gui_priv *this)
3576 {
3577         struct graphics *gra=this->gra;
3578         if (! this->root.children) {
3579                 gui_internal_search_idle_end(this);
3580                 gui_internal_search_list_destroy(this);
3581                 graphics_overlay_disable(gra, 0);
3582                 if (!navit_block(this->nav, 0)) {
3583                         if (this->redraw)
3584                                 navit_draw(this->nav);
3585                         else
3586                                 navit_draw_displaylist(this->nav);
3587                 }
3588         }
3589 }
3590
3591 //##############################################################################################################
3592 //# Description: Function to handle mouse clicks and scroll wheel movement
3593 //# Comment:
3594 //# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
3595 //##############################################################################################################
3596 static void gui_internal_button(void *data, int pressed, int button, struct point *p)
3597 {
3598         struct gui_priv *this=data;
3599         struct graphics *gra=this->gra;
3600
3601         dbg(1,"enter %d %d\n", pressed, button);
3602         // if still on the map (not in the menu, yet):
3603         dbg(1,"children=%p ignore_button=%d\n",this->root.children,this->ignore_button);
3604         if (!this->root.children || this->ignore_button) {
3605                 this->ignore_button=0;
3606                 // check whether the position of the mouse changed during press/release OR if it is the scrollwheel
3607                 if (!navit_handle_button(this->nav, pressed, button, p, NULL)) {
3608                         dbg(1,"navit has handled button\n");
3609                         return;
3610                 }
3611                 dbg(1,"menu_on_map_click=%d\n",this->menu_on_map_click);
3612                 if (this->menu_on_map_click && button == 1) 
3613                         gui_internal_cmd_menu(this, p, 0);
3614                 return;
3615         }
3616
3617
3618         // if already in the menu:
3619         if (pressed) {
3620                 this->pressed=1;
3621                 this->current=*p;
3622                 gui_internal_highlight(this);
3623         } else {
3624                 this->pressed=0;
3625                 this->current.x=-1;
3626                 this->current.y=-1;
3627                 graphics_draw_mode(gra, draw_mode_begin);
3628                 gui_internal_call_highlighted(this);
3629                 gui_internal_highlight(this);
3630                 graphics_draw_mode(gra, draw_mode_end);
3631                 gui_internal_check_exit(this);
3632         }
3633 }
3634
3635 //##############################################################################################################
3636 //# Description:
3637 //# Comment:
3638 //# Authors: Martin Schaller (04/2008)
3639 //##############################################################################################################
3640 static void gui_internal_resize(void *data, int w, int h)
3641 {
3642         struct gui_priv *this=data;
3643
3644         if( this->root.w==w && this->root.h==h)
3645                 return;
3646
3647         this->root.w=w;
3648         this->root.h=h;
3649         dbg(0,"w=%d h=%d children=%p\n", w, h, this->root.children);
3650         navit_handle_resize(this->nav, w, h);
3651         if (this->root.children) {
3652                 gui_internal_prune_menu(this, NULL);
3653                 gui_internal_menu_root(this);
3654         }
3655 }
3656
3657 static void
3658 gui_internal_keynav_point(struct widget *w, int dx, int dy, struct point *p)
3659 {
3660         p->x=w->p.x+w->w/2;
3661         p->y=w->p.y+w->h/2;
3662         if (dx < 0)
3663                 p->x=w->p.x;
3664         if (dx > 0)
3665                 p->x=w->p.x+w->w;
3666         if (dy < 0)
3667                 p->y=w->p.y;
3668         if (dy > 0)
3669                 p->y=w->p.y+w->h;
3670 }
3671
3672 static void
3673 gui_internal_keynav_find_closest(struct widget *wi, struct point *p, int dx, int dy, int *distance, struct widget **result)
3674 {
3675         GList *l=wi->children;
3676         if (wi->state & STATE_SENSITIVE) {
3677                 int dist1,dist2;
3678                 struct point wp;
3679                 gui_internal_keynav_point(wi, -dx, -dy, &wp);
3680                 if (dx) {
3681                         dist1=(wp.x-p->x)*dx;
3682                         dist2=wp.y-p->y;
3683                 } else if (dy) {
3684                         dist1=(wp.y-p->y)*dy;
3685                         dist2=wp.x-p->x;
3686                 } else {
3687                         dist2=wp.x-p->x;
3688                         dist1=wp.y-p->y;
3689                         if (dist1 < 0)
3690                                 dist1=-dist1;
3691                 }
3692                 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);
3693                 if (dist1 >= 0) {
3694                         if (dist2 < 0)
3695                                 dist1-=dist2;
3696                         else
3697                                 dist1+=dist2;
3698                         if (dist1 < *distance) {
3699                                 *result=wi;
3700                                 *distance=dist1;
3701                         }
3702                 }
3703         }
3704         while (l) {
3705                 struct widget *child=l->data;
3706                 gui_internal_keynav_find_closest(child, p, dx, dy, distance, result);
3707                 l=g_list_next(l);
3708         }
3709 }
3710
3711 static void
3712 gui_internal_keynav_highlight_next(struct gui_priv *this, int dx, int dy)
3713 {
3714         struct widget *result,*menu=g_list_last(this->root.children)->data;
3715         struct point p;
3716         int distance;
3717         if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
3718                 gui_internal_keynav_point(this->highlighted, dx, dy, &p);
3719         else {
3720                 p.x=0;
3721                 p.y=0;
3722                 distance=INT_MAX;
3723                 result=NULL;
3724                 gui_internal_keynav_find_closest(menu, &p, 0, 0, &distance, &result);
3725                 if (result) {
3726                         gui_internal_keynav_point(result, dx, dy, &p);
3727                         dbg(1,"result origin=%p p=%d,%d\n", result, p.x, p.y);
3728                 }
3729         }
3730         result=NULL;
3731         distance=INT_MAX;
3732         gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
3733         dbg(1,"result=%p\n", result);
3734         if (! result) {
3735                 if (dx < 0)
3736                         p.x=this->root.w;
3737                 if (dx > 0)
3738                         p.x=0;
3739                 if (dy < 0)
3740                         p.y=this->root.h;
3741                 if (dy > 0)
3742                         p.y=0;
3743                 result=NULL;
3744                 distance=INT_MAX;
3745                 gui_internal_keynav_find_closest(menu, &p, dx, dy, &distance, &result);
3746                 dbg(1,"wraparound result=%p\n", result);
3747         }
3748         gui_internal_highlight_do(this, result);
3749         if (result)
3750                 gui_internal_say(this, result, 1);
3751 }
3752
3753 //##############################################################################################################
3754 //# Description:
3755 //# Comment:
3756 //# Authors: Martin Schaller (04/2008)
3757 //##############################################################################################################
3758 static void gui_internal_keypress(void *data, char *key)
3759 {
3760         struct gui_priv *this=data;
3761         int w,h;
3762         struct point p;
3763         if (!this->root.children) {
3764                 transform_get_size(navit_get_trans(this->nav), &w, &h);
3765                 switch (*key) {
3766                 case NAVIT_KEY_UP:
3767                         p.x=w/2;
3768                         p.y=0;
3769                         navit_set_center_screen(this->nav, &p, 1);
3770                         break;
3771                 case NAVIT_KEY_DOWN:
3772                         p.x=w/2;
3773                         p.y=h;
3774                         navit_set_center_screen(this->nav, &p, 1);
3775                         break;
3776                 case NAVIT_KEY_LEFT:
3777                         p.x=0;
3778                         p.y=h/2;
3779                         navit_set_center_screen(this->nav, &p, 1);
3780                         break;
3781                 case NAVIT_KEY_RIGHT:
3782                         p.x=w;
3783                         p.y=h/2;
3784                         navit_set_center_screen(this->nav, &p, 1);
3785                         break;
3786                 case NAVIT_KEY_ZOOM_IN:
3787                         navit_zoom_in(this->nav, 2, NULL);
3788                         break;
3789                 case NAVIT_KEY_ZOOM_OUT:
3790                         navit_zoom_out(this->nav, 2, NULL);
3791                         break;
3792                 case NAVIT_KEY_RETURN:
3793                 case NAVIT_KEY_MENU:
3794                         gui_internal_cmd_menu(this, NULL, 0);
3795                         break;
3796                 }
3797                 return;
3798         }
3799         graphics_draw_mode(this->gra, draw_mode_begin);
3800         switch (*key) {
3801         case NAVIT_KEY_LEFT:
3802                 gui_internal_keynav_highlight_next(this,-1,0);
3803                 break;
3804         case NAVIT_KEY_RIGHT:
3805                 gui_internal_keynav_highlight_next(this,1,0);
3806                 break;
3807         case NAVIT_KEY_UP:
3808                 gui_internal_keynav_highlight_next(this,0,-1);
3809                 break;
3810         case NAVIT_KEY_DOWN:
3811                 gui_internal_keynav_highlight_next(this,0,1);
3812                 break;
3813         case NAVIT_KEY_BACK:
3814                 break;
3815         case NAVIT_KEY_RETURN:
3816                 if (this->highlighted && this->highlighted_menu == g_list_last(this->root.children)->data)
3817                         gui_internal_call_highlighted(this);
3818                 else
3819                         gui_internal_keypress_do(this, key);
3820                 break;
3821         default:
3822                 gui_internal_keypress_do(this, key);
3823         }
3824         graphics_draw_mode(this->gra, draw_mode_end);
3825         gui_internal_check_exit(this);
3826 }
3827
3828
3829 //##############################################################################################################
3830 //# Description:
3831 //# Comment:
3832 //# Authors: Martin Schaller (04/2008)
3833 //##############################################################################################################
3834 static int gui_internal_set_graphics(struct gui_priv *this, struct graphics *gra)
3835 {
3836         struct window *win;
3837         struct color cbh={0x9fff,0x9fff,0x9fff,0xffff};
3838         struct color cf={0xbfff,0xbfff,0xbfff,0xffff};
3839         struct transformation *trans=navit_get_trans(this->nav);
3840
3841         win=graphics_get_data(gra, "window");
3842         if (! win)
3843                 return 1;
3844         navit_ignore_graphics_events(this->nav, 1);
3845         this->gra=gra;
3846         this->win=win;
3847         navit_ignore_graphics_events(this->nav, 1);
3848         transform_get_size(trans, &this->root.w, &this->root.h);
3849         this->resize_cb=callback_new_attr_1(callback_cast(gui_internal_resize), attr_resize, this);
3850         graphics_add_callback(gra, this->resize_cb);
3851         this->button_cb=callback_new_attr_1(callback_cast(gui_internal_button), attr_button, this);
3852         graphics_add_callback(gra, this->button_cb);
3853         this->motion_cb=callback_new_attr_1(callback_cast(gui_internal_motion), attr_motion, this);
3854         graphics_add_callback(gra, this->motion_cb);
3855         this->keypress_cb=callback_new_attr_1(callback_cast(gui_internal_keypress), attr_keypress, this);
3856         graphics_add_callback(gra, this->keypress_cb);
3857         this->background=graphics_gc_new(gra);
3858         this->background2=graphics_gc_new(gra);
3859         this->highlight_background=graphics_gc_new(gra);
3860         graphics_gc_set_foreground(this->highlight_background, &cbh);
3861         this->foreground=graphics_gc_new(gra);
3862         graphics_gc_set_foreground(this->foreground, &cf);
3863         this->text_background=graphics_gc_new(gra);
3864         this->text_foreground=graphics_gc_new(gra);
3865         graphics_gc_set_foreground(this->background, &this->background_color);
3866         graphics_gc_set_foreground(this->background2, &this->background2_color);
3867         graphics_gc_set_foreground(this->text_background, &this->text_background_color);
3868         graphics_gc_set_foreground(this->text_foreground, &this->text_foreground_color);
3869
3870         // set fullscreen if needed
3871         if (this->fullscreen)
3872                 this->win->fullscreen(this->win, this->fullscreen != 0);
3873         return 0;
3874 }
3875
3876 static void gui_internal_disable_suspend(struct gui_priv *this)
3877 {
3878         if (this->win->disable_suspend)
3879                 this->win->disable_suspend(this->win);
3880 }
3881
3882 //##############################################################################################################
3883 //# Description:
3884 //# Comment:
3885 //# Authors: Martin Schaller (04/2008)
3886 //##############################################################################################################
3887 struct gui_methods gui_internal_methods = {
3888         NULL,
3889         NULL,
3890         gui_internal_set_graphics,
3891         NULL,
3892         NULL,
3893         NULL,
3894         gui_internal_disable_suspend,
3895 };
3896
3897 static void
3898 gui_internal_get_data(struct gui_priv *priv, char *command, struct attr **in, struct attr ***out)
3899 {
3900         struct attr private_data = (struct attr) { attr_private_data, {(void *)&priv->data}};
3901         if (out)
3902                 *out=attr_generic_add_attr(*out, &private_data);
3903 }
3904
3905 static void
3906 gui_internal_add_callback(struct gui_priv *priv, struct callback *cb)
3907 {
3908         callback_list_add(priv->cbl, cb);
3909 }
3910
3911 static void
3912 gui_internal_remove_callback(struct gui_priv *priv, struct callback *cb)
3913 {
3914         callback_list_remove(priv->cbl, cb);
3915 }
3916
3917
3918 static struct gui_internal_methods gui_internal_methods_ext = {
3919         gui_internal_add_callback,
3920         gui_internal_remove_callback,
3921         gui_internal_menu_render,
3922         image_new_xs,
3923         image_new_l,
3924 };
3925
3926
3927 static enum flags
3928 gui_internal_get_flags(struct widget *widget)
3929 {
3930         return widget->flags;
3931 }
3932
3933 static void
3934 gui_internal_set_flags(struct widget *widget, enum flags flags)
3935 {
3936         widget->flags=flags;
3937 }
3938
3939 static int
3940 gui_internal_get_state(struct widget *widget)
3941 {
3942         return widget->state;
3943 }
3944
3945 static void
3946 gui_internal_set_state(struct widget *widget, int state)
3947 {
3948         widget->state=state;
3949 }
3950
3951 static void
3952 gui_internal_set_func(struct widget *widget, void (*func)(struct gui_priv *priv, struct widget *widget, void *data))
3953 {
3954         widget->func=func;
3955 }
3956
3957 static void
3958 gui_internal_set_data(struct widget *widget, void *data)
3959 {
3960         widget->data=data;
3961 }
3962
3963 static void
3964 gui_internal_set_default_background(struct gui_priv *this, struct widget *widget)
3965 {
3966         widget->background=this->background;
3967 }
3968
3969 static struct gui_internal_widget_methods gui_internal_widget_methods = {
3970         gui_internal_widget_append,
3971         gui_internal_button_new,
3972         gui_internal_button_new_with_callback,
3973         gui_internal_box_new,
3974         gui_internal_label_new,
3975         gui_internal_image_new,
3976         gui_internal_keyboard,
3977         gui_internal_menu,
3978         gui_internal_get_flags,
3979         gui_internal_set_flags,
3980         gui_internal_get_state,
3981         gui_internal_set_state,
3982         gui_internal_set_func,
3983         gui_internal_set_data,
3984         gui_internal_set_default_background,
3985 };
3986
3987 static struct command_table commands[] = {
3988         {"menu",command_cast(gui_internal_cmd_menu2)},
3989         {"fullscreen",command_cast(gui_internal_cmd_fullscreen)},
3990         {"get_data",command_cast(gui_internal_get_data)},
3991         {"log",command_cast(gui_internal_cmd_log)},
3992 };
3993
3994
3995 //##############################################################################################################
3996 //# Description:
3997 //# Comment:
3998 //# Authors: Martin Schaller (04/2008)
3999 //##############################################################################################################
4000 static struct gui_priv * gui_internal_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs)
4001 {
4002         struct gui_priv *this;
4003         struct attr *attr;
4004         *meth=gui_internal_methods;
4005         this=g_new0(struct gui_priv, 1);
4006         this->nav=nav;
4007         if ((attr=attr_search(attrs, NULL, attr_menu_on_map_click)))
4008                 this->menu_on_map_click=attr->u.num;
4009         else
4010                 this->menu_on_map_click=1;
4011         if ((attr=attr_search(attrs, NULL, attr_callback_list))) {
4012                 dbg(0,"register\n");
4013                 command_add_table(attr->u.callback_list, commands, sizeof(commands)/sizeof(struct command_table), this);
4014         }
4015
4016         if( (attr=attr_search(attrs,NULL,attr_font_size)))
4017         {
4018           this->config.font_size=attr->u.num;
4019         }
4020         else
4021         {
4022           this->config.font_size=-1;
4023         }
4024         if( (attr=attr_search(attrs,NULL,attr_icon_xs)))
4025         {
4026           this->config.icon_xs=attr->u.num;
4027         }
4028         else
4029         {
4030           this->config.icon_xs=-1;
4031         }
4032         if( (attr=attr_search(attrs,NULL,attr_icon_l)))
4033         {
4034           this->config.icon_l=attr->u.num;
4035         }
4036         else
4037         {
4038           this->config.icon_l=-1;
4039         }
4040         if( (attr=attr_search(attrs,NULL,attr_icon_s)))
4041         {
4042           this->config.icon_s=attr->u.num;
4043         }
4044         else
4045         {
4046           this->config.icon_s=-1;
4047         }
4048         if( (attr=attr_search(attrs,NULL,attr_spacing)))
4049         {
4050           this->config.spacing=attr->u.num;
4051         }
4052         else
4053         {
4054           this->config.spacing=-1;
4055         }
4056         if( (attr=attr_search(attrs,NULL,attr_gui_speech)))
4057         {
4058           this->speech=attr->u.num;
4059         }
4060         if( (attr=attr_search(attrs,NULL,attr_keyboard)))
4061           this->keyboard=attr->u.num;
4062         else
4063           this->keyboard=1;
4064
4065     if( (attr=attr_search(attrs,NULL,attr_fullscreen)))
4066       this->fullscreen=attr->u.num;
4067
4068         if( (attr=attr_search(attrs,NULL,attr_flags)))
4069               this->flags=attr->u.num;
4070         if( (attr=attr_search(attrs,NULL,attr_background_color)))
4071               this->background_color=*attr->u.color;
4072         else
4073               this->background_color=(struct color){0x0,0x0,0x0,0xffff};
4074         if( (attr=attr_search(attrs,NULL,attr_background_color2)))
4075                 this->background2_color=*attr->u.color;
4076         else
4077                 this->background2_color=(struct color){0x4141,0x4141,0x4141,0xffff};
4078         if( (attr=attr_search(attrs,NULL,attr_text_color)))
4079               this->text_foreground_color=*attr->u.color;
4080         else
4081               this->text_foreground_color=(struct color){0xffff,0xffff,0xffff,0xffff};
4082         if( (attr=attr_search(attrs,NULL,attr_columns)))
4083               this->cols=attr->u.num;
4084         if( (attr=attr_search(attrs,NULL,attr_osd_configuration)))
4085               this->osd_configuration=*attr;
4086
4087         if( (attr=attr_search(attrs,NULL,attr_pitch)))
4088               this->pitch=attr->u.num;
4089         else
4090                 this->pitch=20;
4091         this->data.priv=this;
4092         this->data.gui=&gui_internal_methods_ext;
4093         this->data.widget=&gui_internal_widget_methods;
4094         this->cbl=callback_list_new();
4095
4096         return this;
4097 }
4098
4099 //##############################################################################################################
4100 //# Description:
4101 //# Comment:
4102 //# Authors: Martin Schaller (04/2008)
4103 //##############################################################################################################
4104 void plugin_init(void)
4105 {
4106         plugin_register_gui_type("internal", gui_internal_new);
4107 }
4108
4109 /**
4110  * @brief Creates a new table widget.
4111  *
4112  * Creates and returns a new table widget.  This function will
4113  * setup next/previous buttons as children.
4114  *
4115  * @param this The graphics context.
4116  * @param flags widget sizing flags.
4117  * @returns The newly created widget
4118  */
4119 struct widget * gui_internal_widget_table_new(struct gui_priv * this, enum flags flags, int buttons)
4120 {
4121         struct widget * widget = g_new0(struct widget,1);
4122         struct table_data * data = NULL;
4123         widget->type=widget_table;
4124         widget->flags=flags;
4125         widget->data = g_new0(struct table_data,1);
4126         widget->data_free=gui_internal_table_data_free;
4127         data = (struct table_data*)widget->data;
4128
4129
4130         if (buttons) {
4131         data->next_button = gui_internal_button_new_with_callback
4132                 (this,"Next",image_new_xs(this, "gui_active") ,
4133                  gravity_left_center  |orientation_vertical,
4134                  gui_internal_table_button_next,NULL);
4135         data->next_button->data=widget;
4136
4137
4138         data->prev_button =  gui_internal_button_new_with_callback
4139                 (this,"Prev",
4140                  image_new_xs(this, "gui_active")
4141                  ,gravity_right_center |orientation_vertical,
4142                  gui_internal_table_button_prev,NULL);
4143
4144         data->prev_button->data=widget;
4145
4146         data->this=this;
4147
4148         data->button_box=gui_internal_box_new(this,
4149                                               gravity_center|orientation_horizontal);
4150         data->button_box->children=g_list_append(data->button_box->children,
4151                                                  data->next_button);
4152         data->button_box->children=g_list_append(data->button_box->children,
4153                                                  data->prev_button);
4154         //data->button_box->background=this->background2;
4155         data->button_box->bl=this->spacing;
4156         widget->children=g_list_append(widget->children,data->button_box);
4157         gui_internal_widget_pack(this,data->button_box);
4158         }
4159
4160         return widget;
4161
4162 }
4163
4164 /**
4165  * @brief Clears all the rows from the table.
4166  * This function removes all rows from a table.
4167  * New rows can later be added to the table.
4168  */
4169 void gui_internal_widget_table_clear(struct gui_priv * this,struct widget * table)
4170 {
4171   GList * iter;
4172   struct table_data * table_data = (struct table_data* ) table->data;
4173
4174   iter = table->children;
4175   while(iter ) {
4176           if(iter->data != table_data->button_box) {
4177                   struct widget * child = (struct widget*)iter->data;
4178                   gui_internal_widget_destroy(this,child);
4179                   if(table->children == iter) {
4180                           table->children = g_list_remove(iter,iter->data);
4181                           iter=table->children;
4182                   }
4183                   else
4184                           iter = g_list_remove(iter,iter->data);
4185           }
4186           else {
4187                   iter = g_list_next(iter);
4188           }
4189
4190   }
4191   table_data->top_row=NULL;
4192   table_data->bottom_row=NULL;
4193   if(table_data->page_headers)
4194           g_list_free(table_data->page_headers);
4195   table_data->page_headers=NULL;
4196 }
4197
4198
4199 /**
4200  * Creates a new table_row widget.
4201  * @param this The graphics context
4202  * @param flags Sizing flags for the row
4203  * @returns The new table_row widget.
4204  */
4205 struct widget * gui_internal_widget_table_row_new(struct gui_priv * this, enum flags flags)
4206 {
4207         struct widget * widget = g_new0(struct widget,1);
4208         widget->type=widget_table_row;
4209         widget->flags=flags;
4210         return widget;
4211 }
4212
4213
4214
4215 /**
4216  * @brief Computes the column dimensions for the table.
4217  *
4218  * @param w The table widget to compute dimensions for.
4219  *
4220  * This function examines all of the rows and columns for the table w
4221  * and returns a list (GList) of table_column_desc elements that
4222  * describe each column of the table.
4223  *
4224  * The caller is responsible for freeing the returned list.
4225  */
4226 static GList * gui_internal_compute_table_dimensions(struct gui_priv * this,struct widget * w)
4227 {
4228
4229         GList * column_desc = NULL;
4230         GList * current_desc=NULL;
4231         GList * cur_row = w->children;
4232         struct widget * cur_row_widget=NULL;
4233         GList * cur_column=NULL;
4234         struct widget * cell_w=NULL;
4235         struct table_column_desc * current_cell=NULL;
4236         struct table_data * table_data=NULL;
4237         int height=0;
4238         int width=0;
4239         int total_width=0;
4240         int column_count=0;
4241
4242         /**
4243          * Scroll through the the table and
4244          * 1. Compute the maximum width + height of each column across all rows.
4245          */
4246         table_data = (struct table_data*) w->data;
4247         for(cur_row=w->children;  cur_row ; cur_row = g_list_next(cur_row) )
4248         {
4249                 cur_row_widget = (struct widget*) cur_row->data;
4250                 current_desc = column_desc;
4251                 if(cur_row_widget == table_data->button_box)
4252                 {
4253                         continue;
4254                 }
4255                 column_count=0;
4256                 for(cur_column = cur_row_widget->children; cur_column;
4257                     cur_column=g_list_next(cur_column))
4258                 {
4259                         cell_w = (struct widget*) cur_column->data;
4260                         gui_internal_widget_pack(this,cell_w);
4261                         if(current_desc == 0)
4262                         {
4263                                 current_cell = g_new0(struct table_column_desc,1);
4264                                 column_desc = g_list_append(column_desc,current_cell);
4265                                 current_desc = g_list_last(column_desc);
4266                                 current_cell->height=cell_w->h;
4267                                 current_cell->width=cell_w->w;
4268                                 total_width+=cell_w->w;
4269
4270                         }
4271                         else
4272                         {
4273                                 current_cell = current_desc->data;
4274                                 height = cell_w->h;
4275                                 width = cell_w->w;
4276                                 if(current_cell->height < height )
4277                                 {
4278                                         current_cell->height = height;
4279                                 }
4280                                 if(current_cell->width < width)
4281                                 {
4282                                         total_width += (width-current_cell->width);
4283                                         current_cell->width = width;
4284
4285
4286
4287                                 }
4288                                 current_desc = g_list_next(current_desc);
4289                         }
4290                         column_count++;
4291
4292                 }/* column loop */
4293
4294         } /*row loop */
4295
4296
4297         /**
4298          * If the width of all columns is less than the width off
4299          * the table expand each cell proportionally.
4300          *
4301          */
4302         if(total_width+(this->spacing*column_count) < w->w ) {
4303                 for(current_desc=column_desc; current_desc; current_desc=g_list_next(current_desc)) {
4304                         current_cell = (struct table_column_desc*) current_desc->data;
4305                         current_cell->width= ( (current_cell->width+this->spacing)/(float)total_width) * w->w ;
4306                 }
4307         }
4308
4309         return column_desc;
4310 }
4311
4312
4313 /**
4314  * @brief Computes the height and width for the table.
4315  *
4316  * The height and widht are computed to display all cells in the table
4317  * at the requested height/width.
4318  *
4319  * @param this The graphics context
4320  * @param w The widget to pack.
4321  *
4322  */
4323 void gui_internal_table_pack(struct gui_priv * this, struct widget * w)
4324 {
4325
4326         int height=0;
4327         int width=0;
4328         int count=0;
4329         GList * column_data = gui_internal_compute_table_dimensions(this,w);
4330         GList * current=0;
4331         struct table_column_desc * cell_desc=0;
4332         struct table_data * table_data = (struct table_data*)w->data;
4333
4334         for(current = column_data; current; current=g_list_next(current))
4335         {
4336                 if(table_data->button_box == current->data )
4337                 {
4338                         continue;
4339                 }
4340                 cell_desc = (struct table_column_desc *) current->data;
4341                 width = width + cell_desc->width + this->spacing;
4342                 if(height < cell_desc->height)
4343                 {
4344                         height = cell_desc->height ;
4345                 }
4346         }
4347
4348
4349
4350         for(current=w->children; current; current=g_list_next(current))
4351         {
4352                 if(current->data!= table_data->button_box)
4353                 {
4354                         count++;
4355                 }
4356         }
4357         if (table_data->button_box)
4358                 gui_internal_widget_pack(this,table_data->button_box);
4359
4360
4361
4362         if(w->h + w->c.y   > this->root.h   )
4363         {
4364                 /**
4365                  * Do not allow the widget to exceed the screen.
4366                  *
4367                  */
4368                 w->h = this->root.h- w->c.y  - height;
4369         }
4370         w->w = width;
4371
4372         /**
4373          * Deallocate column descriptions.
4374          */
4375         current = column_data;
4376         while( (current = g_list_last(current)) )
4377         {
4378                 current = g_list_remove(current,current->data);
4379         }
4380
4381 }
4382
4383
4384
4385 /**
4386  * @brief Renders a table widget.
4387  *
4388  * @param this The graphics context
4389  * @param w The table widget to render.
4390  */
4391 void gui_internal_table_render(struct gui_priv * this, struct widget * w)
4392 {
4393
4394         int x;
4395         int y;
4396         GList * column_desc=NULL;
4397         GList * cur_row = NULL;
4398         GList * current_desc=NULL;
4399         struct table_data * table_data = (struct table_data*)w->data;
4400         int is_skipped=0;
4401         int is_first_page=1;
4402         struct table_column_desc * dim=NULL;
4403
4404         dbg_assert(table_data);
4405         column_desc = gui_internal_compute_table_dimensions(this,w);
4406         y=w->p.y;
4407
4408         /**
4409          * Skip rows that are on previous pages.
4410          */
4411         cur_row = w->children;
4412         if(table_data->top_row && table_data->top_row != w->children )
4413         {
4414                 cur_row = table_data->top_row;
4415                 is_first_page=0;
4416         }
4417
4418
4419         /**
4420          * Loop through each row.  Drawing each cell with the proper sizes,
4421          * at the proper positions.
4422          */
4423         for(table_data->top_row=cur_row; cur_row; cur_row = g_list_next(cur_row))
4424         {
4425                 GList * cur_column=NULL;
4426                 current_desc = column_desc;
4427                 struct widget * cur_row_widget = (struct widget*)cur_row->data;
4428                 int max_height=0;
4429                 x =w->p.x+this->spacing;
4430                 if(cur_row_widget == table_data->button_box )
4431                 {
4432                         continue;
4433                 }
4434                 dim = (struct table_column_desc*)current_desc->data;
4435
4436                 if( y + dim->height + (table_data->button_box ? table_data->button_box->h : 0) + this->spacing >= w->p.y + w->h )
4437                 {
4438                         /*
4439                          * No more drawing space left.
4440                          */
4441                         is_skipped=1;
4442                         break;
4443
4444                 }
4445                 for(cur_column = cur_row_widget->children; cur_column;
4446                     cur_column=g_list_next(cur_column))
4447                 {
4448                         struct  widget * cur_widget = (struct widget*) cur_column->data;
4449                         dim = (struct table_column_desc*)current_desc->data;
4450
4451                         cur_widget->p.x=x;
4452                         cur_widget->w=dim->width;
4453                         cur_widget->p.y=y;
4454                         cur_widget->h=dim->height;
4455                         x=x+cur_widget->w;
4456                         max_height = dim->height;
4457                         /* We pack the widget before rendering to ensure that the x and y
4458                          * coordinates get pushed down.
4459                          */
4460                         gui_internal_widget_pack(this,cur_widget);
4461                         gui_internal_widget_render(this,cur_widget);
4462
4463                         if(dim->height > max_height)
4464                         {
4465                                 max_height = dim->height;
4466                         }
4467                 }
4468                 y = y + max_height;
4469                 table_data->bottom_row=cur_row;
4470                 current_desc = g_list_next(current_desc);
4471         }
4472         if(table_data->button_box && (is_skipped || !is_first_page)  )
4473         {
4474                 table_data->button_box->p.y =w->p.y+w->h-table_data->button_box->h -
4475                         this->spacing;
4476                 if(table_data->button_box->p.y < y )
4477                 {
4478                         table_data->button_box->p.y=y;
4479                 }
4480                 table_data->button_box->p.x = w->p.x;
4481                 table_data->button_box->w = w->w;
4482                 //    table_data->button_box->h = w->h - y;
4483                 //    table_data->next_button->h=table_data->button_box->h;
4484                 //    table_data->prev_button->h=table_data->button_box->h;
4485                 //    table_data->next_button->c.y=table_data->button_box->c.y;
4486                 //    table_data->prev_button->c.y=table_data->button_box->c.y;
4487
4488                 gui_internal_widget_pack(this,table_data->button_box);
4489                 if(table_data->next_button->p.y > w->p.y + w->h + table_data->next_button->h)
4490                 {
4491
4492                         table_data->button_box->p.y = w->p.y + w->h -
4493                                 table_data->button_box->h;
4494                 }
4495                 if(is_skipped)
4496                 {
4497                         table_data->next_button->state|= STATE_SENSITIVE;
4498                 }
4499                 else
4500                 {
4501                         table_data->next_button->state&= ~STATE_SENSITIVE;
4502                 }
4503
4504                 if(table_data->top_row != w->children)
4505                 {
4506                         table_data->prev_button->state|= STATE_SENSITIVE;
4507                 }
4508                 else
4509                 {
4510                         table_data->prev_button->state&= ~STATE_SENSITIVE;
4511                 }
4512                 gui_internal_widget_render(this,table_data->button_box);
4513
4514
4515         }
4516
4517         /**
4518          * Deallocate column descriptions.
4519          */
4520         current_desc = column_desc;
4521         while( (current_desc = g_list_last(current_desc)) )
4522         {
4523                 current_desc = g_list_remove(current_desc,current_desc->data);
4524         }
4525 }
4526
4527
4528 /**
4529  * @brief Displays Route information
4530  *
4531  * @li The name of the active vehicle
4532  * @param wm The button that was pressed.
4533  * @param v Unused
4534  */
4535 static void
4536 gui_internal_cmd_route_description(struct gui_priv * this, struct widget * wm,void *v)
4537 {
4538
4539
4540         struct widget * menu;
4541         struct widget * row;
4542
4543
4544         if(! this->vehicle_cb)
4545         {
4546           /**
4547            * Register the callback on vehicle updates.
4548            */
4549           this->vehicle_cb = callback_new_attr_1(callback_cast(gui_internal_route_update),
4550                                                        attr_position_coord_geo,this);
4551           navit_add_callback(this->nav,this->vehicle_cb);
4552         }
4553
4554         this->route_data.route_table = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
4555         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
4556
4557         row = gui_internal_widget_table_row_new(this,gravity_left | orientation_horizontal | flags_fill);
4558
4559
4560         menu=gui_internal_menu(this,"Route Description");
4561
4562         menu->free=gui_internal_route_screen_free;
4563         this->route_data.route_showing=1;
4564         this->route_data.route_table->spx = this->spacing;
4565
4566
4567         struct widget * box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
4568
4569         //      gui_internal_widget_append(box,gui_internal_box_new_with_label(this,"Test"));
4570         gui_internal_widget_append(box,this->route_data.route_table);
4571         box->w=menu->w;
4572         box->spx = this->spacing;
4573         this->route_data.route_table->w=box->w;
4574         gui_internal_widget_append(menu,box);
4575         gui_internal_populate_route_table(this,this->nav);
4576         gui_internal_menu_render(this);
4577
4578 }
4579
4580 static int
4581 line_intersection(struct coord* a1, struct coord *a2, struct coord * b1, struct coord *b2, struct coord *res)
4582 {
4583         int n, a, b;
4584         int adx=a2->x-a1->x;
4585         int ady=a2->y-a1->y;
4586         int bdx=b2->x-b1->x;
4587         int bdy=b2->y-b1->y;
4588         n = bdy * adx - bdx * ady;
4589         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
4590         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
4591         if (n < 0) {
4592                 n = -n;
4593                 a = -a;
4594                 b = -b;
4595         }
4596         if (a < 0 || b < 0)
4597                 return 0;
4598         if (a > n || b > n)
4599                 return 0;
4600         if (n == 0) {
4601                 dbg(0,"a=%d b=%d n=%d\n", a, b, n);
4602                 dbg(0,"a1=0x%x,0x%x ad %d,%d\n", a1->x, a1->y, adx, ady);
4603                 dbg(0,"b1=0x%x,0x%x bd %d,%d\n", b1->x, b1->y, bdx, bdy);
4604                 dbg_assert(n != 0);
4605         }
4606         res->x = a1->x + a * adx / n;
4607         res->y = a1->y + a * ady / n;
4608         return 1;
4609 }
4610
4611 struct heightline {
4612         struct heightline *next;
4613         int height;
4614         struct coord_rect bbox;
4615         int count;
4616         struct coord c[0];
4617 };
4618
4619 struct diagram_point {
4620         struct diagram_point *next;
4621         struct coord c;
4622 };
4623
4624 static struct heightline *
4625 item_get_heightline(struct item *item)
4626 {
4627         struct heightline *ret=NULL;
4628         struct street_data *sd;
4629         struct attr attr;
4630         int i,height;
4631
4632         if (item_attr_get(item, attr_label, &attr)) {
4633                 height=atoi(attr.u.str);
4634                 sd=street_get_data(item);
4635                 if (sd && sd->count > 1) {
4636                         ret=g_malloc(sizeof(struct heightline)+sd->count*sizeof(struct coord));
4637                         ret->bbox.lu=sd->c[0];
4638                         ret->bbox.rl=sd->c[0];
4639                         ret->count=sd->count;
4640                         ret->height=height;
4641                         for (i = 0 ; i < sd->count ; i++) {
4642                                 ret->c[i]=sd->c[i];
4643                                 coord_rect_extend(&ret->bbox, sd->c+i);
4644                         }
4645                 }
4646                 street_data_free(sd);
4647         }
4648         return ret;
4649 }
4650
4651
4652 /**
4653  * @brief Displays Route Height Profile
4654  *
4655  * @li The name of the active vehicle
4656  * @param wm The button that was pressed.
4657  * @param v Unused
4658  */
4659 static void gui_internal_cmd_route_height_profile(struct gui_priv * this, struct widget * wm,void *v)
4660 {
4661
4662
4663         struct widget * menu, *box;
4664
4665         struct map * map=NULL;
4666         struct map_rect * mr=NULL;
4667         struct route * route;
4668         struct item * item =NULL;
4669         struct mapset *ms;
4670         struct mapset_handle *msh;
4671         int x,i,first=1,dist=0;
4672         struct coord c,last,res;
4673         struct coord_rect rbbox,dbbox;
4674         struct map_selection sel;
4675         struct heightline *heightline,*heightlines=NULL;
4676         struct diagram_point *min,*diagram_point,*diagram_points=NULL;
4677         sel.next=NULL;
4678         sel.order=18;
4679         sel.range.min=type_height_line_1;
4680         sel.range.max=type_height_line_3;
4681
4682
4683         menu=gui_internal_menu(this,_("Height Profile"));
4684         box = gui_internal_box_new(this, gravity_left_top| orientation_vertical | flags_fill | flags_expand);
4685         gui_internal_widget_append(menu, box);
4686         route = navit_get_route(this->nav);
4687         if (route)
4688                 map = route_get_map(route);
4689         if(map)
4690                 mr = map_rect_new(map,NULL);
4691         if(mr) {
4692                 while((item = map_rect_get_item(mr))) {
4693                         while (item_coord_get(item, &c, 1)) {
4694                                 if (first) {
4695                                         first=0;
4696                                         sel.u.c_rect.lu=c;
4697                                         sel.u.c_rect.rl=c;
4698                                 } else
4699                                         coord_rect_extend(&sel.u.c_rect, &c);
4700                         }
4701                 }
4702                 map_rect_destroy(mr);
4703                 ms=navit_get_mapset(this->nav);
4704                 if (!first && ms) {
4705                         msh=mapset_open(ms);
4706                         while ((map=mapset_next(msh, 1))) {
4707                                 mr=map_rect_new(map, &sel);
4708                                 if (mr) {
4709                                         while((item = map_rect_get_item(mr))) {
4710                                                 if (item->type >= sel.range.min && item->type <= sel.range.max) {
4711                                                         heightline=item_get_heightline(item);
4712                                                         if (heightline) {
4713                                                                 heightline->next=heightlines;
4714                                                                 heightlines=heightline;
4715                                                         }
4716                                                 }
4717                                         }
4718                                         map_rect_destroy(mr);
4719                                 }
4720                         }
4721                         mapset_close(msh);
4722                 }
4723         }
4724         map=NULL;
4725         mr=NULL;
4726         if (route)
4727                 map = route_get_map(route);
4728         if(map)
4729                 mr = map_rect_new(map,NULL);
4730         if(mr && heightlines) {
4731                 while((item = map_rect_get_item(mr))) {
4732                         first=1;
4733                         while (item_coord_get(item, &c, 1)) {
4734                                 if (first)
4735                                         first=0;
4736                                 else {
4737                                         heightline=heightlines;
4738                                         rbbox.lu=last;
4739                                         rbbox.rl=last;
4740                                         coord_rect_extend(&rbbox, &c);
4741                                         while (heightline) {
4742                                                 if (coord_rect_overlap(&rbbox, &heightline->bbox)) {
4743                                                         for (i = 0 ; i < heightline->count - 1; i++) {
4744                                                                 if (heightline->c[i].x != heightline->c[i+1].x || heightline->c[i].y != heightline->c[i+1].y) {
4745                                                                         if (line_intersection(heightline->c+i, heightline->c+i+1, &last, &c, &res)) {
4746                                                                                 diagram_point=g_new(struct diagram_point, 1);
4747                                                                                 diagram_point->c.x=dist+transform_distance(projection_mg, &last, &res);
4748                                                                                 diagram_point->c.y=heightline->height;
4749                                                                                 diagram_point->next=diagram_points;
4750                                                                                 diagram_points=diagram_point;
4751                                                                                 dbg(0,"%d %d\n", diagram_point->c.x, diagram_point->c.y);
4752                                                                         }
4753                                                                 }
4754                                                         }
4755                                                 }
4756                                                 heightline=heightline->next;
4757                                         }
4758                                         dist+=transform_distance(projection_mg, &last, &c);
4759                                 }
4760                                 last=c;
4761                         }
4762
4763                 }
4764                 map_rect_destroy(mr);
4765         }
4766
4767
4768         gui_internal_menu_render(this);
4769         first=1;
4770         diagram_point=diagram_points;
4771         while (diagram_point) {
4772                 if (first) {
4773                         dbbox.lu=diagram_point->c;
4774                         dbbox.rl=diagram_point->c;
4775                         first=0;
4776                 } else
4777                         coord_rect_extend(&dbbox, &diagram_point->c);
4778                 diagram_point=diagram_point->next;
4779         }
4780         dbg(0,"%d %d %d %d\n", dbbox.lu.x, dbbox.lu.y, dbbox.rl.x, dbbox.rl.y);
4781         if (dbbox.rl.x > dbbox.lu.x && dbbox.lu.x*100/(dbbox.rl.x-dbbox.lu.x) <= 25)
4782                 dbbox.lu.x=0;
4783         if (dbbox.lu.y > dbbox.rl.y && dbbox.rl.y*100/(dbbox.lu.y-dbbox.rl.y) <= 25)
4784                 dbbox.rl.y=0;
4785         dbg(0,"%d,%d %dx%d\n", box->p.x, box->p.y, box->w, box->h);
4786         x=dbbox.lu.x;
4787         first=1;
4788         for (;;) {
4789                 struct point p[2];
4790                 min=NULL;
4791                 diagram_point=diagram_points;
4792                 while (diagram_point) {
4793                         if (diagram_point->c.x >= x && (!min || min->c.x > diagram_point->c.x))
4794                                 min=diagram_point;
4795                         diagram_point=diagram_point->next;
4796                 }
4797                 if (! min)
4798                         break;
4799                 p[1].x=(min->c.x-dbbox.lu.x)*(box->w-10)/(dbbox.rl.x-dbbox.lu.x)+box->p.x+5;
4800                 p[1].y=(min->c.y-dbbox.rl.y)*(box->h-10)/(dbbox.lu.y-dbbox.rl.y)+box->p.y+5;
4801                 dbg(0,"%d,%d=%d,%d\n",min->c.x, min->c.y, p[1].x,p[1].y);
4802                 graphics_draw_circle(this->gra, this->foreground, &p[1], 2);
4803                 if (first)
4804                         first=0;
4805                 else
4806                         graphics_draw_lines(this->gra, this->foreground, p, 2);
4807                 p[0]=p[1];
4808                 x=min->c.x+1;
4809         }
4810
4811
4812 }
4813
4814 /**
4815  * @brief Displays Route information
4816  *
4817  * @li The name of the active vehicle
4818  * @param wm The button that was pressed.
4819  * @param v Unused
4820                 */
4821 void gui_internal_cmd_route(struct gui_priv * this, struct widget * wm,void *v)
4822 {
4823         struct widget *w;
4824
4825         graphics_draw_mode(this->gra, draw_mode_begin);
4826         w=gui_internal_menu(this, _("Route"));
4827         w->spx=this->spacing*10;
4828         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Description"),
4829                         image_new_l(this, "gui_actions"), gravity_center|orientation_vertical,
4830                         gui_internal_cmd_route_description, NULL));
4831         gui_internal_widget_append(w, gui_internal_button_new_with_callback(this, _("Height Profile"),
4832                         image_new_l(this, "gui_actions"), gravity_center|orientation_vertical,
4833                         gui_internal_cmd_route_height_profile, NULL));
4834         gui_internal_menu_render(this);
4835         gui_internal_menu_render(this);
4836         graphics_draw_mode(this->gra, draw_mode_end);
4837
4838 }
4839
4840
4841
4842 /**
4843  * @brief handles the 'next page' table event.
4844  * A callback function that is invoked when the 'next page' button is pressed
4845  * to advance the contents of a table widget.
4846  *
4847  * @param this The graphics context.
4848  * @param wm The button widget that was pressed.
4849  */
4850 static void gui_internal_table_button_next(struct gui_priv * this, struct widget * wm, void *data)
4851 {
4852         struct widget * table_widget = (struct widget * ) wm->data;
4853         struct table_data * table_data = NULL;
4854         int found=0;
4855         GList * iterator;
4856
4857         if(table_widget)
4858         {
4859                 table_data = (struct table_data*) table_widget->data;
4860
4861         }
4862         if(table_data)
4863         {
4864                 /**
4865                  * Before advancing to the next page we need to ensure
4866                  * that the current top_row is in the list of previous top_rows
4867                  * so previous page can work.
4868                  *
4869                  */
4870                 for(iterator=table_data->page_headers; iterator != NULL;
4871                     iterator = g_list_next(iterator) )
4872                 {
4873                         if(iterator->data == table_data->top_row)
4874                         {
4875                                 found=1;
4876                                 break;
4877                         }
4878
4879                 }
4880                 if( ! found)
4881                 {
4882                         table_data->page_headers=g_list_append(table_data->page_headers,
4883                                                                table_data->top_row);
4884                 }
4885
4886                 table_data->top_row = g_list_next(table_data->bottom_row);
4887         }
4888         wm->state&= ~STATE_HIGHLIGHTED;
4889         gui_internal_menu_render(this);
4890 }
4891
4892
4893
4894 /**
4895  * @brief handles the 'previous page' table event.
4896  * A callback function that is invoked when the 'previous page' button is pressed
4897  * to go back in the contents of a table widget.
4898  *
4899  * @param this The graphics context.
4900  * @param wm The button widget that was pressed.
4901  */
4902 static void gui_internal_table_button_prev(struct gui_priv * this, struct widget * wm, void *data)
4903 {
4904         struct widget * table_widget = (struct widget * ) wm->data;
4905         struct table_data * table_data = NULL;
4906         GList * current_page_top=NULL;
4907
4908         GList * iterator;
4909         if(table_widget)
4910         {
4911                 table_data = (struct table_data*) table_widget->data;
4912                 if(table_data)
4913                 {
4914                         current_page_top = table_data->top_row;
4915                         for(iterator = table_data->page_headers; iterator != NULL;
4916                             iterator = g_list_next(iterator))
4917                         {
4918                                 if(current_page_top == iterator->data)
4919                                 {
4920                                         break;
4921                                 }
4922                                 table_data->top_row = (GList*) iterator->data;
4923                         }
4924                 }
4925         }
4926         wm->state&= ~STATE_HIGHLIGHTED;
4927         gui_internal_menu_render(this);
4928 }
4929
4930
4931 /**
4932  * @brief deallocates a table_data structure.
4933  *
4934  */
4935 void gui_internal_table_data_free(void * p)
4936 {
4937
4938
4939         /**
4940          * @note button_box and its children (next_button,prev_button)
4941          * have their memory managed by the table itself.
4942          */
4943         struct table_data * table_data =  (struct table_data*) p;
4944         g_list_free(table_data->page_headers);
4945         g_free(p);
4946
4947
4948 }
4949
4950
4951 /**
4952  * @brief Called when the route is updated.
4953  */
4954 void gui_internal_route_update(struct gui_priv * this, struct navit * navit, struct vehicle *v)
4955 {
4956
4957         if(this->route_data.route_showing) {
4958                 gui_internal_populate_route_table(this,navit);
4959                 graphics_draw_mode(this->gra, draw_mode_begin);
4960                 gui_internal_menu_render(this);
4961                 graphics_draw_mode(this->gra, draw_mode_end);
4962         }
4963
4964
4965 }
4966
4967
4968 /**
4969  * @brief Called when the route screen is closed (deallocated).
4970  *
4971  * The main purpose of this function is to remove the widgets from
4972  * references route_data because those widgets are about to be freed.
4973  */
4974 void gui_internal_route_screen_free(struct gui_priv * this_,struct widget * w)
4975 {
4976         if(this_) {
4977                 this_->route_data.route_showing=0;
4978                 this_->route_data.route_table=NULL;
4979                 g_free(w);
4980         }
4981
4982 }
4983
4984 /**
4985  * @brief Populates the route  table with route information
4986  *
4987  * @param this The gui context
4988  * @param navit The navit object
4989  */
4990 void gui_internal_populate_route_table(struct gui_priv * this,
4991                                        struct navit * navit)
4992 {
4993         struct map * map=NULL;
4994         struct map_rect * mr=NULL;
4995         struct navigation * nav = NULL;
4996         struct item * item =NULL;
4997         struct attr attr;
4998         struct widget * label = NULL;
4999         struct widget * row = NULL;
5000         nav = navit_get_navigation(navit);
5001         if(!nav) {
5002                 return;
5003         }
5004         map = navigation_get_map(nav);
5005         if(map)
5006           mr = map_rect_new(map,NULL);
5007         if(mr) {
5008                 gui_internal_widget_table_clear(this,this->route_data.route_table);
5009                 while((item = map_rect_get_item(mr))) {
5010                         if(item_attr_get(item,attr_navigation_long,&attr)) {
5011                           label = gui_internal_label_new(this,attr.u.str);
5012                           row = gui_internal_widget_table_row_new(this,
5013                                                                   gravity_left
5014                                                                   | flags_fill
5015                                                                   | orientation_horizontal);
5016                           row->children=g_list_append(row->children,label);
5017                           gui_internal_widget_append(this->route_data.route_table,row);
5018                         }
5019
5020                 }
5021
5022         }
5023 }