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