JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
allowing nmaster=0 (I think that's a straight idea)
[dwm.git] / view.c
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2  * See LICENSE file for license details.
3  */
4 #include "dwm.h"
5 #include <stdio.h>
6
7 /* static */
8
9 static Client *
10 nexttiled(Client *c) {
11         for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
12         return c;
13 }
14
15 static void
16 togglemax(Client *c) {
17         XEvent ev;
18                 
19         if(c->isfixed)
20                 return;
21
22         if((c->ismax = !c->ismax)) {
23                 c->rx = c->x; c->x = wax;
24                 c->ry = c->y; c->y = way;
25                 c->rw = c->w; c->w = waw - 2 * BORDERPX;
26                 c->rh = c->h; c->h = wah - 2 * BORDERPX;
27         }
28         else {
29                 c->x = c->rx;
30                 c->y = c->ry;
31                 c->w = c->rw;
32                 c->h = c->rh;
33         }
34         resize(c, True, TopLeft);
35         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
36 }
37
38 /* extern */
39
40 void (*arrange)(void) = DEFMODE;
41
42 void
43 detach(Client *c) {
44         if(c->prev)
45                 c->prev->next = c->next;
46         if(c->next)
47                 c->next->prev = c->prev;
48         if(c == clients)
49                 clients = c->next;
50         c->next = c->prev = NULL;
51 }
52
53 void
54 dofloat(void) {
55         Client *c;
56
57         for(c = clients; c; c = c->next) {
58                 if(isvisible(c)) {
59                         resize(c, True, TopLeft);
60                 }
61                 else
62                         ban(c);
63         }
64         if(!sel || !isvisible(sel)) {
65                 for(c = stack; c && !isvisible(c); c = c->snext);
66                 focus(c);
67         }
68         restack();
69 }
70
71 void
72 dotile(void) {
73         unsigned int i, n, mw, mh, tw, th;
74         Client *c;
75
76         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
77                 n++;
78
79         /* window geoms */
80         if(nmaster > 0) {
81                 mh = (n > nmaster) ?  wah / nmaster : wah / (n > 0 ? n : 1);
82                 mw = (n > nmaster) ? (waw * master) / 1000 : waw;
83         }
84         else
85                 mh = mw = 0;
86         th = (n > nmaster) ? wah / (n - nmaster) : 0;
87         tw = waw - mw;
88
89         for(i = 0, c = clients; c; c = c->next)
90                 if(isvisible(c)) {
91                         if(c->isfloat) {
92                                 resize(c, True, TopLeft);
93                                 continue;
94                         }
95                         c->ismax = False;
96                         c->x = wax;
97                         c->y = way;
98                         if((nmaster > 0) && (i < nmaster)) {
99                                 c->y += i * mh;
100                                 c->w = mw - 2 * BORDERPX;
101                                 c->h = mh - 2 * BORDERPX;
102                         }
103                         else {  /* tile window */
104                                 c->x += mw;
105                                 c->w = tw - 2 * BORDERPX;
106                                 if(th > bh) {
107                                         c->y += (i - nmaster) * th;
108                                         c->h = th - 2 * BORDERPX;
109                                 }
110                                 else /* fallback if th < bh */
111                                         c->h = wah - 2 * BORDERPX;
112                         }
113                         resize(c, False, TopLeft);
114                         i++;
115                 }
116                 else
117                         ban(c);
118
119         if(!sel || !isvisible(sel)) {
120                 for(c = stack; c && !isvisible(c); c = c->snext);
121                 focus(c);
122         }
123         restack();
124 }
125
126 void
127 focusnext(Arg *arg) {
128         Client *c;
129    
130         if(!sel)
131                 return;
132         if(!(c = getnext(sel->next)))
133                 c = getnext(clients);
134         if(c) {
135                 focus(c);
136                 restack();
137         }
138 }
139
140 void
141 focusprev(Arg *arg) {
142         Client *c;
143
144         if(!sel)
145                 return;
146         if(!(c = getprev(sel->prev))) {
147                 for(c = clients; c && c->next; c = c->next);
148                 c = getprev(c);
149         }
150         if(c) {
151                 focus(c);
152                 restack();
153         }
154 }
155
156 void
157 incnmaster(Arg *arg) {
158         if((arrange == dofloat)
159         || ((int)nmaster + arg->i < 0)
160         || (((int)nmaster + arg->i > 0) && (wah / (nmaster + arg->i) < bh)))
161                 return;
162         nmaster += arg->i;
163         updatemodetext();
164         if(sel)
165                 arrange();
166         else
167                 drawstatus();
168 }
169
170 Bool
171 isvisible(Client *c) {
172         unsigned int i;
173
174         for(i = 0; i < ntags; i++)
175                 if(c->tags[i] && seltag[i])
176                         return True;
177         return False;
178 }
179
180 void
181 resizemaster(Arg *arg) {
182         if(arg->i == 0)
183                 master = MASTER;
184         else {
185                 if(master + arg->i > 950 || master + arg->i < 50)
186                         return;
187                 master += arg->i;
188         }
189         arrange();
190 }
191
192 void
193 restack(void) {
194         Client *c;
195         XEvent ev;
196
197         if(!sel) {
198                 drawstatus();
199                 return;
200         }
201         if(sel->isfloat || arrange == dofloat) {
202                 XRaiseWindow(dpy, sel->win);
203                 XRaiseWindow(dpy, sel->twin);
204         }
205         if(arrange != dofloat) {
206                 if(!sel->isfloat) {
207                         XLowerWindow(dpy, sel->twin);
208                         XLowerWindow(dpy, sel->win);
209                 }
210                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
211                         if(c == sel)
212                                 continue;
213                         XLowerWindow(dpy, c->twin);
214                         XLowerWindow(dpy, c->win);
215                 }
216         }
217         drawall();
218         XSync(dpy, False);
219         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
220 }
221
222 void
223 togglefloat(Arg *arg) {
224         if (!sel || arrange == dofloat)
225                 return;
226         sel->isfloat = !sel->isfloat;
227         arrange();
228 }
229
230 void
231 togglemode(Arg *arg) {
232         arrange = (arrange == dofloat) ? dotile : dofloat;
233         updatemodetext();
234         if(sel)
235                 arrange();
236         else
237                 drawstatus();
238 }
239
240 void
241 toggleview(Arg *arg) {
242         unsigned int i;
243
244         seltag[arg->i] = !seltag[arg->i];
245         for(i = 0; i < ntags && !seltag[i]; i++);
246         if(i == ntags)
247                 seltag[arg->i] = True; /* cannot toggle last view */
248         arrange();
249 }
250
251 void
252 updatemodetext() {
253         snprintf(mtext, sizeof mtext, arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, nmaster);
254         bmw = textw(mtext);
255 }
256
257 void
258 view(Arg *arg) {
259         unsigned int i;
260
261         for(i = 0; i < ntags; i++)
262                 seltag[i] = (arg->i == -1) ? True : False;
263         if(arg->i >= 0 && arg->i < ntags)
264                 seltag[arg->i] = True;
265         arrange();
266 }
267
268 void
269 zoom(Arg *arg) {
270         unsigned int n;
271         Client *c;
272
273         if(!sel)
274                 return;
275         if(sel->isfloat || (arrange == dofloat)) {
276                 togglemax(sel);
277                 return;
278         }
279         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
280                 n++;
281
282         if((c = sel) == nexttiled(clients))
283                 if(!(c = nexttiled(c->next)))
284                         return;
285         detach(c);
286         if(clients)
287                 clients->prev = c;
288         c->next = clients;
289         clients = c;
290         focus(c);
291         arrange();
292 }