ScrollView.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef UI_SCROLLVIEW_H
  24. #define UI_SCROLLVIEW_H
  25. #include "UIElement.h"
  26. class BorderImage;
  27. class ScrollBar;
  28. //! A scrollable view for showing a (possibly large) child element
  29. class ScrollView : public UIElement
  30. {
  31. DEFINE_TYPE(ScrollView);
  32. public:
  33. //! Construct with name
  34. ScrollView(const std::string& name = std::string());
  35. //! Destruct
  36. virtual ~ScrollView();
  37. //! Set UI element style from XML data
  38. virtual void setStyle(const XMLElement& element, ResourceCache* cache);
  39. //! React to mouse wheel
  40. virtual void onWheel(int delta, int buttons, int qualifiers);
  41. //! React to a key press
  42. virtual void onKey(int key, int buttons, int qualifiers);
  43. //! React to resize
  44. virtual void onResize();
  45. //! Set content element
  46. void setContentElement(UIElement* element);
  47. //! Set view offset from the top-left corner
  48. void setViewPosition(const IntVector2& position);
  49. //! Set view offset from the top-left corner
  50. void setViewPosition(int x, int y);
  51. //! Set scrollbars' visibility
  52. void setScrollBarsVisible(bool horizontal, bool vertical);
  53. //! Set arrow key scroll step. Also sets it on the scrollbars
  54. void setScrollStep(float step);
  55. //! Set arrow key page step
  56. void setPageStep(float step);
  57. //! Set whether scroll step is normalized to content size
  58. void setNormalizeScrollStep(bool enable);
  59. //! Return view offset from the top-left corner
  60. const IntVector2& getViewPosition() const { return mViewPosition; }
  61. //! Return content element
  62. UIElement* getContentElement() const { return mContentElement; }
  63. //! Return horizontal scroll bar
  64. ScrollBar* getHorizontalScrollBar() const { return mHorizontalScrollBar; }
  65. //! Return vertical scroll bar
  66. ScrollBar* getVerticalScrollBar() const { return mVerticalScrollBar; }
  67. //! Return scroll panel
  68. BorderImage* getScrollPanel() const { return mScrollPanel; }
  69. //! Return horizontal scrollbar visibility
  70. bool getHorizontalScrollBarVisible() const;
  71. //! Return vertical scrollbar visibility
  72. bool getVerticalScrollBarVisible() const;
  73. //! Return arrow key scroll step
  74. float getScrollStep() const;
  75. //! Return arrow key page step
  76. float getPageStep() const { return mPageStep; }
  77. //! Return whether scroll step is normalized to content size
  78. bool getNormalizeScrollStep() const;
  79. protected:
  80. //! Update view size from the content element
  81. void updateViewSize();
  82. //! Update the scrollbars' ranges and positions
  83. void updateScrollBars();
  84. //! Limit and update the view with a new position
  85. void updateView(const IntVector2& position);
  86. //! Content element
  87. SharedPtr<UIElement> mContentElement;
  88. //! Horizontal scroll bar
  89. SharedPtr<ScrollBar> mHorizontalScrollBar;
  90. //! Vertical scroll bar
  91. SharedPtr<ScrollBar> mVerticalScrollBar;
  92. //! Scroll panel element
  93. SharedPtr<BorderImage> mScrollPanel;
  94. //! Current view offset from the top-left corner
  95. IntVector2 mViewPosition;
  96. //! Total view size
  97. IntVector2 mViewSize;
  98. //! Arrow key page step
  99. float mPageStep;
  100. //! Ignore scrollbar events flag. Used to prevent possible endless loop when setting position
  101. bool mIgnoreEvents;
  102. private:
  103. //! Handle scrollbar value changed
  104. void handleScrollBarChanged(StringHash eventType, VariantMap& eventData);
  105. //! Handle scrollbar visibility changed
  106. void handleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData);
  107. //! Handle content element resized
  108. void handleElementResized(StringHash eventType, VariantMap& eventData);
  109. //! Handle focus change attempt (check if ScrollView needs to be focused)
  110. void handleTryFocus(StringHash eventType, VariantMap& eventData);
  111. };
  112. #endif // UI_SCROLLVIEW_H