cursesp.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // * This makes emacs happy -*-Mode: C++;-*-
  2. /****************************************************************************
  3. * Copyright 2019,2020 Thomas E. Dickey *
  4. * Copyright 1998-2012,2014 Free Software Foundation, Inc. *
  5. * *
  6. * Permission is hereby granted, free of charge, to any person obtaining a *
  7. * copy of this software and associated documentation files (the *
  8. * "Software"), to deal in the Software without restriction, including *
  9. * without limitation the rights to use, copy, modify, merge, publish, *
  10. * distribute, distribute with modifications, sublicense, and/or sell *
  11. * copies of the Software, and to permit persons to whom the Software is *
  12. * furnished to do so, subject to the following conditions: *
  13. * *
  14. * The above copyright notice and this permission notice shall be included *
  15. * in all copies or substantial portions of the Software. *
  16. * *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  20. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  23. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  24. * *
  25. * Except as contained in this notice, the name(s) of the above copyright *
  26. * holders shall not be used in advertising or otherwise to promote the *
  27. * sale, use or other dealings in this Software without prior written *
  28. * authorization. *
  29. ****************************************************************************/
  30. /****************************************************************************
  31. * Author: Juergen Pfeifer, 1997 *
  32. ****************************************************************************/
  33. #ifndef NCURSES_CURSESP_H_incl
  34. #define NCURSES_CURSESP_H_incl 1
  35. // $Id: cursesp.h,v 1.33 2020/02/02 23:34:34 tom Exp $
  36. #include <cursesw.h>
  37. extern "C" {
  38. # include <panel.h>
  39. }
  40. class NCURSES_IMPEXP NCursesPanel
  41. : public NCursesWindow
  42. {
  43. protected:
  44. PANEL *p;
  45. static NCursesPanel *dummy;
  46. private:
  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. typedef struct {
  50. void* m_user; // the pointer for the user's data
  51. const NCursesPanel* m_back; // backward pointer to C++ object
  52. const PANEL* m_owner; // the panel itself
  53. } UserHook;
  54. inline UserHook *UserPointer()
  55. {
  56. UserHook* uptr = reinterpret_cast<UserHook*>(
  57. const_cast<void *>(::panel_userptr (p)));
  58. return uptr;
  59. }
  60. void init(); // Initialize the panel object
  61. protected:
  62. void set_user(void *user)
  63. {
  64. UserHook* uptr = UserPointer();
  65. if (uptr != 0 && uptr->m_back==this && uptr->m_owner==p) {
  66. uptr->m_user = user;
  67. }
  68. }
  69. // Set the user pointer of the panel.
  70. void *get_user()
  71. {
  72. UserHook* uptr = UserPointer();
  73. void *result = 0;
  74. if (uptr != 0 && uptr->m_back==this && uptr->m_owner==p)
  75. result = uptr->m_user;
  76. return result;
  77. }
  78. void OnError (int err) const THROW2(NCursesException const, NCursesPanelException)
  79. {
  80. if (err==ERR)
  81. THROW(new NCursesPanelException (this, err));
  82. }
  83. // If err is equal to the curses error indicator ERR, an error handler
  84. // is called.
  85. // Get a keystroke. Default implementation calls getch()
  86. virtual int getKey(void);
  87. public:
  88. NCursesPanel(int nlines,
  89. int ncols,
  90. int begin_y = 0,
  91. int begin_x = 0)
  92. : NCursesWindow(nlines,ncols,begin_y,begin_x), p(0)
  93. {
  94. init();
  95. }
  96. // Create a panel with this size starting at the requested position.
  97. NCursesPanel()
  98. : NCursesWindow(::stdscr), p(0)
  99. {
  100. init();
  101. }
  102. // This constructor creates the default Panel associated with the
  103. // ::stdscr window
  104. NCursesPanel& operator=(const NCursesPanel& rhs)
  105. {
  106. if (this != &rhs) {
  107. *this = rhs;
  108. NCursesWindow::operator=(rhs);
  109. }
  110. return *this;
  111. }
  112. NCursesPanel(const NCursesPanel& rhs)
  113. : NCursesWindow(rhs),
  114. p(rhs.p)
  115. {
  116. }
  117. virtual ~NCursesPanel() THROWS(NCursesException);
  118. // basic manipulation
  119. inline void hide()
  120. {
  121. OnError (::hide_panel(p));
  122. }
  123. // Hide the panel. It stays in the stack but becomes invisible.
  124. inline void show()
  125. {
  126. OnError (::show_panel(p));
  127. }
  128. // Show the panel, i.e. make it visible.
  129. inline void top()
  130. {
  131. OnError (::top_panel(p));
  132. }
  133. // Make this panel the top panel in the stack.
  134. inline void bottom()
  135. {
  136. OnError (::bottom_panel(p));
  137. }
  138. // Make this panel the bottom panel in the stack.
  139. // N.B.: The panel associated with ::stdscr is always on the bottom. So
  140. // actually bottom() makes the panel the first above ::stdscr.
  141. virtual int mvwin(int y, int x)
  142. {
  143. OnError(::move_panel(p, y, x));
  144. return OK;
  145. }
  146. inline bool hidden() const
  147. {
  148. return (::panel_hidden (p) ? TRUE : FALSE);
  149. }
  150. // Return TRUE if the panel is hidden, FALSE otherwise.
  151. /* The functions panel_above() and panel_below() are not reflected in
  152. the NCursesPanel class. The reason for this is, that we cannot
  153. assume that a panel retrieved by those operations is one wrapped
  154. by a C++ class. Although this situation might be handled, we also
  155. need a reverse mapping from PANEL to NCursesPanel which needs some
  156. redesign of the low level stuff. At the moment, we define them in the
  157. interface but they will always produce an error. */
  158. inline NCursesPanel& above() const
  159. {
  160. OnError(ERR);
  161. return *dummy;
  162. }
  163. inline NCursesPanel& below() const
  164. {
  165. OnError(ERR);
  166. return *dummy;
  167. }
  168. // Those two are rewrites of the corresponding virtual members of
  169. // NCursesWindow
  170. virtual int refresh();
  171. // Propagate all panel changes to the virtual screen and update the
  172. // physical screen.
  173. virtual int noutrefresh();
  174. // Propagate all panel changes to the virtual screen.
  175. static void redraw();
  176. // Redraw all panels.
  177. // decorations
  178. virtual void frame(const char* title=NULL,
  179. const char* btitle=NULL);
  180. // Put a frame around the panel and put the title centered in the top line
  181. // and btitle in the bottom line.
  182. virtual void boldframe(const char* title=NULL,
  183. const char* btitle=NULL);
  184. // Same as frame(), but use highlighted attributes.
  185. virtual void label(const char* topLabel,
  186. const char* bottomLabel);
  187. // Put the title centered in the top line and btitle in the bottom line.
  188. virtual void centertext(int row,const char* label);
  189. // Put the label text centered in the specified row.
  190. };
  191. /* We use templates to provide a typesafe mechanism to associate
  192. * user data with a panel. A NCursesUserPanel<T> is a panel
  193. * associated with some user data of type T.
  194. */
  195. template<class T> class NCursesUserPanel : public NCursesPanel
  196. {
  197. public:
  198. NCursesUserPanel (int nlines,
  199. int ncols,
  200. int begin_y = 0,
  201. int begin_x = 0,
  202. const T* p_UserData = STATIC_CAST(T*)(0))
  203. : NCursesPanel (nlines, ncols, begin_y, begin_x)
  204. {
  205. if (p)
  206. set_user (const_cast<void *>(reinterpret_cast<const void*>
  207. (p_UserData)));
  208. };
  209. // This creates an user panel of the requested size with associated
  210. // user data pointed to by p_UserData.
  211. NCursesUserPanel(const T* p_UserData = STATIC_CAST(T*)(0)) : NCursesPanel()
  212. {
  213. if (p)
  214. set_user(const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
  215. };
  216. // This creates an user panel associated with the ::stdscr and user data
  217. // pointed to by p_UserData.
  218. virtual ~NCursesUserPanel() THROWS(NCursesException) {};
  219. T* UserData (void)
  220. {
  221. return reinterpret_cast<T*>(get_user ());
  222. };
  223. // Retrieve the user data associated with the panel.
  224. virtual void setUserData (const T* p_UserData)
  225. {
  226. if (p)
  227. set_user (const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
  228. }
  229. // Associate the user panel with the user data pointed to by p_UserData.
  230. };
  231. #endif /* NCURSES_CURSESP_H_incl */