2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "AutoFillPopupMenuClient.h"
34 #include "CSSStyleSelector.h"
35 #include "CSSValueKeywords.h"
37 #include "FrameView.h"
38 #include "HTMLInputElement.h"
39 #include "RenderTheme.h"
40 #include "WebAutoFillClient.h"
42 #include "WebString.h"
43 #include "WebVector.h"
44 #include "WebViewClient.h"
45 #include "WebViewImpl.h"
47 using namespace WebCore;
51 AutoFillPopupMenuClient::AutoFillPopupMenuClient()
52 : m_separatorIndex(-1)
58 AutoFillPopupMenuClient::~AutoFillPopupMenuClient()
62 unsigned AutoFillPopupMenuClient::getSuggestionsCount() const
64 return m_names.size() + ((m_separatorIndex == -1) ? 0 : 1);
67 WebString AutoFillPopupMenuClient::getSuggestion(unsigned listIndex) const
69 int index = convertListIndexToInternalIndex(listIndex);
73 ASSERT(index >= 0 && static_cast<size_t>(index) < m_names.size());
74 return m_names[index];
77 WebString AutoFillPopupMenuClient::getLabel(unsigned listIndex) const
79 int index = convertListIndexToInternalIndex(listIndex);
83 ASSERT(index >= 0 && static_cast<size_t>(index) < m_labels.size());
84 return m_labels[index];
87 WebString AutoFillPopupMenuClient::getIcon(unsigned listIndex) const
89 int index = convertListIndexToInternalIndex(listIndex);
93 ASSERT(index >= 0 && static_cast<size_t>(index) < m_icons.size());
94 return m_icons[index];
97 void AutoFillPopupMenuClient::removeSuggestionAtIndex(unsigned listIndex)
99 if (!canRemoveSuggestionAtIndex(listIndex))
102 int index = convertListIndexToInternalIndex(listIndex);
104 ASSERT(static_cast<unsigned>(index) < m_names.size());
106 m_names.remove(index);
107 m_labels.remove(index);
108 m_icons.remove(index);
109 m_uniqueIDs.remove(index);
111 // Shift the separator index if necessary.
112 if (m_separatorIndex != -1)
116 bool AutoFillPopupMenuClient::canRemoveSuggestionAtIndex(unsigned listIndex)
118 // Only allow deletion of items before the separator that have unique id 0
119 // (i.e. are autocomplete rather than autofill items).
120 int index = convertListIndexToInternalIndex(listIndex);
121 return !m_uniqueIDs[index] && (m_separatorIndex == -1 || listIndex < static_cast<unsigned>(m_separatorIndex));
124 void AutoFillPopupMenuClient::valueChanged(unsigned listIndex, bool fireEvents)
126 WebViewImpl* webView = getWebView();
130 if (m_separatorIndex != -1 && listIndex > static_cast<unsigned>(m_separatorIndex))
133 ASSERT(listIndex < m_names.size());
135 webView->autoFillClient()->didAcceptAutoFillSuggestion(WebNode(getTextField()),
138 m_uniqueIDs[listIndex],
142 void AutoFillPopupMenuClient::selectionChanged(unsigned listIndex, bool fireEvents)
144 WebViewImpl* webView = getWebView();
148 if (m_separatorIndex != -1 && listIndex > static_cast<unsigned>(m_separatorIndex))
151 ASSERT(listIndex < m_names.size());
153 webView->autoFillClient()->didSelectAutoFillSuggestion(WebNode(getTextField()),
156 m_uniqueIDs[listIndex]);
159 void AutoFillPopupMenuClient::selectionCleared()
161 WebViewImpl* webView = getWebView();
163 webView->autoFillClient()->didClearAutoFillSelection(WebNode(getTextField()));
166 String AutoFillPopupMenuClient::itemText(unsigned listIndex) const
168 return getSuggestion(listIndex);
171 String AutoFillPopupMenuClient::itemLabel(unsigned listIndex) const
173 return getLabel(listIndex);
176 String AutoFillPopupMenuClient::itemIcon(unsigned listIndex) const
178 return getIcon(listIndex);
181 bool AutoFillPopupMenuClient::itemIsEnabled(unsigned listIndex) const
183 return !itemIsWarning(listIndex);
186 PopupMenuStyle AutoFillPopupMenuClient::itemStyle(unsigned listIndex) const
188 return itemIsWarning(listIndex) ? *m_warningStyle : *m_regularStyle;
191 PopupMenuStyle AutoFillPopupMenuClient::menuStyle() const
193 return *m_regularStyle;
196 int AutoFillPopupMenuClient::clientPaddingLeft() const
198 // Bug http://crbug.com/7708 seems to indicate the style can be 0.
199 RenderStyle* style = textFieldStyle();
203 return RenderTheme::defaultTheme()->popupInternalPaddingLeft(style);
206 int AutoFillPopupMenuClient::clientPaddingRight() const
208 // Bug http://crbug.com/7708 seems to indicate the style can be 0.
209 RenderStyle* style = textFieldStyle();
213 return RenderTheme::defaultTheme()->popupInternalPaddingRight(style);
216 void AutoFillPopupMenuClient::popupDidHide()
218 WebViewImpl* webView = getWebView();
222 webView->autoFillPopupDidHide();
223 webView->autoFillClient()->didClearAutoFillSelection(WebNode(getTextField()));
226 bool AutoFillPopupMenuClient::itemIsSeparator(unsigned listIndex) const
228 return (m_separatorIndex != -1 && static_cast<unsigned>(m_separatorIndex) == listIndex);
231 bool AutoFillPopupMenuClient::itemIsWarning(unsigned listIndex) const
233 int index = convertListIndexToInternalIndex(listIndex);
237 ASSERT(index >= 0 && static_cast<size_t>(index) < m_uniqueIDs.size());
238 return m_uniqueIDs[index] < 0;
241 void AutoFillPopupMenuClient::setTextFromItem(unsigned listIndex)
243 m_textField->setValue(getSuggestion(listIndex));
246 FontSelector* AutoFillPopupMenuClient::fontSelector() const
248 return m_textField->document()->styleSelector()->fontSelector();
251 HostWindow* AutoFillPopupMenuClient::hostWindow() const
253 return m_textField->document()->view()->hostWindow();
256 PassRefPtr<Scrollbar> AutoFillPopupMenuClient::createScrollbar(
257 ScrollableArea* scrollableArea,
258 ScrollbarOrientation orientation,
259 ScrollbarControlSize size)
261 return Scrollbar::createNativeScrollbar(scrollableArea, orientation, size);
264 void AutoFillPopupMenuClient::initialize(
265 HTMLInputElement* textField,
266 const WebVector<WebString>& names,
267 const WebVector<WebString>& labels,
268 const WebVector<WebString>& icons,
269 const WebVector<int>& uniqueIDs,
272 ASSERT(names.size() == labels.size());
273 ASSERT(names.size() == icons.size());
274 ASSERT(names.size() == uniqueIDs.size());
275 ASSERT(separatorIndex < static_cast<int>(names.size()));
277 m_selectedIndex = -1;
278 m_textField = textField;
280 // The suggestions must be set before initializing the
281 // AutoFillPopupMenuClient.
282 setSuggestions(names, labels, icons, uniqueIDs, separatorIndex);
284 FontDescription regularFontDescription;
285 RenderTheme::defaultTheme()->systemFont(CSSValueWebkitControl,
286 regularFontDescription);
287 RenderStyle* style = m_textField->computedStyle();
288 regularFontDescription.setComputedSize(style->fontDescription().computedSize());
290 Font regularFont(regularFontDescription, 0, 0);
291 regularFont.update(textField->document()->styleSelector()->fontSelector());
292 // The direction of text in popup menu is set the same as the direction of
293 // the input element: textField.
294 m_regularStyle.set(new PopupMenuStyle(Color::black, Color::white, regularFont,
295 true, false, Length(WebCore::Fixed),
296 textField->renderer()->style()->direction(), textField->renderer()->style()->unicodeBidi() == Override));
298 FontDescription warningFontDescription = regularFont.fontDescription();
299 warningFontDescription.setItalic(true);
300 Font warningFont(warningFontDescription, regularFont.letterSpacing(), regularFont.wordSpacing());
301 warningFont.update(regularFont.fontSelector());
302 m_warningStyle.set(new PopupMenuStyle(Color::darkGray,
303 m_regularStyle->backgroundColor(),
305 m_regularStyle->isVisible(),
306 m_regularStyle->isDisplayNone(),
307 m_regularStyle->textIndent(),
308 m_regularStyle->textDirection(),
309 m_regularStyle->hasTextDirectionOverride()));
312 void AutoFillPopupMenuClient::setSuggestions(const WebVector<WebString>& names,
313 const WebVector<WebString>& labels,
314 const WebVector<WebString>& icons,
315 const WebVector<int>& uniqueIDs,
318 ASSERT(names.size() == labels.size());
319 ASSERT(names.size() == icons.size());
320 ASSERT(names.size() == uniqueIDs.size());
321 ASSERT(separatorIndex < static_cast<int>(names.size()));
327 for (size_t i = 0; i < names.size(); ++i) {
328 m_names.append(names[i]);
329 m_labels.append(labels[i]);
330 m_icons.append(icons[i]);
331 m_uniqueIDs.append(uniqueIDs[i]);
334 m_separatorIndex = separatorIndex;
336 // Try to preserve selection if possible.
337 if (getSelectedIndex() >= static_cast<int>(names.size()))
338 setSelectedIndex(-1);
341 int AutoFillPopupMenuClient::convertListIndexToInternalIndex(unsigned listIndex) const
343 if (listIndex == static_cast<unsigned>(m_separatorIndex))
346 if (m_separatorIndex == -1 || listIndex < static_cast<unsigned>(m_separatorIndex))
348 return listIndex - 1;
351 WebViewImpl* AutoFillPopupMenuClient::getWebView() const
353 Frame* frame = m_textField->document()->frame();
357 Page* page = frame->page();
361 return static_cast<ChromeClientImpl*>(page->chrome()->client())->webView();
364 RenderStyle* AutoFillPopupMenuClient::textFieldStyle() const
366 RenderStyle* style = m_textField->computedStyle();
368 // It seems we can only have a 0 style in a TextField if the
369 // node is detached, in which case we the popup should not be
370 // showing. Please report this in http://crbug.com/7708 and
371 // include the page you were visiting.
372 ASSERT_NOT_REACHED();
377 } // namespace WebKit