missing ncurses sources
[tomato.git] / release / src / router / libncurses / c++ / cursesw.cc
blob47e5cf64dcf0ded1e7ca1d88f1ed69cc2ae8fbfb
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 2007-2008,2009 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
31 * Authors:
32 * Thomas E. Dickey
33 * Juergen Pfeifer
35 * The NCursesWindow class was originally based on a file written by
36 * Eric Newton, later modified by Ulrich Drepper and Anatoly Ivasyuk.
37 * However, aside from the compatible interface definition, no trace
38 * of the original code remains in this version: it consists only of
39 * changes introduced since 1995.
42 #include "internal.h"
43 #include "cursesw.h"
45 MODULE_ID("$Id: cursesw.cc,v 1.51 2009/03/28 21:31:37 tom Exp $")
47 #define COLORS_NEED_INITIALIZATION -1
48 #define COLORS_NOT_INITIALIZED 0
49 #define COLORS_MONOCHROME 1
50 #define COLORS_ARE_REALLY_THERE 2
52 #define HaveColors() (colorInitialized == COLORS_ARE_REALLY_THERE)
54 // declare static variables for the class
55 long NCursesWindow::count = 0L;
56 bool NCursesWindow::b_initialized = FALSE;
58 int
59 NCursesWindow::scanw(const char* fmt, ...)
61 int result = ERR;
63 va_list args;
64 va_start(args, fmt);
65 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
66 va_end(args);
68 return result;
72 int
73 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
75 int result = ERR;
77 if (::wmove(w, y, x) != ERR) {
78 va_list args;
79 va_start(args, fmt);
80 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
81 va_end(args);
83 return result;
87 int
88 NCursesWindow::scanw(const char* fmt, va_list args)
90 int result = ERR;
92 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
94 return result;
98 int
99 NCursesWindow::scanw(int y, int x, const char* fmt, va_list args)
101 int result = ERR;
103 if (::wmove(w, y, x) != ERR) {
104 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
106 return result;
111 NCursesWindow::printw(const char * fmt, ...)
113 va_list args;
114 va_start(args, fmt);
115 int result = ::vw_printw(w, fmt, args);
116 va_end(args);
117 return result;
122 NCursesWindow::printw(int y, int x, const char * fmt, ...)
124 va_list args;
125 va_start(args, fmt);
126 int result = ::wmove(w, y, x);
127 if (result == OK) {
128 result = ::vw_printw(w, fmt, args);
130 va_end(args);
131 return result;
136 NCursesWindow::printw(const char * fmt, va_list args)
138 int result = ::vw_printw(w, fmt, args);
139 return result;
144 NCursesWindow::printw(int y, int x, const char * fmt, va_list args)
146 int result = ::wmove(w, y, x);
147 if (result == OK) {
148 result = ::vw_printw(w, fmt, args);
150 return result;
154 void
155 NCursesWindow::set_keyboard(void)
157 keypad(TRUE);
158 meta(TRUE);
161 void
162 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
164 THROW(new NCursesException(msg));
167 void
168 NCursesWindow::initialize()
170 if (!b_initialized) {
171 ::initscr();
172 b_initialized = TRUE;
173 if (colorInitialized == COLORS_NEED_INITIALIZATION) {
174 colorInitialized = COLORS_NOT_INITIALIZED;
175 useColors();
177 ::noecho();
178 ::cbreak();
182 void
183 NCursesWindow::constructing()
185 initialize();
186 ++count;
189 NCursesWindow::NCursesWindow()
190 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
192 constructing();
194 w = static_cast<WINDOW *>(0);
195 set_keyboard();
198 NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
199 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
201 constructing();
203 w = ::newwin(nlines, ncols, begin_y, begin_x);
204 if (w == 0) {
205 err_handler("Cannot construct window");
207 set_keyboard();
210 NCursesWindow::NCursesWindow(WINDOW* window)
211 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
213 constructing();
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"
219 // as "::stdscr".
220 w = window ? window : ::stdscr;
221 set_keyboard();
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)
228 constructing();
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);
237 if (w == 0) {
238 err_handler("Cannot construct subwindow");
241 par = &win;
242 sib = win.subwins;
243 win.subwins = this;
246 NCursesWindow::NCursesWindow(NCursesWindow& win,
247 bool do_box NCURSES_PARAM_INIT(TRUE))
248 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
250 constructing();
251 int myHeight = win.height();
252 int myWidth = win.width();
253 w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
254 if (w == 0) {
255 err_handler("Cannot construct subwindow");
258 par = &win;
259 sib = win.subwins;
260 win.subwins = this;
261 subwins = 0;
263 if (do_box) {
264 win.box();
265 win.touchwin();
269 NCursesWindow NCursesWindow::Clone()
271 WINDOW *d = ::dupwin(w);
272 NCursesWindow W(d);
273 W.subwins = subwins;
274 W.sib = sib;
275 W.par = par;
276 W.alloced = alloced;
277 return 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)
288 initialize();
289 w = win;
292 int _nc_xx_ripoff_init(WINDOW *w, int ncols)
294 int res = ERR;
296 RIPOFFINIT init = *prip++;
297 if (init) {
298 res = init(*(new NCursesWindow(w,ncols)));
300 return res;
303 int NCursesWindow::ripoffline(int ripoff_lines,
304 int (*init)(NCursesWindow& win))
306 int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
307 if (code == OK && init && ripoff_lines) {
308 R_INIT[r_init_idx++] = init;
310 return code;
313 bool
314 NCursesWindow::isDescendant(NCursesWindow& win)
316 bool result = FALSE;
318 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
319 if (p == &win || p->isDescendant(win)) {
320 result = TRUE;
321 break;
324 return result;
327 void
328 NCursesWindow::kill_subwindows()
330 NCursesWindow* p = subwins;
332 subwins = 0;
333 while (p != 0) {
334 NCursesWindow* q = p->sib;
335 p->kill_subwindows();
336 if (p->alloced) {
337 if (p->w != 0)
338 ::delwin(p->w);
340 delete p;
341 p = q;
346 NCursesWindow::~NCursesWindow()
348 kill_subwindows();
350 if (par != 0) {
351 // Remove this window from the parent's list of subwindows.
352 NCursesWindow * next = par->subwins;
353 NCursesWindow * prev = 0;
354 while (next != 0) {
355 if (next == this) {
356 if (prev != 0) {
357 prev->sib = next->sib;
358 } else {
359 par->subwins = next->sib;
361 break;
363 prev = next;
364 next = next->sib;
368 if (alloced && w != 0)
369 ::delwin(w);
371 if (alloced) {
372 --count;
373 if (count == 0) {
374 ::endwin();
375 } else if (count < 0) { // cannot happen!
376 err_handler("Too many windows destroyed");
381 // ---------------------------------------------------------------------
382 // Color stuff
384 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
386 void
387 NCursesWindow::useColors(void)
389 if (colorInitialized == COLORS_NOT_INITIALIZED) {
390 if (b_initialized) {
391 if (::has_colors()) {
392 ::start_color();
393 colorInitialized = COLORS_ARE_REALLY_THERE;
394 } else {
395 colorInitialized = COLORS_MONOCHROME;
397 } else {
398 colorInitialized = COLORS_NEED_INITIALIZATION;
403 short
404 NCursesWindow::getPair() const
406 return static_cast<short>(PAIR_NUMBER(getattrs(w)));
409 short
410 NCursesWindow::getcolor(int getback) const
412 short fore, back;
414 if (HaveColors()) {
415 if (::pair_content(getPair(), &fore, &back) == ERR)
416 err_handler("Can't get color pair");
417 } else {
418 // Monochrome means white on black
419 back = COLOR_BLACK;
420 fore = COLOR_WHITE;
422 return getback ? back : fore;
425 int NCursesWindow::NumberOfColors()
427 return (HaveColors()) ? COLORS : 1;
430 short
431 NCursesWindow::getcolor() const
433 return (HaveColors()) ? getPair() : 0;
437 NCursesWindow::setpalette(short fore, short back, short pair)
439 return (HaveColors()) ? ::init_pair(pair, fore, back) : OK;
443 NCursesWindow::setpalette(short fore, short back)
445 return setpalette(fore, back, getPair());
450 NCursesWindow::setcolor(short pair)
452 if (HaveColors()) {
453 if ((pair < 1) || (pair > COLOR_PAIRS))
454 err_handler("Can't set color pair");
456 attroff(A_COLOR);
457 attrset(COLOR_PAIR(pair));
459 return OK;
462 #if HAVE_HAS_KEY
463 bool NCursesWindow::has_mouse() const
465 return ((::has_key(KEY_MOUSE) || ::has_mouse())
466 ? TRUE : FALSE);
468 #endif