Fix:Core:Correct layering of android surfaces
[navit-package] / navit / graphics.c
index d8da889..343c390 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include "config.h"
 #include "debug.h"
 #include "string.h"
 #include "draw_info.h"
@@ -48,8 +49,8 @@
 #include "util.h"
 #include "callback.h"
 #include "file.h"
+#include "event.h"
 
-static char *navit_sharedir;
 
 //##############################################################################################################
 //# Description: 
@@ -61,11 +62,13 @@ struct graphics
        struct graphics_priv *priv;
        struct graphics_methods meth;
        char *default_font;
-       struct graphics_font *font[16];
+       int font_len;
+       struct graphics_font **font;
        struct graphics_gc *gc[3];
        struct attr **attrs;
        struct callback_list *cbl;
        struct point_rect r;
+       GList *selection;
 };
 
 struct display_context
@@ -75,6 +78,7 @@ struct display_context
        struct graphics_gc *gc;
        struct graphics_image *img;
        enum projection pro;
+       int mindist;
        struct transformation *trans;
 };
 
@@ -97,6 +101,19 @@ struct displaylist {
 };
 
 
+static void draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir);
+static void graphics_process_selection(struct graphics *gra, struct displaylist *dl);
+
+int
+graphics_set_attr(struct graphics *gra, struct attr *attr)
+{
+       int ret=1;
+       dbg(0,"enter\n");
+       if (gra->meth.set_attr)
+               ret=gra->meth.set_attr(gra->priv, attr);
+        return ret != 0;
+}
+
 void
 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
 {
@@ -162,6 +179,27 @@ struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p,
 }
 
 /**
+ * @brief Alters the size, position, alpha and wraparound for an overlay
+ *
+ * @param this_ The overlay's graphics struct
+ * @param p The new position of the overlay
+ * @param w The new width of the overlay
+ * @param h The new height of the overlay
+ * @param alpha The new alpha of the overlay
+ * @param wraparound The new wraparound of the overlay
+ */
+void 
+graphics_overlay_resize(struct graphics *this_, struct point *p, int w, int h, int alpha, int wraparound)
+{
+       if (! this_->meth.overlay_resize) {
+               return;
+       }
+       
+       this_->meth.overlay_resize(this_->priv, p, w, h, alpha, wraparound);
+}
+
+
+/**
  * FIXME
  * @param <>
  * @returns <>
@@ -169,6 +207,8 @@ struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p,
 */
 void graphics_init(struct graphics *this_)
 {
+       if (this_->gc[0])
+               return;
        this_->gc[0]=graphics_gc_new(this_);
        graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
        graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
@@ -179,7 +219,6 @@ void graphics_init(struct graphics *this_)
        graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
        graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
        graphics_background_gc(this_, this_->gc[0]);
-       navit_sharedir = getenv("NAVIT_SHAREDIR");
 }
 
 /**
@@ -228,7 +267,7 @@ struct graphics_font * graphics_font_new(struct graphics *gra, int size, int fla
 void graphics_font_destroy_all(struct graphics *gra) 
 { 
        int i; 
-       for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) { 
+       for(i = 0 ; i < gra->font_len; i++) { 
                if(!gra->font[i]) continue; 
                gra->font[i]->meth.font_destroy(gra->font[i]->priv); 
                gra->font[i] = NULL; 
@@ -284,6 +323,19 @@ void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
        gc->meth.gc_set_background(gc->priv, c);
 }
 
+
+/**
+ * FIXME
+ * @param <>
+ * @returns <>
+ * @author Martin Schaller (04/2008)
+*/
+void graphics_gc_set_stipple(struct graphics_gc *gc, struct graphics_image *img) 
+{
+       gc->meth.gc_set_stipple(gc->priv, img ? img->priv : NULL);
+}
+
+
 /**
  * FIXME
  * @param <>
@@ -436,6 +488,28 @@ void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, str
        this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
 }
 
+void graphics_draw_rectangle_rounded(struct graphics *this_, struct graphics_gc *gc, struct point *plu, int w, int h, int r, int fill)
+{
+       struct point p[r*4+32];
+       struct point pi0={plu->x+r,plu->y+r};
+       struct point pi1={plu->x+w-r,plu->y+r};
+       struct point pi2={plu->x+w-r,plu->y+h-r};
+       struct point pi3={plu->x+r,plu->y+h-r};
+       int i=0;
+
+       draw_circle(&pi2, r*2, 0, -1, 258, p, &i, 1);
+       draw_circle(&pi1, r*2, 0, 255, 258, p, &i, 1);
+       draw_circle(&pi0, r*2, 0, 511, 258, p, &i, 1);
+       draw_circle(&pi3, r*2, 0, 767, 258, p, &i, 1);
+       p[i]=p[0];
+       i++;
+       if (fill)
+               this_->meth.draw_polygon(this_->priv, gc->priv, p, i);
+       else
+               this_->meth.draw_lines(this_->priv, gc->priv, p, i);
+}
+
+
 /**
  * FIXME
  * @param <>
@@ -598,7 +672,7 @@ static guint
 displayitem_hash(gconstpointer key)
 {
        const struct displayitem *di=key;
-       return (di->item.id_hi^di->item.id_lo^((int) di->item.map));
+       return (di->item.id_hi^di->item.id_lo^(GPOINTER_TO_INT(di->item.map)));
 }
 
 static gboolean
@@ -648,7 +722,7 @@ static void display_add(struct displaylist *displaylist, struct item *item, int
                h=g_hash_table_new_full(displayitem_hash, displayitem_equal, g_free, NULL);
                g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), h);
        }
-       g_hash_table_replace(h, di, NULL);
+       g_hash_table_replace(h, di, di);
 }
 
 
@@ -674,14 +748,14 @@ static void label_line(struct graphics *gra, struct graphics_gc *fg, struct grap
                tl=strlen(label)*4;
                th=8;
        }
-       tlm=tl*128;
-       thm=th*144;
+       tlm=tl*32;
+       thm=th*36;
        tlsq = tlm*tlm;
        for (i = 0 ; i < count-1 ; i++) {
                dx=p[i+1].x-p[i].x;
-               dx*=128;
+               dx*=32;
                dy=p[i+1].y-p[i].y;
-               dy*=128;
+               dy*=32;
                lsq = dx*dx+dy*dy;
                if (lsq > tlsq) {
                        l=(int)sqrtf(lsq);
@@ -693,10 +767,10 @@ static void label_line(struct graphics *gra, struct graphics_gc *fg, struct grap
                                x=p[i+1].x;
                                y=p[i+1].y;
                        }
-                       x+=(l-tlm)*dx/l/256;
-                       y+=(l-tlm)*dy/l/256;
-                       x-=dy*thm/l/256;
-                       y+=dx*thm/l/256;
+                       x+=(l-tlm)*dx/l/64;
+                       y+=(l-tlm)*dy/l/64;
+                       x-=dy*thm/l/64;
+                       y+=dx*thm/l/64;
                        p_t.x=x;
                        p_t.y=y;
 #if 0
@@ -938,16 +1012,25 @@ static int
 int_sqrt(unsigned int n)
 {
        unsigned int h, p= 0, q= 1, r= n;
-       while ( q <= n )
-               q <<= 2;
-       while ( q != 1 ) {
+
+       /* avoid q rollover */
+       if(n >= (1<<(sizeof(n)*8-2))) {
+               q = 1<<(sizeof(n)*8-2);
+       } else {
+               while ( q <= n ) {
+                       q <<= 2;
+               }
                q >>= 2;
+       }
+
+       while ( q != 0 ) {
                h = p + q;
                p >>= 1;
                if ( r >= h ) {
                        p += q;
                        r -= h;
-               }
+               }
+               q >>= 2;
        }
        return p;
 }
