ScrollableWidget.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibGUI/Frame.h>
  28. namespace GUI {
  29. class ScrollableWidget : public Frame {
  30. C_OBJECT(ScrollableWidget)
  31. public:
  32. virtual ~ScrollableWidget() override;
  33. Gfx::Size content_size() const { return m_content_size; }
  34. int content_width() const { return m_content_size.width(); }
  35. int content_height() const { return m_content_size.height(); }
  36. Gfx::Rect visible_content_rect() const;
  37. Gfx::Rect widget_inner_rect() const;
  38. void scroll_into_view(const Gfx::Rect&, Orientation);
  39. void scroll_into_view(const Gfx::Rect&, bool scroll_horizontally, bool scroll_vertically);
  40. void set_scrollbars_enabled(bool);
  41. bool is_scrollbars_enabled() const { return m_scrollbars_enabled; }
  42. Gfx::Size available_size() const;
  43. ScrollBar& vertical_scrollbar() { return *m_vertical_scrollbar; }
  44. const ScrollBar& vertical_scrollbar() const { return *m_vertical_scrollbar; }
  45. ScrollBar& horizontal_scrollbar() { return *m_horizontal_scrollbar; }
  46. const ScrollBar& horizontal_scrollbar() const { return *m_horizontal_scrollbar; }
  47. Widget& corner_widget() { return *m_corner_widget; }
  48. const Widget& corner_widget() const { return *m_corner_widget; }
  49. void scroll_to_top();
  50. void scroll_to_bottom();
  51. int width_occupied_by_vertical_scrollbar() const;
  52. int height_occupied_by_horizontal_scrollbar() const;
  53. void set_should_hide_unnecessary_scrollbars(bool b) { m_should_hide_unnecessary_scrollbars = b; }
  54. bool should_hide_unnecessary_scrollbars() const { return m_should_hide_unnecessary_scrollbars; }
  55. Gfx::Point to_content_position(const Gfx::Point& widget_position) const;
  56. Gfx::Point to_widget_position(const Gfx::Point& content_position) const;
  57. protected:
  58. ScrollableWidget();
  59. virtual void custom_layout() override;
  60. virtual void resize_event(ResizeEvent&) override;
  61. virtual void mousewheel_event(MouseEvent&) override;
  62. virtual void did_scroll() {}
  63. void set_content_size(const Gfx::Size&);
  64. void set_size_occupied_by_fixed_elements(const Gfx::Size&);
  65. private:
  66. void update_scrollbar_ranges();
  67. RefPtr<ScrollBar> m_vertical_scrollbar;
  68. RefPtr<ScrollBar> m_horizontal_scrollbar;
  69. RefPtr<Widget> m_corner_widget;
  70. Gfx::Size m_content_size;
  71. Gfx::Size m_size_occupied_by_fixed_elements;
  72. bool m_scrollbars_enabled { true };
  73. bool m_should_hide_unnecessary_scrollbars { false };
  74. };
  75. }