AbstractButton.h 2.2 KB

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