@@ -960,6 +1043,7 @@ static void
 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
 {
        int x,y;
+       
        x = (dx * wi) / l;
        y = (dy * wi) / l;
        if (x < 0) {
@@ -989,18 +1073,35 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
        int wi, ppos = maxpoints/2, npos = maxpoints/2;
        int state,prec=5;
        int max_circle_points=20;
-       for (i = 0; i < count; i++) {
+       int lscale=16;
+       i=0;
+       for (;;) {
                wi=*width;
                width+=step;
                if (i < count - 1) {
+                       int dxs,dys,lscales;
+
                        dx = (pnt[i + 1].x - pnt[i].x);
                        dy = (pnt[i + 1].y - pnt[i].y);
-                       l = int_sqrt(dx * dx + dy * dy);
+#if 0
+                       l = int_sqrt(dx * dx * lscale * lscale + dy * dy * lscale * lscale);
+#else
+                       dxs=dx*dx;
+                       dys=dy*dy;
+                       lscales=lscale*lscale;
+                       if (dxs + dys > lscales)
+                               l = int_sqrt(dxs+dys)*lscale;
+                       else
+                               l = int_sqrt((dxs+dys)*lscales);
+#endif
                        fow=fowler(-dy, dx);
                }
                if (! l) 
                        l=1;
-               calc_offsets(wi, l, dx, dy, &o);
+               if (wi*lscale > 10000)
+                       lscale=10000/wi;
+               dbg_assert(wi*lscale < 10000);
+               calc_offsets(wi*lscale, l, dx, dy, &o);
                pos.x = pnt[i].x + o.ny;
                pos.y = pnt[i].y + o.px;
                neg.x = pnt[i].x + o.py;
@@ -1059,9 +1160,12 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
                        res[ppos++] = pos;
                        break;
                }
+               i++;
+               if (i >= count)
+                       break;
                if (step) {
                        wi=*width;
-                       calc_offsets(wi, l, dx, dy, &oo);
+                       calc_offsets(wi*lscale, l, dx, dy, &oo);
                } else 
                        oo=o;
                dxo = -dx;
@@ -1255,8 +1359,8 @@ graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, stru
 {
        struct point_rect r=gra->r;
        struct point *pout,*p,*s,pi;
-       struct point p1[count_in+1];
-       struct point p2[count_in+1];
+       struct point p1[count_in*8+1];
+       struct point p2[count_in*8+1];
        int count_out,edge=3;
        int i;
 #if 0
@@ -1303,24 +1407,60 @@ graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, stru
 static void
 display_context_free(struct display_context *dc)
 {
-       g_free(dc->gc);
-       g_free(dc->img);
+       if (dc->gc)
+               graphics_gc_destroy(dc->gc);
+       if (dc->img)
+               graphics_image_free(dc->gra, dc->img);
        dc->gc=NULL;
        dc->img=NULL;
 }
 
+static struct graphics_font *
+get_font(struct graphics *gra, int size)
+{
+       if (size > 64)
+               size=64;
+       if (size >= gra->font_len) {
+               gra->font=g_renew(struct graphics_font *, gra->font, size+1);
+               while (gra->font_len <= size) 
+                       gra->font[gra->font_len++]=NULL;
+       }
+       if (! gra->font[size])
+               gra->font[size]=graphics_font_new(gra, size*20, 0);
+       return gra->font[size];
+}
+
+char *
+graphics_icon_path(char *icon)
+{
+       static char *navit_sharedir;
+       dbg(1,"enter %s\n",icon);
+       if (icon[0] == '/')
+               return g_strdup(icon);
+       else {
+#ifdef HAVE_API_ANDROID
+               return g_strdup_printf("res/drawable/%s", icon);
+#else
+               if (! navit_sharedir)
+                       navit_sharedir = getenv("NAVIT_SHAREDIR");
+               return g_strdup_printf("%s/xpm/%s", navit_sharedir, icon);
+#endif
+       }
+}
+
+
 static void
 displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
 {
        int width[16384];
-       int count;
+       int i,count;
        struct point pa[16384];
        struct graphics *gra=dc->gra;
        struct graphics_gc *gc=dc->gc;
        struct element *e=dc->e;
        struct graphics_image *img=dc->img;
        struct point p;
-       char path[PATH_MAX];
+       char *path;
 
        di->displayed=1;
        if (! gc) {
@@ -1329,10 +1469,10 @@ displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc
                dc->gc=gc;
        }
        if (dc->e->type == element_polyline) {
-               count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, e->u.polyline.width, width);
+               count=transform(dc->trans, dc->pro, di->c, pa, di->count, dc->mindist, e->u.polyline.width, width);
        }
        else
-               count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, 0, NULL);
+               count=transform(dc->trans, dc->pro, di->c, pa, di->count, dc->mindist, 0, NULL);
        switch (e->type) {
        case element_polygon:
 #if 0
@@ -1373,6 +1513,10 @@ displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc
                        if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
                                dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
 #endif
+                       for (i = 0 ; i < count ; i++) {
+                               if (width[i] < 2)
+                                       width[i]=2;
+                       }
                        graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
 #if 0
                        }
@@ -1385,29 +1529,31 @@ displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc
                                gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
                        gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
                        if (di->label && e->text_size) {
+                               struct graphics_font *font=get_font(gra, e->text_size);
                                p.x=pa[0].x+3;
                                p.y=pa[0].y+10;
-                               if (! gra->font[e->text_size])
-                                       gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
-                               gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, di->label, &p, 0x10000, 0);
+                               if (font)
+                                       gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, font->priv, di->label, &p, 0x10000, 0);
+                               else
+                                       dbg(0,"Failed to get font with size %d\n",e->text_size);
                        }
                }
                break;
        case element_text:
                if (count && di->label) {
-                       if (! gra->font[e->text_size])
-                               gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
-                       label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], pa, count, di->label);
+                       struct graphics_font *font=get_font(gra, e->text_size);
+                       if (font)
+                               label_line(gra, gra->gc[2], gra->gc[1], font, pa, count, di->label);
+                       else
+                               dbg(0,"Failed to get font with size %d\n",e->text_size);
                }
                break;
        case element_icon:
                if (count) {
                        if (!img) {
-                               if (e->u.icon.src[0] == '/')
-                                       strcpy(path,e->u.icon.src);
-                               else
-                                       sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
+                               path=graphics_icon_path(e->u.icon.src); 
                                img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
+                               g_free(path);
                                if (img)
                                        dc->img=img;
                                else
@@ -1447,6 +1593,7 @@ static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *dis
        GList *es,*types;
        GHashTable *h;
        enum item_type type;
+       int mindist=0;
 
        es=itm->elements;
        while (es) {
@@ -1457,8 +1604,14 @@ static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *dis
                        type=GPOINTER_TO_INT(types->data);
                        h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(type));
                        if (h) {
+                               if (type == type_poly_water_tiled) {
+                                       mindist=display_list->dc.mindist;
+                                       display_list->dc.mindist=0;                             
+                               }
                                g_hash_table_foreach(h, (GHFunc)displayitem_draw, &display_list->dc);
                                display_context_free(&display_list->dc);
+                               if (type == type_poly_water_tiled) 
+                                       display_list->dc.mindist=mindist;
                        }
                        types=g_list_next(types);
                }
@@ -1472,10 +1625,12 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
        GList *es;
        struct point p;
        struct coord c;
+#if 0
        char *label=NULL;
+#endif
        struct graphics_gc *gc = NULL;
        struct graphics_image *img;
-       char path[PATH_MAX];
+       char *path;
        es=itm->elements;
        c.x=0;
        c.y=0;
@@ -1502,24 +1657,30 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
                                                       e->u.polyline.dash_num);
                        gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
                        break;
