Coverage Report

Created: 2026-08-31 06:23

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.98 2026/08/25 18:38:05 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
0
{
90
0
  struct layout_cell  *lcchild, *lcnext;
91
92
0
  if (lc == NULL || (only_nodes && lc->type == LAYOUT_WINDOWPANE))
93
0
    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
0
{
445
0
  struct window_pane  *wp;
446
0
  struct layout_cell  *lc, *root = w->layout_root;
447
0
  int      status, sb_w, sb_pad;
448
0
  int      old_xoff, old_yoff, changed = 0;
449
0
  u_int      sx, sy, old_sx, old_sy;
450
451
0
  TAILQ_FOREACH(wp, &w->panes, entry) {
452
0
    if ((lc = wp->layout_cell) == NULL || wp == skip)
453
0
      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
0
  if (changed)
507
0
    redraw_invalidate_scene(w);
508
0
}
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
  /* Floating cells do not take space from the tiled layout. */
543
0
  if (!layout_cell_is_tiled(lc) && !layout_cell_has_tiled_child(lc))
544
0
    return (0);
545
546
0
  if (lc->type == LAYOUT_WINDOWPANE) {
547
    /* Space available in this cell only. */
548
0
    if (type == LAYOUT_LEFTRIGHT) {
549
0
      available = lc->g.sx;
550
0
      if (w->sb == PANE_SCROLLBARS_ALWAYS)
551
0
        minimum = PANE_MINIMUM + sb_style->width +
552
0
            sb_style->pad;
553
0
      else
554
0
        minimum = PANE_MINIMUM;
555
0
    } else {
556
0
      available = lc->g.sy;
557
0
      if (layout_add_horizontal_border(root, lc, status))
558
0
        minimum = PANE_MINIMUM + 1;
559
0
      else
560
0
        minimum = PANE_MINIMUM;
561
0
    }
562
0
    if (available > minimum)
563
0
      available -= minimum;
564
0
    else
565
0
      available = 0;
566
0
  } else if (lc->type == type) {
567
    /* Same type: total of available space in all child cells. */
568
0
    available = 0;
569
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry)
570
0
      available += layout_resize_check(w, lcchild, type);
571
0
  } else {
572
    /* Different type: minimum of available space in child cells. */
573
0
    minimum = UINT_MAX;
574
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
575
0
      if (!layout_cell_is_tiled(lcchild) &&
576
0
          !layout_cell_has_tiled_child(lcchild))
577
0
        continue;
578
0
      available = layout_resize_check(w, lcchild, type);
579
0
      if (available < minimum)
580
0
        minimum = available;
581
0
    }
582
0
    available = minimum;
583
0
  }
584
585
0
  return (available);
586
0
}
587
588
/*
589
 * Adjust cell size evenly, including altering its children. This function
590
 * expects the change to have already been bounded to the space available.
591
 */
592
void
593
layout_resize_adjust(struct window *w, struct layout_cell *lc,
594
    enum layout_type type, int change)
595
0
{
596
0
  struct layout_cell  *lcchild;
597
0
  int      changed;
598
599
  /* Adjust the cell size. */
600
0
  if (type == LAYOUT_LEFTRIGHT)
601
0
    lc->g.sx += change;
602
0
  else
603
0
    lc->g.sy += change;
604
605
  /* If this is a leaf cell, that is all that is necessary. */
606
0
  if (type == LAYOUT_WINDOWPANE)
607
0
    return;
608
609
  /* Child cell runs in a different direction. */
610
0
  if (lc->type != type) {
611
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
612
0
      if (!layout_cell_is_tiled(lcchild) &&
613
0
          !layout_cell_has_tiled_child(lcchild))
614
0
        continue;
615
0
      layout_resize_adjust(w, lcchild, type, change);
616
0
    }
617
0
    return;
618
0
  }
619
620
  /*
621
   * If a node doesn't contain any tiled cells, there is nothing to do.
622
   */
623
0
  if (!layout_cell_has_tiled_child(lc))
624
0
    return;
625
626
  /*
627
   * Child cell runs in the same direction. Adjust each child equally
628
   * until no further change is possible.
629
   */
630
0
  while (change != 0) {
631
0
    changed = 0;
632
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
633
0
      if (change == 0)
634
0
        break;
635
0
      if (!layout_cell_is_tiled(lcchild) &&
636
0
          !layout_cell_has_tiled_child(lcchild))
637
0
        continue;
638
0
      if (change > 0) {
639
0
        layout_resize_adjust(w, lcchild, type, 1);
640
0
        change--;
641
0
        changed = 1;
642
0
        continue;
643
0
      }
644
0
      if (layout_resize_check(w, lcchild, type) > 0) {
645
0
        layout_resize_adjust(w, lcchild, type, -1);
646
0
        change++;
647
0
        changed = 1;
648
0
      }
649
0
    }
650
0
    if (!changed)
651
0
      break;
652
0
  }
653
0
}
654
655
/* Resizes a cell to a specified size */
656
void
657
layout_resize_set_size(struct window *w, struct layout_cell *lc,
658
    enum layout_type type, u_int size)
659
0
{
660
0
  int change;
661
662
0
  if (type == LAYOUT_LEFTRIGHT)
663
0
    change = size - lc->g.sx;
664
0
  else
665
0
    change = size - lc->g.sy;
666
0
  layout_resize_adjust(w, lc, type, change);
667
0
}
668
669
/* Find and return the nearest neighbour to a cell in a specific direction. */
670
static struct layout_cell *
671
layout_cell_get_neighbour_dir(struct layout_cell *lc, int direction)
672
0
{
673
0
  struct layout_cell  *lcn = lc;
674
675
0
  while (1) {
676
0
    if (direction)
677
0
      lcn = TAILQ_NEXT(lcn, entry);
678
0
    else
679
0
      lcn = TAILQ_PREV(lcn, layout_cells, entry);
680
681
0
    if (lcn == NULL ||
682
0
        layout_cell_is_tiled(lcn) ||
683
0
        layout_cell_has_tiled_child(lcn))
684
0
      return (lcn);
685
0
  }
686
0
}
687
688
/*
689
 * Find and return the nearest neighbour. Prefers cells "after" the specified
690
 * cell. This behavior defines how cell dimensions are redistributed when a cell
691
 * is hidden/shown and floated/tiled.
692
 */
693
struct layout_cell *
694
layout_cell_get_neighbour(struct layout_cell *lc)
695
0
{
696
0
  struct layout_cell  *lcother, *lcparent = lc->parent;
697
0
  int      direction = 1;
698
699
0
  if (lcparent == NULL)
700
0
    return (NULL);
701
702
0
  if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
703
0
    direction = !direction;
704
705
0
  lcother = layout_cell_get_neighbour_dir(lc, direction);
706
0
  if (lcother == NULL)
707
0
    lcother = layout_cell_get_neighbour_dir(lc, !direction);
708
709
0
  return (lcother);
710
0
}
711
712
713
/* Destroy a cell and redistribute the space. */
714
void
715
layout_destroy_cell(struct window *w, struct layout_cell *lc,
716
    struct layout_cell **lcroot)
