JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
implemented nmaster appearance in mode label (using %u)
[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         mw = (n > nmaster) ? (waw * master) / 1000 : waw;
80         mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
81         tw = waw - mw;
82         th = (n > nmaster) ? wah / (n - nmaster) : 0;
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         snprintf(mtext, sizeof mtext, arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, nmaster);
157         bmw = textw(mtext);
158         if(sel)
159                 arrange();
160         else
161                 drawstatus();
162 }
163
164 Bool
165 isvisible(Client *c) {
166         unsigned int i;
167
168         for(i = 0; i < ntags; i++)
169                 if(c->tags[i] && seltag[i])
170                         return True;
171         return False;
172 }
173
174 void
175 resizemaster(Arg *arg) {
176         if(arg->i == 0)
177                 master = MASTER;
178         else {
179                 if(master + arg->i > 950 || master + arg->i < 50)
180                         return;
181                 master += arg->i;
182         }
183         arrange();
184 }
185
186 void
187 restack(void) {
188         Client *c;
189         XEvent ev;
190
191         if(!sel) {
192                 drawstatus();
193                 return;
194         }
195         if(sel->isfloat || arrange == dofloat) {
196                 XRaiseWindow(dpy, sel->win);
197                 XRaiseWindow(dpy, sel->twin);
198         }
199         if(arrange != dofloat) {
200                 if(!sel->isfloat) {
201                         XLowerWindow(dpy, sel->twin);
202                         XLowerWindow(dpy, sel->win);
203                 }
204                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
205                         if(c == sel)
206                                 continue;
207                         XLowerWindow(dpy, c->twin);
208                         XLowerWindow(dpy, c->win);
209                 }
210         }
211         drawall();
212         XSync(dpy, False);
213         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
214 }
215
216 void
217 togglefloat(Arg *arg) {
218         if (!sel || arrange == dofloat)
219                 return;
220         sel->isfloat = !sel->isfloat;
221         arrange();
222 }
223
224 void
225 togglemode(Arg *arg) {
226         arrange = (arrange == dofloat) ? dotile : dofloat;
227         snprintf(mtext, sizeof mtext, arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, nmaster);
228         bmw = textw(mtext);
229         if(sel)
230                 arrange();
231         else
232                 drawstatus();
233 }
234
235 void
236 toggleview(Arg *arg) {
237         unsigned int i;
238
239         seltag[arg->i] = !seltag[arg->i];
240         for(i = 0; i < ntags && !seltag[i]; i++);
241         if(i == ntags)
242                 seltag[arg->i] = True; /* cannot toggle last view */
243         arrange();
244 }
245
246 void
247 view(Arg *arg) {
248         unsigned int i;
249
250         for(i = 0; i < ntags; i++)
251                 seltag[i] = (arg->i == -1) ? True : False;
252         if(arg->i >= 0 && arg->i < ntags)
253                 seltag[arg->i] = True;
254         arrange();
255 }
256
257 void
258 zoom(Arg *arg) {
259         unsigned int n;
260         Client *c;
261
262         if(!sel)
263                 return;
264         if(sel->isfloat || (arrange == dofloat)) {
265                 togglemax(sel);
266                 return;
267         }
268         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
269                 n++;
270
271         if((c = sel) == nexttiled(clients))
272                 if(!(c = nexttiled(c->next)))
273                         return;
274         detach(c);
275         if(clients)
276                 clients->prev = c;
277         c->next = clients;
278         clients = c;
279         focus(c);
280         arrange();
281 }