[BACK]Return to cmd-resize-pane.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-resize-pane.c, Revision 1.45

1.45    ! nicm        1: /* $OpenBSD: cmd-resize-pane.c,v 1.44 2020/04/13 13:42:35 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.22      nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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>
1.39      nicm       22: #include <string.h>
1.1       nicm       23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Increase or decrease pane size.
                     28:  */
                     29:
1.28      nicm       30: static enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       31:
1.25      nicm       32: static void    cmd_resize_pane_mouse_update(struct client *,
                     33:                    struct mouse_event *);
1.18      nicm       34:
1.1       nicm       35: const struct cmd_entry cmd_resize_pane_entry = {
1.20      nicm       36:        .name = "resize-pane",
                     37:        .alias = "resizep",
                     38:
1.41      nicm       39:        .args = { "DLMRTt:Ux:y:Z", 0, 1 },
                     40:        .usage = "[-DLMRTUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
1.20      nicm       41:                 "[adjustment]",
                     42:
1.30      nicm       43:        .target = { 't', CMD_FIND_PANE, 0 },
1.21      nicm       44:
1.26      nicm       45:        .flags = CMD_AFTERHOOK,
1.20      nicm       46:        .exec = cmd_resize_pane_exec
1.1       nicm       47: };
                     48:
1.25      nicm       49: static enum cmd_retval
1.28      nicm       50: cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       51: {
1.42      nicm       52:        struct args             *args = cmd_get_args(self);
1.45    ! nicm       53:        struct cmdq_state       *state = cmdq_get_state(item);
1.43      nicm       54:        struct cmd_find_state   *target = cmdq_get_target(item);
                     55:        struct window_pane      *wp = target->wp;
                     56:        struct winlink          *wl = target->wl;
1.19      nicm       57:        struct window           *w = wl->window;
1.43      nicm       58:        struct client           *c = cmdq_get_client(item);
                     59:        struct session          *s = target->s;
1.40      nicm       60:        const char              *errstr;
                     61:        char                    *cause;
1.1       nicm       62:        u_int                    adjust;
1.40      nicm       63:        int                      x, y;
1.41      nicm       64:        struct grid             *gd = wp->base.grid;
                     65:
                     66:        if (args_has(args, 'T')) {
                     67:                if (!TAILQ_EMPTY(&wp->modes))
                     68:                        return (CMD_RETURN_NORMAL);
                     69:                adjust = screen_size_y(&wp->base) - 1 - wp->base.cy;
                     70:                if (adjust > gd->hsize)
                     71:                        adjust = gd->hsize;
                     72:                grid_remove_history(gd, adjust);
                     73:                wp->base.cy += adjust;
                     74:                wp->flags |= PANE_REDRAW;
                     75:                return (CMD_RETURN_NORMAL);
                     76:        }
1.1       nicm       77:
1.18      nicm       78:        if (args_has(args, 'M')) {
1.45    ! nicm       79:                if (cmd_mouse_window(&state->event.m, &s) == NULL)
1.18      nicm       80:                        return (CMD_RETURN_NORMAL);
                     81:                if (c == NULL || c->session != s)
                     82:                        return (CMD_RETURN_NORMAL);
                     83:                c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
1.45    ! nicm       84:                cmd_resize_pane_mouse_update(c, &state->event.m);
1.18      nicm       85:                return (CMD_RETURN_NORMAL);
                     86:        }
                     87:
1.15      nicm       88:        if (args_has(args, 'Z')) {
                     89:                if (w->flags & WINDOW_ZOOMED)
                     90:                        window_unzoom(w);
                     91:                else
                     92:                        window_zoom(wp);
                     93:                server_redraw_window(w);
                     94:                server_status_window(w);
                     95:                return (CMD_RETURN_NORMAL);
                     96:        }
                     97:        server_unzoom_window(w);
1.1       nicm       98:
1.9       nicm       99:        if (args->argc == 0)
1.1       nicm      100:                adjust = 1;
                    101:        else {
1.9       nicm      102:                adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
1.1       nicm      103:                if (errstr != NULL) {
1.28      nicm      104:                        cmdq_error(item, "adjustment %s", errstr);
1.11      nicm      105:                        return (CMD_RETURN_ERROR);
1.1       nicm      106:                }
1.13      nicm      107:        }
                    108:
1.40      nicm      109:        if (args_has(args, 'x')) {
                    110:                x = args_percentage(args, 'x', 0, INT_MAX, w->sx, &cause);
                    111:                if (cause != NULL) {
                    112:                        cmdq_error(item, "width %s", cause);
                    113:                        free(cause);
                    114:                        return (CMD_RETURN_ERROR);
1.13      nicm      115:                }
                    116:                layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
                    117:        }
1.40      nicm      118:        if (args_has(args, 'y')) {
                    119:                y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
                    120:                if (cause != NULL) {
                    121:                        cmdq_error(item, "width %s", cause);
                    122:                        free(cause);
                    123:                        return (CMD_RETURN_ERROR);
1.13      nicm      124:                }
                    125:                layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
1.1       nicm      126:        }
                    127:
1.35      nicm      128:        if (args_has(args, 'L'))
1.24      nicm      129:                layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
1.35      nicm      130:        else if (args_has(args, 'R'))
1.24      nicm      131:                layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
1.35      nicm      132:        else if (args_has(args, 'U'))
1.24      nicm      133:                layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
1.35      nicm      134:        else if (args_has(args, 'D'))
1.24      nicm      135:                layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
1.1       nicm      136:        server_redraw_window(wl->window);
                    137:
1.11      nicm      138:        return (CMD_RETURN_NORMAL);
1.18      nicm      139: }
                    140:
