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