Home | History | Annotate | Line # | Download | only in libcurses
resize.c revision 1.1
      1 /*	$NetBSD: resize.c,v 1.1 2001/09/20 11:11:54 blymn Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001
      5  *	Brett Lymn.
      6  *
      7  * This code has been donated to The NetBSD Foundation by the Author.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)resize.c   blymn 2001/08/26";
     42 #else
     43 __RCSID("$NetBSD: resize.c,v 1.1 2001/09/20 11:11:54 blymn Exp $");
     44 #endif
     45 #endif				/* not lint */
     46 
     47 #include <stdlib.h>
     48 
     49 #include "curses.h"
     50 #include "curses_private.h"
     51 
     52 extern struct __winlist	*winlistp;
     53 
     54 static int __resizewin(WINDOW *win, int nlines, int ncols);
     55 
     56 /*
     57  * wresize --
     58  *	Resize the given window to the new size.
     59  */
     60 int
     61 wresize(WINDOW *win, int nlines, int ncols)
     62 {
     63 	__LINE *lp;
     64 	int     i, j;
     65 	__LDATA *sp;
     66 
     67 	if ((win == NULL) || (nlines < 0) || (ncols < 0))
     68 		return ERR;
     69 
     70 	if (win->orig == NULL) {
     71 		if (nlines == 0)
     72 			nlines = LINES - win->begy;
     73 		if (ncols == 0)
     74 			ncols = COLS - win->begx;
     75 	} else {
     76 		  /* subwins must fit inside the parent - check this */
     77 		if ((nlines == 0)
     78 		    || (nlines > (win->orig->maxy + win->orig->begy
     79 				  - win->begy)))
     80 			nlines = win->orig->maxy + win->orig->begy - win->begy;
     81 
     82 		if ((ncols == 0)
     83 		    || (ncols > (win->orig->maxx + win->orig->begx
     84 				 - win->begx)))
     85 			ncols = win->orig->maxx + win->orig->begx - win->begx;
     86 	}
     87 
     88 	if ((__resizewin(win, nlines, ncols)) == ERR)
     89 		return ERR;
     90 
     91 	  /*
     92 	   * we must zot the window contents otherwise lines may pick
     93 	   * up attributes from the previous line when the window is
     94 	   * made smaller.  The client will redraw the window anyway
     95 	   * so this is no big deal.
     96 	   */
     97 	for (i = 0; i < win->maxy; i++) {
     98 		lp = win->lines[i];
     99 		for (sp = lp->line, j = 0; j < win->maxx;
    100 		     j++, sp++) {
    101 			sp->ch = ' ';
    102 			sp->bch = ' ';
    103 			sp->attr = 0;
    104 			sp->battr = 0;
    105 		}
    106 		lp->hash = __hash((char *)(void *)lp->line,
    107 				  (int) (ncols * __LDATASIZE));
    108 	}
    109 
    110 	return OK;
    111 }
    112 
    113 /*
    114  * resizeterm --
    115  *      Resize the terminal window, resizing the dependent windows.
    116  */
    117 int
    118 resizeterm(int nlines, int ncols)
    119 {
    120 	WINDOW *win;
    121 	struct __winlist *list;
    122 	int newlines, newcols, ldelta, cdelta;
    123 
    124 	  /* don't worry if things have not changed... we would like to
    125 	     do this but some bastard programs update LINES and COLS before
    126 	     calling resizeterm thus negating it's effect.
    127 	if ((nlines == LINES) && (ncols == COLS))
    128 	return OK;*/
    129 
    130 #ifdef	DEBUG
    131 	__CTRACE("resizeterm: (%d, %d)\n", nlines, ncols);
    132 #endif
    133 
    134 	ldelta = nlines - __virtscr->maxy;
    135 	cdelta = ncols - __virtscr->maxx;
    136 
    137 	for (list = __winlistp; list != NULL; list = list->nextp) {
    138 		win = list->winp;
    139 		newlines = win->maxy;
    140 		newcols = win->maxx;
    141 
    142 		if (win->begy >= (nlines - 1)) {
    143 			win->begy = nlines - win->maxx - 1;
    144 			if (win->begy < 0)
    145 				win->begy = 0;
    146 		}
    147 
    148 
    149 		if ((newlines + win->begy + ldelta) == nlines)
    150 				newlines = nlines;
    151 
    152 		if (newlines > nlines) {
    153 			newlines = nlines - win->begy;
    154 			if (newlines < 0)
    155 				newlines = 1;
    156 		}
    157 
    158 		if ((newcols + win->begx + cdelta) == ncols)
    159 			newcols = ncols;
    160 
    161 		if (newcols > ncols) {
    162 			newcols = ncols - win->begx;
    163 			if (newcols < 0)
    164 				newcols = 1;
    165 		}
    166 
    167 		if (wresize(win, newlines, newcols) != OK)
    168 			return ERR;
    169 	}
    170 
    171 	LINES = nlines;
    172 	COLS = ncols;
    173 
    174 	  /* tweak the flags now that we have updated the LINES and COLS */
    175 	for (list = __winlistp; list != NULL; list = list->nextp) {
    176 		  __swflags(list->winp);
    177 	}
    178 
    179 	wrefresh(curscr);
    180 	return OK;
    181 }
    182 
    183 /*
    184  * __resizewin --
    185  *	Resize the given window.
    186  */
    187 static int
    188 __resizewin(WINDOW *win, int nlines, int ncols)
    189 {
    190 	__LINE			*lp, *olp, **newlines, *newlspace;
    191 	__LDATA                 *newwspace;
    192 	int			 i;
    193 
    194 #ifdef	DEBUG
    195 	__CTRACE("resize: (%d, %d)\n", nlines, ncols);
    196 	__CTRACE("resize: win->wattr = %0.2o\n", win->wattr);
    197 	__CTRACE("resize: win->flags = %0.2o\n", win->flags);
    198 	__CTRACE("resize: win->maxy = %d\n", win->maxy);
    199 	__CTRACE("resize: win->maxx = %d\n", win->maxx);
    200 	__CTRACE("resize: win->begy = %d\n", win->begy);
    201 	__CTRACE("resize: win->begx = %d\n", win->begx);
    202 	__CTRACE("resize: win->scr_t = %d\n", win->scr_t);
    203 	__CTRACE("resize: win->scr_b = %d\n", win->scr_b);
    204 #endif
    205 
    206 	/* Reallocate line pointer array and line space. */
    207 	if ((newlines = realloc(win->lines,
    208 				nlines * sizeof(__LINE *))) == NULL) {
    209 		return ERR;
    210 	}
    211 	win->lines = newlines;
    212 
    213 	if ((newlspace = realloc(win->lspace,
    214 				 nlines * sizeof(__LINE))) == NULL) {
    215 		return ERR;
    216 	}
    217 	win->lspace = newlspace;
    218 
    219 	/* Don't allocate window and line space if it's a subwindow */
    220 	if (win->orig == NULL) {
    221 		/*
    222 		 * Allocate window space in one chunk.
    223 		 */
    224 		if ((newwspace =
    225 			realloc(win->wspace,
    226 				ncols * nlines * sizeof(__LDATA))) == NULL) {
    227 			return ERR;
    228 		}
    229 
    230 		win->wspace = newwspace;
    231 
    232 		/*
    233 		 * Point line pointers to line space, and lines themselves into
    234 		 * window space.
    235 		 */
    236 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    237 			win->lines[i] = lp;
    238 			lp->line = &win->wspace[i * ncols];
    239 			lp->firstchp = &lp->firstch;
    240 			lp->lastchp = &lp->lastch;
    241 			lp->firstch = 0;
    242 			lp->lastch = ncols - 1;
    243 			lp->flags = __ISDIRTY;
    244 		}
    245 	} else {
    246 
    247 		win->ch_off = win->begx - win->orig->begx;
    248 		  /* Point line pointers to line space. */
    249 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    250 			win->lines[i] = lp;
    251 			olp = win->orig->lines[i + win->begy
    252 					      - win->orig->begy];
    253 			lp->line = &olp->line[win->ch_off];
    254 			lp->firstchp = &olp->firstch;
    255 			lp->lastchp = &olp->lastch;
    256 			lp->hash = __hash((char *)(void *)lp->line,
    257 					  (int) (win->maxx * __LDATASIZE));
    258 			lp->flags = __ISDIRTY;
    259 		}
    260 	}
    261 
    262 
    263 	win->cury = win->curx = 0;
    264 	win->maxy = nlines;
    265 	win->maxx = ncols;
    266 	win->scr_b = win->maxy - 1;
    267 	__swflags(win);
    268 
    269 #ifdef DEBUG
    270 	__CTRACE("resize: win->wattr = %0.2o\n", win->wattr);
    271 	__CTRACE("resize: win->flags = %0.2o\n", win->flags);
    272 	__CTRACE("resize: win->maxy = %d\n", win->maxy);
    273 	__CTRACE("resize: win->maxx = %d\n", win->maxx);
    274 	__CTRACE("resize: win->begy = %d\n", win->begy);
    275 	__CTRACE("resize: win->begx = %d\n", win->begx);
    276 	__CTRACE("resize: win->scr_t = %d\n", win->scr_t);
    277 	__CTRACE("resize: win->scr_b = %d\n", win->scr_b);
    278 #endif
    279 	return OK;
    280 }
    281 
    282