Fix:Core:Removed annoying debug message
[navit-package] / navit / graphics.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: graphics.c
23 //# Description: 
24 //# Comment: 
25 //# Authors: Martin Schaller (04/2008)
26 //#
27 //##############################################################################################################
28
29 #include <glib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include "debug.h"
34 #include "string.h"
35 #include "draw_info.h"
36 #include "point.h"
37 #include "graphics.h"
38 #include "projection.h"
39 #include "item.h"
40 #include "map.h"
41 #include "coord.h"
42 #include "transform.h"
43 #include "plugin.h"
44 #include "profile.h"
45 #include "mapset.h"
46 #include "layout.h"
47 #include "route.h"
48 #include "util.h"
49 #include "callback.h"
50 #include "file.h"
51
52 static char *navit_sharedir;
53
54 //##############################################################################################################
55 //# Description: 
56 //# Comment: 
57 //# Authors: Martin Schaller (04/2008)
58 //##############################################################################################################
59 struct graphics
60 {
61         struct graphics_priv *priv;
62         struct graphics_methods meth;
63         char *default_font;
64         struct graphics_font *font[16];
65         struct graphics_gc *gc[3];
66         struct attr **attrs;
67         struct callback_list *cbl;
68         struct point_rect r;
69 };
70
71 struct display_context
72 {
73         struct graphics *gra;
74         struct element *e;
75         struct graphics_gc *gc;
76         struct graphics_image *img;
77         enum projection pro;
78         struct transformation *trans;
79 };
80
81 struct displaylist {
82         GHashTable *dl;
83         int busy;
84         int workload;
85         struct callback *cb;
86         struct layout *layout;
87         struct display_context dc;
88         int order;
89         struct mapset *ms;
90         struct mapset_handle *msh;
91         struct map *m;
92         int conv;
93         struct map_selection *sel;
94         struct map_rect *mr;
95         struct callback *idle_cb;
96         struct event_idle *idle_ev;
97 };
98
99
100 void
101 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
102 {
103         gra->r=*pr;
104 }
105
106 /**
107  * Creates a new graphics object
108  * attr type required
109  * @param <>
110  * @returns <>
111  * @author Martin Schaller (04/2008)
112 */
113 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
114 {
115         struct graphics *this_;
116         struct attr *type_attr;
117         struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl);
118
119         if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
120                 return NULL;
121         }
122
123         graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
124         if (! graphicstype_new)
125                 return NULL;
126         this_=g_new0(struct graphics, 1);
127         this_->cbl=callback_list_new();
128         this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
129         this_->attrs=attr_list_dup(attrs);
130         return this_;
131 }
132
133 /**
134  * FIXME
135  * @param <>
136  * @returns <>
137  * @author Martin Schaller (04/2008)
138 */
139 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
140 {
141         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
142 }
143
144 /**
145  * FIXME
146  * @param <>
147  * @returns <>
148  * @author Martin Schaller (04/2008)
149 */
150 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha, int wraparound)
151 {
152         struct graphics *this_;
153         if (!parent->meth.overlay_new)
154                 return NULL;
155         this_=g_new0(struct graphics, 1);
156         this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha, wraparound);
157         if (!this_->priv) {
158                 g_free(this_);
159                 this_=NULL;
160         }
161         return this_;
162 }
163
164 /**
165  * FIXME
166  * @param <>
167  * @returns <>
168  * @author Martin Schaller (04/2008)
169 */
170 void graphics_init(struct graphics *this_)
171 {
172         this_->gc[0]=graphics_gc_new(this_);
173         graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
174         graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
175         this_->gc[1]=graphics_gc_new(this_);
176         graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
177         graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
178         this_->gc[2]=graphics_gc_new(this_);
179         graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
180         graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
181         graphics_background_gc(this_, this_->gc[0]);
182         navit_sharedir = getenv("NAVIT_SHAREDIR");
183 }
184
185 /**
186  * FIXME
187  * @param <>
188  * @returns <>
189  * @author Martin Schaller (04/2008)
190 */
191 void * graphics_get_data(struct graphics *this_, char *type)
192 {
193         return (this_->meth.get_data(this_->priv, type));
194 }
195
196 void graphics_add_callback(struct graphics *this_, struct callback *cb)
197 {
198         callback_list_add(this_->cbl, cb);
199 }
200
201 void graphics_remove_callback(struct graphics *this_, struct callback *cb)
202 {
203         callback_list_remove(this_->cbl, cb);
204 }
205
206 /**
207  * FIXME
208  * @param <>
209  * @returns <>
210  * @author Martin Schaller (04/2008)
211 */
212 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
213 {
214         struct graphics_font *this_;
215
216         this_=g_new0(struct graphics_font,1);
217         this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
218         return this_;
219 }
220
221 /**
222  * Free all loaded fonts.
223  * Used when switching layouts.
224  * @param gra The graphics instance
225  * @returns nothing
226  * @author Sarah Nordstrom (05/2008)
227  */
228 void graphics_font_destroy_all(struct graphics *gra) 
229
230         int i; 
231         for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) { 
232                 if(!gra->font[i]) continue; 
233                 gra->font[i]->meth.font_destroy(gra->font[i]->priv); 
234                 gra->font[i] = NULL; 
235         }
236 }
237
238 /**
239  * FIXME
240  * @param <>
241  * @returns <>
242  * @author Martin Schaller (04/2008)
243 */
244 struct graphics_gc * graphics_gc_new(struct graphics *gra)
245 {
246         struct graphics_gc *this_;
247
248         this_=g_new0(struct graphics_gc,1);
249         this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
250         return this_;
251 }
252
253 /**
254  * FIXME
255  * @param <>
256  * @returns <>
257  * @author Martin Schaller (04/2008)
258 */
259 void graphics_gc_destroy(struct graphics_gc *gc)
260 {
261         gc->meth.gc_destroy(gc->priv);
262         g_free(gc);
263 }
264
265 /**
266  * FIXME
267  * @param <>
268  * @returns <>
269  * @author Martin Schaller (04/2008)
270 */
271 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
272 {
273         gc->meth.gc_set_foreground(gc->priv, c);
274 }
275
276 /**
277  * FIXME
278  * @param <>
279  * @returns <>
280  * @author Martin Schaller (04/2008)
281 */
282 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
283 {
284         gc->meth.gc_set_background(gc->priv, c);
285 }
286
287 /**
288  * FIXME
289  * @param <>
290  * @returns <>
291  * @author Martin Schaller (04/2008)
292 */
293 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
294 {
295         gc->meth.gc_set_linewidth(gc->priv, width);
296 }
297
298 /**
299  * FIXME
300  * @param <>
301  * @returns <>
302  * @author Martin Schaller (04/2008)
303 */
304 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
305 {
306         if (gc->meth.gc_set_dashes)
307                 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
308 }
309
310 /**
311  * Create a new image from file path scaled to w and h pixels
312  * @param gra the graphics instance
313  * @param path path of the image to load
314  * @param w width to rescale to
315  * @param h height to rescale to
316  * @returns <>
317  * @author Martin Schaller (04/2008)
318 */
319 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
320 {
321         struct graphics_image *this_;
322
323         this_=g_new0(struct graphics_image,1);
324         this_->height=h;
325         this_->width=w;
326         this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, 0);
327         if (! this_->priv) {
328                 g_free(this_);
329                 this_=NULL;
330         }
331         return this_;
332 }
333
334 /**
335  * Create a new image from file path scaled to w and h pixels and possibly rotated
336  * @param gra the graphics instance
337  * @param path path of the image to load
338  * @param w width to rescale to
339  * @param h height to rescale to
340  * @param rotate angle to rotate the image. Warning, graphics might only support 90 degree steps here
341  * @returns <>
342  * @author Martin Schaller (04/2008)
343 */
344 struct graphics_image * graphics_image_new_scaled_rotated(struct graphics *gra, char *path, int w, int h, int rotate)
345 {
346         struct graphics_image *this_;
347
348         this_=g_new0(struct graphics_image,1);
349         this_->height=h;
350         this_->width=w;
351         this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, rotate);
352         if (! this_->priv) {
353                 g_free(this_);
354                 this_=NULL;
355         }
356         return this_;
357 }
358
359 /**
360  * Create a new image from file path
361  * @param gra the graphics instance
362  * @param path path of the image to load
363  * @returns <>
364  * @author Martin Schaller (04/2008)
365 */
366 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
367 {
368         return graphics_image_new_scaled(gra, path, -1, -1);
369 }
370
371 /**
372  * FIXME
373  * @param <>
374  * @returns <>
375  * @author Martin Schaller (04/2008)
376 */
377 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
378 {
379         if (gra->meth.image_free)
380                 gra->meth.image_free(gra->priv, img->priv);
381         g_free(img);
382 }
383
384 /**
385  * FIXME
386  * @param <>
387  * @returns <>
388  * @author Martin Schaller (04/2008)
389 */
390 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
391 {
392         this_->meth.draw_restore(this_->priv, p, w, h);
393 }
394
395 /**
396  * FIXME
397  * @param <>
398  * @returns <>
399  * @author Martin Schaller (04/2008)
400 */
401 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
402 {
403         this_->meth.draw_mode(this_->priv, mode);
404 }
405
406 /**
407  * FIXME
408  * @param <>
409  * @returns <>
410  * @author Martin Schaller (04/2008)
411 */
412 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
413 {
414         this_->meth.draw_lines(this_->priv, gc->priv, p, count);
415 }
416
417 /**
418  * FIXME
419  * @param <>
420  * @returns <>
421  * @author Martin Schaller (04/2008)
422 */
423 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
424 {
425         this_->meth.draw_circle(this_->priv, gc->priv, p, r);
426 }
427
428 /**
429  * FIXME
430  * @param <>
431  * @returns <>
432  * @author Martin Schaller (04/2008)
433 */
434 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
435 {
436         this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
437 }
438
439 /**
440  * FIXME
441  * @param <>
442  * @returns <>
443  * @author Martin Schaller (04/2008)
444 */
445 void graphics_draw_text(struct graphics *this_, struct graphics_gc *gc1, struct graphics_gc *gc2, struct graphics_font *font, char *text, struct point *p, int dx, int dy)
446 {
447         this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
448 }
449
450 /**
451  * FIXME
452  * @param <>
453  * @returns <>
454  * @author Martin Schaller (04/2008)
455 */
456 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret, int estimate)
457 {
458         this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret, estimate);
459 }
460
461 /**
462  * FIXME
463  * @param <>
464  * @returns <>
465  * @author Martin Schaller (04/2008)
466 */
467 void graphics_overlay_disable(struct graphics *this_, int disable)
468 {
469         if (this_->meth.overlay_disable)
470                 this_->meth.overlay_disable(this_->priv, disable);
471 }
472
473 /**
474  * FIXME
475  * @param <>
476  * @returns <>
477  * @author Martin Schaller (04/2008)
478 */
479 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
480 {
481         this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
482 }
483
484
485 //##############################################################################################################
486 //# Description:
487 //# Comment:
488 //# Authors: Martin Schaller (04/2008)
489 //##############################################################################################################
490 int
491 graphics_draw_drag(struct graphics *this_, struct point *p)
492 {
493         if (!this_->meth.draw_drag)
494                 return 0;
495         this_->meth.draw_drag(this_->priv, p);
496         return 1;
497 }
498
499 void
500 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
501 {
502         this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
503 }
504
505 #include "attr.h"
506 #include "popup.h"
507 #include <stdio.h>
508
509
510 #if 0
511 //##############################################################################################################
512 //# Description: 
513 //# Comment: 
514 //# Authors: Martin Schaller (04/2008)
515 //##############################################################################################################
516 static void popup_view_html(struct popup_item *item, char *file)
517 {
518         char command[1024];
519         sprintf(command,"firefox %s", file);
520         system(command);
521 }
522
523 struct transformatin *tg;
524 enum projection pg;
525
526 //##############################################################################################################
527 //# Description: 
528 //# Comment: 
529 //# Authors: Martin Schaller (04/2008)
530 //##############################################################################################################
531 static void graphics_popup(struct display_list *list, struct popup_item **popup)
532 {
533         struct item *item;
534         struct attr attr;
535         struct map_rect *mr;
536         struct coord c;
537         struct popup_item *curr_item,*last=NULL;
538         item=list->data;
539         mr=map_rect_new(item->map, NULL, NULL, 0);
540         printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
541         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
542         if (item) {
543                 if (item_attr_get(item, attr_name, &attr)) {
544                         curr_item=popup_item_new_text(popup,attr.u.str,1);
545                         if (item_attr_get(item, attr_info_html, &attr)) {
546                                 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
547                         }
548                         if (item_attr_get(item, attr_price_html, &attr)) {
549                                 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
550                         }
551                         curr_item->submenu=last;
552                 }
553         }
554         map_rect_destroy(mr);
555 }
556 #endif
557
558 /**
559  * FIXME
560  * @param <>
561  * @returns <>
562  * @author Martin Schaller (04/2008)
563 */
564 struct displayitem {
565         struct item item;
566         char *label;
567         int displayed;
568         int count;
569         struct coord c[0];
570 };
571
572 /**
573  * FIXME
574  * @param <>
575  * @returns <>
576  * @author Martin Schaller (04/2008)
577 */
578 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
579 {
580         GHashTable *hash=value;
581         if (hash) 
582                 g_hash_table_destroy(hash);
583         return TRUE;
584 }
585
586 /**
587  * FIXME
588  * @param <>
589  * @returns <>
590  * @author Martin Schaller (04/2008)
591 */
592 static void xdisplay_free(GHashTable *display_list)
593 {
594         g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
595 }
596
597 static guint
598 displayitem_hash(gconstpointer key)
599 {
600         const struct displayitem *di=key;
601         return (di->item.id_hi^di->item.id_lo^((int) di->item.map));
602 }
603
604 static gboolean
605 displayitem_equal(gconstpointer a, gconstpointer b)
606 {
607         const struct displayitem *dia=a;
608         const struct displayitem *dib=b;
609         if (item_is_equal(dia->item, dib->item))
610                 return TRUE;
611         return FALSE;
612 }
613
614
615 /**
616  * FIXME
617  * @param <>
618  * @returns <>
619  * @author Martin Schaller (04/2008)
620 */
621 static void display_add(struct displaylist *displaylist, struct item *item, int count, struct coord *c, char *label)
622 {
623         struct displayitem *di;
624         int len;
625         GHashTable *h;
626         char *p;
627
628         len=sizeof(*di)+count*sizeof(*c);
629         if (label)
630                 len+=strlen(label)+1;
631
632         p=g_malloc(len);
633
634         di=(struct displayitem *)p;
635         di->displayed=0;
636         p+=sizeof(*di)+count*sizeof(*c);
637         di->item=*item;
638         if (label) {
639                 di->label=p;
640                 strcpy(di->label, label);
641         } else 
642                 di->label=NULL;
643         di->count=count;
644         memcpy(di->c, c, count*sizeof(*c));
645
646         h=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
647         if (! h) {
648                 h=g_hash_table_new_full(displayitem_hash, displayitem_equal, g_free, NULL);
649                 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), h);
650         }
651         g_hash_table_replace(h, di, NULL);
652 }
653
654
655 /**
656  * FIXME
657  * @param <>
658  * @returns <>
659  * @author Martin Schaller (04/2008)
660 */
661 static void label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
662 {
663         int i,x,y,tl,tlm,th,thm,tlsq,l;
664         float lsq;
665         double dx,dy;
666         struct point p_t;
667         struct point pb[5];
668
669         if (gra->meth.get_text_bbox) {
670                 gra->meth.get_text_bbox(gra->priv, font->priv, label, 0x10000, 0x0, pb, 1);
671                 tl=(pb[2].x-pb[0].x);
672                 th=(pb[0].y-pb[1].y);
673         } else {
674                 tl=strlen(label)*4;
675                 th=8;
676         }
677         tlm=tl*128;
678         thm=th*144;
679         tlsq = tlm*tlm;
680         for (i = 0 ; i < count-1 ; i++) {
681                 dx=p[i+1].x-p[i].x;
682                 dx*=128;
683                 dy=p[i+1].y-p[i].y;
684                 dy*=128;
685                 lsq = dx*dx+dy*dy;
686                 if (lsq > tlsq) {
687                         l=(int)sqrtf(lsq);
688                         x=p[i].x;
689                         y=p[i].y;
690                         if (dx < 0) {
691                                 dx=-dx;
692                                 dy=-dy;
693                                 x=p[i+1].x;
694                                 y=p[i+1].y;
695                         }
696                         x+=(l-tlm)*dx/l/256;
697                         y+=(l-tlm)*dy/l/256;
698                         x-=dy*thm/l/256;
699                         y+=dx*thm/l/256;
700                         p_t.x=x;
701                         p_t.y=y;
702 #if 0
703                         dbg(0,"display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
704 #endif
705                         if (x < gra->r.rl.x && x + tl > gra->r.lu.x && y + tl > gra->r.lu.y && y - tl < gra->r.rl.y) 
706                                 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
707                 }
708         }
709 }
710
711 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
712 {
713         struct point pnt[3];
714         pnt[0]=pnt[1]=pnt[2]=*p;
715         pnt[0].x+=-dx*l/65536+dy*l/65536;
716         pnt[0].y+=-dy*l/65536-dx*l/65536;
717         pnt[2].x+=-dx*l/65536-dy*l/65536;
718         pnt[2].y+=-dy*l/65536+dx*l/65536;
719         gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
720 }
721
722 static void display_draw_arrows(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count)
723 {
724         int i,dx,dy,l;
725         struct point p;
726         for (i = 0 ; i < count-1 ; i++) {
727                 dx=pnt[i+1].x-pnt[i].x; 
728                 dy=pnt[i+1].y-pnt[i].y;
729                 l=sqrt(dx*dx+dy*dy);
730                 if (l) {
731                         dx=dx*65536/l;
732                         dy=dy*65536/l;
733                         p=pnt[i];
734                         p.x+=dx*15/65536;
735                         p.y+=dy*15/65536;
736                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
737                         p=pnt[i+1];
738                         p.x-=dx*15/65536;
739                         p.y-=dy*15/65536;
740                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
741                 }
742         }
743 }
744
745 static int
746 intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, int bdy,
747               struct point * res)
748 {
749         int n, a, b;
750         n = bdy * adx - bdx * ady;
751         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
752         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
753         if (n < 0) {
754                 n = -n;
755                 a = -a;
756                 b = -b;
757         }
758 #if 0
759         if (a < 0 || b < 0)
760                 return 0;
761         if (a > n || b > n)
762                 return 0;
763 #endif
764         if (n == 0)
765                 return 0;
766         res->x = a1->x + a * adx / n;
767         res->y = a1->y + a * ady / n;
768         return 1;
769 }
770
771 struct circle {
772         short x,y,fowler;
773 } circle64[]={
774 {0,128,0},
775 {13,127,13},
776 {25,126,25},
777 {37,122,38},
778 {49,118,53},
779 {60,113,67},
780 {71,106,85},
781 {81,99,104},
782 {91,91,128},
783 {99,81,152},
784 {106,71,171},
785 {113,60,189},
786 {118,49,203},
787 {122,37,218},
788 {126,25,231},
789 {127,13,243},
790 {128,0,256},
791 {127,-13,269},
792 {126,-25,281},
793 {122,-37,294},
794 {118,-49,309},
795 {113,-60,323},
796 {106,-71,341},
797 {99,-81,360},
798 {91,-91,384},
799 {81,-99,408},
800 {71,-106,427},
801 {60,-113,445},
802 {49,-118,459},
803 {37,-122,474},
804 {25,-126,487},
805 {13,-127,499},
806 {0,-128,512},
807 {-13,-127,525},
808 {-25,-126,537},
809 {-37,-122,550},
810 {-49,-118,565},
811 {-60,-113,579},
812 {-71,-106,597},
813 {-81,-99,616},
814 {-91,-91,640},
815 {-99,-81,664},
816 {-106,-71,683},
817 {-113,-60,701},
818 {-118,-49,715},
819 {-122,-37,730},
820 {-126,-25,743},
821 {-127,-13,755},
822 {-128,0,768},
823 {-127,13,781},
824 {-126,25,793},
825 {-122,37,806},
826 {-118,49,821},
827 {-113,60,835},
828 {-106,71,853},
829 {-99,81,872},
830 {-91,91,896},
831 {-81,99,920},
832 {-71,106,939},
833 {-60,113,957},
834 {-49,118,971},
835 {-37,122,986},
836 {-25,126,999},
837 {-13,127,1011},
838 };
839
840 static void
841 draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir)
842 {
843         struct circle *c;
844
845 #if 0
846         dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
847 #endif
848         int count=64;
849         int end=start+len;
850         int i,step;
851         c=circle64;
852         if (diameter > 128)
853                 step=1;
854         else if (diameter > 64)
855                 step=2;
856         else if (diameter > 24)
857                 step=4;
858         else if (diameter > 8)
859                 step=8;
860         else
861                 step=16;
862         if (len > 0) {
863                 while (start < 0) {
864                         start+=1024;
865                         end+=1024;
866                 }
867                 while (end > 0) {
868                         i=0;
869                         while (i < count && c[i].fowler <= start)
870                                 i+=step;
871                         while (i < count && c[i].fowler < end) {
872                                 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
873                                 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
874                                 (*pos)+=dir;
875                                 i+=step;
876                         }
877                         end-=1024;
878                         start-=1024;
879                 }
880         } else {
881                 while (start > 1024) {
882                         start-=1024;
883                         end-=1024;
884                 }
885                 while (end < 1024) {
886                         i=count-1;
887                         while (i >= 0 && c[i].fowler >= start)
888                                 i-=step;
889                         while (i >= 0 && c[i].fowler > end) {
890                                 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
891                                 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
892                                 (*pos)+=dir;
893                                 i-=step;
894                         }
895                         start+=1024;
896                         end+=1024;
897                 }
898         }
899 }
900
901
902 static int
903 fowler(int dy, int dx)
904 {
905         int adx, ady;           /* Absolute Values of Dx and Dy */
906         int code;               /* Angular Region Classification Code */
907
908         adx = (dx < 0) ? -dx : dx;      /* Compute the absolute values. */
909         ady = (dy < 0) ? -dy : dy;
910
911         code = (adx < ady) ? 1 : 0;
912         if (dx < 0)
913                 code += 2;
914         if (dy < 0)
915                 code += 4;
916
917         switch (code) {
918         case 0:
919                 return (dx == 0) ? 0 : 128*ady / adx;   /* [  0, 45] */
920         case 1:
921                 return (256 - (128*adx / ady)); /* ( 45, 90] */
922         case 3:
923                 return (256 + (128*adx / ady)); /* ( 90,135) */
924         case 2:
925                 return (512 - (128*ady / adx)); /* [135,180] */
926         case 6:
927                 return (512 + (128*ady / adx)); /* (180,225] */
928         case 7:
929                 return (768 - (128*adx / ady)); /* (225,270) */
930         case 5:
931                 return (768 + (128*adx / ady)); /* [270,315) */
932         case 4:
933                 return (1024 - (128*ady / adx));/* [315,360) */
934         }
935         return 0;
936 }
937 static int
938 int_sqrt(unsigned int n)
939 {
940         unsigned int h, p= 0, q= 1, r= n;
941         while ( q <= n )
942                 q <<= 2;
943         while ( q != 1 ) {
944                 q >>= 2;
945                 h = p + q;
946                 p >>= 1;
947                 if ( r >= h ) {
948                         p += q;
949                         r -= h;
950                 }
951         }
952         return p;
953 }
954
955 struct offset {
956         int px,py,nx,ny;
957 };
958
959 static void
960 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
961 {
962         int x,y;
963         x = (dx * wi) / l;
964         y = (dy * wi) / l;
965         if (x < 0) {
966                 res->nx = -x/2;
967                 res->px = (x-1)/2;
968         } else {
969                 res->nx = -(x+1)/2;
970                 res->px = x/2;
971         }
972         if (y < 0) {
973                 res->ny = -y/2;
974                 res->py = (y-1)/2;
975         } else {
976                 res->ny = -(y+1)/2;
977                 res->py = y/2;
978         }
979 }
980
981 static void
982 graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count, int *width, int step)
983 {
984         int maxpoints=200;
985         struct point res[maxpoints], pos, poso, neg, nego;
986         int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
987         struct offset o,oo;
988         int fow=0, fowo=0, delta;
989         int wi, ppos = maxpoints/2, npos = maxpoints/2;
990         int state,prec=5;
991         int max_circle_points=20;
992         for (i = 0; i < count; i++) {
993                 wi=*width;
994                 width+=step;
995                 if (i < count - 1) {
996                         dx = (pnt[i + 1].x - pnt[i].x);
997                         dy = (pnt[i + 1].y - pnt[i].y);
998                         l = int_sqrt(dx * dx + dy * dy);
999                         fow=fowler(-dy, dx);
1000                 }
1001                 if (! l) 
1002                         l=1;
1003                 calc_offsets(wi, l, dx, dy, &o);
1004                 pos.x = pnt[i].x + o.ny;
1005                 pos.y = pnt[i].y + o.px;
1006                 neg.x = pnt[i].x + o.py;
1007                 neg.y = pnt[i].y + o.nx;
1008                 if (! i)
1009                         state=0;
1010                 else if (i == count-1) 
1011                         state=2;
1012                 else if (npos < max_circle_points || ppos >= maxpoints-max_circle_points)
1013                         state=3;
1014                 else
1015                         state=1;
1016                 switch (state) {
1017                 case 1:
1018                        if (fowo != fow) {
1019                                 poso.x = pnt[i].x + oo.ny;
1020                                 poso.y = pnt[i].y + oo.px;
1021                                 nego.x = pnt[i].x + oo.py;
1022                                 nego.y = pnt[i].y + oo.nx;
1023                                 delta=fowo-fow;
1024                                 if (delta < 0)
1025                                         delta+=1024;
1026                                 if (delta < 512) {
1027                                         if (intersection(&pos, dx, dy, &poso, dxo, dyo, &res[ppos]))
1028                                                 ppos++;
1029                                         res[--npos] = nego;
1030                                         --npos;
1031                                         draw_circle(&pnt[i], wi, prec, fowo-512, -delta, res, &npos, -1);
1032                                         res[npos] = neg;
1033                                 } else {
1034                                         res[ppos++] = poso;
1035                                         draw_circle(&pnt[i], wi, prec, fowo, 1024-delta, res, &ppos, 1);
1036                                         res[ppos++] = pos;
1037                                         if (intersection(&neg, dx, dy, &nego, dxo, dyo, &res[npos - 1]))
1038                                                 npos--;
1039                                 }
1040                         }
1041                         break;
1042                 case 2:
1043                 case 3:
1044                         res[--npos] = neg;
1045                         --npos;
1046                         draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
1047                         res[npos] = pos;
1048                         res[ppos++] = pos;
1049                         dbg_assert(npos > 0);
1050                         dbg_assert(ppos < maxpoints);
1051                         gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
1052                         if (state == 2)
1053                                 break;
1054                         npos=maxpoints/2;
1055                         ppos=maxpoints/2;
1056                 case 0:
1057                         res[ppos++] = neg;
1058                         draw_circle(&pnt[i], wi, prec, fow+512, 512, res, &ppos, 1);
1059                         res[ppos++] = pos;
1060                         break;
1061                 }
1062                 if (step) {
1063                         wi=*width;
1064                         calc_offsets(wi, l, dx, dy, &oo);
1065                 } else 
1066                         oo=o;
1067                 dxo = -dx;
1068                 dyo = -dy;
1069                 fowo=fow;
1070         }
1071 }
1072
1073
1074 struct wpoint {
1075         int x,y,w;
1076 };
1077
1078 static int
1079 clipcode(struct wpoint *p, struct point_rect *r)
1080 {
1081         int code=0;
1082         if (p->x < r->lu.x)
1083                 code=1;
1084         if (p->x > r->rl.x)
1085                 code=2;
1086         if (p->y < r->lu.y)
1087                 code |=4;
1088         if (p->y > r->rl.y)
1089                 code |=8;
1090         return code;
1091 }
1092
1093
1094 static int
1095 clip_line(struct wpoint *p1, struct wpoint *p2, struct point_rect *r)
1096 {
1097         int code1,code2,ret=1;
1098         int dx,dy,dw;
1099         code1=clipcode(p1, r);
1100         if (code1)
1101                 ret |= 2;
1102         code2=clipcode(p2, r);
1103         if (code2)
1104                 ret |= 4;
1105         dx=p2->x-p1->x;
1106         dy=p2->y-p1->y;
1107         dw=p2->w-p1->w;
1108         while (code1 || code2) {
1109                 if (code1 & code2)
1110                         return 0;
1111                 if (code1 & 1) {
1112                         p1->y+=(r->lu.x-p1->x)*dy/dx;
1113                         p1->w+=(r->lu.x-p1->x)*dw/dx;
1114                         p1->x=r->lu.x;
1115                 } else if (code1 & 2) {
1116                         p1->y+=(r->rl.x-p1->x)*dy/dx;
1117                         p1->w+=(r->rl.x-p1->x)*dw/dx;
1118                         p1->x=r->rl.x;
1119                 } else if (code1 & 4) {
1120                         p1->x+=(r->lu.y-p1->y)*dx/dy;
1121                         p1->w+=(r->lu.y-p1->y)*dw/dy;
1122                         p1->y=r->lu.y;
1123                 } else if (code1 & 8) {
1124                         p1->x+=(r->rl.y-p1->y)*dx/dy;
1125                         p1->w+=(r->rl.y-p1->y)*dw/dy;
1126                         p1->y=r->rl.y;
1127                 }
1128                 code1=clipcode(p1, r);
1129                 if (code1 & code2)
1130                         return 0;
1131                 if (code2 & 1) {
1132                         p2->y+=(r->lu.x-p2->x)*dy/dx;
1133                         p2->w+=(r->lu.x-p2->x)*dw/dx;
1134                         p2->x=r->lu.x;
1135                 } else if (code2 & 2) {
1136                         p2->y+=(r->rl.x-p2->x)*dy/dx;
1137                         p2->w+=(r->rl.x-p2->x)*dw/dx;
1138                         p2->x=r->rl.x;
1139                 } else if (code2 & 4) {
1140                         p2->x+=(r->lu.y-p2->y)*dx/dy;
1141                         p2->w+=(r->lu.y-p2->y)*dw/dy;
1142                         p2->y=r->lu.y;
1143                 } else if (code2 & 8) {
1144                         p2->x+=(r->rl.y-p2->y)*dx/dy;
1145                         p2->w+=(r->rl.y-p2->y)*dw/dy;
1146                         p2->y=r->rl.y;
1147                 }
1148                 code2=clipcode(p2, r);
1149         }
1150         return ret;
1151 }
1152
1153 static void
1154 graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pa, int count, int *width, int step, int poly)
1155 {
1156         struct point p[count+1];
1157         int w[count*step+1];
1158         struct wpoint p1,p2;
1159         int i,code,out=0;
1160         int wmax;
1161         struct point_rect r=gra->r;
1162
1163         wmax=width[0];
1164         if (step) {
1165                 for (i = 1 ; i < count ; i++) {
1166                         if (width[i*step] > wmax)
1167                                 wmax=width[i*step];
1168                 }
1169         }
1170         if (wmax <= 0)
1171                 return;
1172         r.lu.x-=wmax;
1173         r.lu.y-=wmax;
1174         r.rl.x+=wmax;
1175         r.rl.y+=wmax;
1176         for (i = 0 ; i < count ; i++) {
1177                 if (i) {
1178                         p1.x=pa[i-1].x;
1179                         p1.y=pa[i-1].y;
1180                         p1.w=width[(i-1)*step];
1181                         p2.x=pa[i].x;
1182                         p2.y=pa[i].y;
1183                         p2.w=width[i*step];
1184                         /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
1185                         code=clip_line(&p1, &p2, &r);
1186                         if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
1187                                 p[out].x=p1.x;
1188                                 p[out].y=p1.y;
1189                                 w[out*step]=p1.w;
1190                                 out++;
1191                         }
1192                         if (code) {
1193                                 p[out].x=p2.x;
1194                                 p[out].y=p2.y;
1195                                 w[out*step]=p2.w;
1196                                 out++;
1197                         }
1198                         if (i == count-1 || (code & 4)) {
1199                                 if (out > 1) {
1200                                         if (poly) {     
1201                                                 graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
1202                                         } else
1203                                                 gra->meth.draw_lines(gra->priv, gc->priv, p, out);
1204                                         out=0;
1205                                 }
1206                         }
1207                 }
1208         }
1209 }
1210
1211 static int
1212 is_inside(struct point *p, struct point_rect *r, int edge)
1213 {
1214         switch(edge) {
1215         case 0:
1216                 return p->x >= r->lu.x;
1217         case 1:
1218                 return p->x <= r->rl.x;
1219         case 2:
1220                 return p->y >= r->lu.y;
1221         case 3:
1222                 return p->y <= r->rl.y;
1223         default:
1224                 return 0;
1225         }
1226 }
1227
1228 static void
1229 poly_intersection(struct point *p1, struct point *p2, struct point_rect *r, int edge, struct point *ret)
1230 {
1231         int dx=p2->x-p1->x;
1232         int dy=p2->y-p1->y;
1233         switch(edge) {
1234         case 0:
1235                 ret->y=p1->y+(r->lu.x-p1->x)*dy/dx;
1236                 ret->x=r->lu.x;
1237                 break;
1238         case 1:
1239                 ret->y=p1->y+(r->rl.x-p1->x)*dy/dx;
1240                 ret->x=r->rl.x;
1241                 break;
1242         case 2:
1243                 ret->x=p1->x+(r->lu.y-p1->y)*dx/dy;
1244                 ret->y=r->lu.y;
1245                 break;
1246         case 3:
1247                 ret->x=p1->x+(r->rl.y-p1->y)*dx/dy;
1248                 ret->y=r->rl.y;
1249                 break;
1250         }
1251 }
1252
1253 static void
1254 graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pin, int count_in)
1255 {
1256         struct point_rect r=gra->r;
1257         struct point *pout,*p,*s,pi;
1258         struct point p1[count_in+1];
1259         struct point p2[count_in+1];
1260         int count_out,edge=3;
1261         int i;
1262 #if 0
1263         r.lu.x+=20;
1264         r.lu.y+=20;
1265         r.rl.x-=20;
1266         r.rl.y-=20;
1267 #endif
1268
1269         pout=p1;
1270         for (edge = 0 ; edge < 4 ; edge++) {
1271                 p=pin;
1272                 s=pin+count_in-1;
1273                 count_out=0;
1274                 for (i = 0 ; i < count_in ; i++) {
1275                         if (is_inside(p, &r, edge)) {
1276                                 if (! is_inside(s, &r, edge)) {
1277                                         poly_intersection(s,p,&r,edge,&pi);
1278                                         pout[count_out++]=pi;
1279                                 }
1280                                 pout[count_out++]=*p;
1281                         } else {
1282                                 if (is_inside(s, &r, edge)) {
1283                                         poly_intersection(p,s,&r,edge,&pi);
1284                                         pout[count_out++]=pi;
1285                                 }
1286                         }
1287                         s=p;
1288                         p++;
1289                 }
1290                 count_in=count_out;
1291                 if (pin == p1) {
1292                         pin=p2;
1293                         pout=p1;
1294                 } else {
1295                         pin=p1;
1296                         pout=p2;
1297                 }
1298         }
1299         gra->meth.draw_polygon(gra->priv, gc->priv, pin, count_in);
1300 }
1301
1302
1303 static void
1304 display_context_free(struct display_context *dc)
1305 {
1306         g_free(dc->gc);
1307         g_free(dc->img);
1308         dc->gc=NULL;
1309         dc->img=NULL;
1310 }
1311
1312 static void
1313 displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
1314 {
1315         int width[16384];
1316         int count;
1317         struct point pa[16384];
1318         struct graphics *gra=dc->gra;
1319         struct graphics_gc *gc=dc->gc;
1320         struct element *e=dc->e;
1321         struct graphics_image *img=dc->img;
1322         struct point p;
1323         char path[PATH_MAX];
1324
1325         di->displayed=1;
1326         if (! gc) {
1327                 gc=graphics_gc_new(gra);
1328                 gc->meth.gc_set_foreground(gc->priv, &e->color);
1329                 dc->gc=gc;
1330         }
1331         if (dc->e->type == element_polyline) {
1332                 count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, e->u.polyline.width, width);
1333         }
1334         else
1335                 count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, 0, NULL);
1336         switch (e->type) {
1337         case element_polygon:
1338 #if 0
1339                 {
1340                         int i;
1341                         for (i = 0 ; i < count ; i++) {
1342                                 dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
1343                         }
1344                 }
1345                 dbg(0,"element_polygon count=%d\n",count);
1346 #endif
1347 #if 1
1348                 graphics_draw_polygon_clipped(gra, gc, pa, count);
1349 #endif
1350                 break;
1351         case element_polyline:
1352 #if 0
1353                 if (e->u.polyline.width > 1) {
1354                         graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
1355                 } else {
1356 #else
1357                 {
1358 #if 0
1359                          if (e->u.polyline.width > 1)
1360                                      gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1361 #else
1362                         gc->meth.gc_set_linewidth(gc->priv, 1);
1363 #endif
1364
1365                         
1366 #endif
1367                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1368                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1369                                                        e->u.polyline.offset,
1370                                                        e->u.polyline.dash_table,
1371                                                        e->u.polyline.dash_num);
1372 #if 0
1373                         if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
1374                                 dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
1375 #endif
1376                         graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1377 #if 0
1378                         }
1379 #endif
1380                 }
1381                 break;
1382         case element_circle:
1383                 if (count) {
1384                         if (e->u.circle.width > 1) 
1385                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1386                         gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
1387                         if (di->label && e->text_size) {
1388                                 p.x=pa[0].x+3;
1389                                 p.y=pa[0].y+10;
1390                                 if (! gra->font[e->text_size])
1391                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1392                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, di->label, &p, 0x10000, 0);
1393                         }
1394                 }
1395                 break;
1396         case element_text:
1397                 if (count && di->label) {
1398                         if (! gra->font[e->text_size])
1399                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1400                         label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], pa, count, di->label);
1401                 }
1402                 break;
1403         case element_icon:
1404                 if (count) {
1405                         if (!img) {
1406                                 if (e->u.icon.src[0] == '/')
1407                                         strcpy(path,e->u.icon.src);
1408                                 else
1409                                         sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1410                                 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1411                                 if (img)
1412                                         dc->img=img;
1413                                 else
1414                                         dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1415                         }
1416                         if (img) {
1417                                 p.x=pa[0].x - img->hot.x;
1418                                 p.y=pa[0].y - img->hot.y;
1419                                 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1420                         }
1421                 }
1422                 break;
1423         case element_image:
1424                 dbg(1,"image: '%s'\n", di->label);
1425                 if (gra->meth.draw_image_warp)
1426                         gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1427                 else
1428                         dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1429                 break;
1430         case element_arrows:
1431                 display_draw_arrows(gra,gc,pa,count);
1432                 break;
1433         default:
1434                 printf("Unhandled element type %d\n", e->type);
1435         
1436         }
1437 }
1438 /**
1439  * FIXME
1440  * @param <>
1441  * @returns <>
1442  * @author Martin Schaller (04/2008)
1443 */
1444 static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *display_list, struct itemgra *itm)
1445 {
1446         struct element *e;
1447         GList *es,*types;
1448         GHashTable *h;
1449         enum item_type type;
1450
1451         es=itm->elements;
1452         while (es) {
1453                 e=es->data;
1454                 display_list->dc.e=e;
1455                 types=itm->type;
1456                 while (types) {
1457                         type=GPOINTER_TO_INT(types->data);
1458                         h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(type));
1459                         if (h) {
1460                                 g_hash_table_foreach(h, (GHFunc)displayitem_draw, &display_list->dc);
1461                                 display_context_free(&display_list->dc);
1462                         }
1463                         types=g_list_next(types);
1464                 }
1465                 es=g_list_next(es);
1466         }
1467 }
1468
1469 void
1470 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
1471 {
1472         GList *es;
1473         struct point p;
1474         struct coord c;
1475         char *label=NULL;
1476         struct graphics_gc *gc = NULL;
1477         struct graphics_image *img;
1478         char path[PATH_MAX];
1479         es=itm->elements;
1480         c.x=0;
1481         c.y=0;
1482         while (es) {
1483                 struct element *e=es->data;
1484                 int count=e->coord_count;
1485                 struct point pnt[count+1];
1486                 if (count)
1487                         transform(t, projection_screen, e->coord, pnt, count, 0, 0, NULL);
1488                 else {
1489                         transform(t, projection_screen, &c, pnt, 1, 0, 0, NULL);
1490                         count=1;
1491                 }
1492                 gc=graphics_gc_new(gra);
1493                 gc->meth.gc_set_foreground(gc->priv, &e->color);
1494                 switch (e->type) {
1495                 case element_polyline:
1496                         if (e->u.polyline.width > 1) 
1497                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1498                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1499                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1500                                                        e->u.polyline.offset,
1501                                                        e->u.polyline.dash_table,
1502                                                        e->u.polyline.dash_num);
1503                         gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
1504                         break;
1505                 case element_circle:
1506                         if (e->u.circle.width > 1) 
1507                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1508                         gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
1509                         if (label && e->text_size) {
1510                                 p.x=pnt[0].x+3;
1511                                 p.y=pnt[0].y+10;
1512                         if (! gra->font[e->text_size])
1513                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1514                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
1515                         }
1516                         break;
1517                 case element_icon:
1518                         if (e->u.icon.src[0] == '/') 
1519                                 strcpy(path,e->u.icon.src);
1520                         else
1521                                 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1522                         img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1523                         if (! img)
1524                                 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1525                         else {
1526                                 p.x=pnt[0].x - img->hot.x;
1527                                 p.y=pnt[0].y - img->hot.y;
1528                                 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
1529                                 graphics_image_free(gra, img);
1530                         }
1531                         break;
1532                 default:
1533                         dbg(0,"dont know how to draw %d\n", e->type);
1534                 }
1535                 graphics_gc_destroy(gc);
1536                 es=g_list_next(es);
1537         }
1538 }
1539
1540 /**
1541  * FIXME
1542  * @param <>
1543  * @returns <>
1544  * @author Martin Schaller (04/2008)
1545 */
1546 static void xdisplay_draw_layer(struct displaylist *display_list, struct graphics *gra, struct layer *lay, int order)
1547 {
1548         GList *itms;
1549         struct itemgra *itm;
1550
1551         itms=lay->itemgras;
1552         while (itms) {
1553                 itm=itms->data;
1554                 if (order >= itm->order.min && order <= itm->order.max) 
1555                         xdisplay_draw_elements(gra, display_list, itm);
1556                 itms=g_list_next(itms);
1557         }
1558 }
1559
1560 /**
1561  * FIXME
1562  * @param <>
1563  * @returns <>
1564  * @author Martin Schaller (04/2008)
1565 */
1566 static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra, struct layout *l, int order)
1567 {
1568         GList *lays;
1569         struct layer *lay;
1570         
1571         lays=l->layers;
1572         while (lays) {
1573                 lay=lays->data;
1574                 xdisplay_draw_layer(display_list, gra, lay, order);
1575                 lays=g_list_next(lays);
1576         }
1577 }
1578
1579 /**
1580  * FIXME
1581  * @param <>
1582  * @returns <>
1583  * @author Martin Schaller (04/2008)
1584 */
1585 extern void *route_selection;
1586
1587 static void
1588 do_draw(struct displaylist *displaylist, int cancel)
1589 {
1590         struct item *item;
1591         int count,max=16384,workload=0;
1592         struct coord ca[max];
1593         struct attr attr;
1594
1595         while (!cancel) {
1596                 if (!displaylist->msh) {
1597                         displaylist->msh=mapset_open(displaylist->ms);
1598                 }
1599                 if (!displaylist->m) {
1600                         displaylist->m=mapset_next(displaylist->msh, 1);
1601                         if (!displaylist->m) {
1602                                 mapset_close(displaylist->msh);
1603                                 displaylist->msh=NULL;
1604                                 break;
1605                         }
1606                         displaylist->dc.pro=map_projection(displaylist->m);
1607                         displaylist->conv=map_requires_conversion(displaylist->m);
1608                         displaylist->sel=transform_get_selection(displaylist->dc.trans, displaylist->dc.pro, displaylist->order);
1609                         displaylist->mr=map_rect_new(displaylist->m, displaylist->sel);
1610                 }
1611                 if (displaylist->mr) {
1612                         while ((item=map_rect_get_item(displaylist->mr))) {
1613                                 count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, displaylist->sel);
1614                                 if (! count)
1615                                         continue;
1616                                 if (count == max) 
1617                                         dbg(0,"point count overflow %d\n", count);
1618                                 if (!item_attr_get(item, attr_label, &attr))
1619                                         attr.u.str=NULL;
1620                                 if (displaylist->conv && attr.u.str && attr.u.str[0]) {
1621                                         char *str=map_convert_string(displaylist->m, attr.u.str);
1622                                         display_add(displaylist, item, count, ca, str);
1623                                         map_convert_free(str);
1624                                 } else
1625                                         display_add(displaylist, item, count, ca, attr.u.str);
1626                                 workload++;
1627                                 if (workload == displaylist->workload)
1628                                         return;
1629                         }
1630                         map_rect_destroy(displaylist->mr);
1631                 }
1632                 map_selection_destroy(displaylist->sel);
1633                 displaylist->mr=NULL;
1634                 displaylist->sel=NULL;
1635                 displaylist->m=NULL;
1636         }
1637         event_remove_idle(displaylist->idle_ev);
1638         displaylist->idle_ev=NULL;
1639         displaylist->busy=0;
1640         graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, 1);
1641         map_rect_destroy(displaylist->mr);
1642         map_selection_destroy(displaylist->sel);
1643         mapset_close(displaylist->msh);
1644         displaylist->mr=NULL;
1645         displaylist->sel=NULL;
1646         displaylist->m=NULL;
1647         callback_call_1(displaylist->cb, cancel);
1648 }
1649
1650 /**
1651  * FIXME
1652  * @param <>
1653  * @returns <>
1654  * @author Martin Schaller (04/2008)
1655 */
1656 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
1657 {
1658         int order=transform_get_order(trans);
1659         struct point p;
1660         displaylist->dc.trans=trans;
1661         p.x=0;
1662         p.y=0;
1663         // FIXME find a better place to set the background color
1664         if (l) {
1665                 graphics_gc_set_background(gra->gc[0], &l->color);
1666                 graphics_gc_set_foreground(gra->gc[0], &l->color);
1667                 gra->default_font = g_strdup(l->font);
1668         }
1669         graphics_background_gc(gra, gra->gc[0]);
1670         gra->meth.draw_mode(gra->priv, draw_mode_begin);
1671         gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
1672         if (l) 
1673                 xdisplay_draw(displaylist, gra, l, order+l->order_delta);
1674         if (callback)
1675                 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1676         gra->meth.draw_mode(gra->priv, draw_mode_end);
1677 }
1678
1679 /**
1680  * FIXME
1681  * @param <>
1682  * @returns <>
1683  * @author Martin Schaller (04/2008)
1684 */
1685 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l, int async, struct callback *cb)
1686 {
1687         int order=transform_get_order(trans);
1688
1689         dbg(1,"enter");
1690         if (async == 1 && displaylist->busy)
1691                 return;
1692         xdisplay_free(displaylist->dl);
1693         dbg(1,"order=%d\n", order);
1694
1695         displaylist->dc.gra=gra;
1696         displaylist->ms=mapsets->data;
1697         displaylist->dc.trans=trans;
1698         displaylist->workload=async ? 1000 : 0;
1699         displaylist->cb=cb;
1700         if (l)
1701                 order+=l->order_delta;
1702         displaylist->order=order;
1703         displaylist->busy=1;
1704         displaylist->layout=l;
1705         if (async) {
1706                 if (! displaylist->idle_cb)
1707                         displaylist->idle_cb=callback_new_2(do_draw, displaylist, 0);
1708                 displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
1709         } else
1710                 do_draw(displaylist, 0);
1711 }
1712
1713 /**
1714  * FIXME
1715  * @param <>
1716  * @returns <>
1717  * @author Martin Schaller (04/2008)
1718 */
1719 struct displaylist_handle {
1720         GList *hl_head,*hl,*l_head,*l;
1721 };
1722
1723 /**
1724  * FIXME
1725  * @param <>
1726  * @returns <>
1727  * @author Martin Schaller (04/2008)
1728 */
1729 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1730 {
1731         struct displaylist_handle *ret;
1732
1733         ret=g_new0(struct displaylist_handle, 1);
1734         ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1735         ret->l_head=ret->l=g_hash_to_list_keys(ret->hl->data);
1736
1737         return ret;
1738 }
1739
1740 /**
1741  * FIXME
1742  * @param <>
1743  * @returns <>
1744  * @author Martin Schaller (04/2008)
1745 */
1746 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1747 {
1748         struct displayitem *ret;
1749         if (! dlh->l) {
1750                 dlh->hl=g_list_next(dlh->hl);
1751                 if (!dlh->hl)
1752                         return NULL;
1753                 g_list_free(dlh->l_head);
1754                 dlh->l_head=dlh->l=g_hash_to_list_keys(dlh->hl->data);
1755         }
1756         ret=dlh->l->data;
1757         dlh->l=g_list_next(dlh->l);
1758         return ret;
1759 }
1760
1761 /**
1762  * FIXME
1763  * @param <>
1764  * @returns <>
1765  * @author Martin Schaller (04/2008)
1766 */
1767 void graphics_displaylist_close(struct displaylist_handle *dlh)
1768 {
1769         g_list_free(dlh->hl_head);
1770         g_list_free(dlh->l_head);
1771         g_free(dlh);
1772 }
1773
1774 /**
1775  * FIXME
1776  * @param <>
1777  * @returns <>
1778  * @author Martin Schaller (04/2008)
1779 */
1780 struct displaylist * graphics_displaylist_new(void)
1781 {
1782         struct displaylist *ret=g_new0(struct displaylist, 1);
1783
1784         ret->dl=g_hash_table_new(NULL,NULL);
1785
1786         return ret;
1787 }
1788
1789 /**
1790  * FIXME
1791  * @param <>
1792  * @returns <>
1793  * @author Martin Schaller (04/2008)
1794 */
1795 struct item * graphics_displayitem_get_item(struct displayitem *di)
1796 {
1797         return &di->item;       
1798 }
1799
1800 /**
1801  * FIXME
1802  * @param <>
1803  * @returns <>
1804  * @author Martin Schaller (04/2008)
1805 */
1806 char * graphics_displayitem_get_label(struct displayitem *di)
1807 {
1808         return di->label;
1809 }
1810
1811 /**
1812  * FIXME
1813  * @param <>
1814  * @returns <>
1815  * @author Martin Schaller (04/2008)
1816 */
1817 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1818 {
1819         if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1820                 return 0;
1821         if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1822                 return 0;
1823         if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1824                 return 1;
1825         }
1826         return 0;
1827 }
1828
1829 /**
1830  * FIXME
1831  * @param <>
1832  * @returns <>
1833  * @author Martin Schaller (04/2008)
1834 */
1835 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1836 {
1837         int vx,vy,wx,wy;
1838         int c1,c2;
1839         struct point line_p;
1840
1841         vx=line_p1->x-line_p0->x;
1842         vy=line_p1->y-line_p0->y;
1843         wx=p->x-line_p0->x;
1844         wy=p->y-line_p0->y;
1845
1846         c1=vx*wx+vy*wy;
1847         if ( c1 <= 0 )
1848                 return within_dist_point(p, line_p0, dist);
1849         c2=vx*vx+vy*vy;
1850         if ( c2 <= c1 )
1851                 return within_dist_point(p, line_p1, dist);
1852
1853         line_p.x=line_p0->x+vx*c1/c2;
1854         line_p.y=line_p0->y+vy*c1/c2;
1855         return within_dist_point(p, &line_p, dist);
1856 }
1857
1858 /**
1859  * FIXME
1860  * @param <>
1861  * @returns <>
1862  * @author Martin Schaller (04/2008)
1863 */
1864 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1865 {
1866         int i;
1867         for (i = 0 ; i < count-1 ; i++) {
1868                 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1869                         return 1;
1870                 }
1871         }
1872         if (close)
1873                 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1874         return 0;
1875 }
1876
1877 /**
1878  * FIXME
1879  * @param <>
1880  * @returns <>
1881  * @author Martin Schaller (04/2008)
1882 */
1883 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1884 {
1885         int i, j, c = 0;
1886         for (i = 0, j = count-1; i < count; j = i++) {
1887                 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1888                 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1889                 (p->x < (poly_pnt[j].x - poly_pnt[i].x) * (p->y - poly_pnt[i].y) / (poly_pnt[j].y - poly_pnt[i].y) + poly_pnt[i].x)) 
1890                         c = !c;
1891         }
1892         if (! c)
1893                 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1894         return c;
1895 }
1896
1897 /**
1898  * FIXME
1899  * @param <>
1900  * @returns <>
1901  * @author Martin Schaller (04/2008)
1902 */
1903 int graphics_displayitem_within_dist(struct displaylist *displaylist, struct displayitem *di, struct point *p, int dist)
1904 {
1905         struct point pa[16384];
1906         int count;
1907
1908         count=transform(displaylist->dc.trans, displaylist->dc.pro, di->c, pa, di->count, 1, 0, NULL);
1909         
1910         if (di->item.type < type_line) {
1911                 return within_dist_point(p, &pa[0], dist);
1912         }
1913         if (di->item.type < type_area) {
1914                 return within_dist_polyline(p, pa, count, dist, 0);
1915         }
1916         return within_dist_polygon(p, pa, count, dist);
1917 }