Serenity Operating System
at master 84 lines 2.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <AK/String.h> 11#include <LibGUI/Widget.h> 12#include <LibGfx/TextWrapping.h> 13 14namespace GUI { 15 16class AbstractButton : public Widget { 17 C_OBJECT_ABSTRACT(AbstractButton); 18 19public: 20 virtual ~AbstractButton() override = default; 21 22 Function<void(bool)> on_checked; 23 24 virtual void set_text(String); 25 String const& text() const { return m_text; } 26 27 bool is_exclusive() const { return m_exclusive; } 28 void set_exclusive(bool b) { m_exclusive = b; } 29 30 bool is_checked() const { return m_checked; } 31 void set_checked(bool, AllowCallback = AllowCallback::Yes); 32 33 bool is_checkable() const { return m_checkable; } 34 void set_checkable(bool); 35 36 bool is_hovered() const { return m_hovered; } 37 bool is_being_pressed() const { return m_being_pressed; } 38 bool was_being_pressed() const { return m_was_being_pressed; } 39 40 unsigned allowed_mouse_buttons_for_pressing() const { return m_allowed_mouse_buttons_for_pressing; } 41 void set_allowed_mouse_buttons_for_pressing(unsigned allowed_buttons) { m_allowed_mouse_buttons_for_pressing = allowed_buttons; } 42 43 virtual void click(unsigned modifiers = 0) = 0; 44 virtual void double_click(unsigned) { } 45 virtual void middle_mouse_click(unsigned) { } 46 virtual bool is_uncheckable() const { return true; } 47 48 int auto_repeat_interval() const { return m_auto_repeat_interval; } 49 void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; } 50 51protected: 52 explicit AbstractButton(String = {}); 53 54 virtual void mousedown_event(MouseEvent&) override; 55 virtual void mousemove_event(MouseEvent&) override; 56 virtual void mouseup_event(MouseEvent&) override; 57 virtual void doubleclick_event(GUI::MouseEvent&) override; 58 virtual void keydown_event(KeyEvent&) override; 59 virtual void keyup_event(KeyEvent&) override; 60 virtual void enter_event(Core::Event&) override; 61 virtual void leave_event(Core::Event&) override; 62 virtual void focusout_event(GUI::FocusEvent&) override; 63 virtual void change_event(Event&) override; 64 65 void paint_text(Painter&, Gfx::IntRect const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextWrapping = Gfx::TextWrapping::DontWrap); 66 67private: 68 String m_text; 69 bool m_checked { false }; 70 bool m_checkable { false }; 71 bool m_hovered { false }; 72 bool m_being_pressed { false }; 73 bool m_was_being_pressed { false }; 74 bool m_being_keyboard_pressed { false }; 75 bool m_exclusive { false }; 76 77 MouseButton m_pressed_mouse_button { MouseButton::None }; 78 unsigned m_allowed_mouse_buttons_for_pressing { MouseButton::Primary }; 79 80 int m_auto_repeat_interval { 0 }; 81 RefPtr<Core::Timer> m_auto_repeat_timer; 82}; 83 84}