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 /app/menus/simple_menu_model.h | |
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 'app/menus/simple_menu_model.h')
-rw-r--r-- | app/menus/simple_menu_model.h | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/app/menus/simple_menu_model.h b/app/menus/simple_menu_model.h new file mode 100644 index 0000000..7d73703 --- /dev/null +++ b/app/menus/simple_menu_model.h @@ -0,0 +1,107 @@ +// 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 APP_MENUS_SIMPLE_MENU_MODEL_H_ +#define APP_MENUS_SIMPLE_MENU_MODEL_H_ + +#include <vector> + +#include "base/string16.h" +#include "app/menus/menu_model.h" + +namespace menus { + +// A simple MenuModel 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 MenuModel directly. +// The breadth of MenuModel is not exposed through this API. +class SimpleMenuModel : public MenuModel { + 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, + menus::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, MenuModel* model); + void AddSubMenuWithStringId(int string_id, MenuModel* model); + + // Overridden from MenuModel: + 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, + menus::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 MenuModel* 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; + MenuModel* submenu; + }; + std::vector<Item> items_; + + Delegate* delegate_; + + DISALLOW_COPY_AND_ASSIGN(SimpleMenuModel); +}; + +} // namespace menus + +#endif // APP_MENUS_SIMPLE_MENU_MODEL_H_ |