newwin.c revision 1.44 1 /* $NetBSD: newwin.c,v 1.44 2007/01/21 13:25:36 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.44 2007/01/21 13:25:36 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->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;
145
146 #ifdef DEBUG
147 __CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off);
148 #endif
149
150 for (i = 0; i < maxy; i++) {
151 lp = win->lines[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->ch = ' ';
158 sp->attr = 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(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n",
183 orig, nlines, ncols, 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->bch = orig->bch;
199 win->battr = orig->battr;
200 win->reqy = nlines;
201 win->reqx = ncols;
202 win->nextp = orig->nextp;
203 orig->nextp = win;
204 win->orig = orig;
205
206 /* Initialize flags here so that refresh can also use __set_subwin. */
207 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
208 lp->flags = 0;
209 __set_subwin(orig, win);
210 return (win);
211 }
212 /*
213 * This code is shared with mvwin().
214 */
215 void
216 __set_subwin(WINDOW *orig, WINDOW *win)
217 {
218 int i;
219 __LINE *lp, *olp;
220
221 win->ch_off = win->begx - orig->begx;
222 /* Point line pointers to line space. */
223 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
224 win->lines[i] = lp;
225 olp = orig->lines[i + win->begy - orig->begy];
226 #ifdef DEBUG
227 lp->sentinel = SENTINEL_VALUE;
228 #endif
229 lp->line = &olp->line[win->ch_off];
230 lp->firstchp = &olp->firstch;
231 lp->lastchp = &olp->lastch;
232 lp->hash = __hash((char *)(void *)lp->line,
233 (size_t) (win->maxx * __LDATASIZE));
234 }
235
236 #ifdef DEBUG
237 __CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n",
238 win->ch_off);
239 #endif
240 }
241 /*
242 * __makenew --
243 * Set up a window buffer and returns a pointer to it.
244 */
245 static WINDOW *
246 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub,
247 int ispad)
248 {
249 WINDOW *win;
250 __LINE *lp;
251 struct __winlist *wlp, *wlp2;
252 int i;
253
254
255 #ifdef DEBUG
256 __CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n",
257 nlines, ncols, by, bx);
258 #endif
259 if (nlines <= 0 || ncols <= 0)
260 return NULL;
261
262 if ((win = malloc(sizeof(WINDOW))) == NULL)
263 return (NULL);
264 #ifdef DEBUG
265 __CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win);
266 #endif
267
268 /* Set up line pointer array and line space. */
269 if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
270 free(win);
271 return NULL;
272 }
273 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
274 free(win->lines);
275 free(win);
276 return NULL;
277 }
278 /* Don't allocate window and line space if it's a subwindow */
279 if (sub)
280 win->wspace = NULL;
281 else {
282 /*
283 * Allocate window space in one chunk.
284 */
285 if ((win->wspace =
286 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
287 free(win->lspace);
288 free(win->lines);
289 free(win);
290 return NULL;
291 }
292 /*
293 * Append window to window list.
294 */
295 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
296 free(win->wspace);
297 free(win->lspace);
298 free(win->lines);
299 free(win);
300 return NULL;
301 }
302 wlp->winp = win;
303 wlp->nextp = NULL;
304 if (screen->winlistp == NULL)
305 screen->winlistp = wlp;
306 else {
307 wlp2 = screen->winlistp;
308 while (wlp2->nextp != NULL)
309 wlp2 = wlp2->nextp;
310 wlp2->nextp = wlp;
311 }
312 /*
313 * Point line pointers to line space, and lines themselves into
314 * window space.
315 */
316 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
317 win->lines[i] = lp;
318 lp->line = &win->wspace[i * ncols];
319 #ifdef DEBUG
320 lp->sentinel = SENTINEL_VALUE;
321 #endif
322 lp->firstchp = &lp->firstch;
323 lp->lastchp = &lp->lastch;
324 if (ispad) {
325 lp->firstch = 0;
326 lp->lastch = ncols;
327 } else {
328 lp->firstch = ncols;
329 lp->lastch = 0;
330 }
331 }
332 }
333 #ifdef DEBUG
334 __CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols);
335 #endif
336 win->screen = screen;
337 win->cury = win->curx = 0;
338 win->maxy = nlines;
339 win->maxx = ncols;
340 win->reqy = nlines;
341 win->reqx = ncols;
342
343 win->begy = by;
344 win->begx = bx;
345 win->flags = (__IDLINE | __IDCHAR);
346 win->delay = -1;
347 win->wattr = 0;
348 win->scr_t = 0;
349 win->scr_b = win->maxy - 1;
350 if (ispad) {
351 win->flags |= __ISPAD;
352 win->pbegy = 0;
353 win->pbegx = 0;
354 win->sbegy = 0;
355 win->sbegx = 0;
356 win->smaxy = 0;
357 win->smaxx = 0;
358 } else
359 __swflags(win);
360 #ifdef DEBUG
361 __CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr);
362 __CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags);
363 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy);
364 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx);
365 __CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy);
366 __CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx);
367 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t);
368 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b);
369 #endif
370 return (win);
371 }
372
373 void
374 __swflags(WINDOW *win)
375 {
376 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
377 if (win->begx + win->maxx == COLS && !(win->flags & __ISPAD)) {
378 win->flags |= __ENDLINE;
379 if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
380 win->flags |= __FULLWIN;
381 if (win->begy + win->maxy == LINES)
382 win->flags |= __SCROLLWIN;
383 }
384 }
385