#ifndef NCURSES_CURSESM_H_incl
#define NCURSES_CURSESM_H_incl 1
#include <cursesp.h>
extern "C" {
# include <menu.h>
}
class NCURSES_IMPEXP NCursesMenuItem {
friend class NCursesMenu;
protected:
ITEM *item;
inline void OnError (int err) const THROWS(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;
if (p_name && !item)
OnError (E_SYSTEM_ERROR);
}
virtual ~NCursesMenuItem ();
inline const char* name () const {
return ::item_name (item);
}
inline const char* description () const {
return ::item_description (item);
}
inline int (index) (void) const {
return ::item_index (item);
}
inline void options_on (Item_Options options) {
OnError (::item_opts_on (item, options));
}
inline void options_off (Item_Options options) {
OnError (::item_opts_off (item, options));
}
inline Item_Options options () const {
return ::item_opts (item);
}
inline void set_options (Item_Options options) {
OnError (::set_item_opts (item, options));
}
inline void set_value (bool f) {
OnError (::set_item_value (item,f));
}
inline bool value () const {
return ::item_value (item);
}
inline bool visible () const {
return ::item_visible (item);
}
virtual bool action();
};
typedef bool ITEMCALLBACK(NCursesMenuItem&);
class NCURSES_IMPEXP NCursesMenuCallbackItem : public NCursesMenuItem {
private:
ITEMCALLBACK* p_fct;
public:
NCursesMenuCallbackItem(ITEMCALLBACK* fct = NULL,
const char* p_name = NULL,
const char* p_descript = NULL )
: NCursesMenuItem (p_name, p_descript),
p_fct (fct) {
}
virtual ~NCursesMenuCallbackItem();
bool action();
};
class NCURSES_IMPEXP NCursesMenu : public NCursesPanel {
protected:
MENU *menu;
private:
NCursesWindow* sub; bool b_sub_owner; bool b_framed; bool b_autoDelete;
NCursesMenuItem** my_items;
typedef struct {
void* m_user; const NCursesMenu* m_back; const MENU* m_owner;
} UserHook;
static inline NCursesMenu* getHook(const MENU *m) {
UserHook* hook = (UserHook*)::menu_userptr(m);
assert(hook != 0 && hook->m_owner==m);
return (NCursesMenu*)(hook->m_back);
}
static void mnu_init(MENU *);
static void mnu_term(MENU *);
static void itm_init(MENU *);
static void itm_term(MENU *);
ITEM** mapItems(NCursesMenuItem* nitems[]);
protected:
inline void set_user(void *user) {
UserHook* uptr = (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);
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) {
if (err != E_OK)
THROW(new NCursesMenuException (this, err));
}
virtual int driver (int c) ;
NCursesMenu( int lines,
int cols,
int begin_y = 0,
int begin_x = 0)
: NCursesPanel(lines,cols,begin_y,begin_x),
menu ((MENU*)0) {
}
public:
NCursesMenu (NCursesMenuItem* Items[],
bool with_frame=FALSE, bool autoDelete_Items=FALSE) : NCursesPanel() {
InitMenu(Items, with_frame, autoDelete_Items);
}
NCursesMenu (NCursesMenuItem* Items[],
int lines,
int cols,
int begin_y = 0,
int begin_x = 0,
bool with_frame=FALSE, bool autoDelete_Items=FALSE) : NCursesPanel(lines, cols, begin_y, begin_x) {
InitMenu(Items, with_frame, autoDelete_Items);
}
virtual ~NCursesMenu ();
inline NCursesWindow& subWindow() const {
assert(sub!=NULL);
return *sub;
}
void setSubWindow(NCursesWindow& sub);
inline void setItems(NCursesMenuItem* Items[]) {
OnError(::set_menu_items(menu,mapItems(Items)));
}
inline void unpost (void) {
OnError (::unpost_menu (menu));
}
inline void post(bool flag = TRUE) {
flag ? OnError (::post_menu(menu)) : OnError (::unpost_menu (menu));
}
inline void scale (int& mrows, int& mcols) const {
OnError (::scale_menu (menu, &mrows, &mcols));
}
inline void set_format(int mrows, int mcols) {
OnError (::set_menu_format(menu, mrows, mcols));
}
inline void menu_format(int& rows,int& cols) {
::menu_format(menu,&rows,&cols);
}
inline NCursesMenuItem* items() const {
return *my_items;
}
inline int count() const {
return ::item_count(menu);
}
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 *mark) {
OnError (::set_menu_mark (menu, mark));
}
inline static const char* request_name(int c) {
return ::menu_request_name(c);
}
inline char* pattern() const {
return ::menu_pattern(menu);
}
bool set_pattern (const char *pat);
virtual void setDefaultAttributes();
inline chtype back() const {
return ::menu_back(menu);
}
inline chtype fore() const {
return ::menu_fore(menu);
}
inline chtype grey() const {
return ::menu_grey(menu);
}
inline chtype set_background(chtype a) {
return ::set_menu_back(menu,a);
}
inline chtype set_foreground(chtype a) {
return ::set_menu_fore(menu,a);
}
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 int pad() const {
return ::menu_pad(menu);
}
inline void set_pad (int padch) {
OnError (::set_menu_pad (menu, padch));
}
inline void position_cursor () const {
OnError (::pos_menu_cursor (menu));
}
inline void set_current(NCursesMenuItem& I) {
OnError (::set_current_item(menu, I.item));
}
inline int top_row (void) const {
return ::top_row (menu);
}
inline void set_top_row (int row) {
OnError (::set_top_row (menu, row));
}
inline void setSpacing(int spc_description,
int spc_rows,
int spc_columns) {
OnError(::set_menu_spacing(menu,
spc_description,
spc_rows,
spc_columns));
}
inline void Spacing(int& spc_description,
int& spc_rows,
int& spc_columns) const {
OnError(::menu_spacing(menu,
&spc_description,
&spc_rows,
&spc_columns));
}
inline void frame(const char *title=NULL, const char* btitle=NULL) {
if (b_framed)
NCursesPanel::frame(title,btitle);
else
OnError(E_SYSTEM_ERROR);
}
inline void boldframe(const char *title=NULL, const char* btitle=NULL) {
if (b_framed)
NCursesPanel::boldframe(title,btitle);
else
OnError(E_SYSTEM_ERROR);
}
inline void label(const char *topLabel, const char *bottomLabel) {
if (b_framed)
NCursesPanel::label(topLabel,bottomLabel);
else
OnError(E_SYSTEM_ERROR);
}
virtual void On_Menu_Init();
virtual void On_Menu_Termination();
virtual void On_Item_Init(NCursesMenuItem& item);
virtual void On_Item_Termination(NCursesMenuItem& item);
virtual int virtualize(int c);
inline NCursesMenuItem* operator[](int i) const {
if ( (i < 0) || (i >= ::item_count (menu)) )
OnError (E_BAD_ARGUMENT);
return (my_items[i]);
}
virtual NCursesMenuItem* operator()(void);
virtual void On_Request_Denied(int c) const;
virtual void On_Not_Selectable(int c) const;
virtual void On_No_Match(int c) const;
virtual void On_Unknown_Command(int c) const;
};
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)
: NCursesMenuItem (p_name, p_descript) {
if (item)
OnError (::set_item_userptr (item, (void *)p_UserData));
};
virtual ~NCursesUserItem() {};
inline const T* UserData (void) const {
return (const T*)::item_userptr (item);
};
inline virtual void setUserData(const T* p_UserData) {
if (item)
OnError (::set_item_userptr (item, (void *)p_UserData));
}
};
template<class T> class NCURSES_IMPEXP NCursesUserMenu : public NCursesMenu
{
protected:
NCursesUserMenu( int lines,
int cols,
int begin_y = 0,
int begin_x = 0,
const T* p_UserData = (T*)0)
: NCursesMenu(lines,cols,begin_y,begin_x) {
if (menu)
set_user ((void *)p_UserData);
}
public:
NCursesUserMenu (NCursesMenuItem Items[],
const T* p_UserData = (T*)0,
bool with_frame=FALSE,
bool autoDelete_Items=FALSE)
: NCursesMenu (Items, with_frame, autoDelete_Items) {
if (menu)
set_user ((void *)p_UserData);
};
NCursesUserMenu (NCursesMenuItem Items[],
int lines,
int cols,
int begin_y = 0,
int begin_x = 0,
const T* p_UserData = (T*)0,
bool with_frame=FALSE)
: NCursesMenu (Items, lines, cols, begin_y, begin_x, with_frame) {
if (menu)
set_user ((void *)p_UserData);
};
virtual ~NCursesUserMenu() {
};
inline T* UserData (void) const {
return (T*)get_user ();
};
inline virtual void setUserData (const T* p_UserData) {
if (menu)
set_user ((void *)p_UserData);
}
};
#endif // NCURSES_CURSESM_H_incl