2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
20 //##############################################################################################################
25 //# Authors: Martin Schaller (04/2008)
27 //##############################################################################################################
36 #include "draw_info.h"
39 #include "projection.h"
43 #include "transform.h"
55 //##############################################################################################################
58 //# Authors: Martin Schaller (04/2008)
59 //##############################################################################################################
62 struct graphics* parent;
63 struct graphics_priv *priv;
64 struct graphics_methods meth;
67 struct graphics_font **font;
68 struct graphics_gc *gc[3];
70 struct callback_list *cbl;
72 int gamma,brightness,contrast;
78 * Counter for z_order of displayitems;
81 GHashTable *image_cache_hash;
84 struct display_context
88 struct graphics_gc *gc;
89 struct graphics_gc *gc_background;
90 struct graphics_image *img;
93 struct transformation *trans;
98 #define HASH_SIZE 1024
102 struct displayitem *di;
110 struct layout *layout, *layout_hashed;
111 struct display_context dc;
112 int order, order_hashed, max_offset;
114 struct mapset_handle *msh;
117 struct map_selection *sel;
119 struct callback *idle_cb;
120 struct event_idle *idle_ev;
122 struct hash_entry hash_entries[HASH_SIZE];
126 struct displaylist_icon_cache {
131 static void draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir);
132 static void graphics_process_selection(struct graphics *gra, struct displaylist *dl);
133 static void graphics_gc_init(struct graphics *this_);
136 clear_hash(struct displaylist *dl)
139 for (i = 0 ; i < HASH_SIZE ; i++)
140 dl->hash_entries[i].type=type_none;
143 static struct hash_entry *
144 get_hash_entry(struct displaylist *dl, enum item_type type)
146 int hashidx=(type*2654435761UL) & (HASH_SIZE-1);
147 int offset=dl->max_offset;
149 if (!dl->hash_entries[hashidx].type)
151 if (dl->hash_entries[hashidx].type == type)
152 return &dl->hash_entries[hashidx];
153 hashidx=(hashidx+1)&(HASH_SIZE-1);
154 } while (offset-- > 0);
158 static struct hash_entry *
159 set_hash_entry(struct displaylist *dl, enum item_type type)
161 int hashidx=(type*2654435761UL) & (HASH_SIZE-1);
164 if (!dl->hash_entries[hashidx].type) {
165 dl->hash_entries[hashidx].type=type;
166 if (dl->max_offset < offset)
167 dl->max_offset=offset;
168 return &dl->hash_entries[hashidx];
170 if (dl->hash_entries[hashidx].type == type)
171 return &dl->hash_entries[hashidx];
172 hashidx=(hashidx+1)&(HASH_SIZE-1);
179 graphics_set_attr_do(struct graphics *gra, struct attr *attr)
181 switch (attr->type) {
183 gra->gamma=attr->u.num;
185 case attr_brightness:
186 gra->brightness=attr->u.num;
189 gra->contrast=attr->u.num;
192 gra->font_size=attr->u.num;
197 gra->colormgmt=(gra->gamma != 65536 || gra->brightness != 0 || gra->contrast != 65536);
198 graphics_gc_init(gra);
203 graphics_set_attr(struct graphics *gra, struct attr *attr)
207 if (gra->meth.set_attr)
208 ret=gra->meth.set_attr(gra->priv, attr);
210 ret=graphics_set_attr_do(gra, attr);
215 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
221 * Creates a new graphics object
225 * @author Martin Schaller (04/2008)
227 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
229 struct graphics *this_;
230 struct attr *type_attr;
231 struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl);
233 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
237 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
238 if (! graphicstype_new)
240 this_=g_new0(struct graphics, 1);
241 this_->cbl=callback_list_new();
242 this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
243 this_->attrs=attr_list_dup(attrs);
245 this_->contrast=65536;
248 this_->image_cache_hash = g_hash_table_new_full(g_str_hash, g_str_equal,g_free,g_free);
250 graphics_set_attr_do(this_,*attrs);
260 * @author Martin Schaller (04/2008)
262 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
264 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
271 * @author Martin Schaller (04/2008)
273 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha, int wraparound)
275 struct graphics *this_;
276 struct point_rect pr;
277 if (!parent->meth.overlay_new)
279 this_=g_new0(struct graphics, 1);
280 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha, wraparound);
281 this_->image_cache_hash = parent->image_cache_hash;
282 this_->parent = parent;
288 graphics_set_rect(this_, &pr);
297 * @brief Alters the size, position, alpha and wraparound for an overlay
299 * @param this_ The overlay's graphics struct
300 * @param p The new position of the overlay
301 * @param w The new width of the overlay
302 * @param h The new height of the overlay
303 * @param alpha The new alpha of the overlay
304 * @param wraparound The new wraparound of the overlay
307 graphics_overlay_resize(struct graphics *this_, struct point *p, int w, int h, int alpha, int wraparound)
309 if (! this_->meth.overlay_resize) {
313 this_->meth.overlay_resize(this_->priv, p, w, h, alpha, wraparound);
317 graphics_gc_init(struct graphics *this_)
319 struct color background={ COLOR_BACKGROUND_ };
320 struct color black={ COLOR_BLACK_ };
321 struct color white={ COLOR_WHITE_ };
322 if (!this_->gc[0] || !this_->gc[1] || !this_->gc[2])
324 graphics_gc_set_background(this_->gc[0], &background );
325 graphics_gc_set_foreground(this_->gc[0], &background );
326 graphics_gc_set_background(this_->gc[1], &black );
327 graphics_gc_set_foreground(this_->gc[1], &white );
328 graphics_gc_set_background(this_->gc[2], &white );
329 graphics_gc_set_foreground(this_->gc[2], &black );
338 * @author Martin Schaller (04/2008)
340 void graphics_init(struct graphics *this_)
344 this_->gc[0]=graphics_gc_new(this_);
345 this_->gc[1]=graphics_gc_new(this_);
346 this_->gc[2]=graphics_gc_new(this_);
347 graphics_gc_init(this_);
348 graphics_background_gc(this_, this_->gc[0]);
355 * @author Martin Schaller (04/2008)
357 void * graphics_get_data(struct graphics *this_, const char *type)
359 return (this_->meth.get_data(this_->priv, type));
362 void graphics_add_callback(struct graphics *this_, struct callback *cb)
364 callback_list_add(this_->cbl, cb);
367 void graphics_remove_callback(struct graphics *this_, struct callback *cb)
369 callback_list_remove(this_->cbl, cb);
376 * @author Martin Schaller (04/2008)
378 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
380 struct graphics_font *this_;
382 this_=g_new0(struct graphics_font,1);
383 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
387 struct graphics_font * graphics_named_font_new(struct graphics *gra, char *font, int size, int flags)
389 struct graphics_font *this_;
391 this_=g_new0(struct graphics_font,1);
392 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, font, size, flags);
399 * Called when navit exits
400 * @param gra The graphics instance
402 * @author David Tegze (02/2011)
404 void graphics_free(struct graphics *gra)
409 /* If it's not an overlay, free the image cache. */
411 struct graphics_image *img;
414 /* We can't specify context (pointer to struct graphics) for g_hash_table_new to have it passed to free function
415 so we have to free img->priv manually, the rest would be freed by g_hash_table_destroy. GHashTableIter isn't used because it
416 broke n800 build at r5107.
418 for(ll=l=g_hash_to_list(gra->image_cache_hash);l;l=g_list_next(l)) {
420 if (img && gra->meth.image_free)
421 gra->meth.image_free(gra->priv, img->priv);
424 g_hash_table_destroy(gra->image_cache_hash);
427 graphics_gc_destroy(gra->gc[0]);
428 graphics_gc_destroy(gra->gc[1]);
429 graphics_gc_destroy(gra->gc[2]);
430 g_free(gra->default_font);
431 graphics_font_destroy_all(gra);
432 gra->meth.graphics_destroy(gra->priv);
437 * Free all loaded fonts.
438 * Used when switching layouts.
439 * @param gra The graphics instance
441 * @author Sarah Nordstrom (05/2008)
443 void graphics_font_destroy_all(struct graphics *gra)
446 for(i = 0 ; i < gra->font_len; i++) {
447 if(!gra->font[i]) continue;
448 gra->font[i]->meth.font_destroy(gra->font[i]->priv);
457 * @author Martin Schaller (04/2008)
459 struct graphics_gc * graphics_gc_new(struct graphics *gra)
461 struct graphics_gc *this_;
463 this_=g_new0(struct graphics_gc,1);
464 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
473 * @author Martin Schaller (04/2008)
475 void graphics_gc_destroy(struct graphics_gc *gc)
479 gc->meth.gc_destroy(gc->priv);
484 graphics_convert_color(struct graphics *gra, struct color *in, struct color *out)
487 if (gra->brightness) {
488 out->r+=gra->brightness;
489 out->g+=gra->brightness;
490 out->b+=gra->brightness;
492 if (gra->contrast != 65536) {
493 out->r=out->r*gra->contrast/65536;
494 out->g=out->g*gra->contrast/65536;
495 out->b=out->b*gra->contrast/65536;
509 if (gra->gamma != 65536) {
510 out->r=pow(out->r/65535.0,gra->gamma/65536.0)*65535.0;
511 out->g=pow(out->g/65535.0,gra->gamma/65536.0)*65535.0;
512 out->b=pow(out->b/65535.0,gra->gamma/65536.0)*65535.0;
520 * @author Martin Schaller (04/2008)
522 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
525 if (gc->gra->colormgmt) {
526 graphics_convert_color(gc->gra, c, &cn);
529 gc->meth.gc_set_foreground(gc->priv, c);
536 * @author Martin Schaller (04/2008)
538 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
541 if (gc->gra->colormgmt) {
542 graphics_convert_color(gc->gra, c, &cn);
545 gc->meth.gc_set_background(gc->priv, c);
553 * @author Martin Schaller (04/2008)
555 void graphics_gc_set_stipple(struct graphics_gc *gc, struct graphics_image *img)
557 gc->meth.gc_set_stipple(gc->priv, img ? img->priv : NULL);
565 * @author Martin Schaller (04/2008)
567 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
569 gc->meth.gc_set_linewidth(gc->priv, width);
576 * @author Martin Schaller (04/2008)
578 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
580 if (gc->meth.gc_set_dashes)
581 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
585 * Create a new image from file path scaled to w and h pixels
586 * @param gra the graphics instance
587 * @param path path of the image to load
588 * @param w width to rescale to
589 * @param h height to rescale to
591 * @author Martin Schaller (04/2008)
593 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
595 return graphics_image_new_scaled_rotated(gra, path, w, h, 0);
599 * Create a new image from file path scaled to w and h pixels and possibly rotated
600 * @param gra the graphics instance
601 * @param path path of the image to load
602 * @param w width to rescale to
603 * @param h height to rescale to
604 * @param rotate angle to rotate the image. Warning, graphics might only support 90 degree steps here
606 * @author Martin Schaller (04/2008)
608 struct graphics_image * graphics_image_new_scaled_rotated(struct graphics *gra, char *path, int w, int h, int rotate)
610 struct graphics_image *this_;
611 char* hash_key = g_strdup_printf("%s*%d*%d*%d",path,w,h,rotate);
613 if ( g_hash_table_lookup_extended( gra->image_cache_hash, hash_key, NULL, (gpointer)&this_) ) {
615 dbg(3,"Found cached image%sfor '%s'\n",this_?" ":" miss ",path);
619 this_=g_new0(struct graphics_image,1);
625 char *s, *name, *new_name;
626 int len=strlen(path);
628 int newwidth=-1, newheight=-1;
630 ext=g_utf8_strrchr(path,-1,'.');
633 /* Dont allow too long or too short file name extensions*/
634 if(ext && ((i>5) || (i<1)))
637 /* Search for _w_h name part, begin from char before extension if it exists */
644 while(s>path && g_ascii_isdigit(*s)) {
647 newheight+=(*s-'0')*k;
652 if(k>1 && s>path && *s=='_') {
655 while(s>path && g_ascii_isdigit(*s)) {
658 newwidth+=(*s-'0')*k;;
664 if(k==1 || s<=path || *s!='_') {
674 /* If exact h and w values were given as function parameters, they take precedence over values guessed from the image name */
680 name=g_strndup(path,s-path);
681 for (i = 1 ; i < 6 ; i++) {
685 /* The best variant both for cpu usage and quality would be prescaled png of a needed size */
686 if (newwidth != -1 && newheight != -1) {
687 new_name=g_strdup_printf("%s_%d_%d.png", name, newwidth, newheight);
691 /* Try to load image by the exact name given by user. For example, if she wants to
692 scale some prescaled png variant to a new size given as function params, or have
693 default png image to be displayed unscaled. */
694 new_name=g_strdup(path);
697 /* Next, try uncompressed and compressed svgs as they should give best quality but
698 rendering might take more cpu resources when the image is displayed for the first time */
699 new_name=g_strdup_printf("%s.svg", name);
702 new_name=g_strdup_printf("%s.svgz", name);
705 /* Scaling the default png to the needed size may give some quality loss */
706 new_name=g_strdup_printf("%s.png", name);
709 /* xpm format is used as a last resort, because its not widely supported and we are moving to svg and png formats */
710 new_name=g_strdup_printf("%s.xpm", name);
716 this_->width=newwidth;
717 this_->height=newheight;
718 dbg(2,"Trying to load image '%s' for '%s' at %dx%d\n", new_name, path, newwidth, newheight);
719 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, new_name, &this_->width, &this_->height, &this_->hot, rotate);
721 dbg(1,"Using image '%s' for '%s' at %dx%d\n", new_name, path, newwidth, newheight);
730 dbg(0,"No image for '%s'\n", path);
735 g_hash_table_insert(gra->image_cache_hash, hash_key, (gpointer)this_ );
741 * Create a new image from file path
742 * @param gra the graphics instance
743 * @param path path of the image to load
745 * @author Martin Schaller (04/2008)
747 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
749 return graphics_image_new_scaled_rotated(gra, path, -1, -1, 0);
756 * @author Martin Schaller (04/2008)
758 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
760 /* Image is cached inside gra->image_cache_hash. So it would be freed only when graphics is destroyed => Do nothing here. */
767 * @author Martin Schaller (04/2008)
769 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
771 this_->meth.draw_restore(this_->priv, p, w, h);
778 * @author Martin Schaller (04/2008)
780 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
782 this_->meth.draw_mode(this_->priv, mode);
789 * @author Martin Schaller (04/2008)
791 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
793 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
800 * @author Martin Schaller (04/2008)
802 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
804 struct point *pnt=g_alloca(sizeof(struct point)*(r*4+64));
807 if(this_->meth.draw_circle)
808 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
811 draw_circle(p, r, 0, -1, 1026, pnt, &i, 1);
814 this_->meth.draw_lines(this_->priv, gc->priv, pnt, i);
822 * @author Martin Schaller (04/2008)
824 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
826 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
829 void graphics_draw_rectangle_rounded(struct graphics *this_, struct graphics_gc *gc, struct point *plu, int w, int h, int r, int fill)
831 struct point *p=g_alloca(sizeof(struct point)*(r*4+32));
832 struct point pi0={plu->x+r,plu->y+r};
833 struct point pi1={plu->x+w-r,plu->y+r};
834 struct point pi2={plu->x+w-r,plu->y+h-r};
835 struct point pi3={plu->x+r,plu->y+h-r};
838 draw_circle(&pi2, r*2, 0, -1, 258, p, &i, 1);
839 draw_circle(&pi1, r*2, 0, 255, 258, p, &i, 1);
840 draw_circle(&pi0, r*2, 0, 511, 258, p, &i, 1);
841 draw_circle(&pi3, r*2, 0, 767, 258, p, &i, 1);
845 this_->meth.draw_polygon(this_->priv, gc->priv, p, i);
847 this_->meth.draw_lines(this_->priv, gc->priv, p, i);
855 * @author Martin Schaller (04/2008)
857 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)
859 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
867 * @author Martin Schaller (04/2008)
869 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret, int estimate)
871 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret, estimate);
878 * @author Martin Schaller (04/2008)
880 void graphics_overlay_disable(struct graphics *this_, int disable)
882 this_->disabled = disable;
883 if (this_->meth.overlay_disable)
884 this_->meth.overlay_disable(this_->priv, disable);
887 int graphics_is_disabled(struct graphics *this_)
889 return this_->disabled || (this_->parent && this_->parent->disabled);
896 * @author Martin Schaller (04/2008)
898 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
900 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
904 //##############################################################################################################
907 //# Authors: Martin Schaller (04/2008)
908 //##############################################################################################################
910 graphics_draw_drag(struct graphics *this_, struct point *p)
912 if (!this_->meth.draw_drag)
914 this_->meth.draw_drag(this_->priv, p);
919 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
921 this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
930 //##############################################################################################################
933 //# Authors: Martin Schaller (04/2008)
934 //##############################################################################################################
935 static void popup_view_html(struct popup_item *item, char *file)
938 sprintf(command,"firefox %s", file);
942 struct transformatin *tg;
945 //##############################################################################################################
948 //# Authors: Martin Schaller (04/2008)
949 //##############################################################################################################
950 static void graphics_popup(struct display_list *list, struct popup_item **popup)
956 struct popup_item *curr_item,*last=NULL;
958 mr=map_rect_new(item->map, NULL, NULL, 0);
959 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
960 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
962 if (item_attr_get(item, attr_name, &attr)) {
963 curr_item=popup_item_new_text(popup,attr.u.str,1);
964 if (item_attr_get(item, attr_info_html, &attr)) {
965 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
967 if (item_attr_get(item, attr_price_html, &attr)) {
968 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
970 curr_item->submenu=last;
973 map_rect_destroy(mr);
982 * @author Martin Schaller (04/2008)
985 struct displayitem *next;
997 * @author Martin Schaller (04/2008)
999 static void xdisplay_free(struct displaylist *dl)
1002 for (i = 0 ; i < HASH_SIZE ; i++) {
1003 struct displayitem *di=dl->hash_entries[i].di;
1005 struct displayitem *next=di->next;
1009 dl->hash_entries[i].di=NULL;
1017 * @author Martin Schaller (04/2008)
1019 static void display_add(struct hash_entry *entry, struct item *item, int count, struct coord *c, char **label, int label_count)
1021 struct displayitem *di;
1025 len=sizeof(*di)+count*sizeof(*c);
1026 if (label && label_count) {
1027 for (i = 0 ; i < label_count ; i++) {
1029 len+=strlen(label[i])+1;
1036 di=(struct displayitem *)p;
1037 p+=sizeof(*di)+count*sizeof(*c);
1040 if (label && label_count) {
1042 for (i = 0 ; i < label_count ; i++) {
1044 strcpy(p, label[i]);
1045 p+=strlen(label[i])+1;
1052 memcpy(di->c, c, count*sizeof(*c));
1062 * @author Martin Schaller (04/2008)
1064 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)
1066 int i,x,y,tl,tlm,th,thm,tlsq,l;
1072 if (gra->meth.get_text_bbox) {
1073 gra->meth.get_text_bbox(gra->priv, font->priv, label, 0x10000, 0x0, pb, 1);
1074 tl=(pb[2].x-pb[0].x);
1075 th=(pb[0].y-pb[1].y);
1083 for (i = 0 ; i < count-1 ; i++) {
1106 dbg(0,"display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
1108 if (x < gra->r.rl.x && x + tl > gra->r.lu.x && y + tl > gra->r.lu.y && y - tl < gra->r.rl.y)
1109 gra->meth.draw_text(gra->priv, fg->priv, bg?bg->priv:NULL, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
1114 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
1116 struct point pnt[3];
1117 pnt[0]=pnt[1]=pnt[2]=*p;
1118 pnt[0].x+=-dx*l/65536+dy*l/65536;
1119 pnt[0].y+=-dy*l/65536-dx*l/65536;
1120 pnt[2].x+=-dx*l/65536-dy*l/65536;
1121 pnt[2].y+=-dy*l/65536+dx*l/65536;
1122 gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
1125 static void display_draw_arrows(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count)
1129 for (i = 0 ; i < count-1 ; i++) {
1130 dx=pnt[i+1].x-pnt[i].x;
1131 dy=pnt[i+1].y-pnt[i].y;
1132 l=sqrt(dx*dx+dy*dy);
1139 display_draw_arrow(&p, dx, dy, 10, gc, gra);
1143 display_draw_arrow(&p, dx, dy, 10, gc, gra);
1149 intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, int bdy,
1153 n = bdy * adx - bdx * ady;
1154 a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
1155 b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
1169 res->x = a1->x + a * adx / n;
1170 res->y = a1->y + a * ady / n;
1244 draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir)
1249 dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
1257 else if (diameter > 64)
1259 else if (diameter > 24)
1261 else if (diameter > 8)
1272 while (i < count && c[i].fowler <= start)
1274 while (i < count && c[i].fowler < end) {
1275 if (1< *pos || 0<dir) {
1276 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
1277 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
1286 while (start > 1024) {
1290 while (end < 1024) {
1292 while (i >= 0 && c[i].fowler >= start)
1294 while (i >= 0 && c[i].fowler > end) {
1295 if (1< *pos || 0<dir) {
1296 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
1297 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
1310 fowler(int dy, int dx)
1312 int adx, ady; /* Absolute Values of Dx and Dy */
1313 int code; /* Angular Region Classification Code */
1315 adx = (dx < 0) ? -dx : dx; /* Compute the absolute values. */
1316 ady = (dy < 0) ? -dy : dy;
1318 code = (adx < ady) ? 1 : 0;
1326 return (dx == 0) ? 0 : 128*ady / adx; /* [ 0, 45] */
1328 return (256 - (128*adx / ady)); /* ( 45, 90] */
1330 return (256 + (128*adx / ady)); /* ( 90,135) */
1332 return (512 - (128*ady / adx)); /* [135,180] */
1334 return (512 + (128*ady / adx)); /* (180,225] */
1336 return (768 - (128*adx / ady)); /* (225,270) */
1338 return (768 + (128*adx / ady)); /* [270,315) */
1340 return (1024 - (128*ady / adx));/* [315,360) */
1345 int_sqrt(unsigned int n)
1347 unsigned int h, p= 0, q= 1, r= n;
1349 /* avoid q rollover */
1350 if(n >= (1<<(sizeof(n)*8-2))) {
1351 q = 1<<(sizeof(n)*8-2);
1376 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
1399 graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count, int *width, int step)
1402 struct point *res=g_alloca(sizeof(struct point)*maxpoints);
1403 struct point pos, poso, neg, nego;
1404 int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
1405 struct offset o,oo={0,0,0,0};
1406 int fow=0, fowo=0, delta;
1407 int wi, ppos = maxpoints/2, npos = maxpoints/2;
1409 int max_circle_points=20;
1415 if (i < count - 1) {
1416 int dxs,dys,lscales;
1418 dx = (pnt[i + 1].x - pnt[i].x);
1419 dy = (pnt[i + 1].y - pnt[i].y);
1421 l = int_sqrt(dx * dx * lscale * lscale + dy * dy * lscale * lscale);
1425 lscales=lscale*lscale;
1426 if (dxs + dys > lscales)
1427 l = int_sqrt(dxs+dys)*lscale;
1429 l = int_sqrt((dxs+dys)*lscales);
1431 fow=fowler(-dy, dx);
1435 if (wi*lscale > 10000)
1437 dbg_assert(wi*lscale <= 10000);
1438 calc_offsets(wi*lscale, l, dx, dy, &o);
1439 pos.x = pnt[i].x + o.ny;
1440 pos.y = pnt[i].y + o.px;
1441 neg.x = pnt[i].x + o.py;
1442 neg.y = pnt[i].y + o.nx;
1445 else if (i == count-1)
1447 else if (npos < max_circle_points || ppos >= maxpoints-max_circle_points)
1454 poso.x = pnt[i].x + oo.ny;
1455 poso.y = pnt[i].y + oo.px;
1456 nego.x = pnt[i].x + oo.py;
1457 nego.y = pnt[i].y + oo.nx;
1462 if (intersection(&pos, dx, dy, &poso, dxo, dyo, &res[ppos]))
1466 draw_circle(&pnt[i], wi, prec, fowo-512, -delta, res, &npos, -1);
1470 draw_circle(&pnt[i], wi, prec, fowo, 1024-delta, res, &ppos, 1);
1472 if (intersection(&neg, dx, dy, &nego, dxo, dyo, &res[npos - 1]))
1481 draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
1484 dbg_assert(npos > 0);
1485 dbg_assert(ppos < maxpoints);
1486 gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
1493 draw_circle(&pnt[i], wi, prec, fow+512, 512, res, &ppos, 1);
1502 calc_offsets(wi*lscale, l, dx, dy, &oo);
1517 clipcode(struct wpoint *p, struct point_rect *r)
1533 clip_line(struct wpoint *p1, struct wpoint *p2, struct point_rect *r)
1535 int code1,code2,ret=1;
1537 code1=clipcode(p1, r);
1540 code2=clipcode(p2, r);
1546 while (code1 || code2) {
1550 p1->y+=(r->lu.x-p1->x)*dy/dx;
1551 p1->w+=(r->lu.x-p1->x)*dw/dx;
1553 } else if (code1 & 2) {
1554 p1->y+=(r->rl.x-p1->x)*dy/dx;
1555 p1->w+=(r->rl.x-p1->x)*dw/dx;
1557 } else if (code1 & 4) {
1558 p1->x+=(r->lu.y-p1->y)*dx/dy;
1559 p1->w+=(r->lu.y-p1->y)*dw/dy;
1561 } else if (code1 & 8) {
1562 p1->x+=(r->rl.y-p1->y)*dx/dy;
1563 p1->w+=(r->rl.y-p1->y)*dw/dy;
1566 code1=clipcode(p1, r);
1570 p2->y+=(r->lu.x-p2->x)*dy/dx;
1571 p2->w+=(r->lu.x-p2->x)*dw/dx;
1573 } else if (code2 & 2) {
1574 p2->y+=(r->rl.x-p2->x)*dy/dx;
1575 p2->w+=(r->rl.x-p2->x)*dw/dx;
1577 } else if (code2 & 4) {
1578 p2->x+=(r->lu.y-p2->y)*dx/dy;
1579 p2->w+=(r->lu.y-p2->y)*dw/dy;
1581 } else if (code2 & 8) {
1582 p2->x+=(r->rl.y-p2->y)*dx/dy;
1583 p2->w+=(r->rl.y-p2->y)*dw/dy;
1586 code2=clipcode(p2, r);
1592 graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pa, int count, int *width, int step, int poly)
1594 struct point *p=g_alloca(sizeof(struct point)*(count+1));
1595 int *w=g_alloca(sizeof(int)*(count*step+1));
1596 struct wpoint p1,p2;
1599 struct point_rect r=gra->r;
1603 for (i = 1 ; i < count ; i++) {
1604 if (width[i*step] > wmax)
1614 for (i = 0 ; i < count ; i++) {
1618 p1.w=width[(i-1)*step];
1622 /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
1623 code=clip_line(&p1, &p2, &r);
1624 if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
1636 if (i == count-1 || (code & 4)) {
1639 graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
1641 gra->meth.draw_lines(gra->priv, gc->priv, p, out);
1650 is_inside(struct point *p, struct point_rect *r, int edge)
1654 return p->x >= r->lu.x;
1656 return p->x <= r->rl.x;
1658 return p->y >= r->lu.y;
1660 return p->y <= r->rl.y;
1667 poly_intersection(struct point *p1, struct point *p2, struct point_rect *r, int edge, struct point *ret)
1673 ret->y=p1->y+(r->lu.x-p1->x)*dy/dx;
1677 ret->y=p1->y+(r->rl.x-p1->x)*dy/dx;
1681 ret->x=p1->x+(r->lu.y-p1->y)*dx/dy;
1685 ret->x=p1->x+(r->rl.y-p1->y)*dx/dy;
1692 graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pin, int count_in)
1694 struct point_rect r=gra->r;
1695 struct point *pout,*p,*s,pi,*p1,*p2;
1697 struct point *pa1=g_alloca(sizeof(struct point) * (count_in < limit ? count_in*8+1:0));
1698 struct point *pa2=g_alloca(sizeof(struct point) * (count_in < limit ? count_in*8+1:0));
1699 int count_out,edge=3;
1707 if (count_in < limit) {
1711 p1=g_new(struct point, count_in*8+1);
1712 p2=g_new(struct point, count_in*8+1);
1716 for (edge = 0 ; edge < 4 ; edge++) {
1720 for (i = 0 ; i < count_in ; i++) {
1721 if (is_inside(p, &r, edge)) {
1722 if (! is_inside(s, &r, edge)) {
1723 poly_intersection(s,p,&r,edge,&pi);
1724 pout[count_out++]=pi;
1726 pout[count_out++]=*p;
1728 if (is_inside(s, &r, edge)) {
1729 poly_intersection(p,s,&r,edge,&pi);
1730 pout[count_out++]=pi;
1745 gra->meth.draw_polygon(gra->priv, gc->priv, pin, count_in);
1746 if (count_in >= limit) {
1754 display_context_free(struct display_context *dc)
1757 graphics_gc_destroy(dc->gc);
1758 if (dc->gc_background)
1759 graphics_gc_destroy(dc->gc_background);
1761 graphics_image_free(dc->gra, dc->img);
1763 dc->gc_background=NULL;
1767 static struct graphics_font *
1768 get_font(struct graphics *gra, int size)
1772 if (size >= gra->font_len) {
1773 gra->font=g_renew(struct graphics_font *, gra->font, size+1);
1774 while (gra->font_len <= size)
1775 gra->font[gra->font_len++]=NULL;
1777 if (! gra->font[size])
1778 gra->font[size]=graphics_font_new(gra, size*gra->font_size, 0);
1779 return gra->font[size];
1782 void graphics_draw_text_std(struct graphics *this_, int text_size, char *text, struct point *p)
1784 struct graphics_font *font=get_font(this_, text_size);
1785 struct point bbox[4];
1788 graphics_get_text_bbox(this_, font, text, 0x10000, 0, bbox, 0);
1789 for (i = 0 ; i < 4 ; i++) {
1793 graphics_draw_rectangle(this_, this_->gc[2], &bbox[1], bbox[2].x-bbox[0].x, bbox[0].y-bbox[1].y+5);
1794 graphics_draw_text(this_, this_->gc[1], this_->gc[2], font, text, p, 0x10000, 0);
1798 graphics_icon_path(char *icon)
1800 static char *navit_sharedir;
1802 struct file_wordexp *wordexp=NULL;
1803 dbg(1,"enter %s\n",icon);
1804 if (strchr(icon, '$')) {
1805 wordexp=file_wordexp_new(icon);
1806 if (file_wordexp_get_count(wordexp))
1807 icon=file_wordexp_get_array(wordexp)[0];
1809 if (strchr(icon,'/'))
1812 #ifdef HAVE_API_ANDROID
1813 // get resources for the correct screen density
1815 // this part not needed, android unpacks only the correct version into res/drawable dir!
1816 // dbg(1,"android icon_path %s\n",icon);
1817 // static char *android_density;
1818 // android_density = getenv("ANDROID_DENSITY");
1819 // ret=g_strdup_printf("res/drawable-%s/%s",android_density ,icon);
1820 ret=g_strdup_printf("res/drawable/%s" ,icon);
1822 if (! navit_sharedir)
1823 navit_sharedir = getenv("NAVIT_SHAREDIR");
1824 ret=g_strdup_printf("%s/xpm/%s", navit_sharedir, icon);
1828 file_wordexp_destroy(wordexp);
1833 limit_count(struct coord *c, int count)
1836 for (i = 1 ; i < count ; i++) {
1837 if (c[i].x == c[0].x && c[i].y == c[0].y)
1845 displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
1847 int *width=g_alloca(sizeof(int)*dc->maxlen);
1848 struct point *pa=g_alloca(sizeof(struct point)*dc->maxlen);
1849 struct graphics *gra=dc->gra;
1850 struct graphics_gc *gc=dc->gc;
1851 struct element *e=dc->e;
1852 struct graphics_image *img=dc->img;
1857 int i,count=di->count,mindist=dc->mindist;
1859 di->z_order=++(gra->current_z_order);
1862 gc=graphics_gc_new(gra);
1863 graphics_gc_set_foreground(gc, &e->color);
1866 if (item_type_is_area(dc->type) && (dc->e->type == element_polyline || dc->e->type == element_text))
1867 count=limit_count(di->c, count);
1868 if (dc->type == type_poly_water_tiled)
1870 if (dc->e->type == element_polyline)
1871 count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, e->u.polyline.width, width);
1873 count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, 0, NULL);
1875 case element_polygon:
1876 graphics_draw_polygon_clipped(gra, gc, pa, count);
1878 case element_polyline:
1880 gc->meth.gc_set_linewidth(gc->priv, 1);
1881 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1882 graphics_gc_set_dashes(gc, e->u.polyline.width,
1883 e->u.polyline.offset,
1884 e->u.polyline.dash_table,
1885 e->u.polyline.dash_num);
1886 for (i = 0 ; i < count ; i++) {
1890 graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1893 case element_circle:
1895 if (e->u.circle.width > 1)
1896 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1897 graphics_draw_circle(gra, gc, pa, e->u.circle.radius);
1898 if (di->label && e->text_size) {
1899 struct graphics_font *font=get_font(gra, e->text_size);
1900 struct graphics_gc *gc_background=dc->gc_background;
1901 if (! gc_background && e->u.circle.background_color.a) {
1902 gc_background=graphics_gc_new(gra);
1903 graphics_gc_set_foreground(gc_background, &e->u.circle.background_color);
1904 dc->gc_background=gc_background;
1909 gra->meth.draw_text(gra->priv, gc->priv, gc_background?gc_background->priv:NULL, font->priv, di->label, &p, 0x10000, 0);
1911 dbg(0,"Failed to get font with size %d\n",e->text_size);
1916 if (count && di->label) {
1917 struct graphics_font *font=get_font(gra, e->text_size);
1918 struct graphics_gc *gc_background=dc->gc_background;
1919 if (! gc_background && e->u.text.background_color.a) {
1920 gc_background=graphics_gc_new(gra);
1921 graphics_gc_set_foreground(gc_background, &e->u.text.background_color);
1922 dc->gc_background=gc_background;
1925 label_line(gra, gc, gc_background, font, pa, count, di->label);
1927 dbg(0,"Failed to get font with size %d\n",e->text_size);
1932 if (!img || item_is_custom_poi(di->item)) {
1933 if (item_is_custom_poi(di->item)) {
1937 graphics_image_free(dc->gra, img);
1939 if (!src || !src[0])
1941 icon=g_strdup_printf(src,di->label+strlen(di->label)+1);
1942 path=graphics_icon_path(icon);
1945 path=graphics_icon_path(e->u.icon.src);
1946 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1950 dbg(0,"failed to load icon '%s'\n", path);
1954 p.x=pa[0].x - img->hot.x;
1955 p.y=pa[0].y - img->hot.y;
1956 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1961 dbg(1,"image: '%s'\n", di->label);
1962 if (gra->meth.draw_image_warp)
1963 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1965 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1967 case element_arrows:
1968 display_draw_arrows(gra,gc,pa,count);
1971 dbg(0, "Unhandled element type %d\n", e->type);
1981 * @author Martin Schaller (04/2008)
1983 static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *display_list, struct itemgra *itm)
1987 struct display_context *dc=&display_list->dc;
1988 struct hash_entry *entry;
1996 dc->type=GPOINTER_TO_INT(types->data);
1997 entry=get_hash_entry(display_list, dc->type);
1998 if (entry && entry->di) {
1999 displayitem_draw(entry->di, NULL, dc);
2000 display_context_free(dc);
2002 types=g_list_next(types);
2009 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t, char *label)
2012 struct display_context dc;
2014 char *buffer=g_alloca(sizeof(struct displayitem)+max_coord*sizeof(struct coord));
2015 struct displayitem *di=(struct displayitem *)buffer;
2017 di->item.type=type_none;
2025 dc.gc_background=NULL;
2027 dc.pro=projection_screen;
2031 dc.maxlen=max_coord;
2033 struct element *e=es->data;
2034 if (e->coord_count) {
2035 if (e->coord_count > max_coord) {
2036 dbg(0,"maximum number of coords reached: %d > %d\n",e->coord_count,max_coord);
2037 di->count=max_coord;
2039 di->count=e->coord_count;
2040 memcpy(di->c, e->coord, di->count*sizeof(struct coord));
2048 displayitem_draw(di, NULL, &dc);
2049 display_context_free(&dc);
2058 * @author Martin Schaller (04/2008)
2060 static void xdisplay_draw_layer(struct displaylist *display_list, struct graphics *gra, struct layer *lay, int order)
2063 struct itemgra *itm;
2068 if (order >= itm->order.min && order <= itm->order.max)
2069 xdisplay_draw_elements(gra, display_list, itm);
2070 itms=g_list_next(itms);
2079 * @author Martin Schaller (04/2008)
2081 static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra, struct layout *l, int order)
2086 gra->current_z_order=0;
2091 xdisplay_draw_layer(display_list, gra, lay, order);
2092 lays=g_list_next(lays);
2100 * @author Martin Schaller (04/2008)
2102 extern void *route_selection;
2105 displaylist_update_layers(struct displaylist *displaylist, GList *layers, int order)
2108 struct layer *layer=layers->data;
2109 GList *itemgras=layer->itemgras;
2111 struct itemgra *itemgra=itemgras->data;
2112 GList *types=itemgra->type;
2113 if (itemgra->order.min <= order && itemgra->order.max >= order) {
2115 enum item_type type=(enum item_type) types->data;
2116 set_hash_entry(displaylist, type);
2117 types=g_list_next(types);
2120 itemgras=g_list_next(itemgras);
2122 layers=g_list_next(layers);
2127 displaylist_update_hash(struct displaylist *displaylist)
2129 displaylist->max_offset=0;
2130 clear_hash(displaylist);
2131 displaylist_update_layers(displaylist, displaylist->layout->layers, displaylist->order);
2132 dbg(1,"max offset %d\n",displaylist->max_offset);
2137 * @brief Returns selection structure based on displaylist transform, projection and order.
2138 * Use this function to get map selection if you are going to fetch complete item data from the map based on displayitem reference.
2139 * @param displaylist
2140 * @returns Pointer to selection structure
2142 struct map_selection *displaylist_get_selection(struct displaylist *displaylist)
2144 return transform_get_selection(displaylist->dc.trans, displaylist->dc.pro, displaylist->order);
2148 * @brief Compare displayitems based on their zorder values.
2149 * Use with g_list_insert_sorted to sort less shaded items to be before more shaded ones in the result list.
2151 static int displaylist_cmp_zorder(const struct displayitem *a, const struct displayitem *b)
2153 if(a->z_order>b->z_order)
2155 if(a->z_order<b->z_order)
2161 * @brief Returns list of displayitems clicked at given coordinates. The deeper item is in current layout, the deeper it will be in the list.
2162 * @param displaylist
2163 * @param p clicked point
2164 * @param radius radius of clicked area
2165 * @returns GList of displayitems
2167 GList *displaylist_get_clicked_list(struct displaylist *displaylist, struct point *p, int radius)
2170 struct displayitem *di;
2171 struct displaylist_handle *dlh=graphics_displaylist_open(displaylist);
2173 while ((di=graphics_displaylist_next(dlh))) {
2174 if (di->z_order>0 && graphics_displayitem_within_dist(displaylist, di, p,radius))
2175 l=g_list_insert_sorted(l,(gpointer) di, (GCompareFunc) displaylist_cmp_zorder);
2184 do_draw(struct displaylist *displaylist, int cancel, int flags)
2187 int count,max=displaylist->dc.maxlen,workload=0;
2188 struct coord *ca=g_alloca(sizeof(struct coord)*max);
2189 struct attr attr,attr2;
2190 enum projection pro;
2192 if (displaylist->order != displaylist->order_hashed || displaylist->layout != displaylist->layout_hashed) {
2193 displaylist_update_hash(displaylist);
2194 displaylist->order_hashed=displaylist->order;
2195 displaylist->layout_hashed=displaylist->layout;
2198 pro=transform_get_projection(displaylist->dc.trans);
2200 if (!displaylist->msh)
2201 displaylist->msh=mapset_open(displaylist->ms);
2202 if (!displaylist->m) {
2203 displaylist->m=mapset_next(displaylist->msh, 1);
2204 if (!displaylist->m) {
2205 mapset_close(displaylist->msh);
2206 displaylist->msh=NULL;
2209 displaylist->dc.pro=map_projection(displaylist->m);
2210 displaylist->conv=map_requires_conversion(displaylist->m);
2211 if (route_selection)
2212 displaylist->sel=route_selection;
2214 displaylist->sel=displaylist_get_selection(displaylist);
2215 displaylist->mr=map_rect_new(displaylist->m, displaylist->sel);
2217 if (displaylist->mr) {
2218 while ((item=map_rect_get_item(displaylist->mr))) {
2221 struct hash_entry *entry;
2222 if (item == &busy_item) {
2223 if (displaylist->workload)
2228 entry=get_hash_entry(displaylist, item->type);
2231 count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, displaylist->sel);
2234 if (displaylist->dc.pro != pro)
2235 transform_from_to_count(ca, displaylist->dc.pro, ca, pro, count);
2237 dbg(0,"point count overflow %d for %s "ITEM_ID_FMT"\n", count,item_to_name(item->type),ITEM_ID_ARGS(*item));
2238 displaylist->dc.maxlen=max*2;
2240 if (item_is_custom_poi(*item)) {
2241 if (item_attr_get(item, attr_icon_src, &attr2))
2242 labels[1]=map_convert_string(displaylist->m, attr2.u.str);
2250 if (item_attr_get(item, attr_label, &attr)) {
2251 labels[0]=attr.u.str;
2256 if (displaylist->conv && label_count) {
2257 labels[0]=map_convert_string(displaylist->m, labels[0]);
2258 display_add(entry, item, count, ca, labels, label_count);
2259 map_convert_free(labels[0]);
2261 display_add(entry, item, count, ca, labels, label_count);
2263 map_convert_free(labels[1]);
2265 if (workload == displaylist->workload)
2268 map_rect_destroy(displaylist->mr);
2270 if (!route_selection)
2271 map_selection_destroy(displaylist->sel);
2272 displaylist->mr=NULL;
2273 displaylist->sel=NULL;
2274 displaylist->m=NULL;
2276 profile(1,"process_selection\n");
2277 if (displaylist->idle_ev)
2278 event_remove_idle(displaylist->idle_ev);
2279 displaylist->idle_ev=NULL;
2280 callback_destroy(displaylist->idle_cb);
2281 displaylist->idle_cb=NULL;
2282 displaylist->busy=0;
2283 graphics_process_selection(displaylist->dc.gra, displaylist);
2284 profile(1,"draw\n");
2286 graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, flags);
2287 map_rect_destroy(displaylist->mr);
2288 if (!route_selection)
2289 map_selection_destroy(displaylist->sel);
2290 mapset_close(displaylist->msh);
2291 displaylist->mr=NULL;
2292 displaylist->sel=NULL;
2293 displaylist->m=NULL;
2294 displaylist->msh=NULL;
2295 profile(1,"callback\n");
2296 callback_call_1(displaylist->cb, cancel);
2304 * @author Martin Schaller (04/2008)
2306 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int flags)
2308 int order=transform_get_order(trans);
2309 if(displaylist->dc.trans && displaylist->dc.trans!=trans)
2310 transform_destroy(displaylist->dc.trans);
2311 if(displaylist->dc.trans!=trans)
2312 displaylist->dc.trans=transform_dup(trans);
2313 displaylist->dc.gra=gra;
2314 displaylist->dc.mindist=transform_get_scale(trans)/2;
2315 // FIXME find a better place to set the background color
2317 graphics_gc_set_background(gra->gc[0], &l->color);
2318 graphics_gc_set_foreground(gra->gc[0], &l->color);
2319 gra->default_font = g_strdup(l->font);
2321 graphics_background_gc(gra, gra->gc[0]);
2323 callback_list_call_attr_0(gra->cbl, attr_predraw);
2324 gra->meth.draw_mode(gra->priv, (flags & 8)?draw_mode_begin_clear:draw_mode_begin);
2326 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &gra->r.lu, gra->r.rl.x-gra->r.lu.x, gra->r.rl.y-gra->r.lu.y);
2328 order+=l->order_delta;
2329 xdisplay_draw(displaylist, gra, l, order>0?order:0);
2332 callback_list_call_attr_0(gra->cbl, attr_postdraw);
2334 gra->meth.draw_mode(gra->priv, draw_mode_end);
2337 static void graphics_load_mapset(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb, int flags)
2339 int order=transform_get_order(trans);
2342 if (displaylist->busy) {
2345 do_draw(displaylist, 1, flags);
2347 xdisplay_free(displaylist);
2348 dbg(1,"order=%d\n", order);
2350 displaylist->dc.gra=gra;
2351 displaylist->ms=mapset;
2352 if(displaylist->dc.trans && displaylist->dc.trans!=trans)
2353 transform_destroy(displaylist->dc.trans);
2354 if(displaylist->dc.trans!=trans)
2355 displaylist->dc.trans=transform_dup(trans);
2356 displaylist->workload=async ? 100 : 0;
2360 order+=l->order_delta;
2361 displaylist->order=order>0?order:0;
2362 displaylist->busy=1;
2363 displaylist->layout=l;
2365 if (! displaylist->idle_cb)
2366 displaylist->idle_cb=callback_new_3(callback_cast(do_draw), displaylist, 0, flags);
2367 displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
2369 do_draw(displaylist, 0, flags);
2375 * @author Martin Schaller (04/2008)
2377 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb, int flags)
2379 graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb, flags);
2383 graphics_draw_cancel(struct graphics *gra, struct displaylist *displaylist)
2385 if (!displaylist->busy)
2387 do_draw(displaylist, 1, 0);
2395 * @author Martin Schaller (04/2008)
2397 struct displaylist_handle {
2398 struct displaylist *dl;
2399 struct displayitem *di;
2407 * @author Martin Schaller (04/2008)
2409 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
2411 struct displaylist_handle *ret;
2413 ret=g_new0(struct displaylist_handle, 1);
2414 ret->dl=displaylist;
2423 * @author Martin Schaller (04/2008)
2425 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
2427 struct displayitem *ret;
2436 if (dlh->hashidx == HASH_SIZE) {
2440 if (dlh->dl->hash_entries[dlh->hashidx].type)
2441 dlh->di=dlh->dl->hash_entries[dlh->hashidx].di;
2451 * @author Martin Schaller (04/2008)
2453 void graphics_displaylist_close(struct displaylist_handle *dlh)
2462 * @author Martin Schaller (04/2008)
2464 struct displaylist * graphics_displaylist_new(void)
2466 struct displaylist *ret=g_new0(struct displaylist, 1);
2468 ret->dc.maxlen=16384;
2473 void graphics_displaylist_destroy(struct displaylist *displaylist)
2475 if(displaylist->dc.trans)
2476 transform_destroy(displaylist->dc.trans);
2477 g_free(displaylist);
2483 * Get the map item which given displayitem is based on.
2484 * NOTE: returned structure doesn't contain any attributes or coordinates. type, map, idhi and idlow seem to be the only useable members.
2485 * @param di pointer to displayitem structure
2486 * @returns Pointer to struct item
2487 * @author Martin Schaller (04/2008)
2489 struct item * graphics_displayitem_get_item(struct displayitem *di)
2495 * Get the number of this item as it was last displayed on the screen, dependent of current layout. Items with lower numbers
2496 * are shaded by items with higher ones when they overlap. Zero means item was not displayed at all. If the item is displayed twice, its topmost
2497 * occurence is used.
2498 * @param di pointer to displayitem structure
2499 * @returns z-order of current item.
2501 int graphics_displayitem_get_z_order(struct displayitem *di)
2508 graphics_displayitem_get_coord_count(struct displayitem *di)
2517 * @author Martin Schaller (04/2008)
2519 char * graphics_displayitem_get_label(struct displayitem *di)
2525 graphics_displayitem_get_displayed(struct displayitem *di)
2534 * @author Martin Schaller (04/2008)
2536 static int within_dist_point(struct point *p0, struct point *p1, int dist)
2538 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
2540 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
2542 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
2552 * @author Martin Schaller (04/2008)
2554 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
2558 struct point line_p;
2560 if (line_p0->x < line_p1->x) {
2561 if (p->x < line_p0->x - dist)
2563 if (p->x > line_p1->x + dist)
2566 if (p->x < line_p1->x - dist)
2568 if (p->x > line_p0->x + dist)
2571 if (line_p0->y < line_p1->y) {
2572 if (p->y < line_p0->y - dist)
2574 if (p->y > line_p1->y + dist)
2577 if (p->y < line_p1->y - dist)
2579 if (p->y > line_p0->y + dist)
2583 vx=line_p1->x-line_p0->x;
2584 vy=line_p1->y-line_p0->y;
2590 return within_dist_point(p, line_p0, dist);
2593 return within_dist_point(p, line_p1, dist);
2595 line_p.x=line_p0->x+vx*c1/c2;
2596 line_p.y=line_p0->y+vy*c1/c2;
2597 return within_dist_point(p, &line_p, dist);
2604 * @author Martin Schaller (04/2008)
2606 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
2609 for (i = 0 ; i < count-1 ; i++) {
2610 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
2615 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
2623 * @author Martin Schaller (04/2008)
2625 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
2628 for (i = 0, j = count-1; i < count; j = i++) {
2629 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
2630 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
2631 (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))
2635 return within_dist_polyline(p, poly_pnt, count, dist, 1);
2643 * @author Martin Schaller (04/2008)
2645 int graphics_displayitem_within_dist(struct displaylist *displaylist, struct displayitem *di, struct point *p, int dist)
2647 struct point *pa=g_alloca(sizeof(struct point)*displaylist->dc.maxlen);
2650 count=transform(displaylist->dc.trans, displaylist->dc.pro, di->c, pa, di->count, 1, 0, NULL);
2652 if (di->item.type < type_line) {
2653 return within_dist_point(p, &pa[0], dist);
2655 if (di->item.type < type_area) {
2656 return within_dist_polyline(p, pa, count, dist, 0);
2658 return within_dist_polygon(p, pa, count, dist);
2663 graphics_process_selection_item(struct displaylist *dl, struct item *item)
2666 struct displayitem di,*di_res;
2668 int count,max=dl->dc.maxlen;
2669 struct coord ca[max];
2671 struct map_rect *mr;
2676 h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
2678 di_res=g_hash_table_lookup(h, &di);
2680 di.item.type=(enum item_type)item->priv_data;
2681 display_add(dl, &di.item, di_res->count, di_res->c, NULL, 0);
2685 mr=map_rect_new(item->map, NULL);
2686 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
2687 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
2688 if (!item_attr_get(item, attr_label, &attr))
2690 if (dl->conv && attr.u.str && attr.u.str[0]) {
2691 char *str=map_convert_string(item->map, attr.u.str);
2692 display_add(dl, item, count, ca, &str, 1);
2693 map_convert_free(str);
2695 display_add(dl, item, count, ca, &attr.u.str, 1);
2696 map_rect_destroy(mr);
2701 graphics_add_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
2703 struct item *item_dup=g_new(struct item, 1);
2705 item_dup->priv_data=(void *)type;
2706 gra->selection=g_list_append(gra->selection, item_dup);
2708 graphics_process_selection_item(dl, item_dup);
2712 graphics_remove_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
2718 curr=gra->selection;
2721 struct item *sitem=curr->data;
2722 if (item_is_equal(*item,*sitem)) {
2724 struct displayitem di;
2732 h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
2734 g_hash_table_remove(h, &di);
2738 gra->selection=g_list_remove(gra->selection, curr->data);
2749 graphics_clear_selection(struct graphics *gra, struct displaylist *dl)
2751 while (gra->selection) {
2752 struct item *item=(struct item *)gra->selection->data;
2753 graphics_remove_selection(gra, item, (enum item_type)item->priv_data,dl);
2758 graphics_process_selection(struct graphics *gra, struct displaylist *dl)
2762 curr=gra->selection;
2764 struct item *item=curr->data;
2765 graphics_process_selection_item(dl, item);
2766 curr=g_list_next(curr);