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