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>
|
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:
|
2020-02-02 11:07:41 -03:00
|
|
|
virtual ~Button() override;
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
void set_icon(RefPtr<Gfx::Bitmap>&&);
|
|
|
|
|
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
|
|
|
|
|
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;
|
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;
|
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>);
|
|
|
|
|
|
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:
|
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 };
|
2019-01-20 01:49:48 -02:00
|
|
|
};
|
2020-02-02 11:07:41 -03:00
|
|
|
|
|
|
|
|
}
|