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