summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/AbstractSlider.cpp
blob: 24e2d3c03a7259956af657a0e6961925a43cb1c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Assertions.h>
#include <AK/StdLibExtras.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Slider.h>
#include <LibGfx/Palette.h>
#include <LibGfx/StylePainter.h>

namespace GUI {

AbstractSlider::AbstractSlider(Orientation orientation)
    : m_orientation(orientation)
{
    REGISTER_INT_PROPERTY("value", value, set_value);
    REGISTER_INT_PROPERTY("min", min, set_min);
    REGISTER_INT_PROPERTY("max", max, set_max);
    REGISTER_INT_PROPERTY("step", step, set_step);
    REGISTER_INT_PROPERTY("page_step", page_step, set_page_step);
    REGISTER_ENUM_PROPERTY("orientation", this->orientation, set_orientation, Orientation,
        { Orientation::Horizontal, "Horizontal" },
        { Orientation::Vertical, "Vertical" });
}

AbstractSlider::~AbstractSlider()
{
}

void AbstractSlider::set_orientation(Orientation value)
{
    if (m_orientation == value)
        return;
    m_orientation = value;
    update();
}

void AbstractSlider::set_page_step(int page_step)
{
    m_page_step = AK::max(0, page_step);
}

void AbstractSlider::set_range(int min, int max)
{
    VERIFY(min <= max);
    if (m_min == min && m_max == max)
        return;
    m_min = min;
    m_max = max;
    m_value = clamp(m_value, m_min, m_max);
    update();
}

void AbstractSlider::set_value(int value)
{
    value = clamp(value, m_min, m_max);
    if (m_value == value)
        return;
    m_value = value;
    update();

    if (on_change)
        on_change(m_value);
}

}