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