]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/ncurses/base/lib_newwin.c
This commit was generated by cvs2svn to compensate for changes in r160157,
[FreeBSD/FreeBSD.git] / contrib / ncurses / ncurses / base / lib_newwin.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000,2001 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.33 2001/12/19 01:06:30 tom Exp $")
44
45 NCURSES_EXPORT(int)
46 _nc_freewin(WINDOW *win)
47 {
48     WINDOWLIST *p, *q;
49     int i;
50     int result = ERR;
51
52     if (win != 0) {
53         for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
54             if (&(p->win) == win) {
55                 if (q == 0)
56                     _nc_windows = p->next;
57                 else
58                     q->next = p->next;
59
60                 if (!(win->_flags & _SUBWIN)) {
61                     for (i = 0; i <= win->_maxy; i++)
62                         FreeIfNeeded(win->_line[i].text);
63                 }
64                 free(win->_line);
65                 free(p);
66
67                 if (win == curscr)
68                     curscr = 0;
69                 if (win == stdscr)
70                     stdscr = 0;
71                 if (win == newscr)
72                     newscr = 0;
73
74                 result = OK;
75                 T(("...deleted win=%p", win));
76                 break;
77             }
78         }
79     }
80     return result;
81 }
82
83 NCURSES_EXPORT(WINDOW *)
84 newwin(int num_lines, int num_columns, int begy, int begx)
85 {
86     WINDOW *win;
87     NCURSES_CH_T *ptr;
88     int i;
89
90     T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
91
92     if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
93         returnWin(0);
94
95     if (num_lines == 0)
96         num_lines = SP->_lines_avail - begy;
97     if (num_columns == 0)
98         num_columns = screen_columns - begx;
99
100     if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail)
101         returnWin(0);
102
103     if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
104         returnWin(0);
105
106     for (i = 0; i < num_lines; i++) {
107         win->_line[i].text = typeCalloc(NCURSES_CH_T, (unsigned) num_columns);
108         if (win->_line[i].text == 0) {
109             (void) _nc_freewin(win);
110             returnWin(0);
111         }
112         for (ptr = win->_line[i].text;
113              ptr < win->_line[i].text + num_columns;
114              ptr++)
115             SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
116     }
117
118     returnWin(win);
119 }
120
121 NCURSES_EXPORT(WINDOW *)
122 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
123 {
124     WINDOW *win;
125     int i;
126     int flags = _SUBWIN;
127
128     T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
129        begy, begx));
130
131     /*
132      * make sure window fits inside the original one
133      */
134     if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
135         returnWin(0);
136     if (begy + num_lines > orig->_maxy + 1
137         || begx + num_columns > orig->_maxx + 1)
138         returnWin(0);
139
140     if (num_lines == 0)
141         num_lines = orig->_maxy + 1 - begy;
142
143     if (num_columns == 0)
144         num_columns = orig->_maxx + 1 - begx;
145
146     if (orig->_flags & _ISPAD)
147         flags |= _ISPAD;
148
149     if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy,
150                            orig->_begx + begx, flags)) == 0)
151         returnWin(0);
152
153     win->_pary = begy;
154     win->_parx = begx;
155     win->_attrs = orig->_attrs;
156     win->_nc_bkgd = orig->_nc_bkgd;
157
158     for (i = 0; i < num_lines; i++)
159         win->_line[i].text = &orig->_line[begy++].text[begx];
160
161     win->_parent = orig;
162
163     returnWin(win);
164 }
165
166 NCURSES_EXPORT(WINDOW *)
167 subwin(WINDOW *w, int l, int c, int y, int x)
168 {
169     T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
170     T(("parent has begy = %d, begx = %d", w->_begy, w->_begx));
171
172     returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
173 }
174
175 static bool
176 dimension_limit(int value)
177 {
178     NCURSES_SIZE_T test = value;
179     return (test == value && value > 0);
180 }
181
182 NCURSES_EXPORT(WINDOW *)
183 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
184 {
185     int i;
186     WINDOWLIST *wp;
187     WINDOW *win;
188     bool is_pad = (flags & _ISPAD);
189
190     T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx));
191
192     if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
193         return 0;
194
195     if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
196         return 0;
197
198     win = &(wp->win);
199
200     if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
201         free(win);
202         return 0;
203     }
204
205     win->_curx = 0;
206     win->_cury = 0;
207     win->_maxy = num_lines - 1;
208     win->_maxx = num_columns - 1;
209     win->_begy = begy;
210     win->_begx = begx;
211     win->_yoffset = SP->_topstolen;
212
213     win->_flags = flags;
214     win->_attrs = A_NORMAL;
215     SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
216
217     win->_clear = is_pad ? FALSE : (num_lines == screen_lines
218                                     && num_columns == screen_columns);
219     win->_idlok = FALSE;
220     win->_idcok = TRUE;
221     win->_scroll = FALSE;
222     win->_leaveok = FALSE;
223     win->_use_keypad = FALSE;
224     win->_delay = -1;
225     win->_immed = FALSE;
226     win->_sync = 0;
227     win->_parx = -1;
228     win->_pary = -1;
229     win->_parent = 0;
230
231     win->_regtop = 0;
232     win->_regbottom = num_lines - 1;
233
234     win->_pad._pad_y = -1;
235     win->_pad._pad_x = -1;
236     win->_pad._pad_top = -1;
237     win->_pad._pad_bottom = -1;
238     win->_pad._pad_left = -1;
239     win->_pad._pad_right = -1;
240
241     for (i = 0; i < num_lines; i++) {
242         /*
243          * This used to do
244          *
245          * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
246          *
247          * which marks the whole window unchanged.  That's how
248          * SVr1 curses did it, but SVr4 curses marks the whole new
249          * window changed.
250          *
251          * With the old SVr1-like code, say you have stdscr full of
252          * characters, then create a new window with newwin(),
253          * then do a printw(win, "foo        ");, the trailing spaces are
254          * completely ignored by the following refreshes.  So, you
255          * get "foojunkjunk" on the screen instead of "foo        " as
256          * you actually intended.
257          *
258          * SVr4 doesn't do this.  Instead the spaces are actually written.
259          * So that's how we want ncurses to behave.
260          */
261         win->_line[i].firstchar = 0;
262         win->_line[i].lastchar = num_columns - 1;
263
264         if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
265     }
266
267     if (!is_pad && (begx + num_columns == screen_columns)) {
268         win->_flags |= _ENDLINE;
269
270         if (begx == 0 && num_lines == screen_lines && begy == 0)
271             win->_flags |= _FULLWIN;
272
273         if (begy + num_lines == screen_lines)
274             win->_flags |= _SCROLLWIN;
275     }
276
277     wp->next = _nc_windows;
278     _nc_windows = wp;
279
280     T((T_CREATE("window %p"), win));
281
282     return (win);
283 }