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