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