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