nsSliderFrame.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 nsSliderFrame_h__
  6. #define nsSliderFrame_h__
  7. #include "mozilla/Attributes.h"
  8. #include "nsRepeatService.h"
  9. #include "nsBoxFrame.h"
  10. #include "nsIAtom.h"
  11. #include "nsCOMPtr.h"
  12. #include "nsITimer.h"
  13. #include "nsIDOMEventListener.h"
  14. class nsITimer;
  15. class nsSliderFrame;
  16. nsIFrame* NS_NewSliderFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
  17. class nsSliderMediator final : public nsIDOMEventListener
  18. {
  19. public:
  20. NS_DECL_ISUPPORTS
  21. nsSliderFrame* mSlider;
  22. explicit nsSliderMediator(nsSliderFrame* aSlider) { mSlider = aSlider; }
  23. virtual void SetSlider(nsSliderFrame* aSlider) { mSlider = aSlider; }
  24. NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) override;
  25. protected:
  26. virtual ~nsSliderMediator() {}
  27. };
  28. class nsSliderFrame : public nsBoxFrame
  29. {
  30. public:
  31. NS_DECL_FRAMEARENA_HELPERS
  32. NS_DECL_QUERYFRAME_TARGET(nsSliderFrame)
  33. NS_DECL_QUERYFRAME
  34. friend class nsSliderMediator;
  35. explicit nsSliderFrame(nsStyleContext* aContext);
  36. virtual ~nsSliderFrame();
  37. #ifdef DEBUG_FRAME_DUMP
  38. virtual nsresult GetFrameName(nsAString& aResult) const override {
  39. return MakeFrameName(NS_LITERAL_STRING("SliderFrame"), aResult);
  40. }
  41. #endif
  42. virtual nsSize GetXULPrefSize(nsBoxLayoutState& aBoxLayoutState) override;
  43. virtual nsSize GetXULMinSize(nsBoxLayoutState& aBoxLayoutState) override;
  44. virtual nsSize GetXULMaxSize(nsBoxLayoutState& aBoxLayoutState) override;
  45. NS_IMETHOD DoXULLayout(nsBoxLayoutState& aBoxLayoutState) override;
  46. // nsIFrame overrides
  47. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  48. virtual void BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
  49. const nsDisplayListSet& aLists) override;
  50. virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
  51. const nsDisplayListSet& aLists) override;
  52. virtual nsresult AttributeChanged(int32_t aNameSpaceID,
  53. nsIAtom* aAttribute,
  54. int32_t aModType) override;
  55. virtual void Init(nsIContent* aContent,
  56. nsContainerFrame* aParent,
  57. nsIFrame* asPrevInFlow) override;
  58. virtual nsresult HandleEvent(nsPresContext* aPresContext,
  59. mozilla::WidgetGUIEvent* aEvent,
  60. nsEventStatus* aEventStatus) override;
  61. virtual nsIAtom* GetType() const override;
  62. // nsContainerFrame overrides
  63. virtual void SetInitialChildList(ChildListID aListID,
  64. nsFrameList& aChildList) override;
  65. virtual void AppendFrames(ChildListID aListID,
  66. nsFrameList& aFrameList) override;
  67. virtual void InsertFrames(ChildListID aListID,
  68. nsIFrame* aPrevFrame,
  69. nsFrameList& aFrameList) override;
  70. virtual void RemoveFrame(ChildListID aListID,
  71. nsIFrame* aOldFrame) override;
  72. nsresult StartDrag(nsIDOMEvent* aEvent);
  73. nsresult StopDrag();
  74. bool StartAPZDrag();
  75. static int32_t GetCurrentPosition(nsIContent* content);
  76. static int32_t GetMinPosition(nsIContent* content);
  77. static int32_t GetMaxPosition(nsIContent* content);
  78. static int32_t GetIncrement(nsIContent* content);
  79. static int32_t GetPageIncrement(nsIContent* content);
  80. static int32_t GetIntegerAttribute(nsIContent* content, nsIAtom* atom, int32_t defaultValue);
  81. void EnsureOrient();
  82. NS_IMETHOD HandlePress(nsPresContext* aPresContext,
  83. mozilla::WidgetGUIEvent* aEvent,
  84. nsEventStatus* aEventStatus) override;
  85. NS_IMETHOD HandleMultiplePress(nsPresContext* aPresContext,
  86. mozilla::WidgetGUIEvent* aEvent,
  87. nsEventStatus* aEventStatus,
  88. bool aControlHeld) override
  89. {
  90. return NS_OK;
  91. }
  92. NS_IMETHOD HandleDrag(nsPresContext* aPresContext,
  93. mozilla::WidgetGUIEvent* aEvent,
  94. nsEventStatus* aEventStatus) override
  95. {
  96. return NS_OK;
  97. }
  98. NS_IMETHOD HandleRelease(nsPresContext* aPresContext,
  99. mozilla::WidgetGUIEvent* aEvent,
  100. nsEventStatus* aEventStatus) override;
  101. // Return the ratio the scrollbar thumb should move in proportion to the
  102. // scrolled frame.
  103. float GetThumbRatio() const;
  104. private:
  105. bool GetScrollToClick();
  106. nsIFrame* GetScrollbar();
  107. bool ShouldScrollForEvent(mozilla::WidgetGUIEvent* aEvent);
  108. bool ShouldScrollToClickForEvent(mozilla::WidgetGUIEvent* aEvent);
  109. bool IsEventOverThumb(mozilla::WidgetGUIEvent* aEvent);
  110. void PageUpDown(nscoord change);
  111. void SetCurrentThumbPosition(nsIContent* aScrollbar, nscoord aNewPos, bool aIsSmooth,
  112. bool aMaySnap);
  113. void SetCurrentPosition(nsIContent* aScrollbar, int32_t aNewPos, bool aIsSmooth);
  114. void SetCurrentPositionInternal(nsIContent* aScrollbar, int32_t pos,
  115. bool aIsSmooth);
  116. void CurrentPositionChanged();
  117. void DragThumb(bool aGrabMouseEvents);
  118. void AddListener();
  119. void RemoveListener();
  120. bool isDraggingThumb();
  121. void StartRepeat() {
  122. nsRepeatService::GetInstance()->Start(Notify, this);
  123. }
  124. void StopRepeat() {
  125. nsRepeatService::GetInstance()->Stop(Notify, this);
  126. }
  127. void Notify();
  128. static void Notify(void* aData) {
  129. (static_cast<nsSliderFrame*>(aData))->Notify();
  130. }
  131. void PageScroll(nscoord aChange);
  132. nsPoint mDestinationPoint;
  133. RefPtr<nsSliderMediator> mMediator;
  134. float mRatio;
  135. nscoord mDragStart;
  136. nscoord mThumbStart;
  137. int32_t mCurPos;
  138. nscoord mChange;
  139. bool mDragFinished;
  140. // true if an attribute change has been caused by the user manipulating the
  141. // slider. This allows notifications to tell how a slider's current position
  142. // was changed.
  143. bool mUserChanged;
  144. // true if we've handed off the scrolling to APZ. This means that we should
  145. // ignore scrolling events as the position will be updated by APZ. If we were
  146. // to process these events then the scroll position update would conflict
  147. // causing the scroll position to jump.
  148. bool mScrollingWithAPZ;
  149. // true if displayport suppression is active, for more performant
  150. // scrollbar-dragging behaviour.
  151. bool mSuppressionActive;
  152. static bool gMiddlePref;
  153. static int32_t gSnapMultiplier;
  154. }; // class nsSliderFrame
  155. #endif