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