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