aewl

view view.c @ 423:6ba5dd429122

applied checking existance of >2 tiles patch (proposed by sander) to zoom and resizecol
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 05 Sep 2006 16:00:09 +0200
parents 44225ee80236
children 67f37b93ac73
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 /* extern */
43 void (*arrange)(Arg *) = DEFMODE;
45 void
46 detach(Client *c)
47 {
48 if(c->prev)
49 c->prev->next = c->next;
50 if(c->next)
51 c->next->prev = c->prev;
52 if(c == clients)
53 clients = c->next;
54 c->next = c->prev = NULL;
55 }
57 void
58 dofloat(Arg *arg)
59 {
60 Client *c;
62 maximized = False;
64 for(c = clients; c; c = c->next) {
65 if(isvisible(c)) {
66 resize(c, True, TopLeft);
67 }
68 else
69 ban(c);
70 }
71 if(!sel || !isvisible(sel))
72 focus(getnext(clients));
73 restack();
74 }
76 void
77 dotile(Arg *arg)
78 {
79 int h, i, n, w;
80 Client *c;
82 maximized = False;
84 w = sw - mw;
85 for(n = 0, c = clients; c; c = c->next)
86 if(isvisible(c) && !c->isfloat)
87 n++;
89 if(n > 1)
90 h = (sh - bh) / (n - 1);
91 else
92 h = sh - bh;
94 for(i = 0, c = clients; c; c = c->next) {
95 if(isvisible(c)) {
96 if(c->isfloat) {
97 resize(c, True, TopLeft);
98 continue;
99 }
100 if(n == 1) {
101 c->x = sx;
102 c->y = sy + bh;
103 c->w = sw - 2;
104 c->h = sh - 2 - bh;
105 }
106 else if(i == 0) {
107 c->x = sx;
108 c->y = sy + bh;
109 c->w = mw - 2;
110 c->h = sh - 2 - bh;
111 }
112 else if(h > bh) {
113 c->x = sx + mw;
114 c->y = sy + (i - 1) * h + bh;
115 c->w = w - 2;
116 if(i + 1 == n)
117 c->h = sh - c->y - 2;
118 else
119 c->h = h - 2;
120 }
121 else { /* fallback if h < bh */
122 c->x = sx + mw;
123 c->y = sy + bh;
124 c->w = w - 2;
125 c->h = sh - 2 - bh;
126 }
127 resize(c, False, TopLeft);
128 i++;
129 }
130 else
131 ban(c);
132 }
133 if(!sel || !isvisible(sel))
134 focus(getnext(clients));
135 restack();
136 }
138 void
139 focusnext(Arg *arg)
140 {
141 Client *c;
143 if(!sel)
144 return;
146 if(!(c = getnext(sel->next)))
147 c = getnext(clients);
148 if(c) {
149 focus(c);
150 restack();
151 }
152 }
154 void
155 focusprev(Arg *arg)
156 {
157 Client *c;
159 if(!sel)
160 return;
162 if(!(c = getprev(sel->prev))) {
163 for(c = clients; c && c->next; c = c->next);
164 c = getprev(c);
165 }
166 if(c) {
167 focus(c);
168 restack();
169 }
170 }
172 Bool
173 isvisible(Client *c)
174 {
175 unsigned int i;
177 for(i = 0; i < ntags; i++)
178 if(c->tags[i] && seltag[i])
179 return True;
180 return False;
181 }
183 void
184 resizecol(Arg *arg)
185 {
186 unsigned int n;
187 Client *c;
189 for(n = 0, c = clients; c; c = c->next)
190 if(isvisible(c) && !c->isfloat)
191 n++;
192 if(!sel || sel->isfloat || n < 2 || (arrange != dotile) || maximized)
193 return;
195 if(sel == getnext(clients)) {
196 if(mw + arg->i > sw - 100)
197 return;
198 mw += arg->i;
199 }
200 else {
201 if(mw - arg->i < 100)
202 return;
203 mw -= arg->i;
204 }
205 arrange(NULL);
206 }
208 void
209 restack()
210 {
211 static unsigned int nwins = 0;
212 static Window *wins = NULL;
213 unsigned int f, fi, m, mi, n;
214 Client *c;
215 XEvent ev;
217 for(f = 0, m = 0, c = clients; c; c = c->next)
218 if(isvisible(c)) {
219 if(c->isfloat || arrange == dofloat)
220 f++;
221 else
222 m++;
223 }
224 if(!(n = 2 * (f + m))) {
225 drawstatus();
226 return;
227 }
228 if(nwins < n) {
229 nwins = n;
230 wins = erealloc(wins, nwins * sizeof(Window));
231 }
233 fi = 0;
234 mi = 2 * f;
235 if(sel) {
236 if(sel->isfloat || arrange == dofloat) {
237 wins[fi++] = sel->twin;
238 wins[fi++] = sel->win;
239 }
240 else {
241 wins[mi++] = sel->twin;
242 wins[mi++] = sel->win;
243 }
244 }
245 for(c = clients; c; c = c->next)
246 if(isvisible(c) && c != sel) {
247 if(c->isfloat || arrange == dofloat) {
248 wins[fi++] = c->twin;
249 wins[fi++] = c->win;
250 }
251 else {
252 wins[mi++] = c->twin;
253 wins[mi++] = c->win;
254 }
255 }
256 XRestackWindows(dpy, wins, n);
257 drawall();
258 XSync(dpy, False);
259 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
260 }
262 void
263 togglemode(Arg *arg)
264 {
265 arrange = (arrange == dofloat) ? dotile : dofloat;
266 if(sel)
267 arrange(NULL);
268 else
269 drawstatus();
270 }
272 void
273 toggleview(Arg *arg)
274 {
275 unsigned int i;
277 seltag[arg->i] = !seltag[arg->i];
278 for(i = 0; i < ntags && !seltag[i]; i++);
279 if(i == ntags)
280 seltag[arg->i] = True; /* cannot toggle last view */
281 reorder();
282 arrange(NULL);
283 }
285 void
286 view(Arg *arg)
287 {
288 unsigned int i;
290 for(i = 0; i < ntags; i++)
291 seltag[i] = False;
292 seltag[arg->i] = True;
293 reorder();
294 arrange(NULL);
295 }
297 void
298 viewall(Arg *arg)
299 {
300 unsigned int i;
302 for(i = 0; i < ntags; i++)
303 seltag[i] = True;
304 reorder();
305 arrange(NULL);
306 }
308 void
309 zoom(Arg *arg)
310 {
311 unsigned int n;
312 Client *c;
314 for(n = 0, c = clients; c; c = c->next)
315 if(isvisible(c) && !c->isfloat)
316 n++;
317 if(!sel || sel->isfloat || n < 2 || (arrange != dotile) || maximized)
318 return;
320 if((c = sel) == getnext(clients))
321 if(!(c = getnext(c->next)))
322 return;
323 detach(c);
324 c->next = clients;
325 clients->prev = c;
326 clients = c;
327 focus(c);
328 arrange(NULL);
329 }