libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/ncursesp.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       ncursesp.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef _NCURSESP_H
00026 #define _NCURSESP_H
00027 
00028 #include <iosfwd>
00029 
00030 #include "ncursesw.h"
00031 #include <ncursesw/etip.h>
00032 #include <ncursesw/panel.h>
00033 
00034 
00035 class NCursesPanel : public NCursesWindow
00036 {
00037     friend std::ostream & operator<<( std::ostream & Stream, const NCursesPanel & Obj_Cv );
00038     friend std::ostream & operator<<( std::ostream & Stream, const NCursesPanel * Obj_Cv );
00039 
00040     friend class NCDialog;
00041 
00042 protected:
00043 
00044     PANEL *p;
00045     static NCursesPanel *dummy;
00046 
00047 private:
00048     /**
00049      * This structure is used for the panel's user data field to link the
00050      * PANEL* to the C++ object and to provide extra space for a user pointer.
00051     */
00052 
00053     typedef struct
00054     {
00055         /**
00056          * the pointer for the user's data
00057         */
00058         void *              m_user;
00059         /**
00060          * backward pointer to C++ object
00061         */
00062         const NCursesPanel* m_back;
00063         /**
00064          * the panel itself
00065         */
00066         const PANEL*        m_owner;
00067     } UserHook;
00068 
00069     /**
00070      * Initialize the panel object
00071     */
00072     void init();
00073 
00074 protected:
00075     /**
00076      * Set the user pointer of the panel.
00077      */
00078     void set_user( void *user )
00079     {
00080         UserHook* uptr = ( UserHook* )::panel_userptr( p );
00081         assert( uptr && uptr->m_back == this && uptr->m_owner == p );
00082         uptr->m_user = user;
00083     }
00084 
00085     void *get_user() const
00086     {
00087         UserHook* uptr = ( UserHook* )::panel_userptr( p );
00088         assert( uptr && uptr->m_back == this && uptr->m_owner == p );
00089         return uptr->m_user;
00090     }
00091 
00092     static const NCursesPanel * get_Panel_of( const PANEL & pan )
00093     {
00094         UserHook* uptr = ( UserHook* )::panel_userptr( &pan );
00095 
00096         if ( uptr && uptr->m_owner == &pan
00097              && uptr->m_back && uptr->m_back->p == &pan )
00098         {
00099             return uptr->m_back;
00100         }
00101 
00102         return 0;
00103     }
00104 
00105     /**
00106      * If err is equal to the curses error indicator ERR, an error handler
00107      * is called.
00108      */
00109     void OnError( int err ) const THROWS( NCursesPanelException )
00110     {
00111         if ( err == ERR )
00112             THROW( new NCursesPanelException( this, err ) );
00113     }
00114 
00115 public:
00116     /**
00117      * Create a panel with this size starting at the requested position.
00118      */
00119     NCursesPanel( int lines,
00120                   int cols,
00121                   int begin_y = 0,
00122                   int begin_x = 0 )
00123         : NCursesWindow( lines, cols, begin_y, begin_x ), p(0)
00124     {
00125         init();
00126     }
00127 
00128     /**
00129      * This constructor creates the default Panel associated with the
00130      * ::stdscr window
00131      */
00132     NCursesPanel() : NCursesWindow( ::stdscr ), p(0) { init(); }
00133 
00134     virtual ~NCursesPanel();
00135 
00136     // basic manipulation
00137 
00138     /**
00139      * Resize the panel window.
00140      */
00141     virtual int resize( int lines, int columns )
00142     {
00143         ::wresize( w, lines, columns );
00144         return ::replace_panel( p, w );
00145     }
00146 
00147     /**
00148      * Hide the panel. It stays in the stack but becomes invisible.
00149      */
00150     inline void hide()
00151     {
00152         // [ma] hiding a hiden one should not abort.
00153         if ( !hidden() )
00154         {
00155             OnError( ::hide_panel( p ) );
00156         }
00157     }
00158 
00159     /**
00160      * Show the panel, i.e. make it visible.
00161      */
00162     inline void show()
00163     {
00164         OnError( ::show_panel( p ) );
00165     }
00166 
00167     /**
00168      * Make this panel the top panel in the stack.
00169      */
00170     inline void top()
00171     {
00172         OnError( ::top_panel( p ) );
00173     }
00174 
00175     /**
00176      * Make this panel the bottom panel in the stack.
00177      * N.B.: The panel associated with ::stdscr is always on the bottom. So
00178      * actually bottom() makes the panel the first above ::stdscr.
00179      */
00180     inline void bottom()
00181     {
00182         // warning FIX for broken bottom_panel (libpanel)
00183         // [ma] panel stack is messed up if the last panel is
00184         // moved to the bottom.
00185         if ( ::panel_above( 0 ) != p )
00186         {
00187             OnError( ::bottom_panel( p ) );
00188         }
00189     }
00190 
00191     inline int mvwin( int y, int x )
00192     {
00193         OnError( ::move_panel( p, y, x ) );
00194         return OK;
00195     }
00196 
00197     /**
00198      * Return TRUE if the panel is hidden, FALSE otherwise.
00199     */
00200     inline bool hidden() const
00201     {
00202         return ( ::panel_hidden( p ) );
00203     }
00204 
00205     /**
00206      * The functions panel_above() and panel_below() are not reflected in
00207      * the NCursesPanel class. The reason for this is, that we cannot
00208      * assume that a panel retrieved by those operations is one wrapped
00209      * by a C++ class. Although this situation might be handled, we also
00210      * need a reverse mapping from PANEL to NCursesPanel which needs some
00211      * redesign of the low level stuff. At the moment, we define them in the
00212      * interface but they will always produce an error.
00213      */
00214     inline NCursesPanel& above() const
00215     {
00216         OnError( ERR );
00217         return *dummy;
00218     }
00219 
00220     inline NCursesPanel& below() const
00221     {
00222         OnError( ERR );
00223         return *dummy;
00224     }
00225 
00226     inline PANEL * PANEL_above() const
00227     {
00228         return( p ? ::panel_above( p ) : 0 );
00229     }
00230 
00231     inline PANEL * PANEL_below() const
00232     {
00233         return( p ? ::panel_below( p ) : 0 );
00234     }
00235 
00236     int transparent( int y, int x );
00237 
00238     // Those two are rewrites of the corresponding virtual members of NCursesWindow
00239 
00240     /**
00241      * Propagate all panel changes to the virtual screen and update the
00242      * physical screen.
00243      */
00244     virtual int refresh();
00245 
00246     /**
00247      * Propagate all panel changes to the virtual screen.
00248      */
00249     virtual int noutrefresh();
00250 
00251     /**
00252      * Redraw all panels.
00253      */
00254     static void redraw();
00255 
00256     // decorations
00257     /**
00258      * Put a frame around the panel and put the title centered in the top line
00259      * and btitle in the bottom line.
00260      */
00261     virtual void frame( const char* title = NULL,
00262                         const char* btitle = NULL );
00263 
00264     /**
00265      * Same as frame(), but use highlighted attributes.
00266      */
00267     virtual void boldframe( const char* title = NULL,
00268                             const char* btitle = NULL );
00269 
00270     /**
00271      * Put the title centered in the top line and btitle in the bottom line.
00272      */
00273     virtual void label( const char* topLabel,
00274                         const char* bottomLabel );
00275 
00276     /**
00277      * Put the label text centered in the specified row.
00278     */
00279     virtual void centertext( int row, const char* label );
00280 };
00281 
00282 
00283 /**
00284  * @short Associate user data with a panel.
00285  * We use templates to provide a typesafe mechanism to associate
00286  * user data with a panel. A NCursesUserPanel<T> is a panel
00287  * associated with some user data of type T.
00288  */
00289 template<class T> class NCursesUserPanel : public NCursesPanel
00290 {
00291 
00292 public:
00293     /**
00294      * This creates an user panel of the requested size with associated
00295      * user data pointed to by p_UserData.
00296      */
00297     NCursesUserPanel( int lines,
00298                       int cols,
00299                       int begin_y = 0,
00300                       int begin_x = 0,
00301                       const T* p_UserData = ( T* )0 )
00302         : NCursesPanel( lines, cols, begin_y, begin_x )
00303     {
00304         if ( p )
00305             set_user(( void * )p_UserData );
00306     };
00307 
00308     /**
00309      * This creates an user panel associated with the ::stdscr and user data
00310      * pointed to by p_UserData.
00311      */
00312     NCursesUserPanel( const T* p_UserData = ( T* )0 ) : NCursesPanel()
00313     {
00314         if ( p )
00315             set_user(( void * )p_UserData );
00316     };
00317 
00318     virtual ~NCursesUserPanel() {};
00319 
00320     /**
00321      * Retrieve the user data associated with the panel.
00322      */
00323     T* UserData( void ) const
00324     {
00325         return ( T* )get_user();
00326     };
00327 
00328     /**
00329      * Associate the user panel with the user data pointed to by p_UserData.
00330      */
00331     virtual void setUserData( const T* p_UserData )
00332     {
00333         if ( p )
00334             set_user(( void * )p_UserData );
00335     }
00336 
00337     /**
00338      * Retrieve the user data if associated with the PANEL.
00339      */
00340     static T* UserDataOf( const PANEL & pan )
00341     {
00342         const NCursesUserPanel<T> * p = dynamic_cast<const NCursesUserPanel<T>*>( get_Panel_of( pan ) );
00343 
00344         if ( p )
00345         {
00346             return p->UserData();
00347         }
00348 
00349         return ( T* )0;
00350     };
00351 };
00352 
00353 #endif // _NCURSESP_H
 All Classes Functions Variables