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