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