layout-set.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <string.h>
  19. #include "tmux.h"
  20. /*
  21. * Set window layouts - predefined methods to arrange windows. These are
  22. * one-off and generate a layout tree.
  23. */
  24. void layout_set_even_h(struct window *);
  25. void layout_set_even_v(struct window *);
  26. void layout_set_main_h(struct window *);
  27. void layout_set_main_v(struct window *);
  28. void layout_set_tiled(struct window *);
  29. const struct {
  30. const char *name;
  31. void (*arrange)(struct window *);
  32. } layout_sets[] = {
  33. { "even-horizontal", layout_set_even_h },
  34. { "even-vertical", layout_set_even_v },
  35. { "main-horizontal", layout_set_main_h },
  36. { "main-vertical", layout_set_main_v },
  37. { "tiled", layout_set_tiled },
  38. };
  39. int
  40. layout_set_lookup(const char *name)
  41. {
  42. u_int i;
  43. int matched = -1;
  44. for (i = 0; i < nitems(layout_sets); i++) {
  45. if (strncmp(layout_sets[i].name, name, strlen(name)) == 0) {
  46. if (matched != -1) /* ambiguous */
  47. return (-1);
  48. matched = i;
  49. }
  50. }
  51. return (matched);
  52. }
  53. u_int
  54. layout_set_select(struct window *w, u_int layout)
  55. {
  56. if (layout > nitems(layout_sets) - 1)
  57. layout = nitems(layout_sets) - 1;
  58. if (layout_sets[layout].arrange != NULL)
  59. layout_sets[layout].arrange(w);
  60. w->lastlayout = layout;
  61. return (layout);
  62. }
  63. u_int
  64. layout_set_next(struct window *w)
  65. {
  66. u_int layout;
  67. if (w->lastlayout == -1)
  68. layout = 0;
  69. else {
  70. layout = w->lastlayout + 1;
  71. if (layout > nitems(layout_sets) - 1)
  72. layout = 0;
  73. }
  74. if (layout_sets[layout].arrange != NULL)
  75. layout_sets[layout].arrange(w);
  76. w->lastlayout = layout;
  77. return (layout);
  78. }
  79. u_int
  80. layout_set_previous(struct window *w)
  81. {
  82. u_int layout;
  83. if (w->lastlayout == -1)
  84. layout = nitems(layout_sets) - 1;
  85. else {
  86. layout = w->lastlayout;
  87. if (layout == 0)
  88. layout = nitems(layout_sets) - 1;
  89. else
  90. layout--;
  91. }
  92. if (layout_sets[layout].arrange != NULL)
  93. layout_sets[layout].arrange(w);
  94. w->lastlayout = layout;
  95. return (layout);
  96. }
  97. void
  98. layout_set_even_h(struct window *w)
  99. {
  100. struct window_pane *wp;
  101. struct layout_cell *lc, *lcnew;
  102. u_int i, n, width, xoff;
  103. layout_print_cell(w->layout_root, __func__, 1);
  104. /* Get number of panes. */
  105. n = window_count_panes(w);
  106. if (n <= 1)
  107. return;
  108. /* How many can we fit? */
  109. width = (w->sx - (n - 1)) / n;
  110. if (width < PANE_MINIMUM)
  111. width = PANE_MINIMUM;
  112. /* Free the old root and construct a new. */
  113. layout_free(w);
  114. lc = w->layout_root = layout_create_cell(NULL);
  115. layout_set_size(lc, w->sx, w->sy, 0, 0);
  116. layout_make_node(lc, LAYOUT_LEFTRIGHT);
  117. /* Build new leaf cells. */
  118. i = xoff = 0;
  119. TAILQ_FOREACH(wp, &w->panes, entry) {
  120. /* Create child cell. */
  121. lcnew = layout_create_cell(lc);
  122. layout_set_size(lcnew, width, w->sy, xoff, 0);
  123. layout_make_leaf(lcnew, wp);
  124. TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
  125. i++;
  126. xoff += width + 1;
  127. }
  128. /* Allocate any remaining space. */
  129. if (w->sx > xoff - 1) {
  130. lc = TAILQ_LAST(&lc->cells, layout_cells);
  131. layout_resize_adjust(lc, LAYOUT_LEFTRIGHT, w->sx - (xoff - 1));
  132. }
  133. /* Fix cell offsets. */
  134. layout_fix_offsets(lc);
  135. layout_fix_panes(w, w->sx, w->sy);
  136. layout_print_cell(w->layout_root, __func__, 1);
  137. server_redraw_window(w);
  138. }
  139. void
  140. layout_set_even_v(struct window *w)
  141. {
  142. struct window_pane *wp;
  143. struct layout_cell *lc, *lcnew;
  144. u_int i, n, height, yoff;
  145. layout_print_cell(w->layout_root, __func__, 1);
  146. /* Get number of panes. */
  147. n = window_count_panes(w);
  148. if (n <= 1)
  149. return;
  150. /* How many can we fit? */
  151. height = (w->sy - (n - 1)) / n;
  152. if (height < PANE_MINIMUM)
  153. height = PANE_MINIMUM;
  154. /* Free the old root and construct a new. */
  155. layout_free(w);
  156. lc = w->layout_root = layout_create_cell(NULL);
  157. layout_set_size(lc, w->sx, w->sy, 0, 0);
  158. layout_make_node(lc, LAYOUT_TOPBOTTOM);
  159. /* Build new leaf cells. */
  160. i = yoff = 0;
  161. TAILQ_FOREACH(wp, &w->panes, entry) {
  162. /* Create child cell. */
  163. lcnew = layout_create_cell(lc);
  164. layout_set_size(lcnew, w->sx, height, 0, yoff);
  165. layout_make_leaf(lcnew, wp);
  166. TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
  167. i++;
  168. yoff += height + 1;
  169. }
  170. /* Allocate any remaining space. */
  171. if (w->sy > yoff - 1) {
  172. lc = TAILQ_LAST(&lc->cells, layout_cells);
  173. layout_resize_adjust(lc, LAYOUT_TOPBOTTOM, w->sy - (yoff - 1));
  174. }
  175. /* Fix cell offsets. */
  176. layout_fix_offsets(lc);
  177. layout_fix_panes(w, w->sx, w->sy);
  178. layout_print_cell(w->layout_root, __func__, 1);
  179. server_redraw_window(w);
  180. }
  181. void
  182. layout_set_main_h(struct window *w)
  183. {
  184. struct window_pane *wp;
  185. struct layout_cell *lc, *lcmain, *lcrow, *lcchild;
  186. u_int n, mainheight, otherheight, width, height;
  187. u_int used, i, j, columns, rows, totalrows;
  188. layout_print_cell(w->layout_root, __func__, 1);
  189. /* Get number of panes. */
  190. n = window_count_panes(w);
  191. if (n <= 1)
  192. return;
  193. n--; /* take off main pane */
  194. /* How many rows and columns will be needed, not counting main? */
  195. columns = (w->sx + 1) / (PANE_MINIMUM + 1); /* maximum columns */
  196. if (columns == 0)
  197. columns = 1;
  198. rows = 1 + (n - 1) / columns;
  199. columns = 1 + (n - 1) / rows;
  200. width = (w->sx - (n - 1)) / columns;
  201. /* Get the main pane height and add one for separator line. */
  202. mainheight = options_get_number(w->options, "main-pane-height") + 1;
  203. /* Get the optional other pane height and add one for separator line. */
  204. otherheight = options_get_number(w->options, "other-pane-height") + 1;
  205. /*
  206. * If an other pane height was specified, honour it so long as it
  207. * doesn't shrink the main height to less than the main-pane-height
  208. */
  209. if (otherheight > 1 && w->sy - otherheight > mainheight)
  210. mainheight = w->sy - otherheight;
  211. if (mainheight < PANE_MINIMUM + 1)
  212. mainheight = PANE_MINIMUM + 1;
  213. /* Try and make everything fit. */
  214. totalrows = rows * (PANE_MINIMUM + 1) - 1;
  215. if (mainheight + totalrows > w->sy) {
  216. if (totalrows + PANE_MINIMUM + 1 > w->sy)
  217. mainheight = PANE_MINIMUM + 2;
  218. else
  219. mainheight = w->sy - totalrows;
  220. height = PANE_MINIMUM;
  221. } else
  222. height = (w->sy - mainheight - (rows - 1)) / rows;
  223. /* Free old tree and create a new root. */
  224. layout_free(w);
  225. lc = w->layout_root = layout_create_cell(NULL);
  226. layout_set_size(lc, w->sx, mainheight + rows * (height + 1) - 1, 0, 0);
  227. layout_make_node(lc, LAYOUT_TOPBOTTOM);
  228. /* Create the main pane. */
  229. lcmain = layout_create_cell(lc);
  230. layout_set_size(lcmain, w->sx, mainheight - 1, 0, 0);
  231. layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes));
  232. TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry);
  233. /* Create a grid of the remaining cells. */
  234. wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
  235. for (j = 0; j < rows; j++) {
  236. /* If this is the last cell, all done. */
  237. if (wp == NULL)
  238. break;
  239. /* Create the new row. */
  240. lcrow = layout_create_cell(lc);
  241. layout_set_size(lcrow, w->sx, height, 0, 0);
  242. TAILQ_INSERT_TAIL(&lc->cells, lcrow, entry);
  243. /* If only one column, just use the row directly. */
  244. if (columns == 1) {
  245. layout_make_leaf(lcrow, wp);
  246. wp = TAILQ_NEXT(wp, entry);
  247. continue;
  248. }
  249. /* Add in the columns. */
  250. layout_make_node(lcrow, LAYOUT_LEFTRIGHT);
  251. for (i = 0; i < columns; i++) {
  252. /* Create and add a pane cell. */
  253. lcchild = layout_create_cell(lcrow);
  254. layout_set_size(lcchild, width, height, 0, 0);
  255. layout_make_leaf(lcchild, wp);
  256. TAILQ_INSERT_TAIL(&lcrow->cells, lcchild, entry);
  257. /* Move to the next cell. */
  258. if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
  259. break;
  260. }
  261. /* Adjust the row to fit the full width if necessary. */
  262. if (i == columns)
  263. i--;
  264. used = ((i + 1) * (width + 1)) - 1;
  265. if (w->sx <= used)
  266. continue;
  267. lcchild = TAILQ_LAST(&lcrow->cells, layout_cells);
  268. layout_resize_adjust(lcchild, LAYOUT_LEFTRIGHT, w->sx - used);
  269. }
  270. /* Adjust the last row height to fit if necessary. */
  271. used = mainheight + (rows * height) + rows - 1;
  272. if (w->sy > used) {
  273. lcrow = TAILQ_LAST(&lc->cells, layout_cells);
  274. layout_resize_adjust(lcrow, LAYOUT_TOPBOTTOM, w->sy - used);
  275. }
  276. /* Fix cell offsets. */
  277. layout_fix_offsets(lc);
  278. layout_fix_panes(w, w->sx, w->sy);
  279. layout_print_cell(w->layout_root, __func__, 1);
  280. server_redraw_window(w);
  281. }
  282. void
  283. layout_set_main_v(struct window *w)
  284. {
  285. struct window_pane *wp;
  286. struct layout_cell *lc, *lcmain, *lccolumn, *lcchild;
  287. u_int n, mainwidth, otherwidth, width, height;
  288. u_int used, i, j, columns, rows, totalcolumns;
  289. layout_print_cell(w->layout_root, __func__, 1);
  290. /* Get number of panes. */
  291. n = window_count_panes(w);
  292. if (n <= 1)
  293. return;
  294. n--; /* take off main pane */
  295. /* How many rows and columns will be needed, not counting main? */
  296. rows = (w->sy + 1) / (PANE_MINIMUM + 1); /* maximum rows */
  297. if (rows == 0)
  298. rows = 1;
  299. columns = 1 + (n - 1) / rows;
  300. rows = 1 + (n - 1) / columns;
  301. height = (w->sy - (n - 1)) / rows;
  302. /* Get the main pane width and add one for separator line. */
  303. mainwidth = options_get_number(w->options, "main-pane-width") + 1;
  304. /* Get the optional other pane width and add one for separator line. */
  305. otherwidth = options_get_number(w->options, "other-pane-width") + 1;
  306. /*
  307. * If an other pane width was specified, honour it so long as it
  308. * doesn't shrink the main width to less than the main-pane-width
  309. */
  310. if (otherwidth > 1 && w->sx - otherwidth > mainwidth)
  311. mainwidth = w->sx - otherwidth;
  312. if (mainwidth < PANE_MINIMUM + 1)
  313. mainwidth = PANE_MINIMUM + 1;
  314. /* Try and make everything fit. */
  315. totalcolumns = columns * (PANE_MINIMUM + 1) - 1;
  316. if (mainwidth + totalcolumns > w->sx) {
  317. if (totalcolumns + PANE_MINIMUM + 1 > w->sx)
  318. mainwidth = PANE_MINIMUM + 2;
  319. else
  320. mainwidth = w->sx - totalcolumns;
  321. width = PANE_MINIMUM;
  322. } else
  323. width = (w->sx - mainwidth - (columns - 1)) / columns;
  324. /* Free old tree and create a new root. */
  325. layout_free(w);
  326. lc = w->layout_root = layout_create_cell(NULL);
  327. layout_set_size(lc, mainwidth + columns * (width + 1) - 1, w->sy, 0, 0);
  328. layout_make_node(lc, LAYOUT_LEFTRIGHT);
  329. /* Create the main pane. */
  330. lcmain = layout_create_cell(lc);
  331. layout_set_size(lcmain, mainwidth - 1, w->sy, 0, 0);
  332. layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes));
  333. TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry);
  334. /* Create a grid of the remaining cells. */
  335. wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
  336. for (j = 0; j < columns; j++) {
  337. /* If this is the last cell, all done. */
  338. if (wp == NULL)
  339. break;
  340. /* Create the new column. */
  341. lccolumn = layout_create_cell(lc);
  342. layout_set_size(lccolumn, width, w->sy, 0, 0);
  343. TAILQ_INSERT_TAIL(&lc->cells, lccolumn, entry);
  344. /* If only one row, just use the row directly. */
  345. if (rows == 1) {
  346. layout_make_leaf(lccolumn, wp);
  347. wp = TAILQ_NEXT(wp, entry);
  348. continue;
  349. }
  350. /* Add in the rows. */
  351. layout_make_node(lccolumn, LAYOUT_TOPBOTTOM);
  352. for (i = 0; i < rows; i++) {
  353. /* Create and add a pane cell. */
  354. lcchild = layout_create_cell(lccolumn);
  355. layout_set_size(lcchild, width, height, 0, 0);
  356. layout_make_leaf(lcchild, wp);
  357. TAILQ_INSERT_TAIL(&lccolumn->cells, lcchild, entry);
  358. /* Move to the next cell. */
  359. if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
  360. break;
  361. }
  362. /* Adjust the column to fit the full height if necessary. */
  363. if (i == rows)
  364. i--;
  365. used = ((i + 1) * (height + 1)) - 1;
  366. if (w->sy <= used)
  367. continue;
  368. lcchild = TAILQ_LAST(&lccolumn->cells, layout_cells);
  369. layout_resize_adjust(lcchild, LAYOUT_TOPBOTTOM, w->sy - used);
  370. }
  371. /* Adjust the last column width to fit if necessary. */
  372. used = mainwidth + (columns * width) + columns - 1;
  373. if (w->sx > used) {
  374. lccolumn = TAILQ_LAST(&lc->cells, layout_cells);
  375. layout_resize_adjust(lccolumn, LAYOUT_LEFTRIGHT, w->sx - used);
  376. }
  377. /* Fix cell offsets. */
  378. layout_fix_offsets(lc);
  379. layout_fix_panes(w, w->sx, w->sy);
  380. layout_print_cell(w->layout_root, __func__, 1);
  381. server_redraw_window(w);
  382. }
  383. void
  384. layout_set_tiled(struct window *w)
  385. {
  386. struct window_pane *wp;
  387. struct layout_cell *lc, *lcrow, *lcchild;
  388. u_int n, width, height, used;
  389. u_int i, j, columns, rows;
  390. layout_print_cell(w->layout_root, __func__, 1);
  391. /* Get number of panes. */
  392. n = window_count_panes(w);
  393. if (n <= 1)
  394. return;
  395. /* How many rows and columns are wanted? */
  396. rows = columns = 1;
  397. while (rows * columns < n) {
  398. rows++;
  399. if (rows * columns < n)
  400. columns++;
  401. }
  402. /* What width and height should they be? */
  403. width = (w->sx - (columns - 1)) / columns;
  404. if (width < PANE_MINIMUM)
  405. width = PANE_MINIMUM;
  406. height = (w->sy - (rows - 1)) / rows;
  407. if (height < PANE_MINIMUM)
  408. height = PANE_MINIMUM;
  409. /* Free old tree and create a new root. */
  410. layout_free(w);
  411. lc = w->layout_root = layout_create_cell(NULL);
  412. layout_set_size(lc, (width + 1) * columns - 1,
  413. (height + 1) * rows - 1, 0, 0);
  414. layout_make_node(lc, LAYOUT_TOPBOTTOM);
  415. /* Create a grid of the cells. */
  416. wp = TAILQ_FIRST(&w->panes);
  417. for (j = 0; j < rows; j++) {
  418. /* If this is the last cell, all done. */
  419. if (wp == NULL)
  420. break;
  421. /* Create the new row. */
  422. lcrow = layout_create_cell(lc);
  423. layout_set_size(lcrow, w->sx, height, 0, 0);
  424. TAILQ_INSERT_TAIL(&lc->cells, lcrow, entry);
  425. /* If only one column, just use the row directly. */
  426. if (n - (j * columns) == 1 || columns == 1) {
  427. layout_make_leaf(lcrow, wp);
  428. wp = TAILQ_NEXT(wp, entry);
  429. continue;
  430. }
  431. /* Add in the columns. */
  432. layout_make_node(lcrow, LAYOUT_LEFTRIGHT);
  433. for (i = 0; i < columns; i++) {
  434. /* Create and add a pane cell. */
  435. lcchild = layout_create_cell(lcrow);
  436. layout_set_size(lcchild, width, height, 0, 0);
  437. layout_make_leaf(lcchild, wp);
  438. TAILQ_INSERT_TAIL(&lcrow->cells, lcchild, entry);
  439. /* Move to the next cell. */
  440. if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
  441. break;
  442. }
  443. /*
  444. * Adjust the row and columns to fit the full width if
  445. * necessary.
  446. */
  447. if (i == columns)
  448. i--;
  449. used = ((i + 1) * (width + 1)) - 1;
  450. if (w->sx <= used)
  451. continue;
  452. lcchild = TAILQ_LAST(&lcrow->cells, layout_cells);
  453. layout_resize_adjust(lcchild, LAYOUT_LEFTRIGHT, w->sx - used);
  454. }
  455. /* Adjust the last row height to fit if necessary. */
  456. used = (rows * height) + rows - 1;
  457. if (w->sy > used) {
  458. lcrow = TAILQ_LAST(&lc->cells, layout_cells);
  459. layout_resize_adjust(lcrow, LAYOUT_TOPBOTTOM, w->sy - used);
  460. }
  461. /* Fix cell offsets. */
  462. layout_fix_offsets(lc);
  463. layout_fix_panes(w, w->sx, w->sy);
  464. layout_print_cell(w->layout_root, __func__, 1);
  465. server_redraw_window(w);
  466. }