Add:Core:Made cursor configurable via navit.xml
[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 "debug.h"
34 #include "string.h"
35 #include "draw_info.h"
36 #include "point.h"
37 #include "graphics.h"
38 #include "projection.h"
39 #include "map.h"
40 #include "coord.h"
41 #include "transform.h"
42 #include "plugin.h"
43 #include "profile.h"
44 #include "mapset.h"
45 #include "layout.h"
46 #include "route.h"
47 #include "util.h"
48 #include "callback.h"
49 #include "file.h"
50
51 static char *navit_sharedir;
52
53 //##############################################################################################################
54 //# Description: 
55 //# Comment: 
56 //# Authors: Martin Schaller (04/2008)
57 //##############################################################################################################
58 struct graphics
59 {
60         struct graphics_priv *priv;
61         struct graphics_methods meth;
62         char *default_font;
63         struct graphics_font *font[16];
64         struct graphics_gc *gc[3];
65         struct attr **attrs;
66         struct callback_list *cbl;
67         int ready;
68 };
69
70
71 struct displaylist {
72         GHashTable *dl;
73 };
74
75 /**
76  * Creates a new graphics object
77  * attr type required
78  * @param <>
79  * @returns <>
80  * @author Martin Schaller (04/2008)
81 */
82 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
83 {
84         struct graphics *this_;
85         struct attr *type_attr;
86         struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl);
87
88         if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
89                 return NULL;
90         }
91
92         graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
93         if (! graphicstype_new)
94                 return NULL;
95         this_=g_new0(struct graphics, 1);
96         this_->cbl=callback_list_new();
97         this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
98         this_->attrs=attr_list_dup(attrs);
99         return this_;
100 }
101
102 /**
103  * FIXME
104  * @param <>
105  * @returns <>
106  * @author Martin Schaller (04/2008)
107 */
108 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
109 {
110         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
111 }
112
113 /**
114  * FIXME
115  * @param <>
116  * @returns <>
117  * @author Martin Schaller (04/2008)
118 */
119 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha)
120 {
121         struct graphics *this_;
122         this_=g_new0(struct graphics, 1);
123         this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha);
124         return this_;
125 }
126
127 /**
128  * FIXME
129  * @param <>
130  * @returns <>
131  * @author Martin Schaller (04/2008)
132 */
133 void graphics_init(struct graphics *this_)
134 {
135         this_->gc[0]=graphics_gc_new(this_);
136         graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
137         graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
138         this_->gc[1]=graphics_gc_new(this_);
139         graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
140         graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
141         this_->gc[2]=graphics_gc_new(this_);
142         graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
143         graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
144         graphics_background_gc(this_, this_->gc[0]);
145         navit_sharedir = getenv("NAVIT_SHAREDIR");
146 }
147
148 /**
149  * FIXME
150  * @param <>
151  * @returns <>
152  * @author Martin Schaller (04/2008)
153 */
154 void * graphics_get_data(struct graphics *this_, char *type)
155 {
156         return (this_->meth.get_data(this_->priv, type));
157 }
158
159 void graphics_add_callback(struct graphics *this_, struct callback *cb)
160 {
161         callback_list_add(this_->cbl, cb);
162 }
163
164 /**
165  * FIXME
166  * @param <>
167  * @returns <>
168  * @author Martin Schaller (04/2008)
169 */
170 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
171 {
172         struct graphics_font *this_;
173
174         this_=g_new0(struct graphics_font,1);
175         this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
176         return this_;
177 }
178
179 /**
180  * Free all loaded fonts.
181  * Used when switching layouts.
182  * @param gra The graphics instance
183  * @returns nothing
184  * @author Sarah Nordstrom (05/2008)
185  */
186 void graphics_font_destroy_all(struct graphics *gra) 
187
188         int i; 
189         for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) { 
190                 if(!gra->font[i]) continue; 
191                 gra->font[i]->meth.font_destroy(gra->font[i]->priv); 
192                 gra->font[i] = NULL; 
193         }
194 }
195
196 /**
197  * FIXME
198  * @param <>
199  * @returns <>
200  * @author Martin Schaller (04/2008)
201 */
202 struct graphics_gc * graphics_gc_new(struct graphics *gra)
203 {
204         struct graphics_gc *this_;
205
206         this_=g_new0(struct graphics_gc,1);
207         this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
208         return this_;
209 }
210
211 /**
212  * FIXME
213  * @param <>
214  * @returns <>
215  * @author Martin Schaller (04/2008)
216 */
217 void graphics_gc_destroy(struct graphics_gc *gc)
218 {
219         gc->meth.gc_destroy(gc->priv);
220         g_free(gc);
221 }
222
223 /**
224  * FIXME
225  * @param <>
226  * @returns <>
227  * @author Martin Schaller (04/2008)
228 */
229 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
230 {
231         gc->meth.gc_set_foreground(gc->priv, c);
232 }
233
234 /**
235  * FIXME
236  * @param <>
237  * @returns <>
238  * @author Martin Schaller (04/2008)
239 */
240 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
241 {
242         gc->meth.gc_set_background(gc->priv, c);
243 }
244
245 /**
246  * FIXME
247  * @param <>
248  * @returns <>
249  * @author Martin Schaller (04/2008)
250 */
251 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
252 {
253         gc->meth.gc_set_linewidth(gc->priv, width);
254 }
255
256 /**
257  * FIXME
258  * @param <>
259  * @returns <>
260  * @author Martin Schaller (04/2008)
261 */
262 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
263 {
264         if (gc->meth.gc_set_dashes)
265                 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
266 }
267
268 /**
269  * Create a new image from file path scaled to w and h pixels
270  * @param gra the graphics instance
271  * @param path path of the image to load
272  * @param w width to rescale to
273  * @param h height to rescale to
274  * @returns <>
275  * @author Martin Schaller (04/2008)
276 */
277 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
278 {
279         struct graphics_image *this_;
280
281         this_=g_new0(struct graphics_image,1);
282         this_->height=h;
283         this_->width=w;
284         this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
285         if (! this_->priv) {
286                 g_free(this_);
287                 this_=NULL;
288         }
289         return this_;
290 }
291
292 /**
293  * Create a new image from file path
294  * @param gra the graphics instance
295  * @param path path of the image to load
296  * @returns <>
297  * @author Martin Schaller (04/2008)
298 */
299 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
300 {
301         return graphics_image_new_scaled(gra, path, -1, -1);
302 }
303
304 /**
305  * FIXME
306  * @param <>
307  * @returns <>
308  * @author Martin Schaller (04/2008)
309 */
310 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
311 {
312         if (gra->meth.image_free)
313                 gra->meth.image_free(gra->priv, img->priv);
314         g_free(img);
315 }
316
317 /**
318  * FIXME
319  * @param <>
320  * @returns <>
321  * @author Martin Schaller (04/2008)
322 */
323 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
324 {
325         this_->meth.draw_restore(this_->priv, p, w, h);
326 }
327
328 /**
329  * FIXME
330  * @param <>
331  * @returns <>
332  * @author Martin Schaller (04/2008)
333 */
334 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
335 {
336         this_->meth.draw_mode(this_->priv, mode);
337 }
338
339 /**
340  * FIXME
341  * @param <>
342  * @returns <>
343  * @author Martin Schaller (04/2008)
344 */
345 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
346 {
347         this_->meth.draw_lines(this_->priv, gc->priv, p, count);
348 }
349
350 /**
351  * FIXME
352  * @param <>
353  * @returns <>
354  * @author Martin Schaller (04/2008)
355 */
356 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
357 {
358         this_->meth.draw_circle(this_->priv, gc->priv, p, r);
359 }
360
361 /**
362  * FIXME
363  * @param <>
364  * @returns <>
365  * @author Martin Schaller (04/2008)
366 */
367 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
368 {
369         this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
370 }
371
372 /**
373  * FIXME
374  * @param <>
375  * @returns <>
376  * @author Martin Schaller (04/2008)
377 */
378 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)
379 {
380         this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
381 }
382
383 /**
384  * FIXME
385  * @param <>
386  * @returns <>
387  * @author Martin Schaller (04/2008)
388 */
389 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret)
390 {
391         this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret);
392 }
393
394 /**
395  * FIXME
396  * @param <>
397  * @returns <>
398  * @author Martin Schaller (04/2008)
399 */
400 void graphics_overlay_disable(struct graphics *this_, int disable)
401 {
402         if (this_->meth.overlay_disable)
403                 this_->meth.overlay_disable(this_->priv, disable);
404 }
405
406 /**
407  * FIXME
408  * @param <>
409  * @returns <>
410  * @author Martin Schaller (04/2008)
411 */
412 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
413 {
414         this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
415 }
416
417
418 //##############################################################################################################
419 //# Description:
420 //# Comment:
421 //# Authors: Martin Schaller (04/2008)
422 //##############################################################################################################
423 int
424 graphics_draw_drag(struct graphics *this_, struct point *p)
425 {
426         if (!this_->meth.draw_drag)
427                 return 0;
428         this_->meth.draw_drag(this_->priv, p);
429         return 1;
430 }
431
432 void
433 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
434 {
435         this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
436 }
437
438 #include "attr.h"
439 #include "popup.h"
440 #include <stdio.h>
441
442
443 #if 0
444 //##############################################################################################################
445 //# Description: 
446 //# Comment: 
447 //# Authors: Martin Schaller (04/2008)
448 //##############################################################################################################
449 static void popup_view_html(struct popup_item *item, char *file)
450 {
451         char command[1024];
452         sprintf(command,"firefox %s", file);
453         system(command);
454 }
455
456 //##############################################################################################################
457 //# Description: 
458 //# Comment: 
459 //# Authors: Martin Schaller (04/2008)
460 //##############################################################################################################
461 static void graphics_popup(struct display_list *list, struct popup_item **popup)
462 {
463         struct item *item;
464         struct attr attr;
465         struct map_rect *mr;
466         struct coord c;
467         struct popup_item *curr_item,*last=NULL;
468         item=list->data;
469         mr=map_rect_new(item->map, NULL, NULL, 0);
470         printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
471         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
472         if (item) {
473                 if (item_attr_get(item, attr_name, &attr)) {
474                         curr_item=popup_item_new_text(popup,attr.u.str,1);
475                         if (item_attr_get(item, attr_info_html, &attr)) {
476                                 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
477                         }
478                         if (item_attr_get(item, attr_price_html, &attr)) {
479                                 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
480                         }
481                         curr_item->submenu=last;
482                 }
483         }
484         map_rect_destroy(mr);
485 }
486 #endif
487
488 /**
489  * FIXME
490  * @param <>
491  * @returns <>
492  * @author Martin Schaller (04/2008)
493 */
494 struct displayitem {
495         struct item item;
496         char *label;
497         int displayed;
498         int count;
499         struct point pnt[0];
500 };
501
502 /**
503  * FIXME
504  * @param <>
505  * @returns <>
506  * @author Martin Schaller (04/2008)
507 */
508 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
509 {
510         GList *h, *l;
511         h=value;
512         l=h;
513         while (l) {
514                 struct displayitem *di=l->data;
515                 if (! di->displayed && di->item.type < type_line) 
516                         dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
517                 g_free(l->data);
518                 l=g_list_next(l);
519         }
520         g_list_free(h);
521         return TRUE;
522 }
523
524 /**
525  * FIXME
526  * @param <>
527  * @returns <>
528  * @author Martin Schaller (04/2008)
529 */
530 static void xdisplay_free(GHashTable *display_list)
531 {
532         g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
533 }
534
535 /**
536  * FIXME
537  * @param <>
538  * @returns <>
539  * @author Martin Schaller (04/2008)
540 */
541 void display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
542 {
543         struct displayitem *di;
544         int len;
545         GList *l;
546         char *p;
547
548         len=sizeof(*di)+count*sizeof(*pnt);
549         if (label)
550                 len+=strlen(label)+1;
551
552         p=g_malloc(len);
553
554         di=(struct displayitem *)p;
555         di->displayed=0;
556         p+=sizeof(*di)+count*sizeof(*pnt);
557         di->item=*item;
558         if (label) {
559                 di->label=p;
560                 strcpy(di->label, label);
561         } else 
562                 di->label=NULL;
563         di->count=count;
564         memcpy(di->pnt, pnt, count*sizeof(*pnt));
565
566         l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
567         l=g_list_prepend(l, di);
568         g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
569 }
570
571
572 /**
573  * FIXME
574  * @param <>
575  * @returns <>
576  * @author Martin Schaller (04/2008)
577 */
578 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)
579 {
580         int i,x,y,tl;
581         double dx,dy,l;
582         struct point p_t;
583
584         tl=strlen(label)*400;
585         for (i = 0 ; i < count-1 ; i++) {
586                 dx=p[i+1].x-p[i].x;
587                 dx*=100;
588                 dy=p[i+1].y-p[i].y;
589                 dy*=100;
590                 l=(int)sqrt((float)(dx*dx+dy*dy));
591                 if (l > tl) {
592                         x=p[i].x;
593                         y=p[i].y;
594                         if (dx < 0) {
595                                 dx=-dx;
596                                 dy=-dy;
597                                 x=p[i+1].x;
598                                 y=p[i+1].y;
599                         }
600                         x+=(l-tl)*dx/l/200;
601                         y+=(l-tl)*dy/l/200;
602                         x-=dy*45/l/10;
603                         y+=dx*45/l/10;
604                         p_t.x=x;
605                         p_t.y=y;
606         #if 0
607                         printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
608         #endif
609                         gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
610                 }
611         }
612 }
613
614 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
615 {
616         struct point pnt[3];
617         pnt[0]=pnt[1]=pnt[2]=*p;
618         pnt[0].x+=-dx*l/65536+dy*l/65536;
619         pnt[0].y+=-dy*l/65536-dx*l/65536;
620         pnt[2].x+=-dx*l/65536-dy*l/65536;
621         pnt[2].y+=-dy*l/65536+dx*l/65536;
622         gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
623 }
624
625 static void display_draw_arrows(struct displayitem *di, struct graphics_gc *gc, struct graphics *gra)
626 {
627         int i,dx,dy,l;
628         struct point p;
629         for (i = 0 ; i < di->count-1 ; i++) {
630                 dx=di->pnt[i+1].x-di->pnt[i].x; 
631                 dy=di->pnt[i+1].y-di->pnt[i].y;
632                 l=sqrt(dx*dx+dy*dy);
633                 if (l) {
634                         dx=dx*65536/l;
635                         dy=dy*65536/l;
636                         p=di->pnt[i];
637                         p.x+=dx*15/65536;
638                         p.y+=dy*15/65536;
639                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
640                         p=di->pnt[i+1];
641                         p.x-=dx*15/65536;
642                         p.y-=dy*15/65536;
643                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
644                 }
645         }
646 }
647
648 /**
649  * FIXME
650  * @param <>
651  * @returns <>
652  * @author Martin Schaller (04/2008)
653 */
654 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
655 {
656         struct element *e;
657         GList *l,*ls,*es,*types;
658         enum item_type type;
659         struct graphics_gc *gc = NULL;
660         struct graphics_image *img;
661         struct point p;
662         char path[PATH_MAX];
663
664         es=itm->elements;
665         while (es) {
666                 e=es->data;
667                 types=itm->type;
668                 while (types) {
669                         type=GPOINTER_TO_INT(types->data);
670                         ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
671                         l=ls;
672                         if (gc)
673                                 graphics_gc_destroy(gc);
674                         gc=NULL;
675                         img=NULL;
676                         while (l) {
677                                 struct displayitem *di;
678                                 di=l->data;
679                                 di->displayed=1;
680                                 if (! gc) {
681                                         gc=graphics_gc_new(gra);
682                                         gc->meth.gc_set_foreground(gc->priv, &e->color);
683                                 }
684                                 switch (e->type) {
685                                 case element_polygon:
686                                         gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
687                                         break;
688                                 case element_polyline:
689                                         if (e->u.polyline.width > 1) 
690                                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
691                                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
692                                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
693                                                                        e->u.polyline.offset,
694                                                                        e->u.polyline.dash_table,
695                                                                        e->u.polyline.dash_num);
696                                         gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
697                                         break;
698                                 case element_circle:
699                                         if (e->u.circle.width > 1) 
700                                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
701                                         gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
702                                         if (di->label && e->text_size) {
703                                                 p.x=di->pnt[0].x+3;
704                                                 p.y=di->pnt[0].y+10;
705                                                 if (! gra->font[e->text_size])
706                                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
707                                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, di->label, &p, 0x10000, 0);
708                                         }
709                                         break;
710                                 case element_text:
711                                         if (di->label) {
712                                                 if (! gra->font[e->text_size])
713                                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
714                                                 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], di->pnt, di->count, di->label);
715                                         }
716                                         break;
717                                 case element_icon:
718                                         if (!img) {
719                                                 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
720                                                 img=graphics_image_new_scaled(gra, path, e->u.icon.width, e->u.icon.height);
721                                                 if (! img)
722                                                         dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
723                                         }
724                                         if (img) {
725                                                 p.x=di->pnt[0].x - img->hot.x;
726                                                 p.y=di->pnt[0].y - img->hot.y;
727                                                 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
728                                                 graphics_image_free(gra, img);
729                                                 img = NULL;
730                                         }
731                                         break;
732                                 case element_image:
733                                         dbg(1,"image: '%s'\n", di->label);
734                                         if (gra->meth.draw_image_warp)
735                                                 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
736                                         else
737                                                 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
738                                         break;
739                                 case element_arrows:
740                                         display_draw_arrows(di,gc,gra);
741                                         break;
742                                 default:
743                                         printf("Unhandled element type %d\n", e->type);
744                                 
745                                 }
746                                 l=g_list_next(l);
747                         }
748                         types=g_list_next(types);
749                 }
750                 es=g_list_next(es);
751         }
752         if (gc)
753                 graphics_gc_destroy(gc);
754 }
755
756 void
757 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
758 {
759         GList *es;
760         struct point p;
761         char *label=NULL;
762         struct graphics_gc *gc = NULL;
763         es=itm->elements;
764         while (es) {
765                 struct element *e=es->data;
766                 int count=e->coord_count;
767                 struct point pnt[count];
768                 transform(t, projection_screen, e->coord, pnt, count, 0);
769                 gc=graphics_gc_new(gra);
770                 gc->meth.gc_set_foreground(gc->priv, &e->color);
771                 switch (e->type) {
772                 case element_polyline:
773                         if (e->u.polyline.width > 1) 
774                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
775                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
776                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
777                                                        e->u.polyline.offset,
778                                                        e->u.polyline.dash_table,
779                                                        e->u.polyline.dash_num);
780                         gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
781                         break;
782                 case element_circle:
783                         if (e->u.circle.width > 1) 
784                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
785                         gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
786                         if (label && e->text_size) {
787                                 p.x=pnt[0].x+3;
788                                 p.y=pnt[0].y+10;
789                         if (! gra->font[e->text_size])
790                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
791                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
792                         }
793                 break;
794                 default:
795                         dbg(0,"dont know how to draw %d\n", e->type);
796                 }
797                 graphics_gc_destroy(gc);
798                 es=g_list_next(es);
799         }
800 }
801
802 /**
803  * FIXME
804  * @param <>
805  * @returns <>
806  * @author Martin Schaller (04/2008)
807 */
808 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
809 {
810         GList *itms;
811         struct itemgra *itm;
812
813         itms=lay->itemgras;
814         while (itms) {
815                 itm=itms->data;
816                 if (order >= itm->order.min && order <= itm->order.max) 
817                         xdisplay_draw_elements(gra, display_list, itm);
818                 itms=g_list_next(itms);
819         }
820 }
821
822 /**
823  * FIXME
824  * @param <>
825  * @returns <>
826  * @author Martin Schaller (04/2008)
827 */
828 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
829 {
830         GList *lays;
831         struct layer *lay;
832         
833         lays=l->layers;
834         while (lays) {
835                 lay=lays->data;
836                 xdisplay_draw_layer(display_list, gra, lay, order);
837                 lays=g_list_next(lays);
838         }
839 }
840
841 /**
842  * FIXME
843  * @param <>
844  * @returns <>
845  * @author Martin Schaller (04/2008)
846 */
847 extern void *route_selection;
848
849 /**
850  * FIXME
851  * @param <>
852  * @returns <>
853  * @author Martin Schaller (04/2008)
854 */
855 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
856 {
857         enum projection pro;
858         struct map_rect *mr;
859         struct item *item;
860         int conv,count,max=16384;
861         struct point pnt[max];
862         struct coord ca[max];
863         struct attr attr;
864         struct map_selection *sel;
865
866         pro=map_projection(m);
867         conv=map_requires_conversion(m);
868         sel=transform_get_selection(t, pro, order);
869         if (route_selection)
870                 mr=map_rect_new(m, route_selection);
871         else
872                 mr=map_rect_new(m, sel);
873         if (! mr) {
874                 map_selection_destroy(sel);
875                 return;
876         }
877         while ((item=map_rect_get_item(mr))) {
878                 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
879                 if (item->type >= type_line && count < 2) {
880                         dbg(1,"poly from map has only %d points\n", count);
881                         continue;
882                 }
883                 if (item->type < type_line) {
884                         if (! map_selection_contains_point(sel, &ca[0])) {
885                                 dbg(1,"point not visible\n");
886                                 continue;
887                         }
888                 } else if (item->type < type_area) {
889                         if (! map_selection_contains_polyline(sel, ca, count)) {
890                                 dbg(1,"polyline not visible\n");
891                                 continue;
892                         }
893                 } else {
894                         if (! map_selection_contains_polygon(sel, ca, count)) {
895                                 dbg(1,"polygon not visible\n");
896                                 continue;
897                         }
898                 }
899                 if (count == max) 
900                         dbg(0,"point count overflow\n", count);
901                 count=transform(t, pro, ca, pnt, count, 1);
902                 if (item->type >= type_line && count < 2) {
903                         dbg(1,"poly from transform has only %d points\n", count);
904                         continue;
905                 }
906                 if (!item_attr_get(item, attr_label, &attr))
907                         attr.u.str=NULL;
908                 if (conv && attr.u.str && attr.u.str[0]) {
909                         char *str=map_convert_string(m, attr.u.str);
910                         display_add(displaylist, item, count, pnt, str);
911                         map_convert_free(str);
912                 } else
913                         display_add(displaylist, item, count, pnt, attr.u.str);
914         }
915         map_rect_destroy(mr);
916         map_selection_destroy(sel);
917 }
918
919 /**
920  * FIXME
921  * @param <>
922  * @returns <>
923  * @author Martin Schaller (04/2008)
924 */
925 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
926 {
927         struct mapset *ms;
928         struct map *m;
929         struct mapset_handle *h;
930
931         if (! mapsets)
932                 return;
933         ms=mapsets->data;
934         h=mapset_open(ms);
935         while ((m=mapset_next(h, 1))) {
936                 do_draw_map(displaylist, t, m, order);
937         }
938         mapset_close(h);
939 }
940
941 /**
942  * FIXME
943  * @param <>
944  * @returns <>
945  * @author Martin Schaller (04/2008)
946 */
947 int graphics_ready(struct graphics *this_)
948 {
949         return this_->ready;
950 }
951
952 /**
953  * FIXME
954  * @param <>
955  * @returns <>
956  * @author Martin Schaller (04/2008)
957 */
958 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
959 {
960         int order=transform_get_order(trans);
961         struct point p;
962         p.x=0;
963         p.y=0;
964         // FIXME find a better place to set the background color
965         graphics_gc_set_background(gra->gc[0], &l->color);
966         graphics_gc_set_foreground(gra->gc[0], &l->color);
967         gra->default_font = g_strdup(l->font);
968         graphics_background_gc(gra, gra->gc[0]);
969         gra->meth.draw_mode(gra->priv, draw_mode_begin);
970         gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
971         xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
972         if (callback)
973                 callback_list_call_attr_0(gra->cbl, attr_postdraw);
974         gra->meth.draw_mode(gra->priv, draw_mode_end);
975 }
976
977 /**
978  * FIXME
979  * @param <>
980  * @returns <>
981  * @author Martin Schaller (04/2008)
982 */
983 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
984 {
985         struct displaylist_handle *dlh;
986         struct displayitem *di;
987         int i;
988
989         dlh=graphics_displaylist_open(displaylist);
990         while ((di=graphics_displaylist_next(dlh))) {
991                 for (i = 0 ; i < di->count ; i++) {
992                         di->pnt[i].x+=dx;
993                         di->pnt[i].y+=dy;
994                 }
995         }
996         graphics_displaylist_close(dlh);
997 }
998
999 /**
1000  * FIXME
1001  * @param <>
1002  * @returns <>
1003  * @author Martin Schaller (04/2008)
1004 */
1005 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
1006 {
1007         int order=transform_get_order(trans);
1008
1009         dbg(1,"enter");
1010
1011 #if 0
1012         printf("scale=%d center=0x%x,0x%x mercator scale=%f\n", scale, co->trans->center.x, co->trans->center.y, transform_scale(co->trans->center.y));
1013 #endif
1014         
1015         xdisplay_free(displaylist->dl);
1016         dbg(1,"order=%d\n", order);
1017
1018
1019 #if 0
1020         for (i = 0 ; i < data_window_type_end; i++) {
1021                 data_window_begin(co->data_window[i]);  
1022         }
1023 #endif
1024         profile(0,NULL);
1025         order+=l->order_delta;
1026         do_draw(displaylist, trans, mapsets, order);
1027 //      profile(1,"do_draw");
1028         graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1029         profile(1,"xdisplay_draw");
1030         profile(0,"end");
1031   
1032 #if 0
1033         for (i = 0 ; i < data_window_type_end; i++) {
1034                 data_window_end(co->data_window[i]);    
1035         }
1036 #endif
1037         gra->ready=1;
1038 }
1039
1040 /**
1041  * FIXME
1042  * @param <>
1043  * @returns <>
1044  * @author Martin Schaller (04/2008)
1045 */
1046 struct displaylist_handle {
1047         GList *hl_head,*hl,*l;
1048 };
1049
1050 /**
1051  * FIXME
1052  * @param <>
1053  * @returns <>
1054  * @author Martin Schaller (04/2008)
1055 */
1056 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1057 {
1058         struct displaylist_handle *ret;
1059
1060         ret=g_new0(struct displaylist_handle, 1);
1061         ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1062
1063         return ret;
1064 }
1065
1066 /**
1067  * FIXME
1068  * @param <>
1069  * @returns <>
1070  * @author Martin Schaller (04/2008)
1071 */
1072 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1073 {
1074         struct displayitem *ret;
1075         if (! dlh->l) {
1076                 if (!dlh->hl)
1077                         return NULL;
1078                 dlh->l=dlh->hl->data;
1079                 dlh->hl=g_list_next(dlh->hl);
1080         }
1081         ret=dlh->l->data;
1082         dlh->l=g_list_next(dlh->l);
1083         return ret;
1084 }
1085
1086 /**
1087  * FIXME
1088  * @param <>
1089  * @returns <>
1090  * @author Martin Schaller (04/2008)
1091 */
1092 void graphics_displaylist_close(struct displaylist_handle *dlh)
1093 {
1094         g_list_free(dlh->hl_head);
1095         g_free(dlh);
1096 }
1097
1098 /**
1099  * FIXME
1100  * @param <>
1101  * @returns <>
1102  * @author Martin Schaller (04/2008)
1103 */
1104 struct displaylist * graphics_displaylist_new(void)
1105 {
1106         struct displaylist *ret=g_new(struct displaylist, 1);
1107
1108         ret->dl=g_hash_table_new(NULL,NULL);
1109
1110         return ret;
1111 }
1112
1113 /**
1114  * FIXME
1115  * @param <>
1116  * @returns <>
1117  * @author Martin Schaller (04/2008)
1118 */
1119 struct item * graphics_displayitem_get_item(struct displayitem *di)
1120 {
1121         return &di->item;       
1122 }
1123
1124 /**
1125  * FIXME
1126  * @param <>
1127  * @returns <>
1128  * @author Martin Schaller (04/2008)
1129 */
1130 char * graphics_displayitem_get_label(struct displayitem *di)
1131 {
1132         return di->label;
1133 }
1134
1135 /**
1136  * FIXME
1137  * @param <>
1138  * @returns <>
1139  * @author Martin Schaller (04/2008)
1140 */
1141 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1142 {
1143         if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1144                 return 0;
1145         if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1146                 return 0;
1147         if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1148                 return 1;
1149         }
1150         return 0;
1151 }
1152
1153 /**
1154  * FIXME
1155  * @param <>
1156  * @returns <>
1157  * @author Martin Schaller (04/2008)
1158 */
1159 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1160 {
1161         int vx,vy,wx,wy;
1162         int c1,c2;
1163         struct point line_p;
1164
1165         vx=line_p1->x-line_p0->x;
1166         vy=line_p1->y-line_p0->y;
1167         wx=p->x-line_p0->x;
1168         wy=p->y-line_p0->y;
1169
1170         c1=vx*wx+vy*wy;
1171         if ( c1 <= 0 )
1172                 return within_dist_point(p, line_p0, dist);
1173         c2=vx*vx+vy*vy;
1174         if ( c2 <= c1 )
1175                 return within_dist_point(p, line_p1, dist);
1176
1177         line_p.x=line_p0->x+vx*c1/c2;
1178         line_p.y=line_p0->y+vy*c1/c2;
1179         return within_dist_point(p, &line_p, dist);
1180 }
1181
1182 /**
1183  * FIXME
1184  * @param <>
1185  * @returns <>
1186  * @author Martin Schaller (04/2008)
1187 */
1188 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1189 {
1190         int i;
1191         for (i = 0 ; i < count-1 ; i++) {
1192                 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1193                         return 1;
1194                 }
1195         }
1196         if (close)
1197                 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1198         return 0;
1199 }
1200
1201 /**
1202  * FIXME
1203  * @param <>
1204  * @returns <>
1205  * @author Martin Schaller (04/2008)
1206 */
1207 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1208 {
1209         int i, j, c = 0;
1210         for (i = 0, j = count-1; i < count; j = i++) {
1211                 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1212                 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1213                 (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)) 
1214                         c = !c;
1215         }
1216         if (! c)
1217                 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1218         return c;
1219 }
1220
1221 /**
1222  * FIXME
1223  * @param <>
1224  * @returns <>
1225  * @author Martin Schaller (04/2008)
1226 */
1227 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1228 {
1229         if (di->item.type < type_line) {
1230                 return within_dist_point(p, &di->pnt[0], dist);
1231         }
1232         if (di->item.type < type_area) {
1233                 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
1234         }
1235         return within_dist_polygon(p, di->pnt, di->count, dist);
1236 }