717
0
{
718
0
  struct layout_cell  *lcother = NULL, *lcparent;
719
0
  int      change;
720
721
  /* If no parent, this is the last pane in a window. */
722
0
  lcparent = lc->parent;
723
0
  if (lcparent == NULL) {
724
0
    if (lc->wp != NULL)
725
0
      *lcroot = NULL;
726
0
    layout_free_cell(lc, 0);
727
0
    return;
728
0
  }
729
730
0
  if (!layout_cell_is_tiled(lc)) {
731
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
732
0
    layout_free_cell(lc, 0);
733
0
    goto out;
734
0
  }
735
736
0
  lcother = layout_cell_get_neighbour(lc);
737
0
  if (lcother != NULL) {
738
0
    if (lcparent->type == LAYOUT_LEFTRIGHT)
739
0
      change = lc->g.sx + 1;
740
0
    else
741
0
      change = lc->g.sy + 1;
742
0
    layout_resize_adjust(w, lcother, lcparent->type, change);
743
0
  } else
744
0
    layout_remove_tile(w, lcparent);
745
746
  /* Remove this from the parent's list. */
747
0
  TAILQ_REMOVE(&lcparent->cells, lc, entry);
748
0
  layout_free_cell(lc, 0);
749
750
0
out:
751
  /*
752
   * If the parent now has one cell, remove the parent from the tree and
753
   * replace it by that cell.
754
   */
755
0
  lc = TAILQ_FIRST(&lcparent->cells);
756
0
  if (lc != NULL && TAILQ_NEXT(lc, entry) == NULL) {
757
0
    TAILQ_REMOVE(&lcparent->cells, lc, entry);
758
759
0
    lc->parent = lcparent->parent;
760
0
    if (lc->parent == NULL) {
761
0
      if (layout_cell_is_tiled(lc)) {
762
0
        lc->g.xoff = 0;
763
0
        lc->g.yoff = 0;
764
0
      }
765
0
      *lcroot = lc;
766
0
    } else
767
0
      TAILQ_REPLACE(&lc->parent->cells, lcparent, lc, entry);
768
769
0
    layout_free_cell(lcparent, 0);
770
0
  }
771
0
}
772
773
/* Initialize layout for pane. */
774
void
775
layout_init(struct window *w, struct window_pane *wp)
776
0
{
777
0
  struct layout_cell  *lc;
778
779
0
  lc = w->layout_root = layout_create_cell(NULL);
780
0
  layout_set_size(lc, w->sx, w->sy, 0, 0);
781
0
  layout_make_leaf(lc, wp);
782
0
  layout_fix_panes(w, NULL);
783
0
}
784
785
/* Free layout for pane. */
786
void
787
layout_free(struct window *w, int only_nodes)
788
0
{
789
0
  layout_free_cell(w->layout_root, only_nodes);
790
0
}
791
792
/* Resize the entire layout after window resize. */
793
void
794
layout_resize(struct window *w, u_int sx, u_int sy)
795
0
{
796
0
  struct layout_cell  *lc = w->layout_root;
797
0
  int      xlimit, ylimit, xchange, ychange;
798
799
  /*
800
   * Adjust horizontally. Do not attempt to reduce the layout lower than
801
   * the minimum (more than the amount returned by layout_resize_check).
802
   *
803
   * This can mean that the window size is smaller than the total layout
804
   * size: redrawing this is handled at a higher level, but it does leave
805
   * a problem with growing the window size here: if the current size is
806
   * < the minimum, growing proportionately by adding to each pane is
807
   * wrong as it would keep the layout size larger than the window size.
808
   * Instead, spread the difference between the minimum and the new size
809
   * out proportionately - this should leave the layout fitting the new
810
   * window size.
811
   */
812
0
  if (lc->type == LAYOUT_WINDOWPANE && (lc->flags & LAYOUT_CELL_FLOATING))
813
0
    return;
814
0
  xchange = sx - lc->g.sx;
815
0
  xlimit = layout_resize_check(w, lc, LAYOUT_LEFTRIGHT);
816
0
  if (xchange < 0 && xchange < -xlimit)
817
0
    xchange = -xlimit;
818
0
  if (xlimit == 0) {
819
0
    if (sx <= lc->g.sx) /* lc->g.sx is minimum possible */
820
0
      xchange = 0;
821
0
    else
822
0
      xchange = sx - lc->g.sx;
823
0
  }
824
0
  if (xchange != 0)
825
0
    layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, xchange);
826
827
  /* Adjust vertically in a similar fashion. */
828
0
  ychange = sy - lc->g.sy;
829
0
  ylimit = layout_resize_check(w, lc, LAYOUT_TOPBOTTOM);
830
0
  if (ychange < 0 && ychange < -ylimit)
831
0
    ychange = -ylimit;
832
0
  if (ylimit == 0) {
833
0
    if (sy <= lc->g.sy) /* lc->g.sy is minimum possible */
834
0
      ychange = 0;
835
0
    else
836
0
      ychange = sy - lc->g.sy;
837
0
  }
838
0
  if (ychange != 0)
839
0
    layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, ychange);
840
841
  /* Fix cell offsets. */
842
0
  layout_fix_offsets(w);
843
0
  layout_fix_panes(w, NULL);
844
0
}
845
846
/* Resize a pane to an absolute size. */
847
void
848
layout_resize_pane_to(struct window_pane *wp, enum layout_type type,
849
    u_int new_size)
850
0
{
851
0
  struct layout_cell     *lc, *lcparent;
852
0
  int     change, size;
853
854
0
  lc = wp->layout_cell;
855
856
  /* Find next parent of the same type. */
857
0
  lcparent = lc->parent;
858
0
  while (lcparent != NULL && lcparent->type != type) {
859
0
    lc = lcparent;
860
0
    lcparent = lc->parent;
861
0
  }
862
0
  if (lcparent == NULL)
863
0
    return;
864
865
  /* Work out the size adjustment. */
866
0
  if (type == LAYOUT_LEFTRIGHT)
867
0
    size = lc->g.sx;
868
0
  else
869
0
    size = lc->g.sy;
870
0
  if (layout_cell_is_last_tiled(lc))
871
0
    change = size - new_size;
872
0
  else
873
0
    change = new_size - size;
874
875
  /* Resize the pane. */
876
0
  layout_resize_pane(wp, type, change, 1);
877
0
}
878
879
/* Resize a floating pane to an absolute size. */
880
int
881
layout_resize_floating_pane_to(struct window_pane *wp, enum layout_type type,
882
    u_int size, char **cause)
883
0
{
884
0
  struct layout_cell  *lc = wp->layout_cell;
885
886
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
887
0
    *cause = xstrdup("pane is not floating");
888
0
    return (-1);
889
0
  }
890
891
0
  if (window_pane_get_pane_lines(wp) != PANE_LINES_NONE &&
892
0
      size >= PANE_MINIMUM + 2)
893
0
    size -= 2;
894
0
  if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
895
0
    *cause = xstrdup("size is too big or too small");
896
0
    return (-1);
897
0
  }
898
899
0
  if (type == LAYOUT_TOPBOTTOM) {
900
0
    if (lc->g.sy == size)
901
0
      return (0);
902
0
    lc->g.sy = size;
903
0
  } else {
904
0
    if (lc->g.sx == size)
905
0
      return (0);
906
0
    lc->g.sx = size;
907
0
  }
908
0
  redraw_invalidate_scene(wp->window);
909
0
  return (0);
910
0
}
911
912
/* Resize a floating pane relative to its current size. */
913
int
914
layout_resize_floating_pane(struct window_pane *wp, enum layout_type type,
915
    int change, int opposite, char **cause)
