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