WebCore/platform/chromium/PopupMenuChromium.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (c) 2008, 2009, 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 PopupMenuChromium_h
       
    32 #define PopupMenuChromium_h
       
    33 
       
    34 #include "config.h"
       
    35 #include "PopupMenuClient.h"
       
    36 
       
    37 #include "FramelessScrollView.h"
       
    38 #include "IntRect.h"
       
    39 
       
    40 namespace WebCore {
       
    41 
       
    42 class ChromeClientChromium;
       
    43 class FrameView;
       
    44 class PopupListBox;
       
    45 
       
    46 // A container for the data for each menu item (e.g. represented by <option>
       
    47 // or <optgroup> in a <select> widget) and is used by PopupListBox.
       
    48 struct PopupItem {
       
    49     enum Type {
       
    50         TypeOption,
       
    51         TypeGroup,
       
    52         TypeSeparator
       
    53     };
       
    54 
       
    55     PopupItem(const String& label, Type type)
       
    56         : label(label)
       
    57         , type(type)
       
    58         , yOffset(0)
       
    59     {
       
    60     }
       
    61     String label;
       
    62     Type type;
       
    63     int yOffset; // y offset of this item, relative to the top of the popup.
       
    64     bool enabled;
       
    65 };
       
    66 
       
    67 // FIXME: Our FramelessScrollView classes should probably implement HostWindow!
       
    68 
       
    69 // The PopupContainer class holds a PopupListBox (see cpp file).  Its sole purpose is to be
       
    70 // able to draw a border around its child.  All its paint/event handling is
       
    71 // just forwarded to the child listBox (with the appropriate transforms).
       
    72 // NOTE: this class is exposed so it can be instantiated direcly for the
       
    73 // autofill popup.  We cannot use the Popup class directly in that case as the
       
    74 // autofill popup should not be focused when shown and we want to forward the
       
    75 // key events to it (through handleKeyEvent).
       
    76 
       
    77 struct PopupContainerSettings {
       
    78     // Whether the PopupMenuClient should be told to change its text when a
       
    79     // new item is selected by using the arrow keys.
       
    80     bool setTextOnIndexChange;
       
    81 
       
    82     // Whether the selection should be accepted when the popup menu is
       
    83     // closed (through ESC being pressed or the focus going away).
       
    84     // Note that when TAB is pressed, the selection is always accepted
       
    85     // regardless of this setting.
       
    86     bool acceptOnAbandon;
       
    87 
       
    88     // Whether we should move the selection to the first/last item when
       
    89     // the user presses down/up arrow keys and the last/first item is
       
    90     // selected.
       
    91     bool loopSelectionNavigation;
       
    92 
       
    93     // Whether we should restrict the width of the PopupListBox or not.
       
    94     // Autocomplete popups are restricted, combo-boxes (select tags) aren't.
       
    95     bool restrictWidthOfListBox;
       
    96 
       
    97     // A hint on the display directionality of the item text in popup menu.
       
    98     //
       
    99     // We could either display the items in the drop-down using its DOM element's
       
   100     // directionality, or we could display the items in the drop-down using heuristics:
       
   101     // such as in its first strong directionality character's direction.
       
   102     // Please refer to the discussion (especially comment #7 and #10) in
       
   103     // https://bugs.webkit.org/show_bug.cgi?id=27889 for details.
       
   104     enum DirectionalityHint {
       
   105         // Use the DOM element's directionality to display the item text in popup menu.
       
   106         DOMElementDirection,
       
   107         // Use the item text's first strong-directional character's directionality
       
   108         // to display the item text in popup menu.
       
   109         FirstStrongDirectionalCharacterDirection,
       
   110     };
       
   111     DirectionalityHint itemTextDirectionalityHint;
       
   112 };
       
   113 
       
   114 class PopupContainer : public FramelessScrollView {
       
   115 public:
       
   116     enum PopupType {
       
   117         Select, // HTML select popup.
       
   118         Suggestion, // Autocomplete/autofill popup.
       
   119     };
       
   120 
       
   121     static PassRefPtr<PopupContainer> create(PopupMenuClient*, PopupType,
       
   122                                              const PopupContainerSettings&);
       
   123 
       
   124     // Whether a key event should be sent to this popup.
       
   125     virtual bool isInterestedInEventForKey(int keyCode);
       
   126 
       
   127     // FramelessScrollView
       
   128     virtual void paint(GraphicsContext*, const IntRect&);
       
   129     virtual void hide();
       
   130     virtual bool handleMouseDownEvent(const PlatformMouseEvent&);
       
   131     virtual bool handleMouseMoveEvent(const PlatformMouseEvent&);
       
   132     virtual bool handleMouseReleaseEvent(const PlatformMouseEvent&);
       
   133     virtual bool handleWheelEvent(const PlatformWheelEvent&);
       
   134     virtual bool handleKeyEvent(const PlatformKeyboardEvent&);
       
   135 
       
   136     // PopupContainer methods
       
   137 
       
   138     // Show the popup
       
   139     void showPopup(FrameView*);
       
   140 
       
   141     // Used on Mac Chromium for HTML select popup menus.
       
   142     void showExternal(const IntRect&, FrameView*, int index);
       
   143 
       
   144     // Show the popup in the specified rect for the specified frame.
       
   145     // Note: this code was somehow arbitrarily factored-out of the Popup class
       
   146     // so WebViewImpl can create a PopupContainer. This method is used for
       
   147     // displaying auto complete popup menus on Mac Chromium, and for all
       
   148     // popups on other platforms.
       
   149     void show(const IntRect&, FrameView*, int index);
       
   150 
       
   151     // Hides the popup.
       
   152     void hidePopup();
       
   153 
       
   154     // The popup was hidden.
       
   155     void notifyPopupHidden();
       
   156 
       
   157     // Compute size of widget and children.
       
   158     void layout();
       
   159 
       
   160     PopupListBox* listBox() const { return m_listBox.get(); }
       
   161 
       
   162     // Gets the index of the item that the user is currently moused-over or
       
   163     // has selected with the keyboard up/down arrows.
       
   164     int selectedIndex() const;
       
   165 
       
   166     // Refresh the popup values from the PopupMenuClient.
       
   167     void refresh();
       
   168 
       
   169     // The menu per-item data.
       
   170     const WTF::Vector<PopupItem*>& popupData() const;
       
   171 
       
   172     // The height of a row in the menu.
       
   173     int menuItemHeight() const;
       
   174 
       
   175     // The size of the font being used.
       
   176     int menuItemFontSize() const;
       
   177 
       
   178     // The style of the menu being used.
       
   179     PopupMenuStyle menuStyle() const;
       
   180 
       
   181     PopupType popupType() const { return m_popupType; }
       
   182 
       
   183 private:
       
   184     friend class WTF::RefCounted<PopupContainer>;
       
   185 
       
   186     PopupContainer(PopupMenuClient*, PopupType popupType, const PopupContainerSettings&);
       
   187     ~PopupContainer();
       
   188 
       
   189     // Paint the border.
       
   190     void paintBorder(GraphicsContext*, const IntRect&);
       
   191 
       
   192     // Returns the ChromeClient of the page this popup is associated with.
       
   193     ChromeClientChromium* chromeClientChromium();
       
   194 
       
   195     RefPtr<PopupListBox> m_listBox;
       
   196     RefPtr<FrameView> m_frameView;
       
   197 
       
   198     PopupContainerSettings m_settings;
       
   199     PopupType m_popupType;
       
   200     // Whether the popup is currently open.
       
   201     bool m_popupOpen;
       
   202 };
       
   203 
       
   204 } // namespace WebCore
       
   205 
       
   206 #endif