Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderListBox.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
3  *               2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "core/rendering/RenderListBox.h"
32
33 #include "core/HTMLNames.h"
34 #include "core/accessibility/AXObjectCache.h"
35 #include "core/css/CSSFontSelector.h"
36 #include "core/css/resolver/StyleResolver.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/NodeRenderStyle.h"
39 #include "core/dom/StyleEngine.h"
40 #include "core/editing/FrameSelection.h"
41 #include "core/frame/FrameView.h"
42 #include "core/frame/LocalFrame.h"
43 #include "core/html/HTMLOptGroupElement.h"
44 #include "core/html/HTMLOptionElement.h"
45 #include "core/html/HTMLSelectElement.h"
46 #include "core/page/EventHandler.h"
47 #include "core/page/FocusController.h"
48 #include "core/page/Page.h"
49 #include "core/page/SpatialNavigation.h"
50 #include "core/rendering/HitTestResult.h"
51 #include "core/rendering/PaintInfo.h"
52 #include "core/rendering/RenderScrollbar.h"
53 #include "core/rendering/RenderText.h"
54 #include "core/rendering/RenderTheme.h"
55 #include "core/rendering/RenderView.h"
56 #include "core/rendering/TextRunConstructor.h"
57 #include "platform/fonts/FontCache.h"
58 #include "platform/graphics/GraphicsContext.h"
59 #include "platform/scroll/Scrollbar.h"
60 #include "platform/text/BidiTextRun.h"
61 #include <math.h>
62
63 namespace blink {
64
65 using namespace HTMLNames;
66
67 // The minSize constant was originally defined to render scrollbars correctly.
68 // This might vary for different platforms.
69 const int minSize = 4;
70
71 // Default size when the multiple attribute is present but size attribute is absent.
72 const int defaultSize = 4;
73
74 const int defaultPaddingBottom = 1;
75
76 RenderListBox::RenderListBox(Element* element)
77     : RenderBlockFlow(element)
78 {
79     ASSERT(element);
80     ASSERT(element->isHTMLElement());
81     ASSERT(isHTMLSelectElement(element));
82 }
83
84 RenderListBox::~RenderListBox()
85 {
86 }
87
88 inline HTMLSelectElement* RenderListBox::selectElement() const
89 {
90     return toHTMLSelectElement(node());
91 }
92
93 int RenderListBox::size() const
94 {
95     int specifiedSize = selectElement()->size();
96     if (specifiedSize > 1)
97         return max(minSize, specifiedSize);
98
99     return defaultSize;
100 }
101
102 LayoutUnit RenderListBox::defaultItemHeight() const
103 {
104     return style()->fontMetrics().height() + defaultPaddingBottom;
105 }
106
107 LayoutUnit RenderListBox::itemHeight() const
108 {
109     HTMLSelectElement* select = selectElement();
110     if (!select)
111         return 0;
112     RenderObject* baseItemRenderer = firstChild();
113     if (!baseItemRenderer)
114         return defaultItemHeight();
115     if (baseItemRenderer->node() && isHTMLOptGroupElement(baseItemRenderer->node()))
116         baseItemRenderer = baseItemRenderer->slowFirstChild();
117     if (!baseItemRenderer || !baseItemRenderer->isBox())
118         return defaultItemHeight();
119     return toRenderBox(baseItemRenderer)->height();
120 }
121
122 void RenderListBox::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
123 {
124     LayoutUnit height = itemHeight() * size();
125     // FIXME: The item height should have been added before updateLogicalHeight was called to avoid this hack.
126     updateIntrinsicContentLogicalHeight(height);
127
128     height += borderAndPaddingHeight();
129
130     RenderBox::computeLogicalHeight(height, logicalTop, computedValues);
131 }
132
133 void RenderListBox::stopAutoscroll()
134 {
135     HTMLSelectElement* select = selectElement();
136     if (select->isDisabledFormControl())
137         return;
138     select->handleMouseRelease();
139 }
140
141 } // namespace blink