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