bugfix of bugfix (thx to Sander)
[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);
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);
60                 }
61                 else
62                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
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         /* window geoms */
79         mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
80         mw = (n > nmaster) ? (waw * master) / 1000 : waw;
81         th = (n > nmaster) ? wah / (n - nmaster) : 0;
82         tw = waw - mw;
83
84         for(i = 0, c = clients; c; c = c->next)
85                 if(isvisible(c)) {
86                         if(c->isfloat) {
87                                 resize(c, True);
88                                 continue;
89                         }
90                         c->ismax = False;
91                         c->x = wax;
92                         c->y = way;
93                         if(i < nmaster) {
94                                 c->y += i * mh;
95                                 c->w = mw - 2 * BORDERPX;
96                                 c->h = mh - 2 * BORDERPX;
97                         }
98                         else {  /* tile window */
99                                 c->x += mw;
100                                 c->w = tw - 2 * BORDERPX;
101                                 if(th > 2 * BORDERPX) {
102                                         c->y += (i - nmaster) * th;
103                                         c->h = th - 2 * BORDERPX;
104                                 }
105                                 else /* fallback if th <= 2 * BORDERPX */
106                                         c->h = wah - 2 * BORDERPX;
107                         }
108                         resize(c, False);
109                         i++;
110                 }
111                 else
112                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
113         if(!sel || !isvisible(sel)) {
114                 for(c = stack; c && !isvisible(c); c = c->snext);
115                 focus(c);
116         }
117         restack();
118 }
119
120 void
121 focusnext(Arg *arg) {
122         Client *c;
123    
124         if(!sel)
125                 return;
126         if(!(c = getnext(sel->next)))
127                 c = getnext(clients);
128         if(c) {
129                 focus(c);
130                 restack();
131         }
132 }
133
134 void
135 focusprev(Arg *arg) {
136         Client *c;
137
138         if(!sel)
139                 return;
140         if(!(c = getprev(sel->prev))) {
141                 for(c = clients; c && c->next; c = c->next);
142                 c = getprev(c);
143         }
144         if(c) {
145                 focus(c);
146                 restack();
147         }
148 }
149
150 void
151 incnmaster(Arg *arg) {
152         if((arrange == dofloat) || (nmaster + arg->i < 1)
153                 || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
154                 return;
155         nmaster += arg->i;
156         if(sel)
157                 arrange();
158         else
159                 drawstatus();
160 }
161
162 Bool
163 isvisible(Client *c) {
164         unsigned int i;
165
166         for(i = 0; i < ntags; i++)
167                 if(c->tags[i] && seltag[i])
168                         return True;
169         return False;
170 }
171
172 void
173 resizemaster(Arg *arg) {
174         if(arg->i == 0)
175                 master = MASTER;
176         else {
177                 if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
178                         || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
179                         return;
180                 master += arg->i;
181         }
182         arrange();
183 }
184
185 void
186 restack(void) {
187         Client *c;
188         XEvent ev;
189
190         drawstatus();
191         if(!sel)
192                 return;
193         if(sel->isfloat || arrange == dofloat)
194                 XRaiseWindow(dpy, sel->win);
195         if(arrange != dofloat) {
196                 if(!sel->isfloat)
197                         XLowerWindow(dpy, sel->win);
198                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
199                         if(c == sel)
200                                 continue;
201                         XLowerWindow(dpy, c->win);
202                 }
203         }
204         XSync(dpy, False);
205         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
206 }
207
208 void
209 togglefloat(Arg *arg) {
210         if (!sel || arrange == dofloat)
211                 return;
212         sel->isfloat = !sel->isfloat;
213         arrange();
214 }
215
216 void
217 togglemode(Arg *arg) {
218         arrange = (arrange == dofloat) ? dotile : dofloat;
219         if(sel)
220                 arrange();
221         else
222                 drawstatus();
223 }
224
225 void
226 toggleview(Arg *arg) {
227         unsigned int i;
228
229         seltag[arg->i] = !seltag[arg->i];
230         for(i = 0; i < ntags && !seltag[i]; i++);
231         if(i == ntags)
232                 seltag[arg->i] = True; /* cannot toggle last view */
233         arrange();
234 }
235
236 void
237 view(Arg *arg) {
238         unsigned int i;
239
240         for(i = 0; i < ntags; i++)
241                 seltag[i] = (arg->i == -1) ? True : False;
242         if(arg->i >= 0 && arg->i < ntags)
243                 seltag[arg->i] = True;
244         arrange();
245 }
246
247 void
248 zoom(Arg *arg) {
249         unsigned int n;
250         Client *c;
251
252         if(!sel)
253                 return;
254         if(sel->isfloat || (arrange == dofloat)) {
255                 togglemax(sel);
256                 return;
257         }
258         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
259                 n++;
260
261         if((c = sel) == nexttiled(clients))
262                 if(!(c = nexttiled(c->next)))
263                         return;
264         detach(c);
265         if(clients)
266                 clients->prev = c;
267         c->next = clients;
268         clients = c;
269         focus(c);
270         arrange();
271 }