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