ScrollView.h 4.6 KB

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