Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmpdcurses/pdcurses/pad.c
3153 views
1
/* PDCurses */
2
3
#include <curspriv.h>
4
5
/*man-start**************************************************************
6
7
pad
8
---
9
10
### Synopsis
11
12
WINDOW *newpad(int nlines, int ncols);
13
WINDOW *subpad(WINDOW *orig, int nlines, int ncols,
14
int begy, int begx);
15
int prefresh(WINDOW *win, int py, int px, int sy1, int sx1,
16
int sy2, int sx2);
17
int pnoutrefresh(WINDOW *w, int py, int px, int sy1, int sx1,
18
int sy2, int sx2);
19
int pechochar(WINDOW *pad, chtype ch);
20
int pecho_wchar(WINDOW *pad, const cchar_t *wch);
21
22
bool is_pad(const WINDOW *pad);
23
24
### Description
25
26
A pad is a special kind of window, which is not restricted by the
27
screen size, and is not necessarily associated with a particular part
28
of the screen. You can use a pad when you need a large window, and
29
only a part of the window will be on the screen at one time. Pads are
30
not refreshed automatically (e.g., from scrolling or echoing of
31
input). You can't call wrefresh() with a pad as an argument; use
32
prefresh() or pnoutrefresh() instead. Note that these routines
33
require additional parameters to specify the part of the pad to be
34
displayed, and the location to use on the screen.
35
36
newpad() creates a new pad data structure.
37
38
subpad() creates a new sub-pad within a pad, at position (begy,
39
begx), with dimensions of nlines lines and ncols columns. This
40
position is relative to the pad, and not to the screen as with
41
subwin. Changes to either the parent pad or sub-pad will affect both.
42
When using sub-pads, you may need to call touchwin() before calling
43
prefresh().
44
45
pnoutrefresh() copies the specified pad to the virtual screen.
46
47
prefresh() calls pnoutrefresh(), followed by doupdate().
48
49
These routines are analogous to wnoutrefresh() and wrefresh(). (py,
50
px) specifies the upper left corner of the part of the pad to be
51
displayed; (sy1, sx1) and (sy2, sx2) describe the screen rectangle
52
that will contain the selected part of the pad.
53
54
pechochar() is functionally equivalent to addch() followed by a call
55
to prefresh(), with the last-used coordinates and dimensions.
56
pecho_wchar() is the wide-character version.
57
58
is_pad() reports whether the specified window is a pad.
59
60
### Return Value
61
62
All functions except is_pad() return OK on success and ERR on error.
63
64
### Portability
65
X/Open ncurses NetBSD
66
newpad Y Y Y
67
subpad Y Y Y
68
prefresh Y Y Y
69
pnoutrefresh Y Y Y
70
pechochar Y Y Y
71
pecho_wchar Y Y Y
72
is_pad - Y Y
73
74
**man-end****************************************************************/
75
76
#include <string.h>
77
78
/* save values for pechochar() */
79
80
static int save_pminrow, save_pmincol;
81
static int save_sminrow, save_smincol, save_smaxrow, save_smaxcol;
82
83
WINDOW *newpad(int nlines, int ncols)
84
{
85
WINDOW *win;
86
87
PDC_LOG(("newpad() - called: lines=%d cols=%d\n", nlines, ncols));
88
89
win = PDC_makenew(nlines, ncols, 0, 0);
90
if (win)
91
win = PDC_makelines(win);
92
93
if (!win)
94
return (WINDOW *)NULL;
95
96
werase(win);
97
98
win->_flags = _PAD;
99
100
/* save default values in case pechochar() is the first call to
101
prefresh(). */
102
103
save_pminrow = 0;
104
save_pmincol = 0;
105
save_sminrow = 0;
106
save_smincol = 0;
107
save_smaxrow = min(LINES, nlines) - 1;
108
save_smaxcol = min(COLS, ncols) - 1;
109
110
return win;
111
}
112
113
WINDOW *subpad(WINDOW *orig, int nlines, int ncols, int begy, int begx)
114
{
115
WINDOW *win;
116
int i;
117
118
PDC_LOG(("subpad() - called: lines=%d cols=%d begy=%d begx=%d\n",
119
nlines, ncols, begy, begx));
120
121
if (!orig || !(orig->_flags & _PAD))
122
return (WINDOW *)NULL;
123
124
/* make sure window fits inside the original one */
125
126
if (begy < 0 || begx < 0 ||
127
(begy + nlines) > orig->_maxy ||
128
(begx + ncols) > orig->_maxx)
129
return (WINDOW *)NULL;
130
131
if (!nlines)
132
nlines = orig->_maxy - begy;
133
134
if (!ncols)
135
ncols = orig->_maxx - begx;
136
137
win = PDC_makenew(nlines, ncols, begy, begx);
138
if (!win)
139
return (WINDOW *)NULL;
140
141
/* initialize window variables */
142
143
win->_attrs = orig->_attrs;
144
win->_leaveit = orig->_leaveit;
145
win->_scroll = orig->_scroll;
146
win->_nodelay = orig->_nodelay;
147
win->_use_keypad = orig->_use_keypad;
148
win->_parent = orig;
149
150
for (i = 0; i < nlines; i++)
151
win->_y[i] = orig->_y[begy + i] + begx;
152
153
win->_flags = _SUBPAD;
154
155
/* save default values in case pechochar() is the first call
156
to prefresh(). */
157
158
save_pminrow = 0;
159
save_pmincol = 0;
160
save_sminrow = 0;
161
save_smincol = 0;
162
save_smaxrow = min(LINES, nlines) - 1;
163
save_smaxcol = min(COLS, ncols) - 1;
164
165
return win;
166
}
167
168
int prefresh(WINDOW *win, int py, int px, int sy1, int sx1, int sy2, int sx2)
169
{
170
PDC_LOG(("prefresh() - called\n"));
171
172
if (pnoutrefresh(win, py, px, sy1, sx1, sy2, sx2) == ERR)
173
return ERR;
174
175
doupdate();
176
return OK;
177
}
178
179
int pnoutrefresh(WINDOW *w, int py, int px, int sy1, int sx1, int sy2, int sx2)
180
{
181
int num_cols;
182
int sline;
183
int pline;
184
185
PDC_LOG(("pnoutrefresh() - called\n"));
186
187
if (py < 0)
188
py = 0;
189
if (px < 0)
190
px = 0;
191
if (sy1 < 0)
192
sy1 = 0;
193
if (sx1 < 0)
194
sx1 = 0;
195
196
if ((!w || !(w->_flags & (_PAD|_SUBPAD)) ||
197
(sy2 >= LINES) || (sx2 >= COLS)) ||
198
(sy2 < sy1) || (sx2 < sx1))
199
return ERR;
200
201
sline = sy1;
202
pline = py;
203
204
num_cols = min((sx2 - sx1 + 1), (w->_maxx - px));
205
206
while (sline <= sy2)
207
{
208
if (pline < w->_maxy)
209
{
210
memcpy(curscr->_y[sline] + sx1, w->_y[pline] + px,
211
num_cols * sizeof(chtype));
212
213
if ((curscr->_firstch[sline] == _NO_CHANGE)
214
|| (curscr->_firstch[sline] > sx1))
215
curscr->_firstch[sline] = sx1;
216
217
if (sx2 > curscr->_lastch[sline])
218
curscr->_lastch[sline] = sx2;
219
220
w->_firstch[pline] = _NO_CHANGE; /* updated now */
221
w->_lastch[pline] = _NO_CHANGE; /* updated now */
222
}
223
224
sline++;
225
pline++;
226
}
227
228
if (w->_clear)
229
{
230
w->_clear = FALSE;
231
curscr->_clear = TRUE;
232
}
233
234
/* position the cursor to the pad's current position if possible --
235
is the pad current position going to end up displayed? if not,
236
then don't move the cursor; if so, move it to the correct place */
237
238
if (!w->_leaveit && w->_cury >= py && w->_curx >= px &&
239
w->_cury <= py + (sy2 - sy1) && w->_curx <= px + (sx2 - sx1))
240
{
241
curscr->_cury = (w->_cury - py) + sy1;
242
curscr->_curx = (w->_curx - px) + sx1;
243
}
244
245
return OK;
246
}
247
248
int pechochar(WINDOW *pad, chtype ch)
249
{
250
PDC_LOG(("pechochar() - called\n"));
251
252
if (waddch(pad, ch) == ERR)
253
return ERR;
254
255
return prefresh(pad, save_pminrow, save_pmincol, save_sminrow,
256
save_smincol, save_smaxrow, save_smaxcol);
257
}
258
259
#ifdef PDC_WIDE
260
int pecho_wchar(WINDOW *pad, const cchar_t *wch)
261
{
262
PDC_LOG(("pecho_wchar() - called\n"));
263
264
if (!wch || (waddch(pad, *wch) == ERR))
265
return ERR;
266
267
return prefresh(pad, save_pminrow, save_pmincol, save_sminrow,
268
save_smincol, save_smaxrow, save_smaxcol);
269
}
270
#endif
271
272
bool is_pad(const WINDOW *pad)
273
{
274
PDC_LOG(("is_pad() - called\n"));
275
276
if (!pad)
277
return FALSE;
278
279
return (pad->_flags & _PAD) ? TRUE : FALSE;
280
}
281
282