dwm-meillo

annotate view.c @ 505:2c29d74b11dc

first step to a more flexible dotile() algorithm
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 29 Sep 2006 12:38:27 +0200
parents 0cefc169ff67
children 2824b5d0f0f0
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@487 10 minclient(void) {
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@487 28 reorder(void) {
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@502 52 c->rw = c->w; c->w = sw - 2 * BORDERPX;
arg@502 53 c->rh = c->h; c->h = sh - bh - 2 * BORDERPX;
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@505 68 Bool isvertical = VERTICALSTACK;
arg@505 69 StackPos stackpos = STACKPOS;
arg@327 70
arg@327 71 void
arg@461 72 detach(Client *c) {
arg@378 73 if(c->prev)
arg@378 74 c->prev->next = c->next;
arg@378 75 if(c->next)
arg@378 76 c->next->prev = c->prev;
arg@378 77 if(c == clients)
arg@378 78 clients = c->next;
arg@378 79 c->next = c->prev = NULL;
arg@378 80 }
arg@378 81
arg@378 82 void
arg@461 83 dofloat(Arg *arg) {
arg@402 84 Client *c;
arg@400 85
arg@327 86 for(c = clients; c; c = c->next) {
arg@327 87 if(isvisible(c)) {
arg@327 88 resize(c, True, TopLeft);
arg@327 89 }
arg@327 90 else
arg@327 91 ban(c);
arg@327 92 }
arg@446 93 if(!sel || !isvisible(sel)) {
arg@450 94 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 95 focus(c);
arg@446 96 }
arg@327 97 restack();
arg@327 98 }
arg@327 99
arg@504 100 /* This algorithm is based on a (M)aster area and a (S)tacking area.
arg@504 101 * It supports following arrangements:
arg@505 102 * MMMS MMMM SMMM
arg@505 103 * MMMS MMMM SMMM
arg@505 104 * MMMS SSSS SMMM
arg@504 105 */
arg@327 106 void
arg@461 107 dotile(Arg *arg) {
arg@327 108 int h, i, n, w;
arg@402 109 Client *c;
arg@400 110
arg@488 111 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
arg@488 112 n++;
arg@327 113
arg@505 114 if(isvertical) {
arg@505 115 if(stackpos == StackBottom) {
arg@505 116 w = sw;
arg@505 117 if(n > 1)
arg@505 118 h = (sh - bh) / (n - 1);
arg@505 119 else
arg@505 120 h = sh - bh;
arg@505 121 }
arg@505 122 else {
arg@505 123 w = sw - master;
arg@505 124 if(n > 1)
arg@505 125 h = (sh - bh) / (n - 1);
arg@505 126 else
arg@505 127 h = sh - bh;
arg@505 128 }
arg@505 129 }
arg@505 130 else { /* horizontal stack */
arg@505 131
arg@505 132 }
arg@327 133
arg@327 134 for(i = 0, c = clients; c; c = c->next) {
arg@327 135 if(isvisible(c)) {
arg@327 136 if(c->isfloat) {
arg@327 137 resize(c, True, TopLeft);
arg@327 138 continue;
arg@327 139 }
arg@488 140 c->ismax = False;
arg@327 141 if(n == 1) {
arg@327 142 c->x = sx;
arg@327 143 c->y = sy + bh;
arg@502 144 c->w = sw - 2 * BORDERPX;
arg@502 145 c->h = sh - 2 * BORDERPX - bh;
arg@327 146 }
arg@327 147 else if(i == 0) {
arg@327 148 c->x = sx;
arg@327 149 c->y = sy + bh;
arg@505 150 c->w = master - 2 * BORDERPX;
arg@502 151 c->h = sh - 2 * BORDERPX - bh;
arg@327 152 }
arg@327 153 else if(h > bh) {
arg@505 154 c->x = sx + master;
arg@327 155 c->y = sy + (i - 1) * h + bh;
arg@502 156 c->w = w - 2 * BORDERPX;
arg@327 157 if(i + 1 == n)
arg@502 158 c->h = sh - c->y - 2 * BORDERPX;
arg@327 159 else
arg@502 160 c->h = h - 2 * BORDERPX;
arg@327 161 }
arg@327 162 else { /* fallback if h < bh */
arg@505 163 c->x = sx + master;
arg@327 164 c->y = sy + bh;
arg@502 165 c->w = w - 2 * BORDERPX;
arg@502 166 c->h = sh - 2 * BORDERPX - bh;
arg@327 167 }
arg@327 168 resize(c, False, TopLeft);
arg@327 169 i++;
arg@327 170 }
arg@327 171 else
arg@327 172 ban(c);
arg@327 173 }
arg@446 174 if(!sel || !isvisible(sel)) {
arg@450 175 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 176 focus(c);
arg@446 177 }
arg@327 178 restack();
arg@327 179 }
arg@327 180
arg@327 181 void
arg@461 182 focusnext(Arg *arg) {
arg@327 183 Client *c;
arg@327 184
arg@327 185 if(!sel)
arg@327 186 return;
arg@327 187
arg@327 188 if(!(c = getnext(sel->next)))
arg@327 189 c = getnext(clients);
arg@327 190 if(c) {
arg@327 191 focus(c);
arg@327 192 restack();
arg@327 193 }
arg@327 194 }
arg@327 195
arg@327 196 void
arg@461 197 focusprev(Arg *arg) {
arg@327 198 Client *c;
arg@327 199
arg@327 200 if(!sel)
arg@327 201 return;
arg@327 202
arg@327 203 if(!(c = getprev(sel->prev))) {
arg@327 204 for(c = clients; c && c->next; c = c->next);
arg@327 205 c = getprev(c);
arg@327 206 }
arg@327 207 if(c) {
arg@327 208 focus(c);
arg@327 209 restack();
arg@327 210 }
arg@327 211 }
arg@327 212
arg@420 213 Bool
arg@461 214 isvisible(Client *c) {
arg@420 215 unsigned int i;
arg@420 216
arg@420 217 for(i = 0; i < ntags; i++)
arg@420 218 if(c->tags[i] && seltag[i])
arg@420 219 return True;
arg@420 220 return False;
arg@420 221 }
arg@420 222
arg@415 223 void
arg@461 224 resizecol(Arg *arg) {
arg@423 225 unsigned int n;
arg@423 226 Client *c;
arg@418 227
arg@430 228 for(n = 0, c = clients; c; c = c->next)
arg@430 229 if(isvisible(c) && !c->isfloat)
arg@423 230 n++;
arg@486 231 if(!sel || sel->isfloat || n < 2 || (arrange == dofloat))
arg@415 232 return;
arg@423 233
arg@415 234 if(sel == getnext(clients)) {
arg@505 235 if(master + arg->i > sw - 100 || master + arg->i < 100)
arg@415 236 return;
arg@505 237 master += arg->i;
arg@415 238 }
arg@415 239 else {
arg@505 240 if(master - arg->i > sw - 100 || master - arg->i < 100)
arg@415 241 return;
arg@505 242 master -= arg->i;
arg@415 243 }
arg@415 244 arrange(NULL);
arg@415 245 }
arg@415 246
arg@327 247 void
arg@487 248 restack(void) {
arg@327 249 Client *c;
arg@327 250 XEvent ev;
arg@481 251
arg@437 252 if(!sel) {
arg@437 253 drawstatus();
arg@436 254 return;
arg@437 255 }
arg@436 256 if(sel->isfloat || arrange == dofloat) {
arg@436 257 XRaiseWindow(dpy, sel->win);
arg@436 258 XRaiseWindow(dpy, sel->twin);
arg@436 259 }
arg@446 260 if(arrange != dofloat)
arg@436 261 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
arg@436 262 XLowerWindow(dpy, c->twin);
arg@436 263 XLowerWindow(dpy, c->win);
arg@327 264 }
arg@327 265 drawall();
arg@327 266 XSync(dpy, False);
arg@327 267 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@327 268 }
arg@327 269
arg@327 270 void
arg@461 271 togglemode(Arg *arg) {
arg@333 272 arrange = (arrange == dofloat) ? dotile : dofloat;
arg@327 273 if(sel)
arg@327 274 arrange(NULL);
arg@327 275 else
arg@327 276 drawstatus();
arg@327 277 }
arg@327 278
arg@327 279 void
arg@461 280 toggleview(Arg *arg) {
arg@327 281 unsigned int i;
arg@327 282
arg@327 283 seltag[arg->i] = !seltag[arg->i];
arg@327 284 for(i = 0; i < ntags && !seltag[i]; i++);
arg@327 285 if(i == ntags)
arg@327 286 seltag[arg->i] = True; /* cannot toggle last view */
arg@381 287 reorder();
arg@327 288 arrange(NULL);
arg@327 289 }
arg@327 290
arg@327 291 void
arg@461 292 view(Arg *arg) {
arg@327 293 unsigned int i;
arg@327 294
arg@327 295 for(i = 0; i < ntags; i++)
arg@327 296 seltag[i] = False;
arg@327 297 seltag[arg->i] = True;
arg@381 298 reorder();
arg@327 299 arrange(NULL);
arg@327 300 }
arg@327 301
arg@327 302 void
arg@461 303 viewall(Arg *arg) {
arg@395 304 unsigned int i;
arg@395 305
arg@395 306 for(i = 0; i < ntags; i++)
arg@395 307 seltag[i] = True;
arg@397 308 reorder();
arg@395 309 arrange(NULL);
arg@395 310 }
arg@395 311
arg@395 312 void
arg@461 313 zoom(Arg *arg) {
arg@423 314 unsigned int n;
arg@423 315 Client *c;
arg@473 316
arg@473 317 if(!sel)
arg@473 318 return;
arg@473 319
arg@473 320 if(sel->isfloat || (arrange == dofloat)) {
arg@480 321 togglemax(sel);
arg@473 322 return;
arg@473 323 }
arg@327 324
arg@430 325 for(n = 0, c = clients; c; c = c->next)
arg@430 326 if(isvisible(c) && !c->isfloat)
arg@423 327 n++;
arg@486 328 if(n < 2 || (arrange == dofloat))
arg@327 329 return;
arg@327 330
arg@430 331 if((c = sel) == nexttiled(clients))
arg@433 332 if(!(c = nexttiled(c->next)))
arg@429 333 return;
arg@443 334 detach(c);
arg@443 335 if(clients)
arg@443 336 clients->prev = c;
arg@443 337 c->next = clients;
arg@443 338 clients = c;
arg@378 339 focus(c);
arg@327 340 arrange(NULL);
arg@327 341 }