916
0
{
917
0
  struct layout_cell  *lc = wp->layout_cell;
918
0
  u_int      size;
919
920
0
  if (~lc->flags & LAYOUT_CELL_FLOATING) {
921
0
    *cause = xstrdup("pane is not floating");
922
0
    return (-1);
923
0
  }
924
0
  if (change == 0)
925
0
    return (0);
926
927
0
  if (type == LAYOUT_TOPBOTTOM) {
928
0
    size = lc->g.sy + change;
929
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
930
0
      *cause = xstrdup("change is too big or too small");
931
0
      return (-1);
932
0
    }
933
0
    lc->g.sy = size;
934
0
    if (opposite)
935
0
      lc->g.yoff -= change;
936
0
  } else {
937
0
    size = lc->g.sx + change;
938
0
    if (size < PANE_MINIMUM || size > PANE_MAXIMUM) {
939
0
      *cause = xstrdup("change is too big or too small");
940
0
      return (-1);
941
0
    }
942
0
    lc->g.sx = size;
943
0
    if (opposite)
944
0
      lc->g.xoff -= change;
945
0
  }
946
0
  redraw_invalidate_scene(wp->window);
947
0
  return (0);
948
0
}
949
950
/* Resize a layout cell. */
951
void
952
layout_resize_layout(struct window *w, struct layout_cell *lc,
953
    enum layout_type type, int change, int opposite)
954
0
{
955
0
  int needed, size;
956
957
  /* Grow or shrink the cell. */
958
0
  needed = change;
959
0
  while (needed != 0) {
960
0
    if (change > 0) {
961
0
      size = layout_resize_pane_grow(w, lc, type, needed,
962
0
          opposite);
963
0
      needed -= size;
964
0
    } else {
965
0
      size = layout_resize_pane_shrink(w, lc, type, needed);
966
0
      needed += size;
967
0
    }
968
969
0
    if (size == 0) /* no more change possible */
970
0
      break;
971
0
  }
972
973
  /* Fix cell offsets. */
974
0
  layout_fix_offsets(w);
975
0
  layout_fix_panes(w, NULL);
976
0
  events_fire_window("window-layout-changed", w);
977
0
}
978
979
/* Resize a single pane within the layout. */
980
void
981
layout_resize_pane(struct window_pane *wp, enum layout_type type, int change,
982
    int opposite)
983
0
{
984
0
  struct layout_cell  *lc = wp->layout_cell, *lcparent;
985
986
  /* Find next parent of the same type. */
987
0
  lcparent = lc->parent;
988
0
  while (lcparent != NULL && lcparent->type != type) {
989
0
    lc = lcparent;
990
0
    lcparent = lc->parent;
991
0
  }
992
0
  if (lcparent == NULL)
993
0
    return;
994
995
  /* If this is the last tiled cell, move back one. */
996
0
  if (layout_cell_is_last_tiled(lc)) {
997
0
    lc = layout_cell_get_neighbour_dir(lc, 0);
998
0
    if (lc == NULL)
999
0
      return;
1000
0
  }
1001
1002
0
  layout_resize_layout(wp->window, lc, type, change, opposite);
1003
0
}
1004
1005
/* Helper function to grow pane. */
1006
static int
1007
layout_resize_pane_grow(struct window *w, struct layout_cell *lc,
1008
    enum layout_type type, int needed, int opposite)
1009
0
{
1010
0
  struct layout_cell  *lcadd, *lcremove;
1011
0
  u_int      size = 0;
1012
1013
  /* Growing. Always add to the current cell. */
1014
0
  lcadd = lc;
1015
1016
  /* Look towards the tail for a suitable cell for reduction. */
1017
0
  lcremove = layout_cell_get_neighbour_dir(lc, 1);
1018
0
  while (lcremove != NULL) {
1019
0
    size = layout_resize_check(w, lcremove, type);
1020
0
    if (size > 0)
1021
0
      break;
1022
0
    lcremove = layout_cell_get_neighbour_dir(lcremove, 1);
1023
0
  }
1024
1025
  /* If none found, look towards the head. */
1026
0
  if (opposite && lcremove == NULL) {
1027
0
    lcremove = layout_cell_get_neighbour_dir(lc, 0);
1028
0
    while (lcremove != NULL) {
1029
0
      size = layout_resize_check(w, lcremove, type);
1030
0
      if (size > 0)
1031
0
        break;
1032
0
      lcremove = layout_cell_get_neighbour_dir(lcremove, 0);
1033
0
    }
1034
0
  }
1035
0
  if (lcremove == NULL)
1036
0
    return (0);
1037
1038
  /* Change the cells. */
1039
0
  if (size > (u_int) needed)
1040
0
    size = needed;
1041
0
  layout_resize_adjust(w, lcadd, type, size);
1042
0
  layout_resize_adjust(w, lcremove, type, -size);
1043
0
  return (size);
1044
0
}
1045
1046
/* Helper function to shrink pane. */
1047
static int
1048
layout_resize_pane_shrink(struct window *w, struct layout_cell *lc,
1049
    enum layout_type type, int needed)
1050
0
{
1051
0
  struct layout_cell  *lcadd, *lcremove;
1052
0
  u_int      size;
1053
1054
  /* Shrinking. Find cell to remove from by walking towards head. */
1055
0
  lcremove = lc;
1056
0
  do {
1057
0
    size = layout_resize_check(w, lcremove, type);
1058
0
    if (size != 0)
1059
0
      break;
1060
0
    lcremove = layout_cell_get_neighbour_dir(lcremove, 0);
1061
0
  } while (lcremove != NULL);
1062
0
  if (lcremove == NULL)
1063
0
    return (0);
1064
1065
  /* And add onto the next cell (from the original cell). */
1066
0
  lcadd = layout_cell_get_neighbour_dir(lc, 1);
1067
0
  if (lcadd == NULL)
1068
0
    return (0);
1069
1070
  /* Change the cells. */
1071
0
  if (size > (u_int) -needed)
1072
0
    size = -needed;
1073
0
  layout_resize_adjust(w, lcadd, type, size);
1074
0
  layout_resize_adjust(w, lcremove, type, -size);
1075
0
  return (size);
1076
0
}
1077
1078
/* Assign window pane to new cell. */
1079
void
1080
layout_assign_pane(struct layout_cell *lc, struct window_pane *wp,
1081
    int do_not_resize)
1082
0
{
1083
0
  layout_make_leaf(lc, wp);
1084
0
  if (do_not_resize)
1085
0
    layout_fix_panes(wp->window, wp);
1086
0
  else
1087
0
    layout_fix_panes(wp->window, NULL);
1088
0
}
1089
1090
/* Calculate the new pane size for resized parent. */
1091
static u_int
1092
layout_new_pane_size(struct window *w, u_int previous, struct layout_cell *lc,
1093
    enum layout_type type, u_int size, u_int count_left, u_int size_left)
1094
0
{
1095
0
  u_int new_size, min, max, available;
1096
1097
  /* If this is the last cell, it can take all of the remaining size. */
1098
0
  if (count_left == 1)
1099
0
    return (size_left);
1100
1101
  /* How much is available in this parent? */
1102
0
  available = layout_resize_check(w, lc, type);
1103
1104
  /*
1105
   * Work out the minimum size of this cell and the new size
1106
   * proportionate to the previous size.
1107
   */
1108
0
  min = (PANE_MINIMUM + 1) * (count_left - 1);
1109
0
  if (type == LAYOUT_LEFTRIGHT) {
1110
0
    if (lc->g.sx - available > min)
1111
0
      min = lc->g.sx - available;
1112
0
    new_size = (lc->g.sx * size) / previous;
1113
0
  } else {
1114
0
    if (lc->g.sy - available > min)
1115
0
      min = lc->g.sy - available;
1116
0
    new_size = (lc->g.sy * size) / previous;
1117
0
  }
1118
1119
  /* Check against the maximum and minimum size. */
1120
0
  max = size_left - min;
1121
0
  if (new_size > max)
1122
0
    new_size = max;
1123
0
  if (new_size < PANE_MINIMUM)
1124
0
    new_size = PANE_MINIMUM;
1125
0
  return (new_size);
1126
0
}
1127
1128
/* Check if the cell and all its children can be resized to a specific size. */
1129
static int
1130
layout_set_size_check(struct window *w, struct layout_cell *lc,
1131
    enum layout_type type, int size)
