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