Scrollbar.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <LibGUI/AbstractSlider.h>
  9. namespace GUI {
  10. class Scrollbar : public AbstractSlider {
  11. C_OBJECT(Scrollbar);
  12. public:
  13. virtual ~Scrollbar() override;
  14. bool is_scrollable() const { return max() != min(); }
  15. bool has_scrubber() const;
  16. enum Component {
  17. None,
  18. DecrementButton,
  19. IncrementButton,
  20. Gutter,
  21. Scrubber,
  22. };
  23. protected:
  24. explicit Scrollbar(Gfx::Orientation = Gfx::Orientation::Vertical);
  25. virtual void paint_event(PaintEvent&) override;
  26. virtual void mousedown_event(MouseEvent&) override;
  27. virtual void mouseup_event(MouseEvent&) override;
  28. virtual void mousemove_event(MouseEvent&) override;
  29. virtual void mousewheel_event(MouseEvent&) override;
  30. virtual void leave_event(Core::Event&) override;
  31. virtual void change_event(Event&) override;
  32. private:
  33. int default_button_size() const { return 16; }
  34. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  35. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  36. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  37. Gfx::IntRect decrement_button_rect() const;
  38. Gfx::IntRect increment_button_rect() const;
  39. Gfx::IntRect scrubber_rect() const;
  40. float unclamped_scrubber_size() const;
  41. int visible_scrubber_size() const;
  42. int scrubbable_range_in_pixels() const;
  43. void on_automatic_scrolling_timer_fired();
  44. void set_automatic_scrolling_active(bool, Component);
  45. void scroll_to_position(const Gfx::IntPoint&);
  46. void scroll_by_page(const Gfx::IntPoint&);
  47. Component component_at_position(const Gfx::IntPoint&);
  48. int m_scrub_start_value { 0 };
  49. Gfx::IntPoint m_scrub_origin;
  50. Component m_hovered_component { Component::None };
  51. Component m_pressed_component { Component::None };
  52. Gfx::IntPoint m_last_mouse_position;
  53. RefPtr<Core::Timer> m_automatic_scrolling_timer;
  54. };
  55. }