1 /* $NetBSD: newwin.c,v 1.42 2004/03/28 08:58:37 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)newwin.c 8.3 (Berkeley) 7/27/94"; 36 #else 37 __RCSID("$NetBSD: newwin.c,v 1.42 2004/03/28 08:58:37 jdc Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <stdlib.h> 42 43 #include "curses.h" 44 #include "curses_private.h" 45 46 47 static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by, 48 int bx, int sub, int ispad); 49 static WINDOW *__subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, 50 int ispad); 51 52 /* 53 * derwin -- 54 * Create a new window in the same manner as subwin but (by, bx) 55 * are relative to the origin of window orig instead of absolute. 56 */ 57 WINDOW * 58 derwin(WINDOW *orig, int nlines, int ncols, int by, int bx) 59 { 60 61 return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx, 62 FALSE); 63 } 64 65 /* 66 * subpad -- 67 * Create a new pad in the same manner as subwin but (by, bx) 68 * are relative to the origin of window orig instead of absolute. 69 */ 70 WINDOW * 71 subpad(WINDOW *orig, int nlines, int ncols, int by, int bx) 72 { 73 74 return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx, 75 TRUE); 76 } 77 78 /* 79 * dupwin -- 80 * Create a copy of the given window. 81 */ 82 WINDOW * 83 dupwin(WINDOW *win) 84 { 85 WINDOW *new_one; 86 87 if ((new_one = __newwin(_cursesi_screen, win->maxy, win->maxx, 88 win->begy, win->begx, FALSE)) == NULL) 89 return NULL; 90 91 overwrite(win, new_one); 92 return new_one; 93 } 94 95 /* 96 * newwin -- 97 * Allocate space for and set up defaults for a new window. 98 */ 99 WINDOW * 100 newwin(int nlines, int ncols, int by, int bx) 101 { 102 return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE); 103 } 104 105 /* 106 * newpad -- 107 * Allocate space for and set up defaults for a new pad. 108 */ 109 WINDOW * 110 newpad(int nlines, int ncols) 111 { 112 if (nlines < 1 || ncols < 1) 113 return NULL; 114 return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE); 115 } 116 117 WINDOW * 118 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad) 119 { 120 WINDOW *win; 121 __LINE *lp; 122 int i, j; 123 int maxy, maxx; 124 __LDATA *sp; 125 126 if (by < 0 || bx < 0) 127 return (NULL); 128 129 maxy = nlines > 0 ? nlines : LINES - by + nlines; 130 maxx = ncols > 0 ? ncols : COLS - bx + ncols; 131 132 if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL) 133 return (NULL); 134 135 win->nextp = win; 136 win->ch_off = 0; 137 win->orig = NULL; 138 win->reqy = nlines; 139 win->reqx = ncols; 140 141 #ifdef DEBUG 142 __CTRACE("newwin: win->ch_off = %d\n", win->ch_off); 143 #endif 144 145 for (i = 0; i < maxy; i++) { 146 lp = win->lines[i]; 147 if (ispad) 148 lp->flags = __ISDIRTY; 149 else 150 lp->flags = 0; 151 for (sp = lp->line, j = 0; j < maxx; j++, sp++) { 152 sp->ch = ' '; 153 sp->bch = ' '; 154 sp->attr = 0; 155 if (__using_color) 156 sp->battr = __default_color; 157 else 158 sp->battr = 0; 159 } 160 lp->hash = __hash((char *)(void *)lp->line, 161 (size_t) (ncols * __LDATASIZE)); 162 } 163 return (win); 164 } 165 166 WINDOW * 167 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx) 168 { 169 170 return __subwin(orig, nlines, ncols, by, bx, FALSE); 171 } 172 173 WINDOW * 174 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad) 175 { 176 int i; 177 __LINE *lp; 178 WINDOW *win; 179 int maxy, maxx; 180 181 #ifdef DEBUG 182 __CTRACE("subwin: (%p, %d, %d, %d, %d, %d)\n", orig, nlines, ncols, 183 by, bx, ispad); 184 #endif 185 if (orig == NULL || orig->orig != NULL) 186 return NULL; 187 188 /* Make sure window fits inside the original one. */ 189 maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines; 190 maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols; 191 if (by < orig->begy || bx < orig->begx 192 || by + maxy > orig->maxy + orig->begy 193 || bx + maxx > orig->maxx + orig->begx) 194 return (NULL); 195 if ((win = __makenew(_cursesi_screen, maxy, maxx, 196 by, bx, 1, ispad)) == NULL) 197 return (NULL); 198 win->reqy = nlines; 199 win->reqx = ncols; 200 win->nextp = orig->nextp; 201 orig->nextp = win; 202 win->orig = orig; 203 204 /* Initialize flags here so that refresh can also use __set_subwin. */ 205 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) 206 lp->flags = 0; 207 __set_subwin(orig, win); 208 return (win); 209 } 210 /* 211 * This code is shared with mvwin(). 212 */ 213 void 214 __set_subwin(WINDOW *orig, WINDOW *win) 215 { 216 int i; 217 __LINE *lp, *olp; 218 219 win->ch_off = win->begx - orig->begx; 220 /* Point line pointers to line space. */ 221 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) { 222 win->lines[i] = lp; 223 olp = orig->lines[i + win->begy - orig->begy]; 224 #ifdef DEBUG 225 lp->sentinel = SENTINEL_VALUE; 226 #endif 227 lp->line = &olp->line[win->ch_off]; 228 lp->firstchp = &olp->firstch; 229 lp->lastchp = &olp->lastch; 230 lp->hash = __hash((char *)(void *)lp->line, 231 (size_t) (win->maxx * __LDATASIZE)); 232 } 233 234 #ifdef DEBUG 235 __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off); 236 #endif 237 } 238 /* 239 * __makenew -- 240 * Set up a window buffer and returns a pointer to it. 241 */ 242 static WINDOW * 243 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub, 244 int ispad) 245 { 246 WINDOW *win; 247 __LINE *lp; 248 struct __winlist *wlp, *wlp2; 249 int i; 250 251 252 #ifdef DEBUG 253 __CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx); 254 #endif 255 if (nlines <= 0 || ncols <= 0) 256 return NULL; 257 258 if ((win = malloc(sizeof(WINDOW))) == NULL) 259 return (NULL); 260 #ifdef DEBUG 261 __CTRACE("makenew: win = %p\n", win); 262 #endif 263 264 /* Set up line pointer array and line space. */ 265 if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) { 266 free(win); 267 return NULL; 268 } 269 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) { 270 free(win->lines); 271 free(win); 272 return NULL; 273 } 274 /* Don't allocate window and line space if it's a subwindow */ 275 if (sub) 276 win->wspace = NULL; 277 else { 278 /* 279 * Allocate window space in one chunk. 280 */ 281 if ((win->wspace = 282 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) { 283 free(win->lspace); 284 free(win->lines); 285 free(win); 286 return NULL; 287 } 288 /* 289 * Append window to window list. 290 */ 291 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) { 292 free(win->wspace); 293 free(win->lspace); 294 free(win->lines); 295 free(win); 296 return NULL; 297 } 298 wlp->winp = win; 299 wlp->nextp = NULL; 300 if (screen->winlistp == NULL) 301 screen->winlistp = wlp; 302 else { 303 wlp2 = screen->winlistp; 304 while (wlp2->nextp != NULL) 305 wlp2 = wlp2->nextp; 306 wlp2->nextp = wlp; 307 } 308 /* 309 * Point line pointers to line space, and lines themselves into 310 * window space. 311 */ 312 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) { 313 win->lines[i] = lp; 314 lp->line = &win->wspace[i * ncols]; 315 #ifdef DEBUG 316 lp->sentinel = SENTINEL_VALUE; 317 #endif 318 lp->firstchp = &lp->firstch; 319 lp->lastchp = &lp->lastch; 320 if (ispad) { 321 lp->firstch = 0; 322 lp->lastch = ncols; 323 } else { 324 lp->firstch = ncols; 325 lp->lastch = 0; 326 } 327 } 328 } 329 #ifdef DEBUG 330 __CTRACE("makenew: ncols = %d\n", ncols); 331 #endif 332 win->screen = screen; 333 win->cury = win->curx = 0; 334 win->maxy = nlines; 335 win->maxx = ncols; 336 win->reqy = nlines; 337 win->reqx = ncols; 338 339 win->begy = by; 340 win->begx = bx; 341 win->flags = (__IDLINE | __IDCHAR); 342 win->delay = -1; 343 win->wattr = 0; 344 win->bch = ' '; 345 if (__using_color) 346 win->battr = __default_color; 347 else 348 win->battr = 0; 349 win->scr_t = 0; 350 win->scr_b = win->maxy - 1; 351 if (ispad) { 352 win->flags |= __ISPAD; 353 win->pbegy = 0; 354 win->pbegx = 0; 355 win->sbegy = 0; 356 win->sbegx = 0; 357 win->smaxy = 0; 358 win->smaxx = 0; 359 } else 360 __swflags(win); 361 #ifdef DEBUG 362 __CTRACE("makenew: win->wattr = %08x\n", win->wattr); 363 __CTRACE("makenew: win->flags = %#.4x\n", win->flags); 364 __CTRACE("makenew: win->maxy = %d\n", win->maxy); 365 __CTRACE("makenew: win->maxx = %d\n", win->maxx); 366 __CTRACE("makenew: win->begy = %d\n", win->begy); 367 __CTRACE("makenew: win->begx = %d\n", win->begx); 368 __CTRACE("makenew: win->bch = %s\n", unctrl(win->bch)); 369 __CTRACE("makenew: win->battr = %08x\n", win->battr); 370 __CTRACE("makenew: win->scr_t = %d\n", win->scr_t); 371 __CTRACE("makenew: win->scr_b = %d\n", win->scr_b); 372 #endif 373 return (win); 374 } 375 376 void 377 __swflags(WINDOW *win) 378 { 379 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK); 380 if (win->begx + win->maxx == COLS && !(win->flags & __ISPAD)) { 381 win->flags |= __ENDLINE; 382 if (win->begx == 0 && win->maxy == LINES && win->begy == 0) 383 win->flags |= __FULLWIN; 384 if (win->begy + win->maxy == LINES) 385 win->flags |= __SCROLLWIN; 386 } 387 } 388