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:
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
22 NS_NewSVGSwitchFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
);
24 explicit nsSVGSwitchFrame(nsStyleContext
* aContext
) :
25 nsSVGSwitchFrameBase(aContext
) {}
28 NS_DECL_FRAMEARENA_HELPERS
31 virtual void Init(nsIContent
* aContent
,
32 nsContainerFrame
* aParent
,
33 nsIFrame
* aPrevInFlow
) MOZ_OVERRIDE
;
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
);
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
;
65 nsIFrame
*GetActiveChildFrame();
68 //----------------------------------------------------------------------
72 NS_NewSVGSwitchFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
74 return new (aPresShell
) nsSVGSwitchFrame(aContext
);
77 NS_IMPL_FRAMEARENA_HELPERS(nsSVGSwitchFrame
)
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
);
93 nsSVGSwitchFrame::GetType() const
95 return nsGkAtoms::svgSwitchFrame
;
99 nsSVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
100 const nsRect
& aDirtyRect
,
101 const nsDisplayListSet
& aLists
)
103 nsIFrame
* kid
= GetActiveChildFrame();
105 BuildDisplayListForChild(aBuilder
, kid
, aDirtyRect
, aLists
);
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)
122 nsIFrame
*kid
= GetActiveChildFrame();
124 nsSVGUtils::PaintFrameWithEffects(aContext
, aDirtyRect
, kid
, aTransformRoot
);
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
);
141 // Transform the point from our SVG user space to our child's.
142 gfxPoint point
= aPoint
;
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()) {
152 point
= m
.Transform(point
);
154 return svgFrame
->GetFrameForPoint(point
);
161 nsSVGSwitchFrame::GetCoveredRegion()
165 nsIFrame
*kid
= GetActiveChildFrame();
166 nsISVGChildFrame
* child
= do_QueryFrame(kid
);
168 rect
= child
->GetCoveredRegion();
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)) {
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
);
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
);
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
);
233 nsSVGSwitchFrame::GetBBoxContribution(const Matrix
&aToBBoxUserspace
,
236 nsIFrame
* kid
= GetActiveChildFrame();
237 nsISVGChildFrame
* svgKid
= do_QueryFrame(kid
);
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
);
251 nsSVGSwitchFrame::GetActiveChildFrame()
253 nsIContent
*activeChild
=
254 static_cast<mozilla::dom::SVGSwitchElement
*>(mContent
)->GetActiveChild();
257 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
258 kid
= kid
->GetNextSibling()) {
260 if (activeChild
== kid
->GetContent()) {