GAbstractButton.cpp 6.0 KB

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