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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#include <AK/HashMap.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GEventLoop.h>
#include <LibGUI/GMenu.h>
//#define GMENU_DEBUG
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 StringView& name)
: m_name(name)
{
}
GMenu::~GMenu()
{
unrealize_menu();
}
void GMenu::add_action(NonnullRefPtr<GAction> action)
{
m_items.append(make<GMenuItem>(m_menu_id, move(action)));
#ifdef GMENU_DEBUG
dbgprintf("GMenu::add_action(): MenuItem Menu ID: %d\n", m_menu_id);
#endif
}
void GMenu::add_submenu(NonnullOwnPtr<GMenu> submenu)
{
m_items.append(make<GMenuItem>(m_menu_id, move(submenu)));
}
void GMenu::add_separator()
{
m_items.append(make<GMenuItem>(m_menu_id, GMenuItem::Separator));
}
void GMenu::realize_if_needed()
{
if (m_menu_id == -1)
realize_menu();
}
void GMenu::popup(const Point& screen_position)
{
realize_if_needed();
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::PopupMenu;
request.menu.menu_id = m_menu_id;
request.menu.position = screen_position;
GWindowServerConnection::the().post_message_to_server(request);
}
void GMenu::dismiss()
{
if (m_menu_id == -1)
return;
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::DismissMenu;
request.menu.menu_id = m_menu_id;
GWindowServerConnection::the().post_message_to_server(request);
}
int GMenu::realize_menu()
{
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::CreateMenu;
ASSERT(m_name.length() < (ssize_t)sizeof(request.text));
strcpy(request.text, m_name.characters());
request.text_length = m_name.length();
auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidCreateMenu);
m_menu_id = response.menu.menu_id;
#ifdef GMENU_DEBUG
dbgprintf("GMenu::realize_menu(): New menu ID: %d\n", m_menu_id);
#endif
ASSERT(m_menu_id > 0);
for (int i = 0; i < m_items.size(); ++i) {
auto& item = m_items[i];
item.set_menu_id({}, m_menu_id);
item.set_identifier({}, i);
if (item.type() == GMenuItem::Separator) {
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::AddMenuSeparator;
request.menu.menu_id = m_menu_id;
request.menu.submenu_id = -1;
GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidAddMenuSeparator);
continue;
}
if (item.type() == GMenuItem::Submenu) {
auto& submenu = *item.submenu();
submenu.realize_if_needed();
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::AddMenuItem;
request.menu.menu_id = m_menu_id;
request.menu.submenu_id = submenu.menu_id();
request.menu.identifier = i;
// FIXME: It should be possible to disable a submenu.
request.menu.enabled = true;
request.menu.checkable = false;
request.menu.checked = false;
ASSERT(submenu.name().length() < (ssize_t)sizeof(request.text));
strcpy(request.text, submenu.name().characters());
request.text_length = submenu.name().length();
GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidAddMenuItem);
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.submenu_id = -1;
request.menu.identifier = i;
request.menu.enabled = action.is_enabled();
request.menu.checkable = action.is_checkable();
if (action.icon()) {
ASSERT(action.icon()->format() == GraphicsBitmap::Format::RGBA32);
ASSERT(action.icon()->size() == Size(16, 16));
if (action.icon()->shared_buffer_id() == -1) {
auto shared_buffer = SharedBuffer::create_with_size(action.icon()->size_in_bytes());
ASSERT(shared_buffer);
auto shared_icon = GraphicsBitmap::create_with_shared_buffer(GraphicsBitmap::Format::RGBA32, *shared_buffer, action.icon()->size());
memcpy(shared_buffer->data(), action.icon()->bits(0), action.icon()->size_in_bytes());
shared_buffer->seal();
shared_buffer->share_with(GWindowServerConnection::the().server_pid());
action.set_icon(shared_icon);
}
request.menu.icon_buffer_id = action.icon()->shared_buffer_id();
} else {
request.menu.icon_buffer_id = -1;
}
if (action.is_checkable())
request.menu.checked = action.is_checked();
ASSERT(action.text().length() < (ssize_t)sizeof(request.text));
strcpy(request.text, action.text().characters());
request.text_length = action.text().length();
if (action.shortcut().is_valid()) {
auto shortcut_text = action.shortcut().to_string();
ASSERT(shortcut_text.length() < (ssize_t)sizeof(request.menu.shortcut_text));
strcpy(request.menu.shortcut_text, shortcut_text.characters());
request.menu.shortcut_text_length = shortcut_text.length();
} else {
request.menu.shortcut_text_length = 0;
}
GWindowServerConnection::the().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 == -1)
return;
all_menus().remove(m_menu_id);
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::DestroyMenu;
request.menu.menu_id = m_menu_id;
GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidDestroyMenu);
m_menu_id = 0;
}
GAction* GMenu::action_at(int index)
{
if (index >= m_items.size())
return nullptr;
return m_items[index].action();
}
|