GAbstractButton.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <LibCore/CTimer.h>
  3. #include <LibGUI/GWidget.h>
  4. #include <LibDraw/TextAlignment.h>
  5. class GPainter;
  6. class GAbstractButton : public GWidget {
  7. C_OBJECT(GAbstractButton)
  8. public:
  9. virtual ~GAbstractButton() override;
  10. Function<void(bool)> on_checked;
  11. void set_text(const StringView&);
  12. const String& text() const { return m_text; }
  13. bool is_exclusive() const { return m_exclusive; }
  14. void set_exclusive(bool b) { m_exclusive = b; }
  15. bool is_checked() const { return m_checked; }
  16. void set_checked(bool);
  17. bool is_checkable() const { return m_checkable; }
  18. void set_checkable(bool);
  19. bool is_hovered() const { return m_hovered; }
  20. bool is_being_pressed() const { return m_being_pressed; }
  21. virtual void click() = 0;
  22. virtual bool accepts_focus() const override { return true; }
  23. virtual bool supports_keyboard_activation() const override { return true; }
  24. virtual bool is_uncheckable() const { return true; }
  25. int auto_repeat_interval() const { return m_auto_repeat_interval; }
  26. void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; }
  27. protected:
  28. explicit GAbstractButton(GWidget* parent);
  29. GAbstractButton(const StringView&, GWidget* parent);
  30. virtual void mousedown_event(GMouseEvent&) override;
  31. virtual void mousemove_event(GMouseEvent&) override;
  32. virtual void mouseup_event(GMouseEvent&) override;
  33. virtual void keydown_event(GKeyEvent&) override;
  34. virtual void enter_event(CEvent&) override;
  35. virtual void leave_event(CEvent&) override;
  36. virtual void change_event(GEvent&) override;
  37. void paint_text(GPainter&, const Rect&, const Font&, TextAlignment);
  38. private:
  39. virtual bool is_abstract_button() const final { return true; }
  40. String m_text;
  41. bool m_checked { false };
  42. bool m_checkable { false };
  43. bool m_hovered { false };
  44. bool m_being_pressed { false };
  45. bool m_exclusive { false };
  46. int m_auto_repeat_interval { 0 };
  47. ObjectPtr<CTimer> m_auto_repeat_timer;
  48. };
  49. template<>
  50. inline bool is<GAbstractButton>(const CObject& object)
  51. {
  52. if (!is<GWidget>(object))
  53. return false;
  54. return to<GWidget>(object).is_abstract_button();
  55. }