Coverage Report

Created: 2026-07-16 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/layout.c
Line
Count
Source
1
/* $OpenBSD: layout.c,v 1.96 2026/07/15 13:02:33 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 * Copyright (c) 2016 Stephen Kent <smkent@smkent.net>
6
 *
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.
10
 *
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.
18
 */
19
20
#include <sys/types.h>
21
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "tmux.h"
26
27
/*
28
 * The window layout is a tree of cells each of which can be one of: a
29
 * left-right container for a list of cells, a top-bottom container for a list
30
 * of cells, or a container for a window pane. 'Node' will be used to refer to
31
 * a cell which contains a list of cells, and 'leaf' to refer to a cell that
32
 * contains a window pane. A leaf is considered to be 'tiled' if it is to be
33
 * drawn as a part of the tiled layout. A 'neighbour' is a sibling that is also
34
 * tiled or a node that contains a tiled leaf in a subtree. A cell's 'split'
35
 * size refers to the side that is shortened when splitting it, determined by
36
 * the parent's type.
37
 *
38
 * Each window has a pointer to the root of its layout tree (containing its
39
 * panes), every pane has a pointer back to the cell containing it, and each
40
 * cell a pointer to its parent cell. Every cell has a position in the root
41
 * layout tree. This position is retained through cell state changes such as
42
 * floating or hiding.
43
 */
44
45
static u_int  layout_resize_check(struct window *, struct layout_cell *,
46
        enum layout_type);
47
static int  layout_resize_pane_grow(struct window *, struct layout_cell *,
48
        enum layout_type, int, int);
49
static int  layout_resize_pane_shrink(struct window *, struct layout_cell *,
50
        enum layout_type, int);
51
static u_int  layout_new_pane_size(struct window *, u_int,
52
        struct layout_cell *, enum layout_type, u_int, u_int,
53
        u_int);
54
static int  layout_set_size_check(struct window *, struct layout_cell *,
55
        enum layout_type, int);
56
static void layout_resize_child_cells(struct window *,
57
        struct layout_cell *);
58
59
/* Initializes cell geometry to sentinel values. */
60
static void
61
layout_geometry_init(struct layout_geometry *lg)
62
0
{
63
0
  lg->sx = UINT_MAX;
64
0
  lg->sy = UINT_MAX;
65
0
  lg->xoff = INT_MAX;
66
0
  lg->yoff = INT_MAX;
67
0
}
68
69
/* Create a new layout cell. */
70
struct layout_cell *
71
layout_create_cell(struct layout_cell *lcparent)
72
0
{
73
0
  struct layout_cell  *lc;
74
75
0
  lc = xcalloc(1, sizeof *lc);
76
0
  lc->type = LAYOUT_WINDOWPANE;
77
0
  lc->parent = lcparent;
78
0
  TAILQ_INIT(&lc->cells);
79
80
0
  layout_geometry_init(&lc->g);
81
0
  layout_geometry_init(&lc->fg);
82
83
0
  return (lc);
84
0
}
85
86
/* Free a layout cell. */
87
void
88
layout_free_cell(struct layout_cell *lc, int only_nodes)
89
23.2k
{
90
23.2k
  struct layout_cell  *lcchild, *lcnext;
91
92
23.2k
  if (lc == NULL || (only_nodes && lc->type == LAYOUT_WINDOWPANE))
93
23.2k
    return;
94
95
0
  switch (lc->type) {
96
0
  case LAYOUT_LEFTRIGHT:
97
0
  case LAYOUT_TOPBOTTOM:
98
0
    lcchild = TAILQ_FIRST(&lc->cells);
99
0
    while (lcchild != NULL) {
100
0
      lcnext = TAILQ_NEXT(lcchild, entry);
101
0
      if (!only_nodes || lcchild->type != LAYOUT_WINDOWPANE) {
102
0
        TAILQ_REMOVE(&lc->cells, lcchild, entry);
103
0
        layout_free_cell(lcchild, only_nodes);
104
0
      }
105
0
      lcchild = lcnext;
106
0
    }
107
0
    break;
108
0
  case LAYOUT_WINDOWPANE:
109
0
    if (lc->wp != NULL) {
110
0
      lc->wp->layout_cell->parent = NULL;
111
0
      lc->wp->layout_cell = NULL;
112
0
    }
113
0
    break;
114
0
  }
115
116
0
  free(lc);
117
0
}
118
119
/* Log a cell. */
120
void
121
layout_print_cell(struct layout_cell *lc, const char *hdr, u_int n)
122
0
{
123
0
  struct layout_cell  *lcchild;
124
0
  const char    *type;
125
126
0
  if (lc == NULL)
127
0
    return;
128
129
0
  switch (lc->type) {
130
0
  case LAYOUT_LEFTRIGHT:
131
0
    type = "LEFTRIGHT";
132
0
    break;
133
0
  case LAYOUT_TOPBOTTOM:
134
0
    type = "TOPBOTTOM";
135
0
    break;
136
0
  case LAYOUT_WINDOWPANE:
137
0
    type = "WINDOWPANE";
138
0
    break;
139
0
  default:
140
0
    type = "UNKNOWN";
141
0
    break;
142
0
  }
143
0
  log_debug("%s:%*s%p type %s [parent %p] wp=%p [%d,%d %ux%u]", hdr, n,
144
0
      " ", lc, type, lc->parent, lc->wp, lc->g.xoff, lc->g.yoff, lc->g.sx,
145
0
      lc->g.sy);
146
0
  switch (lc->type) {
147
0
  case LAYOUT_LEFTRIGHT:
148
0
  case LAYOUT_TOPBOTTOM:
149
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
150
0
      layout_print_cell(lcchild, hdr, n + 1);
151
0
    break;
152
0
  case LAYOUT_WINDOWPANE:
153
0
    break;
154
0
  }
155
0
}
156
157
/* Search for a cell by the border position. */
158
struct layout_cell *
159
layout_search_by_border(struct layout_cell *lc, u_int x, u_int y)
160
0
{
161
0
  struct layout_cell  *lcchild, *last = NULL;
162
163
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
164
0
    if ((int)x >= lcchild->g.xoff &&
165
0
        (int)x < lcchild->g.xoff + (int)lcchild->g.sx &&
166
0
        (int)y >= lcchild->g.yoff &&
167
0
        (int)y < lcchild->g.yoff + (int)lcchild->g.sy) {
168
      /* Inside the cell - recurse. */
169
0
      return (layout_search_by_border(lcchild, x, y));
170
0
    }
171
172
0
    if (last == NULL) {
173
0
      last = lcchild;
174
0
      continue;
175
0
    }
176
177
0
    switch (lc->type) {
178
0
    case LAYOUT_LEFTRIGHT:
179
0
      if ((int)x < lcchild->g.xoff &&
180
0
          (int)x >= last->g.xoff + (int)last->g.sx)
181
0
        return (last);
182
0
      break;
183
0
    case LAYOUT_TOPBOTTOM:
184
0
      if ((int)y < lcchild->g.yoff &&
185
0
          (int)y >= last->g.yoff + (int)last->g.sy)
186
0
        return (last);
187
0
      break;
188
0
    case LAYOUT_WINDOWPANE:
189
0
      break;
190
0
    }
191
192
0
    last = lcchild;
193
0
  }
194
195
0
  return (NULL);
196
0
}
197
198
/* Set cell size. */
199
void
200
layout_set_size(struct layout_cell *lc, u_int sx, u_int sy, int xoff, int yoff)
201
0
{
202
0
  lc->g.sx = sx;
203
0
  lc->g.sy = sy;
204
205
0
  lc->g.xoff = xoff;
206
0
  lc->g.yoff = yoff;
207
0
}
208
209
/* Make a cell a leaf cell. */
210
void
211
layout_make_leaf(struct layout_cell *lc, struct window_pane *wp)
212
0
{
213
0
  lc->type = LAYOUT_WINDOWPANE;
214
215
0
  TAILQ_INIT(&lc->cells);
216
217
0
  wp->layout_cell = lc;
218
0
  lc->wp = wp;
219
0
}
220
221
/* Make a cell a node cell. */
222
void
223
layout_make_node(struct layout_cell *lc, enum layout_type type)
224
0
{
225
0
  if (type == LAYOUT_WINDOWPANE)
226
0
    fatalx("bad layout type");
227
0
  lc->type = type;
228
229
0
  TAILQ_INIT(&lc->cells);
230
231
0
  if (lc->wp != NULL)
232
0
    lc->wp->layout_cell = NULL;
233
0
  lc->wp = NULL;
234
0
}
235
236
/* Fix z-indexes. */
237
void
238
layout_fix_zindexes(struct window *w, struct layout_cell *lc)
239
0
{
240
0
  struct layout_cell  *lcchild;
241
242
0
  if (lc == NULL)
243
0
    return;
244
245
0
  switch (lc->type) {
246
0
  case LAYOUT_WINDOWPANE:
247
0
    TAILQ_INSERT_TAIL(&w->z_index, lc->wp, zentry);
248
0
    break;
249
0
  case LAYOUT_LEFTRIGHT:
250
0
  case LAYOUT_TOPBOTTOM:
251
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
252
0
      layout_fix_zindexes(w, lcchild);
253
0
    return;
254
0
  default:
255
0
    fatalx("bad layout type");
256
0
  }
257
0
}
258
259
int
260
layout_cell_is_tiled(struct layout_cell *lc)
261
0
{
262
0
  int is_leaf = lc->type == LAYOUT_WINDOWPANE;
263
0
  int is_floating = lc->flags & LAYOUT_CELL_FLOATING;
264
265
0
  return is_leaf && !is_floating;
266
0
}
267
268
static int
269
layout_cell_has_tiled_child(struct layout_cell *lc)
270
0
{
271
0
  struct layout_cell      *lcchild;
272
273
0
  if (lc->type == LAYOUT_WINDOWPANE)
274
0
    return (0);
275
276
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
277
0
    if (layout_cell_is_tiled(lcchild) ||
278
0
        layout_cell_has_tiled_child(lcchild))
279
0
      return (1);
280
0
  }
