1/*
2 * This file is part of the select element renderer in WebCore.
3 *
4 * Copyright (C) 2006, 2007, 2009, 2014 Apple Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#pragma once
32
33#include "RenderBlockFlow.h"
34#include "ScrollableArea.h"
35#include <wtf/Optional.h>
36
37namespace WebCore {
38
39class HTMLSelectElement;
40
41class RenderListBox final : public RenderBlockFlow, public ScrollableArea {
42 WTF_MAKE_ISO_ALLOCATED(RenderListBox);
43public:
44 RenderListBox(HTMLSelectElement&, RenderStyle&&);
45 virtual ~RenderListBox();
46
47 HTMLSelectElement& selectElement() const;
48
49 void selectionChanged();
50
51 void setOptionsChanged(bool changed) { m_optionsChanged = changed; }
52
53 int listIndexAtOffset(const LayoutSize&);
54 LayoutRect itemBoundingBoxRect(const LayoutPoint&, int index);
55
56 bool scrollToRevealElementAtListIndex(int index);
57 bool listIndexIsVisible(int index);
58
59 int scrollToward(const IntPoint&); // Returns the new index or -1 if no scroll occurred
60
61 int size() const;
62
63 bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1, Element** stopElement = nullptr, RenderBox* startBox = nullptr, const IntPoint& wheelEventAbsolutePoint = IntPoint()) override;
64
65 bool scrolledToTop() const override;
66 bool scrolledToBottom() const override;
67 bool scrolledToLeft() const override;
68 bool scrolledToRight() const override;
69
70private:
71 void willBeDestroyed() override;
72
73 void element() const = delete;
74
75 const char* renderName() const override { return "RenderListBox"; }
76
77 bool isListBox() const override { return true; }
78
79 void updateFromElement() override;
80 bool hasControlClip() const override { return true; }
81 void paintObject(PaintInfo&, const LayoutPoint&) override;
82 LayoutRect controlClipRect(const LayoutPoint&) const override;
83
84 bool isPointInOverflowControl(HitTestResult&, const LayoutPoint& locationInContainer, const LayoutPoint& accumulatedOffset) override;
85
86 bool logicalScroll(ScrollLogicalDirection, ScrollGranularity, float multiplier = 1, Element** stopElement = nullptr) override;
87
88 void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
89 void computePreferredLogicalWidths() override;
90 int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const override;
91 LogicalExtentComputedValues computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop) const override;
92
93 void layout() override;
94
95 void addFocusRingRects(Vector<LayoutRect>&, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer = nullptr) override;
96
97 bool canBeProgramaticallyScrolled() const override { return true; }
98 void autoscroll(const IntPoint&) override;
99 void stopAutoscroll() override;
100
101 virtual bool shouldPanScroll() const { return true; }
102 void panScroll(const IntPoint&) override;
103
104 int verticalScrollbarWidth() const override;
105 int scrollLeft() const override;
106 int scrollTop() const override;
107 int scrollWidth() const override;
108 int scrollHeight() const override;
109 void setScrollLeft(int, ScrollType, ScrollClamping) override;
110 void setScrollTop(int, ScrollType, ScrollClamping) override;
111
112 bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
113
114 // ScrollableArea interface.
115 void setScrollOffset(const ScrollOffset&) override;
116
117 ScrollPosition scrollPosition() const override;
118 ScrollPosition minimumScrollPosition() const override;
119 ScrollPosition maximumScrollPosition() const override;
120
121 void invalidateScrollbarRect(Scrollbar&, const IntRect&) override;
122 bool isActive() const override;
123 bool isScrollCornerVisible() const override { return false; } // We don't support resize on list boxes yet. If we did these would have to change.
124 IntRect scrollCornerRect() const override { return IntRect(); }
125 void invalidateScrollCornerRect(const IntRect&) override { }
126 IntRect convertFromScrollbarToContainingView(const Scrollbar&, const IntRect&) const override;
127 IntRect convertFromContainingViewToScrollbar(const Scrollbar&, const IntRect&) const override;
128 IntPoint convertFromScrollbarToContainingView(const Scrollbar&, const IntPoint&) const override;
129 IntPoint convertFromContainingViewToScrollbar(const Scrollbar&, const IntPoint&) const override;
130 Scrollbar* verticalScrollbar() const override { return m_vBar.get(); }
131 IntSize contentsSize() const override;
132 IntSize visibleSize() const override { return IntSize(width(), height()); }
133 IntPoint lastKnownMousePosition() const override;
134 bool isHandlingWheelEvent() const override;
135 bool shouldSuspendScrollAnimations() const override;
136 bool forceUpdateScrollbarsOnMainThreadForPerformanceTesting() const override;
137
138 ScrollableArea* enclosingScrollableArea() const override;
139 bool isScrollableOrRubberbandable() override;
140 bool hasScrollableOrRubberbandableAncestor() override;
141 IntRect scrollableAreaBoundingBox(bool* = nullptr) const override;
142 bool usesMockScrollAnimator() const override;
143 void logMockScrollAnimatorMessage(const String&) const override;
144
145 // NOTE: This should only be called by the overridden setScrollOffset from ScrollableArea.
146 void scrollTo(int newOffset);
147
148 using PaintFunction = WTF::Function<void(PaintInfo&, const LayoutPoint&, int listItemIndex)>;
149 void paintItem(PaintInfo&, const LayoutPoint&, const PaintFunction&);
150
151 void setHasVerticalScrollbar(bool hasScrollbar);
152 Ref<Scrollbar> createScrollbar();
153 void destroyScrollbar();
154
155 int maximumNumberOfItemsThatFitInPaddingBottomArea() const;
156
157 int numberOfVisibleItemsInPaddingTop() const;
158 int numberOfVisibleItemsInPaddingBottom() const;
159
160 void computeFirstIndexesVisibleInPaddingTopBottomAreas();
161
162 LayoutUnit itemHeight() const;
163 void valueChanged(unsigned listIndex);
164
165 enum class ConsiderPadding { Yes, No };
166 int numVisibleItems(ConsiderPadding = ConsiderPadding::No) const;
167 int numItems() const;
168 LayoutUnit listHeight() const;
169 void paintScrollbar(PaintInfo&, const LayoutPoint&);
170 void paintItemForeground(PaintInfo&, const LayoutPoint&, int listIndex);
171 void paintItemBackground(PaintInfo&, const LayoutPoint&, int listIndex);
172 void scrollToRevealSelection();
173
174 bool shouldPlaceBlockDirectionScrollbarOnLeft() const final { return RenderBlockFlow::shouldPlaceBlockDirectionScrollbarOnLeft(); }
175
176 bool m_optionsChanged;
177 bool m_scrollToRevealSelectionAfterLayout;
178 bool m_inAutoscroll;
179 int m_optionsWidth;
180 int m_indexOffset;
181
182 Optional<int> m_indexOfFirstVisibleItemInsidePaddingTopArea;
183 Optional<int> m_indexOfFirstVisibleItemInsidePaddingBottomArea;
184
185 RefPtr<Scrollbar> m_vBar;
186};
187
188} // namepace WebCore
189
190SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderListBox, isListBox())
191