]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesp.h
f347875f2c79a708d0a01200a4b2fd96ee636e1f
[ncurses.git] / c++ / cursesp.h
1 // * This makes emacs happy -*-Mode: C++;-*-
2 #ifndef _CURSESP_H
3 #define _CURSESP_H
4
5 // $Id: cursesp.h,v 1.10 1998/02/17 09:01:28 juergen Exp $
6
7 #include <cursesw.h>
8
9 extern "C" {
10 #  include <panel.h>
11 }
12
13 class NCursesPanel : public NCursesWindow {
14 protected:
15   PANEL *p;
16   static NCursesPanel *dummy;
17
18 private:
19   // This structure is used for the panel's user data field to link the
20   // PANEL* to the C++ object and to provide extra space for a user pointer.
21   typedef struct {
22     void*               m_user;      // the pointer for the user's data
23     const NCursesPanel* m_back;      // backward pointer to C++ object
24     const PANEL*        m_owner;     // the panel itself
25   } UserHook;
26
27   void init();                       // Initialize the panel object
28
29 protected:
30   void set_user(void *user) {
31     UserHook* uptr = (UserHook*)::panel_userptr (p);
32     assert (uptr && uptr->m_back==this && uptr->m_owner==p);
33     uptr->m_user = user;
34   }
35   // Set the user pointer of the panel.
36   
37   void *get_user() {
38     UserHook* uptr = (UserHook*)::panel_userptr (p);
39     assert (uptr && uptr->m_back==this && uptr->m_owner==p);
40     return uptr->m_user;
41   }
42   
43   void OnError (int err) const THROWS((NCursesPanelException)) {
44     if (err==ERR)
45       THROW(new NCursesPanelException (this, err));
46   }
47   // If err is equal to the curses error indicator ERR, an error handler
48   // is called.
49
50 public:
51   NCursesPanel(int lines,
52                int cols,
53                int begin_y = 0,
54                int begin_x = 0)
55     : NCursesWindow(lines,cols,begin_y,begin_x) {
56       init();
57   } 
58   // Create a panel with this size starting at the requested position.
59
60   NCursesPanel() : NCursesWindow(::stdscr) { init(); }
61   // This constructor creates the default Panel associated with the
62   // ::stdscr window
63
64   virtual ~NCursesPanel();
65   
66   // basic manipulation
67   inline void hide() {
68     OnError (::hide_panel(p));
69   }
70   // Hide the panel. It stays in the stack but becomes invisible.
71
72   inline void show() {
73     OnError (::show_panel(p));
74   }
75   // Show the panel, i.e. make it visible.
76
77   inline void top() {
78     OnError (::top_panel(p));
79   }
80   // Make this panel the top panel in the stack.
81   
82   inline void bottom() {
83     OnError (::bottom_panel(p));
84   }
85   // Make this panel the bottom panel in the stack.
86   // N.B.: The panel associated with ::stdscr is always on the bottom. So
87   // actually bottom() makes the panel the first above ::stdscr.
88   
89   inline int mvwin(int y, int x) {
90     OnError(::move_panel(p, y, x));
91     return OK;
92   }
93   
94   inline bool hidden() const {
95     return (::panel_hidden (p) ? TRUE : FALSE);
96   }
97   // Return TRUE if the panel is hidden, FALSE otherwise.
98
99 /* The functions panel_above() and panel_below() are not reflected in
100    the NCursesPanel class. The reason for this is, that we cannot
101    assume that a panel retrieved by those operations is one wrapped
102    by a C++ class. Although this situation might be handled, we also
103    need a reverse mapping from PANEL to NCursesPanel which needs some
104    redesign of the low level stuff. At the moment, we define them in the
105    interface but they will always produce an error. */
106   inline NCursesPanel& above() const {
107     OnError(ERR);
108     return *dummy;
109   }
110
111   inline NCursesPanel& below() const {
112     OnError(ERR);
113     return *dummy;
114   }
115
116   // Those two are rewrites of the corresponding virtual members of
117   // NCursesWindow
118   int refresh();
119   // Propagate all panel changes to the virtual screen and update the 
120   // physical screen.
121
122   int noutrefresh();
123   // Propagate all panel changes to the virtual screen.
124
125   static void redraw();
126   // Redraw all panels.
127  
128   // decorations
129   virtual void frame(const char* title=NULL, 
130                      const char* btitle=NULL);
131   // Put a frame around the panel and put the title centered in the top line
132   // and btitle in the bottom line.
133
134   virtual void boldframe(const char* title=NULL,
135                          const char* btitle=NULL);
136   // Same as frame(), but use highlighted attributes.
137
138   virtual void label(const char* topLabel,
139                      const char* bottomLabel);
140   // Put the title centered in the top line and btitle in the bottom line.
141
142   virtual void centertext(int row,const char* label);
143   // Put the label text centered in the specified row.
144 };
145
146 /* We use templates to provide a typesafe mechanism to associate
147  * user data with a panel. A NCursesUserPanel<T> is a panel 
148  * associated with some user data of type T.
149  */
150 template<class T> class NCursesUserPanel : public NCursesPanel
151 {
152 public:
153   NCursesUserPanel (int lines,
154                     int cols,
155                     int begin_y = 0,
156                     int begin_x = 0,
157                     const T* p_UserData = (T*)0)
158     : NCursesPanel (lines, cols, begin_y, begin_x) {
159       if (p)
160         set_user ((void *)p_UserData);
161   };
162   // This creates an user panel of the requested size with associated
163   // user data pointed to by p_UserData.
164   
165   NCursesUserPanel(const T* p_UserData = (T*)0) : NCursesPanel() {
166     if (p)
167       set_user((void *)p_UserData);
168   };
169   // This creates an user panel associated with the ::stdscr and user data
170   // pointed to by p_UserData.
171
172   virtual ~NCursesUserPanel() {};
173
174   T* UserData (void) const {
175     return (T*)get_user ();
176   };
177   // Retrieve the user data associated with the panel.
178   
179   virtual void setUserData (const T* p_UserData) {
180     if (p)
181       set_user ((void *)p_UserData);
182   }
183   // Associate the user panel with the user data pointed to by p_UserData.
184 };
185
186 #endif // _CURSESP_H