[BACK]Return to newwin.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libcurses

Annotation of src/lib/libcurses/newwin.c, Revision 1.29

1.29    ! blymn       1: /*     $NetBSD: newwin.c,v 1.28 2002/01/02 10:38:28 blymn Exp $        */
1.8       mikel       2:
1.1       cgd         3: /*
1.7       cgd         4:  * Copyright (c) 1981, 1993, 1994
1.5       cgd         5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
1.8       mikel      36: #include <sys/cdefs.h>
1.1       cgd        37: #ifndef lint
1.8       mikel      38: #if 0
1.7       cgd        39: static char sccsid[] = "@(#)newwin.c   8.3 (Berkeley) 7/27/94";
1.8       mikel      40: #else
1.29    ! blymn      41: __RCSID("$NetBSD: newwin.c,v 1.28 2002/01/02 10:38:28 blymn Exp $");
1.8       mikel      42: #endif
1.10      mrg        43: #endif                         /* not lint */
1.1       cgd        44:
1.4       mycroft    45: #include <stdlib.h>
1.1       cgd        46:
1.7       cgd        47: #include "curses.h"
1.13      blymn      48: #include "curses_private.h"
1.7       cgd        49:
1.15      jdc        50: extern struct __winlist        *winlistp;
                     51:
1.26      blymn      52: static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
                     53:                         int bx, int sub);
1.20      blymn      54:
                     55: /*
                     56:  * derwin --
                     57:  *      Create a new window in the same manner as subwin but (by, bx)
                     58:  *      are relative to the origin of window orig instead of absolute.
                     59:  */
                     60: WINDOW *
                     61: derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
                     62: {
                     63:        if (orig == NULL)
                     64:                return ERR;
1.28      blymn      65:
1.20      blymn      66:        return subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx);
                     67: }
1.5       cgd        68:
1.22      blymn      69: /*
                     70:  * dupwin --
                     71:  *      Create a copy of the given window.
                     72:  */
                     73: WINDOW *
                     74: dupwin(WINDOW *win)
                     75: {
                     76:        WINDOW *new_one;
                     77:
                     78:        if ((new_one =
                     79:             newwin(win->maxy, win->maxx, win->begy, win->begx)) == NULL)
                     80:                return NULL;
                     81:
                     82:        overwrite(win, new_one);
                     83:        return new_one;
                     84: }
                     85:
1.28      blymn      86:
1.4       mycroft    87: /*
                     88:  * newwin --
                     89:  *     Allocate space for and set up defaults for a new window.
                     90:  */
1.1       cgd        91: WINDOW *
1.19      blymn      92: newwin(int nlines, int ncols, int by, int bx)
1.1       cgd        93: {
1.26      blymn      94:        return __newwin(_cursesi_screen, nlines, ncols, by, bx);
                     95: }
                     96:
                     97: WINDOW *
                     98: __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx)
                     99: {
1.9       perry     100:        WINDOW *win;
                    101:        __LINE *lp;
1.10      mrg       102:        int     i, j;
1.9       perry     103:        __LDATA *sp;
1.1       cgd       104:
1.19      blymn     105:        if (nlines == 0)
                    106:                nlines = LINES - by;
                    107:        if (ncols == 0)
                    108:                ncols = COLS - bx;
1.5       cgd       109:
1.26      blymn     110:        if ((win = __makenew(screen, nlines, ncols, by, bx, 0)) == NULL)
1.4       mycroft   111:                return (NULL);
1.5       cgd       112:
                    113:        win->nextp = win;
                    114:        win->ch_off = 0;
                    115:        win->orig = NULL;
                    116:
                    117: #ifdef DEBUG
                    118:        __CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
                    119: #endif
                    120:
1.19      blymn     121:        for (i = 0; i < nlines; i++) {
1.5       cgd       122:                lp = win->lines[i];
                    123:                lp->flags = 0;
1.19      blymn     124:                for (sp = lp->line, j = 0; j < ncols; j++, sp++) {
1.5       cgd       125:                        sp->ch = ' ';
1.21      jdc       126:                        sp->bch = ' ';
1.5       cgd       127:                        sp->attr = 0;
1.21      jdc       128:                        sp->battr = 0;
1.5       cgd       129:                }
1.12      christos  130:                lp->hash = __hash((char *)(void *)lp->line,
1.19      blymn     131:                    (int) (ncols * __LDATASIZE));
1.1       cgd       132:        }
1.4       mycroft   133:        return (win);
1.1       cgd       134: }
                    135:
                    136: WINDOW *
