Home | History | Annotate | Line # | Download | only in dist
cmd-resize-pane.c revision 1.1.1.6
      1  1.1.1.5  christos /* $OpenBSD$ */
      2      1.1      jmmv 
      3      1.1      jmmv /*
      4  1.1.1.6  christos  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      5      1.1      jmmv  *
      6      1.1      jmmv  * Permission to use, copy, modify, and distribute this software for any
      7      1.1      jmmv  * purpose with or without fee is hereby granted, provided that the above
      8      1.1      jmmv  * copyright notice and this permission notice appear in all copies.
      9      1.1      jmmv  *
     10      1.1      jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11      1.1      jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12      1.1      jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13      1.1      jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14      1.1      jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15      1.1      jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16      1.1      jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17      1.1      jmmv  */
     18      1.1      jmmv 
     19      1.1      jmmv #include <sys/types.h>
     20      1.1      jmmv 
     21      1.1      jmmv #include <stdlib.h>
     22      1.1      jmmv 
     23      1.1      jmmv #include "tmux.h"
     24      1.1      jmmv 
     25      1.1      jmmv /*
     26      1.1      jmmv  * Increase or decrease pane size.
     27      1.1      jmmv  */
     28      1.1      jmmv 
     29  1.1.1.3  christos enum cmd_retval	 cmd_resize_pane_exec(struct cmd *, struct cmd_q *);
     30      1.1      jmmv 
     31  1.1.1.5  christos void	cmd_resize_pane_mouse_update(struct client *, struct mouse_event *);
     32  1.1.1.5  christos 
     33      1.1      jmmv const struct cmd_entry cmd_resize_pane_entry = {
     34  1.1.1.6  christos 	.name = "resize-pane",
     35  1.1.1.6  christos 	.alias = "resizep",
     36  1.1.1.6  christos 
     37  1.1.1.6  christos 	.args = { "DLMRt:Ux:y:Z", 0, 1 },
     38  1.1.1.6  christos 	.usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
     39  1.1.1.6  christos 		 "[adjustment]",
     40  1.1.1.6  christos 
     41  1.1.1.6  christos 	.tflag = CMD_PANE,
     42  1.1.1.6  christos 
     43  1.1.1.6  christos 	.flags = 0,
     44  1.1.1.6  christos 	.exec = cmd_resize_pane_exec
     45      1.1      jmmv };
     46      1.1      jmmv 
     47  1.1.1.3  christos enum cmd_retval
     48  1.1.1.3  christos cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq)
     49      1.1      jmmv {
     50  1.1.1.2      jmmv 	struct args		*args = self->args;
     51  1.1.1.6  christos 	struct window_pane	*wp = cmdq->state.tflag.wp;
     52  1.1.1.6  christos 	struct winlink		*wl = cmdq->state.tflag.wl;
     53  1.1.1.6  christos 	struct window		*w = wl->window;
     54  1.1.1.5  christos 	struct client		*c = cmdq->client;
     55  1.1.1.6  christos 	struct session		*s = cmdq->state.tflag.s;
     56      1.1      jmmv 	const char	       	*errstr;
     57  1.1.1.3  christos 	char			*cause;
     58      1.1      jmmv 	u_int			 adjust;
     59  1.1.1.3  christos 	int			 x, y;
     60      1.1      jmmv 
     61  1.1.1.5  christos 	if (args_has(args, 'M')) {
     62  1.1.1.5  christos 		if (cmd_mouse_window(&cmdq->item->mouse, &s) == NULL)
     63  1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     64  1.1.1.5  christos 		if (c == NULL || c->session != s)
     65  1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     66  1.1.1.5  christos 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
     67  1.1.1.5  christos 		cmd_resize_pane_mouse_update(c, &cmdq->item->mouse);
     68  1.1.1.5  christos 		return (CMD_RETURN_NORMAL);
     69  1.1.1.5  christos 	}
     70  1.1.1.5  christos 
     71  1.1.1.3  christos 	if (args_has(args, 'Z')) {
     72  1.1.1.3  christos 		if (w->flags & WINDOW_ZOOMED)
     73  1.1.1.3  christos 			window_unzoom(w);
     74  1.1.1.3  christos 		else
     75  1.1.1.3  christos 			window_zoom(wp);
     76  1.1.1.3  christos 		server_redraw_window(w);
     77  1.1.1.3  christos 		server_status_window(w);
     78  1.1.1.3  christos 		return (CMD_RETURN_NORMAL);
     79  1.1.1.3  christos 	}
     80  1.1.1.3  christos 	server_unzoom_window(w);
     81      1.1      jmmv 
     82  1.1.1.2      jmmv 	if (args->argc == 0)
     83      1.1      jmmv 		adjust = 1;
     84      1.1      jmmv 	else {
     85  1.1.1.2      jmmv 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
     86      1.1      jmmv 		if (errstr != NULL) {
     87  1.1.1.3  christos 			cmdq_error(cmdq, "adjustment %s", errstr);
     88  1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     89  1.1.1.3  christos 		}
     90  1.1.1.3  christos 	}
     91  1.1.1.3  christos 
     92  1.1.1.3  christos 	if (args_has(self->args, 'x')) {
     93  1.1.1.3  christos 		x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX,
     94  1.1.1.3  christos 		    &cause);
     95  1.1.1.3  christos 		if (cause != NULL) {
     96  1.1.1.3  christos 			cmdq_error(cmdq, "width %s", cause);
     97  1.1.1.3  christos 			free(cause);
     98  1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     99  1.1.1.3  christos 		}
    100  1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
    101  1.1.1.3  christos 	}
    102  1.1.1.3  christos 	if (args_has(self->args, 'y')) {
    103  1.1.1.3  christos 		y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX,
    104  1.1.1.3  christos 		    &cause);
    105  1.1.1.3  christos 		if (cause != NULL) {
    106  1.1.1.3  christos 			cmdq_error(cmdq, "height %s", cause);
    107  1.1.1.3  christos 			free(cause);
    108  1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    109      1.1      jmmv 		}
    110  1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
    111      1.1      jmmv 	}
    112      1.1      jmmv 
    113  1.1.1.2      jmmv 	if (args_has(self->args, 'L'))
    114      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust);
    115  1.1.1.2      jmmv 	else if (args_has(self->args, 'R'))
    116      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust);
    117  1.1.1.2      jmmv 	else if (args_has(self->args, 'U'))
    118      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust);
    119  1.1.1.2      jmmv 	else if (args_has(self->args, 'D'))
    120      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust);
    121      1.1      jmmv 	server_redraw_window(wl->window);
    122      1.1      jmmv 
    123  1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    124      1.1      jmmv }
    125  1.1.1.5  christos 
    126  1.1.1.5  christos void
    127  1.1.1.5  christos cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
    128  1.1.1.5  christos {
    129  1.1.1.5  christos 	struct winlink		*wl;
    130  1.1.1.5  christos 	struct window_pane	*wp;
    131  1.1.1.5  christos 	int			 found;
    132  1.1.1.5  christos 	u_int			 y, ly;
    133  1.1.1.5  christos 
    134  1.1.1.5  christos 	wl = cmd_mouse_window(m, NULL);
    135  1.1.1.5  christos 	if (wl == NULL) {
    136  1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    137  1.1.1.5  christos 		return;
    138  1.1.1.5  christos 	}
    139  1.1.1.5  christos 
    140  1.1.1.5  christos 	y = m->y;
    141  1.1.1.5  christos 	if (m->statusat == 0 && y > 0)
    142  1.1.1.5  christos 		y--;
    143  1.1.1.5  christos 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
    144  1.1.1.5  christos 		y = m->statusat - 1;
    145  1.1.1.5  christos 	ly = m->ly;
    146  1.1.1.5  christos 	if (m->statusat == 0 && ly > 0)
    147  1.1.1.5  christos 		ly--;
    148  1.1.1.5  christos 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
    149  1.1.1.5  christos 		ly = m->statusat - 1;
    150  1.1.1.5  christos 
    151  1.1.1.5  christos 	found = 0;
    152  1.1.1.5  christos 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
    153  1.1.1.5  christos 		if (!window_pane_visible(wp))
    154  1.1.1.5  christos 			continue;
    155  1.1.1.5  christos 
    156  1.1.1.5  christos 		if (wp->xoff + wp->sx == m->lx &&
    157  1.1.1.5  christos 		    wp->yoff <= 1 + ly && wp->yoff + wp->sy >= ly) {
    158  1.1.1.5  christos 			layout_resize_pane(wp, LAYOUT_LEFTRIGHT, m->x - m->lx);
    159  1.1.1.5  christos 			found = 1;
    160  1.1.1.5  christos 		}
    161  1.1.1.5  christos 		if (wp->yoff + wp->sy == ly &&
    162  1.1.1.5  christos 		    wp->xoff <= 1 + m->lx && wp->xoff + wp->sx >= m->lx) {
    163  1.1.1.5  christos 			layout_resize_pane(wp, LAYOUT_TOPBOTTOM, y - ly);
    164  1.1.1.5  christos 			found = 1;
    165  1.1.1.5  christos 		}
    166  1.1.1.5  christos 	}
    167  1.1.1.5  christos 	if (found)
    168  1.1.1.5  christos 		server_redraw_window(wl->window);
    169  1.1.1.5  christos 	else
    170  1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    171  1.1.1.5  christos }
    172