From 882e27ed5c8f54c2ac783594a07a78dbb9dc6c30 Mon Sep 17 00:00:00 2001 From: "rhashimoto@chromium.org" Date: Wed, 25 May 2011 19:41:35 +0000 Subject: Add MenuModelAdapter to wrap ui::MenuModel with views::MenuDelegate interface. - added ViewsDelegate::GetDispositionForEvent() pure virtual - added MenuModelAdapter - added unit test for MenuModelAdapter - added comment for MenuModel::GetFirstItemIndex() that callers may pass NULL for NativeMenu argument - fixed index offset bug in MenuModel::GetModelAndIndexForCommandId() - fixed spurious call to MenuDelegate::IsCommandEnabled() BUG=none TEST=included Review URL: http://codereview.chromium.org/7067032 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86677 0039d316-1c4b-4281-b951-d872f2087c98 --- ui/base/models/menu_model.cc | 12 +++++++----- ui/base/models/menu_model.h | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'ui') diff --git a/ui/base/models/menu_model.cc b/ui/base/models/menu_model.cc index c7defd9..1d90c36 100644 --- a/ui/base/models/menu_model.cc +++ b/ui/base/models/menu_model.cc @@ -16,17 +16,19 @@ bool MenuModel::IsVisibleAt(int index) const { bool MenuModel::GetModelAndIndexForCommandId(int command_id, MenuModel** model, int* index) { - int item_count = (*model)->GetItemCount(); + const int item_count = (*model)->GetItemCount(); + const int index_offset = (*model)->GetFirstItemIndex(NULL); for (int i = 0; i < item_count; ++i) { - if ((*model)->GetTypeAt(i) == TYPE_SUBMENU) { - MenuModel* submenu_model = (*model)->GetSubmenuModelAt(i); + const int candidate_index = i + index_offset; + if ((*model)->GetTypeAt(candidate_index) == TYPE_SUBMENU) { + MenuModel* submenu_model = (*model)->GetSubmenuModelAt(candidate_index); if (GetModelAndIndexForCommandId(command_id, &submenu_model, index)) { *model = submenu_model; return true; } } - if ((*model)->GetCommandIdAt(i) == command_id) { - *index = i; + if ((*model)->GetCommandIdAt(candidate_index) == command_id) { + *index = candidate_index; return true; } } diff --git a/ui/base/models/menu_model.h b/ui/base/models/menu_model.h index daca3da..a0c3627 100644 --- a/ui/base/models/menu_model.h +++ b/ui/base/models/menu_model.h @@ -44,7 +44,8 @@ class MenuModel { // Returns the index of the first item. This is 0 for most menus except the // system menu on Windows. |native_menu| is the menu to locate the start index - // within. It is guaranteed to be reset to a clean default state. + // within. It is guaranteed to be reset to a clean default state. Some + // callers of this method may pass NULL for native_menu. // IMPORTANT: If the model implementation returns something _other_ than 0 // here, it must offset the values for |index| it passes to the // methods below by this number - this is NOT done automatically! -- cgit v1.1