281
0
  return (0);
282
0
}
283
284
static int
285
layout_cell_is_first_tiled(struct layout_cell *lc)
286
0
{
287
0
  struct layout_cell      *lcchild, *lcparent = lc->parent;
288
289
0
  if (lcparent == NULL)
290
0
    return (layout_cell_is_tiled(lc));
291
292
0
  TAILQ_FOREACH(lcchild, &lcparent->cells, entry) {
293
0
    if (layout_cell_is_tiled(lcchild) ||
294
0
        layout_cell_has_tiled_child(lcchild))
295
0
      break;
296
0
  }
297
298
0
  return (lcchild == lc);
299
0
}
300
301
static struct layout_cell *
302
layout_cell_get_first_tiled(struct layout_cell *lc)
303
0
{
304
0
  struct layout_cell  *lcchild, *lcchild2;
305
306
0
  if (layout_cell_is_tiled(lc))
307
0
    return (lc);
308
0
  if (lc->type == LAYOUT_WINDOWPANE)
309
0
    return (NULL);
310
311
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
312
0
    if (layout_cell_is_tiled(lcchild))
313
0
      return (lcchild);
314
0
    if (lcchild->type != LAYOUT_WINDOWPANE) {
315
0
      lcchild2 = layout_cell_get_first_tiled(lcchild);
316
0
      if (lcchild2 != NULL)
317
0
        return (lcchild2);
318
0
    }
319
0
  }
320
0
  return (NULL);
321
0
}
322
323
/* Fix cell offsets for a child cell. */
324
static void
325
layout_fix_offsets1(struct layout_cell *lc)
326
0
{
327
0
  struct layout_cell  *lcchild;
328
0
  int      xoff, yoff;
329
330
0
  if (lc->type == LAYOUT_LEFTRIGHT) {
331
0
    xoff = lc->g.xoff;
332
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
333
0
      if (!layout_cell_is_tiled(lcchild) &&
334
0
          !layout_cell_has_tiled_child(lcchild))
335
0
        continue;
336
0
      lcchild->g.xoff = xoff;
337
0
      lcchild->g.yoff = lc->g.yoff;
338
0
      if (lcchild->type != LAYOUT_WINDOWPANE)
339
0
        layout_fix_offsets1(lcchild);
340
0
      xoff += lcchild->g.sx + 1;
341
0
    }
342
0
  } else {
343
0
    yoff = lc->g.yoff;
344
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
345
0
      if (!layout_cell_is_tiled(lcchild) &&
346
0
          !layout_cell_has_tiled_child(lcchild))
347
0
        continue;
348
0
      lcchild->g.xoff = lc->g.xoff;
349
0
      lcchild->g.yoff = yoff;
350
0
      if (lcchild->type != LAYOUT_WINDOWPANE)
351
0
        layout_fix_offsets1(lcchild);
352
0
      yoff += lcchild->g.sy + 1;
353
0
    }
354
0
  }
355
0
}
356
357
/* Update cell offsets based on their sizes. */
358
void
359
layout_fix_offsets(struct window *w)
360
0
{
361
0
  struct layout_cell  *lc = w->layout_root;
362
363
  /* Root consists of a single floating cell */
364
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
365
0
    return;
366
367
0
  lc->g.xoff = 0;
368
0
  lc->g.yoff = 0;
369
370
0
  layout_fix_offsets1(lc);
371
0
}
372
373
static int
374
layout_cell_is_last_tiled(struct layout_cell *lc)
375
0
{
376
0
  struct layout_cell      *lcchild, *lcparent = lc->parent;
377
378
0
  if (lcparent == NULL)
379
0
    return (layout_cell_is_tiled(lc));
380
381
0
  TAILQ_FOREACH_REVERSE(lcchild, &lcparent->cells, layout_cells, entry) {
382
0
    if (layout_cell_is_tiled(lcchild) ||
383
0
        layout_cell_has_tiled_child(lcchild))
384
0
      break;
385
0
  }
386
387
0
  return (lcchild == lc);
388
0
}
389
390
/* Is this a top cell? */
391
static int
392
layout_cell_is_top(struct layout_cell *root, struct layout_cell *lc)
393
0
{
394
0
  struct layout_cell  *next;
395
396
0
  while (lc != root) {
397
0
    next = lc->parent;
398
0
    if (next == NULL)
399
0
      return (0);
400
0
    if (next->type == LAYOUT_TOPBOTTOM &&
401
0
        !layout_cell_is_first_tiled(lc))
402
0
      return (0);
403
0
    lc = next;
404
0
  }
405
0
  return (1);
406
0
}
407
408
/* Is this a bottom cell? */
409
static int
410
layout_cell_is_bottom(struct layout_cell *root, struct layout_cell *lc)
411
0
{
412
0
  struct layout_cell  *next;
413
414
0
  while (lc != root) {
415
0
    next = lc->parent;
416
0
    if (next == NULL)
417
0
      return (0);
418
0
    if (next->type == LAYOUT_TOPBOTTOM &&
419
0
        !layout_cell_is_last_tiled(lc))
420
0
      return (0);
421
0
    lc = next;
422
0
  }
423
0
  return (1);
424
0
}
425
426
/*
427
 * Returns 1 if we need to add an extra line for the pane status line. This is
428
 * the case for the most upper or lower panes only.
429
 */
430
int
431
layout_add_horizontal_border(struct layout_cell *root, struct layout_cell *lc,
432
    int status)
433
0
{
434
0
  if (status == PANE_STATUS_TOP)
435
0
    return (layout_cell_is_top(root, lc));
436
0
  if (status == PANE_STATUS_BOTTOM)
437
0
    return (layout_cell_is_bottom(root, lc));
438
0
  return (0);
439
0
}
440
441
/* Update pane offsets and sizes based on their cells. */
442
void
443
layout_fix_panes(struct window *w, struct window_pane *skip)
444
716
{
445
716
  struct window_pane  *wp;
446
716
  struct layout_cell  *lc, *root = w->layout_root;
447
716
  int      status, sb_w, sb_pad;
448
716
  int      old_xoff, old_yoff, changed = 0;
449
716
  u_int      sx, sy, old_sx, old_sy;
450
451
716
  TAILQ_FOREACH(wp, &w->panes, entry) {
452
716
    if ((lc = wp->layout_cell) == NULL || wp == skip)
453
716
      continue;
454
455
0
    old_xoff = wp->xoff;
456
0
    old_yoff = wp->yoff;
457
0
    old_sx = wp->sx;
458
0
    old_sy = wp->sy;
459
460
0
    wp->xoff = lc->g.xoff;
461
0
    wp->yoff = lc->g.yoff;
462
0
    sx = lc->g.sx;
463
0
    sy = lc->g.sy;
464
465
0
    status = window_pane_get_pane_status(wp);
466
0
    if (!window_pane_is_floating(wp) &&
467
0
        layout_add_horizontal_border(root, lc, status)) {
468
0
      if (status == PANE_STATUS_TOP)
469
0
        wp->yoff++;
470
0
      if (sy > 1)
471
0
        sy--;
472
0
    }
473
474
0
    if (window_pane_scrollbar_reserve(wp)) {
475
0
      sb_w = wp->scrollbar_style.width;
476
0
      sb_pad = wp->scrollbar_style.pad;
477
0
      if (sb_w < 1)
478
0
        sb_w = 1;
479
0
      if (sb_pad < 0)
480
0
        sb_pad = 0;
481
0
      if (w->sb_pos == PANE_SCROLLBARS_LEFT) {
482
0
        if ((int)sx - sb_w - sb_pad < PANE_MINIMUM) {
483
0
          wp->xoff = wp->xoff +
484
0
              (int)sx - PANE_MINIMUM;
485
0
          sx = PANE_MINIMUM;
486
0
        } else {
487
0
          sx = sx - sb_w - sb_pad;
488
0
          wp->xoff = wp->xoff + sb_w + sb_pad;
489
0
        }
490
0
      } else /* sb_pos == PANE_SCROLLBARS_RIGHT */
491
0
        if ((int)sx - sb_w - sb_pad < PANE_MINIMUM)
492
0
          sx = PANE_MINIMUM;
493
0
        else
494
0
          sx = sx - sb_w - sb_pad;
495
0
      wp->flags |= PANE_REDRAWSCROLLBAR;
496
0
    }
497
498
0
    window_pane_resize(wp, sx, sy);
499
500
0
    if (wp->xoff != old_xoff ||
501
0
        wp->yoff != old_yoff ||
502
0
        wp->sx != old_sx ||
503
0
        wp->sy != old_sy)
504
0
      changed = 1;
505
0
  }
506
716
  if (changed)
507
0
    redraw_invalidate_scene(w);
508
716
}
509
510
/* Count the number of available cells in a layout. */
511
u_int
512
layout_count_cells(struct layout_cell *lc)
513
0
{
514
0
  struct layout_cell  *lcchild;
515
0
  u_int      count = 0;
516
517
0
  switch (lc->type) {
518
0
  case LAYOUT_WINDOWPANE:
519
0
    return (1);
520
0
  case LAYOUT_LEFTRIGHT:
521
0
  case LAYOUT_TOPBOTTOM:
522
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
523
0
      count += layout_count_cells(lcchild);
524
0
    return (count);
525
0
  default:
526
0
    fatalx("bad layout type");
527
0
  }
528
0
}
529
530
/* Calculate how much size is available to be removed from a cell. */
531
static u_int
532
layout_resize_check(struct window *w, struct layout_cell *lc,
533
    enum layout_type type)
