2009-05-18 21:55:53 +00:00
|
|
|
/* $Id: layout.c,v 1.12 2009-05-18 21:55:53 nicm Exp $ */
|
2009-04-01 18:21:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2009-05-16 11:48:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-04-01 21:10:08 +00:00
|
|
|
/*
|
2009-05-18 21:01:38 +00:00
|
|
|
* Each layout has two functions, _refresh to relayout the panes and _resize to
|
|
|
|
* resize a single pane.
|
|
|
|
*
|
|
|
|
* Second argument (int) to _refresh is 1 if the only change has been that the
|
|
|
|
* active pane has changed. If 0 then panes, active pane or both may have
|
|
|
|
* changed.
|
2009-04-01 21:10:08 +00:00
|
|
|
*/
|
2009-05-18 21:01:38 +00:00
|
|
|
|
|
|
|
void layout_active_only_refresh(struct window *, int);
|
2009-05-18 21:29:11 +00:00
|
|
|
void layout_even_h_refresh(struct window *, int);
|
|
|
|
void layout_even_v_refresh(struct window *, int);
|
|
|
|
void layout_left_v_refresh(struct window *, int);
|
2009-04-01 18:21:42 +00:00
|
|
|
|
|
|
|
const struct {
|
2009-05-18 21:01:38 +00:00
|
|
|
const char *name;
|
|
|
|
void (*refresh)(struct window *, int);
|
|
|
|
void (*resize)(struct window_pane *, int);
|
2009-04-01 18:21:42 +00:00
|
|
|
} layouts[] = {
|
2009-05-18 21:29:11 +00:00
|
|
|
{ "manual-vertical", layout_manual_v_refresh, layout_manual_v_resize },
|
2009-05-18 21:01:38 +00:00
|
|
|
{ "active-only", layout_active_only_refresh, NULL },
|
2009-05-18 21:29:11 +00:00
|
|
|
{ "even-horizontal", layout_even_h_refresh, NULL },
|
|
|
|
{ "even-vertical", layout_even_v_refresh, NULL },
|
|
|
|
{ "left-vertical", layout_left_v_refresh, NULL },
|
2009-05-04 17:58:27 +00:00
|
|
|
};
|
2009-04-01 18:21:42 +00:00
|
|
|
|
2009-04-30 16:27:29 +00:00
|
|
|
const char *
|
|
|
|
layout_name(struct window *w)
|
|
|
|
{
|
|
|
|
return (layouts[w->layout].name);
|
2009-05-16 11:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
layout_lookup(const char *name)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
int matched = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(layouts); i++) {
|
|
|
|
if (strncmp(layouts[i].name, name, strlen(name)) == 0) {
|
|
|
|
if (matched != -1) /* ambiguous */
|
|
|
|
return (-1);
|
|
|
|
matched = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (matched);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
layout_select(struct window *w, u_int layout)
|
|
|
|
{
|
|
|
|
if (layout > nitems(layouts) - 1 || layout == w->layout)
|
|
|
|
return (-1);
|
|
|
|
w->layout = layout;
|
|
|
|
|
|
|
|
layout_refresh(w, 0);
|
|
|
|
return (0);
|
2009-04-30 16:27:29 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
void
|
|
|
|
layout_next(struct window *w)
|
|
|
|
{
|
|
|
|
w->layout++;
|
2009-05-18 21:01:38 +00:00
|
|
|
if (w->layout > nitems(layouts) - 1)
|
2009-04-01 18:21:42 +00:00
|
|
|
w->layout = 0;
|
2009-04-01 21:10:08 +00:00
|
|
|
layout_refresh(w, 0);
|
2009-04-01 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2009-04-30 21:17:06 +00:00
|
|
|
void
|
|
|
|
layout_previous(struct window *w)
|
|
|
|
{
|
|
|
|
if (w->layout == 0)
|
|
|
|
w->layout = nitems(layouts) - 1;
|
|
|
|
else
|
|
|
|
w->layout--;
|
|
|
|
layout_refresh(w, 0);
|
|
|
|
}
|
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
void
|
2009-05-18 21:01:38 +00:00
|
|
|
layout_refresh(struct window *w, int active_only)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
2009-05-18 21:01:38 +00:00
|
|
|
layouts[w->layout].refresh(w, active_only);
|
2009-04-01 18:21:42 +00:00
|
|
|
server_redraw_window(w);
|
|
|
|
}
|
|
|
|
|
2009-05-18 21:16:09 +00:00
|
|
|
int
|
2009-05-18 21:01:38 +00:00
|
|
|
layout_resize(struct window_pane *wp, int adjust)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
2009-05-18 21:16:09 +00:00
|
|
|
struct window *w = wp->window;
|
|
|
|
|
|
|
|
if (layouts[w->layout].resize == NULL)
|
|
|
|
return (-1);
|
|
|
|
layouts[w->layout].resize(wp, adjust);
|
|
|
|
return (0);
|
2009-04-01 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-18 21:01:38 +00:00
|
|
|
layout_active_only_refresh(struct window *w, unused int active_only)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
2009-05-04 17:58:27 +00:00
|
|
|
if (wp == w->active) {
|
2009-04-01 18:21:42 +00:00
|
|
|
wp->flags &= ~PANE_HIDDEN;
|
|
|
|
wp->xoff = wp->yoff = 0;
|
|
|
|
window_pane_resize(wp, w->sx, w->sy);
|
|
|
|
} else
|
|
|
|
wp->flags |= PANE_HIDDEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-18 21:29:11 +00:00
|
|
|
layout_even_h_refresh(struct window *w, int active_only)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int i, n, width, xoff;
|
|
|
|
|
2009-05-18 21:01:38 +00:00
|
|
|
if (active_only)
|
2009-04-01 21:10:08 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-01 18:48:09 +00:00
|
|
|
/* Get number of panes. */
|
2009-04-01 18:21:42 +00:00
|
|
|
n = window_count_panes(w);
|
2009-04-01 18:48:09 +00:00
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* How many can we fit? */
|
2009-04-01 18:21:42 +00:00
|
|
|
if (w->sx / n < PANE_MINIMUM) {
|
|
|
|
width = PANE_MINIMUM;
|
|
|
|
n = w->sx / PANE_MINIMUM;
|
|
|
|
} else
|
|
|
|
width = w->sx / n;
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
/* Fit the panes. */
|
|
|
|
i = xoff = 0;
|
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (i > n) {
|
|
|
|
wp->flags |= PANE_HIDDEN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wp->flags &= ~PANE_HIDDEN;
|
|
|
|
|
|
|
|
wp->xoff = xoff;
|
|
|
|
wp->yoff = 0;
|
|
|
|
if (i != n - 1)
|
|
|
|
window_pane_resize(wp, width - 1, w->sy);
|
|
|
|
else
|
|
|
|
window_pane_resize(wp, width, w->sy);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
xoff += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any space left? */
|
|
|
|
while (xoff++ < w->sx) {
|
|
|
|
wp = TAILQ_LAST(&w->panes, window_panes);
|
|
|
|
window_pane_resize(wp, wp->sx + 1, wp->sy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-18 21:29:11 +00:00
|
|
|
layout_even_v_refresh(struct window *w, int active_only)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int i, n, height, yoff;
|
|
|
|
|
2009-05-18 21:01:38 +00:00
|
|
|
if (active_only)
|
2009-04-01 21:10:08 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-01 18:48:09 +00:00
|
|
|
/* Get number of panes. */
|
2009-04-01 18:21:42 +00:00
|
|
|
n = window_count_panes(w);
|
2009-04-01 18:48:09 +00:00
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* How many can we fit? */
|
2009-04-01 18:21:42 +00:00
|
|
|
if (w->sy / n < PANE_MINIMUM) {
|
|
|
|
height = PANE_MINIMUM;
|
|
|
|
n = w->sy / PANE_MINIMUM;
|
|
|
|
} else
|
|
|
|
height = w->sy / n;
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
/* Fit the panes. */
|
|
|
|
i = yoff = 0;
|
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (i > n) {
|
|
|
|
wp->flags |= PANE_HIDDEN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wp->flags &= ~PANE_HIDDEN;
|
|
|
|
|
|
|
|
wp->xoff = 0;
|
|
|
|
wp->yoff = yoff;
|
|
|
|
if (i != n - 1)
|
|
|
|
window_pane_resize(wp, w->sx, height - 1);
|
|
|
|
else
|
|
|
|
window_pane_resize(wp, w->sx, height);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
yoff += height;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any space left? */
|
|
|
|
while (yoff++ < w->sy) {
|
|
|
|
wp = TAILQ_LAST(&w->panes, window_panes);
|
|
|
|
window_pane_resize(wp, wp->sx, wp->sy + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-18 21:29:11 +00:00
|
|
|
layout_left_v_refresh(struct window *w, int active_only)
|
2009-04-01 18:21:42 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
2009-05-18 21:55:53 +00:00
|
|
|
u_int i, n, mainwidth, height, yoff;
|
2009-04-01 18:21:42 +00:00
|
|
|
|
2009-05-18 21:01:38 +00:00
|
|
|
if (active_only)
|
2009-04-01 21:10:08 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-01 18:48:09 +00:00
|
|
|
/* Get number of panes. */
|
|
|
|
n = window_count_panes(w);
|
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
|
2009-05-18 21:55:53 +00:00
|
|
|
/* Get the main pane width and add one for separator line. */
|
|
|
|
mainwidth = options_get_number(&w->options, "main-pane-width") + 1;
|
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
/* Need >1 pane and minimum columns; if fewer, display active only. */
|
2009-05-18 21:55:53 +00:00
|
|
|
if (n == 1 || w->sx < mainwidth + PANE_MINIMUM) {
|
2009-05-18 21:01:38 +00:00
|
|
|
layout_active_only_refresh(w, active_only);
|
2009-04-01 18:21:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-04-01 18:48:09 +00:00
|
|
|
n--;
|
2009-04-01 18:21:42 +00:00
|
|
|
|
|
|
|
/* How many can we fit, not including first? */
|
|
|
|
if (w->sy / n < PANE_MINIMUM) {
|
|
|
|
height = PANE_MINIMUM;
|
|
|
|
n = w->sy / PANE_MINIMUM;
|
|
|
|
} else
|
|
|
|
height = w->sy / n;
|
2009-05-04 17:58:27 +00:00
|
|
|
|
2009-04-01 18:21:42 +00:00
|
|
|
/* Fit the panes. */
|
|
|
|
i = yoff = 0;
|
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp == TAILQ_FIRST(&w->panes)) {
|
|
|
|
wp->xoff = 0;
|
|
|
|
wp->yoff = 0;
|
2009-05-18 21:55:53 +00:00
|
|
|
window_pane_resize(wp, mainwidth - 1, w->sy);
|
2009-04-01 18:21:42 +00:00
|
|
|
wp->flags &= ~PANE_HIDDEN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > n) {
|
|
|
|
wp->flags |= PANE_HIDDEN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wp->flags &= ~PANE_HIDDEN;
|
|
|
|
|
2009-05-18 21:55:53 +00:00
|
|
|
wp->xoff = mainwidth;
|
2009-04-01 18:21:42 +00:00
|
|
|
wp->yoff = yoff;
|
|
|
|
if (i != n - 1)
|
2009-05-18 21:55:53 +00:00
|
|
|
window_pane_resize(wp, w->sx - mainwidth, height - 1);
|
2009-04-01 18:21:42 +00:00
|
|
|
else
|
2009-05-18 21:55:53 +00:00
|
|
|
window_pane_resize(wp, w->sx - mainwidth, height);
|
2009-04-01 18:21:42 +00:00
|
|
|
|
|
|
|
i++;
|
|
|
|
yoff += height;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any space left? */
|
|
|
|
while (yoff++ < w->sy) {
|
|
|
|
wp = TAILQ_LAST(&w->panes, window_panes);
|
|
|
|
while (wp != NULL && wp == TAILQ_FIRST(&w->panes))
|
|
|
|
wp = TAILQ_PREV(wp, window_panes, entry);
|
|
|
|
if (wp == NULL)
|
|
|
|
break;
|
|
|
|
window_pane_resize(wp, wp->sx, wp->sy + 1);
|
|
|
|
}
|
|
|
|
}
|