1.19      blymn     137: subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
1.1       cgd       138: {
1.10      mrg       139:        int     i;
1.5       cgd       140:        __LINE *lp;
1.9       perry     141:        WINDOW *win;
1.1       cgd       142:
1.4       mycroft   143:        /* Make sure window fits inside the original one. */
                    144: #ifdef DEBUG
1.19      blymn     145:        __CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nlines, ncols, by, bx);
1.4       mycroft   146: #endif
1.5       cgd       147:        if (by < orig->begy || bx < orig->begx
1.19      blymn     148:            || by + nlines > orig->maxy + orig->begy
                    149:            || bx + ncols > orig->maxx + orig->begx)
1.4       mycroft   150:                return (NULL);
1.19      blymn     151:        if (nlines == 0)
                    152:                nlines = orig->maxy + orig->begy - by;
                    153:        if (ncols == 0)
                    154:                ncols = orig->maxx + orig->begx - bx;
1.26      blymn     155:        if ((win = __makenew(_cursesi_screen, nlines, ncols,
                    156:                             by, bx, 1)) == NULL)
1.4       mycroft   157:                return (NULL);
1.5       cgd       158:        win->nextp = orig->nextp;
                    159:        orig->nextp = win;
                    160:        win->orig = orig;
                    161:
                    162:        /* Initialize flags here so that refresh can also use __set_subwin. */
                    163:        for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
                    164:                lp->flags = 0;
1.4       mycroft   165:        __set_subwin(orig, win);
                    166:        return (win);
1.1       cgd       167: }
                    168: /*
1.4       mycroft   169:  * This code is shared with mvwin().
1.1       cgd       170:  */
1.4       mycroft   171: void
1.16      blymn     172: __set_subwin(WINDOW *orig, WINDOW *win)
1.1       cgd       173: {
1.10      mrg       174:        int     i;
1.5       cgd       175:        __LINE *lp, *olp;
                    176:
                    177:        win->ch_off = win->begx - orig->begx;
1.10      mrg       178:        /* Point line pointers to line space. */
1.5       cgd       179:        for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
                    180:                win->lines[i] = lp;
1.11      mycroft   181:                olp = orig->lines[i + win->begy - orig->begy];
1.29    ! blymn     182: #ifdef DEBUG
        !           183:                lp->sentinel = SENTINEL_VALUE;
        !           184: #endif
1.18      mycroft   185:                lp->line = &olp->line[win->ch_off];
1.5       cgd       186:                lp->firstchp = &olp->firstch;
                    187:                lp->lastchp = &olp->lastch;
1.12      christos  188:                lp->hash = __hash((char *)(void *)lp->line,
                    189:                    (int) (win->maxx * __LDATASIZE));
1.5       cgd       190:        }
1.1       cgd       191:
1.4       mycroft   192: #ifdef DEBUG
1.5       cgd       193:        __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
1.4       mycroft   194: #endif
1.1       cgd       195: }
                    196: /*
1.5       cgd       197:  * __makenew --
1.4       mycroft   198:  *     Set up a window buffer and returns a pointer to it.
1.1       cgd       199:  */
                    200: static WINDOW *
1.26      blymn     201: __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub)
1.4       mycroft   202: {
1.15      jdc       203:        WINDOW                  *win;
                    204:        __LINE                  *lp;
                    205:        struct __winlist        *wlp, *wlp2;
                    206:        int                      i;
1.10      mrg       207:
1.1       cgd       208:
1.4       mycroft   209: #ifdef DEBUG
1.19      blymn     210:        __CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx);
1.4       mycroft   211: #endif
1.27      blymn     212:        if ((win = malloc(sizeof(WINDOW))) == NULL)
1.4       mycroft   213:                return (NULL);
                    214: #ifdef DEBUG
1.19      blymn     215:        __CTRACE("makenew: nlines = %d\n", nlines);
1.4       mycroft   216: #endif
1.5       cgd       217:
1.10      mrg       218:        /* Set up line pointer array and line space. */
1.19      blymn     219:        if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
1.1       cgd       220:                free(win);
1.5       cgd       221:                return NULL;
                    222:        }
1.19      blymn     223:        if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
1.10      mrg       224:                free(win);
                    225:                free(win->lines);
