1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/**
* Copyright (C) 2006, 2007, 2010 Apple Inc. All rights reserved.
* (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderSearchField.h"
#include "CSSFontSelector.h"
#include "CSSValueKeywords.h"
#include "Chrome.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "FrameView.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HitTestResult.h"
#include "LocalizedStrings.h"
#include "Page.h"
#include "PlatformKeyboardEvent.h"
#include "RenderLayer.h"
#include "RenderScrollbar.h"
#include "RenderTheme.h"
#include "SearchPopupMenu.h"
#include "Settings.h"
#include "SimpleFontData.h"
#include "StyleResolver.h"
#include "TextControlInnerElements.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
// ----------------------------
RenderSearchField::RenderSearchField(Element* element)
: RenderTextControlSingleLine(element)
, m_searchPopupIsVisible(false)
, m_searchPopup(0)
{
ASSERT(element->isHTMLElement());
ASSERT(element->toInputElement());
ASSERT(element->toInputElement()->isSearchField());
}
RenderSearchField::~RenderSearchField()
{
if (m_searchPopup) {
m_searchPopup->popupMenu()->disconnectClient();
m_searchPopup = 0;
}
}
inline HTMLElement* RenderSearchField::resultsButtonElement() const
{
return inputElement()->resultsButtonElement();
}
inline HTMLElement* RenderSearchField::cancelButtonElement() const
{
return inputElement()->cancelButtonElement();
}
void RenderSearchField::addSearchResult()
{
HTMLInputElement* input = inputElement();
if (input->maxResults() <= 0)
return;
String value = input->value();
if (value.isEmpty())
return;
Settings* settings = document()->settings();
if (!settings || settings->privateBrowsingEnabled())
return;
int size = static_cast<int>(m_recentSearches.size());
for (int i = size - 1; i >= 0; --i) {
if (m_recentSearches[i] == value)
m_recentSearches.remove(i);
}
m_recentSearches.insert(0, value);
while (static_cast<int>(m_recentSearches.size()) > input->maxResults())
m_recentSearches.removeLast();
const AtomicString& name = autosaveName();
if (!m_searchPopup)
m_searchPopup = document()->page()->chrome().createSearchPopupMenu(this);
m_searchPopup->saveRecentSearches(name, m_recentSearches);
}
void RenderSearchField::showPopup()
{
if (m_searchPopupIsVisible)
return;
if (!m_searchPopup)
m_searchPopup = document()->page()->chrome().createSearchPopupMenu(this);
if (!m_searchPopup->enabled())
return;
m_searchPopupIsVisible = true;
const AtomicString& name = autosaveName();
m_searchPopup->loadRecentSearches(name, m_recentSearches);
// Trim the recent searches list if the maximum size has changed since we last saved.
HTMLInputElement* input = inputElement();
if (static_cast<int>(m_recentSearches.size()) > input->maxResults()) {
do {
m_recentSearches.removeLast();
} while (static_cast<int>(m_recentSearches.size()) > input->maxResults());
m_searchPopup->saveRecentSearches(name, m_recentSearches);
}
m_searchPopup->popupMenu()->show(pixelSnappedIntRect(absoluteBoundingBoxRect()), document()->view(), -1);
}
void RenderSearchField::hidePopup()
{
if (m_searchPopup)
m_searchPopup->popupMenu()->hide();
}
LayoutUnit RenderSearchField::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const
{
HTMLElement* resultsButton = resultsButtonElement();
if (RenderBox* resultsRenderer = resultsButton ? resultsButton->renderBox() : 0) {
resultsRenderer->updateLogicalHeight();
nonContentHeight = max(nonContentHeight, resultsRenderer->borderAndPaddingLogicalHeight() + resultsRenderer->marginLogicalHeight());
lineHeight = max(lineHeight, resultsRenderer->logicalHeight());
}
HTMLElement* cancelButton = cancelButtonElement();
if (RenderBox* cancelRenderer = cancelButton ? cancelButton->renderBox() : 0) {
cancelRenderer->updateLogicalHeight();
nonContentHeight = max(nonContentHeight, cancelRenderer->borderAndPaddingLogicalHeight() + cancelRenderer->marginLogicalHeight());
lineHeight = max(lineHeight, cancelRenderer->logicalHeight());
}
return lineHeight + nonContentHeight;
}
void RenderSearchField::updateFromElement()
{
RenderTextControlSingleLine::updateFromElement();
if (cancelButtonElement())
updateCancelButtonVisibility();
if (m_searchPopupIsVisible)
m_searchPopup->popupMenu()->updateFromElement();
}
void RenderSearchField::updateCancelButtonVisibility() const
{
RenderObject* cancelButtonRenderer = cancelButtonElement()->renderer();
if (!cancelButtonRenderer)
return;
const RenderStyle* curStyle = cancelButtonRenderer->style();
EVisibility buttonVisibility = visibilityForCancelButton();
if (curStyle->visibility() == buttonVisibility)
return;
RefPtr<RenderStyle> cancelButtonStyle = RenderStyle::clone(curStyle);
cancelButtonStyle->setVisibility(buttonVisibility);
cancelButtonRenderer->setStyle(cancelButtonStyle);
}
EVisibility RenderSearchField::visibilityForCancelButton() const
{
return (style()->visibility() == HIDDEN || inputElement()->value().isEmpty()) ? HIDDEN : VISIBLE;
}
const AtomicString& RenderSearchField::autosaveName() const
{
return toElement(node())->getAttribute(autosaveAttr);
}
// PopupMenuClient methods
void RenderSearchField::valueChanged(unsigned listIndex, bool fireEvents)
{
ASSERT(static_cast<int>(listIndex) < listSize());
HTMLInputElement* input = inputElement();
if (static_cast<int>(listIndex) == (listSize() - 1)) {
if (fireEvents) {
m_recentSearches.clear();
const AtomicString& name = autosaveName();
if (!name.isEmpty()) {
if (!m_searchPopup)
m_searchPopup = document()->page()->chrome().createSearchPopupMenu(this);
m_searchPopup->saveRecentSearches(name, m_recentSearches);
}
}
} else {
input->setValue(itemText(listIndex));
if (fireEvents)
input->onSearch();
input->select();
}
}
String RenderSearchField::itemText(unsigned listIndex) const
{
int size = listSize();
if (size == 1) {
ASSERT(!listIndex);
return searchMenuNoRecentSearchesText();
}
if (!listIndex)
return searchMenuRecentSearchesText();
if (itemIsSeparator(listIndex))
return String();
if (static_cast<int>(listIndex) == (size - 1))
return searchMenuClearRecentSearchesText();
return m_recentSearches[listIndex - 1];
}
String RenderSearchField::itemLabel(unsigned) const
{
return String();
}
String RenderSearchField::itemIcon(unsigned) const
{
return String();
}
bool RenderSearchField::itemIsEnabled(unsigned listIndex) const
{
if (!listIndex || itemIsSeparator(listIndex))
return false;
return true;
}
PopupMenuStyle RenderSearchField::itemStyle(unsigned) const
{
return menuStyle();
}
PopupMenuStyle RenderSearchField::menuStyle() const
{
return PopupMenuStyle(style()->visitedDependentColor(CSSPropertyColor), style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->font(), style()->visibility() == VISIBLE,
style()->display() == NONE, style()->textIndent(), style()->direction(), isOverride(style()->unicodeBidi()), PopupMenuStyle::CustomBackgroundColor);
}
int RenderSearchField::clientInsetLeft() const
{
// Inset the menu by the radius of the cap on the left so that
// it only runs along the straight part of the bezel.
return height() / 2;
}
int RenderSearchField::clientInsetRight() const
{
// Inset the menu by the radius of the cap on the right so that
// it only runs along the straight part of the bezel (unless it needs
// to be wider).
return height() / 2;
}
LayoutUnit RenderSearchField::clientPaddingLeft() const
{
LayoutUnit padding = paddingLeft();
if (RenderBox* box = innerBlockElement() ? innerBlockElement()->renderBox() : 0)
padding += box->x();
return padding;
}
LayoutUnit RenderSearchField::clientPaddingRight() const
{
LayoutUnit padding = paddingRight();
if (RenderBox* containerBox = containerElement() ? containerElement()->renderBox() : 0) {
if (RenderBox* innerBlockBox = innerBlockElement() ? innerBlockElement()->renderBox() : 0)
padding += containerBox->width() - (innerBlockBox->x() + innerBlockBox->width());
}
return padding;
}
int RenderSearchField::listSize() const
{
// If there are no recent searches, then our menu will have 1 "No recent searches" item.
if (!m_recentSearches.size())
return 1;
// Otherwise, leave room in the menu for a header, a separator, and the "Clear recent searches" item.
return m_recentSearches.size() + 3;
}
int RenderSearchField::selectedIndex() const
{
return -1;
}
void RenderSearchField::popupDidHide()
{
m_searchPopupIsVisible = false;
}
bool RenderSearchField::itemIsSeparator(unsigned listIndex) const
{
// The separator will be the second to last item in our list.
return static_cast<int>(listIndex) == (listSize() - 2);
}
bool RenderSearchField::itemIsLabel(unsigned listIndex) const
{
return !listIndex;
}
bool RenderSearchField::itemIsSelected(unsigned) const
{
return false;
}
void RenderSearchField::setTextFromItem(unsigned listIndex)
{
inputElement()->setValue(itemText(listIndex));
}
FontSelector* RenderSearchField::fontSelector() const
{
return document()->ensureStyleResolver()->fontSelector();
}
HostWindow* RenderSearchField::hostWindow() const
{
return document()->view()->hostWindow();
}
PassRefPtr<Scrollbar> RenderSearchField::createScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
{
RefPtr<Scrollbar> widget;
bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR);
if (hasCustomScrollbarStyle)
widget = RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, this->node());
else
widget = Scrollbar::createNativeScrollbar(scrollableArea, orientation, controlSize);
return widget.release();
}
LayoutUnit RenderSearchField::computeLogicalHeightLimit() const
{
return logicalHeight();
}
void RenderSearchField::centerContainerIfNeeded(RenderBox* containerRenderer) const
{
if (!containerRenderer)
return;
if (containerRenderer->logicalHeight() <= contentLogicalHeight())
return;
// A quirk for find-in-page box on Safari Windows.
// http://webkit.org/b/63157
LayoutUnit logicalHeightDiff = containerRenderer->logicalHeight() - contentLogicalHeight();
containerRenderer->setLogicalTop(containerRenderer->logicalTop() - (logicalHeightDiff / 2 + layoutMod(logicalHeightDiff, 2)));
}
}
|