ScrollBar.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 page() const { return m_page; }
  40. int step() const { return m_step; }
  41. int big_step() const { return m_big_step; }
  42. void set_min(int min) { set_range(min, max(), page()); }
  43. void set_max(int max) { set_range(min(), max, page()); }
  44. void set_page(int page) { set_range(min(), max(), page); }
  45. void set_range(int min, int max) { set_range(min, max, page()); }
  46. void set_range(int min, int max, int page);
  47. void set_value(int value);
  48. void set_step(int step) { m_step = step; }
  49. void set_big_step(int big_step) { m_big_step = big_step; }
  50. bool has_scrubber() const;
  51. Function<void(int)> on_change;
  52. enum Component {
  53. None,
  54. DecrementButton,
  55. IncrementButton,
  56. Gutter,
  57. Scrubber,
  58. };
  59. private:
  60. explicit ScrollBar(Gfx::Orientation = Gfx::Orientation::Vertical);
  61. virtual void paint_event(PaintEvent&) override;
  62. virtual void mousedown_event(MouseEvent&) override;
  63. virtual void mouseup_event(MouseEvent&) override;
  64. virtual void mousemove_event(MouseEvent&) override;
  65. virtual void mousewheel_event(MouseEvent&) override;
  66. virtual void leave_event(Core::Event&) override;
  67. virtual void change_event(Event&) override;
  68. int default_button_size() const { return 16; }
  69. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  70. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  71. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  72. Gfx::IntRect decrement_button_rect() const;
  73. Gfx::IntRect increment_button_rect() const;
  74. Gfx::IntRect decrement_gutter_rect() const;
  75. Gfx::IntRect increment_gutter_rect() const;
  76. Gfx::IntRect scrubber_rect() const;
  77. int unclamped_scrubber_size() const;
  78. int visible_scrubber_size() const;
  79. int scrubbable_range_in_pixels() const;
  80. void on_automatic_scrolling_timer_fired();
  81. void set_automatic_scrolling_active(bool, Component);
  82. void scroll_to_position(const Gfx::IntPoint&);
  83. void scroll_by_page(const Gfx::IntPoint&);
  84. Component component_at_position(const Gfx::IntPoint&);
  85. int m_min { 0 };
  86. int m_max { 0 };
  87. int m_page { 0 };
  88. int m_value { 0 };
  89. int m_step { 1 };
  90. int m_big_step { 5 };
  91. int m_scrub_start_value { 0 };
  92. Gfx::IntPoint m_scrub_origin;
  93. Gfx::Orientation m_orientation { Gfx::Orientation::Vertical };
  94. Component m_hovered_component { Component::None };
  95. Component m_pressed_component { Component::None };
  96. Gfx::IntPoint m_last_mouse_position;
  97. RefPtr<Core::Timer> m_automatic_scrolling_timer;
  98. };
  99. }