534
0
{
535
0
  struct layout_cell  *lcchild, *root = w->layout_root;
536
0
  struct style    *sb_style = &w->active->scrollbar_style;
537
0
  u_int      available, minimum;
538
0
  int      status;
539
540
0
  status = window_get_pane_status(w);
541
542
0
  if (lc->type == LAYOUT_WINDOWPANE) {
543
    /* Space available in this cell only. */
544
0
    if (type == LAYOUT_LEFTRIGHT) {
545
0
      available = lc->g.sx;
546
0
      if (w->sb == PANE_SCROLLBARS_ALWAYS)
547
0
        minimum = PANE_MINIMUM + sb_style->width +
548
0
            sb_style->pad;
549
0
      else
550
0
        minimum = PANE_MINIMUM;
551
0
    } else {
552
0
      available = lc->g.sy;
553
0
      if (layout_add_horizontal_border(root, lc, status))
554
0
        minimum = PANE_MINIMUM + 1;
555
0
      else
556
0
        minimum = PANE_MINIMUM;
557
0
    }
558
0
    if (available > minimum)
559
0
      available -= minimum;
560
0
    else
561
0
      available = 0;
562
0
  } else if (lc->type == type) {
563
    /* Same type: total of available space in all child cells. */
564
0
    available = 0;
565
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
566
0
      available += layout_resize_check(w, lcchild, type);
567
0
  } else {
568
    /* Different type: minimum of available space in child cells. */
569
0
    minimum = UINT_MAX;
570
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
571
0
      available = layout_resize_check(w, lcchild, type);
572
0
      if (available < minimum)
573
0
        minimum = available;
574
0
    }
575
0
    available = minimum;
576
0
  }
577
578
0
  return (available);
579
0
}
580
581
/*
582
 * Adjust cell size evenly, including altering its children. This function
583
 * expects the change to have already been bounded to the space available.
584
 */
585
void
586
layout_resize_adjust(struct window *w, struct layout_cell *lc,
587
    enum layout_type type, int change)
588
0
{
589
0
  struct layout_cell  *lcchild;
590
0
  int      changed;
591
592
  /* Adjust the cell size. */
593
0
  if (type == LAYOUT_LEFTRIGHT)
594
0
    lc->g.sx += change;
595
0
  else
596
0
    lc->g.sy += change;
597
598
  /* If this is a leaf cell, that is all that is necessary. */
599
0
  if (type == LAYOUT_WINDOWPANE)
600
0
    return;
601
602
  /* Child cell runs in a different direction. */
603
0
  if (lc->type != type) {
604
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
605
0
      if (!layout_cell_is_tiled(lcchild) &&
606
0
          !layout_cell_has_tiled_child(lcchild))
607
0
        continue;
608
0
      layout_resize_adjust(w, lcchild, type, change);
609
0
    }
610
0
    return;
611
0
  }
612
613
  /*
614
   * If a node doesn't contain any tiled cells, there is nothing to do.
615
   */
616
0
  if (!layout_cell_has_tiled_child(lc))
617
0
    return;
618
619
  /*
620
   * Child cell runs in the same direction. Adjust each child equally
621
   * until no further change is possible.
622
   */
623
0
  while (change != 0) {
624
0
    changed = 0;
625
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
626
0
      if (change == 0)
627
0
        break;
628
0
      if (!layout_cell_is_tiled(lcchild) &&
629
0
          !layout_cell_has_tiled_child(lcchild))
630
0
        continue;
631
0
      if (change > 0) {
632
0
        layout_resize_adjust(w, lcchild, type, 1);
633
0
        change--;
634
0
        changed = 1;
635
0
        continue;
636
0
      }
637
0
      if (layout_resize_check(w, lcchild, type) > 0) {
638
0
        layout_resize_adjust(w, lcchild, type, -1);
639
0
        change++;
640
0
        changed = 1;
641
0
      }
642
0
    }
643
0
    if (!changed)
644
0
      break;
645
0
  }
646
0
}
647
648
/* Resizes a cell to a specified size */
649
void
650
layout_resize_set_size(struct window *w, struct layout_cell *lc,
651
    enum layout_type type, u_int size)
652
0
{
653
0
  int change;
654
655
0
  if (type == LAYOUT_LEFTRIGHT)
656
0
    change = size - lc->g.sx;
657
0
  else
658
0
    change = size - lc->g.sy;
659
0
  layout_resize_adjust(w, lc, type, change);
660
0
}
661
662
/* Find and return the nearest neighbour to a cell in a specific direction. */
663
static struct layout_cell *
664
layout_cell_get_neighbour_direction(struct layout_cell *lc, int direction)
665
0
{
666
0
  struct layout_cell  *lcn = lc;
667
668
0
  while (1) {
669
0
    if (direction)
670
0
      lcn = TAILQ_NEXT(lcn, entry);
671
0
    else
672
0
      lcn = TAILQ_PREV(lcn, layout_cells, entry);
673
674
0
    if (lcn == NULL ||
675
0
        layout_cell_is_tiled(lcn) ||
676
0
        layout_cell_has_tiled_child(lcn))
677
0
      return (lcn);
678
0
  }
679
0
}
680
681
/*
682
 * Find and return the nearest neighbour. Prefers cells "after" the specified
683
 * cell. This behavior defines how cell dimensions are redistributed when a cell
684
 * is hidden/shown and floated/tiled.
685
 */
686
struct layout_cell *
687
layout_cell_get_neighbour(struct layout_cell *lc)
688
0
{
689
0
  struct layout_cell  *lcother, *lcparent = lc->parent;
690
0
  int      direction = 1;
691
692
0
  if (lcparent == NULL)
693
0
    return (NULL);
694
695
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
696
0
    direction = !direction;
697
698
0
  lcother = layout_cell_get_neighbour_direction(lc, direction);
699
0
  if (lcother == NULL)
700
0
    lcother = layout_cell_get_neighbour_direction(lc, !direction);
701
702
0
  return (lcother);
703
0
}
704
705
706
/* Destroy a cell and redistribute the space. */
707
void
708
layout_destroy_cell(struct window *w, struct layout_cell *lc,
709
    struct layout_cell **lcroot)
710
0
{
711
0
  struct layout_cell  *lcother = NULL, *lcparent;
712
0
  int      change;
713
714
  /* If no parent, this is the last pane in a window. */
715
0
  lcparent = lc->parent;
716
0
  if (lcparent == NULL) {
717
0
    if (lc->wp != NULL)
718
0
      *lcroot = NULL;
719
0
    layout_free_cell(lc, 0);
720
0
    return;
721
0
  }
722
723
0
  if (!layout_cell_is_tiled(lc)) {
724
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
725
0
    layout_free_cell(lc, 0);
726
0
    goto out;
727
0
  }
728
729
0
  lcother = layout_cell_get_neighbour(lc);
730
0
  if (lcother != NULL) {
731
0
    if (lcparent->type == LAYOUT_LEFTRIGHT)
732
0
      change = lc->g.sx + 1;
733
0
    else
734
0
      change = lc->g.sy + 1;
735
0
    layout_resize_adjust(w, lcother, lcparent->type, change);
736
0
  } else
737
0
    layout_remove_tile(w, lcparent);
738
739
  /* Remove this from the parent's list. */
740
0
  TAILQ_REMOVE(&lcparent->cells, lc, entry);
741
0
  layout_free_cell(lc, 0);
742
743
0
out:
744
  /*
745
   * If the parent now has one cell, remove the parent from the tree and
746
   * replace it by that cell.
747
   */
748
0
  lc = TAILQ_FIRST(&lcparent->cells);
749
0
  if (lc != NULL && TAILQ_NEXT(lc, entry) == NULL) {
750
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
751
752
0
    lc->parent = lcparent->parent;
753
0
    if (lc->parent == NULL) {
754
0
      if (layout_cell_is_tiled(lc)) {
755
0
        lc->g.xoff = 0;
756
0
        lc->g.yoff = 0;
757
0
      }
758
0
      *lcroot = lc;
759
0
    } else
760
0
      TAILQ_REPLACE(&lc->parent->cells, lcparent, lc, entry);
761
762
0
    layout_free_cell(lcparent, 0);
763
0
  }
764
0
}
765
766
/* Initialize layout for pane. */
767
void
768
layout_init(struct window *w, struct window_pane *wp)
769
0
{
770
0
  struct layout_cell  *lc;
771
772
0
  lc = w->layout_root = layout_create_cell(NULL);
773
0
  layout_set_size(lc, w->sx, w->sy, 0, 0);
774
0
  layout_make_leaf(lc, wp);
775
0
  layout_fix_panes(w, NULL);
776
0
}
777
778
/* Free layout for pane. */
779
void
780
layout_free(struct window *w, int only_nodes)
781
0
{
782
0
  layout_free_cell(w->layout_root, only_nodes);
783
0
}
784
785
/* Resize the entire layout after window resize. */
786
void
787
layout_resize(struct window *w, u_int sx, u_int sy)
788
0
{
789
0
  struct layout_cell  *lc = w->layout_root;
790
0
  int      xlimit, ylimit, xchange, ychange;
791
792
  /*
793
   * Adjust horizontally. Do not attempt to reduce the layout lower than
794
   * the minimum (more than the amount returned by layout_resize_check).
795
   *
796
   * This can mean that the window size is smaller than the total layout
797
   * size: redrawing this is handled at a higher level, but it does leave
798
   * a problem with growing the window size here: if the current size is
799
   * < the minimum, growing proportionately by adding to each pane is
800
   * wrong as it would keep the layout size larger than the window size.
801
   * Instead, spread the difference between the minimum and the new size
802
   * out proportionately - this should leave the layout fitting the new
803
   * window size.
804
   */
805
0
  if (lc->type == LAYOUT_WINDOWPANE && (lc->flags & LAYOUT_CELL_FLOATING))
806
0
    return;
807
0
  xchange = sx - lc->g.sx;
808
0
  xlimit = layout_resize_check(w, lc, LAYOUT_LEFTRIGHT);
809
0
  if (xchange < 0 && xchange < -xlimit)
810
0
    xchange = -xlimit;
811
0
  if (xlimit == 0) {
812
0
    if (sx <= lc->g.sx) /* lc->g.sx is minimum possible */
813
0
      xchange = 0;
814
0
    else
815
0
      xchange = sx - lc->g.sx;
816
0
  }
817
0
  if (xchange != 0)
818
0
    layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, xchange);
