Coverage Report

Created: 2018-09-25 14:53

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