OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2009 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 "AutocompletePopupMenuClient.h" |
| 33 |
| 34 #include "CSSValueKeywords.h" |
| 35 #include "CSSStyleSelector.h" |
| 36 #include "FrameView.h" |
| 37 #include "HTMLInputElement.h" |
| 38 #include "RenderTheme.h" |
| 39 #include "WebVector.h" |
| 40 #include "WebViewImpl.h" |
| 41 |
| 42 using namespace WebCore; |
| 43 |
| 44 namespace WebKit { |
| 45 |
| 46 AutocompletePopupMenuClient::AutocompletePopupMenuClient(WebViewImpl* webView) |
| 47 : m_textField(0) |
| 48 , m_selectedIndex(0) |
| 49 , m_webView(webView) |
| 50 { |
| 51 } |
| 52 |
| 53 AutocompletePopupMenuClient::~AutocompletePopupMenuClient() |
| 54 { |
| 55 } |
| 56 |
| 57 void AutocompletePopupMenuClient::initialize( |
| 58 HTMLInputElement* textField, |
| 59 const WebVector<WebString>& suggestions, |
| 60 int defaultSuggestionIndex) |
| 61 { |
| 62 ASSERT(defaultSuggestionIndex < static_cast<int>(suggestions.size())); |
| 63 m_textField = textField; |
| 64 m_selectedIndex = defaultSuggestionIndex; |
| 65 setSuggestions(suggestions); |
| 66 |
| 67 FontDescription fontDescription; |
| 68 m_webView->theme()->systemFont(CSSValueWebkitControl, fontDescription); |
| 69 // Use a smaller font size to match IE/Firefox. |
| 70 // FIXME: http://crbug.com/7376 use the system size instead of a |
| 71 // fixed font size value. |
| 72 fontDescription.setComputedSize(12.0); |
| 73 Font font(fontDescription, 0, 0); |
| 74 font.update(textField->document()->styleSelector()->fontSelector()); |
| 75 // The direction of text in popup menu is set the same as the direction of |
| 76 // the input element: textField. |
| 77 m_style.set(new PopupMenuStyle(Color::black, Color::white, font, true, |
| 78 Length(WebCore::Fixed), |
| 79 textField->renderer()->style()->direction())); |
| 80 } |
| 81 |
| 82 void AutocompletePopupMenuClient::valueChanged(unsigned listIndex, bool fireEvents) |
| 83 { |
| 84 m_textField->setValue(m_suggestions[listIndex]); |
| 85 EditorClientImpl* editor = |
| 86 static_cast<EditorClientImpl*>(m_webView->page()->editorClient()); |
| 87 ASSERT(editor); |
| 88 editor->onAutofillSuggestionAccepted( |
| 89 static_cast<HTMLInputElement*>(m_textField.get())); |
| 90 } |
| 91 |
| 92 String AutocompletePopupMenuClient::itemText(unsigned listIndex) const |
| 93 { |
| 94 return m_suggestions[listIndex]; |
| 95 } |
| 96 |
| 97 PopupMenuStyle AutocompletePopupMenuClient::itemStyle(unsigned listIndex) const |
| 98 { |
| 99 return *m_style; |
| 100 } |
| 101 |
| 102 PopupMenuStyle AutocompletePopupMenuClient::menuStyle() const |
| 103 { |
| 104 return *m_style; |
| 105 } |
| 106 |
| 107 int AutocompletePopupMenuClient::clientPaddingLeft() const |
| 108 { |
| 109 // Bug http://crbug.com/7708 seems to indicate the style can be NULL. |
| 110 RenderStyle* style = textFieldStyle(); |
| 111 return style ? m_webView->theme()->popupInternalPaddingLeft(style) : 0; |
| 112 } |
| 113 |
| 114 int AutocompletePopupMenuClient::clientPaddingRight() const |
| 115 { |
| 116 // Bug http://crbug.com/7708 seems to indicate the style can be NULL. |
| 117 RenderStyle* style = textFieldStyle(); |
| 118 return style ? m_webView->theme()->popupInternalPaddingRight(style) : 0; |
| 119 } |
| 120 |
| 121 void AutocompletePopupMenuClient::popupDidHide() |
| 122 { |
| 123 m_webView->autoCompletePopupDidHide(); |
| 124 } |
| 125 |
| 126 void AutocompletePopupMenuClient::setTextFromItem(unsigned listIndex) |
| 127 { |
| 128 m_textField->setValue(m_suggestions[listIndex]); |
| 129 } |
| 130 |
| 131 FontSelector* AutocompletePopupMenuClient::fontSelector() const |
| 132 { |
| 133 return m_textField->document()->styleSelector()->fontSelector(); |
| 134 } |
| 135 |
| 136 HostWindow* AutocompletePopupMenuClient::hostWindow() const |
| 137 { |
| 138 return m_textField->document()->view()->hostWindow(); |
| 139 } |
| 140 |
| 141 PassRefPtr<Scrollbar> AutocompletePopupMenuClient::createScrollbar( |
| 142 ScrollbarClient* client, |
| 143 ScrollbarOrientation orientation, |
| 144 ScrollbarControlSize size) |
| 145 { |
| 146 return Scrollbar::createNativeScrollbar(client, orientation, size); |
| 147 } |
| 148 |
| 149 void AutocompletePopupMenuClient::setSuggestions(const WebVector<WebString>& suggestions) |
| 150 { |
| 151 m_suggestions.clear(); |
| 152 for (size_t i = 0; i < suggestions.size(); ++i) |
| 153 m_suggestions.append(suggestions[i]); |
| 154 // Try to preserve selection if possible. |
| 155 if (m_selectedIndex >= static_cast<int>(suggestions.size())) |
| 156 m_selectedIndex = -1; |
| 157 } |
| 158 |
| 159 void AutocompletePopupMenuClient::removeItemAtIndex(int index) |
| 160 { |
| 161 ASSERT(index >= 0 && index < static_cast<int>(m_suggestions.size())); |
| 162 m_suggestions.remove(index); |
| 163 } |
| 164 |
| 165 RenderStyle* AutocompletePopupMenuClient::textFieldStyle() const |
| 166 { |
| 167 RenderStyle* style = m_textField->computedStyle(); |
| 168 if (!style) { |
| 169 // It seems we can only have an NULL style in a TextField if the |
| 170 // node is dettached, in which case we the popup shoud not be |
| 171 // showing. Please report this in http://crbug.com/7708 and |
| 172 // include the page you were visiting. |
| 173 ASSERT_NOT_REACHED(); |
| 174 } |
| 175 return style; |
| 176 } |
| 177 |
| 178 } // namespace WebKit |
OLD | NEW |