AbstractButton.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <LibGfx/Palette.h>
  27. #include <LibGUI/AbstractButton.h>
  28. #include <LibGUI/Painter.h>
  29. namespace GUI {
  30. AbstractButton::AbstractButton(Widget* parent)
  31. : AbstractButton({}, parent)
  32. {
  33. }
  34. AbstractButton::AbstractButton(const StringView& text, Widget* parent)
  35. : Widget(parent)
  36. , m_text(text)
  37. {
  38. m_auto_repeat_timer = Core::Timer::construct(this);
  39. m_auto_repeat_timer->on_timeout = [this] {
  40. click();
  41. };
  42. }
  43. AbstractButton::~AbstractButton()
  44. {
  45. }
  46. void AbstractButton::set_text(const StringView& text)
  47. {
  48. if (m_text == text)
  49. return;
  50. m_text = text;
  51. update();
  52. }
  53. void AbstractButton::set_checked(bool checked)
  54. {
  55. if (m_checked == checked)
  56. return;
  57. m_checked = checked;
  58. if (is_exclusive() && checked) {
  59. parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
  60. if (!sibling.is_exclusive() || !sibling.is_checked())
  61. return IterationDecision::Continue;
  62. sibling.m_checked = false;
  63. sibling.update();
  64. if (sibling.on_checked)
  65. sibling.on_checked(false);
  66. return IterationDecision::Continue;
  67. });
  68. m_checked = true;
  69. }
  70. update();
  71. if (on_checked)
  72. on_checked(checked);
  73. }
  74. void AbstractButton::set_checkable(bool checkable)
  75. {
  76. if (m_checkable == checkable)
  77. return;
  78. m_checkable = checkable;
  79. update();
  80. }
  81. void AbstractButton::mousemove_event(MouseEvent& event)
  82. {
  83. bool is_over = rect().contains(event.position());
  84. m_hovered = is_over;
  85. if (event.buttons() & MouseButton::Left) {
  86. if (is_enabled()) {
  87. bool being_pressed = is_over;
  88. if (being_pressed != m_being_pressed) {
  89. m_being_pressed = being_pressed;
  90. if (m_auto_repeat_interval) {
  91. if (!m_being_pressed)
  92. m_auto_repeat_timer->stop();
  93. else
  94. m_auto_repeat_timer->start(m_auto_repeat_interval);
  95. }
  96. update();
  97. }
  98. }
  99. }
  100. Widget::mousemove_event(event);
  101. }
  102. void AbstractButton::mousedown_event(MouseEvent& event)
  103. {
  104. #ifdef GABSTRACTBUTTON_DEBUG
  105. dbgprintf("AbstractButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  106. #endif
  107. if (event.button() == MouseButton::Left) {
  108. if (is_enabled()) {
  109. m_being_pressed = true;
  110. update();
  111. if (m_auto_repeat_interval) {
  112. click();
  113. m_auto_repeat_timer->start(m_auto_repeat_interval);
  114. }
  115. }
  116. }
  117. Widget::mousedown_event(event);
  118. }
  119. void AbstractButton::mouseup_event(MouseEvent& event)
  120. {
  121. #ifdef GABSTRACTBUTTON_DEBUG
  122. dbgprintf("AbstractButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  123. #endif
  124. if (event.button() == MouseButton::Left) {
  125. bool was_auto_repeating = m_auto_repeat_timer->is_active();
  126. m_auto_repeat_timer->stop();
  127. if (is_enabled()) {
  128. bool was_being_pressed = m_being_pressed;
  129. m_being_pressed = false;
  130. update();
  131. if (was_being_pressed && !was_auto_repeating)
  132. click();
  133. }
  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) {
  150. click();
  151. event.accept();
  152. return;
  153. }
  154. Widget::keydown_event(event);
  155. }
  156. void AbstractButton::paint_text(Painter& painter, const Gfx::Rect& 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().button_text(), Gfx::TextElision::Right);
  167. if (is_focused())
  168. painter.draw_rect(clipped_rect.inflated(6, 4), Color(140, 140, 140));
  169. }
  170. void AbstractButton::change_event(Event& event)
  171. {
  172. if (event.type() == Event::Type::EnabledChange) {
  173. if (!is_enabled()) {
  174. bool was_being_pressed = m_being_pressed;
  175. m_being_pressed = false;
  176. if (was_being_pressed)
  177. update();
  178. }
  179. }
  180. Widget::change_event(event);
  181. }
  182. }