2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-15 01:56:30 +01:00
|
|
|
#include <AK/Badge.h>
|
2021-05-17 13:31:14 +02:00
|
|
|
#include <AK/IDAllocator.h>
|
2020-02-15 01:56:30 +01:00
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
|
#include <LibGUI/MenuItem.h>
|
2021-04-13 16:18:20 +02:00
|
|
|
#include <LibGUI/Menubar.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/WindowServerConnection.h>
|
2019-02-11 14:43:43 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2021-05-17 13:31:14 +02:00
|
|
|
static IDAllocator s_menubar_id_allocator;
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
Menubar::Menubar()
|
2019-02-11 14:43:43 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
Menubar::~Menubar()
|
2019-02-11 14:43:43 +01:00
|
|
|
{
|
2019-02-13 17:54:30 +01:00
|
|
|
unrealize_menubar();
|
2019-02-11 14:43:43 +01:00
|
|
|
}
|
2019-02-11 15:37:12 +01:00
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
Menu& Menubar::add_menu(String name)
|
2020-04-04 12:18:40 +02:00
|
|
|
{
|
2020-04-21 16:01:00 +02:00
|
|
|
auto& menu = add<Menu>(move(name));
|
|
|
|
|
m_menus.append(menu);
|
|
|
|
|
return menu;
|
2019-02-11 15:37:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
int Menubar::realize_menubar()
|
2019-02-13 17:54:30 +01:00
|
|
|
{
|
2021-05-17 13:31:14 +02:00
|
|
|
auto menubar_id = s_menubar_id_allocator.allocate();
|
|
|
|
|
WindowServerConnection::the().async_create_menubar(menubar_id);
|
|
|
|
|
return menubar_id;
|
2019-02-13 17:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Menubar::unrealize_menubar()
|
2019-02-13 17:54:30 +01:00
|
|
|
{
|
2019-05-26 01:07:30 +02:00
|
|
|
if (m_menubar_id == -1)
|
2019-02-13 17:54:30 +01:00
|
|
|
return;
|
2021-05-10 11:52:30 +02:00
|
|
|
WindowServerConnection::the().async_destroy_menubar(m_menubar_id);
|
2019-05-26 01:07:30 +02:00
|
|
|
m_menubar_id = -1;
|
2019-02-13 17:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Menubar::notify_added_to_window(Badge<Window>)
|
2019-02-11 15:37:12 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_menubar_id == -1);
|
2019-02-13 17:54:30 +01:00
|
|
|
m_menubar_id = realize_menubar();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_menubar_id != -1);
|
2019-02-12 00:52:19 +01:00
|
|
|
for (auto& menu : m_menus) {
|
2019-07-24 09:12:23 +02:00
|
|
|
int menu_id = menu.realize_menu();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(menu_id != -1);
|
2021-05-03 21:27:22 +02:00
|
|
|
WindowServerConnection::the().async_add_menu_to_menubar(m_menubar_id, menu_id);
|
2019-02-12 00:52:19 +01:00
|
|
|
}
|
2019-02-11 15:37:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Menubar::notify_removed_from_window(Badge<Window>)
|
2019-02-11 15:37:12 +01:00
|
|
|
{
|
2019-02-13 17:54:30 +01:00
|
|
|
unrealize_menubar();
|
2019-02-11 15:37:12 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|