1132
0
{
1133
0
  struct layout_cell  *lcchild;
1134
0
  u_int      new_size, available, previous, count, idx;
1135
1136
  /* Cells with no children must just be bigger than minimum. */
1137
0
  if (lc->type == LAYOUT_WINDOWPANE)
1138
0
    return (size >= PANE_MINIMUM);
1139
0
  available = size;
1140
1141
  /* Count number of children. */
1142
0
  count = 0;
1143
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry)
1144
0
    count++;
1145
1146
  /* Check new size will work for each child. */
1147
0
  if (lc->type == type) {
1148
0
    if (available < (count * 2) - 1)
1149
0
      return (0);
1150
1151
0
    if (type == LAYOUT_LEFTRIGHT)
1152
0
      previous = lc->g.sx;
1153
0
    else
1154
0
      previous = lc->g.sy;
1155
1156
0
    idx = 0;
1157
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1158
0
      new_size = layout_new_pane_size(w, previous, lcchild,
1159
0
          type, size, count - idx, available);
1160
0
      if (idx == count - 1) {
1161
0
        if (new_size > available)
1162
0
          return (0);
1163
0
        available -= new_size;
1164
0
      } else {
1165
0
        if (new_size + 1 > available)
1166
0
          return (0);
1167
0
        available -= new_size + 1;
1168
0
      }
1169
0
      if (!layout_set_size_check(w, lcchild, type, new_size))
1170
0
        return (0);
1171
0
      idx++;
1172
0
    }
1173
0
  } else {
1174
0
    TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1175
0
      if (lcchild->type == LAYOUT_WINDOWPANE)
1176
0
        continue;
1177
0
      if (!layout_set_size_check(w, lcchild, type, size))
1178
0
        return (0);
1179
0
    }
1180
0
  }
1181
1182
0
  return (1);
1183
0
}
1184
1185
/* Resize all child cells to fit within the current cell. */
1186
static void
1187
layout_resize_child_cells(struct window *w, struct layout_cell *lc)
1188
0
{
1189
0
  struct layout_cell  *lcchild;
1190
0
  u_int      prev, available, count, idx;
1191
1192
0
  if (lc->type == LAYOUT_WINDOWPANE)
1193
0
    return;
1194
1195
  /* What is the current size used? */
1196
0
  count = 0;
1197
0
  prev = 0;
1198
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1199
0
    if (!layout_cell_is_tiled(lcchild) &&
1200
0
        !layout_cell_has_tiled_child(lcchild))
1201
0
      continue;
1202
0
    count++;
1203
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1204
0
      prev += lcchild->g.sx;
1205
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1206
0
      prev += lcchild->g.sy;
1207
0
  }
1208
0
  prev += (count - 1);
1209
1210
  /* And how much is available? */
1211
0
  available = 0;
1212
0
  if (lc->type == LAYOUT_LEFTRIGHT)
1213
0
    available = lc->g.sx;
1214
0
  else if (lc->type == LAYOUT_TOPBOTTOM)
1215
0
    available = lc->g.sy;
1216
1217
  /* Resize children into the new size. */
1218
0
  idx = 0;
1219
0
  TAILQ_FOREACH(lcchild, &lc->cells, entry) {
1220
0
    if (!layout_cell_is_tiled(lcchild) &&
1221
0
        !layout_cell_has_tiled_child(lcchild))
1222
0
      continue;
1223
0
    if (lc->type == LAYOUT_TOPBOTTOM) {
1224
0
      lcchild->g.sx = lc->g.sx;
1225
0
      lcchild->g.xoff = lc->g.xoff;
1226
0
    } else {
1227
0
      lcchild->g.sx = layout_new_pane_size(w, prev, lcchild,
1228
0
          lc->type, lc->g.sx, count - idx, available);
1229
0
      available -= (lcchild->g.sx + 1);
1230
0
    }
1231
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1232
0
      lcchild->g.sy = lc->g.sy;
1233
0
      lcchild->g.yoff = lc->g.yoff;
1234
0
    } else {
1235
0
      lcchild->g.sy = layout_new_pane_size(w, prev, lcchild,
1236
0
          lc->type, lc->g.sy, count - idx, available);
1237
0
      available -= (lcchild->g.sy + 1);
1238
0
    }
1239
0
    layout_resize_child_cells(w, lcchild);
1240
0
    idx++;
1241
0
  }
1242
0
}
1243
1244
/*
1245
 * Replaces the provided layout cell with a new node of the specified type and
1246
 * inserts the cell into it. Used when creating new cells requires a different
1247
 * layout type, or when the root layout is a window pane.
1248
 */
1249
struct layout_cell *
1250
layout_replace_with_node(struct window *w, struct layout_cell *lc,
1251
    enum layout_type type)
1252
0
{
1253
0
  struct layout_cell  *lcparent;
1254
1255
0
  lcparent = layout_create_cell(lc->parent);
1256
0
  layout_make_node(lcparent, type);
1257
0
  layout_set_size(lcparent, lc->g.sx, lc->g.sy, lc->g.xoff, lc->g.yoff);
1258
0
  if (lc->parent == NULL)
1259
0
    w->layout_root = lcparent;
1260
0
  else
1261
0
    TAILQ_REPLACE(&lc->parent->cells, lc, lcparent, entry);
1262
1263
  /* Insert the old cell. */
1264
0
  lc->parent = lcparent;
1265
0
  TAILQ_INSERT_HEAD(&lcparent->cells, lc, entry);
1266
1267
0
  return (lcparent);
1268
0
}
1269
1270
/* Checks if there is enough space for two new panes. */
1271
int
1272
layout_split_check_space(struct window_pane *wp, struct layout_cell *lc,
1273
   enum layout_type type)
1274
0
{
1275
0
  struct layout_cell  *root = wp->window->layout_root;
1276
0
  struct style    *sb_style = &wp->scrollbar_style;
1277
0
  u_int      minimum, sx = lc->g.sx, sy = lc->g.sy;
1278
0
  int      status;
1279
1280
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1281
0
    fatalx("floating cells cannot be split");
1282
1283
0
  status = window_get_pane_status(wp->window);
1284
1285
0
  switch (type) {
1286
0
  case LAYOUT_LEFTRIGHT:
1287
0
    if (wp->window->sb == PANE_SCROLLBARS_ALWAYS) {
1288
0
      minimum = PANE_MINIMUM * 2 + sb_style->width +
1289
0
          sb_style->pad;
1290
0
    } else
1291
0
      minimum = PANE_MINIMUM * 2 + 1;
1292
0
    if (sx < minimum)
1293
0
      return (0);
1294
0
    break;
1295
0
  case LAYOUT_TOPBOTTOM:
1296
0
    if (layout_add_horizontal_border(root, lc, status))
1297
0
      minimum = PANE_MINIMUM * 2 + 2;
1298
0
    else
1299
0
      minimum = PANE_MINIMUM * 2 + 1;
1300
0
    if (sy < minimum)
1301
0
      return (0);
1302
0
    break;
1303
0
  default:
1304
0
    fatalx("bad layout type");
1305
0
  }
1306
1307
0
  return (1);
1308
0
}
1309
1310
/* Calculates the new cell sizes when splitting a pane. */
1311
void
1312
layout_split_sizes(struct layout_cell *lc, int size, int before,
1313
    enum layout_type type, u_int *size1, u_int *size2, u_int *saved_size)
