Add:Core:Possibility to set brightness, contrast and gamma for graphics
[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 != 655536 || gra->brightness != 65536 || 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         }
1558         if (item_type_is_area(dc->type) && (dc->e->type == element_polyline || dc->e->type == element_text))
1559                 count=limit_count(di->c, count);
1560         if (dc->type == type_poly_water_tiled)
1561                 mindist=0;
1562         if (dc->e->type == element_polyline) 
1563                 count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, e->u.polyline.width, width);
1564         else
1565                 count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, 0, NULL);
1566         switch (e->type) {
1567         case element_polygon:
1568 #if 0
1569                 {
1570                         int i;
1571                         for (i = 0 ; i < count ; i++) {
1572                                 dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
1573                         }
1574                 }
1575                 dbg(0,"element_polygon count=%d\n",count);
1576 #endif
1577 #if 1
1578                 graphics_draw_polygon_clipped(gra, gc, pa, count);
1579 #endif
1580                 break;
1581         case element_polyline:
1582 #if 0
1583                 if (e->u.polyline.width > 1) {
1584                         graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
1585                 } else {
1586 #else
1587                 {
1588 #if 0
1589                          if (e->u.polyline.width > 1)
1590                                      gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1591 #else
1592                         gc->meth.gc_set_linewidth(gc->priv, 1);
1593 #endif
1594
1595                         
1596 #endif
1597                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1598                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1599                                                        e->u.polyline.offset,
1600                                                        e->u.polyline.dash_table,
1601                                                        e->u.polyline.dash_num);
1602 #if 0
1603                         if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
1604                                 dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
1605 #endif
1606                         for (i = 0 ; i < count ; i++) {
1607                                 if (width[i] < 2)
1608                                         width[i]=2;
1609                         }
1610                         graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1611 #if 0
1612                         }
1613 #endif
1614                 }
1615                 break;
1616         case element_circle:
1617                 if (count) {
1618                         if (e->u.circle.width > 1) 
1619                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1620                         gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
1621                         if (di->label && e->text_size) {
1622                                 struct graphics_font *font=get_font(gra, e->text_size);
1623                                 p.x=pa[0].x+3;
1624                                 p.y=pa[0].y+10;
1625                                 if (font)
1626                                         gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, font->priv, di->label, &p, 0x10000, 0);
1627                                 else
1628                                         dbg(0,"Failed to get font with size %d\n",e->text_size);
1629                         }
1630                 }
1631                 break;
1632         case element_text:
1633                 if (count && di->label) {
1634                         struct graphics_font *font=get_font(gra, e->text_size);
1635                         if (font)
1636                                 label_line(gra, gra->gc[2], gra->gc[1], font, pa, count, di->label);
1637                         else
1638                                 dbg(0,"Failed to get font with size %d\n",e->text_size);
1639                 }
1640                 break;
1641         case element_icon:
1642                 if (count) {
1643                         if (!img) {
1644                                 path=graphics_icon_path(e->u.icon.src); 
1645                                 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1646                                 g_free(path);
1647                                 if (img)
1648                                         dc->img=img;
1649                                 else
1650                                         dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1651                         }
1652                         if (img) {
1653                                 p.x=pa[0].x - img->hot.x;
1654                                 p.y=pa[0].y - img->hot.y;
1655                                 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1656                         }
1657                 }
1658                 break;
1659         case element_image:
1660                 dbg(1,"image: '%s'\n", di->label);
1661                 if (gra->meth.draw_image_warp)
1662                         gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1663                 else
1664                         dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1665                 break;
1666         case element_arrows:
1667                 display_draw_arrows(gra,gc,pa,count);
1668                 break;
1669         default:
1670                 printf("Unhandled element type %d\n", e->type);
1671         
1672         }
1673 }
1674 /**
1675  * FIXME
1676  * @param <>
1677  * @returns <>
1678  * @author Martin Schaller (04/2008)
1679 */
1680 static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *display_list, struct itemgra *itm)
1681 {
1682         struct element *e;
1683         GList *es,*types;
1684         GHashTable *h;
1685         struct display_context *dc=&display_list->dc;
1686
1687         es=itm->elements;
1688         while (es) {
1689                 e=es->data;
1690                 dc->e=e;
1691                 types=itm->type;
1692                 while (types) {
1693                         dc->type=GPOINTER_TO_INT(types->data);
1694                         h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(dc->type));
1695                         if (h) {
1696                                 g_hash_table_foreach(h, (GHFunc)displayitem_draw, dc);
1697                                 display_context_free(dc);
1698                         }
1699                         types=g_list_next(types);
1700                 }
1701                 es=g_list_next(es);
1702         }
1703 }
1704
1705 void
1706 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
1707 {
1708         GList *es;
1709         struct point p;
1710         struct coord c;
1711 #if 0
1712         char *label=NULL;
1713 #endif
1714         struct graphics_gc *gc = NULL;
1715         struct graphics_image *img;
1716         char *path;
1717         es=itm->elements;
1718         c.x=0;
1719         c.y=0;
1720         while (es) {
1721                 struct element *e=es->data;
1722                 int count=e->coord_count;
1723                 struct point pnt[count+1];
1724                 if (count)
1725                         transform(t, projection_screen, e->coord, pnt, count, 0, 0, NULL);
1726                 else {
1727                         transform(t, projection_screen, &c, pnt, 1, 0, 0, NULL);
1728                         count=1;
1729                 }
1730                 gc=graphics_gc_new(gra);
1731                 graphics_gc_set_foreground(gc, &e->color);
1732                 switch (e->type) {
1733                 case element_polyline:
1734                         if (e->u.polyline.width > 1) 
1735                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1736                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1737                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1738                                                        e->u.polyline.offset,
1739                                                        e->u.polyline.dash_table,
1740                                                        e->u.polyline.dash_num);
1741                         gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
1742                         break;
1743                 case element_polygon:
1744                         gra->meth.draw_polygon(gra->priv, gc->priv, pnt, count);
1745                         break;
1746                 case element_circle:
1747                         if (e->u.circle.width > 1) 
1748                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1749                         gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
1750                         #if 0
1751                         // Leftover code,  displayitem_draw is intended to be merged with with graphics_draw_itemgra
1752                         if (label && e->text_size) {
1753                                 struct graphics_font *font=get_font(gra, e->text_size);
1754                                 p.x=pnt[0].x+3;
1755                                 p.y=pnt[0].y+10;
1756                                 if (font) 
1757                                         gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, font->priv, label, &p, 0x10000, 0);
1758                                 else
1759                                         dbg(0,"Failed to get font with size %d\n",e->text_size);
1760                         }
1761                         # endif
1762                         break;
1763                 case element_icon:
1764                         path=graphics_icon_path(e->u.icon.src); 
1765                         img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1766                         g_free(path);
1767                         if (! img)
1768                                 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1769                         else {
1770                                 p.x=pnt[0].x - img->hot.x;
1771                                 p.y=pnt[0].y - img->hot.y;
1772                                 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
1773                                 graphics_image_free(gra, img);
1774                         }
1775                         break;
1776                 default:
1777                         dbg(0,"dont know how to draw %d\n", e->type);
1778                 }
1779                 graphics_gc_destroy(gc);
1780                 es=g_list_next(es);
1781         }
1782 }
1783
1784 /**
1785  * FIXME
1786  * @param <>
1787  * @returns <>
1788  * @author Martin Schaller (04/2008)
1789 */
1790 static void xdisplay_draw_layer(struct displaylist *display_list, struct graphics *gra, struct layer *lay, int order)
1791 {
1792         GList *itms;
1793         struct itemgra *itm;
1794
1795         itms=lay->itemgras;
1796         while (itms) {
1797                 itm=itms->data;
1798                 if (order >= itm->order.min && order <= itm->order.max) 
1799                         xdisplay_draw_elements(gra, display_list, itm);
1800                 itms=g_list_next(itms);
1801         }
1802 }
1803
1804 /**
1805  * FIXME
1806  * @param <>
1807  * @returns <>
1808  * @author Martin Schaller (04/2008)
1809 */
1810 static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra, struct layout *l, int order)
1811 {
1812         GList *lays;
1813         struct layer *lay;
1814         
1815         lays=l->layers;
1816         while (lays) {
1817                 lay=lays->data;
1818                 xdisplay_draw_layer(display_list, gra, lay, order);
1819                 lays=g_list_next(lays);
1820         }
1821 }
1822
1823 /**
1824  * FIXME
1825  * @param <>
1826  * @returns <>
1827  * @author Martin Schaller (04/2008)
1828 */
1829 extern void *route_selection;
1830
1831 static void
1832 do_draw(struct displaylist *displaylist, int cancel, int flags)
1833 {
1834         struct item *item;
1835         int count,max=displaylist->dc.maxlen,workload=0;
1836         struct coord ca[max];
1837         struct attr attr;
1838
1839         profile(0,NULL);
1840         while (!cancel) {
1841                 if (!displaylist->msh) 
1842                         displaylist->msh=mapset_open(displaylist->ms);
1843                 if (!displaylist->m) {
1844                         displaylist->m=mapset_next(displaylist->msh, 1);
1845                         if (!displaylist->m) {
1846                                 mapset_close(displaylist->msh);
1847                                 displaylist->msh=NULL;
1848                                 break;
1849                         }
1850                         displaylist->dc.pro=map_projection(displaylist->m);
1851                         displaylist->conv=map_requires_conversion(displaylist->m);
1852                         displaylist->sel=transform_get_selection(displaylist->dc.trans, displaylist->dc.pro, displaylist->order);
1853                         displaylist->mr=map_rect_new(displaylist->m, displaylist->sel);
1854                 }
1855                 if (displaylist->mr) {
1856                         while ((item=map_rect_get_item(displaylist->mr))) {
1857                                 count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, displaylist->sel);
1858                                 if (! count)
1859                                         continue;
1860                                 if (count == max) {
1861                                         dbg(0,"point count overflow %d for %s "ITEM_ID_FMT"\n", count,item_to_name(item->type),ITEM_ID_ARGS(*item));
1862                                         displaylist->dc.maxlen=max*2;
1863                                 }
1864                                 if (!item_attr_get(item, attr_label, &attr))
1865                                         attr.u.str=NULL;
1866                                 if (displaylist->conv && attr.u.str && attr.u.str[0]) {
1867                                         char *str=map_convert_string(displaylist->m, attr.u.str);
1868                                         display_add(displaylist, item, count, ca, str);
1869                                         map_convert_free(str);
1870                                 } else
1871                                         display_add(displaylist, item, count, ca, attr.u.str);
1872                                 workload++;
1873                                 if (workload == displaylist->workload)
1874                                         return;
1875                         }
1876                         map_rect_destroy(displaylist->mr);
1877                 }
1878                 map_selection_destroy(displaylist->sel);
1879                 displaylist->mr=NULL;
1880                 displaylist->sel=NULL;
1881                 displaylist->m=NULL;
1882         }
1883         profile(1,"process_selection\n");
1884         event_remove_idle(displaylist->idle_ev);
1885         displaylist->idle_ev=NULL;
1886         callback_destroy(displaylist->idle_cb);
1887         displaylist->idle_cb=NULL;
1888         displaylist->busy=0;
1889         graphics_process_selection(displaylist->dc.gra, displaylist);
1890         profile(1,"draw\n");
1891         if (! cancel) 
1892                 graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, flags);
1893         map_rect_destroy(displaylist->mr);
1894         map_selection_destroy(displaylist->sel);
1895         mapset_close(displaylist->msh);
1896         displaylist->mr=NULL;
1897         displaylist->sel=NULL;
1898         displaylist->m=NULL;
1899         displaylist->msh=NULL;
1900         profile(1,"callback\n");
1901         callback_call_1(displaylist->cb, cancel);
1902         profile(0,"end\n");
1903 }
1904
1905 /**
1906  * FIXME
1907  * @param <>
1908  * @returns <>
1909  * @author Martin Schaller (04/2008)
1910 */
1911 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int flags)
1912 {
1913         int order=transform_get_order(trans);
1914         displaylist->dc.trans=trans;
1915         displaylist->dc.gra=gra;
1916         displaylist->dc.mindist=transform_get_scale(trans)/2;
1917         // FIXME find a better place to set the background color
1918         if (l) {
1919                 graphics_gc_set_background(gra->gc[0], &l->color);
1920                 graphics_gc_set_foreground(gra->gc[0], &l->color);
1921                 gra->default_font = g_strdup(l->font);
1922         }
1923         graphics_background_gc(gra, gra->gc[0]);
1924         gra->meth.draw_mode(gra->priv, (flags & 8)?draw_mode_begin_clear:draw_mode_begin);
1925         if (!(flags & 2))
1926                 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);
1927         if (l) 
1928                 xdisplay_draw(displaylist, gra, l, order+l->order_delta);
1929         if (flags & 1)
1930                 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1931         if (!(flags & 4))
1932                 gra->meth.draw_mode(gra->priv, draw_mode_end);
1933 }
1934
1935 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)
1936 {
1937         int order=transform_get_order(trans);
1938
1939         dbg(1,"enter");
1940         if (displaylist->busy) {
1941                 if (async == 1)
1942                         return;
1943                 do_draw(displaylist, 1, flags);
1944         }
1945         xdisplay_free(displaylist->dl);
1946         dbg(1,"order=%d\n", order);
1947
1948         displaylist->dc.gra=gra;
1949         displaylist->ms=mapset;
1950         displaylist->dc.trans=trans;
1951         displaylist->workload=async ? 100 : 0;
1952         displaylist->cb=cb;
1953         if (l)
1954                 order+=l->order_delta;
1955         displaylist->order=order;
1956         displaylist->busy=1;
1957         displaylist->layout=l;
1958         if (async) {
1959                 if (! displaylist->idle_cb)
1960                         displaylist->idle_cb=callback_new_3(callback_cast(do_draw), displaylist, 0, flags);
1961                 displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
1962         } else
1963                 do_draw(displaylist, 0, flags);
1964 }
1965 /**
1966  * FIXME
1967  * @param <>
1968  * @returns <>
1969  * @author Martin Schaller (04/2008)
1970 */
1971 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)
1972 {
1973         graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb, flags);
1974 }
1975
1976 int
1977 graphics_draw_cancel(struct graphics *gra, struct displaylist *displaylist)
1978 {
1979         if (!displaylist->busy)
1980                 return 0;
1981         do_draw(displaylist, 1, 0);
1982         return 1;
1983 }
1984
1985 /**
1986  * FIXME
1987  * @param <>
1988  * @returns <>
1989  * @author Martin Schaller (04/2008)
1990 */
1991 struct displaylist_handle {
1992         GList *hl_head,*hl,*l_head,*l;
1993 };
1994
1995 /**
1996  * FIXME
1997  * @param <>
1998  * @returns <>
1999  * @author Martin Schaller (04/2008)
2000 */
2001 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
2002 {
2003         struct displaylist_handle *ret;
2004
2005         ret=g_new0(struct displaylist_handle, 1);
2006         if (!displaylist->dl)
2007                 return NULL;
2008         ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
2009         if (!ret->hl) {
2010                 g_free(ret);
2011                 return NULL;
2012         }
2013         ret->l_head=ret->l=g_hash_to_list_keys(ret->hl->data);
2014
2015         return ret;
2016 }
2017
2018 /**
2019  * FIXME
2020  * @param <>
2021  * @returns <>
2022  * @author Martin Schaller (04/2008)
2023 */
2024 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
2025 {
2026         struct displayitem *ret;
2027         if (!dlh)
2028                 return NULL;
2029         if (! dlh->l) {
2030                 dlh->hl=g_list_next(dlh->hl);
2031                 if (!dlh->hl)
2032                         return NULL;
2033                 g_list_free(dlh->l_head);
2034                 dlh->l_head=dlh->l=g_hash_to_list_keys(dlh->hl->data);
2035         }
2036         ret=dlh->l->data;
2037         dlh->l=g_list_next(dlh->l);
2038         return ret;
2039 }
2040
2041 /**
2042  * FIXME
2043  * @param <>
2044  * @returns <>
2045  * @author Martin Schaller (04/2008)
2046 */
2047 void graphics_displaylist_close(struct displaylist_handle *dlh)
2048 {
2049         if (dlh) {
2050                 g_list_free(dlh->hl_head);
2051                 g_list_free(dlh->l_head);
2052                 g_free(dlh);
2053         }
2054 }
2055
2056 /**
2057  * FIXME
2058  * @param <>
2059  * @returns <>
2060  * @author Martin Schaller (04/2008)
2061 */
2062 struct displaylist * graphics_displaylist_new(void)
2063 {
2064         struct displaylist *ret=g_new0(struct displaylist, 1);
2065
2066         ret->dl=g_hash_table_new(NULL,NULL);
2067         ret->dc.maxlen=16384;
2068
2069         return ret;
2070 }
2071
2072 /**
2073  * FIXME
2074  * @param <>
2075  * @returns <>
2076  * @author Martin Schaller (04/2008)
2077 */
2078 struct item * graphics_displayitem_get_item(struct displayitem *di)
2079 {
2080         return &di->item;       
2081 }
2082
2083 int
2084 graphics_displayitem_get_coord_count(struct displayitem *di)
2085 {
2086         return di->count;
2087 }
2088
2089 /**
2090  * FIXME
2091  * @param <>
2092  * @returns <>
2093  * @author Martin Schaller (04/2008)
2094 */
2095 char * graphics_displayitem_get_label(struct displayitem *di)
2096 {
2097         return di->label;
2098 }
2099
2100 int
2101 graphics_displayitem_get_displayed(struct displayitem *di)
2102 {
2103         return di->displayed;
2104 }
2105
2106 /**
2107  * FIXME
2108  * @param <>
2109  * @returns <>
2110  * @author Martin Schaller (04/2008)
2111 */
2112 static int within_dist_point(struct point *p0, struct point *p1, int dist)
2113 {
2114         if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
2115                 return 0;
2116         if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
2117                 return 0;
2118         if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
2119                 return 1;
2120         }
2121         return 0;
2122 }
2123
2124 /**
2125  * FIXME
2126  * @param <>
2127  * @returns <>
2128  * @author Martin Schaller (04/2008)
2129 */
2130 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
2131 {
2132         int vx,vy,wx,wy;
2133         int c1,c2;
2134         struct point line_p;
2135
2136         if (line_p0->x < line_p1->x) {
2137                 if (p->x < line_p0->x - dist)
2138                         return 0;
2139                 if (p->x > line_p1->x + dist)
2140                         return 0;
2141         } else {
2142                 if (p->x < line_p1->x - dist)
2143                         return 0;
2144                 if (p->x > line_p0->x + dist)
2145                         return 0;
2146         }
2147         if (line_p0->y < line_p1->y) {
2148                 if (p->y < line_p0->y - dist)
2149                         return 0;
2150                 if (p->y > line_p1->y + dist)
2151                         return 0;
2152         } else {
2153                 if (p->y < line_p1->y - dist)
2154                         return 0;
2155                 if (p->y > line_p0->y + dist)
2156                         return 0;
2157         }
2158                 
2159         vx=line_p1->x-line_p0->x;
2160         vy=line_p1->y-line_p0->y;
2161         wx=p->x-line_p0->x;
2162         wy=p->y-line_p0->y;
2163
2164         c1=vx*wx+vy*wy;
2165         if ( c1 <= 0 )
2166                 return within_dist_point(p, line_p0, dist);
2167         c2=vx*vx+vy*vy;
2168         if ( c2 <= c1 )
2169                 return within_dist_point(p, line_p1, dist);
2170
2171         line_p.x=line_p0->x+vx*c1/c2;
2172         line_p.y=line_p0->y+vy*c1/c2;
2173         return within_dist_point(p, &line_p, dist);
2174 }
2175
2176 /**
2177  * FIXME
2178  * @param <>
2179  * @returns <>
2180  * @author Martin Schaller (04/2008)
2181 */
2182 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
2183 {
2184         int i;
2185         for (i = 0 ; i < count-1 ; i++) {
2186                 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
2187                         return 1;
2188                 }
2189         }
2190         if (close)
2191                 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
2192         return 0;
2193 }
2194
2195 /**
2196  * FIXME
2197  * @param <>
2198  * @returns <>
2199  * @author Martin Schaller (04/2008)
2200 */
2201 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
2202 {
2203         int i, j, c = 0;
2204         for (i = 0, j = count-1; i < count; j = i++) {
2205                 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
2206                 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
2207                 (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)) 
2208                         c = !c;
2209         }
2210         if (! c)
2211                 return within_dist_polyline(p, poly_pnt, count, dist, 1);
2212         return c;
2213 }
2214
2215 /**
2216  * FIXME
2217  * @param <>
2218  * @returns <>
2219  * @author Martin Schaller (04/2008)
2220 */
2221 int graphics_displayitem_within_dist(struct displaylist *displaylist, struct displayitem *di, struct point *p, int dist)
2222 {
2223         struct point pa[displaylist->dc.maxlen];
2224         int count;
2225
2226         count=transform(displaylist->dc.trans, displaylist->dc.pro, di->c, pa, di->count, 1, 0, NULL);
2227         
2228         if (di->item.type < type_line) {
2229                 return within_dist_point(p, &pa[0], dist);
2230         }
2231         if (di->item.type < type_area) {
2232                 return within_dist_polyline(p, pa, count, dist, 0);
2233         }
2234         return within_dist_polygon(p, pa, count, dist);
2235 }
2236
2237
2238 static void
2239 graphics_process_selection_item(struct displaylist *dl, struct item *item)
2240 {
2241         struct displayitem di,*di_res;
2242         GHashTable *h;
2243         int count,max=dl->dc.maxlen;
2244         struct coord ca[max];
2245         struct attr attr;
2246         struct map_rect *mr;
2247
2248         di.item=*item;
2249         di.label=NULL;
2250         di.displayed=0;
2251         di.count=0;
2252         h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
2253         if (h) {
2254                 di_res=g_hash_table_lookup(h, &di);
2255                 if (di_res) {
2256                         di.item.type=(enum item_type)item->priv_data;
2257                         display_add(dl, &di.item, di_res->count, di_res->c, NULL);
2258                         return;
2259                 }
2260         }
2261         mr=map_rect_new(item->map, NULL);
2262         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
2263         count=item_coord_get(item, ca, item->type < type_line ? 1: max);
2264         if (!item_attr_get(item, attr_label, &attr))
2265                 attr.u.str=NULL;
2266         if (dl->conv && attr.u.str && attr.u.str[0]) {
2267                 char *str=map_convert_string(item->map, attr.u.str);
2268                 display_add(dl, item, count, ca, str);
2269                 map_convert_free(str);
2270         } else
2271                 display_add(dl, item, count, ca, attr.u.str);
2272         map_rect_destroy(mr);
2273 }
2274
2275 void
2276 graphics_add_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
2277 {
2278         struct item *item_dup=g_new(struct item, 1);
2279         *item_dup=*item;
2280         item_dup->priv_data=(void *)type;
2281         gra->selection=g_list_append(gra->selection, item_dup);
2282         if (dl)
2283                 graphics_process_selection_item(dl, item_dup);
2284 }
2285
2286 void
2287 graphics_remove_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
2288 {
2289         GList *curr;
2290         int found;
2291
2292         for (;;) {
2293                 curr=gra->selection;
2294                 found=0;
2295                 while (curr) {
2296                         struct item *sitem=curr->data;
2297                         if (item_is_equal(*item,*sitem)) {
2298                                 if (dl) {
2299                                         struct displayitem di;
2300                                         GHashTable *h;
2301                                         di.item=*sitem;
2302                                         di.label=NULL;
2303                                         di.displayed=0;
2304                                         di.count=0;
2305                                         di.item.type=type;
2306                                         h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
2307                                         if (h)
2308                                                 g_hash_table_remove(h, &di);
2309                                 }
2310                                 g_free(sitem);
2311                                 gra->selection=g_list_remove(gra->selection, curr->data);
2312                                 found=1;
2313                                 break;
2314                         }
2315                 }
2316                 if (!found)
2317                         return;
2318         }
2319 }
2320
2321 void
2322 graphics_clear_selection(struct graphics *gra, struct displaylist *dl)
2323 {
2324         while (gra->selection) {
2325                 struct item *item=(struct item *)gra->selection->data;
2326                 graphics_remove_selection(gra, item, (enum item_type)item->priv_data,dl);
2327         }
2328 }
2329
2330 static void
2331 graphics_process_selection(struct graphics *gra, struct displaylist *dl)
2332 {
2333         GList *curr;
2334
2335         curr=gra->selection;
2336         while (curr) {
2337                 struct item *item=curr->data;
2338                 graphics_process_selection_item(dl, item);
2339                 curr=g_list_next(curr);
2340         }
2341 }