AbstractButton.cpp 6.7 KB

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