xref: /original-bsd/lib/libcurses/newwin.c (revision 6c57d260)
1 /*
2  * allocate space for and set up defaults for a new window
3  *
4  * 01/26/81 (Berkeley) %W
5  */
6 
7 # include	"curses.ext"
8 
9 short	*calloc();
10 WINDOW	*malloc();
11 
12 static WINDOW	*makenew();
13 
14 # undef		nl	/* don't need it here, and it interferes	*/
15 
16 WINDOW *
17 newwin(num_lines, num_cols, begy, begx)
18 int	num_lines, num_cols, begy, begx;
19 {
20 	reg WINDOW	*win;
21 	reg char	*sp;
22 	reg int		i, by, bx, nl, nc;
23 
24 	by = begy;
25 	bx = begx;
26 	nl = num_lines;
27 	nc = num_cols;
28 
29 	if (nl == 0)
30 		nl = LINES - by;
31 	if (nc == 0)
32 		nc = COLS - bx;
33 	if ((win = makenew(nl, nc, by, bx)) == NULL)
34 		return ERR;
35 	for (i = 0; i < nl; i++)
36 		if ((win->_y[i] = (char *) calloc(nc, sizeof (char))) == NULL) {
37 			reg int		j;
38 
39 			for (j = 0; j < i; j++)
40 				cfree(win->_y[j]);
41 			cfree(win->_firstch);
42 			cfree(win->_lastch);
43 			cfree(win->_y);
44 			cfree(win);
45 			return ERR;
46 		}
47 		else
48 			for (sp = win->_y[i]; sp < win->_y[i] + nc; )
49 				*sp++ = ' ';
50 	return win;
51 }
52 
53 WINDOW *
54 subwin(orig, num_lines, num_cols, begy, begx)
55 reg WINDOW	*orig;
56 int		num_lines, num_cols, begy, begx; {
57 
58 	reg int		i;
59 	reg WINDOW	*win;
60 	reg int		by, bx, nl, nc;
61 	reg int		j, k;
62 
63 	by = begy;
64 	bx = begx;
65 	nl = num_lines;
66 	nc = num_cols;
67 
68 	/*
69 	 * make sure window fits inside the original one
70 	 */
71 # ifdef	DEBUG
72 	fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
73 # endif
74 	if (by < orig->_begy || bx < orig->_begx
75 	    || by + nl > orig->_maxy || bx + nc > orig->_maxx)
76 		return ERR;
77 	if (nl == 0)
78 		nl = orig->_maxy - orig->_begy - by;
79 	if (nc == 0)
80 		nc = orig->_maxx - orig->_begx - bx;
81 	if ((win = makenew(nl, nc, by, bx)) == NULL)
82 		return ERR;
83 	j = orig->_begy + by;
84 	k = orig->_begx + bx;
85 	for (i = 0; i < nl; i++)
86 		win->_y[i] = &orig->_y[j++][k];
87 	win->_flags = _SUBWIN;
88 	return win;
89 }
90 
91 /*
92  *	This routine sets up a window buffer and returns a pointer to it.
93  */
94 static WINDOW *
95 makenew(num_lines, num_cols, begy, begx)
96 int	num_lines, num_cols, begy, begx; {
97 
98 	reg int		i;
99 	reg WINDOW	*win;
100 	reg int		by, bx, nl, nc;
101 
102 	by = begy;
103 	bx = begx;
104 	nl = num_lines;
105 	nc = num_cols;
106 
107 # ifdef	DEBUG
108 	fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
109 # endif
110 	if ((win = (WINDOW *) malloc(sizeof (WINDOW))) == NULL)
111 		return NULL;
112 # ifdef DEBUG
113 	fprintf(outf, "MAKENEW: nl = %d\n", nl);
114 # endif
115 	if ((win->_y = (char **) calloc(nl, sizeof (char *))) == NULL) {
116 		cfree(win);
117 		return (WINDOW *) NULL;
118 	}
119 	if ((win->_firstch = calloc(nl, sizeof (short))) == NULL) {
120 		cfree(win);
121 		cfree(win->_y);
122 	}
123 	if ((win->_lastch = calloc(nl, sizeof (short))) == NULL) {
124 		cfree(win);
125 		cfree(win->_y);
126 		cfree(win->_firstch);
127 	}
128 # ifdef DEBUG
129 	fprintf(outf, "MAKENEW: nc = %d\n", nc);
130 # endif
131 	win->_cury = win->_curx = 0;
132 	win->_clear = (nl == LINES && nc == COLS);
133 	win->_maxy = nl;
134 	win->_maxx = nc;
135 	win->_begy = by;
136 	win->_begx = bx;
137 	win->_scroll = win->_leave = FALSE;
138 	for (i = 0; i < nl; i++)
139 		win->_firstch[i] = win->_lastch[i] = _NOCHANGE;
140 	if (bx + nc == COLS) {
141 		win->_flags |= _ENDLINE;
142 		if (bx == 0 && nl == LINES && by == 0)
143 			win->_flags |= _FULLWIN;
144 		if (by + nl == LINES)
145 			win->_flags |= _SCROLLWIN;
146 	}
147 # ifdef DEBUG
148 	fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
149 	fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
150 	fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
151 	fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
152 	fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
153 	fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
154 	fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
155 	fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
156 # endif
157 	return win;
158 }
159