libyui-ncurses  2.44.1
 All Classes Functions Variables
ncursesp.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: ncursesp.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef _NCURSESP_H
26 #define _NCURSESP_H
27 
28 #include <iosfwd>
29 
30 #include "ncursesw.h"
31 #include <ncursesw/etip.h>
32 #include <ncursesw/panel.h>
33 
34 
36 {
37  friend std::ostream & operator<<( std::ostream & Stream, const NCursesPanel & Obj_Cv );
38  friend std::ostream & operator<<( std::ostream & Stream, const NCursesPanel * Obj_Cv );
39 
40  friend class NCDialog;
41 
42 protected:
43 
44  PANEL *p;
45  static NCursesPanel *dummy;
46 
47 private:
48  /**
49  * This structure is used for the panel's user data field to link the
50  * PANEL* to the C++ object and to provide extra space for a user pointer.
51  */
52 
53  typedef struct
54  {
55  /**
56  * the pointer for the user's data
57  */
58  void * m_user;
59  /**
60  * backward pointer to C++ object
61  */
62  const NCursesPanel* m_back;
63  /**
64  * the panel itself
65  */
66  const PANEL* m_owner;
67  } UserHook;
68 
69  /**
70  * Initialize the panel object
71  */
72  void init();
73 
74 protected:
75  /**
76  * Set the user pointer of the panel.
77  */
78  void set_user( void *user )
79  {
80  UserHook* uptr = ( UserHook* )::panel_userptr( p );
81  assert( uptr && uptr->m_back == this && uptr->m_owner == p );
82  uptr->m_user = user;
83  }
84 
85  void *get_user() const
86  {
87  UserHook* uptr = ( UserHook* )::panel_userptr( p );
88  assert( uptr && uptr->m_back == this && uptr->m_owner == p );
89  return uptr->m_user;
90  }
91 
92  static const NCursesPanel * get_Panel_of( const PANEL & pan )
93  {
94  UserHook* uptr = ( UserHook* )::panel_userptr( &pan );
95 
96  if ( uptr && uptr->m_owner == &pan
97  && uptr->m_back && uptr->m_back->p == &pan )
98  {
99  return uptr->m_back;
100  }
101 
102  return 0;
103  }
104 
105  /**
106  * If err is equal to the curses error indicator ERR, an error handler
107  * is called.
108  */
109  void OnError( int err ) const THROWS( NCursesPanelException )
110  {
111  if ( err == ERR )
112  THROW( new NCursesPanelException( this, err ) );
113  }
114 
115 public:
116  /**
117  * Create a panel with this size starting at the requested position.
118  */
120  int cols,
121  int begin_y = 0,
122  int begin_x = 0 )
123  : NCursesWindow( lines, cols, begin_y, begin_x ), p(0)
124  {
125  init();
126  }
127 
128  /**
129  * This constructor creates the default Panel associated with the
130  * ::stdscr window
131  */
132  NCursesPanel() : NCursesWindow( ::stdscr ), p(0) { init(); }
133 
134  virtual ~NCursesPanel();
135 
136  // basic manipulation
137 
138  /**
139  * Resize the panel window.
140  */
141  virtual int resize( int lines, int columns )
142  {
143  ::wresize( w, lines, columns );
144  return ::replace_panel( p, w );
145  }
146 
147  /**
148  * Hide the panel. It stays in the stack but becomes invisible.
149  */
150  inline void hide()
151  {
152  // [ma] hiding a hiden one should not abort.
153  if ( !hidden() )
154  {
155  OnError( ::hide_panel( p ) );
156  }
157  }
158 
159  /**
160  * Show the panel, i.e. make it visible.
161  */
162  inline void show()
163  {
164  OnError( ::show_panel( p ) );
165  }
166 
167  /**
168  * Make this panel the top panel in the stack.
169  */
170  inline void top()
171  {
172  OnError( ::top_panel( p ) );
173  }
174 
175  /**
176  * Make this panel the bottom panel in the stack.
177  * N.B.: The panel associated with ::stdscr is always on the bottom. So
178  * actually bottom() makes the panel the first above ::stdscr.
179  */
180  inline void bottom()
181  {
182  // warning FIX for broken bottom_panel (libpanel)
183  // [ma] panel stack is messed up if the last panel is
184  // moved to the bottom.
185  if ( ::panel_above( 0 ) != p )
186  {
187  OnError( ::bottom_panel( p ) );
188  }
189  }
190 
191  inline int mvwin( int y, int x )
192  {
193  OnError( ::move_panel( p, y, x ) );
194  return OK;
195  }
196 
197  /**
198  * Return TRUE if the panel is hidden, FALSE otherwise.
199  */
200  inline bool hidden() const
201  {
202  return ( ::panel_hidden( p ) );
203  }
204 
205  /**
206  * The functions panel_above() and panel_below() are not reflected in
207  * the NCursesPanel class. The reason for this is, that we cannot
208  * assume that a panel retrieved by those operations is one wrapped
209  * by a C++ class. Although this situation might be handled, we also
210  * need a reverse mapping from PANEL to NCursesPanel which needs some
211  * redesign of the low level stuff. At the moment, we define them in the
212  * interface but they will always produce an error.
213  */
214  inline NCursesPanel& above() const
215  {
216  OnError( ERR );
217  return *dummy;
218  }
219 
220  inline NCursesPanel& below() const
221  {
222  OnError( ERR );
223  return *dummy;
224  }
225 
226  inline PANEL * PANEL_above() const
227  {
228  return( p ? ::panel_above( p ) : 0 );
229  }
230 
231  inline PANEL * PANEL_below() const
232  {
233  return( p ? ::panel_below( p ) : 0 );
234  }
235 
236  int transparent( int y, int x );
237 
238  // Those two are rewrites of the corresponding virtual members of NCursesWindow
239 
240  /**
241  * Propagate all panel changes to the virtual screen and update the
242  * physical screen.
243  */
244  virtual int refresh();
245 
246  /**
247  * Propagate all panel changes to the virtual screen.
248  */
249  virtual int noutrefresh();
250 
251  /**
252  * Redraw all panels.
253  */
254  static void redraw();
255 
256  // decorations
257  /**
258  * Put a frame around the panel and put the title centered in the top line
259  * and btitle in the bottom line.
260  */
261  virtual void frame( const char* title = NULL,
262  const char* btitle = NULL );
263 
264  /**
265  * Same as frame(), but use highlighted attributes.
266  */
267  virtual void boldframe( const char* title = NULL,
268  const char* btitle = NULL );
269 
270  /**
271  * Put the title centered in the top line and btitle in the bottom line.
272  */
273  virtual void label( const char* topLabel,
274  const char* bottomLabel );
275 
276  /**
277  * Put the label text centered in the specified row.
278  */
279  virtual void centertext( int row, const char* label );
280 };
281 
282 
283 /**
284  * @short Associate user data with a panel.
285  * We use templates to provide a typesafe mechanism to associate
286  * user data with a panel. A NCursesUserPanel<T> is a panel
287  * associated with some user data of type T.
288  */
289 template<class T> class NCursesUserPanel : public NCursesPanel
290 {
291 
292 public:
293  /**
294  * This creates an user panel of the requested size with associated
295  * user data pointed to by p_UserData.
296  */
298  int cols,
299  int begin_y = 0,
300  int begin_x = 0,
301  const T* p_UserData = ( T* )0 )
302  : NCursesPanel( lines, cols, begin_y, begin_x )
303  {
304  if ( p )
305  set_user(( void * )p_UserData );
306  };
307 
308  /**
309  * This creates an user panel associated with the ::stdscr and user data
310  * pointed to by p_UserData.
311  */
312  NCursesUserPanel( const T* p_UserData = ( T* )0 ) : NCursesPanel()
313  {
314  if ( p )
315  set_user(( void * )p_UserData );
316  };
317 
318  virtual ~NCursesUserPanel() {};
319 
320  /**
321  * Retrieve the user data associated with the panel.
322  */
323  T* UserData( void ) const
324  {
325  return ( T* )get_user();
326  };
327 
328  /**
329  * Associate the user panel with the user data pointed to by p_UserData.
330  */
331  virtual void setUserData( const T* p_UserData )
332  {
333  if ( p )
334  set_user(( void * )p_UserData );
335  }
336 
337  /**
338  * Retrieve the user data if associated with the PANEL.
339  */
340  static T* UserDataOf( const PANEL & pan )
341  {
342  const NCursesUserPanel<T> * p = dynamic_cast<const NCursesUserPanel<T>*>( get_Panel_of( pan ) );
343 
344  if ( p )
345  {
346  return p->UserData();
347  }
348 
349  return ( T* )0;
350  };
351 };
352 
353 #endif // _NCURSESP_H
bool hidden() const
Definition: ncursesp.h:200
C++ class for windows.
Definition: ncursesw.h:904
virtual void label(const char *topLabel, const char *bottomLabel)
Definition: ncursesp.cc:154
NCursesPanel & above() const
Definition: ncursesp.h:214
static int lines()
Definition: ncursesw.h:1042
NCursesUserPanel(const T *p_UserData=(T *) 0)
Definition: ncursesp.h:312
static void redraw()
Definition: ncursesp.cc:94
virtual int refresh()
Definition: ncursesp.cc:112
void show()
Definition: ncursesp.h:162
int mvwin(int y, int x)
Definition: ncursesp.h:191
virtual void centertext(int row, const char *label)
Definition: ncursesp.cc:164
static int cols()
Definition: ncursesw.h:1047
virtual void frame(const char *title=NULL, const char *btitle=NULL)
Definition: ncursesp.cc:134
void OnError(int err) const THROWS(NCursesPanelException)
Definition: ncursesp.h:109
void bottom()
Definition: ncursesp.h:180
Associate user data with a panel. We use templates to provide a typesafe mechanism to associate user ...
Definition: ncursesp.h:289
NCursesUserPanel(int lines, int cols, int begin_y=0, int begin_x=0, const T *p_UserData=(T *) 0)
Definition: ncursesp.h:297
NCursesPanel(int lines, int cols, int begin_y=0, int begin_x=0)
Definition: ncursesp.h:119
void hide()
Definition: ncursesp.h:150
virtual int noutrefresh()
Definition: ncursesp.cc:119
void top()
Definition: ncursesp.h:170
virtual void boldframe(const char *title=NULL, const char *btitle=NULL)
Definition: ncursesp.cc:126
virtual int resize(int lines, int columns)
Definition: ncursesp.h:141
virtual void setUserData(const T *p_UserData)
Definition: ncursesp.h:331
static T * UserDataOf(const PANEL &pan)
Definition: ncursesp.h:340
WINDOW * w
Definition: ncursesw.h:947
void set_user(void *user)
Definition: ncursesp.h:78
T * UserData(void) const
Definition: ncursesp.h:323