Xinqi Bao's Git

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