vendor/NCURSES: Remove version tag.
[dragonfly.git] / contrib / ncurses / ncurses / base / lib_newwin.c
1 /****************************************************************************
2  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34 /*
35 **      lib_newwin.c
36 **
37 **      The routines newwin(), subwin() and their dependent
38 **
39 */
40
41 #include <curses.priv.h>
42
43 MODULE_ID("$Id: lib_newwin.c,v 1.34 2002/08/18 00:12:30 tom Exp $")
44
45 static WINDOW *
46 remove_window_from_screen(WINDOW *win)
47 {
48     SCREEN **scan = &_nc_screen_chain;
49
50     while (*scan) {
51         SCREEN *sp = *scan;
52         if (sp->_curscr == win) {
53             sp->_curscr = 0;
54             if (win == curscr)
55                 curscr = 0;
56         } else if (sp->_stdscr == win) {
57             sp->_stdscr = 0;
58             if (win == stdscr)
59                 stdscr = 0;
60         } else if (sp->_newscr == win) {
61             sp->_newscr = 0;
62             if (win == newscr)
63                 newscr = 0;
64         } else {
65             scan = &(*scan)->_next_screen;
66             continue;
67         }
68         break;
69     }
70
71     return 0;
72 }
73
74 NCURSES_EXPORT(int)
75 _nc_freewin(WINDOW *win)
76 {
77     WINDOWLIST *p, *q;
78     int i;
79     int result = ERR;
80
81     if (win != 0) {
82         for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
83             if (&(p->win) == win) {
84                 remove_window_from_screen(win);
85                 if (q == 0)
86                     _nc_windows = p->next;
87                 else
88                     q->next = p->next;
89
90                 if (!(win->_flags & _SUBWIN)) {
91                     for (i = 0; i <= win->_maxy; i++)
92                         FreeIfNeeded(win->_line[i].text);
93                 }
94                 free(win->_line);
95                 free(p);
96
97                 result = OK;
98                 T(("...deleted win=%p", win));
99                 break;
100             }
101         }
102     }
103     return result;
104 }
105
106 NCURSES_EXPORT(WINDOW *)
107 newwin(int num_lines, int num_columns, int begy, int begx)
108 {
109     WINDOW *win;
110     NCURSES_CH_T *ptr;
111     int i;
112
113     T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
114
115     if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
116         returnWin(0);
117
118     if (num_lines == 0)
119         num_lines = SP->_lines_avail - begy;
120     if (num_columns == 0)
121         num_columns = screen_columns - begx;
122
123     if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail)
124         returnWin(0);
125
126     if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
127         returnWin(0);
128
129     for (i = 0; i < num_lines; i++) {
130         win->_line[i].text = typeCalloc(NCURSES_CH_T, (unsigned) num_columns);
131         if (win->_line[i].text == 0) {
132             (void) _nc_freewin(win);
133             returnWin(0);
134         }
135         for (ptr = win->_line[i].text;
136              ptr < win->_line[i].text + num_columns;
137              ptr++)
138             SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
139     }
140
141     returnWin(win);
142 }
143
144 NCURSES_EXPORT(WINDOW *)
145 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
146 {
147     WINDOW *win;
148     int i;
149     int flags = _SUBWIN;
150
151     T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
152        begy, begx));
153
154     /*
155      * make sure window fits inside the original one
156      */
157     if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
158         returnWin(0);
159     if (begy + num_lines > orig->_maxy + 1
160         || begx + num_columns > orig->_maxx + 1)
161         returnWin(0);
162
163     if (num_lines == 0)
164         num_lines = orig->_maxy + 1 - begy;
165
166     if (num_columns == 0)
167         num_columns = orig->_maxx + 1 - begx;
168
169     if (orig->_flags & _ISPAD)
170         flags |= _ISPAD;
171
172     if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy,
173                            orig->_begx + begx, flags)) == 0)
174         returnWin(0);
175
176     win->_pary = begy;
177     win->_parx = begx;
178     win->_attrs = orig->_attrs;
179     win->_nc_bkgd = orig->_nc_bkgd;
180
181     for (i = 0; i < num_lines; i++)
182         win->_line[i].text = &orig->_line[begy++].text[begx];
183
184     win->_parent = orig;
185
186     returnWin(win);
187 }
188
189 NCURSES_EXPORT(WINDOW *)
190 subwin(WINDOW *w, int l, int c, int y, int x)
191 {
192     T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
193     T(("parent has begy = %d, begx = %d", w->_begy, w->_begx));
194
195     returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
196 }
197
198 static bool
199 dimension_limit(int value)
200 {
201     NCURSES_SIZE_T test = value;
202     return (test == value && value > 0);
203 }
204
205 NCURSES_EXPORT(WINDOW *)
206 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
207 {
208     int i;
209     WINDOWLIST *wp;
210     WINDOW *win;
211     bool is_pad = (flags & _ISPAD);
212
213     T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx));
214
215     if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
216         return 0;
217
218     if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
219         return 0;
220
221     win = &(wp->win);
222
223     if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
224         free(win);
225         return 0;
226     }
227
228     win->_curx = 0;
229     win->_cury = 0;
230     win->_maxy = num_lines - 1;
231     win->_maxx = num_columns - 1;
232     win->_begy = begy;
233     win->_begx = begx;
234     win->_yoffset = SP->_topstolen;
235
236     win->_flags = flags;
237     win->_attrs = A_NORMAL;
238     SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
239
240     win->_clear = is_pad ? FALSE : (num_lines == screen_lines
241                                     && num_columns == screen_columns);
242     win->_idlok = FALSE;
243     win->_idcok = TRUE;
244     win->_scroll = FALSE;
245     win->_leaveok = FALSE;
246     win->_use_keypad = FALSE;
247     win->_delay = -1;
248     win->_immed = FALSE;
249     win->_sync = 0;
250     win->_parx = -1;
251     win->_pary = -1;
252     win->_parent = 0;
253
254     win->_regtop = 0;
255     win->_regbottom = num_lines - 1;
256
257     win->_pad._pad_y = -1;
258     win->_pad._pad_x = -1;
259     win->_pad._pad_top = -1;
260     win->_pad._pad_bottom = -1;
261     win->_pad._pad_left = -1;
262     win->_pad._pad_right = -1;
263
264     for (i = 0; i < num_lines; i++) {
265         /*
266          * This used to do
267          *
268          * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
269          *
270          * which marks the whole window unchanged.  That's how
271          * SVr1 curses did it, but SVr4 curses marks the whole new
272          * window changed.
273          *
274          * With the old SVr1-like code, say you have stdscr full of
275          * characters, then create a new window with newwin(),
276          * then do a printw(win, "foo        ");, the trailing spaces are
277          * completely ignored by the following refreshes.  So, you
278          * get "foojunkjunk" on the screen instead of "foo        " as
279          * you actually intended.
280          *
281          * SVr4 doesn't do this.  Instead the spaces are actually written.
282          * So that's how we want ncurses to behave.
283          */
284         win->_line[i].firstchar = 0;
285         win->_line[i].lastchar = num_columns - 1;
286
287         if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
288     }
289
290     if (!is_pad && (begx + num_columns == screen_columns)) {
291         win->_flags |= _ENDLINE;
292
293         if (begx == 0 && num_lines == screen_lines && begy == 0)
294             win->_flags |= _FULLWIN;
295
296         if (begy + num_lines == screen_lines)
297             win->_flags |= _SCROLLWIN;
298     }
299
300     wp->next = _nc_windows;
301     _nc_windows = wp;
302
303     T((T_CREATE("window %p"), win));
304
305     return (win);
306 }