Home | History | Annotate | Download | only in menu
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_VIEWS_CONTROLS_MENU_MENU_DELEGATE_H_
      6 #define UI_VIEWS_CONTROLS_MENU_MENU_DELEGATE_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/logging.h"
     12 #include "base/strings/string16.h"
     13 #include "third_party/skia/include/core/SkColor.h"
     14 #include "ui/base/dragdrop/drag_drop_types.h"
     15 #include "ui/base/dragdrop/os_exchange_data.h"
     16 #include "ui/base/ui_base_types.h"
     17 #include "ui/views/controls/menu/menu_types.h"
     18 #include "ui/views/views_export.h"
     19 
     20 using ui::OSExchangeData;
     21 
     22 namespace gfx {
     23 class FontList;
     24 class Point;
     25 }
     26 
     27 namespace ui {
     28 class Accelerator;
     29 class DropTargetEvent;
     30 }
     31 
     32 namespace views {
     33 
     34 class MenuButton;
     35 class MenuItemView;
     36 
     37 // MenuDelegate --------------------------------------------------------------
     38 
     39 // Delegate for a menu. This class is used as part of MenuItemView, see it
     40 // for details.
     41 // TODO(sky): merge this with ui::MenuModel.
     42 class VIEWS_EXPORT MenuDelegate {
     43  public:
     44   // Used during drag and drop to indicate where the drop indicator should
     45   // be rendered.
     46   enum DropPosition {
     47     DROP_UNKNOWN = -1,
     48 
     49     // Indicates a drop is not allowed here.
     50     DROP_NONE,
     51 
     52     // Indicates the drop should occur before the item.
     53     DROP_BEFORE,
     54 
     55     // Indicates the drop should occur after the item.
     56     DROP_AFTER,
     57 
     58     // Indicates the drop should occur on the item.
     59     DROP_ON
     60   };
     61 
     62   virtual ~MenuDelegate();
     63 
     64   // Whether or not an item should be shown as checked. This is invoked for
     65   // radio buttons and check buttons.
     66   virtual bool IsItemChecked(int id) const;
     67 
     68   // The string shown for the menu item. This is only invoked when an item is
     69   // added with an empty label.
     70   virtual base::string16 GetLabel(int id) const;
     71 
     72   // The font for the menu item label.
     73   virtual const gfx::FontList* GetLabelFontList(int id) const;
     74 
     75   // Whether this item should be displayed with a bolder color when disabled.
     76   virtual bool GetShouldUseDisabledEmphasizedForegroundColor(
     77       int command_id) const;
     78 
     79   // Override the text color of a given menu item dependent on the
     80   // |command_id| and its |is_hovered| state. Returns true if it chooses to
     81   // override the color.
     82   //
     83   // TODO(erg): Remove this interface. Injecting raw colors into the menu
     84   // circumvents the NativeTheme.
     85   virtual bool GetForegroundColor(int command_id,
     86                                   bool is_hovered,
     87                                   SkColor* override_color) const;
     88 
     89   // Override the background color of a given menu item dependent on the
     90   // |command_id| and its |is_hovered| state. Returns true if it chooses to
     91   // override the color.
     92   //
     93   // TODO(erg): Remove this interface. Injecting raw colors into the menu
     94   // circumvents the NativeTheme.
     95   virtual bool GetBackgroundColor(int command_id,
     96                                   bool is_hovered,
     97                                   SkColor* override_color) const;
     98 
     99   // The tooltip shown for the menu item. This is invoked when the user
    100   // hovers over the item, and no tooltip text has been set for that item.
    101   virtual base::string16 GetTooltipText(int id,
    102                                         const gfx::Point& screen_loc) const;
    103 
    104   // If there is an accelerator for the menu item with id |id| it is set in
    105   // |accelerator| and true is returned.
    106   virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) const;
    107 
    108   // Shows the context menu with the specified id. This is invoked when the
    109   // user does the appropriate gesture to show a context menu. The id
    110   // identifies the id of the menu to show the context menu for.
    111   // is_mouse_gesture is true if this is the result of a mouse gesture.
    112   // If this is not the result of a mouse gesture |p| is the recommended
    113   // location to display the content menu at. In either case, |p| is in
    114   // screen coordinates.
    115   // Returns true if a context menu was displayed, otherwise false
    116   virtual bool ShowContextMenu(MenuItemView* source,
    117                                int id,
    118                                const gfx::Point& p,
    119                                ui::MenuSourceType source_type);
    120 
    121   // Controller
    122   virtual bool SupportsCommand(int id) const;
    123   virtual bool IsCommandEnabled(int id) const;
    124   virtual bool IsCommandVisible(int id) const;
    125   virtual bool GetContextualLabel(int id, base::string16* out) const;
    126   virtual void ExecuteCommand(int id) {
    127   }
    128 
    129   // If nested menus are showing (nested menus occur when a menu shows a context
    130   // menu) this is invoked to determine if all the menus should be closed when
    131   // the user selects the menu with the command |id|. This returns true to
    132   // indicate that all menus should be closed. Return false if only the
    133   // context menu should be closed.
    134   virtual bool ShouldCloseAllMenusOnExecute(int id);
    135 
    136   // Executes the specified command. mouse_event_flags give the flags of the
    137   // mouse event that triggered this to be invoked (ui::MouseEvent
    138   // flags). mouse_event_flags is 0 if this is triggered by a user gesture
    139   // other than a mouse event.
    140   virtual void ExecuteCommand(int id, int mouse_event_flags);
    141 
    142   // Returns true if ExecuteCommand() should be invoked while leaving the
    143   // menu open. Default implementation returns true.
    144   virtual bool ShouldExecuteCommandWithoutClosingMenu(int id,
    145                                                       const ui::Event& e);
    146 
    147   // Returns true if the specified event is one the user can use to trigger, or
    148   // accept, the item. Defaults to left or right mouse buttons or tap.
    149   virtual bool IsTriggerableEvent(MenuItemView* view, const ui::Event& e);
    150 
    151   // Invoked to determine if drops can be accepted for a submenu. This is
    152   // ONLY invoked for menus that have submenus and indicates whether or not
    153   // a drop can occur on any of the child items of the item. For example,
    154   // consider the following menu structure:
    155   //
    156   // A
    157   //   B
    158   //   C
    159   //
    160   // Where A has a submenu with children B and C. This is ONLY invoked for
    161   // A, not B and C.
    162   //
    163 
    164   // To restrict which children can be dropped on override GetDropOperation.
    165   virtual bool CanDrop(MenuItemView* menu, const OSExchangeData& data);
    166 
    167   // See view for a description of this method.
    168   virtual bool GetDropFormats(
    169       MenuItemView* menu,
    170       int* formats,
    171       std::set<OSExchangeData::CustomFormat>* custom_formats);
    172 
    173   // See view for a description of this method.
    174   virtual bool AreDropTypesRequired(MenuItemView* menu);
    175 
    176   // Returns the drop operation for the specified target menu item. This is
    177   // only invoked if CanDrop returned true for the parent menu. position
    178   // is set based on the location of the mouse, reset to specify a different
    179   // position.
    180   //
    181   // If a drop should not be allowed, returned ui::DragDropTypes::DRAG_NONE.
    182   virtual int GetDropOperation(MenuItemView* item,
    183                                const ui::DropTargetEvent& event,
    184                                DropPosition* position);
    185 
    186   // Invoked to perform the drop operation. This is ONLY invoked if CanDrop()
    187   // returned true for the parent menu item, and GetDropOperation() returned an
    188   // operation other than ui::DragDropTypes::DRAG_NONE.
    189   //
    190   // |menu| is the menu the drop occurred on.
    191   virtual int OnPerformDrop(MenuItemView* menu,
    192                             DropPosition position,
    193                             const ui::DropTargetEvent& event);
    194 
    195   // Invoked to determine if it is possible for the user to drag the specified
    196   // menu item.
    197   virtual bool CanDrag(MenuItemView* menu);
    198 
    199   // Invoked to write the data for a drag operation to data. sender is the
    200   // MenuItemView being dragged.
    201   virtual void WriteDragData(MenuItemView* sender, OSExchangeData* data);
    202 
    203   // Invoked to determine the drag operations for a drag session of sender.
    204   // See DragDropTypes for possible values.
    205   virtual int GetDragOperations(MenuItemView* sender);
    206 
    207   // Notification the menu has closed. This is only sent when running the
    208   // menu for a drop.
    209   virtual void DropMenuClosed(MenuItemView* menu) {
    210   }
    211 
    212   // Notification that the user has highlighted the specified item.
    213   virtual void SelectionChanged(MenuItemView* menu) {
    214   }
    215 
    216   // If the user drags the mouse outside the bounds of the menu the delegate
    217   // is queried for a sibling menu to show. If this returns non-null the
    218   // current menu is hidden, and the menu returned from this method is shown.
    219   //
    220   // The delegate owns the returned menu, not the controller.
    221   virtual MenuItemView* GetSiblingMenu(MenuItemView* menu,
    222                                        const gfx::Point& screen_point,
    223                                        MenuAnchorPosition* anchor,
    224                                        bool* has_mnemonics,
    225                                        MenuButton** button);
    226 
    227   // Returns the max width menus can grow to be.
    228   virtual int GetMaxWidthForMenu(MenuItemView* menu);
    229 
    230   // Invoked prior to a menu being shown.
    231   virtual void WillShowMenu(MenuItemView* menu);
    232 
    233   // Invoked prior to a menu being hidden.
    234   virtual void WillHideMenu(MenuItemView* menu);
    235 
    236   // Returns additional horizontal spacing for the icon of the given item.
    237   // The |command_id| specifies the item of interest, the |icon_size| tells the
    238   // function the size of the icon and it will then return |left_margin|
    239   // and |right_margin| accordingly. Note: Negative values can be returned.
    240   virtual void GetHorizontalIconMargins(int command_id,
    241                                         int icon_size,
    242                                         int* left_margin,
    243                                         int* right_margin) const;
    244   // Returns true if the labels should reserve additional spacing for e.g.
    245   // submenu indicators at the end of the line.
    246   virtual bool ShouldReserveSpaceForSubmenuIndicator() const;
    247 };
    248 
    249 }  // namespace views
    250 
    251 #endif  // UI_VIEWS_CONTROLS_MENU_MENU_DELEGATE_H_
    252