1/*
2 * This file is part of the select element renderer in WebCore.
3 *
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
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#ifndef RenderMenuList_h
25#define RenderMenuList_h
26
27#include "LayoutRect.h"
28#include "PopupMenu.h"
29#include "PopupMenuClient.h"
30#include "RenderFlexibleBox.h"
31
32#if PLATFORM(MAC)
33#define POPUP_MENU_PULLS_DOWN 0
34#else
35#define POPUP_MENU_PULLS_DOWN 1
36#endif
37
38namespace WebCore {
39
40class HTMLSelectElement;
41class RenderText;
42
43class RenderMenuList : public RenderFlexibleBox, private PopupMenuClient {
44
45public:
46    RenderMenuList(Element*);
47    virtual ~RenderMenuList();
48
49public:
50    bool popupIsVisible() const { return m_popupIsVisible; }
51    void showPopup();
52    void hidePopup();
53
54    void setOptionsChanged(bool changed) { m_optionsChanged = changed; }
55
56    void didSetSelectedIndex(int listIndex);
57
58    String text() const;
59
60private:
61    HTMLSelectElement* selectElement() const;
62
63    virtual bool isMenuList() const { return true; }
64
65    virtual void addChild(RenderObject* newChild, RenderObject* beforeChild = 0);
66    virtual void removeChild(RenderObject*);
67    virtual bool createsAnonymousWrapper() const { return true; }
68
69    virtual void updateFromElement();
70
71    virtual LayoutRect controlClipRect(const LayoutPoint&) const;
72    virtual bool hasControlClip() const { return true; }
73    virtual bool canHaveGeneratedChildren() const OVERRIDE { return false; }
74    virtual bool canBeReplacedWithInlineRunIn() const OVERRIDE;
75
76    virtual const char* renderName() const { return "RenderMenuList"; }
77
78    virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
79    virtual void computePreferredLogicalWidths() OVERRIDE;
80
81    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
82
83    virtual bool requiresForcedStyleRecalcPropagation() const { return true; }
84
85    // PopupMenuClient methods
86    virtual void valueChanged(unsigned listIndex, bool fireOnChange = true) OVERRIDE;
87    virtual void selectionChanged(unsigned, bool) OVERRIDE { }
88    virtual void selectionCleared() OVERRIDE { }
89    virtual String itemText(unsigned listIndex) const OVERRIDE;
90    virtual String itemLabel(unsigned listIndex) const OVERRIDE;
91    virtual String itemIcon(unsigned listIndex) const OVERRIDE;
92    virtual String itemToolTip(unsigned listIndex) const OVERRIDE;
93    virtual String itemAccessibilityText(unsigned listIndex) const OVERRIDE;
94    virtual bool itemIsEnabled(unsigned listIndex) const OVERRIDE;
95    virtual PopupMenuStyle itemStyle(unsigned listIndex) const OVERRIDE;
96    virtual PopupMenuStyle menuStyle() const OVERRIDE;
97    virtual int clientInsetLeft() const OVERRIDE;
98    virtual int clientInsetRight() const OVERRIDE;
99    virtual LayoutUnit clientPaddingLeft() const OVERRIDE;
100    virtual LayoutUnit clientPaddingRight() const OVERRIDE;
101    virtual int listSize() const OVERRIDE;
102    virtual int selectedIndex() const OVERRIDE;
103    virtual void popupDidHide() OVERRIDE;
104    virtual bool itemIsSeparator(unsigned listIndex) const OVERRIDE;
105    virtual bool itemIsLabel(unsigned listIndex) const OVERRIDE;
106    virtual bool itemIsSelected(unsigned listIndex) const OVERRIDE;
107    virtual bool shouldPopOver() const OVERRIDE { return !POPUP_MENU_PULLS_DOWN; }
108    virtual bool valueShouldChangeOnHotTrack() const OVERRIDE { return true; }
109    virtual void setTextFromItem(unsigned listIndex) OVERRIDE;
110    virtual void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true) OVERRIDE;
111    virtual bool multiple() const OVERRIDE;
112    virtual FontSelector* fontSelector() const OVERRIDE;
113    virtual HostWindow* hostWindow() const OVERRIDE;
114    virtual PassRefPtr<Scrollbar> createScrollbar(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize) OVERRIDE;
115
116    virtual bool hasLineIfEmpty() const { return true; }
117
118    // Flexbox defines baselines differently than regular blocks.
119    // For backwards compatibility, menulists need to do the regular block behavior.
120    virtual int baselinePosition(FontBaseline baseline, bool firstLine, LineDirectionMode direction, LinePositionMode position) const OVERRIDE
121    {
122        return RenderBlock::baselinePosition(baseline, firstLine, direction, position);
123    }
124    virtual int firstLineBoxBaseline() const OVERRIDE { return RenderBlock::firstLineBoxBaseline(); }
125    virtual int inlineBlockBaseline(LineDirectionMode direction) const OVERRIDE { return RenderBlock::inlineBlockBaseline(direction); }
126
127    void getItemBackgroundColor(unsigned listIndex, Color&, bool& itemHasCustomBackgroundColor) const;
128
129    void createInnerBlock();
130    void adjustInnerStyle();
131    void setText(const String&);
132    void setTextFromOption(int optionIndex);
133    void updateOptionsWidth();
134
135    void didUpdateActiveOption(int optionIndex);
136
137    RenderText* m_buttonText;
138    RenderBlock* m_innerBlock;
139
140    bool m_optionsChanged;
141    int m_optionsWidth;
142
143    int m_lastActiveIndex;
144
145    RefPtr<RenderStyle> m_optionStyle;
146
147    RefPtr<PopupMenu> m_popup;
148    bool m_popupIsVisible;
149};
150
151inline RenderMenuList* toRenderMenuList(RenderObject* object)
152{
153    ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isMenuList());
154    return static_cast<RenderMenuList*>(object);
155}
156
157// This will catch anyone doing an unnecessary cast.
158void toRenderMenuList(const RenderMenuList*);
159
160}
161
162#endif
163