4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
26 * The window layout is a tree of cells each of which can be one of: a
27 * left-right container for a list of cells, a top-bottom container for a list
28 * of cells, or a container for a window pane.
30 * Each window has a pointer to the root of its layout tree (containing its
31 * panes), every pane has a pointer back to the cell containing it, and each
32 * cell a pointer to its parent cell.
35 int layout_resize_pane_grow(struct layout_cell
*, enum layout_type
, int);
36 int layout_resize_pane_shrink(struct layout_cell
*, enum layout_type
, int);
39 layout_create_cell(struct layout_cell
*lcparent
)
41 struct layout_cell
*lc
;
43 lc
= xmalloc(sizeof *lc
);
44 lc
->type
= LAYOUT_WINDOWPANE
;
45 lc
->parent
= lcparent
;
47 TAILQ_INIT(&lc
->cells
);
61 layout_free_cell(struct layout_cell
*lc
)
63 struct layout_cell
*lcchild
;
66 case LAYOUT_LEFTRIGHT
:
67 case LAYOUT_TOPBOTTOM
:
68 while (!TAILQ_EMPTY(&lc
->cells
)) {
69 lcchild
= TAILQ_FIRST(&lc
->cells
);
70 TAILQ_REMOVE(&lc
->cells
, lcchild
, entry
);
71 layout_free_cell(lcchild
);
74 case LAYOUT_WINDOWPANE
:
76 lc
->wp
->layout_cell
= NULL
;
84 layout_print_cell(struct layout_cell
*lc
, const char *hdr
, u_int n
)
86 struct layout_cell
*lcchild
;
89 "%s:%*s%p type %u [parent %p] wp=%p [%u,%u %ux%u]", hdr
, n
, " ", lc
,
90 lc
->type
, lc
->parent
, lc
->wp
, lc
->xoff
, lc
->yoff
, lc
->sx
, lc
->sy
);
92 case LAYOUT_LEFTRIGHT
:
93 case LAYOUT_TOPBOTTOM
:
94 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
95 layout_print_cell(lcchild
, hdr
, n
+ 1);
97 case LAYOUT_WINDOWPANE
:
104 struct layout_cell
*lc
, u_int sx
, u_int sy
, u_int xoff
, u_int yoff
)
114 layout_make_leaf(struct layout_cell
*lc
, struct window_pane
*wp
)
116 lc
->type
= LAYOUT_WINDOWPANE
;
118 TAILQ_INIT(&lc
->cells
);
120 wp
->layout_cell
= lc
;
125 layout_make_node(struct layout_cell
*lc
, enum layout_type type
)
127 if (type
== LAYOUT_WINDOWPANE
)
128 fatalx("bad layout type");
131 TAILQ_INIT(&lc
->cells
);
134 lc
->wp
->layout_cell
= NULL
;
138 /* Fix cell offsets based on their sizes. */
140 layout_fix_offsets(struct layout_cell
*lc
)
142 struct layout_cell
*lcchild
;
145 if (lc
->type
== LAYOUT_LEFTRIGHT
) {
147 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
148 lcchild
->xoff
= xoff
;
149 lcchild
->yoff
= lc
->yoff
;
150 if (lcchild
->type
!= LAYOUT_WINDOWPANE
)
151 layout_fix_offsets(lcchild
);
152 xoff
+= lcchild
->sx
+ 1;
156 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
157 lcchild
->xoff
= lc
->xoff
;
158 lcchild
->yoff
= yoff
;
159 if (lcchild
->type
!= LAYOUT_WINDOWPANE
)
160 layout_fix_offsets(lcchild
);
161 yoff
+= lcchild
->sy
+ 1;
166 /* Update pane offsets and sizes based on their cells. */
168 layout_fix_panes(struct window
*w
, u_int wsx
, u_int wsy
)
170 struct window_pane
*wp
;
171 struct layout_cell
*lc
;
174 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
175 if ((lc
= wp
->layout_cell
) == NULL
)
181 * Layout cells are limited by the smallest size of other cells
182 * within the same row or column; if this isn't the case
183 * resizing becomes difficult.
185 * However, panes do not have to take up their entire cell, so
186 * they can be cropped to the window edge if the layout
187 * overflows and they are partly visible.
189 * This stops cells being hidden unnecessarily.
193 * Work out the horizontal size. If the pane is actually
194 * outside the window or the entire pane is already visible,
197 if (lc
->xoff
>= wsx
|| lc
->xoff
+ lc
->sx
< wsx
)
206 * Similarly for the vertical size; the minimum vertical size
207 * is two because scroll regions cannot be one line.
209 if (lc
->yoff
>= wsy
|| lc
->yoff
+ lc
->sy
< wsy
)
217 window_pane_resize(wp
, sx
, sy
);
221 /* Count the number of available cells in a layout. */
223 layout_count_cells(struct layout_cell
*lc
)
225 struct layout_cell
*lcchild
;
229 case LAYOUT_WINDOWPANE
:
231 case LAYOUT_LEFTRIGHT
:
232 case LAYOUT_TOPBOTTOM
:
234 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
235 n
+= layout_count_cells(lcchild
);
238 fatalx("bad layout type");
242 /* Calculate how much size is available to be removed from a cell. */
244 layout_resize_check(struct layout_cell
*lc
, enum layout_type type
)
246 struct layout_cell
*lcchild
;
247 u_int available
, minimum
;
249 if (lc
->type
== LAYOUT_WINDOWPANE
) {
250 /* Space available in this cell only. */
251 if (type
== LAYOUT_LEFTRIGHT
)
256 if (available
> PANE_MINIMUM
)
257 available
-= PANE_MINIMUM
;
260 } else if (lc
->type
== type
) {
261 /* Same type: total of available space in all child cells. */
263 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
264 available
+= layout_resize_check(lcchild
, type
);
266 /* Different type: minimum of available space in child cells. */
268 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
269 available
= layout_resize_check(lcchild
, type
);
270 if (available
< minimum
)
280 * Adjust cell size evenly, including altering its children. This function
281 * expects the change to have already been bounded to the space available.
284 layout_resize_adjust(struct layout_cell
*lc
, enum layout_type type
, int change
)
286 struct layout_cell
*lcchild
;
288 /* Adjust the cell size. */
289 if (type
== LAYOUT_LEFTRIGHT
)
294 /* If this is a leaf cell, that is all that is necessary. */
295 if (type
== LAYOUT_WINDOWPANE
)
298 /* Child cell runs in a different direction. */
299 if (lc
->type
!= type
) {
300 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
)
301 layout_resize_adjust(lcchild
, type
, change
);
306 * Child cell runs in the same direction. Adjust each child equally
307 * until no further change is possible.
309 while (change
!= 0) {
310 TAILQ_FOREACH(lcchild
, &lc
->cells
, entry
) {
314 layout_resize_adjust(lcchild
, type
, 1);
318 if (layout_resize_check(lcchild
, type
) > 0) {
319 layout_resize_adjust(lcchild
, type
, -1);
326 /* Destroy a cell and redistribute the space. */
328 layout_destroy_cell(struct layout_cell
*lc
, struct layout_cell
**lcroot
)
330 struct layout_cell
*lcother
, *lcparent
;
333 * If no parent, this is the last pane so window close is imminent and
334 * there is no need to resize anything.
336 lcparent
= lc
->parent
;
337 if (lcparent
== NULL
) {
338 layout_free_cell(lc
);
343 /* Merge the space into the previous or next cell. */
344 if (lc
== TAILQ_FIRST(&lcparent
->cells
))
345 lcother
= TAILQ_NEXT(lc
, entry
);
347 lcother
= TAILQ_PREV(lc
, layout_cells
, entry
);
348 if (lcparent
->type
== LAYOUT_LEFTRIGHT
)
349 layout_resize_adjust(lcother
, lcparent
->type
, lc
->sx
+ 1);
351 layout_resize_adjust(lcother
, lcparent
->type
, lc
->sy
+ 1);
353 /* Remove this from the parent's list. */
354 TAILQ_REMOVE(&lcparent
->cells
, lc
, entry
);
355 layout_free_cell(lc
);
358 * If the parent now has one cell, remove the parent from the tree and
359 * replace it by that cell.
361 lc
= TAILQ_FIRST(&lcparent
->cells
);
362 if (TAILQ_NEXT(lc
, entry
) == NULL
) {
363 TAILQ_REMOVE(&lcparent
->cells
, lc
, entry
);
365 lc
->parent
= lcparent
->parent
;
366 if (lc
->parent
== NULL
) {
367 lc
->xoff
= 0; lc
->yoff
= 0;
370 TAILQ_REPLACE(&lc
->parent
->cells
, lcparent
, lc
, entry
);
372 layout_free_cell(lcparent
);
377 layout_init(struct window
*w
)
379 struct layout_cell
*lc
;
381 lc
= w
->layout_root
= layout_create_cell(NULL
);
382 layout_set_size(lc
, w
->sx
, w
->sy
, 0, 0);
383 layout_make_leaf(lc
, TAILQ_FIRST(&w
->panes
));
385 layout_fix_panes(w
, w
->sx
, w
->sy
);
389 layout_free(struct window
*w
)
391 layout_free_cell(w
->layout_root
);
394 /* Resize the entire layout after window resize. */
396 layout_resize(struct window
*w
, u_int sx
, u_int sy
)
398 struct layout_cell
*lc
= w
->layout_root
;
399 int xlimit
, ylimit
, xchange
, ychange
;
402 * Adjust horizontally. Do not attempt to reduce the layout lower than
403 * the minimum (more than the amount returned by layout_resize_check).
405 * This can mean that the window size is smaller than the total layout
406 * size: redrawing this is handled at a higher level, but it does leave
407 * a problem with growing the window size here: if the current size is
408 * < the minimum, growing proportionately by adding to each pane is
409 * wrong as it would keep the layout size larger than the window size.
410 * Instead, spread the difference between the minimum and the new size
411 * out proportionately - this should leave the layout fitting the new
414 xchange
= sx
- w
->sx
;
415 xlimit
= layout_resize_check(lc
, LAYOUT_LEFTRIGHT
);
416 if (xchange
< 0 && xchange
< -xlimit
)
419 if (sx
<= lc
->sx
) /* lc->sx is minimum possible */
422 xchange
= sx
- lc
->sx
;
425 layout_resize_adjust(lc
, LAYOUT_LEFTRIGHT
, xchange
);
427 /* Adjust vertically in a similar fashion. */
428 ychange
= sy
- w
->sy
;
429 ylimit
= layout_resize_check(lc
, LAYOUT_TOPBOTTOM
);
430 if (ychange
< 0 && ychange
< -ylimit
)
433 if (sy
<= lc
->sy
) /* lc->sy is minimum possible */
436 ychange
= sy
- lc
->sy
;
439 layout_resize_adjust(lc
, LAYOUT_TOPBOTTOM
, ychange
);
441 /* Fix cell offsets. */
442 layout_fix_offsets(lc
);
443 layout_fix_panes(w
, sx
, sy
);
446 /* Resize a single pane within the layout. */
448 layout_resize_pane(struct window_pane
*wp
, enum layout_type type
, int change
)
450 struct layout_cell
*lc
, *lcparent
;
453 lc
= wp
->layout_cell
;
455 /* Find next parent of the same type. */
456 lcparent
= lc
->parent
;
457 while (lcparent
!= NULL
&& lcparent
->type
!= type
) {
459 lcparent
= lc
->parent
;
461 if (lcparent
== NULL
)
464 /* If this is the last cell, move back one. */
465 if (lc
== TAILQ_LAST(&lcparent
->cells
, layout_cells
))
466 lc
= TAILQ_PREV(lc
, layout_cells
, entry
);
468 /* Grow or shrink the cell. */
470 while (needed
!= 0) {
472 size
= layout_resize_pane_grow(lc
, type
, needed
);
475 size
= layout_resize_pane_shrink(lc
, type
, needed
);
479 if (size
== 0) /* no more change possible */
483 /* Fix cell offsets. */
484 layout_fix_offsets(wp
->window
->layout_root
);
485 layout_fix_panes(wp
->window
, wp
->window
->sx
, wp
->window
->sy
);
489 layout_resize_pane_mouse(struct client
*c
, struct mouse_event
*mouse
)
492 struct window_pane
*wp
;
495 w
= c
->session
->curw
->window
;
498 if ((c
->last_mouse
.b
& MOUSE_BUTTON
) != MOUSE_UP
&&
499 (c
->last_mouse
.b
& MOUSE_RESIZE_PANE
)) {
500 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
501 if (wp
->xoff
+ wp
->sx
== c
->last_mouse
.x
&&
502 wp
->yoff
<= 1 + c
->last_mouse
.y
&&
503 wp
->yoff
+ wp
->sy
>= c
->last_mouse
.y
) {
504 layout_resize_pane(wp
, LAYOUT_LEFTRIGHT
,
505 mouse
->x
- c
->last_mouse
.x
);
508 if (wp
->yoff
+ wp
->sy
== c
->last_mouse
.y
&&
509 wp
->xoff
<= 1 + c
->last_mouse
.x
&&
510 wp
->xoff
+ wp
->sx
>= c
->last_mouse
.x
) {
511 layout_resize_pane(wp
, LAYOUT_TOPBOTTOM
,
512 mouse
->y
- c
->last_mouse
.y
);
517 server_redraw_window(w
);
518 } else if (mouse
->b
!= MOUSE_UP
&&
519 mouse
->b
== (mouse
->b
& MOUSE_BUTTON
)) {
520 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
521 if ((wp
->xoff
+ wp
->sx
== mouse
->x
&&
522 wp
->yoff
<= 1 + mouse
->y
&&
523 wp
->yoff
+ wp
->sy
>= mouse
->y
) ||
524 (wp
->yoff
+ wp
->sy
== mouse
->y
&&
525 wp
->xoff
<= 1 + mouse
->x
&&
526 wp
->xoff
+ wp
->sx
>= mouse
->x
)) {
532 mouse
->b
|= MOUSE_RESIZE_PANE
;
536 layout_resize_pane_grow(
537 struct layout_cell
*lc
, enum layout_type type
, int needed
)
539 struct layout_cell
*lcadd
, *lcremove
;
542 /* Growing. Always add to the current cell. */
545 /* Look towards the tail for a suitable cell for reduction. */
546 lcremove
= TAILQ_NEXT(lc
, entry
);
547 while (lcremove
!= NULL
) {
548 size
= layout_resize_check(lcremove
, type
);
551 lcremove
= TAILQ_NEXT(lcremove
, entry
);
554 /* If none found, look towards the head. */
555 if (lcremove
== NULL
) {
556 lcremove
= TAILQ_PREV(lc
, layout_cells
, entry
);
557 while (lcremove
!= NULL
) {
558 size
= layout_resize_check(lcremove
, type
);
561 lcremove
= TAILQ_PREV(lcremove
, layout_cells
, entry
);
563 if (lcremove
== NULL
)
567 /* Change the cells. */
568 if (size
> (u_int
) needed
)
570 layout_resize_adjust(lcadd
, type
, size
);
571 layout_resize_adjust(lcremove
, type
, -size
);
576 layout_resize_pane_shrink(
577 struct layout_cell
*lc
, enum layout_type type
, int needed
)
579 struct layout_cell
*lcadd
, *lcremove
;
582 /* Shrinking. Find cell to remove from by walking towards head. */
585 size
= layout_resize_check(lcremove
, type
);
588 lcremove
= TAILQ_PREV(lcremove
, layout_cells
, entry
);
589 } while (lcremove
!= NULL
);
590 if (lcremove
== NULL
)
593 /* And add onto the next cell (from the original cell). */
594 lcadd
= TAILQ_NEXT(lc
, entry
);
598 /* Change the cells. */
599 if (size
> (u_int
) -needed
)
601 layout_resize_adjust(lcadd
, type
, size
);
602 layout_resize_adjust(lcremove
, type
, -size
);
606 /* Assign window pane to newly split cell. */
608 layout_assign_pane(struct layout_cell
*lc
, struct window_pane
*wp
)
610 layout_make_leaf(lc
, wp
);
611 layout_fix_panes(wp
->window
, wp
->window
->sx
, wp
->window
->sy
);
615 * Split a pane into two. size is a hint, or -1 for default half/half
616 * split. This must be followed by layout_assign_pane before much else happens!
619 layout_split_pane(struct window_pane
*wp
, enum layout_type type
, int size
)
621 struct layout_cell
*lc
, *lcparent
, *lcnew
;
622 u_int sx
, sy
, xoff
, yoff
, size1
, size2
;
624 lc
= wp
->layout_cell
;
626 /* Copy the old cell size. */
632 /* Check there is enough space for the two new panes. */
634 case LAYOUT_LEFTRIGHT
:
635 if (sx
< PANE_MINIMUM
* 2 + 1)
638 case LAYOUT_TOPBOTTOM
:
639 if (sy
< PANE_MINIMUM
* 2 + 1)
643 fatalx("bad layout type");
646 if (lc
->parent
!= NULL
&& lc
->parent
->type
== type
) {
648 * If the parent exists and is of the same type as the split,
649 * create a new cell and insert it after this one.
652 /* Create the new child cell. */
653 lcnew
= layout_create_cell(lc
->parent
);
654 TAILQ_INSERT_AFTER(&lc
->parent
->cells
, lc
, lcnew
, entry
);
657 * Otherwise create a new parent and insert it.
660 /* Create and insert the replacement parent. */
661 lcparent
= layout_create_cell(lc
->parent
);
662 layout_make_node(lcparent
, type
);
663 layout_set_size(lcparent
, sx
, sy
, xoff
, yoff
);
664 if (lc
->parent
== NULL
)
665 wp
->window
->layout_root
= lcparent
;
667 TAILQ_REPLACE(&lc
->parent
->cells
, lc
, lcparent
, entry
);
669 /* Insert the old cell. */
670 lc
->parent
= lcparent
;
671 TAILQ_INSERT_HEAD(&lcparent
->cells
, lc
, entry
);
673 /* Create the new child cell. */
674 lcnew
= layout_create_cell(lcparent
);
675 TAILQ_INSERT_TAIL(&lcparent
->cells
, lcnew
, entry
);
678 /* Set new cell sizes. size is the target size or -1 for middle split,
679 * size1 is the size of the top/left and size2 the bottom/right.
682 case LAYOUT_LEFTRIGHT
:
684 size2
= ((sx
+ 1) / 2) - 1;
687 if (size2
< PANE_MINIMUM
)
688 size2
= PANE_MINIMUM
;
689 else if (size2
> sx
- 2)
691 size1
= sx
- 1 - size2
;
692 layout_set_size(lc
, size1
, sy
, xoff
, yoff
);
693 layout_set_size(lcnew
, size2
, sy
, xoff
+ lc
->sx
+ 1, yoff
);
695 case LAYOUT_TOPBOTTOM
:
697 size2
= ((sy
+ 1) / 2) - 1;
700 if (size2
< PANE_MINIMUM
)
701 size2
= PANE_MINIMUM
;
702 else if (size2
> sy
- 2)
704 size1
= sy
- 1 - size2
;
705 layout_set_size(lc
, sx
, size1
, xoff
, yoff
);
706 layout_set_size(lcnew
, sx
, size2
, xoff
, yoff
+ lc
->sy
+ 1);
709 fatalx("bad layout type");
712 /* Assign the panes. */
713 layout_make_leaf(lc
, wp
);
718 /* Destroy the cell associated with a pane. */
720 layout_close_pane(struct window_pane
*wp
)
722 /* Remove the cell. */
723 layout_destroy_cell(wp
->layout_cell
, &wp
->window
->layout_root
);
725 /* Fix pane offsets and sizes. */
726 if (wp
->window
->layout_root
!= NULL
) {
727 layout_fix_offsets(wp
->window
->layout_root
);
728 layout_fix_panes(wp
->window
, wp
->window
->sx
, wp
->window
->sy
);