Fix:Core:Correct parameter handling for color management
[navit-package] / navit / graphics.c
index 5eb04da..a2f3736 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,20 +62,84 @@ 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;
-       int ready;
        struct point_rect r;
+       int gamma,brightness,contrast;
+       int colormgmt;
+       GList *selection;
 };
 
+struct display_context
+{
+       struct graphics *gra;
+       struct element *e;
+       struct graphics_gc *gc;
+       struct graphics_image *img;
+       enum projection pro;
+       int mindist;
+       struct transformation *trans;
+       enum item_type type;
+       int maxlen;
+};
 
 struct displaylist {
        GHashTable *dl;
+       int busy;
+       int workload;
+       struct callback *cb;
+       struct layout *layout;
+       struct display_context dc;
+       int order;
+       struct mapset *ms;
+       struct mapset_handle *msh;
+       struct map *m;
+       int conv;
+       struct map_selection *sel;
+       struct map_rect *mr;
+       struct callback *idle_cb;
+       struct event_idle *idle_ev;
 };
 
 
+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);
+
+static int
+graphics_set_attr_do(struct graphics *gra, struct attr *attr)
+{
+       switch (attr->type) {
+       case attr_gamma:
+               gra->gamma=attr->u.num;
+               break;
+       case attr_brightness:
+               gra->brightness=attr->u.num;
+               break;
+       case attr_contrast:
+               gra->contrast=attr->u.num;
+               break;
+       default:
+               return 0;
+       }
+       gra->colormgmt=(gra->gamma != 65536 || gra->brightness != 0 || gra->contrast != 65536);
+       return 1;
+}
+
+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);
+       if (!ret)
+               ret=graphics_set_attr_do(gra, attr);
+        return ret != 0;
+}
+
 void
 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
 {
@@ -105,6 +170,13 @@ struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
        this_->cbl=callback_list_new();
        this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
        this_->attrs=attr_list_dup(attrs);
+       this_->brightness=0;
+       this_->contrast=65536;
+       this_->gamma=65536;
+       while (*attrs) {
+               graphics_set_attr_do(this_,*attrs);
+               attrs++;
+       }
        return this_;
 }
 
@@ -140,6 +212,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 <>
@@ -147,6 +240,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 });
@@ -157,7 +252,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");
 }
 
 /**
@@ -176,6 +270,11 @@ void graphics_add_callback(struct graphics *this_, struct callback *cb)
        callback_list_add(this_->cbl, cb);
 }
 
+void graphics_remove_callback(struct graphics *this_, struct callback *cb)
+{
+       callback_list_remove(this_->cbl, cb);
+}
+
 /**
  * FIXME
  * @param <>
@@ -201,7 +300,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; 
@@ -220,6 +319,7 @@ struct graphics_gc * graphics_gc_new(struct graphics *gra)
 
        this_=g_new0(struct graphics_gc,1);
        this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
+       this_->gra=gra;
        return this_;
 }
 
@@ -235,6 +335,39 @@ void graphics_gc_destroy(struct graphics_gc *gc)
        g_free(gc);
 }
 
+static void
+graphics_convert_color(struct graphics *gra, struct color *in, struct color *out)
+{
+       *out=*in;
+       if (gra->brightness) {
+               out->r+=gra->brightness;
+               out->g+=gra->brightness;
+               out->b+=gra->brightness;
+       }
+       if (gra->contrast != 65536) {
+               out->r=out->r*gra->contrast/65536;
+               out->g=out->g*gra->contrast/65536;
+               out->b=out->b*gra->contrast/65536;
+       }
+       if (out->r < 0)
+               out->r=0;
+       if (out->r > 65535)
+               out->r=65535;
+       if (out->g < 0)
+               out->g=0;
+       if (out->g > 65535)
+               out->g=65535;
+       if (out->b < 0)
+               out->b=0;
+       if (out->b > 65535)
+               out->b=65535;
+       if (gra->gamma != 65536) {
+               out->r=pow(out->r/65535.0,gra->gamma/65536.0)*65535.0;
+               out->g=pow(out->g/65535.0,gra->gamma/65536.0)*65535.0;
+               out->b=pow(out->b/65535.0,gra->gamma/65536.0)*65535.0;
+       }
+}
+
 /**
  * FIXME
  * @param <>
@@ -243,6 +376,11 @@ void graphics_gc_destroy(struct graphics_gc *gc)
 */
 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
 {
+       struct color cn;
+       if (gc->gra->colormgmt) {
+               graphics_convert_color(gc->gra, c, &cn);
+               c=&cn;
+       }
        gc->meth.gc_set_foreground(gc->priv, c);
 }
 
