1 /* $OpenBSD: lib_newwin.c,v 1.3 2000/06/19 03:53:43 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.24 2000/04/29 18:49:51 tom Exp $") 46 47 void 48 _nc_freewin(WINDOW *win) 49 { 50 WINDOWLIST *p, *q; 51 int i; 52 53 if (win != 0) { 54 for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) { 55 if (p->win == win) { 56 if (q == 0) 57 _nc_windows = p->next; 58 else 59 q->next = p->next; 60 free(p); 61 62 if (!(win->_flags & _SUBWIN)) { 63 for (i = 0; i <= win->_maxy; i++) 64 FreeIfNeeded(win->_line[i].text); 65 } 66 free(win->_line); 67 free(win); 68 69 if (win == curscr) 70 curscr = 0; 71 if (win == stdscr) 72 stdscr = 0; 73 if (win == newscr) 74 newscr = 0; 75 76 T(("...deleted win=%p", win)); 77 break; 78 } 79 } 80 } 81 } 82 83 WINDOW * 84 newwin(int num_lines, int num_columns, int begy, int begx) 85 { 86 WINDOW *win; 87 chtype *ptr; 88 int i; 89 90 T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx)); 91 92 if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0) 93 returnWin(0); 94 95 if (num_lines == 0) 96 num_lines = SP->_lines_avail - begy; 97 if (num_columns == 0) 98 num_columns = screen_columns - begx; 99 100 if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail) 101 returnWin(0); 102 103 if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0) 104 returnWin(0); 105 106 for (i = 0; i < num_lines; i++) { 107 win->_line[i].text = typeCalloc(chtype, (unsigned) num_columns); 108 if (win->_line[i].text == 0) { 109 _nc_freewin(win); 110 returnWin(0); 111 } 112 for (ptr = win->_line[i].text; ptr < win->_line[i].text + 113 num_columns;) 114 *ptr++ = ' '; 115 } 116 117 T(("newwin: returned window is %p", win)); 118 119 returnWin(win); 120 } 121 122 WINDOW * 123 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx) 124 { 125 WINDOW *win; 126 int i; 127 int flags = _SUBWIN; 128 129 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns, 130 begy, begx)); 131 132 /* 133 ** make sure window fits inside the original one 134 */ 135 if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0) 136 returnWin(0); 137 if (begy + num_lines > orig->_maxy + 1 138 || begx + num_columns > orig->_maxx + 1) 139 returnWin(0); 140 141 if (num_lines == 0) 142 num_lines = orig->_maxy + 1 - begy; 143 144 if (num_columns == 0) 145 num_columns = orig->_maxx + 1 - begx; 146 147 if (orig->_flags & _ISPAD) 148 flags |= _ISPAD; 149 150 if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy, 151 orig->_begx + begx, flags)) == 0) 152 returnWin(0); 153 154 win->_pary = begy; 155 win->_parx = begx; 156 win->_attrs = orig->_attrs; 157 win->_bkgd = orig->_bkgd; 158 159 for (i = 0; i < num_lines; i++) 160 win->_line[i].text = &orig->_line[begy++].text[begx]; 161 162 win->_parent = orig; 163 164 T(("derwin: returned window is %p", win)); 165 166 returnWin(win); 167 } 168 169 WINDOW * 170 subwin(WINDOW *w, int l, int c, int y, int x) 171 { 172 T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x)); 173 T(("parent has begy = %d, begx = %d", w->_begy, w->_begx)); 174 175 returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx)); 176 } 177 178 static bool 179 dimension_limit(int value) 180 { 181 NCURSES_SIZE_T test = value; 182 return (test == value && value > 0); 183 } 184 185 WINDOW * 186 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags) 187 { 188 int i; 189 WINDOWLIST *wp; 190 WINDOW *win; 191 bool is_pad = (flags & _ISPAD); 192 193 T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx)); 194 195 if (!dimension_limit(num_lines) || !dimension_limit(num_columns)) 196 return 0; 197 198 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0) 199 return 0; 200 201 if ((win = typeCalloc(WINDOW, 1)) == 0) 202 return 0; 203 204 if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) { 205 free(win); 206 return 0; 207 } 208 209 win->_curx = 0; 210 win->_cury = 0; 211 win->_maxy = num_lines - 1; 212 win->_maxx = num_columns - 1; 213 win->_begy = begy; 214 win->_begx = begx; 215 win->_yoffset = SP->_topstolen; 216 217 win->_flags = flags; 218 win->_attrs = A_NORMAL; 219 win->_bkgd = BLANK; 220 221 win->_clear = is_pad ? FALSE : (num_lines == screen_lines && num_columns 222 == screen_columns); 223 win->_idlok = FALSE; 224 win->_idcok = TRUE; 225 win->_scroll = FALSE; 226 win->_leaveok = FALSE; 227 win->_use_keypad = FALSE; 228 win->_delay = -1; 229 win->_immed = FALSE; 230 win->_sync = 0; 231 win->_parx = -1; 232 win->_pary = -1; 233 win->_parent = 0; 234 235 win->_regtop = 0; 236 win->_regbottom = num_lines - 1; 237 238 win->_pad._pad_y = -1; 239 win->_pad._pad_x = -1; 240 win->_pad._pad_top = -1; 241 win->_pad._pad_bottom = -1; 242 win->_pad._pad_left = -1; 243 win->_pad._pad_right = -1; 244 245 for (i = 0; i < num_lines; i++) { 246 /* 247 * This used to do 248 * 249 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE; 250 * 251 * which marks the whole window unchanged. That's how 252 * SVr1 curses did it, but SVr4 curses marks the whole new 253 * window changed. 254 * 255 * With the old SVr1-like code, say you have stdscr full of 256 * characters, then create a new window with newwin(), 257 * then do a printw(win, "foo ");, the trailing spaces are 258 * completely ignored by the following refreshes. So, you 259 * get "foojunkjunk" on the screen instead of "foo " as 260 * you actually intended. 261 * 262 * SVr4 doesn't do this. Instead the spaces are actually written. 263 * So that's how we want ncurses to behave. 264 */ 265 win->_line[i].firstchar = 0; 266 win->_line[i].lastchar = num_columns - 1; 267 268 if_USE_SCROLL_HINTS(win->_line[i].oldindex = i); 269 } 270 271 if (!is_pad && (begx + num_columns == screen_columns)) { 272 win->_flags |= _ENDLINE; 273 274 if (begx == 0 && num_lines == screen_lines && begy == 0) 275 win->_flags |= _FULLWIN; 276 277 if (begy + num_lines == screen_lines) 278 win->_flags |= _SCROLLWIN; 279 } 280 281 wp->next = _nc_windows; 282 wp->win = win; 283 _nc_windows = wp; 284 285 T((T_CREATE("window %p"), win)); 286 287 return (win); 288 } 289