1314
0
{
1315
0
  u_int s1, s2, ss;
1316
0
  u_int sx = lc->g.sx, sy = lc->g.sy;
1317
1318
0
  if (type == LAYOUT_LEFTRIGHT)
1319
0
    ss = sx;
1320
0
  else
1321
0
    ss = sy;
1322
0
  if (size < 0)
1323
0
    s2 = ((ss + 1) / 2) - 1;
1324
0
  else if (before)
1325
0
    s2 = ss - size - 1;
1326
0
  else
1327
0
    s2 = size;
1328
0
  if (s2 < PANE_MINIMUM)
1329
0
    s2 = PANE_MINIMUM;
1330
0
  else if (s2 > ss - 2)
1331
0
    s2 = ss - 2;
1332
0
  s1 = ss - 1 - s2;
1333
1334
0
  *size1 = s1;
1335
0
  *size2 = s2;
1336
0
  *saved_size = ss;
1337
0
}
1338
1339
/*
1340
 * Split a pane into two. size is a hint, or -1 for default half/half
1341
 * split. This must be followed by layout_assign_pane before much else happens!
1342
 */
1343
struct layout_cell *
1344
layout_split_pane(struct window_pane *wp, enum layout_type type, int size,
1345
    int flags)
1346
0
{
1347
0
  struct layout_cell  *lc, *lcparent, *lcnew, *lc1, *lc2;
1348
0
  u_int      sx, sy, xoff, yoff, size1, size2;
1349
0
  u_int      new_size, saved_size, resize_first = 0;
1350
0
  int      full_size = (flags & SPAWN_FULLSIZE);
1351
0
  int      before = (flags & SPAWN_BEFORE);
1352
1353
  /*
1354
   * If full_size is specified, add a new cell at the top of the window
1355
   * layout. Otherwise, split the cell for the current pane.
1356
   */
1357
0
  if (full_size)
1358
0
    lc = wp->window->layout_root;
1359
0
  else
1360
0
    lc = wp->layout_cell;
1361
1362
  /* Copy the old cell size. */
1363
0
  sx = lc->g.sx;
1364
0
  sy = lc->g.sy;
1365
0
  xoff = lc->g.xoff;
1366
0
  yoff = lc->g.yoff;
1367
1368
  /* Check there is enough space for the two new panes. */
1369
0
  if (!layout_split_check_space(wp, lc, type))
1370
0
    return (NULL);
1371
1372
  /*
1373
   * Calculate new cell sizes. size is the target size or -1 for middle
1374
   * split, size1 is the size of the top/left and size2 the bottom/right.
1375
   */
1376
0
  layout_split_sizes(lc, size, before, type, &size1, &size2, &saved_size);
1377
1378
  /* Which size are we using? */
1379
0
  if (flags & SPAWN_BEFORE)
1380
0
    new_size = size2;
1381
0
  else
1382
0
    new_size = size1;
1383
1384
  /* Confirm there is enough space for full size pane. */
1385
0
  if (full_size && !layout_set_size_check(wp->window, lc, type, new_size))
1386
0
    return (NULL);
1387
1388
0
  if (lc->parent != NULL && lc->parent->type == type) {
1389
    /*
1390
     * If the parent exists and is of the same type as the split,
1391
     * create a new cell and insert it after this one.
1392
     */
1393
0
    lcparent = lc->parent;
1394
0
    lcnew = layout_create_cell(lcparent);
1395
0
    if (flags & SPAWN_BEFORE)
1396
0
      TAILQ_INSERT_BEFORE(lc, lcnew, entry);
1397
0
    else
1398
0
      TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1399
0
  } else if (full_size && lc->parent == NULL && lc->type == type) {
1400
    /*
1401
     * If the new full size pane is the same type as the root
1402
     * split, insert the new pane under the existing root cell
1403
     * instead of creating a new root cell. The existing layout
1404
     * must be resized before inserting the new cell.
1405
     */
1406
0
    if (lc->type == LAYOUT_LEFTRIGHT) {
1407
0
      lc->g.sx = new_size;
1408
0
      layout_resize_child_cells(wp->window, lc);
1409
0
      lc->g.sx = saved_size;
1410
0
    } else if (lc->type == LAYOUT_TOPBOTTOM) {
1411
0
      lc->g.sy = new_size;
1412
0
      layout_resize_child_cells(wp->window, lc);
1413
0
      lc->g.sy = saved_size;
1414
0
    }
1415
0
    resize_first = 1;
1416
1417
    /* Create the new cell. */
1418
0
    lcnew = layout_create_cell(lc);
1419
0
    size = saved_size - 1 - new_size;
1420
0
    if (lc->type == LAYOUT_LEFTRIGHT)
1421
0
      layout_set_size(lcnew, size, sy, 0, 0);
1422
0
    else if (lc->type == LAYOUT_TOPBOTTOM)
1423
0
      layout_set_size(lcnew, sx, size, 0, 0);
1424
0
    if (flags & SPAWN_BEFORE)
1425
0
      TAILQ_INSERT_HEAD(&lc->cells, lcnew, entry);
1426
0
    else
1427
0
      TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
1428
0
  } else {
1429
    /*
1430
     * Otherwise create a new parent and insert it.
1431
     */
1432
1433
    /* Create and insert the replacement parent. */
1434
0
    lcparent = layout_replace_with_node(wp->window, lc, type);
1435
1436
    /* Create the new child cell. */
1437
0
    lcnew = layout_create_cell(lcparent);
1438
0
    if (flags & SPAWN_BEFORE)
1439
0
      TAILQ_INSERT_HEAD(&lcparent->cells, lcnew, entry);
1440
0
    else
1441
0
      TAILQ_INSERT_TAIL(&lcparent->cells, lcnew, entry);
1442
0
  }
1443
0
  if (flags & SPAWN_BEFORE) {
1444
0
    lc1 = lcnew;
1445
0
    lc2 = lc;
1446
0
  } else {
1447
0
    lc1 = lc;
1448
0
    lc2 = lcnew;
1449
0
  }
1450
1451
  /*
1452
   * Set new cell sizes. size1 is the size of the top/left and size2 the
1453
   * bottom/right.
1454
   */
1455
0
  if (!resize_first && type == LAYOUT_LEFTRIGHT) {
1456
0
    layout_set_size(lc1, size1, sy, xoff, yoff);
1457
0
    layout_set_size(lc2, size2, sy, xoff + lc1->g.sx + 1, yoff);
1458
0
  } else if (!resize_first && type == LAYOUT_TOPBOTTOM) {
1459
0
    layout_set_size(lc1, sx, size1, xoff, yoff);
1460
0
    layout_set_size(lc2, sx, size2, xoff, yoff + lc1->g.sy + 1);
1461
0
  }
1462
0
  if (full_size) {
1463
0
    if (!resize_first)
1464
0
      layout_resize_child_cells(wp->window, lc);
1465
0
    layout_fix_offsets(wp->window);
1466
0
  } else
1467
0
    layout_make_leaf(lc, wp);
1468
1469
0
  return (lcnew);
1470
0
}
1471
1472
/*
1473
 * Creates a cell for a new floating pane. This must be followed by
1474
 * layout_assign_pane before much else happens!
1475
 */
1476
struct layout_cell *
1477
layout_floating_pane(struct window *w, struct window_pane *wp,
1478
    struct layout_geometry *lg)
