Boot-to-ramdisk image generation scripts
[minix3.git] / lib / libcurses / newwin.c
blob4084b66e971b4e9bf411460d4daa89ead30d6953
1 /* $NetBSD: newwin.c,v 1.47 2009/07/22 16:57:15 roy Exp $ */
3 /*
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.47 2009/07/22 16:57:15 roy Exp $");
38 #endif
39 #endif /* not lint */
41 #include <stdlib.h>
43 #include "curses.h"
44 #include "curses_private.h"
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);
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.
57 WINDOW *
58 derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
61 return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
62 FALSE);
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.
70 WINDOW *
71 subpad(WINDOW *orig, int nlines, int ncols, int by, int bx)
74 return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
75 TRUE);
79 * dupwin --
80 * Create a copy of the given window.
82 WINDOW *
83 dupwin(WINDOW *win)
85 WINDOW *new_one;
87 if ((new_one = __newwin(_cursesi_screen, win->maxy, win->maxx,
88 win->begy, win->begx, FALSE)) == NULL)
89 return NULL;
91 overwrite(win, new_one);
92 return new_one;
96 * newwin --
97 * Allocate space for and set up defaults for a new window.
99 WINDOW *
100 newwin(int nlines, int ncols, int by, int bx)
102 return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE);
106 * newpad --
107 * Allocate space for and set up defaults for a new pad.
109 WINDOW *
110 newpad(int nlines, int ncols)
112 if (nlines < 1 || ncols < 1)
113 return NULL;
114 return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE);
117 WINDOW *
118 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad)
120 WINDOW *win;
121 __LINE *lp;
122 int i, j;
123 int maxy, maxx;
124 __LDATA *sp;
126 if (by < 0 || bx < 0)
127 return (NULL);
129 maxy = nlines > 0 ? nlines : LINES - by + nlines;
130 maxx = ncols > 0 ? ncols : COLS - bx + ncols;
132 if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL)
133 return (NULL);
135 win->bch = ' ';
136 if (__using_color)
137 win->battr = __default_color;
138 else
139 win->battr = 0;
140 win->nextp = win;
141 win->ch_off = 0;
142 win->orig = NULL;
143 win->reqy = nlines;
144 win->reqx = ncols;
146 #ifdef DEBUG
147 __CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off);
148 #endif
150 for (i = 0; i < maxy; i++) {
151 lp = win->alines[i];
152 if (ispad)
153 lp->flags = __ISDIRTY;
154 else
155 lp->flags = 0;
156 for (sp = lp->line, j = 0; j < maxx; j++, sp++) {
157 sp->attr = 0;
158 #ifndef HAVE_WCHAR
159 sp->ch = win->bch;
160 #else
161 sp->ch = ( wchar_t )btowc(( int ) win->bch );
162 sp->nsp = NULL;
163 SET_WCOL( *sp, 1 );
164 #endif /* HAVE_WCHAR */
166 lp->hash = __hash((char *)(void *)lp->line,
167 (size_t) (ncols * __LDATASIZE));
169 return (win);
172 WINDOW *
173 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
176 return __subwin(orig, nlines, ncols, by, bx, FALSE);
179 static WINDOW *
180 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad)
182 int i;
183 __LINE *lp;
184 WINDOW *win;
185 int maxy, maxx;
187 #ifdef DEBUG
188 __CTRACE(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n",
189 orig, nlines, ncols, by, bx, ispad);
190 #endif
191 if (orig == NULL || orig->orig != NULL)
192 return NULL;
194 /* Make sure window fits inside the original one. */
195 maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines;
196 maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols;
197 if (by < orig->begy || bx < orig->begx
198 || by + maxy > orig->maxy + orig->begy
199 || bx + maxx > orig->maxx + orig->begx)
200 return (NULL);
201 if ((win = __makenew(_cursesi_screen, maxy, maxx,
202 by, bx, 1, ispad)) == NULL)
203 return (NULL);
204 win->bch = orig->bch;
205 win->battr = orig->battr;
206 win->reqy = nlines;
207 win->reqx = ncols;
208 win->nextp = orig->nextp;
209 orig->nextp = win;
210 win->orig = orig;
212 /* Initialize flags here so that refresh can also use __set_subwin. */
213 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
214 lp->flags = 0;
215 __set_subwin(orig, win);
216 return (win);
219 * This code is shared with mvwin().
221 void
222 __set_subwin(WINDOW *orig, WINDOW *win)
224 int i;
225 __LINE *lp, *olp;
226 #ifdef HAVE_WCHAR
227 __LDATA *cp;
228 int j;
229 nschar_t *np;
230 #endif /* HAVE_WCHAR */
232 win->ch_off = win->begx - orig->begx;
233 /* Point line pointers to line space. */
234 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
235 win->alines[i] = lp;
236 olp = orig->alines[i + win->begy - orig->begy];
237 #ifdef DEBUG
238 lp->sentinel = SENTINEL_VALUE;
239 #endif
240 lp->line = &olp->line[win->ch_off];
241 lp->firstchp = &olp->firstch;
242 lp->lastchp = &olp->lastch;
243 #ifndef HAVE_WCHAR
244 lp->hash = __hash((char *)(void *)lp->line,
245 (size_t) (win->maxx * __LDATASIZE));
246 #else
247 for ( cp = lp->line, j = 0; j < win->maxx; j++, cp++ ) {
248 lp->hash = __hash_more( &cp->ch,
249 sizeof( wchar_t ), lp->hash );
250 lp->hash = __hash_more( &cp->attr,
251 sizeof( wchar_t ), lp->hash );
252 if ( cp->nsp ) {
253 np = cp->nsp;
254 while ( np ) {
255 lp->hash = __hash_more( &np->ch,
256 sizeof( wchar_t ), lp->hash );
257 np = np->next;
261 #endif /* HAVE_WCHAR */
264 #ifdef DEBUG
265 __CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n",
266 win->ch_off);
267 #endif
270 * __makenew --
271 * Set up a window buffer and returns a pointer to it.
273 static WINDOW *
274 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub,
275 int ispad)
277 WINDOW *win;
278 __LINE *lp;
279 struct __winlist *wlp, *wlp2;
280 int i;
283 #ifdef DEBUG
284 __CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n",
285 nlines, ncols, by, bx);
286 #endif
287 if (nlines <= 0 || ncols <= 0)
288 return NULL;
290 if ((win = malloc(sizeof(WINDOW))) == NULL)
291 return (NULL);
292 #ifdef DEBUG
293 __CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win);
294 #endif
296 /* Set up line pointer array and line space. */
297 if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) {
298 free(win);
299 return NULL;
301 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
302 free(win->alines);
303 free(win);
304 return NULL;
306 /* Don't allocate window and line space if it's a subwindow */
307 if (sub)
308 win->wspace = NULL;
309 else {
311 * Allocate window space in one chunk.
313 if ((win->wspace =
314 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
315 free(win->lspace);
316 free(win->alines);
317 free(win);
318 return NULL;
321 * Append window to window list.
323 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
324 free(win->wspace);
325 free(win->lspace);
326 free(win->alines);
327 free(win);
328 return NULL;
330 wlp->winp = win;
331 wlp->nextp = NULL;
332 if (screen->winlistp == NULL)
333 screen->winlistp = wlp;
334 else {
335 wlp2 = screen->winlistp;
336 while (wlp2->nextp != NULL)
337 wlp2 = wlp2->nextp;
338 wlp2->nextp = wlp;
341 * Point line pointers to line space, and lines themselves into
342 * window space.
344 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
345 win->alines[i] = lp;
346 lp->line = &win->wspace[i * ncols];
347 #ifdef DEBUG
348 lp->sentinel = SENTINEL_VALUE;
349 #endif
350 lp->firstchp = &lp->firstch;
351 lp->lastchp = &lp->lastch;
352 if (ispad) {
353 lp->firstch = 0;
354 lp->lastch = ncols;
355 } else {
356 lp->firstch = ncols;
357 lp->lastch = 0;
361 #ifdef DEBUG
362 __CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols);
363 #endif
364 win->screen = screen;
365 win->cury = win->curx = 0;
366 win->maxy = nlines;
367 win->maxx = ncols;
368 win->reqy = nlines;
369 win->reqx = ncols;
371 win->begy = by;
372 win->begx = bx;
373 win->flags = (__IDLINE | __IDCHAR);
374 win->delay = -1;
375 win->wattr = 0;
376 #ifdef HAVE_WCHAR
377 win->bnsp = NULL;
378 SET_BGWCOL( *win, 1 );
379 #endif /* HAVE_WCHAR */
380 win->scr_t = 0;
381 win->scr_b = win->maxy - 1;
382 if (ispad) {
383 win->flags |= __ISPAD;
384 win->pbegy = 0;
385 win->pbegx = 0;
386 win->sbegy = 0;
387 win->sbegx = 0;
388 win->smaxy = 0;
389 win->smaxx = 0;
390 } else
391 __swflags(win);
392 #ifdef DEBUG
393 __CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr);
394 __CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags);
395 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy);
396 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx);
397 __CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy);
398 __CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx);
399 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t);
400 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b);
401 #endif
402 return (win);
405 void
406 __swflags(WINDOW *win)
408 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
409 if (win->begx + win->maxx == COLS && !(win->flags & __ISPAD)) {
410 win->flags |= __ENDLINE;
411 if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
412 win->flags |= __FULLWIN;
413 if (win->begy + win->maxy == LINES)
414 win->flags |= __SCROLLWIN;