Commit | Line | Data |
---|---|---|
87c6fcf8 | 1 | /* |
2f14f200 KB |
2 | * Copyright (c) 1981 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
e21c485a | 5 | * %sccs.include.redist.c% |
87c6fcf8 DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
fd541d22 | 9 | static char sccsid[] = "@(#)newwin.c 5.16 (Berkeley) %G%"; |
bfe08488 | 10 | #endif /* not lint */ |
87c6fcf8 | 11 | |
bfe08488 KB |
12 | #include <curses.h> |
13 | #include <stdlib.h> | |
f1d89179 | 14 | |
bfe08488 | 15 | #undef nl /* Don't need it here, and it interferes. */ |
f1d89179 | 16 | |
d6048d89 | 17 | static WINDOW *__makenew __P((int, int, int, int, int)); |
0c5c5188 EA |
18 | |
19 | void __set_subwin __P((WINDOW *, WINDOW *)); | |
f1d89179 | 20 | |
bfe08488 KB |
21 | /* |
22 | * newwin -- | |
23 | * Allocate space for and set up defaults for a new window. | |
24 | */ | |
f1d89179 | 25 | WINDOW * |
0c5c5188 EA |
26 | newwin(nl, nc, by, bx) |
27 | register int nl, nc, by, bx; | |
f1d89179 | 28 | { |
bfe08488 | 29 | register WINDOW *win; |
1c04a491 | 30 | register __LINE *lp; |
0c5c5188 | 31 | register int i, j; |
1c04a491 | 32 | register __LDATA *sp; |
f1d89179 | 33 | |
f1d89179 KA |
34 | if (nl == 0) |
35 | nl = LINES - by; | |
36 | if (nc == 0) | |
37 | nc = COLS - bx; | |
0c5c5188 | 38 | |
d6048d89 | 39 | if ((win = __makenew(nl, nc, by, bx, 0)) == NULL) |
bfe08488 | 40 | return (NULL); |
0c5c5188 EA |
41 | |
42 | win->nextp = win; | |
43 | win->ch_off = 0; | |
44 | ||
bfe08488 | 45 | #ifdef DEBUG |
0c5c5188 | 46 | __TRACE("newwin: win->ch_off = %d\n", win->ch_off); |
bfe08488 | 47 | #endif |
0c5c5188 | 48 | |
1c04a491 | 49 | for (lp = win->lines[0], i = 0; i < nl; i++, lp = win->lines[i]) { |
0c5c5188 | 50 | lp->flags = 0; |
1c04a491 EA |
51 | for (sp = lp->line, j = 0; j < nc; j++, sp++) { |
52 | sp->ch = ' '; | |
53 | sp->attr = 0; | |
1589e7d3 | 54 | } |
99eb2ba6 | 55 | lp->hash = __hash((char *) lp->line, nc * __LDATASIZE); |
0c5c5188 | 56 | } |
bfe08488 | 57 | return (win); |
f1d89179 KA |
58 | } |
59 | ||
60 | WINDOW * | |
0c5c5188 | 61 | subwin(orig, nl, nc, by, bx) |
bfe08488 | 62 | register WINDOW *orig; |
0c5c5188 | 63 | register int by, bx, nl, nc; |
52f06ac3 | 64 | { |
bfe08488 | 65 | register WINDOW *win; |
f1d89179 | 66 | |
bfe08488 KB |
67 | /* Make sure window fits inside the original one. */ |
68 | #ifdef DEBUG | |
69 | __TRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx); | |
70 | #endif | |
0c5c5188 EA |
71 | if (by < orig->begy || bx < orig->begx |
72 | || by + nl > orig->maxy + orig->begy | |
73 | || bx + nc > orig->maxx + orig->begx) | |
bfe08488 | 74 | return (NULL); |
f1d89179 | 75 | if (nl == 0) |
0c5c5188 | 76 | nl = orig->maxy + orig->begy - by; |
f1d89179 | 77 | if (nc == 0) |
0c5c5188 | 78 | nc = orig->maxx + orig->begx - bx; |
d6048d89 | 79 | if ((win = __makenew(nl, nc, by, bx, 1)) == NULL) |
bfe08488 | 80 | return (NULL); |
0c5c5188 EA |
81 | win->nextp = orig->nextp; |
82 | orig->nextp = win; | |
83 | win->orig = orig; | |
bfe08488 KB |
84 | __set_subwin(orig, win); |
85 | return (win); | |
f1d89179 KA |
86 | } |
87 | ||
52f06ac3 | 88 | /* |
bfe08488 | 89 | * This code is shared with mvwin(). |
52f06ac3 | 90 | */ |
a2af4b61 KB |
91 | void |
92 | __set_subwin(orig, win) | |
bfe08488 | 93 | register WINDOW *orig, *win; |
52f06ac3 | 94 | { |
fd541d22 EA |
95 | int i; |
96 | __LINE *lp, *olp; | |
97 | ||
d6048d89 | 98 | win->ch_off = win->begx - orig->begx; |
fd541d22 EA |
99 | /* Point line pointers to line space. */ |
100 | for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) { | |
101 | win->lines[i] = lp; | |
102 | olp = orig->lines[i + win->begy]; | |
103 | lp->line = &olp->line[win->begx]; | |
104 | lp->firstchp = &olp->firstch; | |
105 | lp->lastchp = &olp->lastch; | |
106 | lp->flags = 0; | |
107 | lp->hash = __hash((char *) lp->line, win->maxx * __LDATASIZE); | |
108 | } | |
52f06ac3 | 109 | |
bfe08488 | 110 | #ifdef DEBUG |
0c5c5188 | 111 | __TRACE("__set_subwin: win->ch_off = %d\n", win->ch_off); |
bfe08488 | 112 | #endif |
52f06ac3 JB |
113 | } |
114 | ||
f1d89179 | 115 | /* |
d6048d89 | 116 | * __makenew -- |
bfe08488 | 117 | * Set up a window buffer and returns a pointer to it. |
f1d89179 KA |
118 | */ |
119 | static WINDOW * | |
d6048d89 | 120 | __makenew(nl, nc, by, bx, sub) |
0c5c5188 | 121 | register int by, bx, nl, nc; |
d6048d89 | 122 | int sub; |
bfe08488 KB |
123 | { |
124 | register WINDOW *win; | |
1c04a491 | 125 | register __LINE *lp; |
0c5c5188 | 126 | int i; |
1c04a491 | 127 | |
f1d89179 | 128 | |
bfe08488 KB |
129 | #ifdef DEBUG |
130 | __TRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx); | |
131 | #endif | |
132 | if ((win = malloc(sizeof(*win))) == NULL) | |
133 | return (NULL); | |
134 | #ifdef DEBUG | |
135 | __TRACE("makenew: nl = %d\n", nl); | |
136 | #endif | |
0c5c5188 | 137 | |
fd541d22 EA |
138 | /* |
139 | * Set up line pointer array and line space. | |
140 | */ | |
141 | if ((win->lines = malloc (nl * sizeof(__LINE *))) == NULL) { | |
142 | free(win); | |
143 | return NULL; | |
144 | } | |
145 | if ((win->lspace = malloc (nl * sizeof(__LINE))) == NULL) { | |
146 | free (win); | |
147 | free (win->lines); | |
148 | return NULL; | |
149 | } | |
150 | ||
151 | /* Don't allocate window and line space if it's a subwindow */ | |
d6048d89 | 152 | if (!sub) { |
d6048d89 EA |
153 | /* |
154 | * Allocate window space in one chunk. | |
155 | */ | |
156 | if ((win->wspace = | |
157 | malloc(nc * nl * sizeof(__LDATA))) == NULL) { | |
158 | free(win->lines); | |
159 | free(win->lspace); | |
160 | free(win); | |
161 | return NULL; | |
162 | } | |
163 | ||
164 | /* | |
165 | * Point line pointers to line space, and lines themselves into | |
166 | * window space. | |
167 | */ | |
168 | for (lp = win->lspace, i = 0; i < nl; i++, lp++) { | |
169 | win->lines[i] = lp; | |
170 | lp->line = &win->wspace[i * nc]; | |
171 | lp->firstchp = &lp->firstch; | |
172 | lp->lastchp = &lp->lastch; | |
173 | lp->firstch = 0; | |
174 | lp->lastch = 0; | |
175 | } | |
0c5c5188 | 176 | } |
bfe08488 KB |
177 | #ifdef DEBUG |
178 | __TRACE("makenew: nc = %d\n", nc); | |
179 | #endif | |
0c5c5188 EA |
180 | win->cury = win->curx = 0; |
181 | win->maxy = nl; | |
182 | win->maxx = nc; | |
183 | ||
184 | win->begy = by; | |
185 | win->begx = bx; | |
186 | win->flags = 0; | |
bfe08488 KB |
187 | __swflags(win); |
188 | #ifdef DEBUG | |
0c5c5188 EA |
189 | __TRACE("makenew: win->flags = %0.2o\n", win->flags); |
190 | __TRACE("makenew: win->maxy = %d\n", win->maxy); | |
191 | __TRACE("makenew: win->maxx = %d\n", win->maxx); | |
192 | __TRACE("makenew: win->begy = %d\n", win->begy); | |
193 | __TRACE("makenew: win->begx = %d\n", win->begx); | |
bfe08488 KB |
194 | #endif |
195 | return (win); | |
f1d89179 | 196 | } |
52f06ac3 | 197 | |
da7184c5 | 198 | void |
bfe08488 KB |
199 | __swflags(win) |
200 | register WINDOW *win; | |
52f06ac3 | 201 | { |
0c5c5188 EA |
202 | win->flags &= |
203 | ~(__ENDLINE | __FULLLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK); | |
204 | if (win->begx + win->maxx == COLS) { | |
205 | win->flags |= __ENDLINE; | |
206 | if (win->begx == 0) { | |
52f06ac3 | 207 | if (AL && DL) |
0c5c5188 EA |
208 | win->flags |= __FULLLINE; |
209 | if (win->maxy == LINES && win->begy == 0) | |
210 | win->flags |= __FULLWIN; | |
52f06ac3 | 211 | } |
0c5c5188 EA |
212 | if (win->begy + win->maxy == LINES) |
213 | win->flags |= __SCROLLWIN; | |
52f06ac3 JB |
214 | } |
215 | } |