ncursesw-morphos/c++/cursesw.cc

471 lines
11 KiB
C++
Raw Normal View History

1997-05-15 12:00:00 +08:00
// * this is for making emacs happy: -*-Mode: C++;-*-
/****************************************************************************
* Copyright 2019,2020 Thomas E. Dickey *
* Copyright 1998-2012,2014 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
1997-05-15 12:00:00 +08:00
/*
* Authors:
* Thomas E. Dickey
* Juergen Pfeifer
*
* The NCursesWindow class was originally based on a file written by
* Eric Newton, later modified by Ulrich Drepper and Anatoly Ivasyuk.
* However, aside from the compatible interface definition, no trace
* of the original code remains in this version: it consists only of
* changes introduced since 1995.
*/
1997-05-15 12:00:00 +08:00
#include "internal.h"
2002-10-13 11:35:53 +08:00
#include "cursesw.h"
1997-05-15 12:00:00 +08:00
MODULE_ID("$Id: cursesw.cc,v 1.56 2020/02/02 23:34:34 tom Exp $")
1997-05-15 12:00:00 +08:00
#define COLORS_NEED_INITIALIZATION -1
#define COLORS_NOT_INITIALIZED 0
#define COLORS_MONOCHROME 1
#define COLORS_ARE_REALLY_THERE 2
#define HaveColors() (colorInitialized == COLORS_ARE_REALLY_THERE)
1997-05-15 12:00:00 +08:00
// declare static variables for the class
1998-03-01 12:21:12 +08:00
long NCursesWindow::count = 0L;
bool NCursesWindow::b_initialized = FALSE;
1997-05-15 12:00:00 +08:00
int
NCursesWindow::scanw(const char* fmt, ...)
{
2002-10-13 11:35:53 +08:00
int result = ERR;
va_list args;
va_start(args, fmt);
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
va_end(args);
1997-05-15 12:00:00 +08:00
return result;
}
int
NCursesWindow::scanw(int y, int x, const char* fmt, ...)
{
2002-10-13 11:35:53 +08:00
int result = ERR;
if (::wmove(w, y, x) != ERR) {
va_list args;
va_start(args, fmt);
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
va_end(args);
1997-05-15 12:00:00 +08:00
}
return result;
}
2007-12-16 08:40:08 +08:00
int
NCursesWindow::scanw(const char* fmt, va_list args)
{
int result = ERR;
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
return result;
}
int
NCursesWindow::scanw(int y, int x, const char* fmt, va_list args)
{
int result = ERR;
if (::wmove(w, y, x) != ERR) {
result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
}
return result;
}
1997-05-15 12:00:00 +08:00
int
NCursesWindow::printw(const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
int result = ::vw_printw(w, fmt, args);
1997-05-15 12:00:00 +08:00
va_end(args);
return result;
1997-05-15 12:00:00 +08:00
}
int
NCursesWindow::printw(int y, int x, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
2002-10-13 11:35:53 +08:00
int result = ::wmove(w, y, x);
1997-05-15 12:00:00 +08:00
if (result == OK) {
result = ::vw_printw(w, fmt, args);
1997-05-15 12:00:00 +08:00
}
va_end(args);
return result;
}
2007-12-16 08:40:08 +08:00
int
NCursesWindow::printw(const char * fmt, va_list args)
{
int result = ::vw_printw(w, fmt, args);
return result;
}
int
NCursesWindow::printw(int y, int x, const char * fmt, va_list args)
{
int result = ::wmove(w, y, x);
if (result == OK) {
result = ::vw_printw(w, fmt, args);
}
return result;
}
1997-05-15 12:00:00 +08:00
void
NCursesWindow::set_keyboard(void)
1997-05-15 12:00:00 +08:00
{
keypad(TRUE);
meta(TRUE);
1997-05-15 12:00:00 +08:00
}
void
NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
{
THROW(new NCursesException(msg));
}
void
2005-10-10 02:41:57 +08:00
NCursesWindow::initialize()
{
if (!b_initialized) {
::initscr();
b_initialized = TRUE;
if (colorInitialized == COLORS_NEED_INITIALIZATION) {
colorInitialized = COLORS_NOT_INITIALIZED;
useColors();
}
::noecho();
::cbreak();
1998-03-01 12:21:12 +08:00
}
}
void
NCursesWindow::constructing()
{
initialize();
++count;
1997-05-15 12:00:00 +08:00
}
2005-10-10 02:41:57 +08:00
NCursesWindow::NCursesWindow()
: w(0), alloced(FALSE), par(0), subwins(0), sib(0)
2005-10-10 02:41:57 +08:00
{
constructing();
1999-10-24 12:32:42 +08:00
w = static_cast<WINDOW *>(0);
1998-03-01 12:21:12 +08:00
}
2005-10-10 02:41:57 +08:00
NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
: w(0), alloced(TRUE), par(0), subwins(0), sib(0)
1997-05-15 12:00:00 +08:00
{
constructing();
1997-05-15 12:00:00 +08:00
2005-10-10 02:41:57 +08:00
w = ::newwin(nlines, ncols, begin_y, begin_x);
1997-05-15 12:00:00 +08:00
if (w == 0) {
err_handler("Cannot construct window");
}
set_keyboard();
1997-05-15 12:00:00 +08:00
}
NCursesWindow::NCursesWindow(WINDOW* window)
: w(0), alloced(FALSE), par(0), subwins(0), sib(0)
1997-05-15 12:00:00 +08:00
{
constructing();
1999-10-24 12:32:42 +08:00
// We used to use a reference on the "window" parameter, but we cannot do
// that with an opaque pointer (see NCURSES_OPAQUE). If the parameter was
// "::stdscr", that is first set via the "constructing() call, and is null
// up to that point. So we allow a null pointer here as meaning the "same"
// as "::stdscr".
w = window ? window : ::stdscr;
set_keyboard();
1997-05-15 12:00:00 +08:00
}
NCursesWindow::NCursesWindow(NCursesWindow& win, int ny, int nx,
1998-03-01 12:21:12 +08:00
int begin_y, int begin_x, char absrel)
: w(0), alloced(TRUE), par(0), subwins(0), sib(0)
1997-05-15 12:00:00 +08:00
{
constructing();
if (absrel == 'a') { // absolute origin
1998-03-01 12:21:12 +08:00
begin_y -= win.begy();
begin_x -= win.begx();
1997-05-15 12:00:00 +08:00
}
// Link this window into its parent's list of subwindows.
// We use derwin(), since this also works for pads.
w = ::derwin(win.w, ny, nx, begin_y, begin_x);
1997-05-15 12:00:00 +08:00
if (w == 0) {
err_handler("Cannot construct subwindow");
}
par = &win;
sib = win.subwins;
win.subwins = this;
}
1999-10-24 12:32:42 +08:00
NCursesWindow::NCursesWindow(NCursesWindow& win,
bool do_box NCURSES_PARAM_INIT(TRUE))
: w(0), alloced(TRUE), par(0), subwins(0), sib(0)
1999-10-24 12:32:42 +08:00
{
constructing();
int myHeight = win.height();
int myWidth = win.width();
w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
if (w == 0) {
err_handler("Cannot construct subwindow");
}
par = &win;
sib = win.subwins;
win.subwins = this;
subwins = 0;
if (do_box) {
win.box();
win.touchwin();
}
1999-10-24 12:32:42 +08:00
}
2005-10-10 02:41:57 +08:00
NCursesWindow NCursesWindow::Clone()
{
WINDOW *d = ::dupwin(w);
NCursesWindow W(d);
W.subwins = subwins;
W.sib = sib;
W.par = par;
W.alloced = alloced;
return W;
1998-03-01 12:21:12 +08:00
}
typedef int (*RIPOFFINIT)(NCursesWindow&);
static RIPOFFINIT R_INIT[5]; // There can't be more
static int r_init_idx = 0;
static RIPOFFINIT* prip = R_INIT;
2005-10-10 02:41:57 +08:00
NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
: w(0), alloced(FALSE), par(0), subwins(0), sib(0)
2005-10-10 02:41:57 +08:00
{
(void) ncols;
initialize();
w = win;
1998-03-01 12:21:12 +08:00
}
2005-10-10 02:41:57 +08:00
int _nc_xx_ripoff_init(WINDOW *w, int ncols)
1998-03-01 12:21:12 +08:00
{
(void) ncols;
int res = ERR;
RIPOFFINIT init = *prip++;
if (init) {
ncurses 5.7 - patch 20090328 + extend ansi.sys pfkey capability from kf1-kf10 to kf1-kf48, moving function key definitions from emx-base for consistency -TD + correct missing final 'p' in pfkey capability of ansi.sys-old (report by Kalle Olavi Niemitalo). + improve test/ncurses.c 'F' test, show combining characters in color. + quiet a false report by cppcheck in c++/cursesw.cc by eliminating a temporary variable. + use _nc_doalloc() rather than realloc() in a few places in ncurses library to avoid leak in out-of-memory condition (reports by William Egert and Martin Ettl based on cppcheck tool). + add --with-ncurses-wrap-prefix option to test/configure (discussion with Charles Wilson). + use ncurses*-config scripts if available for test/configure. + update test/aclocal.m4 and test/configure > patches by Charles Wilson: + modify CF_WITH_LIBTOOL configure check to allow unreleased libtool version numbers (e.g. which include alphabetic chars, as well as digits, after the final '.'). + improve use of -no-undefined option for libtool by setting an intermediate variable LT_UNDEF in the configure script, and then using that in the libtool link-commands. + fix an missing use of NCURSES_PUBLIC_VAR() in tinfo/MKcodes.awk from 2009031 changes. + improve mk-1st.awk script by writing separate cases for the LIBTOOL_LINK command, depending on which library (ncurses, ticlib, termlib) is to be linked. + modify configure.in to allow broken-linker configurations, not just enable-reentrant, to set public wrap prefix.
2009-03-29 08:06:57 +08:00
res = init(*(new NCursesWindow(w,ncols)));
}
return res;
1998-03-01 12:21:12 +08:00
}
int NCursesWindow::ripoffline(int ripoff_lines,
2005-10-10 02:41:57 +08:00
int (*init)(NCursesWindow& win))
{
int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
if (code == OK && init && ripoff_lines) {
R_INIT[r_init_idx++] = init;
}
return code;
1998-03-01 12:21:12 +08:00
}
1997-05-15 12:00:00 +08:00
bool
2005-10-10 02:41:57 +08:00
NCursesWindow::isDescendant(NCursesWindow& win)
{
bool result = FALSE;
for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
if (p == &win || p->isDescendant(win)) {
result = TRUE;
break;
}
1997-05-15 12:00:00 +08:00
}
return result;
1997-05-15 12:00:00 +08:00
}
void
NCursesWindow::kill_subwindows()
{
NCursesWindow* p = subwins;
subwins = 0;
while (p != 0) {
NCursesWindow* q = p->sib;
1997-05-15 12:00:00 +08:00
p->kill_subwindows();
if (p->alloced) {
if (p->w != 0)
::delwin(p->w);
}
delete p;
p = q;
1997-05-15 12:00:00 +08:00
}
}
NCursesWindow::~NCursesWindow() THROWS(NCursesException)
1997-05-15 12:00:00 +08:00
{
kill_subwindows();
if (par != 0) {
// Remove this window from the parent's list of subwindows.
NCursesWindow * next = par->subwins;
NCursesWindow * prev = 0;
while (next != 0) {
if (next == this) {
if (prev != 0) {
prev->sib = next->sib;
} else {
par->subwins = next->sib;
}
1997-05-15 12:00:00 +08:00
break;
}
prev = next;
next = next->sib;
1997-05-15 12:00:00 +08:00
}
}
if (alloced && w != 0)
2002-10-13 11:35:53 +08:00
::delwin(w);
1997-05-15 12:00:00 +08:00
1998-03-01 12:21:12 +08:00
if (alloced) {
--count;
if (count == 0) {
::endwin();
} else if (count < 0) { // cannot happen!
err_handler("Too many windows destroyed");
}
1997-05-15 12:00:00 +08:00
}
}
// ---------------------------------------------------------------------
// Color stuff
//
int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
void
NCursesWindow::useColors(void)
{
1999-10-24 12:32:42 +08:00
if (colorInitialized == COLORS_NOT_INITIALIZED) {
if (b_initialized) {
if (::has_colors()) {
::start_color();
colorInitialized = COLORS_ARE_REALLY_THERE;
} else {
colorInitialized = COLORS_MONOCHROME;
}
} else {
colorInitialized = COLORS_NEED_INITIALIZATION;
1997-05-15 12:00:00 +08:00
}
}
}
NCURSES_PAIRS_T
NCursesWindow::getPair() const
{
return static_cast<NCURSES_PAIRS_T>(PAIR_NUMBER(getattrs(w)));
}
NCURSES_COLOR_T
1999-10-24 12:32:42 +08:00
NCursesWindow::getcolor(int getback) const
1997-05-15 12:00:00 +08:00
{
NCURSES_COLOR_T fore, back;
1997-05-15 12:00:00 +08:00
if (HaveColors()) {
if (::pair_content(getPair(), &fore, &back) == ERR)
err_handler("Can't get color pair");
} else {
// Monochrome means white on black
back = COLOR_BLACK;
fore = COLOR_WHITE;
1997-05-15 12:00:00 +08:00
}
return getback ? back : fore;
}
int NCursesWindow::NumberOfColors()
{
return (HaveColors()) ? COLORS : 1;
1997-05-15 12:00:00 +08:00
}
NCURSES_PAIRS_T
1999-10-24 12:32:42 +08:00
NCursesWindow::getcolor() const
1997-05-15 12:00:00 +08:00
{
return (HaveColors()) ? getPair() : 0;
1997-05-15 12:00:00 +08:00
}
int
NCursesWindow::setpalette(NCURSES_COLOR_T fore, NCURSES_COLOR_T back, NCURSES_PAIRS_T pair)
1997-05-15 12:00:00 +08:00
{
return (HaveColors()) ? ::init_pair(pair, fore, back) : OK;
1997-05-15 12:00:00 +08:00
}
int
NCursesWindow::setpalette(NCURSES_COLOR_T fore, NCURSES_COLOR_T back)
1997-05-15 12:00:00 +08:00
{
return setpalette(fore, back, getPair());
1997-05-15 12:00:00 +08:00
}
int
NCursesWindow::setcolor(NCURSES_PAIRS_T pair)
1997-05-15 12:00:00 +08:00
{
if (HaveColors()) {
if ((pair < 1) || (pair > COLOR_PAIRS))
err_handler("Can't set color pair");
attroff(A_COLOR);
attrset(COLOR_PAIR(pair));
}
return OK;
1997-05-15 12:00:00 +08:00
}
1998-03-01 12:21:12 +08:00
2000-10-21 12:42:11 +08:00
#if HAVE_HAS_KEY
2005-10-10 02:41:57 +08:00
bool NCursesWindow::has_mouse() const
{
return ((::has_key(KEY_MOUSE) || ::has_mouse())
? TRUE : FALSE);
1998-03-01 12:21:12 +08:00
}
2000-07-09 10:46:08 +08:00
#endif