Add:core:Added possibility to use pngs as cursor, added cursor desings from nekohayo
[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, 0);
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 scaled to w and h pixels and possibly rotated
294  * @param gra the graphics instance
295  * @param path path of the image to load
296  * @param w width to rescale to
297  * @param h height to rescale to
298  * @param rotate angle to rotate the image. Warning, graphics might only support 90 degree steps here
299  * @returns <>
300  * @author Martin Schaller (04/2008)
301 */
302 struct graphics_image * graphics_image_new_scaled_rotated(struct graphics *gra, char *path, int w, int h, int rotate)
303 {
304         struct graphics_image *this_;
305
306         this_=g_new0(struct graphics_image,1);
307         this_->height=h;
308         this_->width=w;
309         this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, rotate);
310         if (! this_->priv) {
311                 g_free(this_);
312                 this_=NULL;
313         }
314         return this_;
315 }
316
317 /**
318  * Create a new image from file path
319  * @param gra the graphics instance
320  * @param path path of the image to load
321  * @returns <>
322  * @author Martin Schaller (04/2008)
323 */
324 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
325 {
326         return graphics_image_new_scaled(gra, path, -1, -1);
327 }
328
329 /**
330  * FIXME
331  * @param <>
332  * @returns <>
333  * @author Martin Schaller (04/2008)
334 */
335 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
336 {
337         if (gra->meth.image_free)
338                 gra->meth.image_free(gra->priv, img->priv);
339         g_free(img);
340 }
341
342 /**
343  * FIXME
344  * @param <>
345  * @returns <>
346  * @author Martin Schaller (04/2008)
347 */
348 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
349 {
350         this_->meth.draw_restore(this_->priv, p, w, h);
351 }
352
353 /**
354  * FIXME
355  * @param <>
356  * @returns <>
357  * @author Martin Schaller (04/2008)
358 */
359 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
360 {
361         this_->meth.draw_mode(this_->priv, mode);
362 }
363
364 /**
365  * FIXME
366  * @param <>
367  * @returns <>
368  * @author Martin Schaller (04/2008)
369 */
370 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
371 {
372         this_->meth.draw_lines(this_->priv, gc->priv, p, count);
373 }
374
375 /**
376  * FIXME
377  * @param <>
378  * @returns <>
379  * @author Martin Schaller (04/2008)
380 */
381 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
382 {
383         this_->meth.draw_circle(this_->priv, gc->priv, p, r);
384 }
385
386 /**
387  * FIXME
388  * @param <>
389  * @returns <>
390  * @author Martin Schaller (04/2008)
391 */
392 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
393 {
394         this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
395 }
396
397 /**
398  * FIXME
399  * @param <>
400  * @returns <>
401  * @author Martin Schaller (04/2008)
402 */
403 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)
404 {
405         this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
406 }
407
408 /**
409  * FIXME
410  * @param <>
411  * @returns <>
412  * @author Martin Schaller (04/2008)
413 */
414 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret)
415 {
416         this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret);
417 }
418
419 /**
420  * FIXME
421  * @param <>
422  * @returns <>
423  * @author Martin Schaller (04/2008)
424 */
425 void graphics_overlay_disable(struct graphics *this_, int disable)
426 {
427         if (this_->meth.overlay_disable)
428                 this_->meth.overlay_disable(this_->priv, disable);
429 }
430
431 /**
432  * FIXME
433  * @param <>
434  * @returns <>
435  * @author Martin Schaller (04/2008)
436 */
437 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
438 {
439         this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
440 }
441
442
443 //##############################################################################################################
444 //# Description:
445 //# Comment:
446 //# Authors: Martin Schaller (04/2008)
447 //##############################################################################################################
448 int
449 graphics_draw_drag(struct graphics *this_, struct point *p)
450 {
451         if (!this_->meth.draw_drag)
452                 return 0;
453         this_->meth.draw_drag(this_->priv, p);
454         return 1;
455 }
456
457 void
458 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
459 {
460         this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
461 }
462
463 #include "attr.h"
464 #include "popup.h"
465 #include <stdio.h>
466
467
468 #if 0
469 //##############################################################################################################
470 //# Description: 
471 //# Comment: 
472 //# Authors: Martin Schaller (04/2008)
473 //##############################################################################################################
474 static void popup_view_html(struct popup_item *item, char *file)
475 {
476         char command[1024];
477         sprintf(command,"firefox %s", file);
478         system(command);
479 }
480
481 //##############################################################################################################
482 //# Description: 
483 //# Comment: 
484 //# Authors: Martin Schaller (04/2008)
485 //##############################################################################################################
486 static void graphics_popup(struct display_list *list, struct popup_item **popup)
487 {
488         struct item *item;
489         struct attr attr;
490         struct map_rect *mr;
491         struct coord c;
492         struct popup_item *curr_item,*last=NULL;
493         item=list->data;
494         mr=map_rect_new(item->map, NULL, NULL, 0);
495         printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
496         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
497         if (item) {
498                 if (item_attr_get(item, attr_name, &attr)) {
499                         curr_item=popup_item_new_text(popup,attr.u.str,1);
500                         if (item_attr_get(item, attr_info_html, &attr)) {
501                                 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
502                         }
503                         if (item_attr_get(item, attr_price_html, &attr)) {
504                                 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
505                         }
506                         curr_item->submenu=last;
507                 }
508         }
509         map_rect_destroy(mr);
510 }
511 #endif
512
513 /**
514  * FIXME
515  * @param <>
516  * @returns <>
517  * @author Martin Schaller (04/2008)
518 */
519 struct displayitem {
520         struct item item;
521         char *label;
522         int displayed;
523         int count;
524         struct point pnt[0];
525 };
526
527 /**
528  * FIXME
529  * @param <>
530  * @returns <>
531  * @author Martin Schaller (04/2008)
532 */
533 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
534 {
535         GList *h, *l;
536         h=value;
537         l=h;
538         while (l) {
539                 struct displayitem *di=l->data;
540                 if (! di->displayed && di->item.type < type_line) 
541                         dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
542                 g_free(l->data);
543                 l=g_list_next(l);
544         }
545         g_list_free(h);
546         return TRUE;
547 }
548
549 /**
550  * FIXME
551  * @param <>
552  * @returns <>
553  * @author Martin Schaller (04/2008)
554 */
555 static void xdisplay_free(GHashTable *display_list)
556 {
557         g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
558 }
559
560 /**
561  * FIXME
562  * @param <>
563  * @returns <>
564  * @author Martin Schaller (04/2008)
565 */
566 void display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
567 {
568         struct displayitem *di;
569         int len;
570         GList *l;
571         char *p;
572
573         len=sizeof(*di)+count*sizeof(*pnt);
574         if (label)
575                 len+=strlen(label)+1;
576
577         p=g_malloc(len);
578
579         di=(struct displayitem *)p;
580         di->displayed=0;
581         p+=sizeof(*di)+count*sizeof(*pnt);
582         di->item=*item;
583         if (label) {
584                 di->label=p;
585                 strcpy(di->label, label);
586         } else 
587                 di->label=NULL;
588         di->count=count;
589         memcpy(di->pnt, pnt, count*sizeof(*pnt));
590
591         l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
592         l=g_list_prepend(l, di);
593         g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
594 }
595
596
597 /**
598  * FIXME
599  * @param <>
600  * @returns <>
601  * @author Martin Schaller (04/2008)
602 */
603 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)
604 {
605         int i,x,y,tl;
606         double dx,dy,l;
607         struct point p_t;
608
609         tl=strlen(label)*400;
610         for (i = 0 ; i < count-1 ; i++) {
611                 dx=p[i+1].x-p[i].x;
612                 dx*=100;
613                 dy=p[i+1].y-p[i].y;
614                 dy*=100;
615                 l=(int)sqrt((float)(dx*dx+dy*dy));
616                 if (l > tl) {
617                         x=p[i].x;
618                         y=p[i].y;
619                         if (dx < 0) {
620                                 dx=-dx;
621                                 dy=-dy;
622                                 x=p[i+1].x;
623                                 y=p[i+1].y;
624                         }
625                         x+=(l-tl)*dx/l/200;
626                         y+=(l-tl)*dy/l/200;
627                         x-=dy*45/l/10;
628                         y+=dx*45/l/10;
629                         p_t.x=x;
630                         p_t.y=y;
631         #if 0
632                         printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
633         #endif
634                         gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
635                 }
636         }
637 }
638
639 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
640 {
641         struct point pnt[3];
642         pnt[0]=pnt[1]=pnt[2]=*p;
643         pnt[0].x+=-dx*l/65536+dy*l/65536;
644         pnt[0].y+=-dy*l/65536-dx*l/65536;
645         pnt[2].x+=-dx*l/65536-dy*l/65536;
646         pnt[2].y+=-dy*l/65536+dx*l/65536;
647         gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
648 }
649
650 static void display_draw_arrows(struct displayitem *di, struct graphics_gc *gc, struct graphics *gra)
651 {
652         int i,dx,dy,l;
653         struct point p;
654         for (i = 0 ; i < di->count-1 ; i++) {
655                 dx=di->pnt[i+1].x-di->pnt[i].x; 
656                 dy=di->pnt[i+1].y-di->pnt[i].y;
657                 l=sqrt(dx*dx+dy*dy);
658                 if (l) {
659                         dx=dx*65536/l;
660                         dy=dy*65536/l;
661                         p=di->pnt[i];
662                         p.x+=dx*15/65536;
663                         p.y+=dy*15/65536;
664                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
665                         p=di->pnt[i+1];
666                         p.x-=dx*15/65536;
667                         p.y-=dy*15/65536;
668                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
669                 }
670         }
671 }
672
673 /**
674  * FIXME
675  * @param <>
676  * @returns <>
677  * @author Martin Schaller (04/2008)
678 */
679 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
680 {
681         struct element *e;
682         GList *l,*ls,*es,*types;
683         enum item_type type;
684         struct graphics_gc *gc = NULL;
685         struct graphics_image *img;
686         struct point p;
687         char path[PATH_MAX];
688
689         es=itm->elements;
690         while (es) {
691                 e=es->data;
692                 types=itm->type;
693                 while (types) {
694                         type=GPOINTER_TO_INT(types->data);
695                         ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
696                         l=ls;
697                         if (gc)
698                                 graphics_gc_destroy(gc);
699                         gc=NULL;
700                         img=NULL;
701                         while (l) {
702                                 struct displayitem *di;
703                                 di=l->data;
704                                 di->displayed=1;
705                                 if (! gc) {
706                                         gc=graphics_gc_new(gra);
707                                         gc->meth.gc_set_foreground(gc->priv, &e->color);
708                                 }
709                                 switch (e->type) {
710                                 case element_polygon:
711                                         gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
712                                         break;
713                                 case element_polyline:
714                                         if (e->u.polyline.width > 1) 
715                                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
716                                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
717                                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
718                                                                        e->u.polyline.offset,
719                                                                        e->u.polyline.dash_table,
720                                                                        e->u.polyline.dash_num);
721                                         gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
722                                         break;
723                                 case element_circle:
724                                         if (e->u.circle.width > 1) 
725                                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
726                                         gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
727                                         if (di->label && e->text_size) {
728                                                 p.x=di->pnt[0].x+3;
729                                                 p.y=di->pnt[0].y+10;
730                                                 if (! gra->font[e->text_size])
731                                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
732                                                 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);
733                                         }
734                                         break;
735                                 case element_text:
736                                         if (di->label) {
737                                                 if (! gra->font[e->text_size])
738                                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
739                                                 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], di->pnt, di->count, di->label);
740                                         }
741                                         break;
742                                 case element_icon:
743                                         if (!img) {
744                                                 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
745                                                 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
746                                                 if (! img)
747                                                         dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
748                                         }
749                                         if (img) {
750                                                 p.x=di->pnt[0].x - img->hot.x;
751                                                 p.y=di->pnt[0].y - img->hot.y;
752                                                 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
753                                                 graphics_image_free(gra, img);
754                                                 img = NULL;
755                                         }
756                                         break;
757                                 case element_image:
758                                         dbg(1,"image: '%s'\n", di->label);
759                                         if (gra->meth.draw_image_warp)
760                                                 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
761                                         else
762                                                 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
763                                         break;
764                                 case element_arrows:
765                                         display_draw_arrows(di,gc,gra);
766                                         break;
767                                 default:
768                                         printf("Unhandled element type %d\n", e->type);
769                                 
770                                 }
771                                 l=g_list_next(l);
772                         }
773                         types=g_list_next(types);
774                 }
775                 es=g_list_next(es);
776         }
777         if (gc)
778                 graphics_gc_destroy(gc);
779 }
780
781 void
782 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
783 {
784         GList *es;
785         struct point p;
786         struct coord c;
787         char *label=NULL;
788         struct graphics_gc *gc = NULL;
789         struct graphics_image *img;
790         char path[PATH_MAX];
791         es=itm->elements;
792         c.x=0;
793         c.y=0;
794         while (es) {
795                 struct element *e=es->data;
796                 int count=e->coord_count;
797                 struct point pnt[count+1];
798                 if (count)
799                         transform(t, projection_screen, e->coord, pnt, count, 0);
800                 else {
801                         transform(t, projection_screen, &c, pnt, 1, 0);
802                         count=1;
803                 }
804                 gc=graphics_gc_new(gra);
805                 gc->meth.gc_set_foreground(gc->priv, &e->color);
806                 switch (e->type) {
807                 case element_polyline:
808                         if (e->u.polyline.width > 1) 
809                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
810                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
811                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
812                                                        e->u.polyline.offset,
813                                                        e->u.polyline.dash_table,
814                                                        e->u.polyline.dash_num);
815                         gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
816                         break;
817                 case element_circle:
818                         if (e->u.circle.width > 1) 
819                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
820                         gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
821                         if (label && e->text_size) {
822                                 p.x=pnt[0].x+3;
823                                 p.y=pnt[0].y+10;
824                         if (! gra->font[e->text_size])
825                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
826                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
827                         }
828                         break;
829                 case element_icon:
830                         sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
831                         img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
832                         if (! img)
833                                 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
834                         else {
835                                 p.x=pnt[0].x - img->hot.x;
836                                 p.y=pnt[0].y - img->hot.y;
837                                 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
838                                 graphics_image_free(gra, img);
839                         }
840                         break;
841                 default:
842                         dbg(0,"dont know how to draw %d\n", e->type);
843                 }
844                 graphics_gc_destroy(gc);
845                 es=g_list_next(es);
846         }
847 }
848
849 /**
850  * FIXME
851  * @param <>
852  * @returns <>
853  * @author Martin Schaller (04/2008)
854 */
855 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
856 {
857         GList *itms;
858         struct itemgra *itm;
859
860         itms=lay->itemgras;
861         while (itms) {
862                 itm=itms->data;
863                 if (order >= itm->order.min && order <= itm->order.max) 
864                         xdisplay_draw_elements(gra, display_list, itm);
865                 itms=g_list_next(itms);
866         }
867 }
868
869 /**
870  * FIXME
871  * @param <>
872  * @returns <>
873  * @author Martin Schaller (04/2008)
874 */
875 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
876 {
877         GList *lays;
878         struct layer *lay;
879         
880         lays=l->layers;
881         while (lays) {
882                 lay=lays->data;
883                 xdisplay_draw_layer(display_list, gra, lay, order);
884                 lays=g_list_next(lays);
885         }
886 }
887
888 /**
889  * FIXME
890  * @param <>
891  * @returns <>
892  * @author Martin Schaller (04/2008)
893 */
894 extern void *route_selection;
895
896 /**
897  * FIXME
898  * @param <>
899  * @returns <>
900  * @author Martin Schaller (04/2008)
901 */
902 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
903 {
904         enum projection pro;
905         struct map_rect *mr;
906         struct item *item;
907         int conv,count,max=16384;
908         struct point pnt[max];
909         struct coord ca[max];
910         struct attr attr;
911         struct map_selection *sel;
912
913         pro=map_projection(m);
914         conv=map_requires_conversion(m);
915         sel=transform_get_selection(t, pro, order);
916         if (route_selection)
917                 mr=map_rect_new(m, route_selection);
918         else
919                 mr=map_rect_new(m, sel);
920         if (! mr) {
921                 map_selection_destroy(sel);
922                 return;
923         }
924         while ((item=map_rect_get_item(mr))) {
925                 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
926                 if (item->type >= type_line && count < 2) {
927                         dbg(1,"poly from map has only %d points\n", count);
928                         continue;
929                 }
930                 if (item->type < type_line) {
931                         if (! map_selection_contains_point(sel, &ca[0])) {
932                                 dbg(1,"point not visible\n");
933                                 continue;
934                         }
935                 } else if (item->type < type_area) {
936                         if (! map_selection_contains_polyline(sel, ca, count)) {
937                                 dbg(1,"polyline not visible\n");
938                                 continue;
939                         }
940                 } else {
941                         if (! map_selection_contains_polygon(sel, ca, count)) {
942                                 dbg(1,"polygon not visible\n");
943                                 continue;
944                         }
945                 }
946                 if (count == max) 
947                         dbg(0,"point count overflow\n", count);
948                 count=transform(t, pro, ca, pnt, count, 1);
949                 if (item->type >= type_line && count < 2) {
950                         dbg(1,"poly from transform has only %d points\n", count);
951                         continue;
952                 }
953                 if (!item_attr_get(item, attr_label, &attr))
954                         attr.u.str=NULL;
955                 if (conv && attr.u.str && attr.u.str[0]) {
956                         char *str=map_convert_string(m, attr.u.str);
957                         display_add(displaylist, item, count, pnt, str);
958                         map_convert_free(str);
959                 } else
960                         display_add(displaylist, item, count, pnt, attr.u.str);
961         }
962         map_rect_destroy(mr);
963         map_selection_destroy(sel);
964 }
965
966 /**
967  * FIXME
968  * @param <>
969  * @returns <>
970  * @author Martin Schaller (04/2008)
971 */
972 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
973 {
974         struct mapset *ms;
975         struct map *m;
976         struct mapset_handle *h;
977
978         if (! mapsets)
979                 return;
980         ms=mapsets->data;
981         h=mapset_open(ms);
982         while ((m=mapset_next(h, 1))) {
983                 do_draw_map(displaylist, t, m, order);
984         }
985         mapset_close(h);
986 }
987
988 /**
989  * FIXME
990  * @param <>
991  * @returns <>
992  * @author Martin Schaller (04/2008)
993 */
994 int graphics_ready(struct graphics *this_)
995 {
996         return this_->ready;
997 }
998
999 /**
1000  * FIXME
1001  * @param <>
1002  * @returns <>
1003  * @author Martin Schaller (04/2008)
1004 */
1005 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
1006 {
1007         int order=transform_get_order(trans);
1008         struct point p;
1009         p.x=0;
1010         p.y=0;
1011         // FIXME find a better place to set the background color
1012         graphics_gc_set_background(gra->gc[0], &l->color);
1013         graphics_gc_set_foreground(gra->gc[0], &l->color);
1014         gra->default_font = g_strdup(l->font);
1015         graphics_background_gc(gra, gra->gc[0]);
1016         gra->meth.draw_mode(gra->priv, draw_mode_begin);
1017         gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
1018         xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
1019         if (callback)
1020                 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1021         gra->meth.draw_mode(gra->priv, draw_mode_end);
1022 }
1023
1024 /**
1025  * FIXME
1026  * @param <>
1027  * @returns <>
1028  * @author Martin Schaller (04/2008)
1029 */
1030 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
1031 {
1032         struct displaylist_handle *dlh;
1033         struct displayitem *di;
1034         int i;
1035
1036         dlh=graphics_displaylist_open(displaylist);
1037         while ((di=graphics_displaylist_next(dlh))) {
1038                 for (i = 0 ; i < di->count ; i++) {
1039                         di->pnt[i].x+=dx;
1040                         di->pnt[i].y+=dy;
1041                 }
1042         }
1043         graphics_displaylist_close(dlh);
1044 }
1045
1046 /**
1047  * FIXME
1048  * @param <>
1049  * @returns <>
1050  * @author Martin Schaller (04/2008)
1051 */
1052 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
1053 {
1054         int order=transform_get_order(trans);
1055
1056         dbg(1,"enter");
1057
1058 #if 0
1059         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));
1060 #endif
1061         
1062         xdisplay_free(displaylist->dl);
1063         dbg(1,"order=%d\n", order);
1064
1065
1066 #if 0
1067         for (i = 0 ; i < data_window_type_end; i++) {
1068                 data_window_begin(co->data_window[i]);  
1069         }
1070 #endif
1071         profile(0,NULL);
1072         order+=l->order_delta;
1073         do_draw(displaylist, trans, mapsets, order);
1074 //      profile(1,"do_draw");
1075         graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1076         profile(1,"xdisplay_draw");
1077         profile(0,"end");
1078   
1079 #if 0
1080         for (i = 0 ; i < data_window_type_end; i++) {
1081                 data_window_end(co->data_window[i]);    
1082         }
1083 #endif
1084         gra->ready=1;
1085 }
1086
1087 /**
1088  * FIXME
1089  * @param <>
1090  * @returns <>
1091  * @author Martin Schaller (04/2008)
1092 */
1093 struct displaylist_handle {
1094         GList *hl_head,*hl,*l;
1095 };
1096
1097 /**
1098  * FIXME
1099  * @param <>
1100  * @returns <>
1101  * @author Martin Schaller (04/2008)
1102 */
1103 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1104 {
1105         struct displaylist_handle *ret;
1106
1107         ret=g_new0(struct displaylist_handle, 1);
1108         ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1109
1110         return ret;
1111 }
1112
1113 /**
1114  * FIXME
1115  * @param <>
1116  * @returns <>
1117  * @author Martin Schaller (04/2008)
1118 */
1119 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1120 {
1121         struct displayitem *ret;
1122         if (! dlh->l) {
1123                 if (!dlh->hl)
1124                         return NULL;
1125                 dlh->l=dlh->hl->data;
1126                 dlh->hl=g_list_next(dlh->hl);
1127         }
1128         ret=dlh->l->data;
1129         dlh->l=g_list_next(dlh->l);
1130         return ret;
1131 }
1132
1133 /**
1134  * FIXME
1135  * @param <>
1136  * @returns <>
1137  * @author Martin Schaller (04/2008)
1138 */
1139 void graphics_displaylist_close(struct displaylist_handle *dlh)
1140 {
1141         g_list_free(dlh->hl_head);
1142         g_free(dlh);
1143 }
1144
1145 /**
1146  * FIXME
1147  * @param <>
1148  * @returns <>
1149  * @author Martin Schaller (04/2008)
1150 */
1151 struct displaylist * graphics_displaylist_new(void)
1152 {
1153         struct displaylist *ret=g_new(struct displaylist, 1);
1154
1155         ret->dl=g_hash_table_new(NULL,NULL);
1156
1157         return ret;
1158 }
1159
1160 /**
1161  * FIXME
1162  * @param <>
1163  * @returns <>
1164  * @author Martin Schaller (04/2008)
1165 */
1166 struct item * graphics_displayitem_get_item(struct displayitem *di)
1167 {
1168         return &di->item;       
1169 }
1170
1171 /**
1172  * FIXME
1173  * @param <>
1174  * @returns <>
1175  * @author Martin Schaller (04/2008)
1176 */
1177 char * graphics_displayitem_get_label(struct displayitem *di)
1178 {
1179         return di->label;
1180 }
1181
1182 /**
1183  * FIXME
1184  * @param <>
1185  * @returns <>
1186  * @author Martin Schaller (04/2008)
1187 */
1188 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1189 {
1190         if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1191                 return 0;
1192         if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1193                 return 0;
1194         if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1195                 return 1;
1196         }
1197         return 0;
1198 }
1199
1200 /**
1201  * FIXME
1202  * @param <>
1203  * @returns <>
1204  * @author Martin Schaller (04/2008)
1205 */
1206 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1207 {
1208         int vx,vy,wx,wy;
1209         int c1,c2;
1210         struct point line_p;
1211
1212         vx=line_p1->x-line_p0->x;
1213         vy=line_p1->y-line_p0->y;
1214         wx=p->x-line_p0->x;
1215         wy=p->y-line_p0->y;
1216
1217         c1=vx*wx+vy*wy;
1218         if ( c1 <= 0 )
1219                 return within_dist_point(p, line_p0, dist);
1220         c2=vx*vx+vy*vy;
1221         if ( c2 <= c1 )
1222                 return within_dist_point(p, line_p1, dist);
1223
1224         line_p.x=line_p0->x+vx*c1/c2;
1225         line_p.y=line_p0->y+vy*c1/c2;
1226         return within_dist_point(p, &line_p, dist);
1227 }
1228
1229 /**
1230  * FIXME
1231  * @param <>
1232  * @returns <>
1233  * @author Martin Schaller (04/2008)
1234 */
1235 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1236 {
1237         int i;
1238         for (i = 0 ; i < count-1 ; i++) {
1239                 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1240                         return 1;
1241                 }
1242         }
1243         if (close)
1244                 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1245         return 0;
1246 }
1247
1248 /**
1249  * FIXME
1250  * @param <>
1251  * @returns <>
1252  * @author Martin Schaller (04/2008)
1253 */
1254 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1255 {
1256         int i, j, c = 0;
1257         for (i = 0, j = count-1; i < count; j = i++) {
1258                 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1259                 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1260                 (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)) 
1261                         c = !c;
1262         }
1263         if (! c)
1264                 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1265         return c;
1266 }
1267
1268 /**
1269  * FIXME
1270  * @param <>
1271  * @returns <>
1272  * @author Martin Schaller (04/2008)
1273 */
1274 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1275 {
1276         if (di->item.type < type_line) {
1277                 return within_dist_point(p, &di->pnt[0], dist);
1278         }
1279         if (di->item.type < type_area) {
1280                 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
1281         }
1282         return within_dist_polygon(p, di->pnt, di->count, dist);
1283 }