Bumping manifests a=b2g-bump
[gecko.git] / layout / svg / nsSVGSwitchFrame.cpp
blob1b51cbb3e1f69cc5604026c41a1d4061e83a2a58
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 // Keep in (case-insensitive) order:
7 #include "gfxRect.h"
8 #include "nsSVGEffects.h"
9 #include "nsSVGGFrame.h"
10 #include "mozilla/dom/SVGSwitchElement.h"
11 #include "nsSVGUtils.h"
13 class nsRenderingContext;
15 using namespace mozilla::gfx;
17 typedef nsSVGGFrame nsSVGSwitchFrameBase;
19 class nsSVGSwitchFrame : public nsSVGSwitchFrameBase
21 friend nsIFrame*
22 NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
23 protected:
24 explicit nsSVGSwitchFrame(nsStyleContext* aContext) :
25 nsSVGSwitchFrameBase(aContext) {}
27 public:
28 NS_DECL_FRAMEARENA_HELPERS
30 #ifdef DEBUG
31 virtual void Init(nsIContent* aContent,
32 nsContainerFrame* aParent,
33 nsIFrame* aPrevInFlow) MOZ_OVERRIDE;
34 #endif
36 /**
37 * Get the "type" of the frame
39 * @see nsGkAtoms::svgSwitchFrame
41 virtual nsIAtom* GetType() const MOZ_OVERRIDE;
43 #ifdef DEBUG_FRAME_DUMP
44 virtual nsresult GetFrameName(nsAString& aResult) const MOZ_OVERRIDE
46 return MakeFrameName(NS_LITERAL_STRING("SVGSwitch"), aResult);
48 #endif
50 virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
51 const nsRect& aDirtyRect,
52 const nsDisplayListSet& aLists) MOZ_OVERRIDE;
54 // nsISVGChildFrame interface:
55 virtual nsresult PaintSVG(nsRenderingContext* aContext,
56 const nsIntRect *aDirtyRect,
57 nsIFrame* aTransformRoot) MOZ_OVERRIDE;
58 nsIFrame* GetFrameForPoint(const gfxPoint& aPoint) MOZ_OVERRIDE;
59 nsRect GetCoveredRegion() MOZ_OVERRIDE;
60 virtual void ReflowSVG() MOZ_OVERRIDE;
61 virtual SVGBBox GetBBoxContribution(const Matrix &aToBBoxUserspace,
62 uint32_t aFlags) MOZ_OVERRIDE;
64 private:
65 nsIFrame *GetActiveChildFrame();
68 //----------------------------------------------------------------------
69 // Implementation
71 nsIFrame*
72 NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
74 return new (aPresShell) nsSVGSwitchFrame(aContext);
77 NS_IMPL_FRAMEARENA_HELPERS(nsSVGSwitchFrame)
79 #ifdef DEBUG
80 void
81 nsSVGSwitchFrame::Init(nsIContent* aContent,
82 nsContainerFrame* aParent,
83 nsIFrame* aPrevInFlow)
85 NS_ASSERTION(aContent->IsSVG(nsGkAtoms::svgSwitch),
86 "Content is not an SVG switch");
88 nsSVGSwitchFrameBase::Init(aContent, aParent, aPrevInFlow);
90 #endif /* DEBUG */
92 nsIAtom *
93 nsSVGSwitchFrame::GetType() const
95 return nsGkAtoms::svgSwitchFrame;
98 void
99 nsSVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
100 const nsRect& aDirtyRect,
101 const nsDisplayListSet& aLists)
103 nsIFrame* kid = GetActiveChildFrame();
104 if (kid) {
105 BuildDisplayListForChild(aBuilder, kid, aDirtyRect, aLists);
109 nsresult
110 nsSVGSwitchFrame::PaintSVG(nsRenderingContext* aContext,
111 const nsIntRect *aDirtyRect,
112 nsIFrame* aTransformRoot)
114 NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() ||
115 (mState & NS_FRAME_IS_NONDISPLAY),
116 "If display lists are enabled, only painting of non-display "
117 "SVG should take this code path");
119 if (StyleDisplay()->mOpacity == 0.0)
120 return NS_OK;
122 nsIFrame *kid = GetActiveChildFrame();
123 if (kid) {
124 nsSVGUtils::PaintFrameWithEffects(aContext, aDirtyRect, kid, aTransformRoot);
126 return NS_OK;
130 nsIFrame*
131 nsSVGSwitchFrame::GetFrameForPoint(const gfxPoint& aPoint)
133 NS_ASSERTION(!NS_SVGDisplayListHitTestingEnabled() ||
134 (mState & NS_FRAME_IS_NONDISPLAY),
135 "If display lists are enabled, only hit-testing of non-display "
136 "SVG should take this code path");
138 nsIFrame *kid = GetActiveChildFrame();
139 nsISVGChildFrame* svgFrame = do_QueryFrame(kid);
140 if (svgFrame) {
141 // Transform the point from our SVG user space to our child's.
142 gfxPoint point = aPoint;
143 gfxMatrix m =
144 static_cast<const nsSVGElement*>(mContent)->
145 PrependLocalTransformsTo(gfxMatrix(), nsSVGElement::eChildToUserSpace);
146 m = static_cast<const nsSVGElement*>(kid->GetContent())->
147 PrependLocalTransformsTo(m, nsSVGElement::eUserSpaceToParent);
148 if (!m.IsIdentity()) {
149 if (!m.Invert()) {
150 return nullptr;
152 point = m.Transform(point);
154 return svgFrame->GetFrameForPoint(point);
157 return nullptr;
160 nsRect
161 nsSVGSwitchFrame::GetCoveredRegion()
163 nsRect rect;
165 nsIFrame *kid = GetActiveChildFrame();
166 nsISVGChildFrame* child = do_QueryFrame(kid);
167 if (child) {
168 rect = child->GetCoveredRegion();
170 return rect;
173 void
174 nsSVGSwitchFrame::ReflowSVG()
176 NS_ASSERTION(nsSVGUtils::OuterSVGIsCallingReflowSVG(this),
177 "This call is probably a wasteful mistake");
179 NS_ABORT_IF_FALSE(!(GetStateBits() & NS_FRAME_IS_NONDISPLAY),
180 "ReflowSVG mechanism not designed for this");
182 if (!nsSVGUtils::NeedsReflowSVG(this)) {
183 return;
186 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
187 // then our outer-<svg> has previously had its initial reflow. In that case
188 // we need to make sure that that bit has been removed from ourself _before_
189 // recursing over our children to ensure that they know too. Otherwise, we
190 // need to remove it _after_ recursing over our children so that they know
191 // the initial reflow is currently underway.
193 bool isFirstReflow = (mState & NS_FRAME_FIRST_REFLOW);
195 bool outerSVGHasHadFirstReflow =
196 (GetParent()->GetStateBits() & NS_FRAME_FIRST_REFLOW) == 0;
198 if (outerSVGHasHadFirstReflow) {
199 mState &= ~NS_FRAME_FIRST_REFLOW; // tell our children
202 nsOverflowAreas overflowRects;
204 nsIFrame *child = GetActiveChildFrame();
205 nsISVGChildFrame* svgChild = do_QueryFrame(child);
206 if (svgChild) {
207 NS_ABORT_IF_FALSE(!(child->GetStateBits() & NS_FRAME_IS_NONDISPLAY),
208 "Check for this explicitly in the |if|, then");
209 svgChild->ReflowSVG();
211 // We build up our child frame overflows here instead of using
212 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
213 // frame list, and we're iterating over that list now anyway.
214 ConsiderChildOverflow(overflowRects, child);
217 if (isFirstReflow) {
218 // Make sure we have our filter property (if any) before calling
219 // FinishAndStoreOverflow (subsequent filter changes are handled off
220 // nsChangeHint_UpdateEffects):
221 nsSVGEffects::UpdateEffects(this);
224 FinishAndStoreOverflow(overflowRects, mRect.Size());
226 // Remove state bits after FinishAndStoreOverflow so that it doesn't
227 // invalidate on first reflow:
228 mState &= ~(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
229 NS_FRAME_HAS_DIRTY_CHILDREN);
232 SVGBBox
233 nsSVGSwitchFrame::GetBBoxContribution(const Matrix &aToBBoxUserspace,
234 uint32_t aFlags)
236 nsIFrame* kid = GetActiveChildFrame();
237 nsISVGChildFrame* svgKid = do_QueryFrame(kid);
238 if (svgKid) {
239 nsIContent *content = kid->GetContent();
240 gfxMatrix transform = ThebesMatrix(aToBBoxUserspace);
241 if (content->IsSVG()) {
242 transform = static_cast<nsSVGElement*>(content)->
243 PrependLocalTransformsTo(transform);
245 return svgKid->GetBBoxContribution(ToMatrix(transform), aFlags);
247 return SVGBBox();
250 nsIFrame *
251 nsSVGSwitchFrame::GetActiveChildFrame()
253 nsIContent *activeChild =
254 static_cast<mozilla::dom::SVGSwitchElement*>(mContent)->GetActiveChild();
256 if (activeChild) {
257 for (nsIFrame* kid = mFrames.FirstChild(); kid;
258 kid = kid->GetNextSibling()) {
260 if (activeChild == kid->GetContent()) {
261 return kid;
265 return nullptr;