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 #include "nsHTMLButtonControlFrame.h"
8 #include "nsContainerFrame.h"
9 #include "nsIFormControlFrame.h"
10 #include "nsPresContext.h"
11 #include "nsGkAtoms.h"
12 #include "nsButtonFrameRenderer.h"
13 #include "nsCSSAnonBoxes.h"
14 #include "nsFormControlFrame.h"
15 #include "nsNameSpaceManager.h"
16 #include "nsStyleSet.h"
17 #include "nsDisplayList.h"
20 using namespace mozilla
;
23 NS_NewHTMLButtonControlFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
25 return new (aPresShell
) nsHTMLButtonControlFrame(aContext
);
28 NS_IMPL_FRAMEARENA_HELPERS(nsHTMLButtonControlFrame
)
30 nsHTMLButtonControlFrame::nsHTMLButtonControlFrame(nsStyleContext
* aContext
)
31 : nsContainerFrame(aContext
)
35 nsHTMLButtonControlFrame::~nsHTMLButtonControlFrame()
40 nsHTMLButtonControlFrame::DestroyFrom(nsIFrame
* aDestructRoot
)
42 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame
*>(this), false);
43 nsContainerFrame::DestroyFrom(aDestructRoot
);
47 nsHTMLButtonControlFrame::Init(nsIContent
* aContent
,
48 nsContainerFrame
* aParent
,
49 nsIFrame
* aPrevInFlow
)
51 nsContainerFrame::Init(aContent
, aParent
, aPrevInFlow
);
52 mRenderer
.SetFrame(this, PresContext());
55 NS_QUERYFRAME_HEAD(nsHTMLButtonControlFrame
)
56 NS_QUERYFRAME_ENTRY(nsIFormControlFrame
)
57 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame
)
61 nsHTMLButtonControlFrame::AccessibleType()
63 return a11y::eHTMLButtonType
;
68 nsHTMLButtonControlFrame::GetType() const
70 return nsGkAtoms::HTMLButtonControlFrame
;
74 nsHTMLButtonControlFrame::SetFocus(bool aOn
, bool aRepaint
)
79 nsHTMLButtonControlFrame::HandleEvent(nsPresContext
* aPresContext
,
80 WidgetGUIEvent
* aEvent
,
81 nsEventStatus
* aEventStatus
)
83 // if disabled do nothing
84 if (mRenderer
.isDisabled()) {
88 // mouse clicks are handled by content
89 // we don't want our children to get any events. So just pass it to frame.
90 return nsFrame::HandleEvent(aPresContext
, aEvent
, aEventStatus
);
95 nsHTMLButtonControlFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
96 const nsRect
& aDirtyRect
,
97 const nsDisplayListSet
& aLists
)
99 // Clip to our border area for event hit testing.
100 Maybe
<DisplayListClipState::AutoSaveRestore
> eventClipState
;
101 const bool isForEventDelivery
= aBuilder
->IsForEventDelivery();
102 if (isForEventDelivery
) {
103 eventClipState
.emplace(aBuilder
);
104 nsRect
rect(aBuilder
->ToReferenceFrame(this), GetSize());
106 bool hasRadii
= GetBorderRadii(radii
);
107 eventClipState
->ClipContainingBlockDescendants(rect
, hasRadii
? radii
: nullptr);
111 if (IsVisibleForPainting(aBuilder
)) {
112 mRenderer
.DisplayButton(aBuilder
, aLists
.BorderBackground(), &onTop
);
115 nsDisplayListCollection set
;
117 // Do not allow the child subtree to receive events.
118 if (!isForEventDelivery
) {
119 DisplayListClipState::AutoSaveRestore
clipState(aBuilder
);
121 if (IsInput() || StyleDisplay()->mOverflowX
!= NS_STYLE_OVERFLOW_VISIBLE
) {
122 nsMargin border
= StyleBorder()->GetComputedBorder();
123 nsRect
rect(aBuilder
->ToReferenceFrame(this), GetSize());
124 rect
.Deflate(border
);
126 bool hasRadii
= GetPaddingBoxBorderRadii(radii
);
127 clipState
.ClipContainingBlockDescendants(rect
, hasRadii
? radii
: nullptr);
130 BuildDisplayListForChild(aBuilder
, mFrames
.FirstChild(), aDirtyRect
, set
,
131 DISPLAY_CHILD_FORCE_PSEUDO_STACKING_CONTEXT
);
132 // That should put the display items in set.Content()
135 // Put the foreground outline and focus rects on top of the children
136 set
.Content()->AppendToTop(&onTop
);
139 DisplayOutline(aBuilder
, aLists
);
141 // to draw border when selected in editor
142 DisplaySelectionOverlay(aBuilder
, aLists
.Content());
146 nsHTMLButtonControlFrame::GetMinISize(nsRenderingContext
* aRenderingContext
)
149 DISPLAY_MIN_WIDTH(this, result
);
151 nsIFrame
* kid
= mFrames
.FirstChild();
152 result
= nsLayoutUtils::IntrinsicForContainer(aRenderingContext
,
154 nsLayoutUtils::MIN_ISIZE
);
156 result
+= GetWritingMode().IsVertical()
157 ? mRenderer
.GetAddedButtonBorderAndPadding().TopBottom()
158 : mRenderer
.GetAddedButtonBorderAndPadding().LeftRight();
164 nsHTMLButtonControlFrame::GetPrefISize(nsRenderingContext
* aRenderingContext
)
167 DISPLAY_PREF_WIDTH(this, result
);
169 nsIFrame
* kid
= mFrames
.FirstChild();
170 result
= nsLayoutUtils::IntrinsicForContainer(aRenderingContext
,
172 nsLayoutUtils::PREF_ISIZE
);
174 result
+= GetWritingMode().IsVertical()
175 ? mRenderer
.GetAddedButtonBorderAndPadding().TopBottom()
176 : mRenderer
.GetAddedButtonBorderAndPadding().LeftRight();
182 nsHTMLButtonControlFrame::Reflow(nsPresContext
* aPresContext
,
183 nsHTMLReflowMetrics
& aDesiredSize
,
184 const nsHTMLReflowState
& aReflowState
,
185 nsReflowStatus
& aStatus
)
187 DO_GLOBAL_REFLOW_COUNT("nsHTMLButtonControlFrame");
188 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aDesiredSize
, aStatus
);
190 NS_PRECONDITION(aReflowState
.ComputedISize() != NS_INTRINSICSIZE
,
191 "Should have real computed inline-size by now");
193 if (mState
& NS_FRAME_FIRST_REFLOW
) {
194 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame
*>(this), true);
198 nsIFrame
* firstKid
= mFrames
.FirstChild();
200 MOZ_ASSERT(firstKid
, "Button should have a child frame for its contents");
201 MOZ_ASSERT(!firstKid
->GetNextSibling(),
202 "Button should have exactly one child frame");
203 MOZ_ASSERT(firstKid
->StyleContext()->GetPseudo() ==
204 nsCSSAnonBoxes::buttonContent
,
205 "Button's child frame has unexpected pseudo type!");
207 // XXXbz Eventually we may want to check-and-bail if
208 // !aReflowState.ShouldReflowAllKids() &&
209 // !NS_SUBTREE_DIRTY(firstKid).
210 // We'd need to cache our ascent for that, of course.
212 // Reflow the contents of the button.
213 // (This populates our aDesiredSize, too.)
214 ReflowButtonContents(aPresContext
, aDesiredSize
,
215 aReflowState
, firstKid
);
217 ConsiderChildOverflow(aDesiredSize
.mOverflowAreas
, firstKid
);
219 aStatus
= NS_FRAME_COMPLETE
;
220 FinishReflowWithAbsoluteFrames(aPresContext
, aDesiredSize
,
221 aReflowState
, aStatus
);
223 // We're always complete and we don't support overflow containers
224 // so we shouldn't have a next-in-flow ever.
225 aStatus
= NS_FRAME_COMPLETE
;
226 MOZ_ASSERT(!GetNextInFlow());
228 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aDesiredSize
);
231 // Helper-function that lets us clone the button's reflow state, but with its
232 // ComputedWidth and ComputedHeight reduced by the amount of renderer-specific
233 // focus border and padding that we're using. (This lets us provide a more
234 // appropriate content-box size for descendents' percent sizes to resolve
236 static nsHTMLReflowState
237 CloneReflowStateWithReducedContentBox(
238 const nsHTMLReflowState
& aButtonReflowState
,
239 const nsMargin
& aFocusPadding
)
241 nscoord adjustedWidth
=
242 aButtonReflowState
.ComputedWidth() - aFocusPadding
.LeftRight();
243 adjustedWidth
= std::max(0, adjustedWidth
);
245 // (Only adjust height if it's an actual length.)
246 nscoord adjustedHeight
= aButtonReflowState
.ComputedHeight();
247 if (adjustedHeight
!= NS_INTRINSICSIZE
) {
248 adjustedHeight
-= aFocusPadding
.TopBottom();
249 adjustedHeight
= std::max(0, adjustedHeight
);
252 nsHTMLReflowState
clone(aButtonReflowState
);
253 clone
.SetComputedWidth(adjustedWidth
);
254 clone
.SetComputedHeight(adjustedHeight
);
260 nsHTMLButtonControlFrame::ReflowButtonContents(nsPresContext
* aPresContext
,
261 nsHTMLReflowMetrics
& aButtonDesiredSize
,
262 const nsHTMLReflowState
& aButtonReflowState
,
265 WritingMode wm
= GetWritingMode();
266 LogicalSize availSize
= aButtonReflowState
.ComputedSize(wm
);
267 availSize
.BSize(wm
) = NS_INTRINSICSIZE
;
269 // Buttons have some bonus renderer-determined border/padding,
270 // which occupies part of the button's content-box area:
271 LogicalMargin focusPadding
=
272 LogicalMargin(wm
, mRenderer
.GetAddedButtonBorderAndPadding());
274 // See whether out availSize's inline-size is big enough. If it's
275 // smaller than our intrinsic min iSize, that means that the kid
276 // wouldn't really fit. In that case, we overflow into our internal
277 // focuspadding (which other browsers don't have) so that there's a
278 // little more space for it.
279 // Note that GetMinISize includes the focusPadding.
280 nscoord IOverflow
= GetMinISize(aButtonReflowState
.rendContext
) -
281 aButtonReflowState
.ComputedISize();
282 nscoord IFocusPadding
= focusPadding
.IStartEnd(wm
);
283 nscoord focusPaddingReduction
= std::min(IFocusPadding
,
284 std::max(IOverflow
, 0));
285 if (focusPaddingReduction
> 0) {
286 nscoord startReduction
= focusPadding
.IStart(wm
);
287 if (focusPaddingReduction
!= IFocusPadding
) {
288 startReduction
= NSToCoordRound(startReduction
*
289 (float(focusPaddingReduction
) /
290 float(IFocusPadding
)));
292 focusPadding
.IStart(wm
) -= startReduction
;
293 focusPadding
.IEnd(wm
) -= focusPaddingReduction
- startReduction
;
296 // shorthand for a value we need to use in a bunch of places
297 const LogicalMargin
& clbp
= aButtonReflowState
.ComputedLogicalBorderPadding();
299 // Indent the child inside us by the focus border. We must do this separate
300 // from the regular border.
301 availSize
.ISize(wm
) -= focusPadding
.IStartEnd(wm
);
303 LogicalPoint
childPos(wm
);
304 childPos
.I(wm
) = focusPadding
.IStart(wm
) + clbp
.IStart(wm
);
305 availSize
.ISize(wm
) = std::max(availSize
.ISize(wm
), 0);
307 // Give child a clone of the button's reflow state, with height/width reduced
308 // by focusPadding, so that descendants with height:100% don't protrude.
309 nsHTMLReflowState adjustedButtonReflowState
=
310 CloneReflowStateWithReducedContentBox(aButtonReflowState
,
311 focusPadding
.GetPhysicalMargin(wm
));
313 nsHTMLReflowState
contentsReflowState(aPresContext
,
314 adjustedButtonReflowState
,
315 aFirstKid
, availSize
);
317 nsReflowStatus contentsReflowStatus
;
318 nsHTMLReflowMetrics
contentsDesiredSize(aButtonReflowState
);
319 childPos
.B(wm
) = focusPadding
.BStart(wm
) + clbp
.BStart(wm
);
321 // We just pass 0 for coordinates here, as the child will be repositioned
322 // later by FinishReflowChild.
323 ReflowChild(aFirstKid
, aPresContext
,
324 contentsDesiredSize
, contentsReflowState
,
325 0, 0, NS_FRAME_NO_MOVE_FRAME
, contentsReflowStatus
);
326 MOZ_ASSERT(NS_FRAME_IS_COMPLETE(contentsReflowStatus
),
327 "We gave button-contents frame unconstrained available height, "
328 "so it should be complete");
330 // Compute the button's content-box height:
331 nscoord buttonContentBoxBSize
= 0;
332 if (aButtonReflowState
.ComputedBSize() != NS_INTRINSICSIZE
) {
333 // Button has a fixed block-size -- that's its content-box bSize.
334 buttonContentBoxBSize
= aButtonReflowState
.ComputedBSize();
336 // Button is intrinsically sized -- it should shrinkwrap the
337 // button-contents' bSize, plus any focus-padding space:
338 buttonContentBoxBSize
=
339 contentsDesiredSize
.BSize(wm
) + focusPadding
.BStartEnd(wm
);
341 // Make sure we obey min/max-bSize in the case when we're doing intrinsic
342 // sizing (we get it for free when we have a non-intrinsic
343 // aButtonReflowState.ComputedBSize()). Note that we do this before
344 // adjusting for borderpadding, since mComputedMaxBSize and
345 // mComputedMinBSize are content bSizes.
346 buttonContentBoxBSize
=
347 NS_CSS_MINMAX(buttonContentBoxBSize
,
348 aButtonReflowState
.ComputedMinBSize(),
349 aButtonReflowState
.ComputedMaxBSize());
352 // Center child in the block-direction in the button
353 // (technically, inside of the button's focus-padding area)
355 buttonContentBoxBSize
- focusPadding
.BStartEnd(wm
) -
356 contentsDesiredSize
.BSize(wm
);
358 childPos
.B(wm
) = std::max(0, extraSpace
/ 2);
360 // Adjust childPos.B() to be in terms of the button's frame-rect, instead of
361 // its focus-padding rect:
362 childPos
.B(wm
) += focusPadding
.BStart(wm
) + clbp
.BStart(wm
);
364 nscoord containerWidth
= contentsDesiredSize
.Width() +
365 clbp
.LeftRight(wm
) + focusPadding
.LeftRight(wm
);
368 nsPoint physicalPos
=
369 childPos
.GetPhysicalPoint(wm
, containerWidth
- contentsDesiredSize
.Width());
370 FinishReflowChild(aFirstKid
, aPresContext
,
371 contentsDesiredSize
, &contentsReflowState
,
372 physicalPos
.x
, physicalPos
.y
, 0);
374 // Make sure we have a useful 'ascent' value for the child
375 if (contentsDesiredSize
.BlockStartAscent() ==
376 nsHTMLReflowMetrics::ASK_FOR_BASELINE
) {
377 WritingMode wm
= aButtonReflowState
.GetWritingMode();
378 contentsDesiredSize
.SetBlockStartAscent(aFirstKid
->GetLogicalBaseline(wm
));
381 // OK, we're done with the child frame.
382 // Use what we learned to populate the button frame's reflow metrics.
383 // * Button's height & width are content-box size + border-box contribution:
384 aButtonDesiredSize
.SetSize(wm
,
385 LogicalSize(wm
, aButtonReflowState
.ComputedISize() + clbp
.IStartEnd(wm
),
386 buttonContentBoxBSize
+ clbp
.BStartEnd(wm
)));
388 // * Button's ascent is its child's ascent, plus the child's block-offset
389 // within our frame... unless it's orthogonal, in which case we'll use the
390 // contents inline-size as an approximation for now.
391 // XXX is there a better strategy? should we include border-padding?
392 if (aButtonDesiredSize
.GetWritingMode().IsOrthogonalTo(wm
)) {
393 aButtonDesiredSize
.SetBlockStartAscent(contentsDesiredSize
.ISize(wm
));
395 aButtonDesiredSize
.SetBlockStartAscent(contentsDesiredSize
.BlockStartAscent() +
399 aButtonDesiredSize
.SetOverflowAreasToDesiredBounds();
402 nsresult
nsHTMLButtonControlFrame::SetFormProperty(nsIAtom
* aName
, const nsAString
& aValue
)
404 if (nsGkAtoms::value
== aName
) {
405 return mContent
->SetAttr(kNameSpaceID_None
, nsGkAtoms::value
,
412 nsHTMLButtonControlFrame::GetAdditionalStyleContext(int32_t aIndex
) const
414 return mRenderer
.GetStyleContext(aIndex
);
418 nsHTMLButtonControlFrame::SetAdditionalStyleContext(int32_t aIndex
,
419 nsStyleContext
* aStyleContext
)
421 mRenderer
.SetStyleContext(aIndex
, aStyleContext
);
426 nsHTMLButtonControlFrame::AppendFrames(ChildListID aListID
,
427 nsFrameList
& aFrameList
)
429 MOZ_CRASH("unsupported operation");
433 nsHTMLButtonControlFrame::InsertFrames(ChildListID aListID
,
434 nsIFrame
* aPrevFrame
,
435 nsFrameList
& aFrameList
)
437 MOZ_CRASH("unsupported operation");
441 nsHTMLButtonControlFrame::RemoveFrame(ChildListID aListID
,
444 MOZ_CRASH("unsupported operation");