1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 //
8 // nsScrollbarFrame
9 //
10 
11 #ifndef nsScrollbarFrame_h__
12 #define nsScrollbarFrame_h__
13 
14 #include "mozilla/Attributes.h"
15 #include "nsIAnonymousContentCreator.h"
16 #include "nsBoxFrame.h"
17 
18 class nsIScrollbarMediator;
19 
20 namespace mozilla {
21 class PresShell;
22 namespace dom {
23 class Element;
24 }
25 }  // namespace mozilla
26 
27 nsIFrame* NS_NewScrollbarFrame(mozilla::PresShell* aPresShell,
28                                mozilla::ComputedStyle* aStyle);
29 
30 class nsScrollbarFrame final : public nsBoxFrame,
31                                public nsIAnonymousContentCreator {
32   using Element = mozilla::dom::Element;
33 
34  public:
nsScrollbarFrame(ComputedStyle * aStyle,nsPresContext * aPresContext)35   explicit nsScrollbarFrame(ComputedStyle* aStyle, nsPresContext* aPresContext)
36       : nsBoxFrame(aStyle, aPresContext, kClassID),
37         mIncrement(0),
38         mSmoothScroll(false),
39         mScrollbarMediator(nullptr),
40         mUpTopButton(nullptr),
41         mDownTopButton(nullptr),
42         mSlider(nullptr),
43         mThumb(nullptr),
44         mUpBottomButton(nullptr),
45         mDownBottomButton(nullptr) {}
46 
47   NS_DECL_QUERYFRAME
NS_DECL_FRAMEARENA_HELPERS(nsScrollbarFrame)48   NS_DECL_FRAMEARENA_HELPERS(nsScrollbarFrame)
49 
50 #ifdef DEBUG_FRAME_DUMP
51   virtual nsresult GetFrameName(nsAString& aResult) const override {
52     return MakeFrameName(NS_LITERAL_STRING("ScrollbarFrame"), aResult);
53   }
54 #endif
55 
56   // nsIFrame overrides
57   virtual nsresult AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute,
58                                     int32_t aModType) override;
59 
60   NS_IMETHOD HandlePress(nsPresContext* aPresContext,
61                          mozilla::WidgetGUIEvent* aEvent,
62                          nsEventStatus* aEventStatus) override;
63 
64   NS_IMETHOD HandleMultiplePress(nsPresContext* aPresContext,
65                                  mozilla::WidgetGUIEvent* aEvent,
66                                  nsEventStatus* aEventStatus,
67                                  bool aControlHeld) override;
68 
69   MOZ_CAN_RUN_SCRIPT
70   NS_IMETHOD HandleDrag(nsPresContext* aPresContext,
71                         mozilla::WidgetGUIEvent* aEvent,
72                         nsEventStatus* aEventStatus) override;
73 
74   NS_IMETHOD HandleRelease(nsPresContext* aPresContext,
75                            mozilla::WidgetGUIEvent* aEvent,
76                            nsEventStatus* aEventStatus) override;
77 
78   virtual void DestroyFrom(nsIFrame* aDestructRoot,
79                            PostDestroyData& aPostDestroyData) override;
80 
81   virtual void Init(nsIContent* aContent, nsContainerFrame* aParent,
82                     nsIFrame* aPrevInFlow) override;
83 
84   virtual void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
85                       const ReflowInput& aReflowInput,
86                       nsReflowStatus& aStatus) override;
87 
88   void SetScrollbarMediatorContent(nsIContent* aMediator);
89   nsIScrollbarMediator* GetScrollbarMediator();
90 
91   // nsBox methods
92 
93   /**
94    * Treat scrollbars as clipping their children; overflowing children
95    * will not be allowed to set an overflow rect on this
96    * frame. This means that when the scroll code decides to hide a
97    * scrollframe by setting its height or width to zero, that will
98    * hide the children too.
99    */
DoesClipChildren()100   virtual bool DoesClipChildren() override { return true; }
101 
102   virtual nsresult GetXULMargin(nsMargin& aMargin) override;
103 
104   /**
105    * The following three methods set the value of mIncrement when a
106    * scrollbar button is pressed.
107    */
108   void SetIncrementToLine(int32_t aDirection);
109   void SetIncrementToPage(int32_t aDirection);
110   void SetIncrementToWhole(int32_t aDirection);
111   /**
112    * MoveToNewPosition() adds mIncrement to the current position and
113    * updates the curpos attribute.
114    * @returns The new position after clamping, in CSS Pixels
115    * @note This method might destroy the frame, pres shell, and other objects.
116    */
117   int32_t MoveToNewPosition();
GetIncrement()118   int32_t GetIncrement() { return mIncrement; }
119 
120   // nsIAnonymousContentCreator
121   virtual nsresult CreateAnonymousContent(
122       nsTArray<ContentInfo>& aElements) override;
123   virtual void AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
124                                         uint32_t aFilter) override;
125 
126   void UpdateChildrenAttributeValue(nsAtom* aAttribute, bool aNotify);
127 
128  protected:
129   int32_t mIncrement;  // Amount to scroll, in CSSPixels
130   bool mSmoothScroll;
131 
132  private:
133   nsCOMPtr<nsIContent> mScrollbarMediator;
134 
135   nsCOMPtr<Element> mUpTopButton;
136   nsCOMPtr<Element> mDownTopButton;
137   nsCOMPtr<Element> mSlider;
138   nsCOMPtr<Element> mThumb;
139   nsCOMPtr<Element> mUpBottomButton;
140   nsCOMPtr<Element> mDownBottomButton;
141 };  // class nsScrollbarFrame
142 
143 #endif
144