AbstractButton.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibCore/Timer.h>
  28. #include <LibGfx/TextAlignment.h>
  29. #include <LibGUI/Widget.h>
  30. namespace GUI {
  31. class Painter;
  32. class AbstractButton : public Widget {
  33. C_OBJECT_ABSTRACT(GAbstractButton)
  34. public:
  35. virtual ~AbstractButton() override;
  36. Function<void(bool)> on_checked;
  37. void set_text(const StringView&);
  38. const String& text() const { return m_text; }
  39. bool is_exclusive() const { return m_exclusive; }
  40. void set_exclusive(bool b) { m_exclusive = b; }
  41. bool is_checked() const { return m_checked; }
  42. void set_checked(bool);
  43. bool is_checkable() const { return m_checkable; }
  44. void set_checkable(bool);
  45. bool is_hovered() const { return m_hovered; }
  46. bool is_being_pressed() const { return m_being_pressed; }
  47. virtual void click() = 0;
  48. virtual bool accepts_focus() const override { return true; }
  49. virtual bool is_uncheckable() const { return true; }
  50. int auto_repeat_interval() const { return m_auto_repeat_interval; }
  51. void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; }
  52. protected:
  53. explicit AbstractButton(Widget* parent);
  54. AbstractButton(const StringView&, Widget* parent);
  55. virtual void mousedown_event(MouseEvent&) override;
  56. virtual void mousemove_event(MouseEvent&) override;
  57. virtual void mouseup_event(MouseEvent&) override;
  58. virtual void keydown_event(KeyEvent&) override;
  59. virtual void enter_event(Core::Event&) override;
  60. virtual void leave_event(Core::Event&) override;
  61. virtual void change_event(Event&) override;
  62. void paint_text(Painter&, const Gfx::Rect&, const Gfx::Font&, Gfx::TextAlignment);
  63. private:
  64. virtual bool is_abstract_button() const final { return true; }
  65. String m_text;
  66. bool m_checked { false };
  67. bool m_checkable { false };
  68. bool m_hovered { false };
  69. bool m_being_pressed { false };
  70. bool m_exclusive { false };
  71. int m_auto_repeat_interval { 0 };
  72. RefPtr<Core::Timer> m_auto_repeat_timer;
  73. };
  74. }
  75. template<>
  76. inline bool Core::is<GUI::AbstractButton>(const Core::Object& object)
  77. {
  78. if (!is<GUI::Widget>(object))
  79. return false;
  80. return to<GUI::Widget>(object).is_abstract_button();
  81. }