- Код: Выделить всё
// (C) Skipy Rich
template<class T, class P=mainmenu_commands>
class mainmenu_static_t : public P
{
typedef void (T::*PEXECFUNC)(service_ptr_t<service_base>&);
struct MENUITEM {
GUID guid;
const char *name;
PEXECFUNC func;
const char *description;
};
pfc::array_t<MENUITEM> m_items;
const GUID &m_parent;
t_uint32 m_sort_priority;
public:
t_uint32 get_command_count() { return (t_uint32)m_items.get_size(); }
GUID get_command(t_uint32 p_index) { return m_items[p_index].guid; }
void get_name(t_uint32 p_index,pfc::string_base & p_out)
{ p_out = m_items[p_index].name; }
GUID get_parent() { return m_parent; }
t_uint32 get_sort_priority() { return m_sort_priority; }
bool get_description(t_uint32 p_index,pfc::string_base & p_out){
const char *ptr = m_items[p_index].description;
if(ptr){
p_out = ptr;
return true;
}
return false;
}
void execute(t_uint32 p_index, service_ptr_t<service_base> p_callback){
PEXECFUNC ffunc = m_items[p_index].func;
T *pT = static_cast<T*>(this);
(pT->*ffunc)(p_callback);
}
bool get_display(t_uint32 p_index,pfc::string_base & p_text,t_uint32 & p_flags) {
T *pT = static_cast<T*>(this);
p_flags = (pT->get_checked(p_index)?mainmenu_commands::flag_checked:0) |
(pT->get_enabled(p_index)?0:mainmenu_commands::flag_disabled);
get_name(p_index,p_text);
return true;
}
// overrides
bool get_enabled(t_uint32 p_index){ return true; }
bool get_checked(t_uint32 p_index){ return false; }
// interface
void menuitem(
const GUID &guid,
const char *name,
PEXECFUNC func,
const char *description=0
)
{
MENUITEM it = { guid, name, func, description };
m_items.append_single(it);
}
// constructor
mainmenu_static_t(const GUID &parent,t_uint32 sort_priority=mainmenu_commands::sort_priority_dontcare)
: m_parent(parent)
, m_sort_priority(sort_priority){}
};
Пример использования:
- Код: Выделить всё
// (C) Skipy Rich
class menu : public mainmenu_static_t<menu>
{
typedef mainmenu_static_t<menu> baseClass;
bool m_bChecked1;
bool m_bChecked2;
public:
menu() : baseClass(mainmenu_groups::view)
, m_bChecked1(false)
, m_bChecked2(false)
{
static const GUID guid1 = /* GUID */;
static const GUID guid2 = /* GUID */;
menuitem(guid1, "Item1", &menu::onoff1, "Toggles checkmark of Item1");
menuitem(guid2, "Item2", &menu::onoff2, "Toggles checkmark of Item2");
}
void onoff1(service_ptr_t<service_base> &p_callback)
{
m_bChecked1 = !m_bChecked1;
}
void onoff2(service_ptr_t<service_base> &p_callback)
{
m_bChecked2 = !m_bChecked2;
}
bool get_checked(t_uint32 p_index){
if(p_index == 0)
return m_bChecked1;
return m_bChecked2;
}
};
static mainmenu_commands_factory_t<menu> dummy;