]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesw.cc
ncurses 5.6 - patch 20070127
[ncurses.git] / c++ / cursesw.cc
1 // * 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
45 MODULE_ID("$Id: cursesw.cc,v 1.46 2007/01/27 22:31:12 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
55 long NCursesWindow::count = 0L;
56 bool NCursesWindow::b_initialized = FALSE;
57
58 int
59 NCursesWindow::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
72 int
73 NCursesWindow::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
87 int
88 NCursesWindow::printw(const char * fmt, ...)
89 {
90     va_list args;
91     va_start(args, fmt);
92     int result = ::vw_printw(w, fmt, args);
93     va_end(args);
94     return result;
95 }
96
97
98 int
99 NCursesWindow::printw(int y, int x, const char * fmt, ...)
100 {
101     va_list args;
102     va_start(args, fmt);
103     int result = ::wmove(w, y, x);
104     if (result == OK) {
105         result = ::vw_printw(w, fmt, args);
106     }
107     va_end(args);
108     return result;
109 }
110
111
112 void
113 NCursesWindow::set_keyboard(void)
114 {
115     keypad(TRUE);
116     meta(TRUE);
117 }
118
119 void
120 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
121 {
122   THROW(new NCursesException(msg));
123 }
124
125 void
126 NCursesWindow::initialize()
127 {
128     if (!b_initialized) {
129         ::initscr();
130         b_initialized = TRUE;
131         if (colorInitialized == COLORS_NEED_INITIALIZATION) {
132             colorInitialized = COLORS_NOT_INITIALIZED;
133             useColors();
134         }
135         ::noecho();
136         ::cbreak();
137     }
138 }
139
140 void
141 NCursesWindow::constructing()
142 {
143     initialize();
144     ++count;
145 }
146
147 NCursesWindow::NCursesWindow()
148   : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
149 {
150     constructing();
151
152     w = static_cast<WINDOW *>(0);
153     set_keyboard();
154 }
155
156 NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
157   : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
158 {
159     constructing();
160
161     w = ::newwin(nlines, ncols, begin_y, begin_x);
162     if (w == 0) {
163         err_handler("Cannot construct window");
164     }
165     set_keyboard();
166 }
167
168 NCursesWindow::NCursesWindow(WINDOW* &window)
169   : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
170 {
171     constructing();
172
173     w = window;
174     set_keyboard();
175 }
176
177 NCursesWindow::NCursesWindow(NCursesWindow& win, int ny, int nx,
178                              int begin_y, int begin_x, char absrel)
179   : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
180 {
181     constructing();
182     if (absrel == 'a') {        // absolute origin
183         begin_y -= win.begy();
184         begin_x -= win.begx();
185     }
186
187     // Link this window into its parent's list of subwindows.
188     // We use derwin(), since this also works for pads.
189     w = ::derwin(win.w, ny, nx, begin_y, begin_x);
190     if (w == 0) {
191         err_handler("Cannot construct subwindow");
192     }
193
194     par = &win;
195     sib = win.subwins;
196     win.subwins = this;
197 }
198
199 NCursesWindow::NCursesWindow(NCursesWindow& win,
200                                 bool do_box NCURSES_PARAM_INIT(TRUE))
201   : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
202 {
203     constructing();
204     int myHeight = win.height();
205     int myWidth  = win.width();
206     w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
207     if (w == 0) {
208         err_handler("Cannot construct subwindow");
209     }
210
211     par = &win;
212     sib = win.subwins;
213     win.subwins = this;
214     subwins = 0;
215
216     if (do_box) {
217         win.box();
218         win.touchwin();
219     }
220 }
221
222 NCursesWindow NCursesWindow::Clone()
223 {
224     WINDOW *d = ::dupwin(w);
225     NCursesWindow W(d);
226     W.subwins = subwins;
227     W.sib = sib;
228     W.par = par;
229     W.alloced = alloced;
230     return W;
231 }
232
233 typedef int (*RIPOFFINIT)(NCursesWindow&);
234 static RIPOFFINIT R_INIT[5];       // There can't be more
235 static int r_init_idx   = 0;
236 static RIPOFFINIT* prip = R_INIT;
237
238 NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
239   : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
240 {
241     initialize();
242     w = win;
243     assert((w->_maxx +1 ) == ncols);
244 }
245
246 int _nc_xx_ripoff_init(WINDOW *w, int ncols)
247 {
248     int res = ERR;
249
250     RIPOFFINIT init = *prip++;
251     if (init) {
252         NCursesWindow* W = new NCursesWindow(w,ncols);
253         res = init(*W);
254     }
255     return res;
256 }
257
258 int NCursesWindow::ripoffline(int ripoff_lines,
259                               int (*init)(NCursesWindow& win))
260 {
261     int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
262     if (code == OK && init && ripoff_lines) {
263         R_INIT[r_init_idx++] = init;
264     }
265     return code;
266 }
267
268 bool
269 NCursesWindow::isDescendant(NCursesWindow& win)
270 {
271     bool result = FALSE;
272
273     for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
274         if (p == &win || p->isDescendant(win)) {
275             result = TRUE;
276             break;
277         }
278     }
279     return result;
280 }
281
282 void
283 NCursesWindow::kill_subwindows()
284 {
285     NCursesWindow* p = subwins;
286
287     subwins = 0;
288     while (p != 0) {
289         NCursesWindow* q = p->sib;
290         p->kill_subwindows();
291         if (p->alloced) {
292             if (p->w != 0)
293                 ::delwin(p->w);
294         }
295         delete p;
296         p = q;
297     }
298 }
299
300
301 NCursesWindow::~NCursesWindow()
302 {
303     kill_subwindows();
304
305     if (par != 0) {
306         // Remove this window from the parent's list of subwindows.
307         NCursesWindow * next = par->subwins;
308         NCursesWindow * prev = 0;
309         while (next != 0) {
310             if (next == this) {
311                 if (prev != 0) {
312                     prev->sib = next->sib;
313                 } else {
314                     par->subwins = next->sib;
315                 }
316                 break;
317             }
318             prev = next;
319             next = next->sib;
320         }
321     }
322
323     if (alloced && w != 0)
324         ::delwin(w);
325
326     if (alloced) {
327         --count;
328         if (count == 0) {
329             ::endwin();
330         } else if (count < 0) { // cannot happen!
331             err_handler("Too many windows destroyed");
332         }
333     }
334 }
335
336 // ---------------------------------------------------------------------
337 // Color stuff
338 //
339 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
340
341 void
342 NCursesWindow::useColors(void)
343 {
344     if (colorInitialized == COLORS_NOT_INITIALIZED) {
345         if (b_initialized) {
346             if (::has_colors()) {
347                 ::start_color();
348                 colorInitialized = COLORS_ARE_REALLY_THERE;
349             } else {
350                 colorInitialized = COLORS_MONOCHROME;
351             }
352         } else {
353             colorInitialized = COLORS_NEED_INITIALIZATION;
354         }
355     }
356 }
357
358 short
359 NCursesWindow::getcolor(int getback) const
360 {
361     short fore, back;
362
363     if (HaveColors()) {
364         if (::pair_content(static_cast<short>(PAIR_NUMBER(w->_attrs)), &fore, &back) == ERR)
365             err_handler("Can't get color pair");
366     } else {
367         // Monochrome means white on black
368         back = COLOR_BLACK;
369         fore = COLOR_WHITE;
370     }
371     return getback ? back : fore;
372 }
373
374 int NCursesWindow::NumberOfColors()
375 {
376     return (HaveColors()) ? COLORS : 1;
377 }
378
379 short
380 NCursesWindow::getcolor() const
381 {
382     return (HaveColors()) ? PAIR_NUMBER(w->_attrs) : 0;
383 }
384
385 int
386 NCursesWindow::setpalette(short fore, short back, short pair)
387 {
388     return (HaveColors()) ? ::init_pair(pair, fore, back) : OK;
389 }
390
391 int
392 NCursesWindow::setpalette(short fore, short back)
393 {
394     return setpalette(fore, back, static_cast<short>(PAIR_NUMBER(w->_attrs)));
395 }
396
397
398 int
399 NCursesWindow::setcolor(short pair)
400 {
401     if (HaveColors()) {
402         if ((pair < 1) || (pair > COLOR_PAIRS))
403             err_handler("Can't set color pair");
404
405         attroff(A_COLOR);
406         attrset(COLOR_PAIR(pair));
407     }
408     return OK;
409 }
410
411 #if HAVE_HAS_KEY
412 bool NCursesWindow::has_mouse() const
413 {
414     return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
415              ? TRUE : FALSE);
416 }
417 #endif