1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright 2019,2020 Thomas E. Dickey *
4 * Copyright 1998-2012,2014 Free Software Foundation, Inc. *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
29 ****************************************************************************/
36 * The NCursesWindow class was originally based on a file written by
37 * Eric Newton, later modified by Ulrich Drepper and Anatoly Ivasyuk.
38 * However, aside from the compatible interface definition, no trace
39 * of the original code remains in this version: it consists only of
40 * changes introduced since 1995.
46 MODULE_ID("$Id: cursesw.cc,v 1.56 2020/02/02 23:34:34 tom Exp $")
48 #define COLORS_NEED_INITIALIZATION -1
49 #define COLORS_NOT_INITIALIZED 0
50 #define COLORS_MONOCHROME 1
51 #define COLORS_ARE_REALLY_THERE 2
53 #define HaveColors() (colorInitialized == COLORS_ARE_REALLY_THERE)
55 // declare static variables for the class
56 long NCursesWindow::count = 0L;
57 bool NCursesWindow::b_initialized = FALSE;
60 NCursesWindow::scanw(const char* fmt, ...)
66 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
74 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
78 if (::wmove(w, y, x) != ERR) {
81 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
89 NCursesWindow::scanw(const char* fmt, va_list args)
93 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
100 NCursesWindow::scanw(int y, int x, const char* fmt, va_list args)
104 if (::wmove(w, y, x) != ERR) {
105 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
112 NCursesWindow::printw(const char * fmt, ...)
116 int result = ::vw_printw(w, fmt, args);
123 NCursesWindow::printw(int y, int x, const char * fmt, ...)
127 int result = ::wmove(w, y, x);
129 result = ::vw_printw(w, fmt, args);
137 NCursesWindow::printw(const char * fmt, va_list args)
139 int result = ::vw_printw(w, fmt, args);
145 NCursesWindow::printw(int y, int x, const char * fmt, va_list args)
147 int result = ::wmove(w, y, x);
149 result = ::vw_printw(w, fmt, args);
156 NCursesWindow::set_keyboard(void)
163 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
165 THROW(new NCursesException(msg));
169 NCursesWindow::initialize()
171 if (!b_initialized) {
173 b_initialized = TRUE;
174 if (colorInitialized == COLORS_NEED_INITIALIZATION) {
175 colorInitialized = COLORS_NOT_INITIALIZED;
184 NCursesWindow::constructing()
190 NCursesWindow::NCursesWindow()
191 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
195 w = static_cast<WINDOW *>(0);
198 NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
199 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
203 w = ::newwin(nlines, ncols, begin_y, begin_x);
205 err_handler("Cannot construct window");
210 NCursesWindow::NCursesWindow(WINDOW* window)
211 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
215 // We used to use a reference on the "window" parameter, but we cannot do
216 // that with an opaque pointer (see NCURSES_OPAQUE). If the parameter was
217 // "::stdscr", that is first set via the "constructing() call, and is null
218 // up to that point. So we allow a null pointer here as meaning the "same"
220 w = window ? window : ::stdscr;
224 NCursesWindow::NCursesWindow(NCursesWindow& win, int ny, int nx,
225 int begin_y, int begin_x, char absrel)
226 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
229 if (absrel == 'a') { // absolute origin
230 begin_y -= win.begy();
231 begin_x -= win.begx();
234 // Link this window into its parent's list of subwindows.
235 // We use derwin(), since this also works for pads.
236 w = ::derwin(win.w, ny, nx, begin_y, begin_x);
238 err_handler("Cannot construct subwindow");
246 NCursesWindow::NCursesWindow(NCursesWindow& win,
247 bool do_box NCURSES_PARAM_INIT(TRUE))
248 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
251 int myHeight = win.height();
252 int myWidth = win.width();
253 w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
255 err_handler("Cannot construct subwindow");
269 NCursesWindow NCursesWindow::Clone()
271 WINDOW *d = ::dupwin(w);
280 typedef int (*RIPOFFINIT)(NCursesWindow&);
281 static RIPOFFINIT R_INIT[5]; // There can't be more
282 static int r_init_idx = 0;
283 static RIPOFFINIT* prip = R_INIT;
285 NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
286 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
293 int _nc_xx_ripoff_init(WINDOW *w, int ncols)
298 RIPOFFINIT init = *prip++;
300 res = init(*(new NCursesWindow(w,ncols)));
305 int NCursesWindow::ripoffline(int ripoff_lines,
306 int (*init)(NCursesWindow& win))
308 int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
309 if (code == OK && init && ripoff_lines) {
310 R_INIT[r_init_idx++] = init;
316 NCursesWindow::isDescendant(NCursesWindow& win)
320 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
321 if (p == &win || p->isDescendant(win)) {
330 NCursesWindow::kill_subwindows()
332 NCursesWindow* p = subwins;
336 NCursesWindow* q = p->sib;
337 p->kill_subwindows();
348 NCursesWindow::~NCursesWindow() THROWS(NCursesException)
353 // Remove this window from the parent's list of subwindows.
354 NCursesWindow * next = par->subwins;
355 NCursesWindow * prev = 0;
359 prev->sib = next->sib;
361 par->subwins = next->sib;
370 if (alloced && w != 0)
377 } else if (count < 0) { // cannot happen!
378 err_handler("Too many windows destroyed");
383 // ---------------------------------------------------------------------
386 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
389 NCursesWindow::useColors(void)
391 if (colorInitialized == COLORS_NOT_INITIALIZED) {
393 if (::has_colors()) {
395 colorInitialized = COLORS_ARE_REALLY_THERE;
397 colorInitialized = COLORS_MONOCHROME;
400 colorInitialized = COLORS_NEED_INITIALIZATION;
406 NCursesWindow::getPair() const
408 return static_cast<NCURSES_PAIRS_T>(PAIR_NUMBER(getattrs(w)));
412 NCursesWindow::getcolor(int getback) const
414 NCURSES_COLOR_T fore, back;
417 if (::pair_content(getPair(), &fore, &back) == ERR)
418 err_handler("Can't get color pair");
420 // Monochrome means white on black
424 return getback ? back : fore;
427 int NCursesWindow::NumberOfColors()
429 return (HaveColors()) ? COLORS : 1;
433 NCursesWindow::getcolor() const
435 return (HaveColors()) ? getPair() : 0;
439 NCursesWindow::setpalette(NCURSES_COLOR_T fore, NCURSES_COLOR_T back, NCURSES_PAIRS_T pair)
441 return (HaveColors()) ? ::init_pair(pair, fore, back) : OK;
445 NCursesWindow::setpalette(NCURSES_COLOR_T fore, NCURSES_COLOR_T back)
447 return setpalette(fore, back, getPair());
452 NCursesWindow::setcolor(NCURSES_PAIRS_T pair)
455 if ((pair < 1) || (pair > COLOR_PAIRS))
456 err_handler("Can't set color pair");
459 attrset(COLOR_PAIR(pair));
465 bool NCursesWindow::has_mouse() const
467 return ((::has_key(KEY_MOUSE) || ::has_mouse())