dwm-meillo
view view.c @ 436:b3659c3c5dab
sanders solution is convincing and elegant
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Wed, 06 Sep 2006 11:54:16 +0200 |
parents | a6b8994af164 |
children | 433a5c662f73 |
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
5 #include "dwm.h"
6 #include <stdio.h>
8 /* static */
10 static Client *
11 minclient()
12 {
13 Client *c, *min;
15 for(min = c = clients; c; c = c->next)
16 if(c->weight < min->weight)
17 min = c;
18 return min;
19 }
22 static void
23 reorder()
24 {
25 Client *c, *newclients, *tail;
27 newclients = tail = NULL;
28 while((c = minclient())) {
29 detach(c);
30 if(tail) {
31 c->prev = tail;
32 tail->next = c;
33 tail = c;
34 }
35 else
36 tail = newclients = c;
37 }
38 clients = newclients;
39 }
41 static Client *
42 nexttiled(Client *c)
43 {
44 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
45 return c;
46 }
48 /* extern */
50 void (*arrange)(Arg *) = DEFMODE;
52 void
53 detach(Client *c)
54 {
55 if(c->prev)
56 c->prev->next = c->next;
57 if(c->next)
58 c->next->prev = c->prev;
59 if(c == clients)
60 clients = c->next;
61 c->next = c->prev = NULL;
62 }
64 void
65 dofloat(Arg *arg)
66 {
67 Client *c;
69 maximized = False;
71 for(c = clients; c; c = c->next) {
72 if(isvisible(c)) {
73 resize(c, True, TopLeft);
74 }
75 else
76 ban(c);
77 }
78 if(!sel || !isvisible(sel))
79 focus(getnext(clients));
80 restack();
81 }
83 void
84 dotile(Arg *arg)
85 {
86 int h, i, n, w;
87 Client *c;
89 maximized = False;
91 w = sw - mw;
92 for(n = 0, c = clients; c; c = c->next)
93 if(isvisible(c) && !c->isfloat)
94 n++;
96 if(n > 1)
97 h = (sh - bh) / (n - 1);
98 else
99 h = sh - bh;
101 for(i = 0, c = clients; c; c = c->next) {
102 if(isvisible(c)) {
103 if(c->isfloat) {
104 resize(c, True, TopLeft);
105 continue;
106 }
107 if(n == 1) {
108 c->x = sx;
109 c->y = sy + bh;
110 c->w = sw - 2;
111 c->h = sh - 2 - bh;
112 }
113 else if(i == 0) {
114 c->x = sx;
115 c->y = sy + bh;
116 c->w = mw - 2;
117 c->h = sh - 2 - bh;
118 }
119 else if(h > bh) {
120 c->x = sx + mw;
121 c->y = sy + (i - 1) * h + bh;
122 c->w = w - 2;
123 if(i + 1 == n)
124 c->h = sh - c->y - 2;
125 else
126 c->h = h - 2;
127 }
128 else { /* fallback if h < bh */
129 c->x = sx + mw;
130 c->y = sy + bh;
131 c->w = w - 2;
132 c->h = sh - 2 - bh;
133 }
134 resize(c, False, TopLeft);
135 i++;
136 }
137 else
138 ban(c);
139 }
140 if(!sel || !isvisible(sel))
141 focus(getnext(clients));
142 restack();
143 }
145 void
146 focusnext(Arg *arg)
147 {
148 Client *c;
150 if(!sel)
151 return;
153 if(!(c = getnext(sel->next)))
154 c = getnext(clients);
155 if(c) {
156 focus(c);
157 restack();
158 }
159 }
161 void
162 focusprev(Arg *arg)
163 {
164 Client *c;
166 if(!sel)
167 return;
169 if(!(c = getprev(sel->prev))) {
170 for(c = clients; c && c->next; c = c->next);
171 c = getprev(c);
172 }
173 if(c) {
174 focus(c);
175 restack();
176 }
177 }
179 Bool
180 isvisible(Client *c)
181 {
182 unsigned int i;
184 for(i = 0; i < ntags; i++)
185 if(c->tags[i] && seltag[i])
186 return True;
187 return False;
188 }
190 void
191 resizecol(Arg *arg)
192 {
193 unsigned int n;
194 Client *c;
196 for(n = 0, c = clients; c; c = c->next)
197 if(isvisible(c) && !c->isfloat)
198 n++;
199 if(!sel || sel->isfloat || n < 2 || (arrange != dotile) || maximized)
200 return;
202 if(sel == getnext(clients)) {
203 if(mw + arg->i > sw - 100 || mw + arg->i < 100)
204 return;
205 mw += arg->i;
206 }
207 else {
208 if(mw - arg->i > sw - 100 || mw - arg->i < 100)
209 return;
210 mw -= arg->i;
211 }
212 arrange(NULL);
213 }
215 void
216 restack()
217 {
218 Client *c;
219 XEvent ev;
221 if(!sel)
222 return;
223 if(sel->isfloat || arrange == dofloat) {
224 XRaiseWindow(dpy, sel->win);
225 XRaiseWindow(dpy, sel->twin);
226 }
227 if(arrange != dofloat)
228 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
229 XLowerWindow(dpy, c->twin);
230 XLowerWindow(dpy, c->win);
231 }
232 drawall();
233 XSync(dpy, False);
234 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
235 }
237 void
238 togglemode(Arg *arg)
239 {
240 arrange = (arrange == dofloat) ? dotile : dofloat;
241 if(sel)
242 arrange(NULL);
243 else
244 drawstatus();
245 }
247 void
248 toggleview(Arg *arg)
249 {
250 unsigned int i;
252 seltag[arg->i] = !seltag[arg->i];
253 for(i = 0; i < ntags && !seltag[i]; i++);
254 if(i == ntags)
255 seltag[arg->i] = True; /* cannot toggle last view */
256 reorder();
257 arrange(NULL);
258 }
260 void
261 view(Arg *arg)
262 {
263 unsigned int i;
265 for(i = 0; i < ntags; i++)
266 seltag[i] = False;
267 seltag[arg->i] = True;
268 reorder();
269 arrange(NULL);
270 }
272 void
273 viewall(Arg *arg)
274 {
275 unsigned int i;
277 for(i = 0; i < ntags; i++)
278 seltag[i] = True;
279 reorder();
280 arrange(NULL);
281 }
283 void
284 zoom(Arg *arg)
285 {
286 unsigned int n;
287 Client *c;
289 for(n = 0, c = clients; c; c = c->next)
290 if(isvisible(c) && !c->isfloat)
291 n++;
292 if(!sel || sel->isfloat || n < 2 || (arrange != dotile) || maximized)
293 return;
295 if((c = sel) == nexttiled(clients))
296 if(!(c = nexttiled(c->next)))
297 return;
298 detach(c);
299 c->next = clients;
300 clients->prev = c;
301 clients = c;
302 focus(c);
303 arrange(NULL);
304 }