#ifdef M_RCSID
#ifndef lint
static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/newwin.c 1.9 1995/09/28 20:15:58 ant Exp $";
#endif
#endif
#include <private.h>
#include <stdlib.h>
WINDOW *
__m_newwin(parent, nlines, ncols, begy, begx)
WINDOW *parent;
int nlines, ncols, begy, begx;
{
WINDOW *w;
int x, y, dx, dy;
#ifdef M_CURSES_TRACE
__m_trace(
"__m_newwin(%p, %d, %d, %d, %d)",
parent, nlines, ncols, begy, begx
);
#endif
if (parent == (WINDOW *) 0) {
if (nlines == 0) {
nlines = lines;
if (0 <= begy)
nlines -= begy;
}
if (ncols == 0) {
ncols = columns;
if (0 <= begx)
ncols -= begx;
}
} else {
if (begy < parent->_begy || begx < parent->_begx
|| parent->_maxy < (begy-parent->_begy) + nlines
|| parent->_maxx < (begx-parent->_begx) + ncols)
goto error_1;
if (nlines == 0)
nlines = parent->_maxy - (begy - parent->_begy);
if (ncols == 0)
ncols = parent->_maxx - (begx - parent->_begx);
}
if (0 <= begy) {
if (lines < begy + nlines)
goto error_1;
}
if (0 <= begx) {
if (columns < begx + ncols)
goto error_1;
}
w = (WINDOW *) calloc(1, sizeof *w);
if (w == (WINDOW *) 0)
goto error_1;
w->_first = (short *) calloc(
(size_t) (nlines + nlines), sizeof *w->_first
);
if (w->_first == (short *) 0)
goto error_2;
w->_last = &w->_first[nlines];
w->_line = (cchar_t **) calloc((size_t) nlines, sizeof *w->_line);
if (w->_line == (cchar_t **) 0)
goto error_2;
(void) setcchar(
&w->_bg, M_MB_L(" "), WA_NORMAL, 0, (void *) 0
);
(void) setcchar(
&w->_fg, M_MB_L(" "), WA_NORMAL, 0, (void *) 0
);
if (parent == (WINDOW *) 0) {
w->_base = (cchar_t *) malloc(
(size_t) (nlines * ncols) * sizeof *w->_base
);
if (w->_base == (cchar_t *) 0)
goto error_2;
w->_line[y = 0] = w->_base;
do {
for (x = 0; x < ncols; ++x)
w->_line[y][x] = w->_bg;
w->_line[y+1] = &w->_line[y][x];
} while (++y < nlines-1);
} else {
dy = begy - parent->_begy;
dx = begx - parent->_begx;
w->_base = (cchar_t *) 0;
for (y = 0; y < nlines; ++y)
w->_line[y] = &parent->_line[dy++][dx];
}
w->_begy = (short) begy;
w->_begx = (short) begx;
w->_cury = w->_curx = 0;
w->_maxy = (short) nlines;
w->_maxx = (short) ncols;
w->_parent = parent;
w->_top = 0;
w->_bottom = (short) nlines;
w->_scroll = 0;
w->_vmin = 1;
w->_vtime = 0;
w->_flags = W_USE_TIMEOUT;
if ((begy < 0 && begx < 0)
|| (parent != (WINDOW *) 0 && (parent->_flags & W_IS_PAD))) {
w->_flags |= W_IS_PAD;
w->_begy = w->_begx = 0;
} else if (begx + ncols == columns) {
w->_flags |= W_END_LINE;
if (begx == 0) {
w->_flags |= W_FULL_LINE;
if (begy == 0 && nlines == lines)
w->_flags |= W_FULL_WINDOW;
}
if (begy + nlines == lines)
w->_flags |= W_SCROLL_WINDOW;
}
if (w->_flags & W_FULL_WINDOW)
w->_flags |= W_CLEAR_WINDOW;
(void) wtouchln(w, 0, w->_maxy, 0);
return __m_return_pointer("__m_newwin", w);
error_2:
(void) delwin(w);
error_1:
return __m_return_pointer("__m_newwin", (WINDOW *) 0);
}
int
delwin(w)
WINDOW *w;
{
if (w == (WINDOW *) 0)
return OK;
#ifdef M_CURSES_TRACE
__m_trace(
"delwin(%p) which is a %s%s.", w,
(w->_parent == (WINDOW *) 0) ? "normal " : "sub-",
(w->_flags & W_IS_PAD) ? "pad" : "window"
);
#endif
if (w->_line != (cchar_t **) 0) {
if (w->_base != (cchar_t *) 0)
free(w->_base);
free(w->_line);
}
if (w->_first != (short *) 0)
free(w->_first);
free(w);
return __m_return_code("delwin", OK);
}
WINDOW *
derwin(parent, nlines, ncols, begy, begx)
WINDOW *parent;
int nlines, ncols, begy, begx;
{
WINDOW *w;
#ifdef M_CURSES_TRACE
__m_trace(
"derwin(%p, %d, %d, %d, %d)",
parent, nlines, ncols, begy, begx
);
#endif
if (parent == (WINDOW *) 0)
return __m_return_pointer("derwin", (WINDOW *) 0);
begy += parent->_begy;
begx += parent->_begx;
w = __m_newwin(parent, nlines, ncols, begy, begx);
return __m_return_pointer("derwin", w);
}
WINDOW *
newwin(nlines, ncols, begy, begx)
int nlines, ncols, begy, begx;
{
WINDOW *w;
#ifdef M_CURSES_TRACE
__m_trace("newwin(%d, %d, %d, %d)", nlines, ncols, begy, begx);
#endif
w = __m_newwin((WINDOW *) 0, nlines, ncols, begy, begx);
return __m_return_pointer("newwin", w);
}
WINDOW *
subwin(parent, nlines, ncols, begy, begx)
WINDOW *parent;
int nlines, ncols, begy, begx;
{
WINDOW *w;
#ifdef M_CURSES_TRACE
__m_trace(
"subwin(%p, %d, %d, %d, %d)",
parent, nlines, ncols, begy, begx
);
#endif
if (parent == (WINDOW *) 0)
return __m_return_pointer("subwin", (WINDOW *) 0);
w = __m_newwin(parent, nlines, ncols, begy, begx);
return __m_return_pointer("subwin", w);
}