|
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 "Event.h" |
|
26 #include "EventNames.h" |
|
27 #include "Frame.h" |
|
28 #include "HTMLNames.h" |
|
29 #include "HTMLTextAreaElement.h" |
|
30 #include "HitTestResult.h" |
|
31 |
|
32 namespace WebCore { |
|
33 |
|
34 RenderTextControlMultiLine::RenderTextControlMultiLine(Node* node, bool placeholderVisible) |
|
35 : RenderTextControl(node, placeholderVisible) |
|
36 { |
|
37 } |
|
38 |
|
39 RenderTextControlMultiLine::~RenderTextControlMultiLine() |
|
40 { |
|
41 if (node()) |
|
42 static_cast<HTMLTextAreaElement*>(node())->rendererWillBeDestroyed(); |
|
43 } |
|
44 |
|
45 void RenderTextControlMultiLine::subtreeHasChanged() |
|
46 { |
|
47 RenderTextControl::subtreeHasChanged(); |
|
48 HTMLTextAreaElement* textArea = static_cast<HTMLTextAreaElement*>(node()); |
|
49 textArea->setFormControlValueMatchesRenderer(false); |
|
50 textArea->setNeedsValidityCheck(); |
|
51 |
|
52 if (!node()->focused()) |
|
53 return; |
|
54 |
|
55 node()->dispatchEvent(Event::create(eventNames().inputEvent, true, false)); |
|
56 |
|
57 if (Frame* frame = this->frame()) |
|
58 frame->textDidChangeInTextArea(textArea); |
|
59 } |
|
60 |
|
61 bool RenderTextControlMultiLine::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction hitTestAction) |
|
62 { |
|
63 if (!RenderTextControl::nodeAtPoint(request, result, x, y, tx, ty, hitTestAction)) |
|
64 return false; |
|
65 |
|
66 bool resultIsTextValueOrPlaceholder |
|
67 = (!m_placeholderVisible && result.innerNode() == innerTextElement()) |
|
68 || (m_placeholderVisible && result.innerNode()->isDescendantOf(innerTextElement())); |
|
69 if (result.innerNode() == node() || resultIsTextValueOrPlaceholder) |
|
70 hitInnerTextElement(result, x, y, tx, ty); |
|
71 |
|
72 return true; |
|
73 } |
|
74 |
|
75 void RenderTextControlMultiLine::forwardEvent(Event* event) |
|
76 { |
|
77 RenderTextControl::forwardEvent(event); |
|
78 } |
|
79 |
|
80 float RenderTextControlMultiLine::getAvgCharWidth(AtomicString family) |
|
81 { |
|
82 // Since Lucida Grande is the default font, we want this to match the width |
|
83 // of Courier New, the default font for textareas in IE, Firefox and Safari Win. |
|
84 // 1229 is the avgCharWidth value in the OS/2 table for Courier New. |
|
85 if (family == AtomicString("Lucida Grande")) |
|
86 return scaleEmToUnits(1229); |
|
87 |
|
88 return RenderTextControl::getAvgCharWidth(family); |
|
89 } |
|
90 |
|
91 int RenderTextControlMultiLine::preferredContentWidth(float charWidth) const |
|
92 { |
|
93 int factor = static_cast<HTMLTextAreaElement*>(node())->cols(); |
|
94 return static_cast<int>(ceilf(charWidth * factor)) + scrollbarThickness(); |
|
95 } |
|
96 |
|
97 void RenderTextControlMultiLine::adjustControlHeightBasedOnLineHeight(int lineHeight) |
|
98 { |
|
99 setHeight(height() + lineHeight * static_cast<HTMLTextAreaElement*>(node())->rows()); |
|
100 } |
|
101 |
|
102 int RenderTextControlMultiLine::baselinePosition(bool, bool) const |
|
103 { |
|
104 return height() + marginTop() + marginBottom(); |
|
105 } |
|
106 |
|
107 void RenderTextControlMultiLine::updateFromElement() |
|
108 { |
|
109 createSubtreeIfNeeded(0); |
|
110 RenderTextControl::updateFromElement(); |
|
111 |
|
112 HTMLTextAreaElement* textArea = static_cast<HTMLTextAreaElement*>(node()); |
|
113 if (m_placeholderVisible) |
|
114 setInnerTextValue(textArea->strippedPlaceholder()); |
|
115 else |
|
116 setInnerTextValue(textArea->value()); |
|
117 } |
|
118 |
|
119 void RenderTextControlMultiLine::cacheSelection(int start, int end) |
|
120 { |
|
121 static_cast<HTMLTextAreaElement*>(node())->cacheSelection(start, end); |
|
122 } |
|
123 |
|
124 PassRefPtr<RenderStyle> RenderTextControlMultiLine::createInnerTextStyle(const RenderStyle* startStyle) const |
|
125 { |
|
126 RefPtr<RenderStyle> textBlockStyle; |
|
127 if (m_placeholderVisible) { |
|
128 if (RenderStyle* pseudoStyle = getCachedPseudoStyle(INPUT_PLACEHOLDER)) |
|
129 textBlockStyle = RenderStyle::clone(pseudoStyle); |
|
130 } |
|
131 if (!textBlockStyle) { |
|
132 textBlockStyle = RenderStyle::create(); |
|
133 textBlockStyle->inheritFrom(startStyle); |
|
134 } |
|
135 |
|
136 adjustInnerTextStyle(startStyle, textBlockStyle.get()); |
|
137 textBlockStyle->setDisplay(BLOCK); |
|
138 |
|
139 return textBlockStyle.release(); |
|
140 } |
|
141 |
|
142 RenderStyle* RenderTextControlMultiLine::textBaseStyle() const |
|
143 { |
|
144 return style(); |
|
145 } |
|
146 |
|
147 } |