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