1479
0
{
1480
0
  struct layout_cell  *lc, *lcnew, *lcparent;
1481
1482
0
  if (wp == NULL)
1483
0
    lc = w->layout_root;
1484
0
  else
1485
0
    lc = wp->layout_cell;
1486
0
  lcparent = lc->parent;
1487
1488
0
  if (lcparent == NULL) {
1489
    /*
1490
     * Adding a pane to a root that isn't a node. Must create and
1491
     * insert a new root.
1492
     */
1493
0
    lcparent = layout_replace_with_node(w, lc, LAYOUT_TOPBOTTOM);
1494
0
  }
1495
1496
0
  lcnew = layout_create_cell(lcparent);
1497
0
  TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
1498
0
  lcnew->flags |= LAYOUT_CELL_FLOATING;
1499
0
  layout_set_size(lcnew, lg->sx, lg->sy, lg->xoff, lg->yoff);
1500
1501
0
  return (lcnew);
1502
0
}
1503
1504
/* Destroy the cell associated with a pane. */
1505
void
1506
layout_close_pane(struct window_pane *wp)
1507
0
{
1508
0
  struct window *w = wp->window;
1509
1510
0
  if (wp->layout_cell == NULL)
1511
0
    return;
1512
1513
  /* Remove the cell. */
1514
0
  layout_destroy_cell(w, wp->layout_cell, &w->layout_root);
1515
0
  wp->layout_cell = NULL;
1516
1517
  /* Fix pane offsets and sizes. */
1518
0
  if (w->layout_root != NULL) {
1519
0
    layout_fix_offsets(w);
1520
0
    layout_fix_panes(w, NULL);
1521
0
  }
1522
0
  events_fire_window("window-layout-changed", w);
1523
0
}
1524
1525
/* Spread out cells inside a parent cell. */
1526
int
1527
layout_spread_cell(struct window *w, struct layout_cell *parent)
1528
0
{
1529
0
  struct layout_cell  *lc, *root = w->layout_root;
1530
0
  u_int      number, each, size, this, remainder;
1531
0
  int      change, changed, status;
1532
1533
0
  number = 0;
1534
0
  TAILQ_FOREACH (lc, &parent->cells, entry)
1535
0
    if (layout_cell_is_tiled(lc))
1536
0
      number++;
1537
0
  if (number <= 1)
1538
0
    return (0);
1539
0
  status = window_get_pane_status(w);
1540
1541
0
  if (parent->type == LAYOUT_LEFTRIGHT)
1542
0
    size = parent->g.sx;
1543
0
  else if (parent->type == LAYOUT_TOPBOTTOM) {
1544
0
    if (layout_add_horizontal_border(root, parent, status))
1545
0
      size = parent->g.sy - 1;
1546
0
    else
1547
0
      size = parent->g.sy;
1548
0
  } else
1549
0
    return (0);
1550
0
  if (size < number - 1)
1551
0
    return (0);
1552
0
  each = (size - (number - 1)) / number;
1553
0
  if (each == 0)
1554
0
    return (0);
1555
1556
  /*
1557
   * Remaining space after assigning that which can be evenly
1558
   * distributed.
1559
   */
1560
0
  remainder = size - (number * (each + 1)) + 1;
1561
1562
0
  changed = 0;
1563
0
  TAILQ_FOREACH (lc, &parent->cells, entry) {
1564
0
    if (!layout_cell_is_tiled(lc))
1565
0
      continue;
1566
0
    change = 0;
1567
0
    if (parent->type == LAYOUT_LEFTRIGHT) {
1568
0
      change = each - (int)lc->g.sx;
1569
0
      if (remainder > 0) {
1570
0
        change++;
1571
0
        remainder--;
1572
0
      }
1573
0
      layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, change);
1574
0
    } else if (parent->type == LAYOUT_TOPBOTTOM) {
1575
0
      if (layout_add_horizontal_border(root, lc, status))
1576
0
        this = each + 1;
1577
0
      else
1578
0
        this = each;
1579
0
      if (remainder > 0) {
1580
0
        this++;
1581
0
        remainder--;
1582
0
      }
1583
0
      change = this - (int)lc->g.sy;
1584
0
      layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, change);
1585
0
    }
1586
0
    if (change != 0)
1587
0
      changed = 1;
1588
0
  }
1589
0
  return (changed);
1590
0
}
1591
1592
/* Spread out cells evenly. */
1593
void
1594
layout_spread_out(struct window_pane *wp)
1595
0
{
1596
0
  struct layout_cell  *parent;
1597
0
  struct window   *w = wp->window;
1598
1599
0
  parent = wp->layout_cell->parent;
1600
0
  if (parent == NULL)
1601
0
    return;
1602
1603
0
  do {
1604
0
    if (layout_spread_cell(w, parent)) {
1605
0
      layout_fix_offsets(w);
1606
0
      layout_fix_panes(w, NULL);
1607
0
      break;
1608
0
    }
1609
0
  } while ((parent = parent->parent) != NULL);
1610
0
}
1611
1612
/* Get a new tiled cell. */
1613
struct layout_cell *
1614
layout_get_tiled_cell(struct cmdq_item *item, struct args *args,
1615
    struct window *w, struct window_pane *wp, int flags, char **cause)
1616
0
{
1617
0
  struct layout_cell  *lc;
1618
0
  enum layout_type   type = LAYOUT_TOPBOTTOM;
1619
0
  u_int      curval;
1620
0
  int      size = -1;
1621
0
  char      *error = NULL;
1622
1623
0
  if (window_pane_is_floating(wp)) {
1624
0
    *cause = xstrdup("can't split a floating pane");
1625
0
    return (NULL);
1626
0
  }
1627
1628
0
  if (flags & SPAWN_HORIZONTAL)
1629
0
    type = LAYOUT_LEFTRIGHT;
1630
1631
0
  if (args_has(args, 'l') || args_has(args, 'p')) {
1632
0
    if (flags & SPAWN_FULLSIZE) {
1633
0
      if (type == LAYOUT_TOPBOTTOM)
1634
0
        curval = w->sy;
1635
0
      else
1636
0
        curval = w->sx;
1637
0
    } else {
1638
0
      if (type == LAYOUT_TOPBOTTOM)
1639
0
        curval = wp->sy;
1640
0
      else
1641
0
        curval = wp->sx;
1642
0
    }
1643
0
  }
1644
1645
0
  if (args_has(args, 'l')) {
1646
0
    size = args_percentage_and_expand(args, 'l', 0, INT_MAX, curval,
1647
0
        item, &error);
1648
0
  } else if (args_has(args, 'p')) {
1649
0
    size = args_strtonum_and_expand(args, 'p', 0, 100, item,
1650
0
        &error);
1651
0
    if (error == NULL)
1652
0
      size = curval * size / 100;
1653
0
  }
1654
0
  if (error != NULL) {
1655
0
    xasprintf(cause, "invalid tiled geometry %s", error);
1656
0
    free(error);
1657
0
    return (NULL);
1658
0
  }
1659
1660
0
  if (window_active_pane_is_over_zoom(w))
1661
0
    window_push_zoom(w, 0, 1);
1662
0
  else
1663
0
    window_push_zoom(w, 1, (flags & SPAWN_ZOOM));
1664
0
  lc = layout_split_pane(wp, type, size, flags);
1665
0
  if (lc == NULL)
1666
0
    *cause = xstrdup("no space for a new pane");
1667
1668
0
  return (lc);
1669
0
}
1670
1671
struct layout_cell *
1672
layout_get_floating_cell(struct cmdq_item *item, struct args *args,
1673
    enum pane_lines lines, struct window *w, struct window_pane *wp, int flags,
1674
    char **cause)
