JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
take bar into account
[dwm.git] / tile.c
diff --git a/tile.c b/tile.c
index a9bc0f5..8269676 100644 (file)
--- a/tile.c
+++ b/tile.c
-/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
- * See LICENSE file for license details.
- */
-#include "dwm.h"
+/* See LICENSE file for copyright and license details. */
+int bx, by, bw, bh, blw, mx, my, mw, mh, tx, ty, tw, th, wx, wy, ww, wh;
 
-/* static */
+void setmfact(const char *arg);
+void tile(void);
+void tileresize(Client *c, int x, int y, int w, int h);
+void updatetilegeom(void);
 
-static void
-togglemax(Client *c) {
-       XEvent ev;
-               
-       if(c->isfixed)
-               return;
+void
+setmfact(const char *arg) {
+       double d;
 
-       if((c->ismax = !c->ismax)) {
-               c->rx = c->x;
-               c->ry = c->y;
-               c->rw = c->w;
-               c->rh = c->h;
-               resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
+       if(!arg || lt->arrange != tile)
+               return;
+       else {
+               d = strtod(arg, NULL);
+               if(arg[0] == '-' || arg[0] == '+')
+                       d += mfact;
+               if(d < 0.1 || d > 0.9)
+                       return;
+               mfact = d;
        }
-       else
-               resize(c, c->rx, c->ry, c->rw, c->rh, True);
-       while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
+       updatetilegeom();
+       arrange();
 }
 
-/* extern */
-
 void
-dotile(void) {
-       unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
+tile(void) {
+       int x, y, h, w;
+       unsigned int i, n;
        Client *c;
 
-       for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
-               n++;
-       /* window geoms */
-       mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
-       mw = (n > nmaster) ? (waw * master) / 1000 : waw;
-       th = (n > nmaster) ? wah / (n - nmaster) : 0;
-       tw = waw - mw;
+       for(n = 0, c = nextunfloating(clients); c; c = nextunfloating(c->next), n++);
+       if(n == 0)
+               return;
 
-       for(i = 0, c = clients; c; c = c->next)
-               if(isvisible(c)) {
-                       if(c->isbanned)
-                               XMoveWindow(dpy, c->win, c->x, c->y);
-                       c->isbanned = False;
-                       if(c->isfloat)
-                               continue;
-                       c->ismax = False;
-                       nx = wax;
-                       ny = way;
-                       if(i < nmaster) {
-                               ny += i * mh;
-                               nw = mw - 2 * BORDERPX;
-                               nh = mh - 2 * BORDERPX;
-                       }
-                       else {  /* tile window */
-                               nx += mw;
-                               nw = tw - 2 * BORDERPX;
-                               if(th > 2 * BORDERPX) {
-                                       ny += (i - nmaster) * th;
-                                       nh = th - 2 * BORDERPX;
-                               }
-                               else /* fallback if th <= 2 * BORDERPX */
-                                       nh = wah - 2 * BORDERPX;
-                       }
-                       resize(c, nx, ny, nw, nh, False);
-                       i++;
-               }
-               else {
-                       c->isbanned = True;
-                       XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
-               }
-       if(!sel || !isvisible(sel)) {
-               for(c = stack; c && !isvisible(c); c = c->snext);
-               focus(c);
+       /* master */
+       c = nextunfloating(clients);
+
+       if(n == 1)
+               tileresize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw);
+       else
+               tileresize(c, mx, my, mw - 2 * c->bw, mh - 2 * c->bw);
+
+       if(--n == 0)
+               return;
+
+       /* tile stack */
+       x = (tx > c->x + c->w) ? c->x + c->w + 2 * c->bw : tw;
+       y = ty;
+       w = (tx > c->x + c->w) ? wx + ww - x : tw;
+       h = th / n;
+       if(h < bh)
+               h = th;
+
+       for(i = 0, c = nextunfloating(c->next); c; c = nextunfloating(c->next), i++) {
+               if(i + 1 == n) /* remainder */
+                       tileresize(c, x, y, w - 2 * c->bw, (ty + th) - y - 2 * c->bw);
+               else
+                       tileresize(c, x, y, w - 2 * c->bw, h - 2 * c->bw);
+               if(h != th)
+                       y = c->y + c->h + 2 * c->bw;
        }
-       restack();
 }
 
 void
-incnmaster(Arg *arg) {
-       if((arrange == dofloat) || (nmaster + arg->i < 1)
-       || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
-               return;
-       nmaster += arg->i;
-       if(sel)
-               arrange();
-       else
-               drawstatus();
+tileresize(Client *c, int x, int y, int w, int h) {
+       resize(c, x, y, w, h, resizehints);
+       if(resizehints && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
+               /* client doesn't accept size constraints */
+               resize(c, x, y, w, h, False);
 }
 
 void
-resizemaster(Arg *arg) {
-       if(arrange != dotile)
-               return;
-       if(arg->i == 0)
-               master = MASTER;
-       else {
-               if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
-               || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
+zoom(const char *arg) {
+       Client *c = sel;
+
+       if(c == nextunfloating(clients))
+               if(!c || !(c = nextunfloating(c->next)))
                        return;
-               master += arg->i;
+       if(lt->arrange == tile && !sel->isfloating) {
+               detach(c);
+               attach(c);
+               focus(c);
        }
        arrange();
 }
 
 void
-zoom(Arg *arg) {
-       unsigned int n;
-       Client *c;
+updatetilegeom(void) {
+       /* master area geometry */
+       mx = wx;
+       my = wy;
+       mw = mfact * ww;
+       mh = wh;
 
-       if(!sel)
-               return;
-       if(sel->isfloat || (arrange == dofloat)) {
-               togglemax(sel);
-               return;
-       }
-       for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
-               n++;
-
-       if((c = sel) == nextmanaged(clients))
-               if(!(c = nextmanaged(c->next)))
-                       return;
-       detach(c);
-       if(clients)
-               clients->prev = c;
-       c->next = clients;
-       clients = c;
-       focus(c);
-       arrange();
+       /* tile area geometry */
+       tx = mx + mw;
+       ty = wy;
+       tw = ww - mw;
+       th = wh;
 }