1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef AutoFillPopupMenuClient_h
32#define AutoFillPopupMenuClient_h
33
34#include "PopupMenuClient.h"
35
36namespace WebCore {
37class HTMLInputElement;
38class PopupMenuStyle;
39class RenderStyle;
40}
41
42namespace WebKit {
43class WebString;
44class WebViewImpl;
45template <typename T> class WebVector;
46
47// The AutoFill suggestions popup menu client, used to display name suggestions
48// with right-justified labels.
49class AutoFillPopupMenuClient : public WebCore::PopupMenuClient {
50public:
51    AutoFillPopupMenuClient();
52    virtual ~AutoFillPopupMenuClient();
53
54    // Returns the number of suggestions available.
55    virtual unsigned getSuggestionsCount() const;
56
57    // Returns the suggestion at |listIndex|.
58    virtual WebString getSuggestion(unsigned listIndex) const;
59
60    // Returns the label at |listIndex|.
61    virtual WebString getLabel(unsigned listIndex) const;
62
63    // Returns the icon at |listIndex|.
64    virtual WebString getIcon(unsigned listIndex) const;
65
66    // Removes the suggestion at |listIndex| from the list of suggestions.
67    virtual void removeSuggestionAtIndex(unsigned listIndex);
68
69    // Returns true if the suggestion at |listIndex| can be removed.
70    bool canRemoveSuggestionAtIndex(unsigned listIndex);
71
72    // WebCore::PopupMenuClient methods:
73    virtual void valueChanged(unsigned listIndex, bool fireEvents = true);
74    virtual void selectionChanged(unsigned, bool);
75    virtual void selectionCleared();
76    virtual WTF::String itemText(unsigned listIndex) const;
77    virtual WTF::String itemLabel(unsigned listIndex) const;
78    virtual WTF::String itemIcon(unsigned listIndex) const;
79    virtual WTF::String itemToolTip(unsigned lastIndex) const { return WTF::String(); }
80    virtual WTF::String itemAccessibilityText(unsigned lastIndex) const { return WTF::String(); }
81    virtual bool itemIsEnabled(unsigned listIndex) const;
82    virtual WebCore::PopupMenuStyle itemStyle(unsigned listIndex) const;
83    virtual WebCore::PopupMenuStyle menuStyle() const;
84    virtual int clientInsetLeft() const { return 0; }
85    virtual int clientInsetRight() const { return 0; }
86    virtual int clientPaddingLeft() const;
87    virtual int clientPaddingRight() const;
88    virtual int listSize() const { return getSuggestionsCount(); }
89    virtual int selectedIndex() const { return m_selectedIndex; }
90    virtual void popupDidHide();
91    virtual bool itemIsSeparator(unsigned listIndex) const;
92    virtual bool itemIsLabel(unsigned listIndex) const { return false; }
93    virtual bool itemIsSelected(unsigned listIndex) const { return false; }
94    virtual bool shouldPopOver() const { return false; }
95    virtual bool valueShouldChangeOnHotTrack() const { return false; }
96    virtual void setTextFromItem(unsigned listIndex);
97    virtual WebCore::FontSelector* fontSelector() const;
98    virtual WebCore::HostWindow* hostWindow() const;
99    virtual PassRefPtr<WebCore::Scrollbar> createScrollbar(
100        WebCore::ScrollableArea* client,
101        WebCore::ScrollbarOrientation orientation,
102        WebCore::ScrollbarControlSize size);
103
104    void initialize(WebCore::HTMLInputElement*,
105                    const WebVector<WebString>& names,
106                    const WebVector<WebString>& labels,
107                    const WebVector<WebString>& icons,
108                    const WebVector<int>& uniqueIDs,
109                    int separatorIndex);
110
111    void setSuggestions(const WebVector<WebString>& names,
112                        const WebVector<WebString>& labels,
113                        const WebVector<WebString>& icons,
114                        const WebVector<int>& uniqueIDs,
115                        int separatorIndex);
116
117private:
118    // Convert the specified index from an index into the visible list (which might
119    // include a separator entry) to an index to |m_names| and |m_labels|.
120    // Returns -1 if the given index points to the separator.
121    int convertListIndexToInternalIndex(unsigned) const;
122    WebViewImpl* getWebView() const;
123    WebCore::HTMLInputElement* getTextField() const { return m_textField.get(); }
124    WebCore::RenderStyle* textFieldStyle() const;
125
126    int getSelectedIndex() const { return m_selectedIndex; }
127    void setSelectedIndex(int index) { m_selectedIndex = index; }
128
129    bool itemIsWarning(unsigned listIndex) const;
130
131    // The names, labels and icons that make up the contents of the menu items.
132    Vector<WTF::String> m_names;
133    Vector<WTF::String> m_labels;
134    Vector<WTF::String> m_icons;
135    Vector<int> m_uniqueIDs;
136
137    // The index of the separator.  -1 if there is no separator.
138    int m_separatorIndex;
139
140    // The index of the selected item.  -1 if there is no selected item.
141    int m_selectedIndex;
142
143    RefPtr<WebCore::HTMLInputElement> m_textField;
144    OwnPtr<WebCore::PopupMenuStyle> m_regularStyle;
145    OwnPtr<WebCore::PopupMenuStyle> m_warningStyle;
146};
147
148} // namespace WebKit
149
150#endif
151