1675
0
{
1676
0
  struct layout_cell  *lcnew, *lc = wp->layout_cell;
1677
0
  struct layout_geometry   fg;
1678
1679
0
  layout_geometry_init(&fg);
1680
0
  if (flags & SPAWN_SPLIT) {
1681
0
    if (layout_split_floating_cell(lc, w, &fg, lines, flags, cause)
1682
0
        != 0)
1683
0
      return (NULL);
1684
0
  } else {
1685
0
    if (layout_floating_args_parse(item, args, lines, w, &fg, cause)
1686
0
        != 0)
1687
0
      return (NULL);
1688
0
  }
1689
1690
0
  if (flags & SPAWN_FLOATOVERZOOM)
1691
0
    window_push_zoom(wp->window, 0, 1);
1692
0
  else if (window_active_pane_is_over_zoom(w))
1693
0
    window_push_zoom(wp->window, 0, 1);
1694
0
  else
1695
0
    window_push_zoom(wp->window, 1, (flags & SPAWN_ZOOM));
1696
0
  lcnew = layout_floating_pane(w, wp, &fg);
1697
0
  return (lcnew);
1698
0
}
1699
1700
int
1701
layout_floating_args_parse(struct cmdq_item *item, struct args *args,
1702
    enum pane_lines lines, struct window *w, struct layout_geometry *lg,
1703
    char **cause)
1704
0
{
1705
0
  int  sx, sy, ox, oy;
1706
0
  char  *error = NULL;
1707
1708
0
  sx = lg->sx == UINT_MAX ? w->sx / 2 : lg->sx;
1709
0
  sy = lg->sy == UINT_MAX ? w->sy / 4 : lg->sy;
1710
0
  ox = lg->xoff;
1711
0
  oy = lg->yoff;
1712
1713
0
  if (args_has(args, 'x')) {
1714
0
    sx = args_percentage_and_expand(args, 'x', 0, PANE_MAXIMUM,
1715
0
        w->sx, item, &error);
1716
0
    if (error != NULL) {
1717
0
      xasprintf(cause, "position %s", error);
1718
0
      free(error);
1719
0
      return (-1);
1720
0
    }
1721
0
    if (lines != PANE_LINES_NONE)
1722
0
      sx -= 2;
1723
0
  }
1724
0
  if (args_has(args, 'y')) {
1725
0
    sy = args_percentage_and_expand(args, 'y', 0, PANE_MAXIMUM,
1726
0
        w->sy, item, &error);
1727
0
    if (error != NULL) {
1728
0
      xasprintf(cause, "position %s", error);
1729
0
      free(error);
1730
0
      return (-1);
1731
0
    }
1732
0
    if (lines != PANE_LINES_NONE)
1733
0
      sy -= 2;
1734
0
  }
1735
0
  if (args_has(args, 'X')) {
1736
0
    ox = args_percentage_and_expand(args, 'X', -sx, w->sx,
1737
0
        w->sx, item, &error);
1738
0
    if (error != NULL) {
1739
0
      xasprintf(cause, "position %s", error);
1740
0
      free(error);
1741
0
      return (-1);
1742
0
    }
1743
0
  }
1744
0
  if (args_has(args, 'Y')) {
1745
0
    oy = args_percentage_and_expand(args, 'Y', -sy, w->sy,
1746
0
        w->sy, item, &error);
1747
0
    if (error != NULL) {
1748
0
      xasprintf(cause, "position %s", error);
1749
0
      free(error);
1750
0
      return (-1);
1751
0
    }
1752
0
  }
1753
1754
0
  if (ox == INT_MAX) {
1755
0
    if (w->last_new_pane_x == 0)
1756
0
      ox = 4;
1757
0
    else {
1758
0
      ox = w->last_new_pane_x + 4;
1759
0
      if (w->last_new_pane_x > w->sx)
1760
0
        ox = 4;
1761
0
    }
1762
0
    w->last_new_pane_x = ox;
1763
0
  } else if (args_has(args, 'X'))
1764
0
    if (lines != PANE_LINES_NONE)
1765
0
      ox += 1;
1766
0
  if (oy == INT_MAX) {
1767
0
    if (w->last_new_pane_y == 0)
1768
0
      oy = 2;
1769
0
    else {
1770
0
      oy = w->last_new_pane_y + 2;
1771
0
      if (w->last_new_pane_y > w->sy)
1772
0
        oy = 2;
1773
0
    }
1774
0
    w->last_new_pane_y = oy;
1775
0
  } else if (args_has(args, 'Y'))
1776
0
    if (lines != PANE_LINES_NONE)
1777
0
      oy += 1;
1778
1779
0
  if (sx < PANE_MINIMUM || sx > PANE_MAXIMUM) {
1780
0
    *cause = xstrdup("invalid width");
1781
0
    return (-1);
1782
0
  }
1783
0
  if (sy < PANE_MINIMUM || sy > PANE_MAXIMUM) {
1784
0
    *cause = xstrdup("invalid height");
1785
0
    return (-1);
1786
0
  }
1787
1788
0
  lg->sx = sx;
1789
0
  lg->sy = sy;
1790
0
  lg->xoff = ox;
1791
0
  lg->yoff = oy;
1792
0
  return (0);
1793
0
}
1794
1795
int
1796
layout_split_floating_cell(struct layout_cell *lc, struct window *w,
1797
    struct layout_geometry *out, enum pane_lines lines, int flags,
1798
    char **cause)
