ScrollView.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "UIElement.h"
  24. namespace Urho3D
  25. {
  26. class BorderImage;
  27. class ScrollBar;
  28. /// Scrollable %UI element for showing a (possibly large) child element.
  29. class ScrollView : public UIElement
  30. {
  31. OBJECT(ScrollView);
  32. public:
  33. /// Construct.
  34. ScrollView(Context* context);
  35. /// Destruct.
  36. virtual ~ScrollView();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Apply attribute changes that can not be applied immediately.
  40. virtual void ApplyAttributes();
  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. /// Set view position attribut.
  74. void SetViewPositionAttr(const IntVector2& value);
  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. /// View offset attribute.
  95. IntVector2 viewPositionAttr_;
  96. /// Arrow key page step.
  97. float pageStep_;
  98. /// Ignore scrollbar events flag. Used to prevent possible endless loop when setting position.
  99. bool ignoreEvents_;
  100. private:
  101. /// Handle scrollbar value changed.
  102. void HandleScrollBarChanged(StringHash eventType, VariantMap& eventData);
  103. /// Handle scrollbar visibility changed.
  104. void HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData);
  105. /// Handle content element resized.
  106. void HandleElementResized(StringHash eventType, VariantMap& eventData);
  107. };
  108. }