grid.c 1.6 KB

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