diff options
author | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-04 17:50:26 +0000 |
---|---|---|
committer | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-04 17:50:26 +0000 |
commit | b284843ba6a1e90c12f91eaf796726433dc2537d (patch) | |
tree | 96edc5a4528e4d685c89544bfc13712e2984964a /views | |
parent | e83ce1e3f2fcacb3d400287d78567d22a2a06cae (diff) | |
download | chromium_src-b284843ba6a1e90c12f91eaf796726433dc2537d.zip chromium_src-b284843ba6a1e90c12f91eaf796726433dc2537d.tar.gz chromium_src-b284843ba6a1e90c12f91eaf796726433dc2537d.tar.bz2 |
Refactor the menu model to live outside views/ so it can be shared
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/465005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33827 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views')
-rw-r--r-- | views/accelerator.h | 54 | ||||
-rw-r--r-- | views/controls/button/button_dropdown.cc | 2 | ||||
-rw-r--r-- | views/controls/button/button_dropdown.h | 4 | ||||
-rw-r--r-- | views/controls/menu/menu_2.cc | 28 | ||||
-rw-r--r-- | views/controls/menu/menu_2.h | 97 | ||||
-rw-r--r-- | views/controls/menu/menu_delegate.h | 2 | ||||
-rw-r--r-- | views/controls/menu/native_menu_gtk.cc | 29 | ||||
-rw-r--r-- | views/controls/menu/native_menu_gtk.h | 10 | ||||
-rw-r--r-- | views/controls/menu/native_menu_win.cc | 18 | ||||
-rw-r--r-- | views/controls/menu/native_menu_win.h | 12 | ||||
-rw-r--r-- | views/controls/menu/simple_menu_model.cc | 142 | ||||
-rw-r--r-- | views/controls/menu/simple_menu_model.h | 107 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_win.cc | 7 | ||||
-rw-r--r-- | views/controls/textfield/native_textfield_win.h | 11 | ||||
-rw-r--r-- | views/views.gyp | 2 | ||||
-rw-r--r-- | views/widget/widget.h | 7 | ||||
-rw-r--r-- | views/widget/widget_gtk.cc | 2 | ||||
-rw-r--r-- | views/widget/widget_gtk.h | 2 | ||||
-rw-r--r-- | views/widget/widget_win.cc | 2 | ||||
-rw-r--r-- | views/widget/widget_win.h | 2 |
20 files changed, 67 insertions, 473 deletions
diff --git a/views/accelerator.h b/views/accelerator.h index 4aab592..a8d4c16 100644 --- a/views/accelerator.h +++ b/views/accelerator.h @@ -13,16 +13,16 @@ #include <string> -#include "base/keyboard_codes.h" +#include "app/menus/accelerator.h" #include "views/event.h" namespace views { -class Accelerator { +class Accelerator : public menus::Accelerator { public: Accelerator(base::KeyboardCode keycode, - bool shift_pressed, bool ctrl_pressed, bool alt_pressed) - : key_code_(keycode) { + bool shift_pressed, bool ctrl_pressed, bool alt_pressed) { + key_code_ = keycode; modifiers_ = 0; if (shift_pressed) modifiers_ |= Event::EF_SHIFT_DOWN; @@ -32,37 +32,6 @@ class Accelerator { modifiers_ |= Event::EF_ALT_DOWN; } - Accelerator(const Accelerator& accelerator) { - key_code_ = accelerator.key_code_; - modifiers_ = accelerator.modifiers_; - } - - ~Accelerator() { } - - Accelerator& operator=(const Accelerator& accelerator) { - if (this != &accelerator) { - key_code_ = accelerator.key_code_; - modifiers_ = accelerator.modifiers_; - } - return *this; - } - - // We define the < operator so that the KeyboardShortcut can be used as a key - // in a std::map. - bool operator <(const Accelerator& rhs) const { - if (key_code_ != rhs.key_code_) - return key_code_ < rhs.key_code_; - return modifiers_ < rhs.modifiers_; - } - - bool operator ==(const Accelerator& rhs) const { - return (key_code_ == rhs.key_code_) && (modifiers_ == rhs.modifiers_); - } - - bool operator !=(const Accelerator& rhs) const { - return !(*this == rhs); - } - bool IsShiftDown() const { return (modifiers_ & Event::EF_SHIFT_DOWN) == Event::EF_SHIFT_DOWN; } @@ -75,23 +44,8 @@ class Accelerator { return (modifiers_ & Event::EF_ALT_DOWN) == Event::EF_ALT_DOWN; } - base::KeyboardCode GetKeyCode() const { - return key_code_; - } - - int modifiers() const { - return modifiers_; - } - // Returns a string with the localized shortcut if any. std::wstring GetShortcutText() const; - - private: - // The window keycode (VK_...). - base::KeyboardCode key_code_; - - // The state of the Shift/Ctrl/Alt keys (see event.h). - int modifiers_; }; // An interface that classes that want to register for keyboard accelerators diff --git a/views/controls/button/button_dropdown.cc b/views/controls/button/button_dropdown.cc index 490eccf..a2740ca 100644 --- a/views/controls/button/button_dropdown.cc +++ b/views/controls/button/button_dropdown.cc @@ -23,7 +23,7 @@ static const int kMenuTimerDelay = 500; //////////////////////////////////////////////////////////////////////////////// ButtonDropDown::ButtonDropDown(ButtonListener* listener, - Menu2Model* model) + menus::MenuModel* model) : ImageButton(listener), model_(model), y_position_on_lbuttondown_(0), diff --git a/views/controls/button/button_dropdown.h b/views/controls/button/button_dropdown.h index bc1b595..118aebd 100644 --- a/views/controls/button/button_dropdown.h +++ b/views/controls/button/button_dropdown.h @@ -21,7 +21,7 @@ namespace views { //////////////////////////////////////////////////////////////////////////////// class ButtonDropDown : public ImageButton { public: - ButtonDropDown(ButtonListener* listener, Menu2Model* model); + ButtonDropDown(ButtonListener* listener, menus::MenuModel* model); virtual ~ButtonDropDown(); // Accessibility accessors, overridden from View. @@ -53,7 +53,7 @@ class ButtonDropDown : public ImageButton { void ShowDropDownMenu(gfx::NativeView window); // The model that populates the attached menu. - Menu2Model* model_; + menus::MenuModel* model_; scoped_ptr<Menu2> menu_; // Y position of mouse when left mouse button is pressed diff --git a/views/controls/menu/menu_2.cc b/views/controls/menu/menu_2.cc index 2a0ed56..8c9cddc 100644 --- a/views/controls/menu/menu_2.cc +++ b/views/controls/menu/menu_2.cc @@ -9,33 +9,7 @@ namespace views { -//////////////////////////////////////////////////////////////////////////////// -// Menu2Model, public: - -// static -bool Menu2Model::GetModelAndIndexForCommandId(int command_id, - Menu2Model** model, int* index) { - int item_count = (*model)->GetItemCount(); - for (int i = 0; i < item_count; ++i) { - if ((*model)->GetTypeAt(i) == TYPE_SUBMENU) { - Menu2Model* submenu_model = (*model)->GetSubmenuModelAt(i); - if (GetModelAndIndexForCommandId(command_id, &submenu_model, index)) { - *model = submenu_model; - return true; - } - } - if ((*model)->GetCommandIdAt(i) == command_id) { - *index = i; - return true; - } - } - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -// Menu2, public: - -Menu2::Menu2(Menu2Model* model) +Menu2::Menu2(menus::MenuModel* model) : model_(model), ALLOW_THIS_IN_INITIALIZER_LIST( wrapper_(MenuWrapper::CreateWrapper(this))) { diff --git a/views/controls/menu/menu_2.h b/views/controls/menu/menu_2.h index e91d5d3..2b9ea06 100644 --- a/views/controls/menu/menu_2.h +++ b/views/controls/menu/menu_2.h @@ -5,109 +5,20 @@ #ifndef CONTROLS_MENU_VIEWS_MENU_2_H_ #define CONTROLS_MENU_VIEWS_MENU_2_H_ -#include "app/gfx/native_widget_types.h" +#include "app/menus/menu_model.h" #include "base/scoped_ptr.h" -#include "base/string16.h" #include "views/controls/menu/menu_wrapper.h" namespace gfx { class Point; } -class SkBitmap; namespace views { -class Accelerator; -class Menu2; - -// The Menu2Model is an interface implemented by an object that provides the -// content of a menu. -class Menu2Model { - public: - virtual ~Menu2Model() {} - - // The type of item. - enum ItemType { - TYPE_COMMAND, - TYPE_CHECK, - TYPE_RADIO, - TYPE_SEPARATOR, - TYPE_SUBMENU - }; - - // Returns true if any of the items within the model have icons. Not all - // platforms support icons in menus natively and so this is a hint for - // triggering a custom rendering mode. - virtual bool HasIcons() const = 0; - - // 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. - // 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! - virtual int GetFirstItemIndex(gfx::NativeMenu native_menu) const { return 0; } - - // Returns the number of items in the menu. - virtual int GetItemCount() const = 0; - - // Returns the type of item at the specified index. - virtual ItemType GetTypeAt(int index) const = 0; - - // Returns the command id of the item at the specified index. - virtual int GetCommandIdAt(int index) const = 0; - - // Returns the label of the item at the specified index. - virtual string16 GetLabelAt(int index) const = 0; - - // Returns true if the label at the specified index can change over the course - // of the menu's lifetime. If this function returns true, the label of the - // menu item will be updated each time the menu is shown. - virtual bool IsLabelDynamicAt(int index) const = 0; - - // Gets the acclerator information for the specified index, returning true if - // there is a shortcut accelerator for the item, false otherwise. - virtual bool GetAcceleratorAt(int index, - views::Accelerator* accelerator) const = 0; - - // Returns the checked state of the item at the specified index. - virtual bool IsItemCheckedAt(int index) const = 0; - - // Returns the id of the group of radio items that the item at the specified - // index belongs to. - virtual int GetGroupIdAt(int index) const = 0; - - // Gets the icon for the item at the specified index, returning true if there - // is an icon, false otherwise. - virtual bool GetIconAt(int index, SkBitmap* icon) const = 0; - - // Returns the enabled state of the item at the specified index. - virtual bool IsEnabledAt(int index) const = 0; - - // Returns the model for the submenu at the specified index. - virtual Menu2Model* GetSubmenuModelAt(int index) const = 0; - - // Called when the highlighted menu item changes to the item at the specified - // index. - virtual void HighlightChangedTo(int index) = 0; - - // Called when the item at the specified index has been activated. - virtual void ActivatedAt(int index) = 0; - - // Called when the menu is about to be shown. - virtual void MenuWillShow() {} - - // Retrieves the model and index that contains a specific command id. Returns - // true if an item with the specified command id is found. |model| is inout, - // and specifies the model to start searching from. - static bool GetModelAndIndexForCommandId(int command_id, Menu2Model** model, - int* index); -}; - // A menu. Populated from a model, and relies on a delegate to execute commands. class Menu2 { public: - explicit Menu2(Menu2Model* model); + explicit Menu2(menus::MenuModel* model); virtual ~Menu2() {} // How the menu is aligned relative to the point it is shown at. @@ -137,10 +48,10 @@ class Menu2 { gfx::NativeMenu GetNativeMenu() const; // Accessors. - Menu2Model* model() const { return model_; } + menus::MenuModel* model() const { return model_; } private: - Menu2Model* model_; + menus::MenuModel* model_; // The object that actually implements the menu. scoped_ptr<MenuWrapper> wrapper_; diff --git a/views/controls/menu/menu_delegate.h b/views/controls/menu/menu_delegate.h index 2f7cd66..cb06f21 100644 --- a/views/controls/menu/menu_delegate.h +++ b/views/controls/menu/menu_delegate.h @@ -24,7 +24,7 @@ class MenuButton; // Delegate for a menu. This class is used as part of MenuItemView, see it // for details. -// TODO(sky): merge this with Menu2Model. +// TODO(sky): merge this with menus::MenuModel. class MenuDelegate : Controller { public: // Used during drag and drop to indicate where the drop indicator should diff --git a/views/controls/menu/native_menu_gtk.cc b/views/controls/menu/native_menu_gtk.cc index f3e303b..9c5b39a 100644 --- a/views/controls/menu/native_menu_gtk.cc +++ b/views/controls/menu/native_menu_gtk.cc @@ -8,6 +8,7 @@ #include <string> #include "app/gfx/gtk_util.h" +#include "app/menus/menu_model.h" #include "base/keyboard_codes.h" #include "base/message_loop.h" #include "base/string_util.h" @@ -48,10 +49,10 @@ std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { } // Returns true if the menu item type specified can be executed as a command. -bool MenuTypeCanExecute(views::Menu2Model::ItemType type) { - return type == views::Menu2Model::TYPE_COMMAND || - type == views::Menu2Model::TYPE_CHECK || - type == views::Menu2Model::TYPE_RADIO; +bool MenuTypeCanExecute(menus::MenuModel::ItemType type) { + return type == menus::MenuModel::TYPE_COMMAND || + type == menus::MenuModel::TYPE_CHECK || + type == menus::MenuModel::TYPE_RADIO; } } // namespace @@ -61,7 +62,7 @@ namespace views { //////////////////////////////////////////////////////////////////////////////// // NativeMenuGtk, public: -NativeMenuGtk::NativeMenuGtk(Menu2Model* model) +NativeMenuGtk::NativeMenuGtk(menus::MenuModel* model) : model_(model), menu_(NULL), menu_shown_(false), @@ -105,10 +106,10 @@ void NativeMenuGtk::Rebuild() { std::map<int, GtkRadioMenuItem*> radio_groups_; for (int i = 0; i < model_->GetItemCount(); ++i) { - Menu2Model::ItemType type = model_->GetTypeAt(i); - if (type == Menu2Model::TYPE_SEPARATOR) { + menus::MenuModel::ItemType type = model_->GetTypeAt(i); + if (type == menus::MenuModel::TYPE_SEPARATOR) { AddSeparatorAt(i); - } else if (type == Menu2Model::TYPE_RADIO) { + } else if (type == menus::MenuModel::TYPE_RADIO) { const int radio_group_id = model_->GetGroupIdAt(i); std::map<int, GtkRadioMenuItem*>::const_iterator iter = radio_groups_.find(radio_group_id); @@ -160,12 +161,12 @@ GtkWidget* NativeMenuGtk::AddMenuItemAt(int index, std::string label = ConvertAcceleratorsFromWindowsStyle(UTF16ToUTF8( model_->GetLabelAt(index))); - Menu2Model::ItemType type = model_->GetTypeAt(index); + menus::MenuModel::ItemType type = model_->GetTypeAt(index); switch (type) { - case Menu2Model::TYPE_CHECK: + case menus::MenuModel::TYPE_CHECK: menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str()); break; - case Menu2Model::TYPE_RADIO: + case menus::MenuModel::TYPE_RADIO: if (radio_group) { menu_item = gtk_radio_menu_item_new_with_mnemonic_from_widget( radio_group, label.c_str()); @@ -174,8 +175,8 @@ GtkWidget* NativeMenuGtk::AddMenuItemAt(int index, menu_item = gtk_radio_menu_item_new_with_mnemonic(NULL, label.c_str()); } break; - case Menu2Model::TYPE_SUBMENU: - case Menu2Model::TYPE_COMMAND: { + case menus::MenuModel::TYPE_SUBMENU: + case menus::MenuModel::TYPE_COMMAND: { SkBitmap icon; // Create menu item with icon if icon exists. if (model_->HasIcons() && model_->GetIconAt(index, &icon)) { @@ -193,7 +194,7 @@ GtkWidget* NativeMenuGtk::AddMenuItemAt(int index, break; } - if (type == Menu2Model::TYPE_SUBMENU) { + if (type == menus::MenuModel::TYPE_SUBMENU) { // TODO(beng): we're leaking these objects right now... consider some other // arrangement. Menu2* submenu = new Menu2(model_->GetSubmenuModelAt(index)); diff --git a/views/controls/menu/native_menu_gtk.h b/views/controls/menu/native_menu_gtk.h index b055f4f..e83995a 100644 --- a/views/controls/menu/native_menu_gtk.h +++ b/views/controls/menu/native_menu_gtk.h @@ -9,15 +9,17 @@ #include "views/controls/menu/menu_wrapper.h" -namespace views { +namespace menus { +class MenuModel; +} -class Menu2Model; +namespace views { // A Gtk implementation of MenuWrapper. // TODO(beng): rename to MenuGtk once the old class is dead. class NativeMenuGtk : public MenuWrapper { public: - explicit NativeMenuGtk(Menu2Model* model); + explicit NativeMenuGtk(menus::MenuModel* model); virtual ~NativeMenuGtk(); // Overridden from MenuWrapper: @@ -50,7 +52,7 @@ class NativeMenuGtk : public MenuWrapper { // Gtk signal handlers. static void CallActivate(GtkMenuItem* menu_item, NativeMenuGtk* native_menu); - Menu2Model* model_; + menus::MenuModel* model_; GtkWidget* menu_; diff --git a/views/controls/menu/native_menu_win.cc b/views/controls/menu/native_menu_win.cc index 0af8a9af..9553b73 100644 --- a/views/controls/menu/native_menu_win.cc +++ b/views/controls/menu/native_menu_win.cc @@ -122,7 +122,7 @@ class NativeMenuWin::MenuHostWindow { // Called when the user selects a specific item. void OnMenuCommand(int position, HMENU menu) { NativeMenuWin* intergoat = GetNativeMenuWinFromHMENU(menu); - Menu2Model* model = intergoat->model_; + menus::MenuModel* model = intergoat->model_; model->ActivatedAt(position); } @@ -300,7 +300,7 @@ const wchar_t* NativeMenuWin::MenuHostWindow::kMenuHostWindowKey = //////////////////////////////////////////////////////////////////////////////// // NativeMenuWin, public: -NativeMenuWin::NativeMenuWin(Menu2Model* model, HWND system_menu_for) +NativeMenuWin::NativeMenuWin(menus::MenuModel* model, HWND system_menu_for) : model_(model), menu_(NULL), owner_draw_(l10n_util::NeedOverrideDefaultUIFont(NULL, NULL) && @@ -339,7 +339,7 @@ void NativeMenuWin::Rebuild() { for (int menu_index = first_item_index_; menu_index < first_item_index_ + model_->GetItemCount(); ++menu_index) { int model_index = menu_index - first_item_index_; - if (model_->GetTypeAt(model_index) == Menu2Model::TYPE_SEPARATOR) + if (model_->GetTypeAt(model_index) == menus::MenuModel::TYPE_SEPARATOR) AddSeparatorItemAt(menu_index, model_index); else AddMenuItemAt(menu_index, model_index); @@ -390,13 +390,13 @@ void NativeMenuWin::AddMenuItemAt(int menu_index, int model_index) { ItemData* item_data = new ItemData; item_data->label = std::wstring(); - Menu2Model::ItemType type = model_->GetTypeAt(model_index); - if (type == Menu2Model::TYPE_SUBMENU) { + menus::MenuModel::ItemType type = model_->GetTypeAt(model_index); + if (type == menus::MenuModel::TYPE_SUBMENU) { item_data->submenu.reset(new Menu2(model_->GetSubmenuModelAt(model_index))); mii.fMask |= MIIM_SUBMENU; mii.hSubMenu = item_data->submenu->GetNativeMenu(); } else { - if (type == Menu2Model::TYPE_RADIO) + if (type == menus::MenuModel::TYPE_RADIO) mii.fType |= MFT_RADIOCHECK; mii.wID = model_->GetCommandIdAt(model_index); } @@ -456,8 +456,8 @@ void NativeMenuWin::UpdateMenuItemInfoForString( int model_index, const std::wstring& label) { std::wstring formatted = label; - Menu2Model::ItemType type = model_->GetTypeAt(model_index); - if (type != Menu2Model::TYPE_SUBMENU) { + menus::MenuModel::ItemType type = model_->GetTypeAt(model_index); + if (type != menus::MenuModel::TYPE_SUBMENU) { // Add accelerator details to the label if provided. views::Accelerator accelerator(base::VKEY_UNKNOWN, false, false, false); if (model_->GetAcceleratorAt(model_index, &accelerator)) { @@ -521,7 +521,7 @@ void NativeMenuWin::CreateHostWindow() { //////////////////////////////////////////////////////////////////////////////// // SystemMenuModel: -SystemMenuModel::SystemMenuModel(SimpleMenuModel::Delegate* delegate) +SystemMenuModel::SystemMenuModel(menus::SimpleMenuModel::Delegate* delegate) : SimpleMenuModel(delegate) { } diff --git a/views/controls/menu/native_menu_win.h b/views/controls/menu/native_menu_win.h index 428f624..ba0144e 100644 --- a/views/controls/menu/native_menu_win.h +++ b/views/controls/menu/native_menu_win.h @@ -7,14 +7,12 @@ #include <vector> +#include "app/menus/simple_menu_model.h" #include "base/scoped_ptr.h" #include "views/controls/menu/menu_wrapper.h" -#include "views/controls/menu/simple_menu_model.h" namespace views { -class Menu2Model; - // A Windows implementation of MenuWrapper. // TODO(beng): rename to MenuWin once the old class is dead. class NativeMenuWin : public MenuWrapper { @@ -22,7 +20,7 @@ class NativeMenuWin : public MenuWrapper { // Construct a NativeMenuWin, with a model and delegate. If |system_menu_for| // is non-NULL, the NativeMenuWin wraps the system menu for that window. // The caller owns the model and the delegate. - NativeMenuWin(Menu2Model* model, HWND system_menu_for); + NativeMenuWin(menus::MenuModel* model, HWND system_menu_for); virtual ~NativeMenuWin(); // Overridden from MenuWrapper: @@ -83,7 +81,7 @@ class NativeMenuWin : public MenuWrapper { void CreateHostWindow(); // Our attached model and delegate. - Menu2Model* model_; + menus::MenuModel* model_; HMENU menu_; @@ -113,12 +111,12 @@ class NativeMenuWin : public MenuWrapper { // A SimpleMenuModel subclass that allows the system menu for a window to be // wrapped. -class SystemMenuModel : public SimpleMenuModel { +class SystemMenuModel : public menus::SimpleMenuModel { public: explicit SystemMenuModel(Delegate* delegate); virtual ~SystemMenuModel(); - // Overridden from Menu2Model: + // Overridden from menus::MenuModel: virtual int GetFirstItemIndex(gfx::NativeMenu native_menu) const; protected: diff --git a/views/controls/menu/simple_menu_model.cc b/views/controls/menu/simple_menu_model.cc deleted file mode 100644 index 87b5de9..0000000 --- a/views/controls/menu/simple_menu_model.cc +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this -// source code is governed by a BSD-style license that can be found in the -// LICENSE file. - -#include "views/controls/menu/simple_menu_model.h" - -#include "app/l10n_util.h" - -namespace views { - -//////////////////////////////////////////////////////////////////////////////// -// SimpleMenuModel, public: - -SimpleMenuModel::SimpleMenuModel(Delegate* delegate) : delegate_(delegate) { -} - -SimpleMenuModel::~SimpleMenuModel() { -} - -void SimpleMenuModel::AddItem(int command_id, const string16& label) { - Item item = { command_id, label, TYPE_COMMAND, -1, NULL }; - items_.push_back(item); -} - -void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { - AddItem(command_id, l10n_util::GetStringUTF16(string_id)); -} - -void SimpleMenuModel::AddSeparator() { - Item item = { -1, string16(), TYPE_SEPARATOR, -1, NULL }; - items_.push_back(item); -} - -void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { - Item item = { command_id, label, TYPE_CHECK, -1, NULL }; - items_.push_back(item); -} - -void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { - AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); -} - -void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, - int group_id) { - Item item = { command_id, label, TYPE_RADIO, group_id, NULL }; - items_.push_back(item); -} - -void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, - int group_id) { - AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); -} - -void SimpleMenuModel::AddSubMenu(const string16& label, Menu2Model* model) { - Item item = { -1, label, TYPE_SUBMENU, -1, model }; - items_.push_back(item); -} - -void SimpleMenuModel::AddSubMenuWithStringId(int string_id, Menu2Model* model) { - AddSubMenu(l10n_util::GetStringUTF16(string_id), model); -} - -//////////////////////////////////////////////////////////////////////////////// -// SimpleMenuModel, Menu2Model implementation: - -bool SimpleMenuModel::HasIcons() const { - return false; -} - -int SimpleMenuModel::GetItemCount() const { - return static_cast<int>(items_.size()); -} - -Menu2Model::ItemType SimpleMenuModel::GetTypeAt(int index) const { - return items_.at(FlipIndex(index)).type; -} - -int SimpleMenuModel::GetCommandIdAt(int index) const { - return items_.at(FlipIndex(index)).command_id; -} - -string16 SimpleMenuModel::GetLabelAt(int index) const { - if (IsLabelDynamicAt(index)) - return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); - return items_.at(FlipIndex(index)).label; -} - -bool SimpleMenuModel::IsLabelDynamicAt(int index) const { - if (delegate_) - return delegate_->IsLabelForCommandIdDynamic(GetCommandIdAt(index)); - return false; -} - -bool SimpleMenuModel::GetAcceleratorAt(int index, - views::Accelerator* accelerator) const { - if (delegate_) { - return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), - accelerator); - } - return false; -} - -bool SimpleMenuModel::IsItemCheckedAt(int index) const { - if (!delegate_) - return false; - int item_index = FlipIndex(index); - Menu2Model::ItemType item_type = items_[item_index].type; - return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? - delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; -} - -int SimpleMenuModel::GetGroupIdAt(int index) const { - return items_.at(FlipIndex(index)).group_id; -} - -bool SimpleMenuModel::GetIconAt(int index, SkBitmap* icon) const { - return false; -} - -bool SimpleMenuModel::IsEnabledAt(int index) const { - int command_id = GetCommandIdAt(index); - // Submenus have a command id of -1, they should always be enabled. - if (!delegate_ || command_id == -1) - return true; - return delegate_->IsCommandIdEnabled(command_id); -} - -void SimpleMenuModel::HighlightChangedTo(int index) { - if (delegate_) - delegate_->CommandIdHighlighted(GetCommandIdAt(index)); -} - -void SimpleMenuModel::ActivatedAt(int index) { - if (delegate_) - delegate_->ExecuteCommand(GetCommandIdAt(index)); -} - -Menu2Model* SimpleMenuModel::GetSubmenuModelAt(int index) const { - return items_.at(FlipIndex(index)).submenu; -} - -} // namespace views diff --git a/views/controls/menu/simple_menu_model.h b/views/controls/menu/simple_menu_model.h deleted file mode 100644 index 2be2cbc..0000000 --- a/views/controls/menu/simple_menu_model.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this -// source code is governed by a BSD-style license that can be found in the -// LICENSE file. - -#ifndef CONTROLS_MENU_VIEWS_SIMPLE_MENU_MODEL_H_ -#define CONTROLS_MENU_VIEWS_SIMPLE_MENU_MODEL_H_ - -#include <vector> - -#include "base/string16.h" -#include "views/controls/menu/menu_2.h" - -namespace views { - -// A simple Menu2Model implementation with an imperative API for adding menu -// items. This makes it easy to construct fixed menus. Menus populated by -// dynamic data sources may be better off implementing Menu2Model directly. -// The breadth of Menu2Model is not exposed through this API. -class SimpleMenuModel : public Menu2Model { - public: - class Delegate { - public: - // Methods for determining the state of specific command ids. - virtual bool IsCommandIdChecked(int command_id) const = 0; - virtual bool IsCommandIdEnabled(int command_id) const = 0; - - // Gets the accelerator for the specified command id. Returns true if the - // command id has a valid accelerator, false otherwise. - virtual bool GetAcceleratorForCommandId( - int command_id, - views::Accelerator* accelerator) = 0; - - // Some command ids have labels that change over time. - virtual bool IsLabelForCommandIdDynamic(int command_id) const { - return false; - } - virtual string16 GetLabelForCommandId(int command_id) const { - return string16(); - } - - // Notifies the delegate that the item with the specified command id was - // visually highlighted within the menu. - virtual void CommandIdHighlighted(int command_id) {} - - // Performs the action associated with the specified command id. - virtual void ExecuteCommand(int command_id) = 0; - }; - - // The Delegate can be NULL, though if it is items can't be checked or - // disabled. - explicit SimpleMenuModel(Delegate* delegate); - virtual ~SimpleMenuModel(); - - // Methods for adding items to the model. - void AddItem(int command_id, const string16& label); - void AddItemWithStringId(int command_id, int string_id); - void AddSeparator(); - void AddCheckItem(int command_id, const string16& label); - void AddCheckItemWithStringId(int command_id, int string_id); - void AddRadioItem(int command_id, const string16& label, int group_id); - void AddRadioItemWithStringId(int command_id, int string_id, int group_id); - void AddSubMenu(const string16& label, Menu2Model* model); - void AddSubMenuWithStringId(int string_id, Menu2Model* model); - - // Overridden from Menu2Model: - virtual bool HasIcons() const; - virtual int GetItemCount() const; - virtual ItemType GetTypeAt(int index) const; - virtual int GetCommandIdAt(int index) const; - virtual string16 GetLabelAt(int index) const; - virtual bool IsLabelDynamicAt(int index) const; - virtual bool GetAcceleratorAt(int index, - views::Accelerator* accelerator) const; - virtual bool IsItemCheckedAt(int index) const; - virtual int GetGroupIdAt(int index) const; - virtual bool GetIconAt(int index, SkBitmap* icon) const; - virtual bool IsEnabledAt(int index) const; - virtual void HighlightChangedTo(int index); - virtual void ActivatedAt(int index); - virtual Menu2Model* GetSubmenuModelAt(int index) const; - - protected: - // Some variants of this model (SystemMenuModel) relies on items to be - // inserted backwards. This is counter-intuitive for the API, so rather than - // forcing customers to insert things backwards, we return the indices - // backwards instead. That's what this method is for. By default, it just - // returns what it's passed. - virtual int FlipIndex(int index) const { return index; } - - private: - struct Item { - int command_id; - string16 label; - ItemType type; - int group_id; - Menu2Model* submenu; - }; - std::vector<Item> items_; - - Delegate* delegate_; - - DISALLOW_COPY_AND_ASSIGN(SimpleMenuModel); -}; - -} // namespace views - -#endif // CONTROLS_MENU_VIEWS_SIMPLE_MENU_MODEL_H_ diff --git a/views/controls/textfield/native_textfield_win.cc b/views/controls/textfield/native_textfield_win.cc index cb7db37..bdf9eec 100644 --- a/views/controls/textfield/native_textfield_win.cc +++ b/views/controls/textfield/native_textfield_win.cc @@ -18,6 +18,7 @@ #include "grit/app_strings.h" #include "skia/ext/skia_utils_win.h" #include "views/controls/menu/menu_win.h" +#include "views/controls/menu/menu_2.h" #include "views/controls/native/native_view_host.h" #include "views/controls/textfield/textfield.h" #include "views/focus/focus_manager.h" @@ -239,7 +240,7 @@ gfx::NativeView NativeTextfieldWin::GetTestingHandle() const { } //////////////////////////////////////////////////////////////////////////////// -// NativeTextfieldWin, SimpleMenuModel::Delegate implementation: +// NativeTextfieldWin, menus::SimpleMenuModel::Delegate implementation: bool NativeTextfieldWin::IsCommandIdChecked(int command_id) const { return false; @@ -259,7 +260,7 @@ bool NativeTextfieldWin::IsCommandIdEnabled(int command_id) const { } bool NativeTextfieldWin::GetAcceleratorForCommandId(int command_id, - Accelerator* accelerator) { + menus::Accelerator* accelerator) { // The standard Ctrl-X, Ctrl-V and Ctrl-C are not defined as accelerators // anywhere so we need to check for them explicitly here. switch (command_id) { @@ -881,7 +882,7 @@ ITextDocument* NativeTextfieldWin::GetTextObjectModel() const { void NativeTextfieldWin::BuildContextMenu() { if (context_menu_contents_.get()) return; - context_menu_contents_.reset(new SimpleMenuModel(this)); + context_menu_contents_.reset(new menus::SimpleMenuModel(this)); context_menu_contents_->AddItemWithStringId(IDS_APP_UNDO, IDS_APP_UNDO); context_menu_contents_->AddSeparator(); context_menu_contents_->AddItemWithStringId(IDS_APP_CUT, IDS_APP_CUT); diff --git a/views/controls/textfield/native_textfield_win.h b/views/controls/textfield/native_textfield_win.h index a291a71..a5fb887 100644 --- a/views/controls/textfield/native_textfield_win.h +++ b/views/controls/textfield/native_textfield_win.h @@ -14,14 +14,15 @@ #include <vsstyle.h> #include "app/gfx/insets.h" +#include "app/menus/simple_menu_model.h" #include "base/scoped_comptr_win.h" -#include "views/controls/menu/simple_menu_model.h" #include "views/controls/textfield/native_textfield_wrapper.h" namespace views { class NativeViewHost; class Textfield; +class Menu2; static const int kDefaultEditStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; @@ -32,7 +33,7 @@ class NativeTextfieldWin CWinTraits<kDefaultEditStyle> >, public CRichEditCommands<NativeTextfieldWin>, public NativeTextfieldWrapper, - public SimpleMenuModel::Delegate { + public menus::SimpleMenuModel::Delegate { public: DECLARE_WND_CLASS(L"ViewsTextfieldEdit"); @@ -61,11 +62,11 @@ class NativeTextfieldWin virtual View* GetView(); virtual gfx::NativeView GetTestingHandle() const; - // Overridden from SimpleMenuModel::Delegate: + // Overridden from menus::SimpleMenuModel::Delegate: virtual bool IsCommandIdChecked(int command_id) const; virtual bool IsCommandIdEnabled(int command_id) const; virtual bool GetAcceleratorForCommandId(int command_id, - Accelerator* accelerator); + menus::Accelerator* accelerator); virtual void ExecuteCommand(int command_id); // CWindowImpl @@ -193,7 +194,7 @@ class NativeTextfieldWin static bool did_load_library_; // The contents of the context menu for the edit. - scoped_ptr<SimpleMenuModel> context_menu_contents_; + scoped_ptr<menus::SimpleMenuModel> context_menu_contents_; scoped_ptr<Menu2> context_menu_; // Border insets. diff --git a/views/views.gyp b/views/views.gyp index 5184423..ec2b7c80 100644 --- a/views/views.gyp +++ b/views/views.gyp @@ -133,8 +133,6 @@ 'controls/menu/native_menu_gtk.h', 'controls/menu/native_menu_win.cc', 'controls/menu/native_menu_win.h', - 'controls/menu/simple_menu_model.cc', - 'controls/menu/simple_menu_model.h', 'controls/menu/submenu_view.cc', 'controls/menu/submenu_view.h', 'controls/menu/view_menu_delegate.h', diff --git a/views/widget/widget.h b/views/widget/widget.h index 38a6703..8de4b6c 100644 --- a/views/widget/widget.h +++ b/views/widget/widget.h @@ -15,9 +15,12 @@ class Point; class Rect; } +namespace menus { +class Accelerator; +} + namespace views { -class Accelerator; class FocusManager; class RootView; class TooltipManager; @@ -155,7 +158,7 @@ class Widget { // Returns the accelerator given a command id. Returns false if there is // no accelerator associated with a given id, which is a common condition. virtual bool GetAccelerator(int cmd_id, - Accelerator* accelerator) = 0; + menus::Accelerator* accelerator) = 0; // Returns the Window containing this Widget, or NULL if not contained in a // window. diff --git a/views/widget/widget_gtk.cc b/views/widget/widget_gtk.cc index 02e7ae7..4cb54a5 100644 --- a/views/widget/widget_gtk.cc +++ b/views/widget/widget_gtk.cc @@ -519,7 +519,7 @@ TooltipManager* WidgetGtk::GetTooltipManager() { return tooltip_manager_.get(); } -bool WidgetGtk::GetAccelerator(int cmd_id, Accelerator* accelerator) { +bool WidgetGtk::GetAccelerator(int cmd_id, menus::Accelerator* accelerator) { NOTIMPLEMENTED(); return false; } diff --git a/views/widget/widget_gtk.h b/views/widget/widget_gtk.h index eab01c4..e748945 100644 --- a/views/widget/widget_gtk.h +++ b/views/widget/widget_gtk.h @@ -152,7 +152,7 @@ class WidgetGtk virtual void GenerateMousePressedForView(View* view, const gfx::Point& point); virtual TooltipManager* GetTooltipManager(); - virtual bool GetAccelerator(int cmd_id, Accelerator* accelerator); + virtual bool GetAccelerator(int cmd_id, menus::Accelerator* accelerator); virtual Window* GetWindow(); virtual const Window* GetWindow() const; virtual void SetNativeWindowProperty(const std::wstring& name, diff --git a/views/widget/widget_win.cc b/views/widget/widget_win.cc index 892d47f..a2075c1 100644 --- a/views/widget/widget_win.cc +++ b/views/widget/widget_win.cc @@ -320,7 +320,7 @@ void WidgetWin::GenerateMousePressedForView(View* view, ProcessMousePressed(point_in_widget.ToPOINT(), MK_LBUTTON, false, false); } -bool WidgetWin::GetAccelerator(int cmd_id, Accelerator* accelerator) { +bool WidgetWin::GetAccelerator(int cmd_id, menus::Accelerator* accelerator) { return false; } diff --git a/views/widget/widget_win.h b/views/widget/widget_win.h index 43c4357..28a3782 100644 --- a/views/widget/widget_win.h +++ b/views/widget/widget_win.h @@ -196,7 +196,7 @@ class WidgetWin : public app::WindowImpl, virtual TooltipManager* GetTooltipManager(); virtual void GenerateMousePressedForView(View* view, const gfx::Point& point); - virtual bool GetAccelerator(int cmd_id, Accelerator* accelerator); + virtual bool GetAccelerator(int cmd_id, menus::Accelerator* accelerator); virtual Window* GetWindow(); virtual const Window* GetWindow() const; virtual void SetNativeWindowProperty(const std::wstring& name, |