@@ -254,9 +392,27 @@ void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
 */
 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
 {
+       struct color cn;
+       if (gc->gra->colormgmt) {
+               graphics_convert_color(gc->gra, c, &cn);
+               c=&cn;
+       }
        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 <>
@@ -409,6 +565,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 <>
@@ -550,17 +728,9 @@ struct displayitem {
 */
 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
 {
-       GList *h, *l;
-       h=value;
-       l=h;
-       while (l) {
-               struct displayitem *di=l->data;
-               if (! di->displayed && di->item.type < type_line) 
-                       dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
-               g_free(l->data);
-               l=g_list_next(l);
-       }
-       g_list_free(h);
+       GHashTable *hash=value;
+       if (hash) 
+               g_hash_table_destroy(hash);
        return TRUE;
 }
 
@@ -575,6 +745,24 @@ static void xdisplay_free(GHashTable *display_list)
        g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
 }
 
+static guint
+displayitem_hash(gconstpointer key)
+{
+       const struct displayitem *di=key;
+       return (di->item.id_hi^di->item.id_lo^(GPOINTER_TO_INT(di->item.map)));
+}
+
+static gboolean
+displayitem_equal(gconstpointer a, gconstpointer b)
+{
+       const struct displayitem *dia=a;
+       const struct displayitem *dib=b;
+       if (item_is_equal(dia->item, dib->item))
+                return TRUE;
+        return FALSE;
+}
+
+
 /**
  * FIXME
  * @param <>
@@ -585,7 +773,7 @@ static void display_add(struct displaylist *displaylist, struct item *item, int
 {
        struct displayitem *di;
        int len;
-       GList *l;
+       GHashTable *h;
        char *p;
 
        len=sizeof(*di)+count*sizeof(*c);
@@ -606,9 +794,12 @@ static void display_add(struct displaylist *displaylist, struct item *item, int
        di->count=count;
        memcpy(di->c, c, count*sizeof(*c));
 
-       l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
-       l=g_list_prepend(l, di);
-       g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
+       h=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
+       if (! h) {
+               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, di);
 }
 
 
@@ -634,14 +825,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);
@@ -653,10 +844,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
@@ -729,24 +920,72 @@ intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, in
 }
 
 struct circle {
-       int x,y,fowler;
-} lw10[]={
-{0,10,0},
-{4,9,56},
-{7,7,128},
-{9,4,200},
-{10,0,256},
-{9,-4,312},
-{7,-7,384},
-{4,-9,456},
-{0,-10,512},
-{-4,-9,568},
-{-7,-7,640},
-{-9,-4,712},
-{-10,0,768},
-{-9,4,824},
-{-7,7,896},
-{-4,9,968},
+       short x,y,fowler;
+} circle64[]={
+{0,128,0},
+{13,127,13},
+{25,126,25},
+{37,122,38},
+{49,118,53},
+{60,113,67},
+{71,106,85},
+{81,99,104},
+{91,91,128},
+{99,81,152},
+{106,71,171},
+{113,60,189},
+{118,49,203},
+{122,37,218},
+{126,25,231},
+{127,13,243},
+{128,0,256},
+{127,-13,269},
+{126,-25,281},
+{122,-37,294},
+{118,-49,309},
+{113,-60,323},
+{106,-71,341},
+{99,-81,360},
+{91,-91,384},
+{81,-99,408},
+{71,-106,427},
+{60,-113,445},
+{49,-118,459},
+{37,-122,474},
+{25,-126,487},
+{13,-127,499},
+{0,-128,512},
+{-13,-127,525},
+{-25,-126,537},
+{-37,-122,550},
+{-49,-118,565},
+{-60,-113,579},
+{-71,-106,597},
+{-81,-99,616},
+{-91,-91,640},
+{-99,-81,664},
+{-106,-71,683},
+{-113,-60,701},
+{-118,-49,715},
+{-122,-37,730},
+{-126,-25,743},
+{-127,-13,755},
+{-128,0,768},
+{-127,13,781},
+{-126,25,793},
+{-122,37,806},
+{-118,49,821},
+{-113,60,835},
+{-106,71,853},
+{-99,81,872},
+{-91,91,896},
+{-81,99,920},
+{-71,106,939},
+{-60,113,957},
+{-49,118,971},
+{-37,122,986},
+{-25,126,999},
+{-13,127,1011},
 };
 
 static void
@@ -757,21 +996,34 @@ draw_circle(struct point *pnt, int diameter, int scale, int start, int len, stru
 #if 0
        dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
 #endif
+       int count=64;
        int end=start+len;
-       int i;
-       c=lw10;
+       int i,step;
+       c=circle64;
+       if (diameter > 128)
+               step=1;
+       else if (diameter > 64)
+               step=2;
+       else if (diameter > 24)
+               step=4;
+       else if (diameter > 8)
+               step=8;
+       else
+               step=16;
        if (len > 0) {
                while (start < 0) {
                        start+=1024;
                        end+=1024;
                }
                while (end > 0) {
-                       for (i = 0 ; i < 16 ; i++) {
-                               if (c[i].fowler > start && c[i].fowler < end) {
-                                       res[*pos].x=pnt->x+c[i].x*diameter/20;
-                                       res[*pos].y=pnt->y+c[i].y*diameter/20;
-                                       (*pos)+=dir;
-                               }
+                       i=0;
+                       while (i < count && c[i].fowler <= start)
+                               i+=step;
+                       while (i < count && c[i].fowler < end) {
+                               res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
+                               res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
+                               (*pos)+=dir;
+                               i+=step;
                        }
                        end-=1024;
                        start-=1024;
@@ -782,12 +1034,14 @@ draw_circle(struct point *pnt, int diameter, int scale, int start, int len, stru
                        end-=1024;
                }
                while (end < 1024) {
-                       for (i = 15 ; i >= 0 ; i--) {
-                                            if (c[i].fowler < start && c[i].fowler > end) {
-                                       res[*pos].x=pnt->x+c[i].x*diameter/20;
-                                       res[*pos].y=pnt->y+c[i].y*diameter/20;
-                                       (*pos)+=dir;
-                               }
+                       i=count-1;
+                       while (i >= 0 && c[i].fowler >= start)
+                               i-=step;
+                       while (i >= 0 && c[i].fowler > end) {
+                               res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
+                               res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
+                               (*pos)+=dir;
+                               i-=step;
                        }
                        start+=1024;
                        end+=1024;
@@ -835,16 +1089,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;
 }
@@ -857,6 +1120,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) {
@@ -880,27 +1144,41 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
 {
        int maxpoints=200;
        struct point res[maxpoints], pos, poso, neg, nego;
-       int i, j, dx, dy, l, dxo, dyo;
+       int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
        struct offset o,oo;
-       int fow, fowo, delta;
+       int fow=0, fowo=0, delta;
        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;
-#if 0
-               dbg(0,"p[%d of %d]=%d,%d wi=%d l=%d\n", i,count-1,pnt[i].x,pnt[i].y, wi, l);
-#endif
-               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;
@@ -913,9 +1191,6 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
                        state=3;
                else
                        state=1;
-#if 0
-               dbg(0,"state=%d npos=%d ppos=%d\n", state, npos, ppos);
-#endif
                switch (state) {
                case 1:
                       if (fowo != fow) {
@@ -949,16 +1224,9 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
                        draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
                        res[npos] = pos;
                        res[ppos++] = pos;
-#if 0
-                       dbg(0,"npos=%d ppos=%d\n", npos, ppos);
-#endif
                        dbg_assert(npos > 0);
                        dbg_assert(ppos < maxpoints);
                        gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
-#if 0
-                       for (j = npos ; j < ppos ; j++)
-                               dbg(0,"res[%d]=%d,%d\n", j-npos, res[j].x, res[j].y);
-#endif
                        if (state == 2)
                                break;
                        npos=maxpoints/2;
@@ -969,9 +1237,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;
@@ -980,8 +1251,6 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
        }
 }
 
-struct transformation *tg;
-enum projection pg;
 
 struct wpoint {
        int x,y,w;
@@ -1068,8 +1337,7 @@ graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, str
        struct point p[count+1];
        int w[count*step+1];
        struct wpoint p1,p2;
-       int i,code,out=0,codeo=0;
-       int dx,dy;
+       int i,code,out=0;
        int wmax;
        struct point_rect r=gra->r;
 
@@ -1086,11 +1354,6 @@ graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, str
        r.lu.y-=wmax;
        r.rl.x+=wmax;
        r.rl.y+=wmax;
-#if 0
-       for (i = 0 ; i < count ; i++) {
-               dbg(0,"in[%d]=%d,%d (%d)\n", i, pa[i].x, pa[i].y, width[i*step]);
-       }
-#endif
        for (i = 0 ; i < count ; i++) {
                if (i) {
                        p1.x=pa[i-1].x;
@@ -1101,34 +1364,19 @@ graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, str
                        p2.w=width[i*step];
                        /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
                        code=clip_line(&p1, &p2, &r);
-#if 0
-                       dbg(0,"code=%d\n", code);
-#endif
                        if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
-#if 0
-                               dbg(0,"start = %d,%d\n", p1.x, p1.y);
-#endif
                                p[out].x=p1.x;
                                p[out].y=p1.y;
                                w[out*step]=p1.w;
                                out++;
                        }
                        if (code) {
-#if 0
-                               dbg(0,"end = %d,%d\n", p2.x, p2.y);
-#endif
                                p[out].x=p2.x;
                                p[out].y=p2.y;
                                w[out*step]=p2.w;
                                out++;
                        }
                        if (i == count-1 || (code & 4)) {
-#if 0
-                               int j;
-                               for (j = 0 ; j < out ; j++) {
-                                       dbg(0,"out[%d]=%d,%d (%d)\n", j, p[j].x, p[j].y, w[j*step]);
-                               }
-#endif
                                if (out > 1) {
                                        if (poly) {     
                                                graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
@@ -1139,9 +1387,6 @@ graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, str
                        }
                }
        }
-#if 0
-       dbg(0,"done\n");
-#endif
 }
 
 static int
@@ -1156,6 +1401,8 @@ is_inside(struct point *p, struct point_rect *r, int edge)
                return p->y >= r->lu.y;
        case 3:
                return p->y <= r->rl.y;
+       default:
+               return 0;
        }
 }
 
@@ -1189,8 +1436,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
@@ -1234,158 +1481,225 @@ graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, stru
 }
 
 
+static void
+display_context_free(struct display_context *dc)
+{
+       if (dc->gc)
+               graphics_gc_destroy(dc->gc);
+       if (dc->img)
+               graphics_image_free(dc->gra, dc->img);
+       dc->gc=NULL;
+       dc->img=NULL;
+}
 
-/**
- * FIXME
- * @param <>
- * @returns <>
- * @author Martin Schaller (04/2008)
-*/
-static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
+static struct graphics_font *
+get_font(struct graphics *gra, int size)
 {
-       struct element *e;
-       GList *l,*ls,*es,*types;
-       enum item_type type;
-       struct graphics_gc *gc = NULL;
-       struct graphics_image *img=NULL;
+       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 int
+limit_count(struct coord *c, int count)
+{
+       int i;
+       for (i = 1 ; i < count ; i++) {
+               if (c[i].x == c[0].x && c[i].y == c[0].y)
+                       return i+1;
+       }
+       return count;
+}
+
+
+static void
+displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
+{
+       int width[dc->maxlen];
+       int i,count=di->count,mindist=dc->mindist;
+       struct point pa[dc->maxlen];
+       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];
-       struct point pa[16384];
-       int width[16384];
-       int count;
+       char *path;
 
-       es=itm->elements;
-       while (es) {
-               e=es->data;
-               types=itm->type;
-               while (types) {
-                       type=GPOINTER_TO_INT(types->data);
-                       ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
-                       l=ls;
-                       if (gc)
-                               graphics_gc_destroy(gc);
-                       if (img)
-                               graphics_image_free(gra, img);
-                       gc=NULL;
-                       img=NULL;
-                       while (l) {
-                               struct displayitem *di;
-                               di=l->data;
-                               di->displayed=1;
-                               if (! gc) {
-                                       gc=graphics_gc_new(gra);
-                                       gc->meth.gc_set_foreground(gc->priv, &e->color);
-                               }
-                               if (e->type == element_polyline) {
-                                       count=transform(tg, pg, di->c, pa, di->count, 1, e->u.polyline.width, width);
-                               }
-                               else
-                                       count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
-                               switch (e->type) {
-                               case element_polygon:
+       di->displayed=1;
+       if (! gc) {
+               gc=graphics_gc_new(gra);
+               graphics_gc_set_foreground(gc, &e->color);
+       }
+       if (item_type_is_area(dc->type) && (dc->e->type == element_polyline || dc->e->type == element_text))
+               count=limit_count(di->c, count);
+       if (dc->type == type_poly_water_tiled)
+               mindist=0;
+       if (dc->e->type == element_polyline) 
+               count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, e->u.polyline.width, width);
+       else
+               count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, 0, NULL);
+       switch (e->type) {
+       case element_polygon:
 #if 0
-                                       {
-                                               int i;
-                                               for (i = 0 ; i < count ; i++) {
-                                                       dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
-                                               }
-                                       }
-                                       dbg(0,"element_polygon count=%d\n",count);
+               {
+                       int i;
+                       for (i = 0 ; i < count ; i++) {
+                               dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
+                       }
+               }
+               dbg(0,"element_polygon count=%d\n",count);
 #endif
 #if 1
-                                       graphics_draw_polygon_clipped(gra, gc, pa, count);
+               graphics_draw_polygon_clipped(gra, gc, pa, count);
 #endif
-                                       break;
-                               case element_polyline:
+               break;
+       case element_polyline:
+#if 0
+               if (e->u.polyline.width > 1) {
+                       graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
+               } else {
+#else
+               {
 #if 0
-                                       if (e->u.polyline.width > 1) {
-                                               graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
-                                       } else {
+                        if (e->u.polyline.width > 1)
+                                    gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
 #else
-                                       {
-                                                if (e->u.polyline.width > 1)
-                                                            gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
+                       gc->meth.gc_set_linewidth(gc->priv, 1);
+#endif
 
-                                               
+                       
 #endif
-                                               if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
-                                                       graphics_gc_set_dashes(gc, e->u.polyline.width, 
-                                                                              e->u.polyline.offset,
-                                                                              e->u.polyline.dash_table,
-                                                                              e->u.polyline.dash_num);
+                       if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
+                               graphics_gc_set_dashes(gc, e->u.polyline.width, 
+                                                      e->u.polyline.offset,
+                                                      e->u.polyline.dash_table,
+                                                      e->u.polyline.dash_num);
 #if 0
-                                               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));
+                       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
-                                               graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
+                       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
-                                               }
+                       }
 #endif
-                                       }
-                                       break;
-                               case element_circle:
-                                       if (count) {
-                                               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, pa, e->u.circle.radius);
-                                               if (di->label && 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);
-                                               }
-                                       }
-                                       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);
-                                       }
-                                       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);
-                                                       img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
-                                                       if (! img)
-                                                               dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
-                                               }
-                                               if (img) {
-                                                       p.x=pa[0].x - img->hot.x;
-                                                       p.y=pa[0].y - img->hot.y;
-                                                       gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
-                                               }
-                                       }
-                                       break;
-                               case element_image:
-                                       dbg(1,"image: '%s'\n", di->label);
-                                       if (gra->meth.draw_image_warp)
-                                               gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
-                                       else
-                                               dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
-                                       break;
-                               case element_arrows:
-                                       display_draw_arrows(gra,gc,pa,count);
-                                       break;
-                               default:
-                                       printf("Unhandled element type %d\n", e->type);
-                               
-                               }
-                               l=g_list_next(l);
+               }
+               break;
+       case element_circle:
+               if (count) {
+                       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, 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 (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) {
+                       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) {
+                               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
+                                       dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
+                       }
+                       if (img) {
+                               p.x=pa[0].x - img->hot.x;
+                               p.y=pa[0].y - img->hot.y;
+                               gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
+                       }
+               }
+               break;
+       case element_image:
+               dbg(1,"image: '%s'\n", di->label);
+               if (gra->meth.draw_image_warp)
+                       gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
+               else
+                       dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
+               break;
+       case element_arrows:
+               display_draw_arrows(gra,gc,pa,count);
+               break;
+       default:
+               printf("Unhandled element type %d\n", e->type);
+       
+       }
+}
+/**
+ * FIXME
+ * @param <>
+ * @returns <>
+ * @author Martin Schaller (04/2008)
+*/
+static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *display_list, struct itemgra *itm)
+{
+       struct element *e;
+       GList *es,*types;
+       GHashTable *h;
+       struct display_context *dc=&display_list->dc;
+
+       es=itm->elements;
+       while (es) {
+               e=es->data;
+               dc->e=e;
+               types=itm->type;
+               while (types) {
+                       dc->type=GPOINTER_TO_INT(types->data);
+                       h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(dc->type));
+                       if (h) {
+                               g_hash_table_foreach(h, (GHFunc)displayitem_draw, dc);
+                               display_context_free(dc);
                        }
                        types=g_list_next(types);
                }
                es=g_list_next(es);
        }
