Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.27
      1  1.27     blymn /*	$NetBSD: newwin.c,v 1.27 2001/12/11 11:18:17 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.27     blymn __RCSID("$NetBSD: newwin.c,v 1.27 2001/12/11 11:18:17 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.15       jdc extern struct __winlist	*winlistp;
     51  1.15       jdc 
     52  1.26     blymn static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
     53  1.26     blymn 			 int bx, int sub);
     54  1.20     blymn 
     55  1.20     blymn /*
     56  1.20     blymn  * derwin --
     57  1.20     blymn  *      Create a new window in the same manner as subwin but (by, bx)
     58  1.20     blymn  *      are relative to the origin of window orig instead of absolute.
     59  1.20     blymn  */
     60  1.20     blymn WINDOW *
     61  1.20     blymn derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
     62  1.20     blymn {
     63  1.20     blymn 	if (orig == NULL)
     64  1.20     blymn 		return ERR;
     65  1.20     blymn 
     66  1.20     blymn 	return subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx);
     67  1.20     blymn }
     68   1.5       cgd 
     69  1.22     blymn /*
     70  1.22     blymn  * dupwin --
     71  1.22     blymn  *      Create a copy of the given window.
     72  1.22     blymn  */
     73  1.22     blymn WINDOW *
     74  1.22     blymn dupwin(WINDOW *win)
     75  1.22     blymn {
     76  1.22     blymn 	WINDOW *new_one;
     77  1.22     blymn 
     78  1.22     blymn 	if ((new_one =
     79  1.22     blymn 	     newwin(win->maxy, win->maxx, win->begy, win->begx)) == NULL)
     80  1.22     blymn 		return NULL;
     81  1.22     blymn 
     82  1.22     blymn 	overwrite(win, new_one);
     83  1.22     blymn 	return new_one;
     84  1.22     blymn }
     85  1.22     blymn 
     86  1.22     blymn 
     87   1.4   mycroft /*
     88   1.4   mycroft  * newwin --
     89   1.4   mycroft  *	Allocate space for and set up defaults for a new window.
     90   1.4   mycroft  */
     91   1.1       cgd WINDOW *
     92  1.19     blymn newwin(int nlines, int ncols, int by, int bx)
     93   1.1       cgd {
     94  1.26     blymn 	return __newwin(_cursesi_screen, nlines, ncols, by, bx);
     95  1.26     blymn }
     96  1.26     blymn 
     97  1.26     blymn WINDOW *
     98  1.26     blymn __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx)
     99  1.26     blymn {
    100   1.9     perry 	WINDOW *win;
    101   1.9     perry 	__LINE *lp;
    102  1.10       mrg 	int     i, j;
    103   1.9     perry 	__LDATA *sp;
    104   1.1       cgd 
    105  1.19     blymn 	if (nlines == 0)
    106  1.19     blymn 		nlines = LINES - by;
    107  1.19     blymn 	if (ncols == 0)
    108  1.19     blymn 		ncols = COLS - bx;
    109   1.5       cgd 
    110  1.26     blymn 	if ((win = __makenew(screen, nlines, ncols, by, bx, 0)) == NULL)
    111   1.4   mycroft 		return (NULL);
    112   1.5       cgd 
    113   1.5       cgd 	win->nextp = win;
    114   1.5       cgd 	win->ch_off = 0;
    115   1.5       cgd 	win->orig = NULL;
    116   1.5       cgd 
    117   1.5       cgd #ifdef DEBUG
    118   1.5       cgd 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
    119   1.5       cgd #endif
    120   1.5       cgd 
    121  1.19     blymn 	for (i = 0; i < nlines; i++) {
    122   1.5       cgd 		lp = win->lines[i];
    123   1.5       cgd 		lp->flags = 0;
    124  1.19     blymn 		for (sp = lp->line, j = 0; j < ncols; j++, sp++) {
    125   1.5       cgd 			sp->ch = ' ';
    126  1.21       jdc 			sp->bch = ' ';
    127   1.5       cgd 			sp->attr = 0;
    128  1.21       jdc 			sp->battr = 0;
    129   1.5       cgd 		}
    130  1.12  christos 		lp->hash = __hash((char *)(void *)lp->line,
    131  1.19     blymn 		    (int) (ncols * __LDATASIZE));
    132   1.1       cgd 	}
    133   1.4   mycroft 	return (win);
    134   1.1       cgd }
    135   1.1       cgd 
    136   1.1       cgd WINDOW *
    137  1.19     blymn subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
    138   1.1       cgd {
    139  1.10       mrg 	int     i;
    140   1.5       cgd 	__LINE *lp;
    141   1.9     perry 	WINDOW *win;
    142   1.1       cgd 
    143   1.4   mycroft 	/* Make sure window fits inside the original one. */
    144   1.4   mycroft #ifdef	DEBUG
    145  1.19     blymn 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nlines, ncols, by, bx);
    146   1.4   mycroft #endif
    147   1.5       cgd 	if (by < orig->begy || bx < orig->begx
    148  1.19     blymn 	    || by + nlines > orig->maxy + orig->begy
    149  1.19     blymn 	    || bx + ncols > orig->maxx + orig->begx)
    150   1.4   mycroft 		return (NULL);
    151  1.19     blymn 	if (nlines == 0)
    152  1.19     blymn 		nlines = orig->maxy + orig->begy - by;
    153  1.19     blymn 	if (ncols == 0)
    154  1.19     blymn 		ncols = orig->maxx + orig->begx - bx;
    155  1.26     blymn 	if ((win = __makenew(_cursesi_screen, nlines, ncols,
    156  1.26     blymn 			     by, bx, 1)) == NULL)
    157   1.4   mycroft 		return (NULL);
    158   1.5       cgd 	win->nextp = orig->nextp;
    159   1.5       cgd 	orig->nextp = win;
    160   1.5       cgd 	win->orig = orig;
    161   1.5       cgd 
    162   1.5       cgd 	/* Initialize flags here so that refresh can also use __set_subwin. */
    163   1.5       cgd 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    164   1.5       cgd 		lp->flags = 0;
    165   1.4   mycroft 	__set_subwin(orig, win);
    166   1.4   mycroft 	return (win);
    167   1.1       cgd }
    168   1.1       cgd /*
    169   1.4   mycroft  * This code is shared with mvwin().
    170   1.1       cgd  */
    171   1.4   mycroft void
    172  1.16     blymn __set_subwin(WINDOW *orig, WINDOW *win)
    173   1.1       cgd {
    174  1.10       mrg 	int     i;
    175   1.5       cgd 	__LINE *lp, *olp;
    176   1.5       cgd 
    177   1.5       cgd 	win->ch_off = win->begx - orig->begx;
    178  1.10       mrg 	/* Point line pointers to line space. */
    179   1.5       cgd 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    180   1.5       cgd 		win->lines[i] = lp;
    181  1.11   mycroft 		olp = orig->lines[i + win->begy - orig->begy];
    182  1.18   mycroft 		lp->line = &olp->line[win->ch_off];
    183   1.5       cgd 		lp->firstchp = &olp->firstch;
    184   1.5       cgd 		lp->lastchp = &olp->lastch;
    185  1.12  christos 		lp->hash = __hash((char *)(void *)lp->line,
    186  1.12  christos 		    (int) (win->maxx * __LDATASIZE));
    187   1.5       cgd 	}
    188   1.1       cgd 
    189   1.4   mycroft #ifdef DEBUG
    190   1.5       cgd 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
    191   1.4   mycroft #endif
    192   1.1       cgd }
    193   1.1       cgd /*
    194   1.5       cgd  * __makenew --
    195   1.4   mycroft  *	Set up a window buffer and returns a pointer to it.
    196   1.1       cgd  */
    197   1.1       cgd static WINDOW *
    198  1.26     blymn __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub)
    199   1.4   mycroft {
    200  1.15       jdc 	WINDOW			*win;
    201  1.15       jdc 	__LINE			*lp;
    202  1.15       jdc 	struct __winlist	*wlp, *wlp2;
    203  1.15       jdc 	int			 i;
    204  1.10       mrg 
    205   1.1       cgd 
    206   1.4   mycroft #ifdef	DEBUG
    207  1.19     blymn 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx);
    208   1.4   mycroft #endif
    209  1.27     blymn 	if ((win = malloc(sizeof(WINDOW))) == NULL)
    210   1.4   mycroft 		return (NULL);
    211   1.4   mycroft #ifdef DEBUG
    212  1.19     blymn 	__CTRACE("makenew: nlines = %d\n", nlines);
    213   1.4   mycroft #endif
    214   1.5       cgd 
    215  1.10       mrg 	/* Set up line pointer array and line space. */
    216  1.19     blymn 	if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
    217   1.1       cgd 		free(win);
    218   1.5       cgd 		return NULL;
    219   1.5       cgd 	}
    220  1.19     blymn 	if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
    221  1.10       mrg 		free(win);
    222  1.10       mrg 		free(win->lines);
    223   1.5       cgd 		return NULL;
    224   1.5       cgd 	}
    225   1.5       cgd 	/* Don't allocate window and line space if it's a subwindow */
    226   1.5       cgd 	if (!sub) {
    227   1.5       cgd 		/*
    228   1.5       cgd 		 * Allocate window space in one chunk.
    229   1.5       cgd 		 */
    230  1.10       mrg 		if ((win->wspace =
    231  1.19     blymn 			malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
    232   1.5       cgd 			free(win->lines);
    233   1.5       cgd 			free(win->lspace);
    234   1.5       cgd 			free(win);
    235   1.5       cgd 			return NULL;
    236  1.15       jdc 		}
    237  1.15       jdc 		/*
    238  1.15       jdc 		 * Append window to window list.
    239  1.15       jdc 		 */
    240  1.15       jdc 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
    241  1.15       jdc 			free(win->wspace);
    242  1.15       jdc 			free(win->lines);
    243  1.15       jdc 			free(win->lspace);
    244  1.15       jdc 			free(win);
    245  1.15       jdc 			return NULL;
    246  1.15       jdc 		}
    247  1.15       jdc 		wlp->winp = win;
    248  1.15       jdc 		wlp->nextp = NULL;
    249  1.26     blymn 		if (screen->winlistp == NULL)
    250  1.26     blymn 			screen->winlistp = wlp;
    251  1.15       jdc 		else {
    252  1.26     blymn 			wlp2 = screen->winlistp;
    253  1.15       jdc 			while (wlp2->nextp != NULL)
    254  1.15       jdc 				wlp2 = wlp2->nextp;
    255  1.15       jdc 			wlp2->nextp = wlp;
    256   1.5       cgd 		}
    257   1.5       cgd 		/*
    258   1.5       cgd 		 * Point line pointers to line space, and lines themselves into
    259   1.5       cgd 		 * window space.
    260   1.5       cgd 		 */
    261  1.19     blymn 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    262   1.5       cgd 			win->lines[i] = lp;
    263  1.19     blymn 			lp->line = &win->wspace[i * ncols];
    264   1.5       cgd 			lp->firstchp = &lp->firstch;
    265   1.5       cgd 			lp->lastchp = &lp->lastch;
    266   1.5       cgd 			lp->firstch = 0;
    267   1.5       cgd 			lp->lastch = 0;
    268   1.5       cgd 		}
    269   1.1       cgd 	}
    270   1.4   mycroft #ifdef DEBUG
    271  1.19     blymn 	__CTRACE("makenew: ncols = %d\n", ncols);
    272   1.4   mycroft #endif
    273   1.5       cgd 	win->cury = win->curx = 0;
    274  1.19     blymn 	win->maxy = nlines;
    275  1.19     blymn 	win->maxx = ncols;
    276   1.5       cgd 
    277   1.5       cgd 	win->begy = by;
    278   1.5       cgd 	win->begx = bx;
    279   1.5       cgd 	win->flags = 0;
    280  1.17       jdc 	win->delay = -1;
    281  1.13     blymn 	win->wattr = 0;
    282  1.21       jdc 	win->bch = ' ';
    283  1.14       jdc 	win->battr = 0;
    284  1.25       jdc 	win->scr_t = 0;
    285  1.24       jdc 	win->scr_b = win->maxy - 1;
    286   1.4   mycroft 	__swflags(win);
    287   1.4   mycroft #ifdef DEBUG
    288  1.13     blymn 	__CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
    289   1.5       cgd 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
    290   1.5       cgd 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
    291   1.5       cgd 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
    292   1.5       cgd 	__CTRACE("makenew: win->begy = %d\n", win->begy);
    293   1.5       cgd 	__CTRACE("makenew: win->begx = %d\n", win->begx);
    294  1.24       jdc 	__CTRACE("makenew: win->scr_t = %d\n", win->scr_t);
    295  1.24       jdc 	__CTRACE("makenew: win->scr_b = %d\n", win->scr_b);
    296   1.4   mycroft #endif
    297   1.4   mycroft 	return (win);
    298   1.1       cgd }
    299   1.1       cgd 
    300   1.4   mycroft void
    301  1.16     blymn __swflags(WINDOW *win)
    302   1.1       cgd {
    303   1.7       cgd 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    304   1.5       cgd 	if (win->begx + win->maxx == COLS) {
    305   1.5       cgd 		win->flags |= __ENDLINE;
    306   1.7       cgd 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
    307   1.7       cgd 			win->flags |= __FULLWIN;
    308   1.5       cgd 		if (win->begy + win->maxy == LINES)
    309   1.5       cgd 			win->flags |= __SCROLLWIN;
    310   1.1       cgd 	}
    311   1.1       cgd }
    312