]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesw.cc
ncurses 5.2
[ncurses.git] / c++ / cursesw.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2
3 /*
4   Copyright (C) 1989 Free Software Foundation
5   written by Eric Newton (newton@rocky.oswego.edu)
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  (drepper@karlsruhe.gmd.de)
20           and Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
21
22   modified by Juergen Pfeifer (juergen.pfeifer@gmx.net)
23 */
24
25 #include "cursesw.h"
26 #include "internal.h"
27
28 MODULE_ID("$Id: cursesw.cc,v 1.17 2000/09/02 18:55:31 tom Exp $")
29
30 #define COLORS_NEED_INITIALIZATION  -1
31 #define COLORS_NOT_INITIALIZED       0
32 #define COLORS_MONOCHROME            1
33 #define COLORS_ARE_REALLY_THERE      2
34
35 // declare static variables for the class
36 long NCursesWindow::count = 0L;
37 bool NCursesWindow::b_initialized = FALSE;
38
39 #if defined(__GNUG__)
40 #  ifndef _IO_va_list
41 #    define _IO_va_list char *
42 #  endif
43 #endif
44
45 int
46 NCursesWindow::scanw(const char* fmt, ...)
47 {
48 #if defined(__GNUG__)
49     va_list args;
50     va_start(args, fmt);
51     char buf[BUFSIZ];
52     int result = wgetstr(w, buf);
53     if (result == OK) {
54         strstreambuf ss(buf, sizeof(buf));
55         result = ss.vscan(fmt, (_IO_va_list)args);
56     }
57     va_end(args);
58     return result;
59 #else
60     return ERR;
61 #endif
62 }
63
64
65 int
66 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
67 {
68 #if defined(__GNUG__)
69     va_list args;
70     va_start(args, fmt);
71     char buf[BUFSIZ];
72     int result = wmove(w, y, x);
73     if (result == OK) {
74         result = wgetstr(w, buf);
75         if (result == OK) {
76             strstreambuf ss(buf, sizeof(buf));
77             result = ss.vscan(fmt, (_IO_va_list)args);
78         }
79     }
80     va_end(args);
81     return result;
82 #else
83     return ERR;
84 #endif
85 }
86
87
88 int
89 NCursesWindow::printw(const char * fmt, ...)
90 {
91     va_list args;
92     va_start(args, fmt);
93     char buf[BUFSIZ];
94     vsprintf(buf, fmt, args);
95     va_end(args);
96     return waddstr(w, buf);
97 }
98
99
100 int
101 NCursesWindow::printw(int y, int x, const char * fmt, ...)
102 {
103     va_list args;
104     va_start(args, fmt);
105     int result = wmove(w, y, x);
106     if (result == OK) {
107         char buf[BUFSIZ];
108         vsprintf(buf, fmt, args);
109         result = waddstr(w, buf);
110     }
111     va_end(args);
112     return result;
113 }
114
115
116 void
117 NCursesWindow::init(void)
118 {
119     leaveok(0);
120     keypad(1);
121     meta(1);
122 }
123
124 void
125 NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
126 {
127   THROW(new NCursesException(msg));
128 }
129
130 void
131 NCursesWindow::initialize() {
132   if (!b_initialized) {
133     ::initscr();
134     b_initialized = TRUE;
135     if (colorInitialized==COLORS_NEED_INITIALIZATION) {
136       colorInitialized=COLORS_NOT_INITIALIZED;
137       useColors();
138     }
139     ::noecho();
140     ::cbreak();
141   }
142 }
143
144 NCursesWindow::NCursesWindow() {
145   if (!b_initialized)
146     initialize();
147
148   w = (WINDOW *)0;
149   init();
150   alloced = FALSE;
151   subwins = par = sib = 0;
152   count++;
153 }
154
155 NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
156 {
157     if (!b_initialized)
158       initialize();
159
160     w = ::newwin(lines, cols, begin_y, begin_x);
161     if (w == 0) {
162         err_handler("Cannot construct window");
163     }
164     init();
165
166     alloced = TRUE;
167     subwins = par = sib = 0;
168     count++;
169 }
170
171 NCursesWindow::NCursesWindow(WINDOW* &window)
172 {
173     if (!b_initialized)
174       initialize();
175
176     w = window;
177     init();
178     alloced = FALSE;
179     subwins = par = sib = 0;
180     count++;
181 }
182
183 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
184                              int begin_y, int begin_x, char absrel)
185 {
186     if (absrel == 'a') { // absolute origin
187         begin_y -= win.begy();
188         begin_x -= win.begx();
189     }
190
191     // Even though we treat subwindows as a tree, the standard curses
192     // library needs the `subwin' call to link to the parent in
193     // order to correctly perform refreshes, etc.
194     // Friendly enough, this also works for pads.
195     w = ::derwin(win.w, l, c, begin_y, begin_x);
196     if (w == 0) {
197         err_handler("Cannot construct subwindow");
198     }
199
200     par = &win;
201     sib = win.subwins;
202     win.subwins = this;
203     subwins = 0;
204     alloced = TRUE;
205     count++;
206 }
207
208 NCursesWindow::NCursesWindow(NCursesWindow& win,
209                                 bool do_box NCURSES_PARAM_INIT(TRUE))
210 {
211   w = :: derwin(win.w,win.height()-2,win.width()-2,1,1);
212   if (w == 0) {
213     err_handler("Cannot construct subwindow");
214   }
215
216   par = &win;
217   sib = win.subwins;
218   win.subwins = this;
219   subwins = 0;
220   alloced = TRUE;
221   count++;
222
223   if (do_box) {
224     win.box();
225     win.touchwin();
226   }
227 }
228
229 NCursesWindow NCursesWindow::Clone() {
230   WINDOW *d = ::dupwin(w);
231   NCursesWindow W(d);
232   W.subwins = subwins;
233   W.sib = sib;
234   W.par = par;
235   W.alloced = alloced;
236   return W;
237 }
238
239 typedef int (*RIPOFFINIT)(NCursesWindow&);
240 static RIPOFFINIT R_INIT[5];       // There can't be more
241 static int r_init_idx   = 0;
242 static RIPOFFINIT* prip = R_INIT;
243
244 extern "C" int _nc_ripoffline(int,int (*init)(WINDOW*,int));
245
246 NCursesWindow::NCursesWindow(WINDOW *win, int cols) {
247   w = win;
248   assert((w->_maxx+1)==cols);
249   alloced = FALSE;
250   subwins = par = sib = 0;
251 }
252
253 int NCursesWindow::ripoff_init(WINDOW *w, int cols)
254 {
255   int res = ERR;
256
257   RIPOFFINIT init = *prip++;
258   if (init) {
259     NCursesWindow* W = new NCursesWindow(w,cols);
260     res = init(*W);
261   }
262   return res;
263 }
264
265 int NCursesWindow::ripoffline(int ripoff_lines,
266                               int (*init)(NCursesWindow& win)) {
267   int code = ::_nc_ripoffline(ripoff_lines,ripoff_init);
268   if (code==OK && init && ripoff_lines) {
269     R_INIT[r_init_idx++] = init;
270   }
271   return code;
272 }
273
274 bool
275 NCursesWindow::isDescendant(NCursesWindow& win) {
276   for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
277     if (p==&win)
278       return TRUE;
279     else {
280       if (p->isDescendant(win))
281         return TRUE;
282     }
283   }
284   return FALSE;
285 }
286
287 void
288 NCursesWindow::kill_subwindows()
289 {
290     for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
291         p->kill_subwindows();
292         if (p->alloced) {
293             if (p->w != 0)
294                 ::delwin(p->w);
295             p->alloced = FALSE;
296         }
297         p->w = 0; // cause a run-time error if anyone attempts to use...
298     }
299 }
300
301
302 NCursesWindow::~NCursesWindow()
303 {
304     kill_subwindows();
305
306     if (par != 0) {  // Snip us from the parent's list of subwindows.
307         NCursesWindow * win = par->subwins;
308         NCursesWindow * trail = 0;
309         for (;;) {
310             if (win == 0)
311                 break;
312             else if (win == this) {
313                 if (trail != 0)
314                     trail->sib = win->sib;
315                 else
316                     par->subwins = win->sib;
317                 break;
318             } else {
319                 trail = win;
320                 win = win->sib;
321             }
322         }
323     }
324
325     if (alloced && w != 0)
326         delwin(w);
327
328     if (alloced) {
329       --count;
330       if (count == 0) {
331         ::endwin();
332       }
333       else if (count < 0) { // cannot happen!
334         err_handler("Too many windows destroyed");
335       }
336     }
337 }
338
339 // ---------------------------------------------------------------------
340 // Color stuff
341 //
342 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
343
344 void
345 NCursesWindow::useColors(void)
346 {
347     if (colorInitialized == COLORS_NOT_INITIALIZED) {
348       if (b_initialized) {
349         if (::has_colors()) {
350           ::start_color();
351           colorInitialized = COLORS_ARE_REALLY_THERE;
352         }
353         else
354           colorInitialized = COLORS_MONOCHROME;
355       }
356       else
357         colorInitialized = COLORS_NEED_INITIALIZATION;
358     }
359 }
360
361 short
362 NCursesWindow::getcolor(int getback) const
363 {
364     short fore, back;
365
366     if (colorInitialized==COLORS_ARE_REALLY_THERE) {
367       if (pair_content((short)PAIR_NUMBER(w->_attrs), &fore, &back))
368         err_handler("Can't get color pair");
369     }
370     else {
371       // Monochrome means white on black
372       back = COLOR_BLACK;
373       fore = COLOR_WHITE;
374     }
375     return getback ? back : fore;
376 }
377
378 int NCursesWindow::NumberOfColors()
379 {
380   if (colorInitialized==COLORS_ARE_REALLY_THERE)
381     return COLORS;
382   else
383     return 1; // monochrome (actually there are two ;-)
384 }
385
386 short
387 NCursesWindow::getcolor() const
388 {
389   if (colorInitialized==COLORS_ARE_REALLY_THERE)
390     return PAIR_NUMBER(w->_attrs);
391   else
392     return 0; // we only have pair zero
393 }
394
395 int
396 NCursesWindow::setpalette(short fore, short back, short pair)
397 {
398   if (colorInitialized==COLORS_ARE_REALLY_THERE)
399     return init_pair(pair, fore, back);
400   else
401     return OK;
402 }
403
404 int
405 NCursesWindow::setpalette(short fore, short back)
406 {
407   if (colorInitialized==COLORS_ARE_REALLY_THERE)
408     return setpalette(fore, back, (short)PAIR_NUMBER(w->_attrs));
409   else
410     return OK;
411 }
412
413
414 int
415 NCursesWindow::setcolor(short pair)
416 {
417   if (colorInitialized==COLORS_ARE_REALLY_THERE) {
418     if ((pair < 1) || (pair > COLOR_PAIRS))
419       err_handler("Can't set color pair");
420
421     attroff(A_COLOR);
422     attrset(COLOR_PAIR(pair));
423   }
424   return OK;
425 }
426
427 #if HAVE_HAS_KEY
428 extern "C" int _nc_has_mouse(void);
429
430 bool NCursesWindow::has_mouse() const {
431   return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
432           ? TRUE : FALSE);
433 }
434 #endif