Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "MenuItem.h"
28#include "ClientConnection.h"
29#include "Menu.h"
30#include "WindowManager.h"
31#include <LibGfx/Bitmap.h>
32
33namespace WindowServer {
34
35MenuItem::MenuItem(Menu& menu, unsigned identifier, const String& text, const String& shortcut_text, bool enabled, bool checkable, bool checked, const Gfx::Bitmap* icon)
36 : m_menu(menu)
37 , m_type(Text)
38 , m_enabled(enabled)
39 , m_checkable(checkable)
40 , m_checked(checked)
41 , m_identifier(identifier)
42 , m_text(text)
43 , m_shortcut_text(shortcut_text)
44 , m_icon(icon)
45{
46}
47
48MenuItem::MenuItem(Menu& menu, Type type)
49 : m_menu(menu)
50 , m_type(type)
51{
52}
53
54MenuItem::~MenuItem()
55{
56}
57
58void MenuItem::set_enabled(bool enabled)
59{
60 if (m_enabled == enabled)
61 return;
62 m_enabled = enabled;
63 m_menu.redraw();
64}
65
66void MenuItem::set_checked(bool checked)
67{
68 if (m_checked == checked)
69 return;
70 m_checked = checked;
71 m_menu.redraw();
72}
73
74Menu* MenuItem::submenu()
75{
76 ASSERT(is_submenu());
77 ASSERT(m_menu.client());
78 return m_menu.client()->find_menu_by_id(m_submenu_id);
79}
80
81Gfx::Rect MenuItem::rect() const
82{
83 if (!m_menu.is_scrollable())
84 return m_rect;
85 return m_rect.translated(0, m_menu.item_height() - (m_menu.scroll_offset() * m_menu.item_height()));
86}
87
88}