JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removed client title bar
[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                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
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                         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
113         if(!sel || !isvisible(sel)) {
114                 for(c = stack; c && !isvisible(c); c = c->snext);
115                 focus(c);
116         }
117         restack();
118 }
119
120 void
121 focusnext(Arg *arg) {
122         Client *c;
123    
124         if(!sel)
125                 return;
126         if(!(c = getnext(sel->next)))
127                 c = getnext(clients);
128         if(c) {
129                 focus(c);
130                 restack();
131         }
132 }
133
134 void
135 focusprev(Arg *arg) {
136         Client *c;
137
138         if(!sel)
139                 return;
140         if(!(c = getprev(sel->prev))) {
141                 for(c = clients; c && c->next; c = c->next);
142                 c = getprev(c);
143         }
144         if(c) {
145                 focus(c);
146                 restack();
147         }
148 }
149
150 void
151 incnmaster(Arg *arg) {
152         if((arrange == dofloat) || (nmaster + arg->i < 1) || (wah / (nmaster + arg->i) < bh))
153                 return;
154         nmaster += arg->i;
155         updatemodetext();
156         if(sel)
157                 arrange();
158         else
159                 drawstatus();
160 }
161
162 Bool
163 isvisible(Client *c) {
164         unsigned int i;
165
166         for(i = 0; i < ntags; i++)
167                 if(c->tags[i] && seltag[i])
168                         return True;
169         return False;
170 }
171
172 void
173 resizemaster(Arg *arg) {
174         if(arg->i == 0)
175                 master = MASTER;
176         else {
177                 if(master + arg->i > 950 || master + arg->i < 50)
178                         return;
179                 master += arg->i;
180         }
181         arrange();
182 }
183
184 void
185 restack(void) {
186         Client *c;
187         XEvent ev;
188
189         if(!sel) {
190                 drawstatus();
191                 return;
192         }
193         if(sel->isfloat || arrange == dofloat)
194                 XRaiseWindow(dpy, sel->win);
195         if(arrange != dofloat) {
196                 if(!sel->isfloat)
197                         XLowerWindow(dpy, sel->win);
198                 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
199                         if(c == sel)
200                                 continue;
201                         XLowerWindow(dpy, c->win);
202                 }
203         }
204         drawall();
205         XSync(dpy, False);
206         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
207 }
208
209 void
210 togglefloat(Arg *arg) {
211         if (!sel || arrange == dofloat)
212                 return;
213         sel->isfloat = !sel->isfloat;
214         arrange();
215 }
216
217 void
218 togglemode(Arg *arg) {
219         arrange = (arrange == dofloat) ? dotile : dofloat;
220         updatemodetext();
221         if(sel)
222                 arrange();
223         else
224                 drawstatus();
225 }
226
227 void
228 toggleview(Arg *arg) {
229         unsigned int i;
230
231         seltag[arg->i] = !seltag[arg->i];
232         for(i = 0; i < ntags && !seltag[i]; i++);
233         if(i == ntags)
234                 seltag[arg->i] = True; /* cannot toggle last view */
235         arrange();
236 }
237
238 void
239 updatemodetext() {
240         snprintf(mtext, sizeof mtext, arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, nmaster);
241         bmw = textw(mtext);
242 }
243
244 void
245 view(Arg *arg) {
246         unsigned int i;
247
248         for(i = 0; i < ntags; i++)
249                 seltag[i] = (arg->i == -1) ? True : False;
250         if(arg->i >= 0 && arg->i < ntags)
251                 seltag[arg->i] = True;
252         arrange();
253 }
254
255 void
256 zoom(Arg *arg) {
257         unsigned int n;
258         Client *c;
259
260         if(!sel)
261                 return;
262         if(sel->isfloat || (arrange == dofloat)) {
263                 togglemax(sel);
264                 return;
265         }
266         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
267                 n++;
268
269         if((c = sel) == nexttiled(clients))
270                 if(!(c = nexttiled(c->next)))
271                         return;
272         detach(c);
273         if(clients)
274                 clients->prev = c;
275         c->next = clients;
276         clients = c;
277         focus(c);
278         arrange();
279 }