819
820
  /* Adjust vertically in a similar fashion. */
821
0
  ychange = sy - lc->g.sy;
822
0
  ylimit = layout_resize_check(w, lc, LAYOUT_TOPBOTTOM);
823
0
  if (ychange < 0 && ychange < -ylimit)
824
0
    ychange = -ylimit;
825
0
  if (ylimit == 0) {
826
0
    if (sy <= lc->g.sy) /* lc->g.sy is minimum possible */
827
0
      ychange = 0;
828
0
    else
829
0
      ychange = sy - lc->g.sy;
830
0
  }
831
0
  if (ychange != 0)
832
0
    layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, ychange);
833
834
  /* Fix cell offsets. */
835
0
  layout_fix_offsets(w);
836
0
  layout_fix_panes(w, NULL);
837
0
}
838
839
/* Resize a pane to an absolute size. */
840
void
841
layout_resize_pane_to(struct window_pane *wp, enum layout_type type,
842
    u_int new_size)
843
0
{
844
0
  struct layout_cell     *lc, *lcparent;
845
0
  int     change, size;
846
847
0
  lc = wp->layout_cell;
848
849
  /* Find next parent of the same type. */
850
0
  lcparent = lc->parent;
851
0
  while (lcparent != NULL && lcparent->type != type) {
852
0
    lc = lcparent;
853
0
    lcparent = lc->parent;
854
0
  }
855
0
  if (lcparent == NULL)
856
0
    return;
857
858
  /* Work out the size adjustment. */
859
0
  if (type == LAYOUT_LEFTRIGHT)
860
0
    size = lc->g.sx;
861
0
  else
862
0
    size = lc->g.sy;
863
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
864
0
    change = size - new_size;
865
0
  else
866
0
    change = new_size - size;
867
868
  /* Resize the pane. */
869
0
  layout_resize_pane(wp, type, change, 1);
870
0
}
871
872
/* Resize a floating pane to an absolute size. */
873
int
874
layout_resize_floating_pane_to(struct window_pane *wp, enum layout_type type,
875
    u_int size, char **cause)
876
0
{
877
0
  struct layout_cell  *lc = wp->layout_cell;
878
879
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
880
0
    *cause = xstrdup("pane is not floating");
881
0
    return (-1);
882
0
  }
883
884
0
  if (window_pane_get_pane_lines(wp) != PANE_LINES_NONE &&
885
0
      size >= PANE_MINIMUM + 2)
886
0
    size -= 2;
887
0
  if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
888
0
    *cause = xstrdup("size is too big or too small");
889
0
    return (-1);
890
0
  }
891
892
0
  if (type == LAYOUT_TOPBOTTOM) {
893
0
    if (lc->g.sy == size)
894
0
      return (0);
895
0
    lc->g.sy = size;
896
0
  } else {
897
0
    if (lc->g.sx == size)
898
0
      return (0);
899
0
    lc->g.sx = size;
900
0
  }
901
0
  redraw_invalidate_scene(wp->window);
902
0
  return (0);
903
0
}
904
905
/* Resize a floating pane relative to its current size. */
906
int
907
layout_resize_floating_pane(struct window_pane *wp, enum layout_type type,
908
    int change, int opposite, char **cause)
909
0
{
910
0
  struct layout_cell  *lc = wp->layout_cell;
911
0
  u_int      size;
912
913
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
914
0
    *cause = xstrdup("pane is not floating");
915
0
    return (-1);
916
0
  }
917
0
  if (change == 0)
918
0
    return (0);
919
920
0
  if (type == LAYOUT_TOPBOTTOM) {
921
0
    size = lc->g.sy + change;
922
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
923
0
      *cause = xstrdup("change is too big or too small");
924
0
      return (-1);
925
0
    }
926
0
    lc->g.sy = size;
927
0
    if (opposite)
928
0
      lc->g.yoff -= change;
929
0
  } else {
930
0
    size = lc->g.sx + change;
931
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
932
0
      *cause = xstrdup("change is too big or too small");
933
0
      return (-1);
934
0
    }
935
0
    lc->g.sx = size;
936
0
    if (opposite)
937
0
      lc->g.xoff -= change;
938
0
  }
939
0
  redraw_invalidate_scene(wp->window);
940
0
  return (0);
941
0
}
942
943
/* Resize a layout cell. */
944
void
945
layout_resize_layout(struct window *w, struct layout_cell *lc,
946
    enum layout_type type, int change, int opposite)
947
0
{
948
0
  int needed, size;
949
950
  /* Grow or shrink the cell. */
951
0
  needed = change;
952
0
  while (needed != 0) {
953
0
    if (change > 0) {
954
0
      size = layout_resize_pane_grow(w, lc, type, needed,
955
0
          opposite);
956
0
      needed -= size;
957
0
    } else {
958
0
      size = layout_resize_pane_shrink(w, lc, type, needed);
959
0
      needed += size;
960
0
    }
961
962
0
    if (size == 0) /* no more change possible */
963
0
      break;
964
0
  }
965
966
  /* Fix cell offsets. */
967
0
  layout_fix_offsets(w);
968
0
  layout_fix_panes(w, NULL);
969
0
  events_fire_window("window-layout-changed", w);
970
0
}
971
972
/* Resize a single pane within the layout. */
973
void
974
layout_resize_pane(struct window_pane *wp, enum layout_type type, int change,
975
    int opposite)
976
0
{
977
0
  struct layout_cell  *lc = wp->layout_cell, *lcparent;
978
979
  /* Find next parent of the same type. */
980
0
  lcparent = lc->parent;
981
0
  while (lcparent != NULL && lcparent->type != type) {
982
0
    lc = lcparent;
983
0
    lcparent = lc->parent;
984
0
  }
985
0
  if (lcparent == NULL)
986
0
    return;
987
988
  /* If this is the last cell, move back one. */
989
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells)) {
990
0
    do
991
0
      lc = TAILQ_PREV(lc, layout_cells, entry);
992
0
    while (lc->flags & LAYOUT_CELL_FLOATING);
993
0
  }
994
995
0
  layout_resize_layout(wp->window, lc, type, change, opposite);
996
0
}
997
998
/* Helper function to grow pane. */
999
static int
1000
layout_resize_pane_grow(struct window *w, struct layout_cell *lc,
1001
    enum layout_type type, int needed, int opposite)
1002
0
{
1003
0
  struct layout_cell  *lcadd, *lcremove;
1004
0
  u_int      size = 0;
1005
1006
  /* Growing. Always add to the current cell. */
1007
0
  lcadd = lc;
1008
1009
  /* Look towards the tail for a suitable cell for reduction. */
1010
0
  lcremove = TAILQ_NEXT(lc, entry);
1011
0
  while (lcremove != NULL) {
1012
0
    size = layout_resize_check(w, lcremove, type);
1013
0
    if (size > 0)
1014
0
      break;
1015
0
    lcremove = TAILQ_NEXT(lcremove, entry);
1016
0
  }
1017
1018
  /* If none found, look towards the head. */
1019
0
  if (opposite && lcremove == NULL) {
1020
0
    lcremove = TAILQ_PREV(lc, layout_cells, entry);
1021
0
    while (lcremove != NULL) {
1022
0
      size = layout_resize_check(w, lcremove, type);
1023
0
      if (size > 0)
1024
0
        break;
1025
0
      lcremove = TAILQ_PREV(lcremove, layout_cells, entry);
1026
0
    }
1027
0
  }
1028
0
  if (lcremove == NULL)
1029
0
    return (0);
1030
1031
  /* Change the cells. */
1032
0
  if (size > (u_int) needed)
1033
0
    size = needed;
1034
0
  layout_resize_adjust(w, lcadd, type, size);
1035
0
  layout_resize_adjust(w, lcremove, type, -size);
1036
0
  return (size);
1037
0
}
1038
1039
/* Helper function to shrink pane. */
1040
static int
1041
layout_resize_pane_shrink(struct window *w, struct layout_cell *lc,
1042
    enum layout_type type, int needed)
1043
0
{
1044
0
  struct layout_cell  *lcadd, *lcremove;
1045
0
  u_int      size;
1046
1047
  /* Shrinking. Find cell to remove from by walking towards head. */
1048
0
  lcremove = lc;
1049
0
  do {
1050
0
    size = layout_resize_check(w, lcremove, type);
1051
0
    if (size != 0)
1052
0
      break;
1053
0
    lcremove = TAILQ_PREV(lcremove, layout_cells, entry);
1054
0
  } while (lcremove != NULL);
1055
0
  if (lcremove == NULL)
1056
0
    return (0);
1057
1058
  /* And add onto the next cell (from the original cell). */
1059
0
  lcadd = TAILQ_NEXT(lc, entry);
1060
0
  if (lcadd == NULL)
1061
0
    return (0);
1062
1063
  /* Change the cells. */
1064
0
  if (size > (u_int) -needed)
1065
0
    size = -needed;
1066
0
  layout_resize_adjust(w, lcadd, type, size);
1067
0
  layout_resize_adjust(w, lcremove, type, -size);
1068
0
  return (size);
1069
0
}
1070
1071
/* Assign window pane to new cell. */
1072
void
1073
layout_assign_pane(struct layout_cell *lc, struct window_pane *wp,
1074
    int do_not_resize)
