ScrollBar.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #pragma once
  27. #include <AK/Function.h>
  28. #include <LibGUI/Widget.h>
  29. namespace GUI {
  30. class ScrollBar final : public Widget {
  31. C_OBJECT(ScrollBar)
  32. public:
  33. virtual ~ScrollBar() override;
  34. Gfx::Orientation orientation() const { return m_orientation; }
  35. bool is_scrollable() const { return max() != min(); }
  36. int value() const { return m_value; }
  37. int min() const { return m_min; }
  38. int max() const { return m_max; }
  39. int step() const { return m_step; }
  40. int big_step() const { return m_big_step; }
  41. void set_min(int min) { set_range(min, max()); }
  42. void set_max(int max) { set_range(min(), max); }
  43. void set_range(int min, int max);
  44. void set_value(int value);
  45. void set_step(int step) { m_step = step; }
  46. void set_big_step(int big_step) { m_big_step = big_step; }
  47. bool has_scrubber() const;
  48. Function<void(int)> on_change;
  49. enum Component {
  50. Invalid,
  51. DecrementButton,
  52. IncrementButton,
  53. Gutter,
  54. Scrubber,
  55. };
  56. private:
  57. explicit ScrollBar(Gfx::Orientation = Gfx::Orientation::Vertical);
  58. virtual void paint_event(PaintEvent&) override;
  59. virtual void mousedown_event(MouseEvent&) override;
  60. virtual void mouseup_event(MouseEvent&) override;
  61. virtual void mousemove_event(MouseEvent&) override;
  62. virtual void mousewheel_event(MouseEvent&) override;
  63. virtual void leave_event(Core::Event&) override;
  64. virtual void change_event(Event&) override;
  65. int default_button_size() const { return 16; }
  66. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  67. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  68. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  69. Gfx::Rect decrement_button_rect() const;
  70. Gfx::Rect increment_button_rect() const;
  71. Gfx::Rect decrement_gutter_rect() const;
  72. Gfx::Rect increment_gutter_rect() const;
  73. Gfx::Rect scrubber_rect() const;
  74. int scrubber_size() const;
  75. int scrubbable_range_in_pixels() const;
  76. void on_automatic_scrolling_timer_fired();
  77. void set_automatic_scrolling_active(bool);
  78. int m_min { 0 };
  79. int m_max { 0 };
  80. int m_value { 0 };
  81. int m_step { 1 };
  82. int m_big_step { 5 };
  83. bool m_scrubbing { false };
  84. int m_scrub_start_value { 0 };
  85. Gfx::Point m_scrub_origin;
  86. Gfx::Orientation m_orientation { Gfx::Orientation::Vertical };
  87. Component m_hovered_component { Component::Invalid };
  88. bool m_scrubber_in_use { false };
  89. enum class AutomaticScrollingDirection {
  90. None = 0,
  91. Decrement,
  92. Increment,
  93. };
  94. AutomaticScrollingDirection m_automatic_scrolling_direction { AutomaticScrollingDirection::None };
  95. RefPtr<Core::Timer> m_automatic_scrolling_timer;
  96. };
  97. }