CTTE: RenderObject's createAnonymous() and setDocumentForAnonymous() should take...
[WebKit-https.git] / Source / WebCore / rendering / mathml / RenderMathMLRow.cpp
1 /*
2  * Copyright (C) 2010 Alex Milowski (alex@milowski.com). All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if ENABLE(MATHML)
29
30 #include "RenderMathMLRow.h"
31
32 #include "MathMLNames.h"
33 #include "RenderMathMLOperator.h"
34
35 namespace WebCore {
36
37 using namespace MathMLNames;
38
39 RenderMathMLRow::RenderMathMLRow(Element* element)
40     : RenderMathMLBlock(element)
41 {
42 }
43
44 // FIXME: Change all these createAnonymous... routines to return a PassOwnPtr<>.
45 RenderMathMLRow* RenderMathMLRow::createAnonymousWithParentRenderer(const RenderObject* parent)
46 {
47     RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), FLEX);
48     RenderMathMLRow* newMRow = new (parent->renderArena()) RenderMathMLRow(0);
49     newMRow->setDocumentForAnonymous(parent->document());
50     newMRow->setStyle(newStyle.release());
51     return newMRow;
52 }
53
54 void RenderMathMLRow::computePreferredLogicalWidths()
55 {
56     ASSERT(preferredLogicalWidthsDirty() && needsLayout());
57
58 #ifndef NDEBUG
59     // FIXME: Remove this once mathml stops modifying the render tree here.
60     SetLayoutNeededForbiddenScope layoutForbiddenScope(this, false);
61 #endif
62
63     computeChildrenPreferredLogicalHeights();
64     int stretchLogicalHeight = 0;
65     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
66         if (child->isRenderMathMLBlock()) {
67             RenderMathMLOperator* renderMo = toRenderMathMLBlock(child)->unembellishedOperator();
68             // FIXME: Only skip renderMo if it is stretchy.
69             if (renderMo)
70                 continue;
71         }
72         stretchLogicalHeight = max<int>(stretchLogicalHeight, roundToInt(preferredLogicalHeightAfterSizing(child)));
73     }
74     if (!stretchLogicalHeight)
75         stretchLogicalHeight = style()->fontSize();
76     
77     // Set the sizes of (possibly embellished) stretchy operator children.
78     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
79         if (child->isRenderMathMLBlock()) {
80             RenderMathMLOperator* renderMo = toRenderMathMLBlock(child)->unembellishedOperator();
81             if (renderMo)
82                 renderMo->stretchToHeight(stretchLogicalHeight);
83         }
84     }
85
86     RenderMathMLBlock::computePreferredLogicalWidths();
87     
88     // Shrink our logical width to its probable value now without triggering unnecessary relayout of our children.
89     ASSERT(needsLayout() && logicalWidth() >= maxPreferredLogicalWidth());
90     setLogicalWidth(maxPreferredLogicalWidth());
91 }
92
93 void RenderMathMLRow::layout()
94 {
95     // Our computePreferredLogicalWidths() may change our logical width and then layout our children, which
96     // RenderBlock::layout()'s relayoutChildren logic isn't expecting.
97     if (preferredLogicalWidthsDirty())
98         computePreferredLogicalWidths();
99     
100     RenderMathMLBlock::layout();
101 }
102
103 }
104
105 #endif // ENABLE(MATHML)