1075
0
{
1076
0
  layout_make_leaf(lc, wp);
1077
0
  if (do_not_resize)
1078
0
    layout_fix_panes(wp->window, wp);
1079
0
  else
1080
0
    layout_fix_panes(wp->window, NULL);
1081
0
}
1082
1083
/* Calculate the new pane size for resized parent. */
1084
static u_int
1085
layout_new_pane_size(struct window *w, u_int previous, struct layout_cell *lc,
1086
    enum layout_type type, u_int size, u_int count_left, u_int size_left)
1087
0
{
1088
0
  u_int new_size, min, max, available;
1089
1090
  /* If this is the last cell, it can take all of the remaining size. */
1091
0
  if (count_left == 1)
1092
0
    return (size_left);
1093
1094
  /* How much is available in this parent? */
1095
0
  available = layout_resize_check(w, lc, type);
1096
1097
  /*
1098
   * Work out the minimum size of this cell and the new size
1099
   * proportionate to the previous size.
1100
   */
1101
0
  min = (PANE_MINIMUM + 1) * (count_left - 1);
1102
0
  if (type == LAYOUT_LEFTRIGHT) {
1103
0
    if (lc->g.sx - available > min)
1104
0
      min = lc->g.sx - available;
1105
0
    new_size = (lc->g.sx * size) / previous;
1106
0
  } else {
1107
0
    if (lc->g.sy - available > min)
1108
0
      min = lc->g.sy - available;
1109
0
    new_size = (lc->g.sy * size) / previous;
1110
0
  }
1111
1112
  /* Check against the maximum and minimum size. */
1113
0
  max = size_left - min;
1114
0
  if (new_size > max)
1115
0
    new_size = max;
1116
0
  if (new_size < PANE_MINIMUM)
1117
0
    new_size = PANE_MINIMUM;
1118
0
  return (new_size);
1119
0
}
1120
1121
/* Check if the cell and all its children can be resized to a specific size. */
1122
static int
1123
layout_set_size_check(struct window *w, struct layout_cell *lc,
1124
    enum layout_type type, int size)
1125
0
{
1126
0
  struct layout_cell  *lcchild;
1127
0
  u_int      new_size, available, previous, count, idx;
1128
1129
  /* Cells with no children must just be bigger than minimum. */
1130
0
  if (lc->type == LAYOUT_WINDOWPANE)
1131
0
    return (size >= PANE_MINIMUM);
1132
0
  available = size;
1133
1134
  /* Count number of children. */
1135
0
  count = 0;
1136
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry)
1137
0
    count++;
1138
1139
  /* Check new size will work for each child. */
1140
0
  if (lc->type == type) {
1141
0
    if (available < (count * 2) - 1)
1142
0
      return (0);
1143
1144
0
    if (type == LAYOUT_LEFTRIGHT)
1145
0
      previous = lc->g.sx;
1146
0
    else
1147
0
      previous = lc->g.sy;
1148
1149
0
    idx = 0;
1150
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1151
0
      new_size = layout_new_pane_size(w, previous, lcchild,
1152
0
          type, size, count - idx, available);
1153
0
      if (idx == count - 1) {
1154
0
        if (new_size > available)
1155
0
          return (0);
1156
0
        available -= new_size;
1157
0
      } else {
1158
0
        if (new_size + 1 > available)
1159
0
          return (0);
1160
0
        available -= new_size + 1;
1161
0
      }
1162
0
      if (!layout_set_size_check(w, lcchild, type, new_size))
1163
0
        return (0);
1164
0
      idx++;
1165
0
    }
1166
0
  } else {
1167
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1168
0
      if (lcchild->type == LAYOUT_WINDOWPANE)
1169
0
        continue;
1170
0
      if (!layout_set_size_check(w, lcchild, type, size))
1171
0
        return (0);
1172
0
    }
1173
0
  }
1174
1175
0
  return (1);
1176
0
}
1177
1178
/* Resize all child cells to fit within the current cell. */
1179
static void
1180
layout_resize_child_cells(struct window *w, struct layout_cell *lc)
1181
0
{
1182
0
  struct layout_cell  *lcchild;
1183
0
  u_int      prev, available, count, idx;
1184
1185
0
  if (lc->type == LAYOUT_WINDOWPANE)
1186
0
    return;
1187
1188
  /* What is the current size used? */
1189
0
  count = 0;
1190
0
  prev = 0;
1191
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1192
0
    if (!layout_cell_is_tiled(lcchild) &&
1193
0
        !layout_cell_has_tiled_child(lcchild))
1194
0
      continue;
1195
0
    count++;
1196
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1197
0
      prev += lcchild->g.sx;
1198
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1199
0
      prev += lcchild->g.sy;
1200
0
  }
1201
0
  prev += (count - 1);
1202
1203
  /* And how much is available? */
1204
0
  available = 0;
1205
0
  if (lc->type == LAYOUT_LEFTRIGHT)
1206
0
    available = lc->g.sx;
1207
0
  else if (lc->type == LAYOUT_TOPBOTTOM)
1208
0
    available = lc->g.sy;
1209
1210
  /* Resize children into the new size. */
1211
0
  idx = 0;
1212
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1213
0
    if (!layout_cell_is_tiled(lcchild) &&
1214
0
        !layout_cell_has_tiled_child(lcchild))
1215
0
      continue;
1216
0
    if (lc->type == LAYOUT_TOPBOTTOM) {
1217
0
      lcchild->g.sx = lc->g.sx;
1218
0
      lcchild->g.xoff = lc->g.xoff;
1219
0
    } else {
1220
0
      lcchild->g.sx = layout_new_pane_size(w, prev, lcchild,
1221
0
          lc->type, lc->g.sx, count - idx, available);
1222
0
      available -= (lcchild->g.sx + 1);
1223
0
    }
1224
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1225
0
      lcchild->g.sy = lc->g.sy;
1226
0
      lcchild->g.yoff = lc->g.yoff;
1227
0
    } else {
1228
0
      lcchild->g.sy = layout_new_pane_size(w, prev, lcchild,
1229
0
          lc->type, lc->g.sy, count - idx, available);
1230
0
      available -= (lcchild->g.sy + 1);
1231
0
    }
1232
0
    layout_resize_child_cells(w, lcchild);
1233
0
    idx++;
1234
0
  }
1235
0
}
1236
1237
/*
1238
 * Replaces the provided layout cell with a new node of the specified type and
1239
 * inserts the cell into it. Used when creating new cells requires a different
1240
 * layout type, or when the root layout is a window pane.
1241
 */
1242
struct layout_cell *
1243
layout_replace_with_node(struct window *w, struct layout_cell *lc,
1244
    enum layout_type type)
1245
0
{
1246
0
  struct layout_cell  *lcparent;
1247
1248
0
  lcparent = layout_create_cell(lc->parent);
1249
0
  layout_make_node(lcparent, type);
1250
0
  layout_set_size(lcparent, lc->g.sx, lc->g.sy, lc->g.xoff, lc->g.yoff);
1251
0
  if (lc->parent == NULL)
1252
0
    w->layout_root = lcparent;
1253
0
  else
1254
0
    TAILQ_REPLACE(&lc->parent->cells, lc, lcparent, entry);
1255
1256
  /* Insert the old cell. */
1257
0
  lc->parent = lcparent;
1258
0
  TAILQ_INSERT_HEAD(&lcparent->cells, lc, entry);
1259
1260
0
  return (lcparent);
1261
0
}
1262
1263
/* Checks if there is enough space for two new panes. */
1264
int
1265
layout_split_check_space(struct window_pane *wp, struct layout_cell *lc,
1266
   enum layout_type type)
1267
0
{
1268
0
  struct layout_cell  *root = wp->window->layout_root;
1269
0
  struct style    *sb_style = &wp->scrollbar_style;
1270
0
  u_int      minimum, sx = lc->g.sx, sy = lc->g.sy;
1271
0
  int      status;
1272
1273
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1274
0
    fatalx("floating cells cannot be split");
1275
1276
0
  status = window_get_pane_status(wp->window);
1277
1278
0
  switch (type) {
1279
0
  case LAYOUT_LEFTRIGHT:
1280
0
    if (wp->window->sb == PANE_SCROLLBARS_ALWAYS) {
1281
0
      minimum = PANE_MINIMUM * 2 + sb_style->width +
1282
0
          sb_style->pad;
1283
0
    } else
1284
0
      minimum = PANE_MINIMUM * 2 + 1;
1285
0
    if (sx < minimum)
1286
0
      return (0);
1287
0
    break;
1288
0
  case LAYOUT_TOPBOTTOM:
1289
0
    if (layout_add_horizontal_border(root, lc, status))
1290
0
      minimum = PANE_MINIMUM * 2 + 2;
1291
0
    else
1292
0
      minimum = PANE_MINIMUM * 2 + 1;
1293
0
    if (sy < minimum)
1294
0
      return (0);
1295
0
    break;
1296
0
  default:
1297
0
    fatalx("bad layout type");
1298
0
  }
1299
1300
0
  return (1);
1301
0
}
1302
1303
/* Calculates the new cell sizes when splitting a pane. */
1304
void
1305
layout_split_sizes(struct layout_cell *lc, int size, int before,
1306
    enum layout_type type, u_int *size1, u_int *size2, u_int *saved_size)
