AbstractButton.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <LibGUI/Widget.h>
  28. namespace GUI {
  29. class AbstractButton : public Widget {
  30. C_OBJECT_ABSTRACT(AbstractButton)
  31. public:
  32. virtual ~AbstractButton() override;
  33. Function<void(bool)> on_checked;
  34. void set_text(const StringView&);
  35. const String& text() const { return m_text; }
  36. bool is_exclusive() const { return m_exclusive; }
  37. void set_exclusive(bool b) { m_exclusive = b; }
  38. bool is_checked() const { return m_checked; }
  39. void set_checked(bool);
  40. bool is_checkable() const { return m_checkable; }
  41. void set_checkable(bool);
  42. bool is_hovered() const { return m_hovered; }
  43. bool is_being_pressed() const { return m_being_pressed; }
  44. virtual void click(unsigned modifiers = 0) = 0;
  45. virtual bool accepts_focus() const override { return true; }
  46. virtual bool is_uncheckable() const { return true; }
  47. int auto_repeat_interval() const { return m_auto_repeat_interval; }
  48. void set_auto_repeat_interval(int interval) { m_auto_repeat_interval = interval; }
  49. protected:
  50. explicit AbstractButton(const StringView& = {});
  51. virtual void mousedown_event(MouseEvent&) override;
  52. virtual void mousemove_event(MouseEvent&) override;
  53. virtual void mouseup_event(MouseEvent&) override;
  54. virtual void keydown_event(KeyEvent&) override;
  55. virtual void enter_event(Core::Event&) override;
  56. virtual void leave_event(Core::Event&) override;
  57. virtual void change_event(Event&) override;
  58. void paint_text(Painter&, const Gfx::IntRect&, const Gfx::Font&, Gfx::TextAlignment);
  59. private:
  60. virtual bool is_abstract_button() const final { return true; }
  61. String m_text;
  62. bool m_checked { false };
  63. bool m_checkable { false };
  64. bool m_hovered { false };
  65. bool m_being_pressed { false };
  66. bool m_exclusive { false };
  67. int m_auto_repeat_interval { 0 };
  68. RefPtr<Core::Timer> m_auto_repeat_timer;
  69. };
  70. }
  71. AK_BEGIN_TYPE_TRAITS(GUI::AbstractButton)
  72. static bool is_type(const Core::Object& object) { return is<GUI::Widget>(object) && downcast<GUI::Widget>(object).is_abstract_button(); }
  73. AK_END_TYPE_TRAITS()