]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesp.h
ncurses 6.2 - patch 20200509
[ncurses.git] / c++ / cursesp.h
1 // * This makes emacs happy -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright 2019,2020 Thomas E. Dickey                                     *
4  * Copyright 1998-2012,2014 Free Software Foundation, Inc.                  *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30
31 /****************************************************************************
32  *   Author: Juergen Pfeifer, 1997                                          *
33  ****************************************************************************/
34
35 #ifndef NCURSES_CURSESP_H_incl
36 #define NCURSES_CURSESP_H_incl 1
37
38 // $Id: cursesp.h,v 1.33 2020/02/02 23:34:34 tom Exp $
39
40 #include <cursesw.h>
41
42 extern "C" {
43 #  include <panel.h>
44 }
45
46 class NCURSES_IMPEXP NCursesPanel
47   : public NCursesWindow
48 {
49 protected:
50   PANEL *p;
51   static NCursesPanel *dummy;
52
53 private:
54   // This structure is used for the panel's user data field to link the
55   // PANEL* to the C++ object and to provide extra space for a user pointer.
56   typedef struct {
57     void*               m_user;      // the pointer for the user's data
58     const NCursesPanel* m_back;      // backward pointer to C++ object
59     const PANEL*        m_owner;     // the panel itself
60   } UserHook;
61
62   inline UserHook *UserPointer()
63   {
64     UserHook* uptr = reinterpret_cast<UserHook*>(
65                            const_cast<void *>(::panel_userptr (p)));
66     return uptr;
67   }
68
69   void init();                       // Initialize the panel object
70
71 protected:
72   void set_user(void *user)
73   {
74     UserHook* uptr = UserPointer();
75     if (uptr != 0 && uptr->m_back==this && uptr->m_owner==p) {
76       uptr->m_user = user;
77     }
78   }
79   // Set the user pointer of the panel.
80
81   void *get_user()
82   {
83     UserHook* uptr = UserPointer();
84     void *result = 0;
85     if (uptr != 0 && uptr->m_back==this && uptr->m_owner==p)
86       result = uptr->m_user;
87     return result;
88   }
89
90   void OnError (int err) const THROW2(NCursesException const, NCursesPanelException)
91   {
92     if (err==ERR)
93       THROW(new NCursesPanelException (this, err));
94   }
95   // If err is equal to the curses error indicator ERR, an error handler
96   // is called.
97
98   // Get a keystroke. Default implementation calls getch()
99   virtual int getKey(void);
100
101 public:
102   NCursesPanel(int nlines,
103                int ncols,
104                int begin_y = 0,
105                int begin_x = 0)
106     : NCursesWindow(nlines,ncols,begin_y,begin_x), p(0)
107   {
108     init();
109   }
110   // Create a panel with this size starting at the requested position.
111
112   NCursesPanel()
113     : NCursesWindow(::stdscr), p(0)
114   {
115     init();
116   }
117   // This constructor creates the default Panel associated with the
118   // ::stdscr window
119
120   NCursesPanel& operator=(const NCursesPanel& rhs)
121   {
122     if (this != &rhs) {
123       *this = rhs;
124       NCursesWindow::operator=(rhs);
125     }
126     return *this;
127   }
128
129   NCursesPanel(const NCursesPanel& rhs)
130     : NCursesWindow(rhs),
131       p(rhs.p)
132   {
133   }
134
135   virtual ~NCursesPanel() THROWS(NCursesException);
136
137   // basic manipulation
138   inline void hide()
139   {
140     OnError (::hide_panel(p));
141   }
142   // Hide the panel. It stays in the stack but becomes invisible.
143
144   inline void show()
145   {
146     OnError (::show_panel(p));
147   }
148   // Show the panel, i.e. make it visible.
149
150   inline void top()
151   {
152     OnError (::top_panel(p));
153   }
154   // Make this panel the top panel in the stack.
155
156   inline void bottom()
157   {
158     OnError (::bottom_panel(p));
159   }
160   // Make this panel the bottom panel in the stack.
161   // N.B.: The panel associated with ::stdscr is always on the bottom. So
162   // actually bottom() makes the panel the first above ::stdscr.
163
164   virtual int mvwin(int y, int x)
165   {
166     OnError(::move_panel(p, y, x));
167     return OK;
168   }
169
170   inline bool hidden() const
171   {
172     return (::panel_hidden (p) ? TRUE : FALSE);
173   }
174   // Return TRUE if the panel is hidden, FALSE otherwise.
175
176 /* The functions panel_above() and panel_below() are not reflected in
177    the NCursesPanel class. The reason for this is, that we cannot
178    assume that a panel retrieved by those operations is one wrapped
179    by a C++ class. Although this situation might be handled, we also
180    need a reverse mapping from PANEL to NCursesPanel which needs some
181    redesign of the low level stuff. At the moment, we define them in the
182    interface but they will always produce an error. */
183   inline NCursesPanel& above() const
184   {
185     OnError(ERR);
186     return *dummy;
187   }
188
189   inline NCursesPanel& below() const
190   {
191     OnError(ERR);
192     return *dummy;
193   }
194
195   // Those two are rewrites of the corresponding virtual members of
196   // NCursesWindow
197   virtual int refresh();
198   // Propagate all panel changes to the virtual screen and update the
199   // physical screen.
200
201   virtual int noutrefresh();
202   // Propagate all panel changes to the virtual screen.
203
204   static void redraw();
205   // Redraw all panels.
206
207   // decorations
208   virtual void frame(const char* title=NULL,
209                      const char* btitle=NULL);
210   // Put a frame around the panel and put the title centered in the top line
211   // and btitle in the bottom line.
212
213   virtual void boldframe(const char* title=NULL,
214                          const char* btitle=NULL);
215   // Same as frame(), but use highlighted attributes.
216
217   virtual void label(const char* topLabel,
218                      const char* bottomLabel);
219   // Put the title centered in the top line and btitle in the bottom line.
220
221   virtual void centertext(int row,const char* label);
222   // Put the label text centered in the specified row.
223 };
224
225 /* We use templates to provide a typesafe mechanism to associate
226  * user data with a panel. A NCursesUserPanel<T> is a panel
227  * associated with some user data of type T.
228  */
229 template<class T> class NCursesUserPanel : public NCursesPanel
230 {
231 public:
232   NCursesUserPanel (int nlines,
233                     int ncols,
234                     int begin_y = 0,
235                     int begin_x = 0,
236                     const T* p_UserData = STATIC_CAST(T*)(0))
237     : NCursesPanel (nlines, ncols, begin_y, begin_x)
238   {
239       if (p)
240         set_user (const_cast<void *>(reinterpret_cast<const void*>
241                                      (p_UserData)));
242   };
243   // This creates an user panel of the requested size with associated
244   // user data pointed to by p_UserData.
245
246   NCursesUserPanel(const T* p_UserData = STATIC_CAST(T*)(0)) : NCursesPanel()
247   {
248     if (p)
249       set_user(const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
250   };
251   // This creates an user panel associated with the ::stdscr and user data
252   // pointed to by p_UserData.
253
254   virtual ~NCursesUserPanel() THROWS(NCursesException) {};
255
256   T* UserData (void)
257   {
258     return reinterpret_cast<T*>(get_user ());
259   };
260   // Retrieve the user data associated with the panel.
261
262   virtual void setUserData (const T* p_UserData)
263   {
264     if (p)
265       set_user (const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
266   }
267   // Associate the user panel with the user data pointed to by p_UserData.
268 };
269
270 #endif /* NCURSES_CURSESP_H_incl */