initial import
[vuplus_webkit] / Source / WebCore / html / SearchInputType.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "SearchInputType.h"
33
34 #include "HTMLInputElement.h"
35 #include "KeyboardEvent.h"
36 #include "RenderTextControlSingleLine.h"
37 #include "ShadowRoot.h"
38 #include "TextControlInnerElements.h"
39 #include <wtf/PassOwnPtr.h>
40
41 namespace WebCore {
42
43 inline SearchInputType::SearchInputType(HTMLInputElement* element)
44     : BaseTextInputType(element)
45     , m_searchEventTimer(this, &SearchInputType::searchEventTimerFired)
46 {
47 }
48
49 PassOwnPtr<InputType> SearchInputType::create(HTMLInputElement* element)
50 {
51     return adoptPtr(new SearchInputType(element));
52 }
53
54 const AtomicString& SearchInputType::formControlType() const
55 {
56     return InputTypeNames::search();
57 }
58
59 bool SearchInputType::shouldRespectSpeechAttribute()
60 {
61     return true;
62 }
63
64 bool SearchInputType::isSearchField() const
65 {
66     return true;
67 }
68
69 bool SearchInputType::needsContainer() const
70 {
71     return true;
72 }
73
74 void SearchInputType::createShadowSubtree()
75 {
76     ASSERT(!m_resultsButton);
77     ASSERT(!m_cancelButton);
78
79     TextFieldInputType::createShadowSubtree();
80     HTMLElement* container = containerElement();
81     HTMLElement* textWrapper = innerBlockElement();
82     ASSERT(container);
83     ASSERT(textWrapper);
84
85     ExceptionCode ec = 0;
86     m_resultsButton = SearchFieldResultsButtonElement::create(element()->document());
87     container->insertBefore(m_resultsButton, textWrapper, ec);
88
89     m_cancelButton = SearchFieldCancelButtonElement::create(element()->document());
90     container->insertBefore(m_cancelButton, textWrapper->nextSibling(), ec);
91 }
92
93 HTMLElement* SearchInputType::resultsButtonElement() const
94 {
95     return m_resultsButton.get();
96 }
97
98 HTMLElement* SearchInputType::cancelButtonElement() const
99 {
100     return m_cancelButton.get();
101 }
102
103 void SearchInputType::handleKeydownEvent(KeyboardEvent* event)
104 {
105     if (element()->disabled() || element()->readOnly()) {
106         TextFieldInputType::handleKeydownEvent(event);
107         return;
108     }
109
110     const String& key = event->keyIdentifier();
111     if (key == "U+001B") {
112         RefPtr<HTMLInputElement> input = element();
113         input->setValueForUser("");
114         input->onSearch();
115         event->setDefaultHandled();
116         return;
117     }
118     TextFieldInputType::handleKeydownEvent(event);
119 }
120
121 void SearchInputType::destroyShadowSubtree()
122 {
123     TextFieldInputType::destroyShadowSubtree();
124     m_resultsButton.clear();
125     m_cancelButton.clear();
126 }
127
128 void SearchInputType::startSearchEventTimer()
129 {
130     ASSERT(element()->renderer());
131     unsigned length = element()->innerTextValue().length();
132
133     if (!length) {
134         stopSearchEventTimer();
135         element()->onSearch();
136         return;
137     }
138
139     // After typing the first key, we wait 0.5 seconds.
140     // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on.
141     m_searchEventTimer.startOneShot(max(0.2, 0.6 - 0.1 * length));
142 }
143
144 void SearchInputType::stopSearchEventTimer()
145 {
146     m_searchEventTimer.stop();
147 }
148
149 void SearchInputType::searchEventTimerFired(Timer<SearchInputType>*)
150 {
151     element()->onSearch();
152 }
153
154
155 } // namespace WebCore