static int tablewidth(Table *t, int tw, int sep) { Tablecell *c; int i, w, tmin, tmax, d; int *maxw, *minw; int totw; maxw = emalloc(sizeof(int)*t->ncol); minw = emalloc(sizeof(int)*t->ncol); for(c=t->cells; c!=nil; c=c->next){ if(dimenkind(c->wspec) != Dnone){ d = c->minw; c->minw = c->maxw = max(dimwidth(c->wspec, tw), c->minw); c->minw = d; } if(c->colspan != 1) continue; maxw[c->col] = max(maxw[c->col], c->maxw); minw[c->col] = max(minw[c->col], c->minw); } totw = 0; fixcols(t, maxw, sep, TRUE); tmax = getwidth(maxw, t->ncol); if(tmax <= tw){ d = 0; if(tw>tmax && dimenkind(t->width)!=Dnone && t->availw!=Tablemax) d = (tw-tmax)/t->ncol; for(i=0; i<t->ncol; i++){ t->cols[i].width = maxw[i] + d; totw += t->cols[i].width; } }else{ fixcols(t, minw, sep, FALSE); tmin = getwidth(minw, t->ncol); w = tw - tmin; d = tmax - tmin; for(i=0; i<t->ncol; i++){ if(w<=0 || d<=0) t->cols[i].width = minw[i]; else t->cols[i].width = minw[i] + (maxw[i] - minw[i])*w/d; totw += t->cols[i].width; } } free(minw); free(maxw); return totw; }
int dimwidth(Dimen d, int w) { int s, k; k = dimenkind(d); if(k == Dnone) return w; s = dimenspec(d); if(k == Dpixels) w = s; else if(k==Dpercent && s<100) w = s*w/100; return w; }
static void settable(Table *t) { Tablecell *c; Lay *lay; for(c=t->cells; c!=nil; c=c->next){ lay = layitems(c->content, Rect(0,0,0,0), FALSE); c->minw = Dx(lay->r); layfree(lay); if(dimenkind(c->wspec) == Dnone){ lay = layitems(c->content, Rect(0,0,Tablemax,0), FALSE); c->maxw = Dx(lay->r); layfree(lay); } } }
void frdims(Dimen *d, int n, int t, int **ret) { int totpix, totpcnt, totrel; double spix, spcnt, relu, vd; int tt, trest, totpixrel, minrelu, i; int *x, *spec, *kind; if(n == 1) { *ret = x = emalloc(sizeof(int)); x[0] = t; return; } totpix = totpcnt = totrel = 0; spec = emalloc(n*sizeof(int)); kind = emalloc(n*sizeof(int)); for(i=0; i<n; i++) { spec[i] = dimenspec(d[i]); if(spec[i] < 0) spec[i] = 0; kind[i] = dimenkind(d[i]); switch(kind[i]) { case Dpixels: totpix += spec[i]; break; case Dpercent: totpcnt += spec[i]; break; case Drelative: totrel += spec[i]; break; case Dnone: totrel++; break; } } spix = spcnt = 1.0; minrelu = 0; if(totrel > 0) minrelu = Scrollsize+Scrollgap; relu = (double)minrelu; tt = totpix + t*totpcnt/100 + totrel*minrelu; if(tt < t) { if(totrel == 0) { if(totpcnt != 0) spcnt = (double)((t-totpix)*100)/(double)(t*totpcnt); else spix = (double)t/(double)totpix; } else relu += (double)(t-tt)/(double)totrel; } else { totpixrel = totpix + totrel*minrelu; if(totpixrel < t) spcnt = (double)((t-totpixrel)*100)/(double)(t*totpcnt); else { trest = t - totrel*minrelu; if(trest > 0) spcnt = (double)trest/(double)(totpix + (t*totpcnt/100)); else { spcnt = (double)t/(double)tt; relu = 0.0; } spix = spcnt; } } x = emalloc(n * sizeof(int)); tt = 0; for(i=0; i<n-1; i++) { vd = (double)spec[i]; switch(kind[i]) { case Dpixels: vd = vd*spix; break; case Dpercent: vd = vd*(double)t*spcnt/100.0; break; case Drelative: vd = vd*relu; break; case Dnone: vd = relu; break; } x[i] = (int)(vd+.5); tt += x[i]; } x[n - 1] = t - tt; *ret = x; free(spec); free(kind); }