2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-04-05 14:33:13 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2020-09-18 09:49:51 +02:00
|
|
|
#include <LibGUI/AbstractButton.h>
|
2020-02-06 12:07:05 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/StylePainter.h>
|
|
|
|
|
#include <LibGfx/TextAlignment.h>
|
2019-01-20 04:49:48 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-04-12 02:53:27 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class Button : public AbstractButton {
|
2020-12-28 13:18:10 +01:00
|
|
|
C_OBJECT(Button);
|
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
public:
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~Button() override;
|
2019-01-20 04:49:48 +01:00
|
|
|
|
2020-02-06 11:56:38 +01: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 23:13:47 +01:00
|
|
|
|
2020-02-06 11:56:38 +01: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 14:15:57 +02:00
|
|
|
|
2020-05-12 20:30:33 +02:00
|
|
|
Function<void(unsigned modifiers)> on_click;
|
2021-05-15 22:55:57 +02:00
|
|
|
Function<void(ContextMenuEvent&)> on_context_menu_request;
|
2019-01-20 04:49:48 +01:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
void set_button_style(Gfx::ButtonStyle style) { m_button_style = style; }
|
|
|
|
|
Gfx::ButtonStyle button_style() const { return m_button_style; }
|
2019-02-20 09:22:38 +01:00
|
|
|
|
2020-05-12 20:30:33 +02:00
|
|
|
virtual void click(unsigned modifiers = 0) override;
|
2020-05-18 21:25:28 -04:00
|
|
|
virtual void context_menu_event(ContextMenuEvent&) override;
|
2019-03-19 01:41:00 +01:00
|
|
|
|
2021-04-17 18:20:41 +02:00
|
|
|
Action* action() { return m_action; }
|
|
|
|
|
Action const* action() const { return m_action; }
|
2020-02-02 15:07:41 +01:00
|
|
|
void set_action(Action&);
|
2019-04-12 02:53:27 +02:00
|
|
|
|
2019-07-09 22:10:03 +02:00
|
|
|
virtual bool is_uncheckable() const override;
|
2019-06-22 10:39:13 +02:00
|
|
|
|
2021-03-25 22:41:40 +01:00
|
|
|
int icon_spacing() const { return m_icon_spacing; }
|
|
|
|
|
void set_icon_spacing(int spacing) { m_icon_spacing = spacing; }
|
|
|
|
|
|
2021-04-05 14:33:13 +02:00
|
|
|
void set_menu(RefPtr<GUI::Menu>);
|
|
|
|
|
|
2019-04-13 03:08:16 +02:00
|
|
|
protected:
|
2020-12-28 13:18:10 +01:00
|
|
|
explicit Button(String text = {});
|
2021-04-05 14:33:13 +02:00
|
|
|
virtual void mousedown_event(MouseEvent&) override;
|
2021-04-29 00:43:30 +02:00
|
|
|
virtual void mousemove_event(MouseEvent&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void paint_event(PaintEvent&) override;
|
2019-01-20 04:49:48 +01:00
|
|
|
|
2019-04-13 03:08:16 +02:00
|
|
|
private:
|
2020-02-06 11:56:38 +01:00
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2021-04-05 14:33:13 +02:00
|
|
|
RefPtr<GUI::Menu> m_menu;
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::ButtonStyle m_button_style { Gfx::ButtonStyle::Normal };
|
|
|
|
|
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
2020-02-02 15:07:41 +01:00
|
|
|
WeakPtr<Action> m_action;
|
2021-03-25 22:41:40 +01:00
|
|
|
int m_icon_spacing { 4 };
|
2019-01-20 04:49:48 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|