JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
4e2ffb35e526701517cf2b109e8286b91699220f
[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, TopLeft);
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                         resize(c, True, TopLeft);
60                 }
61                 else
62                         ban(c);
63         }
64         if(!sel || !isvisible(sel)) {
65                 for(c = stack; c && !isvisible(c); c = c->snext);
66                 focus(c);
67         }
68         restack();
69 }
70
71 void
72 dotile(void) {
73         unsigned int i, n, mw, mh, tw, th;
74         Client *c;
75
76         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
77                 n++;
78         /* window geoms */
79         mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
80         mw = (n > nmaster) ? (waw * master) / 1000 : waw;
81         th = (n > nmaster) ? wah / (n - nmaster) : 0;
82         tw = waw - mw;
83
84         for(i = 0, c = clients; c; c = c->next)
85                 if(isvisible(c)) {
86                         if(c->isfloat) {
87                                 resize(c, True, TopLeft);
88                                 continue;
89                         }
90                         c->ismax = False;
91                         c->x = wax;
92                         c->y = way;
93                         if(i < nmaster) {
94                                 c->y += i * mh;
95                                 c->w = mw - 2 * BORDERPX;
96                                 c->h = mh - 2 * BORDERPX;
97                         }
98                         else {  /* tile window */
99                                 c->x += mw;
100                                 c->w = tw - 2 * BORDERPX;
101                                 if(th > bh) {
102                                         c->y += (i - nmaster) * 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 void
152 incnmaster(Arg *arg) {
153         if((arrange == dofloat) || (nmaster + arg->i < 1) || (wah / (nmaster + arg->i) < bh))
154                 return;
155         nmaster += arg->i;
156         updatemodetext();
157         if(sel)
158                 arrange();
159         else
160                 drawstatus();
161 }
162
163 Bool
164 isvisible(Client *c) {
165         unsigned int i;
166
167         for(i = 0; i < ntags; i++)
168                 if(c->tags[i] && seltag[i])
169                         return True;
170         return False;
171 }
172
173 void
174 resizemaster(Arg *arg) {
175         if(arg->i == 0)
176                 master = MASTER;
177         else {
178                 if(master + arg->i > 950 || master + arg->i < 50)
179                         return;
180                 master += arg->i;
181         }
182         arrange();
183 }
184
185 void
186 restack(void) {
187         Client *c;
188         XEvent ev;
189
190         if(!sel) {
191                 drawstatus();
192                 return;
193         }
194         if(sel->isfloat || arrange == dofloat) {
195                 XRaiseWindow(dpy, sel->win);
196                 XRaiseWindow(dpy, sel->twin);
197         }
198         if(arrange != dofloat) {
199                 if(!sel->isfloat) {
200                         XLowerWindow(dpy, sel->twin);
201                         XLowerWindow(dpy, sel->win);
202                 }
203                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
204                         if(c == sel)
205                                 continue;
206                         XLowerWindow(dpy, c->twin);
207                         XLowerWindow(dpy, c->win);
208                 }
209         }
210         drawall();
211         XSync(dpy, False);
212         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
213 }
214
215 void
216 togglefloat(Arg *arg) {
217         if (!sel || arrange == dofloat)
218                 return;
219         sel->isfloat = !sel->isfloat;
220         arrange();
221 }
222
223 void
224 togglemode(Arg *arg) {
225         arrange = (arrange == dofloat) ? dotile : dofloat;
226         updatemodetext();
227         if(sel)
228                 arrange();
229         else
230                 drawstatus();
231 }
232
233 void
234 toggleview(Arg *arg) {
235         unsigned int i;
236
237         seltag[arg->i] = !seltag[arg->i];
238         for(i = 0; i < ntags && !seltag[i]; i++);
239         if(i == ntags)
240                 seltag[arg->i] = True; /* cannot toggle last view */
241         arrange();
242 }
243
244 void
245 updatemodetext() {
246         snprintf(mtext, sizeof mtext, arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, nmaster);
247         bmw = textw(mtext);
248 }
249
250 void
251 view(Arg *arg) {
252         unsigned int i;
253
254         for(i = 0; i < ntags; i++)
255                 seltag[i] = (arg->i == -1) ? True : False;
256         if(arg->i >= 0 && arg->i < ntags)
257                 seltag[arg->i] = True;
258         arrange();
259 }
260
261 void
262 zoom(Arg *arg) {
263         unsigned int n;
264         Client *c;
265
266         if(!sel)
267                 return;
268         if(sel->isfloat || (arrange == dofloat)) {
269                 togglemax(sel);
270                 return;
271         }
272         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
273                 n++;
274
275         if((c = sel) == nexttiled(clients))
276                 if(!(c = nexttiled(c->next)))
277                         return;
278         detach(c);
279         if(clients)
280                 clients->prev = c;
281         c->next = clients;
282         clients = c;
283         focus(c);
284         arrange();
285 }