AbstractButton.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #pragma once
  8. #include <LibGUI/Widget.h>
  9. #include <LibGfx/TextWrapping.h>
  10. namespace GUI {
  11. class AbstractButton : public Widget {
  12. C_OBJECT_ABSTRACT(AbstractButton);
  13. public:
  14. virtual ~AbstractButton() override = default;
  15. Function<void(bool)> on_checked;
  16. void set_text(String);
  17. String const& text() const { return m_text; }
  18. bool is_exclusive() const { return m_exclusive; }
  19. void set_exclusive(bool b) { m_exclusive = b; }
  20. bool is_checked() const { return m_checked; }
  21. void set_checked(bool, AllowCallback = AllowCallback::Yes);
  22. bool is_checkable() const { return m_checkable; }
  23. void set_checkable(bool);
  24. bool is_hovered() const { return m_hovered; }
  25. bool is_being_pressed() const { return m_being_pressed; }
  26. unsigned allowed_mouse_buttons_for_pressing() const { return m_allowed_mouse_buttons_for_pressing; }
  27. void set_allowed_mouse_buttons_for_pressing(unsigned allowed_buttons) { m_allowed_mouse_buttons_for_pressing = allowed_buttons; }
  28. virtual void click(unsigned modifiers = 0) = 0;
  29. virtual void middle_mouse_click(unsigned) {};
  30. virtual bool is_uncheckable() const { return true; }
  31. int auto_repeat_interval() const { return m_auto_repeat_interval; }
  32. void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; }
  33. protected:
  34. explicit AbstractButton(String = {});
  35. virtual void mousedown_event(MouseEvent&) override;
  36. virtual void mousemove_event(MouseEvent&) override;
  37. virtual void mouseup_event(MouseEvent&) override;
  38. virtual void keydown_event(KeyEvent&) override;
  39. virtual void keyup_event(KeyEvent&) override;
  40. virtual void enter_event(Core::Event&) override;
  41. virtual void leave_event(Core::Event&) override;
  42. virtual void focusout_event(GUI::FocusEvent&) override;
  43. virtual void change_event(Event&) override;
  44. void paint_text(Painter&, Gfx::IntRect const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextWrapping = Gfx::TextWrapping::DontWrap);
  45. private:
  46. String m_text;
  47. bool m_checked { false };
  48. bool m_checkable { false };
  49. bool m_hovered { false };
  50. bool m_being_pressed { false };
  51. bool m_being_keyboard_pressed { false };
  52. bool m_exclusive { false };
  53. MouseButton m_pressed_mouse_button { MouseButton::None };
  54. unsigned m_allowed_mouse_buttons_for_pressing { MouseButton::Primary };
  55. int m_auto_repeat_interval { 0 };
  56. RefPtr<Core::Timer> m_auto_repeat_timer;
  57. };
  58. }