Home | History | Annotate | Line # | Download | only in dist
cmd-resize-pane.c revision 1.1.1.5
      1  1.1.1.5  christos /* $OpenBSD$ */
      2      1.1      jmmv 
      3      1.1      jmmv /*
      4      1.1      jmmv  * Copyright (c) 2009 Nicholas Marriott <nicm (at) users.sourceforge.net>
      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      jmmv 	"resize-pane", "resizep",
     35  1.1.1.5  christos 	"DLMRt:Ux:y:Z", 0, 1,
     36  1.1.1.5  christos 	"[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE
     37  1.1.1.5  christos 	" [adjustment]",
     38  1.1.1.2      jmmv 	0,
     39  1.1.1.2      jmmv 	cmd_resize_pane_exec
     40      1.1      jmmv };
     41      1.1      jmmv 
     42  1.1.1.3  christos enum cmd_retval
     43  1.1.1.3  christos cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq)
     44      1.1      jmmv {
     45  1.1.1.2      jmmv 	struct args		*args = self->args;
     46  1.1.1.5  christos 	struct client		*c = cmdq->client;
     47  1.1.1.5  christos 	struct session		*s;
     48      1.1      jmmv 	struct winlink		*wl;
     49  1.1.1.3  christos 	struct window		*w;
     50      1.1      jmmv 	const char	       	*errstr;
     51  1.1.1.3  christos 	char			*cause;
     52      1.1      jmmv 	struct window_pane	*wp;
     53      1.1      jmmv 	u_int			 adjust;
     54  1.1.1.3  christos 	int			 x, y;
     55      1.1      jmmv 
     56  1.1.1.5  christos 	if (args_has(args, 'M')) {
     57  1.1.1.5  christos 		if (cmd_mouse_window(&cmdq->item->mouse, &s) == NULL)
     58  1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     59  1.1.1.5  christos 		if (c == NULL || c->session != s)
     60  1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     61  1.1.1.5  christos 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
     62  1.1.1.5  christos 		cmd_resize_pane_mouse_update(c, &cmdq->item->mouse);
     63  1.1.1.5  christos 		return (CMD_RETURN_NORMAL);
     64  1.1.1.5  christos 	}
     65  1.1.1.5  christos 
     66  1.1.1.3  christos 	if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL)
     67  1.1.1.3  christos 		return (CMD_RETURN_ERROR);
     68  1.1.1.3  christos 	w = wl->window;
     69  1.1.1.3  christos 
     70  1.1.1.3  christos 	if (args_has(args, 'Z')) {
     71  1.1.1.3  christos 		if (w->flags & WINDOW_ZOOMED)
     72  1.1.1.3  christos 			window_unzoom(w);
     73  1.1.1.3  christos 		else
     74  1.1.1.3  christos 			window_zoom(wp);
     75  1.1.1.3  christos 		server_redraw_window(w);
     76  1.1.1.3  christos 		server_status_window(w);
     77  1.1.1.3  christos 		return (CMD_RETURN_NORMAL);
     78  1.1.1.3  christos 	}
     79  1.1.1.3  christos 	server_unzoom_window(w);
     80      1.1      jmmv 
     81  1.1.1.2      jmmv 	if (args->argc == 0)
     82      1.1      jmmv 		adjust = 1;
     83      1.1      jmmv 	else {
     84  1.1.1.2      jmmv 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
     85      1.1      jmmv 		if (errstr != NULL) {
     86  1.1.1.3  christos 			cmdq_error(cmdq, "adjustment %s", errstr);
     87  1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     88  1.1.1.3  christos 		}
     89  1.1.1.3  christos 	}
     90  1.1.1.3  christos 
     91  1.1.1.3  christos 	if (args_has(self->args, 'x')) {
     92  1.1.1.3  christos 		x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX,
     93  1.1.1.3  christos 		    &cause);
     94  1.1.1.3  christos 		if (cause != NULL) {
     95  1.1.1.3  christos 			cmdq_error(cmdq, "width %s", cause);
     96  1.1.1.3  christos 			free(cause);
     97  1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     98  1.1.1.3  christos 		}
     99  1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
    100  1.1.1.3  christos 	}
    101  1.1.1.3  christos 	if (args_has(self->args, 'y')) {
    102  1.1.1.3  christos 		y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX,
    103  1.1.1.3  christos 		    &cause);
    104  1.1.1.3  christos 		if (cause != NULL) {
    105  1.1.1.3  christos 			cmdq_error(cmdq, "height %s", cause);
    106  1.1.1.3  christos 			free(cause);
    107  1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    108      1.1      jmmv 		}
    109  1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
    110      1.1      jmmv 	}
    111      1.1      jmmv 
    112  1.1.1.2      jmmv 	if (args_has(self->args, 'L'))
    113      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust);
    114  1.1.1.2      jmmv 	else if (args_has(self->args, 'R'))
    115      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust);
    116  1.1.1.2      jmmv 	else if (args_has(self->args, 'U'))
    117      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust);
    118  1.1.1.2      jmmv 	else if (args_has(self->args, 'D'))
    119      1.1      jmmv 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust);
    120      1.1      jmmv 	server_redraw_window(wl->window);
    121      1.1      jmmv 
    122  1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    123      1.1      jmmv }
    124  1.1.1.5  christos 
    125  1.1.1.5  christos void
    126  1.1.1.5  christos cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
    127  1.1.1.5  christos {
    128  1.1.1.5  christos 	struct winlink		*wl;
    129  1.1.1.5  christos 	struct window_pane	*wp;
    130  1.1.1.5  christos 	int			 found;
    131  1.1.1.5  christos 	u_int			 y, ly;
    132  1.1.1.5  christos 
    133  1.1.1.5  christos 	wl = cmd_mouse_window(m, NULL);
    134  1.1.1.5  christos 	if (wl == NULL) {
    135  1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    136  1.1.1.5  christos 		return;
    137  1.1.1.5  christos 	}
    138  1.1.1.5  christos 
    139  1.1.1.5  christos 	y = m->y;
    140  1.1.1.5  christos 	if (m->statusat == 0 && y > 0)
    141  1.1.1.5  christos 		y--;
    142  1.1.1.5  christos 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
    143  1.1.1.5  christos 		y = m->statusat - 1;
    144  1.1.1.5  christos 	ly = m->ly;
    145  1.1.1.5  christos 	if (m->statusat == 0 && ly > 0)
    146  1.1.1.5  christos 		ly--;
    147  1.1.1.5  christos 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
    148  1.1.1.5  christos 		ly = m->statusat - 1;
    149  1.1.1.5  christos 
    150  1.1.1.5  christos 	found = 0;
    151  1.1.1.5  christos 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
    152  1.1.1.5  christos 		if (!window_pane_visible(wp))
    153  1.1.1.5  christos 			continue;
    154  1.1.1.5  christos 
    155  1.1.1.5  christos 		if (wp->xoff + wp->sx == m->lx &&
    156  1.1.1.5  christos 		    wp->yoff <= 1 + ly && wp->yoff + wp->sy >= ly) {
    157  1.1.1.5  christos 			layout_resize_pane(wp, LAYOUT_LEFTRIGHT, m->x - m->lx);
    158  1.1.1.5  christos 			found = 1;
    159  1.1.1.5  christos 		}
    160  1.1.1.5  christos 		if (wp->yoff + wp->sy == ly &&
    161  1.1.1.5  christos 		    wp->xoff <= 1 + m->lx && wp->xoff + wp->sx >= m->lx) {
    162  1.1.1.5  christos 			layout_resize_pane(wp, LAYOUT_TOPBOTTOM, y - ly);
    163  1.1.1.5  christos 			found = 1;
    164  1.1.1.5  christos 		}
    165  1.1.1.5  christos 	}
    166  1.1.1.5  christos 	if (found)
    167  1.1.1.5  christos 		server_redraw_window(wl->window);
    168  1.1.1.5  christos 	else
    169  1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    170  1.1.1.5  christos }
    171