GScrollBar.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(Orientation, GWidget* parent);
  33. virtual void paint_event(GPaintEvent&) override;
  34. virtual void mousedown_event(GMouseEvent&) override;
  35. virtual void mouseup_event(GMouseEvent&) override;
  36. virtual void mousemove_event(GMouseEvent&) override;
  37. virtual void mousewheel_event(GMouseEvent&) override;
  38. virtual void leave_event(CEvent&) override;
  39. virtual void change_event(GEvent&) override;
  40. int default_button_size() const { return 16; }
  41. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  42. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  43. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  44. Rect decrement_button_rect() const;
  45. Rect increment_button_rect() const;
  46. Rect decrement_gutter_rect() const;
  47. Rect increment_gutter_rect() const;
  48. Rect scrubber_rect() const;
  49. int scrubber_size() const;
  50. int scrubbable_range_in_pixels() const;
  51. void on_automatic_scrolling_timer_fired();
  52. void set_automatic_scrolling_active(bool);
  53. int m_min { 0 };
  54. int m_max { 0 };
  55. int m_value { 0 };
  56. int m_step { 1 };
  57. int m_big_step { 5 };
  58. bool m_scrubbing { false };
  59. int m_scrub_start_value { 0 };
  60. Point m_scrub_origin;
  61. Orientation m_orientation { Orientation::Vertical };
  62. Component m_hovered_component { Component::Invalid };
  63. bool m_scrubber_in_use { false };
  64. enum class AutomaticScrollingDirection {
  65. None = 0,
  66. Decrement,
  67. Increment,
  68. };
  69. AutomaticScrollingDirection m_automatic_scrolling_direction { AutomaticScrollingDirection::None };
  70. ObjectPtr<CTimer> m_automatic_scrolling_timer;
  71. };