+               case element_polygon:
+                       gra->meth.draw_polygon(gra->priv, gc->priv, pnt, count);
+                       break;
                case element_circle:
                        if (e->u.circle.width > 1) 
                                gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
                        gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
+                       #if 0
+                       // Leftover code,  displayitem_draw is intended to be merged with with graphics_draw_itemgra
                        if (label && e->text_size) {
+                               struct graphics_font *font=get_font(gra, e->text_size);
                                p.x=pnt[0].x+3;
                                p.y=pnt[0].y+10;
-                       if (! gra->font[e->text_size])
-                               gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
-                               gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
+                               if (font) 
+                                       gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, font->priv, label, &p, 0x10000, 0);
+                               else
+                                       dbg(0,"Failed to get font with size %d\n",e->text_size);
                        }
+                       # endif
                        break;
                case element_icon:
-                       if (e->u.icon.src[0] == '/') 
-                               strcpy(path,e->u.icon.src);
-                       else
-                               sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
+                       path=graphics_icon_path(e->u.icon.src); 
                        img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
+                       g_free(path);
                        if (! img)
                                dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
                        else {
@@ -1592,11 +1753,10 @@ do_draw(struct displaylist *displaylist, int cancel)
        struct coord ca[max];
        struct attr attr;
 
-       fprintf(stderr,"d\n");
+       profile(0,NULL);
        while (!cancel) {
-               if (!displaylist->msh) {
+               if (!displaylist->msh) 
                        displaylist->msh=mapset_open(displaylist->ms);
-               }
                if (!displaylist->m) {
                        displaylist->m=mapset_next(displaylist->msh, 1);
                        if (!displaylist->m) {
@@ -1635,17 +1795,24 @@ do_draw(struct displaylist *displaylist, int cancel)
                displaylist->sel=NULL;
                displaylist->m=NULL;
        }
+       profile(1,"process_selection\n");
        event_remove_idle(displaylist->idle_ev);
        displaylist->idle_ev=NULL;
        displaylist->busy=0;
-       graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, 1);
+       graphics_process_selection(displaylist->dc.gra, displaylist);
+       profile(1,"draw\n");
+       if (! cancel) 
+               graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, 1);
        map_rect_destroy(displaylist->mr);
        map_selection_destroy(displaylist->sel);
        mapset_close(displaylist->msh);
        displaylist->mr=NULL;
        displaylist->sel=NULL;
        displaylist->m=NULL;
+       displaylist->msh=NULL;
+       profile(1,"callback\n");
        callback_call_1(displaylist->cb, cancel);
+       profile(0,"end\n");
 }
 
 /**
@@ -1654,13 +1821,12 @@ do_draw(struct displaylist *displaylist, int cancel)
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
+void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int flags)
 {
        int order=transform_get_order(trans);
-       struct point p;
        displaylist->dc.trans=trans;
-       p.x=0;
-       p.y=0;
+       displaylist->dc.gra=gra;
+       displaylist->dc.mindist=transform_get_scale(trans)/2;
        // FIXME find a better place to set the background color
        if (l) {
                graphics_gc_set_background(gra->gc[0], &l->color);
@@ -1668,35 +1834,34 @@ void graphics_displaylist_draw(struct graphics *gra, struct displaylist *display
                gra->default_font = g_strdup(l->font);
        }
        graphics_background_gc(gra, gra->gc[0]);
-       gra->meth.draw_mode(gra->priv, draw_mode_begin);
-       gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
+       gra->meth.draw_mode(gra->priv, (flags & 8)?draw_mode_begin_clear:draw_mode_begin);
+       if (!(flags & 2))
+               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);
        if (l) 
                xdisplay_draw(displaylist, gra, l, order+l->order_delta);
-       if (callback)
+       if (flags & 1)
                callback_list_call_attr_0(gra->cbl, attr_postdraw);
-       gra->meth.draw_mode(gra->priv, draw_mode_end);
+       if (!(flags & 4))
+               gra->meth.draw_mode(gra->priv, draw_mode_end);
 }
 
-/**
- * FIXME
- * @param <>
- * @returns <>
- * @author Martin Schaller (04/2008)
-*/
-void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l, int async, struct callback *cb)
+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 order=transform_get_order(trans);
 
        dbg(1,"enter");
