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 and Thomas Dickey (noting that more than 70% of this file has been changed)
29 MODULE_ID("$Id: cursesw.cc,v 1.32 2005/08/13 18:12:17 tom 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()
155 if (!b_initialized) {
157 b_initialized = TRUE;
158 if (colorInitialized==COLORS_NEED_INITIALIZATION) {
159 colorInitialized=COLORS_NOT_INITIALIZED;
167 NCursesWindow::NCursesWindow()
168 : w(0), alloced(0), par(0), subwins(0), sib(0)
172 w = static_cast<WINDOW *>(0);
175 subwins = par = sib = 0;
179 NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
180 : w(0), alloced(0), par(0), subwins(0), sib(0)
184 w = ::newwin(nlines, ncols, begin_y, begin_x);
186 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)
203 subwins = par = sib = 0;
207 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
208 int begin_y, int begin_x, char absrel)
209 : w(0), alloced(0), par(0), subwins(0), sib(0)
212 if (absrel == 'a') { // absolute origin
213 begin_y -= win.begy();
214 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);
223 err_handler("Cannot construct subwindow");
234 NCursesWindow::NCursesWindow(NCursesWindow& win,
235 bool do_box NCURSES_PARAM_INIT(TRUE))
236 : w(0), alloced(0), 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");
259 NCursesWindow NCursesWindow::Clone()
261 WINDOW *d = ::dupwin(w);
270 typedef int (*RIPOFFINIT)(NCursesWindow&);
271 static RIPOFFINIT R_INIT[5]; // There can't be more
272 static int r_init_idx = 0;
273 static RIPOFFINIT* prip = R_INIT;
275 NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
276 : w(0), alloced(0), par(0), subwins(0), sib(0)
280 assert((w->_maxx+1)==ncols);
282 subwins = par = sib = 0;
285 int _nc_xx_ripoff_init(WINDOW *w, int ncols)
289 RIPOFFINIT init = *prip++;
291 NCursesWindow* W = new NCursesWindow(w,ncols);
297 int NCursesWindow::ripoffline(int ripoff_lines,
298 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;
308 NCursesWindow::isDescendant(NCursesWindow& win)
310 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
314 if (p->isDescendant(win))
322 NCursesWindow::kill_subwindows()
324 for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
325 p->kill_subwindows();
331 p->w = 0; // cause a run-time error if anyone attempts to use...
336 NCursesWindow::~NCursesWindow()
340 if (par != 0) { // Snip us from the parent's list of subwindows.
341 NCursesWindow * win = par->subwins;
342 NCursesWindow * trail = 0;
346 else if (win == this) {
348 trail->sib = win->sib;
350 par->subwins = win->sib;
359 if (alloced && w != 0)
367 else if (count < 0) { // cannot happen!
368 err_handler("Too many windows destroyed");
373 // ---------------------------------------------------------------------
376 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
379 NCursesWindow::useColors(void)
381 if (colorInitialized == COLORS_NOT_INITIALIZED) {
383 if (::has_colors()) {
385 colorInitialized = COLORS_ARE_REALLY_THERE;
388 colorInitialized = COLORS_MONOCHROME;
391 colorInitialized = COLORS_NEED_INITIALIZATION;
396 NCursesWindow::getcolor(int getback) const
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
409 return getback ? back : fore;
412 int NCursesWindow::NumberOfColors()
414 if (colorInitialized==COLORS_ARE_REALLY_THERE)
417 return 1; // monochrome (actually there are two ;-)
421 NCursesWindow::getcolor() const
423 if (colorInitialized==COLORS_ARE_REALLY_THERE)
424 return PAIR_NUMBER(w->_attrs);
426 return 0; // we only have pair zero
430 NCursesWindow::setpalette(short fore, short back, short pair)
432 if (colorInitialized==COLORS_ARE_REALLY_THERE)
433 return ::init_pair(pair, fore, back);
439 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)));
449 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");
456 attrset(COLOR_PAIR(pair));
462 bool NCursesWindow::has_mouse() const
464 return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())