13 #include "transform.h"
14 #include "projection.h"
20 #include "navigation.h"
27 struct layout *layout_current;
29 struct action *action;
30 struct transformation *trans;
31 struct compass *compass;
32 struct map_data *map_data;
35 struct statusbar *statusbar;
38 struct navigation *navigation;
39 struct cursor *cursor;
40 struct speech *speech;
41 struct vehicle *vehicle;
43 struct map_flags *flags;
46 struct displaylist *displaylist;
59 navit_add_mapset(struct navit *this_, struct mapset *ms)
61 this_->mapsets = g_list_append(this_->mapsets, ms);
65 navit_get_mapset(struct navit *this_)
67 return this_->mapsets->data;
71 navit_add_layout(struct navit *this_, struct layout *lay)
73 this_->layouts = g_list_append(this_->layouts, lay);
77 navit_draw(struct navit *this_)
79 transform_setup_source_rect(this_->trans);
80 graphics_draw(this_->gra, this_->displaylist, this_->mapsets, this_->trans, this_->layouts, this_->route);
85 navit_draw_displaylist(struct navit *this_)
88 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layouts, this_->route);
93 navit_resize(void *data, int w, int h)
95 struct navit *this_=data;
96 transform_set_size(this_->trans, w, h);
101 navit_button(void *data, int pressed, int button, struct point *p)
103 struct navit *this_=data;
104 if (pressed && button == 1) {
106 if (! transform_within_border(this_->trans, p, border)) {
107 navit_set_center_screen(this_, p);
109 popup(this_, button, p);
111 if (pressed && button == 2)
112 navit_set_center_screen(this_, p);
113 if (pressed && button == 3)
114 popup(this_, button, p);
115 if (pressed && button == 4)
116 navit_zoom_in(this_, 2);
117 if (pressed && button == 5)
118 navit_zoom_out(this_, 2);
122 navit_zoom_in(struct navit *this_, int factor)
124 long scale=transform_get_scale(this_->trans)/factor;
127 transform_set_scale(this_->trans, scale);
132 navit_zoom_out(struct navit *this_, int factor)
134 long scale=transform_get_scale(this_->trans)*factor;
135 transform_set_scale(this_->trans,scale);
140 navit_new(const char *ui, const char *graphics, struct coord *center, enum projection pro, int zoom)
142 struct navit *this_=g_new0(struct navit, 1);
144 this_->cursor_flag=1;
145 this_->trans=transform_new();
146 transform_set_projection(this_->trans, pro);
148 transform_setup(this_->trans, center, zoom, 0);
149 /* this_->flags=g_new0(struct map_flags, 1); */
150 this_->displaylist=graphics_displaylist_new();
151 this_->gui=gui_new(this_, ui, 792, 547);
153 g_warning("failed to create gui '%s'", ui);
154 navit_destroy(this_);
157 if (gui_has_main_loop(this_->gui)) {
158 if (! main_loop_gui) {
159 main_loop_gui=this_->gui;
161 g_warning("gui with main loop already active, ignoring this instance");
162 navit_destroy(this_);
166 this_->menubar=gui_menubar_new(this_->gui);
167 this_->toolbar=gui_toolbar_new(this_->gui);
168 this_->statusbar=gui_statusbar_new(this_->gui);
169 this_->gra=graphics_new(graphics);
171 g_warning("failed to create graphics '%s'", graphics);
172 navit_destroy(this_);
175 graphics_register_resize_callback(this_->gra, navit_resize, this_);
176 graphics_register_button_callback(this_->gra, navit_button, this_);
177 if (gui_set_graphics(this_->gui, this_->gra)) {
178 g_warning("failed to connect graphics '%s' to gui '%s'\n", graphics, ui);
179 navit_destroy(this_);
182 graphics_init(this_->gra);
187 navit_map_toggle(struct menu *menu, void *this__p, void *map_p)
189 if ((menu_get_toggle(menu) != 0) != (map_get_active(map_p) != 0)) {
190 map_set_active(map_p, (menu_get_toggle(menu) != 0));
196 navit_projection_set(struct menu *menu, void *this__p, void *pro_p)
198 struct navit *this_=this__p;
199 enum projection pro=(enum projection) pro_p;
203 c=transform_center(this_->trans);
204 transform_to_geo(transform_get_projection(this_->trans), c, &g);
205 transform_set_projection(this_->trans, pro);
206 transform_from_geo(pro, &g, c);
211 navit_set_destination_menu(struct menu *menu, void *this__p, void *c_p)
213 struct navit *this_=this__p;
216 route_set_destination(this_->route, c);
223 navit_set_destination(struct navit *this_, struct coord *c, char *description)
227 buffer=g_strdup_printf("0x%x 0x%x %s\n", c->x, c->y, description);
228 fd=open("destination.txt", O_RDWR|O_CREAT|O_APPEND, 0644);
230 write(fd, buffer, strlen(buffer));
234 route_set_destination(this_->route, c);
239 struct navit *global_navit;
242 navit_debug(struct navit *this_)
249 struct search_list *sl;
250 struct search_list_result *res;
252 debug_level_set("data_mg:town_search_get_item",1);
253 debug_level_set("data_mg:town_search_compare",1);
254 debug_level_set("data_mg:tree_search_next",1);
255 sl=search_list_new(this_->mapsets->data);
256 attr.type=attr_country_all;
258 search_list_search(sl, &attr, 1);
259 while (res=search_list_get_result(sl)) {
260 printf("country result\n");
262 attr.type=attr_town_name;
264 search_list_search(sl, &attr, 1);
265 while (res=search_list_get_result(sl)) {
266 printf("town result\n");
268 search_list_destroy(sl);
273 navit_show_roadbook(struct navigation *nav, void *data)
275 struct navigation_list *list;
278 list=navigation_list_new(nav);
279 while ((str=navigation_list_get(list, navigation_mode_long))) {
282 navigation_list_destroy(list);
286 navit_init(struct navit *this_)
288 struct menu *mapmen,*men,*men2;
290 struct mapset_handle *handle;
291 struct mapset *ms=this_->mapsets->data;
293 if (this_->menubar) {
294 mapmen=menu_add(this_->menubar, "Map", menu_type_submenu, NULL, NULL, NULL);
295 // menu_add(map, "Test", menu_type_menu, NULL, NULL);
296 men=menu_add(mapmen, "Layout", menu_type_submenu, NULL, NULL, NULL);
297 menu_add(men, "Test", menu_type_menu, NULL, NULL, NULL);
298 men=menu_add(mapmen, "Projection", menu_type_submenu, NULL, NULL, NULL);
299 menu_add(men, "M&G", menu_type_menu, navit_projection_set, this_, (void *)projection_mg);
300 menu_add(men, "Garmin", menu_type_menu, navit_projection_set, this_, (void *)projection_garmin);
301 handle=mapset_open(ms);
302 while ((map=mapset_next(handle,0))) {
303 char *s=g_strdup_printf("%s:%s", map_get_type(map), map_get_filename(map));
304 men2=menu_add(mapmen, s, menu_type_toggle, navit_map_toggle, this_, map);
305 menu_set_toggle(men2, map_get_active(map));
308 mapset_close(handle);
311 struct mapset *ms=this_->mapsets->data;
317 this_->route=route_new(ms);
318 this_->navigation=navigation_new(ms);
319 dbg(0,"navigation_register_callback(%p, ... %p)\n", this_->navigation, this_);
320 navigation_register_callback(this_->navigation, navigation_mode_long, navit_show_roadbook, this_);
322 this_->track=track_new(ms);
325 if (this_->menubar) {
326 men=menu_add(this_->menubar, "Route", menu_type_submenu, NULL, NULL, NULL);
327 men=menu_add(men, "Former Destinations", menu_type_submenu, NULL, NULL, NULL);
329 f=fopen("destination.txt", "r");
331 while (! feof(f) && fgets(buffer, 2048, f)) {
332 if ((pos=coord_parse(buffer, projection_mg, &c))) {
333 if (buffer[pos] && buffer[pos] != '\n' ) {
334 struct coord *cn=g_new(struct coord, 1);
336 buffer[strlen(buffer)-1]='\0';
338 menu_add(men, buffer+pos+1, menu_type_menu, navit_set_destination_menu, this_, cn);
345 route_set_destination(this_->route, &c);
353 navit_set_center(struct navit *this_, struct coord *center)
355 struct coord *c=transform_center(this_->trans);
362 navit_set_center_cursor(struct navit *this_, struct coord *cursor, int dir, int xpercent, int ypercent)
364 struct coord *c=transform_center(this_->trans);
369 transform_get_size(this_->trans, &width, &height);
371 transform_set_angle(this_->trans, dir);
372 p.x=(100-xpercent)*width/100;
373 p.y=(100-ypercent)*height/100;
374 transform_reverse(this_->trans, &p, &cnew);
383 navit_set_center_screen(struct navit *this_, struct point *p)
386 transform_reverse(this_->trans, p, &c);
387 navit_set_center(this_, &c);
391 navit_toggle_cursor(struct navit *this_)
393 this_->cursor_flag=1-this_->cursor_flag;
397 navit_cursor_offscreen(struct cursor *cursor, void *this__p)
399 struct navit *this_=this__p;
401 if (this_->cursor_flag)
402 navit_set_center(this_, cursor_pos_get(cursor));
406 navit_cursor_update(struct cursor *cursor, void *this__p)
408 struct navit *this_=this__p;
409 struct coord *cursor_c=cursor_pos_get(cursor);
410 int dir=cursor_get_dir(cursor);
413 struct coord c=*cursor_c;
414 if (track_update(this_->track, &c, dir)) {
416 cursor_pos_set(cursor, cursor_c);
417 if (this_->route && this_->update_curr == 1)
418 route_set_position_from_track(this_->route, this_->track);
421 if (this_->route && this_->update_curr == 1)
422 route_set_position(this_->route, cursor_c);
424 if (this_->route && this_->update_curr == 1)
425 navigation_update(this_->navigation, this_->route);
426 if (this_->cursor_flag) {
427 if (this_->follow_curr == 1)
428 navit_set_center_cursor(this_, cursor_c, dir, 50, 80);
430 if (this_->follow_curr > 1)
431 this_->follow_curr--;
433 this_->follow_curr=this_->follow;
434 if (this_->update_curr > 1)
435 this_->update_curr--;
437 this_->update_curr=this_->update;
441 navit_set_position(struct navit *this_, struct coord *c)
444 route_set_position(this_->route, c);
445 if (this_->navigation) {
446 navigation_update(this_->navigation, this_->route);
453 navit_vehicle_add(struct navit *this_, struct vehicle *v, struct color *c, int update, int follow)
456 this_->update_curr=this_->update=update;
457 this_->follow_curr=this_->follow=follow;
458 this_->cursor=cursor_new(this_->gra, v, c, this_->trans);
459 cursor_register_offscreen_callback(this_->cursor, navit_cursor_offscreen, this_);
460 cursor_register_update_callback(this_->cursor, navit_cursor_update, this_);
465 navit_get_gui(struct navit *this_)
470 struct transformation *
471 navit_get_trans(struct navit *this_)
477 navit_get_route(struct navit *this_)
483 navit_get_navigation(struct navit *this_)
485 return this_->navigation;
489 navit_get_displaylist(struct navit *this_)
491 return this_->displaylist;
495 navit_destroy(struct navit *this_)