AbstractScrollableWidget.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/Frame.h>
  8. #include <LibGUI/Scrollbar.h>
  9. namespace GUI {
  10. class AbstractScrollableWidget : public Frame {
  11. C_OBJECT_ABSTRACT(AbstractScrollableWidget);
  12. public:
  13. virtual ~AbstractScrollableWidget() override;
  14. Gfx::IntSize content_size() const { return m_content_size; }
  15. int content_width() const { return m_content_size.width(); }
  16. int content_height() const { return m_content_size.height(); }
  17. Gfx::IntRect visible_content_rect() const;
  18. Gfx::IntRect widget_inner_rect() const;
  19. Gfx::IntRect viewport_rect_in_content_coordinates() const
  20. {
  21. auto viewport_rect = visible_content_rect();
  22. viewport_rect.set_size(widget_inner_rect().size());
  23. return viewport_rect;
  24. }
  25. void scroll_into_view(const Gfx::IntRect&, Orientation);
  26. void scroll_into_view(const Gfx::IntRect&, bool scroll_horizontally, bool scroll_vertically);
  27. void set_scrollbars_enabled(bool);
  28. bool is_scrollbars_enabled() const { return m_scrollbars_enabled; }
  29. Gfx::IntSize available_size() const;
  30. Gfx::IntSize excess_size() const;
  31. Scrollbar& vertical_scrollbar() { return *m_vertical_scrollbar; }
  32. const Scrollbar& vertical_scrollbar() const { return *m_vertical_scrollbar; }
  33. Scrollbar& horizontal_scrollbar() { return *m_horizontal_scrollbar; }
  34. const Scrollbar& horizontal_scrollbar() const { return *m_horizontal_scrollbar; }
  35. Widget& corner_widget() { return *m_corner_widget; }
  36. const Widget& corner_widget() const { return *m_corner_widget; }
  37. void scroll_to_top();
  38. void scroll_to_bottom();
  39. void set_automatic_scrolling_timer(bool active);
  40. virtual Gfx::IntPoint automatic_scroll_delta_from_position(const Gfx::IntPoint&) const;
  41. int width_occupied_by_vertical_scrollbar() const;
  42. int height_occupied_by_horizontal_scrollbar() const;
  43. void set_should_hide_unnecessary_scrollbars(bool b) { m_should_hide_unnecessary_scrollbars = b; }
  44. bool should_hide_unnecessary_scrollbars() const { return m_should_hide_unnecessary_scrollbars; }
  45. Gfx::IntPoint to_content_position(const Gfx::IntPoint& widget_position) const;
  46. Gfx::IntPoint to_widget_position(const Gfx::IntPoint& content_position) const;
  47. Gfx::IntRect to_content_rect(const Gfx::IntRect& widget_rect) const { return { to_content_position(widget_rect.location()), widget_rect.size() }; }
  48. Gfx::IntRect to_widget_rect(const Gfx::IntRect& content_rect) const { return { to_widget_position(content_rect.location()), content_rect.size() }; }
  49. protected:
  50. AbstractScrollableWidget();
  51. virtual void custom_layout() override;
  52. virtual void resize_event(ResizeEvent&) override;
  53. virtual void mousewheel_event(MouseEvent&) override;
  54. virtual void did_scroll() { }
  55. void set_content_size(const Gfx::IntSize&);
  56. void set_size_occupied_by_fixed_elements(const Gfx::IntSize&);
  57. virtual void on_automatic_scrolling_timer_fired() {};
  58. int autoscroll_threshold() const { return m_autoscroll_threshold; }
  59. private:
  60. class AbstractScrollableWidgetScrollbar final : public Scrollbar {
  61. C_OBJECT(AbstractScrollableWidgetScrollbar);
  62. protected:
  63. explicit AbstractScrollableWidgetScrollbar(AbstractScrollableWidget& owner, Gfx::Orientation orientation)
  64. : Scrollbar(orientation)
  65. , m_owner(owner)
  66. {
  67. }
  68. virtual void mousewheel_event(MouseEvent& event) override
  69. {
  70. m_owner.handle_wheel_event(event, *this);
  71. }
  72. private:
  73. AbstractScrollableWidget& m_owner;
  74. };
  75. friend class ScrollableWidgetScrollbar;
  76. void update_scrollbar_ranges();
  77. void handle_wheel_event(MouseEvent&, Widget&);
  78. RefPtr<AbstractScrollableWidgetScrollbar> m_vertical_scrollbar;
  79. RefPtr<AbstractScrollableWidgetScrollbar> m_horizontal_scrollbar;
  80. RefPtr<Widget> m_corner_widget;
  81. Gfx::IntSize m_content_size;
  82. Gfx::IntSize m_size_occupied_by_fixed_elements;
  83. bool m_scrollbars_enabled { true };
  84. bool m_should_hide_unnecessary_scrollbars { false };
  85. RefPtr<Core::Timer> m_automatic_scrolling_timer;
  86. bool m_active_scrolling_enabled { false };
  87. int m_autoscroll_threshold { 20 };
  88. };
  89. }