1/*
2 * This file is part of the select element renderer in WebCore.
3 *
4 * Copyright (C) 2006, 2007, 2009 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 Computer, 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#ifndef RenderListBox_h
32#define RenderListBox_h
33
34#include "RenderBlock.h"
35#include "ScrollableArea.h"
36
37namespace WebCore {
38
39class HTMLSelectElement;
40
41class RenderListBox : public RenderBlock, private ScrollableArea {
42public:
43    explicit RenderListBox(Element*);
44    virtual ~RenderListBox();
45
46    void selectionChanged();
47
48    void setOptionsChanged(bool changed) { m_optionsChanged = changed; }
49
50    int listIndexAtOffset(const LayoutSize&);
51    LayoutRect itemBoundingBoxRect(const LayoutPoint&, int index);
52
53    bool scrollToRevealElementAtListIndex(int index);
54    bool listIndexIsVisible(int index);
55
56    int scrollToward(const IntPoint&); // Returns the new index or -1 if no scroll occurred
57
58    int size() const;
59
60private:
61    HTMLSelectElement* selectElement() const;
62
63    virtual const char* renderName() const { return "RenderListBox"; }
64
65    virtual bool isListBox() const { return true; }
66
67    virtual void updateFromElement();
68    virtual bool canBeReplacedWithInlineRunIn() const OVERRIDE;
69    virtual bool hasControlClip() const { return true; }
70    virtual void paintObject(PaintInfo&, const LayoutPoint&);
71    virtual LayoutRect controlClipRect(const LayoutPoint&) const;
72
73    virtual bool isPointInOverflowControl(HitTestResult&, const LayoutPoint& locationInContainer, const LayoutPoint& accumulatedOffset);
74
75    virtual bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1, Node** stopNode = 0);
76    virtual bool logicalScroll(ScrollLogicalDirection, ScrollGranularity, float multiplier = 1, Node** stopNode = 0);
77
78    virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
79    virtual void computePreferredLogicalWidths() OVERRIDE;
80    virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const;
81    virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const OVERRIDE;
82
83    virtual void layout();
84
85    virtual void addFocusRingRects(Vector<IntRect>&, const LayoutPoint&);
86
87    virtual bool canBeProgramaticallyScrolled() const { return true; }
88    virtual void autoscroll(const IntPoint&);
89    virtual void stopAutoscroll();
90
91    virtual bool shouldPanScroll() const { return true; }
92    virtual void panScroll(const IntPoint&);
93
94    virtual int verticalScrollbarWidth() const;
95    virtual int scrollLeft() const;
96    virtual int scrollTop() const;
97    virtual int scrollWidth() const;
98    virtual int scrollHeight() const;
99    virtual void setScrollLeft(int);
100    virtual void setScrollTop(int);
101
102    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
103
104    // ScrollableArea interface.
105    virtual int scrollSize(ScrollbarOrientation) const OVERRIDE;
106    virtual int scrollPosition(Scrollbar*) const OVERRIDE;
107    virtual void setScrollOffset(const IntPoint&) OVERRIDE;
108    virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE;
109    virtual bool isActive() const OVERRIDE;
110    virtual bool isScrollCornerVisible() const OVERRIDE { return false; } // We don't support resize on list boxes yet. If we did these would have to change.
111    virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
112    virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
113    virtual IntRect convertFromScrollbarToContainingView(const Scrollbar*, const IntRect&) const OVERRIDE;
114    virtual IntRect convertFromContainingViewToScrollbar(const Scrollbar*, const IntRect&) const OVERRIDE;
115    virtual IntPoint convertFromScrollbarToContainingView(const Scrollbar*, const IntPoint&) const OVERRIDE;
116    virtual IntPoint convertFromContainingViewToScrollbar(const Scrollbar*, const IntPoint&) const OVERRIDE;
117    virtual Scrollbar* verticalScrollbar() const OVERRIDE { return m_vBar.get(); }
118    virtual IntSize contentsSize() const OVERRIDE;
119    virtual int visibleHeight() const OVERRIDE;
120    virtual int visibleWidth() const OVERRIDE;
121    virtual IntPoint lastKnownMousePosition() const OVERRIDE;
122    virtual bool isHandlingWheelEvent() const OVERRIDE;
123    virtual bool shouldSuspendScrollAnimations() const OVERRIDE;
124    virtual bool scrollbarsCanBeActive() const OVERRIDE;
125    virtual bool scrollbarAnimationsAreSuppressed() const OVERRIDE;
126
127    virtual ScrollableArea* enclosingScrollableArea() const OVERRIDE;
128    virtual IntRect scrollableAreaBoundingBox() const OVERRIDE;
129
130    // NOTE: This should only be called by the overriden setScrollOffset from ScrollableArea.
131    void scrollTo(int newOffset);
132
133    void setHasVerticalScrollbar(bool hasScrollbar);
134    PassRefPtr<Scrollbar> createScrollbar();
135    void destroyScrollbar();
136
137    LayoutUnit itemHeight() const;
138    void valueChanged(unsigned listIndex);
139    int numVisibleItems() const;
140    int numItems() const;
141    LayoutUnit listHeight() const;
142    void paintScrollbar(PaintInfo&, const LayoutPoint&);
143    void paintItemForeground(PaintInfo&, const LayoutPoint&, int listIndex);
144    void paintItemBackground(PaintInfo&, const LayoutPoint&, int listIndex);
145    void scrollToRevealSelection();
146
147    bool m_optionsChanged;
148    bool m_scrollToRevealSelectionAfterLayout;
149    bool m_inAutoscroll;
150    int m_optionsWidth;
151    int m_indexOffset;
152
153    RefPtr<Scrollbar> m_vBar;
154};
155
156inline RenderListBox* toRenderListBox(RenderObject* object)
157{
158    ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isListBox());
159    return static_cast<RenderListBox*>(object);
160}
161
162// This will catch anyone doing an unnecessary cast.
163void toRenderListBox(const RenderListBox*);
164
165} // namepace WebCore
166
167#endif // RenderListBox_h
168