MyGUI 3.5.3
MyGUI_ScrollView.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"
8#include "MyGUI_ScrollView.h"
9#include "MyGUI_SkinManager.h"
11#include "MyGUI_ScrollBar.h"
12
13namespace MyGUI
14{
15
16 const int SCROLL_VIEW_MOUSE_WHEEL = 50; // pixels for mouse wheel
17 const int SCROLL_VIEW_SCROLL_PAGE = 16; // pixels for scroll buttons
18
19 ScrollView::ScrollView() :
20 mContentAlign(Align::Center)
21 {
22 mChangeContentByResize = false;
23 }
24
25 void ScrollView::initialiseOverride()
26 {
27 Base::initialiseOverride();
28
29 // FIXME we need keyboard focus
30 setNeedKeyFocus(true);
31
32 assignWidget(mScrollViewClient, "Client");
33 if (getClientWidget() != nullptr)
34 {
35 getClientWidget()->eventMouseWheel += newDelegate(this, &ScrollView::notifyMouseWheel);
36 }
37
38 // create widget that will be real parent for child widgets
39 Widget* realClient = _getClientWidget()->createWidget<Widget>("Default", IntCoord(), Align::Default);
40 realClient->eventMouseWheel += newDelegate(this, &ScrollView::notifyMouseWheel);
41 setWidgetClient(realClient);
42
44 assignWidget(mVScroll, "VScroll");
45 if (mVScroll != nullptr)
46 {
47 mVScroll->eventScrollChangePosition += newDelegate(this, &ScrollView::notifyScrollChangePosition);
48 }
49
51 assignWidget(mHScroll, "HScroll");
52 if (mHScroll != nullptr)
53 {
54 mHScroll->eventScrollChangePosition += newDelegate(this, &ScrollView::notifyScrollChangePosition);
55 }
56
57 updateView();
58 }
59
60 void ScrollView::shutdownOverride()
61 {
62 mVScroll = nullptr;
63 mHScroll = nullptr;
64 mScrollViewClient = nullptr;
65
66 Base::shutdownOverride();
67 }
68
69 void ScrollView::setPosition(const IntPoint& _point)
70 {
71 Base::setPosition(_point);
72 }
73
74 void ScrollView::setSize(const IntSize& _size)
75 {
76 Base::setSize(_size);
77
78 updateView();
79 }
80
81 void ScrollView::setCoord(const IntCoord& _coord)
82 {
83 Base::setCoord(_coord);
84
85 updateView();
86 }
87
88 void ScrollView::notifyScrollChangePosition(ScrollBar* _sender, size_t _position)
89 {
90 if (_sender == mVScroll)
91 {
92 IntPoint point = getClientWidget()->getPosition();
93 point.top = -(int)_position;
94 getClientWidget()->setPosition(point);
95 }
96 else if (_sender == mHScroll)
97 {
98 IntPoint point = getClientWidget()->getPosition();
99 point.left = -(int)_position;
100 getClientWidget()->setPosition(point);
101 }
102 }
103
104 void ScrollView::notifyMouseWheel(Widget* _sender, int _rel)
105 {
106 if (mVRange != 0)
107 {
108 IntPoint point = getClientWidget()->getPosition();
109 int offset = -point.top;
110 if (_rel < 0)
111 offset += SCROLL_VIEW_MOUSE_WHEEL;
112 else
113 offset -= SCROLL_VIEW_MOUSE_WHEEL;
114
115 if (offset < 0)
116 offset = 0;
117 else if (offset > (int)mVRange)
118 offset = mVRange;
119
120 if (offset != point.top)
121 {
122 point.top = -offset;
123 if (mVScroll != nullptr)
124 {
125 mVScroll->setScrollPosition(offset);
126 }
127 getClientWidget()->setPosition(point);
128 }
129 }
130 else if (mHRange != 0)
131 {
132 IntPoint point = getClientWidget()->getPosition();
133 int offset = -point.left;
134 if (_rel < 0)
135 offset += SCROLL_VIEW_MOUSE_WHEEL;
136 else
137 offset -= SCROLL_VIEW_MOUSE_WHEEL;
138
139 if (offset < 0)
140 offset = 0;
141 else if (offset > (int)mHRange)
142 offset = mHRange;
143
144 if (offset != point.left)
145 {
146 point.left = -offset;
147 if (mHScroll != nullptr)
148 {
149 mHScroll->setScrollPosition(offset);
150 }
151 getClientWidget()->setPosition(point);
152 }
153 }
154 }
155
156 IntSize ScrollView::getContentSize() const
157 {
158 return getClientWidget()->getSize();
159 }
160
161 IntPoint ScrollView::getContentPosition() const
162 {
163 return IntPoint() - getClientWidget()->getPosition();
164 }
165
166 void ScrollView::setContentPosition(const IntPoint& _point)
167 {
168 getClientWidget()->setPosition(IntPoint() - _point);
169 }
170
171 IntSize ScrollView::getViewSize() const
172 {
173 return mScrollViewClient == nullptr ? getSize() : mScrollViewClient->getSize();
174 }
175
176 size_t ScrollView::getVScrollPage() const
177 {
179 }
180
181 size_t ScrollView::getHScrollPage() const
182 {
184 }
185
186 void ScrollView::updateView()
187 {
188 updateScrollSize();
189 updateScrollPosition();
190 }
191
192 void ScrollView::setVisibleVScroll(bool _value)
193 {
194 mVisibleVScroll = _value;
195 updateView();
196 }
197
198 void ScrollView::setVisibleHScroll(bool _value)
199 {
200 mVisibleHScroll = _value;
201 updateView();
202 }
203
204 void ScrollView::setCanvasAlign(Align _value)
205 {
206 mContentAlign = _value;
207 updateView();
208 }
209
210 void ScrollView::setCanvasSize(const IntSize& _value)
211 {
212 getClientWidget()->setSize(_value);
213 updateView();
214 }
215
216 IntSize ScrollView::getCanvasSize() const
217 {
218 return getClientWidget()->getSize();
219 }
220
221 void ScrollView::setPropertyOverride(std::string_view _key, std::string_view _value)
222 {
224 if (_key == "VisibleVScroll")
225 setVisibleVScroll(utility::parseValue<bool>(_value));
226
228 else if (_key == "VisibleHScroll")
229 setVisibleHScroll(utility::parseValue<bool>(_value));
230
232 else if (_key == "CanvasAlign")
233 setCanvasAlign(utility::parseValue<Align>(_value));
234
236 else if (_key == "CanvasSize")
237 setCanvasSize(utility::parseValue<IntSize>(_value));
238
239 else
240 {
241 Base::setPropertyOverride(_key, _value);
242 return;
243 }
244
245 eventChangeProperty(this, _key, _value);
246 }
247
248 bool ScrollView::isVisibleVScroll() const
249 {
250 return mVisibleVScroll;
251 }
252
253 bool ScrollView::isVisibleHScroll() const
254 {
255 return mVisibleHScroll;
256 }
257
258 Align ScrollView::getCanvasAlign() const
259 {
260 return mContentAlign;
261 }
262
263 void ScrollView::setCanvasSize(int _width, int _height)
264 {
265 setCanvasSize(IntSize(_width, _height));
266 }
267
268 Align ScrollView::getContentAlign() const
269 {
270 return mContentAlign;
271 }
272
273 void ScrollView::setViewOffset(const IntPoint& _value)
274 {
275 IntPoint value = _value;
276 IntPoint currentOffset = getClientWidget()->getPosition();
277
278 if (mHRange != 0)
279 {
280 if (value.left > 0)
281 value.left = 0;
282 else if (value.left < -(int)mHRange)
283 value.left = -(int)mHRange;
284 }
285 else
286 {
287 value.left = currentOffset.left;
288 }
289
290 if (mVRange != 0)
291 {
292 if (value.top > 0)
293 value.top = 0;
294 else if (value.top < -(int)mVRange)
295 value.top = -(int)mVRange;
296 }
297 else
298 {
299 value.top = currentOffset.top;
300 }
301
302 if (mHScroll != nullptr)
303 mHScroll->setScrollPosition(-value.left);
304
305 if (mVScroll != nullptr)
306 mVScroll->setScrollPosition(-value.top);
307
308 getClientWidget()->setPosition(value);
309 }
310
311 IntPoint ScrollView::getViewOffset() const
312 {
313 return getClientWidget()->getPosition();
314 }
315
316 IntCoord ScrollView::getViewCoord() const
317 {
318 return mScrollViewClient == nullptr ? getCoord() : mScrollViewClient->getCoord();
319 }
320
321 ScrollBar* ScrollView::getVScroll() const
322 {
323 return mVScroll;
324 }
325
326} // namespace MyGUI
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
const int SCROLL_VIEW_MOUSE_WHEEL
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
const int SCROLL_VIEW_SCROLL_PAGE
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))