xref: /original-bsd/lib/libcurses/newwin.c (revision fbed46ce)
1 /*
2  * allocate space for and set up defaults for a new window
3  *
4  * 02/16/82 (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 + orig->_begy
76 	    || bx + nc > orig->_maxx + orig->_begx)
77 		return ERR;
78 	if (nl == 0)
79 		nl = orig->_maxy + orig->_begy - by;
80 	if (nc == 0)
81 		nc = orig->_maxx + orig->_begx - bx;
82 	if ((win = makenew(nl, nc, by, bx)) == NULL)
83 		return ERR;
84 	j = by - orig->_begy;
85 	k = bx - orig->_begx;
86 	for (i = 0; i < nl; i++)
87 		win->_y[i] = &orig->_y[j++][k];
88 	win->_flags = _SUBWIN;
89 	return win;
90 }
91 
92 /*
93  *	This routine sets up a window buffer and returns a pointer to it.
94  */
95 static WINDOW *
96 makenew(num_lines, num_cols, begy, begx)
97 int	num_lines, num_cols, begy, begx; {
98 
99 	reg int		i;
100 	reg WINDOW	*win;
101 	reg int		by, bx, nl, nc;
102 
103 	by = begy;
104 	bx = begx;
105 	nl = num_lines;
106 	nc = num_cols;
107 
108 # ifdef	DEBUG
109 	fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
110 # endif
111 	if ((win = (WINDOW *) calloc(1, sizeof (WINDOW))) == NULL)
112 		return NULL;
113 # ifdef DEBUG
114 	fprintf(outf, "MAKENEW: nl = %d\n", nl);
115 # endif
116 	if ((win->_y = (char **) calloc(nl, sizeof (char *))) == NULL) {
117 		cfree(win);
118 		return NULL;
119 	}
120 	if ((win->_firstch = calloc(nl, sizeof (short))) == NULL) {
121 		cfree(win);
122 		cfree(win->_y);
123 		return NULL;
124 	}
125 	if ((win->_lastch = calloc(nl, sizeof (short))) == NULL) {
126 		cfree(win);
127 		cfree(win->_y);
128 		cfree(win->_firstch);
129 		return NULL;
130 	}
131 # ifdef DEBUG
132 	fprintf(outf, "MAKENEW: nc = %d\n", nc);
133 # endif
134 	win->_cury = win->_curx = 0;
135 	win->_clear = (nl == LINES && nc == COLS);
136 	win->_maxy = nl;
137 	win->_maxx = nc;
138 	win->_begy = by;
139 	win->_begx = bx;
140 	win->_flags = 0;
141 	win->_scroll = win->_leave = FALSE;
142 	for (i = 0; i < nl; i++)
143 		win->_firstch[i] = win->_lastch[i] = _NOCHANGE;
144 	if (bx + nc == COLS) {
145 		win->_flags |= _ENDLINE;
146 		if (bx == 0 && nl == LINES && by == 0)
147 			win->_flags |= _FULLWIN;
148 		if (by + nl == LINES)
149 			win->_flags |= _SCROLLWIN;
150 	}
151 # ifdef DEBUG
152 	fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
153 	fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
154 	fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
155 	fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
156 	fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
157 	fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
158 	fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
159 	fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
160 # endif
161 	return win;
162 }
163