GScrollBar.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. public:
  7. explicit GScrollBar(Orientation, GWidget* parent);
  8. virtual ~GScrollBar() override;
  9. Orientation orientation() const { return m_orientation; }
  10. int value() const { return m_value; }
  11. int min() const { return m_min; }
  12. int max() const { return m_max; }
  13. int step() const { return m_step; }
  14. int big_step() const { return m_big_step; }
  15. void set_min(int min) { set_range(min, max()); }
  16. void set_max(int max) { set_range(min(), max); }
  17. void set_range(int min, int max);
  18. void set_value(int value);
  19. void set_step(int step) { m_step = step; }
  20. void set_big_step(int big_step) { m_big_step = big_step; }
  21. bool has_scrubber() const;
  22. Function<void(int)> on_change;
  23. virtual const char* class_name() const override { return "GScrollBar"; }
  24. enum Component
  25. {
  26. Invalid,
  27. DecrementButton,
  28. IncrementButton,
  29. Gutter,
  30. Scrubber,
  31. };
  32. private:
  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 leave_event(CEvent&) override;
  38. virtual void change_event(GEvent&) override;
  39. int button_size() const { return 16; }
  40. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  41. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  42. Rect decrement_button_rect() const;
  43. Rect increment_button_rect() const;
  44. Rect decrement_gutter_rect() const;
  45. Rect increment_gutter_rect() const;
  46. Rect scrubber_rect() const;
  47. int scrubber_size() const;
  48. int scrubbable_range_in_pixels() const;
  49. void on_automatic_scrolling_timer_fired();
  50. void set_automatic_scrolling_active(bool);
  51. int m_min { 0 };
  52. int m_max { 0 };
  53. int m_value { 0 };
  54. int m_step { 1 };
  55. int m_big_step { 5 };
  56. bool m_scrubbing { false };
  57. int m_scrub_start_value { 0 };
  58. Point m_scrub_origin;
  59. Orientation m_orientation { Orientation::Vertical };
  60. Component m_hovered_component { Component::Invalid };
  61. enum class AutomaticScrollingDirection
  62. {
  63. None = 0,
  64. Decrement,
  65. Increment,
  66. };
  67. AutomaticScrollingDirection m_automatic_scrolling_direction { AutomaticScrollingDirection::None };
  68. CTimer m_automatic_scrolling_timer;
  69. };