Fix:Core:Double _ in popups to avoid it being mistaken for hotkey indication
[navit-package] / navit / popup.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 #include <stdarg.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <glib.h>
26 #include "popup.h"
27 #include "debug.h"
28 #include "navit.h"
29 #include "coord.h"
30 #include "gui.h"
31 #include "menu.h"
32 #include "point.h"
33 #include "transform.h"
34 #include "projection.h"
35 #include "item.h"
36 #include "map.h"
37 #include "graphics.h"
38 #include "callback.h"
39 #include "route.h"
40 #include "navit_nls.h"
41
42 #if 0
43 static void
44 popup_set_no_passing(struct popup_item *item, void *param)
45 {
46 #if 0
47         struct display_list *l=param;
48         struct segment *seg=(struct segment *)(l->data);
49         struct street_str *str=(struct street_str *)(seg->data[0]);
50         char log[256];
51         int segid=str->segid;
52         if (segid < 0)
53                 segid=-segid;
54
55         sprintf(log,"Attributes Street 0x%x updated: limit=0x%x(0x%x)", segid, 0x33, str->limit);
56         str->limit=0x33;
57         log_write(log, seg->blk_inf.file, str, sizeof(*str));
58 #endif
59 }
60
61 #endif
62
63 static void
64 popup_set_destination(struct navit *nav, struct pcoord *pc)
65 {
66         struct coord c;
67         struct coord_geo g;
68         char buffer[1024];
69         char buffer_geo[1024];
70         c.x = pc->x;
71         c.y = pc->y;
72         transform_to_geo(transform_get_projection(navit_get_trans(nav)), &c, &g);
73         coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer_geo,sizeof(buffer_geo));
74         sprintf(buffer,"Map Point %s", buffer_geo);
75         navit_set_destination(nav, pc, buffer);
76 }
77
78 static void
79 popup_set_bookmark(struct navit *nav, struct pcoord *pc)
80 {
81         struct coord c;
82         struct coord_geo g;
83         char buffer[1024];
84         char buffer_geo[1024];
85         c.x = pc->x;
86         c.y = pc->y;
87         transform_to_geo(pc->pro, &c, &g);
88         coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer_geo,sizeof(buffer_geo));
89         sprintf(buffer,"Map Point %s", buffer_geo);
90         if (!gui_add_bookmark(navit_get_gui(nav), pc, buffer)) 
91                 navit_add_bookmark(nav, pc, buffer);
92 }
93
94
95 extern void *vehicle;
96
97 static void
98 popup_set_position(struct navit *nav, struct pcoord *pc)
99 {
100         dbg(1,"%p %p\n", nav, pc);
101         navit_set_position(nav, pc);
102 }
103
104 #if 0
105 static void
106 popup_break_crossing(struct display_list *l)
107 {
108         struct segment *seg=(struct segment *)(l->data);
109         struct street_str *str=(struct street_str *)(seg->data[0]);
110         char log[256];
111         int segid=str->segid;
112         if (segid < 0)
113                 segid=-segid;
114
115         sprintf(log,"Coordinates Street 0x%x updated: limit=0x%x(0x%x)", segid, 0x33, str->limit);
116         str->limit=0x33;
117         log_write(log, seg->blk_inf.file, str, sizeof(*str));
118 }
119 #endif
120
121
122 #define popup_printf(menu, type, fmt...) popup_printf_cb(menu, type, NULL, fmt)
123
124 static void *
125 popup_printf_cb(void *menu, enum menu_type type, struct callback *cb, const char *fmt, ...)
126 {
127         gchar *str,*us;
128         int usc=0;
129         va_list ap;
130         void *ret;
131
132         va_start(ap, fmt);
133         str=g_strdup_vprintf(fmt, ap);
134         dbg(0,"%s\n", str);
135         us=str;
136         while (*us) {
137                 if (*us == '_')
138                         usc++;
139                 us++;
140         }
141         if (usc) {
142                 gchar *str2=g_malloc(strlen(str)+us+1);
143                 gchar *us2=str2;
144                 us=str;
145                 while (*us) {
146                         if (*us == '_')
147                                 *us2++=*us;
148                         *us2++=*us++;
149                 }
150                 *us2='\0';
151                 g_free(str);
152                 str=str2;
153         }
154         ret=menu_add(menu, str, type, cb);
155         va_end(ap);
156         g_free(str);
157         return ret;
158 }
159
160 static void
161 popup_show_attr_val(struct map *map, void *menu, struct attr *attr)
162 {
163         char *attr_name=attr_to_name(attr->type);
164         char *str;
165
166         str=attr_to_text(attr, map, 1);
167         popup_printf(menu, menu_type_menu, "%s: %s", attr_name, str);
168         g_free(str);
169 }
170
171 #if 0
172 static void
173 popup_show_attr(void *menu, struct item *item, enum attr_type attr_type)
174 {
175         struct attr attr;
176         memset(&attr, 0, sizeof(attr));
177         attr.type=attr_type;
178         if (item_attr_get(item, attr_type, &attr)) 
179                 popup_show_attr_val(menu, &attr);
180 }
181 #endif
182
183 static void
184 popup_show_attrs(struct map *map, void *menu, struct item *item)
185 {
186 #if 0
187         popup_show_attr(menu, item, attr_debug);
188         popup_show_attr(menu, item, attr_address);
189         popup_show_attr(menu, item, attr_phone);
190         popup_show_attr(menu, item, attr_phone);
191         popup_show_attr(menu, item, attr_entry_fee);
192         popup_show_attr(menu, item, attr_open_hours);
193 #else
194         struct attr attr;
195         for (;;) {
196                 memset(&attr, 0, sizeof(attr));
197                 if (item_attr_get(item, attr_any, &attr)) 
198                         popup_show_attr_val(map, menu, &attr);
199                 else
200                         break;
201         }
202         
203 #endif
204 }
205
206 static void
207 popup_show_item(struct navit *nav, void *popup, struct displayitem *di)
208 {
209         struct map_rect *mr;
210         void *menu, *menu_map, *menu_item;
211         char *label;
212         struct item *item;
213
214         label=graphics_displayitem_get_label(di);
215         item=graphics_displayitem_get_item(di);
216
217         if (label) 
218                 menu=popup_printf(popup, menu_type_submenu, "%s '%s'", item_to_name(item->type), label);
219         else
220                 menu=popup_printf(popup, menu_type_submenu, "%s", item_to_name(item->type));
221         menu_item=popup_printf(menu, menu_type_submenu, "Item");
222         popup_printf(menu_item, menu_type_menu, "type: 0x%x", item->type);
223         popup_printf(menu_item, menu_type_menu, "id: 0x%x 0x%x", item->id_hi, item->id_lo);
224         if (item->map) {
225                 mr=map_rect_new(item->map,NULL);
226                 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
227                 dbg(1,"item=%p\n", item);
228                 if (item) {
229                         popup_show_attrs(item->map, menu_item, item);
230                         if (item->type < type_line) {
231                                 struct coord co;
232                                 struct pcoord *c;
233                                 if (item_coord_get(item, &co, 1)) {
234                                         c=g_new(struct pcoord, 1);
235                                         c->pro = transform_get_projection(navit_get_trans(nav));
236                                         c->x = co.x;
237                                         c->y = co.y;
238                                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_set_position), nav, c), _("Set as position"));
239                                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_set_destination), nav, c), _("Set as destination"));
240                                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_set_bookmark), nav, c), _("Add as bookmark"));
241                                 }
242                         }
243                 }
244                 map_rect_destroy(mr);
245                 menu_map=popup_printf(menu, menu_type_submenu, "Map");
246         } else {
247                 popup_printf(menu, menu_type_menu, "(No map)");
248         }
249 }
250
251 static void
252 popup_display(struct navit *nav, void *popup, struct point *p)
253 {
254         struct displaylist_handle *dlh;
255         struct displaylist *display;
256         struct displayitem *di;
257
258         display=navit_get_displaylist(nav);
259         dlh=graphics_displaylist_open(display);
260         while ((di=graphics_displaylist_next(dlh))) {
261                 if (graphics_displayitem_within_dist(display, di, p, 5)) {
262                         popup_show_item(nav, popup, di);
263                 }
264         }
265         graphics_displaylist_close(dlh);
266 }
267
268 static struct pcoord c;
269
270 void
271 popup(struct navit *nav, int button, struct point *p)
272 {
273         void *popup,*men;
274         char buffer[1024];
275         struct coord_geo g;
276         struct coord co;
277
278         popup=gui_popup_new(navit_get_gui(nav));
279         if (! popup)
280                 return;
281         transform_reverse(navit_get_trans(nav), p, &co);
282         men=popup_printf(popup, menu_type_submenu, _("Point 0x%x 0x%x"), co.x, co.y);
283         popup_printf(men, menu_type_menu, _("Screen coord : %d %d"), p->x, p->y);
284         transform_to_geo(transform_get_projection(navit_get_trans(nav)), &co, &g);
285         coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer,sizeof(buffer));
286         popup_printf(men, menu_type_menu, "%s", buffer);
287         popup_printf(men, menu_type_menu, "%f %f", g.lat, g.lng);
288         dbg(1,"%p %p\n", nav, &c);
289         c.pro = transform_get_projection(navit_get_trans(nav));
290         c.x = co.x;
291         c.y = co.y;
292         popup_printf_cb(men, menu_type_menu, callback_new_2(callback_cast(popup_set_position), nav, &c), _("Set as position"));
293         popup_printf_cb(men, menu_type_menu, callback_new_2(callback_cast(popup_set_destination), nav, &c), _("Set as destination"));
294         popup_printf_cb(men, menu_type_menu, callback_new_2(callback_cast(popup_set_bookmark), nav, &c), _("Add as bookmark"));
295         popup_display(nav, popup, p);
296         menu_popup(popup);
297 }