1.5       cgd       226:                return NULL;
                    227:        }
                    228:        /* Don't allocate window and line space if it's a subwindow */
                    229:        if (!sub) {
                    230:                /*
                    231:                 * Allocate window space in one chunk.
                    232:                 */
1.10      mrg       233:                if ((win->wspace =
1.19      blymn     234:                        malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
1.5       cgd       235:                        free(win->lines);
                    236:                        free(win->lspace);
                    237:                        free(win);
                    238:                        return NULL;
1.15      jdc       239:                }
                    240:                /*
                    241:                 * Append window to window list.
                    242:                 */
                    243:                if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
                    244:                        free(win->wspace);
                    245:                        free(win->lines);
                    246:                        free(win->lspace);
                    247:                        free(win);
                    248:                        return NULL;
                    249:                }
                    250:                wlp->winp = win;
                    251:                wlp->nextp = NULL;
1.26      blymn     252:                if (screen->winlistp == NULL)
                    253:                        screen->winlistp = wlp;
1.15      jdc       254:                else {
1.26      blymn     255:                        wlp2 = screen->winlistp;
1.15      jdc       256:                        while (wlp2->nextp != NULL)
                    257:                                wlp2 = wlp2->nextp;
                    258:                        wlp2->nextp = wlp;
1.5       cgd       259:                }
                    260:                /*
                    261:                 * Point line pointers to line space, and lines themselves into
                    262:                 * window space.
                    263:                 */
1.19      blymn     264:                for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
1.5       cgd       265:                        win->lines[i] = lp;
1.19      blymn     266:                        lp->line = &win->wspace[i * ncols];
1.29    ! blymn     267: #ifdef DEBUG
        !           268:                        lp->sentinel = SENTINEL_VALUE;
        !           269: #endif
1.5       cgd       270:                        lp->firstchp = &lp->firstch;
                    271:                        lp->lastchp = &lp->lastch;
                    272:                        lp->firstch = 0;
                    273:                        lp->lastch = 0;
                    274:                }
1.1       cgd       275:        }
1.4       mycroft   276: #ifdef DEBUG
1.19      blymn     277:        __CTRACE("makenew: ncols = %d\n", ncols);
1.4       mycroft   278: #endif
1.5       cgd       279:        win->cury = win->curx = 0;
1.19      blymn     280:        win->maxy = nlines;
                    281:        win->maxx = ncols;
1.5       cgd       282:
                    283:        win->begy = by;
                    284:        win->begx = bx;
1.29    ! blymn     285:        win->flags = (__IDLINE | __IDCHAR);
1.17      jdc       286:        win->delay = -1;
1.13      blymn     287:        win->wattr = 0;
1.21      jdc       288:        win->bch = ' ';
1.14      jdc       289:        win->battr = 0;
1.25      jdc       290:        win->scr_t = 0;
1.24      jdc       291:        win->scr_b = win->maxy - 1;
1.4       mycroft   292:        __swflags(win);
                    293: #ifdef DEBUG
1.13      blymn     294:        __CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
1.5       cgd       295:        __CTRACE("makenew: win->flags = %0.2o\n", win->flags);
                    296:        __CTRACE("makenew: win->maxy = %d\n", win->maxy);
                    297:        __CTRACE("makenew: win->maxx = %d\n", win->maxx);
                    298:        __CTRACE("makenew: win->begy = %d\n", win->begy);
                    299:        __CTRACE("makenew: win->begx = %d\n", win->begx);
1.24      jdc       300:        __CTRACE("makenew: win->scr_t = %d\n", win->scr_t);
                    301:        __CTRACE("makenew: win->scr_b = %d\n", win->scr_b);
1.4       mycroft   302: #endif
                    303:        return (win);
1.1       cgd       304: }
                    305:
1.4       mycroft   306: void
1.16      blymn     307: __swflags(WINDOW *win)
1.1       cgd       308: {
1.7       cgd       309:        win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
1.5       cgd       310:        if (win->begx + win->maxx == COLS) {
                    311:                win->flags |= __ENDLINE;
1.7       cgd       312:                if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
                    313:                        win->flags |= __FULLWIN;
1.5       cgd       314:                if (win->begy + win->maxy == LINES)
                    315:                        win->flags |= __SCROLLWIN;
1.1       cgd       316:        }
                    317: }

CVSweb <webmaster@jp.NetBSD.org>