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/InputTypeNames.h"
28#include "core/dom/shadow/ShadowRoot.h"
29#include "core/html/HTMLInputElement.h"
30#include "core/html/shadow/ShadowElementNames.h"
31
32namespace blink {
33
34using namespace HTMLNames;
35
36// ----------------------------
37
38RenderSearchField::RenderSearchField(HTMLInputElement* element)
39    : RenderTextControlSingleLine(element)
40{
41    ASSERT(element->type() == InputTypeNames::search);
42}
43
44RenderSearchField::~RenderSearchField()
45{
46}
47
48inline Element* RenderSearchField::searchDecorationElement() const
49{
50    return inputElement()->userAgentShadowRoot()->getElementById(ShadowElementNames::searchDecoration());
51}
52
53inline Element* RenderSearchField::cancelButtonElement() const
54{
55    return inputElement()->userAgentShadowRoot()->getElementById(ShadowElementNames::clearButton());
56}
57
58LayoutUnit 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
76LayoutUnit RenderSearchField::computeLogicalHeightLimit() const
77{
78    return logicalHeight();
79}
80
81void RenderSearchField::centerContainerIfNeeded(RenderBox* containerRenderer) const
82{
83    if (!containerRenderer)
84        return;
85
86    if (containerRenderer->logicalHeight() <= contentLogicalHeight())
87        return;
88
89    // A quirk for find-in-page box on Safari Windows.
90    // http://webkit.org/b/63157
91    LayoutUnit logicalHeightDiff = containerRenderer->logicalHeight() - contentLogicalHeight();
92    containerRenderer->setLogicalTop(containerRenderer->logicalTop() - (logicalHeightDiff / 2 + layoutMod(logicalHeightDiff, 2)));
93}
94
95}
96