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