You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
4.4 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* (C)opyright MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. /* static */
  6. static Client *
  7. nexttiled(Client *c) {
  8. for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
  9. return c;
  10. }
  11. static void
  12. togglemax(Client *c) {
  13. XEvent ev;
  14. if(c->isfixed)
  15. return;
  16. if((c->ismax = !c->ismax)) {
  17. c->rx = c->x; c->x = wax;
  18. c->ry = c->y; c->y = way;
  19. c->rw = c->w; c->w = waw - 2 * BORDERPX;
  20. c->rh = c->h; c->h = wah - 2 * BORDERPX;
  21. }
  22. else {
  23. c->x = c->rx;
  24. c->y = c->ry;
  25. c->w = c->rw;
  26. c->h = c->rh;
  27. }
  28. resize(c, True, TopLeft);
  29. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  30. }
  31. /* extern */
  32. void (*arrange)(void) = DEFMODE;
  33. void
  34. detach(Client *c) {
  35. if(c->prev)
  36. c->prev->next = c->next;
  37. if(c->next)
  38. c->next->prev = c->prev;
  39. if(c == clients)
  40. clients = c->next;
  41. c->next = c->prev = NULL;
  42. }
  43. void
  44. dofloat(void) {
  45. Client *c;
  46. for(c = clients; c; c = c->next) {
  47. if(isvisible(c)) {
  48. resize(c, True, TopLeft);
  49. }
  50. else
  51. ban(c);
  52. }
  53. if(!sel || !isvisible(sel)) {
  54. for(c = stack; c && !isvisible(c); c = c->snext);
  55. focus(c);
  56. }
  57. restack();
  58. }
  59. void
  60. dotile(void) {
  61. unsigned int i, n, mpw, th;
  62. Client *c;
  63. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  64. n++;
  65. mpw = (waw * master) / 1000;
  66. for(i = 0, c = clients; c; c = c->next)
  67. if(isvisible(c)) {
  68. if(c->isfloat) {
  69. resize(c, True, TopLeft);
  70. continue;
  71. }
  72. c->ismax = False;
  73. c->x = wax;
  74. c->y = way;
  75. if(n == 1) { /* only 1 window */
  76. c->w = waw - 2 * BORDERPX;
  77. c->h = wah - 2 * BORDERPX;
  78. }
  79. else if(i == 0) { /* master window */
  80. c->w = mpw - 2 * BORDERPX;
  81. c->h = wah - 2 * BORDERPX;
  82. th = wah / (n - 1);
  83. }
  84. else { /* tile window */
  85. c->x += mpw;
  86. c->w = (waw - mpw) - 2 * BORDERPX;
  87. if(th > bh) {
  88. c->y += (i - 1) * th;
  89. c->h = th - 2 * BORDERPX;
  90. }
  91. else /* fallback if th < bh */
  92. c->h = wah - 2 * BORDERPX;
  93. }
  94. resize(c, False, TopLeft);
  95. i++;
  96. }
  97. else
  98. ban(c);
  99. if(!sel || !isvisible(sel)) {
  100. for(c = stack; c && !isvisible(c); c = c->snext);
  101. focus(c);
  102. }
  103. restack();
  104. }
  105. void
  106. focusnext(Arg *arg) {
  107. Client *c;
  108. if(!sel)
  109. return;
  110. if(!(c = getnext(sel->next)))
  111. c = getnext(clients);
  112. if(c) {
  113. focus(c);
  114. restack();
  115. }
  116. }
  117. void
  118. focusprev(Arg *arg) {
  119. Client *c;
  120. if(!sel)
  121. return;
  122. if(!(c = getprev(sel->prev))) {
  123. for(c = clients; c && c->next; c = c->next);
  124. c = getprev(c);
  125. }
  126. if(c) {
  127. focus(c);
  128. restack();
  129. }
  130. }
  131. Bool
  132. isvisible(Client *c) {
  133. unsigned int i;
  134. for(i = 0; i < ntags; i++)
  135. if(c->tags[i] && seltag[i])
  136. return True;
  137. return False;
  138. }
  139. void
  140. resizemaster(Arg *arg) {
  141. if(arg->i == 0)
  142. master = MASTER;
  143. else {
  144. if(master + arg->i > 950 || master + arg->i < 50)
  145. return;
  146. master += arg->i;
  147. }
  148. arrange();
  149. }
  150. void
  151. restack(void) {
  152. Client *c;
  153. XEvent ev;
  154. if(!sel) {
  155. drawstatus();
  156. return;
  157. }
  158. if(sel->isfloat || arrange == dofloat) {
  159. XRaiseWindow(dpy, sel->win);
  160. XRaiseWindow(dpy, sel->twin);
  161. }
  162. if(arrange != dofloat) {
  163. if(!sel->isfloat) {
  164. XLowerWindow(dpy, sel->twin);
  165. XLowerWindow(dpy, sel->win);
  166. }
  167. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  168. if(c == sel)
  169. continue;
  170. XLowerWindow(dpy, c->twin);
  171. XLowerWindow(dpy, c->win);
  172. }
  173. }
  174. drawall();
  175. XSync(dpy, False);
  176. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  177. }
  178. void
  179. togglefloat(Arg *arg) {
  180. if (!sel || arrange == dofloat)
  181. return;
  182. sel->isfloat = !sel->isfloat;
  183. arrange();
  184. }
  185. void
  186. togglemode(Arg *arg) {
  187. arrange = (arrange == dofloat) ? dotile : dofloat;
  188. if(sel)
  189. arrange();
  190. else
  191. drawstatus();
  192. }
  193. void
  194. toggleview(Arg *arg) {
  195. unsigned int i;
  196. seltag[arg->i] = !seltag[arg->i];
  197. for(i = 0; i < ntags && !seltag[i]; i++);
  198. if(i == ntags)
  199. seltag[arg->i] = True; /* cannot toggle last view */
  200. arrange();
  201. }
  202. void
  203. view(Arg *arg) {
  204. unsigned int i;
  205. for(i = 0; i < ntags; i++)
  206. seltag[i] = (arg->i == -1) ? True : False;
  207. if(arg->i >= 0 && arg->i < ntags)
  208. seltag[arg->i] = True;
  209. arrange();
  210. }
  211. void
  212. zoom(Arg *arg) {
  213. unsigned int n;
  214. Client *c;
  215. if(!sel)
  216. return;
  217. if(sel->isfloat || (arrange == dofloat)) {
  218. togglemax(sel);
  219. return;
  220. }
  221. for(n = 0, c = clients; c; c = c->next)
  222. if(isvisible(c) && !c->isfloat)
  223. n++;
  224. if(n < 2 || (arrange == dofloat))
  225. return;
  226. if((c = sel) == nexttiled(clients))
  227. if(!(c = nexttiled(c->next)))
  228. return;
  229. detach(c);
  230. if(clients)
  231. clients->prev = c;
  232. c->next = clients;
  233. clients = c;
  234. focus(c);
  235. arrange();
  236. }