1/**
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 *           (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23#include "RenderTextControlMultiLine.h"
24
25#include "Frame.h"
26#include "HTMLNames.h"
27#include "HTMLTextAreaElement.h"
28#include "HitTestResult.h"
29#include "ShadowRoot.h"
30#include "StyleInheritedData.h"
31#include "TextControlInnerElements.h"
32
33namespace WebCore {
34
35RenderTextControlMultiLine::RenderTextControlMultiLine(Element* element)
36    : RenderTextControl(element)
37{
38}
39
40RenderTextControlMultiLine::~RenderTextControlMultiLine()
41{
42    if (node() && node()->inDocument())
43        static_cast<HTMLTextAreaElement*>(node())->rendererWillBeDestroyed();
44}
45
46bool RenderTextControlMultiLine::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
47{
48    if (!RenderTextControl::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction))
49        return false;
50
51    if (result.innerNode() == node() || result.innerNode() == innerTextElement())
52        hitInnerTextElement(result, locationInContainer.point(), accumulatedOffset);
53
54    return true;
55}
56
57float RenderTextControlMultiLine::getAvgCharWidth(AtomicString family)
58{
59    // Since Lucida Grande is the default font, we want this to match the width
60    // of Courier New, the default font for textareas in IE, Firefox and Safari Win.
61    // 1229 is the avgCharWidth value in the OS/2 table for Courier New.
62    if (family == "Lucida Grande")
63        return scaleEmToUnits(1229);
64
65    return RenderTextControl::getAvgCharWidth(family);
66}
67
68LayoutUnit RenderTextControlMultiLine::preferredContentLogicalWidth(float charWidth) const
69{
70    int factor = static_cast<HTMLTextAreaElement*>(node())->cols();
71    return static_cast<LayoutUnit>(ceilf(charWidth * factor)) + scrollbarThickness();
72}
73
74LayoutUnit RenderTextControlMultiLine::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const
75{
76    return lineHeight * static_cast<HTMLTextAreaElement*>(node())->rows() + nonContentHeight;
77}
78
79int RenderTextControlMultiLine::baselinePosition(FontBaseline baselineType, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
80{
81    return RenderBox::baselinePosition(baselineType, firstLine, direction, linePositionMode);
82}
83
84PassRefPtr<RenderStyle> RenderTextControlMultiLine::createInnerTextStyle(const RenderStyle* startStyle) const
85{
86    RefPtr<RenderStyle> textBlockStyle = RenderStyle::create();
87    textBlockStyle->inheritFrom(startStyle);
88    adjustInnerTextStyle(startStyle, textBlockStyle.get());
89    textBlockStyle->setDisplay(BLOCK);
90
91    return textBlockStyle.release();
92}
93
94RenderStyle* RenderTextControlMultiLine::textBaseStyle() const
95{
96    return style();
97}
98
99RenderObject* RenderTextControlMultiLine::layoutSpecialExcludedChild(bool relayoutChildren)
100{
101    RenderObject* placeholderRenderer = RenderTextControl::layoutSpecialExcludedChild(relayoutChildren);
102    if (!placeholderRenderer)
103        return 0;
104    if (!placeholderRenderer->isBox())
105        return placeholderRenderer;
106    RenderBox* placeholderBox = toRenderBox(placeholderRenderer);
107    placeholderBox->style()->setLogicalWidth(Length(contentLogicalWidth() - placeholderBox->borderAndPaddingLogicalWidth(), Fixed));
108    placeholderBox->layoutIfNeeded();
109    placeholderBox->setX(borderLeft() + paddingLeft());
110    placeholderBox->setY(borderTop() + paddingTop());
111    return placeholderRenderer;
112}
113
114}
115