1// * This makes emacs happy -*-Mode: C++;-*-
2/****************************************************************************
3 * Copyright (c) 1998-2007,2008 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, 1997                                          *
32 ****************************************************************************/
33
34#ifndef NCURSES_CURSESP_H_incl
35#define NCURSES_CURSESP_H_incl 1
36
37// $Id: cursesp.h,v 1.29 2008/08/16 17:20:23 tom Exp $
38
39#include <cursesw.h>
40
41extern "C" {
42#  include <panel.h>
43}
44
45class NCURSES_IMPEXP NCursesPanel
46  : public NCursesWindow
47{
48protected:
49  PANEL *p;
50  static NCursesPanel *dummy;
51
52private:
53  // This structure is used for the panel's user data field to link the
54  // PANEL* to the C++ object and to provide extra space for a user pointer.
55  typedef struct {
56    void*               m_user;      // the pointer for the user's data
57    const NCursesPanel* m_back;      // backward pointer to C++ object
58    const PANEL*        m_owner;     // the panel itself
59  } UserHook;
60
61  inline UserHook *UserPointer()
62  {
63    UserHook* uptr = reinterpret_cast<UserHook*>(
64                           const_cast<void *>(::panel_userptr (p)));
65    return uptr;
66  }
67
68  void init();                       // Initialize the panel object
69
70protected:
71  void set_user(void *user)
72  {
73    UserHook* uptr = UserPointer();
74    if (uptr != 0 && uptr->m_back==this && uptr->m_owner==p) {
75      uptr->m_user = user;
76    }
77  }
78  // Set the user pointer of the panel.
79
80  void *get_user()
81  {
82    UserHook* uptr = UserPointer();
83    void *result = 0;
84    if (uptr != 0 && uptr->m_back==this && uptr->m_owner==p)
85      result = uptr->m_user;
86    return result;
87  }
88
89  void OnError (int err) const THROWS(NCursesPanelException)
90  {
91    if (err==ERR)
92      THROW(new NCursesPanelException (this, err));
93  }
94  // If err is equal to the curses error indicator ERR, an error handler
95  // is called.
96
97  // Get a keystroke. Default implementation calls getch()
98  virtual int getKey(void);
99
100public:
101  NCursesPanel(int nlines,
102	       int ncols,
103	       int begin_y = 0,
104	       int begin_x = 0)
105    : NCursesWindow(nlines,ncols,begin_y,begin_x), p(0)
106  {
107    init();
108  }
109  // Create a panel with this size starting at the requested position.
110
111  NCursesPanel()
112    : NCursesWindow(::stdscr), p(0)
113  {
114    init();
115  }
116  // This constructor creates the default Panel associated with the
117  // ::stdscr window
118
119  NCursesPanel& operator=(const NCursesPanel& rhs)
120  {
121    if (this != &rhs) {
122      *this = rhs;
123      NCursesWindow::operator=(rhs);
124    }
125    return *this;
126  }
127
128  NCursesPanel(const NCursesPanel& rhs)
129    : NCursesWindow(rhs),
130      p(rhs.p)
131  {
132  }
133
134  virtual ~NCursesPanel();
135
136  // basic manipulation
137  inline void hide()
138  {
139    OnError (::hide_panel(p));
140  }
141  // Hide the panel. It stays in the stack but becomes invisible.
142
143  inline void show()
144  {
145    OnError (::show_panel(p));
146  }
147  // Show the panel, i.e. make it visible.
148
149  inline void top()
150  {
151    OnError (::top_panel(p));
152  }
153  // Make this panel the top panel in the stack.
154
155  inline void bottom()
156  {
157    OnError (::bottom_panel(p));
158  }
159  // Make this panel the bottom panel in the stack.
160  // N.B.: The panel associated with ::stdscr is always on the bottom. So
161  // actually bottom() makes the panel the first above ::stdscr.
162
163  virtual int mvwin(int y, int x)
164  {
165    OnError(::move_panel(p, y, x));
166    return OK;
167  }
168
169  inline bool hidden() const
170  {
171    return (::panel_hidden (p) ? TRUE : FALSE);
172  }
173  // Return TRUE if the panel is hidden, FALSE otherwise.
174
175/* The functions panel_above() and panel_below() are not reflected in
176   the NCursesPanel class. The reason for this is, that we cannot
177   assume that a panel retrieved by those operations is one wrapped
178   by a C++ class. Although this situation might be handled, we also
179   need a reverse mapping from PANEL to NCursesPanel which needs some
180   redesign of the low level stuff. At the moment, we define them in the
181   interface but they will always produce an error. */
182  inline NCursesPanel& above() const
183  {
184    OnError(ERR);
185    return *dummy;
186  }
187
188  inline NCursesPanel& below() const
189  {
190    OnError(ERR);
191    return *dummy;
192  }
193
194  // Those two are rewrites of the corresponding virtual members of
195  // NCursesWindow
196  virtual int refresh();
197  // Propagate all panel changes to the virtual screen and update the
198  // physical screen.
199
200  virtual int noutrefresh();
201  // Propagate all panel changes to the virtual screen.
202
203  static void redraw();
204  // Redraw all panels.
205
206  // decorations
207  virtual void frame(const char* title=NULL,
208		     const char* btitle=NULL);
209  // Put a frame around the panel and put the title centered in the top line
210  // and btitle in the bottom line.
211
212  virtual void boldframe(const char* title=NULL,
213			 const char* btitle=NULL);
214  // Same as frame(), but use highlighted attributes.
215
216  virtual void label(const char* topLabel,
217		     const char* bottomLabel);
218  // Put the title centered in the top line and btitle in the bottom line.
219
220  virtual void centertext(int row,const char* label);
221  // Put the label text centered in the specified row.
222};
223
224/* We use templates to provide a typesafe mechanism to associate
225 * user data with a panel. A NCursesUserPanel<T> is a panel
226 * associated with some user data of type T.
227 */
228template<class T> class NCursesUserPanel : public NCursesPanel
229{
230public:
231  NCursesUserPanel (int nlines,
232		    int ncols,
233		    int begin_y = 0,
234		    int begin_x = 0,
235		    const T* p_UserData = STATIC_CAST(T*)(0))
236    : NCursesPanel (nlines, ncols, begin_y, begin_x)
237  {
238      if (p)
239	set_user (const_cast<void *>(p_UserData));
240  };
241  // This creates an user panel of the requested size with associated
242  // user data pointed to by p_UserData.
243
244  NCursesUserPanel(const T* p_UserData = STATIC_CAST(T*)(0)) : NCursesPanel()
245  {
246    if (p)
247      set_user(const_cast<void *>(p_UserData));
248  };
249  // This creates an user panel associated with the ::stdscr and user data
250  // pointed to by p_UserData.
251
252  virtual ~NCursesUserPanel() {};
253
254  T* UserData (void) const
255  {
256    return reinterpret_cast<T*>(get_user ());
257  };
258  // Retrieve the user data associated with the panel.
259
260  virtual void setUserData (const T* p_UserData)
261  {
262    if (p)
263      set_user (const_cast<void *>(p_UserData));
264  }
265  // Associate the user panel with the user data pointed to by p_UserData.
266};
267
268#endif /* NCURSES_CURSESP_H_incl */
269