1799
0
{
1800
0
  struct layout_geometry   old, new;
1801
0
  int      tborder = 1, bborder = w->sy - 1;
1802
0
  int      lborder = 3, rborder = w->sx - 3;
1803
0
  int      border = lines != PANE_LINES_NONE ? 1 : 0;
1804
0
  int      size, space;
1805
1806
  /* First, move the target cell in-bounds. */
1807
0
  memcpy(&old, &lc->g, sizeof old);
1808
0
  if (lborder > old.xoff - border)
1809
0
    old.xoff = lborder + border;
1810
0
  if (rborder < old.xoff + (int)old.sx + border)
1811
0
    old.xoff = rborder - (int)old.sx - border;
1812
0
  if (tborder > old.yoff - border)
1813
0
    old.yoff = tborder + border;
1814
0
  if (bborder < old.yoff + (int)old.sy + border)
1815
0
    old.yoff = bborder - (int)old.sy - border;
1816
1817
  /* Move the new cell to its ideal position. */
1818
0
  memcpy(&new, &old, sizeof new);
1819
0
  if (flags & SPAWN_HORIZONTAL) {
1820
0
    if (flags & SPAWN_BEFORE)
1821
0
      new.xoff -= old.sx + 2 * border;
1822
0
    else
1823
0
      new.xoff += old.sx + 2 * border;
1824
0
  } else {
1825
0
    if (flags & SPAWN_BEFORE)
1826
0
      new.yoff -= old.sy + 2 * border;
1827
0
    else
1828
0
      new.yoff += old.sy + 2 * border;
1829
0
  }
1830
1831
  /*
1832
   * The position of the new cell is checked to see if it is in bounds.
1833
   * If it isn't, the availible space is split and equally given to both
1834
   * cells. Only one border is check because the target cell is in bounds
1835
   * already.
1836
   */
1837
0
  if (lborder > new.xoff - border) {
1838
    /*
1839
     * The space for both panes is calculated. Since the offsets are
1840
     * associated to where pane contents start, we remove pane
1841
     * borders from the space. '1' is added in case the space is
1842
     * odd.
1843
     */
1844
0
    space = old.xoff + old.sx - lborder - 3 * border + 1;
1845
0
    size = space / 2;
1846
0
    new.sx = size;
1847
0
    old.sx = size;
1848
0
    new.xoff = lborder + border;
1849
0
    old.xoff = new.xoff + new.sx + 2 * border;
1850
    /*
1851
     * If the original space was to be odd (now even), subtract 1
1852
     * from the rightmost cell
1853
     */
1854
0
    if (space % 2 == 0)
1855
0
      old.sx -= 1;
1856
0
  } else if (rborder < new.xoff + (int)new.sx + border) {
1857
0
    space = rborder - old.xoff - 3 * border + 1;
1858
0
    size = space / 2;
1859
0
    new.sx = size;
1860
0
    old.sx = size;
1861
0
    new.xoff = old.xoff + old.sx + 2 * border;
1862
0
    if (space % 2 == 0)
1863
0
      new.sx -= 1;
1864
0
  } else if (tborder > new.yoff - border) {
1865
0
    space = old.sy + old.yoff - tborder - 3 * border + 1;
1866
0
    size = space / 2;
1867
0
    new.sy = size;
1868
0
    old.sy = size;
1869
0
    new.yoff = tborder + border;
1870
0
    old.yoff = new.yoff + new.sy + 2 * border;
1871
0
    if (space % 2 == 0)
1872
0
      old.sy -= 1;
1873
0
  } else if (bborder < new.yoff + (int)new.sy + border) {
1874
0
    space = bborder - old.yoff - 3 * border + 1;
1875
0
    size = space / 2;
1876
0
    new.sy = size;
1877
0
    old.sy = size;
1878
0
    new.yoff = old.yoff + old.sy + 2 * border;
1879
0
    if (space % 2 == 0)
1880
0
      new.sy -= 1;
1881
0
  }
1882
1883
  /*
1884
   * Expand the cell to occupy the whole availible space where it was
1885
   * spawned.
1886
   */
1887
0
  if (flags & SPAWN_FULLSIZE) {
1888
0
    if (flags & SPAWN_HORIZONTAL) {
1889
0
      new.yoff = tborder + border;
1890
0
      new.sy = bborder - tborder - 2 * border;
1891
0
      if (flags & SPAWN_BEFORE) {
1892
0
        new.xoff = lborder + border;
1893
0
        new.sx = old.xoff - new.xoff - 2 * border;
1894
0
      } else {
1895
0
        new.sx = rborder - new.xoff - border;
1896
0
      }
1897
0
    } else {
1898
0
      new.xoff = lborder + border;
1899
0
      new.sx = rborder - lborder - 2 * border;
1900
0
      if (flags & SPAWN_BEFORE) {
1901
0
        new.yoff = tborder + border;
1902
0
        new.sy = old.yoff - new.yoff - 2 * border;
1903
0
      } else {
1904
0
        new.sy = bborder - new.yoff - border;
1905
0
      }
1906
0
    }
1907
0
  }
1908
1909
0
  if (new.sx < PANE_MINIMUM || new.sy < PANE_MINIMUM ||
1910
0
      old.sx < PANE_MINIMUM || old.sy < PANE_MINIMUM) {
1911
0
    *cause = xstrdup("no space for a new pane");
1912
0
    return (-1);
1913
0
  }
1914
1915
0
  layout_set_size(lc, old.sx, old.sy, old.xoff, old.yoff);
1916
0
  memcpy(out, &new, sizeof *out);
1917
0
  return (0);
1918
0
}
1919
1920
/*
1921
 * Removes a cell from the tiled layout by giving the cell's space to the
1922
 * nearest neighbour.
1923
 */
1924
int
1925
layout_remove_tile(struct window *w, struct layout_cell *lc)
1926
0
{
1927
0
  struct layout_cell  *lcneighbour, *lcparent;
1928
0
  enum layout_type   type;
1929
0
  int      change;
1930
1931
0
  if (lc->flags & LAYOUT_CELL_FLOATING)
1932
0
    return (-1);
1933
1934
0
  lcneighbour = layout_cell_get_neighbour(lc);
1935
0
  if (lcneighbour == NULL) {
1936
0
    if (lc->parent != NULL)
1937
0
      layout_remove_tile(w, lc->parent);
1938
0
  } else if ((lcparent = lcneighbour->parent) != NULL) {
1939
0
    type = lcparent->type;
1940
    /*
1941
     * Adding the size of the layout cell plus its border to the
1942
     * neighbour.
1943
     */
1944
0
    if (type == LAYOUT_TOPBOTTOM)
1945
0
      change = lc->g.sy + 1;
1946
0
    else
1947
0
      change = lc->g.sx + 1;
1948
0
    layout_resize_adjust(w, lcneighbour, type, change);
1949
0
  }
1950
1951
  /*
1952
   * Zeroing out the cell geometry until the cell is retiled unless this
1953
   * is the top level node.
1954
   */
1955
0
  if (lc->parent != NULL)
1956
0
    layout_set_size(lc, 0, 0, 0, 0);
1957
0
  return (0);
1958
0
}
1959
1960
/*
1961
 * Inserts a cell back into the tiled layout by taking half the space from its
1962
 * nearest neighbour.
1963
 */
1964
int
1965
layout_insert_tile(struct window *w, struct layout_cell *lc)
1966
0
{
1967
0
  struct layout_cell  *lcneighbour, *lctiled, *lcparent;
1968
0
  enum layout_type   type;
1969
0
  u_int      size1, size2, saved_size;
1970
1971
0
  if (lc == NULL)
1972
0
    fatalx("layout cell cannot be null when tiling");
1973
1974
0
  if (layout_cell_is_tiled(lc))
1975
0
    return (-1);
1976
1977
0
  lcparent = lc->parent;
1978
0
  if (lcparent == NULL) {
1979
    /* Only pane in the layout. */
1980
0
    layout_set_size(lc, w->sx, w->sy, 0, 0);
1981
0
    return (0);
1982
0
  }
1983
1984
0
  type = lcparent->type;
1985
0
  lcneighbour = layout_cell_get_neighbour(lc);
1986
0
  if (lcneighbour == NULL) {
1987
    /*
1988
     * This will become the only visible cell in the parent.
1989
     * Tile the parent, then set the child's 'split' size.
1990
     */
1991
0
    layout_insert_tile(w, lcparent);
1992
0
    if (type == LAYOUT_LEFTRIGHT)
1993
0
      size1 = lcparent->g.sx;
1994
0
    else
1995
0
      size1 = lcparent->g.sy;
1996
0
    layout_resize_set_size(w, lc, type, size1);
1997
0
  } else {
1998
    /*
1999
     * If the neighbour is a node, a tiled child in the subtree of
2000
     * the neighbour is needed to check for space.
2001
     */
2002
0
    lctiled = layout_cell_get_first_tiled(lcneighbour);
2003
0
    if (!layout_split_check_space(lctiled->wp, lcneighbour, type))
2004
0
      return (-1);
2005
0
    layout_split_sizes(lcneighbour, -1, 0, type, &size1, &size2,
2006
0
        &saved_size);
2007
0
    layout_resize_set_size(w, lc, type, size1);
2008
0
    layout_resize_set_size(w, lcneighbour, type, size2);
2009
0
  }
2010
2011
  /* Setting opposite of the 'split' size to that of the parent. */
2012
0
  if (lcparent->type == LAYOUT_LEFTRIGHT) {
2013
0
    size1 = lcparent->g.sy;
2014
0
    type = LAYOUT_TOPBOTTOM;
2015
0
  } else {
2016
0
    size1 = lcparent->g.sx;
2017
0
    type = LAYOUT_LEFTRIGHT;
2018
0
  }
2019
0
  layout_resize_set_size(w, lc, type, size1);
2020
2021
0
  return (0);
2022
0
}