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