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 (Juergen.Pfeifer@T-Online.de)
27 MODULE_ID("$Id: cursesw.cc,v 1.5 1997/05/05 20:04:59 tom Exp $")
29 #pragma implementation
33 #define COLORS_NEED_INITIALIZATION -1
34 #define COLORS_NOT_INITIALIZED 0
35 #define COLORS_MONOCHROME 1
36 #define COLORS_ARE_REALLY_THERE 2
39 // declare static variables for the class
40 int NCursesWindow::count = 0;
43 NCursesWindow::scanw(const char* fmt, ...)
49 int result = wgetstr(w, buf);
51 strstreambuf ss(buf, BUFSIZ);
52 result = ss.vscan(fmt, args);
63 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
69 int result = wmove(w, y, x);
71 result = wgetstr(w, buf);
73 strstreambuf ss(buf, BUFSIZ);
74 result = ss.vscan(fmt, args);
86 NCursesWindow::printw(const char * fmt, ...)
91 vsprintf(buf, fmt, args);
93 return waddstr(w, buf);
98 NCursesWindow::printw(int y, int x, const char * fmt, ...)
102 int result = wmove(w, y, x);
105 vsprintf(buf, fmt, args);
106 result = waddstr(w, buf);
114 NCursesWindow::init(void)
123 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
125 THROW(new NCursesException(msg));
129 NCursesWindow::initialize() {
131 if (colorInitialized==COLORS_NEED_INITIALIZATION) {
132 colorInitialized=COLORS_NOT_INITIALIZED;
139 NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
144 w = ::newwin(lines, cols, begin_y, begin_x);
146 err_handler("Cannot construct window");
151 subwins = par = sib = 0;
155 NCursesWindow::NCursesWindow(WINDOW* &window)
163 subwins = par = sib = 0;
168 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
169 int by, int bx, char absrel)
171 if (absrel == 'r') { // relative origin
176 // Even though we treat subwindows as a tree, the standard curses
177 // library needs the `subwin' call to link to the root in
178 // order to correctly perform refreshes, etc.
180 NCursesWindow* root = &win;
181 while (root->par != 0) root = root->par;
183 w = subwin(root->w, l, c, by, bx);
185 err_handler("Cannot construct subwindow");
197 NCursesWindow::isDescendant(NCursesWindow& win) {
198 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
202 if (p->isDescendant(win))
210 NCursesWindow::kill_subwindows()
212 for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
213 p->kill_subwindows();
219 p->w = 0; // cause a run-time error if anyone attempts to use...
224 NCursesWindow::~NCursesWindow()
228 if (par != 0) { // Snip us from the parent's list of subwindows.
229 NCursesWindow * win = par->subwins;
230 NCursesWindow * trail = 0;
234 else if (win == this) {
236 trail->sib = win->sib;
238 par->subwins = win->sib;
247 if (alloced && w != 0)
253 else if (count < 0) { // cannot happen!
254 err_handler("Too many windows destroyed");
258 // ---------------------------------------------------------------------
261 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
264 NCursesWindow::useColors(void)
266 if (colorInitialized == COLORS_NOT_INITIALIZED) {
270 colorInitialized = COLORS_ARE_REALLY_THERE;
273 colorInitialized = COLORS_MONOCHROME;
276 colorInitialized = COLORS_NEED_INITIALIZATION;
281 NCursesWindow::getcolor(int getback) const
285 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
286 if (pair_content(PAIR_NUMBER(w->_attrs), &fore, &back))
287 err_handler("Can't get color pair");
290 // Monochrome means white on black
294 return getback ? back : fore;
297 int NCursesWindow::NumberOfColors()
299 if (colorInitialized==COLORS_ARE_REALLY_THERE)
302 return 1; // monochrome (actually there are two ;-)
306 NCursesWindow::getcolor() const
308 if (colorInitialized==COLORS_ARE_REALLY_THERE)
309 return PAIR_NUMBER(w->_attrs);
311 return 0; // we only have pair zero
315 NCursesWindow::setpalette(short fore, short back, short pair)
317 if (colorInitialized==COLORS_ARE_REALLY_THERE)
318 return init_pair(pair, fore, back);
324 NCursesWindow::setpalette(short fore, short back)
326 if (colorInitialized==COLORS_ARE_REALLY_THERE)
327 return setpalette(fore, back, PAIR_NUMBER(w->_attrs));
334 NCursesWindow::setcolor(short pair)
336 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
337 if ((pair < 1) || (pair > COLOR_PAIRS))
338 err_handler("Can't set color pair");
341 attrset(COLOR_PAIR(pair));