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)
28 MODULE_ID("$Id: cursesw.cc,v 1.10 1997/10/20 21:26:35 juergen Exp $")
30 #define COLORS_NEED_INITIALIZATION -1
31 #define COLORS_NOT_INITIALIZED 0
32 #define COLORS_MONOCHROME 1
33 #define COLORS_ARE_REALLY_THERE 2
35 // declare static variables for the class
36 long NCursesWindow::count = 0L;
37 bool NCursesWindow::b_initialized = FALSE;
40 NCursesWindow::scanw(const char* fmt, ...)
46 int result = wgetstr(w, buf);
48 strstreambuf ss(buf, BUFSIZ);
49 result = ss.vscan(fmt, args);
60 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
66 int result = wmove(w, y, x);
68 result = wgetstr(w, buf);
70 strstreambuf ss(buf, BUFSIZ);
71 result = ss.vscan(fmt, args);
83 NCursesWindow::printw(const char * fmt, ...)
88 vsprintf(buf, fmt, args);
90 return waddstr(w, buf);
95 NCursesWindow::printw(int y, int x, const char * fmt, ...)
99 int result = wmove(w, y, x);
102 vsprintf(buf, fmt, args);
103 result = waddstr(w, buf);
111 NCursesWindow::init(void)
119 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
121 THROW(new NCursesException(msg));
125 NCursesWindow::initialize() {
126 if (!b_initialized) {
128 b_initialized = TRUE;
129 if (colorInitialized==COLORS_NEED_INITIALIZATION) {
130 colorInitialized=COLORS_NOT_INITIALIZED;
138 NCursesWindow::NCursesWindow() {
145 subwins = par = sib = 0;
149 NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
154 w = ::newwin(lines, cols, begin_y, begin_x);
156 err_handler("Cannot construct window");
161 subwins = par = sib = 0;
165 NCursesWindow::NCursesWindow(WINDOW* &window)
173 subwins = par = sib = 0;
177 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
178 int begin_y, int begin_x, char absrel)
180 if (absrel == 'a') { // absolute origin
181 begin_y -= win.begy();
182 begin_x -= win.begx();
185 // Even though we treat subwindows as a tree, the standard curses
186 // library needs the `subwin' call to link to the parent in
187 // order to correctly perform refreshes, etc.
188 // Friendly enough, this also works for pads.
189 w = ::derwin(win.w, l, c, begin_y, begin_x);
191 err_handler("Cannot construct subwindow");
202 NCursesWindow NCursesWindow::Clone() {
203 WINDOW *d = ::dupwin(w);
212 typedef int (*RIPOFFINIT)(NCursesWindow&);
213 static RIPOFFINIT R_INIT[5]; // There can't be more
214 static int r_init_idx = 0;
215 static RIPOFFINIT* prip = R_INIT;
217 extern "C" int _nc_ripoffline(int,int (*init)(WINDOW*,int));
219 NCursesWindow::NCursesWindow(WINDOW *win, int cols) {
221 assert((w->_maxx+1)==cols);
223 subwins = par = sib = 0;
226 int NCursesWindow::ripoff_init(WINDOW *w, int cols)
230 RIPOFFINIT init = *prip++;
232 NCursesWindow* W = new NCursesWindow(w,cols);
238 int NCursesWindow::ripoffline(int ripoff_lines,
239 int (*init)(NCursesWindow& win)) {
240 int code = ::_nc_ripoffline(ripoff_lines,ripoff_init);
241 if (code==OK && init && ripoff_lines) {
242 R_INIT[r_init_idx++] = init;
248 NCursesWindow::isDescendant(NCursesWindow& win) {
249 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
253 if (p->isDescendant(win))
261 NCursesWindow::kill_subwindows()
263 for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
264 p->kill_subwindows();
270 p->w = 0; // cause a run-time error if anyone attempts to use...
275 NCursesWindow::~NCursesWindow()
279 if (par != 0) { // Snip us from the parent's list of subwindows.
280 NCursesWindow * win = par->subwins;
281 NCursesWindow * trail = 0;
285 else if (win == this) {
287 trail->sib = win->sib;
289 par->subwins = win->sib;
298 if (alloced && w != 0)
306 else if (count < 0) { // cannot happen!
307 err_handler("Too many windows destroyed");
312 // ---------------------------------------------------------------------
315 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
318 NCursesWindow::useColors(void)
320 if (colorInitialized == COLORS_NOT_INITIALIZED) {
322 if (::has_colors()) {
324 colorInitialized = COLORS_ARE_REALLY_THERE;
327 colorInitialized = COLORS_MONOCHROME;
330 colorInitialized = COLORS_NEED_INITIALIZATION;
335 NCursesWindow::getcolor(int getback) const
339 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
340 if (pair_content(PAIR_NUMBER(w->_attrs), &fore, &back))
341 err_handler("Can't get color pair");
344 // Monochrome means white on black
348 return getback ? back : fore;
351 int NCursesWindow::NumberOfColors()
353 if (colorInitialized==COLORS_ARE_REALLY_THERE)
356 return 1; // monochrome (actually there are two ;-)
360 NCursesWindow::getcolor() const
362 if (colorInitialized==COLORS_ARE_REALLY_THERE)
363 return PAIR_NUMBER(w->_attrs);
365 return 0; // we only have pair zero
369 NCursesWindow::setpalette(short fore, short back, short pair)
371 if (colorInitialized==COLORS_ARE_REALLY_THERE)
372 return init_pair(pair, fore, back);
378 NCursesWindow::setpalette(short fore, short back)
380 if (colorInitialized==COLORS_ARE_REALLY_THERE)
381 return setpalette(fore, back, PAIR_NUMBER(w->_attrs));
388 NCursesWindow::setcolor(short pair)
390 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
391 if ((pair < 1) || (pair > COLOR_PAIRS))
392 err_handler("Can't set color pair");
395 attrset(COLOR_PAIR(pair));
400 extern "C" int _nc_has_mouse(void);
402 bool NCursesWindow::has_mouse() const {
403 return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
407 NCursesPad::NCursesPad(int lines, int cols) : NCursesWindow() {
408 w = ::newpad(lines,cols);
411 err_handler("Cannot construct window");