JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied Gottox patch to simplify the resizing of col, instead of resizing the current...
[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 minclient(void) {
10         Client *c, *min;
11
12         if((clients && clients->isfloat) || arrange == dofloat)
13                 return clients; /* don't touch floating order */
14         for(min = c = clients; c; c = c->next)
15                 if(c->weight < min->weight)
16                         min = c;
17         return min;
18 }
19
20 static Client *
21 nexttiled(Client *c) {
22         for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
23         return c;
24 }
25
26 static void
27 reorder(void) {
28         Client *c, *newclients, *tail;
29
30         newclients = tail = NULL;
31         while((c = minclient())) {
32                 detach(c);
33                 if(tail) {
34                         c->prev = tail;
35                         tail->next = c;
36                         tail = c;
37                 }
38                 else
39                         tail = newclients = c;
40         }
41         clients = newclients;
42 }
43
44 static void
45 togglemax(Client *c) {
46         XEvent ev;
47                 
48         if(c->isfixed)
49                 return;
50
51         if((c->ismax = !c->ismax)) {
52                 c->rx = c->x; c->x = sx;
53                 c->ry = c->y; c->y = bh;
54                 c->rw = c->w; c->w = sw - 2 * BORDERPX;
55                 c->rh = c->h; c->h = sh - bh - 2 * BORDERPX;
56         }
57         else {
58                 c->x = c->rx;
59                 c->y = c->ry;
60                 c->w = c->rw;
61                 c->h = c->rh;
62         }
63         resize(c, True, TopLeft);
64         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
65 }
66
67 /* extern */
68
69 void (*arrange)(void) = DEFMODE;
70
71 void
72 detach(Client *c) {
73         if(c->prev)
74                 c->prev->next = c->next;
75         if(c->next)
76                 c->next->prev = c->prev;
77         if(c == clients)
78                 clients = c->next;
79         c->next = c->prev = NULL;
80 }
81
82 void
83 dofloat(void) {
84         Client *c;
85
86         for(c = clients; c; c = c->next) {
87                 if(isvisible(c)) {
88                         resize(c, True, TopLeft);
89                 }
90                 else
91                         ban(c);
92         }
93         if(!sel || !isvisible(sel)) {
94                 for(c = stack; c && !isvisible(c); c = c->snext);
95                 focus(c);
96         }
97         restack();
98 }
99
100 void
101 dotile(void) {
102         unsigned int i, n, mpx, stackw, stackh, th;
103         Client *c;
104
105         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
106                 n++;
107         mpx = (sw * master) / 1000;
108         stackw = sw - mpx;
109         stackh = sh - bh;
110         th = stackh;
111         if(n > 1)
112                 th /= (n - 1);
113
114         for(i = 0, c = clients; c; c = c->next)
115                 if(isvisible(c)) {
116                         if(c->isfloat) {
117                                 resize(c, True, TopLeft);
118                                 continue;
119                         }
120                         c->ismax = False;
121                         c->x = sx;
122                         c->y = sy + bh;
123                         if(n == 1) { /* only 1 window */
124                                 c->w = sw - 2 * BORDERPX;
125                                 c->h = sh - 2 * BORDERPX - bh;
126                         }
127                         else if(i == 0) { /* master window */
128                                 c->w = mpx - 2 * BORDERPX;
129                                 c->h = sh - bh - 2 * BORDERPX;
130                         }
131                         else {  /* tile window */
132                                 c->x += mpx;
133                                 c->w = stackw - 2 * BORDERPX;
134                                 if(th > bh) {
135                                         c->y = sy + (i - 1) * th + bh;
136                                         if(i + 1 == n)
137                                                 c->h = sh - c->y - 2 * BORDERPX;
138                                         else
139                                                 c->h = th - 2 * BORDERPX;
140                                 }
141                                 else /* fallback if th < bh */
142                                         c->h = stackh - 2 * BORDERPX;
143                         }
144                         resize(c, False, TopLeft);
145                         i++;
146                 }
147                 else
148                         ban(c);
149
150         if(!sel || !isvisible(sel)) {
151                 for(c = stack; c && !isvisible(c); c = c->snext);
152                 focus(c);
153         }
154         restack();
155 }
156
157 void
158 focusnext(Arg *arg) {
159         Client *c;
160    
161         if(!sel)
162                 return;
163         if(!(c = getnext(sel->next)))
164                 c = getnext(clients);
165         if(c) {
166                 focus(c);
167                 restack();
168         }
169 }
170
171 void
172 focusprev(Arg *arg) {
173         Client *c;
174
175         if(!sel)
176                 return;
177         if(!(c = getprev(sel->prev))) {
178                 for(c = clients; c && c->next; c = c->next);
179                 c = getprev(c);
180         }
181         if(c) {
182                 focus(c);
183                 restack();
184         }
185 }
186
187 Bool
188 isvisible(Client *c) {
189         unsigned int i;
190
191         for(i = 0; i < ntags; i++)
192                 if(c->tags[i] && seltag[i])
193                         return True;
194         return False;
195 }
196
197 void
198 resizecol(Arg *arg) {
199         if(master + arg->i > 950 || master + arg->i < 50)
200                 return;
201         master += arg->i;
202         arrange();
203 }
204
205 void
206 restack(void) {
207         Client *c;
208         XEvent ev;
209
210         if(!sel) {
211                 drawstatus();
212                 return;
213         }
214         if(sel->isfloat || arrange == dofloat) {
215                 XRaiseWindow(dpy, sel->win);
216                 XRaiseWindow(dpy, sel->twin);
217         }
218         if(arrange != dofloat) {
219                 if(!sel->isfloat) {
220                         XLowerWindow(dpy, sel->twin);
221                         XLowerWindow(dpy, sel->win);
222                 }
223                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
224                         if(c == sel)
225                                 continue;
226                         XLowerWindow(dpy, c->twin);
227                         XLowerWindow(dpy, c->win);
228                 }
229         }
230         drawall();
231         XSync(dpy, False);
232         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
233 }
234
235 void
236 togglemode(Arg *arg) {
237         arrange = (arrange == dofloat) ? dotile : dofloat;
238         if(sel)
239                 arrange();
240         else
241                 drawstatus();
242 }
243
244 void
245 toggleview(Arg *arg) {
246         unsigned int i;
247
248         seltag[arg->i] = !seltag[arg->i];
249         for(i = 0; i < ntags && !seltag[i]; i++);
250         if(i == ntags)
251                 seltag[arg->i] = True; /* cannot toggle last view */
252         reorder();
253         arrange();
254 }
255
256 void
257 view(Arg *arg) {
258         unsigned int i;
259
260         for(i = 0; i < ntags; i++)
261                 seltag[i] = False;
262         seltag[arg->i] = True;
263         reorder();
264         arrange();
265 }
266
267 void
268 viewall(Arg *arg) {
269         unsigned int i;
270
271         for(i = 0; i < ntags; i++)
272                 seltag[i] = True;
273         reorder();
274         arrange();
275 }
276
277 void
278 zoom(Arg *arg) {
279         unsigned int n;
280         Client *c;
281
282         if(!sel)
283                 return;
284         if(sel->isfloat || (arrange == dofloat)) {
285                 togglemax(sel);
286                 return;
287         }
288         for(n = 0, c = clients; c; c = c->next)
289                 if(isvisible(c) && !c->isfloat)
290                         n++;
291         if(n < 2 || (arrange == dofloat))
292                 return;
293         if((c = sel) == nexttiled(clients))
294                 if(!(c = nexttiled(c->next)))
295                         return;
296         detach(c);
297         if(clients)
298                 clients->prev = c;
299         c->next = clients;
300         clients = c;
301         focus(c);
302         arrange();
303 }