nsListBoxBodyFrame.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsListBoxBodyFrame_h
  6. #define nsListBoxBodyFrame_h
  7. #include "mozilla/Attributes.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsBoxFrame.h"
  10. #include "nsIScrollbarMediator.h"
  11. #include "nsIReflowCallback.h"
  12. #include "nsBoxLayoutState.h"
  13. #include "nsThreadUtils.h"
  14. #include "nsPIBoxObject.h"
  15. class nsPresContext;
  16. class nsListScrollSmoother;
  17. nsIFrame* NS_NewListBoxBodyFrame(nsIPresShell* aPresShell,
  18. nsStyleContext* aContext);
  19. class nsListBoxBodyFrame final : public nsBoxFrame,
  20. public nsIScrollbarMediator,
  21. public nsIReflowCallback
  22. {
  23. nsListBoxBodyFrame(nsStyleContext* aContext,
  24. nsBoxLayout* aLayoutManager);
  25. virtual ~nsListBoxBodyFrame();
  26. public:
  27. NS_DECL_QUERYFRAME_TARGET(nsListBoxBodyFrame)
  28. NS_DECL_QUERYFRAME
  29. NS_DECL_FRAMEARENA_HELPERS
  30. // non-virtual ListBoxObject
  31. int32_t GetNumberOfVisibleRows();
  32. int32_t GetIndexOfFirstVisibleRow();
  33. nsresult EnsureIndexIsVisible(int32_t aRowIndex);
  34. nsresult ScrollToIndex(int32_t aRowIndex);
  35. nsresult ScrollByLines(int32_t aNumLines);
  36. nsresult GetItemAtIndex(int32_t aIndex, nsIDOMElement **aResult);
  37. nsresult GetIndexOfItem(nsIDOMElement *aItem, int32_t *aResult);
  38. friend nsIFrame* NS_NewListBoxBodyFrame(nsIPresShell* aPresShell,
  39. nsStyleContext* aContext);
  40. // nsIFrame
  41. virtual void Init(nsIContent* aContent,
  42. nsContainerFrame* aParent,
  43. nsIFrame* aPrevInFlow) override;
  44. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  45. virtual nsresult AttributeChanged(int32_t aNameSpaceID, nsIAtom* aAttribute, int32_t aModType) override;
  46. // nsIScrollbarMediator
  47. virtual void ScrollByPage(nsScrollbarFrame* aScrollbar, int32_t aDirection,
  48. nsIScrollbarMediator::ScrollSnapMode snapMode
  49. = nsIScrollbarMediator::DISABLE_SNAP) override;
  50. virtual void ScrollByWhole(nsScrollbarFrame* aScrollbar, int32_t aDirection,
  51. nsIScrollbarMediator::ScrollSnapMode snapMode
  52. = nsIScrollbarMediator::DISABLE_SNAP) override;
  53. virtual void ScrollByLine(nsScrollbarFrame* aScrollbar, int32_t aDirection,
  54. nsIScrollbarMediator::ScrollSnapMode snapMode
  55. = nsIScrollbarMediator::DISABLE_SNAP) override;
  56. virtual void RepeatButtonScroll(nsScrollbarFrame* aScrollbar) override;
  57. virtual void ThumbMoved(nsScrollbarFrame* aScrollbar,
  58. int32_t aOldPos,
  59. int32_t aNewPos) override;
  60. virtual void ScrollbarReleased(nsScrollbarFrame* aScrollbar) override {}
  61. virtual void VisibilityChanged(bool aVisible) override;
  62. virtual nsIFrame* GetScrollbarBox(bool aVertical) override;
  63. virtual void ScrollbarActivityStarted() const override {}
  64. virtual void ScrollbarActivityStopped() const override {}
  65. virtual bool IsScrollbarOnRight() const override {
  66. return (StyleVisibility()->mDirection == NS_STYLE_DIRECTION_LTR);
  67. }
  68. virtual bool ShouldSuppressScrollbarRepaints() const override {
  69. return false;
  70. }
  71. // nsIReflowCallback
  72. virtual bool ReflowFinished() override;
  73. virtual void ReflowCallbackCanceled() override;
  74. NS_IMETHOD DoXULLayout(nsBoxLayoutState& aBoxLayoutState) override;
  75. virtual void MarkIntrinsicISizesDirty() override;
  76. virtual nsSize GetXULMinSizeForScrollArea(nsBoxLayoutState& aBoxLayoutState) override;
  77. virtual nsSize GetXULPrefSize(nsBoxLayoutState& aBoxLayoutState) override;
  78. // size calculation
  79. int32_t GetRowCount();
  80. int32_t GetRowHeightAppUnits() { return mRowHeight; }
  81. int32_t GetFixedRowSize();
  82. void SetRowHeight(nscoord aRowHeight);
  83. nscoord GetYPosition();
  84. nscoord GetAvailableHeight();
  85. nscoord ComputeIntrinsicISize(nsBoxLayoutState& aBoxLayoutState);
  86. // scrolling
  87. nsresult InternalPositionChangedCallback();
  88. nsresult InternalPositionChanged(bool aUp, int32_t aDelta);
  89. // Process pending position changed events, then do the position change.
  90. // This can wipe out the frametree.
  91. nsresult DoInternalPositionChangedSync(bool aUp, int32_t aDelta);
  92. // Actually do the internal position change. This can wipe out the frametree
  93. nsresult DoInternalPositionChanged(bool aUp, int32_t aDelta);
  94. nsListScrollSmoother* GetSmoother();
  95. void VerticalScroll(int32_t aDelta);
  96. // Update the scroll index given a position, in CSS pixels
  97. void UpdateIndex(int32_t aNewPos);
  98. // frames
  99. nsIFrame* GetFirstFrame();
  100. nsIFrame* GetLastFrame();
  101. // lazy row creation and destruction
  102. void CreateRows();
  103. void DestroyRows(int32_t& aRowsToLose);
  104. void ReverseDestroyRows(int32_t& aRowsToLose);
  105. nsIFrame* GetFirstItemBox(int32_t aOffset, bool* aCreated);
  106. nsIFrame* GetNextItemBox(nsIFrame* aBox, int32_t aOffset, bool* aCreated);
  107. bool ContinueReflow(nscoord height);
  108. NS_IMETHOD ListBoxAppendFrames(nsFrameList& aFrameList);
  109. NS_IMETHOD ListBoxInsertFrames(nsIFrame* aPrevFrame, nsFrameList& aFrameList);
  110. void OnContentInserted(nsIContent* aContent);
  111. void OnContentRemoved(nsPresContext* aPresContext, nsIContent* aContainer,
  112. nsIFrame* aChildFrame, nsIContent* aOldNextSibling);
  113. void GetListItemContentAt(int32_t aIndex, nsIContent** aContent);
  114. void GetListItemNextSibling(nsIContent* aListItem, nsIContent** aContent, int32_t& aSiblingIndex);
  115. void PostReflowCallback();
  116. bool SetBoxObject(nsPIBoxObject* aBoxObject)
  117. {
  118. NS_ENSURE_TRUE(!mBoxObject, false);
  119. mBoxObject = aBoxObject;
  120. return true;
  121. }
  122. virtual bool SupportsOrdinalsInChildren() override;
  123. virtual bool ComputesOwnOverflowArea() override { return true; }
  124. protected:
  125. class nsPositionChangedEvent;
  126. friend class nsPositionChangedEvent;
  127. class nsPositionChangedEvent : public mozilla::Runnable
  128. {
  129. public:
  130. nsPositionChangedEvent(nsListBoxBodyFrame* aFrame,
  131. bool aUp, int32_t aDelta) :
  132. mFrame(aFrame), mUp(aUp), mDelta(aDelta)
  133. {}
  134. NS_IMETHOD Run() override
  135. {
  136. if (!mFrame) {
  137. return NS_OK;
  138. }
  139. mFrame->mPendingPositionChangeEvents.RemoveElement(this);
  140. return mFrame->DoInternalPositionChanged(mUp, mDelta);
  141. }
  142. void Revoke() {
  143. mFrame = nullptr;
  144. }
  145. nsListBoxBodyFrame* mFrame;
  146. bool mUp;
  147. int32_t mDelta;
  148. };
  149. void ComputeTotalRowCount();
  150. int32_t ToRowIndex(nscoord aPos) const;
  151. void RemoveChildFrame(nsBoxLayoutState &aState, nsIFrame *aChild);
  152. nsTArray< RefPtr<nsPositionChangedEvent> > mPendingPositionChangeEvents;
  153. nsCOMPtr<nsPIBoxObject> mBoxObject;
  154. // frame markers
  155. nsWeakFrame mTopFrame;
  156. nsIFrame* mBottomFrame;
  157. nsIFrame* mLinkupFrame;
  158. nsListScrollSmoother* mScrollSmoother;
  159. int32_t mRowsToPrepend;
  160. // row height
  161. int32_t mRowCount;
  162. nscoord mRowHeight;
  163. nscoord mAvailableHeight;
  164. nscoord mStringWidth;
  165. // scrolling
  166. int32_t mCurrentIndex; // Row-based
  167. int32_t mOldIndex;
  168. int32_t mYPosition;
  169. int32_t mTimePerRow;
  170. // row height
  171. bool mRowHeightWasSet;
  172. // scrolling
  173. bool mScrolling;
  174. bool mAdjustScroll;
  175. bool mReflowCallbackPosted;
  176. };
  177. #endif // nsListBoxBodyFrame_h