nsMeterFrame.cpp 9.4 KB

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