• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_ScrollView.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009     
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014     
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019     
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_ScrollView.h"
00025 #include "MyGUI_SkinManager.h"
00026 #include "MyGUI_ISubWidgetText.h"
00027 #include "MyGUI_VScroll.h"
00028 #include "MyGUI_HScroll.h"
00029 
00030 namespace MyGUI
00031 {
00032 
00033     const int SCROLL_VIEW_MOUSE_WHEEL = 50; // колличество пикселей для колеса мыши
00034     const int SCROLL_VIEW_SCROLL_PAGE = 16; // колличество пикселей для кнопок скрола
00035 
00036     ScrollView::ScrollView() :
00037         mIsFocus(false),
00038         mIsPressed(false),
00039         mScrollClient(nullptr),
00040         mContentAlign(Align::Center)
00041     {
00042         mChangeContentByResize = false;
00043         mContentAlign = Align::Center;
00044     }
00045 
00046     void ScrollView::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, WidgetPtr _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
00047     {
00048         Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
00049 
00050         initialiseWidgetSkin(_info);
00051     }
00052 
00053     ScrollView::~ScrollView()
00054     {
00055         shutdownWidgetSkin();
00056     }
00057 
00058     void ScrollView::baseChangeWidgetSkin(ResourceSkin* _info)
00059     {
00060         shutdownWidgetSkin();
00061         Base::baseChangeWidgetSkin(_info);
00062         initialiseWidgetSkin(_info);
00063     }
00064 
00065     void ScrollView::initialiseWidgetSkin(ResourceSkin* _info)
00066     {
00067         // нам нужен фокус клавы
00068         mNeedKeyFocus = true;
00069 
00070         for (VectorWidgetPtr::iterator iter=mWidgetChildSkin.begin(); iter!=mWidgetChildSkin.end(); ++iter)
00071         {
00072             if (*(*iter)->_getInternalData<std::string>() == "Client")
00073             {
00074                 MYGUI_DEBUG_ASSERT( ! mScrollClient, "widget already assigned");
00075                 mScrollClient = (*iter);
00076                 mScrollClient->eventMouseSetFocus = newDelegate(this, &ScrollView::notifyMouseSetFocus);
00077                 mScrollClient->eventMouseLostFocus = newDelegate(this, &ScrollView::notifyMouseLostFocus);
00078                 mScrollClient->eventMouseWheel = newDelegate(this, &ScrollView::notifyMouseWheel);
00079                 mClient = mScrollClient;
00080 
00081                 // создаем холcт, реальный владелец детей
00082                 mWidgetClient = mScrollClient->createWidget<Widget>("Default", IntCoord(), Align::Default);
00083                 mWidgetClient->eventMouseWheel = newDelegate(this, &ScrollView::notifyMouseWheel);
00084                 mWidgetClient->eventMouseSetFocus = newDelegate(this, &ScrollView::notifyMouseSetFocus);
00085                 mWidgetClient->eventMouseLostFocus = newDelegate(this, &ScrollView::notifyMouseLostFocus);
00086             }
00087             else if (*(*iter)->_getInternalData<std::string>() == "VScroll")
00088             {
00089                 MYGUI_DEBUG_ASSERT( ! mVScroll, "widget already assigned");
00090                 mVScroll = (*iter)->castType<VScroll>();
00091                 mVScroll->eventScrollChangePosition = newDelegate(this, &ScrollView::notifyScrollChangePosition);
00092             }
00093             else if (*(*iter)->_getInternalData<std::string>() == "HScroll")
00094             {
00095                 MYGUI_DEBUG_ASSERT( ! mHScroll, "widget already assigned");
00096                 mHScroll = (*iter)->castType<HScroll>();
00097                 mHScroll->eventScrollChangePosition = newDelegate(this, &ScrollView::notifyScrollChangePosition);
00098             }
00099         }
00100 
00101         MYGUI_ASSERT(nullptr != mScrollClient, "Child Widget Client not found in skin (ScrollView must have Client)");
00102 
00103         updateView();
00104     }
00105 
00106     void ScrollView::shutdownWidgetSkin()
00107     {
00108         mWidgetClient = nullptr;
00109         mVScroll = nullptr;
00110         mHScroll = nullptr;
00111         mScrollClient = nullptr;
00112     }
00113 
00114     void ScrollView::notifyMouseSetFocus(WidgetPtr _sender, WidgetPtr _old)
00115     {
00116         if ( (_old == mScrollClient) || (mIsFocus) ) return;
00117         mIsFocus = true;
00118         updateScrollViewState();
00119     }
00120 
00121     void ScrollView::notifyMouseLostFocus(WidgetPtr _sender, WidgetPtr _new)
00122     {
00123         if ( (_new == mScrollClient) || (false == mIsFocus) ) return;
00124         mIsFocus = false;
00125         updateScrollViewState();
00126     }
00127 
00128     void ScrollView::onKeySetFocus(WidgetPtr _old)
00129     {
00130         if (false == mIsPressed)
00131         {
00132             mIsPressed = true;
00133             updateScrollViewState();
00134         }
00135 
00136         Base::onKeySetFocus(_old);
00137     }
00138 
00139     void ScrollView::onKeyLostFocus(WidgetPtr _new)
00140     {
00141         if (mIsPressed)
00142         {
00143             mIsPressed = false;
00144             updateScrollViewState();
00145         }
00146 
00147         Base::onKeyLostFocus(_new);
00148     }
00149 
00150     void ScrollView::updateScrollViewState()
00151     {
00152         if (!mEnabled) setState("disabled");
00153         else if (mIsPressed)
00154         {
00155             if (mIsFocus) setState("pushed");
00156             else setState("normal_checked");
00157         }
00158         else if (mIsFocus) setState("highlighted");
00159         else setState("normal");
00160     }
00161 
00162     void ScrollView::setPosition(const IntPoint& _point)
00163     {
00164         Base::setPosition(_point);
00165     }
00166 
00167     void ScrollView::setSize(const IntSize& _size)
00168     {
00169         Base::setSize(_size);
00170 
00171         updateView();
00172     }
00173 
00174     void ScrollView::setCoord(const IntCoord& _coord)
00175     {
00176         Base::setCoord(_coord);
00177 
00178         updateView();
00179     }
00180 
00181     void ScrollView::notifyScrollChangePosition(VScrollPtr _sender, size_t _position)
00182     {
00183         if (_sender == mVScroll)
00184         {
00185             IntPoint point = mWidgetClient->getPosition();
00186             point.top = -(int)_position;
00187             mWidgetClient->setPosition(point);
00188         }
00189         else if (_sender == mHScroll)
00190         {
00191             IntPoint point = mWidgetClient->getPosition();
00192             point.left = -(int)_position;
00193             mWidgetClient->setPosition(point);
00194         }
00195     }
00196 
00197     void ScrollView::notifyMouseWheel(WidgetPtr _sender, int _rel)
00198     {
00199         if (mVRange != 0)
00200         {
00201             IntPoint point = mWidgetClient->getPosition();
00202             int offset = -point.top;
00203             if (_rel < 0) offset += SCROLL_VIEW_MOUSE_WHEEL;
00204             else  offset -= SCROLL_VIEW_MOUSE_WHEEL;
00205 
00206             if (offset < 0) offset = 0;
00207             else if (offset > (int)mVRange) offset = mVRange;
00208 
00209             if (offset != point.top)
00210             {
00211                 point.top = -offset;
00212                 if (mVScroll != nullptr)
00213                 {
00214                     mVScroll->setScrollPosition(offset);
00215                 }
00216                 mWidgetClient->setPosition(point);
00217             }
00218         }
00219         else if (mHRange != 0)
00220         {
00221             IntPoint point = mWidgetClient->getPosition();
00222             int offset = -point.left;
00223             if (_rel < 0) offset += SCROLL_VIEW_MOUSE_WHEEL;
00224             else  offset -= SCROLL_VIEW_MOUSE_WHEEL;
00225 
00226             if (offset < 0) offset = 0;
00227             else if (offset > (int)mHRange) offset = mHRange;
00228 
00229             if (offset != point.left)
00230             {
00231                 point.left = -offset;
00232                 if (mHScroll != nullptr)
00233                 {
00234                     mHScroll->setScrollPosition(offset);
00235                 }
00236                 mWidgetClient->setPosition(point);
00237             }
00238         }
00239     }
00240 
00241     WidgetPtr 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)
00242     {
00243         return mWidgetClient->createWidgetT(_style, _type, _skin, _coord, _align, _layer, _name);
00244     }
00245 
00246     IntSize ScrollView::getContentSize()
00247     {
00248         return mWidgetClient->getSize();
00249     }
00250 
00251     IntPoint ScrollView::getContentPosition()
00252     {
00253         return IntPoint() - mWidgetClient->getPosition();
00254     }
00255 
00256     void ScrollView::setContentPosition(const IntPoint& _point)
00257     {
00258         mWidgetClient->setPosition(IntPoint() - _point);
00259     }
00260 
00261     IntSize ScrollView::getViewSize()
00262     {
00263         return mScrollClient->getSize();
00264     }
00265 
00266     size_t ScrollView::getVScrollPage()
00267     {
00268         return SCROLL_VIEW_SCROLL_PAGE;
00269     }
00270 
00271     size_t ScrollView::getHScrollPage()
00272     {
00273         return SCROLL_VIEW_SCROLL_PAGE;
00274     }
00275 
00276     void ScrollView::updateView()
00277     {
00278         updateScrollSize();
00279         updateScrollPosition();
00280     }
00281 
00282     void ScrollView::setVisibleVScroll(bool _value)
00283     {
00284         mVisibleVScroll = _value;
00285         updateView();
00286     }
00287 
00288     void ScrollView::setVisibleHScroll(bool _value)
00289     {
00290         mVisibleHScroll = _value;
00291         updateView();
00292     }
00293 
00294     void ScrollView::setCanvasAlign(Align _value)
00295     {
00296         mContentAlign = _value;
00297         updateView();
00298     }
00299 
00300     void ScrollView::setCanvasSize(const IntSize& _value)
00301     {
00302         mWidgetClient->setSize(_value); updateView();
00303     }
00304 
00305     void ScrollView::setProperty(const std::string& _key, const std::string& _value)
00306     {
00307         if (_key == "ScrollView_VisibleVScroll") setVisibleVScroll(utility::parseValue<bool>(_value));
00308         else if (_key == "ScrollView_VisibleHScroll") setVisibleHScroll(utility::parseValue<bool>(_value));
00309         else if (_key == "ScrollView_CanvasAlign") setCanvasAlign(Align::parse(_value));
00310         else if (_key == "ScrollView_CanvasSize") setCanvasSize(utility::parseValue<IntSize>(_value));
00311 
00312 #ifndef MYGUI_DONT_USE_OBSOLETE
00313         else if (_key == "ScrollView_VScroll")
00314         {
00315             MYGUI_LOG(Warning, "ScrollView_VScroll is obsolete, use ScrollView_VisibleVScroll");
00316             setVisibleVScroll(utility::parseValue<bool>(_value));
00317         }
00318         else if (_key == "ScrollView_HScroll")
00319         {
00320             MYGUI_LOG(Warning, "ScrollView_HScroll is obsolete, use ScrollView_VisibleHScroll");
00321             setVisibleHScroll(utility::parseValue<bool>(_value));
00322         }
00323 #endif // MYGUI_DONT_USE_OBSOLETE
00324 
00325         else Base::setProperty(_key, _value);
00326     }
00327 
00328 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1