]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_newwin.c
ncurses 4.1
[ncurses.git] / ncurses / lib_newwin.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23
24 /*
25 **      lib_newwin.c
26 **
27 **      The routines newwin(), subwin() and their dependent
28 **
29 */
30
31 #include <curses.priv.h>
32
33 MODULE_ID("$Id: lib_newwin.c,v 1.17 1997/02/15 21:46:05 tom Exp $")
34
35 void _nc_freewin(WINDOW *win)
36 {
37 WINDOWLIST *p, *q;
38 int     i;
39
40         if (win != 0) {
41                 for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
42                         if (p->win == win) {
43                                 if (q == 0)
44                                         _nc_windows = p->next;
45                                 else
46                                         q->next = p->next;
47                                 free(p);
48
49                                 if (! (win->_flags & _SUBWIN)) {
50                                         for (i = 0; i <= win->_maxy && win->_line[i].text; i++)
51                                                 free(win->_line[i].text);
52                                 }
53                                 free(win->_line);
54                                 free(win);
55
56                                 if (win == curscr) curscr = 0;
57                                 if (win == stdscr) stdscr = 0;
58                                 if (win == newscr) newscr = 0;
59
60                                 T(("...deleted win=%p", win));
61                                 break;
62                         }
63                 }
64         }
65 }
66
67 WINDOW * newwin(int num_lines, int num_columns, int begy, int begx)
68 {
69 WINDOW  *win;
70 chtype  *ptr;
71 int     i;
72
73         T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
74
75         if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
76                 returnWin(0);
77
78         if (num_lines == 0)
79             num_lines = SP->_lines_avail - begy;
80         if (num_columns == 0)
81             num_columns = screen_columns - begx;
82
83         if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail)
84                 returnWin(0);
85
86         if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
87                 returnWin(0);
88
89         for (i = 0; i < num_lines; i++) {
90             if ((win->_line[i].text = typeCalloc(chtype, (unsigned)num_columns)) == 0) {
91                 _nc_freewin(win);
92                 returnWin(0);
93             }
94             for (ptr = win->_line[i].text; ptr < win->_line[i].text + num_columns; )
95                 *ptr++ = ' ';
96         }
97
98         T(("newwin: returned window is %p", win));
99
100         returnWin(win);
101 }
102
103 WINDOW * derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
104 {
105 WINDOW  *win;
106 int     i;
107 int     flags = _SUBWIN;
108
109         T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns, begy, begx));
110
111         /*
112         ** make sure window fits inside the original one
113         */
114         if ( begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
115             returnWin(0);
116         if ( begy + num_lines > orig->_maxy + 1
117                 || begx + num_columns > orig->_maxx + 1)
118             returnWin(0);
119
120         if (num_lines == 0)
121             num_lines = orig->_maxy - begy;
122
123         if (num_columns == 0)
124             num_columns = orig->_maxx - begx;
125
126         if (orig->_flags & _ISPAD)
127           flags |= _ISPAD;
128
129         if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy, orig->_begx + begx, flags)) == 0)
130             returnWin(0);
131
132         win->_pary = begy;
133         win->_parx = begx;
134         win->_attrs = orig->_attrs;
135         win->_bkgd = orig->_bkgd;
136
137         for (i = 0; i < num_lines; i++)
138             win->_line[i].text = &orig->_line[begy++].text[begx];
139
140         win->_parent = orig;
141
142         T(("derwin: returned window is %p", win));
143
144         returnWin(win);
145 }
146
147
148 WINDOW *subwin(WINDOW *w, int l, int c, int y, int x)
149 {
150         T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
151         T(("parent has begy = %d, begx = %d", w->_begy, w->_begx));
152
153         returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
154 }
155
156 WINDOW *
157 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
158 {
159 int     i;
160 WINDOWLIST *wp;
161 WINDOW  *win;
162 bool    is_pad = (flags & _ISPAD);
163
164         T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx));
165
166         if (num_lines <= 0 || num_columns <= 0)
167                 return 0;
168
169         if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
170                 return 0;
171
172         if ((win = typeCalloc(WINDOW, 1)) == 0)
173                 return 0;
174
175         if ((win->_line = typeCalloc(struct ldat, ((unsigned)num_lines))) == 0) {
176                 free(win);
177                 return 0;
178         }
179
180         win->_curx       = 0;
181         win->_cury       = 0;
182         win->_maxy       = num_lines - 1;
183         win->_maxx       = num_columns - 1;
184         win->_begy       = begy;
185         win->_begx       = begx;
186         win->_yoffset    = SP->_topstolen;
187
188         win->_flags      = flags;
189         win->_attrs      = A_NORMAL;
190         win->_bkgd       = BLANK;
191
192         win->_clear      = is_pad ? FALSE : (num_lines == screen_lines  &&  num_columns == screen_columns);
193         win->_idlok      = FALSE;
194         win->_idcok      = TRUE;
195         win->_scroll     = FALSE;
196         win->_leaveok    = FALSE;
197         win->_use_keypad = FALSE;
198         win->_delay      = -1;
199         win->_immed      = FALSE;
200         win->_sync       = 0;
201         win->_parx       = -1;
202         win->_pary       = -1;
203         win->_parent     = 0;
204
205         win->_regtop     = 0;
206         win->_regbottom  = num_lines - 1;
207
208         win->_pad._pad_y      = -1;
209         win->_pad._pad_x      = -1;
210         win->_pad._pad_top    = -1;
211         win->_pad._pad_bottom = -1;
212         win->_pad._pad_left   = -1;
213         win->_pad._pad_right  = -1;
214
215         for (i = 0; i < num_lines; i++)
216         {
217             /*
218              * This used to do
219              *
220              * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
221              *
222              * which marks the whole window unchanged.  That's how
223              * SVr1 curses did it, but SVr4 curses marks the whole new
224              * window changed.
225              *
226              * With the old SVr1-like code, say you have stdscr full of
227              * characters, then create a new window with newwin(),
228              * then do a printw(win, "foo        ");, the trailing spaces are
229              * completely ignored by the following refreshes.  So, you
230              * get "foojunkjunk" on the screen instead of "foo        " as
231              * you actually intended.
232              *
233              * SVr4 doesn't do this.  Instead the spaces are actually written.
234              * So that's how we want ncurses to behave.
235              */
236             win->_line[i].firstchar = 0;
237             win->_line[i].lastchar = num_columns-1;
238
239             win->_line[i].oldindex = i;
240         }
241
242         if (!is_pad && (begx + num_columns == screen_columns)) {
243                 win->_flags |= _ENDLINE;
244
245                 if (begx == 0  &&  num_lines == screen_lines  &&  begy == 0)
246                         win->_flags |= _FULLWIN;
247
248                 if (begy + num_lines == screen_lines)
249                         win->_flags |= _SCROLLWIN;
250         }
251
252         wp->next = _nc_windows;
253         wp->win  = win;
254         _nc_windows = wp;
255
256         T((T_CREATE("window %p"), win));
257
258         return(win);
259 }