Line data Source code
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/. */
5 :
6 : #include "nsMeterFrame.h"
7 :
8 : #include "nsIContent.h"
9 : #include "nsPresContext.h"
10 : #include "nsGkAtoms.h"
11 : #include "nsNameSpaceManager.h"
12 : #include "nsIDocument.h"
13 : #include "nsIPresShell.h"
14 : #include "nsNodeInfoManager.h"
15 : #include "nsContentCreatorFunctions.h"
16 : #include "nsContentUtils.h"
17 : #include "nsFormControlFrame.h"
18 : #include "nsFontMetrics.h"
19 : #include "mozilla/dom/Element.h"
20 : #include "mozilla/dom/HTMLMeterElement.h"
21 : #include "nsContentList.h"
22 : #include "nsCSSPseudoElements.h"
23 : #include "nsStyleSet.h"
24 : #include "mozilla/StyleSetHandle.h"
25 : #include "mozilla/StyleSetHandleInlines.h"
26 : #include "nsThemeConstants.h"
27 : #include <algorithm>
28 :
29 : using namespace mozilla;
30 : using mozilla::dom::Element;
31 : using mozilla::dom::HTMLMeterElement;
32 :
33 : nsIFrame*
34 0 : NS_NewMeterFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
35 : {
36 0 : return new (aPresShell) nsMeterFrame(aContext);
37 : }
38 :
39 0 : NS_IMPL_FRAMEARENA_HELPERS(nsMeterFrame)
40 :
41 0 : nsMeterFrame::nsMeterFrame(nsStyleContext* aContext)
42 : : nsContainerFrame(aContext, kClassID)
43 0 : , mBarDiv(nullptr)
44 : {
45 0 : }
46 :
47 0 : nsMeterFrame::~nsMeterFrame()
48 : {
49 0 : }
50 :
51 : void
52 0 : nsMeterFrame::DestroyFrom(nsIFrame* aDestructRoot)
53 : {
54 0 : NS_ASSERTION(!GetPrevContinuation(),
55 : "nsMeterFrame should not have continuations; if it does we "
56 : "need to call RegUnregAccessKey only for the first.");
57 0 : nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
58 0 : nsContentUtils::DestroyAnonymousContent(&mBarDiv);
59 0 : nsContainerFrame::DestroyFrom(aDestructRoot);
60 0 : }
61 :
62 : nsresult
63 0 : nsMeterFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
64 : {
65 : // Get the NodeInfoManager and tag necessary to create the meter bar div.
66 0 : nsCOMPtr<nsIDocument> doc = mContent->GetComposedDoc();
67 :
68 : // Create the div.
69 0 : mBarDiv = doc->CreateHTMLElement(nsGkAtoms::div);
70 :
71 : // Associate ::-moz-meter-bar pseudo-element to the anonymous child.
72 0 : mBarDiv->SetPseudoElementType(CSSPseudoElementType::mozMeterBar);
73 :
74 0 : aElements.AppendElement(mBarDiv);
75 :
76 0 : return NS_OK;
77 : }
78 :
79 : void
80 0 : nsMeterFrame::AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
81 : uint32_t aFilter)
82 : {
83 0 : if (mBarDiv) {
84 0 : aElements.AppendElement(mBarDiv);
85 : }
86 0 : }
87 :
88 0 : NS_QUERYFRAME_HEAD(nsMeterFrame)
89 0 : NS_QUERYFRAME_ENTRY(nsMeterFrame)
90 0 : NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
91 0 : NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
92 :
93 :
94 : void
95 0 : nsMeterFrame::Reflow(nsPresContext* aPresContext,
96 : ReflowOutput& aDesiredSize,
97 : const ReflowInput& aReflowInput,
98 : nsReflowStatus& aStatus)
99 : {
100 0 : MarkInReflow();
101 0 : DO_GLOBAL_REFLOW_COUNT("nsMeterFrame");
102 0 : DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
103 :
104 0 : NS_ASSERTION(mBarDiv, "Meter bar div must exist!");
105 0 : NS_ASSERTION(!GetPrevContinuation(),
106 : "nsMeterFrame should not have continuations; if it does we "
107 : "need to call RegUnregAccessKey only for the first.");
108 :
109 0 : if (mState & NS_FRAME_FIRST_REFLOW) {
110 0 : nsFormControlFrame::RegUnRegAccessKey(this, true);
111 : }
112 :
113 0 : nsIFrame* barFrame = mBarDiv->GetPrimaryFrame();
114 0 : NS_ASSERTION(barFrame, "The meter frame should have a child with a frame!");
115 :
116 0 : ReflowBarFrame(barFrame, aPresContext, aReflowInput, aStatus);
117 :
118 0 : aDesiredSize.SetSize(aReflowInput.GetWritingMode(),
119 0 : aReflowInput.ComputedSizeWithBorderPadding());
120 :
121 0 : aDesiredSize.SetOverflowAreasToDesiredBounds();
122 0 : ConsiderChildOverflow(aDesiredSize.mOverflowAreas, barFrame);
123 0 : FinishAndStoreOverflow(&aDesiredSize);
124 :
125 0 : aStatus.Reset();
126 :
127 0 : NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aDesiredSize);
128 0 : }
129 :
130 : void
131 0 : nsMeterFrame::ReflowBarFrame(nsIFrame* aBarFrame,
132 : nsPresContext* aPresContext,
133 : const ReflowInput& aReflowInput,
134 : nsReflowStatus& aStatus)
135 : {
136 0 : bool vertical = ResolvedOrientationIsVertical();
137 0 : WritingMode wm = aBarFrame->GetWritingMode();
138 0 : LogicalSize availSize = aReflowInput.ComputedSize(wm);
139 0 : availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
140 : ReflowInput reflowInput(aPresContext, aReflowInput,
141 0 : aBarFrame, availSize);
142 0 : nscoord size = vertical ? aReflowInput.ComputedHeight()
143 0 : : aReflowInput.ComputedWidth();
144 0 : nscoord xoffset = aReflowInput.ComputedPhysicalBorderPadding().left;
145 0 : nscoord yoffset = aReflowInput.ComputedPhysicalBorderPadding().top;
146 :
147 : // NOTE: Introduce a new function getPosition in the content part ?
148 0 : HTMLMeterElement* meterElement = static_cast<HTMLMeterElement*>(mContent);
149 :
150 0 : double max = meterElement->Max();
151 0 : double min = meterElement->Min();
152 0 : double value = meterElement->Value();
153 :
154 0 : double position = max - min;
155 0 : position = position != 0 ? (value - min) / position : 1;
156 :
157 0 : size = NSToCoordRound(size * position);
158 :
159 0 : if (!vertical && (wm.IsVertical() ? wm.IsVerticalRL() : !wm.IsBidiLTR())) {
160 0 : xoffset += aReflowInput.ComputedWidth() - size;
161 : }
162 :
163 : // The bar position is *always* constrained.
164 0 : if (vertical) {
165 : // We want the bar to begin at the bottom.
166 0 : yoffset += aReflowInput.ComputedHeight() - size;
167 :
168 0 : size -= reflowInput.ComputedPhysicalMargin().TopBottom() +
169 0 : reflowInput.ComputedPhysicalBorderPadding().TopBottom();
170 0 : size = std::max(size, 0);
171 0 : reflowInput.SetComputedHeight(size);
172 : } else {
173 0 : size -= reflowInput.ComputedPhysicalMargin().LeftRight() +
174 0 : reflowInput.ComputedPhysicalBorderPadding().LeftRight();
175 0 : size = std::max(size, 0);
176 0 : reflowInput.SetComputedWidth(size);
177 : }
178 :
179 0 : xoffset += reflowInput.ComputedPhysicalMargin().left;
180 0 : yoffset += reflowInput.ComputedPhysicalMargin().top;
181 :
182 0 : ReflowOutput barDesiredSize(reflowInput);
183 0 : ReflowChild(aBarFrame, aPresContext, barDesiredSize, reflowInput, xoffset,
184 0 : yoffset, 0, aStatus);
185 : FinishReflowChild(aBarFrame, aPresContext, barDesiredSize, &reflowInput,
186 0 : xoffset, yoffset, 0);
187 0 : }
188 :
189 : nsresult
190 0 : nsMeterFrame::AttributeChanged(int32_t aNameSpaceID,
191 : nsIAtom* aAttribute,
192 : int32_t aModType)
193 : {
194 0 : NS_ASSERTION(mBarDiv, "Meter bar div must exist!");
195 :
196 0 : if (aNameSpaceID == kNameSpaceID_None &&
197 0 : (aAttribute == nsGkAtoms::value ||
198 0 : aAttribute == nsGkAtoms::max ||
199 0 : aAttribute == nsGkAtoms::min )) {
200 0 : nsIFrame* barFrame = mBarDiv->GetPrimaryFrame();
201 0 : NS_ASSERTION(barFrame, "The meter frame should have a child with a frame!");
202 0 : PresContext()->PresShell()->FrameNeedsReflow(barFrame,
203 : nsIPresShell::eResize,
204 0 : NS_FRAME_IS_DIRTY);
205 0 : InvalidateFrame();
206 : }
207 :
208 0 : return nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute,
209 0 : aModType);
210 : }
211 :
212 : LogicalSize
213 0 : nsMeterFrame::ComputeAutoSize(gfxContext* aRenderingContext,
214 : WritingMode aWM,
215 : const LogicalSize& aCBSize,
216 : nscoord aAvailableISize,
217 : const LogicalSize& aMargin,
218 : const LogicalSize& aBorder,
219 : const LogicalSize& aPadding,
220 : ComputeSizeFlags aFlags)
221 : {
222 : RefPtr<nsFontMetrics> fontMet =
223 0 : nsLayoutUtils::GetFontMetricsForFrame(this, 1.0f);
224 :
225 0 : const WritingMode wm = GetWritingMode();
226 0 : LogicalSize autoSize(wm);
227 0 : autoSize.BSize(wm) = autoSize.ISize(wm) = fontMet->Font().size; // 1em
228 :
229 0 : if (ResolvedOrientationIsVertical() == wm.IsVertical()) {
230 0 : autoSize.ISize(wm) *= 5; // 5em
231 : } else {
232 0 : autoSize.BSize(wm) *= 5; // 5em
233 : }
234 :
235 0 : return autoSize.ConvertTo(aWM, wm);
236 : }
237 :
238 : nscoord
239 0 : nsMeterFrame::GetMinISize(gfxContext *aRenderingContext)
240 : {
241 : RefPtr<nsFontMetrics> fontMet =
242 0 : nsLayoutUtils::GetFontMetricsForFrame(this, 1.0f);
243 :
244 0 : nscoord minISize = fontMet->Font().size; // 1em
245 :
246 0 : if (ResolvedOrientationIsVertical() == GetWritingMode().IsVertical()) {
247 : // The orientation is inline
248 0 : minISize *= 5; // 5em
249 : }
250 :
251 0 : return minISize;
252 : }
253 :
254 : nscoord
255 0 : nsMeterFrame::GetPrefISize(gfxContext *aRenderingContext)
256 : {
257 0 : return GetMinISize(aRenderingContext);
258 : }
259 :
260 : bool
261 0 : nsMeterFrame::ShouldUseNativeStyle() const
262 : {
263 0 : nsIFrame* barFrame = mBarDiv->GetPrimaryFrame();
264 :
265 : // Use the native style if these conditions are satisfied:
266 : // - both frames use the native appearance;
267 : // - neither frame has author specified rules setting the border or the
268 : // background.
269 0 : return StyleDisplay()->mAppearance == NS_THEME_METERBAR &&
270 0 : !PresContext()->HasAuthorSpecifiedRules(this,
271 0 : NS_AUTHOR_SPECIFIED_BORDER | NS_AUTHOR_SPECIFIED_BACKGROUND) &&
272 0 : barFrame &&
273 0 : barFrame->StyleDisplay()->mAppearance == NS_THEME_METERCHUNK &&
274 0 : !PresContext()->HasAuthorSpecifiedRules(barFrame,
275 0 : NS_AUTHOR_SPECIFIED_BORDER | NS_AUTHOR_SPECIFIED_BACKGROUND);
276 : }
277 :
278 : Element*
279 0 : nsMeterFrame::GetPseudoElement(CSSPseudoElementType aType)
280 : {
281 0 : if (aType == CSSPseudoElementType::mozMeterBar) {
282 0 : return mBarDiv;
283 : }
284 :
285 0 : return nsContainerFrame::GetPseudoElement(aType);
286 : }
|