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