[BACK]Return to layout-set.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/layout-set.c, Revision 1.2

1.2     ! nicm        1: /* $OpenBSD: layout-set.c,v 1.1 2009/07/19 13:21:40 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@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 <string.h>
                     22:
                     23: #include "tmux.h"
                     24:
                     25: /*
                     26:  * Set window layouts - predefined methods to arrange windows. These are one-off
                     27:  * and generate a layout tree.
                     28:  */
                     29:
                     30: void   layout_set_even_h(struct window *);
                     31: void   layout_set_even_v(struct window *);
                     32: void   layout_set_main_h(struct window *);
                     33: void   layout_set_main_v(struct window *);
                     34:
                     35: const struct {
                     36:        const char      *name;
                     37:        void            (*arrange)(struct window *);
                     38: } layout_sets[] = {
                     39:        { "even-horizontal", layout_set_even_h },
                     40:        { "even-vertical", layout_set_even_v },
                     41:        { "main-horizontal", layout_set_main_h },
                     42:        { "main-vertical", layout_set_main_v },
                     43: };
                     44:
                     45: const char *
                     46: layout_set_name(u_int layout)
                     47: {
                     48:        return (layout_sets[layout].name);
                     49: }
                     50:
                     51: int
                     52: layout_set_lookup(const char *name)
                     53: {
                     54:        u_int   i;
                     55:        int     matched = -1;
                     56:
                     57:        for (i = 0; i < nitems(layout_sets); i++) {
                     58:                if (strncmp(layout_sets[i].name, name, strlen(name)) == 0) {
                     59:                        if (matched != -1)      /* ambiguous */
                     60:                                return (-1);
                     61:                        matched = i;
                     62:                }
                     63:        }
                     64:
                     65:        return (matched);
                     66: }
                     67:
                     68: u_int
                     69: layout_set_select(struct window *w, u_int layout)
                     70: {
                     71:        if (layout > nitems(layout_sets) - 1)
                     72:                layout = nitems(layout_sets) - 1;
                     73:
                     74:        if (layout_sets[layout].arrange != NULL)
                     75:                layout_sets[layout].arrange(w);
                     76:
                     77:        w->layout = layout;
                     78:        return (layout);
                     79: }
                     80:
                     81: u_int
                     82: layout_set_next(struct window *w)
                     83: {
                     84:        u_int   layout = w->layout;
                     85:
                     86:        if (layout_sets[layout].arrange != NULL)
                     87:                layout_sets[layout].arrange(w);
                     88:
                     89:        w->layout++;
                     90:        if (w->layout > nitems(layout_sets) - 1)
                     91:                w->layout = 0;
                     92:        return (layout);
                     93: }
                     94:
                     95: u_int
                     96: layout_set_previous(struct window *w)
                     97: {
                     98:        u_int   layout = w->layout;
                     99:
                    100:        if (layout_sets[layout].arrange != NULL)
                    101:                layout_sets[layout].arrange(w);
                    102:
                    103:        if (w->layout == 0)
                    104:                w->layout = nitems(layout_sets) - 1;
                    105:        else
                    106:                w->layout--;
                    107:        return (layout);
                    108: }
                    109:
                    110: void
                    111: layout_set_even_h(struct window *w)
                    112: {
                    113:        struct window_pane      *wp;
                    114:        struct layout_cell      *lc, *lcnew;
                    115:        u_int                    i, n, width, xoff;
                    116:
                    117:        layout_print_cell(w->layout_root, __func__, 1);
                    118:
                    119:        /* Get number of panes. */
                    120:        n = window_count_panes(w);
                    121:        if (n <= 1)
                    122:                return;
                    123:
                    124:        /* How many can we fit? */
1.2     ! nicm      125:        if (w->sx / n < PANE_MINIMUM + 1)
1.1       nicm      126:                width = PANE_MINIMUM + 1;
1.2     ! nicm      127:        else
1.1       nicm      128:                width = w->sx / n;
                    129:
                    130:        /* Free the old root and construct a new. */
                    131:        layout_free(w);
                    132:        lc = w->layout_root = layout_create_cell(NULL);
                    133:        layout_set_size(lc, w->sx, w->sy, 0, 0);
                    134:        layout_make_node(lc, LAYOUT_LEFTRIGHT);
                    135:
                    136:        /* Build new leaf cells. */
                    137:        i = xoff = 0;
                    138:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    139:                /* Create child cell. */
                    140:                lcnew = layout_create_cell(lc);
                    141:                layout_set_size(lcnew, width - 1, w->sy, xoff, 0);
                    142:                layout_make_leaf(lcnew, wp);
                    143:                TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
                    144:
                    145:                i++;
                    146:                xoff += width;
                    147:        }
                    148:
                    149:        /* Allocate any remaining space. */
                    150:        if (w->sx > xoff - 1) {
                    151:                lc = TAILQ_LAST(&lc->cells, layout_cells);
                    152:                layout_resize_adjust(lc, LAYOUT_LEFTRIGHT, w->sx - (xoff - 1));
                    153:        }
                    154:
                    155:        /* Fix cell offsets. */
                    156:        layout_fix_offsets(lc);
                    157:        layout_fix_panes(w, w->sx, w->sy);
                    158:
                    159:        layout_print_cell(w->layout_root, __func__, 1);
                    160:
                    161:        server_redraw_window(w);
                    162: }
                    163:
                    164: void
                    165: layout_set_even_v(struct window *w)
                    166: {
                    167:        struct window_pane      *wp;
                    168:        struct layout_cell      *lc, *lcnew;
                    169:        u_int                    i, n, height, yoff;
                    170:
                    171:        layout_print_cell(w->layout_root, __func__, 1);
                    172:
                    173:        /* Get number of panes. */
                    174:        n = window_count_panes(w);
                    175:        if (n <= 1)
                    176:                return;
                    177:
                    178:        /* How many can we fit? */
1.2     ! nicm      179:        if (w->sy / n < PANE_MINIMUM + 1)
1.1       nicm      180:                height = PANE_MINIMUM + 1;
1.2     ! nicm      181:        else
1.1       nicm      182:                height = w->sy / n;
                    183:
                    184:        /* Free the old root and construct a new. */
                    185:        layout_free(w);
                    186:        lc = w->layout_root = layout_create_cell(NULL);
                    187:        layout_set_size(lc, w->sx, w->sy, 0, 0);
                    188:        layout_make_node(lc, LAYOUT_TOPBOTTOM);
                    189:
                    190:        /* Build new leaf cells. */
                    191:        i = yoff = 0;
                    192:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    193:                /* Create child cell. */
                    194:                lcnew = layout_create_cell(lc);
                    195:                layout_set_size(lcnew, w->sx, height - 1, 0, yoff);
                    196:                layout_make_leaf(lcnew, wp);
                    197:                TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
                    198:
                    199:                i++;
                    200:                yoff += height;
                    201:        }
                    202:
                    203:        /* Allocate any remaining space. */
                    204:        if (w->sy > yoff - 1) {
                    205:                lc = TAILQ_LAST(&lc->cells, layout_cells);
                    206:                layout_resize_adjust(lc, LAYOUT_TOPBOTTOM, w->sy - (yoff - 1));
                    207:        }
                    208:
                    209:        /* Fix cell offsets. */
                    210:        layout_fix_offsets(lc);
                    211:        layout_fix_panes(w, w->sx, w->sy);
                    212:
                    213:        layout_print_cell(w->layout_root, __func__, 1);
                    214:
                    215:        server_redraw_window(w);
                    216: }
                    217:
                    218: void
                    219: layout_set_main_h(struct window *w)
                    220: {
                    221:        struct window_pane      *wp;
                    222:        struct layout_cell      *lc, *lcmain, *lcrow, *lcchild;
                    223:        u_int                    n, mainheight, width, height, used;
                    224:        u_int                    i, j, columns, rows, totalrows;
                    225:
                    226:        layout_print_cell(w->layout_root, __func__, 1);
                    227:
                    228:        /* Get number of panes. */
                    229:        n = window_count_panes(w);
                    230:        if (n <= 1)
                    231:                return;
                    232:        n--;    /* take off main pane */
                    233:
                    234:        /* How many rows and columns will be needed? */
                    235:        columns = w->sx / (PANE_MINIMUM + 1);   /* maximum columns */
                    236:        rows = 1 + (n - 1) / columns;
                    237:        columns = 1 + (n - 1) / rows;
                    238:        width = w->sx / columns;
                    239:
                    240:        /* Get the main pane height and add one for separator line. */
                    241:        mainheight = options_get_number(&w->options, "main-pane-height") + 1;
                    242:        if (mainheight < PANE_MINIMUM + 1)
                    243:                mainheight = PANE_MINIMUM + 1;
                    244:
                    245:        /* Try and make everything fit. */
                    246:        totalrows = rows * (PANE_MINIMUM + 1) - 1;
                    247:        if (mainheight + totalrows > w->sy) {
                    248:                if (totalrows + PANE_MINIMUM + 1 > w->sy)
                    249:                        mainheight = PANE_MINIMUM + 2;
                    250:                else
                    251:                        mainheight = w->sy - totalrows;
                    252:                height = PANE_MINIMUM + 1;
                    253:        } else
                    254:                height = (w->sy - mainheight) / rows;
                    255:
                    256:        /* Free old tree and create a new root. */
                    257:        layout_free(w);
                    258:        lc = w->layout_root = layout_create_cell(NULL);
                    259:        layout_set_size(lc, w->sx, mainheight + rows * height, 0, 0);
                    260:        layout_make_node(lc, LAYOUT_TOPBOTTOM);
                    261:
                    262:        /* Create the main pane. */
                    263:        lcmain = layout_create_cell(lc);
                    264:        layout_set_size(lcmain, w->sx, mainheight - 1, 0, 0);
                    265:        layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes));
                    266:        TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry);
                    267:
                    268:        /* Create a grid of the remaining cells. */
                    269:        wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
                    270:        for (j = 0; j < rows; j++) {
                    271:                /* If this is the last cell, all done. */
                    272:                if (wp == NULL)
                    273:                        break;
                    274:
                    275:                /* Create the new row. */
                    276:                lcrow = layout_create_cell(lc);
                    277:                layout_set_size(lcrow, w->sx, height - 1, 0, 0);
                    278:                TAILQ_INSERT_TAIL(&lc->cells, lcrow, entry);
                    279:
                    280:                /* If only one column, just use the row directly. */
                    281:                if (columns == 1) {
                    282:                        layout_make_leaf(lcrow, wp);
                    283:                        wp = TAILQ_NEXT(wp, entry);
                    284:                        continue;
                    285:                }
                    286:
                    287:                /* Add in the columns. */
                    288:                layout_make_node(lcrow, LAYOUT_LEFTRIGHT);
                    289:                for (i = 0; i < columns; i++) {
                    290:                        /* Create and add a pane cell. */
                    291:                        lcchild = layout_create_cell(lcrow);
                    292:                        layout_set_size(lcchild, width - 1, height - 1, 0, 0);
                    293:                        layout_make_leaf(lcchild, wp);
                    294:                        TAILQ_INSERT_TAIL(&lcrow->cells, lcchild, entry);
                    295:
                    296:                        /* Move to the next cell. */
                    297:                        if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    298:                                break;
                    299:                }
                    300:
                    301:                /* Adjust the row to fit the full width if necessary. */
                    302:                if (i == columns)
                    303:                        i--;
                    304:                used = ((i + 1) * width) - 1;
                    305:                if (w->sx <= used)
                    306:                        continue;
                    307:                lcchild = TAILQ_LAST(&lcrow->cells, layout_cells);
                    308:                layout_resize_adjust(lcchild, LAYOUT_LEFTRIGHT, w->sx - used);
                    309:        }
                    310:
                    311:        /* Adjust the last row height to fit if necessary. */
                    312:        used = mainheight + (rows * height) - 1;
                    313:        if (w->sy > used) {
                    314:                lcrow = TAILQ_LAST(&lc->cells, layout_cells);
                    315:                layout_resize_adjust(lcrow, LAYOUT_TOPBOTTOM, w->sy - used);
                    316:        }
                    317:
                    318:        /* Fix cell offsets. */
                    319:        layout_fix_offsets(lc);
                    320:        layout_fix_panes(w, w->sx, w->sy);
                    321:
                    322:        layout_print_cell(w->layout_root, __func__, 1);
                    323:
                    324:        server_redraw_window(w);
                    325: }
                    326:
                    327: void
                    328: layout_set_main_v(struct window *w)
                    329: {
                    330:        struct window_pane      *wp;
                    331:        struct layout_cell      *lc, *lcmain, *lccolumn, *lcchild;
                    332:        u_int                    n, mainwidth, width, height, used;
                    333:        u_int                    i, j, columns, rows, totalcolumns;
                    334:
                    335:        layout_print_cell(w->layout_root, __func__, 1);
                    336:
                    337:        /* Get number of panes. */
                    338:        n = window_count_panes(w);
                    339:        if (n <= 1)
                    340:                return;
                    341:        n--;    /* take off main pane */
                    342:
                    343:        /* How many rows and columns will be needed? */
                    344:        rows = w->sy / (PANE_MINIMUM + 1);      /* maximum rows */
                    345:        columns = 1 + (n - 1) / rows;
                    346:        rows = 1 + (n - 1) / columns;
                    347:        height = w->sy / rows;
                    348:
                    349:        /* Get the main pane width and add one for separator line. */
                    350:        mainwidth = options_get_number(&w->options, "main-pane-width") + 1;
                    351:        if (mainwidth < PANE_MINIMUM + 1)
                    352:                mainwidth = PANE_MINIMUM + 1;
                    353:
                    354:        /* Try and make everything fit. */
                    355:        totalcolumns = columns * (PANE_MINIMUM + 1) - 1;
                    356:        if (mainwidth + totalcolumns > w->sx) {
                    357:                if (totalcolumns + PANE_MINIMUM + 1 > w->sx)
                    358:                        mainwidth = PANE_MINIMUM + 2;
                    359:                else
                    360:                        mainwidth = w->sx - totalcolumns;
                    361:                width = PANE_MINIMUM + 1;
                    362:        } else
                    363:                width = (w->sx - mainwidth) / columns;
                    364:
                    365:        /* Free old tree and create a new root. */
                    366:        layout_free(w);
                    367:        lc = w->layout_root = layout_create_cell(NULL);
                    368:        layout_set_size(lc, mainwidth + columns * width, w->sy, 0, 0);
                    369:        layout_make_node(lc, LAYOUT_LEFTRIGHT);
                    370:
                    371:        /* Create the main pane. */
                    372:        lcmain = layout_create_cell(lc);
                    373:        layout_set_size(lcmain, mainwidth - 1, w->sy, 0, 0);
                    374:        layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes));
                    375:        TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry);
                    376:
                    377:        /* Create a grid of the remaining cells. */
                    378:        wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
                    379:        for (j = 0; j < columns; j++) {
                    380:                /* If this is the last cell, all done. */
                    381:                if (wp == NULL)
                    382:                        break;
                    383:
                    384:                /* Create the new column. */
                    385:                lccolumn = layout_create_cell(lc);
                    386:                layout_set_size(lccolumn, width - 1, w->sy, 0, 0);
                    387:                TAILQ_INSERT_TAIL(&lc->cells, lccolumn, entry);
                    388:
                    389:                /* If only one row, just use the row directly. */
                    390:                if (rows == 1) {
                    391:                        layout_make_leaf(lccolumn, wp);
                    392:                        wp = TAILQ_NEXT(wp, entry);
                    393:                        continue;
                    394:                }
                    395:
                    396:                /* Add in the rows. */
                    397:                layout_make_node(lccolumn, LAYOUT_TOPBOTTOM);
                    398:                for (i = 0; i < rows; i++) {
                    399:                        /* Create and add a pane cell. */
                    400:                        lcchild = layout_create_cell(lccolumn);
                    401:                        layout_set_size(lcchild, width - 1, height - 1, 0, 0);
                    402:                        layout_make_leaf(lcchild, wp);
                    403:                        TAILQ_INSERT_TAIL(&lccolumn->cells, lcchild, entry);
                    404:
                    405:                        /* Move to the next cell. */
                    406:                        if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    407:                                break;
                    408:                }
                    409:
                    410:                /* Adjust the column to fit the full height if necessary. */
                    411:                if (i == rows)
                    412:                        i--;
                    413:                used = ((i + 1) * height) - 1;
                    414:                if (w->sy <= used)
                    415:                        continue;
                    416:                lcchild = TAILQ_LAST(&lccolumn->cells, layout_cells);
                    417:                layout_resize_adjust(lcchild, LAYOUT_TOPBOTTOM, w->sy - used);
                    418:        }
                    419:
                    420:        /* Adjust the last column width to fit if necessary. */
                    421:        used = mainwidth + (columns * width) - 1;
                    422:        if (w->sx > used) {
                    423:                lccolumn = TAILQ_LAST(&lc->cells, layout_cells);
                    424:                layout_resize_adjust(lccolumn, LAYOUT_LEFTRIGHT, w->sx - used);
                    425:        }
                    426:
                    427:        /* Fix cell offsets. */
                    428:        layout_fix_offsets(lc);
                    429:        layout_fix_panes(w, w->sx, w->sy);
                    430:
                    431:        layout_print_cell(w->layout_root, __func__, 1);
                    432:
                    433:        server_redraw_window(w);
                    434: }