zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 1 | /** |
| 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.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 33 | #include "InputElement.h" |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 34 | #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.org | 149606a | 2009-04-29 20:31:53 +0000 | [diff] [blame] | 42 | #include "SimpleFontData.h" |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 43 | #include "TextControlInnerElements.h" |
| 44 | |
zimmermann@webkit.org | d5c1289 | 2008-12-29 00:16:19 +0000 | [diff] [blame] | 45 | using namespace std; |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 46 | |
| 47 | namespace WebCore { |
| 48 | |
| 49 | using namespace HTMLNames; |
| 50 | |
eric@webkit.org | e6cd79c | 2009-09-26 18:56:04 +0000 | [diff] [blame] | 51 | RenderTextControlSingleLine::RenderTextControlSingleLine(Node* node, bool placeholderVisible) |
| 52 | : RenderTextControl(node, placeholderVisible) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 53 | , m_searchPopupIsVisible(false) |
| 54 | , m_shouldDrawCapsLockIndicator(false) |
| 55 | , m_searchEventTimer(this, &RenderTextControlSingleLine::searchEventTimerFired) |
| 56 | , m_searchPopup(0) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | RenderTextControlSingleLine::~RenderTextControlSingleLine() |
| 61 | { |
| 62 | if (m_searchPopup) { |
| 63 | m_searchPopup->disconnectClient(); |
| 64 | m_searchPopup = 0; |
| 65 | } |
| 66 | |
| 67 | if (m_innerBlock) |
| 68 | m_innerBlock->detach(); |
| 69 | } |
| 70 | |
eric@webkit.org | b05364f | 2009-08-24 08:28:51 +0000 | [diff] [blame] | 71 | RenderStyle* RenderTextControlSingleLine::textBaseStyle() const |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 72 | { |
eric@webkit.org | b05364f | 2009-08-24 08:28:51 +0000 | [diff] [blame] | 73 | return m_innerBlock ? m_innerBlock->renderer()->style() : style(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void RenderTextControlSingleLine::addSearchResult() |
| 77 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 78 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 79 | HTMLInputElement* input = static_cast<HTMLInputElement*>(node()); |
| 80 | if (input->maxResults() <= 0) |
| 81 | return; |
| 82 | |
| 83 | String value = input->value(); |
| 84 | if (value.isEmpty()) |
| 85 | return; |
| 86 | |
| 87 | Settings* settings = document()->settings(); |
| 88 | if (!settings || settings->privateBrowsingEnabled()) |
| 89 | return; |
| 90 | |
| 91 | int size = static_cast<int>(m_recentSearches.size()); |
| 92 | for (int i = size - 1; i >= 0; --i) { |
| 93 | if (m_recentSearches[i] == value) |
| 94 | m_recentSearches.remove(i); |
| 95 | } |
| 96 | |
| 97 | m_recentSearches.insert(0, value); |
| 98 | while (static_cast<int>(m_recentSearches.size()) > input->maxResults()) |
| 99 | m_recentSearches.removeLast(); |
| 100 | |
| 101 | const AtomicString& name = autosaveName(); |
| 102 | if (!m_searchPopup) |
| 103 | m_searchPopup = SearchPopupMenu::create(this); |
| 104 | |
| 105 | m_searchPopup->saveRecentSearches(name, m_recentSearches); |
| 106 | } |
| 107 | |
| 108 | void RenderTextControlSingleLine::stopSearchEventTimer() |
| 109 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 110 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 111 | m_searchEventTimer.stop(); |
| 112 | } |
| 113 | |
| 114 | void RenderTextControlSingleLine::showPopup() |
| 115 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 116 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 117 | if (m_searchPopupIsVisible) |
| 118 | return; |
| 119 | |
| 120 | if (!m_searchPopup) |
| 121 | m_searchPopup = SearchPopupMenu::create(this); |
| 122 | |
| 123 | if (!m_searchPopup->enabled()) |
| 124 | return; |
| 125 | |
| 126 | m_searchPopupIsVisible = true; |
| 127 | |
| 128 | const AtomicString& name = autosaveName(); |
| 129 | m_searchPopup->loadRecentSearches(name, m_recentSearches); |
| 130 | |
| 131 | // Trim the recent searches list if the maximum size has changed since we last saved. |
| 132 | HTMLInputElement* input = static_cast<HTMLInputElement*>(node()); |
| 133 | if (static_cast<int>(m_recentSearches.size()) > input->maxResults()) { |
| 134 | do { |
| 135 | m_recentSearches.removeLast(); |
| 136 | } while (static_cast<int>(m_recentSearches.size()) > input->maxResults()); |
| 137 | |
| 138 | m_searchPopup->saveRecentSearches(name, m_recentSearches); |
| 139 | } |
| 140 | |
| 141 | m_searchPopup->show(absoluteBoundingBoxRect(true), document()->view(), -1); |
| 142 | } |
| 143 | |
| 144 | void RenderTextControlSingleLine::hidePopup() |
| 145 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 146 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 147 | if (m_searchPopup) |
| 148 | m_searchPopup->hide(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void RenderTextControlSingleLine::subtreeHasChanged() |
| 152 | { |
eric@webkit.org | 6e55fa2 | 2009-11-18 23:46:06 +0000 | [diff] [blame] | 153 | bool wasChanged = wasChangedSinceLastChangeEvent(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 154 | RenderTextControl::subtreeHasChanged(); |
| 155 | |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 156 | InputElement* input = inputElement(); |
eric@webkit.org | e235317 | 2009-09-17 02:13:49 +0000 | [diff] [blame] | 157 | // We don't need to call sanitizeUserInputValue() function here because |
| 158 | // InputElement::handleBeforeTextInsertedEvent() has already called |
| 159 | // sanitizeUserInputValue(). |
| 160 | // sanitizeValue() is needed because IME input doesn't dispatch BeforeTextInsertedEvent. |
| 161 | input->setValueFromRenderer(input->sanitizeValue(text())); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 162 | |
simon.fraser@apple.com | f71e81d | 2009-03-11 19:00:13 +0000 | [diff] [blame] | 163 | if (m_cancelButton) |
| 164 | updateCancelButtonVisibility(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 165 | |
| 166 | // If the incremental attribute is set, then dispatch the search event |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 167 | if (input->searchEventsShouldBeDispatched()) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 168 | startSearchEventTimer(); |
| 169 | |
eric@webkit.org | 6e55fa2 | 2009-11-18 23:46:06 +0000 | [diff] [blame] | 170 | if (!wasChanged && node()->focused()) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 171 | if (Frame* frame = document()->frame()) |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 172 | frame->textFieldDidBeginEditing(static_cast<Element*>(node())); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 173 | } |
| 174 | |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 175 | if (node()->focused()) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 176 | if (Frame* frame = document()->frame()) |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 177 | frame->textDidChangeInTextField(static_cast<Element*>(node())); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | void RenderTextControlSingleLine::paint(PaintInfo& paintInfo, int tx, int ty) |
| 182 | { |
| 183 | RenderTextControl::paint(paintInfo, tx, ty); |
| 184 | |
| 185 | if (paintInfo.phase == PaintPhaseBlockBackground && m_shouldDrawCapsLockIndicator) { |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 186 | IntRect contentsRect = contentBoxRect(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 187 | |
| 188 | // Convert the rect into the coords used for painting the content |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 189 | contentsRect.move(tx + x(), ty + y()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 190 | theme()->paintCapsLockIndicator(this, paintInfo, contentsRect); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void RenderTextControlSingleLine::layout() |
| 195 | { |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 196 | int oldHeight = height(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 197 | calcHeight(); |
| 198 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 199 | int oldWidth = width(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 200 | calcWidth(); |
| 201 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 202 | bool relayoutChildren = oldHeight != height() || oldWidth != width(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 203 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 204 | RenderBox* innerTextRenderer = innerTextElement()->renderBox(); |
| 205 | RenderBox* innerBlockRenderer = m_innerBlock ? m_innerBlock->renderBox() : 0; |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 206 | |
| 207 | // Set the text block height |
| 208 | int desiredHeight = textBlockHeight(); |
| 209 | int currentHeight = innerTextRenderer->height(); |
| 210 | |
mitz@apple.com | ae136e5 | 2009-06-18 17:54:16 +0000 | [diff] [blame] | 211 | if (currentHeight > height()) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 212 | if (desiredHeight != currentHeight) |
| 213 | relayoutChildren = true; |
| 214 | innerTextRenderer->style()->setHeight(Length(desiredHeight, Fixed)); |
mitz@apple.com | ae136e5 | 2009-06-18 17:54:16 +0000 | [diff] [blame] | 215 | if (m_innerBlock) |
| 216 | innerBlockRenderer->style()->setHeight(Length(desiredHeight, Fixed)); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Set the text block width |
| 220 | int desiredWidth = textBlockWidth(); |
| 221 | if (desiredWidth != innerTextRenderer->width()) |
| 222 | relayoutChildren = true; |
| 223 | innerTextRenderer->style()->setWidth(Length(desiredWidth, Fixed)); |
| 224 | |
| 225 | if (m_innerBlock) { |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 226 | int innerBlockWidth = width() - paddingLeft() - paddingRight() - borderLeft() - borderRight(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 227 | if (innerBlockWidth != innerBlockRenderer->width()) |
| 228 | relayoutChildren = true; |
| 229 | innerBlockRenderer->style()->setWidth(Length(innerBlockWidth, Fixed)); |
| 230 | } |
| 231 | |
| 232 | RenderBlock::layoutBlock(relayoutChildren); |
| 233 | |
mitz@apple.com | ae136e5 | 2009-06-18 17:54:16 +0000 | [diff] [blame] | 234 | // Center the child block vertically |
| 235 | RenderBox* childBlock = innerBlockRenderer ? innerBlockRenderer : innerTextRenderer; |
| 236 | currentHeight = childBlock->height(); |
| 237 | if (currentHeight < height()) |
| 238 | childBlock->setLocation(childBlock->x(), (height() - currentHeight) / 2); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 239 | } |
| 240 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 241 | bool RenderTextControlSingleLine::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction hitTestAction) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 242 | { |
| 243 | // 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 |
| 244 | // was on the control but not on the inner element (see Radar 4617841). |
| 245 | |
| 246 | // 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, |
| 247 | // and act as if we've hit the close block if we're to the right of the inner text block. |
| 248 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 249 | if (!RenderTextControl::nodeAtPoint(request, result, xPos, yPos, tx, ty, hitTestAction)) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 250 | return false; |
| 251 | |
simon.fraser@apple.com | 68ed99e | 2009-03-11 05:43:58 +0000 | [diff] [blame] | 252 | // If we hit a node inside the inner text element, say that we hit that element, |
| 253 | // and if we hit our node (e.g. we're over the border or padding), also say that we hit the |
| 254 | // inner text element so that it gains focus. |
| 255 | if (result.innerNode()->isDescendantOf(innerTextElement()) || result.innerNode() == node()) |
| 256 | hitInnerTextElement(result, xPos, yPos, tx, ty); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 257 | |
simon.fraser@apple.com | 68ed99e | 2009-03-11 05:43:58 +0000 | [diff] [blame] | 258 | // If we're not a search field, or we already found the results or cancel buttons, we're done. |
| 259 | if (!m_innerBlock || result.innerNode() == m_resultsButton || result.innerNode() == m_cancelButton) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 260 | return true; |
| 261 | |
| 262 | Node* innerNode = 0; |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 263 | RenderBox* innerBlockRenderer = m_innerBlock->renderBox(); |
| 264 | RenderBox* innerTextRenderer = innerTextElement()->renderBox(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 265 | |
| 266 | IntPoint localPoint = result.localPoint(); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 267 | localPoint.move(-innerBlockRenderer->x(), -innerBlockRenderer->y()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 268 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 269 | int textLeft = tx + x() + innerBlockRenderer->x() + innerTextRenderer->x(); |
| 270 | if (m_resultsButton && m_resultsButton->renderer() && xPos < textLeft) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 271 | innerNode = m_resultsButton.get(); |
| 272 | |
| 273 | if (!innerNode) { |
| 274 | int textRight = textLeft + innerTextRenderer->width(); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 275 | if (m_cancelButton && m_cancelButton->renderer() && xPos > textRight) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 276 | innerNode = m_cancelButton.get(); |
| 277 | } |
| 278 | |
| 279 | if (innerNode) { |
| 280 | result.setInnerNode(innerNode); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 281 | localPoint.move(-innerNode->renderBox()->x(), -innerNode->renderBox()->y()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | result.setLocalPoint(localPoint); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | void RenderTextControlSingleLine::forwardEvent(Event* event) |
| 289 | { |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 290 | RenderBox* innerTextRenderer = innerTextElement()->renderBox(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 291 | |
| 292 | if (event->type() == eventNames().blurEvent) { |
| 293 | if (innerTextRenderer) { |
| 294 | if (RenderLayer* innerLayer = innerTextRenderer->layer()) |
| 295 | innerLayer->scrollToOffset(style()->direction() == RTL ? innerLayer->scrollWidth() : 0, 0); |
| 296 | } |
| 297 | |
| 298 | capsLockStateMayHaveChanged(); |
| 299 | } else if (event->type() == eventNames().focusEvent) |
| 300 | capsLockStateMayHaveChanged(); |
| 301 | |
| 302 | if (!event->isMouseEvent()) { |
| 303 | RenderTextControl::forwardEvent(event); |
| 304 | return; |
| 305 | } |
| 306 | |
simon.fraser@apple.com | 032683d | 2009-03-22 17:35:38 +0000 | [diff] [blame] | 307 | FloatPoint localPoint = innerTextRenderer->absoluteToLocal(static_cast<MouseEvent*>(event)->absoluteLocation(), false, true); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 308 | if (m_resultsButton && localPoint.x() < innerTextRenderer->borderBoxRect().x()) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 309 | m_resultsButton->defaultEventHandler(event); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 310 | else if (m_cancelButton && localPoint.x() > innerTextRenderer->borderBoxRect().right()) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 311 | m_cancelButton->defaultEventHandler(event); |
| 312 | else |
| 313 | RenderTextControl::forwardEvent(event); |
| 314 | } |
| 315 | |
weinig@apple.com | 1a57c82 | 2009-02-04 22:27:50 +0000 | [diff] [blame] | 316 | void RenderTextControlSingleLine::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 317 | { |
| 318 | RenderTextControl::styleDidChange(diff, oldStyle); |
| 319 | |
| 320 | if (RenderObject* innerBlockRenderer = m_innerBlock ? m_innerBlock->renderer() : 0) { |
| 321 | // We may have set the width and the height in the old style in layout(). |
| 322 | // Reset them now to avoid getting a spurious layout hint. |
| 323 | innerBlockRenderer->style()->setHeight(Length()); |
| 324 | innerBlockRenderer->style()->setWidth(Length()); |
| 325 | innerBlockRenderer->setStyle(createInnerBlockStyle(style())); |
| 326 | } |
| 327 | |
| 328 | if (RenderObject* resultsRenderer = m_resultsButton ? m_resultsButton->renderer() : 0) |
| 329 | resultsRenderer->setStyle(createResultsButtonStyle(style())); |
| 330 | |
| 331 | if (RenderObject* cancelRenderer = m_cancelButton ? m_cancelButton->renderer() : 0) |
| 332 | cancelRenderer->setStyle(createCancelButtonStyle(style())); |
eric@webkit.org | d2bb5a0 | 2009-03-17 23:44:11 +0000 | [diff] [blame] | 333 | |
| 334 | setHasOverflowClip(false); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void RenderTextControlSingleLine::capsLockStateMayHaveChanged() |
| 338 | { |
| 339 | if (!node() || !document()) |
| 340 | return; |
| 341 | |
| 342 | // Only draw the caps lock indicator if these things are true: |
| 343 | // 1) The field is a password field |
| 344 | // 2) The frame is active |
| 345 | // 3) The element is focused |
| 346 | // 4) The caps lock is on |
| 347 | bool shouldDrawCapsLockIndicator = false; |
| 348 | |
| 349 | if (Frame* frame = document()->frame()) |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 350 | shouldDrawCapsLockIndicator = inputElement()->isPasswordField() |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 351 | && frame->selection()->isFocusedAndActive() |
| 352 | && document()->focusedNode() == node() |
| 353 | && PlatformKeyboardEvent::currentCapsLockState(); |
| 354 | |
| 355 | if (shouldDrawCapsLockIndicator != m_shouldDrawCapsLockIndicator) { |
| 356 | m_shouldDrawCapsLockIndicator = shouldDrawCapsLockIndicator; |
| 357 | repaint(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | int RenderTextControlSingleLine::textBlockWidth() const |
| 362 | { |
| 363 | int width = RenderTextControl::textBlockWidth(); |
| 364 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 365 | if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 366 | resultsRenderer->calcWidth(); |
adele@apple.com | f32f267 | 2009-01-15 02:05:25 +0000 | [diff] [blame] | 367 | width -= resultsRenderer->width() + resultsRenderer->marginLeft() + resultsRenderer->marginRight(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 368 | } |
| 369 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 370 | if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 371 | cancelRenderer->calcWidth(); |
adele@apple.com | f32f267 | 2009-01-15 02:05:25 +0000 | [diff] [blame] | 372 | width -= cancelRenderer->width() + cancelRenderer->marginLeft() + cancelRenderer->marginRight(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return width; |
| 376 | } |
ojan@chromium.org | 68b773d | 2010-02-13 03:20:20 +0000 | [diff] [blame] | 377 | |
| 378 | float RenderTextControlSingleLine::getAvgCharWidth(AtomicString family) |
| 379 | { |
| 380 | // Since Lucida Grande is the default font, we want this to match the width |
| 381 | // of MS Shell Dlg, the default font for textareas in Firefox, Safari Win and |
| 382 | // IE for some encodings (in IE, the default font is encoding specific). |
| 383 | // 901 is the avgCharWidth value in the OS/2 table for MS Shell Dlg. |
| 384 | if (family == AtomicString("Lucida Grande")) |
| 385 | return scaleEmToUnits(901); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 386 | |
ojan@chromium.org | 68b773d | 2010-02-13 03:20:20 +0000 | [diff] [blame] | 387 | return RenderTextControl::getAvgCharWidth(family); |
| 388 | } |
| 389 | |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 390 | int RenderTextControlSingleLine::preferredContentWidth(float charWidth) const |
| 391 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 392 | int factor = inputElement()->size(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 393 | if (factor <= 0) |
| 394 | factor = 20; |
| 395 | |
| 396 | int result = static_cast<int>(ceilf(charWidth * factor)); |
| 397 | |
ojan@chromium.org | 68b773d | 2010-02-13 03:20:20 +0000 | [diff] [blame] | 398 | float maxCharWidth = 0.f; |
| 399 | AtomicString family = style()->font().family().family(); |
| 400 | // Since Lucida Grande is the default font, we want this to match the width |
| 401 | // of MS Shell Dlg, the default font for textareas in Firefox, Safari Win and |
| 402 | // IE for some encodings (in IE, the default font is encoding specific). |
| 403 | // 4027 is the (xMax - xMin) value in the "head" font table for MS Shell Dlg. |
| 404 | if (family == AtomicString("Lucida Grande")) |
| 405 | maxCharWidth = scaleEmToUnits(4027); |
| 406 | else if (hasValidAvgCharWidth(family)) |
| 407 | maxCharWidth = roundf(style()->font().primaryFont()->maxCharWidth()); |
| 408 | |
ojan@chromium.org | 149606a | 2009-04-29 20:31:53 +0000 | [diff] [blame] | 409 | // For text inputs, IE adds some extra width. |
ojan@chromium.org | 68b773d | 2010-02-13 03:20:20 +0000 | [diff] [blame] | 410 | if (maxCharWidth > 0.f) |
| 411 | result += maxCharWidth - charWidth; |
ojan@chromium.org | 149606a | 2009-04-29 20:31:53 +0000 | [diff] [blame] | 412 | |
hyatt@apple.com | 55fe6bd | 2009-01-23 07:14:49 +0000 | [diff] [blame] | 413 | if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 414 | result += resultsRenderer->borderLeft() + resultsRenderer->borderRight() + |
| 415 | resultsRenderer->paddingLeft() + resultsRenderer->paddingRight(); |
| 416 | |
hyatt@apple.com | 55fe6bd | 2009-01-23 07:14:49 +0000 | [diff] [blame] | 417 | if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 418 | result += cancelRenderer->borderLeft() + cancelRenderer->borderRight() + |
| 419 | cancelRenderer->paddingLeft() + cancelRenderer->paddingRight(); |
| 420 | |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | void RenderTextControlSingleLine::adjustControlHeightBasedOnLineHeight(int lineHeight) |
| 425 | { |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 426 | if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) { |
hyatt@apple.com | 801ed3f | 2009-01-30 18:16:03 +0000 | [diff] [blame] | 427 | toRenderBlock(resultsRenderer)->calcHeight(); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 428 | setHeight(max(height(), |
| 429 | resultsRenderer->borderTop() + resultsRenderer->borderBottom() + |
| 430 | resultsRenderer->paddingTop() + resultsRenderer->paddingBottom() + |
| 431 | resultsRenderer->marginTop() + resultsRenderer->marginBottom())); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 432 | lineHeight = max(lineHeight, resultsRenderer->height()); |
| 433 | } |
| 434 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 435 | if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) { |
hyatt@apple.com | 801ed3f | 2009-01-30 18:16:03 +0000 | [diff] [blame] | 436 | toRenderBlock(cancelRenderer)->calcHeight(); |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 437 | setHeight(max(height(), |
| 438 | cancelRenderer->borderTop() + cancelRenderer->borderBottom() + |
| 439 | cancelRenderer->paddingTop() + cancelRenderer->paddingBottom() + |
| 440 | cancelRenderer->marginTop() + cancelRenderer->marginBottom())); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 441 | lineHeight = max(lineHeight, cancelRenderer->height()); |
| 442 | } |
| 443 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 444 | setHeight(height() + lineHeight); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void RenderTextControlSingleLine::createSubtreeIfNeeded() |
| 448 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 449 | if (!inputElement()->isSearchField()) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 450 | RenderTextControl::createSubtreeIfNeeded(m_innerBlock.get()); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | if (!m_innerBlock) { |
| 455 | // Create the inner block element |
| 456 | m_innerBlock = new TextControlInnerElement(document(), node()); |
| 457 | m_innerBlock->attachInnerElement(node(), createInnerBlockStyle(style()), renderArena()); |
| 458 | } |
| 459 | |
| 460 | if (!m_resultsButton) { |
| 461 | // Create the search results button element |
| 462 | m_resultsButton = new SearchFieldResultsButtonElement(document()); |
| 463 | m_resultsButton->attachInnerElement(m_innerBlock.get(), createResultsButtonStyle(m_innerBlock->renderer()->style()), renderArena()); |
| 464 | } |
| 465 | |
| 466 | // Create innerText element before adding the cancel button |
| 467 | RenderTextControl::createSubtreeIfNeeded(m_innerBlock.get()); |
| 468 | |
| 469 | if (!m_cancelButton) { |
| 470 | // Create the cancel button element |
| 471 | m_cancelButton = new SearchFieldCancelButtonElement(document()); |
| 472 | m_cancelButton->attachInnerElement(m_innerBlock.get(), createCancelButtonStyle(m_innerBlock->renderer()->style()), renderArena()); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void RenderTextControlSingleLine::updateFromElement() |
| 477 | { |
| 478 | createSubtreeIfNeeded(); |
| 479 | RenderTextControl::updateFromElement(); |
| 480 | |
simon.fraser@apple.com | f71e81d | 2009-03-11 19:00:13 +0000 | [diff] [blame] | 481 | if (m_cancelButton) |
| 482 | updateCancelButtonVisibility(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 483 | |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 484 | if (m_placeholderVisible) { |
| 485 | ExceptionCode ec = 0; |
eric@webkit.org | e6cd79c | 2009-09-26 18:56:04 +0000 | [diff] [blame] | 486 | innerTextElement()->setInnerText(static_cast<Element*>(node())->getAttribute(placeholderAttr), ec); |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 487 | ASSERT(!ec); |
eric@webkit.org | b6e6211 | 2009-12-16 18:43:27 +0000 | [diff] [blame] | 488 | } else { |
| 489 | if (!inputElement()->suggestedValue().isNull()) |
| 490 | setInnerTextValue(inputElement()->suggestedValue()); |
| 491 | else |
| 492 | setInnerTextValue(inputElement()->value()); |
| 493 | } |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 494 | |
| 495 | if (m_searchPopupIsVisible) |
| 496 | m_searchPopup->updateFromElement(); |
| 497 | } |
| 498 | |
| 499 | void RenderTextControlSingleLine::cacheSelection(int start, int end) |
| 500 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 501 | inputElement()->cacheSelection(start, end); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerTextStyle(const RenderStyle* startStyle) const |
| 505 | { |
| 506 | RefPtr<RenderStyle> textBlockStyle; |
eric@webkit.org | b05364f | 2009-08-24 08:28:51 +0000 | [diff] [blame] | 507 | if (m_placeholderVisible) { |
mitz@apple.com | 0b86b49 | 2009-05-28 05:25:53 +0000 | [diff] [blame] | 508 | if (RenderStyle* pseudoStyle = getCachedPseudoStyle(INPUT_PLACEHOLDER)) |
| 509 | textBlockStyle = RenderStyle::clone(pseudoStyle); |
| 510 | } |
| 511 | if (!textBlockStyle) { |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 512 | textBlockStyle = RenderStyle::create(); |
| 513 | textBlockStyle->inheritFrom(startStyle); |
| 514 | } |
| 515 | |
| 516 | adjustInnerTextStyle(startStyle, textBlockStyle.get()); |
| 517 | |
| 518 | textBlockStyle->setWhiteSpace(PRE); |
| 519 | textBlockStyle->setWordWrap(NormalWordWrap); |
| 520 | textBlockStyle->setOverflowX(OHIDDEN); |
| 521 | textBlockStyle->setOverflowY(OHIDDEN); |
| 522 | |
| 523 | // Do not allow line-height to be smaller than our default. |
| 524 | if (textBlockStyle->font().lineSpacing() > lineHeight(true, true)) |
| 525 | textBlockStyle->setLineHeight(Length(-100.0f, Percent)); |
| 526 | |
| 527 | textBlockStyle->setDisplay(m_innerBlock ? INLINE_BLOCK : BLOCK); |
| 528 | |
| 529 | // We're adding one extra pixel of padding to match WinIE. |
| 530 | textBlockStyle->setPaddingLeft(Length(1, Fixed)); |
| 531 | textBlockStyle->setPaddingRight(Length(1, Fixed)); |
| 532 | |
| 533 | // When the placeholder is going to be displayed, temporarily override the text security to be "none". |
| 534 | // After this, updateFromElement will immediately update the text displayed. |
| 535 | // When the placeholder is no longer visible, updatePlaceholderVisiblity will reset the style, |
| 536 | // and the text security mode will be set back to the computed value correctly. |
eric@webkit.org | b05364f | 2009-08-24 08:28:51 +0000 | [diff] [blame] | 537 | if (m_placeholderVisible) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 538 | textBlockStyle->setTextSecurity(TSNONE); |
| 539 | |
| 540 | return textBlockStyle.release(); |
| 541 | } |
| 542 | |
| 543 | PassRefPtr<RenderStyle> RenderTextControlSingleLine::createInnerBlockStyle(const RenderStyle* startStyle) const |
| 544 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 545 | ASSERT(node()->isHTMLElement()); |
| 546 | |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 547 | RefPtr<RenderStyle> innerBlockStyle = RenderStyle::create(); |
| 548 | innerBlockStyle->inheritFrom(startStyle); |
| 549 | |
| 550 | innerBlockStyle->setDisplay(BLOCK); |
| 551 | innerBlockStyle->setDirection(LTR); |
| 552 | |
| 553 | // 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. |
| 554 | innerBlockStyle->setUserModify(READ_ONLY); |
| 555 | |
| 556 | return innerBlockStyle.release(); |
| 557 | } |
| 558 | |
| 559 | PassRefPtr<RenderStyle> RenderTextControlSingleLine::createResultsButtonStyle(const RenderStyle* startStyle) const |
| 560 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 561 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 562 | HTMLInputElement* input = static_cast<HTMLInputElement*>(node()); |
| 563 | |
| 564 | RefPtr<RenderStyle> resultsBlockStyle; |
| 565 | if (input->maxResults() < 0) |
weinig@apple.com | edad01d | 2009-02-04 21:02:02 +0000 | [diff] [blame] | 566 | resultsBlockStyle = getCachedPseudoStyle(SEARCH_DECORATION); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 567 | else if (!input->maxResults()) |
weinig@apple.com | edad01d | 2009-02-04 21:02:02 +0000 | [diff] [blame] | 568 | resultsBlockStyle = getCachedPseudoStyle(SEARCH_RESULTS_DECORATION); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 569 | else |
weinig@apple.com | edad01d | 2009-02-04 21:02:02 +0000 | [diff] [blame] | 570 | resultsBlockStyle = getCachedPseudoStyle(SEARCH_RESULTS_BUTTON); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 571 | |
| 572 | if (!resultsBlockStyle) |
| 573 | resultsBlockStyle = RenderStyle::create(); |
| 574 | |
| 575 | if (startStyle) |
| 576 | resultsBlockStyle->inheritFrom(startStyle); |
| 577 | |
| 578 | return resultsBlockStyle.release(); |
| 579 | } |
| 580 | |
| 581 | PassRefPtr<RenderStyle> RenderTextControlSingleLine::createCancelButtonStyle(const RenderStyle* startStyle) const |
| 582 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 583 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 584 | RefPtr<RenderStyle> cancelBlockStyle; |
| 585 | |
weinig@apple.com | edad01d | 2009-02-04 21:02:02 +0000 | [diff] [blame] | 586 | if (RefPtr<RenderStyle> pseudoStyle = getCachedPseudoStyle(SEARCH_CANCEL_BUTTON)) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 587 | // We may be sharing style with another search field, but we must not share the cancel button style. |
| 588 | cancelBlockStyle = RenderStyle::clone(pseudoStyle.get()); |
| 589 | else |
| 590 | cancelBlockStyle = RenderStyle::create(); |
| 591 | |
| 592 | if (startStyle) |
| 593 | cancelBlockStyle->inheritFrom(startStyle); |
| 594 | |
simon.fraser@apple.com | f71e81d | 2009-03-11 19:00:13 +0000 | [diff] [blame] | 595 | cancelBlockStyle->setVisibility(visibilityForCancelButton()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 596 | return cancelBlockStyle.release(); |
| 597 | } |
| 598 | |
simon.fraser@apple.com | f71e81d | 2009-03-11 19:00:13 +0000 | [diff] [blame] | 599 | void RenderTextControlSingleLine::updateCancelButtonVisibility() const |
| 600 | { |
| 601 | if (!m_cancelButton->renderer()) |
| 602 | return; |
| 603 | |
| 604 | const RenderStyle* curStyle = m_cancelButton->renderer()->style(); |
| 605 | EVisibility buttonVisibility = visibilityForCancelButton(); |
| 606 | if (curStyle->visibility() == buttonVisibility) |
| 607 | return; |
| 608 | |
| 609 | RefPtr<RenderStyle> cancelButtonStyle = RenderStyle::clone(curStyle); |
| 610 | cancelButtonStyle->setVisibility(buttonVisibility); |
| 611 | m_cancelButton->renderer()->setStyle(cancelButtonStyle); |
| 612 | } |
| 613 | |
| 614 | EVisibility RenderTextControlSingleLine::visibilityForCancelButton() const |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 615 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 616 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 617 | HTMLInputElement* input = static_cast<HTMLInputElement*>(node()); |
simon.fraser@apple.com | f71e81d | 2009-03-11 19:00:13 +0000 | [diff] [blame] | 618 | return input->value().isEmpty() ? HIDDEN : VISIBLE; |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | const AtomicString& RenderTextControlSingleLine::autosaveName() const |
| 622 | { |
| 623 | return static_cast<Element*>(node())->getAttribute(autosaveAttr); |
| 624 | } |
| 625 | |
| 626 | void RenderTextControlSingleLine::startSearchEventTimer() |
| 627 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 628 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 629 | unsigned length = text().length(); |
| 630 | |
| 631 | // If there's no text, fire the event right away. |
| 632 | if (!length) { |
| 633 | stopSearchEventTimer(); |
| 634 | static_cast<HTMLInputElement*>(node())->onSearch(); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | // After typing the first key, we wait 0.5 seconds. |
| 639 | // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on. |
| 640 | m_searchEventTimer.startOneShot(max(0.2, 0.6 - 0.1 * length)); |
| 641 | } |
| 642 | |
| 643 | void RenderTextControlSingleLine::searchEventTimerFired(Timer<RenderTextControlSingleLine>*) |
| 644 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 645 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 646 | static_cast<HTMLInputElement*>(node())->onSearch(); |
| 647 | } |
| 648 | |
| 649 | // PopupMenuClient methods |
| 650 | void RenderTextControlSingleLine::valueChanged(unsigned listIndex, bool fireEvents) |
| 651 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 652 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 653 | ASSERT(static_cast<int>(listIndex) < listSize()); |
| 654 | HTMLInputElement* input = static_cast<HTMLInputElement*>(node()); |
| 655 | if (static_cast<int>(listIndex) == (listSize() - 1)) { |
| 656 | if (fireEvents) { |
| 657 | m_recentSearches.clear(); |
| 658 | const AtomicString& name = autosaveName(); |
| 659 | if (!name.isEmpty()) { |
| 660 | if (!m_searchPopup) |
| 661 | m_searchPopup = SearchPopupMenu::create(this); |
| 662 | m_searchPopup->saveRecentSearches(name, m_recentSearches); |
| 663 | } |
| 664 | } |
| 665 | } else { |
| 666 | input->setValue(itemText(listIndex)); |
| 667 | if (fireEvents) |
| 668 | input->onSearch(); |
| 669 | input->select(); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | String RenderTextControlSingleLine::itemText(unsigned listIndex) const |
| 674 | { |
| 675 | int size = listSize(); |
| 676 | if (size == 1) { |
| 677 | ASSERT(!listIndex); |
| 678 | return searchMenuNoRecentSearchesText(); |
| 679 | } |
| 680 | if (!listIndex) |
| 681 | return searchMenuRecentSearchesText(); |
| 682 | if (itemIsSeparator(listIndex)) |
| 683 | return String(); |
| 684 | if (static_cast<int>(listIndex) == (size - 1)) |
| 685 | return searchMenuClearRecentSearchesText(); |
| 686 | return m_recentSearches[listIndex - 1]; |
| 687 | } |
| 688 | |
| 689 | bool RenderTextControlSingleLine::itemIsEnabled(unsigned listIndex) const |
| 690 | { |
| 691 | if (!listIndex || itemIsSeparator(listIndex)) |
| 692 | return false; |
| 693 | return true; |
| 694 | } |
| 695 | |
darin@apple.com | 98a7ac6 | 2009-01-05 17:26:53 +0000 | [diff] [blame] | 696 | PopupMenuStyle RenderTextControlSingleLine::itemStyle(unsigned) const |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 697 | { |
| 698 | return menuStyle(); |
| 699 | } |
| 700 | |
| 701 | PopupMenuStyle RenderTextControlSingleLine::menuStyle() const |
| 702 | { |
adachan@apple.com | 2ad6513 | 2009-03-23 23:46:27 +0000 | [diff] [blame] | 703 | return PopupMenuStyle(style()->color(), style()->backgroundColor(), style()->font(), style()->visibility() == VISIBLE, style()->textIndent(), style()->direction()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | int RenderTextControlSingleLine::clientInsetLeft() const |
| 707 | { |
| 708 | // Inset the menu by the radius of the cap on the left so that |
| 709 | // it only runs along the straight part of the bezel. |
| 710 | return height() / 2; |
| 711 | } |
| 712 | |
| 713 | int RenderTextControlSingleLine::clientInsetRight() const |
| 714 | { |
| 715 | // Inset the menu by the radius of the cap on the right so that |
| 716 | // it only runs along the straight part of the bezel (unless it needs |
| 717 | // to be wider). |
| 718 | return height() / 2; |
| 719 | } |
| 720 | |
| 721 | int RenderTextControlSingleLine::clientPaddingLeft() const |
| 722 | { |
adele@apple.com | 524122c | 2009-01-10 01:12:04 +0000 | [diff] [blame] | 723 | int padding = paddingLeft(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 724 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 725 | if (RenderBox* resultsRenderer = m_resultsButton ? m_resultsButton->renderBox() : 0) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 726 | padding += resultsRenderer->width(); |
| 727 | |
| 728 | return padding; |
| 729 | } |
| 730 | |
| 731 | int RenderTextControlSingleLine::clientPaddingRight() const |
| 732 | { |
adele@apple.com | 524122c | 2009-01-10 01:12:04 +0000 | [diff] [blame] | 733 | int padding = paddingRight(); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 734 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 735 | if (RenderBox* cancelRenderer = m_cancelButton ? m_cancelButton->renderBox() : 0) |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 736 | padding += cancelRenderer->width(); |
| 737 | |
| 738 | return padding; |
| 739 | } |
| 740 | |
| 741 | int RenderTextControlSingleLine::listSize() const |
| 742 | { |
| 743 | // If there are no recent searches, then our menu will have 1 "No recent searches" item. |
| 744 | if (!m_recentSearches.size()) |
| 745 | return 1; |
| 746 | // Otherwise, leave room in the menu for a header, a separator, and the "Clear recent searches" item. |
| 747 | return m_recentSearches.size() + 3; |
| 748 | } |
| 749 | |
| 750 | int RenderTextControlSingleLine::selectedIndex() const |
| 751 | { |
| 752 | return -1; |
| 753 | } |
| 754 | |
pkasting@chromium.org | 43ee810 | 2010-01-22 22:05:40 +0000 | [diff] [blame] | 755 | void RenderTextControlSingleLine::popupDidHide() |
andersca@apple.com | f3f3006 | 2009-09-14 22:14:13 +0000 | [diff] [blame] | 756 | { |
| 757 | m_searchPopupIsVisible = false; |
| 758 | } |
| 759 | |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 760 | bool RenderTextControlSingleLine::itemIsSeparator(unsigned listIndex) const |
| 761 | { |
ddkilzer@apple.com | dfdc3fd | 2009-07-08 13:55:55 +0000 | [diff] [blame] | 762 | // The separator will be the second to last item in our list. |
| 763 | return static_cast<int>(listIndex) == (listSize() - 2); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | bool RenderTextControlSingleLine::itemIsLabel(unsigned listIndex) const |
| 767 | { |
| 768 | return listIndex == 0; |
| 769 | } |
| 770 | |
| 771 | bool RenderTextControlSingleLine::itemIsSelected(unsigned) const |
| 772 | { |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | void RenderTextControlSingleLine::setTextFromItem(unsigned listIndex) |
| 777 | { |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 778 | ASSERT(node()->isHTMLElement()); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 779 | static_cast<HTMLInputElement*>(node())->setValue(itemText(listIndex)); |
| 780 | } |
| 781 | |
| 782 | FontSelector* RenderTextControlSingleLine::fontSelector() const |
| 783 | { |
| 784 | return document()->styleSelector()->fontSelector(); |
| 785 | } |
| 786 | |
| 787 | HostWindow* RenderTextControlSingleLine::hostWindow() const |
| 788 | { |
| 789 | return document()->view()->hostWindow(); |
| 790 | } |
| 791 | |
eric@webkit.org | d2bb5a0 | 2009-03-17 23:44:11 +0000 | [diff] [blame] | 792 | void RenderTextControlSingleLine::autoscroll() |
| 793 | { |
| 794 | RenderLayer* layer = innerTextElement()->renderBox()->layer(); |
| 795 | if (layer) |
| 796 | layer->autoscroll(); |
| 797 | } |
| 798 | |
| 799 | int RenderTextControlSingleLine::scrollWidth() const |
| 800 | { |
| 801 | if (innerTextElement()) |
| 802 | return innerTextElement()->scrollWidth(); |
| 803 | return RenderBlock::scrollWidth(); |
| 804 | } |
| 805 | |
| 806 | int RenderTextControlSingleLine::scrollHeight() const |
| 807 | { |
| 808 | if (innerTextElement()) |
| 809 | return innerTextElement()->scrollHeight(); |
| 810 | return RenderBlock::scrollHeight(); |
| 811 | } |
| 812 | |
| 813 | int RenderTextControlSingleLine::scrollLeft() const |
| 814 | { |
| 815 | if (innerTextElement()) |
| 816 | return innerTextElement()->scrollLeft(); |
| 817 | return RenderBlock::scrollLeft(); |
| 818 | } |
| 819 | |
| 820 | int RenderTextControlSingleLine::scrollTop() const |
| 821 | { |
| 822 | if (innerTextElement()) |
| 823 | return innerTextElement()->scrollTop(); |
| 824 | return RenderBlock::scrollTop(); |
| 825 | } |
| 826 | |
| 827 | void RenderTextControlSingleLine::setScrollLeft(int newLeft) |
| 828 | { |
| 829 | if (innerTextElement()) |
| 830 | innerTextElement()->setScrollLeft(newLeft); |
| 831 | } |
| 832 | |
| 833 | void RenderTextControlSingleLine::setScrollTop(int newTop) |
| 834 | { |
| 835 | if (innerTextElement()) |
| 836 | innerTextElement()->setScrollTop(newTop); |
| 837 | } |
| 838 | |
timothy@apple.com | 8af4f2e | 2009-10-01 13:18:03 +0000 | [diff] [blame] | 839 | bool RenderTextControlSingleLine::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode) |
eric@webkit.org | d2bb5a0 | 2009-03-17 23:44:11 +0000 | [diff] [blame] | 840 | { |
| 841 | RenderLayer* layer = innerTextElement()->renderBox()->layer(); |
| 842 | if (layer && layer->scroll(direction, granularity, multiplier)) |
| 843 | return true; |
timothy@apple.com | 8af4f2e | 2009-10-01 13:18:03 +0000 | [diff] [blame] | 844 | return RenderBlock::scroll(direction, granularity, multiplier, stopNode); |
eric@webkit.org | d2bb5a0 | 2009-03-17 23:44:11 +0000 | [diff] [blame] | 845 | } |
| 846 | |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 847 | PassRefPtr<Scrollbar> RenderTextControlSingleLine::createScrollbar(ScrollbarClient* client, ScrollbarOrientation orientation, ScrollbarControlSize controlSize) |
| 848 | { |
| 849 | RefPtr<Scrollbar> widget; |
weinig@apple.com | edad01d | 2009-02-04 21:02:02 +0000 | [diff] [blame] | 850 | bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR); |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 851 | if (hasCustomScrollbarStyle) |
| 852 | widget = RenderScrollbar::createCustomScrollbar(client, orientation, this); |
| 853 | else |
| 854 | widget = Scrollbar::createNativeScrollbar(client, orientation, controlSize); |
| 855 | return widget.release(); |
| 856 | } |
| 857 | |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 858 | InputElement* RenderTextControlSingleLine::inputElement() const |
| 859 | { |
zimmermann@webkit.org | 6d4da35 | 2009-01-22 22:53:47 +0000 | [diff] [blame] | 860 | return toInputElement(static_cast<Element*>(node())); |
zimmermann@webkit.org | dceb5db | 2009-01-20 21:02:58 +0000 | [diff] [blame] | 861 | } |
| 862 | |
zimmermann@webkit.org | 9402bba | 2008-12-28 13:54:17 +0000 | [diff] [blame] | 863 | } |