| 1 | // * this is for making emacs happy: -*-Mode: C++;-*-
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | Copyright (C) 1989 Free Software Foundation
|
|---|
| 5 | written by Eric Newton ([email protected])
|
|---|
| 6 |
|
|---|
| 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.
|
|---|
| 18 |
|
|---|
| 19 | modified by Ulrich Drepper ([email protected])
|
|---|
| 20 | and Anatoly Ivasyuk ([email protected])
|
|---|
| 21 |
|
|---|
| 22 | modified by Juergen Pfeifer
|
|---|
| 23 | and Thomas Dickey (noting that more than 70% of this file has been changed)
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | #include "internal.h"
|
|---|
| 27 | #include "cursesw.h"
|
|---|
| 28 |
|
|---|
| 29 | MODULE_ID("$Id: cursesw.cc,v 1.32 2005/08/13 18:12:17 tom Exp $")
|
|---|
| 30 |
|
|---|
| 31 | #define COLORS_NEED_INITIALIZATION -1
|
|---|
| 32 | #define COLORS_NOT_INITIALIZED 0
|
|---|
| 33 | #define COLORS_MONOCHROME 1
|
|---|
| 34 | #define COLORS_ARE_REALLY_THERE 2
|
|---|
| 35 |
|
|---|
| 36 | // declare static variables for the class
|
|---|
| 37 | long NCursesWindow::count = 0L;
|
|---|
| 38 | bool NCursesWindow::b_initialized = FALSE;
|
|---|
| 39 |
|
|---|
| 40 | /*
|
|---|
| 41 | * The ncurses library has a fallback for vsscanf(), which may work...
|
|---|
| 42 | */
|
|---|
| 43 | #if !(USE_STRSTREAM_VSCAN || USE_STRSTREAM_VSCAN_CAST)
|
|---|
| 44 | # undef USE_STDIO_VSCAN
|
|---|
| 45 | # define USE_STDIO_VSCAN 1
|
|---|
| 46 | #endif
|
|---|
| 47 |
|
|---|
| 48 | #if defined(__GNUG__)
|
|---|
| 49 | # ifndef _IO_va_list
|
|---|
| 50 | # define _IO_va_list char *
|
|---|
| 51 | # endif
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | int
|
|---|
| 55 | NCursesWindow::scanw(const char* fmt, ...)
|
|---|
| 56 | {
|
|---|
| 57 | int result = ERR;
|
|---|
| 58 | char buf[BUFSIZ];
|
|---|
| 59 |
|
|---|
| 60 | if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
|
|---|
| 61 | va_list args;
|
|---|
| 62 | va_start(args, fmt);
|
|---|
| 63 | #if USE_STDIO_VSCAN
|
|---|
| 64 | if (::vsscanf(buf, fmt, args) != -1)
|
|---|
| 65 | result = OK;
|
|---|
| 66 | #elif USE_STRSTREAM_VSCAN /* powerpc, os390 */
|
|---|
| 67 | strstreambuf ss(buf, sizeof(buf));
|
|---|
| 68 | if (ss.vscan(fmt, args) != -1)
|
|---|
| 69 | result = OK;
|
|---|
| 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)
|
|---|
| 73 | result = OK;
|
|---|
| 74 | #endif
|
|---|
| 75 | va_end(args);
|
|---|
| 76 | }
|
|---|
| 77 | return result;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | int
|
|---|
| 82 | NCursesWindow::scanw(int y, int x, const char* fmt, ...)
|
|---|
| 83 | {
|
|---|
| 84 | int result = ERR;
|
|---|
| 85 | char buf[BUFSIZ];
|
|---|
| 86 |
|
|---|
| 87 | if (::wmove(w, y, x) != ERR) {
|
|---|
| 88 | if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
|
|---|
| 89 | va_list args;
|
|---|
| 90 | va_start(args, fmt);
|
|---|
| 91 | #if USE_STDIO_VSCAN
|
|---|
| 92 | if (::vsscanf(buf, fmt, args) != -1)
|
|---|
| 93 | result = OK;
|
|---|
| 94 | #elif USE_STRSTREAM_VSCAN /* powerpc, os390 */
|
|---|
| 95 | strstreambuf ss(buf, sizeof(buf));
|
|---|
| 96 | if (ss.vscan(fmt, args) != -1)
|
|---|
| 97 | result = OK;
|
|---|
| 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)
|
|---|
| 101 | result = OK;
|
|---|
| 102 | #endif
|
|---|
| 103 | va_end(args);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | return result;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | int
|
|---|
| 111 | NCursesWindow::printw(const char * fmt, ...)
|
|---|
| 112 | {
|
|---|
| 113 | va_list args;
|
|---|
| 114 | va_start(args, fmt);
|
|---|
| 115 | char buf[BUFSIZ];
|
|---|
| 116 | ::vsprintf(buf, fmt, args);
|
|---|
| 117 | va_end(args);
|
|---|
| 118 | return waddstr(w, buf);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | int
|
|---|
| 123 | NCursesWindow::printw(int y, int x, const char * fmt, ...)
|
|---|
| 124 | {
|
|---|
| 125 | va_list args;
|
|---|
| 126 | va_start(args, fmt);
|
|---|
| 127 | int result = ::wmove(w, y, x);
|
|---|
| 128 | if (result == OK) {
|
|---|
| 129 | char buf[BUFSIZ];
|
|---|
| 130 | ::vsprintf(buf, fmt, args);
|
|---|
| 131 | result = waddstr(w, buf);
|
|---|
| 132 | }
|
|---|
| 133 | va_end(args);
|
|---|
| 134 | return result;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | void
|
|---|
| 139 | NCursesWindow::init(void)
|
|---|
| 140 | {
|
|---|
| 141 | leaveok(0);
|
|---|
| 142 | keypad(1);
|
|---|
| 143 | meta(1);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | void
|
|---|
| 147 | NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
|
|---|
| 148 | {
|
|---|
| 149 | THROW(new NCursesException(msg));
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | void
|
|---|
| 153 | NCursesWindow::initialize()
|
|---|
| 154 | {
|
|---|
| 155 | if (!b_initialized) {
|
|---|
| 156 | ::initscr();
|
|---|
| 157 | b_initialized = TRUE;
|
|---|
| 158 | if (colorInitialized==COLORS_NEED_INITIALIZATION) {
|
|---|
| 159 | colorInitialized=COLORS_NOT_INITIALIZED;
|
|---|
| 160 | useColors();
|
|---|
| 161 | }
|
|---|
| 162 | ::noecho();
|
|---|
| 163 | ::cbreak();
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | NCursesWindow::NCursesWindow()
|
|---|
| 168 | : w(0), alloced(0), par(0), subwins(0), sib(0)
|
|---|
| 169 | {
|
|---|
| 170 | initialize();
|
|---|
| 171 |
|
|---|
| 172 | w = static_cast<WINDOW *>(0);
|
|---|
| 173 | init();
|
|---|
| 174 | alloced = FALSE;
|
|---|
| 175 | subwins = par = sib = 0;
|
|---|
| 176 | count++;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
|
|---|
| 180 | : w(0), alloced(0), par(0), subwins(0), sib(0)
|
|---|
| 181 | {
|
|---|
| 182 | initialize();
|
|---|
| 183 |
|
|---|
| 184 | w = ::newwin(nlines, ncols, begin_y, begin_x);
|
|---|
| 185 | if (w == 0) {
|
|---|
| 186 | err_handler("Cannot construct window");
|
|---|
| 187 | }
|
|---|
| 188 | init();
|
|---|
| 189 |
|
|---|
| 190 | alloced = TRUE;
|
|---|
| 191 | subwins = par = sib = 0;
|
|---|
| 192 | count++;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | NCursesWindow::NCursesWindow(WINDOW* &window)
|
|---|
| 196 | : w(0), alloced(0), par(0), subwins(0), sib(0)
|
|---|
| 197 | {
|
|---|
| 198 | initialize();
|
|---|
| 199 |
|
|---|
| 200 | w = window;
|
|---|
| 201 | init();
|
|---|
| 202 | alloced = FALSE;
|
|---|
| 203 | subwins = par = sib = 0;
|
|---|
| 204 | count++;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 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)
|
|---|
| 210 | {
|
|---|
| 211 | initialize();
|
|---|
| 212 | if (absrel == 'a') { // absolute origin
|
|---|
| 213 | begin_y -= win.begy();
|
|---|
| 214 | begin_x -= win.begx();
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 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);
|
|---|
| 222 | if (w == 0) {
|
|---|
| 223 | err_handler("Cannot construct subwindow");
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | par = &win;
|
|---|
| 227 | sib = win.subwins;
|
|---|
| 228 | win.subwins = this;
|
|---|
| 229 | subwins = 0;
|
|---|
| 230 | alloced = TRUE;
|
|---|
| 231 | count++;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | NCursesWindow::NCursesWindow(NCursesWindow& win,
|
|---|
| 235 | bool do_box NCURSES_PARAM_INIT(TRUE))
|
|---|
| 236 | : w(0), alloced(0), par(0), subwins(0), sib(0)
|
|---|
| 237 | {
|
|---|
| 238 | initialize();
|
|---|
| 239 | int myHeight = win.height();
|
|---|
| 240 | int myWidth = win.width();
|
|---|
| 241 | w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
|
|---|
| 242 | if (w == 0) {
|
|---|
| 243 | err_handler("Cannot construct subwindow");
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | par = &win;
|
|---|
| 247 | sib = win.subwins;
|
|---|
| 248 | win.subwins = this;
|
|---|
| 249 | subwins = 0;
|
|---|
| 250 | alloced = TRUE;
|
|---|
| 251 | count++;
|
|---|
| 252 |
|
|---|
| 253 | if (do_box) {
|
|---|
| 254 | win.box();
|
|---|
| 255 | win.touchwin();
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | NCursesWindow NCursesWindow::Clone()
|
|---|
| 260 | {
|
|---|
| 261 | WINDOW *d = ::dupwin(w);
|
|---|
| 262 | NCursesWindow W(d);
|
|---|
| 263 | W.subwins = subwins;
|
|---|
| 264 | W.sib = sib;
|
|---|
| 265 | W.par = par;
|
|---|
| 266 | W.alloced = alloced;
|
|---|
| 267 | return W;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 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;
|
|---|
| 274 |
|
|---|
| 275 | NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
|
|---|
| 276 | : w(0), alloced(0), par(0), subwins(0), sib(0)
|
|---|
| 277 | {
|
|---|
| 278 | initialize();
|
|---|
| 279 | w = win;
|
|---|
| 280 | assert((w->_maxx+1)==ncols);
|
|---|
| 281 | alloced = FALSE;
|
|---|
| 282 | subwins = par = sib = 0;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | int _nc_xx_ripoff_init(WINDOW *w, int ncols)
|
|---|
| 286 | {
|
|---|
| 287 | int res = ERR;
|
|---|
| 288 |
|
|---|
| 289 | RIPOFFINIT init = *prip++;
|
|---|
| 290 | if (init) {
|
|---|
| 291 | NCursesWindow* W = new NCursesWindow(w,ncols);
|
|---|
| 292 | res = init(*W);
|
|---|
| 293 | }
|
|---|
| 294 | return res;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | int NCursesWindow::ripoffline(int ripoff_lines,
|
|---|
| 298 | int (*init)(NCursesWindow& win))
|
|---|
| 299 | {
|
|---|
| 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;
|
|---|
| 303 | }
|
|---|
| 304 | return code;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | bool
|
|---|
| 308 | NCursesWindow::isDescendant(NCursesWindow& win)
|
|---|
| 309 | {
|
|---|
| 310 | for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
|
|---|
| 311 | if (p==&win)
|
|---|
| 312 | return TRUE;
|
|---|
| 313 | else {
|
|---|
| 314 | if (p->isDescendant(win))
|
|---|
| 315 | return TRUE;
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | return FALSE;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | void
|
|---|
| 322 | NCursesWindow::kill_subwindows()
|
|---|
| 323 | {
|
|---|
| 324 | for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
|
|---|
| 325 | p->kill_subwindows();
|
|---|
| 326 | if (p->alloced) {
|
|---|
| 327 | if (p->w != 0)
|
|---|
| 328 | ::delwin(p->w);
|
|---|
| 329 | p->alloced = FALSE;
|
|---|
| 330 | }
|
|---|
| 331 | p->w = 0; // cause a run-time error if anyone attempts to use...
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | NCursesWindow::~NCursesWindow()
|
|---|
| 337 | {
|
|---|
| 338 | kill_subwindows();
|
|---|
| 339 |
|
|---|
| 340 | if (par != 0) { // Snip us from the parent's list of subwindows.
|
|---|
| 341 | NCursesWindow * win = par->subwins;
|
|---|
| 342 | NCursesWindow * trail = 0;
|
|---|
| 343 | for (;;) {
|
|---|
| 344 | if (win == 0)
|
|---|
| 345 | break;
|
|---|
| 346 | else if (win == this) {
|
|---|
| 347 | if (trail != 0)
|
|---|
| 348 | trail->sib = win->sib;
|
|---|
| 349 | else
|
|---|
| 350 | par->subwins = win->sib;
|
|---|
| 351 | break;
|
|---|
| 352 | } else {
|
|---|
| 353 | trail = win;
|
|---|
| 354 | win = win->sib;
|
|---|
| 355 | }
|
|---|
| 356 | }
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (alloced && w != 0)
|
|---|
| 360 | ::delwin(w);
|
|---|
| 361 |
|
|---|
| 362 | if (alloced) {
|
|---|
| 363 | --count;
|
|---|
| 364 | if (count == 0) {
|
|---|
| 365 | ::endwin();
|
|---|
| 366 | }
|
|---|
| 367 | else if (count < 0) { // cannot happen!
|
|---|
| 368 | err_handler("Too many windows destroyed");
|
|---|
| 369 | }
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | // ---------------------------------------------------------------------
|
|---|
| 374 | // Color stuff
|
|---|
| 375 | //
|
|---|
| 376 | int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
|
|---|
| 377 |
|
|---|
| 378 | void
|
|---|
| 379 | NCursesWindow::useColors(void)
|
|---|
| 380 | {
|
|---|
| 381 | if (colorInitialized == COLORS_NOT_INITIALIZED) {
|
|---|
| 382 | if (b_initialized) {
|
|---|
| 383 | if (::has_colors()) {
|
|---|
| 384 | ::start_color();
|
|---|
| 385 | colorInitialized = COLORS_ARE_REALLY_THERE;
|
|---|
| 386 | }
|
|---|
| 387 | else
|
|---|
| 388 | colorInitialized = COLORS_MONOCHROME;
|
|---|
| 389 | }
|
|---|
| 390 | else
|
|---|
| 391 | colorInitialized = COLORS_NEED_INITIALIZATION;
|
|---|
| 392 | }
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | short
|
|---|
| 396 | NCursesWindow::getcolor(int getback) const
|
|---|
| 397 | {
|
|---|
| 398 | short fore, back;
|
|---|
| 399 |
|
|---|
| 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");
|
|---|
| 403 | }
|
|---|
| 404 | else {
|
|---|
| 405 | // Monochrome means white on black
|
|---|
| 406 | back = COLOR_BLACK;
|
|---|
| 407 | fore = COLOR_WHITE;
|
|---|
| 408 | }
|
|---|
| 409 | return getback ? back : fore;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | int NCursesWindow::NumberOfColors()
|
|---|
| 413 | {
|
|---|
| 414 | if (colorInitialized==COLORS_ARE_REALLY_THERE)
|
|---|
| 415 | return COLORS;
|
|---|
| 416 | else
|
|---|
| 417 | return 1; // monochrome (actually there are two ;-)
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | short
|
|---|
| 421 | NCursesWindow::getcolor() const
|
|---|
| 422 | {
|
|---|
| 423 | if (colorInitialized==COLORS_ARE_REALLY_THERE)
|
|---|
| 424 | return PAIR_NUMBER(w->_attrs);
|
|---|
| 425 | else
|
|---|
| 426 | return 0; // we only have pair zero
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | int
|
|---|
| 430 | NCursesWindow::setpalette(short fore, short back, short pair)
|
|---|
| 431 | {
|
|---|
| 432 | if (colorInitialized==COLORS_ARE_REALLY_THERE)
|
|---|
| 433 | return ::init_pair(pair, fore, back);
|
|---|
| 434 | else
|
|---|
| 435 | return OK;
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | int
|
|---|
| 439 | NCursesWindow::setpalette(short fore, short back)
|
|---|
| 440 | {
|
|---|
| 441 | if (colorInitialized==COLORS_ARE_REALLY_THERE)
|
|---|
| 442 | return setpalette(fore, back, static_cast<short>(PAIR_NUMBER(w->_attrs)));
|
|---|
| 443 | else
|
|---|
| 444 | return OK;
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 | int
|
|---|
| 449 | NCursesWindow::setcolor(short pair)
|
|---|
| 450 | {
|
|---|
| 451 | if (colorInitialized==COLORS_ARE_REALLY_THERE) {
|
|---|
| 452 | if ((pair < 1) || (pair > COLOR_PAIRS))
|
|---|
| 453 | err_handler("Can't set color pair");
|
|---|
| 454 |
|
|---|
| 455 | attroff(A_COLOR);
|
|---|
| 456 | attrset(COLOR_PAIR(pair));
|
|---|
| 457 | }
|
|---|
| 458 | return OK;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | #if HAVE_HAS_KEY
|
|---|
| 462 | bool NCursesWindow::has_mouse() const
|
|---|
| 463 | {
|
|---|
| 464 | return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
|
|---|
| 465 | ? TRUE : FALSE);
|
|---|
| 466 | }
|
|---|
| 467 | #endif
|
|---|