MyGUI  3.0.1
MyGUI_ScrollView.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_ScrollView.h"
25 #include "MyGUI_SkinManager.h"
26 #include "MyGUI_ISubWidgetText.h"
27 #include "MyGUI_VScroll.h"
28 #include "MyGUI_HScroll.h"
29 
30 namespace MyGUI
31 {
32 
33  const int SCROLL_VIEW_MOUSE_WHEEL = 50; // колличество пикселей для колеса мыши
34  const int SCROLL_VIEW_SCROLL_PAGE = 16; // колличество пикселей для кнопок скрола
35 
37  mIsFocus(false),
38  mIsPressed(false),
39  mScrollClient(nullptr),
40  mContentAlign(Align::Center)
41  {
42  mChangeContentByResize = false;
44  }
45 
46  void ScrollView::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
47  {
48  Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
49 
50  initialiseWidgetSkin(_info);
51  }
52 
54  {
55  shutdownWidgetSkin();
56  }
57 
59  {
60  shutdownWidgetSkin();
62  initialiseWidgetSkin(_info);
63  }
64 
65  void ScrollView::initialiseWidgetSkin(ResourceSkin* _info)
66  {
67  // нам нужен фокус клавы
68  mNeedKeyFocus = true;
69 
70  for (VectorWidgetPtr::iterator iter=mWidgetChildSkin.begin(); iter!=mWidgetChildSkin.end(); ++iter)
71  {
72  if (*(*iter)->_getInternalData<std::string>() == "Client")
73  {
74  MYGUI_DEBUG_ASSERT( ! mScrollClient, "widget already assigned");
75  mScrollClient = (*iter);
80 
81  // создаем холcт, реальный владелец детей
86  }
87  else if (*(*iter)->_getInternalData<std::string>() == "VScroll")
88  {
89  MYGUI_DEBUG_ASSERT( ! mVScroll, "widget already assigned");
90  mVScroll = (*iter)->castType<VScroll>();
92  }
93  else if (*(*iter)->_getInternalData<std::string>() == "HScroll")
94  {
95  MYGUI_DEBUG_ASSERT( ! mHScroll, "widget already assigned");
96  mHScroll = (*iter)->castType<HScroll>();
98  }
99  }
100 
101  //MYGUI_ASSERT(nullptr != mScrollClient, "Child Widget Client not found in skin (ScrollView must have Client)");
102 
103  updateView();
104  }
105 
106  void ScrollView::shutdownWidgetSkin()
107  {
108  mWidgetClient = nullptr;
109  mVScroll = nullptr;
110  mHScroll = nullptr;
111  mScrollClient = nullptr;
112  }
113 
115  {
116  if ((_old == mScrollClient) || (mIsFocus))
117  return;
118 
119  mIsFocus = true;
121  }
122 
124  {
125  if ((_new == mScrollClient) || (!mIsFocus))
126  return;
127 
128  mIsFocus = false;
130  }
131 
133  {
134  if (!mIsPressed)
135  {
136  mIsPressed = true;
138  }
139 
140  Base::onKeySetFocus(_old);
141  }
142 
144  {
145  if (mIsPressed)
146  {
147  mIsPressed = false;
149  }
150 
151  Base::onKeyLostFocus(_new);
152  }
153 
155  {
156  if (!mEnabled) setState("disabled");
157  else if (mIsPressed)
158  {
159  if (mIsFocus) setState("pushed");
160  else setState("normal_checked");
161  }
162  else if (mIsFocus) setState("highlighted");
163  else setState("normal");
164  }
165 
166  void ScrollView::setPosition(const IntPoint& _point)
167  {
168  Base::setPosition(_point);
169  }
170 
171  void ScrollView::setSize(const IntSize& _size)
172  {
173  Base::setSize(_size);
174 
175  updateView();
176  }
177 
178  void ScrollView::setCoord(const IntCoord& _coord)
179  {
180  Base::setCoord(_coord);
181 
182  updateView();
183  }
184 
185  void ScrollView::notifyScrollChangePosition(VScroll* _sender, size_t _position)
186  {
187  if (mWidgetClient == nullptr)
188  return;
189 
190  if (_sender == mVScroll)
191  {
193  point.top = -(int)_position;
194  mWidgetClient->setPosition(point);
195  }
196  else if (_sender == mHScroll)
197  {
199  point.left = -(int)_position;
200  mWidgetClient->setPosition(point);
201  }
202  }
203 
204  void ScrollView::notifyMouseWheel(Widget* _sender, int _rel)
205  {
206  if (mWidgetClient == nullptr)
207  return;
208 
209  if (mVRange != 0)
210  {
212  int offset = -point.top;
213  if (_rel < 0) offset += SCROLL_VIEW_MOUSE_WHEEL;
214  else offset -= SCROLL_VIEW_MOUSE_WHEEL;
215 
216  if (offset < 0) offset = 0;
217  else if (offset > (int)mVRange) offset = mVRange;
218 
219  if (offset != point.top)
220  {
221  point.top = -offset;
222  if (mVScroll != nullptr)
223  {
224  mVScroll->setScrollPosition(offset);
225  }
226  mWidgetClient->setPosition(point);
227  }
228  }
229  else if (mHRange != 0)
230  {
232  int offset = -point.left;
233  if (_rel < 0) offset += SCROLL_VIEW_MOUSE_WHEEL;
234  else offset -= SCROLL_VIEW_MOUSE_WHEEL;
235 
236  if (offset < 0) offset = 0;
237  else if (offset > (int)mHRange) offset = mHRange;
238 
239  if (offset != point.left)
240  {
241  point.left = -offset;
242  if (mHScroll != nullptr)
243  {
244  mHScroll->setScrollPosition(offset);
245  }
246  mWidgetClient->setPosition(point);
247  }
248  }
249  }
250 
251  Widget* ScrollView::baseCreateWidget(WidgetStyle _style, const std::string& _type, const std::string& _skin, const IntCoord& _coord, Align _align, const std::string& _layer, const std::string& _name)
252  {
253  if (mWidgetClient == nullptr)
254  return Base::baseCreateWidget(_style, _type, _skin, _coord, _align, _layer, _name);
255  return mWidgetClient->createWidgetT(_style, _type, _skin, _coord, _align, _layer, _name);
256  }
257 
258  IntSize ScrollView::getContentSize()
259  {
260  return mWidgetClient == nullptr ? IntSize() : mWidgetClient->getSize();
261  }
262 
263  IntPoint ScrollView::getContentPosition()
264  {
265  return mWidgetClient == nullptr ? IntPoint() : (IntPoint() - mWidgetClient->getPosition());
266  }
267 
268  void ScrollView::setContentPosition(const IntPoint& _point)
269  {
270  if (mWidgetClient != nullptr)
271  mWidgetClient->setPosition(IntPoint() - _point);
272  }
273 
274  IntSize ScrollView::getViewSize() const
275  {
276  return mScrollClient == nullptr ? IntSize() : mScrollClient->getSize();
277  }
278 
279  size_t ScrollView::getVScrollPage()
280  {
282  }
283 
284  size_t ScrollView::getHScrollPage()
285  {
287  }
288 
290  {
293  }
294 
296  {
297  mVisibleVScroll = _value;
298  updateView();
299  }
300 
302  {
303  mVisibleHScroll = _value;
304  updateView();
305  }
306 
308  {
309  mContentAlign = _value;
310  updateView();
311  }
312 
313  void ScrollView::setCanvasSize(const IntSize& _value)
314  {
315  if (mWidgetClient != nullptr)
316  mWidgetClient->setSize(_value); updateView();
317  }
318 
319  void ScrollView::setProperty(const std::string& _key, const std::string& _value)
320  {
321  if (_key == "ScrollView_VisibleVScroll") setVisibleVScroll(utility::parseValue<bool>(_value));
322  else if (_key == "ScrollView_VisibleHScroll") setVisibleHScroll(utility::parseValue<bool>(_value));
323  else if (_key == "ScrollView_CanvasAlign") setCanvasAlign(utility::parseValue<Align>(_value));
324  else if (_key == "ScrollView_CanvasSize") setCanvasSize(utility::parseValue<IntSize>(_value));
325 
326 #ifndef MYGUI_DONT_USE_OBSOLETE
327  else if (_key == "ScrollView_VScroll")
328  {
329  MYGUI_LOG(Warning, "ScrollView_VScroll is obsolete, use ScrollView_VisibleVScroll");
330  setVisibleVScroll(utility::parseValue<bool>(_value));
331  }
332  else if (_key == "ScrollView_HScroll")
333  {
334  MYGUI_LOG(Warning, "ScrollView_HScroll is obsolete, use ScrollView_VisibleHScroll");
335  setVisibleHScroll(utility::parseValue<bool>(_value));
336  }
337 #endif // MYGUI_DONT_USE_OBSOLETE
338 
339  else
340  {
341  Base::setProperty(_key, _value);
342  return;
343  }
344  eventChangeProperty(this, _key, _value);
345  }
346 
348  {
349  return mScrollClient == nullptr ? getCoord() : mScrollClient->getCoord();
350  }
351 
353  {
354  return mWidgetClient == nullptr ? IntSize() : mWidgetClient->getSize();
355  }
356 
357 } // namespace MyGUI