1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*LINTLIBRARY*/
#ifndef lint
static char
sccsid[] = "@(#)newwin.c 1.7 89/07/13 SMI"; /* from UCB 5.1 85/06/07 */
#endif /* not lint */
/*
* allocate space for and set up defaults for a new window
*
*/
#include "curses.ext"
#include <malloc.h>
#define SMALLOC (short *)malloc
/* forward declaration */
static WINDOW *makenew(int, int, int, int);
#undef nl /* don't need it here, and it interferes */
WINDOW *
newwin(int num_lines, int num_cols, int begy, int begx)
{
WINDOW *win;
char *sp;
int i, by, bx, nl, nc;
int j;
by = begy;
bx = begx;
nl = num_lines;
nc = num_cols;
if (nl == 0)
nl = LINES - by;
if (nc == 0)
nc = COLS - bx;
if ((win = makenew(nl, nc, by, bx)) == NULL)
return (ERR);
if ((win->_firstch = SMALLOC(nl * sizeof (win->_firstch[0]))) == NULL) {
free(win->_y);
free(win);
return (NULL);
}
if ((win->_lastch = SMALLOC(nl * sizeof (win->_lastch[0]))) == NULL) {
free(win->_y);
free(win->_firstch);
free(win);
return (NULL);
}
win->_nextp = win;
for (i = 0; i < nl; i++) {
win->_firstch[i] = _NOCHANGE;
win->_lastch[i] = _NOCHANGE;
}
for (i = 0; i < nl; i++)
if ((win->_y[i] = malloc(nc * sizeof (win->_y[0]))) == NULL) {
for (j = 0; j < i; j++)
free(win->_y[j]);
free(win->_firstch);
free(win->_lastch);
free(win->_y);
free(win);
return (ERR);
}
else
for (sp = win->_y[i]; sp < win->_y[i] + nc; )
*sp++ = ' ';
win->_ch_off = 0;
#ifdef DEBUG
fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
#endif
return (win);
}
WINDOW *
subwin(WINDOW *orig, int num_lines, int num_cols, int begy, int begx)
{
WINDOW *win;
int by, bx, nl, nc;
by = begy;
bx = begx;
nl = num_lines;
nc = num_cols;
/*
* make sure window fits inside the original one
*/
#ifdef DEBUG
fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
#endif
if (by < orig->_begy || bx < orig->_begx ||
by + nl > orig->_maxy + orig->_begy ||
bx + nc > orig->_maxx + orig->_begx)
return (ERR);
if (nl == 0)
nl = orig->_maxy + orig->_begy - by;
if (nc == 0)
nc = orig->_maxx + orig->_begx - bx;
if ((win = makenew(nl, nc, by, bx)) == NULL)
return (ERR);
win->_nextp = orig->_nextp;
orig->_nextp = win;
win->_orig = orig;
_set_subwin_(orig, win);
return (win);
}
/*
* this code is shared with mvwin()
*/
void
_set_subwin_(WINDOW *orig, WINDOW *win)
{
int i, j, k;
j = win->_begy - orig->_begy;
k = win->_begx - orig->_begx;
win->_ch_off = (short)k;
#ifdef DEBUG
fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
#endif
win->_firstch = &orig->_firstch[j];
win->_lastch = &orig->_lastch[j];
for (i = 0; i < win->_maxy; i++, j++)
win->_y[i] = &orig->_y[j][k];
}
/*
* This routine sets up a window buffer and returns a pointer to it.
*/
static WINDOW *
makenew(int num_lines, int num_cols, int begy, int begx)
{
WINDOW *win;
int by, bx, nl, nc;
by = begy;
bx = begx;
nl = num_lines;
nc = num_cols;
#ifdef DEBUG
fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
#endif
if ((win = (WINDOW *) malloc(sizeof (*win))) == NULL)
return (NULL);
#ifdef DEBUG
fprintf(outf, "MAKENEW: nl = %d\n", nl);
#endif
if ((win->_y = (char **)malloc(nl * sizeof (win->_y[0]))) == NULL) {
free(win);
return (NULL);
}
#ifdef DEBUG
fprintf(outf, "MAKENEW: nc = %d\n", nc);
#endif
win->_cury = win->_curx = 0;
win->_clear = FALSE;
win->_maxy = (short)nl;
win->_maxx = (short)nc;
win->_begy = (short)by;
win->_begx = (short)bx;
win->_flags = 0;
win->_scroll = win->_leave = FALSE;
_swflags_(win);
win->_orig = NULL;
#ifdef DEBUG
fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
#endif
return (win);
}
void
_swflags_(WINDOW *win)
{
win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
if (win->_begx + win->_maxx == COLS) {
win->_flags |= _ENDLINE;
if (win->_begx == 0) {
if (AL && DL)
win->_flags |= _FULLLINE;
if (win->_maxy == LINES && win->_begy == 0)
win->_flags |= _FULLWIN;
}
if (win->_begy + win->_maxy == LINES)
win->_flags |= _SCROLLWIN;
}
}
|