2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2022-08-30 20:46:33 -05:00
|
|
|
* Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
|
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
|
|
|
*/
|
|
|
|
|
|
2021-02-27 08:23:42 -05:00
|
|
|
#include <LibGUI/Button.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/SpinBox.h>
|
2020-02-23 10:31:26 +01:00
|
|
|
#include <LibGUI/TextBox.h>
|
2019-04-09 16:29:00 +02:00
|
|
|
|
2021-01-02 16:30:13 -07:00
|
|
|
REGISTER_WIDGET(GUI, SpinBox)
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2020-02-23 12:07:13 +01:00
|
|
|
SpinBox::SpinBox()
|
2019-04-09 16:29:00 +02:00
|
|
|
{
|
2022-02-22 20:25:30 +01:00
|
|
|
set_min_size({ 40, 22 });
|
|
|
|
|
set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
|
2020-02-23 10:31:26 +01:00
|
|
|
m_editor = add<TextBox>();
|
2022-07-11 17:32:29 +00:00
|
|
|
m_editor->set_text("0"sv);
|
2022-03-08 01:41:09 -06:00
|
|
|
m_editor->on_change = [this, weak_this = make_weak_ptr()] {
|
|
|
|
|
if (!weak_this)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-12 21:07:52 +02:00
|
|
|
auto value = m_editor->text().to_uint();
|
2022-08-30 20:46:33 -05:00
|
|
|
if (!value.has_value() && m_editor->text().length() > 0)
|
|
|
|
|
m_editor->do_delete();
|
|
|
|
|
};
|
|
|
|
|
m_editor->on_focusout = [this] {
|
|
|
|
|
auto value = m_editor->text().to_int();
|
2020-06-12 21:07:52 +02:00
|
|
|
if (value.has_value())
|
|
|
|
|
set_value(value.value());
|
2019-04-09 16:29:00 +02:00
|
|
|
else
|
2022-08-30 20:46:33 -05:00
|
|
|
set_value(min());
|
2019-04-09 16:29:00 +02:00
|
|
|
};
|
2020-09-23 18:57:39 +02:00
|
|
|
m_editor->on_up_pressed = [this] {
|
|
|
|
|
set_value(m_value + 1);
|
|
|
|
|
};
|
|
|
|
|
m_editor->on_down_pressed = [this] {
|
|
|
|
|
set_value(m_value - 1);
|
|
|
|
|
};
|
2022-03-08 21:07:32 -06:00
|
|
|
m_editor->on_return_pressed = [this] {
|
|
|
|
|
if (on_return_pressed)
|
|
|
|
|
on_return_pressed();
|
|
|
|
|
};
|
2020-07-19 10:23:12 -04:00
|
|
|
|
2021-02-27 08:23:42 -05:00
|
|
|
m_increment_button = add<Button>();
|
2021-09-01 11:46:24 -04:00
|
|
|
m_increment_button->set_button_style(Gfx::ButtonStyle::ThickCap);
|
2022-07-11 17:32:29 +00:00
|
|
|
m_increment_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/upward-triangle.png"sv).release_value_but_fixme_should_propagate_errors());
|
2020-10-30 10:58:27 +01:00
|
|
|
m_increment_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
|
2020-05-12 20:30:33 +02:00
|
|
|
m_increment_button->on_click = [this](auto) { set_value(m_value + 1); };
|
2019-07-13 10:27:19 +02:00
|
|
|
m_increment_button->set_auto_repeat_interval(150);
|
2021-02-27 08:23:42 -05:00
|
|
|
m_decrement_button = add<Button>();
|
2021-09-01 11:46:24 -04:00
|
|
|
m_decrement_button->set_button_style(Gfx::ButtonStyle::ThickCap);
|
2022-07-11 17:32:29 +00:00
|
|
|
m_decrement_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/downward-triangle.png"sv).release_value_but_fixme_should_propagate_errors());
|
2020-10-30 10:58:27 +01:00
|
|
|
m_decrement_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
|
2020-05-12 20:30:33 +02:00
|
|
|
m_decrement_button->on_click = [this](auto) { set_value(m_value - 1); };
|
2019-07-13 10:27:19 +02:00
|
|
|
m_decrement_button->set_auto_repeat_interval(150);
|
2020-12-30 13:58:38 +03:30
|
|
|
|
|
|
|
|
REGISTER_INT_PROPERTY("min", min, set_min);
|
|
|
|
|
REGISTER_INT_PROPERTY("max", max, set_max);
|
2019-04-09 16:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 17:02:48 -04:00
|
|
|
void SpinBox::set_value(int value, AllowCallback allow_callback)
|
2019-04-09 16:29:00 +02:00
|
|
|
{
|
2020-01-20 13:16:58 +01:00
|
|
|
value = clamp(value, m_min, m_max);
|
2019-04-09 16:29:00 +02:00
|
|
|
if (m_value == value)
|
|
|
|
|
return;
|
|
|
|
|
m_value = value;
|
2022-08-30 20:59:58 -05:00
|
|
|
|
|
|
|
|
m_increment_button->set_enabled(m_value < m_max);
|
|
|
|
|
m_decrement_button->set_enabled(m_value > m_min);
|
|
|
|
|
|
2019-07-03 14:56:27 +02:00
|
|
|
m_editor->set_text(String::number(value));
|
2019-04-09 16:29:00 +02:00
|
|
|
update();
|
2021-09-21 17:02:48 -04:00
|
|
|
if (on_change && allow_callback == AllowCallback::Yes)
|
2019-04-09 16:29:00 +02:00
|
|
|
on_change(value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 17:02:48 -04:00
|
|
|
void SpinBox::set_range(int min, int max, AllowCallback allow_callback)
|
2019-04-09 16:29:00 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(min <= max);
|
2019-04-09 16:29:00 +02:00
|
|
|
if (m_min == min && m_max == max)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_min = min;
|
|
|
|
|
m_max = max;
|
|
|
|
|
|
|
|
|
|
int old_value = m_value;
|
2020-01-20 13:16:58 +01:00
|
|
|
m_value = clamp(m_value, m_min, m_max);
|
2020-06-29 14:15:27 +02:00
|
|
|
if (m_value != old_value) {
|
|
|
|
|
m_editor->set_text(String::number(m_value));
|
2021-09-21 17:02:48 -04:00
|
|
|
if (on_change && allow_callback == AllowCallback::Yes)
|
2020-06-29 14:15:27 +02:00
|
|
|
on_change(m_value);
|
|
|
|
|
}
|
2019-04-09 16:29:00 +02:00
|
|
|
|
2022-09-06 18:18:23 +02:00
|
|
|
m_increment_button->set_enabled(m_value < m_max);
|
|
|
|
|
m_decrement_button->set_enabled(m_value > m_min);
|
|
|
|
|
|
2019-04-09 16:29:00 +02:00
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-24 10:20:25 +01:00
|
|
|
void SpinBox::mousewheel_event(MouseEvent& event)
|
|
|
|
|
{
|
2021-12-13 23:22:28 +01:00
|
|
|
auto wheel_delta = event.wheel_delta_y() / abs(event.wheel_delta_y());
|
2021-03-07 14:51:14 -05:00
|
|
|
if (event.modifiers() == KeyModifier::Mod_Ctrl)
|
|
|
|
|
wheel_delta *= 6;
|
|
|
|
|
set_value(m_value - wheel_delta);
|
2020-02-24 10:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void SpinBox::resize_event(ResizeEvent& event)
|
2019-04-09 16:29:00 +02:00
|
|
|
{
|
2019-04-10 02:09:06 +02:00
|
|
|
int frame_thickness = m_editor->frame_thickness();
|
|
|
|
|
int button_height = (event.size().height() / 2) - frame_thickness;
|
2019-04-10 01:37:08 +02:00
|
|
|
int button_width = 15;
|
2019-04-10 02:09:06 +02:00
|
|
|
m_increment_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
|
|
|
|
|
m_decrement_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness + button_height, button_width, button_height);
|
|
|
|
|
m_editor->set_relative_rect(0, 0, width(), height());
|
2019-04-09 16:29:00 +02:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|