1307
0
{
1308
0
  u_int s1, s2, ss;
1309
0
  u_int sx = lc->g.sx, sy = lc->g.sy;
1310
1311
0
  if (type == LAYOUT_LEFTRIGHT)
1312
0
    ss = sx;
1313
0
  else
1314
0
    ss = sy;
1315
0
  if (size < 0)
1316
0
    s2 = ((ss + 1) / 2) - 1;
1317
0
  else if (before)
1318
0
    s2 = ss - size - 1;
1319
0
  else
1320
0
    s2 = size;
1321
0
  if (s2 < PANE_MINIMUM)
1322
0
    s2 = PANE_MINIMUM;
1323
0
  else if (s2 > ss - 2)
1324
0
    s2 = ss - 2;
1325
0
  s1 = ss - 1 - s2;
1326
1327
0
  *size1 = s1;
1328
0
  *size2 = s2;
1329
0
  *saved_size = ss;
1330
0
}
1331
1332
/*
1333
 * Split a pane into two. size is a hint, or -1 for default half/half
1334
 * split. This must be followed by layout_assign_pane before much else happens!
1335
 */
1336
struct layout_cell *
1337
layout_split_pane(struct window_pane *wp, enum layout_type type, int size,
1338
    int flags)
1339
0
{
1340
0
  struct layout_cell  *lc, *lcparent, *lcnew, *lc1, *lc2;
1341
0
  u_int      sx, sy, xoff, yoff, size1, size2;
1342
0
  u_int      new_size, saved_size, resize_first = 0;
1343
0
  int      full_size = (flags & SPAWN_FULLSIZE);
1344
0
  int      before = (flags & SPAWN_BEFORE);
1345
1346
  /*
1347
   * If full_size is specified, add a new cell at the top of the window
1348
   * layout. Otherwise, split the cell for the current pane.
1349
   */
1350
0
  if (full_size)
1351
0
    lc = wp->window->layout_root;
1352
0
  else
1353
0
    lc = wp->layout_cell;
1354
1355
  /* Copy the old cell size. */
1356
0
  sx = lc->g.sx;
1357
0
  sy = lc->g.sy;
1358
0
  xoff = lc->g.xoff;
1359
0
  yoff = lc->g.yoff;
1360
1361
  /* Check there is enough space for the two new panes. */
1362
0
  if (!layout_split_check_space(wp, lc, type))
1363
0
    return (NULL);
1364
1365
  /*
1366
   * Calculate new cell sizes. size is the target size or -1 for middle
1367
   * split, size1 is the size of the top/left and size2 the bottom/right.
1368
   */
1369
0
  layout_split_sizes(lc, size, before, type, &size1, &size2, &saved_size);
1370
1371
  /* Which size are we using? */
1372
0
  if (flags & SPAWN_BEFORE)
1373
0
    new_size = size2;
1374
0
  else
1375
0
    new_size = size1;
1376
1377
  /* Confirm there is enough space for full size pane. */
1378
0
  if (full_size && !layout_set_size_check(wp->window, lc, type, new_size))
1379
0
    return (NULL);
1380
1381
0
  if (lc->parent != NULL && lc->parent->type == type) {
1382
    /*
1383
     * If the parent exists and is of the same type as the split,
1384
     * create a new cell and insert it after this one.
1385
     */
1386
0
    lcparent = lc->parent;
1387
0
    lcnew = layout_create_cell(lcparent);
1388
0
    if (flags & SPAWN_BEFORE)
1389
0
      TAILQ_INSERT_BEFORE(lc, lcnew, entry);
1390
0
    else
1391
0
      TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1392
0
  } else if (full_size && lc->parent == NULL && lc->type == type) {
1393
    /*
1394
     * If the new full size pane is the same type as the root
1395
     * split, insert the new pane under the existing root cell
1396
     * instead of creating a new root cell. The existing layout
1397
     * must be resized before inserting the new cell.
1398
     */
1399
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1400
0
      lc->g.sx = new_size;
1401
0
      layout_resize_child_cells(wp->window, lc);
1402
0
      lc->g.sx = saved_size;
1403
0
    } else if (lc->type == LAYOUT_TOPBOTTOM) {
1404
0
      lc->g.sy = new_size;
1405
0
      layout_resize_child_cells(wp->window, lc);
1406
0
      lc->g.sy = saved_size;
1407
0
    }
1408
0
    resize_first = 1;
1409
1410
    /* Create the new cell. */
1411
0
    lcnew = layout_create_cell(lc);
1412
0
    size = saved_size - 1 - new_size;
1413
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1414
0
      layout_set_size(lcnew, size, sy, 0, 0);
1415
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1416
0
      layout_set_size(lcnew, sx, size, 0, 0);
1417
0
    if (flags & SPAWN_BEFORE)
1418
0
      TAILQ_INSERT_HEAD(&lc->cells, lcnew, entry);
1419
0
    else
1420
0
      TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
1421
0
  } else {
1422
    /*
1423
     * Otherwise create a new parent and insert it.
1424
     */
1425
1426
    /* Create and insert the replacement parent. */
1427
0
    lcparent = layout_replace_with_node(wp->window, lc, type);
1428
1429
    /* Create the new child cell. */
1430
0
    lcnew = layout_create_cell(lcparent);
1431
0
    if (flags & SPAWN_BEFORE)
1432
0
      TAILQ_INSERT_HEAD(&lcparent->cells, lcnew, entry);
1433
0
    else
1434
0
      TAILQ_INSERT_TAIL(&lcparent->cells, lcnew, entry);
1435
0
  }
1436
0
  if (flags & SPAWN_BEFORE) {
1437
0
    lc1 = lcnew;
1438
0
    lc2 = lc;
1439
0
  } else {
1440
0
    lc1 = lc;
1441
0
    lc2 = lcnew;
1442
0
  }
1443
1444
  /*
1445
   * Set new cell sizes. size1 is the size of the top/left and size2 the
1446
   * bottom/right.
1447
   */
1448
0
  if (!resize_first && type == LAYOUT_LEFTRIGHT) {
1449
0
    layout_set_size(lc1, size1, sy, xoff, yoff);
1450
0
    layout_set_size(lc2, size2, sy, xoff + lc1->g.sx + 1, yoff);
1451
0
  } else if (!resize_first && type == LAYOUT_TOPBOTTOM) {
1452
0
    layout_set_size(lc1, sx, size1, xoff, yoff);
1453
0
    layout_set_size(lc2, sx, size2, xoff, yoff + lc1->g.sy + 1);
1454
0
  }
1455
0
  if (full_size) {
1456
0
    if (!resize_first)
1457
0
      layout_resize_child_cells(wp->window, lc);
1458
0
    layout_fix_offsets(wp->window);
1459
0
  } else
1460
0
    layout_make_leaf(lc, wp);
1461
1462
0
  return (lcnew);
1463
0
}
1464
1465
/*
1466
 * Creates a cell for a new floating pane. This must be followed by
1467
 * layout_assign_pane before much else happens!
1468
 */
1469
struct layout_cell *
1470
layout_floating_pane(struct window *w, struct window_pane *wp,
1471
    struct layout_geometry *lg)
1472
0
{
1473
0
  struct layout_cell  *lc, *lcnew, *lcparent;
1474
1475
0
  if (wp == NULL)
1476
0
    lc = w->layout_root;
1477
0
  else
1478
0
    lc = wp->layout_cell;
1479
0
  lcparent = lc->parent;
1480
1481
0
  if (lcparent == NULL) {
1482
    /*
1483
     * Adding a pane to a root that isn't a node. Must create and
1484
     * insert a new root.
1485
     */
1486
0
    lcparent = layout_replace_with_node(w, lc, LAYOUT_TOPBOTTOM);
1487
0
  }
1488
1489
0
  lcnew = layout_create_cell(lcparent);
1490
0
  TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1491
0
  lcnew->flags |= LAYOUT_CELL_FLOATING;
1492
0
  layout_set_size(lcnew, lg->sx, lg->sy, lg->xoff, lg->yoff);
1493
1494
0
  return (lcnew);
1495
0
}
1496
1497
/* Destroy the cell associated with a pane. */
1498
void
1499
layout_close_pane(struct window_pane *wp)
1500
0
{
1501
0
  struct window *w = wp->window;
1502
1503
0
  if (wp->layout_cell == NULL)
1504
0
    return;
1505
1506
  /* Remove the cell. */
1507
0
  layout_destroy_cell(w, wp->layout_cell, &w->layout_root);
1508
0
  wp->layout_cell = NULL;
1509
1510
  /* Fix pane offsets and sizes. */
1511
0
  if (w->layout_root != NULL) {
1512
0
    layout_fix_offsets(w);
1513
0
    layout_fix_panes(w, NULL);
1514
0
  }
1515
0
  events_fire_window("window-layout-changed", w);
1516
0
}
1517
1518
/* Spread out cells inside a parent cell. */
1519
int
1520
layout_spread_cell(struct window *w, struct layout_cell *parent)
1521
0
{
1522
0
  struct layout_cell  *lc, *root = w->layout_root;
1523
0
  u_int      number, each, size, this, remainder;
1524
0
  int      change, changed, status;
1525
1526
0
  number = 0;
1527
0
  TAILQ_FOREACH (lc, &parent->cells, entry)
1528
0
    if (layout_cell_is_tiled(lc))
1529
0
      number++;
1530
0
  if (number <= 1)
1531
0
    return (0);
1532
0
  status = window_get_pane_status(w);
1533
1534
0
  if (parent->type == LAYOUT_LEFTRIGHT)
1535
0
    size = parent->g.sx;
1536
0
  else if (parent->type == LAYOUT_TOPBOTTOM) {
1537
0
    if (layout_add_horizontal_border(root, parent, status))
1538
0
      size = parent->g.sy - 1;
1539
0
    else
1540
0
      size = parent->g.sy;
1541
0
  } else
1542
0
    return (0);
1543
0
  if (size < number - 1)
1544
0
    return (0);
1545
0
  each = (size - (number - 1)) / number;
1546
0
  if (each == 0)
1547
0
    return (0);
1548
1549
  /*
1550
   * Remaining space after assigning that which can be evenly
1551
   * distributed.
1552
   */
1553
0
  remainder = size - (number * (each + 1)) + 1;
1554
1555
0
  changed = 0;
1556
0
  TAILQ_FOREACH (lc, &parent->cells, entry) {
1557
0
    if (!layout_cell_is_tiled(lc))
1558
0
      continue;
1559
0
    change = 0;
1560
0
    if (parent->type == LAYOUT_LEFTRIGHT) {
1561
0
      change = each - (int)lc->g.sx;
1562
0
      if (remainder > 0) {
1563
0
        change++;
1564
0
        remainder--;
1565
0
      }
1566
0
      layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, change);
1567
0
    } else if (parent->type == LAYOUT_TOPBOTTOM) {
1568
0
      if (layout_add_horizontal_border(root, lc, status))
1569
0
        this = each + 1;
1570
0
      else
1571
0
        this = each;
1572
0
      if (remainder > 0) {
1573
0
        this++;
1574
0
        remainder--;
1575
0
      }
1576
0
      change = this - (int)lc->g.sy;
1577
0
      layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, change);
1578
0
    }
