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#pragma once
28
29#include <AK/NonnullOwnPtrVector.h>
30#include <AK/String.h>
31#include <AK/WeakPtr.h>
32#include <LibCore/Object.h>
33#include <LibGfx/Font.h>
34#include <LibGfx/Forward.h>
35#include <LibGfx/Rect.h>
36#include <WindowServer/Cursor.h>
37#include <WindowServer/MenuItem.h>
38#include <WindowServer/Window.h>
39
40namespace WindowServer {
41
42class ClientConnection;
43class MenuBar;
44class Event;
45
46class Menu final : public Core::Object {
47 C_OBJECT(Menu)
48public:
49 Menu(ClientConnection*, int menu_id, const String& name);
50 virtual ~Menu() override;
51
52 ClientConnection* client() { return m_client; }
53 const ClientConnection* client() const { return m_client; }
54 int menu_id() const { return m_menu_id; }
55
56 MenuBar* menubar() { return m_menubar; }
57 const MenuBar* menubar() const { return m_menubar; }
58 void set_menubar(MenuBar* menubar) { m_menubar = menubar; }
59
60 bool is_empty() const { return m_items.is_empty(); }
61 int item_count() const { return m_items.size(); }
62 const MenuItem& item(int index) const { return m_items.at(index); }
63 MenuItem& item(int index) { return m_items.at(index); }
64
65 void add_item(NonnullOwnPtr<MenuItem>&& item) { m_items.append(move(item)); }
66
67 String name() const { return m_name; }
68
69 template<typename Callback>
70 void for_each_item(Callback callback) const
71 {
72 for (auto& item : m_items)
73 callback(item);
74 }
75
76 Gfx::Rect text_rect_in_menubar() const { return m_text_rect_in_menubar; }
77 void set_text_rect_in_menubar(const Gfx::Rect& rect) { m_text_rect_in_menubar = rect; }
78
79 Gfx::Rect rect_in_menubar() const { return m_rect_in_menubar; }
80 void set_rect_in_menubar(const Gfx::Rect& rect) { m_rect_in_menubar = rect; }
81
82 Window* menu_window() { return m_menu_window.ptr(); }
83 Window& ensure_menu_window();
84
85 Window* window_menu_of() { return m_window_menu_of; }
86 void set_window_menu_of(Window& window) { m_window_menu_of = window.make_weak_ptr(); }
87 bool is_window_menu_open() { return m_is_window_menu_open; }
88 void set_window_menu_open(bool is_open) { m_is_window_menu_open = is_open; }
89
90 int content_width() const;
91
92 int item_height() const { return 20; }
93 int frame_thickness() const { return 3; }
94 int horizontal_padding() const { return left_padding() + right_padding(); }
95 int left_padding() const { return 14; }
96 int right_padding() const { return 14; }
97
98 void draw();
99 const Gfx::Font& font() const;
100 const Gfx::Font& title_font() const;
101 void set_title_font(const Gfx::Font& font);
102
103 MenuItem* item_with_identifier(unsigned);
104 void redraw();
105
106 MenuItem* hovered_item() const;
107 void clear_hovered_item();
108
109 Function<void(MenuItem&)> on_item_activation;
110
111 void close();
112
113 void popup(const Gfx::Point&, bool is_submenu = false);
114
115 bool is_menu_ancestor_of(const Menu&) const;
116
117 void redraw_if_theme_changed();
118
119 bool is_scrollable() const { return m_scrollable; }
120 int scroll_offset() const { return m_scroll_offset; }
121
122private:
123 virtual void event(Core::Event&) override;
124
125 RefPtr<Gfx::Font> m_title_font { &Gfx::Font::default_font() };
126
127 void handle_mouse_move_event(const MouseEvent&);
128 int visible_item_count() const;
129
130 int item_index_at(const Gfx::Point&);
131 int padding_between_text_and_shortcut() const { return 50; }
132 void did_activate(MenuItem&);
133 void open_hovered_item();
134 void update_for_new_hovered_item();
135 void decend_into_submenu_at_hovered_item();
136
137 ClientConnection* m_client { nullptr };
138 int m_menu_id { 0 };
139 String m_name;
140 Gfx::Rect m_rect_in_menubar;
141 Gfx::Rect m_text_rect_in_menubar;
142 MenuBar* m_menubar { nullptr };
143 NonnullOwnPtrVector<MenuItem> m_items;
144 RefPtr<Window> m_menu_window;
145
146 WeakPtr<Window> m_window_menu_of;
147 bool m_is_window_menu_open = { false };
148 Gfx::Point m_last_position_in_hover;
149 int m_theme_index_at_last_paint { -1 };
150 int m_hovered_item_index { -1 };
151 bool m_in_submenu { false };
152
153 bool m_scrollable { false };
154 int m_scroll_offset { 0 };
155 int m_max_scroll_offset { 0 };
156};
157
158}