summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Menubar.cpp
blob: dac6acdaf41bb0f34ce1a3b3d58730df07daaeb8 (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Badge.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuItem.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/WindowServerConnection.h>

namespace GUI {

Menubar::Menubar()
{
}

Menubar::~Menubar()
{
    unrealize_menubar();
}

Menu& Menubar::add_menu(String name)
{
    auto& menu = add<Menu>(move(name));
    m_menus.append(menu);
    return menu;
}

int Menubar::realize_menubar()
{
    return WindowServerConnection::the().send_sync<Messages::WindowServer::CreateMenubar>()->menubar_id();
}

void Menubar::unrealize_menubar()
{
    if (m_menubar_id == -1)
        return;
    WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyMenubar>(m_menubar_id);
    m_menubar_id = -1;
}

void Menubar::notify_added_to_window(Badge<Window>)
{
    VERIFY(m_menubar_id == -1);
    m_menubar_id = realize_menubar();
    VERIFY(m_menubar_id != -1);
    for (auto& menu : m_menus) {
        int menu_id = menu.realize_menu();
        VERIFY(menu_id != -1);
        WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuToMenubar>(m_menubar_id, menu_id);
    }
}

void Menubar::notify_removed_from_window(Badge<Window>)
{
    unrealize_menubar();
}

}