1 // * this is for making emacs happy: -*-Mode: C++;-*-
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 Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en
29 MODULE_ID("$Id: cursesw.cc,v 1.25 2002/07/06 15:47:52 juergen Exp $")
31 #define COLORS_NEED_INITIALIZATION -1
32 #define COLORS_NOT_INITIALIZED 0
33 #define COLORS_MONOCHROME 1
34 #define COLORS_ARE_REALLY_THERE 2
36 // declare static variables for the class
37 long NCursesWindow::count = 0L;
38 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 NCursesWindow::scanw(const char* fmt, ...)
60 if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
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)
82 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
87 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)
111 NCursesWindow::printw(const char * fmt, ...)
116 ::vsprintf(buf, fmt, args);
118 return waddstr(w, buf);
123 NCursesWindow::printw(int y, int x, const char * fmt, ...)
127 int result = ::wmove(w, y, x);
130 ::vsprintf(buf, fmt, args);
131 result = waddstr(w, buf);
139 NCursesWindow::init(void)
147 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
149 THROW(new NCursesException(msg));
153 NCursesWindow::initialize() {
154 if (!b_initialized) {
156 b_initialized = TRUE;
157 if (colorInitialized==COLORS_NEED_INITIALIZATION) {
158 colorInitialized=COLORS_NOT_INITIALIZED;
166 NCursesWindow::NCursesWindow() {
172 subwins = par = sib = 0;
176 NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
180 w = ::newwin(lines, cols, begin_y, begin_x);
182 err_handler("Cannot construct window");
187 subwins = par = sib = 0;
191 NCursesWindow::NCursesWindow(WINDOW* &window)
198 subwins = par = sib = 0;
202 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
203 int begin_y, int begin_x, char absrel)
206 if (absrel == 'a') { // absolute origin
207 begin_y -= win.begy();
208 begin_x -= win.begx();
211 // Even though we treat subwindows as a tree, the standard curses
212 // library needs the `subwin' call to link to the parent in
213 // order to correctly perform refreshes, etc.
214 // Friendly enough, this also works for pads.
215 w = ::derwin(win.w, l, c, begin_y, begin_x);
217 err_handler("Cannot construct subwindow");
228 NCursesWindow::NCursesWindow(NCursesWindow& win,
229 bool do_box NCURSES_PARAM_INIT(TRUE))
232 w = :: derwin(win.w,win.height()-2,win.width()-2,1,1);
234 err_handler("Cannot construct subwindow");
250 NCursesWindow NCursesWindow::Clone() {
251 WINDOW *d = ::dupwin(w);
260 typedef int (*RIPOFFINIT)(NCursesWindow&);
261 static RIPOFFINIT R_INIT[5]; // There can't be more
262 static int r_init_idx = 0;
263 static RIPOFFINIT* prip = R_INIT;
265 extern "C" int _nc_ripoffline(int,int (*init)(WINDOW*,int));
267 NCursesWindow::NCursesWindow(WINDOW *win, int cols) {
270 assert((w->_maxx+1)==cols);
272 subwins = par = sib = 0;
275 int NCursesWindow::ripoff_init(WINDOW *w, int cols)
279 RIPOFFINIT init = *prip++;
281 NCursesWindow* W = new NCursesWindow(w,cols);
287 int NCursesWindow::ripoffline(int ripoff_lines,
288 int (*init)(NCursesWindow& win)) {
289 int code = ::_nc_ripoffline(ripoff_lines,ripoff_init);
290 if (code==OK && init && ripoff_lines) {
291 R_INIT[r_init_idx++] = init;
297 NCursesWindow::isDescendant(NCursesWindow& win) {
298 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
302 if (p->isDescendant(win))
310 NCursesWindow::kill_subwindows()
312 for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
313 p->kill_subwindows();
319 p->w = 0; // cause a run-time error if anyone attempts to use...
324 NCursesWindow::~NCursesWindow()
328 if (par != 0) { // Snip us from the parent's list of subwindows.
329 NCursesWindow * win = par->subwins;
330 NCursesWindow * trail = 0;
334 else if (win == this) {
336 trail->sib = win->sib;
338 par->subwins = win->sib;
347 if (alloced && w != 0)
355 else if (count < 0) { // cannot happen!
356 err_handler("Too many windows destroyed");
361 // ---------------------------------------------------------------------
364 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
367 NCursesWindow::useColors(void)
369 if (colorInitialized == COLORS_NOT_INITIALIZED) {
371 if (::has_colors()) {
373 colorInitialized = COLORS_ARE_REALLY_THERE;
376 colorInitialized = COLORS_MONOCHROME;
379 colorInitialized = COLORS_NEED_INITIALIZATION;
384 NCursesWindow::getcolor(int getback) const
388 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
389 if (::pair_content((short)PAIR_NUMBER(w->_attrs), &fore, &back))
390 err_handler("Can't get color pair");
393 // Monochrome means white on black
397 return getback ? back : fore;
400 int NCursesWindow::NumberOfColors()
402 if (colorInitialized==COLORS_ARE_REALLY_THERE)
405 return 1; // monochrome (actually there are two ;-)
409 NCursesWindow::getcolor() const
411 if (colorInitialized==COLORS_ARE_REALLY_THERE)
412 return PAIR_NUMBER(w->_attrs);
414 return 0; // we only have pair zero
418 NCursesWindow::setpalette(short fore, short back, short pair)
420 if (colorInitialized==COLORS_ARE_REALLY_THERE)
421 return ::init_pair(pair, fore, back);
427 NCursesWindow::setpalette(short fore, short back)
429 if (colorInitialized==COLORS_ARE_REALLY_THERE)
430 return setpalette(fore, back, (short)PAIR_NUMBER(w->_attrs));
437 NCursesWindow::setcolor(short pair)
439 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
440 if ((pair < 1) || (pair > COLOR_PAIRS))
441 err_handler("Can't set color pair");
444 attrset(COLOR_PAIR(pair));
450 extern "C" int _nc_has_mouse(void);
452 bool NCursesWindow::has_mouse() const {
453 return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())