blob: a5db44e9f26c00bd197308787d939681d2a3888f [file] [log] [blame]
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +00001/**
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 "RenderTextControlSingleLine.h"
24
25#include "CSSStyleSelector.h"
26#include "Event.h"
27#include "EventNames.h"
28#include "Frame.h"
29#include "FrameView.h"
30#include "HitTestResult.h"
31#include "HTMLInputElement.h"
32#include "HTMLNames.h"
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +000033#include "InputElement.h"
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +000034#include "LocalizedStrings.h"
35#include "MouseEvent.h"
36#include "PlatformKeyboardEvent.h"
37#include "RenderScrollbar.h"
38#include "RenderTheme.h"
39#include "SearchPopupMenu.h"
40#include "SelectionController.h"
41#include "Settings.h"
ojan@chromium.org149606a2009-04-29 20:31:53 +000042#include "SimpleFontData.h"
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +000043#include "TextControlInnerElements.h"
44
zimmermann@webkit.orgd5c12892008-12-29 00:16:19 +000045using namespace std;
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +000046
47namespace WebCore {
48
49using namespace HTMLNames;
50
51RenderTextControlSingleLine::RenderTextControlSingleLine(Node* node)
52 : RenderTextControl(node)
53 , m_placeholderVisible(false)
54 , m_searchPopupIsVisible(false)
55 , m_shouldDrawCapsLockIndicator(false)
56 , m_searchEventTimer(this, &RenderTextControlSingleLine::searchEventTimerFired)
57 , m_searchPopup(0)
58{
59}
60
61RenderTextControlSingleLine::~RenderTextControlSingleLine()
62{
63 if (m_searchPopup) {
64 m_searchPopup->disconnectClient();
65 m_searchPopup = 0;
66 }
67
68 if (m_innerBlock)
69 m_innerBlock->detach();
70}
71
72bool RenderTextControlSingleLine::placeholderShouldBeVisible() const
73{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +000074 return inputElement()->placeholderShouldBeVisible();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +000075}
76
77void RenderTextControlSingleLine::updatePlaceholderVisibility()
78{
79 RenderStyle* parentStyle = m_innerBlock ? m_innerBlock->renderer()->style() : style();
80
81 RefPtr<RenderStyle> textBlockStyle = createInnerTextStyle(parentStyle);
82 HTMLElement* innerText = innerTextElement();
83 innerText->renderer()->setStyle(textBlockStyle);
84
85 for (Node* n = innerText->firstChild(); n; n = n->traverseNextNode(innerText)) {
86 if (RenderObject* renderer = n->renderer())
87 renderer->setStyle(textBlockStyle);
88 }
89
90 updateFromElement();
91}
92
93void RenderTextControlSingleLine::addSearchResult()
94{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +000095 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +000096 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
97 if (input->maxResults() <= 0)
98 return;
99
100 String value = input->value();
101 if (value.isEmpty())
102 return;
103
104 Settings* settings = document()->settings();
105 if (!settings || settings->privateBrowsingEnabled())
106 return;
107
108 int size = static_cast<int>(m_recentSearches.size());
109 for (int i = size - 1; i >= 0; --i) {
110 if (m_recentSearches[i] == value)
111 m_recentSearches.remove(i);
112 }
113
114 m_recentSearches.insert(0, value);
115 while (static_cast<int>(m_recentSearches.size()) > input->maxResults())
116 m_recentSearches.removeLast();
117
118 const AtomicString& name = autosaveName();
119 if (!m_searchPopup)
120 m_searchPopup = SearchPopupMenu::create(this);
121
122 m_searchPopup->saveRecentSearches(name, m_recentSearches);
123}
124
125void RenderTextControlSingleLine::stopSearchEventTimer()
126{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000127 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000128 m_searchEventTimer.stop();
129}
130
131void RenderTextControlSingleLine::showPopup()
132{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000133 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000134 if (m_searchPopupIsVisible)
135 return;
136
137 if (!m_searchPopup)
138 m_searchPopup = SearchPopupMenu::create(this);
139
140 if (!m_searchPopup->enabled())
141 return;
142
143 m_searchPopupIsVisible = true;
144
145 const AtomicString& name = autosaveName();
146 m_searchPopup->loadRecentSearches(name, m_recentSearches);
147
148 // Trim the recent searches list if the maximum size has changed since we last saved.
149 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
150 if (static_cast<int>(m_recentSearches.size()) > input->maxResults()) {
151 do {
152 m_recentSearches.removeLast();
153 } while (static_cast<int>(m_recentSearches.size()) > input->maxResults());
154
155 m_searchPopup->saveRecentSearches(name, m_recentSearches);
156 }
157
158 m_searchPopup->show(absoluteBoundingBoxRect(true), document()->view(), -1);
159}
160
161void RenderTextControlSingleLine::hidePopup()
162{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000163 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000164 if (m_searchPopup)
165 m_searchPopup->hide();
166
167 m_searchPopupIsVisible = false;
168}
169
170void RenderTextControlSingleLine::subtreeHasChanged()
171{
172 bool wasEdited = isEdited();
173 RenderTextControl::subtreeHasChanged();
174
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000175 InputElement* input = inputElement();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000176 input->setValueFromRenderer(input->constrainValue(text()));
177
simon.fraser@apple.comf71e81d2009-03-11 19:00:13 +0000178 if (m_cancelButton)
179 updateCancelButtonVisibility();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000180
181 // If the incremental attribute is set, then dispatch the search event
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000182 if (input->searchEventsShouldBeDispatched())
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000183 startSearchEventTimer();
184
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000185 if (!wasEdited && node()->focused()) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000186 if (Frame* frame = document()->frame())
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000187 frame->textFieldDidBeginEditing(static_cast<Element*>(node()));
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000188 }
189
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000190 if (node()->focused()) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000191 if (Frame* frame = document()->frame())
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000192 frame->textDidChangeInTextField(static_cast<Element*>(node()));
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000193 }
194}
195
196void RenderTextControlSingleLine::paint(PaintInfo& paintInfo, int tx, int ty)
197{
198 RenderTextControl::paint(paintInfo, tx, ty);
199
200 if (paintInfo.phase == PaintPhaseBlockBackground && m_shouldDrawCapsLockIndicator) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000201 IntRect contentsRect = contentBoxRect();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000202
203 // Convert the rect into the coords used for painting the content
hyatt@apple.comd885df72009-01-22 02:31:52 +0000204 contentsRect.move(tx + x(), ty + y());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000205 theme()->paintCapsLockIndicator(this, paintInfo, contentsRect);
206 }
207}
208
209void RenderTextControlSingleLine::layout()
210{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000211 int oldHeight = height();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000212 calcHeight();
213
hyatt@apple.comd885df72009-01-22 02:31:52 +0000214 int oldWidth = width();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000215 calcWidth();
216
hyatt@apple.comd885df72009-01-22 02:31:52 +0000217 bool relayoutChildren = oldHeight != height() || oldWidth != width();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000218
hyatt@apple.comd885df72009-01-22 02:31:52 +0000219 RenderBox* innerTextRenderer = innerTextElement()->renderBox();
220 RenderBox* innerBlockRenderer = m_innerBlock ? m_innerBlock->renderBox() : 0;
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000221
222 // Set the text block height
223 int desiredHeight = textBlockHeight();
224 int currentHeight = innerTextRenderer->height();
225
mitz@apple.comae136e52009-06-18 17:54:16 +0000226 if (currentHeight > height()) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000227 if (desiredHeight != currentHeight)
228 relayoutChildren = true;
229 innerTextRenderer->style()->setHeight(Length(desiredHeight, Fixed));
mitz@apple.comae136e52009-06-18 17:54:16 +0000230 if (m_innerBlock)
231 innerBlockRenderer->style()->setHeight(Length(desiredHeight, Fixed));
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000232 }
233
234 // Set the text block width
235 int desiredWidth = textBlockWidth();
236 if (desiredWidth != innerTextRenderer->width())
237 relayoutChildren = true;
238 innerTextRenderer->style()->setWidth(Length(desiredWidth, Fixed));
239
240 if (m_innerBlock) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000241 int innerBlockWidth = width() - paddingLeft() - paddingRight() - borderLeft() - borderRight();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000242 if (innerBlockWidth != innerBlockRenderer->width())
243 relayoutChildren = true;
244 innerBlockRenderer->style()->setWidth(Length(innerBlockWidth, Fixed));
245 }
246
247 RenderBlock::layoutBlock(relayoutChildren);
248
mitz@apple.comae136e52009-06-18 17:54:16 +0000249 // Center the child block vertically
250 RenderBox* childBlock = innerBlockRenderer ? innerBlockRenderer : innerTextRenderer;
251 currentHeight = childBlock->height();
252 if (currentHeight < height())
253 childBlock->setLocation(childBlock->x(), (height() - currentHeight) / 2);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000254}
255
hyatt@apple.comd885df72009-01-22 02:31:52 +0000256bool RenderTextControlSingleLine::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction hitTestAction)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000257{
258 // If we're within the text control, we want to act as if we've hit the inner text block element, in case the point
259 // was on the control but not on the inner element (see Radar 4617841).
260
261 // In a search field, we want to act as if we've hit the results block if we're to the left of the inner text block,
262 // and act as if we've hit the close block if we're to the right of the inner text block.
263
hyatt@apple.comd885df72009-01-22 02:31:52 +0000264 if (!RenderTextControl::nodeAtPoint(request, result, xPos, yPos, tx, ty, hitTestAction))
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000265 return false;
266
simon.fraser@apple.com68ed99e2009-03-11 05:43:58 +0000267 // If we hit a node inside the inner text element, say that we hit that element,
268 // and if we hit our node (e.g. we're over the border or padding), also say that we hit the
269 // inner text element so that it gains focus.
270 if (result.innerNode()->isDescendantOf(innerTextElement()) || result.innerNode() == node())
271 hitInnerTextElement(result, xPos, yPos, tx, ty);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000272
simon.fraser@apple.com68ed99e2009-03-11 05:43:58 +0000273 // If we're not a search field, or we already found the results or cancel buttons, we're done.
274 if (!m_innerBlock || result.innerNode() == m_resultsButton || result.innerNode() == m_cancelButton)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000275 return true;
276
277 Node* innerNode = 0;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000278 RenderBox* innerBlockRenderer = m_innerBlock->renderBox();
279 RenderBox* innerTextRenderer = innerTextElement()->renderBox();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000280
281 IntPoint localPoint = result.localPoint();
hyatt@apple.comd885df72009-01-22 02:31:52 +0000282 localPoint.move(-innerBlockRenderer->x(), -innerBlockRenderer->y());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000283
hyatt@apple.comd885df72009-01-22 02:31:52 +0000284 int textLeft = tx + x() + innerBlockRenderer->x() + innerTextRenderer->x();
285 if (m_resultsButton && m_resultsButton->renderer() && xPos < textLeft)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000286 innerNode = m_resultsButton.get();
287
288 if (!innerNode) {
289 int textRight = textLeft + innerTextRenderer->width();
hyatt@apple.comd885df72009-01-22 02:31:52 +0000290 if (m_cancelButton && m_cancelButton->renderer() && xPos > textRight)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000291 innerNode = m_cancelButton.get();
292 }
293
294 if (innerNode) {
295 result.setInnerNode(innerNode);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000296 localPoint.move(-innerNode->renderBox()->x(), -innerNode->renderBox()->y());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000297 }
298
299 result.setLocalPoint(localPoint);
300 return true;
301}
302
303void RenderTextControlSingleLine::forwardEvent(Event* event)
304{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000305 RenderBox* innerTextRenderer = innerTextElement()->renderBox();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000306
307 if (event->type() == eventNames().blurEvent) {
308 if (innerTextRenderer) {
309 if (RenderLayer* innerLayer = innerTextRenderer->layer())
310 innerLayer->scrollToOffset(style()->direction() == RTL ? innerLayer->scrollWidth() : 0, 0);
311 }
312
313 capsLockStateMayHaveChanged();
314 } else if (event->type() == eventNames().focusEvent)
315 capsLockStateMayHaveChanged();
316
317 if (!event->isMouseEvent()) {
318 RenderTextControl::forwardEvent(event);
319 return;
320 }
321
simon.fraser@apple.com032683d2009-03-22 17:35:38 +0000322 FloatPoint localPoint = innerTextRenderer->absoluteToLocal(static_cast<MouseEvent*>(event)->absoluteLocation(), false, true);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000323 if (m_resultsButton && localPoint.x() < innerTextRenderer->borderBoxRect().x())
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000324 m_resultsButton->defaultEventHandler(event);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000325 else if (m_cancelButton && localPoint.x() > innerTextRenderer->borderBoxRect().right())
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000326 m_cancelButton->defaultEventHandler(event);
327 else
328 RenderTextControl::forwardEvent(event);
329}
330
weinig@apple.com1a57c822009-02-04 22:27:50 +0000331void RenderTextControlSingleLine::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000332{
333 RenderTextControl::styleDidChange(diff, oldStyle);
334
335 if (RenderObject* innerBlockRenderer = m_innerBlock ? m_innerBlock->renderer() : 0) {
336 // We may have set the width and the height in the old style in layout().
337 // Reset them now to avoid getting a spurious layout hint.
338 innerBlockRenderer->style()->setHeight(Length());
339 innerBlockRenderer->style()->setWidth(Length());
340 innerBlockRenderer->setStyle(createInnerBlockStyle(style()));
341 }
342
343 if (RenderObject* resultsRenderer = m_resultsButton ? m_resultsButton->renderer() : 0)
344 resultsRenderer->setStyle(createResultsButtonStyle(style()));
345
346 if (RenderObject* cancelRenderer = m_cancelButton ? m_cancelButton->renderer() : 0)
347 cancelRenderer->setStyle(createCancelButtonStyle(style()));
eric@webkit.orgd2bb5a02009-03-17 23:44:11 +0000348
349 setHasOverflowClip(false);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000350}
351
352void RenderTextControlSingleLine::capsLockStateMayHaveChanged()
353{
354 if (!node() || !document())
355 return;
356
357 // Only draw the caps lock indicator if these things are true:
358 // 1) The field is a password field
359 // 2) The frame is active
360 // 3) The element is focused
361 // 4) The caps lock is on
362 bool shouldDrawCapsLockIndicator = false;
363
364 if (Frame* frame = document()->frame())
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000365 shouldDrawCapsLockIndicator = inputElement()->isPasswordField()
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000366 && frame->selection()->isFocusedAndActive()
367 && document()->focusedNode() == node()
368 && PlatformKeyboardEvent::currentCapsLockState();
369
370 if (shouldDrawCapsLockIndicator != m_shouldDrawCapsLockIndicator) {
371 m_shouldDrawCapsLockIndicator = shouldDrawCapsLockIndicator;
372 repaint();
373 }
374}
375
376int RenderTextControlSingleLine::textBlockWidth() const
377{
378 int width = RenderTextControl::textBlockWidth();
379
hyatt@apple.comd885df72009-01-22 02:31:52 +0000380 if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000381 resultsRenderer->calcWidth();
adele@apple.comf32f2672009-01-15 02:05:25 +0000382 width -= resultsRenderer->width() + resultsRenderer->marginLeft() + resultsRenderer->marginRight();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000383 }
384
hyatt@apple.comd885df72009-01-22 02:31:52 +0000385 if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000386 cancelRenderer->calcWidth();
adele@apple.comf32f2672009-01-15 02:05:25 +0000387 width -= cancelRenderer->width() + cancelRenderer->marginLeft() + cancelRenderer->marginRight();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000388 }
389
390 return width;
391}
392
393int RenderTextControlSingleLine::preferredContentWidth(float charWidth) const
394{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000395 int factor = inputElement()->size();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000396 if (factor <= 0)
397 factor = 20;
398
399 int result = static_cast<int>(ceilf(charWidth * factor));
400
ojan@chromium.org149606a2009-04-29 20:31:53 +0000401 // For text inputs, IE adds some extra width.
402 result += style()->font().primaryFont()->maxCharWidth() - charWidth;
403
hyatt@apple.com55fe6bd2009-01-23 07:14:49 +0000404 if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000405 result += resultsRenderer->borderLeft() + resultsRenderer->borderRight() +
406 resultsRenderer->paddingLeft() + resultsRenderer->paddingRight();
407
hyatt@apple.com55fe6bd2009-01-23 07:14:49 +0000408 if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000409 result += cancelRenderer->borderLeft() + cancelRenderer->borderRight() +
410 cancelRenderer->paddingLeft() + cancelRenderer->paddingRight();
411
412 return result;
413}
414
415void RenderTextControlSingleLine::adjustControlHeightBasedOnLineHeight(int lineHeight)
416{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000417 if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) {
hyatt@apple.com801ed3f2009-01-30 18:16:03 +0000418 toRenderBlock(resultsRenderer)->calcHeight();
hyatt@apple.comd885df72009-01-22 02:31:52 +0000419 setHeight(max(height(),
420 resultsRenderer->borderTop() + resultsRenderer->borderBottom() +
421 resultsRenderer->paddingTop() + resultsRenderer->paddingBottom() +
422 resultsRenderer->marginTop() + resultsRenderer->marginBottom()));
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000423 lineHeight = max(lineHeight, resultsRenderer->height());
424 }
425
hyatt@apple.comd885df72009-01-22 02:31:52 +0000426 if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) {
hyatt@apple.com801ed3f2009-01-30 18:16:03 +0000427 toRenderBlock(cancelRenderer)->calcHeight();
hyatt@apple.comd885df72009-01-22 02:31:52 +0000428 setHeight(max(height(),
429 cancelRenderer->borderTop() + cancelRenderer->borderBottom() +
430 cancelRenderer->paddingTop() + cancelRenderer->paddingBottom() +
431 cancelRenderer->marginTop() + cancelRenderer->marginBottom()));
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000432 lineHeight = max(lineHeight, cancelRenderer->height());
433 }
434
hyatt@apple.comd885df72009-01-22 02:31:52 +0000435 setHeight(height() + lineHeight);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000436}
437
438void RenderTextControlSingleLine::createSubtreeIfNeeded()
439{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000440 if (!inputElement()->isSearchField()) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000441 RenderTextControl::createSubtreeIfNeeded(m_innerBlock.get());
442 return;
443 }
444
445 if (!m_innerBlock) {
446 // Create the inner block element
447 m_innerBlock = new TextControlInnerElement(document(), node());
448 m_innerBlock->attachInnerElement(node(), createInnerBlockStyle(style()), renderArena());
449 }
450
451 if (!m_resultsButton) {
452 // Create the search results button element
453 m_resultsButton = new SearchFieldResultsButtonElement(document());
454 m_resultsButton->attachInnerElement(m_innerBlock.get(), createResultsButtonStyle(m_innerBlock->renderer()->style()), renderArena());
455 }
456
457 // Create innerText element before adding the cancel button
458 RenderTextControl::createSubtreeIfNeeded(m_innerBlock.get());
459
460 if (!m_cancelButton) {
461 // Create the cancel button element
462 m_cancelButton = new SearchFieldCancelButtonElement(document());
463 m_cancelButton->attachInnerElement(m_innerBlock.get(), createCancelButtonStyle(m_innerBlock->renderer()->style()), renderArena());
464 }
465}
466
467void RenderTextControlSingleLine::updateFromElement()
468{
469 createSubtreeIfNeeded();
470 RenderTextControl::updateFromElement();
471
472 bool placeholderVisibilityShouldChange = m_placeholderVisible != placeholderShouldBeVisible();
473 m_placeholderVisible = placeholderShouldBeVisible();
474
simon.fraser@apple.comf71e81d2009-03-11 19:00:13 +0000475 if (m_cancelButton)
476 updateCancelButtonVisibility();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000477
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000478 if (m_placeholderVisible) {
479 ExceptionCode ec = 0;
eric@webkit.orge734fc12009-05-13 03:38:18 +0000480 innerTextElement()->setInnerText(inputElement()->placeholder(), ec);
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000481 ASSERT(!ec);
zimmermann@webkit.orgb75da282009-05-07 21:43:11 +0000482 } else if (!static_cast<Element*>(node())->formControlValueMatchesRenderer() || placeholderVisibilityShouldChange)
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000483 setInnerTextValue(inputElement()->value());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000484
485 if (m_searchPopupIsVisible)
486 m_searchPopup->updateFromElement();
487}
488
489void RenderTextControlSingleLine::cacheSelection(int start, int end)
490{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000491 inputElement()->cacheSelection(start, end);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000492}
493
494PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerTextStyle(const RenderStyle* startStyle) const
495{
496 RefPtr<RenderStyle> textBlockStyle;
497 if (placeholderShouldBeVisible()) {
mitz@apple.com0b86b492009-05-28 05:25:53 +0000498 if (RenderStyle* pseudoStyle = getCachedPseudoStyle(INPUT_PLACEHOLDER))
499 textBlockStyle = RenderStyle::clone(pseudoStyle);
500 }
501 if (!textBlockStyle) {
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000502 textBlockStyle = RenderStyle::create();
503 textBlockStyle->inheritFrom(startStyle);
504 }
505
506 adjustInnerTextStyle(startStyle, textBlockStyle.get());
507
508 textBlockStyle->setWhiteSpace(PRE);
509 textBlockStyle->setWordWrap(NormalWordWrap);
510 textBlockStyle->setOverflowX(OHIDDEN);
511 textBlockStyle->setOverflowY(OHIDDEN);
512
513 // Do not allow line-height to be smaller than our default.
514 if (textBlockStyle->font().lineSpacing() > lineHeight(true, true))
515 textBlockStyle->setLineHeight(Length(-100.0f, Percent));
516
517 textBlockStyle->setDisplay(m_innerBlock ? INLINE_BLOCK : BLOCK);
518
519 // We're adding one extra pixel of padding to match WinIE.
520 textBlockStyle->setPaddingLeft(Length(1, Fixed));
521 textBlockStyle->setPaddingRight(Length(1, Fixed));
522
523 // When the placeholder is going to be displayed, temporarily override the text security to be "none".
524 // After this, updateFromElement will immediately update the text displayed.
525 // When the placeholder is no longer visible, updatePlaceholderVisiblity will reset the style,
526 // and the text security mode will be set back to the computed value correctly.
527 if (placeholderShouldBeVisible())
528 textBlockStyle->setTextSecurity(TSNONE);
529
530 return textBlockStyle.release();
531}
532
533PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerBlockStyle(const RenderStyle* startStyle) const
534{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000535 ASSERT(node()->isHTMLElement());
536
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000537 RefPtr<RenderStyle> innerBlockStyle = RenderStyle::create();
538 innerBlockStyle->inheritFrom(startStyle);
539
540 innerBlockStyle->setDisplay(BLOCK);
541 innerBlockStyle->setDirection(LTR);
542
543 // We don't want the shadow dom to be editable, so we set this block to read-only in case the input itself is editable.
544 innerBlockStyle->setUserModify(READ_ONLY);
545
546 return innerBlockStyle.release();
547}
548
549PassRefPtr<RenderStyle> RenderTextControlSingleLine::createResultsButtonStyle(const RenderStyle* startStyle) const
550{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000551 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000552 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
553
554 RefPtr<RenderStyle> resultsBlockStyle;
555 if (input->maxResults() < 0)
weinig@apple.comedad01d2009-02-04 21:02:02 +0000556 resultsBlockStyle = getCachedPseudoStyle(SEARCH_DECORATION);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000557 else if (!input->maxResults())
weinig@apple.comedad01d2009-02-04 21:02:02 +0000558 resultsBlockStyle = getCachedPseudoStyle(SEARCH_RESULTS_DECORATION);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000559 else
weinig@apple.comedad01d2009-02-04 21:02:02 +0000560 resultsBlockStyle = getCachedPseudoStyle(SEARCH_RESULTS_BUTTON);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000561
562 if (!resultsBlockStyle)
563 resultsBlockStyle = RenderStyle::create();
564
565 if (startStyle)
566 resultsBlockStyle->inheritFrom(startStyle);
567
568 return resultsBlockStyle.release();
569}
570
571PassRefPtr<RenderStyle> RenderTextControlSingleLine::createCancelButtonStyle(const RenderStyle* startStyle) const
572{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000573 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000574 RefPtr<RenderStyle> cancelBlockStyle;
575
weinig@apple.comedad01d2009-02-04 21:02:02 +0000576 if (RefPtr<RenderStyle> pseudoStyle = getCachedPseudoStyle(SEARCH_CANCEL_BUTTON))
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000577 // We may be sharing style with another search field, but we must not share the cancel button style.
578 cancelBlockStyle = RenderStyle::clone(pseudoStyle.get());
579 else
580 cancelBlockStyle = RenderStyle::create();
581
582 if (startStyle)
583 cancelBlockStyle->inheritFrom(startStyle);
584
simon.fraser@apple.comf71e81d2009-03-11 19:00:13 +0000585 cancelBlockStyle->setVisibility(visibilityForCancelButton());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000586 return cancelBlockStyle.release();
587}
588
simon.fraser@apple.comf71e81d2009-03-11 19:00:13 +0000589void RenderTextControlSingleLine::updateCancelButtonVisibility() const
590{
591 if (!m_cancelButton->renderer())
592 return;
593
594 const RenderStyle* curStyle = m_cancelButton->renderer()->style();
595 EVisibility buttonVisibility = visibilityForCancelButton();
596 if (curStyle->visibility() == buttonVisibility)
597 return;
598
599 RefPtr<RenderStyle> cancelButtonStyle = RenderStyle::clone(curStyle);
600 cancelButtonStyle->setVisibility(buttonVisibility);
601 m_cancelButton->renderer()->setStyle(cancelButtonStyle);
602}
603
604EVisibility RenderTextControlSingleLine::visibilityForCancelButton() const
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000605{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000606 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000607 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
simon.fraser@apple.comf71e81d2009-03-11 19:00:13 +0000608 return input->value().isEmpty() ? HIDDEN : VISIBLE;
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000609}
610
611const AtomicString& RenderTextControlSingleLine::autosaveName() const
612{
613 return static_cast<Element*>(node())->getAttribute(autosaveAttr);
614}
615
616void RenderTextControlSingleLine::startSearchEventTimer()
617{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000618 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000619 unsigned length = text().length();
620
621 // If there's no text, fire the event right away.
622 if (!length) {
623 stopSearchEventTimer();
624 static_cast<HTMLInputElement*>(node())->onSearch();
625 return;
626 }
627
628 // After typing the first key, we wait 0.5 seconds.
629 // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on.
630 m_searchEventTimer.startOneShot(max(0.2, 0.6 - 0.1 * length));
631}
632
633void RenderTextControlSingleLine::searchEventTimerFired(Timer<RenderTextControlSingleLine>*)
634{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000635 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000636 static_cast<HTMLInputElement*>(node())->onSearch();
637}
638
639// PopupMenuClient methods
640void RenderTextControlSingleLine::valueChanged(unsigned listIndex, bool fireEvents)
641{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000642 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000643 ASSERT(static_cast<int>(listIndex) < listSize());
644 HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
645 if (static_cast<int>(listIndex) == (listSize() - 1)) {
646 if (fireEvents) {
647 m_recentSearches.clear();
648 const AtomicString& name = autosaveName();
649 if (!name.isEmpty()) {
650 if (!m_searchPopup)
651 m_searchPopup = SearchPopupMenu::create(this);
652 m_searchPopup->saveRecentSearches(name, m_recentSearches);
653 }
654 }
655 } else {
656 input->setValue(itemText(listIndex));
657 if (fireEvents)
658 input->onSearch();
659 input->select();
660 }
661}
662
663String RenderTextControlSingleLine::itemText(unsigned listIndex) const
664{
665 int size = listSize();
666 if (size == 1) {
667 ASSERT(!listIndex);
668 return searchMenuNoRecentSearchesText();
669 }
670 if (!listIndex)
671 return searchMenuRecentSearchesText();
672 if (itemIsSeparator(listIndex))
673 return String();
674 if (static_cast<int>(listIndex) == (size - 1))
675 return searchMenuClearRecentSearchesText();
676 return m_recentSearches[listIndex - 1];
677}
678
679bool RenderTextControlSingleLine::itemIsEnabled(unsigned listIndex) const
680{
681 if (!listIndex || itemIsSeparator(listIndex))
682 return false;
683 return true;
684}
685
darin@apple.com98a7ac62009-01-05 17:26:53 +0000686PopupMenuStyle RenderTextControlSingleLine::itemStyle(unsigned) const
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000687{
688 return menuStyle();
689}
690
691PopupMenuStyle RenderTextControlSingleLine::menuStyle() const
692{
adachan@apple.com2ad65132009-03-23 23:46:27 +0000693 return PopupMenuStyle(style()->color(), style()->backgroundColor(), style()->font(), style()->visibility() == VISIBLE, style()->textIndent(), style()->direction());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000694}
695
696int RenderTextControlSingleLine::clientInsetLeft() const
697{
698 // Inset the menu by the radius of the cap on the left so that
699 // it only runs along the straight part of the bezel.
700 return height() / 2;
701}
702
703int RenderTextControlSingleLine::clientInsetRight() const
704{
705 // Inset the menu by the radius of the cap on the right so that
706 // it only runs along the straight part of the bezel (unless it needs
707 // to be wider).
708 return height() / 2;
709}
710
711int RenderTextControlSingleLine::clientPaddingLeft() const
712{
adele@apple.com524122c2009-01-10 01:12:04 +0000713 int padding = paddingLeft();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000714
hyatt@apple.comd885df72009-01-22 02:31:52 +0000715 if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000716 padding += resultsRenderer->width();
717
718 return padding;
719}
720
721int RenderTextControlSingleLine::clientPaddingRight() const
722{
adele@apple.com524122c2009-01-10 01:12:04 +0000723 int padding = paddingRight();
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000724
hyatt@apple.comd885df72009-01-22 02:31:52 +0000725 if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0)
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000726 padding += cancelRenderer->width();
727
728 return padding;
729}
730
731int RenderTextControlSingleLine::listSize() const
732{
733 // If there are no recent searches, then our menu will have 1 "No recent searches" item.
734 if (!m_recentSearches.size())
735 return 1;
736 // Otherwise, leave room in the menu for a header, a separator, and the "Clear recent searches" item.
737 return m_recentSearches.size() + 3;
738}
739
740int RenderTextControlSingleLine::selectedIndex() const
741{
742 return -1;
743}
744
745bool RenderTextControlSingleLine::itemIsSeparator(unsigned listIndex) const
746{
747 // The separator will be the second to last item in our list.
748 return static_cast<int>(listIndex) == (listSize() - 2);
749}
750
751bool RenderTextControlSingleLine::itemIsLabel(unsigned listIndex) const
752{
753 return listIndex == 0;
754}
755
756bool RenderTextControlSingleLine::itemIsSelected(unsigned) const
757{
758 return false;
759}
760
761void RenderTextControlSingleLine::setTextFromItem(unsigned listIndex)
762{
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000763 ASSERT(node()->isHTMLElement());
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000764 static_cast<HTMLInputElement*>(node())->setValue(itemText(listIndex));
765}
766
767FontSelector* RenderTextControlSingleLine::fontSelector() const
768{
769 return document()->styleSelector()->fontSelector();
770}
771
772HostWindow* RenderTextControlSingleLine::hostWindow() const
773{
774 return document()->view()->hostWindow();
775}
776
eric@webkit.orgd2bb5a02009-03-17 23:44:11 +0000777void RenderTextControlSingleLine::autoscroll()
778{
779 RenderLayer* layer = innerTextElement()->renderBox()->layer();
780 if (layer)
781 layer->autoscroll();
782}
783
784int RenderTextControlSingleLine::scrollWidth() const
785{
786 if (innerTextElement())
787 return innerTextElement()->scrollWidth();
788 return RenderBlock::scrollWidth();
789}
790
791int RenderTextControlSingleLine::scrollHeight() const
792{
793 if (innerTextElement())
794 return innerTextElement()->scrollHeight();
795 return RenderBlock::scrollHeight();
796}
797
798int RenderTextControlSingleLine::scrollLeft() const
799{
800 if (innerTextElement())
801 return innerTextElement()->scrollLeft();
802 return RenderBlock::scrollLeft();
803}
804
805int RenderTextControlSingleLine::scrollTop() const
806{
807 if (innerTextElement())
808 return innerTextElement()->scrollTop();
809 return RenderBlock::scrollTop();
810}
811
812void RenderTextControlSingleLine::setScrollLeft(int newLeft)
813{
814 if (innerTextElement())
815 innerTextElement()->setScrollLeft(newLeft);
816}
817
818void RenderTextControlSingleLine::setScrollTop(int newTop)
819{
820 if (innerTextElement())
821 innerTextElement()->setScrollTop(newTop);
822}
823
824bool RenderTextControlSingleLine::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
825{
826 RenderLayer* layer = innerTextElement()->renderBox()->layer();
827 if (layer && layer->scroll(direction, granularity, multiplier))
828 return true;
829 return RenderBlock::scroll(direction, granularity, multiplier);
830}
831
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000832PassRefPtr<Scrollbar> RenderTextControlSingleLine::createScrollbar(ScrollbarClient* client, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
833{
834 RefPtr<Scrollbar> widget;
weinig@apple.comedad01d2009-02-04 21:02:02 +0000835 bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR);
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000836 if (hasCustomScrollbarStyle)
837 widget = RenderScrollbar::createCustomScrollbar(client, orientation, this);
838 else
839 widget = Scrollbar::createNativeScrollbar(client, orientation, controlSize);
840 return widget.release();
841}
842
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000843InputElement* RenderTextControlSingleLine::inputElement() const
844{
zimmermann@webkit.org6d4da352009-01-22 22:53:47 +0000845 return toInputElement(static_cast<Element*>(node()));
zimmermann@webkit.orgdceb5db2009-01-20 21:02:58 +0000846}
847
zimmermann@webkit.org9402bba2008-12-28 13:54:17 +0000848}