summaryrefslogtreecommitdiff
path: root/LibGUI/GMenu.cpp
blob: 0a859d5978f32aaa9296ce22c0a5c96c86216654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <LibGUI/GAction.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GEventLoop.h>
#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;
}

GMenu::GMenu(const String& name)
    : m_name(name)
{
}

GMenu::~GMenu()
{
    unrealize_menu();
}

void GMenu::add_action(RetainPtr<GAction>&& action)
{
    m_items.append(make<GMenuItem>(move(action)));
}

void GMenu::add_separator()
{
    m_items.append(make<GMenuItem>(GMenuItem::Separator));
}

int GMenu::realize_menu()
{
    WSAPI_ClientMessage request;
    request.type = WSAPI_ClientMessage::Type::CreateMenu;
    ASSERT(m_name.length() < sizeof(request.text));
    strcpy(request.text, m_name.characters());
    request.text_length = m_name.length();
    auto response = GEventLoop::main().sync_request(request, WSAPI_ServerMessage::Type::DidCreateMenu);
    m_menu_id = response.menu.menu_id;

    ASSERT(m_menu_id > 0);
    for (size_t i = 0; i < m_items.size(); ++i) {
        auto& item = *m_items[i];
        if (item.type() == GMenuItem::Separator) {
            WSAPI_ClientMessage request;
            request.type = WSAPI_ClientMessage::Type::AddMenuSeparator;
            request.menu.menu_id = m_menu_id;
            GEventLoop::main().sync_request(request, WSAPI_ServerMessage::Type::DidAddMenuSeparator);
            continue;
        }
        if (item.type() == GMenuItem::Action) {
            auto& action = *item.action();
            WSAPI_ClientMessage request;
            request.type = WSAPI_ClientMessage::Type::AddMenuItem;
            request.menu.menu_id = m_menu_id;
            request.menu.identifier = i;
            ASSERT(action.text().length() < sizeof(request.text));
            strcpy(request.text, action.text().characters());
            request.text_length = action.text().length();
            GEventLoop::main().sync_request(request, WSAPI_ServerMessage::Type::DidAddMenuItem);
        }
    }
    all_menus().set(m_menu_id, this);
    return m_menu_id;
}

void GMenu::unrealize_menu()
{
    if (!m_menu_id)
        return;
    all_menus().remove(m_menu_id);
    WSAPI_ClientMessage request;
    request.type = WSAPI_ClientMessage::Type::DestroyMenu;
    request.menu.menu_id = m_menu_id;
    GEventLoop::main().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyMenu);
    m_menu_id = 0;
}

GAction* GMenu::action_at(size_t index)
{
    if (index >= m_items.size())
        return nullptr;
    return m_items[index]->action();
}