blob: 825d08ddd102ada225d4759d55b9bc464bbd6507 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301// * this is for making emacs happy: -*-Mode: C++;-*-
2/****************************************************************************
3 * Copyright (c) 2007 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 ****************************************************************************/
29
30/*
31 * Authors:
32 * Thomas E. Dickey
33 * Juergen Pfeifer
34 *
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.
40 */
41
42#include "internal.h"
43#include "cursesw.h"
44
45MODULE_ID("$Id: cursesw.cc,v 1.49 2007/12/15 23:01:57 tom Exp $")
46
47#define COLORS_NEED_INITIALIZATION -1
48#define COLORS_NOT_INITIALIZED 0
49#define COLORS_MONOCHROME 1
50#define COLORS_ARE_REALLY_THERE 2
51
52#define HaveColors() (colorInitialized == COLORS_ARE_REALLY_THERE)
53
54// declare static variables for the class
55long NCursesWindow::count = 0L;
56bool NCursesWindow::b_initialized = FALSE;
57
58int
59NCursesWindow::scanw(const char* fmt, ...)
60{
61 int result = ERR;
62
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);
67
68 return result;
69}
70
71
72int
73NCursesWindow::scanw(int y, int x, const char* fmt, ...)
74{
75 int result = ERR;
76
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);
82 }
83 return result;
84}
85
86
87int
88NCursesWindow::scanw(const char* fmt, va_list args)
89{
90 int result = ERR;
91
92 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
93
94 return result;
95}
96
97
98int
99NCursesWindow::scanw(int y, int x, const char* fmt, va_list args)
100{
101 int result = ERR;
102
103 if (::wmove(w, y, x) != ERR) {
104 result = ::vw_scanw (w, const_cast<NCURSES_CONST char *>(fmt), args);
105 }
106 return result;
107}
108
109
110int
111NCursesWindow::printw(const char * fmt, ...)
112{
113 va_list args;
114 va_start(args, fmt);
115 int result = ::vw_printw(w, fmt, args);
116 va_end(args);
117 return result;
118}
119
120
121int
122NCursesWindow::printw(int y, int x, const char * fmt, ...)
123{
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);
129 }
130 va_end(args);
131 return result;
132}
133
134
135int
136NCursesWindow::printw(const char * fmt, va_list args)
137{
138 int result = ::vw_printw(w, fmt, args);
139 return result;
140}
141
142
143int
144NCursesWindow::printw(int y, int x, const char * fmt, va_list args)
145{
146 int result = ::wmove(w, y, x);
147 if (result == OK) {
148 result = ::vw_printw(w, fmt, args);
149 }
150 return result;
151}
152
153
154void
155NCursesWindow::set_keyboard(void)
156{
157 keypad(TRUE);
158 meta(TRUE);
159}
160
161void
162NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
163{
164 THROW(new NCursesException(msg));
165}
166
167void
168NCursesWindow::initialize()
169{
170 if (!b_initialized) {
171 ::initscr();
172 b_initialized = TRUE;
173 if (colorInitialized == COLORS_NEED_INITIALIZATION) {
174 colorInitialized = COLORS_NOT_INITIALIZED;
175 useColors();
176 }
177 ::noecho();
178 ::cbreak();
179 }
180}
181
182void
183NCursesWindow::constructing()
184{
185 initialize();
186 ++count;
187}
188
189NCursesWindow::NCursesWindow()
190 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
191{
192 constructing();
193
194 w = static_cast<WINDOW *>(0);
195 set_keyboard();
196}
197
198NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
199 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
200{
201 constructing();
202
203 w = ::newwin(nlines, ncols, begin_y, begin_x);
204 if (w == 0) {
205 err_handler("Cannot construct window");
206 }
207 set_keyboard();
208}
209
210NCursesWindow::NCursesWindow(WINDOW* window)
211 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
212{
213 constructing();
214
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();
222}
223
224NCursesWindow::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)
227{
228 constructing();
229 if (absrel == 'a') { // absolute origin
230 begin_y -= win.begy();
231 begin_x -= win.begx();
232 }
233
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");
239 }
240
241 par = &win;
242 sib = win.subwins;
243 win.subwins = this;
244}
245
246NCursesWindow::NCursesWindow(NCursesWindow& win,
247 bool do_box NCURSES_PARAM_INIT(TRUE))
248 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
249{
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");
256 }
257
258 par = &win;
259 sib = win.subwins;
260 win.subwins = this;
261 subwins = 0;
262
263 if (do_box) {
264 win.box();
265 win.touchwin();
266 }
267}
268
269NCursesWindow NCursesWindow::Clone()
270{
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;
278}
279
280typedef int (*RIPOFFINIT)(NCursesWindow&);
281static RIPOFFINIT R_INIT[5]; // There can't be more
282static int r_init_idx = 0;
283static RIPOFFINIT* prip = R_INIT;
284
285NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
286 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
287{
288 initialize();
289 w = win;
290 assert((w->_maxx +1 ) == ncols);
291}
292
293int _nc_xx_ripoff_init(WINDOW *w, int ncols)
294{
295 int res = ERR;
296
297 RIPOFFINIT init = *prip++;
298 if (init) {
299 NCursesWindow* W = new NCursesWindow(w,ncols);
300 res = init(*W);
301 }
302 return res;
303}
304
305int NCursesWindow::ripoffline(int ripoff_lines,
306 int (*init)(NCursesWindow& win))
307{
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;
311 }
312 return code;
313}
314
315bool
316NCursesWindow::isDescendant(NCursesWindow& win)
317{
318 bool result = FALSE;
319
320 for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
321 if (p == &win || p->isDescendant(win)) {
322 result = TRUE;
323 break;
324 }
325 }
326 return result;
327}
328
329void
330NCursesWindow::kill_subwindows()
331{
332 NCursesWindow* p = subwins;
333
334 subwins = 0;
335 while (p != 0) {
336 NCursesWindow* q = p->sib;
337 p->kill_subwindows();
338 if (p->alloced) {
339 if (p->w != 0)
340 ::delwin(p->w);
341 }
342 delete p;
343 p = q;
344 }
345}
346
347
348NCursesWindow::~NCursesWindow()
349{
350 kill_subwindows();
351
352 if (par != 0) {
353 // Remove this window from the parent's list of subwindows.
354 NCursesWindow * next = par->subwins;
355 NCursesWindow * prev = 0;
356 while (next != 0) {
357 if (next == this) {
358 if (prev != 0) {
359 prev->sib = next->sib;
360 } else {
361 par->subwins = next->sib;
362 }
363 break;
364 }
365 prev = next;
366 next = next->sib;
367 }
368 }
369
370 if (alloced && w != 0)
371 ::delwin(w);
372
373 if (alloced) {
374 --count;
375 if (count == 0) {
376 ::endwin();
377 } else if (count < 0) { // cannot happen!
378 err_handler("Too many windows destroyed");
379 }
380 }
381}
382
383// ---------------------------------------------------------------------
384// Color stuff
385//
386int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
387
388void
389NCursesWindow::useColors(void)
390{
391 if (colorInitialized == COLORS_NOT_INITIALIZED) {
392 if (b_initialized) {
393 if (::has_colors()) {
394 ::start_color();
395 colorInitialized = COLORS_ARE_REALLY_THERE;
396 } else {
397 colorInitialized = COLORS_MONOCHROME;
398 }
399 } else {
400 colorInitialized = COLORS_NEED_INITIALIZATION;
401 }
402 }
403}
404
405short
406NCursesWindow::getPair() const
407{
408 return static_cast<short>(PAIR_NUMBER(getattrs(w)));
409}
410
411short
412NCursesWindow::getcolor(int getback) const
413{
414 short fore, back;
415
416 if (HaveColors()) {
417 if (::pair_content(getPair(), &fore, &back) == ERR)
418 err_handler("Can't get color pair");
419 } else {
420 // Monochrome means white on black
421 back = COLOR_BLACK;
422 fore = COLOR_WHITE;
423 }
424 return getback ? back : fore;
425}
426
427int NCursesWindow::NumberOfColors()
428{
429 return (HaveColors()) ? COLORS : 1;
430}
431
432short
433NCursesWindow::getcolor() const
434{
435 return (HaveColors()) ? getPair() : 0;
436}
437
438int
439NCursesWindow::setpalette(short fore, short back, short pair)
440{
441 return (HaveColors()) ? ::init_pair(pair, fore, back) : OK;
442}
443
444int
445NCursesWindow::setpalette(short fore, short back)
446{
447 return setpalette(fore, back, getPair());
448}
449
450
451int
452NCursesWindow::setcolor(short pair)
453{
454 if (HaveColors()) {
455 if ((pair < 1) || (pair > COLOR_PAIRS))
456 err_handler("Can't set color pair");
457
458 attroff(A_COLOR);
459 attrset(COLOR_PAIR(pair));
460 }
461 return OK;
462}
463
464#if HAVE_HAS_KEY
465bool NCursesWindow::has_mouse() const
466{
467 return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
468 ? TRUE : FALSE);
469}
470#endif