2019-02-11 16:43:43 +03:00
|
|
|
#include <LibGUI/GMenu.h>
|
2019-02-12 02:52:19 +03:00
|
|
|
#include <LibC/gui.h>
|
2019-02-12 12:08:35 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
|
|
|
|
static HashMap<int, GMenu*>& all_menus()
|
|
|
|
{
|
|
|
|
static HashMap<int, GMenu*>* map;
|
|
|
|
if (!map)
|
|
|
|
map = new HashMap<int, GMenu*>();
|
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMenu* GMenu::from_menu_id(int menu_id)
|
|
|
|
{
|
|
|
|
auto it = all_menus().find(menu_id);
|
|
|
|
if (it == all_menus().end())
|
|
|
|
return nullptr;
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2019-02-11 16:43:43 +03:00
|
|
|
|
2019-02-11 17:37:12 +03:00
|
|
|
GMenu::GMenu(const String& name)
|
|
|
|
: m_name(name)
|
2019-02-11 16:43:43 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GMenu::~GMenu()
|
|
|
|
{
|
2019-02-12 02:52:19 +03:00
|
|
|
if (m_menu_id) {
|
2019-02-12 12:08:35 +03:00
|
|
|
all_menus().remove(m_menu_id);
|
2019-02-12 02:52:19 +03:00
|
|
|
gui_menu_destroy(m_menu_id);
|
|
|
|
m_menu_id = 0;
|
|
|
|
}
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:37:12 +03:00
|
|
|
void GMenu::add_item(unsigned identifier, const String& text)
|
|
|
|
{
|
|
|
|
m_items.append({ identifier, text });
|
|
|
|
}
|
2019-02-11 16:43:43 +03:00
|
|
|
|
2019-02-11 17:37:12 +03:00
|
|
|
void GMenu::add_separator()
|
|
|
|
{
|
|
|
|
m_items.append(GMenuItem(GMenuItem::Separator));
|
|
|
|
}
|
2019-02-12 02:52:19 +03:00
|
|
|
|
|
|
|
int GMenu::realize_menu()
|
|
|
|
{
|
|
|
|
m_menu_id = gui_menu_create(m_name.characters());
|
|
|
|
ASSERT(m_menu_id > 0);
|
|
|
|
for (auto& item : m_items) {
|
|
|
|
if (item.type() == GMenuItem::Separator)
|
|
|
|
gui_menu_add_separator(m_menu_id);
|
|
|
|
else if (item.type() == GMenuItem::Text)
|
|
|
|
gui_menu_add_item(m_menu_id, item.identifier(), item.text().characters());
|
|
|
|
}
|
2019-02-12 12:08:35 +03:00
|
|
|
all_menus().set(m_menu_id, this);
|
2019-02-12 02:52:19 +03:00
|
|
|
return m_menu_id;
|
|
|
|
}
|