2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-01-01 00:30:28 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-03 21:42:08 +01:00
|
|
|
#include <AK/JsonObject.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibCore/Timer.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/AbstractButton.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2021-01-01 00:40:12 +01:00
|
|
|
#include <LibGUI/Window.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-05-24 16:32:20 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-12-28 13:18:10 +01:00
|
|
|
AbstractButton::AbstractButton(String text)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
2020-12-28 13:18:10 +01:00
|
|
|
set_text(move(text));
|
|
|
|
|
2020-10-30 10:58:27 +01:00
|
|
|
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
2020-10-08 11:02:36 +02:00
|
|
|
set_background_role(Gfx::ColorRole::Button);
|
|
|
|
set_foreground_role(Gfx::ColorRole::ButtonText);
|
|
|
|
|
2020-02-23 10:31:26 +01:00
|
|
|
m_auto_repeat_timer = add<Core::Timer>();
|
2019-09-20 15:19:46 +02:00
|
|
|
m_auto_repeat_timer->on_timeout = [this] {
|
2019-07-13 10:27:19 +02:00
|
|
|
click();
|
|
|
|
};
|
2020-09-15 21:33:37 +02:00
|
|
|
|
|
|
|
REGISTER_STRING_PROPERTY("text", text, set_text);
|
|
|
|
REGISTER_BOOL_PROPERTY("checked", is_checked, set_checked);
|
|
|
|
REGISTER_BOOL_PROPERTY("checkable", is_checkable, set_checkable);
|
|
|
|
REGISTER_BOOL_PROPERTY("exclusive", is_exclusive, set_exclusive);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
AbstractButton::~AbstractButton()
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-28 13:18:10 +01:00
|
|
|
void AbstractButton::set_text(String text)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
if (m_text == text)
|
|
|
|
return;
|
2020-12-28 13:18:10 +01:00
|
|
|
m_text = move(text);
|
2019-05-24 16:32:20 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::set_checked(bool checked)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
if (m_checked == checked)
|
|
|
|
return;
|
|
|
|
m_checked = checked;
|
2019-06-12 05:57:26 +02:00
|
|
|
|
2020-10-30 16:44:56 +01:00
|
|
|
if (is_exclusive() && checked && parent_widget()) {
|
2021-01-01 00:40:12 +01:00
|
|
|
bool sibling_had_focus = false;
|
2020-02-02 15:07:41 +01:00
|
|
|
parent_widget()->for_each_child_of_type<AbstractButton>([&](auto& sibling) {
|
2021-01-01 00:40:12 +01:00
|
|
|
if (!sibling.is_exclusive())
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
if (window() && window()->focused_widget() == &sibling)
|
|
|
|
sibling_had_focus = true;
|
|
|
|
if (!sibling.is_checked())
|
2019-06-12 05:57:26 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
sibling.m_checked = false;
|
|
|
|
sibling.update();
|
|
|
|
if (sibling.on_checked)
|
|
|
|
sibling.on_checked(false);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
m_checked = true;
|
2021-01-01 00:40:12 +01:00
|
|
|
if (sibling_had_focus)
|
|
|
|
set_focus(true);
|
2019-06-12 05:57:26 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 16:32:20 +02:00
|
|
|
update();
|
2019-05-24 17:11:42 +02:00
|
|
|
if (on_checked)
|
|
|
|
on_checked(checked);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::set_checkable(bool checkable)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
if (m_checkable == checkable)
|
|
|
|
return;
|
|
|
|
m_checkable = checkable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::mousemove_event(MouseEvent& event)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
bool is_over = rect().contains(event.position());
|
|
|
|
m_hovered = is_over;
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.buttons() & MouseButton::Left) {
|
2020-04-29 19:05:30 +02:00
|
|
|
bool being_pressed = is_over;
|
|
|
|
if (being_pressed != m_being_pressed) {
|
|
|
|
m_being_pressed = being_pressed;
|
|
|
|
if (m_auto_repeat_interval) {
|
|
|
|
if (!m_being_pressed)
|
|
|
|
m_auto_repeat_timer->stop();
|
|
|
|
else
|
|
|
|
m_auto_repeat_timer->start(m_auto_repeat_interval);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
2020-04-29 19:05:30 +02:00
|
|
|
update();
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget::mousemove_event(event);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::mousedown_event(MouseEvent& event)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() == MouseButton::Left) {
|
2020-04-29 19:05:30 +02:00
|
|
|
m_being_pressed = true;
|
|
|
|
update();
|
2019-07-13 10:27:19 +02:00
|
|
|
|
2020-04-29 19:05:30 +02:00
|
|
|
if (m_auto_repeat_interval) {
|
|
|
|
click();
|
|
|
|
m_auto_repeat_timer->start(m_auto_repeat_interval);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget::mousedown_event(event);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::mouseup_event(MouseEvent& event)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() == MouseButton::Left) {
|
2019-09-20 15:19:46 +02:00
|
|
|
bool was_auto_repeating = m_auto_repeat_timer->is_active();
|
|
|
|
m_auto_repeat_timer->stop();
|
2020-04-29 19:05:30 +02:00
|
|
|
bool was_being_pressed = m_being_pressed;
|
|
|
|
m_being_pressed = false;
|
|
|
|
update();
|
|
|
|
if (was_being_pressed && !was_auto_repeating)
|
2020-05-12 20:30:33 +02:00
|
|
|
click(event.modifiers());
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget::mouseup_event(event);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::enter_event(Core::Event&)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
m_hovered = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::leave_event(Core::Event&)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
|
|
|
m_hovered = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::keydown_event(KeyEvent& event)
|
2019-05-24 16:32:20 +02:00
|
|
|
{
|
2020-10-26 20:46:30 +01:00
|
|
|
if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) {
|
2021-06-02 15:59:08 -06:00
|
|
|
m_being_pressed = true;
|
|
|
|
update();
|
|
|
|
event.accept();
|
|
|
|
return;
|
|
|
|
} else if (m_being_pressed && event.key() == KeyCode::Key_Escape) {
|
|
|
|
m_being_pressed = false;
|
|
|
|
update();
|
2019-09-20 20:37:31 +02:00
|
|
|
event.accept();
|
|
|
|
return;
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget::keydown_event(event);
|
2019-05-24 16:32:20 +02:00
|
|
|
}
|
2019-05-24 22:54:37 +02:00
|
|
|
|
2021-06-02 15:59:08 -06:00
|
|
|
void AbstractButton::keyup_event(KeyEvent& event)
|
|
|
|
{
|
2021-07-12 19:08:58 -05:00
|
|
|
bool was_being_pressed = m_being_pressed;
|
|
|
|
m_being_pressed = false;
|
|
|
|
if (was_being_pressed && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space)) {
|
2021-06-02 15:59:08 -06:00
|
|
|
click(event.modifiers());
|
2021-08-03 13:23:27 +02:00
|
|
|
update();
|
2021-06-02 15:59:08 -06:00
|
|
|
event.accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Widget::keyup_event(event);
|
|
|
|
}
|
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
void AbstractButton::paint_text(Painter& painter, const Gfx::IntRect& rect, const Gfx::Font& font, Gfx::TextAlignment text_alignment, Gfx::TextWrapping text_wrapping)
|
2019-05-24 22:54:37 +02:00
|
|
|
{
|
2019-05-25 04:01:22 +02:00
|
|
|
auto clipped_rect = rect.intersected(this->rect());
|
|
|
|
|
2019-05-24 22:54:37 +02:00
|
|
|
if (!is_enabled()) {
|
2021-07-27 19:33:03 +00:00
|
|
|
painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, Color::White, Gfx::TextElision::Right, text_wrapping);
|
|
|
|
painter.draw_text(clipped_rect, text(), font, text_alignment, Color::from_rgb(0x808080), Gfx::TextElision::Right, text_wrapping);
|
2019-05-24 22:54:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text().is_empty())
|
|
|
|
return;
|
2021-07-25 21:20:11 +00:00
|
|
|
painter.draw_text(clipped_rect, text(), font, text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping);
|
2019-05-24 22:54:37 +02:00
|
|
|
}
|
2019-05-25 13:40:57 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void AbstractButton::change_event(Event& event)
|
2019-05-25 13:40:57 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.type() == Event::Type::EnabledChange) {
|
2019-05-25 13:40:57 +02:00
|
|
|
if (!is_enabled()) {
|
|
|
|
bool was_being_pressed = m_being_pressed;
|
|
|
|
m_being_pressed = false;
|
|
|
|
if (was_being_pressed)
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget::change_event(event);
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:40:57 +02:00
|
|
|
}
|