Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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/. */
6 #ifndef nsMathMLContainerFrame_h___
7 #define nsMathMLContainerFrame_h___
9 #include "mozilla/Attributes.h"
10 #include "nsContainerFrame.h"
11 #include "nsBlockFrame.h"
12 #include "nsInlineFrame.h"
13 #include "nsMathMLOperators.h"
14 #include "nsMathMLFrame.h"
15 #include "mozilla/Likely.h"
17 /*
18 * Base class for MathML container frames. It acts like an inferred
19 * mrow. By default, this frame uses its Reflow() method to lay its
20 * children horizontally and ensure that their baselines are aligned.
21 * The Reflow() method relies upon Place() to position children.
22 * By overloading Place() in derived classes, it is therefore possible
23 * to position children in various customized ways.
24 */
26 // Options for the preferred size at which to stretch our stretchy children
27 #define STRETCH_CONSIDER_ACTUAL_SIZE 0x00000001 // just use our current size
28 #define STRETCH_CONSIDER_EMBELLISHMENTS 0x00000002 // size calculations include embellishments
30 class nsMathMLContainerFrame : public nsContainerFrame,
31 public nsMathMLFrame {
32 friend class nsMathMLmfencedFrame;
33 public:
34 nsMathMLContainerFrame(nsStyleContext* aContext) : nsContainerFrame(aContext) {}
36 NS_DECL_QUERYFRAME_TARGET(nsMathMLContainerFrame)
37 NS_DECL_QUERYFRAME
38 NS_DECL_FRAMEARENA_HELPERS
40 // --------------------------------------------------------------------------
41 // Overloaded nsMathMLFrame methods -- see documentation in nsIMathMLFrame.h
43 NS_IMETHOD
44 Stretch(nsRenderingContext& aRenderingContext,
45 nsStretchDirection aStretchDirection,
46 nsBoundingMetrics& aContainerSize,
47 nsHTMLReflowMetrics& aDesiredStretchSize) MOZ_OVERRIDE;
49 NS_IMETHOD
50 UpdatePresentationDataFromChildAt(int32_t aFirstIndex,
51 int32_t aLastIndex,
52 uint32_t aFlagsValues,
53 uint32_t aFlagsToUpdate) MOZ_OVERRIDE
54 {
55 PropagatePresentationDataFromChildAt(this, aFirstIndex, aLastIndex,
56 aFlagsValues, aFlagsToUpdate);
57 return NS_OK;
58 }
60 // helper to set the "increment script level" flag on the element belonging
61 // to a child frame given by aChildIndex.
62 // When this flag is set, the style system will increment the scriptlevel
63 // for the child element. This is needed for situations where the style system
64 // cannot itself determine the scriptlevel (mfrac, munder, mover, munderover).
65 // This should be called during reflow. We set the flag and if it changed,
66 // we request appropriate restyling and also queue a post-reflow callback
67 // to ensure that restyle and reflow happens immediately after the current
68 // reflow.
69 void
70 SetIncrementScriptLevel(int32_t aChildIndex, bool aIncrement);
72 // --------------------------------------------------------------------------
73 // Overloaded nsContainerFrame methods -- see documentation in nsIFrame.h
75 virtual bool IsFrameOfType(uint32_t aFlags) const MOZ_OVERRIDE
76 {
77 return !(aFlags & nsIFrame::eLineParticipant) &&
78 nsContainerFrame::IsFrameOfType(aFlags &
79 ~(nsIFrame::eMathML | nsIFrame::eExcludesIgnorableWhitespace));
80 }
82 virtual nsresult
83 AppendFrames(ChildListID aListID,
84 nsFrameList& aFrameList) MOZ_OVERRIDE;
86 virtual nsresult
87 InsertFrames(ChildListID aListID,
88 nsIFrame* aPrevFrame,
89 nsFrameList& aFrameList) MOZ_OVERRIDE;
91 virtual nsresult
92 RemoveFrame(ChildListID aListID,
93 nsIFrame* aOldFrame) MOZ_OVERRIDE;
95 /**
96 * Both GetMinWidth and GetPrefWidth use the intrinsic width metrics
97 * returned by GetIntrinsicMetrics, including ink overflow.
98 */
99 virtual nscoord GetMinWidth(nsRenderingContext *aRenderingContext) MOZ_OVERRIDE;
100 virtual nscoord GetPrefWidth(nsRenderingContext *aRenderingContext) MOZ_OVERRIDE;
102 /**
103 * Return the intrinsic horizontal metrics of the frame's content area.
104 */
105 virtual void
106 GetIntrinsicWidthMetrics(nsRenderingContext* aRenderingContext,
107 nsHTMLReflowMetrics& aDesiredSize);
109 virtual nsresult
110 Reflow(nsPresContext* aPresContext,
111 nsHTMLReflowMetrics& aDesiredSize,
112 const nsHTMLReflowState& aReflowState,
113 nsReflowStatus& aStatus) MOZ_OVERRIDE;
115 virtual nsresult
116 WillReflow(nsPresContext* aPresContext) MOZ_OVERRIDE
117 {
118 mPresentationData.flags &= ~NS_MATHML_ERROR;
119 return nsContainerFrame::WillReflow(aPresContext);
120 }
122 virtual nsresult
123 DidReflow(nsPresContext* aPresContext,
124 const nsHTMLReflowState* aReflowState,
125 nsDidReflowStatus aStatus) MOZ_OVERRIDE
127 {
128 mPresentationData.flags &= ~NS_MATHML_STRETCH_DONE;
129 return nsContainerFrame::DidReflow(aPresContext, aReflowState, aStatus);
130 }
132 virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
133 const nsRect& aDirtyRect,
134 const nsDisplayListSet& aLists) MOZ_OVERRIDE;
136 virtual bool UpdateOverflow() MOZ_OVERRIDE;
138 // Notification when an attribute is changed. The MathML module uses the
139 // following paradigm:
140 //
141 // 1. If the MathML frame class doesn't have any cached automatic data that
142 // depends on the attribute: we just reflow (e.g., this happens with <msub>,
143 // <msup>, <mmultiscripts>, etc). This is the default behavior implemented
144 // by this base class.
145 //
146 // 2. If the MathML frame class has cached automatic data that depends on
147 // the attribute:
148 // 2a. If the automatic data to update resides only within the descendants,
149 // we just re-layout them using ReLayoutChildren(this);
150 // (e.g., this happens with <ms>).
151 // 2b. If the automatic data to update affects us in some way, we ask our parent
152 // to re-layout its children using ReLayoutChildren(mParent);
153 // Therefore, there is an overhead here in that our siblings are re-laid
154 // too (e.g., this happens with <munder>, <mover>, <munderover>).
155 virtual nsresult
156 AttributeChanged(int32_t aNameSpaceID,
157 nsIAtom* aAttribute,
158 int32_t aModType) MOZ_OVERRIDE;
160 // helper function to apply mirroring to a horizontal coordinate, if needed.
161 nscoord
162 MirrorIfRTL(nscoord aParentWidth, nscoord aChildWidth, nscoord aChildLeading)
163 {
164 return (StyleVisibility()->mDirection ?
165 aParentWidth - aChildWidth - aChildLeading : aChildLeading);
166 }
168 // --------------------------------------------------------------------------
169 // Additional methods
171 protected:
172 /* Place :
173 * This method is used to measure or position child frames and other
174 * elements. It may be called any number of times with aPlaceOrigin
175 * false to measure, and the final call of the Reflow process before
176 * returning from Reflow() or Stretch() will have aPlaceOrigin true
177 * to position the elements.
178 *
179 * IMPORTANT: This method uses GetReflowAndBoundingMetricsFor() which must
180 * have been set up with SaveReflowAndBoundingMetricsFor().
181 *
182 * The Place() method will use this information to compute the desired size
183 * of the frame.
184 *
185 * @param aPlaceOrigin [in]
186 * If aPlaceOrigin is false, compute your desired size using the
187 * information from GetReflowAndBoundingMetricsFor. However, child
188 * frames or other elements should not be repositioned.
189 *
190 * If aPlaceOrigin is true, reflow is finished. You should position
191 * all your children, and return your desired size. You should now
192 * use FinishReflowChild() on your children to complete post-reflow
193 * operations.
194 *
195 * @param aDesiredSize [out] parameter where you should return your desired
196 * size and your ascent/descent info. Compute your desired size using
197 * the information from GetReflowAndBoundingMetricsFor, and include
198 * any space you want for border/padding in the desired size you
199 * return.
200 */
201 virtual nsresult
202 Place(nsRenderingContext& aRenderingContext,
203 bool aPlaceOrigin,
204 nsHTMLReflowMetrics& aDesiredSize);
206 // MeasureForWidth:
207 //
208 // A method used by nsMathMLContainerFrame::GetIntrinsicWidth to get the
209 // width that a particular Place method desires. For most frames, this will
210 // just call the object's Place method. However <msqrt> and <menclose> use
211 // nsMathMLContainerFrame::GetIntrinsicWidth to measure the child frames as
212 // if in an <mrow>, and so their frames implement MeasureForWidth to use
213 // nsMathMLContainerFrame::Place.
214 virtual nsresult
215 MeasureForWidth(nsRenderingContext& aRenderingContext,
216 nsHTMLReflowMetrics& aDesiredSize);
219 // helper to re-sync the automatic data in our children and notify our parent to
220 // reflow us when changes (e.g., append/insert/remove) happen in our child list
221 virtual nsresult
222 ChildListChanged(int32_t aModType);
224 // helper to get the preferred size that a container frame should use to fire
225 // the stretch on its stretchy child frames.
226 void
227 GetPreferredStretchSize(nsRenderingContext& aRenderingContext,
228 uint32_t aOptions,
229 nsStretchDirection aStretchDirection,
230 nsBoundingMetrics& aPreferredStretchSize);
232 // helper used by mstyle, mphantom, mpadded and mrow in their implementation
233 // of TransmitAutomaticData() to determine whether they are space-like.
234 nsresult
235 TransmitAutomaticDataForMrowLikeElement();
237 public:
238 // error handlers to provide a visual feedback to the user when an error
239 // (typically invalid markup) was encountered during reflow.
240 nsresult
241 ReflowError(nsRenderingContext& aRenderingContext,
242 nsHTMLReflowMetrics& aDesiredSize);
243 /*
244 * Helper to call ReportErrorToConsole for parse errors involving
245 * attribute/value pairs.
246 * @param aAttribute The attribute for which the parse error occured.
247 * @param aValue The value for which the parse error occured.
248 */
249 nsresult
250 ReportParseError(const char16_t* aAttribute,
251 const char16_t* aValue);
253 /*
254 * Helper to call ReportErrorToConsole when certain tags
255 * have more than the expected amount of children.
256 */
257 nsresult
258 ReportChildCountError();
260 /*
261 * Helper to call ReportErrorToConsole when certain tags have
262 * invalid child tags
263 * @param aChildTag The tag which is forbidden in this context
264 */
265 nsresult
266 ReportInvalidChildError(nsIAtom* aChildTag);
268 /*
269 * Helper to call ReportToConsole when an error occurs.
270 * @param aParams see nsContentUtils::ReportToConsole
271 */
272 nsresult
273 ReportErrorToConsole(const char* aErrorMsgId,
274 const char16_t** aParams = nullptr,
275 uint32_t aParamCount = 0);
277 // helper method to reflow a child frame. We are inline frames, and we don't
278 // know our positions until reflow is finished. That's why we ask the
279 // base method not to worry about our position.
280 nsresult
281 ReflowChild(nsIFrame* aKidFrame,
282 nsPresContext* aPresContext,
283 nsHTMLReflowMetrics& aDesiredSize,
284 const nsHTMLReflowState& aReflowState,
285 nsReflowStatus& aStatus);
287 protected:
288 // helper to add the inter-spacing when <math> is the immediate parent.
289 // Since we don't (yet) handle the root <math> element ourselves, we need to
290 // take special care of the inter-frame spacing on elements for which <math>
291 // is the direct xml parent. This function will be repeatedly called from
292 // left to right on the childframes of <math>, and by so doing it will
293 // emulate the spacing that would have been done by a <mrow> container.
294 // e.g., it fixes <math> <mi>f</mi> <mo>q</mo> <mi>f</mi> <mo>I</mo> </math>
295 virtual nscoord
296 FixInterFrameSpacing(nsHTMLReflowMetrics& aDesiredSize);
298 // helper method to complete the post-reflow hook and ensure that embellished
299 // operators don't terminate their Reflow without receiving a Stretch command.
300 virtual nsresult
301 FinalizeReflow(nsRenderingContext& aRenderingContext,
302 nsHTMLReflowMetrics& aDesiredSize);
304 // Record metrics of a child frame for recovery through the following method
305 static void
306 SaveReflowAndBoundingMetricsFor(nsIFrame* aFrame,
307 const nsHTMLReflowMetrics& aReflowMetrics,
308 const nsBoundingMetrics& aBoundingMetrics);
310 // helper method to facilitate getting the reflow and bounding metrics of a
311 // child frame. The argument aMathMLFrameType, when non null, will return
312 // the 'type' of the frame, which is used to determine the inter-frame
313 // spacing.
314 // IMPORTANT: This function is only meant to be called in Place() methods as
315 // the information is available only when set up with the above method
316 // during Reflow/Stretch() and GetPrefWidth().
317 static void
318 GetReflowAndBoundingMetricsFor(nsIFrame* aFrame,
319 nsHTMLReflowMetrics& aReflowMetrics,
320 nsBoundingMetrics& aBoundingMetrics,
321 eMathMLFrameType* aMathMLFrameType = nullptr);
323 // helper method to clear metrics saved with
324 // SaveReflowAndBoundingMetricsFor() from all child frames.
325 void ClearSavedChildMetrics();
327 // helper to let the update of presentation data pass through
328 // a subtree that may contain non-MathML container frames
329 static void
330 PropagatePresentationDataFor(nsIFrame* aFrame,
331 uint32_t aFlagsValues,
332 uint32_t aFlagsToUpdate);
334 public:
335 static void
336 PropagatePresentationDataFromChildAt(nsIFrame* aParentFrame,
337 int32_t aFirstChildIndex,
338 int32_t aLastChildIndex,
339 uint32_t aFlagsValues,
340 uint32_t aFlagsToUpdate);
342 // Sets flags on aFrame and all descendant frames
343 static void
344 PropagateFrameFlagFor(nsIFrame* aFrame,
345 nsFrameState aFlags);
347 // helper to let the rebuild of automatic data (presentation data
348 // and embellishement data) walk through a subtree that may contain
349 // non-MathML container frames. Note that this method re-builds the
350 // automatic data in the children -- not in aParentFrame itself (except
351 // for those particular operations that the parent frame may do in its
352 // TransmitAutomaticData()). The reason it works this way is because
353 // a container frame knows what it wants for its children, whereas children
354 // have no clue who their parent is. For example, it is <mfrac> who knows
355 // that its children have to be in scriptsizes, and has to transmit this
356 // information to them. Hence, when changes occur in a child frame, the child
357 // has to request the re-build from its parent. Unfortunately, the extra cost
358 // for this is that it will re-sync in the siblings of the child as well.
359 static void
360 RebuildAutomaticDataForChildren(nsIFrame* aParentFrame);
362 // helper to blow away the automatic data cached in a frame's subtree and
363 // re-layout its subtree to reflect changes that may have happen. In the
364 // event where aParentFrame isn't a MathML frame, it will first walk up to
365 // the ancestor that is a MathML frame, and re-layout from there -- this is
366 // to guarantee that automatic data will be rebuilt properly. Note that this
367 // method re-builds the automatic data in the children -- not in the parent
368 // frame itself (except for those particular operations that the parent frame
369 // may do do its TransmitAutomaticData()). @see RebuildAutomaticDataForChildren
370 //
371 // aBits are the bits to pass to FrameNeedsReflow() when we call it.
372 static nsresult
373 ReLayoutChildren(nsIFrame* aParentFrame);
375 protected:
376 // Helper method which positions child frames as an <mrow> on given baseline
377 // y = aBaseline starting from x = aOffsetX, calling FinishReflowChild()
378 // on the frames.
379 void
380 PositionRowChildFrames(nscoord aOffsetX, nscoord aBaseline);
382 // A variant on FinishAndStoreOverflow() that uses the union of child
383 // overflows, the frame bounds, and mBoundingMetrics to set and store the
384 // overflow.
385 void GatherAndStoreOverflow(nsHTMLReflowMetrics* aMetrics);
387 /**
388 * Call DidReflow() if the NS_FRAME_IN_REFLOW frame bit is set on aFirst and
389 * all its next siblings up to, but not including, aStop.
390 * aStop == nullptr meaning all next siblings with the bit set.
391 * The method does nothing if aFirst == nullptr.
392 */
393 static void DidReflowChildren(nsIFrame* aFirst, nsIFrame* aStop = nullptr);
395 private:
396 class RowChildFrameIterator;
397 friend class RowChildFrameIterator;
398 };
401 // --------------------------------------------------------------------------
402 // Currently, to benefit from line-breaking inside the <math> element, <math> is
403 // simply mapping to nsBlockFrame or nsInlineFrame.
404 // A separate implemention needs to provide:
405 // 1) line-breaking
406 // 2) proper inter-frame spacing
407 // 3) firing of Stretch() (in which case FinalizeReflow() would have to be cleaned)
408 // Issues: If/when mathml becomes a pluggable component, the separation will be needed.
409 class nsMathMLmathBlockFrame : public nsBlockFrame {
410 public:
411 NS_DECL_QUERYFRAME_TARGET(nsMathMLmathBlockFrame)
412 NS_DECL_QUERYFRAME
413 NS_DECL_FRAMEARENA_HELPERS
415 friend nsIFrame* NS_NewMathMLmathBlockFrame(nsIPresShell* aPresShell,
416 nsStyleContext* aContext, nsFrameState aFlags);
418 // beware, mFrames is not set by nsBlockFrame
419 // cannot use mFrames{.FirstChild()|.etc} since the block code doesn't set mFrames
420 virtual nsresult
421 SetInitialChildList(ChildListID aListID,
422 nsFrameList& aChildList) MOZ_OVERRIDE
423 {
424 NS_ASSERTION(aListID == kPrincipalList, "unexpected frame list");
425 nsresult rv = nsBlockFrame::SetInitialChildList(aListID, aChildList);
426 // re-resolve our subtree to set any mathml-expected data
427 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
428 return rv;
429 }
431 virtual nsresult
432 AppendFrames(ChildListID aListID,
433 nsFrameList& aFrameList) MOZ_OVERRIDE
434 {
435 NS_ASSERTION(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
436 "unexpected frame list");
437 nsresult rv = nsBlockFrame::AppendFrames(aListID, aFrameList);
438 if (MOZ_LIKELY(aListID == kPrincipalList))
439 nsMathMLContainerFrame::ReLayoutChildren(this);
440 return rv;
441 }
443 virtual nsresult
444 InsertFrames(ChildListID aListID,
445 nsIFrame* aPrevFrame,
446 nsFrameList& aFrameList) MOZ_OVERRIDE
447 {
448 NS_ASSERTION(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
449 "unexpected frame list");
450 nsresult rv = nsBlockFrame::InsertFrames(aListID, aPrevFrame, aFrameList);
451 if (MOZ_LIKELY(aListID == kPrincipalList))
452 nsMathMLContainerFrame::ReLayoutChildren(this);
453 return rv;
454 }
456 virtual nsresult
457 RemoveFrame(ChildListID aListID,
458 nsIFrame* aOldFrame) MOZ_OVERRIDE
459 {
460 NS_ASSERTION(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
461 "unexpected frame list");
462 nsresult rv = nsBlockFrame::RemoveFrame(aListID, aOldFrame);
463 if (MOZ_LIKELY(aListID == kPrincipalList))
464 nsMathMLContainerFrame::ReLayoutChildren(this);
465 return rv;
466 }
468 virtual bool IsFrameOfType(uint32_t aFlags) const MOZ_OVERRIDE {
469 return nsBlockFrame::IsFrameOfType(aFlags &
470 ~(nsIFrame::eMathML | nsIFrame::eExcludesIgnorableWhitespace));
471 }
473 // See nsIMathMLFrame.h
474 bool IsMrowLike() {
475 return mFrames.FirstChild() != mFrames.LastChild() ||
476 !mFrames.FirstChild();
477 }
479 protected:
480 nsMathMLmathBlockFrame(nsStyleContext* aContext) : nsBlockFrame(aContext) {
481 // We should always have a float manager. Not that things can really try
482 // to float out of us anyway, but we need one for line layout.
483 AddStateBits(NS_BLOCK_FLOAT_MGR);
484 }
485 virtual ~nsMathMLmathBlockFrame() {}
486 };
488 // --------------
490 class nsMathMLmathInlineFrame : public nsInlineFrame,
491 public nsMathMLFrame {
492 public:
493 NS_DECL_QUERYFRAME_TARGET(nsMathMLmathInlineFrame)
494 NS_DECL_QUERYFRAME
495 NS_DECL_FRAMEARENA_HELPERS
497 friend nsIFrame* NS_NewMathMLmathInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
499 virtual nsresult
500 SetInitialChildList(ChildListID aListID,
501 nsFrameList& aChildList) MOZ_OVERRIDE
502 {
503 NS_ASSERTION(aListID == kPrincipalList, "unexpected frame list");
504 nsresult rv = nsInlineFrame::SetInitialChildList(aListID, aChildList);
505 // re-resolve our subtree to set any mathml-expected data
506 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
507 return rv;
508 }
510 virtual nsresult
511 AppendFrames(ChildListID aListID,
512 nsFrameList& aFrameList) MOZ_OVERRIDE
513 {
514 NS_ASSERTION(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
515 "unexpected frame list");
516 nsresult rv = nsInlineFrame::AppendFrames(aListID, aFrameList);
517 if (MOZ_LIKELY(aListID == kPrincipalList))
518 nsMathMLContainerFrame::ReLayoutChildren(this);
519 return rv;
520 }
522 virtual nsresult
523 InsertFrames(ChildListID aListID,
524 nsIFrame* aPrevFrame,
525 nsFrameList& aFrameList) MOZ_OVERRIDE
526 {
527 NS_ASSERTION(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
528 "unexpected frame list");
529 nsresult rv = nsInlineFrame::InsertFrames(aListID, aPrevFrame, aFrameList);
530 if (MOZ_LIKELY(aListID == kPrincipalList))
531 nsMathMLContainerFrame::ReLayoutChildren(this);
532 return rv;
533 }
535 virtual nsresult
536 RemoveFrame(ChildListID aListID,
537 nsIFrame* aOldFrame) MOZ_OVERRIDE
538 {
539 NS_ASSERTION(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
540 "unexpected frame list");
541 nsresult rv = nsInlineFrame::RemoveFrame(aListID, aOldFrame);
542 if (MOZ_LIKELY(aListID == kPrincipalList))
543 nsMathMLContainerFrame::ReLayoutChildren(this);
544 return rv;
545 }
547 virtual bool IsFrameOfType(uint32_t aFlags) const MOZ_OVERRIDE {
548 return nsInlineFrame::IsFrameOfType(aFlags &
549 ~(nsIFrame::eMathML | nsIFrame::eExcludesIgnorableWhitespace));
550 }
552 bool
553 IsMrowLike() MOZ_OVERRIDE {
554 return mFrames.FirstChild() != mFrames.LastChild() ||
555 !mFrames.FirstChild();
556 }
558 protected:
559 nsMathMLmathInlineFrame(nsStyleContext* aContext) : nsInlineFrame(aContext) {}
560 virtual ~nsMathMLmathInlineFrame() {}
561 };
563 #endif /* nsMathMLContainerFrame_h___ */