AbstractButton.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/Widget.h>
  8. #include <LibGfx/TextWrapping.h>
  9. namespace GUI {
  10. class AbstractButton : public Widget {
  11. C_OBJECT_ABSTRACT(AbstractButton);
  12. public:
  13. virtual ~AbstractButton() override;
  14. Function<void(bool)> on_checked;
  15. void set_text(String);
  16. const String& text() const { return m_text; }
  17. bool is_exclusive() const { return m_exclusive; }
  18. void set_exclusive(bool b) { m_exclusive = b; }
  19. bool is_checked() const { return m_checked; }
  20. void set_checked(bool, AllowCallback = AllowCallback::Yes);
  21. bool is_checkable() const { return m_checkable; }
  22. void set_checkable(bool);
  23. bool is_hovered() const { return m_hovered; }
  24. bool is_being_pressed() const { return m_being_pressed; }
  25. virtual void click(unsigned modifiers = 0) = 0;
  26. virtual bool is_uncheckable() const { return true; }
  27. int auto_repeat_interval() const { return m_auto_repeat_interval; }
  28. void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; }
  29. protected:
  30. explicit AbstractButton(String = {});
  31. virtual void mousedown_event(MouseEvent&) override;
  32. virtual void mousemove_event(MouseEvent&) override;
  33. virtual void mouseup_event(MouseEvent&) override;
  34. virtual void keydown_event(KeyEvent&) override;
  35. virtual void keyup_event(KeyEvent&) override;
  36. virtual void enter_event(Core::Event&) override;
  37. virtual void leave_event(Core::Event&) override;
  38. virtual void focusout_event(GUI::FocusEvent&) override;
  39. virtual void change_event(Event&) override;
  40. void paint_text(Painter&, const Gfx::IntRect&, const Gfx::Font&, Gfx::TextAlignment, Gfx::TextWrapping = Gfx::TextWrapping::DontWrap);
  41. private:
  42. String m_text;
  43. bool m_checked { false };
  44. bool m_checkable { false };
  45. bool m_hovered { false };
  46. bool m_being_pressed { false };
  47. bool m_being_keyboard_pressed { false };
  48. bool m_exclusive { false };
  49. int m_auto_repeat_interval { 0 };
  50. RefPtr<Core::Timer> m_auto_repeat_timer;
  51. };
  52. }