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/. */
7 #include "nsCheckboxRadioFrame.h"
10 #include "nsLayoutUtils.h"
11 #include "mozilla/dom/HTMLInputElement.h"
12 #include "mozilla/PresShell.h"
13 #include "nsIContent.h"
14 #include "nsStyleConsts.h"
16 using namespace mozilla
;
17 using mozilla::dom::HTMLInputElement
;
21 nsCheckboxRadioFrame
* NS_NewCheckboxRadioFrame(PresShell
* aPresShell
,
22 ComputedStyle
* aStyle
) {
23 return new (aPresShell
)
24 nsCheckboxRadioFrame(aStyle
, aPresShell
->GetPresContext());
27 nsCheckboxRadioFrame::nsCheckboxRadioFrame(ComputedStyle
* aStyle
,
28 nsPresContext
* aPresContext
)
29 : nsAtomicContainerFrame(aStyle
, aPresContext
, kClassID
) {}
31 nsCheckboxRadioFrame::~nsCheckboxRadioFrame() = default;
33 NS_IMPL_FRAMEARENA_HELPERS(nsCheckboxRadioFrame
)
35 NS_QUERYFRAME_HEAD(nsCheckboxRadioFrame
)
36 NS_QUERYFRAME_ENTRY(nsIFormControlFrame
)
37 NS_QUERYFRAME_TAIL_INHERITING(nsAtomicContainerFrame
)
39 nscoord
nsCheckboxRadioFrame::DefaultSize() {
40 if (StyleDisplay()->HasAppearance()) {
41 return PresContext()->Theme()->GetCheckboxRadioPrefSize();
43 return CSSPixel::ToAppUnits(9);
47 void nsCheckboxRadioFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
48 const nsDisplayListSet
& aLists
) {
49 DO_GLOBAL_REFLOW_COUNT_DSP("nsCheckboxRadioFrame");
50 DisplayBorderBackgroundOutline(aBuilder
, aLists
);
54 nscoord
nsCheckboxRadioFrame::GetMinISize(gfxContext
* aRenderingContext
) {
56 DISPLAY_MIN_INLINE_SIZE(this, result
);
57 result
= StyleDisplay()->HasAppearance() ? DefaultSize() : 0;
62 nscoord
nsCheckboxRadioFrame::GetPrefISize(gfxContext
* aRenderingContext
) {
64 DISPLAY_PREF_INLINE_SIZE(this, result
);
65 result
= StyleDisplay()->HasAppearance() ? DefaultSize() : 0;
70 LogicalSize
nsCheckboxRadioFrame::ComputeAutoSize(
71 gfxContext
* aRC
, WritingMode aWM
, const LogicalSize
& aCBSize
,
72 nscoord aAvailableISize
, const LogicalSize
& aMargin
,
73 const LogicalSize
& aBorderPadding
, const StyleSizeOverrides
& aSizeOverrides
,
74 ComputeSizeFlags aFlags
) {
75 LogicalSize
size(aWM
, 0, 0);
76 if (!StyleDisplay()->HasAppearance()) {
80 // Note: this call always set the BSize to NS_UNCONSTRAINEDSIZE.
81 size
= nsAtomicContainerFrame::ComputeAutoSize(
82 aRC
, aWM
, aCBSize
, aAvailableISize
, aMargin
, aBorderPadding
,
83 aSizeOverrides
, aFlags
);
84 size
.BSize(aWM
) = DefaultSize();
88 Maybe
<nscoord
> nsCheckboxRadioFrame::GetNaturalBaselineBOffset(
89 WritingMode aWM
, BaselineSharingGroup aBaselineGroup
,
90 BaselineExportContext
) const {
91 NS_ASSERTION(!IsSubtreeDirty(), "frame must not be dirty");
93 if (aBaselineGroup
== BaselineSharingGroup::Last
) {
97 if (StyleDisplay()->IsBlockOutsideStyle()) {
101 // For appearance:none we use a standard CSS baseline, i.e. synthesized from
103 if (!StyleDisplay()->HasAppearance()) {
107 if (aWM
.IsCentralBaseline()) {
108 return Some(GetLogicalUsedBorderAndPadding(aWM
).BStart(aWM
) +
109 ContentBSize(aWM
) / 2);
111 // This is for compatibility with Chrome, Safari and Edge (Dec 2016).
112 // Treat radio buttons and checkboxes as having an intrinsic baseline
113 // at the block-end of the control (use the block-end content edge rather
114 // than the margin edge).
115 // For "inverted" lines (typically in writing-mode:vertical-lr), use the
116 // block-start end instead.
117 return Some(aWM
.IsLineInverted()
118 ? GetLogicalUsedBorderAndPadding(aWM
).BStart(aWM
)
119 : BSize(aWM
) - GetLogicalUsedBorderAndPadding(aWM
).BEnd(aWM
));
122 void nsCheckboxRadioFrame::Reflow(nsPresContext
* aPresContext
,
123 ReflowOutput
& aDesiredSize
,
124 const ReflowInput
& aReflowInput
,
125 nsReflowStatus
& aStatus
) {
127 DO_GLOBAL_REFLOW_COUNT("nsCheckboxRadioFrame");
128 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aDesiredSize
, aStatus
);
129 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
131 NS_FRAME_TRACE_CALLS
,
132 ("enter nsCheckboxRadioFrame::Reflow: aMaxSize=%d,%d",
133 aReflowInput
.AvailableWidth(), aReflowInput
.AvailableHeight()));
135 const auto wm
= aReflowInput
.GetWritingMode();
136 aDesiredSize
.SetSize(wm
, aReflowInput
.ComputedSizeWithBorderPadding(wm
));
138 if (nsLayoutUtils::FontSizeInflationEnabled(aPresContext
)) {
139 float inflation
= nsLayoutUtils::FontSizeInflationFor(this);
140 aDesiredSize
.Width() *= inflation
;
141 aDesiredSize
.Height() *= inflation
;
144 NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS
,
145 ("exit nsCheckboxRadioFrame::Reflow: size=%d,%d",
146 aDesiredSize
.Width(), aDesiredSize
.Height()));
148 aDesiredSize
.SetOverflowAreasToDesiredBounds();
149 FinishAndStoreOverflow(&aDesiredSize
);
152 void nsCheckboxRadioFrame::SetFocus(bool aOn
, bool aRepaint
) {}
154 nsresult
nsCheckboxRadioFrame::HandleEvent(nsPresContext
* aPresContext
,
155 WidgetGUIEvent
* aEvent
,
156 nsEventStatus
* aEventStatus
) {
157 // Check for disabled content so that selection works properly (?).
158 if (IsContentDisabled()) {
159 return nsIFrame::HandleEvent(aPresContext
, aEvent
, aEventStatus
);
164 nsresult
nsCheckboxRadioFrame::SetFormProperty(nsAtom
* aName
,
165 const nsAString
& aValue
) {