ScrollView.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #pragma once
  24. #include "UIElement.h"
  25. class BorderImage;
  26. class ScrollBar;
  27. /// Scrollable %UI element for showing a (possibly large) child element.
  28. class ScrollView : public UIElement
  29. {
  30. OBJECT(ScrollView);
  31. using UIElement::SetStyle;
  32. public:
  33. /// Construct.
  34. ScrollView(Context* context);
  35. /// Destruct.
  36. virtual ~ScrollView();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// %Set UI element style from XML data.
  40. virtual void SetStyle(const XMLElement& element);
  41. /// React to mouse wheel.
  42. virtual void OnWheel(int delta, int buttons, int qualifiers);
  43. /// React to a key press.
  44. virtual void OnKey(int key, int buttons, int qualifiers);
  45. /// React to resize.
  46. virtual void OnResize();
  47. /// %Set content element.
  48. void SetContentElement(UIElement* element);
  49. /// %Set view offset from the top-left corner.
  50. void SetViewPosition(const IntVector2& position);
  51. /// %Set view offset from the top-left corner.
  52. void SetViewPosition(int x, int y);
  53. /// %Set scrollbars' visibility.
  54. void SetScrollBarsVisible(bool horizontal, bool vertical);
  55. /// %Set arrow key scroll step. Also sets it on the scrollbars.
  56. void SetScrollStep(float step);
  57. /// %Set arrow key page step.
  58. void SetPageStep(float step);
  59. /// Return view offset from the top-left corner.
  60. const IntVector2& GetViewPosition() const { return viewPosition_; }
  61. /// Return content element.
  62. UIElement* GetContentElement() const { return contentElement_; }
  63. /// Return horizontal scroll bar.
  64. ScrollBar* GetHorizontalScrollBar() const { return horizontalScrollBar_; }
  65. /// Return vertical scroll bar.
  66. ScrollBar* GetVerticalScrollBar() const { return verticalScrollBar_; }
  67. /// Return scroll panel.
  68. BorderImage* GetScrollPanel() const { return scrollPanel_; }
  69. /// Return arrow key scroll step.
  70. float GetScrollStep() const;
  71. /// Return arrow key page step.
  72. float GetPageStep() const { return pageStep_; }
  73. protected:
  74. /// Update view size from the content element.
  75. void UpdateViewSize();
  76. /// Update the scrollbars' ranges and positions.
  77. void UpdateScrollBars();
  78. /// Limit and update the view with a new position.
  79. void UpdateView(const IntVector2& position);
  80. /// Content element.
  81. SharedPtr<UIElement> contentElement_;
  82. /// Horizontal scroll bar.
  83. SharedPtr<ScrollBar> horizontalScrollBar_;
  84. /// Vertical scroll bar.
  85. SharedPtr<ScrollBar> verticalScrollBar_;
  86. /// Scroll panel element.
  87. SharedPtr<BorderImage> scrollPanel_;
  88. /// Current view offset from the top-left corner.
  89. IntVector2 viewPosition_;
  90. /// Total view size.
  91. IntVector2 viewSize_;
  92. /// Arrow key page step.
  93. float pageStep_;
  94. /// Ignore scrollbar events flag. Used to prevent possible endless loop when setting position.
  95. bool ignoreEvents_;
  96. private:
  97. /// Handle scrollbar value changed.
  98. void HandleScrollBarChanged(StringHash eventType, VariantMap& eventData);
  99. /// Handle scrollbar visibility changed.
  100. void HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData);
  101. /// Handle content element resized.
  102. void HandleElementResized(StringHash eventType, VariantMap& eventData);
  103. };