OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ | 5 #ifndef VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ |
6 #define VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ | 6 #define VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ |
7 | 7 |
8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
9 #include "views/view.h" | 9 #include "views/view.h" |
10 | 10 |
11 namespace views { | 11 namespace views { |
12 | 12 |
13 class MenuButton; | 13 class MenuButton; |
14 class MenuController; | 14 class MenuController; |
15 class MenuDelegate; | 15 class MenuDelegate; |
16 class SubmenuView; | 16 class SubmenuView; |
17 | 17 |
18 // MenuItemView -------------------------------------------------------------- | 18 // MenuItemView -------------------------------------------------------------- |
19 | 19 |
20 // MenuItemView represents a single menu item with a label and optional icon. | 20 // MenuItemView represents a single menu item with a label and optional icon. |
21 // Each MenuItemView may also contain a submenu, which in turn may contain | 21 // Each MenuItemView may also contain a submenu, which in turn may contain |
22 // any number of child MenuItemViews. | 22 // any number of child MenuItemViews. |
23 // | 23 // |
24 // To use a menu create an initial MenuItemView using the constructor that | 24 // To use a menu create an initial MenuItemView using the constructor that |
25 // takes a MenuDelegate, then create any number of child menu items by way | 25 // takes a MenuDelegate, then create any number of child menu items by way |
26 // of the various AddXXX methods. | 26 // of the various AddXXX methods. |
27 // | 27 // |
28 // MenuItemView is itself a View, which means you can add Views to each | 28 // MenuItemView is itself a View, which means you can add Views to each |
29 // MenuItemView. This normally NOT want you want, rather add other child Views | 29 // MenuItemView. This is normally NOT want you want, rather add other child |
30 // to the submenu of the MenuItemView. | 30 // Views to the submenu of the MenuItemView. Any child views of the MenuItemView |
| 31 // that are focusable can be navigated to by way of the up/down arrow and can be |
| 32 // activated by way of space/return keys. Activating a focusable child results |
| 33 // in |AcceleratorPressed| being invoked. Note, that as menus try not to steal |
| 34 // focus from the hosting window child views do not actually get focus. Instead |
| 35 // |SetHotTracked| is used as the user navigates around. |
31 // | 36 // |
32 // There are two ways to show a MenuItemView: | 37 // There are two ways to show a MenuItemView: |
33 // 1. Use RunMenuAt. This blocks the caller, executing the selected command | 38 // 1. Use RunMenuAt. This blocks the caller, executing the selected command |
34 // on success. | 39 // on success. |
35 // 2. Use RunMenuForDropAt. This is intended for use during a drop session | 40 // 2. Use RunMenuForDropAt. This is intended for use during a drop session |
36 // and does NOT block the caller. Instead the delegate is notified when the | 41 // and does NOT block the caller. Instead the delegate is notified when the |
37 // menu closes via the DropMenuClosed method. | 42 // menu closes via the DropMenuClosed method. |
38 | 43 |
39 class MenuItemView : public View { | 44 class MenuItemView : public View { |
40 public: | 45 public: |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 // item_id The id of the item, used to identify it in delegate callbacks | 107 // item_id The id of the item, used to identify it in delegate callbacks |
103 // or (if delegate is NULL) to identify the command associated | 108 // or (if delegate is NULL) to identify the command associated |
104 // with this item with the controller specified in the ctor. Note | 109 // with this item with the controller specified in the ctor. Note |
105 // that this value should not be 0 as this has a special meaning | 110 // that this value should not be 0 as this has a special meaning |
106 // ("NULL command, no item selected") | 111 // ("NULL command, no item selected") |
107 // label The text label shown. | 112 // label The text label shown. |
108 // type The type of item. | 113 // type The type of item. |
109 void AppendMenuItem(int item_id, | 114 void AppendMenuItem(int item_id, |
110 const std::wstring& label, | 115 const std::wstring& label, |
111 Type type) { | 116 Type type) { |
112 AppendMenuItemInternal(item_id, label, SkBitmap(), type); | 117 AppendMenuItemImpl(item_id, label, SkBitmap(), type); |
113 } | 118 } |
114 | 119 |
115 // Append a submenu to this menu. | 120 // Append a submenu to this menu. |
116 // The returned pointer is owned by this menu. | 121 // The returned pointer is owned by this menu. |
117 MenuItemView* AppendSubMenu(int item_id, | 122 MenuItemView* AppendSubMenu(int item_id, |
118 const std::wstring& label) { | 123 const std::wstring& label) { |
119 return AppendMenuItemInternal(item_id, label, SkBitmap(), SUBMENU); | 124 return AppendMenuItemImpl(item_id, label, SkBitmap(), SUBMENU); |
120 } | 125 } |
121 | 126 |
122 // Append a submenu with an icon to this menu. | 127 // Append a submenu with an icon to this menu. |
123 // The returned pointer is owned by this menu. | 128 // The returned pointer is owned by this menu. |
124 MenuItemView* AppendSubMenuWithIcon(int item_id, | 129 MenuItemView* AppendSubMenuWithIcon(int item_id, |
125 const std::wstring& label, | 130 const std::wstring& label, |
126 const SkBitmap& icon) { | 131 const SkBitmap& icon) { |
127 return AppendMenuItemInternal(item_id, label, icon, SUBMENU); | 132 return AppendMenuItemImpl(item_id, label, icon, SUBMENU); |
128 } | 133 } |
129 | 134 |
130 // This is a convenience for standard text label menu items where the label | 135 // This is a convenience for standard text label menu items where the label |
131 // is provided with this call. | 136 // is provided with this call. |
132 void AppendMenuItemWithLabel(int item_id, | 137 void AppendMenuItemWithLabel(int item_id, |
133 const std::wstring& label) { | 138 const std::wstring& label) { |
134 AppendMenuItem(item_id, label, NORMAL); | 139 AppendMenuItem(item_id, label, NORMAL); |
135 } | 140 } |
136 | 141 |
137 // This is a convenience for text label menu items where the label is | 142 // This is a convenience for text label menu items where the label is |
138 // provided by the delegate. | 143 // provided by the delegate. |
139 void AppendDelegateMenuItem(int item_id) { | 144 void AppendDelegateMenuItem(int item_id) { |
140 AppendMenuItem(item_id, std::wstring(), NORMAL); | 145 AppendMenuItem(item_id, std::wstring(), NORMAL); |
141 } | 146 } |
142 | 147 |
143 // Adds a separator to this menu | 148 // Adds a separator to this menu |
144 void AppendSeparator() { | 149 void AppendSeparator() { |
145 AppendMenuItemInternal(0, std::wstring(), SkBitmap(), SEPARATOR); | 150 AppendMenuItemImpl(0, std::wstring(), SkBitmap(), SEPARATOR); |
146 } | 151 } |
147 | 152 |
148 // Appends a menu item with an icon. This is for the menu item which | 153 // Appends a menu item with an icon. This is for the menu item which |
149 // needs an icon. Calling this function forces the Menu class to draw | 154 // needs an icon. Calling this function forces the Menu class to draw |
150 // the menu, instead of relying on Windows. | 155 // the menu, instead of relying on Windows. |
151 void AppendMenuItemWithIcon(int item_id, | 156 void AppendMenuItemWithIcon(int item_id, |
152 const std::wstring& label, | 157 const std::wstring& label, |
153 const SkBitmap& icon) { | 158 const SkBitmap& icon) { |
154 AppendMenuItemInternal(item_id, label, icon, NORMAL); | 159 AppendMenuItemImpl(item_id, label, icon, NORMAL); |
155 } | 160 } |
156 | 161 |
| 162 // All the AppendXXX methods funnel into this. |
| 163 MenuItemView* AppendMenuItemImpl(int item_id, |
| 164 const std::wstring& label, |
| 165 const SkBitmap& icon, |
| 166 Type type); |
| 167 |
157 // Returns the view that contains child menu items. If the submenu has | 168 // Returns the view that contains child menu items. If the submenu has |
158 // not been creates, this creates it. | 169 // not been creates, this creates it. |
159 virtual SubmenuView* CreateSubmenu(); | 170 virtual SubmenuView* CreateSubmenu(); |
160 | 171 |
161 // Returns true if this menu item has a submenu. | 172 // Returns true if this menu item has a submenu. |
162 virtual bool HasSubmenu() const { return (submenu_ != NULL); } | 173 virtual bool HasSubmenu() const { return (submenu_ != NULL); } |
163 | 174 |
164 // Returns the view containing child menu items. | 175 // Returns the view containing child menu items. |
165 virtual SubmenuView* GetSubmenu() const { return submenu_; } | 176 virtual SubmenuView* GetSubmenu() const { return submenu_; } |
166 | 177 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 has_icons_ = has_icons; | 236 has_icons_ = has_icons; |
226 } | 237 } |
227 | 238 |
228 // Returns the descendant with the specified command. | 239 // Returns the descendant with the specified command. |
229 MenuItemView* GetMenuItemByID(int id); | 240 MenuItemView* GetMenuItemByID(int id); |
230 | 241 |
231 // Invoke if you remove/add children to the menu while it's showing. This | 242 // Invoke if you remove/add children to the menu while it's showing. This |
232 // recalculates the bounds. | 243 // recalculates the bounds. |
233 void ChildrenChanged(); | 244 void ChildrenChanged(); |
234 | 245 |
| 246 // Sizes any child views. |
| 247 virtual void Layout(); |
| 248 |
235 protected: | 249 protected: |
236 // Creates a MenuItemView. This is used by the various AddXXX methods. | 250 // Creates a MenuItemView. This is used by the various AddXXX methods. |
237 MenuItemView(MenuItemView* parent, int command, Type type); | 251 MenuItemView(MenuItemView* parent, int command, Type type); |
238 | 252 |
239 private: | 253 private: |
240 // Calculates all sizes that we can from the OS. | 254 // Calculates all sizes that we can from the OS. |
241 // | 255 // |
242 // This is invoked prior to Running a menu. | 256 // This is invoked prior to Running a menu. |
243 static void UpdateMenuPartSizes(bool has_icons); | 257 static void UpdateMenuPartSizes(bool has_icons); |
244 | 258 |
245 // Called by the two constructors to initialize this menu item. | 259 // Called by the two constructors to initialize this menu item. |
246 void Init(MenuItemView* parent, | 260 void Init(MenuItemView* parent, |
247 int command, | 261 int command, |
248 MenuItemView::Type type, | 262 MenuItemView::Type type, |
249 MenuDelegate* delegate); | 263 MenuDelegate* delegate); |
250 | 264 |
251 // All the AddXXX methods funnel into this. | |
252 MenuItemView* AppendMenuItemInternal(int item_id, | |
253 const std::wstring& label, | |
254 const SkBitmap& icon, | |
255 Type type); | |
256 | |
257 // Invoked by the MenuController when the menu closes as the result of | 265 // Invoked by the MenuController when the menu closes as the result of |
258 // drag and drop run. | 266 // drag and drop run. |
259 void DropMenuClosed(bool notify_delegate); | 267 void DropMenuClosed(bool notify_delegate); |
260 | 268 |
261 // The RunXXX methods call into this to set up the necessary state before | 269 // The RunXXX methods call into this to set up the necessary state before |
262 // running. | 270 // running. |
263 void PrepareForRun(bool has_mnemonics); | 271 void PrepareForRun(bool has_mnemonics); |
264 | 272 |
265 // Returns the flags passed to DrawStringInt. | 273 // Returns the flags passed to DrawStringInt. |
266 int GetDrawStringFlags(); | 274 int GetDrawStringFlags(); |
(...skipping 15 matching lines...) Expand all Loading... |
282 void Paint(gfx::Canvas* canvas, bool for_drag); | 290 void Paint(gfx::Canvas* canvas, bool for_drag); |
283 | 291 |
284 // Destroys the window used to display this menu and recursively destroys | 292 // Destroys the window used to display this menu and recursively destroys |
285 // the windows used to display all descendants. | 293 // the windows used to display all descendants. |
286 void DestroyAllMenuHosts(); | 294 void DestroyAllMenuHosts(); |
287 | 295 |
288 // Returns the various margins. | 296 // Returns the various margins. |
289 int GetTopMargin(); | 297 int GetTopMargin(); |
290 int GetBottomMargin(); | 298 int GetBottomMargin(); |
291 | 299 |
| 300 // Returns the preferred width (and padding) of any children. |
| 301 int GetChildPreferredWidth(); |
| 302 |
292 // The delegate. This is only valid for the root menu item. You shouldn't | 303 // The delegate. This is only valid for the root menu item. You shouldn't |
293 // use this directly, instead use GetDelegate() which walks the tree as | 304 // use this directly, instead use GetDelegate() which walks the tree as |
294 // as necessary. | 305 // as necessary. |
295 MenuDelegate* delegate_; | 306 MenuDelegate* delegate_; |
296 | 307 |
297 // Returns the controller for the run operation, or NULL if the menu isn't | 308 // Returns the controller for the run operation, or NULL if the menu isn't |
298 // showing. | 309 // showing. |
299 MenuController* controller_; | 310 MenuController* controller_; |
300 | 311 |
301 // Used to detect when Cancel was invoked. | 312 // Used to detect when Cancel was invoked. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 | 350 |
340 // Preferred height of menu items. Reset every time a menu is run. | 351 // Preferred height of menu items. Reset every time a menu is run. |
341 static int pref_menu_height_; | 352 static int pref_menu_height_; |
342 | 353 |
343 DISALLOW_COPY_AND_ASSIGN(MenuItemView); | 354 DISALLOW_COPY_AND_ASSIGN(MenuItemView); |
344 }; | 355 }; |
345 | 356 |
346 } // namespace views | 357 } // namespace views |
347 | 358 |
348 #endif // VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ | 359 #endif // VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ |
OLD | NEW |