jpayne@69: // * This makes emacs happy -*-Mode: C++;-*- jpayne@69: /**************************************************************************** jpayne@69: * Copyright 2019-2020,2022 Thomas E. Dickey * jpayne@69: * Copyright 1998-2012,2014 Free Software Foundation, Inc. * jpayne@69: * * jpayne@69: * Permission is hereby granted, free of charge, to any person obtaining a * jpayne@69: * copy of this software and associated documentation files (the * jpayne@69: * "Software"), to deal in the Software without restriction, including * jpayne@69: * without limitation the rights to use, copy, modify, merge, publish, * jpayne@69: * distribute, distribute with modifications, sublicense, and/or sell * jpayne@69: * copies of the Software, and to permit persons to whom the Software is * jpayne@69: * furnished to do so, subject to the following conditions: * jpayne@69: * * jpayne@69: * The above copyright notice and this permission notice shall be included * jpayne@69: * in all copies or substantial portions of the Software. * jpayne@69: * * jpayne@69: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * jpayne@69: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * jpayne@69: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * jpayne@69: * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * jpayne@69: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * jpayne@69: * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * jpayne@69: * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * jpayne@69: * * jpayne@69: * Except as contained in this notice, the name(s) of the above copyright * jpayne@69: * holders shall not be used in advertising or otherwise to promote the * jpayne@69: * sale, use or other dealings in this Software without prior written * jpayne@69: * authorization. * jpayne@69: ****************************************************************************/ jpayne@69: jpayne@69: /**************************************************************************** jpayne@69: * Author: Juergen Pfeifer, 1997 * jpayne@69: ****************************************************************************/ jpayne@69: jpayne@69: // $Id: cursesm.h,v 1.35 2022/08/20 20:52:15 tom Exp $ jpayne@69: jpayne@69: #ifndef NCURSES_CURSESM_H_incl jpayne@69: #define NCURSES_CURSESM_H_incl 1 jpayne@69: jpayne@69: #include jpayne@69: jpayne@69: extern "C" { jpayne@69: # include jpayne@69: } jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // This wraps the ITEM type of jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP NCursesMenuItem jpayne@69: { jpayne@69: friend class NCursesMenu; jpayne@69: jpayne@69: protected: jpayne@69: ITEM *item; jpayne@69: jpayne@69: inline void OnError (int err) const THROW2(NCursesException const, NCursesMenuException) { jpayne@69: if (err != E_OK) jpayne@69: THROW(new NCursesMenuException (err)); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: NCursesMenuItem (const char* p_name = NULL, jpayne@69: const char* p_descript = NULL) jpayne@69: : item(0) jpayne@69: { jpayne@69: item = p_name ? ::new_item (p_name, p_descript) : STATIC_CAST(ITEM*)(0); jpayne@69: if (p_name && !item) jpayne@69: OnError (E_SYSTEM_ERROR); jpayne@69: } jpayne@69: // Create an item. If you pass both parameters as NULL, a delimiting jpayne@69: // item is constructed which can be used to terminate a list of jpayne@69: // NCursesMenu objects. jpayne@69: jpayne@69: NCursesMenuItem& operator=(const NCursesMenuItem& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: NCursesMenuItem(const NCursesMenuItem& rhs) jpayne@69: : item(0) jpayne@69: { jpayne@69: (void) rhs; jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesMenuItem () THROWS(NCursesException); jpayne@69: // Release the items memory jpayne@69: jpayne@69: inline const char* name () const { jpayne@69: return ::item_name (item); jpayne@69: } jpayne@69: // Name of the item jpayne@69: jpayne@69: inline const char* description () const { jpayne@69: return ::item_description (item); jpayne@69: } jpayne@69: // Description of the item jpayne@69: jpayne@69: inline int (index) (void) const { jpayne@69: return ::item_index (item); jpayne@69: } jpayne@69: // Index of the item in an item array (or -1) jpayne@69: jpayne@69: inline void options_on (Item_Options opts) { jpayne@69: OnError (::item_opts_on (item, opts)); jpayne@69: } jpayne@69: // Switch on the items options jpayne@69: jpayne@69: inline void options_off (Item_Options opts) { jpayne@69: OnError (::item_opts_off (item, opts)); jpayne@69: } jpayne@69: // Switch off the item's option jpayne@69: jpayne@69: inline Item_Options options () const { jpayne@69: return ::item_opts (item); jpayne@69: } jpayne@69: // Retrieve the items options jpayne@69: jpayne@69: inline void set_options (Item_Options opts) { jpayne@69: OnError (::set_item_opts (item, opts)); jpayne@69: } jpayne@69: // Set the items options jpayne@69: jpayne@69: inline void set_value (bool f) { jpayne@69: OnError (::set_item_value (item,f)); jpayne@69: } jpayne@69: // Set/Reset the items selection state jpayne@69: jpayne@69: inline bool value () const { jpayne@69: return ::item_value (item); jpayne@69: } jpayne@69: // Retrieve the items selection state jpayne@69: jpayne@69: inline bool visible () const { jpayne@69: return ::item_visible (item); jpayne@69: } jpayne@69: // Retrieve visibility of the item jpayne@69: jpayne@69: virtual bool action(); jpayne@69: // Perform an action associated with this item; you may use this in an jpayne@69: // user supplied driver for a menu; you may derive from this class and jpayne@69: // overload action() to supply items with different actions. jpayne@69: // If an action returns true, the menu will be exited. The default action jpayne@69: // is to do nothing. jpayne@69: }; jpayne@69: jpayne@69: // Prototype for an items callback function. jpayne@69: typedef bool ITEMCALLBACK(NCursesMenuItem&); jpayne@69: jpayne@69: // If you don't like to create a child class for individual items to jpayne@69: // overload action(), you may use this class and provide a callback jpayne@69: // function pointer for items. jpayne@69: class NCURSES_CXX_IMPEXP NCursesMenuCallbackItem : public NCursesMenuItem jpayne@69: { jpayne@69: private: jpayne@69: ITEMCALLBACK* p_fct; jpayne@69: jpayne@69: public: jpayne@69: NCursesMenuCallbackItem(ITEMCALLBACK* fct = NULL, jpayne@69: const char* p_name = NULL, jpayne@69: const char* p_descript = NULL ) jpayne@69: : NCursesMenuItem (p_name, p_descript), jpayne@69: p_fct (fct) { jpayne@69: } jpayne@69: jpayne@69: NCursesMenuCallbackItem& operator=(const NCursesMenuCallbackItem& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: NCursesMenuCallbackItem(const NCursesMenuCallbackItem& rhs) jpayne@69: : NCursesMenuItem(rhs), jpayne@69: p_fct(0) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesMenuCallbackItem() THROWS(NCursesException); jpayne@69: jpayne@69: bool action() NCURSES_OVERRIDE; jpayne@69: }; jpayne@69: jpayne@69: // This are the built-in hook functions in this C++ binding. In C++ we use jpayne@69: // virtual member functions (see below On_..._Init and On_..._Termination) jpayne@69: // to provide this functionality in an object oriented manner. jpayne@69: extern "C" { jpayne@69: void _nc_xx_mnu_init(MENU *); jpayne@69: void _nc_xx_mnu_term(MENU *); jpayne@69: void _nc_xx_itm_init(MENU *); jpayne@69: void _nc_xx_itm_term(MENU *); jpayne@69: } jpayne@69: jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // This wraps the MENU type of jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: class NCURSES_CXX_IMPEXP NCursesMenu : public NCursesPanel jpayne@69: { jpayne@69: protected: jpayne@69: MENU *menu; jpayne@69: jpayne@69: private: jpayne@69: NCursesWindow* sub; // the subwindow object jpayne@69: bool b_sub_owner; // is this our own subwindow? jpayne@69: bool b_framed; // has the menu a border? jpayne@69: bool b_autoDelete; // Delete items when deleting menu? jpayne@69: jpayne@69: NCursesMenuItem** my_items; // The array of items for this menu jpayne@69: jpayne@69: // This structure is used for the menu's user data field to link the jpayne@69: // MENU* to the C++ object and to provide extra space for a user pointer. jpayne@69: typedef struct { jpayne@69: void* m_user; // the pointer for the user's data jpayne@69: const NCursesMenu* m_back; // backward pointer to C++ object jpayne@69: const MENU* m_owner; jpayne@69: } UserHook; jpayne@69: jpayne@69: // Get the backward pointer to the C++ object from a MENU jpayne@69: static inline NCursesMenu* getHook(const MENU *m) { jpayne@69: UserHook* hook = STATIC_CAST(UserHook*)(::menu_userptr(m)); jpayne@69: assert(hook != 0 && hook->m_owner==m); jpayne@69: return const_cast(hook->m_back); jpayne@69: } jpayne@69: jpayne@69: friend void _nc_xx_mnu_init(MENU *); jpayne@69: friend void _nc_xx_mnu_term(MENU *); jpayne@69: friend void _nc_xx_itm_init(MENU *); jpayne@69: friend void _nc_xx_itm_term(MENU *); jpayne@69: jpayne@69: // Calculate ITEM* array for the menu jpayne@69: ITEM** mapItems(NCursesMenuItem* nitems[]); jpayne@69: jpayne@69: protected: jpayne@69: // internal routines jpayne@69: inline void set_user(void *user) { jpayne@69: UserHook* uptr = STATIC_CAST(UserHook*)(::menu_userptr (menu)); jpayne@69: assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==menu); jpayne@69: uptr->m_user = user; jpayne@69: } jpayne@69: jpayne@69: inline void *get_user() { jpayne@69: UserHook* uptr = STATIC_CAST(UserHook*)(::menu_userptr (menu)); jpayne@69: assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==menu); jpayne@69: return uptr->m_user; jpayne@69: } jpayne@69: jpayne@69: void InitMenu (NCursesMenuItem* menu[], jpayne@69: bool with_frame, jpayne@69: bool autoDeleteItems); jpayne@69: jpayne@69: inline void OnError (int err) const THROW2(NCursesException const, NCursesMenuException) { jpayne@69: if (err != E_OK) jpayne@69: THROW(new NCursesMenuException (this, err)); jpayne@69: } jpayne@69: jpayne@69: // this wraps the menu_driver call. jpayne@69: virtual int driver (int c) ; jpayne@69: jpayne@69: // 'Internal' constructor to create a menu without association to jpayne@69: // an array of items. jpayne@69: NCursesMenu( int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0) jpayne@69: : NCursesPanel(nlines,ncols,begin_y,begin_x), jpayne@69: menu (STATIC_CAST(MENU*)(0)), jpayne@69: sub(0), jpayne@69: b_sub_owner(0), jpayne@69: b_framed(0), jpayne@69: b_autoDelete(0), jpayne@69: my_items(0) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: // Make a full window size menu jpayne@69: NCursesMenu (NCursesMenuItem* Items[], jpayne@69: bool with_frame=FALSE, // Reserve space for a frame? jpayne@69: bool autoDelete_Items=FALSE) // Autocleanup of Items? jpayne@69: : NCursesPanel(), jpayne@69: menu(0), jpayne@69: sub(0), jpayne@69: b_sub_owner(0), jpayne@69: b_framed(0), jpayne@69: b_autoDelete(0), jpayne@69: my_items(0) jpayne@69: { jpayne@69: InitMenu(Items, with_frame, autoDelete_Items); jpayne@69: } jpayne@69: jpayne@69: // Make a menu with a window of this size. jpayne@69: NCursesMenu (NCursesMenuItem* Items[], jpayne@69: int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0, jpayne@69: bool with_frame=FALSE, // Reserve space for a frame? jpayne@69: bool autoDelete_Items=FALSE) // Autocleanup of Items? jpayne@69: : NCursesPanel(nlines, ncols, begin_y, begin_x), jpayne@69: menu(0), jpayne@69: sub(0), jpayne@69: b_sub_owner(0), jpayne@69: b_framed(0), jpayne@69: b_autoDelete(0), jpayne@69: my_items(0) jpayne@69: { jpayne@69: InitMenu(Items, with_frame, autoDelete_Items); jpayne@69: } jpayne@69: jpayne@69: NCursesMenu& operator=(const NCursesMenu& rhs) jpayne@69: { jpayne@69: if (this != &rhs) { jpayne@69: *this = rhs; jpayne@69: NCursesPanel::operator=(rhs); jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: NCursesMenu(const NCursesMenu& rhs) jpayne@69: : NCursesPanel(rhs), jpayne@69: menu(rhs.menu), jpayne@69: sub(rhs.sub), jpayne@69: b_sub_owner(rhs.b_sub_owner), jpayne@69: b_framed(rhs.b_framed), jpayne@69: b_autoDelete(rhs.b_autoDelete), jpayne@69: my_items(rhs.my_items) jpayne@69: { jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesMenu () THROWS(NCursesException); jpayne@69: jpayne@69: // Retrieve the menus subwindow jpayne@69: inline NCursesWindow& subWindow() const { jpayne@69: assert(sub!=NULL); jpayne@69: return *sub; jpayne@69: } jpayne@69: jpayne@69: // Set the menus subwindow jpayne@69: void setSubWindow(NCursesWindow& sub); jpayne@69: jpayne@69: // Set these items for the menu jpayne@69: inline void setItems(NCursesMenuItem* Items[]) { jpayne@69: OnError(::set_menu_items(menu,mapItems(Items))); jpayne@69: } jpayne@69: jpayne@69: // Remove the menu from the screen jpayne@69: inline void unpost (void) { jpayne@69: OnError (::unpost_menu (menu)); jpayne@69: } jpayne@69: jpayne@69: // Post the menu to the screen if flag is true, unpost it otherwise jpayne@69: inline void post(bool flag = TRUE) { jpayne@69: flag ? OnError (::post_menu(menu)) : OnError (::unpost_menu (menu)); jpayne@69: } jpayne@69: jpayne@69: // Get the number of rows and columns for this menu jpayne@69: inline void scale (int& mrows, int& mcols) const { jpayne@69: OnError (::scale_menu (menu, &mrows, &mcols)); jpayne@69: } jpayne@69: jpayne@69: // Set the format of this menu jpayne@69: inline void set_format(int mrows, int mcols) { jpayne@69: OnError (::set_menu_format(menu, mrows, mcols)); jpayne@69: } jpayne@69: jpayne@69: // Get the format of this menu jpayne@69: inline void menu_format(int& rows,int& ncols) { jpayne@69: ::menu_format(menu,&rows,&ncols); jpayne@69: } jpayne@69: jpayne@69: // Items of the menu jpayne@69: inline NCursesMenuItem* items() const { jpayne@69: return *my_items; jpayne@69: } jpayne@69: jpayne@69: // Get the number of items in this menu jpayne@69: inline int count() const { jpayne@69: return ::item_count(menu); jpayne@69: } jpayne@69: jpayne@69: // Get the current item (i.e. the one the cursor is located) jpayne@69: inline NCursesMenuItem* current_item() const { jpayne@69: return my_items[::item_index(::current_item(menu))]; jpayne@69: } jpayne@69: jpayne@69: // Get the marker string jpayne@69: inline const char* mark() const { jpayne@69: return ::menu_mark(menu); jpayne@69: } jpayne@69: jpayne@69: // Set the marker string jpayne@69: inline void set_mark(const char *marker) { jpayne@69: OnError (::set_menu_mark (menu, marker)); jpayne@69: } jpayne@69: jpayne@69: // Get the name of the request code c jpayne@69: inline static const char* request_name(int c) { jpayne@69: return ::menu_request_name(c); jpayne@69: } jpayne@69: jpayne@69: // Get the current pattern jpayne@69: inline char* pattern() const { jpayne@69: return ::menu_pattern(menu); jpayne@69: } jpayne@69: jpayne@69: // true if there is a pattern match, false otherwise. jpayne@69: bool set_pattern (const char *pat); jpayne@69: jpayne@69: // set the default attributes for the menu jpayne@69: // i.e. set fore, back and grey attribute jpayne@69: virtual void setDefaultAttributes(); jpayne@69: jpayne@69: // Get the menus background attributes jpayne@69: inline chtype back() const { jpayne@69: return ::menu_back(menu); jpayne@69: } jpayne@69: jpayne@69: // Get the menus foreground attributes jpayne@69: inline chtype fore() const { jpayne@69: return ::menu_fore(menu); jpayne@69: } jpayne@69: jpayne@69: // Get the menus grey attributes (used for unselectable items) jpayne@69: inline chtype grey() const { jpayne@69: return ::menu_grey(menu); jpayne@69: } jpayne@69: jpayne@69: // Set the menus background attributes jpayne@69: inline chtype set_background(chtype a) { jpayne@69: return ::set_menu_back(menu,a); jpayne@69: } jpayne@69: jpayne@69: // Set the menus foreground attributes jpayne@69: inline chtype set_foreground(chtype a) { jpayne@69: return ::set_menu_fore(menu,a); jpayne@69: } jpayne@69: jpayne@69: // Set the menus grey attributes (used for unselectable items) jpayne@69: inline chtype set_grey(chtype a) { jpayne@69: return ::set_menu_grey(menu,a); jpayne@69: } jpayne@69: jpayne@69: inline void options_on (Menu_Options opts) { jpayne@69: OnError (::menu_opts_on (menu,opts)); jpayne@69: } jpayne@69: jpayne@69: inline void options_off(Menu_Options opts) { jpayne@69: OnError (::menu_opts_off(menu,opts)); jpayne@69: } jpayne@69: jpayne@69: inline Menu_Options options() const { jpayne@69: return ::menu_opts(menu); jpayne@69: } jpayne@69: jpayne@69: inline void set_options (Menu_Options opts) { jpayne@69: OnError (::set_menu_opts (menu,opts)); jpayne@69: } jpayne@69: jpayne@69: inline int pad() const { jpayne@69: return ::menu_pad(menu); jpayne@69: } jpayne@69: jpayne@69: inline void set_pad (int padch) { jpayne@69: OnError (::set_menu_pad (menu, padch)); jpayne@69: } jpayne@69: jpayne@69: // Position the cursor to the current item jpayne@69: inline void position_cursor () const { jpayne@69: OnError (::pos_menu_cursor (menu)); jpayne@69: } jpayne@69: jpayne@69: // Set the current item jpayne@69: inline void set_current(NCursesMenuItem& I) { jpayne@69: OnError (::set_current_item(menu, I.item)); jpayne@69: } jpayne@69: jpayne@69: // Get the current top row of the menu jpayne@69: inline int top_row (void) const { jpayne@69: return ::top_row (menu); jpayne@69: } jpayne@69: jpayne@69: // Set the current top row of the menu jpayne@69: inline void set_top_row (int row) { jpayne@69: OnError (::set_top_row (menu, row)); jpayne@69: } jpayne@69: jpayne@69: // spacing control jpayne@69: // Set the spacing for the menu jpayne@69: inline void setSpacing(int spc_description, jpayne@69: int spc_rows, jpayne@69: int spc_columns) { jpayne@69: OnError(::set_menu_spacing(menu, jpayne@69: spc_description, jpayne@69: spc_rows, jpayne@69: spc_columns)); jpayne@69: } jpayne@69: jpayne@69: // Get the spacing info for the menu jpayne@69: inline void Spacing(int& spc_description, jpayne@69: int& spc_rows, jpayne@69: int& spc_columns) const { jpayne@69: OnError(::menu_spacing(menu, jpayne@69: &spc_description, jpayne@69: &spc_rows, jpayne@69: &spc_columns)); jpayne@69: } jpayne@69: jpayne@69: // Decorations jpayne@69: inline void frame(const char *title=NULL, const char* btitle=NULL) NCURSES_OVERRIDE { jpayne@69: if (b_framed) jpayne@69: NCursesPanel::frame(title,btitle); jpayne@69: else jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: } jpayne@69: jpayne@69: inline void boldframe(const char *title=NULL, const char* btitle=NULL) NCURSES_OVERRIDE { jpayne@69: if (b_framed) jpayne@69: NCursesPanel::boldframe(title,btitle); jpayne@69: else jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: } jpayne@69: jpayne@69: inline void label(const char *topLabel, const char *bottomLabel) NCURSES_OVERRIDE { jpayne@69: if (b_framed) jpayne@69: NCursesPanel::label(topLabel,bottomLabel); jpayne@69: else jpayne@69: OnError(E_SYSTEM_ERROR); jpayne@69: } jpayne@69: jpayne@69: // ----- jpayne@69: // Hooks jpayne@69: // ----- jpayne@69: jpayne@69: // Called after the menu gets repositioned in its window. jpayne@69: // This is especially true if the menu is posted. jpayne@69: virtual void On_Menu_Init(); jpayne@69: jpayne@69: // Called before the menu gets repositioned in its window. jpayne@69: // This is especially true if the menu is unposted. jpayne@69: virtual void On_Menu_Termination(); jpayne@69: jpayne@69: // Called after the item became the current item jpayne@69: virtual void On_Item_Init(NCursesMenuItem& item); jpayne@69: jpayne@69: // Called before this item is left as current item. jpayne@69: virtual void On_Item_Termination(NCursesMenuItem& item); jpayne@69: jpayne@69: // Provide a default key virtualization. Translate the keyboard jpayne@69: // code c into a menu request code. jpayne@69: // The default implementation provides a hopefully straightforward jpayne@69: // mapping for the most common keystrokes and menu requests. jpayne@69: virtual int virtualize(int c); jpayne@69: jpayne@69: jpayne@69: // Operators jpayne@69: inline NCursesMenuItem* operator[](int i) const { jpayne@69: if ( (i < 0) || (i >= ::item_count (menu)) ) jpayne@69: OnError (E_BAD_ARGUMENT); jpayne@69: return (my_items[i]); jpayne@69: } jpayne@69: jpayne@69: // Perform the menu's operation jpayne@69: // Return the item where you left the selection mark for a single jpayne@69: // selection menu, or NULL for a multivalued menu. jpayne@69: virtual NCursesMenuItem* operator()(void); jpayne@69: jpayne@69: // -------------------- jpayne@69: // Exception handlers jpayne@69: // Called by operator() jpayne@69: // -------------------- jpayne@69: jpayne@69: // Called if the request is denied jpayne@69: virtual void On_Request_Denied(int c) const; jpayne@69: jpayne@69: // Called if the item is not selectable jpayne@69: virtual void On_Not_Selectable(int c) const; jpayne@69: jpayne@69: // Called if pattern doesn't match jpayne@69: virtual void On_No_Match(int c) const; jpayne@69: jpayne@69: // Called if the command is unknown jpayne@69: virtual void On_Unknown_Command(int c) const; jpayne@69: jpayne@69: }; jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // This is the typical C++ typesafe way to allow to attach jpayne@69: // user data to an item of a menu. Its assumed that the user jpayne@69: // data belongs to some class T. Use T as template argument jpayne@69: // to create a UserItem. jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: template class NCURSES_CXX_IMPEXP NCursesUserItem : public NCursesMenuItem jpayne@69: { jpayne@69: public: jpayne@69: NCursesUserItem (const char* p_name, jpayne@69: const char* p_descript = NULL, jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0)) jpayne@69: : NCursesMenuItem (p_name, p_descript) { jpayne@69: if (item) jpayne@69: OnError (::set_item_userptr (item, const_cast(reinterpret_cast(p_UserData)))); jpayne@69: } jpayne@69: jpayne@69: virtual ~NCursesUserItem() THROWS(NCursesException) {} jpayne@69: jpayne@69: inline const T* UserData (void) const { jpayne@69: return reinterpret_cast(::item_userptr (item)); jpayne@69: }; jpayne@69: jpayne@69: inline virtual void setUserData(const T* p_UserData) { jpayne@69: if (item) jpayne@69: OnError (::set_item_userptr (item, const_cast(reinterpret_cast(p_UserData)))); jpayne@69: } jpayne@69: }; jpayne@69: // jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // The same mechanism is used to attach user data to a menu jpayne@69: // ------------------------------------------------------------------------- jpayne@69: // jpayne@69: template class NCURSES_CXX_IMPEXP NCursesUserMenu : public NCursesMenu jpayne@69: { jpayne@69: protected: jpayne@69: NCursesUserMenu( int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0, jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0)) jpayne@69: : NCursesMenu(nlines,ncols,begin_y,begin_x) { jpayne@69: if (menu) jpayne@69: set_user (const_cast(reinterpret_cast(p_UserData))); jpayne@69: } jpayne@69: jpayne@69: public: jpayne@69: NCursesUserMenu (NCursesMenuItem* Items[], jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0), jpayne@69: bool with_frame=FALSE, jpayne@69: bool autoDelete_Items=FALSE) jpayne@69: : NCursesMenu (Items, with_frame, autoDelete_Items) { jpayne@69: if (menu) jpayne@69: set_user (const_cast(reinterpret_cast(p_UserData))); jpayne@69: }; jpayne@69: jpayne@69: NCursesUserMenu (NCursesMenuItem* Items[], jpayne@69: int nlines, jpayne@69: int ncols, jpayne@69: int begin_y = 0, jpayne@69: int begin_x = 0, jpayne@69: const T* p_UserData = STATIC_CAST(T*)(0), jpayne@69: bool with_frame=FALSE) jpayne@69: : NCursesMenu (Items, nlines, ncols, begin_y, begin_x, with_frame) { jpayne@69: if (menu) jpayne@69: set_user (const_cast(reinterpret_cast(p_UserData))); jpayne@69: }; jpayne@69: jpayne@69: virtual ~NCursesUserMenu() THROWS(NCursesException) { jpayne@69: }; jpayne@69: jpayne@69: inline T* UserData (void) { jpayne@69: return reinterpret_cast(get_user ()); jpayne@69: }; jpayne@69: jpayne@69: inline virtual void setUserData (const T* p_UserData) { jpayne@69: if (menu) jpayne@69: set_user (const_cast(reinterpret_cast(p_UserData))); jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: #endif /* NCURSES_CURSESM_H_incl */