1 /* $NetBSD: newwin.c,v 1.12 1999/06/23 03:26:02 christos 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.12 1999/06/23 03:26:02 christos Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <stdlib.h> 46 47 #include "curses.h" 48 49 #undef nl /* Don't need it here, and it interferes. */ 50 51 static WINDOW *__makenew __P((int, int, int, int, int)); 52 53 void __set_subwin __P((WINDOW *, WINDOW *)); 54 55 /* 56 * newwin -- 57 * Allocate space for and set up defaults for a new window. 58 */ 59 WINDOW * 60 newwin(nl, nc, by, bx) 61 int nl, nc, by, bx; 62 { 63 WINDOW *win; 64 __LINE *lp; 65 int i, j; 66 __LDATA *sp; 67 68 if (nl == 0) 69 nl = LINES - by; 70 if (nc == 0) 71 nc = COLS - bx; 72 73 if ((win = __makenew(nl, nc, by, bx, 0)) == NULL) 74 return (NULL); 75 76 win->nextp = win; 77 win->ch_off = 0; 78 win->orig = NULL; 79 win->delay = -1; 80 81 #ifdef DEBUG 82 __CTRACE("newwin: win->ch_off = %d\n", win->ch_off); 83 #endif 84 85 for (i = 0; i < nl; i++) { 86 lp = win->lines[i]; 87 lp->flags = 0; 88 for (sp = lp->line, j = 0; j < nc; j++, sp++) { 89 sp->ch = ' '; 90 sp->attr = 0; 91 } 92 lp->hash = __hash((char *)(void *)lp->line, 93 (int) (nc * __LDATASIZE)); 94 } 95 return (win); 96 } 97 98 WINDOW * 99 subwin(orig, nl, nc, by, bx) 100 WINDOW *orig; 101 int by, bx, nl, nc; 102 { 103 int i; 104 __LINE *lp; 105 WINDOW *win; 106 107 /* Make sure window fits inside the original one. */ 108 #ifdef DEBUG 109 __CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx); 110 #endif 111 if (by < orig->begy || bx < orig->begx 112 || by + nl > orig->maxy + orig->begy 113 || bx + nc > orig->maxx + orig->begx) 114 return (NULL); 115 if (nl == 0) 116 nl = orig->maxy + orig->begy - by; 117 if (nc == 0) 118 nc = orig->maxx + orig->begx - bx; 119 if ((win = __makenew(nl, nc, by, bx, 1)) == NULL) 120 return (NULL); 121 win->nextp = orig->nextp; 122 orig->nextp = win; 123 win->orig = orig; 124 125 /* Initialize flags here so that refresh can also use __set_subwin. */ 126 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) 127 lp->flags = 0; 128 __set_subwin(orig, win); 129 return (win); 130 } 131 /* 132 * This code is shared with mvwin(). 133 */ 134 void 135 __set_subwin(orig, win) 136 WINDOW *orig, *win; 137 { 138 int i; 139 __LINE *lp, *olp; 140 141 win->ch_off = win->begx - orig->begx; 142 /* Point line pointers to line space. */ 143 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) { 144 win->lines[i] = lp; 145 olp = orig->lines[i + win->begy - orig->begy]; 146 lp->line = &olp->line[win->begx - orig->begx]; 147 lp->firstchp = &olp->firstch; 148 lp->lastchp = &olp->lastch; 149 lp->hash = __hash((char *)(void *)lp->line, 150 (int) (win->maxx * __LDATASIZE)); 151 } 152 153 #ifdef DEBUG 154 __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off); 155 #endif 156 } 157 /* 158 * __makenew -- 159 * Set up a window buffer and returns a pointer to it. 160 */ 161 static WINDOW * 162 __makenew(nl, nc, by, bx, sub) 163 int by, bx, nl, nc; 164 int sub; 165 { 166 WINDOW *win; 167 __LINE *lp; 168 int i; 169 170 171 #ifdef DEBUG 172 __CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx); 173 #endif 174 if ((win = malloc(sizeof(*win))) == NULL) 175 return (NULL); 176 #ifdef DEBUG 177 __CTRACE("makenew: nl = %d\n", nl); 178 #endif 179 180 /* Set up line pointer array and line space. */ 181 if ((win->lines = malloc(nl * sizeof(__LINE *))) == NULL) { 182 free(win); 183 return NULL; 184 } 185 if ((win->lspace = malloc(nl * sizeof(__LINE))) == NULL) { 186 free(win); 187 free(win->lines); 188 return NULL; 189 } 190 /* Don't allocate window and line space if it's a subwindow */ 191 if (!sub) { 192 /* 193 * Allocate window space in one chunk. 194 */ 195 if ((win->wspace = 196 malloc(nc * nl * sizeof(__LDATA))) == NULL) { 197 free(win->lines); 198 free(win->lspace); 199 free(win); 200 return NULL; 201 } 202 /* 203 * Point line pointers to line space, and lines themselves into 204 * window space. 205 */ 206 for (lp = win->lspace, i = 0; i < nl; i++, lp++) { 207 win->lines[i] = lp; 208 lp->line = &win->wspace[i * nc]; 209 lp->firstchp = &lp->firstch; 210 lp->lastchp = &lp->lastch; 211 lp->firstch = 0; 212 lp->lastch = 0; 213 } 214 } 215 #ifdef DEBUG 216 __CTRACE("makenew: nc = %d\n", nc); 217 #endif 218 win->cury = win->curx = 0; 219 win->maxy = nl; 220 win->maxx = nc; 221 222 win->begy = by; 223 win->begx = bx; 224 win->flags = 0; 225 __swflags(win); 226 #ifdef DEBUG 227 __CTRACE("makenew: win->flags = %0.2o\n", win->flags); 228 __CTRACE("makenew: win->maxy = %d\n", win->maxy); 229 __CTRACE("makenew: win->maxx = %d\n", win->maxx); 230 __CTRACE("makenew: win->begy = %d\n", win->begy); 231 __CTRACE("makenew: win->begx = %d\n", win->begx); 232 #endif 233 return (win); 234 } 235 236 void 237 __swflags(win) 238 WINDOW *win; 239 { 240 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK); 241 if (win->begx + win->maxx == COLS) { 242 win->flags |= __ENDLINE; 243 if (win->begx == 0 && win->maxy == LINES && win->begy == 0) 244 win->flags |= __FULLWIN; 245 if (win->begy + win->maxy == LINES) 246 win->flags |= __SCROLLWIN; 247 } 248 } 249