JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixing some minor issues
[dwm.git] / view.c
diff --git a/view.c b/view.c
index c11e349..9bd7584 100644 (file)
--- a/view.c
+++ b/view.c
@@ -1,4 +1,4 @@
-/* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
+/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  * See LICENSE file for license details.
  */
 #include "dwm.h"
@@ -11,6 +11,40 @@ nexttiled(Client *c) {
        return c;
 }
 
+static Bool
+ismaster(Client *c) {
+       Client *cl;
+       unsigned int i;
+
+       for(cl = nexttiled(clients), i = 0; cl && cl != c; cl = nexttiled(cl->next), i++);
+       return i < nmaster;
+}
+
+static void
+pop(Client *c) {
+       detach(c);
+       if(clients)
+               clients->prev = c;
+       c->next = clients;
+       clients = c;
+}
+
+static void
+swap(Client *c1, Client *c2) {
+       Client tmp = *c1;
+       Client *c1p = c1->prev;
+       Client *c1n = c1->next;
+       Client *c2p = c2->prev;
+       Client *c2n = c2->next;
+
+       *c1 = *c2;
+       *c2 = tmp;
+       c1->prev = c1p;
+       c1->next = c1n;
+       c2->prev = c2p;
+       c2->next = c2n;
+}
+
 static void
 togglemax(Client *c) {
        XEvent ev;
@@ -34,6 +68,15 @@ togglemax(Client *c) {
        while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
 }
 
+static Client *
+topofstack() {
+       Client *c;
+       unsigned int i;
+
+       for(c = nexttiled(clients), i = 0; c && i < nmaster; c = nexttiled(c->next), i++);
+       return (i < nmaster) ? NULL : c;
+}
+
 /* extern */
 
 void (*arrange)(void) = DEFMODE;
@@ -69,13 +112,16 @@ dofloat(void) {
 
 void
 dotile(void) {
-       unsigned int i, n, mpx, stackw, th;
+       unsigned int i, n, mw, mh, tw, th;
        Client *c;
 
        for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
                n++;
-       mpx = (waw * master) / 1000;
-       stackw = waw - mpx;
+       /* window geoms */
+       mw = (n > nmaster) ? (waw * master) / 1000 : waw;
+       mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
+       tw = waw - mw;
+       th = (n > nmaster) ? wah / (n - nmaster) : 0;
 
        for(i = 0, c = clients; c; c = c->next)
                if(isvisible(c)) {
@@ -86,20 +132,16 @@ dotile(void) {
                        c->ismax = False;
                        c->x = wax;
                        c->y = way;
-                       if(n == 1) { /* only 1 window */
-                               c->w = waw - 2 * BORDERPX;
-                               c->h = wah - 2 * BORDERPX;
-                       }
-                       else if(i == 0) { /* master window */
-                               c->w = mpx - 2 * BORDERPX;
-                               c->h = wah - 2 * BORDERPX;
-                               th = wah / (n - 1);
+                       if(i < nmaster) {
+                               c->y += i * mh;
+                               c->w = mw - 2 * BORDERPX;
+                               c->h = mh - 2 * BORDERPX;
                        }
                        else {  /* tile window */
-                               c->x += mpx;
-                               c->w = stackw - 2 * BORDERPX;
+                               c->x += mw;
+                               c->w = tw - 2 * BORDERPX;
                                if(th > bh) {
-                                       c->y += (i - 1) * th;
+                                       c->y += (i - nmaster) * th;
                                        c->h = th - 2 * BORDERPX;
                                }
                                else /* fallback if th < bh */
@@ -148,6 +190,14 @@ focusprev(Arg *arg) {
        }
 }
 
+void
+incnmaster(Arg *arg) {
+       if((nmaster + arg->i < 1) || (wah / (nmaster + arg->i) < bh))
+               return;
+       nmaster += arg->i;
+       arrange();
+}
+
 Bool
 isvisible(Client *c) {
        unsigned int i;
@@ -233,17 +283,9 @@ view(Arg *arg) {
        unsigned int i;
 
        for(i = 0; i < ntags; i++)
-               seltag[i] = False;
-       seltag[arg->i] = True;
-       arrange();
-}
-
-void
-viewall(Arg *arg) {
-       unsigned int i;
-
-       for(i = 0; i < ntags; i++)
-               seltag[i] = True;
+               seltag[i] = (arg->i == -1) ? True : False;
+       if(arg->i >= 0 && arg->i < ntags)
+               seltag[arg->i] = True;
        arrange();
 }
 
@@ -258,19 +300,23 @@ zoom(Arg *arg) {
                togglemax(sel);
                return;
        }
-       for(n = 0, c = clients; c; c = c->next)
-               if(isvisible(c) && !c->isfloat)
-                       n++;
-       if(n < 2 || (arrange == dofloat))
+       for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
+               n++;
+
+       c = sel;
+       if(arrange == dofloat)
                return;
-       if((c = sel) == nexttiled(clients))
-               if(!(c = nexttiled(c->next)))
+       else if(n <= nmaster)
+               pop(c);
+       else if(ismaster(sel)) {
+               if(!(c = topofstack()))
                        return;
-       detach(c);
-       if(clients)
-               clients->prev = c;
-       c->next = clients;
-       clients = c;
+               swap(c, sel);
+               c = sel;
+       }
+       else
+               pop(c);
+
        focus(c);
        arrange();
 }