1 /** 2 * Copyright (C) 2006, 2007, 2010 Apple Inc. All rights reserved. 3 * (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) 4 * Copyright (C) 2010 Google Inc. All rights reserved. 5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Library General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Library General Public License for more details. 16 * 17 * You should have received a copy of the GNU Library General Public License 18 * along with this library; see the file COPYING.LIB. If not, write to 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 * Boston, MA 02110-1301, USA. 21 * 22 */ 23 24 #include "config.h" 25 #include "core/rendering/RenderSearchField.h" 26 27 #include "core/html/HTMLInputElement.h" 28 #include "core/html/shadow/ShadowElementNames.h" 29 30 using namespace std; 31 32 namespace WebCore { 33 34 using namespace HTMLNames; 35 36 // ---------------------------- 37 38 RenderSearchField::RenderSearchField(HTMLInputElement* element) 39 : RenderTextControlSingleLine(element) 40 { 41 ASSERT(element->isSearchField()); 42 } 43 44 RenderSearchField::~RenderSearchField() 45 { 46 } 47 48 inline Element* RenderSearchField::searchDecorationElement() const 49 { 50 return inputElement()->uaShadowElementById(ShadowElementNames::searchDecoration()); 51 } 52 53 inline Element* RenderSearchField::cancelButtonElement() const 54 { 55 return inputElement()->uaShadowElementById(ShadowElementNames::clearButton()); 56 } 57 58 LayoutUnit RenderSearchField::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const 59 { 60 Element* searchDecoration = searchDecorationElement(); 61 if (RenderBox* decorationRenderer = searchDecoration ? searchDecoration->renderBox() : 0) { 62 decorationRenderer->updateLogicalHeight(); 63 nonContentHeight = max(nonContentHeight, decorationRenderer->borderAndPaddingLogicalHeight() + decorationRenderer->marginLogicalHeight()); 64 lineHeight = max(lineHeight, decorationRenderer->logicalHeight()); 65 } 66 Element* cancelButton = cancelButtonElement(); 67 if (RenderBox* cancelRenderer = cancelButton ? cancelButton->renderBox() : 0) { 68 cancelRenderer->updateLogicalHeight(); 69 nonContentHeight = max(nonContentHeight, cancelRenderer->borderAndPaddingLogicalHeight() + cancelRenderer->marginLogicalHeight()); 70 lineHeight = max(lineHeight, cancelRenderer->logicalHeight()); 71 } 72 73 return lineHeight + nonContentHeight; 74 } 75 76 void RenderSearchField::updateFromElement() 77 { 78 RenderTextControlSingleLine::updateFromElement(); 79 80 if (cancelButtonElement()) 81 updateCancelButtonVisibility(); 82 } 83 84 void RenderSearchField::updateCancelButtonVisibility() const 85 { 86 RenderObject* cancelButtonRenderer = cancelButtonElement()->renderer(); 87 if (!cancelButtonRenderer) 88 return; 89 90 const RenderStyle* curStyle = cancelButtonRenderer->style(); 91 EVisibility buttonVisibility = visibilityForCancelButton(); 92 if (curStyle->visibility() == buttonVisibility) 93 return; 94 95 RefPtr<RenderStyle> cancelButtonStyle = RenderStyle::clone(curStyle); 96 cancelButtonStyle->setVisibility(buttonVisibility); 97 cancelButtonRenderer->setStyle(cancelButtonStyle); 98 } 99 100 EVisibility RenderSearchField::visibilityForCancelButton() const 101 { 102 return (style()->visibility() == HIDDEN || inputElement()->value().isEmpty()) ? HIDDEN : VISIBLE; 103 } 104 105 LayoutUnit RenderSearchField::computeLogicalHeightLimit() const 106 { 107 return logicalHeight(); 108 } 109 110 void RenderSearchField::centerContainerIfNeeded(RenderBox* containerRenderer) const 111 { 112 if (!containerRenderer) 113 return; 114 115 if (containerRenderer->logicalHeight() <= contentLogicalHeight()) 116 return; 117 118 // A quirk for find-in-page box on Safari Windows. 119 // http://webkit.org/b/63157 120 LayoutUnit logicalHeightDiff = containerRenderer->logicalHeight() - contentLogicalHeight(); 121 containerRenderer->setLogicalTop(containerRenderer->logicalTop() - (logicalHeightDiff / 2 + layoutMod(logicalHeightDiff, 2))); 122 } 123 124 } 125