2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-04-05 09:33:13 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-01-20 01:49:48 -02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2020-09-18 04:49:51 -03:00
|
|
|
#include <LibGUI/AbstractButton.h>
|
2022-02-26 14:50:04 -03:00
|
|
|
#include <LibGUI/Action.h>
|
2020-02-06 08:07:05 -03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-06 08:04:00 -03:00
|
|
|
#include <LibGfx/StylePainter.h>
|
|
|
|
|
#include <LibGfx/TextAlignment.h>
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
namespace GUI {
|
2019-04-11 21:53:27 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
class Button : public AbstractButton {
|
2020-12-28 09:18:10 -03:00
|
|
|
C_OBJECT(Button);
|
|
|
|
|
|
2019-01-20 01:49:48 -02:00
|
|
|
public:
|
2022-07-27 14:52:37 -03:00
|
|
|
enum MenuPosition {
|
|
|
|
|
TopLeft,
|
|
|
|
|
TopRight,
|
|
|
|
|
BottomLeft,
|
|
|
|
|
BottomRight
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
virtual ~Button() override;
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2022-01-11 00:00:44 -03:00
|
|
|
void set_icon(RefPtr<Gfx::Bitmap>);
|
2022-01-06 13:59:58 -03:00
|
|
|
void set_icon_from_path(String const&);
|
2022-04-01 14:58:27 -03:00
|
|
|
Gfx::Bitmap const* icon() const { return m_icon.ptr(); }
|
2020-02-06 07:56:38 -03:00
|
|
|
Gfx::Bitmap* icon() { return m_icon.ptr(); }
|
2019-02-07 20:13:47 -02:00
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
void set_text_alignment(Gfx::TextAlignment text_alignment) { m_text_alignment = text_alignment; }
|
|
|
|
|
Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
|
2019-04-04 09:15:57 -03:00
|
|
|
|
2020-05-12 15:30:33 -03:00
|
|
|
Function<void(unsigned modifiers)> on_click;
|
2022-07-10 12:20:04 -03:00
|
|
|
Function<void(unsigned modifiers)> on_middle_mouse_click;
|
2021-05-15 17:55:57 -03:00
|
|
|
Function<void(ContextMenuEvent&)> on_context_menu_request;
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
void set_button_style(Gfx::ButtonStyle style) { m_button_style = style; }
|
|
|
|
|
Gfx::ButtonStyle button_style() const { return m_button_style; }
|
2019-02-20 05:22:38 -03:00
|
|
|
|
2020-05-12 15:30:33 -03:00
|
|
|
virtual void click(unsigned modifiers = 0) override;
|
2022-07-10 12:20:04 -03:00
|
|
|
virtual void middle_mouse_click(unsigned modifiers = 0) override;
|
2020-05-18 22:25:28 -03:00
|
|
|
virtual void context_menu_event(ContextMenuEvent&) override;
|
2019-03-18 21:41:00 -03:00
|
|
|
|
2021-04-17 13:20:41 -03:00
|
|
|
Action* action() { return m_action; }
|
|
|
|
|
Action const* action() const { return m_action; }
|
2020-02-02 11:07:41 -03:00
|
|
|
void set_action(Action&);
|
2019-04-11 21:53:27 -03:00
|
|
|
|
2019-07-09 17:10:03 -03:00
|
|
|
virtual bool is_uncheckable() const override;
|
2019-06-22 05:39:13 -03:00
|
|
|
|
2021-03-25 18:41:40 -03:00
|
|
|
int icon_spacing() const { return m_icon_spacing; }
|
|
|
|
|
void set_icon_spacing(int spacing) { m_icon_spacing = spacing; }
|
|
|
|
|
|
2021-04-05 09:33:13 -03:00
|
|
|
void set_menu(RefPtr<GUI::Menu>);
|
|
|
|
|
|
2022-01-24 16:22:02 -03:00
|
|
|
bool is_default() const;
|
|
|
|
|
void set_default(bool);
|
|
|
|
|
|
|
|
|
|
bool another_button_has_focus() const { return m_another_button_has_focus; }
|
|
|
|
|
|
2022-02-06 15:40:07 -03:00
|
|
|
void set_mimic_pressed(bool mimic_pressed);
|
|
|
|
|
bool is_mimic_pressed() const { return m_mimic_pressed; };
|
|
|
|
|
|
2022-02-22 16:25:30 -03:00
|
|
|
virtual Optional<UISize> calculated_min_size() const override;
|
|
|
|
|
|
2019-04-12 22:08:16 -03:00
|
|
|
protected:
|
2020-12-28 09:18:10 -03:00
|
|
|
explicit Button(String text = {});
|
2021-04-05 09:33:13 -03:00
|
|
|
virtual void mousedown_event(MouseEvent&) override;
|
2021-04-28 19:43:30 -03:00
|
|
|
virtual void mousemove_event(MouseEvent&) override;
|
2020-02-02 11:07:41 -03:00
|
|
|
virtual void paint_event(PaintEvent&) override;
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2019-04-12 22:08:16 -03:00
|
|
|
private:
|
2022-04-03 02:50:32 -03:00
|
|
|
virtual void timer_event(Core::TimerEvent&) override;
|
|
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2021-04-05 09:33:13 -03:00
|
|
|
RefPtr<GUI::Menu> m_menu;
|
2020-02-06 07:56:38 -03:00
|
|
|
Gfx::ButtonStyle m_button_style { Gfx::ButtonStyle::Normal };
|
|
|
|
|
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
2020-02-02 11:07:41 -03:00
|
|
|
WeakPtr<Action> m_action;
|
2021-03-25 18:41:40 -03:00
|
|
|
int m_icon_spacing { 4 };
|
2022-01-24 16:22:02 -03:00
|
|
|
bool m_another_button_has_focus { false };
|
2022-02-06 15:40:07 -03:00
|
|
|
bool m_mimic_pressed { false };
|
2019-01-20 01:49:48 -02:00
|
|
|
};
|
2020-02-02 11:07:41 -03:00
|
|
|
|
2022-06-10 17:58:25 -03:00
|
|
|
class DialogButton final : public Button {
|
|
|
|
|
C_OBJECT(DialogButton);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
virtual ~DialogButton() override {};
|
|
|
|
|
explicit DialogButton(String text = {})
|
|
|
|
|
: Button(move(text))
|
|
|
|
|
{
|
|
|
|
|
set_fixed_width(80);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
}
|