GScrollBar.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <AK/Function.h>
  3. #include <LibCore/CTimer.h>
  4. #include <LibGUI/GWidget.h>
  5. class GScrollBar final : public GWidget {
  6. C_OBJECT(GScrollBar)
  7. public:
  8. virtual ~GScrollBar() override;
  9. Orientation orientation() const { return m_orientation; }
  10. bool is_scrollable() const { return max() != min(); }
  11. int value() const { return m_value; }
  12. int min() const { return m_min; }
  13. int max() const { return m_max; }
  14. int step() const { return m_step; }
  15. int big_step() const { return m_big_step; }
  16. void set_min(int min) { set_range(min, max()); }
  17. void set_max(int max) { set_range(min(), max); }
  18. void set_range(int min, int max);
  19. void set_value(int value);
  20. void set_step(int step) { m_step = step; }
  21. void set_big_step(int big_step) { m_big_step = big_step; }
  22. bool has_scrubber() const;
  23. Function<void(int)> on_change;
  24. enum Component {
  25. Invalid,
  26. DecrementButton,
  27. IncrementButton,
  28. Gutter,
  29. Scrubber,
  30. };
  31. private:
  32. explicit GScrollBar(GWidget* parent);
  33. explicit GScrollBar(Orientation, GWidget* parent);
  34. virtual void paint_event(GPaintEvent&) override;
  35. virtual void mousedown_event(GMouseEvent&) override;
  36. virtual void mouseup_event(GMouseEvent&) override;
  37. virtual void mousemove_event(GMouseEvent&) override;
  38. virtual void mousewheel_event(GMouseEvent&) override;
  39. virtual void leave_event(CEvent&) override;
  40. virtual void change_event(GEvent&) override;
  41. int default_button_size() const { return 16; }
  42. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  43. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  44. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  45. Rect decrement_button_rect() const;
  46. Rect increment_button_rect() const;
  47. Rect decrement_gutter_rect() const;
  48. Rect increment_gutter_rect() const;
  49. Rect scrubber_rect() const;
  50. int scrubber_size() const;
  51. int scrubbable_range_in_pixels() const;
  52. void on_automatic_scrolling_timer_fired();
  53. void set_automatic_scrolling_active(bool);
  54. int m_min { 0 };
  55. int m_max { 0 };
  56. int m_value { 0 };
  57. int m_step { 1 };
  58. int m_big_step { 5 };
  59. bool m_scrubbing { false };
  60. int m_scrub_start_value { 0 };
  61. Point m_scrub_origin;
  62. Orientation m_orientation { Orientation::Vertical };
  63. Component m_hovered_component { Component::Invalid };
  64. bool m_scrubber_in_use { false };
  65. enum class AutomaticScrollingDirection {
  66. None = 0,
  67. Decrement,
  68. Increment,
  69. };
  70. AutomaticScrollingDirection m_automatic_scrolling_direction { AutomaticScrollingDirection::None };
  71. RefPtr<CTimer> m_automatic_scrolling_timer;
  72. };