Buffer text on window resize
[dvtm.git] / grid.c
blobd9f6a3054c2eebb0590d97fbe9e1fd4c4baa2638
1 static void
2 grid(void) {
3 unsigned int i, m, nm, n, nx, ny, nw, nh, aw, ah, cols, rows;
4 Client *c;
6 for(n = 0, m = 0, c = clients; c; c = c->next,n++)
7 if(c->minimized)
8 m++;
9 /* number of non minimized windows */
10 nm = n - m;
11 /* grid dimensions */
12 for(cols = 0; cols <= nm/2; cols++)
13 if(cols*cols >= nm)
14 break;
15 rows = (cols && (cols - 1) * cols >= nm) ? cols - 1 : cols;
16 /* window geoms (cell height/width) */
17 nh = (wah - m) / (rows ? rows : 1);
18 nw = waw / (cols ? cols : 1);
19 for(i = 0, c = clients; c; c = c->next,i++) {
20 if(!c->minimized){
21 /* if there are less clients in the last row than normal adjust the
22 * split rate to fill the empty space */
23 if(rows > 1 && i == (rows * cols) - cols && (nm - i) <= (nm % cols))
24 nw = waw / (nm - i);
25 nx = (i % cols) * nw + wax;
26 ny = (i / cols) * nh + way;
27 /* adjust height/width of last row/column's windows */
28 ah = (i >= cols * (rows -1)) ? wah - m - nh * rows : 0;
29 /* special case if there are less clients in the last row */
30 if(rows > 1 && i == nm - 1 && (nm - i) < (nm % cols))
31 /* (n % cols) == number of clients in the last row */
32 aw = waw - nw * (nm % cols);
33 else
34 aw = ((i + 1) % cols == 0) ? waw - nw * cols : 0;
35 if(i % cols){
36 mvvline(ny, nx, ACS_VLINE, nh + ah);
37 /* if we are on the first row, or on the last one and there are fewer clients
38 * than normal whose border does not match the line above, print a top tree char
39 * otherwise a plus sign. */
40 if(i <= cols || (i >= rows * cols - cols && nm % cols && (cols - (nm % cols)) % 2))
41 mvaddch(ny, nx, ACS_TTEE);
42 else
43 mvaddch(ny, nx, ACS_PLUS);
44 nx++, aw--;
46 } else {
47 if(i == nm){ /* first minimized client */
48 ny = way + wah - m;
49 nx = wax;
50 nw = waw;
51 nh = 1;
52 aw = 0;
53 ah = 0;
54 } else
55 ny++;
57 resize(c, nx, ny, nw + aw, nh + ah);