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@gmx.net)
28 MODULE_ID("$Id: cursesw.cc,v 1.16 1999/11/13 23:42:17 tom 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;
41 # define _IO_va_list char *
46 NCursesWindow::scanw(const char* fmt, ...)
52 int result = wgetstr(w, buf);
54 strstreambuf ss(buf, sizeof(buf));
55 result = ss.vscan(fmt, (_IO_va_list)args);
66 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
72 int result = wmove(w, y, x);
74 result = wgetstr(w, buf);
76 strstreambuf ss(buf, sizeof(buf));
77 result = ss.vscan(fmt, (_IO_va_list)args);
89 NCursesWindow::printw(const char * fmt, ...)
94 vsprintf(buf, fmt, args);
96 return waddstr(w, buf);
101 NCursesWindow::printw(int y, int x, const char * fmt, ...)
105 int result = wmove(w, y, x);
108 vsprintf(buf, fmt, args);
109 result = waddstr(w, buf);
117 NCursesWindow::init(void)
125 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
127 THROW(new NCursesException(msg));
131 NCursesWindow::initialize() {
132 if (!b_initialized) {
134 b_initialized = TRUE;
135 if (colorInitialized==COLORS_NEED_INITIALIZATION) {
136 colorInitialized=COLORS_NOT_INITIALIZED;
144 NCursesWindow::NCursesWindow() {
151 subwins = par = sib = 0;
155 NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
160 w = ::newwin(lines, cols, begin_y, begin_x);
162 err_handler("Cannot construct window");
167 subwins = par = sib = 0;
171 NCursesWindow::NCursesWindow(WINDOW* &window)
179 subwins = par = sib = 0;
183 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
184 int begin_y, int begin_x, char absrel)
186 if (absrel == 'a') { // absolute origin
187 begin_y -= win.begy();
188 begin_x -= win.begx();
191 // Even though we treat subwindows as a tree, the standard curses
192 // library needs the `subwin' call to link to the parent in
193 // order to correctly perform refreshes, etc.
194 // Friendly enough, this also works for pads.
195 w = ::derwin(win.w, l, c, begin_y, begin_x);
197 err_handler("Cannot construct subwindow");
208 NCursesWindow::NCursesWindow(NCursesWindow& win,
209 bool do_box NCURSES_PARAM_INIT(TRUE))
211 w = :: derwin(win.w,win.height()-2,win.width()-2,1,1);
213 err_handler("Cannot construct subwindow");
229 NCursesWindow NCursesWindow::Clone() {
230 WINDOW *d = ::dupwin(w);
239 typedef int (*RIPOFFINIT)(NCursesWindow&);
240 static RIPOFFINIT R_INIT[5]; // There can't be more
241 static int r_init_idx = 0;
242 static RIPOFFINIT* prip = R_INIT;
244 extern "C" int _nc_ripoffline(int,int (*init)(WINDOW*,int));
246 NCursesWindow::NCursesWindow(WINDOW *win, int cols) {
248 assert((w->_maxx+1)==cols);
250 subwins = par = sib = 0;
253 int NCursesWindow::ripoff_init(WINDOW *w, int cols)
257 RIPOFFINIT init = *prip++;
259 NCursesWindow* W = new NCursesWindow(w,cols);
265 int NCursesWindow::ripoffline(int ripoff_lines,
266 int (*init)(NCursesWindow& win)) {
267 int code = ::_nc_ripoffline(ripoff_lines,ripoff_init);
268 if (code==OK && init && ripoff_lines) {
269 R_INIT[r_init_idx++] = init;
275 NCursesWindow::isDescendant(NCursesWindow& win) {
276 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
280 if (p->isDescendant(win))
288 NCursesWindow::kill_subwindows()
290 for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
291 p->kill_subwindows();
297 p->w = 0; // cause a run-time error if anyone attempts to use...
302 NCursesWindow::~NCursesWindow()
306 if (par != 0) { // Snip us from the parent's list of subwindows.
307 NCursesWindow * win = par->subwins;
308 NCursesWindow * trail = 0;
312 else if (win == this) {
314 trail->sib = win->sib;
316 par->subwins = win->sib;
325 if (alloced && w != 0)
333 else if (count < 0) { // cannot happen!
334 err_handler("Too many windows destroyed");
339 // ---------------------------------------------------------------------
342 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
345 NCursesWindow::useColors(void)
347 if (colorInitialized == COLORS_NOT_INITIALIZED) {
349 if (::has_colors()) {
351 colorInitialized = COLORS_ARE_REALLY_THERE;
354 colorInitialized = COLORS_MONOCHROME;
357 colorInitialized = COLORS_NEED_INITIALIZATION;
362 NCursesWindow::getcolor(int getback) const
366 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
367 if (pair_content((short)PAIR_NUMBER(w->_attrs), &fore, &back))
368 err_handler("Can't get color pair");
371 // Monochrome means white on black
375 return getback ? back : fore;
378 int NCursesWindow::NumberOfColors()
380 if (colorInitialized==COLORS_ARE_REALLY_THERE)
383 return 1; // monochrome (actually there are two ;-)
387 NCursesWindow::getcolor() const
389 if (colorInitialized==COLORS_ARE_REALLY_THERE)
390 return PAIR_NUMBER(w->_attrs);
392 return 0; // we only have pair zero
396 NCursesWindow::setpalette(short fore, short back, short pair)
398 if (colorInitialized==COLORS_ARE_REALLY_THERE)
399 return init_pair(pair, fore, back);
405 NCursesWindow::setpalette(short fore, short back)
407 if (colorInitialized==COLORS_ARE_REALLY_THERE)
408 return setpalette(fore, back, (short)PAIR_NUMBER(w->_attrs));
415 NCursesWindow::setcolor(short pair)
417 if (colorInitialized==COLORS_ARE_REALLY_THERE) {
418 if ((pair < 1) || (pair > COLOR_PAIRS))
419 err_handler("Can't set color pair");
422 attrset(COLOR_PAIR(pair));
428 extern "C" int _nc_has_mouse(void);
430 bool NCursesWindow::has_mouse() const {
431 return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())