]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newwin.c
835bb0bccae1e58b570dde770523352d5fecbfd5
[ncurses.git] / ncurses / base / lib_newwin.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2008 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  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36 **      lib_newwin.c
37 **
38 **      The routines newwin(), subwin() and their dependent
39 **
40 */
41
42 #include <curses.priv.h>
43 #include <stddef.h>
44
45 MODULE_ID("$Id: lib_newwin.c,v 1.45 2008/02/23 20:57:58 tom Exp $")
46
47 static WINDOW *
48 remove_window_from_screen(WINDOW *win)
49 {
50     SCREEN **scan = &_nc_screen_chain;
51
52     while (*scan) {
53         SCREEN *sp = *scan;
54         if (sp->_curscr == win) {
55             sp->_curscr = 0;
56 #if !USE_REENTRANT
57             if (win == curscr)
58                 curscr = 0;
59 #endif
60         } else if (sp->_stdscr == win) {
61             sp->_stdscr = 0;
62 #if !USE_REENTRANT
63             if (win == stdscr)
64                 stdscr = 0;
65 #endif
66         } else if (sp->_newscr == win) {
67             sp->_newscr = 0;
68 #if !USE_REENTRANT
69             if (win == newscr)
70                 newscr = 0;
71 #endif
72         } else {
73             scan = &(*scan)->_next_screen;
74             continue;
75         }
76         break;
77     }
78
79     return 0;
80 }
81
82 NCURSES_EXPORT(int)
83 _nc_freewin(WINDOW *win)
84 {
85     WINDOWLIST *p, *q;
86     int i;
87     int result = ERR;
88
89     T((T_CALLED("_nc_freewin(%p)"), win));
90
91     if (win != 0) {
92         if (_nc_try_global(windowlist) == 0) {
93             for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
94                 if (&(p->win) == win) {
95                     remove_window_from_screen(win);
96                     if (q == 0)
97                         _nc_windows = p->next;
98                     else
99                         q->next = p->next;
100
101                     if (!(win->_flags & _SUBWIN)) {
102                         for (i = 0; i <= win->_maxy; i++)
103                             FreeIfNeeded(win->_line[i].text);
104                     }
105                     free(win->_line);
106                     free(p);
107
108                     result = OK;
109                     T(("...deleted win=%p", win));
110                     break;
111                 }
112             }
113             _nc_unlock_global(windowlist);
114         }
115     }
116     returnCode(result);
117 }
118
119 NCURSES_EXPORT(WINDOW *)
120 newwin(int num_lines, int num_columns, int begy, int begx)
121 {
122     WINDOW *win;
123     NCURSES_CH_T *ptr;
124     int i;
125
126     T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
127
128     if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
129         returnWin(0);
130
131     if (num_lines == 0)
132         num_lines = SP->_lines_avail - begy;
133     if (num_columns == 0)
134         num_columns = screen_columns - begx;
135
136     if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
137         returnWin(0);
138
139     for (i = 0; i < num_lines; i++) {
140         win->_line[i].text = typeCalloc(NCURSES_CH_T, (unsigned) num_columns);
141         if (win->_line[i].text == 0) {
142             (void) _nc_freewin(win);
143             returnWin(0);
144         }
145         for (ptr = win->_line[i].text;
146              ptr < win->_line[i].text + num_columns;
147              ptr++)
148             SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
149     }
150
151     returnWin(win);
152 }
153
154 NCURSES_EXPORT(WINDOW *)
155 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
156 {
157     WINDOW *win;
158     int i;
159     int flags = _SUBWIN;
160
161     T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
162        begy, begx));
163
164     /*
165      * make sure window fits inside the original one
166      */
167     if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
168         returnWin(0);
169     if (begy + num_lines > orig->_maxy + 1
170         || begx + num_columns > orig->_maxx + 1)
171         returnWin(0);
172
173     if (num_lines == 0)
174         num_lines = orig->_maxy + 1 - begy;
175
176     if (num_columns == 0)
177         num_columns = orig->_maxx + 1 - begx;
178
179     if (orig->_flags & _ISPAD)
180         flags |= _ISPAD;
181
182     if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy,
183                            orig->_begx + begx, flags)) == 0)
184         returnWin(0);
185
186     win->_pary = begy;
187     win->_parx = begx;
188     WINDOW_ATTRS(win) = WINDOW_ATTRS(orig);
189     win->_nc_bkgd = orig->_nc_bkgd;
190
191     for (i = 0; i < num_lines; i++)
192         win->_line[i].text = &orig->_line[begy++].text[begx];
193
194     win->_parent = orig;
195
196     returnWin(win);
197 }
198
199 NCURSES_EXPORT(WINDOW *)
200 subwin(WINDOW *w, int l, int c, int y, int x)
201 {
202     T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
203     T(("parent has begy = %ld, begx = %ld", (long) w->_begy, (long) w->_begx));
204
205     returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
206 }
207
208 static bool
209 dimension_limit(int value)
210 {
211     NCURSES_SIZE_T test = value;
212     return (test == value && value > 0);
213 }
214
215 NCURSES_EXPORT(WINDOW *)
216 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
217 {
218     int i;
219     WINDOWLIST *wp;
220     WINDOW *win;
221     bool is_pad = (flags & _ISPAD);
222
223     T((T_CALLED("_nc_makenew(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
224
225     if (SP == 0)
226         returnWin(0);
227
228     if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
229         returnWin(0);
230
231     if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
232         returnWin(0);
233
234     _nc_mutex_init(&(wp->mutex_use_window));
235
236     win = &(wp->win);
237
238     if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
239         free(win);
240         returnWin(0);
241     }
242
243     _nc_lock_global(windowlist);
244
245     win->_curx = 0;
246     win->_cury = 0;
247     win->_maxy = num_lines - 1;
248     win->_maxx = num_columns - 1;
249     win->_begy = begy;
250     win->_begx = begx;
251     win->_yoffset = SP->_topstolen;
252
253     win->_flags = flags;
254     WINDOW_ATTRS(win) = A_NORMAL;
255     SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
256
257     win->_clear = is_pad ? FALSE : (num_lines == screen_lines
258                                     && num_columns == screen_columns);
259     win->_idlok = FALSE;
260     win->_idcok = TRUE;
261     win->_scroll = FALSE;
262     win->_leaveok = FALSE;
263     win->_use_keypad = FALSE;
264     win->_delay = -1;
265     win->_immed = FALSE;
266     win->_sync = 0;
267     win->_parx = -1;
268     win->_pary = -1;
269     win->_parent = 0;
270
271     win->_regtop = 0;
272     win->_regbottom = num_lines - 1;
273
274     win->_pad._pad_y = -1;
275     win->_pad._pad_x = -1;
276     win->_pad._pad_top = -1;
277     win->_pad._pad_bottom = -1;
278     win->_pad._pad_left = -1;
279     win->_pad._pad_right = -1;
280
281     for (i = 0; i < num_lines; i++) {
282         /*
283          * This used to do
284          *
285          * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
286          *
287          * which marks the whole window unchanged.  That's how
288          * SVr1 curses did it, but SVr4 curses marks the whole new
289          * window changed.
290          *
291          * With the old SVr1-like code, say you have stdscr full of
292          * characters, then create a new window with newwin(),
293          * then do a printw(win, "foo        ");, the trailing spaces are
294          * completely ignored by the following refreshes.  So, you
295          * get "foojunkjunk" on the screen instead of "foo        " as
296          * you actually intended.
297          *
298          * SVr4 doesn't do this.  Instead the spaces are actually written.
299          * So that's how we want ncurses to behave.
300          */
301         win->_line[i].firstchar = 0;
302         win->_line[i].lastchar = num_columns - 1;
303
304         if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
305     }
306
307     if (!is_pad && (begx + num_columns == screen_columns)) {
308         win->_flags |= _ENDLINE;
309
310         if (begx == 0 && num_lines == screen_lines && begy == 0)
311             win->_flags |= _FULLWIN;
312
313         if (begy + num_lines == screen_lines)
314             win->_flags |= _SCROLLWIN;
315     }
316
317     wp->next = _nc_windows;
318     _nc_windows = wp;
319
320     T((T_CREATE("window %p"), win));
321
322     _nc_unlock_global(windowlist);
323     returnWin(win);
324 }