9 #include "projection.h"
12 #include "transform.h"
22 struct graphics_priv *priv;
23 struct graphics_methods meth;
24 struct graphics_font *font[16];
25 struct graphics_gc *gc[3];
34 graphics_new(const char *type, struct attr **attrs)
36 struct graphics *this_;
37 struct graphics_priv * (*new)(struct graphics_methods *meth, struct attr **attrs);
39 new=plugin_get_graphics_type(type);
42 this_=g_new0(struct graphics, 1);
43 this_->priv=(*new)(&this_->meth, attrs);
48 graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h)
50 struct graphics *this_;
51 this_=g_new0(struct graphics, 1);
52 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h);
58 graphics_init(struct graphics *this_)
60 this_->gc[0]=graphics_gc_new(this_);
61 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
62 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
63 this_->gc[1]=graphics_gc_new(this_);
64 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000 });
65 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff });
66 this_->gc[2]=graphics_gc_new(this_);
67 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
68 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
69 this_->meth.background_gc(this_->priv, this_->gc[0]->priv);
73 graphics_get_data(struct graphics *this_, char *type)
75 return (this_->meth.get_data(this_->priv, type));
79 graphics_register_resize_callback(struct graphics *this_, void (*callback)(void *data, int w, int h), void *data)
81 this_->meth.register_resize_callback(this_->priv, callback, data);
85 graphics_register_button_callback(struct graphics *this_, void (*callback)(void *data, int pressed, int button, struct point *p), void *data)
87 this_->meth.register_button_callback(this_->priv, callback, data);
91 graphics_register_motion_callback(struct graphics *this_, void (*callback)(void *data, struct point *p), void *data)
93 this_->meth.register_motion_callback(this_->priv, callback, data);
96 struct graphics_font *
97 graphics_font_new(struct graphics *gra, int size)
99 struct graphics_font *this_;
101 this_=g_new0(struct graphics_font,1);
102 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, size);
107 graphics_gc_new(struct graphics *gra)
109 struct graphics_gc *this_;
111 this_=g_new0(struct graphics_gc,1);
112 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
117 graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
119 gc->meth.gc_set_foreground(gc->priv, c);
123 graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
125 gc->meth.gc_set_background(gc->priv, c);
129 graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
131 gc->meth.gc_set_linewidth(gc->priv, width);
134 struct graphics_image *
135 graphics_image_new(struct graphics *gra, char *path)
137 struct graphics_image *this_;
139 this_=g_new0(struct graphics_image,1);
140 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
149 graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
151 this_->meth.draw_restore(this_->priv, p, w, h);
155 graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
157 this_->meth.draw_mode(this_->priv, mode);
161 graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
163 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
167 graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
169 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
174 graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
176 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
186 popup_view_html(struct popup_item *item, char *file)
189 sprintf(command,"firefox %s", file);
194 graphics_popup(struct display_list *list, struct popup_item **popup)
200 struct popup_item *curr_item,*last=NULL;
202 mr=map_rect_new(item->map, NULL, NULL, 0);
203 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
204 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
206 if (item_attr_get(item, attr_name, &attr)) {
207 curr_item=popup_item_new_text(popup,attr.u.str,1);
208 if (item_attr_get(item, attr_info_html, &attr)) {
209 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
211 if (item_attr_get(item, attr_price_html, &attr)) {
212 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
214 curr_item->submenu=last;
217 map_rect_destroy(mr);
230 xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
236 struct displayitem *di=l->data;
237 if (! di->displayed && di->item.type < type_line)
238 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
247 xdisplay_free(GHashTable *display_list)
249 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
253 display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
255 struct displayitem *di;
260 len=sizeof(*di)+count*sizeof(*pnt);
262 len+=strlen(label)+1;
266 di=(struct displayitem *)p;
268 p+=sizeof(*di)+count*sizeof(*pnt);
272 strcpy(di->label, label);
276 memcpy(di->pnt, pnt, count*sizeof(*pnt));
278 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
279 l=g_list_prepend(l, di);
280 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
285 label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
291 tl=strlen(label)*400;
292 for (i = 0 ; i < count-1 ; i++) {
297 l=(int)sqrt((float)(dx*dx+dy*dy));
314 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
316 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
323 xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemtype *itm)
326 GList *l,*ls,*es,*types;
328 struct graphics_gc *gc;
329 struct graphics_image *img;
337 type=GPOINTER_TO_INT(types->data);
338 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
343 struct displayitem *di;
347 gc=graphics_gc_new(gra);
348 gc->meth.gc_set_foreground(gc->priv, &e->color);
351 case element_polygon:
352 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
354 case element_polyline:
355 if (e->u.polyline.width > 1)
356 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
357 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
360 if (e->u.circle.width > 1)
361 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
362 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
365 if (! gra->font[e->label_size])
366 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20);
367 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);
371 if (! gra->font[e->label_size])
372 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20);
373 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->label_size], di->pnt, di->count, di->label);
378 char *icon=g_strdup_printf("xpm/%s", e->u.icon.src);
379 img=graphics_image_new(gra, icon);
382 g_warning("failed to load icon '%s'\n", e->u.icon.src);
385 p.x=di->pnt[0].x - img->hot.x;
386 p.y=di->pnt[0].y - img->hot.y;
387 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
391 printf("image: '%s'\n", di->label);
392 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
395 printf("Unhandled element type %d\n", e->type);
400 types=g_list_next(types);
407 xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
410 struct itemtype *itm;
415 if (order >= itm->order_min && order <= itm->order_max)
416 xdisplay_draw_elements(gra, display_list, itm);
417 itms=g_list_next(itms);
422 xdisplay_draw_layout(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
430 xdisplay_draw_layer(display_list, gra, lay, order);
431 lays=g_list_next(lays);
436 xdisplay_draw(GHashTable *display_list, struct graphics *gra, GList *layouts, int order)
442 xdisplay_draw_layout(display_list, gra, l, order);
444 layouts=g_list_next(layouts);
448 extern void *route_selection;
451 do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order, struct route *route)
453 struct map_selection sel;
459 struct mapset_handle *h;
461 int conv,count,max=16384;
462 struct point pnt[max];
469 sel.order[layer_town]=1*order;
470 sel.order[layer_street]=order;
471 sel.order[layer_poly]=1*order;
474 while ((m=mapset_next(h, 1))) {
475 pro=map_projection(m);
476 conv=map_requires_conversion(m);
477 transform_rect(t, pro, &sel.rect);
479 mr=map_rect_new(m, route_selection);
481 mr=map_rect_new(m, &sel);
482 while ((item=map_rect_get_item(mr))) {
483 if (item->type < type_line) {
484 item_coord_get(item, &c, 1);
485 if (!transform(t, pro, &c, &pnt[0])) {
486 dbg(1,"not visible\n");
492 while (count < max) {
493 if (!item_coord_get(item, &c, 1))
499 coord_rect_extend(&r, &c);
500 transform(t, pro, &c, &pnt[count]);
501 if (! count || pnt[count].x != pnt[count-1].x || pnt[count].y != pnt[count-1].y)
505 g_assert(count < max);
506 if (!transform_contains(t, pro, &r)) {
507 dbg(1,"not visible\n");
510 if (route && route_contains(route, item)) {
513 ritem.type=type_street_route;
514 display_add(displaylist, &ritem, count, pnt, NULL);
517 if (!item_attr_get(item, attr_label, &attr))
519 if (conv && attr.u.str && attr.u.str[0]) {
520 char *str=map_convert_string(m, attr.u.str);
521 display_add(displaylist, item, count, pnt, str);
522 map_convert_free(str);
524 display_add(displaylist, item, count, pnt, attr.u.str);
526 map_rect_destroy(mr);
532 graphics_ready(struct graphics *this_)
538 graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, GList *layouts, struct route *route)
540 int order=transform_get_order(trans);
541 gra->meth.draw_mode(gra->priv, draw_mode_begin);
543 route_draw(route, trans, displaylist);
544 xdisplay_draw(displaylist->dl, gra, layouts, order);
549 graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, GList *layouts, struct route *route)
551 int order=transform_get_order(trans);
556 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));
559 xdisplay_free(displaylist->dl);
560 dbg(1,"order=%d\n", order);
564 for (i = 0 ; i < data_window_type_end; i++) {
565 data_window_begin(co->data_window[i]);
569 do_draw(displaylist, trans, mapsets, order, route);
570 profile(1,"do_draw");
571 graphics_displaylist_draw(gra, displaylist, trans, layouts, route);
572 profile(1,"xdisplay_draw");
575 gra->meth.draw_mode(gra->priv, draw_mode_end);
577 for (i = 0 ; i < data_window_type_end; i++) {
578 data_window_end(co->data_window[i]);
585 struct displaylist_handle {
586 GList *hl_head,*hl,*l;
590 struct displaylist_handle *
591 graphics_displaylist_open(struct displaylist *displaylist)
593 struct displaylist_handle *ret;
595 ret=g_new0(struct displaylist_handle, 1);
596 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
602 graphics_displaylist_next(struct displaylist_handle *dlh)
604 struct displayitem *ret;
608 dlh->l=dlh->hl->data;
609 dlh->hl=g_list_next(dlh->hl);
612 dlh->l=g_list_next(dlh->l);
617 graphics_displaylist_close(struct displaylist_handle *dlh)
619 g_list_free(dlh->hl_head);
624 graphics_displaylist_new(void)
626 struct displaylist *ret=g_new(struct displaylist, 1);
628 ret->dl=g_hash_table_new(NULL,NULL);
634 graphics_displayitem_get_item(struct displayitem *di)
640 graphics_displayitem_get_label(struct displayitem *di)
646 within_dist_point(struct point *p0, struct point *p1, int dist)
648 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
650 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
652 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
659 within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
665 vx=line_p1->x-line_p0->x;
666 vy=line_p1->y-line_p0->y;
672 return within_dist_point(p, line_p0, dist);
675 return within_dist_point(p, line_p1, dist);
677 line_p.x=line_p0->x+vx*c1/c2;
678 line_p.y=line_p0->y+vy*c1/c2;
679 return within_dist_point(p, &line_p, dist);
683 within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
686 for (i = 0 ; i < count-1 ; i++) {
687 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
692 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
697 within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
700 for (i = 0, j = count-1; i < count; j = i++) {
701 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
702 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
703 (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))
707 return within_dist_polyline(p, poly_pnt, count, dist, 1);
712 graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
714 if (di->item.type < type_line) {
715 return within_dist_point(p, &di->pnt[0], dist);
717 if (di->item.type < type_area) {
718 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
720 return within_dist_polygon(p, di->pnt, di->count, dist);