// * This makes emacs happy -*-Mode: C++;-*-
/****************************************************************************
* 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 *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
****************************************************************************/
/****************************************************************************
* Author: Juergen Pfeifer, 1997 *
****************************************************************************/
// $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
// -------------------------------------------------------------------------
// This wraps the ITEM type of <menu.h>
// -------------------------------------------------------------------------
class NCURSES_IMPEXP NCursesMenuItem
friend class NCursesMenu;
inline void OnError (int err) const THROW2(NCursesException const, NCursesMenuException) {
THROW(new NCursesMenuException (err));
NCursesMenuItem (const char* p_name = NULL,
const char* p_descript = NULL)
item = p_name ? ::new_item (p_name, p_descript) : STATIC_CAST(ITEM*)(0);
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
NCursesMenuItem& operator=(const NCursesMenuItem& rhs)
NCursesMenuItem(const NCursesMenuItem& rhs)
virtual ~NCursesMenuItem ();
// Release the items memory
inline const char* name () const {
return ::item_name (item);
inline const char* description () const {
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 opts) {
OnError (::item_opts_on (item, opts));
// Switch on the items options
inline void options_off (Item_Options opts) {
OnError (::item_opts_off (item, opts));
// Switch off the item's option
inline Item_Options options () const {
return ::item_opts (item);
// Retrieve the items options
inline void set_options (Item_Options opts) {
OnError (::set_item_opts (item, opts));
inline void set_value (bool f) {
OnError (::set_item_value (item,f));
// Set/Reset the items selection state
inline bool value () const {
return ::item_value (item);
// Retrieve the items selection state
inline bool visible () const {
return ::item_visible (item);
// Retrieve visibility of the item
// Perform an action associated with this item; you may use this in an
// user supplied driver for a menu; you may derive from this class and
// overload action() to supply items with different actions.
// If an action returns true, the menu will be exited. The default action
// Prototype for an items callback function.
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
NCursesMenuCallbackItem(ITEMCALLBACK* fct = NULL,
const char* p_name = NULL,
const char* p_descript = NULL )
: NCursesMenuItem (p_name, p_descript),
NCursesMenuCallbackItem& operator=(const NCursesMenuCallbackItem& rhs)
NCursesMenuCallbackItem(const NCursesMenuCallbackItem& rhs)
virtual ~NCursesMenuCallbackItem();
// 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.
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
NCursesWindow* sub; // the subwindow object
bool b_sub_owner; // is this our own subwindow?
bool b_framed; // has the menu a border?
bool b_autoDelete; // Delete items when deleting menu?
NCursesMenuItem** my_items; // The array of items for this menu
// This structure is used for the menu's user data field to link the
// MENU* to the C++ object and to provide extra space for a user pointer.
void* m_user; // the pointer for the user's data
const NCursesMenu* m_back; // backward pointer to C++ object
// Get the backward pointer to the C++ object from a MENU
static inline NCursesMenu* getHook(const MENU *m) {
UserHook* hook = STATIC_CAST(UserHook*)(::menu_userptr(m));
assert(hook != 0 && hook->m_owner==m);
return const_cast<NCursesMenu*>(hook->m_back);
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[]);
inline void set_user(void *user) {
UserHook* uptr = STATIC_CAST(UserHook*)(::menu_userptr (menu));
assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==menu);
inline void *get_user() {
UserHook* uptr = STATIC_CAST(UserHook*)(::menu_userptr (menu));
assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==menu);
void InitMenu (NCursesMenuItem* menu[],
inline void OnError (int err) const THROW2(NCursesException const, NCursesMenuException) {
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
: NCursesPanel(nlines,ncols,begin_y,begin_x),
menu (STATIC_CAST(MENU*)(0)),
// Make a full window size menu
NCursesMenu (NCursesMenuItem* Items[],
bool with_frame=FALSE, // Reserve space for a frame?
bool autoDelete_Items=FALSE) // Autocleanup of Items?
InitMenu(Items, with_frame, autoDelete_Items);
// Make a menu with a window of this size.
NCursesMenu (NCursesMenuItem* Items[],
bool with_frame=FALSE, // Reserve space for a frame?
bool autoDelete_Items=FALSE) // Autocleanup of Items?
: NCursesPanel(nlines, ncols, begin_y, begin_x),
InitMenu(Items, with_frame, autoDelete_Items);
NCursesMenu& operator=(const NCursesMenu& rhs)
NCursesPanel::operator=(rhs);
NCursesMenu(const NCursesMenu& rhs)
b_sub_owner(rhs.b_sub_owner),
b_autoDelete(rhs.b_autoDelete),
// Retrieve the menus subwindow
inline NCursesWindow& subWindow() const {
// Set the menus subwindow
void setSubWindow(NCursesWindow& sub);
// Set these items for the menu
inline void setItems(NCursesMenuItem* Items[]) {
OnError(::set_menu_items(menu,mapItems(Items)));
// Remove the menu from the screen
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));
// Get the numer of rows and columns for this menu
inline void scale (int& mrows, int& mcols) const {
OnError (::scale_menu (menu, &mrows, &mcols));
// Set the format of this menu
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& ncols) {
::menu_format(menu,&rows,&ncols);
inline NCursesMenuItem* items() const {
// Get the number of items in this menu
inline int count() const {
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))];
inline const char* mark() const {
return ::menu_mark(menu);
inline void set_mark(const char *marker) {
OnError (::set_menu_mark (menu, marker));
// Get the name of the request code c
inline static const char* request_name(int c) {
return ::menu_request_name(c);
// Get the current pattern
inline char* pattern() const {
return ::menu_pattern(menu);
// true if there is a pattern match, false otherwise.
bool set_pattern (const char *pat);
// set the default attributes for the menu
// i.e. set fore, back and grey attribute
virtual void setDefaultAttributes();
// Get the menus background attributes
inline chtype back() const {
return ::menu_back(menu);
// Get the menus foreground attributes
inline chtype fore() const {
return ::menu_fore(menu);
// Get the menus grey attributes (used for unselectable items)
inline chtype grey() const {
return ::menu_grey(menu);
// Set the menus background attributes
inline chtype set_background(chtype a) {
return ::set_menu_back(menu,a);
// Set the menus foreground attributes
inline chtype set_foreground(chtype a) {
return ::set_menu_fore(menu,a);
// Set the menus grey attributes (used for unselectable items)
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));
inline void options_off(Menu_Options opts) {
OnError (::menu_opts_off(menu,opts));
inline Menu_Options options() const {
return ::menu_opts(menu);
inline void set_options (Menu_Options opts) {
OnError (::set_menu_opts (menu,opts));
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));
inline void set_current(NCursesMenuItem& I) {
OnError (::set_current_item(menu, I.item));
// Get the current top row of the menu
inline int top_row (void) const {
// Set the current top row of the menu
inline void set_top_row (int row) {
OnError (::set_top_row (menu, row));
// Set the spacing for the menu
inline void setSpacing(int spc_description,
OnError(::set_menu_spacing(menu,