Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 22 matching lines...) Expand all Loading... | |
| 33 | 33 |
| 34 namespace views { | 34 namespace views { |
| 35 | 35 |
| 36 class MenuButton; | 36 class MenuButton; |
| 37 class MenuController; | 37 class MenuController; |
| 38 class MenuDelegate; | 38 class MenuDelegate; |
| 39 class SubmenuView; | 39 class SubmenuView; |
| 40 | 40 |
| 41 struct MenuConfig; | 41 struct MenuConfig; |
| 42 | 42 |
| 43 // MenuItemInterface ----------------------------------------------------------- | |
| 44 | |
| 45 // Interface class for the menu items. | |
| 46 class MenuItemInterface { | |
| 47 public: | |
|
rhashimoto
2011/07/29 17:17:13
virtual destructor?
Nikita (slow)
2011/08/05 23:40:26
Obsolete, using ui::MenuModel now.
| |
| 48 // Append a sub-menu to this menu. | |
| 49 // The returned pointer is owned by this menu. | |
| 50 virtual MenuItemInterface* AppendSubMenu(int item_id, | |
| 51 const std::wstring& label) = 0; | |
|
rhashimoto
2011/07/29 17:17:13
nit: wrapped indentation needs to be adjusted in t
Nikita (slow)
2011/08/05 23:40:26
Obsolete, using ui::MenuModel now.
| |
| 52 | |
| 53 // Append a sub-menu with an icon to this menu. | |
| 54 // The returned pointer is owned by this menu. | |
| 55 virtual MenuItemInterface* AppendSubMenuWithIcon(int item_id, | |
| 56 const std::wstring& label, | |
| 57 const SkBitmap& icon) = 0; | |
| 58 | |
| 59 // This is a convenience for standard text label menu items where the label | |
| 60 // is provided with this call. | |
| 61 virtual MenuItemInterface* AppendMenuItemWithLabel(int item_id, | |
| 62 const std::wstring& label) = 0; | |
| 63 | |
| 64 | |
| 65 // Appends a menu item with an icon. This is for the menu item which | |
| 66 // needs an icon. Calling this function forces the Menu class to draw | |
| 67 // the menu, instead of relying on Windows. | |
| 68 virtual MenuItemInterface* AppendMenuItemWithIcon(int item_id, | |
| 69 const std::wstring& label, | |
| 70 const SkBitmap& icon) = 0; | |
| 71 | |
| 72 // Adds a separator to this menu | |
| 73 virtual void AppendSeparator() = 0; | |
| 74 | |
| 75 // Clears the sub-menu. | |
| 76 virtual void ClearSubmenu() = 0; | |
| 77 | |
| 78 // Called when update is done. | |
| 79 virtual void ChildrenChanged() = 0; | |
| 80 | |
| 81 // Hides and cancels the menu. This does nothing if the menu is not open. | |
| 82 virtual void Cancel() = 0; | |
| 83 | |
| 84 virtual void set_margins(int top_margin, int bottom_margin) = 0; | |
| 85 }; | |
| 86 | |
| 43 // MenuItemView -------------------------------------------------------------- | 87 // MenuItemView -------------------------------------------------------------- |
| 44 | 88 |
| 45 // MenuItemView represents a single menu item with a label and optional icon. | 89 // 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 | 90 // Each MenuItemView may also contain a submenu, which in turn may contain |
| 47 // any number of child MenuItemViews. | 91 // any number of child MenuItemViews. |
| 48 // | 92 // |
| 49 // To use a menu create an initial MenuItemView using the constructor that | 93 // 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 | 94 // takes a MenuDelegate, then create any number of child menu items by way |
| 51 // of the various AddXXX methods. | 95 // of the various AddXXX methods. |
| 52 // | 96 // |
| 53 // MenuItemView is itself a View, which means you can add Views to each | 97 // 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 | 98 // 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 | 99 // 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 | 100 // 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 | 101 // 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 | 102 // 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 | 103 // focus from the hosting window child views do not actually get focus. Instead |
| 60 // |SetHotTracked| is used as the user navigates around. | 104 // |SetHotTracked| is used as the user navigates around. |
| 61 // | 105 // |
| 62 // There are two ways to show a MenuItemView: | 106 // There are two ways to show a MenuItemView: |
| 63 // 1. Use RunMenuAt. This blocks the caller, executing the selected command | 107 // 1. Use RunMenuAt. This blocks the caller, executing the selected command |
| 64 // on success. | 108 // on success. |
| 65 // 2. Use RunMenuForDropAt. This is intended for use during a drop session | 109 // 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 | 110 // and does NOT block the caller. Instead the delegate is notified when the |
| 67 // menu closes via the DropMenuClosed method. | 111 // menu closes via the DropMenuClosed method. |
| 68 | 112 |
| 69 class VIEWS_API MenuItemView : public View { | 113 class VIEWS_API MenuItemView : public MenuItemInterface, public View { |
| 70 public: | 114 public: |
| 71 friend class MenuController; | 115 friend class MenuController; |
| 72 | 116 |
| 73 // The menu item view's class name. | 117 // The menu item view's class name. |
| 74 static const char kViewClassName[]; | 118 static const char kViewClassName[]; |
| 75 | 119 |
| 76 // ID used to identify menu items. | 120 // ID used to identify menu items. |
| 77 static const int kMenuItemViewID; | 121 static const int kMenuItemViewID; |
| 78 | 122 |
| 79 // ID used to identify empty menu items. | 123 // ID used to identify empty menu items. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 void RunMenuAt(Widget* parent, | 182 void RunMenuAt(Widget* parent, |
| 139 MenuButton* button, | 183 MenuButton* button, |
| 140 const gfx::Rect& bounds, | 184 const gfx::Rect& bounds, |
| 141 AnchorPosition anchor, | 185 AnchorPosition anchor, |
| 142 bool has_mnemonics); | 186 bool has_mnemonics); |
| 143 void RunMenuForDropAt(Widget* parent, | 187 void RunMenuForDropAt(Widget* parent, |
| 144 const gfx::Rect& bounds, | 188 const gfx::Rect& bounds, |
| 145 AnchorPosition anchor); | 189 AnchorPosition anchor); |
| 146 | 190 |
| 147 // Hides and cancels the menu. This does nothing if the menu is not open. | 191 // Hides and cancels the menu. This does nothing if the menu is not open. |
| 148 void Cancel(); | 192 virtual void Cancel(); |
|
rhashimoto
2011/07/29 17:17:13
group overridden methods, add OVERRIDE
Nikita (slow)
2011/08/05 23:40:26
Obsolete, using ui::MenuModel now.
| |
| 149 | 193 |
| 150 // Add an item to the menu at a specified index. ChildrenChanged() should | 194 // Add an item to the menu at a specified index. ChildrenChanged() should |
| 151 // called after adding menu items if the menu may be active. | 195 // called after adding menu items if the menu may be active. |
| 152 MenuItemView* AddMenuItemAt(int index, | 196 MenuItemView* AddMenuItemAt(int index, |
| 153 int item_id, | 197 int item_id, |
| 154 const std::wstring& label, | 198 const std::wstring& label, |
| 155 const SkBitmap& icon, | 199 const SkBitmap& icon, |
| 156 Type type); | 200 Type type); |
| 157 | 201 |
| 158 // Remove an item from the menu at a specified index. | 202 // Remove an item from the menu at a specified index. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 169 // label The text label shown. | 213 // label The text label shown. |
| 170 // type The type of item. | 214 // type The type of item. |
| 171 MenuItemView* AppendMenuItem(int item_id, | 215 MenuItemView* AppendMenuItem(int item_id, |
| 172 const std::wstring& label, | 216 const std::wstring& label, |
| 173 Type type) { | 217 Type type) { |
| 174 return AppendMenuItemImpl(item_id, label, SkBitmap(), type); | 218 return AppendMenuItemImpl(item_id, label, SkBitmap(), type); |
| 175 } | 219 } |
| 176 | 220 |
| 177 // Append a submenu to this menu. | 221 // Append a submenu to this menu. |
| 178 // The returned pointer is owned by this menu. | 222 // The returned pointer is owned by this menu. |
| 179 MenuItemView* AppendSubMenu(int item_id, | 223 virtual MenuItemInterface* AppendSubMenu(int item_id, |
| 180 const std::wstring& label) { | 224 const std::wstring& label) { |
| 181 return AppendMenuItemImpl(item_id, label, SkBitmap(), SUBMENU); | 225 return AppendMenuItemImpl(item_id, label, SkBitmap(), SUBMENU); |
| 182 } | 226 } |
| 183 | 227 |
| 184 // Append a submenu with an icon to this menu. | 228 // Append a submenu with an icon to this menu. |
| 185 // The returned pointer is owned by this menu. | 229 // The returned pointer is owned by this menu. |
| 186 MenuItemView* AppendSubMenuWithIcon(int item_id, | 230 virtual MenuItemInterface* AppendSubMenuWithIcon(int item_id, |
| 187 const std::wstring& label, | 231 const std::wstring& label, |
| 188 const SkBitmap& icon) { | 232 const SkBitmap& icon) { |
| 189 return AppendMenuItemImpl(item_id, label, icon, SUBMENU); | 233 return AppendMenuItemImpl(item_id, label, icon, SUBMENU); |
| 190 } | 234 } |
| 191 | 235 |
| 192 // This is a convenience for standard text label menu items where the label | 236 // This is a convenience for standard text label menu items where the label |
| 193 // is provided with this call. | 237 // is provided with this call. |
| 194 MenuItemView* AppendMenuItemWithLabel(int item_id, | 238 virtual MenuItemInterface* AppendMenuItemWithLabel(int item_id, |
| 195 const std::wstring& label) { | 239 const std::wstring& label) { |
| 196 return AppendMenuItem(item_id, label, NORMAL); | 240 return AppendMenuItem(item_id, label, NORMAL); |
| 197 } | 241 } |
| 198 | 242 |
| 199 // This is a convenience for text label menu items where the label is | 243 // This is a convenience for text label menu items where the label is |
| 200 // provided by the delegate. | 244 // provided by the delegate. |
| 201 MenuItemView* AppendDelegateMenuItem(int item_id) { | 245 MenuItemView* AppendDelegateMenuItem(int item_id) { |
| 202 return AppendMenuItem(item_id, std::wstring(), NORMAL); | 246 return AppendMenuItem(item_id, std::wstring(), NORMAL); |
| 203 } | 247 } |
| 204 | 248 |
| 205 // Adds a separator to this menu | 249 // Adds a separator to this menu |
| 206 void AppendSeparator() { | 250 virtual void AppendSeparator() { |
| 207 AppendMenuItemImpl(0, std::wstring(), SkBitmap(), SEPARATOR); | 251 AppendMenuItemImpl(0, std::wstring(), SkBitmap(), SEPARATOR); |
| 208 } | 252 } |
| 209 | 253 |
| 210 // Appends a menu item with an icon. This is for the menu item which | 254 // Appends a menu item with an icon. This is for the menu item which |
| 211 // needs an icon. Calling this function forces the Menu class to draw | 255 // needs an icon. Calling this function forces the Menu class to draw |
| 212 // the menu, instead of relying on Windows. | 256 // the menu, instead of relying on Windows. |
| 213 MenuItemView* AppendMenuItemWithIcon(int item_id, | 257 virtual MenuItemInterface* AppendMenuItemWithIcon(int item_id, |
| 214 const std::wstring& label, | 258 const std::wstring& label, |
| 215 const SkBitmap& icon) { | 259 const SkBitmap& icon) { |
| 216 return AppendMenuItemImpl(item_id, label, icon, NORMAL); | 260 return AppendMenuItemImpl(item_id, label, icon, NORMAL); |
| 217 } | 261 } |
| 218 | 262 |
| 219 // Creates a menu item for the specified entry in the model and appends it as | 263 // Creates a menu item for the specified entry in the model and appends it as |
| 220 // a child. |index| should be offset by GetFirstItemIndex() before calling | 264 // a child. |index| should be offset by GetFirstItemIndex() before calling |
| 221 // this function. | 265 // this function. |
| 222 MenuItemView* AppendMenuItemFromModel(ui::MenuModel* model, | 266 MenuItemView* AppendMenuItemFromModel(ui::MenuModel* model, |
| 223 int index, | 267 int index, |
| 224 int id); | 268 int id); |
| 225 | 269 |
| 226 // All the AppendXXX methods funnel into this. | 270 // All the AppendXXX methods funnel into this. |
| 227 MenuItemView* AppendMenuItemImpl(int item_id, | 271 MenuItemView* AppendMenuItemImpl(int item_id, |
| 228 const std::wstring& label, | 272 const std::wstring& label, |
| 229 const SkBitmap& icon, | 273 const SkBitmap& icon, |
| 230 Type type); | 274 Type type); |
| 231 | 275 |
| 232 // Returns the view that contains child menu items. If the submenu has | 276 // Returns the view that contains child menu items. If the submenu has |
| 233 // not been creates, this creates it. | 277 // not been creates, this creates it. |
| 234 virtual SubmenuView* CreateSubmenu(); | 278 virtual SubmenuView* CreateSubmenu(); |
| 235 | 279 |
| 236 // Returns true if this menu item has a submenu. | 280 // Returns true if this menu item has a submenu. |
| 237 virtual bool HasSubmenu() const; | 281 virtual bool HasSubmenu() const; |
| 238 | 282 |
| 283 virtual void ClearSubmenu(); | |
| 284 | |
| 239 // Returns the view containing child menu items. | 285 // Returns the view containing child menu items. |
| 240 virtual SubmenuView* GetSubmenu() const; | 286 virtual SubmenuView* GetSubmenu() const; |
| 241 | 287 |
| 242 // Returns the parent menu item. | 288 // Returns the parent menu item. |
| 243 MenuItemView* GetParentMenuItem() const { return parent_menu_item_; } | 289 MenuItemView* GetParentMenuItem() const { return parent_menu_item_; } |
| 244 | 290 |
| 245 // Sets the title | 291 // Sets the title |
| 246 void SetTitle(const std::wstring& title); | 292 void SetTitle(const std::wstring& title); |
| 247 | 293 |
| 248 // Returns the title. | 294 // Returns the title. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 // makes the menus slightly wider and taller. | 346 // makes the menus slightly wider and taller. |
| 301 void set_has_icons(bool has_icons) { | 347 void set_has_icons(bool has_icons) { |
| 302 has_icons_ = has_icons; | 348 has_icons_ = has_icons; |
| 303 } | 349 } |
| 304 | 350 |
| 305 // Returns the descendant with the specified command. | 351 // Returns the descendant with the specified command. |
| 306 MenuItemView* GetMenuItemByID(int id); | 352 MenuItemView* GetMenuItemByID(int id); |
| 307 | 353 |
| 308 // Invoke if you remove/add children to the menu while it's showing. This | 354 // Invoke if you remove/add children to the menu while it's showing. This |
| 309 // recalculates the bounds. | 355 // recalculates the bounds. |
| 310 void ChildrenChanged(); | 356 virtual void ChildrenChanged(); |
| 311 | 357 |
| 312 // Sizes any child views. | 358 // Sizes any child views. |
| 313 virtual void Layout(); | 359 virtual void Layout(); |
| 314 | 360 |
| 315 // Returns the amount of space needed to accomodate the accelerator. The | 361 // Returns the amount of space needed to accomodate the accelerator. The |
| 316 // space needed for the accelerator is NOT included in the preferred width. | 362 // space needed for the accelerator is NOT included in the preferred width. |
| 317 int GetAcceleratorTextWidth(); | 363 int GetAcceleratorTextWidth(); |
| 318 | 364 |
| 319 // Returns true if the menu has mnemonics. This only useful on the root menu | 365 // Returns true if the menu has mnemonics. This only useful on the root menu |
| 320 // item. | 366 // item. |
| 321 bool has_mnemonics() const { return has_mnemonics_; } | 367 bool has_mnemonics() const { return has_mnemonics_; } |
| 322 | 368 |
| 323 // Set top and bottom margins in pixels. If no margin is set or a | 369 // Set top and bottom margins in pixels. If no margin is set or a |
| 324 // negative margin is specified then MenuConfig values are used. | 370 // negative margin is specified then MenuConfig values are used. |
| 325 void set_margins(int top_margin, int bottom_margin) { | 371 virtual void set_margins(int top_margin, int bottom_margin) { |
| 326 top_margin_ = top_margin; | 372 top_margin_ = top_margin; |
| 327 bottom_margin_ = bottom_margin; | 373 bottom_margin_ = bottom_margin; |
| 328 } | 374 } |
| 329 | 375 |
| 330 // Set the position of the menu with respect to the bounds (top | 376 // Set the position of the menu with respect to the bounds (top |
| 331 // level only). | 377 // level only). |
| 332 void set_menu_position(MenuPosition menu_position) { | 378 void set_menu_position(MenuPosition menu_position) { |
| 333 requested_menu_position_ = menu_position; | 379 requested_menu_position_ = menu_position; |
| 334 } | 380 } |
| 335 | 381 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 493 // position of the menu being shown. | 539 // position of the menu being shown. |
| 494 MenuPosition requested_menu_position_; | 540 MenuPosition requested_menu_position_; |
| 495 MenuPosition actual_menu_position_; | 541 MenuPosition actual_menu_position_; |
| 496 | 542 |
| 497 DISALLOW_COPY_AND_ASSIGN(MenuItemView); | 543 DISALLOW_COPY_AND_ASSIGN(MenuItemView); |
| 498 }; | 544 }; |
| 499 | 545 |
| 500 } // namespace views | 546 } // namespace views |
| 501 | 547 |
| 502 #endif // VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ | 548 #endif // VIEWS_CONTROLS_MENU_MENU_ITEM_VIEW_H_ |
| OLD | NEW |