dwm-meillo

annotate view.c @ 485:759c9d9a959d

applied Jukkas remark (dunno if this is correct though)
author arg@mmvi
date Fri, 22 Sep 2006 16:35:49 +0200
parents c035a0b42560
children 8d564b9e3cd4
rev   line source
arg@327 1 /*
arg@327 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
arg@327 3 * See LICENSE file for license details.
arg@327 4 */
arg@327 5 #include "dwm.h"
arg@327 6
arg@380 7 /* static */
arg@380 8
arg@382 9 static Client *
arg@461 10 minclient() {
arg@382 11 Client *c, *min;
arg@382 12
arg@443 13 if((clients && clients->isfloat) || arrange == dofloat)
arg@443 14 return clients; /* don't touch floating order */
arg@382 15 for(min = c = clients; c; c = c->next)
arg@382 16 if(c->weight < min->weight)
arg@382 17 min = c;
arg@382 18 return min;
arg@382 19 }
arg@382 20
arg@480 21 static Client *
arg@480 22 nexttiled(Client *c) {
arg@480 23 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
arg@480 24 return c;
arg@480 25 }
arg@480 26
arg@442 27 static void
arg@461 28 reorder() {
arg@382 29 Client *c, *newclients, *tail;
arg@380 30
arg@382 31 newclients = tail = NULL;
arg@382 32 while((c = minclient())) {
arg@381 33 detach(c);
arg@382 34 if(tail) {
arg@382 35 c->prev = tail;
arg@382 36 tail->next = c;
arg@382 37 tail = c;
arg@381 38 }
arg@381 39 else
arg@382 40 tail = newclients = c;
arg@380 41 }
arg@382 42 clients = newclients;
arg@380 43 }
arg@380 44
arg@480 45 static void
arg@480 46 togglemax(Client *c)
arg@480 47 {
arg@481 48 XEvent ev;
arg@480 49 if((c->ismax = !c->ismax)) {
arg@480 50 c->rx = c->x; c->x = sx;
arg@480 51 c->ry = c->y; c->y = bh;
arg@480 52 c->rw = c->w; c->w = sw;
arg@481 53 c->rh = c->h; c->h = sh - bh;
arg@480 54 }
arg@480 55 else {
arg@480 56 c->x = c->rx;
arg@480 57 c->y = c->ry;
arg@481 58 c->w = c->rw;
arg@481 59 c->h = c->rh;
arg@480 60 }
arg@480 61 resize(c, True, TopLeft);
arg@480 62 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@430 63 }
arg@430 64
arg@327 65 /* extern */
arg@327 66
arg@327 67 void (*arrange)(Arg *) = DEFMODE;
arg@327 68
arg@327 69 void
arg@461 70 detach(Client *c) {
arg@378 71 if(c->prev)
arg@378 72 c->prev->next = c->next;
arg@378 73 if(c->next)
arg@378 74 c->next->prev = c->prev;
arg@378 75 if(c == clients)
arg@378 76 clients = c->next;
arg@378 77 c->next = c->prev = NULL;
arg@378 78 }
arg@378 79
arg@378 80 void
arg@461 81 dofloat(Arg *arg) {
arg@402 82 Client *c;
arg@400 83
arg@327 84 for(c = clients; c; c = c->next) {
arg@327 85 if(isvisible(c)) {
arg@327 86 resize(c, True, TopLeft);
arg@327 87 }
arg@327 88 else
arg@327 89 ban(c);
arg@327 90 }
arg@446 91 if(!sel || !isvisible(sel)) {
arg@450 92 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 93 focus(c);
arg@446 94 }
arg@327 95 restack();
arg@327 96 }
arg@327 97
arg@327 98 void
arg@461 99 dotile(Arg *arg) {
arg@327 100 int h, i, n, w;
arg@402 101 Client *c;
arg@400 102
arg@327 103 w = sw - mw;
arg@430 104 for(n = 0, c = clients; c; c = c->next)
arg@484 105 if(isvisible(c) && !c->isfloat)
arg@484 106 n++;
arg@327 107
arg@327 108 if(n > 1)
arg@327 109 h = (sh - bh) / (n - 1);
arg@327 110 else
arg@327 111 h = sh - bh;
arg@327 112
arg@327 113 for(i = 0, c = clients; c; c = c->next) {
arg@327 114 if(isvisible(c)) {
arg@327 115 if(c->isfloat) {
arg@327 116 resize(c, True, TopLeft);
arg@327 117 continue;
arg@327 118 }
arg@485 119 if(c->ismax)
arg@485 120 togglemax(c);
arg@327 121 if(n == 1) {
arg@327 122 c->x = sx;
arg@327 123 c->y = sy + bh;
arg@327 124 c->w = sw - 2;
arg@327 125 c->h = sh - 2 - bh;
arg@327 126 }
arg@327 127 else if(i == 0) {
arg@327 128 c->x = sx;
arg@327 129 c->y = sy + bh;
arg@327 130 c->w = mw - 2;
arg@327 131 c->h = sh - 2 - bh;
arg@327 132 }
arg@327 133 else if(h > bh) {
arg@327 134 c->x = sx + mw;
arg@327 135 c->y = sy + (i - 1) * h + bh;
arg@327 136 c->w = w - 2;
arg@327 137 if(i + 1 == n)
arg@327 138 c->h = sh - c->y - 2;
arg@327 139 else
arg@327 140 c->h = h - 2;
arg@327 141 }
arg@327 142 else { /* fallback if h < bh */
arg@327 143 c->x = sx + mw;
arg@327 144 c->y = sy + bh;
arg@327 145 c->w = w - 2;
arg@327 146 c->h = sh - 2 - bh;
arg@327 147 }
arg@327 148 resize(c, False, TopLeft);
arg@327 149 i++;
arg@327 150 }
arg@327 151 else
arg@327 152 ban(c);
arg@327 153 }
arg@446 154 if(!sel || !isvisible(sel)) {
arg@450 155 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 156 focus(c);
arg@446 157 }
arg@327 158 restack();
arg@327 159 }
arg@327 160
arg@327 161 void
arg@461 162 focusnext(Arg *arg) {
arg@327 163 Client *c;
arg@327 164
arg@327 165 if(!sel)
arg@327 166 return;
arg@327 167
arg@327 168 if(!(c = getnext(sel->next)))
arg@327 169 c = getnext(clients);
arg@327 170 if(c) {
arg@327 171 focus(c);
arg@327 172 restack();
arg@327 173 }
arg@327 174 }
arg@327 175
arg@327 176 void
arg@461 177 focusprev(Arg *arg) {
arg@327 178 Client *c;
arg@327 179
arg@327 180 if(!sel)
arg@327 181 return;
arg@327 182
arg@327 183 if(!(c = getprev(sel->prev))) {
arg@327 184 for(c = clients; c && c->next; c = c->next);
arg@327 185 c = getprev(c);
arg@327 186 }
arg@327 187 if(c) {
arg@327 188 focus(c);
arg@327 189 restack();
arg@327 190 }
arg@327 191 }
arg@327 192
arg@420 193 Bool
arg@461 194 isvisible(Client *c) {
arg@420 195 unsigned int i;
arg@420 196
arg@420 197 for(i = 0; i < ntags; i++)
arg@420 198 if(c->tags[i] && seltag[i])
arg@420 199 return True;
arg@420 200 return False;
arg@420 201 }
arg@420 202
arg@415 203 void
arg@461 204 resizecol(Arg *arg) {
arg@423 205 unsigned int n;
arg@423 206 Client *c;
arg@418 207
arg@430 208 for(n = 0, c = clients; c; c = c->next)
arg@430 209 if(isvisible(c) && !c->isfloat)
arg@423 210 n++;
arg@473 211 if(!sel || sel->isfloat || n < 2 || (arrange != dotile))
arg@415 212 return;
arg@423 213
arg@415 214 if(sel == getnext(clients)) {
arg@425 215 if(mw + arg->i > sw - 100 || mw + arg->i < 100)
arg@415 216 return;
arg@415 217 mw += arg->i;
arg@415 218 }
arg@415 219 else {
arg@425 220 if(mw - arg->i > sw - 100 || mw - arg->i < 100)
arg@415 221 return;
arg@415 222 mw -= arg->i;
arg@415 223 }
arg@415 224 arrange(NULL);
arg@415 225 }
arg@415 226
arg@327 227 void
arg@461 228 restack() {
arg@327 229 Client *c;
arg@327 230 XEvent ev;
arg@481 231
arg@437 232 if(!sel) {
arg@437 233 drawstatus();
arg@436 234 return;
arg@437 235 }
arg@436 236 if(sel->isfloat || arrange == dofloat) {
arg@436 237 XRaiseWindow(dpy, sel->win);
arg@436 238 XRaiseWindow(dpy, sel->twin);
arg@436 239 }
arg@446 240 if(arrange != dofloat)
arg@436 241 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
arg@436 242 XLowerWindow(dpy, c->twin);
arg@436 243 XLowerWindow(dpy, c->win);
arg@327 244 }
arg@327 245 drawall();
arg@327 246 XSync(dpy, False);
arg@327 247 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@327 248 }
arg@327 249
arg@327 250 void
arg@461 251 togglemode(Arg *arg) {
arg@333 252 arrange = (arrange == dofloat) ? dotile : dofloat;
arg@327 253 if(sel)
arg@327 254 arrange(NULL);
arg@327 255 else
arg@327 256 drawstatus();
arg@327 257 }
arg@327 258
arg@327 259 void
arg@461 260 toggleview(Arg *arg) {
arg@327 261 unsigned int i;
arg@327 262
arg@327 263 seltag[arg->i] = !seltag[arg->i];
arg@327 264 for(i = 0; i < ntags && !seltag[i]; i++);
arg@327 265 if(i == ntags)
arg@327 266 seltag[arg->i] = True; /* cannot toggle last view */
arg@381 267 reorder();
arg@327 268 arrange(NULL);
arg@327 269 }
arg@327 270
arg@327 271 void
arg@461 272 view(Arg *arg) {
arg@327 273 unsigned int i;
arg@327 274
arg@327 275 for(i = 0; i < ntags; i++)
arg@327 276 seltag[i] = False;
arg@327 277 seltag[arg->i] = True;
arg@381 278 reorder();
arg@327 279 arrange(NULL);
arg@327 280 }
arg@327 281
arg@327 282 void
arg@461 283 viewall(Arg *arg) {
arg@395 284 unsigned int i;
arg@395 285
arg@395 286 for(i = 0; i < ntags; i++)
arg@395 287 seltag[i] = True;
arg@397 288 reorder();
arg@395 289 arrange(NULL);
arg@395 290 }
arg@395 291
arg@395 292 void
arg@461 293 zoom(Arg *arg) {
arg@423 294 unsigned int n;
arg@423 295 Client *c;
arg@473 296
arg@473 297 if(!sel)
arg@473 298 return;
arg@473 299
arg@473 300 if(sel->isfloat || (arrange == dofloat)) {
arg@480 301 togglemax(sel);
arg@473 302 return;
arg@473 303 }
arg@327 304
arg@430 305 for(n = 0, c = clients; c; c = c->next)
arg@430 306 if(isvisible(c) && !c->isfloat)
arg@423 307 n++;
arg@473 308 if(n < 2 || (arrange != dotile))
arg@327 309 return;
arg@327 310
arg@430 311 if((c = sel) == nexttiled(clients))
arg@433 312 if(!(c = nexttiled(c->next)))
arg@429 313 return;
arg@443 314 detach(c);
arg@443 315 if(clients)
arg@443 316 clients->prev = c;
arg@443 317 c->next = clients;
arg@443 318 clients = c;
arg@378 319 focus(c);
arg@327 320 arrange(NULL);
arg@327 321 }