summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/layout-manual.c
blob: 66d472d620bca31ae5acfda835f8ae5c7deb54f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* $OpenBSD: layout-manual.c,v 1.1 2009/06/01 22:58:49 nicm Exp $ */

/*
 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include "tmux.h"

void	layout_manual_v_update_offsets(struct window *);

void
layout_manual_v_refresh(struct window *w, unused int active_only)
{
	struct window_pane	*wp;
	u_int			 npanes, canfit, total;
	int			 left;

	if (active_only)
		return;

	if (TAILQ_EMPTY(&w->panes))
		return;

	/* Clear hidden flags. */
	TAILQ_FOREACH(wp, &w->panes, entry)
	    	wp->flags &= ~PANE_HIDDEN;

	/* Check the new size. */
	npanes = window_count_panes(w);
	if (w->sy <= PANE_MINIMUM * npanes) {
		/* How many can we fit? */
		canfit = w->sy / PANE_MINIMUM;
		if (canfit == 0) {
			/* None. Just use this size for the first. */
			TAILQ_FOREACH(wp, &w->panes, entry) {
				if (wp == TAILQ_FIRST(&w->panes))
					wp->sy = w->sy;
				else
					wp->flags |= PANE_HIDDEN;
			}
		} else {
			/* >=1, set minimum for them all. */
			TAILQ_FOREACH(wp, &w->panes, entry) {
				if (canfit-- > 0)
					wp->sy = PANE_MINIMUM - 1;
				else
					wp->flags |= PANE_HIDDEN;
			}
			/* And increase the first by the rest. */
			TAILQ_FIRST(&w->panes)->sy += 1 + w->sy % PANE_MINIMUM;
		}
	} else {
		/* In theory they will all fit. Find the current total. */
		total = 0;
		TAILQ_FOREACH(wp, &w->panes, entry)
			total += wp->sy;
		total += npanes - 1;

		/* Growing or shrinking? */
		left = w->sy - total;
		if (left > 0) {
			/* Growing. Expand evenly. */
			while (left > 0) {
				TAILQ_FOREACH(wp, &w->panes, entry) {
					wp->sy++;
					if (--left == 0)
						break;
				}
			}
		} else {
			/* Shrinking. Reduce evenly down to minimum. */
			while (left < 0) {
				TAILQ_FOREACH(wp, &w->panes, entry) {
					if (wp->sy <= PANE_MINIMUM - 1)
						continue;
					wp->sy--;
					if (++left == 0)
						break;
				}
			}
		}
	}

	/* Now do the resize. */
	TAILQ_FOREACH(wp, &w->panes, entry) {
		wp->sy--;
	    	window_pane_resize(wp, w->sx, wp->sy + 1);
	}

	/* Fill in the offsets. */
	layout_manual_v_update_offsets(w);

	/* Switch the active window if necessary. */
	window_set_active_pane(w, w->active);
}

void
layout_manual_v_resize(struct window_pane *wp, int adjust)
{
	struct window		*w = wp->window;
	struct window_pane	*wq;

	if (adjust > 0) {
		/*
		 * If this is not the last pane, keep trying to increase size
		 * and remove it from the next panes. If it is the last, do
		 * so on the previous pane.
		 */
		if (TAILQ_NEXT(wp, entry) == NULL) {
			if (wp == TAILQ_FIRST(&w->panes)) {
				/* Only one pane. */
				return;
			}
			wp = TAILQ_PREV(wp, window_panes, entry);
		}
		while (adjust-- > 0) {
			wq = wp;
			while ((wq = TAILQ_NEXT(wq, entry)) != NULL) {
				if (wq->sy <= PANE_MINIMUM)
					continue;
				window_pane_resize(wq, wq->sx, wq->sy - 1);
				break;
			}
			if (wq == NULL)
				break;
			window_pane_resize(wp, wp->sx, wp->sy + 1);
		}
	} else {
		adjust = -adjust;
		/*
		 * If this is not the last pane, keep trying to reduce size
		 * and add to the following pane. If it is the last, do so on
		 * the previous pane.
		 */
		wq = TAILQ_NEXT(wp, entry);
		if (wq == NULL) {
			if (wp == TAILQ_FIRST(&w->panes)) {
				/* Only one pane. */
				return;
			}
			wq = wp;
			wp = TAILQ_PREV(wq, window_panes, entry);
		}
		while (adjust-- > 0) {
			if (wp->sy <= PANE_MINIMUM)
				break;
			window_pane_resize(wq, wq->sx, wq->sy + 1);
			window_pane_resize(wp, wp->sx, wp->sy - 1);
		}
	}

	layout_manual_v_update_offsets(w);
}

void
layout_manual_v_update_offsets(struct window *w)
{
	struct window_pane     *wp;
	u_int			yoff;

	yoff = 0;
	TAILQ_FOREACH(wp, &w->panes, entry) {
		if (wp->flags & PANE_HIDDEN)
			continue;
		wp->xoff = 0;
		wp->yoff = yoff;
		yoff += wp->sy + 1;
	}
}