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