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/ElementTraversal.h"
39 #include "core/dom/NodeRenderStyle.h"
40 #include "core/dom/StyleEngine.h"
41 #include "core/editing/FrameSelection.h"
42 #include "core/frame/FrameView.h"
43 #include "core/frame/LocalFrame.h"
44 #include "core/html/HTMLDivElement.h"
45 #include "core/html/HTMLOptGroupElement.h"
46 #include "core/html/HTMLOptionElement.h"
47 #include "core/html/HTMLSelectElement.h"
48 #include "core/page/EventHandler.h"
49 #include "core/page/FocusController.h"
50 #include "core/page/Page.h"
51 #include "core/page/SpatialNavigation.h"
52 #include "core/rendering/HitTestResult.h"
53 #include "core/rendering/PaintInfo.h"
54 #include "core/rendering/RenderScrollbar.h"
55 #include "core/rendering/RenderLayer.h"
56 #include "core/rendering/RenderText.h"
57 #include "core/rendering/RenderTheme.h"
58 #include "core/rendering/RenderView.h"
59 #include "core/rendering/TextRunConstructor.h"
60 #include "platform/fonts/FontCache.h"
61 #include "platform/graphics/GraphicsContext.h"
62 #include "platform/scroll/Scrollbar.h"
63 #include "platform/text/BidiTextRun.h"
64 #include <math.h>
65
66 namespace blink {
67
68 using namespace HTMLNames;
69
70 // Default size when the multiple attribute is present but size attribute is absent.
71 const int defaultSize = 4;
72
73 const int defaultPaddingBottom = 1;
74
RenderListBox(Element * element)75 RenderListBox::RenderListBox(Element* element)
76 : RenderBlockFlow(element)
77 {
78 ASSERT(element);
79 ASSERT(element->isHTMLElement());
80 ASSERT(isHTMLSelectElement(element));
81 }
82
~RenderListBox()83 RenderListBox::~RenderListBox()
84 {
85 }
86
selectElement() const87 inline HTMLSelectElement* RenderListBox::selectElement() const
88 {
89 return toHTMLSelectElement(node());
90 }
91
size() const92 int RenderListBox::size() const
93 {
94 int specifiedSize = selectElement()->size();
95 if (specifiedSize >= 1)
96 return specifiedSize;
97
98 return defaultSize;
99 }
100
defaultItemHeight() const101 LayoutUnit RenderListBox::defaultItemHeight() const
102 {
103 return style()->fontMetrics().height() + defaultPaddingBottom;
104 }
105
itemHeight() const106 LayoutUnit RenderListBox::itemHeight() const
107 {
108 HTMLSelectElement* select = selectElement();
109 if (!select)
110 return 0;
111 Element* baseItem = ElementTraversal::firstChild(*select);
112 if (!baseItem)
113 return defaultItemHeight();
114 if (isHTMLOptGroupElement(baseItem))
115 baseItem = &toHTMLOptGroupElement(baseItem)->optGroupLabelElement();
116 else if (!isHTMLOptionElement(baseItem))
117 return defaultItemHeight();
118 RenderObject* baseItemRenderer = baseItem->renderer();
119 if (!baseItemRenderer)
120 return defaultItemHeight();
121 if (!baseItemRenderer || !baseItemRenderer->isBox())
122 return defaultItemHeight();
123 return toRenderBox(baseItemRenderer)->height();
124 }
125
computeLogicalHeight(LayoutUnit,LayoutUnit logicalTop,LogicalExtentComputedValues & computedValues) const126 void RenderListBox::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
127 {
128 LayoutUnit height = itemHeight() * size();
129 // FIXME: The item height should have been added before updateLogicalHeight was called to avoid this hack.
130 updateIntrinsicContentLogicalHeight(height);
131
132 height += borderAndPaddingHeight();
133
134 RenderBox::computeLogicalHeight(height, logicalTop, computedValues);
135 }
136
stopAutoscroll()137 void RenderListBox::stopAutoscroll()
138 {
139 HTMLSelectElement* select = selectElement();
140 if (select->isDisabledFormControl())
141 return;
142 select->handleMouseRelease();
143 }
144
computeIntrinsicLogicalWidths(LayoutUnit & minLogicalWidth,LayoutUnit & maxLogicalWidth) const145 void RenderListBox::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
146 {
147 RenderBlockFlow::computeIntrinsicLogicalWidths(minLogicalWidth, maxLogicalWidth);
148 if (style()->width().isPercent())
149 minLogicalWidth = 0;
150 }
151
scrollToRect(const LayoutRect & rect)152 void RenderListBox::scrollToRect(const LayoutRect& rect)
153 {
154 if (hasOverflowClip()) {
155 ASSERT(layer());
156 ASSERT(layer()->scrollableArea());
157 layer()->scrollableArea()->exposeRect(rect, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded);
158 }
159 }
160
161 } // namespace blink
162