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
|
|
|
*/
|
|
|
|
|
|
2019-02-11 14:43:43 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-07-24 09:12:23 +02:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2020-07-10 13:29:21 -06:00
|
|
|
#include <AK/WeakPtr.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/Object.h>
|
2020-07-10 13:29:21 -06:00
|
|
|
#include <LibGUI/Action.h>
|
2021-07-29 10:14:12 +00:00
|
|
|
#include <LibGUI/Event.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGUI/Forward.h>
|
2020-02-15 00:10:34 +01:00
|
|
|
#include <LibGfx/Forward.h>
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
class Menu final : public Core::Object {
|
|
|
|
|
C_OBJECT(Menu)
|
2019-02-11 14:43:43 +01:00
|
|
|
public:
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~Menu() override;
|
2019-02-11 14:43:43 +01:00
|
|
|
|
2020-07-10 13:29:21 -06:00
|
|
|
void realize_menu_if_needed();
|
2020-02-17 20:04:05 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
static Menu* from_menu_id(int);
|
2020-02-17 20:04:05 +01:00
|
|
|
int menu_id() const { return m_menu_id; }
|
2019-02-12 10:08:35 +01:00
|
|
|
|
2019-08-28 21:11:53 +02:00
|
|
|
const String& name() const { return m_name; }
|
2020-07-27 01:57:09 +02:00
|
|
|
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
|
|
|
|
|
void set_icon(const Gfx::Bitmap*);
|
2019-08-28 21:11:53 +02:00
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
Action* action_at(size_t);
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2021-11-24 13:29:17 +01:00
|
|
|
ErrorOr<void> try_add_action(NonnullRefPtr<Action>);
|
|
|
|
|
ErrorOr<void> try_add_separator();
|
2021-11-24 22:23:26 +01:00
|
|
|
ErrorOr<NonnullRefPtr<Menu>> try_add_submenu(String name);
|
2021-11-24 13:29:17 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void add_action(NonnullRefPtr<Action>);
|
2019-02-11 15:37:12 +01:00
|
|
|
void add_separator();
|
2021-11-24 22:23:26 +01:00
|
|
|
Menu& add_submenu(String name);
|
2019-02-11 15:37:12 +01:00
|
|
|
|
2020-07-10 13:29:21 -06:00
|
|
|
void popup(const Gfx::IntPoint& screen_position, const RefPtr<Action>& default_action = nullptr);
|
2019-04-14 01:53:19 +02:00
|
|
|
void dismiss();
|
2019-04-12 17:10:30 +02:00
|
|
|
|
2021-04-05 14:32:34 +02:00
|
|
|
void visibility_did_change(Badge<WindowServerConnection>, bool visible);
|
|
|
|
|
|
|
|
|
|
Function<void(bool)> on_visibility_change;
|
|
|
|
|
|
|
|
|
|
bool is_visible() const { return m_visible; }
|
|
|
|
|
|
2019-02-11 14:43:43 +01:00
|
|
|
private:
|
2021-04-13 16:18:20 +02:00
|
|
|
friend class Menubar;
|
2019-04-18 12:25:00 +02:00
|
|
|
|
2021-04-17 00:47:36 +02:00
|
|
|
explicit Menu(String name = "");
|
|
|
|
|
|
2020-07-10 13:29:21 -06:00
|
|
|
int realize_menu(RefPtr<Action> default_action = nullptr);
|
2019-02-13 18:48:22 +01:00
|
|
|
void unrealize_menu();
|
2020-07-10 13:29:21 -06:00
|
|
|
void realize_if_needed(const RefPtr<Action>& default_action);
|
2019-02-12 00:52:19 +01:00
|
|
|
|
2021-07-29 10:14:12 +00:00
|
|
|
void realize_menu_item(MenuItem&, int item_id);
|
|
|
|
|
|
2019-05-23 19:31:00 -07:00
|
|
|
int m_menu_id { -1 };
|
2019-02-11 15:37:12 +01:00
|
|
|
String m_name;
|
2020-07-27 01:57:09 +02:00
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2020-02-02 15:07:41 +01:00
|
|
|
NonnullOwnPtrVector<MenuItem> m_items;
|
2021-07-29 10:14:12 +00:00
|
|
|
WeakPtr<Action> m_current_default_action;
|
2021-04-05 14:32:34 +02:00
|
|
|
bool m_visible { false };
|
2019-02-11 14:43:43 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|