4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5 * Copyright (c) 2016 Stephen Kent <smkent@smkent.net>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
27 * The window layout is a tree of cells each of which can be one of: a
28 * left-right container for a list of cells, a top-bottom container for a list
29 * of cells, or a container for a window pane.
31 * Each window has a pointer to the root of its layout tree (containing its
32 * panes), every pane has a pointer back to the cell containing it, and each
33 * cell a pointer to its parent cell.
36 static u_int
layout_resize_check(struct window
*, struct layout_cell
*,
38 static int layout_resize_pane_grow(struct window
*, struct layout_cell
*,
39 enum layout_type
, int, int);
40 static int layout_resize_pane_shrink(struct window
*, struct layout_cell
*,
41 enum layout_type
, int);
42 static u_int
layout_new_pane_size(struct window
*, u_int
,
43 struct layout_cell
*, enum layout_type
, u_int
, u_int
,
45 static int layout_set_size_check(struct window
*, struct layout_cell
*,
46 enum layout_type
, int);
47 static void layout_resize_child_cells(struct window
*,
48 struct layout_cell
*);
51 layout_create_cell(struct layout_cell
*lcparent
)
53 struct layout_cell
*lc
;
55 lc
= xmalloc(sizeof *lc
);
56 lc
->type
= LAYOUT_WINDOWPANE
;
57 lc
->parent
= lcparent
;
59 TAILQ_INIT(&lc
->cells
);
73 layout_free_cell(struct layout_cell
*lc
)
75 struct layout_cell
*lcchild
;
78 case LAYOUT_LEFTRIGHT
:
79 case LAYOUT_TOPBOTTOM
:
80 while (!TAILQ_EMPTY(&lc
->cells
)) {
81 lcchild
= TAILQ_FIRST(&lc
->cells
);
82 TAILQ_REMOVE(&lc
->cells
, lcchild
, entry
);
83 layout_free_cell(lcchild
);
86 case LAYOUT_WINDOWPANE
:
88 lc
->wp
->layout_cell
= NULL
;
96 layout_print_cell(struct layout_cell
*lc
, const char *hdr
, u_int n
)
98 struct layout_cell
*lcchild
;
102 case LAYOUT_LEFTRIGHT
:
105 case LAYOUT_TOPBOTTOM
:
108 case LAYOUT_WINDOWPANE
:
115 log_debug("%s:%*s%p type %s [parent %p] wp=%p [%u,%u %ux%u]", hdr
, n
,
116 " ", lc
, type
, lc
->parent
, lc
->wp
, lc
->xoff
, lc
->yoff
, lc
->sx
,
119 case LAYOUT_LEFTRIGHT
:
120 case LAYOUT_TOPBOTTOM
:
121 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
122 layout_print_cell(lcchild
, hdr
, n
+ 1);
124 case LAYOUT_WINDOWPANE
:
130 layout_search_by_border(struct layout_cell
*lc
, u_int x
, u_int y
)
132 struct layout_cell
*lcchild
, *last
= NULL
;
134 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
135 if (x
>= lcchild
->xoff
&& x
< lcchild
->xoff
+ lcchild
->sx
&&
136 y
>= lcchild
->yoff
&& y
< lcchild
->yoff
+ lcchild
->sy
) {
137 /* Inside the cell - recurse. */
138 return (layout_search_by_border(lcchild
, x
, y
));
147 case LAYOUT_LEFTRIGHT
:
148 if (x
< lcchild
->xoff
&& x
>= last
->xoff
+ last
->sx
)
151 case LAYOUT_TOPBOTTOM
:
152 if (y
< lcchild
->yoff
&& y
>= last
->yoff
+ last
->sy
)
155 case LAYOUT_WINDOWPANE
:
166 layout_set_size(struct layout_cell
*lc
, u_int sx
, u_int sy
, u_int xoff
,
177 layout_make_leaf(struct layout_cell
*lc
, struct window_pane
*wp
)
179 lc
->type
= LAYOUT_WINDOWPANE
;
181 TAILQ_INIT(&lc
->cells
);
183 wp
->layout_cell
= lc
;
188 layout_make_node(struct layout_cell
*lc
, enum layout_type type
)
190 if (type
== LAYOUT_WINDOWPANE
)
191 fatalx("bad layout type");
194 TAILQ_INIT(&lc
->cells
);
197 lc
->wp
->layout_cell
= NULL
;
201 /* Fix cell offsets for a child cell. */
203 layout_fix_offsets1(struct layout_cell
*lc
)
205 struct layout_cell
*lcchild
;
208 if (lc
->type
== LAYOUT_LEFTRIGHT
) {
210 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
211 lcchild
->xoff
= xoff
;
212 lcchild
->yoff
= lc
->yoff
;
213 if (lcchild
->type
!= LAYOUT_WINDOWPANE
)
214 layout_fix_offsets1(lcchild
);
215 xoff
+= lcchild
->sx
+ 1;
219 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
220 lcchild
->xoff
= lc
->xoff
;
221 lcchild
->yoff
= yoff
;
222 if (lcchild
->type
!= LAYOUT_WINDOWPANE
)
223 layout_fix_offsets1(lcchild
);
224 yoff
+= lcchild
->sy
+ 1;
229 /* Update cell offsets based on their sizes. */
231 layout_fix_offsets(struct window
*w
)
233 struct layout_cell
*lc
= w
->layout_root
;
238 layout_fix_offsets1(lc
);
241 /* Is this a top cell? */
243 layout_cell_is_top(struct window
*w
, struct layout_cell
*lc
)
245 struct layout_cell
*next
;
247 while (lc
!= w
->layout_root
) {
249 if (next
->type
== LAYOUT_TOPBOTTOM
&&
250 lc
!= TAILQ_FIRST(&next
->cells
))
257 /* Is this a bottom cell? */
259 layout_cell_is_bottom(struct window
*w
, struct layout_cell
*lc
)
261 struct layout_cell
*next
;
263 while (lc
!= w
->layout_root
) {
265 if (next
->type
== LAYOUT_TOPBOTTOM
&&
266 lc
!= TAILQ_LAST(&next
->cells
, layout_cells
))
274 * Returns 1 if we need to add an extra line for the pane status line. This is
275 * the case for the most upper or lower panes only.
278 layout_add_horizontal_border(struct window
*w
, struct layout_cell
*lc
,
281 if (status
== PANE_STATUS_TOP
)
282 return (layout_cell_is_top(w
, lc
));
283 if (status
== PANE_STATUS_BOTTOM
)
284 return (layout_cell_is_bottom(w
, lc
));
288 /* Update pane offsets and sizes based on their cells. */
290 layout_fix_panes(struct window
*w
, struct window_pane
*skip
)
292 struct window_pane
*wp
;
293 struct layout_cell
*lc
;
294 int status
, scrollbars
, sb_pos
;
297 status
= options_get_number(w
->options
, "pane-border-status");
298 scrollbars
= options_get_number(w
->options
, "pane-scrollbars");
299 sb_pos
= options_get_number(w
->options
, "pane-scrollbars-position");
301 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
302 if ((lc
= wp
->layout_cell
) == NULL
|| wp
== skip
)
310 if (layout_add_horizontal_border(w
, lc
, status
)) {
311 if (status
== PANE_STATUS_TOP
)
316 mode
= window_pane_mode(wp
);
317 if (scrollbars
== PANE_SCROLLBARS_ALWAYS
||
318 (scrollbars
== PANE_SCROLLBARS_MODAL
&&
319 mode
!= WINDOW_PANE_NO_MODE
)) {
320 if (sb_pos
== PANE_SCROLLBARS_LEFT
) {
321 sx
= sx
- PANE_SCROLLBARS_WIDTH
;
322 wp
->xoff
= wp
->xoff
+ PANE_SCROLLBARS_WIDTH
;
323 } else /* sb_pos == PANE_SCROLLBARS_RIGHT */
324 sx
= sx
- PANE_SCROLLBARS_WIDTH
;
325 wp
->flags
|= PANE_REDRAWSCROLLBAR
;
328 window_pane_resize(wp
, sx
, sy
);
332 /* Count the number of available cells in a layout. */
334 layout_count_cells(struct layout_cell
*lc
)
336 struct layout_cell
*lcchild
;
340 case LAYOUT_WINDOWPANE
:
342 case LAYOUT_LEFTRIGHT
:
343 case LAYOUT_TOPBOTTOM
:
345 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
346 count
+= layout_count_cells(lcchild
);
349 fatalx("bad layout type");
353 /* Calculate how much size is available to be removed from a cell. */
355 layout_resize_check(struct window
*w
, struct layout_cell
*lc
,
356 enum layout_type type
)
358 struct layout_cell
*lcchild
;
359 u_int available
, minimum
;
360 int status
, scrollbars
;
362 status
= options_get_number(w
->options
, "pane-border-status");
363 scrollbars
= options_get_number(w
->options
, "pane-scrollbars");
365 if (lc
->type
== LAYOUT_WINDOWPANE
) {
366 /* Space available in this cell only. */
367 if (type
== LAYOUT_LEFTRIGHT
) {
370 minimum
= PANE_MINIMUM
+ PANE_SCROLLBARS_WIDTH
;
372 minimum
= PANE_MINIMUM
;
375 if (layout_add_horizontal_border(w
, lc
, status
))
376 minimum
= PANE_MINIMUM
+ 1;
378 minimum
= PANE_MINIMUM
;
380 if (available
> minimum
)
381 available
-= minimum
;
384 } else if (lc
->type
== type
) {
385 /* Same type: total of available space in all child cells. */
387 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
388 available
+= layout_resize_check(w
, lcchild
, type
);
390 /* Different type: minimum of available space in child cells. */
392 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
393 available
= layout_resize_check(w
, lcchild
, type
);
394 if (available
< minimum
)
404 * Adjust cell size evenly, including altering its children. This function
405 * expects the change to have already been bounded to the space available.
408 layout_resize_adjust(struct window
*w
, struct layout_cell
*lc
,
409 enum layout_type type
, int change
)
411 struct layout_cell
*lcchild
;
413 /* Adjust the cell size. */
414 if (type
== LAYOUT_LEFTRIGHT
)
419 /* If this is a leaf cell, that is all that is necessary. */
420 if (type
== LAYOUT_WINDOWPANE
)
423 /* Child cell runs in a different direction. */
424 if (lc
->type
!= type
) {
425 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
426 layout_resize_adjust(w
, lcchild
, type
, change
);
431 * Child cell runs in the same direction. Adjust each child equally
432 * until no further change is possible.
434 while (change
!= 0) {
435 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
439 layout_resize_adjust(w
, lcchild
, type
, 1);
443 if (layout_resize_check(w
, lcchild
, type
) > 0) {
444 layout_resize_adjust(w
, lcchild
, type
, -1);
451 /* Destroy a cell and redistribute the space. */
453 layout_destroy_cell(struct window
*w
, struct layout_cell
*lc
,
454 struct layout_cell
**lcroot
)
456 struct layout_cell
*lcother
, *lcparent
;
459 * If no parent, this is the last pane so window close is imminent and
460 * there is no need to resize anything.
462 lcparent
= lc
->parent
;
463 if (lcparent
== NULL
) {
464 layout_free_cell(lc
);
469 /* Merge the space into the previous or next cell. */
470 if (lc
== TAILQ_FIRST(&lcparent
->cells
))
471 lcother
= TAILQ_NEXT(lc
, entry
);
473 lcother
= TAILQ_PREV(lc
, layout_cells
, entry
);
474 if (lcother
!= NULL
&& lcparent
->type
== LAYOUT_LEFTRIGHT
)
475 layout_resize_adjust(w
, lcother
, lcparent
->type
, lc
->sx
+ 1);
476 else if (lcother
!= NULL
)
477 layout_resize_adjust(w
, lcother
, lcparent
->type
, lc
->sy
+ 1);
479 /* Remove this from the parent's list. */
480 TAILQ_REMOVE(&lcparent
->cells
, lc
, entry
);
481 layout_free_cell(lc
);
484 * If the parent now has one cell, remove the parent from the tree and
485 * replace it by that cell.
487 lc
= TAILQ_FIRST(&lcparent
->cells
);
488 if (TAILQ_NEXT(lc
, entry
) == NULL
) {
489 TAILQ_REMOVE(&lcparent
->cells
, lc
, entry
);
491 lc
->parent
= lcparent
->parent
;
492 if (lc
->parent
== NULL
) {
493 lc
->xoff
= 0; lc
->yoff
= 0;
496 TAILQ_REPLACE(&lc
->parent
->cells
, lcparent
, lc
, entry
);
498 layout_free_cell(lcparent
);
503 layout_init(struct window
*w
, struct window_pane
*wp
)
505 struct layout_cell
*lc
;
507 lc
= w
->layout_root
= layout_create_cell(NULL
);
508 layout_set_size(lc
, w
->sx
, w
->sy
, 0, 0);
509 layout_make_leaf(lc
, wp
);
510 layout_fix_panes(w
, NULL
);
514 layout_free(struct window
*w
)
516 layout_free_cell(w
->layout_root
);
519 /* Resize the entire layout after window resize. */
521 layout_resize(struct window
*w
, u_int sx
, u_int sy
)
523 struct layout_cell
*lc
= w
->layout_root
;
524 int xlimit
, ylimit
, xchange
, ychange
;
527 * Adjust horizontally. Do not attempt to reduce the layout lower than
528 * the minimum (more than the amount returned by layout_resize_check).
530 * This can mean that the window size is smaller than the total layout
531 * size: redrawing this is handled at a higher level, but it does leave
532 * a problem with growing the window size here: if the current size is
533 * < the minimum, growing proportionately by adding to each pane is
534 * wrong as it would keep the layout size larger than the window size.
535 * Instead, spread the difference between the minimum and the new size
536 * out proportionately - this should leave the layout fitting the new
539 xchange
= sx
- lc
->sx
;
540 xlimit
= layout_resize_check(w
, lc
, LAYOUT_LEFTRIGHT
);
541 if (xchange
< 0 && xchange
< -xlimit
)
544 if (sx
<= lc
->sx
) /* lc->sx is minimum possible */
547 xchange
= sx
- lc
->sx
;
550 layout_resize_adjust(w
, lc
, LAYOUT_LEFTRIGHT
, xchange
);
552 /* Adjust vertically in a similar fashion. */
553 ychange
= sy
- lc
->sy
;
554 ylimit
= layout_resize_check(w
, lc
, LAYOUT_TOPBOTTOM
);
555 if (ychange
< 0 && ychange
< -ylimit
)
558 if (sy
<= lc
->sy
) /* lc->sy is minimum possible */
561 ychange
= sy
- lc
->sy
;
564 layout_resize_adjust(w
, lc
, LAYOUT_TOPBOTTOM
, ychange
);
566 /* Fix cell offsets. */
567 layout_fix_offsets(w
);
568 layout_fix_panes(w
, NULL
);
571 /* Resize a pane to an absolute size. */
573 layout_resize_pane_to(struct window_pane
*wp
, enum layout_type type
,
576 struct layout_cell
*lc
, *lcparent
;
579 lc
= wp
->layout_cell
;
581 /* Find next parent of the same type. */
582 lcparent
= lc
->parent
;
583 while (lcparent
!= NULL
&& lcparent
->type
!= type
) {
585 lcparent
= lc
->parent
;
587 if (lcparent
== NULL
)
590 /* Work out the size adjustment. */
591 if (type
== LAYOUT_LEFTRIGHT
)
595 if (lc
== TAILQ_LAST(&lcparent
->cells
, layout_cells
))
596 change
= size
- new_size
;
598 change
= new_size
- size
;
600 /* Resize the pane. */
601 layout_resize_pane(wp
, type
, change
, 1);
605 layout_resize_layout(struct window
*w
, struct layout_cell
*lc
,
606 enum layout_type type
, int change
, int opposite
)
610 /* Grow or shrink the cell. */
612 while (needed
!= 0) {
614 size
= layout_resize_pane_grow(w
, lc
, type
, needed
,
618 size
= layout_resize_pane_shrink(w
, lc
, type
, needed
);
622 if (size
== 0) /* no more change possible */
626 /* Fix cell offsets. */
627 layout_fix_offsets(w
);
628 layout_fix_panes(w
, NULL
);
629 notify_window("window-layout-changed", w
);
632 /* Resize a single pane within the layout. */
634 layout_resize_pane(struct window_pane
*wp
, enum layout_type type
, int change
,
637 struct layout_cell
*lc
, *lcparent
;
639 lc
= wp
->layout_cell
;
641 /* Find next parent of the same type. */
642 lcparent
= lc
->parent
;
643 while (lcparent
!= NULL
&& lcparent
->type
!= type
) {
645 lcparent
= lc
->parent
;
647 if (lcparent
== NULL
)
650 /* If this is the last cell, move back one. */
651 if (lc
== TAILQ_LAST(&lcparent
->cells
, layout_cells
))
652 lc
= TAILQ_PREV(lc
, layout_cells
, entry
);
654 layout_resize_layout(wp
->window
, lc
, type
, change
, opposite
);
657 /* Helper function to grow pane. */
659 layout_resize_pane_grow(struct window
*w
, struct layout_cell
*lc
,
660 enum layout_type type
, int needed
, int opposite
)
662 struct layout_cell
*lcadd
, *lcremove
;
665 /* Growing. Always add to the current cell. */
668 /* Look towards the tail for a suitable cell for reduction. */
669 lcremove
= TAILQ_NEXT(lc
, entry
);
670 while (lcremove
!= NULL
) {
671 size
= layout_resize_check(w
, lcremove
, type
);
674 lcremove
= TAILQ_NEXT(lcremove
, entry
);
677 /* If none found, look towards the head. */
678 if (opposite
&& lcremove
== NULL
) {
679 lcremove
= TAILQ_PREV(lc
, layout_cells
, entry
);
680 while (lcremove
!= NULL
) {
681 size
= layout_resize_check(w
, lcremove
, type
);
684 lcremove
= TAILQ_PREV(lcremove
, layout_cells
, entry
);
687 if (lcremove
== NULL
)
690 /* Change the cells. */
691 if (size
> (u_int
) needed
)
693 layout_resize_adjust(w
, lcadd
, type
, size
);
694 layout_resize_adjust(w
, lcremove
, type
, -size
);
698 /* Helper function to shrink pane. */
700 layout_resize_pane_shrink(struct window
*w
, struct layout_cell
*lc
,
701 enum layout_type type
, int needed
)
703 struct layout_cell
*lcadd
, *lcremove
;
706 /* Shrinking. Find cell to remove from by walking towards head. */
709 size
= layout_resize_check(w
, lcremove
, type
);
712 lcremove
= TAILQ_PREV(lcremove
, layout_cells
, entry
);
713 } while (lcremove
!= NULL
);
714 if (lcremove
== NULL
)
717 /* And add onto the next cell (from the original cell). */
718 lcadd
= TAILQ_NEXT(lc
, entry
);
722 /* Change the cells. */
723 if (size
> (u_int
) -needed
)
725 layout_resize_adjust(w
, lcadd
, type
, size
);
726 layout_resize_adjust(w
, lcremove
, type
, -size
);
730 /* Assign window pane to newly split cell. */
732 layout_assign_pane(struct layout_cell
*lc
, struct window_pane
*wp
,
735 layout_make_leaf(lc
, wp
);
737 layout_fix_panes(wp
->window
, wp
);
739 layout_fix_panes(wp
->window
, NULL
);
742 /* Calculate the new pane size for resized parent. */
744 layout_new_pane_size(struct window
*w
, u_int previous
, struct layout_cell
*lc
,
745 enum layout_type type
, u_int size
, u_int count_left
, u_int size_left
)
747 u_int new_size
, min
, max
, available
;
749 /* If this is the last cell, it can take all of the remaining size. */
753 /* How much is available in this parent? */
754 available
= layout_resize_check(w
, lc
, type
);
757 * Work out the minimum size of this cell and the new size
758 * proportionate to the previous size.
760 min
= (PANE_MINIMUM
+ 1) * (count_left
- 1);
761 if (type
== LAYOUT_LEFTRIGHT
) {
762 if (lc
->sx
- available
> min
)
763 min
= lc
->sx
- available
;
764 new_size
= (lc
->sx
* size
) / previous
;
766 if (lc
->sy
- available
> min
)
767 min
= lc
->sy
- available
;
768 new_size
= (lc
->sy
* size
) / previous
;
771 /* Check against the maximum and minimum size. */
772 max
= size_left
- min
;
775 if (new_size
< PANE_MINIMUM
)
776 new_size
= PANE_MINIMUM
;
780 /* Check if the cell and all its children can be resized to a specific size. */
782 layout_set_size_check(struct window
*w
, struct layout_cell
*lc
,
783 enum layout_type type
, int size
)
785 struct layout_cell
*lcchild
;
786 u_int new_size
, available
, previous
, count
, idx
;
788 /* Cells with no children must just be bigger than minimum. */
789 if (lc
->type
== LAYOUT_WINDOWPANE
)
790 return (size
>= PANE_MINIMUM
);
793 /* Count number of children. */
795 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
798 /* Check new size will work for each child. */
799 if (lc
->type
== type
) {
800 if (available
< (count
* 2) - 1)
803 if (type
== LAYOUT_LEFTRIGHT
)
809 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
810 new_size
= layout_new_pane_size(w
, previous
, lcchild
,
811 type
, size
, count
- idx
, available
);
812 if (idx
== count
- 1) {
813 if (new_size
> available
)
815 available
-= new_size
;
817 if (new_size
+ 1 > available
)
819 available
-= new_size
+ 1;
821 if (!layout_set_size_check(w
, lcchild
, type
, new_size
))
826 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
827 if (lcchild
->type
== LAYOUT_WINDOWPANE
)
829 if (!layout_set_size_check(w
, lcchild
, type
, size
))
837 /* Resize all child cells to fit within the current cell. */
839 layout_resize_child_cells(struct window
*w
, struct layout_cell
*lc
)
841 struct layout_cell
*lcchild
;
842 u_int previous
, available
, count
, idx
;
844 if (lc
->type
== LAYOUT_WINDOWPANE
)
847 /* What is the current size used? */
850 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
852 if (lc
->type
== LAYOUT_LEFTRIGHT
)
853 previous
+= lcchild
->sx
;
854 else if (lc
->type
== LAYOUT_TOPBOTTOM
)
855 previous
+= lcchild
->sy
;
857 previous
+= (count
- 1);
859 /* And how much is available? */
861 if (lc
->type
== LAYOUT_LEFTRIGHT
)
863 else if (lc
->type
== LAYOUT_TOPBOTTOM
)
866 /* Resize children into the new size. */
868 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
869 if (lc
->type
== LAYOUT_TOPBOTTOM
) {
870 lcchild
->sx
= lc
->sx
;
871 lcchild
->xoff
= lc
->xoff
;
873 lcchild
->sx
= layout_new_pane_size(w
, previous
, lcchild
,
874 lc
->type
, lc
->sx
, count
- idx
, available
);
875 available
-= (lcchild
->sx
+ 1);
877 if (lc
->type
== LAYOUT_LEFTRIGHT
)
878 lcchild
->sy
= lc
->sy
;
880 lcchild
->sy
= layout_new_pane_size(w
, previous
, lcchild
,
881 lc
->type
, lc
->sy
, count
- idx
, available
);
882 available
-= (lcchild
->sy
+ 1);
884 layout_resize_child_cells(w
, lcchild
);
890 * Split a pane into two. size is a hint, or -1 for default half/half
891 * split. This must be followed by layout_assign_pane before much else happens!
894 layout_split_pane(struct window_pane
*wp
, enum layout_type type
, int size
,
897 struct layout_cell
*lc
, *lcparent
, *lcnew
, *lc1
, *lc2
;
898 u_int sx
, sy
, xoff
, yoff
, size1
, size2
, minimum
;
899 u_int new_size
, saved_size
, resize_first
= 0;
900 int full_size
= (flags
& SPAWN_FULLSIZE
), status
;
904 * If full_size is specified, add a new cell at the top of the window
905 * layout. Otherwise, split the cell for the current pane.
908 lc
= wp
->window
->layout_root
;
910 lc
= wp
->layout_cell
;
911 status
= options_get_number(wp
->window
->options
, "pane-border-status");
912 scrollbars
= options_get_number(wp
->window
->options
, "pane-scrollbars");
914 /* Copy the old cell size. */
920 /* Check there is enough space for the two new panes. */
922 case LAYOUT_LEFTRIGHT
:
924 minimum
= PANE_MINIMUM
* 2 + PANE_SCROLLBARS_WIDTH
;
926 minimum
= PANE_MINIMUM
* 2 + 1;
930 case LAYOUT_TOPBOTTOM
:
931 if (layout_add_horizontal_border(wp
->window
, lc
, status
))
932 minimum
= PANE_MINIMUM
* 2 + 2;
934 minimum
= PANE_MINIMUM
* 2 + 1;
939 fatalx("bad layout type");
943 * Calculate new cell sizes. size is the target size or -1 for middle
944 * split, size1 is the size of the top/left and size2 the bottom/right.
946 if (type
== LAYOUT_LEFTRIGHT
)
951 size2
= ((saved_size
+ 1) / 2) - 1;
952 else if (flags
& SPAWN_BEFORE
)
953 size2
= saved_size
- size
- 1;
956 if (size2
< PANE_MINIMUM
)
957 size2
= PANE_MINIMUM
;
958 else if (size2
> saved_size
- 2)
959 size2
= saved_size
- 2;
960 size1
= saved_size
- 1 - size2
;
962 /* Which size are we using? */
963 if (flags
& SPAWN_BEFORE
)
968 /* Confirm there is enough space for full size pane. */
969 if (full_size
&& !layout_set_size_check(wp
->window
, lc
, type
, new_size
))
972 if (lc
->parent
!= NULL
&& lc
->parent
->type
== type
) {
974 * If the parent exists and is of the same type as the split,
975 * create a new cell and insert it after this one.
977 lcparent
= lc
->parent
;
978 lcnew
= layout_create_cell(lcparent
);
979 if (flags
& SPAWN_BEFORE
)
980 TAILQ_INSERT_BEFORE(lc
, lcnew
, entry
);
982 TAILQ_INSERT_AFTER(&lcparent
->cells
, lc
, lcnew
, entry
);
983 } else if (full_size
&& lc
->parent
== NULL
&& lc
->type
== type
) {
985 * If the new full size pane is the same type as the root
986 * split, insert the new pane under the existing root cell
987 * instead of creating a new root cell. The existing layout
988 * must be resized before inserting the new cell.
990 if (lc
->type
== LAYOUT_LEFTRIGHT
) {
992 layout_resize_child_cells(wp
->window
, lc
);
994 } else if (lc
->type
== LAYOUT_TOPBOTTOM
) {
996 layout_resize_child_cells(wp
->window
, lc
);
1001 /* Create the new cell. */
1002 lcnew
= layout_create_cell(lc
);
1003 size
= saved_size
- 1 - new_size
;
1004 if (lc
->type
== LAYOUT_LEFTRIGHT
)
1005 layout_set_size(lcnew
, size
, sy
, 0, 0);
1006 else if (lc
->type
== LAYOUT_TOPBOTTOM
)
1007 layout_set_size(lcnew
, sx
, size
, 0, 0);
1008 if (flags
& SPAWN_BEFORE
)
1009 TAILQ_INSERT_HEAD(&lc
->cells
, lcnew
, entry
);
1011 TAILQ_INSERT_TAIL(&lc
->cells
, lcnew
, entry
);
1014 * Otherwise create a new parent and insert it.
1017 /* Create and insert the replacement parent. */
1018 lcparent
= layout_create_cell(lc
->parent
);
1019 layout_make_node(lcparent
, type
);
1020 layout_set_size(lcparent
, sx
, sy
, xoff
, yoff
);
1021 if (lc
->parent
== NULL
)
1022 wp
->window
->layout_root
= lcparent
;
1024 TAILQ_REPLACE(&lc
->parent
->cells
, lc
, lcparent
, entry
);
1026 /* Insert the old cell. */
1027 lc
->parent
= lcparent
;
1028 TAILQ_INSERT_HEAD(&lcparent
->cells
, lc
, entry
);
1030 /* Create the new child cell. */
1031 lcnew
= layout_create_cell(lcparent
);
1032 if (flags
& SPAWN_BEFORE
)
1033 TAILQ_INSERT_HEAD(&lcparent
->cells
, lcnew
, entry
);
1035 TAILQ_INSERT_TAIL(&lcparent
->cells
, lcnew
, entry
);
1037 if (flags
& SPAWN_BEFORE
) {
1046 * Set new cell sizes. size1 is the size of the top/left and size2 the
1049 if (!resize_first
&& type
== LAYOUT_LEFTRIGHT
) {
1050 layout_set_size(lc1
, size1
, sy
, xoff
, yoff
);
1051 layout_set_size(lc2
, size2
, sy
, xoff
+ lc1
->sx
+ 1, yoff
);
1052 } else if (!resize_first
&& type
== LAYOUT_TOPBOTTOM
) {
1053 layout_set_size(lc1
, sx
, size1
, xoff
, yoff
);
1054 layout_set_size(lc2
, sx
, size2
, xoff
, yoff
+ lc1
->sy
+ 1);
1058 layout_resize_child_cells(wp
->window
, lc
);
1059 layout_fix_offsets(wp
->window
);
1061 layout_make_leaf(lc
, wp
);
1066 /* Destroy the cell associated with a pane. */
1068 layout_close_pane(struct window_pane
*wp
)
1070 struct window
*w
= wp
->window
;
1072 /* Remove the cell. */
1073 layout_destroy_cell(w
, wp
->layout_cell
, &w
->layout_root
);
1075 /* Fix pane offsets and sizes. */
1076 if (w
->layout_root
!= NULL
) {
1077 layout_fix_offsets(w
);
1078 layout_fix_panes(w
, NULL
);
1080 notify_window("window-layout-changed", w
);
1084 layout_spread_cell(struct window
*w
, struct layout_cell
*parent
)
1086 struct layout_cell
*lc
;
1087 u_int number
, each
, size
, this;
1088 int change
, changed
, status
, scrollbars
;
1091 TAILQ_FOREACH (lc
, &parent
->cells
, entry
)
1095 status
= options_get_number(w
->options
, "pane-border-status");
1096 scrollbars
= options_get_number(w
->options
, "pane-scrollbars");
1098 if (parent
->type
== LAYOUT_LEFTRIGHT
) {
1100 size
= parent
->sx
- PANE_SCROLLBARS_WIDTH
;
1104 else if (parent
->type
== LAYOUT_TOPBOTTOM
) {
1105 if (layout_add_horizontal_border(w
, parent
, status
))
1106 size
= parent
->sy
- 1;
1111 if (size
< number
- 1)
1113 each
= (size
- (number
- 1)) / number
;
1118 TAILQ_FOREACH (lc
, &parent
->cells
, entry
) {
1119 if (TAILQ_NEXT(lc
, entry
) == NULL
)
1120 each
= size
- ((each
+ 1) * (number
- 1));
1122 if (parent
->type
== LAYOUT_LEFTRIGHT
) {
1123 change
= each
- (int)lc
->sx
;
1124 layout_resize_adjust(w
, lc
, LAYOUT_LEFTRIGHT
, change
);
1125 } else if (parent
->type
== LAYOUT_TOPBOTTOM
) {
1126 if (layout_add_horizontal_border(w
, lc
, status
))
1130 change
= this - (int)lc
->sy
;
1131 layout_resize_adjust(w
, lc
, LAYOUT_TOPBOTTOM
, change
);
1140 layout_spread_out(struct window_pane
*wp
)
1142 struct layout_cell
*parent
;
1143 struct window
*w
= wp
->window
;
1145 parent
= wp
->layout_cell
->parent
;
1150 if (layout_spread_cell(w
, parent
)) {
1151 layout_fix_offsets(w
);
1152 layout_fix_panes(w
, NULL
);
1155 } while ((parent
= parent
->parent
) != NULL
);