-       if (async == 1 && displaylist->busy)
-               return;
+       if (displaylist->busy) {
+               if (async == 1)
+                       return;
+               do_draw(displaylist, 1);
+       }
        xdisplay_free(displaylist->dl);
        dbg(1,"order=%d\n", order);
 
        displaylist->dc.gra=gra;
-       displaylist->ms=mapsets->data;
+       displaylist->ms=mapset;
        displaylist->dc.trans=trans;
-       displaylist->workload=async ? 1000 : 0;
+       displaylist->workload=async ? 100 : 0;
        displaylist->cb=cb;
        if (l)
                order+=l->order_delta;
@@ -1705,12 +1870,32 @@ void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList
        displaylist->layout=l;
        if (async) {
                if (! displaylist->idle_cb)
-                       displaylist->idle_cb=callback_new_2(do_draw, displaylist, 0);
+                       displaylist->idle_cb=callback_new_2(callback_cast(do_draw), displaylist, 0);
                displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
-       } else
+       }
+}
+/**
+ * FIXME
+ * @param <>
+ * @returns <>
+ * @author Martin Schaller (04/2008)
+*/
+void graphics_draw(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb)
+{
+       graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb);
+       if (! async)
                do_draw(displaylist, 0);
 }
 
+int
+graphics_draw_cancel(struct graphics *gra, struct displaylist *displaylist)
+{
+       if (!displaylist->busy)
+               return 0;
+       do_draw(displaylist, 1);
+       return 1;
+}
+
 /**
  * FIXME
  * @param <>
@@ -1839,6 +2024,29 @@ static int within_dist_line(struct point *p, struct point *line_p0, struct point
        int c1,c2;
        struct point line_p;
 
+       if (line_p0->x < line_p1->x) {
+               if (p->x < line_p0->x - dist)
+                       return 0;
+               if (p->x > line_p1->x + dist)
+                       return 0;
+       } else {
+               if (p->x < line_p1->x - dist)
+                       return 0;
+               if (p->x > line_p0->x + dist)
+                       return 0;
+       }
+       if (line_p0->y < line_p1->y) {
+               if (p->y < line_p0->y - dist)
+                       return 0;
+               if (p->y > line_p1->y + dist)
+                       return 0;
+       } else {
+               if (p->y < line_p1->y - dist)
+                       return 0;
+               if (p->y > line_p0->y + dist)
+                       return 0;
+       }
+               
        vx=line_p1->x-line_p0->x;
        vy=line_p1->y-line_p0->y;
        wx=p->x-line_p0->x;
@@ -1916,3 +2124,101 @@ int graphics_displayitem_within_dist(struct displaylist *displaylist, struct dis
        }
        return within_dist_polygon(p, pa, count, dist);
 }
+
+
+static enum item_type
+graphics_selection_type(enum item_type type)
+{
+       if (type < type_line) 
+               return type_selected_point;
+       if (type < type_area) 
+               return type_selected_line;
+       return type_selected_area;
+       
+}
+
+
+static void
+graphics_process_selection_item(struct displaylist *dl, struct item *item)
+{
+       struct displayitem di,*di_res;
+       GHashTable *h;
+
+       di.item=*item;
+       di.label=NULL;
+       di.displayed=0;
+       di.count=0;
+       h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
+       if (h) {
+               di_res=g_hash_table_lookup(h, &di);
+               if (di_res) {
+                       di.item.type=graphics_selection_type(di.item.type);
+                       display_add(dl, &di.item, di_res->count, di_res->c, NULL);
+               }
+       }
+}
+
+void
+graphics_add_selection(struct graphics *gra, struct item *item, struct displaylist *dl)
+{
+       struct item *item_dup=g_new(struct item, 1);
+       *item_dup=*item;
+       gra->selection=g_list_append(gra->selection, item_dup);
+       if (dl)
+               graphics_process_selection_item(dl, item_dup);
+}
+
+void
+graphics_remove_selection(struct graphics *gra, struct item *item, struct displaylist *dl)
+{
+       GList *curr;
+       int found;
+
+       for (;;) {
+               curr=gra->selection;
+               found=0;
+               while (curr) {
+                       struct item *sitem=curr->data;
+                       if (item_is_equal(*item,*sitem)) {
+                               if (dl) {
+                                       struct displayitem di;
+                                       GHashTable *h;
+                                       di.item=*sitem;
+                                       di.label=NULL;
+                                       di.displayed=0;
+                                       di.count=0;
+                                       di.item.type=graphics_selection_type(di.item.type);
+                                       h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
+                                       if (h)
+                                               g_hash_table_remove(h, &di);
+                               }
+                               g_free(sitem);
+                               gra->selection=g_list_remove(gra->selection, curr->data);
+                               found=1;
+                               break;
+                       }
+               }
+               if (!found)
+                       return;
+       }
+}
+
+void
+graphics_clear_selection(struct graphics *gra, struct displaylist *dl)
+{
+       while (gra->selection) 
+               graphics_remove_selection(gra, (struct item *)gra->selection->data, dl);
+}
+
+static void
+graphics_process_selection(struct graphics *gra, struct displaylist *dl)
+{
+       GList *curr;
+
+       curr=gra->selection;
+       while (curr) {
+               struct item *item=curr->data;
+               graphics_process_selection_item(dl, item);
+               curr=g_list_next(curr);
+       }
+}