ScrollView.h 4.5 KB

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