diff --git a/patch/gapplessgrid.c b/patch/gapplessgrid.c index 2757ac4..0330181 100644 --- a/patch/gapplessgrid.c +++ b/patch/gapplessgrid.c @@ -2,12 +2,12 @@ void gaplessgrid(Monitor *m) { - unsigned int n, cols, rows, cn, rn, i, cx, cy, cw, ch; + unsigned int i, n; + int x, y, cols, rows, ch, cw, cn, rn, rrest, crest; // counters int oh, ov, ih, iv; Client *c; getgaps(m, &oh, &ov, &ih, &iv, &n); - if (n == 0) return; @@ -18,21 +18,31 @@ gaplessgrid(Monitor *m) if (n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */ cols = 2; rows = n/cols; + cn = rn = 0; // reset column no, row no, client count + + ch = (m->wh - 2*oh - ih * (rows - 1)) / rows; + cw = (m->ww - 2*ov - iv * (cols - 1)) / cols; + rrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows; + crest = (m->ww - 2*ov - iv * (cols - 1)) - cw * cols; + x = m->wx + ov; + y = m->wy + oh; - /* window geometries */ - cw = cols ? (m->ww - 2*ov - iv*(cols - 1)) / cols : m->ww - 2*ov; - cn = 0; /* current column number */ - rn = 0; /* current row number */ for (i = 0, c = nexttiled(m->clients); c; i++, c = nexttiled(c->next)) { - if (i/rows + 1 > cols - n%cols) + if (i/rows + 1 > cols - n%cols) { rows = n/cols + 1; - ch = rows ? (m->wh - 2*oh - ih*(rows - 1)) / rows : m->wh - 2*oh; - cx = m->wx + ov + cn*(cw + iv); - cy = m->wy + oh + rn*(ch + ih); - resize(c, cx, cy, cw - 2*c->bw, ch - 2*c->bw, False); + ch = (m->wh - 2*oh - ih * (rows - 1)) / rows; + rrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows; + } + resize(c, + x, + y + rn*(ch + ih) + MIN(rn, rrest), + cw + (cn < crest ? 1 : 0) - 2*c->bw, + ch + (rn < rrest ? 1 : 0) - 2*c->bw, + 0); rn++; if (rn >= rows) { rn = 0; + x += cw + ih + (cn < crest ? 1 : 0); cn++; } } @@ -41,10 +51,11 @@ gaplessgrid(Monitor *m) void gaplessgrid(Monitor *m) { - unsigned int n, cols, rows, cn, rn, i, cx, cy, cw, ch; + unsigned int i, n; + int x, y, cols, rows, ch, cw, cn, rn, rrest, crest; // counters Client *c; - for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) ; + for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); if (n == 0) return; @@ -55,21 +66,31 @@ gaplessgrid(Monitor *m) if (n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */ cols = 2; rows = n/cols; + cn = rn = 0; // reset column no, row no, client count + + ch = m->wh / rows; + cw = m->ww / cols; + rrest = m->wh - ch * rows; + crest = m->ww - cw * cols; + x = m->wx; + y = m->wy; - /* window geometries */ - cw = cols ? m->ww / cols : m->ww; - cn = 0; /* current column number */ - rn = 0; /* current row number */ for (i = 0, c = nexttiled(m->clients); c; i++, c = nexttiled(c->next)) { - if (i/rows + 1 > cols - n%cols) + if (i/rows + 1 > cols - n%cols) { rows = n/cols + 1; - ch = rows ? m->wh / rows : m->wh; - cx = m->wx + cn*cw; - cy = m->wy + rn*ch; - resize(c, cx, cy, cw - 2 * c->bw, ch - 2 * c->bw, False); + ch = m->wh / rows; + rrest = m->wh - ch * rows; + } + resize(c, + x, + y + rn*ch + MIN(rn, rrest), + cw + (cn < crest ? 1 : 0) - 2*c->bw, + ch + (rn < rrest ? 1 : 0) - 2*c->bw, + 0); rn++; if (rn >= rows) { rn = 0; + x += cw + (cn < crest ? 1 : 0); cn++; } }