-       if (gc)
-               graphics_gc_destroy(gc);
-       if (img)
-               graphics_image_free(gra, img);
 }
 
 void
@@ -1394,10 +1708,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;
@@ -1412,7 +1728,7 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
                        count=1;
                }
                gc=graphics_gc_new(gra);
-               gc->meth.gc_set_foreground(gc->priv, &e->color);
+               graphics_gc_set_foreground(gc, &e->color);
                switch (e->type) {
                case element_polyline:
                        if (e->u.polyline.width > 1) 
@@ -1424,24 +1740,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 {
@@ -1452,7 +1774,7 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
                        }
                        break;
                default:
-                       dbg(0,"dont know how to draw %d\n", e->type);
+                       dbg(0,"don't know how to draw %d\n", e->type);
                }
                graphics_gc_destroy(gc);
                es=g_list_next(es);
@@ -1465,7 +1787,7 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
+static void xdisplay_draw_layer(struct displaylist *display_list, struct graphics *gra, struct layer *lay, int order)
 {
        GList *itms;
        struct itemgra *itm;
@@ -1485,7 +1807,7 @@ static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra,
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
+static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra, struct layout *l, int order)
 {
        GList *lays;
        struct layer *lay;
@@ -1506,109 +1828,78 @@ static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct
 */
 extern void *route_selection;
 
-/**
- * FIXME
- * @param <>
- * @returns <>
- * @author Martin Schaller (04/2008)
-*/
-static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
+static void
+do_draw(struct displaylist *displaylist, int cancel, int flags)
 {
-       enum projection pro;
-       struct map_rect *mr;
        struct item *item;
-       int conv,count,max=16384;
+       int count,max=displaylist->dc.maxlen,workload=0;
        struct coord ca[max];
        struct attr attr;
-       struct map_selection *sel;
-       int num=0;
 
-       pro=map_projection(m);
-       conv=map_requires_conversion(m);
-       sel=transform_get_selection(t, pro, order);
-       tg=t;
-       pg=pro;
-#if 0
-       sel=NULL;
-#endif
-       if (route_selection)
-               mr=map_rect_new(m, route_selection);
-       else
-               mr=map_rect_new(m, sel);
-       if (! mr) {
-               map_selection_destroy(sel);
-               return;
-       }
-       while ((item=map_rect_get_item(mr))) {
-               num++;
-#if 0
-               if (num < 7599 || num > 7599)
-                       continue;
-#endif
-#if 0
-               if (item->id_hi != 0xb0031 || item->id_lo != 0x20c9aeea)
-                       continue;
-#endif
-               count=item_coord_get(item, ca, item->type < type_line ? 1: max);
-               if (item->type >= type_line && count < 2) {
-                       dbg(1,"poly from map has only %d points\n", count);
-                       continue;
-               }
-               if (item->type < type_line) {
-                       if (! map_selection_contains_point(sel, &ca[0])) {
-                               dbg(1,"point not visible\n");
-                               continue;
-                       }
-               } else if (item->type < type_area) {
-                       if (! map_selection_contains_polyline(sel, ca, count)) {
-                               dbg(1,"polyline not visible\n");
-                               continue;
-                       }
-               } else {
-                       if (! map_selection_contains_polygon(sel, ca, count)) {
-                               dbg(1,"polygon not visible\n");
-                               continue;
+       profile(0,NULL);
+       while (!cancel) {
+               if (!displaylist->msh) 
+                       displaylist->msh=mapset_open(displaylist->ms);
+               if (!displaylist->m) {
+                       displaylist->m=mapset_next(displaylist->msh, 1);
+                       if (!displaylist->m) {
+                               mapset_close(displaylist->msh);
+                               displaylist->msh=NULL;
+                               break;
                        }
+                       displaylist->dc.pro=map_projection(displaylist->m);
+                       displaylist->conv=map_requires_conversion(displaylist->m);
+                       displaylist->sel=transform_get_selection(displaylist->dc.trans, displaylist->dc.pro, displaylist->order);
+                       displaylist->mr=map_rect_new(displaylist->m, displaylist->sel);
                }
-               if (count == max) 
-                       dbg(0,"point count overflow\n", count);
-               if (item->type >= type_line && count < 2) {
-                       dbg(1,"poly from transform has only %d points\n", count);
-                       continue;
+               if (displaylist->mr) {
+                       while ((item=map_rect_get_item(displaylist->mr))) {
+                               count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, displaylist->sel);
+                               if (! count)
+                                       continue;
+                               if (count == max) {
+                                       dbg(0,"point count overflow %d for %s "ITEM_ID_FMT"\n", count,item_to_name(item->type),ITEM_ID_ARGS(*item));
+                                       displaylist->dc.maxlen=max*2;
+                               }
+                               if (!item_attr_get(item, attr_label, &attr))
+                                       attr.u.str=NULL;
+                               if (displaylist->conv && attr.u.str && attr.u.str[0]) {
+                                       char *str=map_convert_string(displaylist->m, attr.u.str);
+                                       display_add(displaylist, item, count, ca, str);
+                                       map_convert_free(str);
+                               } else
+                                       display_add(displaylist, item, count, ca, attr.u.str);
+                               workload++;
+                               if (workload == displaylist->workload)
+                                       return;
+                       }
+                       map_rect_destroy(displaylist->mr);
                }
-               if (!item_attr_get(item, attr_label, &attr))
-                       attr.u.str=NULL;
-               if (conv && attr.u.str && attr.u.str[0]) {
-                       char *str=map_convert_string(m, attr.u.str);
-                       display_add(displaylist, item, count, ca, str);
-                       map_convert_free(str);
-               } else
-                       display_add(displaylist, item, count, ca, attr.u.str);
-       }
-       map_rect_destroy(mr);
-       map_selection_destroy(sel);
-}
-
-/**
- * FIXME
- * @param <>
- * @returns <>
- * @author Martin Schaller (04/2008)
-*/
-static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
-{
-       struct mapset *ms;
-       struct map *m;
-       struct mapset_handle *h;
-
-       if (! mapsets)
-               return;
-       ms=mapsets->data;
-       h=mapset_open(ms);
-       while ((m=mapset_next(h, 1))) {
-               do_draw_map(displaylist, t, m, order);
+               map_selection_destroy(displaylist->sel);
+               displaylist->mr=NULL;
+               displaylist->sel=NULL;
+               displaylist->m=NULL;
        }
-       mapset_close(h);
+       profile(1,"process_selection\n");
+       event_remove_idle(displaylist->idle_ev);
+       displaylist->idle_ev=NULL;
+       callback_destroy(displaylist->idle_cb);
+       displaylist->idle_cb=NULL;
+       displaylist->busy=0;
+       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, flags);
+       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");
 }
 
 /**
@@ -1617,24 +1908,12 @@ static void do_draw(struct displaylist *displaylist, struct transformation *t, G
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-int graphics_ready(struct graphics *this_)
-{
-       return this_->ready;
-}
-
-/**
- * FIXME
- * @param <>
- * @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;
-       tg=trans;
-       p.x=0;
-       p.y=0;
+       displaylist->dc.trans=trans;
+       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);
@@ -1642,80 +1921,65 @@ 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->dl, gra, l, order+l->order_delta);
-       if (callback)
+               xdisplay_draw(displaylist, gra, l, order+l->order_delta);
+       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);
 }
 
-#if 0
-/**
- * FIXME
- * @param <>
- * @returns <>
- * @author Martin Schaller (04/2008)
-*/
-void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
+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)
 {
-       struct displaylist_handle *dlh;
-       struct displayitem *di;
-       int i;
+       int order=transform_get_order(trans);
 
-       dlh=graphics_displaylist_open(displaylist);
-       while ((di=graphics_displaylist_next(dlh))) {
-               for (i = 0 ; i < di->count ; i++) {
-                       di->pnt[i].x+=dx;
-                       di->pnt[i].y+=dy;
-               }
+       dbg(1,"enter");
+       if (displaylist->busy) {
+               if (async == 1)
+                       return;
+               do_draw(displaylist, 1, flags);
        }
-       graphics_displaylist_close(dlh);
-}
-#endif
+       xdisplay_free(displaylist->dl);
+       dbg(1,"order=%d\n", order);
 
+       displaylist->dc.gra=gra;
+       displaylist->ms=mapset;
+       displaylist->dc.trans=trans;
+       displaylist->workload=async ? 100 : 0;
+       displaylist->cb=cb;
+       if (l)
+               order+=l->order_delta;
+       displaylist->order=order;
+       displaylist->busy=1;
+       displaylist->layout=l;
+       if (async) {
+               if (! displaylist->idle_cb)
+                       displaylist->idle_cb=callback_new_3(callback_cast(do_draw), displaylist, 0, flags);
+               displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
+       } else
+               do_draw(displaylist, 0, flags);
+}
 /**
  * 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)
+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)
 {
-       int order=transform_get_order(trans);
-
-       dbg(1,"enter");
-
-#if 0
-       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));
-#endif
-       
-       xdisplay_free(displaylist->dl);
-       dbg(1,"order=%d\n", order);
-
+       graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb, flags);
+}
 
-#if 0
-       for (i = 0 ; i < data_window_type_end; i++) {
-               data_window_begin(co->data_window[i]);  
-       }
-#endif
-       profile(0,NULL);
-       if (l)
-               order+=l->order_delta;
-       if (mapsets)
-               do_draw(displaylist, trans, mapsets, order);
-//     profile(1,"do_draw");
-       graphics_displaylist_draw(gra, displaylist, trans, l, 1);
-       profile(1,"xdisplay_draw");
-       profile(0,"end");
-  
-#if 0
-       for (i = 0 ; i < data_window_type_end; i++) {
-               data_window_end(co->data_window[i]);    
-       }
-#endif
-       gra->ready=1;
+int
+graphics_draw_cancel(struct graphics *gra, struct displaylist *displaylist)
+{
+       if (!displaylist->busy)
+               return 0;
+       do_draw(displaylist, 1, 0);
+       return 1;
 }
 
 /**
@@ -1725,7 +1989,7 @@ void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList
  * @author Martin Schaller (04/2008)
 */
 struct displaylist_handle {
-       GList *hl_head,*hl,*l;
+       GList *hl_head,*hl,*l_head,*l;
 };
 
 /**
@@ -1739,7 +2003,14 @@ struct displaylist_handle * graphics_displaylist_open(struct displaylist *displa
        struct displaylist_handle *ret;
 
        ret=g_new0(struct displaylist_handle, 1);
+       if (!displaylist->dl)
+               return NULL;
        ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
+       if (!ret->hl) {
+               g_free(ret);
+               return NULL;
+       }
+       ret->l_head=ret->l=g_hash_to_list_keys(ret->hl->data);
 
        return ret;
 }
@@ -1753,11 +2024,14 @@ struct displaylist_handle * graphics_displaylist_open(struct displaylist *displa
 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
 {
        struct displayitem *ret;
+       if (!dlh)
+               return NULL;
        if (! dlh->l) {
+               dlh->hl=g_list_next(dlh->hl);
                if (!dlh->hl)
                        return NULL;
-               dlh->l=dlh->hl->data;
-               dlh->hl=g_list_next(dlh->hl);
+               g_list_free(dlh->l_head);
+               dlh->l_head=dlh->l=g_hash_to_list_keys(dlh->hl->data);
        }
        ret=dlh->l->data;
        dlh->l=g_list_next(dlh->l);
@@ -1772,8 +2046,11 @@ struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
 */
 void graphics_displaylist_close(struct displaylist_handle *dlh)
 {
-       g_list_free(dlh->hl_head);
-       g_free(dlh);
+       if (dlh) {
+               g_list_free(dlh->hl_head);
+               g_list_free(dlh->l_head);
+               g_free(dlh);
+       }
 }
 
 /**
@@ -1784,9 +2061,10 @@ void graphics_displaylist_close(struct displaylist_handle *dlh)
 */
 struct displaylist * graphics_displaylist_new(void)
 {
-       struct displaylist *ret=g_new(struct displaylist, 1);
+       struct displaylist *ret=g_new0(struct displaylist, 1);
 
        ret->dl=g_hash_table_new(NULL,NULL);
+       ret->dc.maxlen=16384;
 
        return ret;
 }
@@ -1802,6 +2080,12 @@ struct item * graphics_displayitem_get_item(struct displayitem *di)
        return &di->item;       
 }
 
+int
+graphics_displayitem_get_coord_count(struct displayitem *di)
+{
+       return di->count;
+}
+
 /**
  * FIXME
  * @param <>
@@ -1813,6 +2097,12 @@ char * graphics_displayitem_get_label(struct displayitem *di)
        return di->label;
 }
 
+int
+graphics_displayitem_get_displayed(struct displayitem *di)
+{
+       return di->displayed;
+}
+
 /**
  * FIXME
  * @param <>
@@ -1843,6 +2133,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;
@@ -1905,12 +2218,12 @@ static int within_dist_polygon(struct point *p, struct point *poly_pnt, int coun
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
+int graphics_displayitem_within_dist(struct displaylist *displaylist, struct displayitem *di, struct point *p, int dist)
 {
-       struct point pa[16384];
+       struct point pa[displaylist->dc.maxlen];
        int count;
 
-       count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
+       count=transform(displaylist->dc.trans, displaylist->dc.pro, di->c, pa, di->count, 1, 0, NULL);
        
        if (di->item.type < type_line) {
                return within_dist_point(p, &pa[0], dist);
@@ -1920,3 +2233,109 @@ int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, in
        }
        return within_dist_polygon(p, pa, count, dist);
 }
+
+
+static void
+graphics_process_selection_item(struct displaylist *dl, struct item *item)
+{
+       struct displayitem di,*di_res;
+       GHashTable *h;
+       int count,max=dl->dc.maxlen;
+       struct coord ca[max];
+       struct attr attr;
+       struct map_rect *mr;
+
+       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=(enum item_type)item->priv_data;
+                       display_add(dl, &di.item, di_res->count, di_res->c, NULL);
+                       return;
+               }
+       }
+       mr=map_rect_new(item->map, NULL);
+       item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
+       count=item_coord_get(item, ca, item->type < type_line ? 1: max);
+       if (!item_attr_get(item, attr_label, &attr))
+               attr.u.str=NULL;
+       if (dl->conv && attr.u.str && attr.u.str[0]) {
+               char *str=map_convert_string(item->map, attr.u.str);
+               display_add(dl, item, count, ca, str);
+               map_convert_free(str);
+       } else
+               display_add(dl, item, count, ca, attr.u.str);
+       map_rect_destroy(mr);
+}
+
+void
+graphics_add_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
+{
+       struct item *item_dup=g_new(struct item, 1);
+       *item_dup=*item;
+       item_dup->priv_data=(void *)type;
+       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, enum item_type type, 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=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) {
+               struct item *item=(struct item *)gra->selection->data;
+               graphics_remove_selection(gra, item, (enum item_type)item->priv_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);
+       }
+}