Home | History | Annotate | Line # | Download | only in dist
cmd-resize-pane.c revision 1.1.1.11
      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.7  christos static enum cmd_retval	cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
     30       1.1      jmmv 
     31   1.1.1.7  christos static void	cmd_resize_pane_mouse_update(struct client *,
     32   1.1.1.7  christos 		    struct mouse_event *);
     33   1.1.1.5  christos 
     34       1.1      jmmv const struct cmd_entry cmd_resize_pane_entry = {
     35   1.1.1.6  christos 	.name = "resize-pane",
     36   1.1.1.6  christos 	.alias = "resizep",
     37   1.1.1.6  christos 
     38   1.1.1.6  christos 	.args = { "DLMRt:Ux:y:Z", 0, 1 },
     39   1.1.1.6  christos 	.usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
     40   1.1.1.6  christos 		 "[adjustment]",
     41   1.1.1.6  christos 
     42   1.1.1.8  christos 	.target = { 't', CMD_FIND_PANE, 0 },
     43   1.1.1.6  christos 
     44   1.1.1.7  christos 	.flags = CMD_AFTERHOOK,
     45   1.1.1.6  christos 	.exec = cmd_resize_pane_exec
     46       1.1      jmmv };
     47       1.1      jmmv 
     48   1.1.1.7  christos static enum cmd_retval
     49   1.1.1.7  christos cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
     50       1.1      jmmv {
     51   1.1.1.2      jmmv 	struct args		*args = self->args;
     52   1.1.1.8  christos 	struct cmdq_shared	*shared = item->shared;
     53   1.1.1.8  christos 	struct window_pane	*wp = item->target.wp;
     54   1.1.1.8  christos 	struct winlink		*wl = item->target.wl;
     55   1.1.1.6  christos 	struct window		*w = wl->window;
     56   1.1.1.7  christos 	struct client		*c = item->client;
     57   1.1.1.8  christos 	struct session		*s = item->target.s;
     58       1.1      jmmv 	const char	       	*errstr;
     59   1.1.1.3  christos 	char			*cause;
     60       1.1      jmmv 	u_int			 adjust;
     61   1.1.1.3  christos 	int			 x, y;
     62       1.1      jmmv 
     63   1.1.1.5  christos 	if (args_has(args, 'M')) {
     64   1.1.1.8  christos 		if (cmd_mouse_window(&shared->mouse, &s) == NULL)
     65   1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     66   1.1.1.5  christos 		if (c == NULL || c->session != s)
     67   1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     68   1.1.1.5  christos 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
     69   1.1.1.8  christos 		cmd_resize_pane_mouse_update(c, &shared->mouse);
     70   1.1.1.5  christos 		return (CMD_RETURN_NORMAL);
     71   1.1.1.5  christos 	}
     72   1.1.1.5  christos 
     73   1.1.1.3  christos 	if (args_has(args, 'Z')) {
     74   1.1.1.3  christos 		if (w->flags & WINDOW_ZOOMED)
     75   1.1.1.3  christos 			window_unzoom(w);
     76   1.1.1.3  christos 		else
     77   1.1.1.3  christos 			window_zoom(wp);
     78   1.1.1.3  christos 		server_redraw_window(w);
     79   1.1.1.3  christos 		server_status_window(w);
     80   1.1.1.3  christos 		return (CMD_RETURN_NORMAL);
     81   1.1.1.3  christos 	}
     82   1.1.1.3  christos 	server_unzoom_window(w);
     83       1.1      jmmv 
     84   1.1.1.2      jmmv 	if (args->argc == 0)
     85       1.1      jmmv 		adjust = 1;
     86       1.1      jmmv 	else {
     87   1.1.1.2      jmmv 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
     88       1.1      jmmv 		if (errstr != NULL) {
     89   1.1.1.7  christos 			cmdq_error(item, "adjustment %s", errstr);
     90   1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     91   1.1.1.3  christos 		}
     92   1.1.1.3  christos 	}
     93   1.1.1.3  christos 
     94  1.1.1.10  christos 	if (args_has(args, 'x')) {
     95  1.1.1.10  christos 		x = args_strtonum(args, 'x', PANE_MINIMUM, INT_MAX, &cause);
     96   1.1.1.3  christos 		if (cause != NULL) {
     97   1.1.1.7  christos 			cmdq_error(item, "width %s", cause);
     98   1.1.1.3  christos 			free(cause);
     99   1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    100   1.1.1.3  christos 		}
    101   1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
    102   1.1.1.3  christos 	}
    103  1.1.1.10  christos 	if (args_has(args, 'y')) {
    104  1.1.1.10  christos 		y = args_strtonum(args, 'y', PANE_MINIMUM, INT_MAX, &cause);
    105   1.1.1.3  christos 		if (cause != NULL) {
    106   1.1.1.7  christos 			cmdq_error(item, "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.10  christos 	if (args_has(args, 'L'))
    114   1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
    115  1.1.1.10  christos 	else if (args_has(args, 'R'))
    116   1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
    117  1.1.1.10  christos 	else if (args_has(args, 'U'))
    118   1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
    119  1.1.1.10  christos 	else if (args_has(args, 'D'))
    120   1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
    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.7  christos static 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.9  christos 	struct window		*w;
    131   1.1.1.9  christos 	u_int			 y, ly, x, lx;
    132   1.1.1.9  christos 	static const int         offsets[][2] = {
    133   1.1.1.9  christos 	    { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
    134   1.1.1.9  christos 	};
    135   1.1.1.9  christos 	struct layout_cell	*cells[nitems(offsets)], *lc;
    136   1.1.1.9  christos 	u_int			 ncells = 0, i, j, resizes = 0;
    137   1.1.1.9  christos 	enum layout_type	 type;
    138   1.1.1.5  christos 
    139   1.1.1.5  christos 	wl = cmd_mouse_window(m, NULL);
    140   1.1.1.5  christos 	if (wl == NULL) {
    141   1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    142   1.1.1.5  christos 		return;
    143   1.1.1.5  christos 	}
    144   1.1.1.9  christos 	w = wl->window;
    145   1.1.1.5  christos 
    146  1.1.1.11  christos 	y = m->y + m->oy; x = m->x + m->ox;
    147  1.1.1.11  christos 	if (m->statusat == 0 && y >= m->statuslines)
    148  1.1.1.11  christos 		y -= m->statuslines;
    149   1.1.1.5  christos 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
    150   1.1.1.5  christos 		y = m->statusat - 1;
    151  1.1.1.11  christos 	ly = m->ly + m->oy; lx = m->lx + m->ox;
    152  1.1.1.11  christos 	if (m->statusat == 0 && ly >= m->statuslines)
    153  1.1.1.11  christos 		ly -= m->statuslines;
    154   1.1.1.5  christos 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
    155   1.1.1.5  christos 		ly = m->statusat - 1;
    156   1.1.1.5  christos 
    157   1.1.1.9  christos 	for (i = 0; i < nitems(cells); i++) {
    158   1.1.1.9  christos 		lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
    159   1.1.1.9  christos 		    ly + offsets[i][1]);
    160   1.1.1.9  christos 		if (lc == NULL)
    161   1.1.1.5  christos 			continue;
    162   1.1.1.5  christos 
    163   1.1.1.9  christos 		for (j = 0; j < ncells; j++) {
    164   1.1.1.9  christos 			if (cells[j] == lc) {
    165   1.1.1.9  christos 				lc = NULL;
    166   1.1.1.9  christos 				break;
    167   1.1.1.9  christos 			}
    168   1.1.1.9  christos 		}
    169   1.1.1.9  christos 		if (lc == NULL)
    170   1.1.1.9  christos 			continue;
    171   1.1.1.9  christos 
    172   1.1.1.9  christos 		cells[ncells] = lc;
    173   1.1.1.9  christos 		ncells++;
    174   1.1.1.5  christos 	}
    175   1.1.1.9  christos 	if (ncells == 0)
    176   1.1.1.8  christos 		return;
    177   1.1.1.9  christos 
    178   1.1.1.9  christos 	for (i = 0; i < ncells; i++) {
    179   1.1.1.9  christos 		type = cells[i]->parent->type;
    180   1.1.1.9  christos 		if (y != ly && type == LAYOUT_TOPBOTTOM) {
    181   1.1.1.9  christos 			layout_resize_layout(w, cells[i], type, y - ly, 0);
    182   1.1.1.9  christos 			resizes++;
    183   1.1.1.9  christos 		} else if (x != lx && type == LAYOUT_LEFTRIGHT) {
    184   1.1.1.9  christos 			layout_resize_layout(w, cells[i], type, x - lx, 0);
    185   1.1.1.9  christos 			resizes++;
    186   1.1.1.9  christos 		}
    187   1.1.1.8  christos 	}
    188   1.1.1.9  christos 	if (resizes != 0)
    189   1.1.1.9  christos 		server_redraw_window(w);
    190   1.1.1.5  christos }
    191