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