2019-07-20 16:52:39 +02:00
|
|
|
#include <LibDraw/StylePainter.h>
|
2019-04-29 19:20:34 +02:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-06-07 11:46:02 +02:00
|
|
|
#include <LibGUI/GSlider.h>
|
2019-04-29 19:20:34 +02:00
|
|
|
|
2019-11-10 10:58:03 +01:00
|
|
|
GSlider::GSlider(GWidget* parent)
|
|
|
|
: GSlider(Orientation::Horizontal, parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-20 16:52:39 +02:00
|
|
|
GSlider::GSlider(Orientation orientation, GWidget* parent)
|
2019-04-29 19:20:34 +02:00
|
|
|
: GWidget(parent)
|
2019-07-20 16:52:39 +02:00
|
|
|
, m_orientation(orientation)
|
2019-04-29 19:20:34 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GSlider::~GSlider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSlider::set_range(int min, int max)
|
|
|
|
{
|
|
|
|
ASSERT(min <= max);
|
|
|
|
if (m_min == min && m_max == max)
|
|
|
|
return;
|
|
|
|
m_min = min;
|
|
|
|
m_max = max;
|
|
|
|
|
|
|
|
if (m_value > max)
|
|
|
|
m_value = max;
|
|
|
|
if (m_value < min)
|
|
|
|
m_value = min;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSlider::set_value(int value)
|
|
|
|
{
|
|
|
|
if (value > m_max)
|
|
|
|
value = m_max;
|
|
|
|
if (value < m_min)
|
|
|
|
value = m_min;
|
|
|
|
if (m_value == value)
|
|
|
|
return;
|
|
|
|
m_value = value;
|
|
|
|
update();
|
|
|
|
|
|
|
|
if (on_value_changed)
|
|
|
|
on_value_changed(m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSlider::paint_event(GPaintEvent& event)
|
|
|
|
{
|
|
|
|
GPainter painter(*this);
|
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
2019-07-20 16:52:39 +02:00
|
|
|
Rect track_rect;
|
|
|
|
|
|
|
|
if (orientation() == Orientation::Horizontal) {
|
|
|
|
track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() };
|
|
|
|
track_rect.center_vertically_within(inner_rect());
|
|
|
|
} else {
|
|
|
|
track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() };
|
|
|
|
track_rect.center_horizontally_within(inner_rect());
|
|
|
|
}
|
2019-04-29 19:20:34 +02:00
|
|
|
|
2019-12-24 20:57:54 +01:00
|
|
|
StylePainter::paint_frame(painter, track_rect, palette(), FrameShape::Panel, FrameShadow::Sunken, 1);
|
|
|
|
StylePainter::paint_button(painter, knob_rect(), palette(), ButtonStyle::Normal, false, m_knob_hovered);
|
2019-04-29 19:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect GSlider::knob_rect() const
|
|
|
|
{
|
|
|
|
auto inner_rect = this->inner_rect();
|
2019-07-11 15:31:43 +01:00
|
|
|
Rect rect;
|
2019-07-20 16:52:39 +02:00
|
|
|
rect.set_secondary_offset_for_orientation(orientation(), 0);
|
|
|
|
rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
|
2019-07-11 15:31:43 +01:00
|
|
|
|
|
|
|
if (knob_size_mode() == KnobSizeMode::Fixed) {
|
2019-07-20 16:52:39 +02:00
|
|
|
if (m_max - m_min) {
|
|
|
|
float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min);
|
|
|
|
rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)) - (knob_fixed_primary_size() / 2));
|
|
|
|
} else
|
|
|
|
rect.set_primary_size_for_orientation(orientation(), 0);
|
|
|
|
rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
|
|
|
|
} else {
|
|
|
|
float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min + 1);
|
|
|
|
rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)));
|
2019-07-11 15:31:43 +01:00
|
|
|
if (m_max - m_min)
|
2019-07-20 16:52:39 +02:00
|
|
|
rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
|
2019-07-11 15:31:43 +01:00
|
|
|
else
|
2019-07-20 16:52:39 +02:00
|
|
|
rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
|
2019-07-11 15:31:43 +01:00
|
|
|
}
|
2019-07-20 16:52:39 +02:00
|
|
|
if (orientation() == Orientation::Horizontal)
|
|
|
|
rect.center_vertically_within(inner_rect);
|
|
|
|
else
|
|
|
|
rect.center_horizontally_within(inner_rect);
|
2019-04-29 19:20:34 +02:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSlider::mousedown_event(GMouseEvent& event)
|
|
|
|
{
|
2019-05-24 23:03:11 +02:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
2019-04-29 19:20:34 +02:00
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
if (knob_rect().contains(event.position())) {
|
|
|
|
m_dragging = true;
|
|
|
|
m_drag_origin = event.position();
|
|
|
|
m_drag_origin_value = m_value;
|
|
|
|
return;
|
2019-07-20 16:52:39 +02:00
|
|
|
} else {
|
|
|
|
if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation()))
|
2019-07-11 15:31:43 +01:00
|
|
|
set_value(m_value + 1);
|
2019-07-20 16:52:39 +02:00
|
|
|
else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation()))
|
2019-07-11 15:31:43 +01:00
|
|
|
set_value(m_value - 1);
|
|
|
|
}
|
2019-04-29 19:20:34 +02:00
|
|
|
}
|
|
|
|
return GWidget::mousedown_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSlider::mousemove_event(GMouseEvent& event)
|
|
|
|
{
|
2019-05-24 23:03:11 +02:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
2019-04-30 00:47:46 +02:00
|
|
|
set_knob_hovered(knob_rect().contains(event.position()));
|
2019-04-29 19:20:34 +02:00
|
|
|
if (m_dragging) {
|
2019-07-20 16:52:39 +02:00
|
|
|
float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
|
|
|
|
float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
|
2019-04-29 19:20:34 +02:00
|
|
|
float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range;
|
|
|
|
float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
|
2019-04-29 23:42:55 +02:00
|
|
|
set_value((int)new_value);
|
2019-04-29 19:20:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return GWidget::mousemove_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSlider::mouseup_event(GMouseEvent& event)
|
|
|
|
{
|
2019-05-24 23:03:11 +02:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
2019-04-29 19:20:34 +02:00
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
m_dragging = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GWidget::mouseup_event(event);
|
|
|
|
}
|
2019-04-30 00:47:46 +02:00
|
|
|
|
|
|
|
void GSlider::leave_event(CEvent& event)
|
|
|
|
{
|
2019-05-24 23:03:11 +02:00
|
|
|
if (!is_enabled())
|
|
|
|
return;
|
2019-04-30 00:47:46 +02:00
|
|
|
set_knob_hovered(false);
|
|
|
|
GWidget::leave_event(event);
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:40:57 +02:00
|
|
|
void GSlider::change_event(GEvent& event)
|
|
|
|
{
|
|
|
|
if (event.type() == GEvent::Type::EnabledChange) {
|
2019-05-25 13:44:37 +02:00
|
|
|
if (!is_enabled())
|
2019-05-25 13:40:57 +02:00
|
|
|
m_dragging = false;
|
|
|
|
}
|
|
|
|
GWidget::change_event(event);
|
|
|
|
}
|
|
|
|
|
2019-04-30 00:47:46 +02:00
|
|
|
void GSlider::set_knob_hovered(bool hovered)
|
|
|
|
{
|
|
|
|
if (m_knob_hovered == hovered)
|
|
|
|
return;
|
|
|
|
m_knob_hovered = hovered;
|
|
|
|
update(knob_rect());
|
|
|
|
}
|