1.25      nicm      141: static void
1.18      nicm      142: cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
                    143: {
                    144:        struct winlink          *wl;
1.32      nicm      145:        struct window           *w;
                    146:        u_int                    y, ly, x, lx;
1.34      nicm      147:        static const int         offsets[][2] = {
                    148:            { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
                    149:        };
                    150:        struct layout_cell      *cells[nitems(offsets)], *lc;
1.33      nicm      151:        u_int                    ncells = 0, i, j, resizes = 0;
                    152:        enum layout_type         type;
1.18      nicm      153:
                    154:        wl = cmd_mouse_window(m, NULL);
                    155:        if (wl == NULL) {
                    156:                c->tty.mouse_drag_update = NULL;
                    157:                return;
                    158:        }
1.32      nicm      159:        w = wl->window;
1.18      nicm      160:
1.37      nicm      161:        y = m->y + m->oy; x = m->x + m->ox;
1.38      nicm      162:        if (m->statusat == 0 && y >= m->statuslines)
                    163:                y -= m->statuslines;
1.18      nicm      164:        else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    165:                y = m->statusat - 1;
1.36      nicm      166:        ly = m->ly + m->oy; lx = m->lx + m->ox;
1.38      nicm      167:        if (m->statusat == 0 && ly >= m->statuslines)
                    168:                ly -= m->statuslines;
1.18      nicm      169:        else if (m->statusat > 0 && ly >= (u_int)m->statusat)
                    170:                ly = m->statusat - 1;
                    171:
1.33      nicm      172:        for (i = 0; i < nitems(cells); i++) {
                    173:                lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
                    174:                    ly + offsets[i][1]);
                    175:                if (lc == NULL)
                    176:                        continue;
                    177:
                    178:                for (j = 0; j < ncells; j++) {
                    179:                        if (cells[j] == lc) {
                    180:                                lc = NULL;
                    181:                                break;
                    182:                        }
                    183:                }
                    184:                if (lc == NULL)
                    185:                        continue;
1.32      nicm      186:
1.33      nicm      187:                cells[ncells] = lc;
                    188:                ncells++;
                    189:        }
                    190:        if (ncells == 0)
1.31      nicm      191:                return;
1.32      nicm      192:
1.33      nicm      193:        for (i = 0; i < ncells; i++) {
                    194:                type = cells[i]->parent->type;
                    195:                if (y != ly && type == LAYOUT_TOPBOTTOM) {
                    196:                        layout_resize_layout(w, cells[i], type, y - ly, 0);
                    197:                        resizes++;
                    198:                } else if (x != lx && type == LAYOUT_LEFTRIGHT) {
                    199:                        layout_resize_layout(w, cells[i], type, x - lx, 0);
                    200:                        resizes++;
                    201:                }
                    202:        }
                    203:        if (resizes != 0)
                    204:                server_redraw_window(w);
1.1       nicm      205: }