1579
0
    if (change != 0)
1580
0
      changed = 1;
1581
0
  }
1582
0
  return (changed);
1583
0
}
1584
1585
/* Spread out cells evenly. */
1586
void
1587
layout_spread_out(struct window_pane *wp)
1588
0
{
1589
0
  struct layout_cell  *parent;
1590
0
  struct window   *w = wp->window;
1591
1592
0
  parent = wp->layout_cell->parent;
1593
0
  if (parent == NULL)
1594
0
    return;
1595
1596
0
  do {
1597
0
    if (layout_spread_cell(w, parent)) {
1598
0
      layout_fix_offsets(w);
1599
0
      layout_fix_panes(w, NULL);
1600
0
      break;
1601
0
    }
1602
0
  } while ((parent = parent->parent) != NULL);
1603
0
}
1604
1605
/* Get a new tiled cell. */
1606
struct layout_cell *
1607
layout_get_tiled_cell(struct cmdq_item *item, struct args *args,
1608
    struct window *w, struct window_pane *wp, int flags, char **cause)
1609
0
{
1610
0
  struct layout_cell  *lc;
1611
0
  enum layout_type   type = LAYOUT_TOPBOTTOM;
1612
0
  u_int      curval;
1613
0
  int      size = -1;
1614
0
  char      *error = NULL;
1615
1616
0
  if (window_pane_is_floating(wp)) {
1617
0
    *cause = xstrdup("can't split a floating pane");
1618
0
    return (NULL);
1619
0
  }
1620
1621
0
  if (flags & SPAWN_HORIZONTAL)
1622
0
    type = LAYOUT_LEFTRIGHT;
1623
1624
0
  if (args_has(args, 'l') || args_has(args, 'p')) {
1625
0
    if (flags & SPAWN_FULLSIZE) {
1626
0
      if (type == LAYOUT_TOPBOTTOM)
1627
0
        curval = w->sy;
1628
0
      else
1629
0
        curval = w->sx;
1630
0
    } else {
1631
0
      if (type == LAYOUT_TOPBOTTOM)
1632
0
        curval = wp->sy;
1633
0
      else
1634
0
        curval = wp->sx;
1635
0
    }
1636
0
  }
1637
1638
0
  if (args_has(args, 'l')) {
1639
0
    size = args_percentage_and_expand(args, 'l', 0, INT_MAX, curval,
1640
0
        item, &error);
1641
0
  } else if (args_has(args, 'p')) {
1642
0
    size = args_strtonum_and_expand(args, 'p', 0, 100, item,
1643
0
        &error);
1644
0
    if (error == NULL)
1645
0
      size = curval * size / 100;
1646
0
  }
1647
0
  if (error != NULL) {
1648
0
    xasprintf(cause, "invalid tiled geometry %s", error);
1649
0
    free(error);
1650
0
    return (NULL);
1651
0
  }
1652
1653
0
  window_push_zoom(wp->window, 1, (flags & SPAWN_ZOOM));
1654
0
  lc = layout_split_pane(wp, type, size, flags);
1655
0
  if (lc == NULL)
1656
0
    *cause = xstrdup("no space for a new pane");
1657
1658
0
  return (lc);
1659
0
}
1660
1661
struct layout_cell *
1662
layout_get_floating_cell(struct cmdq_item *item, struct args *args,
1663
    enum pane_lines lines, struct window *w, struct window_pane *wp, int flags,
1664
    char **cause)
1665
0
{
1666
0
  struct layout_cell  *lcnew, *lc = wp->layout_cell;
1667
0
  struct layout_geometry   fg;
1668
1669
0
  layout_geometry_init(&fg);
1670
0
  if (flags & SPAWN_SPLIT) {
1671
0
    if (layout_split_floating_cell(lc, w, &fg, lines, flags, cause)
1672
0
        != 0)
1673
0
      return (NULL);
1674
0
  } else {
1675
0
    if (layout_floating_args_parse(item, args, lines, w, &fg, cause)
1676
0
        != 0)
1677
0
      return (NULL);
1678
0
  }
1679
1680
0
  if (flags & SPAWN_MODAL)
1681
0
    window_push_modal_zoom(w);
1682
0
  else
1683
0
    window_push_zoom(wp->window, 1, (flags & SPAWN_ZOOM));
1684
0
  lcnew = layout_floating_pane(w, wp, &fg);
1685
0
  return (lcnew);
1686
0
}
1687
1688
int
1689
layout_floating_args_parse(struct cmdq_item *item, struct args *args,
1690
    enum pane_lines lines, struct window *w, struct layout_geometry *lg,
1691
    char **cause)
1692
0
{
1693
0
  int  sx, sy, ox, oy;
1694
0
  char  *error = NULL;
1695
1696
0
  sx = lg->sx == UINT_MAX ? w->sx / 2 : lg->sx;
1697
0
  sy = lg->sy == UINT_MAX ? w->sy / 4 : lg->sy;
1698
0
  ox = lg->xoff;
1699
0
  oy = lg->yoff;
1700
1701
0
  if (args_has(args, 'x')) {
1702
0
    sx = args_percentage_and_expand(args, 'x', 0, PANE_MAXIMUM,
1703
0
        w->sx, item, &error);
1704
0
    if (error != NULL) {
1705
0
      xasprintf(cause, "position %s", error);
1706
0
      free(error);
1707
0
      return (-1);
1708
0
    }
1709
0
    if (lines != PANE_LINES_NONE)
1710
0
      sx -= 2;
1711
0
  }
1712
0
  if (args_has(args, 'y')) {
1713
0
    sy = args_percentage_and_expand(args, 'y', 0, PANE_MAXIMUM,
1714
0
        w->sy, item, &error);
1715
0
    if (error != NULL) {
1716
0
      xasprintf(cause, "position %s", error);
1717
0
      free(error);
1718
0
      return (-1);
1719
0
    }
1720
0
    if (lines != PANE_LINES_NONE)
1721
0
      sy -= 2;
1722
0
  }
1723
0
  if (args_has(args, 'X')) {
1724
0
    ox = args_percentage_and_expand(args, 'X', -sx, w->sx,
1725
0
        w->sx, item, &error);
1726
0
    if (error != NULL) {
1727
0
      xasprintf(cause, "position %s", error);
1728
0
      free(error);
1729
0
      return (-1);
1730
0
    }
1731
0
  }
1732
0
  if (args_has(args, 'Y')) {
1733
0
    oy = args_percentage_and_expand(args, 'Y', -sy, w->sy,
1734
0
        w->sy, item, &error);
1735
0
    if (error != NULL) {
1736
0
      xasprintf(cause, "position %s", error);
1737
0
      free(error);
1738
0
      return (-1);
1739
0
    }
1740
0
  }
1741
1742
0
  if (ox == INT_MAX) {
1743
0
    if (w->last_new_pane_x == 0)
1744
0
      ox = 4;
1745
0
    else {
1746
0
      ox = w->last_new_pane_x + 4;
1747
0
      if (w->last_new_pane_x > w->sx)
1748
0
        ox = 4;
1749
0
    }
1750
0
    w->last_new_pane_x = ox;
1751
0
  } else if (args_has(args, 'X'))
1752
0
    if (lines != PANE_LINES_NONE)
1753
0
      ox += 1;
1754
0
  if (oy == INT_MAX) {
1755
0
    if (w->last_new_pane_y == 0)
1756
0
      oy = 2;
1757
0
    else {
1758
0
      oy = w->last_new_pane_y + 2;
1759
0
      if (w->last_new_pane_y > w->sy)
1760
0
        oy = 2;
1761
0
    }
1762
0
    w->last_new_pane_y = oy;
1763
0
  } else if (args_has(args, 'Y'))
1764
0
    if (lines != PANE_LINES_NONE)
1765
0
      oy += 1;
1766
1767
0
  if (sx < PANE_MINIMUM || sx > PANE_MAXIMUM) {
1768
0
    *cause = xstrdup("invalid width");
1769
0
    return (-1);
1770
0
  }
1771
0
  if (sy < PANE_MINIMUM || sy > PANE_MAXIMUM) {
1772
0
    *cause = xstrdup("invalid height");
1773
0
    return (-1);
1774
0
  }
1775
1776
0
  lg->sx = sx;
1777
0
  lg->sy = sy;
1778
0
  lg->xoff = ox;
1779
0
  lg->yoff = oy;
1780
0
  return (0);
1781
0
}
1782
1783
int
1784
layout_split_floating_cell(struct layout_cell *lc, struct window *w,
1785
    struct layout_geometry *out, enum pane_lines lines, int flags,
1786
    char **cause)
