]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newwin.c
e7a30bf937ce3501e9263399e5708490044dd88e
[ncurses.git] / ncurses / base / lib_newwin.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37 **      lib_newwin.c
38 **
39 **      The routines newwin(), subwin() and their dependent
40 **
41 */
42
43 #include <curses.priv.h>
44 #include <stddef.h>
45
46 MODULE_ID("$Id: lib_newwin.c,v 1.54 2009/04/18 21:02:34 tom Exp $")
47
48 #define window_is(name) ((sp)->_##name == win)
49
50 #if USE_REENTRANT
51 #define remove_window(name) \
52                 sp->_##name = 0
53 #else
54 #define remove_window(name) \
55                 sp->_##name = 0; \
56                 if (win == name) \
57                     name = 0
58 #endif
59
60 static void
61 remove_window_from_screen(WINDOW *win)
62 {
63     SCREEN *sp;
64
65     for (each_screen(sp)) {
66         if (window_is(curscr)) {
67             remove_window(curscr);
68             break;
69         } else if (window_is(stdscr)) {
70             remove_window(stdscr);
71             break;
72         } else if (window_is(newscr)) {
73             remove_window(newscr);
74             break;
75         }
76     }
77 }
78
79 NCURSES_EXPORT(int)
80 _nc_freewin(WINDOW *win)
81 {
82     WINDOWLIST *p, *q;
83     int i;
84     int result = ERR;
85
86     T((T_CALLED("_nc_freewin(%p)"), win));
87
88     if (win != 0) {
89         if (_nc_try_global(curses) == 0) {
90             q = 0;
91             for (each_window(p)) {
92                 if (&(p->win) == win) {
93                     remove_window_from_screen(win);
94                     if (q == 0)
95                         _nc_windows = p->next;
96                     else
97                         q->next = p->next;
98
99                     if (!(win->_flags & _SUBWIN)) {
100                         for (i = 0; i <= win->_maxy; i++)
101                             FreeIfNeeded(win->_line[i].text);
102                     }
103                     free(win->_line);
104                     free(p);
105
106                     result = OK;
107                     T(("...deleted win=%p", win));
108                     break;
109                 }
110                 q = p;
111             }
112             _nc_unlock_global(curses);
113         }
114     }
115     returnCode(result);
116 }
117
118 NCURSES_EXPORT(WINDOW *)
119 NCURSES_SP_NAME(newwin) (NCURSES_SP_DCLx
120                          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_PARM->_lines_avail - begy;
133     if (num_columns == 0)
134         num_columns = screen_columns(SP_PARM) - 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 #if NCURSES_SP_FUNCS
155 NCURSES_EXPORT(WINDOW *)
156 newwin(int num_lines, int num_columns, int begy, int begx)
157 {
158     return NCURSES_SP_NAME(newwin) (CURRENT_SCREEN,
159                                     num_lines, num_columns,
160                                     begy, begx);
161 }
162 #endif
163
164 NCURSES_EXPORT(WINDOW *)
165 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
166 {
167     WINDOW *win;
168     int i;
169     int flags = _SUBWIN;
170
171     T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
172        begy, begx));
173
174     /*
175      * make sure window fits inside the original one
176      */
177     if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
178         returnWin(0);
179     if (begy + num_lines > orig->_maxy + 1
180         || begx + num_columns > orig->_maxx + 1)
181         returnWin(0);
182
183     if (num_lines == 0)
184         num_lines = orig->_maxy + 1 - begy;
185
186     if (num_columns == 0)
187         num_columns = orig->_maxx + 1 - begx;
188
189     if (orig->_flags & _ISPAD)
190         flags |= _ISPAD;
191
192     if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy,
193                            orig->_begx + begx, flags)) == 0)
194         returnWin(0);
195
196     win->_pary = begy;
197     win->_parx = begx;
198     WINDOW_ATTRS(win) = WINDOW_ATTRS(orig);
199     win->_nc_bkgd = orig->_nc_bkgd;
200
201     for (i = 0; i < num_lines; i++)
202         win->_line[i].text = &orig->_line[begy++].text[begx];
203
204     win->_parent = orig;
205
206     returnWin(win);
207 }
208
209 NCURSES_EXPORT(WINDOW *)
210 subwin(WINDOW *w, int l, int c, int y, int x)
211 {
212     T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
213     T(("parent has begy = %ld, begx = %ld", (long) w->_begy, (long) w->_begx));
214
215     returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
216 }
217
218 static bool
219 dimension_limit(int value)
220 {
221     NCURSES_SIZE_T test = value;
222     return (test == value && value > 0);
223 }
224
225 NCURSES_EXPORT(WINDOW *)
226 NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_DCLx
227                               int num_lines,
228                               int num_columns,
229                               int begy,
230                               int begx,
231                               int flags)
232 {
233     int i;
234     WINDOWLIST *wp;
235     WINDOW *win;
236     bool is_pad = (flags & _ISPAD);
237
238     T((T_CALLED("_nc_makenew(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
239
240     if (SP_PARM == 0)
241         returnWin(0);
242
243     if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
244         returnWin(0);
245
246     if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
247         returnWin(0);
248
249     win = &(wp->win);
250
251     if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
252         free(win);
253         returnWin(0);
254     }
255
256     _nc_lock_global(curses);
257
258     win->_curx = 0;
259     win->_cury = 0;
260     win->_maxy = num_lines - 1;
261     win->_maxx = num_columns - 1;
262     win->_begy = begy;
263     win->_begx = begx;
264     win->_yoffset = SP_PARM->_topstolen;
265
266     win->_flags = flags;
267     WINDOW_ATTRS(win) = A_NORMAL;
268     SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
269
270     win->_clear = (is_pad
271                    ? FALSE
272                    : (num_lines == screen_lines(SP_PARM)
273                       && num_columns == screen_columns(SP_PARM)));
274     win->_idlok = FALSE;
275     win->_idcok = TRUE;
276     win->_scroll = FALSE;
277     win->_leaveok = FALSE;
278     win->_use_keypad = FALSE;
279     win->_delay = -1;
280     win->_immed = FALSE;
281     win->_sync = 0;
282     win->_parx = -1;
283     win->_pary = -1;
284     win->_parent = 0;
285
286     win->_regtop = 0;
287     win->_regbottom = num_lines - 1;
288
289     win->_pad._pad_y = -1;
290     win->_pad._pad_x = -1;
291     win->_pad._pad_top = -1;
292     win->_pad._pad_bottom = -1;
293     win->_pad._pad_left = -1;
294     win->_pad._pad_right = -1;
295
296     for (i = 0; i < num_lines; i++) {
297         /*
298          * This used to do
299          *
300          * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
301          *
302          * which marks the whole window unchanged.  That's how
303          * SVr1 curses did it, but SVr4 curses marks the whole new
304          * window changed.
305          *
306          * With the old SVr1-like code, say you have stdscr full of
307          * characters, then create a new window with newwin(),
308          * then do a printw(win, "foo        ");, the trailing spaces are
309          * completely ignored by the following refreshes.  So, you
310          * get "foojunkjunk" on the screen instead of "foo        " as
311          * you actually intended.
312          *
313          * SVr4 doesn't do this.  Instead the spaces are actually written.
314          * So that's how we want ncurses to behave.
315          */
316         win->_line[i].firstchar = 0;
317         win->_line[i].lastchar = num_columns - 1;
318
319         if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
320     }
321
322     if (!is_pad && (begx + num_columns == screen_columns(SP_PARM))) {
323         win->_flags |= _ENDLINE;
324
325         if (begx == 0 && num_lines == screen_lines(SP_PARM) && begy == 0)
326             win->_flags |= _FULLWIN;
327
328         if (begy + num_lines == screen_lines(SP_PARM))
329             win->_flags |= _SCROLLWIN;
330     }
331
332     wp->next = _nc_windows;
333     wp->screen = SP_PARM;
334     _nc_windows = wp;
335
336     T((T_CREATE("window %p"), win));
337
338     _nc_unlock_global(curses);
339     returnWin(win);
340 }
341
342 #if NCURSES_SP_FUNCS
343 NCURSES_EXPORT(WINDOW *)
344 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
345 {
346     return NCURSES_SP_NAME(_nc_makenew) (CURRENT_SCREEN,
347                                          num_lines, num_columns,
348                                          begy, begx, flags);
349 }
350 #endif
351
352 /*
353  * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
354  * internally, and it is useful to allow those to be invoked without switching
355  * SCREEN's, e.g., for multi-threaded applications.
356  */
357 NCURSES_EXPORT(SCREEN *)
358 _nc_screen_of(WINDOW *win)
359 {
360     SCREEN *sp = 0;
361
362     if (win != 0) {
363         WINDOWLIST *wp = (WINDOWLIST *) win;
364         sp = wp->screen;
365     }
366     return (sp);
367 }