| 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 | #include <wtf/IsoMallocInlines.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderTextControlMultiLine); |
| 37 | |
| 38 | RenderTextControlMultiLine::RenderTextControlMultiLine(HTMLTextAreaElement& element, RenderStyle&& style) |
| 39 | : RenderTextControl(element, WTFMove(style)) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | RenderTextControlMultiLine::~RenderTextControlMultiLine() |
| 44 | { |
| 45 | // Do not add any code here. Add it to willBeDestroyed() instead. |
| 46 | } |
| 47 | |
| 48 | void RenderTextControlMultiLine::willBeDestroyed() |
| 49 | { |
| 50 | if (textAreaElement().isConnected()) |
| 51 | textAreaElement().rendererWillBeDestroyed(); |
| 52 | |
| 53 | RenderTextControl::willBeDestroyed(); |
| 54 | } |
| 55 | |
| 56 | HTMLTextAreaElement& RenderTextControlMultiLine::textAreaElement() const |
| 57 | { |
| 58 | return downcast<HTMLTextAreaElement>(RenderTextControl::textFormControlElement()); |
| 59 | } |
| 60 | |
| 61 | bool RenderTextControlMultiLine::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) |
| 62 | { |
| 63 | if (!RenderTextControl::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction)) |
| 64 | return false; |
| 65 | |
| 66 | if (result.innerNode() == &textAreaElement() || result.innerNode() == innerTextElement()) |
| 67 | hitInnerTextElement(result, locationInContainer.point(), accumulatedOffset); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | float RenderTextControlMultiLine::getAverageCharWidth() |
| 73 | { |
| 74 | #if !PLATFORM(IOS_FAMILY) |
| 75 | // Since Lucida Grande is the default font, we want this to match the width |
| 76 | // of Courier New, the default font for textareas in IE, Firefox and Safari Win. |
| 77 | // 1229 is the avgCharWidth value in the OS/2 table for Courier New. |
| 78 | if (style().fontCascade().firstFamily() == "Lucida Grande" ) |
| 79 | return scaleEmToUnits(1229); |
| 80 | #endif |
| 81 | |
| 82 | return RenderTextControl::getAverageCharWidth(); |
| 83 | } |
| 84 | |
| 85 | LayoutUnit RenderTextControlMultiLine::preferredContentLogicalWidth(float charWidth) const |
| 86 | { |
| 87 | return ceilf(charWidth * textAreaElement().cols()) + scrollbarThickness(); |
| 88 | } |
| 89 | |
| 90 | LayoutUnit RenderTextControlMultiLine::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const |
| 91 | { |
| 92 | return lineHeight * textAreaElement().rows() + nonContentHeight; |
| 93 | } |
| 94 | |
| 95 | int RenderTextControlMultiLine::baselinePosition(FontBaseline baselineType, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const |
| 96 | { |
| 97 | return RenderBox::baselinePosition(baselineType, firstLine, direction, linePositionMode); |
| 98 | } |
| 99 | |
| 100 | void RenderTextControlMultiLine::layoutExcludedChildren(bool relayoutChildren) |
| 101 | { |
| 102 | RenderTextControl::layoutExcludedChildren(relayoutChildren); |
| 103 | HTMLElement* placeholder = textFormControlElement().placeholderElement(); |
| 104 | RenderElement* placeholderRenderer = placeholder ? placeholder->renderer() : 0; |
| 105 | if (!placeholderRenderer) |
| 106 | return; |
| 107 | if (is<RenderBox>(placeholderRenderer)) { |
| 108 | auto& placeholderBox = downcast<RenderBox>(*placeholderRenderer); |
| 109 | placeholderBox.mutableStyle().setLogicalWidth(Length(contentLogicalWidth() - placeholderBox.borderAndPaddingLogicalWidth(), Fixed)); |
| 110 | placeholderBox.layoutIfNeeded(); |
| 111 | placeholderBox.setX(borderLeft() + paddingLeft()); |
| 112 | placeholderBox.setY(borderTop() + paddingTop()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |