]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesp.h
9ea20aebef6b2bca177594875669523446f4140c
[ncurses.git] / c++ / cursesp.h
1 // * This makes emacs happy -*-Mode: C++;-*-
2 #ifndef _CURSESP_H
3 #define _CURSESP_H
4
5 #include <cursesw.h>
6 #include <etip.h>
7
8 extern "C" {
9 #include <assert.h>
10 #include <panel.h>
11 }
12
13 class NCursesPanel : public NCursesWindow {
14
15 protected:
16   PANEL *p;
17
18 private:
19   // This structure is used for the panel's user data field to link the
20   // PANEL* to the C++ object and to provide extra space for a user pointer.
21   typedef struct {
22     const void*         m_user;      // the pointer for the user's data
23     const NCursesPanel* m_back;      // backward pointer to C++ object
24     const PANEL*        m_owner;     // the panel itself
25   } UserHook;
26
27 protected:
28   void set_user(const void *user) {
29     UserHook* uptr = (UserHook*)::panel_userptr (p);
30     assert (uptr && uptr->m_back==this && uptr->m_owner==p);
31     uptr->m_user = user;
32   }
33   
34   const void *get_user() {
35     UserHook* uptr = (UserHook*)::panel_userptr (p);
36     assert (uptr && uptr->m_back==this && uptr->m_owner==p);
37     return uptr->m_user;
38   }
39   
40   void OnError (int err) const THROWS((NCursesPanelException)) {
41     if (err != OK)
42       THROW(new NCursesPanelException (this, err));
43   }
44
45 public:
46   NCursesPanel(int lines   = 0,
47                int cols    = 0,
48                int begin_y = 0,
49                int begin_x = 0);
50
51   virtual ~NCursesPanel();
52   
53   // basic manipulation
54   inline void hide() {
55     OnError (::hide_panel(p));
56   }
57
58   inline void show() {
59     OnError (::show_panel(p));
60   }
61
62   inline void top() {
63     OnError (::top_panel(p));
64   }
65   
66   inline void bottom() {
67     OnError (::bottom_panel(p));
68   }
69   
70   inline void mvpan(int y, int x) {
71     OnError (::move_panel(p, y, x));
72   }
73
74   inline void mvwin(int y, int x) {
75     OnError (::move_panel(p, y, x));
76   }
77   
78   inline bool hidden() const {
79     return ::panel_hidden (p);
80   }
81
82   static void redraw();         // redraw all panels
83   static void refresh();        // update screen
84   
85   // decorations
86   virtual void frame(const char *title=NULL, const char* btitle=NULL);
87   virtual void boldframe(const char *title=NULL, const char* btitle=NULL);
88   virtual void label(const char *topLabel, const char *bottomLabel);
89   virtual void centertext(int row,const char *label);
90   
91 };
92
93
94 /* We use templates to provide a typesafe mechanism to associate
95  * user data with a panel. A NCursesUserPanel<T> is a panel 
96  * associated with some user data of type T.
97  */
98 template<class T> class NCursesUserPanel : public NCursesPanel
99 {
100 public:
101   NCursesUserPanel (int lines   = 0,
102                     int cols    = 0,
103                     int begin_y = 0,
104                     int begin_x = 0)
105     : NCursesPanel (lines, cols, begin_y, begin_x) {
106   };
107   
108   NCursesUserPanel (const T* p_UserData,
109                     int lines   = 0,
110                     int cols    = 0,
111                     int begin_y = 0,
112                     int begin_x = 0)
113     : NCursesPanel (lines, cols, begin_y, begin_x) {
114       if (p)
115         set_user ((const void *)p_UserData);
116   };
117   
118   virtual ~NCursesUserPanel() {};
119
120   const T* UserData (void) const {
121     return (const T*)get_user ();
122   };
123
124   virtual void setUserData (const T* p_UserData) {
125     if (p)
126       set_user ((const void *)p_UserData);
127   }
128 };
129
130 #endif // _CURSESP_H