Fix:map_csv:Disable default notification of each deleted item.
[profile/ivi/navit.git] / navit / navit / popup.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008, 2010 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include "config.h"
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <glib.h>
29 #include "popup.h"
30 #include "debug.h"
31 #include "navit.h"
32 #include "coord.h"
33 #include "gui.h"
34 #include "menu.h"
35 #include "point.h"
36 #include "transform.h"
37 #include "projection.h"
38 #include "item.h"
39 #include "map.h"
40 #include "graphics.h"
41 #include "callback.h"
42 #include "route.h"
43 #include "navit_nls.h"
44 #include "bookmarks.h"
45
46 #if 0
47 static void
48 popup_set_no_passing(struct popup_item *item, void *param)
49 {
50 #if 0
51         struct display_list *l=param;
52         struct segment *seg=(struct segment *)(l->data);
53         struct street_str *str=(struct street_str *)(seg->data[0]);
54         char log[256];
55         int segid=str->segid;
56         if (segid < 0)
57                 segid=-segid;
58
59         sprintf(log,"Attributes Street 0x%x updated: limit=0x%x(0x%x)", segid, 0x33, str->limit);
60         str->limit=0x33;
61         log_write(log, seg->blk_inf.file, str, sizeof(*str));
62 #endif
63 }
64
65 #endif
66
67 static void
68 popup_traffic_distortion(struct item *item, char *attr)
69 {
70         /* add the configuration directory to the name of the file to use */
71         char *dist_filename = g_strjoin(NULL, navit_get_user_data_directory(TRUE),
72                                                                         "/distortion.txt", NULL);
73         if (dist_filename)                                      /* if we built the filename */
74         {
75                 FILE *map=fopen(dist_filename,"a");
76                 if (map)                                                /* if the file was opened */
77                 {
78                         struct coord c;
79                         struct map_rect *mr;
80                         fprintf(map,"type=traffic_distortion %s\n",attr);
81                         mr=map_rect_new(item->map,NULL);
82                         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
83                         while (item_coord_get(item, &c, 1)) {
84                                 fprintf(map,"0x%x 0x%x\n",c.x,c.y);
85                         }
86                         fclose(map);
87                 }
88                 else
89                 {
90                         dbg(0,"could not open file for distortions !!", item);
91
92                 } /* else - if (map) */
93                 g_free(dist_filename);                  /* free the file name */
94         } /* if (dist_filename) */
95
96 } /* end: popup_traffic_distortion(..) */
97
98  
99 static void
100 popup_traffic_distortion_blocked(struct item *item)
101 {
102         dbg(0,"item=%p\n",item);
103         popup_traffic_distortion(item, "maxspeed=0");
104 }
105
106 static void
107 popup_traffic_distortion_speed(struct item *item, int maxspeed)
108 {
109         char buffer[256];
110         sprintf(buffer,"maxspeed=%d",maxspeed);
111         popup_traffic_distortion(item,buffer);
112 }
113
114 static void
115 popup_traffic_distortion_delay(struct item *item, int delay)
116 {
117         char buffer[256];
118         sprintf(buffer,"delay=%d",delay*600);
119         popup_traffic_distortion(item,buffer);
120 }
121
122 static void
123 popup_set_destination(struct navit *nav, struct pcoord *pc)
124 {
125         struct coord c;
126         struct coord_geo g;
127         char buffer[1024];
128         char buffer_geo[1024];
129         c.x = pc->x;
130         c.y = pc->y;
131         transform_to_geo(transform_get_projection(navit_get_trans(nav)), &c, &g);
132         coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer_geo,sizeof(buffer_geo));
133         sprintf(buffer,"Map Point %s", buffer_geo);
134         navit_set_destination(nav, pc, buffer, 1);
135 }
136
137 static void
138 popup_set_bookmark(struct navit *nav, struct pcoord *pc)
139 {
140     struct attr attr;
141         struct coord c;
142         struct coord_geo g;
143         char buffer[1024];
144         char buffer_geo[1024];
145         c.x = pc->x;
146         c.y = pc->y;
147         transform_to_geo(pc->pro, &c, &g);
148         coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer_geo,sizeof(buffer_geo));
149         sprintf(buffer,"Map Point %s", buffer_geo);
150         if (!gui_add_bookmark(navit_get_gui(nav), pc, buffer)) {
151         navit_get_attr(nav, attr_bookmarks, &attr, NULL);
152                 bookmarks_add_bookmark(attr.u.bookmarks, pc, buffer);
153     }
154 }
155
156
157 extern void *vehicle;
158
159 static void
160 popup_set_position(struct navit *nav, struct pcoord *pc)
161 {
162         dbg(1,"%p %p\n", nav, pc);
163         navit_set_position(nav, pc);
164 }
165
166 #if 0
167 static void
168 popup_break_crossing(struct display_list *l)
169 {
170         struct segment *seg=(struct segment *)(l->data);
171         struct street_str *str=(struct street_str *)(seg->data[0]);
172         char log[256];
173         int segid=str->segid;
174         if (segid < 0)
175                 segid=-segid;
176
177         sprintf(log,"Coordinates Street 0x%x updated: limit=0x%x(0x%x)", segid, 0x33, str->limit);
178         str->limit=0x33;
179         log_write(log, seg->blk_inf.file, str, sizeof(*str));
180 }
181 #endif
182
183
184 #define popup_printf(menu, type, ...) popup_printf_cb(menu, type, NULL, __VA_ARGS__)
185
186 static void *
187 popup_printf_cb(void *menu, enum menu_type type, struct callback *cb, const char *fmt, ...)
188 {
189         gchar *str,*us;
190         int usc=0;
191         va_list ap;
192         void *ret;
193
194         va_start(ap, fmt);
195         str=g_strdup_vprintf(fmt, ap);
196         dbg(0,"%s\n", str);
197         us=str;
198         while (*us) {
199                 if (*us == '_')
200                         usc++;
201                 us++;
202         }
203         if (usc) {
204                 gchar *str2=g_malloc(strlen(str)+usc+1);
205                 gchar *us2=str2;
206                 us=str;
207                 while (*us) {
208                         if (*us == '_')
209                                 *us2++=*us;
210                         *us2++=*us++;
211                 }
212                 *us2='\0';
213                 g_free(str);
214                 str=str2;
215         }
216         ret=menu_add(menu, str, type, cb);
217         va_end(ap);
218         g_free(str);
219         return ret;
220 }
221
222 static void
223 popup_show_attr_val(struct map *map, void *menu, struct attr *attr)
224 {
225         char *attr_name=attr_to_name(attr->type);
226         char *str;
227
228         str=attr_to_text(attr, map, 1);
229         popup_printf(menu, menu_type_menu, "%s: %s", attr_name, str);
230         g_free(str);
231 }
232
233 #if 0
234 static void
235 popup_show_attr(void *menu, struct item *item, enum attr_type attr_type)
236 {
237         struct attr attr;
238         memset(&attr, 0, sizeof(attr));
239         attr.type=attr_type;
240         if (item_attr_get(item, attr_type, &attr)) 
241                 popup_show_attr_val(menu, &attr);
242 }
243 #endif
244
245 static void
246 popup_show_attrs(struct map *map, void *menu, struct item *item)
247 {
248 #if 0
249         popup_show_attr(menu, item, attr_debug);
250         popup_show_attr(menu, item, attr_address);
251         popup_show_attr(menu, item, attr_phone);
252         popup_show_attr(menu, item, attr_phone);
253         popup_show_attr(menu, item, attr_entry_fee);
254         popup_show_attr(menu, item, attr_open_hours);
255 #else
256         struct attr attr;
257         for (;;) {
258                 memset(&attr, 0, sizeof(attr));
259                 if (item_attr_get(item, attr_any, &attr)) 
260                         popup_show_attr_val(map, menu, &attr);
261                 else
262                         break;
263         }
264         
265 #endif
266 }
267
268 static void
269 popup_item_dump(struct item *item)
270 {
271         struct map_rect *mr;
272         mr=map_rect_new(item->map,NULL);
273         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
274         dbg(0,"item=%p\n",item);
275         item_dump_filedesc(item,item->map,stdout);
276         map_rect_destroy(mr);
277 }
278
279
280 static void
281 popup_show_item(struct navit *nav, void *popup, struct displayitem *di)
282 {
283         struct map_rect *mr;
284         void *menu, *menu_map, *menu_item, *menu_dist;
285         char *label;
286         struct item *item,*diitem;
287         int count;
288
289         label=graphics_displayitem_get_label(di);
290         diitem=graphics_displayitem_get_item(di);
291         count=graphics_displayitem_get_coord_count(di);
292
293         dbg_assert(diitem);
294
295         if (label) 
296                 menu=popup_printf(popup, menu_type_submenu, "%s '%s' (%d coords)", item_to_name(diitem->type), label, count);
297         else
298                 menu=popup_printf(popup, menu_type_submenu, "%s (%d coords)", item_to_name(diitem->type), count);
299         menu_item=popup_printf(menu, menu_type_submenu, "Item");
300         popup_printf(menu_item, menu_type_menu, "type: 0x%x", diitem->type);
301         popup_printf(menu_item, menu_type_menu, "id: 0x%x 0x%x", diitem->id_hi, diitem->id_lo);
302         if (diitem->map) {
303                 struct attr type,data;
304                 if (!map_get_attr(diitem->map, attr_type, &type, NULL))
305                         type.u.str="";
306                 if (!map_get_attr(diitem->map, attr_data, &data, NULL))
307                         data.u.str="";
308                 popup_printf(menu_item, menu_type_menu, "map: %s:%s", type.u.str, data.u.str);
309         }
310         if (diitem->map) {
311                 mr=map_rect_new(diitem->map,NULL);
312                 item=map_rect_get_item_byid(mr, diitem->id_hi, diitem->id_lo);
313                 dbg(1,"item=%p\n", item);
314                 if (item) {
315                         popup_show_attrs(item->map, menu_item, item);
316                         popup_printf_cb(menu_item, menu_type_menu, callback_new_1(callback_cast(popup_item_dump), diitem), "Dump");
317                         if (item->type < type_line) {
318                                 struct coord co;
319                                 struct pcoord *c;
320                                 if (item_coord_get(item, &co, 1)) {
321                                         c=g_new(struct pcoord, 1);
322                                         c->pro = transform_get_projection(navit_get_trans(nav));
323                                         c->x = co.x;
324                                         c->y = co.y;
325                                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_set_position), nav, c), _("Set as position"));
326                                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_set_destination), nav, c), _("Set as destination"));
327                                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_set_bookmark), nav, c), _("Add as bookmark"));
328                                 }
329                         }
330                 }
331                 map_rect_destroy(mr);
332                 menu_map=popup_printf(menu, menu_type_submenu, "Map");
333         } else {
334                 popup_printf(menu, menu_type_menu, "(No map)");
335         }
336         if (item_get_default_flags(diitem->type)) {
337                 int speeds[]={5,10,20,30,40,50,60,70,80,90,100};
338                 int delays[]={1,2,3,5,10,15,20,30,45,60,75,90,120,150,180,240,300};
339                 int i;
340                 menu_dist=popup_printf(menu, menu_type_submenu, "Traffic distortion");
341                 popup_printf_cb(menu_dist, menu_type_menu, callback_new_1(callback_cast(popup_traffic_distortion_blocked), diitem), "Blocked");
342                 menu_item=popup_printf(menu_dist, menu_type_submenu,"Max speed");
343                 for (i = 0 ; i < sizeof(speeds)/sizeof(int); i++) {
344                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_traffic_distortion_speed), diitem, speeds[i]), "%d km/h",speeds[i]);
345                 }
346                 menu_item=popup_printf(menu_dist, menu_type_submenu,"Delay");
347                 for (i = 0 ; i < sizeof(delays)/sizeof(int); i++) {
348                         popup_printf_cb(menu_item, menu_type_menu, callback_new_2(callback_cast(popup_traffic_distortion_delay), diitem, delays[i]*600), "%d min",delays[i]);
349                 }
350         }
351 }
352
353 static void
354 popup_display(struct navit *nav, void *popup, struct point *p)
355 {
356         struct displaylist_handle *dlh;
357         struct displaylist *display;
358         struct displayitem *di;
359
360         display=navit_get_displaylist(nav);
361         dlh=graphics_displaylist_open(display);
362         while ((di=graphics_displaylist_next(dlh))) {
363                 if (graphics_displayitem_within_dist(display, di, p, 5)) {
364                         popup_show_item(nav, popup, di);
365                 }
366         }
367         graphics_displaylist_close(dlh);
368 }
369
370 static struct pcoord c;
371
372 void
373 popup(struct navit *nav, int button, struct point *p)
374 {
375         void *popup,*men;
376         char buffer[1024];
377         struct coord_geo g;
378         struct coord co;
379
380         popup=gui_popup_new(navit_get_gui(nav));
381         if (! popup)
382                 return;
383         transform_reverse(navit_get_trans(nav), p, &co);
384         men=popup_printf(popup, menu_type_submenu, _("Point 0x%x 0x%x"), co.x, co.y);
385         popup_printf(men, menu_type_menu, _("Screen coord : %d %d"), p->x, p->y);
386         transform_to_geo(transform_get_projection(navit_get_trans(nav)), &co, &g);
387         coord_format(g.lat,g.lng,DEGREES_MINUTES_SECONDS,buffer,sizeof(buffer));
388         popup_printf(men, menu_type_menu, "%s", buffer);
389         popup_printf(men, menu_type_menu, "%f %f", g.lat, g.lng);
390         dbg(1,"%p %p\n", nav, &c);
391         c.pro = transform_get_projection(navit_get_trans(nav));
392         c.x = co.x;
393         c.y = co.y;
394         popup_printf_cb(men, menu_type_menu, callback_new_2(callback_cast(popup_set_position), nav, &c), _("Set as position"));
395         popup_printf_cb(men, menu_type_menu, callback_new_2(callback_cast(popup_set_destination), nav, &c), _("Set as destination"));
396         popup_printf_cb(men, menu_type_menu, callback_new_2(callback_cast(popup_set_bookmark), nav, &c), _("Add as bookmark"));
397         popup_display(nav, popup, p);
398         menu_popup(popup);
399 }