Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: views/controls/menu/menu_item_view.h

Issue 7720012: Moves ownership of MenuItemView to MenuRunner as well as responbility (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit test Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 15 matching lines...) Expand all
26 namespace gfx { 26 namespace gfx {
27 class Font; 27 class Font;
28 } 28 }
29 29
30 namespace ui { 30 namespace ui {
31 class MenuModel; 31 class MenuModel;
32 } 32 }
33 33
34 namespace views { 34 namespace views {
35 35
36 namespace internal {
37 class MenuRunnerImpl;
38 }
39
36 class MenuButton; 40 class MenuButton;
41 struct MenuConfig;
37 class MenuController; 42 class MenuController;
38 class MenuDelegate; 43 class MenuDelegate;
39 class SubmenuView; 44 class SubmenuView;
40 45
41 struct MenuConfig;
42
43 // MenuItemView -------------------------------------------------------------- 46 // MenuItemView --------------------------------------------------------------
44 47
45 // MenuItemView represents a single menu item with a label and optional icon. 48 // MenuItemView represents a single menu item with a label and optional icon.
46 // Each MenuItemView may also contain a submenu, which in turn may contain 49 // Each MenuItemView may also contain a submenu, which in turn may contain
47 // any number of child MenuItemViews. 50 // any number of child MenuItemViews.
48 // 51 //
49 // To use a menu create an initial MenuItemView using the constructor that 52 // To use a menu create an initial MenuItemView using the constructor that
50 // takes a MenuDelegate, then create any number of child menu items by way 53 // takes a MenuDelegate, then create any number of child menu items by way
51 // of the various AddXXX methods. 54 // of the various AddXXX methods.
52 // 55 //
53 // MenuItemView is itself a View, which means you can add Views to each 56 // MenuItemView is itself a View, which means you can add Views to each
54 // MenuItemView. This is normally NOT want you want, rather add other child 57 // MenuItemView. This is normally NOT want you want, rather add other child
55 // Views to the submenu of the MenuItemView. Any child views of the MenuItemView 58 // Views to the submenu of the MenuItemView. Any child views of the MenuItemView
56 // that are focusable can be navigated to by way of the up/down arrow and can be 59 // that are focusable can be navigated to by way of the up/down arrow and can be
57 // activated by way of space/return keys. Activating a focusable child results 60 // activated by way of space/return keys. Activating a focusable child results
58 // in |AcceleratorPressed| being invoked. Note, that as menus try not to steal 61 // in |AcceleratorPressed| being invoked. Note, that as menus try not to steal
59 // focus from the hosting window child views do not actually get focus. Instead 62 // focus from the hosting window child views do not actually get focus. Instead
60 // |SetHotTracked| is used as the user navigates around. 63 // |SetHotTracked| is used as the user navigates around.
61 // 64 //
62 // There are two ways to show a MenuItemView: 65 // To show the menu use MenuRunner. See MenuRunner for details on how to run
63 // 1. Use RunMenuAt. This blocks the caller, executing the selected command 66 // (show) the menu as well as for details on the life time of the menu.
64 // on success.
65 // 2. Use RunMenuForDropAt. This is intended for use during a drop session
66 // and does NOT block the caller. Instead the delegate is notified when the
67 // menu closes via the DropMenuClosed method.
68 67
69 class VIEWS_EXPORT MenuItemView : public View { 68 class VIEWS_EXPORT MenuItemView : public View {
70 public: 69 public:
71 friend class MenuController; 70 friend class MenuController;
72 71
73 // The menu item view's class name. 72 // The menu item view's class name.
74 static const char kViewClassName[]; 73 static const char kViewClassName[];
75 74
76 // ID used to identify menu items. 75 // ID used to identify menu items.
77 static const int kMenuItemViewID; 76 static const int kMenuItemViewID;
(...skipping 26 matching lines...) Expand all
104 enum MenuPosition { 103 enum MenuPosition {
105 POSITION_BEST_FIT, 104 POSITION_BEST_FIT,
106 POSITION_ABOVE_BOUNDS, 105 POSITION_ABOVE_BOUNDS,
107 POSITION_BELOW_BOUNDS 106 POSITION_BELOW_BOUNDS
108 }; 107 };
109 108
110 // Constructor for use with the top level menu item. This menu is never 109 // Constructor for use with the top level menu item. This menu is never
111 // shown to the user, rather its use as the parent for all menu items. 110 // shown to the user, rather its use as the parent for all menu items.
112 explicit MenuItemView(MenuDelegate* delegate); 111 explicit MenuItemView(MenuDelegate* delegate);
113 112
114 virtual ~MenuItemView();
115
116 // Overridden from View: 113 // Overridden from View:
117 virtual bool GetTooltipText(const gfx::Point& p, 114 virtual bool GetTooltipText(const gfx::Point& p,
118 std::wstring* tooltip) OVERRIDE; 115 std::wstring* tooltip) OVERRIDE;
119 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 116 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
120 117
121 // Returns the preferred height of menu items. This is only valid when the 118 // Returns the preferred height of menu items. This is only valid when the
122 // menu is about to be shown. 119 // menu is about to be shown.
123 static int pref_menu_height() { return pref_menu_height_; } 120 static int pref_menu_height() { return pref_menu_height_; }
124 121
125 // X-coordinate of where the label starts. 122 // X-coordinate of where the label starts.
126 static int label_start() { return label_start_; } 123 static int label_start() { return label_start_; }
127 124
128 // Returns the accessible name to be used with screen readers. Mnemonics are 125 // Returns the accessible name to be used with screen readers. Mnemonics are
129 // removed and the menu item accelerator text is appended. 126 // removed and the menu item accelerator text is appended.
130 static string16 GetAccessibleNameForMenuItem( 127 static string16 GetAccessibleNameForMenuItem(
131 const string16& item_text, const string16& accelerator_text); 128 const string16& item_text, const string16& accelerator_text);
132 129
133 // Run methods. See description above class for details. Both Run methods take
134 // a rectangle, which is used to position the menu. |has_mnemonics| indicates
135 // whether the items have mnemonics. Mnemonics are identified by way of the
136 // character following the '&'. The anchor position is specified for non-RTL
137 // languages; the opposite value will be used for RTL.
138 void RunMenuAt(Widget* parent,
139 MenuButton* button,
140 const gfx::Rect& bounds,
141 AnchorPosition anchor,
142 bool has_mnemonics);
143 void RunMenuForDropAt(Widget* parent,
144 const gfx::Rect& bounds,
145 AnchorPosition anchor);
146
147 // Hides and cancels the menu. This does nothing if the menu is not open. 130 // Hides and cancels the menu. This does nothing if the menu is not open.
148 void Cancel(); 131 void Cancel();
149 132
150 // Add an item to the menu at a specified index. ChildrenChanged() should 133 // Add an item to the menu at a specified index. ChildrenChanged() should
151 // called after adding menu items if the menu may be active. 134 // called after adding menu items if the menu may be active.
152 MenuItemView* AddMenuItemAt(int index, 135 MenuItemView* AddMenuItemAt(int index,
153 int item_id, 136 int item_id,
154 const std::wstring& label, 137 const std::wstring& label,
155 const SkBitmap& icon, 138 const SkBitmap& icon,
156 Type type); 139 Type type);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 virtual void OnPaint(gfx::Canvas* canvas); 264 virtual void OnPaint(gfx::Canvas* canvas);
282 265
283 // Returns the preferred size of this item. 266 // Returns the preferred size of this item.
284 virtual gfx::Size GetPreferredSize(); 267 virtual gfx::Size GetPreferredSize();
285 268
286 // Returns the object responsible for controlling showing the menu. 269 // Returns the object responsible for controlling showing the menu.
287 MenuController* GetMenuController(); 270 MenuController* GetMenuController();
288 271
289 // Returns the delegate. This returns the delegate of the root menu item. 272 // Returns the delegate. This returns the delegate of the root menu item.
290 MenuDelegate* GetDelegate(); 273 MenuDelegate* GetDelegate();
274 void set_delegate(MenuDelegate* delegate) { delegate_ = delegate; }
291 275
292 // Returns the root parent, or this if this has no parent. 276 // Returns the root parent, or this if this has no parent.
293 MenuItemView* GetRootMenuItem(); 277 MenuItemView* GetRootMenuItem();
294 278
295 // Returns the mnemonic for this MenuItemView, or 0 if this MenuItemView 279 // Returns the mnemonic for this MenuItemView, or 0 if this MenuItemView
296 // doesn't have a mnemonic. 280 // doesn't have a mnemonic.
297 char16 GetMnemonic(); 281 char16 GetMnemonic();
298 282
299 // Do we have icons? This only has effect on the top menu. Turning this on 283 // Do we have icons? This only has effect on the top menu. Turning this on
300 // makes the menus slightly wider and taller. 284 // makes the menus slightly wider and taller.
(...skipping 26 matching lines...) Expand all
327 // Set the position of the menu with respect to the bounds (top 311 // Set the position of the menu with respect to the bounds (top
328 // level only). 312 // level only).
329 void set_menu_position(MenuPosition menu_position) { 313 void set_menu_position(MenuPosition menu_position) {
330 requested_menu_position_ = menu_position; 314 requested_menu_position_ = menu_position;
331 } 315 }
332 316
333 protected: 317 protected:
334 // Creates a MenuItemView. This is used by the various AddXXX methods. 318 // Creates a MenuItemView. This is used by the various AddXXX methods.
335 MenuItemView(MenuItemView* parent, int command, Type type); 319 MenuItemView(MenuItemView* parent, int command, Type type);
336 320
321 // MenuRunner owns MenuItemView and should be the only one deleting it.
322 virtual ~MenuItemView();
323
337 virtual void ChildPreferredSizeChanged(View* child) OVERRIDE; 324 virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
338 325
339 virtual std::string GetClassName() const OVERRIDE; 326 virtual std::string GetClassName() const OVERRIDE;
340 327
341 private: 328 private:
329 friend class internal::MenuRunnerImpl; // For access to ~MenuItemView.
330
342 // Calculates all sizes that we can from the OS. 331 // Calculates all sizes that we can from the OS.
343 // 332 //
344 // This is invoked prior to Running a menu. 333 // This is invoked prior to Running a menu.
345 static void UpdateMenuPartSizes(bool has_icons); 334 static void UpdateMenuPartSizes(bool has_icons);
346 335
347 // Called by the two constructors to initialize this menu item. 336 // Called by the two constructors to initialize this menu item.
348 void Init(MenuItemView* parent, 337 void Init(MenuItemView* parent,
349 int command, 338 int command,
350 MenuItemView::Type type, 339 MenuItemView::Type type,
351 MenuDelegate* delegate); 340 MenuDelegate* delegate);
352 341
353 // Invoked by the MenuController when the menu closes as the result of
354 // drag and drop run.
355 void DropMenuClosed(bool notify_delegate);
356
357 // The RunXXX methods call into this to set up the necessary state before 342 // The RunXXX methods call into this to set up the necessary state before
358 // running. 343 // running.
359 void PrepareForRun(bool has_mnemonics, bool show_mnemonics); 344 void PrepareForRun(bool has_mnemonics, bool show_mnemonics);
360 345
361 // Returns the flags passed to DrawStringInt. 346 // Returns the flags passed to DrawStringInt.
362 int GetDrawStringFlags(); 347 int GetDrawStringFlags();
363 348
364 // Returns the font to use for menu text. 349 // Returns the font to use for menu text.
365 const gfx::Font& GetFont(); 350 const gfx::Font& GetFont();
366 351
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 // Calculates the preferred size. 396 // Calculates the preferred size.
412 gfx::Size CalculatePreferredSize(); 397 gfx::Size CalculatePreferredSize();
413 398
414 // Used by MenuController to cache the menu position in use by the 399 // Used by MenuController to cache the menu position in use by the
415 // active menu. 400 // active menu.
416 MenuPosition actual_menu_position() const { return actual_menu_position_; } 401 MenuPosition actual_menu_position() const { return actual_menu_position_; }
417 void set_actual_menu_position(MenuPosition actual_menu_position) { 402 void set_actual_menu_position(MenuPosition actual_menu_position) {
418 actual_menu_position_ = actual_menu_position; 403 actual_menu_position_ = actual_menu_position;
419 } 404 }
420 405
406 void set_controller(MenuController* controller) { controller_ = controller; }
407
421 // The delegate. This is only valid for the root menu item. You shouldn't 408 // The delegate. This is only valid for the root menu item. You shouldn't
422 // use this directly, instead use GetDelegate() which walks the tree as 409 // use this directly, instead use GetDelegate() which walks the tree as
423 // as necessary. 410 // as necessary.
424 MenuDelegate* delegate_; 411 MenuDelegate* delegate_;
425 412
426 // Returns the controller for the run operation, or NULL if the menu isn't 413 // The controller for the run operation, or NULL if the menu isn't showing.
427 // showing.
428 MenuController* controller_; 414 MenuController* controller_;
429 415
430 // Used to detect when Cancel was invoked. 416 // Used to detect when Cancel was invoked.
431 bool canceled_; 417 bool canceled_;
432 418
433 // Our parent. 419 // Our parent.
434 MenuItemView* parent_menu_item_; 420 MenuItemView* parent_menu_item_;
435 421
436 // Type of menu. NOTE: MenuItemView doesn't itself represent SEPARATOR, 422 // Type of menu. NOTE: MenuItemView doesn't itself represent SEPARATOR,
437 // that is handled by an entirely different view class. 423 // that is handled by an entirely different view class.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 // position of the menu being shown. 478 // position of the menu being shown.
493 MenuPosition requested_menu_position_; 479 MenuPosition requested_menu_position_;
494 MenuPosition actual_menu_position_; 480 MenuPosition actual_menu_position_;
495 481
496 DISALLOW_COPY_AND_ASSIGN(MenuItemView); 482 DISALLOW_COPY_AND_ASSIGN(MenuItemView);
497 }; 483 };
498 484
499 } // namespace views 485 } // namespace views
500 486
501 #endif // VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ 487 #endif // VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698