GAbstractButton.h 1.9 KB

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