1787
0
{
1788
0
  struct layout_geometry   old, new;
1789
0
  int      tborder = 1, bborder = w->sy - 1;
1790
0
  int      lborder = 3, rborder = w->sx - 3;
1791
0
  int      border = lines != PANE_LINES_NONE ? 1 : 0;
1792
0
  int      size, space;
1793
1794
  /* First, move the target cell in-bounds. */
1795
0
  memcpy(&old, &lc->g, sizeof old);
1796
0
  if (lborder > old.xoff - border)
1797
0
    old.xoff = lborder + border;
1798
0
  if (rborder < old.xoff + (int)old.sx + border)
1799
0
    old.xoff = rborder - (int)old.sx - border;
1800
0
  if (tborder > old.yoff - border)
1801
0
    old.yoff = tborder + border;
1802
0
  if (bborder < old.yoff + (int)old.sy + border)
1803
0
    old.yoff = bborder - (int)old.sy - border;
1804
1805
  /* Move the new cell to its ideal position. */
1806
0
  memcpy(&new, &old, sizeof new);
1807
0
  if (flags & SPAWN_HORIZONTAL) {
1808
0
    if (flags & SPAWN_BEFORE)
1809
0
      new.xoff -= old.sx + 2 * border;
1810
0
    else
1811
0
      new.xoff += old.sx + 2 * border;
1812
0
  } else {
1813
0
    if (flags & SPAWN_BEFORE)
1814
0
      new.yoff -= old.sy + 2 * border;
1815
0
    else
1816
0
      new.yoff += old.sy + 2 * border;
1817
0
  }
1818
1819
  /*
1820
   * The position of the new cell is checked to see if it is in bounds.
1821
   * If it isn't, the availible space is split and equally given to both
1822
   * cells. Only one border is check because the target cell is in bounds
1823
   * already.
1824
   */
1825
0
  if (lborder > new.xoff - border) {
1826
    /*
1827
     * The space for both panes is calculated. Since the offsets are
1828
     * associated to where pane contents start, we remove pane
1829
     * borders from the space. '1' is added in case the space is
1830
     * odd.
1831
     */
1832
0
    space = old.xoff + old.sx - lborder - 3 * border + 1;
1833
0
    size = space / 2;
1834
0
    new.sx = size;
1835
0
    old.sx = size;
1836
0
    new.xoff = lborder + border;
1837
0
    old.xoff = new.xoff + new.sx + 2 * border;
1838
    /*
1839
     * If the original space was to be odd (now even), subtract 1
1840
     * from the rightmost cell
1841
     */
1842
0
    if (space % 2 == 0)
1843
0
      old.sx -= 1;
1844
0
  } else if (rborder < new.xoff + (int)new.sx + border) {
1845
0
    space = rborder - old.xoff - 3 * border + 1;
1846
0
    size = space / 2;
1847
0
    new.sx = size;
1848
0
    old.sx = size;
1849
0
    new.xoff = old.xoff + old.sx + 2 * border;
1850
0
    if (space % 2 == 0)
1851
0
      new.sx -= 1;
1852
0
  } else if (tborder > new.yoff - border) {
1853
0
    space = old.sy + old.yoff - tborder - 3 * border + 1;
1854
0
    size = space / 2;
1855
0
    new.sy = size;
1856
0
    old.sy = size;
1857
0
    new.yoff = tborder + border;
1858
0
    old.yoff = new.yoff + new.sy + 2 * border;
1859
0
    if (space % 2 == 0)
1860
0
      old.sy -= 1;
1861
0
  } else if (bborder < new.yoff + (int)new.sy + border) {
1862
0
    space = bborder - old.yoff - 3 * border + 1;
1863
0
    size = space / 2;
1864
0
    new.sy = size;
1865
0
    old.sy = size;
1866
0
    new.yoff = old.yoff + old.sy + 2 * border;
1867
0
    if (space % 2 == 0)
1868
0
      new.sy -= 1;
1869
0
  }
1870
1871
  /*
1872
   * Expand the cell to occupy the whole availible space where it was
1873
   * spawned.
1874
   */
1875
0
  if (flags & SPAWN_FULLSIZE) {
1876
0
    if (flags & SPAWN_HORIZONTAL) {
1877
0
      new.yoff = tborder + border;
1878
0
      new.sy = bborder - tborder - 2 * border;
1879
0
      if (flags & SPAWN_BEFORE) {
1880
0
        new.xoff = lborder + border;
1881
0
        new.sx = old.xoff - new.xoff - 2 * border;
1882
0
      } else {
1883
0
        new.sx = rborder - new.xoff - border;
1884
0
      }
1885
0
    } else {
1886
0
      new.xoff = lborder + border;
1887
0
      new.sx = rborder - lborder - 2 * border;
1888
0
      if (flags & SPAWN_BEFORE) {
1889
0
        new.yoff = tborder + border;
1890
0
        new.sy = old.yoff - new.yoff - 2 * border;
1891
0
      } else {
1892
0
        new.sy = bborder - new.yoff - border;
1893
0
      }
1894
0
    }
1895
0
  }
1896
1897
0
  if (new.sx < PANE_MINIMUM || new.sy < PANE_MINIMUM ||
1898
0
      old.sx < PANE_MINIMUM || old.sy < PANE_MINIMUM) {
1899
0
    *cause = xstrdup("no space for a new pane");
1900
0
    return (-1);
1901
0
  }
1902
1903
0
  layout_set_size(lc, old.sx, old.sy, old.xoff, old.yoff);
1904
0
  memcpy(out, &new, sizeof *out);
1905
0
  return (0);
1906
0
}
1907
1908
/*
1909
 * Removes a cell from the tiled layout by giving the cell's space to the
1910
 * nearest neighbour.
1911
 */
1912
int
1913
layout_remove_tile(struct window *w, struct layout_cell *lc)
1914
0
{
1915
0
  struct layout_cell  *lcneighbour, *lcparent;
1916
0
  enum layout_type   type;
1917
0
  int      change;
1918
1919
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1920
0
    return (-1);
1921
1922
0
  lcneighbour = layout_cell_get_neighbour(lc);
1923
0
  if (lcneighbour == NULL) {
1924
0
    if (lc->parent != NULL)
1925
0
      layout_remove_tile(w, lc->parent);
1926
0
  } else if ((lcparent = lcneighbour->parent) != NULL) {
1927
0
    type = lcparent->type;
1928
    /*
1929
     * Adding the size of the layout cell plus its border to the
1930
     * neighbour.
1931
     */
1932
0
    if (type == LAYOUT_TOPBOTTOM)
1933
0
      change = lc->g.sy + 1;
1934
0
    else
1935
0
      change = lc->g.sx + 1;
1936
0
    layout_resize_adjust(w, lcneighbour, type, change);
1937
0
  }
1938
1939
  /*
1940
   * Zeroing out the cell geometry until the cell is retiled unless this
1941
   * is the top level node.
1942
   */
1943
0
  if (lc->parent != NULL)
1944
0
    layout_set_size(lc, 0, 0, 0, 0);
1945
0
  return (0);
1946
0
}
1947
1948
/*
1949
 * Inserts a cell back into the tiled layout by taking half the space from its
1950
 * nearest neighbour.
1951
 */
1952
int
1953
layout_insert_tile(struct window *w, struct layout_cell *lc)
1954
0
{
1955
0
  struct layout_cell  *lcneighbour, *lctiled, *lcparent;
1956
0
  enum layout_type   type;
1957
0
  u_int      size1, size2, saved_size;
1958
1959
0
  if (lc == NULL)
1960
0
    fatalx("layout cell cannot be null when tiling");
1961
1962
0
  if (layout_cell_is_tiled(lc))
1963
0
    return (-1);
1964
1965
0
  lcparent = lc->parent;
1966
0
  if (lcparent == NULL) {
1967
    /* Only pane in the layout. */
1968
0
    layout_set_size(lc, w->sx, w->sy, 0, 0);
1969
0
    return (0);
1970
0
  }
1971
1972
0
  type = lcparent->type;
1973
0
  lcneighbour = layout_cell_get_neighbour(lc);
1974
0
  if (lcneighbour == NULL) {
1975
    /*
1976
     * This will become the only visible cell in the parent.
1977
     * Tile the parent, then set the child's 'split' size.
1978
     */
1979
0
    layout_insert_tile(w, lcparent);
1980
0
    if (type == LAYOUT_LEFTRIGHT)
1981
0
      size1 = lcparent->g.sx;
1982
0
    else
1983
0
      size1 = lcparent->g.sy;
1984
0
    layout_resize_set_size(w, lc, type, size1);
1985
0
  } else {
1986
    /*
1987
     * If the neighbour is a node, a tiled child in the subtree of
1988
     * the neighbour is needed to check for space.
1989
     */
1990
0
    lctiled = layout_cell_get_first_tiled(lcneighbour);
1991
0
    if (!layout_split_check_space(lctiled->wp, lcneighbour, type))
1992
0
      return (-1);
1993
0
    layout_split_sizes(lcneighbour, -1, 0, type, &size1, &size2,
1994
0
        &saved_size);
1995
0
    layout_resize_set_size(w, lc, type, size1);
1996
0
    layout_resize_set_size(w, lcneighbour, type, size2);
1997
0
  }
1998
1999
  /* Setting opposite of the 'split' size to that of the parent. */
2000
0
  if (lcparent->type == LAYOUT_LEFTRIGHT) {
2001
0
    size1 = lcparent->g.sy;
2002
0
    type = LAYOUT_TOPBOTTOM;
2003
0
  } else {
2004
0
    size1 = lcparent->g.sx;
2005
0
    type = LAYOUT_LEFTRIGHT;
2006
0
  }
2007
0
  layout_resize_set_size(w, lc, type, size1);
2008
2009
0
  return (0);
2010
0
}