AbstractButton.cpp 5.9 KB

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