1 /* $OpenBSD: lib_newwin.c,v 1.4 2001/01/22 18:01:42 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 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 45 MODULE_ID("$From: lib_newwin.c,v 1.27 2000/12/10 02:43:27 tom Exp $") 46 47 NCURSES_EXPORT(int) 48 _nc_freewin(WINDOW *win) 49 { 50 WINDOWLIST *p, *q; 51 int i; 52 int result = ERR; 53 54 if (win != 0) { 55 for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) { 56 if (p->win == win) { 57 if (q == 0) 58 _nc_windows = p->next; 59 else 60 q->next = p->next; 61 free(p); 62 63 if (!(win->_flags & _SUBWIN)) { 64 for (i = 0; i <= win->_maxy; i++) 65 FreeIfNeeded(win->_line[i].text); 66 } 67 free(win->_line); 68 free(win); 69 70 if (win == curscr) 71 curscr = 0; 72 if (win == stdscr) 73 stdscr = 0; 74 if (win == newscr) 75 newscr = 0; 76 77 result = OK; 78 T(("...deleted win=%p", win)); 79 break; 80 } 81 } 82 } 83 return result; 84 } 85 86 NCURSES_EXPORT(WINDOW *) 87 newwin 88 (int num_lines, int num_columns, int begy, int begx) 89 { 90 WINDOW *win; 91 chtype *ptr; 92 int i; 93 94 T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx)); 95 96 if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0) 97 returnWin(0); 98 99 if (num_lines == 0) 100 num_lines = SP->_lines_avail - begy; 101 if (num_columns == 0) 102 num_columns = screen_columns - begx; 103 104 if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail) 105 returnWin(0); 106 107 if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0) 108 returnWin(0); 109 110 for (i = 0; i < num_lines; i++) { 111 win->_line[i].text = typeCalloc(chtype, (unsigned) num_columns); 112 if (win->_line[i].text == 0) { 113 (void) _nc_freewin(win); 114 returnWin(0); 115 } 116 for (ptr = win->_line[i].text; ptr < win->_line[i].text + 117 num_columns;) 118 *ptr++ = ' '; 119 } 120 121 T(("newwin: returned window is %p", win)); 122 123 returnWin(win); 124 } 125 126 NCURSES_EXPORT(WINDOW *) 127 derwin 128 (WINDOW *orig, int num_lines, int num_columns, int begy, int begx) 129 { 130 WINDOW *win; 131 int i; 132 int flags = _SUBWIN; 133 134 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns, 135 begy, begx)); 136 137 /* 138 ** make sure window fits inside the original one 139 */ 140 if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0) 141 returnWin(0); 142 if (begy + num_lines > orig->_maxy + 1 143 || begx + num_columns > orig->_maxx + 1) 144 returnWin(0); 145 146 if (num_lines == 0) 147 num_lines = orig->_maxy + 1 - begy; 148 149 if (num_columns == 0) 150 num_columns = orig->_maxx + 1 - begx; 151 152 if (orig->_flags & _ISPAD) 153 flags |= _ISPAD; 154 155 if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy, 156 orig->_begx + begx, flags)) == 0) 157 returnWin(0); 158 159 win->_pary = begy; 160 win->_parx = begx; 161 win->_attrs = orig->_attrs; 162 win->_bkgd = orig->_bkgd; 163 164 for (i = 0; i < num_lines; i++) 165 win->_line[i].text = &orig->_line[begy++].text[begx]; 166 167 win->_parent = orig; 168 169 T(("derwin: returned window is %p", win)); 170 171 returnWin(win); 172 } 173 174 NCURSES_EXPORT(WINDOW *) 175 subwin 176 (WINDOW *w, int l, int c, int y, int x) 177 { 178 T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x)); 179 T(("parent has begy = %d, begx = %d", w->_begy, w->_begx)); 180 181 returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx)); 182 } 183 184 static bool 185 dimension_limit(int value) 186 { 187 NCURSES_SIZE_T test = value; 188 return (test == value && value > 0); 189 } 190 191 NCURSES_EXPORT(WINDOW *) 192 _nc_makenew 193 (int num_lines, int num_columns, int begy, int begx, int flags) 194 { 195 int i; 196 WINDOWLIST *wp; 197 WINDOW *win; 198 bool is_pad = (flags & _ISPAD); 199 200 T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx)); 201 202 if (!dimension_limit(num_lines) || !dimension_limit(num_columns)) 203 return 0; 204 205 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0) 206 return 0; 207 208 if ((win = typeCalloc(WINDOW, 1)) == 0) 209 return 0; 210 211 if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) { 212 free(win); 213 return 0; 214 } 215 216 win->_curx = 0; 217 win->_cury = 0; 218 win->_maxy = num_lines - 1; 219 win->_maxx = num_columns - 1; 220 win->_begy = begy; 221 win->_begx = begx; 222 win->_yoffset = SP->_topstolen; 223 224 win->_flags = flags; 225 win->_attrs = A_NORMAL; 226 win->_bkgd = BLANK; 227 228 win->_clear = is_pad ? FALSE : (num_lines == screen_lines 229 && num_columns == screen_columns); 230 win->_idlok = FALSE; 231 win->_idcok = TRUE; 232 win->_scroll = FALSE; 233 win->_leaveok = FALSE; 234 win->_use_keypad = FALSE; 235 win->_delay = -1; 236 win->_immed = FALSE; 237 win->_sync = 0; 238 win->_parx = -1; 239 win->_pary = -1; 240 win->_parent = 0; 241 242 win->_regtop = 0; 243 win->_regbottom = num_lines - 1; 244 245 win->_pad._pad_y = -1; 246 win->_pad._pad_x = -1; 247 win->_pad._pad_top = -1; 248 win->_pad._pad_bottom = -1; 249 win->_pad._pad_left = -1; 250 win->_pad._pad_right = -1; 251 252 for (i = 0; i < num_lines; i++) { 253 /* 254 * This used to do 255 * 256 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE; 257 * 258 * which marks the whole window unchanged. That's how 259 * SVr1 curses did it, but SVr4 curses marks the whole new 260 * window changed. 261 * 262 * With the old SVr1-like code, say you have stdscr full of 263 * characters, then create a new window with newwin(), 264 * then do a printw(win, "foo ");, the trailing spaces are 265 * completely ignored by the following refreshes. So, you 266 * get "foojunkjunk" on the screen instead of "foo " as 267 * you actually intended. 268 * 269 * SVr4 doesn't do this. Instead the spaces are actually written. 270 * So that's how we want ncurses to behave. 271 */ 272 win->_line[i].firstchar = 0; 273 win->_line[i].lastchar = num_columns - 1; 274 275 if_USE_SCROLL_HINTS(win->_line[i].oldindex = i); 276 } 277 278 if (!is_pad && (begx + num_columns == screen_columns)) { 279 win->_flags |= _ENDLINE; 280 281 if (begx == 0 && num_lines == screen_lines && begy == 0) 282 win->_flags |= _FULLWIN; 283 284 if (begy + num_lines == screen_lines) 285 win->_flags |= _SCROLLWIN; 286 } 287 288 wp->next = _nc_windows; 289 wp->win = win; 290 _nc_windows = wp; 291 292 T((T_CREATE("window %p"), win)); 293 294 return (win); 295 } 296