Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <LibGUI/Widget.h>
10
11namespace GUI {
12
13class AbstractSlider : public Widget {
14 C_OBJECT_ABSTRACT(AbstractSlider);
15
16public:
17 virtual ~AbstractSlider() override = default;
18
19 void set_orientation(Orientation value);
20 Orientation orientation() const { return m_orientation; }
21
22 int value() const { return m_value; }
23 int min() const { return m_min; }
24 int max() const { return m_max; }
25 int step() const { return m_step; }
26 int page_step() const { return m_page_step; }
27 bool jump_to_cursor() const { return m_jump_to_cursor; }
28
29 bool is_min() const { return m_value == m_min; }
30 bool is_max() const { return m_value == m_max; }
31
32 void set_range(int min, int max);
33
34 enum class DoClamp {
35 Yes = 1,
36 No = 0
37 };
38 virtual void set_value(int, AllowCallback = AllowCallback::Yes, DoClamp = DoClamp::Yes);
39
40 void set_min(int min) { set_range(min, max()); }
41 void set_max(int max) { set_range(min(), max); }
42 void set_step(int step) { m_step = step; }
43 void set_page_step(int page_step);
44 void set_jump_to_cursor(bool b) { m_jump_to_cursor = b; }
45
46 virtual void increase_slider_by(int delta) { set_value(value() + delta); }
47 virtual void decrease_slider_by(int delta) { set_value(value() - delta); }
48 virtual void increase_slider_by_page_steps(int page_steps) { set_value(value() + page_step() * page_steps); }
49 virtual void decrease_slider_by_page_steps(int page_steps) { set_value(value() - page_step() * page_steps); }
50 virtual void increase_slider_by_steps(int steps) { set_value(value() + step() * steps); }
51 virtual void decrease_slider_by_steps(int steps) { set_value(value() - step() * steps); }
52
53 Function<void(int)> on_change;
54
55protected:
56 explicit AbstractSlider(Orientation = Orientation::Vertical);
57
58private:
59 int m_value { 0 };
60 int m_min { 0 };
61 int m_max { 0 };
62 int m_step { 1 };
63 int m_page_step { 10 };
64 bool m_jump_to_cursor { false };
65 Orientation m_orientation { Orientation::Horizontal };
66};
67
68}