MyGUI 3.5.3
MyGUI_ScrollViewBase.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
9#include "MyGUI_ScrollBar.h"
10#include <algorithm>
11
12namespace MyGUI
13{
14
15 namespace
16 {
17
18 void updateScrollBar(ScrollBar* _scroll, int _viewSize, int _contentSize, size_t _range, size_t _page)
19 {
20 _scroll->setScrollPage(_page);
21 _scroll->setScrollViewPage(std::max(static_cast<size_t>(std::max(0, _viewSize)), _page));
22 _scroll->setScrollRange(_range + 1);
23 if (_contentSize != 0)
24 _scroll->setTrackSize(
25 static_cast<int>(static_cast<double>(_scroll->getLineSize()) * _viewSize / _contentSize));
26 }
27
28 }
29
30 void ScrollViewBase::updateScrollSize()
31 {
32 if (mScrollViewClient == nullptr)
33 return;
34
35 eraseContent();
36 IntSize contentSize = getContentSize();
37 IntSize viewSize = getViewSize();
38
39 const auto refreshSizes = [&]
40 {
41 if (mChangeContentByResize)
42 {
43 eraseContent();
44 contentSize = getContentSize();
45 }
46 viewSize = getViewSize();
47 };
48
49 const auto setVerticalVisible = [&](bool _visible)
50 {
51 if (mVScroll == nullptr || mVScroll->getVisible() == _visible)
52 return false;
53
54 const int delta = _visible ? -mVScroll->getWidth() : mVScroll->getWidth();
55 mVScroll->setVisible(_visible);
56 mScrollViewClient->setSize(mScrollViewClient->getWidth() + delta, mScrollViewClient->getHeight());
57 if (mHScroll != nullptr)
58 mHScroll->setSize(mHScroll->getWidth() + delta, mHScroll->getHeight());
59 refreshSizes();
60 return true;
61 };
62
63 const auto setHorizontalVisible = [&](bool _visible)
64 {
65 if (mHScroll == nullptr || mHScroll->getVisible() == _visible)
66 return false;
67
68 const int delta = _visible ? -mHScroll->getHeight() : mHScroll->getHeight();
69 mHScroll->setVisible(_visible);
70 mScrollViewClient->setSize(mScrollViewClient->getWidth(), mScrollViewClient->getHeight() + delta);
71 if (mVScroll != nullptr)
72 mVScroll->setSize(mVScroll->getWidth(), mVScroll->getHeight() + delta);
73 refreshSizes();
74 return true;
75 };
76
77 // Preserve the vertical-first layout pass and remeasure after each visibility change.
78 // Showing a scrollbar can require the other one; hiding it can make the other unnecessary.
79 const bool showVertical = mVisibleVScroll && contentSize.height > viewSize.height;
80 if (setVerticalVisible(showVertical))
81 {
82 const bool showHorizontal = mVisibleHScroll && contentSize.width > viewSize.width;
83 if (showHorizontal == showVertical)
84 setHorizontalVisible(showHorizontal);
85 }
86
87 const bool showHorizontal = mVisibleHScroll && contentSize.width > viewSize.width;
88 if (setHorizontalVisible(showHorizontal))
89 {
90 const bool showVerticalAfterResize = mVisibleVScroll && contentSize.height > viewSize.height;
91 if (showVerticalAfterResize == showHorizontal)
92 setVerticalVisible(showVerticalAfterResize);
93 }
94
95 mVRange = static_cast<size_t>(std::max(0, contentSize.height - viewSize.height));
96 mHRange = static_cast<size_t>(std::max(0, contentSize.width - viewSize.width));
97
98 if (mVScroll != nullptr)
99 updateScrollBar(mVScroll, viewSize.height, contentSize.height, mVRange, getVScrollPage());
100 if (mHScroll != nullptr)
101 updateScrollBar(mHScroll, viewSize.width, contentSize.width, mHRange, getHScrollPage());
102 }
103
104 void ScrollViewBase::updateScrollPosition()
105 {
106 const IntSize contentSize = getContentSize();
107 const IntPoint contentPoint = getContentPosition();
108 IntPoint offset = contentPoint;
109
110 const IntSize viewSize = getViewSize();
111
112 const Align align = getContentAlign();
113
114 if (contentSize.width > viewSize.width)
115 {
116 offset.left = std::clamp(offset.left, 0, contentSize.width - viewSize.width);
117 }
118 else
119 {
120 if (align.isLeft())
121 {
122 offset.left = 0;
123 }
124 else if (align.isRight())
125 {
126 offset.left = contentSize.width - viewSize.width;
127 }
128 else
129 {
130 offset.left = (contentSize.width - viewSize.width) / 2;
131 }
132 }
133
134 if (contentSize.height > viewSize.height)
135 {
136 offset.top = std::clamp(offset.top, 0, contentSize.height - viewSize.height);
137 }
138 else
139 {
140 if (align.isTop())
141 {
142 offset.top = 0;
143 }
144 else if (align.isBottom())
145 {
146 offset.top = contentSize.height - viewSize.height;
147 }
148 else
149 {
150 offset.top = (contentSize.height - viewSize.height) / 2;
151 }
152 }
153
154 if (offset != contentPoint)
155 {
156 if (mVScroll != nullptr)
157 mVScroll->setScrollPosition(offset.top);
158 if (mHScroll != nullptr)
159 mHScroll->setScrollPosition(offset.left);
160 setContentPosition(offset);
161 }
162 }
163
164 IntSize ScrollViewBase::getContentSize() const
165 {
166 return {};
167 }
168
169 IntPoint ScrollViewBase::getContentPosition() const
170 {
171 return {};
172 }
173
174 void ScrollViewBase::setContentPosition(const IntPoint& _value)
175 {
176 }
177
178 IntSize ScrollViewBase::getViewSize() const
179 {
180 return {};
181 }
182
183 size_t ScrollViewBase::getHScrollPage() const
184 {
185 return 1;
186 }
187
188 size_t ScrollViewBase::getVScrollPage() const
189 {
190 return 1;
191 }
192
193 Align ScrollViewBase::getContentAlign() const
194 {
195 return Align::Center;
196 }
197
198 void ScrollViewBase::eraseContent()
199 {
200 }
201
202} // namespace MyGUI
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TSize< int > IntSize
Definition MyGUI_Types.h:29