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/. */
6 /* rendering object that goes directly inside the document's scrollbars */
8 #ifndef nsCanvasFrame_h___
9 #define nsCanvasFrame_h___
11 #include "mozilla/Attributes.h"
12 #include "mozilla/EventForwards.h"
13 #include "nsContainerFrame.h"
14 #include "nsIScrollPositionListener.h"
15 #include "nsDisplayList.h"
16 #include "nsIAnonymousContentCreator.h"
19 class nsRenderingContext
;
24 * The root frame is the parent frame for the document element's frame.
25 * It only supports having a single child frame which must be an area
28 class nsCanvasFrame MOZ_FINAL
: public nsContainerFrame
,
29 public nsIScrollPositionListener
,
30 public nsIAnonymousContentCreator
33 explicit nsCanvasFrame(nsStyleContext
* aContext
)
34 : nsContainerFrame(aContext
),
36 mAddedScrollPositionListener(false) {}
38 NS_DECL_QUERYFRAME_TARGET(nsCanvasFrame
)
40 NS_DECL_FRAMEARENA_HELPERS
43 virtual void DestroyFrom(nsIFrame
* aDestructRoot
) MOZ_OVERRIDE
;
46 virtual void SetInitialChildList(ChildListID aListID
,
47 nsFrameList
& aChildList
) MOZ_OVERRIDE
;
48 virtual void AppendFrames(ChildListID aListID
,
49 nsFrameList
& aFrameList
) MOZ_OVERRIDE
;
50 virtual void InsertFrames(ChildListID aListID
,
52 nsFrameList
& aFrameList
) MOZ_OVERRIDE
;
53 virtual void RemoveFrame(ChildListID aListID
,
54 nsIFrame
* aOldFrame
) MOZ_OVERRIDE
;
57 virtual nscoord
GetMinISize(nsRenderingContext
*aRenderingContext
) MOZ_OVERRIDE
;
58 virtual nscoord
GetPrefISize(nsRenderingContext
*aRenderingContext
) MOZ_OVERRIDE
;
59 virtual void Reflow(nsPresContext
* aPresContext
,
60 nsHTMLReflowMetrics
& aDesiredSize
,
61 const nsHTMLReflowState
& aReflowState
,
62 nsReflowStatus
& aStatus
) MOZ_OVERRIDE
;
63 virtual bool IsFrameOfType(uint32_t aFlags
) const MOZ_OVERRIDE
65 return nsContainerFrame::IsFrameOfType(aFlags
&
66 ~(nsIFrame::eCanContainOverflowContainers
));
69 // nsIAnonymousContentCreator
70 virtual nsresult
CreateAnonymousContent(nsTArray
<ContentInfo
>& aElements
) MOZ_OVERRIDE
;
71 virtual void AppendAnonymousContentTo(nsTArray
<nsIContent
*>& aElements
, uint32_t aFilter
) MOZ_OVERRIDE
;
73 // Touch caret handle function
74 mozilla::dom::Element
* GetTouchCaretElement() const
76 return mTouchCaretElement
;
79 // Selection Caret Handle function
80 mozilla::dom::Element
* GetSelectionCaretsStartElement() const
82 return mSelectionCaretsStartElement
;
85 mozilla::dom::Element
* GetSelectionCaretsEndElement() const
87 return mSelectionCaretsEndElement
;
90 /** SetHasFocus tells the CanvasFrame to draw with focus ring
91 * @param aHasFocus true to show focus ring, false to hide it
93 NS_IMETHOD
SetHasFocus(bool aHasFocus
);
95 virtual void BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
96 const nsRect
& aDirtyRect
,
97 const nsDisplayListSet
& aLists
) MOZ_OVERRIDE
;
99 void PaintFocus(nsRenderingContext
& aRenderingContext
, nsPoint aPt
);
101 // nsIScrollPositionListener
102 virtual void ScrollPositionWillChange(nscoord aX
, nscoord aY
) MOZ_OVERRIDE
;
103 virtual void ScrollPositionDidChange(nscoord aX
, nscoord aY
) MOZ_OVERRIDE
{}
106 * Get the "type" of the frame
108 * @see nsGkAtoms::canvasFrame
110 virtual nsIAtom
* GetType() const MOZ_OVERRIDE
;
112 virtual nsresult
StealFrame(nsIFrame
* aChild
, bool aForceNormal
) MOZ_OVERRIDE
114 NS_ASSERTION(!aForceNormal
, "No-one should be passing this in here");
116 // nsCanvasFrame keeps overflow container continuations of its child
117 // frame in main child list
118 nsresult rv
= nsContainerFrame::StealFrame(aChild
, true);
120 rv
= nsContainerFrame::StealFrame(aChild
);
125 #ifdef DEBUG_FRAME_DUMP
126 virtual nsresult
GetFrameName(nsAString
& aResult
) const MOZ_OVERRIDE
;
128 virtual nsresult
GetContentForEvent(mozilla::WidgetEvent
* aEvent
,
129 nsIContent
** aContent
) MOZ_OVERRIDE
;
131 nsRect
CanvasArea() const;
136 bool mAddedScrollPositionListener
;
138 nsCOMPtr
<mozilla::dom::Element
> mTouchCaretElement
;
139 nsCOMPtr
<mozilla::dom::Element
> mSelectionCaretsStartElement
;
140 nsCOMPtr
<mozilla::dom::Element
> mSelectionCaretsEndElement
;
144 * Override nsDisplayBackground methods so that we pass aBGClipRect to
145 * PaintBackground, covering the whole overflow area.
146 * We can also paint an "extra background color" behind the normal
149 class nsDisplayCanvasBackgroundColor
: public nsDisplayItem
{
151 nsDisplayCanvasBackgroundColor(nsDisplayListBuilder
* aBuilder
, nsIFrame
*aFrame
)
152 : nsDisplayItem(aBuilder
, aFrame
)
153 , mColor(NS_RGBA(0,0,0,0))
157 virtual bool ComputeVisibility(nsDisplayListBuilder
* aBuilder
,
158 nsRegion
* aVisibleRegion
) MOZ_OVERRIDE
160 return NS_GET_A(mColor
) > 0;
162 virtual nsRegion
GetOpaqueRegion(nsDisplayListBuilder
* aBuilder
,
163 bool* aSnap
) MOZ_OVERRIDE
165 if (NS_GET_A(mColor
) == 255) {
166 return nsRegion(GetBounds(aBuilder
, aSnap
));
170 virtual bool IsUniform(nsDisplayListBuilder
* aBuilder
, nscolor
* aColor
) MOZ_OVERRIDE
175 virtual nsRect
GetBounds(nsDisplayListBuilder
* aBuilder
, bool* aSnap
) MOZ_OVERRIDE
177 nsCanvasFrame
* frame
= static_cast<nsCanvasFrame
*>(mFrame
);
179 return frame
->CanvasArea() + ToReferenceFrame();
181 virtual void HitTest(nsDisplayListBuilder
* aBuilder
, const nsRect
& aRect
,
182 HitTestState
* aState
, nsTArray
<nsIFrame
*> *aOutFrames
) MOZ_OVERRIDE
184 // We need to override so we don't consider border-radius.
185 aOutFrames
->AppendElement(mFrame
);
188 virtual nsDisplayItemGeometry
* AllocateGeometry(nsDisplayListBuilder
* aBuilder
) MOZ_OVERRIDE
190 return new nsDisplayItemBoundsGeometry(this, aBuilder
);
193 virtual void ComputeInvalidationRegion(nsDisplayListBuilder
* aBuilder
,
194 const nsDisplayItemGeometry
* aGeometry
,
195 nsRegion
* aInvalidRegion
) MOZ_OVERRIDE
197 const nsDisplayItemBoundsGeometry
* geometry
= static_cast<const nsDisplayItemBoundsGeometry
*>(aGeometry
);
198 ComputeInvalidationRegionDifference(aBuilder
, geometry
, aInvalidRegion
);
201 virtual void Paint(nsDisplayListBuilder
* aBuilder
,
202 nsRenderingContext
* aCtx
) MOZ_OVERRIDE
;
204 void SetExtraBackgroundColor(nscolor aColor
)
209 NS_DISPLAY_DECL_NAME("CanvasBackgroundColor", TYPE_CANVAS_BACKGROUND_COLOR
)
210 #ifdef MOZ_DUMP_PAINTING
211 virtual void WriteDebugInfo(nsACString
& aTo
) MOZ_OVERRIDE
;
218 class nsDisplayCanvasBackgroundImage
: public nsDisplayBackgroundImage
{
220 nsDisplayCanvasBackgroundImage(nsDisplayListBuilder
* aBuilder
, nsIFrame
* aFrame
,
221 uint32_t aLayer
, const nsStyleBackground
* aBg
)
222 : nsDisplayBackgroundImage(aBuilder
, aFrame
, aLayer
, aBg
)
225 virtual void Paint(nsDisplayListBuilder
* aBuilder
, nsRenderingContext
* aCtx
) MOZ_OVERRIDE
;
227 virtual void NotifyRenderingChanged() MOZ_OVERRIDE
229 mFrame
->Properties().Delete(nsIFrame::CachedBackgroundImage());
230 mFrame
->Properties().Delete(nsIFrame::CachedBackgroundImageDT());
233 virtual bool ShouldFixToViewport(LayerManager
* aManager
) MOZ_OVERRIDE
235 // Put background-attachment:fixed canvas background images in their own
236 // compositing layer. Since we know their background painting area can't
237 // change (unless the viewport size itself changes), async scrolling
239 return mBackgroundStyle
->mLayers
[mLayer
].mAttachment
== NS_STYLE_BG_ATTACHMENT_FIXED
&&
240 !mBackgroundStyle
->mLayers
[mLayer
].mImage
.IsEmpty();
243 // We still need to paint a background color as well as an image for this item,
244 // so we can't support this yet.
245 virtual bool SupportsOptimizingToImage() MOZ_OVERRIDE
{ return false; }
248 NS_DISPLAY_DECL_NAME("CanvasBackgroundImage", TYPE_CANVAS_BACKGROUND_IMAGE
)
251 class nsDisplayCanvasThemedBackground
: public nsDisplayThemedBackground
{
253 nsDisplayCanvasThemedBackground(nsDisplayListBuilder
* aBuilder
, nsIFrame
* aFrame
)
254 : nsDisplayThemedBackground(aBuilder
, aFrame
)
257 virtual void Paint(nsDisplayListBuilder
* aBuilder
, nsRenderingContext
* aCtx
) MOZ_OVERRIDE
;
259 NS_DISPLAY_DECL_NAME("CanvasThemedBackground", TYPE_CANVAS_THEMED_BACKGROUND
)
262 #endif /* nsCanvasFrame_h___ */