Serenity Operating System
at portability 126 lines 3.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <LibGUI/Widget.h> 30 31namespace GUI { 32 33class Slider : public Widget { 34 C_OBJECT(Slider) 35public: 36 enum class KnobSizeMode { 37 Fixed, 38 Proportional, 39 }; 40 41 virtual ~Slider() override; 42 43 Orientation orientation() const { return m_orientation; } 44 45 int value() const { return m_value; } 46 int min() const { return m_min; } 47 int max() const { return m_max; } 48 int step() const { return m_step; } 49 50 void set_range(int min, int max); 51 void set_value(int); 52 53 void set_min(int min) { set_range(min, max()); } 54 void set_max(int max) { set_range(min(), max); } 55 void set_step(int step) { m_step = step; } 56 57 void set_knob_size_mode(KnobSizeMode mode) { m_knob_size_mode = mode; } 58 KnobSizeMode knob_size_mode() const { return m_knob_size_mode; } 59 60 int track_size() const { return 2; } 61 int knob_fixed_primary_size() const { return 8; } 62 int knob_secondary_size() const { return 20; } 63 64 bool knob_dragging() const { return m_dragging; } 65 Gfx::Rect knob_rect() const; 66 67 Gfx::Rect inner_rect() const 68 { 69 if (orientation() == Orientation::Horizontal) 70 return rect().shrunken(20, 0); 71 return rect().shrunken(0, 20); 72 } 73 74 Function<void(int)> on_value_changed; 75 76protected: 77 explicit Slider(Orientation = Orientation::Vertical); 78 79 virtual void paint_event(PaintEvent&) override; 80 virtual void mousedown_event(MouseEvent&) override; 81 virtual void mousemove_event(MouseEvent&) override; 82 virtual void mouseup_event(MouseEvent&) override; 83 virtual void mousewheel_event(MouseEvent&) override; 84 virtual void leave_event(Core::Event&) override; 85 virtual void change_event(Event&) override; 86 87private: 88 void set_knob_hovered(bool); 89 90 int m_value { 0 }; 91 int m_min { 0 }; 92 int m_max { 100 }; 93 int m_step { 1 }; 94 bool m_knob_hovered { false }; 95 bool m_dragging { false }; 96 int m_drag_origin_value { 0 }; 97 Gfx::Point m_drag_origin; 98 KnobSizeMode m_knob_size_mode { KnobSizeMode::Fixed }; 99 Orientation m_orientation { Orientation::Horizontal }; 100}; 101 102class VerticalSlider final : public Slider { 103 C_OBJECT(VerticalSlider) 104public: 105 virtual ~VerticalSlider() override {} 106 107private: 108 VerticalSlider() 109 : Slider(Orientation::Vertical) 110 { 111 } 112}; 113 114class HorizontalSlider final : public Slider { 115 C_OBJECT(HorizontalSlider) 116public: 117 virtual ~HorizontalSlider() override {} 118 119private: 120 HorizontalSlider() 121 : Slider(Orientation::Horizontal) 122 { 123 } 124}; 125 126}