]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - c++/cursesm.h
ncurses 6.0 - patch 20160123
[ncurses.git] / c++ / cursesm.h
index cc12d33aeb3eea2857a25ae5a09120e5d804d9fd..f8f9665d9d247e8eacb0101758c3d23020b90d9c 100644 (file)
@@ -1,6 +1,6 @@
 // * This makes emacs happy -*-Mode: C++;-*-
 /****************************************************************************
- * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2012,2014 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -31,7 +31,7 @@
  *   Author: Juergen Pfeifer, 1997                                          *
  ****************************************************************************/
 
-// $Id: cursesm.h,v 1.17 2003/10/25 15:04:46 tom Exp $
+// $Id: cursesm.h,v 1.30 2014/08/09 22:06:18 Adam.Jiang Exp $
 
 #ifndef NCURSES_CURSESM_H_incl
 #define NCURSES_CURSESM_H_incl 1
@@ -46,28 +46,45 @@ extern "C" {
 // This wraps the ITEM type of <menu.h>
 // -------------------------------------------------------------------------
 //
-class NCURSES_IMPEXP NCursesMenuItem {
+class NCURSES_IMPEXP NCursesMenuItem
+{
   friend class NCursesMenu;
-  
+
 protected:
   ITEM *item;
 
-  inline void OnError (int err) const THROWS(NCursesMenuException) {
+  inline void OnError (int err) const THROW2(NCursesException const, NCursesMenuException) {
     if (err != E_OK)
       THROW(new NCursesMenuException (err));
   }
 
 public:
   NCursesMenuItem (const char* p_name     = NULL,
-                  const char* p_descript = NULL ) { 
-    item = p_name ? ::new_item (p_name, p_descript) : (ITEM*)0; 
+                  const char* p_descript = NULL)
+    : item(0)
+  {
+    item = p_name ? ::new_item (p_name, p_descript) : STATIC_CAST(ITEM*)(0);
     if (p_name && !item)
       OnError (E_SYSTEM_ERROR);
   }
   // Create an item. If you pass both parameters as NULL, a delimiting
   // item is constructed which can be used to terminate a list of
   // NCursesMenu objects.
-  
+
+  NCursesMenuItem& operator=(const NCursesMenuItem& rhs)
+  {
+    if (this != &rhs) {
+      *this = rhs;
+    }
+    return *this;
+  }
+
+  NCursesMenuItem(const NCursesMenuItem& rhs)
+    : item(0)
+  {
+    (void) rhs;
+  }
+
   virtual ~NCursesMenuItem ();
   // Release the items memory
 
@@ -80,19 +97,19 @@ public:
     return ::item_description (item);
   }
   // Description of the item
-  
+
   inline int (index) (void) const {
     return ::item_index (item);
   }
   // Index of the item in an item array (or -1)
 
-  inline void options_on (Item_Options options) {
-    OnError (::item_opts_on (item, options));
+  inline void options_on (Item_Options opts) {
+    OnError (::item_opts_on (item, opts));
   }
   // Switch on the items options
 
-  inline void options_off (Item_Options options) {
-    OnError (::item_opts_off (item, options));
+  inline void options_off (Item_Options opts) {
+    OnError (::item_opts_off (item, opts));
   }
   // Switch off the item's option
 
@@ -101,8 +118,8 @@ public:
   }
   // Retrieve the items options
 
-  inline void set_options (Item_Options options) {
-    OnError (::set_item_opts (item, options));
+  inline void set_options (Item_Options opts) {
+    OnError (::set_item_opts (item, opts));
   }
   // Set the items options
 
@@ -115,7 +132,7 @@ public:
     return ::item_value (item);
   }
   // Retrieve the items selection state
-  
+
   inline bool visible () const {
     return ::item_visible (item);
   }
@@ -135,7 +152,8 @@ typedef bool ITEMCALLBACK(NCursesMenuItem&);
 // If you don't like to create a child class for individual items to
 // overload action(), you may use this class and provide a callback
 // function pointer for items.
-class NCURSES_IMPEXP NCursesMenuCallbackItem : public NCursesMenuItem {
+class NCURSES_IMPEXP NCursesMenuCallbackItem : public NCursesMenuItem
+{
 private:
   ITEMCALLBACK* p_fct;
 
@@ -146,17 +164,43 @@ public:
     : NCursesMenuItem (p_name, p_descript),
       p_fct (fct) {
   }
-  
+
+  NCursesMenuCallbackItem& operator=(const NCursesMenuCallbackItem& rhs)
+  {
+    if (this != &rhs) {
+      *this = rhs;
+    }
+    return *this;
+  }
+
+  NCursesMenuCallbackItem(const NCursesMenuCallbackItem& rhs)
+    : NCursesMenuItem(rhs),
+      p_fct(0)
+  {
+  }
+
   virtual ~NCursesMenuCallbackItem();
 
   bool action();
 };
+
+  // This are the built-in hook functions in this C++ binding. In C++ we use
+  // virtual member functions (see below On_..._Init and On_..._Termination)
+  // to provide this functionality in an object oriented manner.
+extern "C" {
+  void _nc_xx_mnu_init(MENU *);
+  void _nc_xx_mnu_term(MENU *);
+  void _nc_xx_itm_init(MENU *);
+  void _nc_xx_itm_term(MENU *);
+}
+
 //
 // -------------------------------------------------------------------------
 // This wraps the MENU type of <menu.h>
 // -------------------------------------------------------------------------
 //
-class NCURSES_IMPEXP NCursesMenu : public NCursesPanel {
+class NCURSES_IMPEXP NCursesMenu : public NCursesPanel
+{
 protected:
   MENU *menu;
 
@@ -178,56 +222,59 @@ private:
 
   // Get the backward pointer to the C++ object from a MENU
   static inline NCursesMenu* getHook(const MENU *m) {
-    UserHook* hook = (UserHook*)::menu_userptr(m);
+    UserHook* hook = STATIC_CAST(UserHook*)(::menu_userptr(m));
     assert(hook != 0 && hook->m_owner==m);
-    return (NCursesMenu*)(hook->m_back);
+    return const_cast<NCursesMenu*>(hook->m_back);
   }
 
-  // This are the built-in hook functions in this C++ binding. In C++ we use
-  // virtual member functions (see below On_..._Init and On_..._Termination)
-  // to provide this functionality in an object oriented manner.
-  static void mnu_init(MENU *);
-  static void mnu_term(MENU *);
-  static void itm_init(MENU *);
-  static void itm_term(MENU *);
-  
+  friend void _nc_xx_mnu_init(MENU *);
+  friend void _nc_xx_mnu_term(MENU *);
+  friend void _nc_xx_itm_init(MENU *);
+  friend void _nc_xx_itm_term(MENU *);
+
   // Calculate ITEM* array for the menu
   ITEM** mapItems(NCursesMenuItem* nitems[]);
-  
+
 protected:
-  // internal routines 
+  // internal routines
   inline void set_user(void *user) {
-    UserHook* uptr = (UserHook*)::menu_userptr (menu);
+    UserHook* uptr = STATIC_CAST(UserHook*)(::menu_userptr (menu));
     assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==menu);
     uptr->m_user = user;
   }
 
   inline void *get_user() {
-    UserHook* uptr = (UserHook*)::menu_userptr (menu);
+    UserHook* uptr = STATIC_CAST(UserHook*)(::menu_userptr (menu));
     assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==menu);
     return uptr->m_user;
-  }  
-  
+  }
+
   void InitMenu (NCursesMenuItem* menu[],
                 bool with_frame,
                 bool autoDeleteItems);
 
-  inline void OnError (int err) const THROWS(NCursesMenuException) {
+  inline void OnError (int err) const THROW2(NCursesException const, NCursesMenuException) {
     if (err != E_OK)
       THROW(new NCursesMenuException (this, err));
   }
-  
+
   // this wraps the menu_driver call.
   virtual int driver (int c) ;
-  
+
   // 'Internal' constructor to create a menu without association to
   // an array of items.
-  NCursesMenu( int  lines, 
-              int  cols, 
-              int  begin_y = 0, 
-              int  begin_x = 0) 
-    : NCursesPanel(lines,cols,begin_y,begin_x), 
-      menu ((MENU*)0) {
+  NCursesMenu( int  nlines,
+              int  ncols,
+              int  begin_y = 0,
+              int  begin_x = 0)
+    : NCursesPanel(nlines,ncols,begin_y,begin_x),
+      menu (STATIC_CAST(MENU*)(0)),
+      sub(0),
+      b_sub_owner(0),
+      b_framed(0),
+      b_autoDelete(0),
+      my_items(0)
+  {
   }
 
 public:
@@ -235,22 +282,56 @@ public:
   NCursesMenu (NCursesMenuItem* Items[],
               bool with_frame=FALSE,        // Reserve space for a frame?
               bool autoDelete_Items=FALSE)  // Autocleanup of Items?
-    : NCursesPanel() {
+    : NCursesPanel(),
+      menu(0),
+      sub(0),
+      b_sub_owner(0),
+      b_framed(0),
+      b_autoDelete(0),
+      my_items(0)
+  {
       InitMenu(Items, with_frame, autoDelete_Items);
   }
 
   // Make a menu with a window of this size.
-  NCursesMenu (NCursesMenuItem* Items[], 
-              int  lines, 
-              int  cols, 
-              int  begin_y = 0, 
+  NCursesMenu (NCursesMenuItem* Items[],
+              int  nlines,
+              int  ncols,
+              int  begin_y = 0,
               int  begin_x = 0,
               bool with_frame=FALSE,        // Reserve space for a frame?
               bool autoDelete_Items=FALSE)  // Autocleanup of Items?
-    : NCursesPanel(lines, cols, begin_y, begin_x) {
+    : NCursesPanel(nlines, ncols, begin_y, begin_x),
+      menu(0),
+      sub(0),
+      b_sub_owner(0),
+      b_framed(0),
+      b_autoDelete(0),
+      my_items(0)
+  {
       InitMenu(Items, with_frame, autoDelete_Items);
   }
 
+  NCursesMenu& operator=(const NCursesMenu& rhs)
+  {
+    if (this != &rhs) {
+      *this = rhs;
+      NCursesPanel::operator=(rhs);
+    }
+    return *this;
+  }
+
+  NCursesMenu(const NCursesMenu& rhs)
+    : NCursesPanel(rhs),
+      menu(rhs.menu),
+      sub(rhs.sub),
+      b_sub_owner(rhs.b_sub_owner),
+      b_framed(rhs.b_framed),
+      b_autoDelete(rhs.b_autoDelete),
+      my_items(rhs.my_items)
+  {
+  }
+
   virtual ~NCursesMenu ();
 
   // Retrieve the menus subwindow
@@ -264,17 +345,17 @@ public:
 
   // Set these items for the menu
   inline void setItems(NCursesMenuItem* Items[]) {
-    OnError(::set_menu_items(menu,mapItems(Items)));    
+    OnError(::set_menu_items(menu,mapItems(Items)));
   }
 
   // Remove the menu from the screen
-  inline void unpost (void) { 
-    OnError (::unpost_menu (menu)); 
+  inline void unpost (void) {
+    OnError (::unpost_menu (menu));
   }
-  
+
   // Post the menu to the screen if flag is true, unpost it otherwise
   inline void post(bool flag = TRUE) {
-    flag ? OnError (::post_menu(menu)) : OnError (::unpost_menu (menu)); 
+    flag ? OnError (::post_menu(menu)) : OnError (::unpost_menu (menu));
   }
 
   // Get the numer of rows and columns for this menu
@@ -286,35 +367,35 @@ public:
   inline void set_format(int mrows, int mcols) {
     OnError (::set_menu_format(menu, mrows, mcols));
   }
-  
+
   // Get the format of this menu
-  inline void menu_format(int& rows,int& cols) { 
-    ::menu_format(menu,&rows,&cols); 
+  inline void menu_format(int& rows,int& ncols) {
+    ::menu_format(menu,&rows,&ncols);
   }
-  
+
   // Items of the menu
   inline NCursesMenuItem* items() const {
-    return *my_items; 
+    return *my_items;
   }
 
   // Get the number of items in this menu
   inline int count() const {
-    return ::item_count(menu); 
+    return ::item_count(menu);
   }
 
   // Get the current item (i.e. the one the cursor is located)
   inline NCursesMenuItem* current_item() const {
     return my_items[::item_index(::current_item(menu))];
   }
-  
+
   // Get the marker string
   inline const char* mark() const {
     return ::menu_mark(menu);
   }
 
   // Set the marker string
-  inline void set_mark(const char *mark) {
-    OnError (::set_menu_mark (menu, mark));
+  inline void set_mark(const char *marker) {
+    OnError (::set_menu_mark (menu, marker));
   }
 
   // Get the name of the request code c
@@ -363,7 +444,7 @@ public:
   inline chtype set_grey(chtype a) {
     return ::set_menu_grey(menu,a);
   }
-  
+
   inline void options_on (Menu_Options opts) {
     OnError (::menu_opts_on (menu,opts));
   }
@@ -375,7 +456,7 @@ public:
   inline Menu_Options options() const {
     return ::menu_opts(menu);
   }
-  
+
   inline void set_options (Menu_Options opts) {
     OnError (::set_menu_opts (menu,opts));
   }
@@ -387,7 +468,7 @@ public:
   inline void set_pad (int padch) {
     OnError (::set_menu_pad (menu, padch));
   }
-  
+
   // Position the cursor to the current item
   inline void position_cursor () const {
     OnError (::pos_menu_cursor (menu));
@@ -418,7 +499,7 @@ public:
                               spc_rows,
                               spc_columns));
   }
-  
+
   // Get the spacing info for the menu
   inline void Spacing(int& spc_description,
                      int& spc_rows,
@@ -443,7 +524,7 @@ public:
     else
       OnError(E_SYSTEM_ERROR);
   }
-  
+
   inline void label(const char *topLabel, const char *bottomLabel) {
     if (b_framed)
       NCursesPanel::label(topLabel,bottomLabel);
@@ -468,7 +549,7 @@ public:
 
   // Called before this item is left as current item.
   virtual void On_Item_Termination(NCursesMenuItem& item);
-  
+
   // Provide a default key virtualization. Translate the keyboard
   // code c into a menu request code.
   // The default implementation provides a hopefully straightforward
@@ -495,7 +576,7 @@ public:
 
   // Called if the request is denied
   virtual void On_Request_Denied(int c) const;
-  
+
   // Called if the item is not selectable
   virtual void On_Not_Selectable(int c) const;
 
@@ -519,21 +600,21 @@ template<class T> class NCURSES_IMPEXP NCursesUserItem : public NCursesMenuItem
 public:
   NCursesUserItem (const char* p_name,
                   const char* p_descript = NULL,
-                  const T* p_UserData    = (T*)0)
+                  const T* p_UserData    = STATIC_CAST(T*)(0))
     : NCursesMenuItem (p_name, p_descript) {
       if (item)
-       OnError (::set_item_userptr (item, (void *)p_UserData));
-  };
+       OnError (::set_item_userptr (item, const_cast<void *>(reinterpret_cast<const void*>(p_UserData))));
+  }
 
-  virtual ~NCursesUserItem() {};
+  virtual ~NCursesUserItem() {}
 
   inline const T* UserData (void) const {
-    return (const T*)::item_userptr (item);
+    return reinterpret_cast<const T*>(::item_userptr (item));
   };
 
   inline virtual void setUserData(const T* p_UserData) {
     if (item)
-      OnError (::set_item_userptr (item, (void *)p_UserData));
+      OnError (::set_item_userptr (item, const_cast<void *>(reinterpret_cast<const void *>(p_UserData))));
   }
 };
 //
@@ -544,49 +625,49 @@ public:
 template<class T> class NCURSES_IMPEXP NCursesUserMenu : public NCursesMenu
 {
 protected:
-  NCursesUserMenu( int  lines, 
-                  int  cols, 
-                  int  begin_y = 0, 
+  NCursesUserMenu( int  nlines,
+                  int  ncols,
+                  int  begin_y = 0,
                   int  begin_x = 0,
-                  const T* p_UserData = (T*)0) 
-    : NCursesMenu(lines,cols,begin_y,begin_x) {
+                  const T* p_UserData = STATIC_CAST(T*)(0))
+    : NCursesMenu(nlines,ncols,begin_y,begin_x) {
       if (menu)
-       set_user ((void *)p_UserData);
+       set_user (const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
   }
 
 public:
-  NCursesUserMenu (NCursesMenuItem Items[],
-                  const T* p_UserData = (T*)0,
+  NCursesUserMenu (NCursesMenuItem* Items[],
+                  const T* p_UserData = STATIC_CAST(T*)(0),
                   bool with_frame=FALSE,
                   bool autoDelete_Items=FALSE)
     : NCursesMenu (Items, with_frame, autoDelete_Items) {
       if (menu)
-       set_user ((void *)p_UserData);
+       set_user (const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
   };
-  
-  NCursesUserMenu (NCursesMenuItem Items[],
-                  int lines, 
-                  int cols, 
-                  int begin_y = 0, 
+
+  NCursesUserMenu (NCursesMenuItem* Items[],
+                  int nlines,
+                  int ncols,
+                  int begin_y = 0,
                   int begin_x = 0,
-                  const T* p_UserData = (T*)0,
+                  const T* p_UserData = STATIC_CAST(T*)(0),
                   bool with_frame=FALSE)
-    : NCursesMenu (Items, lines, cols, begin_y, begin_x, with_frame) {
+    : NCursesMenu (Items, nlines, ncols, begin_y, begin_x, with_frame) {
       if (menu)
-       set_user ((void *)p_UserData);
-  };  
-  
+       set_user (const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
+  };
+
   virtual ~NCursesUserMenu() {
   };
-  
-  inline T* UserData (void) const {
-    return (T*)get_user ();
+
+  inline T* UserData (void) {
+    return reinterpret_cast<T*>(get_user ());
   };
 
   inline virtual void setUserData (const T* p_UserData) {
     if (menu)
-      set_user ((void *)p_UserData);
+      set_user (const_cast<void *>(reinterpret_cast<const void*>(p_UserData)));
   }
 };
 
-#endif // NCURSES_CURSESM_H_incl
+#endif /* NCURSES_CURSESM_H_incl */