AbstractButton.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 bool is_uncheckable() const { return true; }
  30. int auto_repeat_interval() const { return m_auto_repeat_interval; }
  31. void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; }
  32. protected:
  33. explicit AbstractButton(String = {});
  34. virtual void mousedown_event(MouseEvent&) override;
  35. virtual void mousemove_event(MouseEvent&) override;
  36. virtual void mouseup_event(MouseEvent&) override;
  37. virtual void keydown_event(KeyEvent&) override;
  38. virtual void keyup_event(KeyEvent&) override;
  39. virtual void enter_event(Core::Event&) override;
  40. virtual void leave_event(Core::Event&) override;
  41. virtual void focusout_event(GUI::FocusEvent&) override;
  42. virtual void change_event(Event&) override;
  43. void paint_text(Painter&, Gfx::IntRect const&, Gfx::Font const&, Gfx::TextAlignment, Gfx::TextWrapping = Gfx::TextWrapping::DontWrap);
  44. private:
  45. String m_text;
  46. bool m_checked { false };
  47. bool m_checkable { false };
  48. bool m_hovered { false };
  49. bool m_being_pressed { false };
  50. bool m_being_keyboard_pressed { false };
  51. bool m_exclusive { false };
  52. MouseButton m_pressed_mouse_button { MouseButton::None };
  53. unsigned m_allowed_mouse_buttons_for_pressing { MouseButton::Primary };
  54. int m_auto_repeat_interval { 0 };
  55. RefPtr<Core::Timer> m_auto_repeat_timer;
  56. };
  57. }