AbstractButton.cpp 6.1 KB

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