1 // This may look like C code, but it is really -*- 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 
20 #ifndef _CursesWindow_h
21 #ifdef __GNUG__
22 #pragma interface
23 #endif
24 #define _CursesWindow_h
25 
26 #include   <curses.h>
27 
28 #if _G_HAVE_CURSES
29 
30 // "Convert" macros to inlines, if needed.
31 #ifdef addch
32 inline int (addch)(char ch)  { return addch(ch); }
33 #undef addch
34 #endif
35 #ifdef addstr
36 /* The (char*) cast is to hack around missing const's */
37 inline int (addstr)(const char * str)  { return addstr((char*)str); }
38 #undef addstr
39 #endif
40 #ifdef clear
41 inline int (clear)()  { return clear(); }
42 #undef clear
43 #endif
44 #ifdef clearok
45 inline int (clearok)(WINDOW* win, int bf)  { return clearok(win, bf); }
46 #undef clearok
47 #else
48 extern "C" int clearok(WINDOW*, int);
49 #endif
50 #ifdef clrtobot
51 inline int (clrtobot)()  { return clrtobot(); }
52 #undef clrtobot
53 #endif
54 #ifdef clrtoeol
55 inline int (clrtoeol)()  { return clrtoeol(); }
56 #undef clrtoeol
57 #endif
58 #ifdef delch
59 inline int (delch)()  { return delch(); }
60 #undef delch
61 #endif
62 #ifdef deleteln
63 inline int (deleteln)()  { return deleteln(); }
64 #undef deleteln
65 #endif
66 #ifdef erase
67 inline int (erase)()  { return erase(); }
68 #undef erase
69 #endif
70 #ifdef flushok
71 inline int (flushok)(WINDOW* _win, int _bf)  { return flushok(_win, _bf); }
72 #undef flushok
73 #else
74 #define _no_flushok
75 #endif
76 #ifdef getch
77 inline int (getch)()  { return getch(); }
78 #undef getch
79 #endif
80 #ifdef getstr
81 inline int (getstr)(char *_str)  { return getstr(_str); }
82 #undef getstr
83 #endif
84 #ifdef getyx
85 inline void (getyx)(WINDOW* win, int& y, int& x) { getyx(win, y, x); }
86 #undef getyx
87 #endif
88 #ifdef inch
89 inline int (inch)()  { return inch(); }
90 #undef inch
91 #endif
92 #ifdef insch
93 inline int (insch)(char c)  { return insch(c); }
94 #undef insch
95 #endif
96 #ifdef insertln
97 inline int (insertln)()  { return insertln(); }
98 #undef insertln
99 #endif
100 #ifdef leaveok
101 inline int (leaveok)(WINDOW* win, int bf)  { return leaveok(win, bf); }
102 #undef leaveok
103 #else
104 extern "C" int leaveok(WINDOW* win, int bf);
105 #endif
106 #ifdef move
107 inline int (move)(int x, int y)  { return move(x, y); }
108 #undef move
109 #endif
110 #ifdef refresh
111 inline int (rfresh)()  { return refresh(); }
112 #undef refresh
113 #endif
114 #ifdef scrollok
115 inline int (scrollok)(WINDOW* win, int bf)  { return scrollok(win, bf); }
116 #undef scrollok
117 #else
118 #ifndef hpux
119 extern "C" int scrollok(WINDOW*, int);
120 #else
121 extern "C" int scrollok(WINDOW*, char);
122 #endif
123 #endif
124 #ifdef standend
125 inline char * (standend)()  { return standend(); }
126 #undef standend
127 #endif
128 #ifdef standout
129 inline char * (standout)()  { return standout(); }
130 #undef standout
131 #endif
132 #ifdef winch
133 inline int (winch)(WINDOW* win) { return winch(win); }
134 #undef winch
135 #endif
136 
137 #ifdef mvwaddch
138 inline int (mvwaddch)(WINDOW *win, int y, int x, char ch)
139 { return mvwaddch(win, y, x, ch); }
140 #undef mvwaddch
141 #endif
142 #ifdef mvwaddstr
143 inline int (mvwaddstr)(WINDOW *win, int y, int x, const char * str)
144 { return mvwaddstr(win, y, x, (char*)str); }
145 #undef mvwaddstr
146 #endif
147 #ifdef mvwdelch
148 inline int (mvwdelch)(WINDOW *win, int y, int x) { return mvwdelch(win, y, x);}
149 #undef mvwdelch
150 #endif
151 #ifdef mvwgetch
152 inline int (mvwgetch)(WINDOW *win, int y, int x) { return mvwgetch(win, y, x);}
153 #undef mvwgetch
154 #endif
155 #ifdef mvwgetstr
156 inline int (mvwgetstr)(WINDOW *win, int y, int x, char *str)
157 {return mvwgetstr(win,y,x, str);}
158 #undef mvwgetstr
159 #endif
160 #ifdef mvwinch
161 inline int (mvwinch)(WINDOW *win, int y, int x) { return mvwinch(win, y, x);}
162 #undef mvwinch
163 #endif
164 #ifdef mvwinsch
165 inline int (mvwinsch)(WINDOW *win, int y, int x, char c)
166 { return mvwinsch(win, y, x, c); }
167 #undef mvwinsch
168 #endif
169 
170 #ifdef mvaddch
171 inline int (mvaddch)(int y, int x, char ch)
172 { return mvaddch(y, x, ch); }
173 #undef mvaddch
174 #endif
175 #ifdef mvaddstr
176 inline int (mvaddstr)(int y, int x, const char * str)
177 { return mvaddstr(y, x, (char*)str); }
178 #undef mvaddstr
179 #endif
180 #ifdef mvdelch
181 inline int (mvdelch)(int y, int x) { return mvdelch(y, x);}
182 #undef mvdelch
183 #endif
184 #ifdef mvgetch
185 inline int (mvgetch)(int y, int x) { return mvgetch(y, x);}
186 #undef mvgetch
187 #endif
188 #ifdef mvgetstr
189 inline int (mvgetstr)(int y, int x, char *str) {return mvgetstr(y, x, str);}
190 #undef mvgetstr
191 #endif
192 #ifdef mvinch
193 inline int (mvinch)(int y, int x) { return mvinch(y, x);}
194 #undef mvinch
195 #endif
196 #ifdef mvinsch
197 inline int (mvinsch)(int y, int x, char c)
198 { return mvinsch(y, x, c); }
199 #undef mvinsch
200 #endif
201 
202 /*
203  *
204  * C++ class for windows.
205  *
206  *
207  */
208 
209 class CursesWindow
210 {
211 protected:
212   static int     count;           // count of all active windows:
213                                   //   We rely on the c++ promise that
214                                   //   all otherwise uninitialized
215                                   //   static class vars are set to 0
216 
217   WINDOW *       w;               // the curses WINDOW
218 
219   int            alloced;         // true if we own the WINDOW
220 
221   CursesWindow*  par;             // parent, if subwindow
222   CursesWindow*  subwins;         // head of subwindows list
223   CursesWindow*  sib;             // next subwindow of parent
224 
225   void           kill_subwindows(); // disable all subwindows
226 
227 public:
228                  CursesWindow(WINDOW* &window);   // useful only for stdscr
229 
230                  CursesWindow(int lines,          // number of lines
231                               int cols,           // number of columns
232                               int begin_y,        // line origin
233                               int begin_x);       // col origin
234 
235                  CursesWindow(CursesWindow& par,  // parent window
236                               int lines,          // number of lines
237                               int cols,           // number of columns
238                               int by,             // absolute or relative
239                               int bx,             //   origins:
240                               char absrel = 'a'); // if `a', by & bx are
241                                                   // absolute screen pos,
242                                                   // else if `r', they are
243                                                   // relative to par origin
244                 ~CursesWindow();
245 
246 // terminal status
247   int            lines(); // number of lines on terminal, *not* window
248   int            cols();  // number of cols  on terminal, *not* window
249 
250 // window status
251   int            height(); // number of lines in this window
252   int            width();  // number of cols in this window
253   int            begx();   // smallest x coord in window
254   int            begy();   // smallest y coord in window
255   int            maxx();   // largest  x coord in window
256   int            maxy();   // largest  x coord in window
257 
258 // window positioning
259   int            move(int y, int x);
260 
261 // coordinate positioning
262   void           getyx(int& y, int& x);
263   int            mvcur(int sy, int ey, int sx, int ex);
264 
265 // input
266   int            getch();
267   int            getstr(char * str);
268   int            scanw(const char *, ...);
269 
270 // input + positioning
271   int            mvgetch(int y, int x);
272   int            mvgetstr(int y, int x, char * str);
273   int            mvscanw(int, int, const char*, ...);
274 
275 // output
276   int            addch(const char ch);
277   int            addstr(const char * str);
278   int            printw(const char * fmt, ...);
279   int            inch();
280   int            insch(char c);
281   int            insertln();
282 
283 // output + positioning
284   int            mvaddch(int y, int x, char ch);
285   int            mvaddstr(int y, int x, const char * str);
286   int            mvprintw(int y, int x, const char * fmt, ...);
287   int            mvinch(int y, int x);
288   int            mvinsch(int y, int x, char ch);
289 
290 // borders
291   int            box(char vert, char  hor);
292 
293 // erasure
294   int            erase();
295   int            clear();
296   int            clearok(int bf);
297   int            clrtobot();
298   int            clrtoeol();
299   int            delch();
300   int            mvdelch(int y, int x);
301   int            deleteln();
302 
303 // screen control
304   int            scroll();
305   int            scrollok(int bf);
306   int            touchwin();
307   int            refresh();
308   int            leaveok(int bf);
309 #ifndef _no_flushok
310   int            flushok(int bf);
311 #endif
312   char*            standout();
313   char*            standend();
314 
315 // multiple window control
316   int            overlay(CursesWindow &win);
317   int            overwrite(CursesWindow &win);
318 
319 
320 // traversal support
321   CursesWindow*  child();
322   CursesWindow*  sibling();
323   CursesWindow*  parent();
324 };
325 
326 
begx()327 inline int CursesWindow::begx()
328 {
329   return w->begx;
330 }
331 
begy()332 inline int CursesWindow::begy()
333 {
334   return w->begy;
335 }
336 
maxx()337 inline int CursesWindow::maxx()
338 {
339   return w->maxx;
340 }
341 
maxy()342 inline int CursesWindow::maxy()
343 {
344   return w->maxy;
345 }
346 
height()347 inline int CursesWindow::height()
348 {
349   return maxy() - begy() + 1;
350 }
351 
width()352 inline int CursesWindow::width()
353 {
354   return maxx() - begx() + 1;
355 }
356 
box(char vert,char hor)357 inline int CursesWindow::box(char vert, char  hor)
358 {
359   return ::box(w, vert, hor);
360 }
361 
overlay(CursesWindow & win)362 inline int CursesWindow::overlay(CursesWindow &win)
363 {
364   return ::overlay(w, win.w);
365 }
366 
overwrite(CursesWindow & win)367 inline int CursesWindow::overwrite(CursesWindow &win)
368 {
369   return ::overwrite(w, win.w);
370 }
371 
scroll()372 inline int CursesWindow::scroll()
373 {
374   return ::scroll(w);
375 }
376 
377 
touchwin()378 inline int CursesWindow::touchwin()
379 {
380   return ::touchwin(w);
381 }
382 
addch(const char ch)383 inline int CursesWindow::addch(const char ch)
384 {
385   return ::waddch(w, ch);
386 }
387 
addstr(const char * str)388 inline int CursesWindow::addstr(const char * str)
389 {
390   // The (char*) cast is to hack around prototypes in curses.h that
391   // have const missing in the parameter lists.  [E.g. SVR4]
392   return ::waddstr(w, (char*)str);
393 }
394 
clear()395 inline int CursesWindow::clear()
396 {
397   return ::wclear(w);
398 }
399 
clrtobot()400 inline int CursesWindow::clrtobot()
401 {
402   return ::wclrtobot(w);
403 }
404 
clrtoeol()405 inline int CursesWindow::clrtoeol()
406 {
407   return ::wclrtoeol(w);
408 }
409 
delch()410 inline int CursesWindow::delch()
411 {
412   return ::wdelch(w);
413 }
414 
deleteln()415 inline int CursesWindow::deleteln()
416 {
417   return ::wdeleteln(w);
418 }
419 
erase()420 inline int CursesWindow::erase()
421 {
422   return ::werase(w);
423 }
424 
getch()425 inline int CursesWindow::getch()
426 {
427   return ::wgetch(w);
428 }
429 
getstr(char * str)430 inline int CursesWindow::getstr(char * str)
431 {
432   return ::wgetstr(w, str);
433 }
434 
inch()435 inline int CursesWindow::inch()
436 {
437   return winch(w);
438 }
439 
insch(char c)440 inline int CursesWindow::insch(char c)
441 {
442   return ::winsch(w, c);
443 }
444 
insertln()445 inline int CursesWindow::insertln()
446 {
447   return ::winsertln(w);
448 }
449 
move(int y,int x)450 inline int CursesWindow::move(int y, int x)
451 {
452   return ::wmove(w, y, x);
453 }
454 
455 
mvcur(int sy,int ey,int sx,int ex)456 inline int CursesWindow::mvcur(int sy, int ey, int sx, int ex)
457 {
458   return ::mvcur(sy, ey, sx,ex);
459 }
460 
mvaddch(int y,int x,char ch)461 inline int CursesWindow::mvaddch(int y, int x, char ch)
462 {
463   return (::wmove(w, y, x)==ERR) ? ERR : ::waddch(w, ch);
464 }
465 
mvgetch(int y,int x)466 inline int CursesWindow::mvgetch(int y, int x)
467 {
468   return (::wmove(w, y, x)==ERR) ? ERR : ::wgetch(w);
469 }
470 
mvaddstr(int y,int x,const char * str)471 inline int CursesWindow::mvaddstr(int y, int x, const char * str)
472 {
473   return (::wmove(w, y, x)==ERR) ? ERR : ::waddstr(w, (char*)str);
474 }
475 
mvgetstr(int y,int x,char * str)476 inline int CursesWindow::mvgetstr(int y, int x, char * str)
477 {
478   return (::wmove(w, y, x)==ERR) ? ERR : ::wgetstr(w, str);
479 }
480 
mvinch(int y,int x)481 inline int CursesWindow::mvinch(int y, int x)
482 {
483   return (::wmove(w, y, x)==ERR) ? ERR : ::winch(w);
484 }
485 
mvdelch(int y,int x)486 inline int CursesWindow::mvdelch(int y, int x)
487 {
488   return (::wmove(w, y, x)==ERR) ? ERR : ::wdelch(w);
489 }
490 
mvinsch(int y,int x,char ch)491 inline int CursesWindow::mvinsch(int y, int x, char ch)
492 {
493   return (::wmove(w, y, x)==ERR) ? ERR : ::winsch(w, ch);
494 }
495 
refresh()496 inline int CursesWindow::refresh()
497 {
498   return ::wrefresh(w);
499 }
500 
clearok(int bf)501 inline int CursesWindow::clearok(int bf)
502 {
503   return ::clearok(w,bf);
504 }
505 
leaveok(int bf)506 inline int CursesWindow::leaveok(int bf)
507 {
508   return ::leaveok(w,bf);
509 }
510 
scrollok(int bf)511 inline int CursesWindow::scrollok(int bf)
512 {
513   return ::scrollok(w,bf);
514 }
515 
516 #ifndef _no_flushok
flushok(int bf)517 inline int CursesWindow::flushok(int bf)
518 {
519   return ::flushok(w, bf);
520 }
521 #endif
522 
getyx(int & y,int & x)523 inline void CursesWindow::getyx(int& y, int& x)
524 {
525   ::getyx(w, y, x);
526 }
527 
standout()528 inline char *CursesWindow::standout()
529 {
530   return ::wstandout(w);
531 }
532 
standend()533 inline char *CursesWindow::standend()
534 {
535   return ::wstandend(w);
536 }
537 
lines()538 inline int CursesWindow::lines()
539 {
540   return LINES;
541 }
542 
cols()543 inline int CursesWindow::cols()
544 {
545   return COLS;
546 }
547 
child()548 inline CursesWindow* CursesWindow::child()
549 {
550   return subwins;
551 }
552 
parent()553 inline CursesWindow* CursesWindow::parent()
554 {
555   return par;
556 }
557 
sibling()558 inline CursesWindow* CursesWindow::sibling()
559 {
560   return sib;
561 }
562 
563 #endif /* _G_HAVE_CURSES */
564 
565 #endif
566