[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.40

1.40    ! nicm        1: /* $OpenBSD: cmd-resize-pane.c,v 1.39 2019/10/15 08:25:37 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:
                     39:        .args = { "DLMRt:Ux:y:Z", 0, 1 },
                     40:        .usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
                     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.9       nicm       52:        struct args             *args = self->args;
1.29      nicm       53:        struct cmdq_shared      *shared = item->shared;
1.30      nicm       54:        struct window_pane      *wp = item->target.wp;
                     55:        struct winlink          *wl = item->target.wl;
1.19      nicm       56:        struct window           *w = wl->window;
1.28      nicm       57:        struct client           *c = item->client;
1.30      nicm       58:        struct session          *s = item->target.s;
1.40    ! nicm       59:        const char              *errstr;
        !            60:        char                    *cause;
1.1       nicm       61:        u_int                    adjust;
1.40    ! nicm       62:        int                      x, y;
1.1       nicm       63:
1.18      nicm       64:        if (args_has(args, 'M')) {
1.29      nicm       65:                if (cmd_mouse_window(&shared->mouse, &s) == NULL)
1.18      nicm       66:                        return (CMD_RETURN_NORMAL);
                     67:                if (c == NULL || c->session != s)
                     68:                        return (CMD_RETURN_NORMAL);
                     69:                c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
1.29      nicm       70:                cmd_resize_pane_mouse_update(c, &shared->mouse);
1.18      nicm       71:                return (CMD_RETURN_NORMAL);
                     72:        }
                     73:
1.15      nicm       74:        if (args_has(args, 'Z')) {
                     75:                if (w->flags & WINDOW_ZOOMED)
                     76:                        window_unzoom(w);
                     77:                else
                     78:                        window_zoom(wp);
                     79:                server_redraw_window(w);
                     80:                server_status_window(w);
                     81:                return (CMD_RETURN_NORMAL);
                     82:        }
                     83:        server_unzoom_window(w);
1.1       nicm       84:
1.9       nicm       85:        if (args->argc == 0)
1.1       nicm       86:                adjust = 1;
                     87:        else {
1.9       nicm       88:                adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
1.1       nicm       89:                if (errstr != NULL) {
1.28      nicm       90:                        cmdq_error(item, "adjustment %s", errstr);
1.11      nicm       91:                        return (CMD_RETURN_ERROR);
1.1       nicm       92:                }
1.13      nicm       93:        }
                     94:
1.40    ! nicm       95:        if (args_has(args, 'x')) {
        !            96:                x = args_percentage(args, 'x', 0, INT_MAX, w->sx, &cause);
        !            97:                if (cause != NULL) {
        !            98:                        cmdq_error(item, "width %s", cause);
        !            99:                        free(cause);
        !           100:                        return (CMD_RETURN_ERROR);
1.13      nicm      101:                }
                    102:                layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
                    103:        }
1.40    ! nicm      104:        if (args_has(args, 'y')) {
        !           105:                y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
        !           106:                if (cause != NULL) {
        !           107:                        cmdq_error(item, "width %s", cause);
        !           108:                        free(cause);
        !           109:                        return (CMD_RETURN_ERROR);
1.13      nicm      110:                }
                    111:                layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
1.1       nicm      112:        }
                    113:
1.35      nicm      114:        if (args_has(args, 'L'))
1.24      nicm      115:                layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
1.35      nicm      116:        else if (args_has(args, 'R'))
1.24      nicm      117:                layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
1.35      nicm      118:        else if (args_has(args, 'U'))
1.24      nicm      119:                layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
1.35      nicm      120:        else if (args_has(args, 'D'))
1.24      nicm      121:                layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
1.1       nicm      122:        server_redraw_window(wl->window);
                    123:
1.11      nicm      124:        return (CMD_RETURN_NORMAL);
1.18      nicm      125: }
                    126:
1.25      nicm      127: static void
1.18      nicm      128: cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
                    129: {
                    130:        struct winlink          *wl;
1.32      nicm      131:        struct window           *w;
                    132:        u_int                    y, ly, x, lx;
1.34      nicm      133:        static const int         offsets[][2] = {
                    134:            { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
                    135:        };
                    136:        struct layout_cell      *cells[nitems(offsets)], *lc;
1.33      nicm      137:        u_int                    ncells = 0, i, j, resizes = 0;
                    138:        enum layout_type         type;
1.18      nicm      139:
                    140:        wl = cmd_mouse_window(m, NULL);
                    141:        if (wl == NULL) {
                    142:                c->tty.mouse_drag_update = NULL;
                    143:                return;
                    144:        }
1.32      nicm      145:        w = wl->window;
1.18      nicm      146:
1.37      nicm      147:        y = m->y + m->oy; x = m->x + m->ox;
1.38      nicm      148:        if (m->statusat == 0 && y >= m->statuslines)
                    149:                y -= m->statuslines;
1.18      nicm      150:        else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    151:                y = m->statusat - 1;
1.36      nicm      152:        ly = m->ly + m->oy; lx = m->lx + m->ox;
1.38      nicm      153:        if (m->statusat == 0 && ly >= m->statuslines)
                    154:                ly -= m->statuslines;
1.18      nicm      155:        else if (m->statusat > 0 && ly >= (u_int)m->statusat)
                    156:                ly = m->statusat - 1;
                    157:
1.33      nicm      158:        for (i = 0; i < nitems(cells); i++) {
                    159:                lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
                    160:                    ly + offsets[i][1]);
                    161:                if (lc == NULL)
                    162:                        continue;
                    163:
                    164:                for (j = 0; j < ncells; j++) {
                    165:                        if (cells[j] == lc) {
                    166:                                lc = NULL;
                    167:                                break;
                    168:                        }
                    169:                }
                    170:                if (lc == NULL)
                    171:                        continue;
1.32      nicm      172:
1.33      nicm      173:                cells[ncells] = lc;
                    174:                ncells++;
                    175:        }
                    176:        if (ncells == 0)
1.31      nicm      177:                return;
1.32      nicm      178:
1.33      nicm      179:        for (i = 0; i < ncells; i++) {
                    180:                type = cells[i]->parent->type;
                    181:                if (y != ly && type == LAYOUT_TOPBOTTOM) {
                    182:                        layout_resize_layout(w, cells[i], type, y - ly, 0);
                    183:                        resizes++;
                    184:                } else if (x != lx && type == LAYOUT_LEFTRIGHT) {
                    185:                        layout_resize_layout(w, cells[i], type, x - lx, 0);
                    186:                        resizes++;
                    187:                }
                    188:        }
                    189:        if (resizes != 0)
                    190:                server_redraw_window(w);
1.1       nicm      191: }