Backed out changeset b88172246b66 due to Win32 debug failures.
[mozilla-central.git] / layout / mathml / nsMathMLContainerFrame.h
blobfb9f1168131372ecd423c907341c09039b9472ef
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla MathML Project.
17 * The Initial Developer of the Original Code is
18 * The University Of Queensland.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Roger B. Sidje <rbs@maths.uq.edu.au>
24 * David J. Fiddes <D.J.Fiddes@hw.ac.uk>
25 * Shyjan Mahamud <mahamud@cs.cmu.edu> (added TeX rendering rules)
26 * Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #ifndef nsMathMLContainerFrame_h___
43 #define nsMathMLContainerFrame_h___
45 #include "nsCOMPtr.h"
46 #include "nsHTMLContainerFrame.h"
47 #include "nsBlockFrame.h"
48 #include "nsInlineFrame.h"
49 #include "nsMathMLAtoms.h"
50 #include "nsMathMLOperators.h"
51 #include "nsMathMLChar.h"
52 #include "nsMathMLFrame.h"
53 #include "nsMathMLParts.h"
56 * Base class for MathML container frames. It acts like an inferred
57 * mrow. By default, this frame uses its Reflow() method to lay its
58 * children horizontally and ensure that their baselines are aligned.
59 * The Reflow() method relies upon Place() to position children.
60 * By overloading Place() in derived classes, it is therefore possible
61 * to position children in various customized ways.
64 // Options for the preferred size at which to stretch our stretchy children
65 #define STRETCH_CONSIDER_ACTUAL_SIZE 0x00000001 // just use our current size
66 #define STRETCH_CONSIDER_EMBELLISHMENTS 0x00000002 // size calculations include embellishments
68 class nsMathMLContainerFrame : public nsHTMLContainerFrame,
69 public nsMathMLFrame {
70 friend class nsMathMLmfencedFrame;
71 public:
72 nsMathMLContainerFrame(nsStyleContext* aContext) : nsHTMLContainerFrame(aContext) {}
74 NS_DECL_QUERYFRAME
75 NS_DECL_FRAMEARENA_HELPERS
77 // --------------------------------------------------------------------------
78 // Overloaded nsMathMLFrame methods -- see documentation in nsIMathMLFrame.h
80 NS_IMETHOD
81 Stretch(nsIRenderingContext& aRenderingContext,
82 nsStretchDirection aStretchDirection,
83 nsBoundingMetrics& aContainerSize,
84 nsHTMLReflowMetrics& aDesiredStretchSize);
86 NS_IMETHOD
87 UpdatePresentationDataFromChildAt(PRInt32 aFirstIndex,
88 PRInt32 aLastIndex,
89 PRUint32 aFlagsValues,
90 PRUint32 aFlagsToUpdate)
92 PropagatePresentationDataFromChildAt(this, aFirstIndex, aLastIndex,
93 aFlagsValues, aFlagsToUpdate);
94 return NS_OK;
97 // helper to set the "increment script level" flag on the element belonging
98 // to a child frame given by aChildIndex.
99 // When this flag is set, the style system will increment the scriptlevel
100 // for the child element. This is needed for situations where the style system
101 // cannot itself determine the scriptlevel (mfrac, munder, mover, munderover).
102 // This should be called during reflow. We set the flag and if it changed,
103 // we request appropriate restyling and also queue a post-reflow callback
104 // to ensure that restyle and reflow happens immediately after the current
105 // reflow.
106 void
107 SetIncrementScriptLevel(PRInt32 aChildIndex, PRBool aIncrement);
109 // --------------------------------------------------------------------------
110 // Overloaded nsHTMLContainerFrame methods -- see documentation in nsIFrame.h
112 virtual PRBool IsFrameOfType(PRUint32 aFlags) const
114 return !(aFlags & nsIFrame::eLineParticipant) &&
115 nsHTMLContainerFrame::IsFrameOfType(aFlags &
116 ~(nsIFrame::eMathML | nsIFrame::eExcludesIgnorableWhitespace));
119 virtual PRIntn GetSkipSides() const { return 0; }
121 NS_IMETHOD
122 AppendFrames(nsIAtom* aListName,
123 nsFrameList& aFrameList);
125 NS_IMETHOD
126 InsertFrames(nsIAtom* aListName,
127 nsIFrame* aPrevFrame,
128 nsFrameList& aFrameList);
130 NS_IMETHOD
131 RemoveFrame(nsIAtom* aListName,
132 nsIFrame* aOldFrame);
135 * Both GetMinWidth and GetPrefWidth return whatever
136 * GetIntrinsicWidth returns.
138 virtual nscoord GetMinWidth(nsIRenderingContext *aRenderingContext);
139 virtual nscoord GetPrefWidth(nsIRenderingContext *aRenderingContext);
142 * Return the intrinsic width of the frame's content area.
144 virtual nscoord GetIntrinsicWidth(nsIRenderingContext *aRenderingContext);
146 NS_IMETHOD
147 Reflow(nsPresContext* aPresContext,
148 nsHTMLReflowMetrics& aDesiredSize,
149 const nsHTMLReflowState& aReflowState,
150 nsReflowStatus& aStatus);
152 NS_IMETHOD
153 WillReflow(nsPresContext* aPresContext)
155 mPresentationData.flags &= ~NS_MATHML_ERROR;
156 return nsHTMLContainerFrame::WillReflow(aPresContext);
159 NS_IMETHOD
160 DidReflow(nsPresContext* aPresContext,
161 const nsHTMLReflowState* aReflowState,
162 nsDidReflowStatus aStatus)
165 mPresentationData.flags &= ~NS_MATHML_STRETCH_DONE;
166 return nsHTMLContainerFrame::DidReflow(aPresContext, aReflowState, aStatus);
169 NS_IMETHOD BuildDisplayList(nsDisplayListBuilder* aBuilder,
170 const nsRect& aDirtyRect,
171 const nsDisplayListSet& aLists);
173 // Notification when an attribute is changed. The MathML module uses the
174 // following paradigm:
176 // 1. If the MathML frame class doesn't have any cached automatic data that
177 // depends on the attribute: we just reflow (e.g., this happens with <msub>,
178 // <msup>, <mmultiscripts>, etc). This is the default behavior implemented
179 // by this base class.
181 // 2. If the MathML frame class has cached automatic data that depends on
182 // the attribute:
183 // 2a. If the automatic data to update resides only within the descendants,
184 // we just re-layout them using ReLayoutChildren(this);
185 // (e.g., this happens with <ms>).
186 // 2b. If the automatic data to update affects us in some way, we ask our parent
187 // to re-layout its children using ReLayoutChildren(mParent);
188 // Therefore, there is an overhead here in that our siblings are re-laid
189 // too (e.g., this happens with <mstyle>, <munder>, <mover>, <munderover>).
190 NS_IMETHOD
191 AttributeChanged(PRInt32 aNameSpaceID,
192 nsIAtom* aAttribute,
193 PRInt32 aModType);
195 // --------------------------------------------------------------------------
196 // Additional methods
198 protected:
199 /* Place :
200 * This method is used to measure or position child frames and other
201 * elements. It may be called any number of times with aPlaceOrigin
202 * false to measure, and the final call of the Reflow process before
203 * returning from Reflow() or Stretch() will have aPlaceOrigin true
204 * to position the elements.
206 * IMPORTANT: This method uses GetReflowAndBoundingMetricsFor() which must
207 * have been set up with SaveReflowAndBoundingMetricsFor().
209 * The Place() method will use this information to compute the desired size
210 * of the frame.
212 * @param aPlaceOrigin [in]
213 * If aPlaceOrigin is false, compute your desired size using the
214 * information from GetReflowAndBoundingMetricsFor. However, child
215 * frames or other elements should not be repositioned.
217 * If aPlaceOrigin is true, reflow is finished. You should position
218 * all your children, and return your desired size. You should now
219 * use FinishReflowChild() on your children to complete post-reflow
220 * operations.
222 * @param aDesiredSize [out] parameter where you should return your desired
223 * size and your ascent/descent info. Compute your desired size using
224 * the information from GetReflowAndBoundingMetricsFor, and include
225 * any space you want for border/padding in the desired size you
226 * return.
228 virtual nsresult
229 Place(nsIRenderingContext& aRenderingContext,
230 PRBool aPlaceOrigin,
231 nsHTMLReflowMetrics& aDesiredSize);
233 // MeasureForWidth:
235 // A method used by nsMathMLContainerFrame::GetIntrinsicWidth to get the
236 // width that a particular Place method desires. For most frames, this will
237 // just call the object's Place method. However <msqrt> and <menclose> use
238 // nsMathMLContainerFrame::GetIntrinsicWidth to measure the child frames as
239 // if in an <mrow>, and so their frames implement MeasureForWidth to use
240 // nsMathMLContainerFrame::Place.
241 virtual nsresult
242 MeasureForWidth(nsIRenderingContext& aRenderingContext,
243 nsHTMLReflowMetrics& aDesiredSize);
246 // helper to re-sync the automatic data in our children and notify our parent to
247 // reflow us when changes (e.g., append/insert/remove) happen in our child list
248 virtual nsresult
249 ChildListChanged(PRInt32 aModType);
251 // helper to get the preferred size that a container frame should use to fire
252 // the stretch on its stretchy child frames.
253 void
254 GetPreferredStretchSize(nsIRenderingContext& aRenderingContext,
255 PRUint32 aOptions,
256 nsStretchDirection aStretchDirection,
257 nsBoundingMetrics& aPreferredStretchSize);
259 public:
260 // error handlers to provide a visual feedback to the user when an error
261 // (typically invalid markup) was encountered during reflow.
262 nsresult
263 ReflowError(nsIRenderingContext& aRenderingContext,
264 nsHTMLReflowMetrics& aDesiredSize);
266 // helper method to reflow a child frame. We are inline frames, and we don't
267 // know our positions until reflow is finished. That's why we ask the
268 // base method not to worry about our position.
269 nsresult
270 ReflowChild(nsIFrame* aKidFrame,
271 nsPresContext* aPresContext,
272 nsHTMLReflowMetrics& aDesiredSize,
273 const nsHTMLReflowState& aReflowState,
274 nsReflowStatus& aStatus);
276 protected:
277 // helper to add the inter-spacing when <math> is the immediate parent.
278 // Since we don't (yet) handle the root <math> element ourselves, we need to
279 // take special care of the inter-frame spacing on elements for which <math>
280 // is the direct xml parent. This function will be repeatedly called from
281 // left to right on the childframes of <math>, and by so doing it will
282 // emulate the spacing that would have been done by a <mrow> container.
283 // e.g., it fixes <math> <mi>f</mi> <mo>q</mo> <mi>f</mi> <mo>I</mo> </math>
284 virtual nscoord
285 FixInterFrameSpacing(nsHTMLReflowMetrics& aDesiredSize);
287 // helper method to complete the post-reflow hook and ensure that embellished
288 // operators don't terminate their Reflow without receiving a Stretch command.
289 virtual nsresult
290 FinalizeReflow(nsIRenderingContext& aRenderingContext,
291 nsHTMLReflowMetrics& aDesiredSize);
293 // Record metrics of a child frame for recovery through the following method
294 static void
295 SaveReflowAndBoundingMetricsFor(nsIFrame* aFrame,
296 const nsHTMLReflowMetrics& aReflowMetrics,
297 const nsBoundingMetrics& aBoundingMetrics);
299 // helper method to facilitate getting the reflow and bounding metrics of a
300 // child frame. The argument aMathMLFrameType, when non null, will return
301 // the 'type' of the frame, which is used to determine the inter-frame
302 // spacing.
303 // IMPORTANT: This function is only meant to be called in Place() methods as
304 // the information is available only when set up with the above method
305 // during Reflow/Stretch() and GetPrefWidth().
306 static void
307 GetReflowAndBoundingMetricsFor(nsIFrame* aFrame,
308 nsHTMLReflowMetrics& aReflowMetrics,
309 nsBoundingMetrics& aBoundingMetrics,
310 eMathMLFrameType* aMathMLFrameType = nsnull);
312 // helper method to clear metrics saved with
313 // SaveReflowAndBoundingMetricsFor() from all child frames.
314 void ClearSavedChildMetrics();
316 // helper to let the update of presentation data pass through
317 // a subtree that may contain non-MathML container frames
318 static void
319 PropagatePresentationDataFor(nsIFrame* aFrame,
320 PRUint32 aFlagsValues,
321 PRUint32 aFlagsToUpdate);
323 public:
324 static void
325 PropagatePresentationDataFromChildAt(nsIFrame* aParentFrame,
326 PRInt32 aFirstChildIndex,
327 PRInt32 aLastChildIndex,
328 PRUint32 aFlagsValues,
329 PRUint32 aFlagsToUpdate);
331 // helper to let the rebuild of automatic data (presentation data
332 // and embellishement data) walk through a subtree that may contain
333 // non-MathML container frames. Note that this method re-builds the
334 // automatic data in the children -- not in aParentFrame itself (except
335 // for those particular operations that the parent frame may do in its
336 // TransmitAutomaticData()). The reason it works this way is because
337 // a container frame knows what it wants for its children, whereas children
338 // have no clue who their parent is. For example, it is <mfrac> who knows
339 // that its children have to be in scriptsizes, and has to transmit this
340 // information to them. Hence, when changes occur in a child frame, the child
341 // has to request the re-build from its parent. Unfortunately, the extra cost
342 // for this is that it will re-sync in the siblings of the child as well.
343 static void
344 RebuildAutomaticDataForChildren(nsIFrame* aParentFrame);
346 // helper to blow away the automatic data cached in a frame's subtree and
347 // re-layout its subtree to reflect changes that may have happen. In the
348 // event where aParentFrame isn't a MathML frame, it will first walk up to
349 // the ancestor that is a MathML frame, and re-layout from there -- this is
350 // to guarantee that automatic data will be rebuilt properly. Note that this
351 // method re-builds the automatic data in the children -- not in the parent
352 // frame itself (except for those particular operations that the parent frame
353 // may do do its TransmitAutomaticData()). @see RebuildAutomaticDataForChildren
355 // aBits are the bits to pass to FrameNeedsReflow() when we call it.
356 static nsresult
357 ReLayoutChildren(nsIFrame* aParentFrame);
359 protected:
360 // Helper method which positions child frames as an <mrow> on given baseline
361 // y = aBaseline starting from x = aOffsetX, calling FinishReflowChild()
362 // on the frames.
363 void
364 PositionRowChildFrames(nscoord aOffsetX, nscoord aBaseline);
366 // A variant on FinishAndStoreOverflow() that uses the union of child
367 // overflows, the frame bounds, and mBoundingMetrics to set and store the
368 // overflow.
369 void GatherAndStoreOverflow(nsHTMLReflowMetrics* aMetrics);
372 * Call DidReflow() if the NS_FRAME_IN_REFLOW frame bit is set on aFirst and
373 * all its next siblings up to, but not including, aStop.
374 * aStop == nsnull meaning all next siblings with the bit set.
375 * The method does nothing if aFirst == nsnull.
377 static void DidReflowChildren(nsIFrame* aFirst, nsIFrame* aStop = nsnull);
379 private:
380 class RowChildFrameIterator;
381 friend class RowChildFrameIterator;
385 // --------------------------------------------------------------------------
386 // Currently, to benefit from line-breaking inside the <math> element, <math> is
387 // simply mapping to nsBlockFrame or nsInlineFrame.
388 // A separate implemention needs to provide:
389 // 1) line-breaking
390 // 2) proper inter-frame spacing
391 // 3) firing of Stretch() (in which case FinalizeReflow() would have to be cleaned)
392 // Issues: If/when mathml becomes a pluggable component, the separation will be needed.
393 class nsMathMLmathBlockFrame : public nsBlockFrame {
394 public:
395 NS_DECL_FRAMEARENA_HELPERS
397 friend nsIFrame* NS_NewMathMLmathBlockFrame(nsIPresShell* aPresShell,
398 nsStyleContext* aContext, PRUint32 aFlags);
400 // beware, mFrames is not set by nsBlockFrame
401 // cannot use mFrames{.FirstChild()|.etc} since the block code doesn't set mFrames
402 NS_IMETHOD
403 SetInitialChildList(nsIAtom* aListName,
404 nsFrameList& aChildList)
406 NS_ASSERTION(!aListName, "unexpected frame list");
407 nsresult rv = nsBlockFrame::SetInitialChildList(aListName, aChildList);
408 // re-resolve our subtree to set any mathml-expected data
409 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
410 return rv;
413 NS_IMETHOD
414 AppendFrames(nsIAtom* aListName,
415 nsFrameList& aFrameList)
417 NS_ASSERTION(!aListName || nsGkAtoms::nextBidi == aListName,
418 "unexpected frame list");
419 nsresult rv = nsBlockFrame::AppendFrames(aListName, aFrameList);
420 if (NS_LIKELY(!aListName))
421 nsMathMLContainerFrame::ReLayoutChildren(this);
422 return rv;
425 NS_IMETHOD
426 InsertFrames(nsIAtom* aListName,
427 nsIFrame* aPrevFrame,
428 nsFrameList& aFrameList)
430 NS_ASSERTION(!aListName || nsGkAtoms::nextBidi == aListName,
431 "unexpected frame list");
432 nsresult rv = nsBlockFrame::InsertFrames(aListName, aPrevFrame, aFrameList);
433 if (NS_LIKELY(!aListName))
434 nsMathMLContainerFrame::ReLayoutChildren(this);
435 return rv;
438 NS_IMETHOD
439 RemoveFrame(nsIAtom* aListName,
440 nsIFrame* aOldFrame)
442 NS_ASSERTION(!aListName || nsGkAtoms::nextBidi == aListName,
443 "unexpected frame list");
444 nsresult rv = nsBlockFrame::RemoveFrame(aListName, aOldFrame);
445 if (NS_LIKELY(!aListName))
446 nsMathMLContainerFrame::ReLayoutChildren(this);
447 return rv;
450 virtual PRBool IsFrameOfType(PRUint32 aFlags) const {
451 return nsBlockFrame::IsFrameOfType(aFlags &
452 ~(nsIFrame::eMathML | nsIFrame::eExcludesIgnorableWhitespace));
455 protected:
456 nsMathMLmathBlockFrame(nsStyleContext* aContext) : nsBlockFrame(aContext) {
457 // We should always have a float manager. Not that things can really try
458 // to float out of us anyway, but we need one for line layout.
459 AddStateBits(NS_BLOCK_FLOAT_MGR);
461 virtual ~nsMathMLmathBlockFrame() {}
464 // --------------
466 class nsMathMLmathInlineFrame : public nsInlineFrame {
467 public:
468 NS_DECL_FRAMEARENA_HELPERS
470 friend nsIFrame* NS_NewMathMLmathInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
472 NS_IMETHOD
473 SetInitialChildList(nsIAtom* aListName,
474 nsFrameList& aChildList)
476 NS_ASSERTION(!aListName, "unexpected frame list");
477 nsresult rv = nsInlineFrame::SetInitialChildList(aListName, aChildList);
478 // re-resolve our subtree to set any mathml-expected data
479 nsMathMLContainerFrame::RebuildAutomaticDataForChildren(this);
480 return rv;
483 NS_IMETHOD
484 AppendFrames(nsIAtom* aListName,
485 nsFrameList& aFrameList)
487 NS_ASSERTION(!aListName || nsGkAtoms::nextBidi == aListName,
488 "unexpected frame list");
489 nsresult rv = nsInlineFrame::AppendFrames(aListName, aFrameList);
490 if (NS_LIKELY(!aListName))
491 nsMathMLContainerFrame::ReLayoutChildren(this);
492 return rv;
495 NS_IMETHOD
496 InsertFrames(nsIAtom* aListName,
497 nsIFrame* aPrevFrame,
498 nsFrameList& aFrameList)
500 NS_ASSERTION(!aListName || nsGkAtoms::nextBidi == aListName,
501 "unexpected frame list");
502 nsresult rv = nsInlineFrame::InsertFrames(aListName, aPrevFrame, aFrameList);
503 if (NS_LIKELY(!aListName))
504 nsMathMLContainerFrame::ReLayoutChildren(this);
505 return rv;
508 NS_IMETHOD
509 RemoveFrame(nsIAtom* aListName,
510 nsIFrame* aOldFrame)
512 NS_ASSERTION(!aListName || nsGkAtoms::nextBidi == aListName,
513 "unexpected frame list");
514 nsresult rv = nsInlineFrame::RemoveFrame(aListName, aOldFrame);
515 if (NS_LIKELY(!aListName))
516 nsMathMLContainerFrame::ReLayoutChildren(this);
517 return rv;
520 virtual PRBool IsFrameOfType(PRUint32 aFlags) const {
521 return nsInlineFrame::IsFrameOfType(aFlags &
522 ~(nsIFrame::eMathML | nsIFrame::eExcludesIgnorableWhitespace));
525 protected:
526 nsMathMLmathInlineFrame(nsStyleContext* aContext) : nsInlineFrame(aContext) {}
527 virtual ~nsMathMLmathInlineFrame() {}
530 #endif /* nsMathMLContainerFrame_h___ */