Coverage Report

Created: 2018-09-25 14:53

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