1
1
// * this is for making emacs happy: -*-Mode: C++;-*-
2
/****************************************************************************
3
* Copyright (c) 2007,2008 Free Software Foundation, Inc. *
5
* Permission is hereby granted, free of charge, to any person obtaining a *
6
* copy of this software and associated documentation files (the *
7
* "Software"), to deal in the Software without restriction, including *
8
* without limitation the rights to use, copy, modify, merge, publish, *
9
* distribute, distribute with modifications, sublicense, and/or sell *
10
* copies of the Software, and to permit persons to whom the Software is *
11
* furnished to do so, subject to the following conditions: *
13
* The above copyright notice and this permission notice shall be included *
14
* in all copies or substantial portions of the Software. *
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24
* Except as contained in this notice, the name(s) of the above copyright *
25
* holders shall not be used in advertising or otherwise to promote the *
26
* sale, use or other dealings in this Software without prior written *
28
****************************************************************************/
4
Copyright (C) 1989 Free Software Foundation
5
written by Eric Newton (newton@rocky.oswego.edu)
7
This file is part of the GNU C++ Library. This library is free
8
software; you can redistribute it and/or modify it under the terms of
9
the GNU Library General Public License as published by the Free
10
Software Foundation; either version 2 of the License, or (at your
11
option) any later version. This library is distributed in the hope
12
that it will be useful, but WITHOUT ANY WARRANTY; without even the
13
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
PURPOSE. See the GNU Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with this library; if not, write to the Free Software
17
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
modified by Ulrich Drepper (drepper@karlsruhe.gmd.de)
20
and Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
22
modified by Juergen Pfeifer
23
and Thomas Dickey (noting that more than 70% of this file has been changed)
35
* The NCursesWindow class was originally based on a file written by
36
* Eric Newton, later modified by Ulrich Drepper and Anatoly Ivasyuk.
37
* However, aside from the compatible interface definition, no trace
38
* of the original code remains in this version: it consists only of
39
* changes introduced since 1995.
26
42
#include "internal.h"
27
43
#include "cursesw.h"
29
MODULE_ID("$Id: cursesw.cc,v 1.32 2005/08/13 18:12:17 tom Exp $")
45
MODULE_ID("$Id: cursesw.cc,v 1.50 2008/11/23 00:17:31 juergen Exp $")
31
47
#define COLORS_NEED_INITIALIZATION -1
32
48
#define COLORS_NOT_INITIALIZED 0
33
49
#define COLORS_MONOCHROME 1
34
50
#define COLORS_ARE_REALLY_THERE 2
52
#define HaveColors() (colorInitialized == COLORS_ARE_REALLY_THERE)
36
54
// declare static variables for the class
37
55
long NCursesWindow::count = 0L;
38
56
bool NCursesWindow::b_initialized = FALSE;
41
* The ncurses library has a fallback for vsscanf(), which may work...
43
#if !(USE_STRSTREAM_VSCAN || USE_STRSTREAM_VSCAN_CAST)
44
# undef USE_STDIO_VSCAN
45
# define USE_STDIO_VSCAN 1
50
# define _IO_va_list char *
55
59
NCursesWindow::scanw(const char* fmt, ...)
60
if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
65
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
73
NCursesWindow::scanw(int y, int x, const char* fmt, ...)
77
if (::wmove(w, y, x) != ERR) {
62
79
va_start(args, fmt);
64
if (::vsscanf(buf, fmt, args) != -1)
66
#elif USE_STRSTREAM_VSCAN /* powerpc, os390 */
67
strstreambuf ss(buf, sizeof(buf));
68
if (ss.vscan(fmt, args) != -1)
70
#elif USE_STRSTREAM_VSCAN_CAST /* pre-gcc 3.0 */
71
strstreambuf ss(buf, sizeof(buf));
72
if (ss.vscan(fmt, (_IO_va_list)args) != -1)
80
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
82
NCursesWindow::scanw(int y, int x, const char* fmt, ...)
88
NCursesWindow::scanw(const char* fmt, va_list args)
92
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
99
NCursesWindow::scanw(int y, int x, const char* fmt, va_list args)
87
103
if (::wmove(w, y, x) != ERR) {
88
if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
92
if (::vsscanf(buf, fmt, args) != -1)
94
#elif USE_STRSTREAM_VSCAN /* powerpc, os390 */
95
strstreambuf ss(buf, sizeof(buf));
96
if (ss.vscan(fmt, args) != -1)
98
#elif USE_STRSTREAM_VSCAN_CAST /* pre-gcc 3.0 */
99
strstreambuf ss(buf, sizeof(buf));
100
if (ss.vscan(fmt, (_IO_va_list)args) != -1)
104
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
126
125
va_start(args, fmt);
127
126
int result = ::wmove(w, y, x);
128
127
if (result == OK) {
130
::vsprintf(buf, fmt, args);
131
result = waddstr(w, buf);
128
result = ::vw_printw(w, fmt, args);
136
NCursesWindow::printw(const char * fmt, va_list args)
138
int result = ::vw_printw(w, fmt, args);
144
NCursesWindow::printw(int y, int x, const char * fmt, va_list args)
146
int result = ::wmove(w, y, x);
148
result = ::vw_printw(w, fmt, args);
139
NCursesWindow::init(void)
155
NCursesWindow::set_keyboard(void)
153
168
NCursesWindow::initialize()
155
if (!b_initialized) {
157
b_initialized = TRUE;
158
if (colorInitialized==COLORS_NEED_INITIALIZATION) {
159
colorInitialized=COLORS_NOT_INITIALIZED;
170
if (!b_initialized) {
172
b_initialized = TRUE;
173
if (colorInitialized == COLORS_NEED_INITIALIZATION) {
174
colorInitialized = COLORS_NOT_INITIALIZED;
183
NCursesWindow::constructing()
167
189
NCursesWindow::NCursesWindow()
168
: w(0), alloced(0), par(0), subwins(0), sib(0)
190
: w(0), alloced(FALSE), par(0), subwins(0), sib(0)
172
w = static_cast<WINDOW *>(0);
175
subwins = par = sib = 0;
194
w = static_cast<WINDOW *>(0);
179
198
NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
180
: w(0), alloced(0), par(0), subwins(0), sib(0)
199
: w(0), alloced(TRUE), par(0), subwins(0), sib(0)
184
203
w = ::newwin(nlines, ncols, begin_y, begin_x);
186
205
err_handler("Cannot construct window");
191
subwins = par = sib = 0;
195
NCursesWindow::NCursesWindow(WINDOW* &window)
196
: w(0), alloced(0), par(0), subwins(0), sib(0)
210
NCursesWindow::NCursesWindow(WINDOW* window)
211
: w(0), alloced(FALSE), par(0), subwins(0), sib(0)
203
subwins = par = sib = 0;
215
// We used to use a reference on the "window" parameter, but we cannot do
216
// that with an opaque pointer (see NCURSES_OPAQUE). If the parameter was
217
// "::stdscr", that is first set via the "constructing() call, and is null
218
// up to that point. So we allow a null pointer here as meaning the "same"
220
w = window ? window : ::stdscr;
207
NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
224
NCursesWindow::NCursesWindow(NCursesWindow& win, int ny, int nx,
208
225
int begin_y, int begin_x, char absrel)
209
: w(0), alloced(0), par(0), subwins(0), sib(0)
226
: w(0), alloced(TRUE), par(0), subwins(0), sib(0)
212
if (absrel == 'a') { // absolute origin
229
if (absrel == 'a') { // absolute origin
213
230
begin_y -= win.begy();
214
231
begin_x -= win.begx();
217
// Even though we treat subwindows as a tree, the standard curses
218
// library needs the `subwin' call to link to the parent in
219
// order to correctly perform refreshes, etc.
220
// Friendly enough, this also works for pads.
221
w = ::derwin(win.w, l, c, begin_y, begin_x);
234
// Link this window into its parent's list of subwindows.
235
// We use derwin(), since this also works for pads.
236
w = ::derwin(win.w, ny, nx, begin_y, begin_x);
223
238
err_handler("Cannot construct subwindow");
227
242
sib = win.subwins;
228
243
win.subwins = this;
234
246
NCursesWindow::NCursesWindow(NCursesWindow& win,
235
247
bool do_box NCURSES_PARAM_INIT(TRUE))
236
: w(0), alloced(0), par(0), subwins(0), sib(0)
248
: w(0), alloced(TRUE), par(0), subwins(0), sib(0)
239
int myHeight = win.height();
240
int myWidth = win.width();
241
w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
243
err_handler("Cannot construct subwindow");
251
int myHeight = win.height();
252
int myWidth = win.width();
253
w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
255
err_handler("Cannot construct subwindow");
259
269
NCursesWindow NCursesWindow::Clone()
261
WINDOW *d = ::dupwin(w);
271
WINDOW *d = ::dupwin(w);
270
280
typedef int (*RIPOFFINIT)(NCursesWindow&);
273
283
static RIPOFFINIT* prip = R_INIT;
275
285
NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
276
: w(0), alloced(0), par(0), subwins(0), sib(0)
286
: w(0), alloced(FALSE), par(0), subwins(0), sib(0)
280
assert((w->_maxx+1)==ncols);
282
subwins = par = sib = 0;
285
292
int _nc_xx_ripoff_init(WINDOW *w, int ncols)
289
RIPOFFINIT init = *prip++;
291
NCursesWindow* W = new NCursesWindow(w,ncols);
296
RIPOFFINIT init = *prip++;
298
NCursesWindow* W = new NCursesWindow(w,ncols);
297
304
int NCursesWindow::ripoffline(int ripoff_lines,
298
305
int (*init)(NCursesWindow& win))
300
int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
301
if (code==OK && init && ripoff_lines) {
302
R_INIT[r_init_idx++] = init;
307
int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
308
if (code == OK && init && ripoff_lines) {
309
R_INIT[r_init_idx++] = init;
308
315
NCursesWindow::isDescendant(NCursesWindow& win)
310
for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
314
if (p->isDescendant(win))
319
for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
320
if (p == &win || p->isDescendant(win)) {
322
329
NCursesWindow::kill_subwindows()
324
for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
331
NCursesWindow* p = subwins;
335
NCursesWindow* q = p->sib;
325
336
p->kill_subwindows();
326
337
if (p->alloced) {
331
p->w = 0; // cause a run-time error if anyone attempts to use...
379
388
NCursesWindow::useColors(void)
381
390
if (colorInitialized == COLORS_NOT_INITIALIZED) {
383
if (::has_colors()) {
385
colorInitialized = COLORS_ARE_REALLY_THERE;
392
if (::has_colors()) {
394
colorInitialized = COLORS_ARE_REALLY_THERE;
396
colorInitialized = COLORS_MONOCHROME;
399
colorInitialized = COLORS_NEED_INITIALIZATION;
388
colorInitialized = COLORS_MONOCHROME;
391
colorInitialized = COLORS_NEED_INITIALIZATION;
405
NCursesWindow::getPair() const
407
return static_cast<short>(PAIR_NUMBER(getattrs(w)));
396
411
NCursesWindow::getcolor(int getback) const
398
413
short fore, back;
400
if (colorInitialized==COLORS_ARE_REALLY_THERE) {
401
if (::pair_content(static_cast<short>(PAIR_NUMBER(w->_attrs)), &fore, &back))
402
err_handler("Can't get color pair");
405
// Monochrome means white on black
416
if (::pair_content(getPair(), &fore, &back) == ERR)
417
err_handler("Can't get color pair");
419
// Monochrome means white on black
409
423
return getback ? back : fore;
412
426
int NCursesWindow::NumberOfColors()
414
if (colorInitialized==COLORS_ARE_REALLY_THERE)
417
return 1; // monochrome (actually there are two ;-)
428
return (HaveColors()) ? COLORS : 1;
421
432
NCursesWindow::getcolor() const
423
if (colorInitialized==COLORS_ARE_REALLY_THERE)
424
return PAIR_NUMBER(w->_attrs);
426
return 0; // we only have pair zero
434
return (HaveColors()) ? getPair() : 0;
430
438
NCursesWindow::setpalette(short fore, short back, short pair)
432
if (colorInitialized==COLORS_ARE_REALLY_THERE)
433
return ::init_pair(pair, fore, back);
440
return (HaveColors()) ? ::init_pair(pair, fore, back) : OK;
439
444
NCursesWindow::setpalette(short fore, short back)
441
if (colorInitialized==COLORS_ARE_REALLY_THERE)
442
return setpalette(fore, back, static_cast<short>(PAIR_NUMBER(w->_attrs)));
446
return setpalette(fore, back, getPair());
449
451
NCursesWindow::setcolor(short pair)
451
if (colorInitialized==COLORS_ARE_REALLY_THERE) {
452
if ((pair < 1) || (pair > COLOR_PAIRS))
453
err_handler("Can't set color pair");
454
if ((pair < 1) || (pair > COLOR_PAIRS))
455
err_handler("Can't set color pair");
456
attrset(COLOR_PAIR(pair));
458
attrset(COLOR_PAIR(pair));
462
464
bool NCursesWindow::has_mouse() const
464
return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
466
return ((::has_key(KEY_MOUSE) || ::has_mouse())