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