Serenity Operating System
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
27#pragma once
28
29#include <LibGUI/Frame.h>
30
31namespace GUI {
32
33class ScrollableWidget : public Frame {
34 C_OBJECT(ScrollableWidget)
35public:
36 virtual ~ScrollableWidget() override;
37
38 Gfx::Size content_size() const { return m_content_size; }
39 int content_width() const { return m_content_size.width(); }
40 int content_height() const { return m_content_size.height(); }
41
42 Gfx::Rect visible_content_rect() const;
43
44 Gfx::Rect widget_inner_rect() const;
45
46 void scroll_into_view(const Gfx::Rect&, Orientation);
47 void scroll_into_view(const Gfx::Rect&, bool scroll_horizontally, bool scroll_vertically);
48
49 void set_scrollbars_enabled(bool);
50 bool is_scrollbars_enabled() const { return m_scrollbars_enabled; }
51
52 Gfx::Size available_size() const;
53
54 ScrollBar& vertical_scrollbar() { return *m_vertical_scrollbar; }
55 const ScrollBar& vertical_scrollbar() const { return *m_vertical_scrollbar; }
56 ScrollBar& horizontal_scrollbar() { return *m_horizontal_scrollbar; }
57 const ScrollBar& horizontal_scrollbar() const { return *m_horizontal_scrollbar; }
58 Widget& corner_widget() { return *m_corner_widget; }
59 const Widget& corner_widget() const { return *m_corner_widget; }
60
61 void scroll_to_top();
62 void scroll_to_bottom();
63
64 int width_occupied_by_vertical_scrollbar() const;
65 int height_occupied_by_horizontal_scrollbar() const;
66
67 void set_should_hide_unnecessary_scrollbars(bool b) { m_should_hide_unnecessary_scrollbars = b; }
68 bool should_hide_unnecessary_scrollbars() const { return m_should_hide_unnecessary_scrollbars; }
69
70 Gfx::Point to_content_position(const Gfx::Point& widget_position) const;
71 Gfx::Point to_widget_position(const Gfx::Point& content_position) const;
72
73protected:
74 ScrollableWidget();
75 virtual void custom_layout() override;
76 virtual void resize_event(ResizeEvent&) override;
77 virtual void mousewheel_event(MouseEvent&) override;
78 virtual void did_scroll() {}
79 void set_content_size(const Gfx::Size&);
80 void set_size_occupied_by_fixed_elements(const Gfx::Size&);
81
82private:
83 void update_scrollbar_ranges();
84
85 RefPtr<ScrollBar> m_vertical_scrollbar;
86 RefPtr<ScrollBar> m_horizontal_scrollbar;
87 RefPtr<Widget> m_corner_widget;
88 Gfx::Size m_content_size;
89 Gfx::Size m_size_occupied_by_fixed_elements;
90 bool m_scrollbars_enabled { true };
91 bool m_should_hide_unnecessary_scrollbars { false };
92};
93
94}