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