2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-12 14:09:48 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Badge.h>
|
2019-02-12 14:09:48 +01:00
|
|
|
#include <AK/Function.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/HashTable.h>
|
2019-06-21 18:45:35 +02:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2019-09-01 15:28:07 -05:00
|
|
|
#include <AK/RefCounted.h>
|
2020-01-10 19:06:00 +03:00
|
|
|
#include <AK/String.h>
|
2019-04-20 21:56:56 +02:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Weakable.h>
|
2020-02-06 12:07:05 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Shortcut.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
class Action;
|
|
|
|
class ActionGroup;
|
|
|
|
class Button;
|
|
|
|
class MenuItem;
|
|
|
|
|
|
|
|
namespace CommonActions {
|
|
|
|
NonnullRefPtr<Action> make_open_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_undo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_redo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_cut_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_copy_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_paste_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_delete_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_quit_action(Function<void(Action&)>);
|
|
|
|
NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
NonnullRefPtr<Action> make_reload_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2019-09-01 15:28:07 -05:00
|
|
|
};
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class Action final : public Core::Object {
|
|
|
|
C_OBJECT(Action)
|
2019-02-12 14:09:48 +01:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class ShortcutScope {
|
2019-04-20 21:56:56 +02:00
|
|
|
None,
|
|
|
|
WidgetLocal,
|
2020-02-02 01:57:57 +01:00
|
|
|
WindowLocal,
|
|
|
|
ApplicationGlobal,
|
2019-04-20 21:56:56 +02:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
static NonnullRefPtr<Action> create(const StringView& text, Function<void(Action&)> callback, Core::Object* parent = nullptr)
|
2019-02-20 02:39:46 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
return adopt(*new Action(text, move(callback), parent));
|
2019-02-20 02:39:46 +01:00
|
|
|
}
|
2020-02-06 11:56:38 +01:00
|
|
|
static NonnullRefPtr<Action> create(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
|
2019-02-20 02:39:46 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
return adopt(*new Action(text, move(icon), move(callback), parent));
|
2019-02-20 02:39:46 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
static NonnullRefPtr<Action> create(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr)
|
2019-03-03 02:52:22 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
return adopt(*new Action(text, shortcut, move(callback), parent));
|
2019-03-03 02:52:22 +01:00
|
|
|
}
|
2020-02-06 11:56:38 +01:00
|
|
|
static NonnullRefPtr<Action> create(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
|
2019-03-02 10:04:49 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
return adopt(*new Action(text, shortcut, move(icon), move(callback), parent));
|
2019-03-02 10:04:49 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~Action() override;
|
2019-04-20 21:56:56 +02:00
|
|
|
|
2019-02-12 14:09:48 +01:00
|
|
|
String text() const { return m_text; }
|
2020-02-02 15:07:41 +01:00
|
|
|
Shortcut shortcut() const { return m_shortcut; }
|
2020-02-06 11:56:38 +01:00
|
|
|
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
|
|
|
|
void set_icon(const Gfx::Bitmap* icon) { m_icon = icon; }
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
const Core::Object* activator() const { return m_activator.ptr(); }
|
|
|
|
Core::Object* activator() { return m_activator.ptr(); }
|
2019-12-09 21:25:48 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Function<void(Action&)> on_activation;
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void activate(Core::Object* activator = nullptr);
|
2019-02-12 14:09:48 +01:00
|
|
|
|
2019-04-12 02:53:27 +02:00
|
|
|
bool is_enabled() const { return m_enabled; }
|
|
|
|
void set_enabled(bool);
|
|
|
|
|
2019-04-26 21:09:56 +02:00
|
|
|
bool is_checkable() const { return m_checkable; }
|
|
|
|
void set_checkable(bool checkable) { m_checkable = checkable; }
|
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
bool is_checked() const
|
|
|
|
{
|
|
|
|
ASSERT(is_checkable());
|
|
|
|
return m_checked;
|
|
|
|
}
|
2019-04-26 21:09:56 +02:00
|
|
|
void set_checked(bool);
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void register_button(Badge<Button>, Button&);
|
|
|
|
void unregister_button(Badge<Button>, Button&);
|
|
|
|
void register_menu_item(Badge<MenuItem>, MenuItem&);
|
|
|
|
void unregister_menu_item(Badge<MenuItem>, MenuItem&);
|
2019-04-12 02:53:27 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
const ActionGroup* group() const { return m_action_group.ptr(); }
|
|
|
|
void set_group(Badge<ActionGroup>, ActionGroup*);
|
2019-07-09 22:10:03 +02:00
|
|
|
|
2019-02-12 14:09:48 +01:00
|
|
|
private:
|
2020-02-02 01:57:57 +01:00
|
|
|
virtual bool is_action() const override { return true; }
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Action(const StringView& text, Function<void(Action&)> = nullptr, Core::Object* = nullptr);
|
|
|
|
Action(const StringView& text, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr);
|
2020-02-06 11:56:38 +01:00
|
|
|
Action(const StringView& text, const Shortcut&, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr);
|
|
|
|
Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr);
|
2019-02-20 02:39:46 +01:00
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_toolbar_button(Callback);
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_menu_item(Callback);
|
2019-04-12 02:53:27 +02:00
|
|
|
|
2019-02-12 14:09:48 +01:00
|
|
|
String m_text;
|
2020-02-06 11:56:38 +01:00
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2020-02-02 15:07:41 +01:00
|
|
|
Shortcut m_shortcut;
|
2019-04-12 02:53:27 +02:00
|
|
|
bool m_enabled { true };
|
2019-04-26 21:09:56 +02:00
|
|
|
bool m_checkable { false };
|
|
|
|
bool m_checked { false };
|
2019-04-20 21:56:56 +02:00
|
|
|
ShortcutScope m_scope { ShortcutScope::None };
|
2019-04-12 02:53:27 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
HashTable<Button*> m_buttons;
|
|
|
|
HashTable<MenuItem*> m_menu_items;
|
|
|
|
WeakPtr<ActionGroup> m_action_group;
|
2020-02-02 12:34:39 +01:00
|
|
|
WeakPtr<Core::Object> m_activator;
|
2019-02-12 14:09:48 +01:00
|
|
|
};
|
2020-02-02 01:57:57 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 01:57:57 +01:00
|
|
|
template<>
|
2020-02-02 15:07:41 +01:00
|
|
|
inline bool Core::is<GUI::Action>(const Core::Object& object)
|
2020-02-02 01:57:57 +01:00
|
|
|
{
|
|
|
|
return object.is_action();
|
|
|
|
}
|