OSDN Git Service

Merge changes I55c6d71a,Ifb3277d4,Ia1b847a2,I7ba9cf3f,Ida2b2a8a,I1280ec90,I72f818d5...
[android-x86/external-webkit.git] / Source / WebCore / rendering / RenderTextControlSingleLine.cpp
1 /**
2  * Copyright (C) 2006, 2007, 2010 Apple Inc. All rights reserved.
3  *           (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) 
4  * Copyright (C) 2010 Google Inc. All rights reserved.
5  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include "config.h"
25 #include "RenderTextControlSingleLine.h"
26
27 #include "Chrome.h"
28 #include "CSSStyleSelector.h"
29 #include "Event.h"
30 #include "EventNames.h"
31 #include "Frame.h"
32 #include "FrameView.h"
33 #include "HTMLInputElement.h"
34 #include "HTMLNames.h"
35 #include "HitTestResult.h"
36 #include "InputElement.h"
37 #include "LocalizedStrings.h"
38 #include "MouseEvent.h"
39 #include "PlatformKeyboardEvent.h"
40 #include "RenderLayer.h"
41 #include "RenderScrollbar.h"
42 #include "RenderTheme.h"
43 #include "SelectionController.h"
44 #include "Settings.h"
45 #include "SimpleFontData.h"
46 #include "TextControlInnerElements.h"
47
48 using namespace std;
49
50 namespace WebCore {
51
52 using namespace HTMLNames;
53
54 VisiblePosition RenderTextControlInnerBlock::positionForPoint(const IntPoint& point)
55 {
56     IntPoint contentsPoint(point);
57
58     // Multiline text controls have the scroll on shadowAncestorNode, so we need to take that
59     // into account here.
60     if (m_multiLine) {
61         RenderTextControl* renderer = toRenderTextControl(node()->shadowAncestorNode()->renderer());
62         if (renderer->hasOverflowClip())
63             contentsPoint += renderer->layer()->scrolledContentOffset();
64     }
65
66     return RenderBlock::positionForPoint(contentsPoint);
67 }
68
69 // ----------------------------
70
71 RenderTextControlSingleLine::RenderTextControlSingleLine(Node* node, bool placeholderVisible)
72     : RenderTextControl(node, placeholderVisible)
73     , m_searchPopupIsVisible(false)
74     , m_shouldDrawCapsLockIndicator(false)
75     , m_searchEventTimer(this, &RenderTextControlSingleLine::searchEventTimerFired)
76     , m_searchPopup(0)
77 {
78 }
79
80 RenderTextControlSingleLine::~RenderTextControlSingleLine()
81 {
82     if (m_searchPopup) {
83         m_searchPopup->popupMenu()->disconnectClient();
84         m_searchPopup = 0;
85     }
86  
87     if (m_innerBlock) {
88         m_innerBlock->detach();
89         m_innerBlock = 0;
90     }
91
92     if (m_innerSpinButton)
93         m_innerSpinButton->detach();
94     if (m_outerSpinButton)
95         m_outerSpinButton->detach();
96 #if ENABLE(INPUT_SPEECH)
97     if (m_speechButton)
98         m_speechButton->detach();
99 #endif
100 }
101
102 RenderStyle* RenderTextControlSingleLine::textBaseStyle() const
103 {
104     return m_innerBlock ? m_innerBlock->renderer()->style() : style();
105 }
106
107 void RenderTextControlSingleLine::addSearchResult()
108 {
109     ASSERT(node()->isHTMLElement());
110     HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
111     if (input->maxResults() <= 0)
112         return;
113
114     String value = input->value();
115     if (value.isEmpty())
116         return;
117
118     Settings* settings = document()->settings();
119     if (!settings || settings->privateBrowsingEnabled())
120         return;
121
122     int size = static_cast<int>(m_recentSearches.size());
123     for (int i = size - 1; i >= 0; --i) {
124         if (m_recentSearches[i] == value)
125             m_recentSearches.remove(i);
126     }
127
128     m_recentSearches.insert(0, value);
129     while (static_cast<int>(m_recentSearches.size()) > input->maxResults())
130         m_recentSearches.removeLast();
131
132     const AtomicString& name = autosaveName();
133     if (!m_searchPopup)
134         m_searchPopup = document()->page()->chrome()->createSearchPopupMenu(this);
135
136     m_searchPopup->saveRecentSearches(name, m_recentSearches);
137 }
138
139 void RenderTextControlSingleLine::stopSearchEventTimer()
140 {
141     ASSERT(node()->isHTMLElement());
142     m_searchEventTimer.stop();
143 }
144
145 void RenderTextControlSingleLine::showPopup()
146 {
147     ASSERT(node()->isHTMLElement());
148     if (m_searchPopupIsVisible)
149         return;
150
151     if (!m_searchPopup)
152         m_searchPopup = document()->page()->chrome()->createSearchPopupMenu(this);
153
154     if (!m_searchPopup->enabled())
155         return;
156
157     m_searchPopupIsVisible = true;
158
159     const AtomicString& name = autosaveName();
160     m_searchPopup->loadRecentSearches(name, m_recentSearches);
161
162     // Trim the recent searches list if the maximum size has changed since we last saved.
163     HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
164     if (static_cast<int>(m_recentSearches.size()) > input->maxResults()) {
165         do {
166             m_recentSearches.removeLast();
167         } while (static_cast<int>(m_recentSearches.size()) > input->maxResults());
168
169         m_searchPopup->saveRecentSearches(name, m_recentSearches);
170     }
171
172     m_searchPopup->popupMenu()->show(absoluteBoundingBoxRect(true), document()->view(), -1);
173 }
174
175 void RenderTextControlSingleLine::hidePopup()
176 {
177     ASSERT(node()->isHTMLElement());
178     if (m_searchPopup)
179         m_searchPopup->popupMenu()->hide();
180 }
181
182 void RenderTextControlSingleLine::subtreeHasChanged()
183 {
184     RenderTextControl::subtreeHasChanged();
185
186     ASSERT(node()->isElementNode());
187     Element* element = static_cast<Element*>(node());
188     bool wasChanged = element->wasChangedSinceLastFormControlChangeEvent();
189     element->setChangedSinceLastFormControlChangeEvent(true);
190
191     InputElement* input = inputElement();
192     // We don't need to call sanitizeUserInputValue() function here because
193     // InputElement::handleBeforeTextInsertedEvent() has already called
194     // sanitizeUserInputValue().
195     // sanitizeValue() is needed because IME input doesn't dispatch BeforeTextInsertedEvent.
196     String value = text();
197     if (input->isAcceptableValue(value))
198         input->setValueFromRenderer(input->sanitizeValue(input->convertFromVisibleValue(value)));
199     if (node()->isHTMLElement()) {
200         // Recalc for :invalid and hasUnacceptableValue() change.
201         static_cast<HTMLInputElement*>(input)->setNeedsStyleRecalc();
202     }
203
204     if (m_cancelButton)
205         updateCancelButtonVisibility();
206
207     // If the incremental attribute is set, then dispatch the search event
208     if (input->searchEventsShouldBeDispatched())
209         startSearchEventTimer();
210
211     if (!wasChanged && node()->focused()) {
212         if (Frame* frame = this->frame())
213             frame->editor()->textFieldDidBeginEditing(static_cast<Element*>(node()));
214     }
215
216     if (node()->focused()) {
217         if (Frame* frame = document()->frame())
218             frame->editor()->textDidChangeInTextField(static_cast<Element*>(node()));
219     }
220 }
221
222 void RenderTextControlSingleLine::paint(PaintInfo& paintInfo, int tx, int ty)
223 {
224     RenderTextControl::paint(paintInfo, tx, ty);
225
226     if (paintInfo.phase == PaintPhaseBlockBackground && m_shouldDrawCapsLockIndicator) {
227         IntRect contentsRect = contentBoxRect();
228
229         // Center vertically like the text.
230         contentsRect.setY((height() - contentsRect.height()) / 2);
231
232         // Convert the rect into the coords used for painting the content
233         contentsRect.move(tx + x(), ty + y());
234         theme()->paintCapsLockIndicator(this, paintInfo, contentsRect);
235     }
236 }
237
238 void RenderTextControlSingleLine::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
239 {
240     paintBoxDecorationsWithSize(paintInfo, tx, ty, width() - decorationWidthRight(), height());
241 }
242
243 void RenderTextControlSingleLine::addFocusRingRects(Vector<IntRect>& rects, int tx, int ty)
244 {
245     int w = width() - decorationWidthRight();
246     if (w && height())
247         rects.append(IntRect(tx, ty, w, height()));
248 }
249
250 void RenderTextControlSingleLine::layout()
251 {
252     int oldHeight = height();
253     computeLogicalHeight();
254
255     int oldWidth = width();
256     computeLogicalWidth();
257
258     bool relayoutChildren = oldHeight != height() || oldWidth != width();
259
260 #ifdef ANDROID_LAYOUT
261     checkAndSetRelayoutChildren(&relayoutChildren);
262 #endif
263
264     RenderBox* innerTextRenderer = innerTextElement()->renderBox();
265     RenderBox* innerBlockRenderer = m_innerBlock ? m_innerBlock->renderBox() : 0;
266
267     // Set the text block height
268     int desiredHeight = textBlockHeight();
269     int currentHeight = innerTextRenderer->height();
270
271     if (currentHeight > height()) {
272         if (desiredHeight != currentHeight)
273             relayoutChildren = true;
274         innerTextRenderer->style()->setHeight(Length(desiredHeight, Fixed));
275         if (m_innerBlock)
276             innerBlockRenderer->style()->setHeight(Length(desiredHeight, Fixed));
277     }
278
279     // Set the text block width
280     int desiredWidth = textBlockWidth();
281     if (desiredWidth != innerTextRenderer->width())
282         relayoutChildren = true;
283     innerTextRenderer->style()->setWidth(Length(desiredWidth, Fixed));
284
285     if (m_innerBlock) {
286         int innerBlockWidth = width() - borderAndPaddingWidth();
287         if (innerBlockWidth != innerBlockRenderer->width())
288             relayoutChildren = true;
289         innerBlockRenderer->style()->setWidth(Length(innerBlockWidth, Fixed));
290     }
291
292     RenderBlock::layoutBlock(relayoutChildren);
293
294     // Center the child block vertically
295     RenderBox* childBlock = innerBlockRenderer ? innerBlockRenderer : innerTextRenderer;
296     currentHeight = childBlock->height();
297     if (currentHeight < height())
298         childBlock->setY((height() - currentHeight) / 2);
299
300     // Ignores the paddings for the inner spin button.
301     if (RenderBox* spinBox = m_innerSpinButton ? m_innerSpinButton->renderBox() : 0) {
302         spinBox->setLocation(spinBox->x() + paddingRight(), borderTop());
303         spinBox->setHeight(height() - borderTop() - borderBottom());
304     }
305
306 #if ENABLE(INPUT_SPEECH)
307     if (RenderBox* button = m_speechButton ? m_speechButton->renderBox() : 0) {
308         if (m_innerBlock) {
309             // This is mostly the case where this is a search field. The speech button is a sibling
310             // of the inner block and laid out at the far right.
311             int x = width() - borderAndPaddingWidth() - button->width() - button->borderAndPaddingWidth();
312             int y = (height() - button->height()) / 2;
313             button->setLocation(x, y);
314         } else {
315             // For non-search fields which are simpler and we let the defaut layout handle things
316             // except for small tweaking below.
317             button->setLocation(button->x() + paddingRight(), (height() - button->height()) / 2);
318         }
319     }
320 #endif
321
322     // Center the spin button vertically, and move it to the right by
323     // padding + border of the text fields.
324     if (RenderBox* spinBox = m_outerSpinButton ? m_outerSpinButton->renderBox() : 0) {
325         int diff = height() - spinBox->height();
326         // If the diff is odd, the top area over the spin button takes the
327         // remaining one pixel. It's good for Mac NSStepper because it has
328         // shadow at the bottom.
329         int y = (diff / 2) + (diff % 2);
330         int x = width() - borderRight() - paddingRight() - spinBox->width();
331         spinBox->setLocation(x, y);
332     }
333 }
334
335 bool RenderTextControlSingleLine::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction hitTestAction)
336 {
337     // 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
338     // was on the control but not on the inner element (see Radar 4617841).
339
340     // 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,
341     // and act as if we've hit the close block if we're to the right of the inner text block.
342
343     if (!RenderTextControl::nodeAtPoint(request, result, xPos, yPos, tx, ty, hitTestAction))
344         return false;
345
346     // If we hit a node inside the inner text element, say that we hit that element,
347     // and if we hit our node (e.g. we're over the border or padding), also say that we hit the
348     // inner text element so that it gains focus.
349     if (result.innerNode()->isDescendantOf(innerTextElement()) || result.innerNode() == node())
350         hitInnerTextElement(result, xPos, yPos, tx, ty);
351
352     // If we found a spin button, we're done.
353     if (m_innerSpinButton && result.innerNode() == m_innerSpinButton)
354         return true;
355     if (m_outerSpinButton && result.innerNode() == m_outerSpinButton)
356         return true;
357 #if ENABLE(INPUT_SPEECH)
358     if (m_speechButton && result.innerNode() == m_speechButton)
359         return true;
360 #endif
361     // If we're not a search field, or we already found the speech, results or cancel buttons, we're done.
362     if (!m_innerBlock || result.innerNode() == m_resultsButton || result.innerNode() == m_cancelButton)
363         return true;
364
365     Node* innerNode = 0;
366     RenderBox* innerBlockRenderer = m_innerBlock->renderBox();
367     RenderBox* innerTextRenderer = innerTextElement()->renderBox();
368
369     IntPoint localPoint = result.localPoint();
370     localPoint.move(-innerBlockRenderer->x(), -innerBlockRenderer->y());
371
372     int textLeft = tx + x() + innerBlockRenderer->x() + innerTextRenderer->x();
373     if (m_resultsButton && m_resultsButton->renderer() && xPos < textLeft)
374         innerNode = m_resultsButton.get();
375
376     if (!innerNode) {
377         int textRight = textLeft + innerTextRenderer->width();
378         if (m_cancelButton && m_cancelButton->renderer() && xPos > textRight)
379             innerNode = m_cancelButton.get();
380     }
381
382     if (innerNode) {
383         result.setInnerNode(innerNode);
384         localPoint.move(-innerNode->renderBox()->x(), -innerNode->renderBox()->y());
385     }
386
387     result.setLocalPoint(localPoint);
388     return true;
389 }
390
391 void RenderTextControlSingleLine::forwardEvent(Event* event)
392 {
393     RenderBox* innerTextRenderer = innerTextElement()->renderBox();
394
395     if (event->type() == eventNames().blurEvent) {
396         if (innerTextRenderer) {
397             if (RenderLayer* innerLayer = innerTextRenderer->layer())
398                 innerLayer->scrollToOffset(!style()->isLeftToRightDirection() ? innerLayer->scrollWidth() : 0, 0);
399         }
400
401         capsLockStateMayHaveChanged();
402     } else if (event->type() == eventNames().focusEvent)
403         capsLockStateMayHaveChanged();
404
405     if (!event->isMouseEvent()) {
406         RenderTextControl::forwardEvent(event);
407         return;
408     }
409
410 #if ENABLE(INPUT_SPEECH)
411     if (RenderBox* speechBox = m_speechButton ? m_speechButton->renderBox() : 0) {
412         FloatPoint pointInTextControlCoords = absoluteToLocal(static_cast<MouseEvent*>(event)->absoluteLocation(), false, true);
413         if (speechBox->frameRect().contains(roundedIntPoint(pointInTextControlCoords))) {
414             m_speechButton->defaultEventHandler(event);
415             return;
416         }
417     }
418 #endif
419
420     FloatPoint localPoint = innerTextRenderer->absoluteToLocal(static_cast<MouseEvent*>(event)->absoluteLocation(), false, true);
421     int textRight = innerTextRenderer->borderBoxRect().maxX();
422
423     if (m_resultsButton && localPoint.x() < innerTextRenderer->borderBoxRect().x())
424         m_resultsButton->defaultEventHandler(event);
425     else if (m_cancelButton && localPoint.x() > textRight)
426         m_cancelButton->defaultEventHandler(event);
427     else if (m_innerSpinButton && localPoint.x() > textRight && m_innerSpinButton->renderBox() && localPoint.x() < textRight + m_innerSpinButton->renderBox()->width())
428         m_innerSpinButton->defaultEventHandler(event);
429     else if (m_outerSpinButton && localPoint.x() > textRight)
430         m_outerSpinButton->defaultEventHandler(event);
431     else
432         RenderTextControl::forwardEvent(event);
433 }
434
435 void RenderTextControlSingleLine::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
436 {
437     RenderTextControl::styleDidChange(diff, oldStyle);
438
439     if (RenderObject* innerBlockRenderer = m_innerBlock ? m_innerBlock->renderer() : 0) {
440         // We may have set the width and the height in the old style in layout().
441         // Reset them now to avoid getting a spurious layout hint.
442         innerBlockRenderer->style()->setHeight(Length());
443         innerBlockRenderer->style()->setWidth(Length());
444         innerBlockRenderer->setStyle(createInnerBlockStyle(style()));
445     }
446
447     if (RenderObject* resultsRenderer = m_resultsButton ? m_resultsButton->renderer() : 0)
448         resultsRenderer->setStyle(createResultsButtonStyle(style()));
449
450     if (RenderObject* cancelRenderer = m_cancelButton ? m_cancelButton->renderer() : 0)
451         cancelRenderer->setStyle(createCancelButtonStyle(style()));
452
453     if (RenderObject* spinRenderer = m_outerSpinButton ? m_outerSpinButton->renderer() : 0)
454         spinRenderer->setStyle(createOuterSpinButtonStyle());
455
456     if (RenderObject* spinRenderer = m_innerSpinButton ? m_innerSpinButton->renderer() : 0)
457         spinRenderer->setStyle(createInnerSpinButtonStyle());
458
459 #if ENABLE(INPUT_SPEECH)
460     if (RenderObject* speechRenderer = m_speechButton ? m_speechButton->renderer() : 0)
461         speechRenderer->setStyle(createSpeechButtonStyle());
462 #endif
463
464     setHasOverflowClip(false);
465 }
466
467 void RenderTextControlSingleLine::capsLockStateMayHaveChanged()
468 {
469     if (!node() || !document())
470         return;
471
472     // Only draw the caps lock indicator if these things are true:
473     // 1) The field is a password field
474     // 2) The frame is active
475     // 3) The element is focused
476     // 4) The caps lock is on
477     bool shouldDrawCapsLockIndicator = false;
478
479     if (Frame* frame = document()->frame())
480         shouldDrawCapsLockIndicator = inputElement()->isPasswordField()
481                                       && frame->selection()->isFocusedAndActive()
482                                       && document()->focusedNode() == node()
483                                       && PlatformKeyboardEvent::currentCapsLockState();
484
485     if (shouldDrawCapsLockIndicator != m_shouldDrawCapsLockIndicator) {
486         m_shouldDrawCapsLockIndicator = shouldDrawCapsLockIndicator;
487         repaint();
488     }
489 }
490
491 bool RenderTextControlSingleLine::hasControlClip() const
492 {
493     bool clip = m_cancelButton;
494     return clip;
495 }
496
497 IntRect RenderTextControlSingleLine::controlClipRect(int tx, int ty) const
498 {
499     // This should only get called for search & speech inputs.
500     ASSERT(hasControlClip());
501
502     IntRect clipRect = IntRect(m_innerBlock->renderBox()->frameRect());
503     clipRect.move(tx, ty);
504     return clipRect;
505 }
506
507 int RenderTextControlSingleLine::textBlockWidth() const
508 {
509     int width = RenderTextControl::textBlockWidth();
510
511     if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) {
512         resultsRenderer->computeLogicalWidth();
513         width -= resultsRenderer->width() + resultsRenderer->marginLeft() + resultsRenderer->marginRight();
514     }
515
516     if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) {
517         cancelRenderer->computeLogicalWidth();
518         width -= cancelRenderer->width() + cancelRenderer->marginLeft() + cancelRenderer->marginRight();
519     }
520
521     if (RenderBox* spinRenderer = m_innerSpinButton ? m_innerSpinButton->renderBox() : 0) {
522         spinRenderer->computeLogicalWidth();
523         width -= spinRenderer->width() + spinRenderer->marginLeft() + spinRenderer->marginRight();
524     }
525
526 #if ENABLE(INPUT_SPEECH)
527     if (RenderBox* speechRenderer = m_speechButton ? m_speechButton->renderBox() : 0) {
528         speechRenderer->computeLogicalWidth();
529         width -= speechRenderer->width() + speechRenderer->marginLeft() + speechRenderer->marginRight();
530     }
531 #endif
532
533     return width - decorationWidthRight();
534 }
535
536 int RenderTextControlSingleLine::decorationWidthRight() const
537 {
538     int width = 0;
539     if (RenderBox* spinRenderer = m_outerSpinButton ? m_outerSpinButton->renderBox() : 0) {
540         spinRenderer->computeLogicalWidth();
541         width += spinRenderer->width() + spinRenderer->marginLeft() + spinRenderer->marginRight();
542     }
543     if (width > 0)
544         width += paddingRight() + borderRight();
545     return width;
546 }
547     
548 float RenderTextControlSingleLine::getAvgCharWidth(AtomicString family)
549 {
550     // Since Lucida Grande is the default font, we want this to match the width
551     // of MS Shell Dlg, the default font for textareas in Firefox, Safari Win and
552     // IE for some encodings (in IE, the default font is encoding specific).
553     // 901 is the avgCharWidth value in the OS/2 table for MS Shell Dlg.
554     if (family == AtomicString("Lucida Grande"))
555         return scaleEmToUnits(901);
556
557     return RenderTextControl::getAvgCharWidth(family);
558 }
559     
560 int RenderTextControlSingleLine::preferredContentWidth(float charWidth) const
561 {
562     int factor = inputElement()->size();
563     if (factor <= 0)
564         factor = 20;
565
566     int result = static_cast<int>(ceilf(charWidth * factor));
567
568     float maxCharWidth = 0.f;
569     AtomicString family = style()->font().family().family();
570     // Since Lucida Grande is the default font, we want this to match the width
571     // of MS Shell Dlg, the default font for textareas in Firefox, Safari Win and
572     // IE for some encodings (in IE, the default font is encoding specific).
573     // 4027 is the (xMax - xMin) value in the "head" font table for MS Shell Dlg.
574     if (family == AtomicString("Lucida Grande"))
575         maxCharWidth = scaleEmToUnits(4027);
576     else if (hasValidAvgCharWidth(family))
577         maxCharWidth = roundf(style()->font().primaryFont()->maxCharWidth());
578
579     // For text inputs, IE adds some extra width.
580     if (maxCharWidth > 0.f)
581         result += maxCharWidth - charWidth;
582
583     if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0)
584         result += resultsRenderer->borderLeft() + resultsRenderer->borderRight() +
585                   resultsRenderer->paddingLeft() + resultsRenderer->paddingRight();
586
587     if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0)
588         result += cancelRenderer->borderLeft() + cancelRenderer->borderRight() +
589                   cancelRenderer->paddingLeft() + cancelRenderer->paddingRight();
590
591 #if ENABLE(INPUT_SPEECH)
592     if (RenderBox* speechRenderer = m_speechButton ? m_speechButton->renderBox() : 0) {
593         result += speechRenderer->borderLeft() + speechRenderer->borderRight() +
594                   speechRenderer->paddingLeft() + speechRenderer->paddingRight();
595     }
596 #endif
597     return result;
598 }
599
600 int RenderTextControlSingleLine::preferredDecorationWidthRight() const
601 {
602     int width = 0;
603     if (RenderBox* spinRenderer = m_outerSpinButton ? m_outerSpinButton->renderBox() : 0) {
604         spinRenderer->computeLogicalWidth();
605         width += spinRenderer->minPreferredLogicalWidth() + spinRenderer->marginLeft() + spinRenderer->marginRight();
606     }
607     if (width > 0)
608         width += paddingRight() + borderRight();
609     return width;
610 }
611
612 void RenderTextControlSingleLine::adjustControlHeightBasedOnLineHeight(int lineHeight)
613 {
614     if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) {
615         resultsRenderer->computeLogicalHeight();
616         setHeight(max(height(),
617                   resultsRenderer->borderTop() + resultsRenderer->borderBottom() +
618                   resultsRenderer->paddingTop() + resultsRenderer->paddingBottom() +
619                   resultsRenderer->marginTop() + resultsRenderer->marginBottom()));
620         lineHeight = max(lineHeight, resultsRenderer->height());
621     }
622     if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) {
623         cancelRenderer->computeLogicalHeight();
624         setHeight(max(height(),
625                   cancelRenderer->borderTop() + cancelRenderer->borderBottom() +
626                   cancelRenderer->paddingTop() + cancelRenderer->paddingBottom() +
627                   cancelRenderer->marginTop() + cancelRenderer->marginBottom()));
628         lineHeight = max(lineHeight, cancelRenderer->height());
629     }
630
631     setHeight(height() + lineHeight);
632 }
633
634 void RenderTextControlSingleLine::createSubtreeIfNeeded()
635 {
636     if (inputElement()->isSearchField()) {
637         if (!m_innerBlock) {
638             // Create the inner block element
639             m_innerBlock = TextControlInnerElement::create(toHTMLElement(node()));
640             m_innerBlock->attachInnerElement(node(), createInnerBlockStyle(style()), renderArena());
641         }
642
643 #if ENABLE(INPUT_SPEECH)
644         if (inputElement()->isSpeechEnabled() && !m_speechButton) {
645             // Create the speech button element.
646             m_speechButton = InputFieldSpeechButtonElement::create(toHTMLElement(node()));
647             m_speechButton->attachInnerElement(node(), createSpeechButtonStyle(), renderArena());
648         }
649 #endif
650
651         if (!m_resultsButton) {
652             // Create the search results button element.
653             m_resultsButton = SearchFieldResultsButtonElement::create(document());
654             m_resultsButton->attachInnerElement(m_innerBlock.get(), createResultsButtonStyle(m_innerBlock->renderer()->style()), renderArena());
655         }
656
657         // Create innerText element before adding the other buttons.
658         RenderTextControl::createSubtreeIfNeeded(m_innerBlock.get());
659
660         if (!m_cancelButton) {
661             // Create the cancel button element.
662             m_cancelButton = SearchFieldCancelButtonElement::create(document());
663             m_cancelButton->attachInnerElement(m_innerBlock.get(), createCancelButtonStyle(m_innerBlock->renderer()->style()), renderArena());
664         }
665     } else {
666         RenderTextControl::createSubtreeIfNeeded(0);
667
668 #if ENABLE(INPUT_SPEECH)
669         if (inputElement()->isSpeechEnabled() && !m_speechButton) {
670             // Create the speech button element.
671             m_speechButton = InputFieldSpeechButtonElement::create(toHTMLElement(node()));
672             m_speechButton->attachInnerElement(node(), createSpeechButtonStyle(), renderArena());
673         }
674 #endif
675
676         bool hasSpinButton = inputElement()->hasSpinButton();
677
678         if (hasSpinButton && !m_innerSpinButton) {
679             m_innerSpinButton = SpinButtonElement::create(toHTMLElement(node()));
680             m_innerSpinButton->attachInnerElement(node(), createInnerSpinButtonStyle(), renderArena());
681         }
682         if (hasSpinButton && !m_outerSpinButton) {
683             m_outerSpinButton = SpinButtonElement::create(toHTMLElement(node()));
684             m_outerSpinButton->attachInnerElement(node(), createOuterSpinButtonStyle(), renderArena());
685         }
686     }
687 }
688
689 void RenderTextControlSingleLine::updateFromElement()
690 {
691     createSubtreeIfNeeded();
692     RenderTextControl::updateFromElement();
693
694     if (m_cancelButton)
695         updateCancelButtonVisibility();
696
697     if (!inputElement()->suggestedValue().isNull())
698         setInnerTextValue(inputElement()->suggestedValue());
699     else {
700         if (node()->hasTagName(inputTag)) {
701             // For HTMLInputElement, update the renderer value if the formControlValueMatchesRenderer()
702             // flag is false. It protects an unacceptable renderer value from
703             // being overwritten with the DOM value.
704             if (!static_cast<HTMLInputElement*>(node())->formControlValueMatchesRenderer())
705                 setInnerTextValue(inputElement()->visibleValue());
706         }
707     }
708
709     if (m_searchPopupIsVisible)
710         m_searchPopup->popupMenu()->updateFromElement();
711 }
712
713 void RenderTextControlSingleLine::cacheSelection(int start, int end)
714 {
715     inputElement()->cacheSelection(start, end);
716 }
717
718 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerTextStyle(const RenderStyle* startStyle) const
719 {
720     RefPtr<RenderStyle> textBlockStyle = RenderStyle::create();   
721     textBlockStyle->inheritFrom(startStyle);
722     adjustInnerTextStyle(startStyle, textBlockStyle.get());
723
724     textBlockStyle->setWhiteSpace(PRE);
725     textBlockStyle->setWordWrap(NormalWordWrap);
726     textBlockStyle->setOverflowX(OHIDDEN);
727     textBlockStyle->setOverflowY(OHIDDEN);
728
729     // Do not allow line-height to be smaller than our default.
730     if (textBlockStyle->fontMetrics().lineSpacing() > lineHeight(true, HorizontalLine, PositionOfInteriorLineBoxes))
731         textBlockStyle->setLineHeight(Length(-100.0f, Percent));
732
733     WebCore::EDisplay display = (m_innerBlock || inputElement()->hasSpinButton() ? INLINE_BLOCK : BLOCK);
734 #if ENABLE(INPUT_SPEECH)
735     if (inputElement()->isSpeechEnabled())
736       display = INLINE_BLOCK;
737 #endif
738     textBlockStyle->setDisplay(display);
739
740     // We're adding one extra pixel of padding to match WinIE.
741     textBlockStyle->setPaddingLeft(Length(1, Fixed));
742     textBlockStyle->setPaddingRight(Length(1, Fixed));
743
744     return textBlockStyle.release();
745 }
746
747 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerBlockStyle(const RenderStyle* startStyle) const
748 {
749     ASSERT(node()->isHTMLElement());
750
751     RefPtr<RenderStyle> innerBlockStyle = RenderStyle::create();
752     innerBlockStyle->inheritFrom(startStyle);
753
754     innerBlockStyle->setDisplay(BLOCK);
755     innerBlockStyle->setDirection(LTR);
756
757     // 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.
758     innerBlockStyle->setUserModify(READ_ONLY);
759
760     return innerBlockStyle.release();
761 }
762
763 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createResultsButtonStyle(const RenderStyle* startStyle) const
764 {
765     ASSERT(node()->isHTMLElement());
766     HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
767
768     RefPtr<RenderStyle> resultsBlockStyle;
769     if (input->maxResults() < 0)
770         resultsBlockStyle = getCachedPseudoStyle(SEARCH_DECORATION);
771     else if (!input->maxResults())
772         resultsBlockStyle = getCachedPseudoStyle(SEARCH_RESULTS_DECORATION);
773     else
774         resultsBlockStyle = getCachedPseudoStyle(SEARCH_RESULTS_BUTTON);
775
776     if (!resultsBlockStyle)
777         resultsBlockStyle = RenderStyle::create();
778
779     if (startStyle)
780         resultsBlockStyle->inheritFrom(startStyle);
781
782     return resultsBlockStyle.release();
783 }
784
785 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createCancelButtonStyle(const RenderStyle* startStyle) const
786 {
787     ASSERT(node()->isHTMLElement());
788     RefPtr<RenderStyle> cancelBlockStyle;
789     
790     if (RefPtr<RenderStyle> pseudoStyle = getCachedPseudoStyle(SEARCH_CANCEL_BUTTON))
791         // We may be sharing style with another search field, but we must not share the cancel button style.
792         cancelBlockStyle = RenderStyle::clone(pseudoStyle.get());
793     else
794         cancelBlockStyle = RenderStyle::create();
795
796     if (startStyle)
797         cancelBlockStyle->inheritFrom(startStyle);
798
799     cancelBlockStyle->setVisibility(visibilityForCancelButton());
800     return cancelBlockStyle.release();
801 }
802
803 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerSpinButtonStyle() const
804 {
805     ASSERT(node()->isHTMLElement());
806     RefPtr<RenderStyle> buttonStyle = getCachedPseudoStyle(INNER_SPIN_BUTTON);
807     if (!buttonStyle)
808         buttonStyle = RenderStyle::create();
809     buttonStyle->inheritFrom(style());
810     return buttonStyle.release();
811 }
812
813 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createOuterSpinButtonStyle() const
814 {
815     ASSERT(node()->isHTMLElement());
816     RefPtr<RenderStyle> buttonStyle = getCachedPseudoStyle(OUTER_SPIN_BUTTON);
817     if (!buttonStyle)
818         buttonStyle = RenderStyle::create();
819     buttonStyle->inheritFrom(style());
820     return buttonStyle.release();
821 }
822
823 #if ENABLE(INPUT_SPEECH)
824 PassRefPtr<RenderStyle> RenderTextControlSingleLine::createSpeechButtonStyle() const
825 {
826     ASSERT(node()->isHTMLElement());
827     RefPtr<RenderStyle> buttonStyle = getCachedPseudoStyle(INPUT_SPEECH_BUTTON);
828     if (!buttonStyle)
829         buttonStyle = RenderStyle::create();
830     buttonStyle->inheritFrom(style());
831     return buttonStyle.release();
832 }
833 #endif
834
835 void RenderTextControlSingleLine::updateCancelButtonVisibility() const
836 {
837     if (!m_cancelButton->renderer())
838         return;
839
840     const RenderStyle* curStyle = m_cancelButton->renderer()->style();
841     EVisibility buttonVisibility = visibilityForCancelButton();
842     if (curStyle->visibility() == buttonVisibility)
843         return;
844
845     RefPtr<RenderStyle> cancelButtonStyle = RenderStyle::clone(curStyle);
846     cancelButtonStyle->setVisibility(buttonVisibility);
847     m_cancelButton->renderer()->setStyle(cancelButtonStyle);
848 }
849
850 EVisibility RenderTextControlSingleLine::visibilityForCancelButton() const
851 {
852     ASSERT(node()->isHTMLElement());
853     HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
854     return input->value().isEmpty() ? HIDDEN : VISIBLE;
855 }
856
857 const AtomicString& RenderTextControlSingleLine::autosaveName() const
858 {
859     return static_cast<Element*>(node())->getAttribute(autosaveAttr);
860 }
861
862 void RenderTextControlSingleLine::startSearchEventTimer()
863 {
864     ASSERT(node()->isHTMLElement());
865     unsigned length = text().length();
866
867     // If there's no text, fire the event right away.
868     if (!length) {
869         stopSearchEventTimer();
870         static_cast<HTMLInputElement*>(node())->onSearch();
871         return;
872     }
873
874     // After typing the first key, we wait 0.5 seconds.
875     // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on.
876     m_searchEventTimer.startOneShot(max(0.2, 0.6 - 0.1 * length));
877 }
878
879 void RenderTextControlSingleLine::searchEventTimerFired(Timer<RenderTextControlSingleLine>*)
880 {
881     ASSERT(node()->isHTMLElement());
882     static_cast<HTMLInputElement*>(node())->onSearch();
883 }
884
885 // PopupMenuClient methods
886 void RenderTextControlSingleLine::valueChanged(unsigned listIndex, bool fireEvents)
887 {
888     ASSERT(node()->isHTMLElement());
889     ASSERT(static_cast<int>(listIndex) < listSize());
890     HTMLInputElement* input = static_cast<HTMLInputElement*>(node());
891     if (static_cast<int>(listIndex) == (listSize() - 1)) {
892         if (fireEvents) {
893             m_recentSearches.clear();
894             const AtomicString& name = autosaveName();
895             if (!name.isEmpty()) {
896                 if (!m_searchPopup)
897                     m_searchPopup = document()->page()->chrome()->createSearchPopupMenu(this);
898                 m_searchPopup->saveRecentSearches(name, m_recentSearches);
899             }
900         }
901     } else {
902         input->setValue(itemText(listIndex));
903         if (fireEvents)
904             input->onSearch();
905         input->select();
906     }
907 }
908
909 String RenderTextControlSingleLine::itemText(unsigned listIndex) const
910 {
911     int size = listSize();
912     if (size == 1) {
913         ASSERT(!listIndex);
914         return searchMenuNoRecentSearchesText();
915     }
916     if (!listIndex)
917         return searchMenuRecentSearchesText();
918     if (itemIsSeparator(listIndex))
919         return String();
920     if (static_cast<int>(listIndex) == (size - 1))
921         return searchMenuClearRecentSearchesText();
922     return m_recentSearches[listIndex - 1];
923 }
924
925 String RenderTextControlSingleLine::itemLabel(unsigned) const
926 {
927     return String();
928 }
929
930 String RenderTextControlSingleLine::itemIcon(unsigned) const
931 {
932     return String();
933 }
934
935 bool RenderTextControlSingleLine::itemIsEnabled(unsigned listIndex) const
936 {
937      if (!listIndex || itemIsSeparator(listIndex))
938         return false;
939     return true;
940 }
941
942 PopupMenuStyle RenderTextControlSingleLine::itemStyle(unsigned) const
943 {
944     return menuStyle();
945 }
946
947 PopupMenuStyle RenderTextControlSingleLine::menuStyle() const
948 {
949     return PopupMenuStyle(style()->visitedDependentColor(CSSPropertyColor), style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->font(), style()->visibility() == VISIBLE, style()->display() == NONE, style()->textIndent(), style()->direction(), style()->unicodeBidi() == Override);
950 }
951
952 int RenderTextControlSingleLine::clientInsetLeft() const
953 {
954     // Inset the menu by the radius of the cap on the left so that
955     // it only runs along the straight part of the bezel.
956     return height() / 2;
957 }
958
959 int RenderTextControlSingleLine::clientInsetRight() const
960 {
961     // Inset the menu by the radius of the cap on the right so that
962     // it only runs along the straight part of the bezel (unless it needs
963     // to be wider).
964     return height() / 2;
965 }
966
967 int RenderTextControlSingleLine::clientPaddingLeft() const
968 {
969     int padding = paddingLeft();
970
971     if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0)
972         padding += resultsRenderer->width() + resultsRenderer->marginLeft() + resultsRenderer->paddingLeft() + resultsRenderer->marginRight() + resultsRenderer->paddingRight();
973
974     return padding;
975 }
976
977 int RenderTextControlSingleLine::clientPaddingRight() const
978 {
979     int padding = paddingRight();
980
981     if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0)
982         padding += cancelRenderer->width() + cancelRenderer->marginLeft() + cancelRenderer->paddingLeft() + cancelRenderer->marginRight() + cancelRenderer->paddingRight();
983
984     return padding;
985 }
986
987 int RenderTextControlSingleLine::listSize() const
988 {
989     // If there are no recent searches, then our menu will have 1 "No recent searches" item.
990     if (!m_recentSearches.size())
991         return 1;
992     // Otherwise, leave room in the menu for a header, a separator, and the "Clear recent searches" item.
993     return m_recentSearches.size() + 3;
994 }
995
996 int RenderTextControlSingleLine::selectedIndex() const
997 {
998     return -1;
999 }
1000
1001 void RenderTextControlSingleLine::popupDidHide()
1002 {
1003     m_searchPopupIsVisible = false;
1004 }
1005
1006 bool RenderTextControlSingleLine::itemIsSeparator(unsigned listIndex) const
1007 {
1008     // The separator will be the second to last item in our list.
1009     return static_cast<int>(listIndex) == (listSize() - 2);
1010 }
1011
1012 bool RenderTextControlSingleLine::itemIsLabel(unsigned listIndex) const
1013 {
1014     return listIndex == 0;
1015 }
1016
1017 bool RenderTextControlSingleLine::itemIsSelected(unsigned) const
1018 {
1019     return false;
1020 }
1021
1022 void RenderTextControlSingleLine::setTextFromItem(unsigned listIndex)
1023 {
1024     ASSERT(node()->isHTMLElement());
1025     static_cast<HTMLInputElement*>(node())->setValue(itemText(listIndex));
1026 }
1027
1028 FontSelector* RenderTextControlSingleLine::fontSelector() const
1029 {
1030     return document()->styleSelector()->fontSelector();
1031 }
1032
1033 HostWindow* RenderTextControlSingleLine::hostWindow() const
1034 {
1035     return document()->view()->hostWindow();
1036 }
1037
1038 void RenderTextControlSingleLine::autoscroll()
1039 {
1040     RenderLayer* layer = innerTextElement()->renderBox()->layer();
1041     if (layer)
1042         layer->autoscroll();
1043 }
1044
1045 int RenderTextControlSingleLine::scrollWidth() const
1046 {
1047     if (innerTextElement())
1048         return innerTextElement()->scrollWidth();
1049     return RenderBlock::scrollWidth();
1050 }
1051
1052 int RenderTextControlSingleLine::scrollHeight() const
1053 {
1054     if (innerTextElement())
1055         return innerTextElement()->scrollHeight();
1056     return RenderBlock::scrollHeight();
1057 }
1058
1059 int RenderTextControlSingleLine::scrollLeft() const
1060 {
1061     if (innerTextElement())
1062         return innerTextElement()->scrollLeft();
1063     return RenderBlock::scrollLeft();
1064 }
1065
1066 int RenderTextControlSingleLine::scrollTop() const
1067 {
1068     if (innerTextElement())
1069         return innerTextElement()->scrollTop();
1070     return RenderBlock::scrollTop();
1071 }
1072
1073 void RenderTextControlSingleLine::setScrollLeft(int newLeft)
1074 {
1075     if (innerTextElement())
1076         innerTextElement()->setScrollLeft(newLeft);
1077 }
1078
1079 void RenderTextControlSingleLine::setScrollTop(int newTop)
1080 {
1081     if (innerTextElement())
1082         innerTextElement()->setScrollTop(newTop);
1083 }
1084
1085 bool RenderTextControlSingleLine::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
1086 {
1087     RenderLayer* layer = innerTextElement()->renderBox()->layer();
1088     if (layer && layer->scroll(direction, granularity, multiplier))
1089         return true;
1090     return RenderBlock::scroll(direction, granularity, multiplier, stopNode);
1091 }
1092
1093 bool RenderTextControlSingleLine::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
1094 {
1095     RenderLayer* layer = innerTextElement()->renderBox()->layer();
1096     if (layer && layer->scroll(logicalToPhysical(direction, style()->isHorizontalWritingMode(), style()->isFlippedBlocksWritingMode()), granularity, multiplier))
1097         return true;
1098     return RenderBlock::logicalScroll(direction, granularity, multiplier, stopNode);
1099 }
1100
1101 PassRefPtr<Scrollbar> RenderTextControlSingleLine::createScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
1102 {
1103     RefPtr<Scrollbar> widget;
1104     bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR);
1105     if (hasCustomScrollbarStyle)
1106         widget = RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, this);
1107     else
1108         widget = Scrollbar::createNativeScrollbar(scrollableArea, orientation, controlSize);
1109     return widget.release();
1110 }
1111
1112 InputElement* RenderTextControlSingleLine::inputElement() const
1113 {
1114     return node()->toInputElement();
1115 }
1116
1117 int RenderTextControlSingleLine::textBlockInsetLeft() const
1118 {
1119     int inset = borderLeft() + clientPaddingLeft();
1120     if (HTMLElement* innerText = innerTextElement()) {
1121         if (RenderBox* innerTextRenderer = innerText->renderBox())
1122             inset += innerTextRenderer->paddingLeft();
1123     }
1124     return inset;
1125 }
1126     
1127 int RenderTextControlSingleLine::textBlockInsetRight() const
1128 {
1129     int inset = borderRight() + clientPaddingRight();
1130     if (HTMLElement* innerText = innerTextElement()) {
1131         if (RenderBox* innerTextRenderer = innerText->renderBox())
1132             inset += innerTextRenderer->paddingRight();
1133     }
1134     return inset;
1135 }
1136
1137 int RenderTextControlSingleLine::textBlockInsetTop() const
1138 {
1139     RenderBox* innerRenderer = 0;
1140     if (m_innerBlock)
1141         innerRenderer = m_innerBlock->renderBox();
1142     else if (HTMLElement* innerText = innerTextElement())
1143         innerRenderer = innerText->renderBox();
1144     
1145     if (innerRenderer)
1146         return innerRenderer->y();
1147     
1148     return borderTop() + paddingTop();
1149 }    
1150
1151 }