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