AbstractButton.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2018-2021, 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. #include <AK/JsonObject.h>
  27. #include <LibCore/Timer.h>
  28. #include <LibGUI/AbstractButton.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Palette.h>
  32. namespace GUI {
  33. AbstractButton::AbstractButton(String text)
  34. {
  35. set_text(move(text));
  36. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  37. set_background_role(Gfx::ColorRole::Button);
  38. set_foreground_role(Gfx::ColorRole::ButtonText);
  39. m_auto_repeat_timer = add<Core::Timer>();
  40. m_auto_repeat_timer->on_timeout = [this] {
  41. click();
  42. };
  43. REGISTER_STRING_PROPERTY("text", text, set_text);
  44. REGISTER_BOOL_PROPERTY("checked", is_checked, set_checked);
  45. REGISTER_BOOL_PROPERTY("checkable", is_checkable, set_checkable);
  46. REGISTER_BOOL_PROPERTY("exclusive", is_exclusive, set_exclusive);
  47. }
  48. AbstractButton::~AbstractButton()
  49. {
  50. }
  51. void AbstractButton::set_text(String text)
  52. {
  53. if (m_text == text)
  54. return;
  55. m_text = move(text);
  56. update();
  57. }
  58. void AbstractButton::set_checked(bool checked)
  59. {
  60. if (m_checked == checked)
  61. return;
  62. m_checked = checked;
  63. if (is_exclusive() && checked && parent_widget()) {
  64. bool sibling_had_focus = false;
  65. parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
  66. if (!sibling.is_exclusive())
  67. return IterationDecision::Continue;
  68. if (window() && window()->focused_widget() == &sibling)
  69. sibling_had_focus = true;
  70. if (!sibling.is_checked())
  71. return IterationDecision::Continue;
  72. sibling.m_checked = false;
  73. sibling.update();
  74. if (sibling.on_checked)
  75. sibling.on_checked(false);
  76. return IterationDecision::Continue;
  77. });
  78. m_checked = true;
  79. if (sibling_had_focus)
  80. set_focus(true);
  81. }
  82. update();
  83. if (on_checked)
  84. on_checked(checked);
  85. }
  86. void AbstractButton::set_checkable(bool checkable)
  87. {
  88. if (m_checkable == checkable)
  89. return;
  90. m_checkable = checkable;
  91. update();
  92. }
  93. void AbstractButton::mousemove_event(MouseEvent& event)
  94. {
  95. bool is_over = rect().contains(event.position());
  96. m_hovered = is_over;
  97. if (event.buttons() & MouseButton::Left) {
  98. bool being_pressed = is_over;
  99. if (being_pressed != m_being_pressed) {
  100. m_being_pressed = being_pressed;
  101. if (m_auto_repeat_interval) {
  102. if (!m_being_pressed)
  103. m_auto_repeat_timer->stop();
  104. else
  105. m_auto_repeat_timer->start(m_auto_repeat_interval);
  106. }
  107. update();
  108. }
  109. }
  110. Widget::mousemove_event(event);
  111. }
  112. void AbstractButton::mousedown_event(MouseEvent& event)
  113. {
  114. if (event.button() == MouseButton::Left) {
  115. m_being_pressed = true;
  116. update();
  117. if (m_auto_repeat_interval) {
  118. click();
  119. m_auto_repeat_timer->start(m_auto_repeat_interval);
  120. }
  121. }
  122. Widget::mousedown_event(event);
  123. }
  124. void AbstractButton::mouseup_event(MouseEvent& event)
  125. {
  126. if (event.button() == MouseButton::Left) {
  127. bool was_auto_repeating = m_auto_repeat_timer->is_active();
  128. m_auto_repeat_timer->stop();
  129. bool was_being_pressed = m_being_pressed;
  130. m_being_pressed = false;
  131. update();
  132. if (was_being_pressed && !was_auto_repeating)
  133. click(event.modifiers());
  134. }
  135. Widget::mouseup_event(event);
  136. }
  137. void AbstractButton::enter_event(Core::Event&)
  138. {
  139. m_hovered = true;
  140. update();
  141. }
  142. void AbstractButton::leave_event(Core::Event&)
  143. {
  144. m_hovered = false;
  145. update();
  146. }
  147. void AbstractButton::keydown_event(KeyEvent& event)
  148. {
  149. if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
  150. click(event.modifiers());
  151. event.accept();
  152. return;
  153. }
  154. Widget::keydown_event(event);
  155. }
  156. void AbstractButton::paint_text(Painter& painter, const Gfx::IntRect& rect, const Gfx::Font& font, Gfx::TextAlignment text_alignment)
  157. {
  158. auto clipped_rect = rect.intersected(this->rect());
  159. if (!is_enabled()) {
  160. painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, Color::White, Gfx::TextElision::Right);
  161. painter.draw_text(clipped_rect, text(), font, text_alignment, Color::from_rgb(0x808080), Gfx::TextElision::Right);
  162. return;
  163. }
  164. if (text().is_empty())
  165. return;
  166. painter.draw_text(clipped_rect, text(), font, text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right);
  167. }
  168. void AbstractButton::change_event(Event& event)
  169. {
  170. if (event.type() == Event::Type::EnabledChange) {
  171. if (!is_enabled()) {
  172. bool was_being_pressed = m_being_pressed;
  173. m_being_pressed = false;
  174. if (was_being_pressed)
  175. update();
  176. }
  177. }
  178. Widget::change_event(event);
  179. }
  180. }