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
3// found in the LICENSE file.
4
5#ifndef UI_BASE_MODELS_BUTTON_MENU_ITEM_MODEL_H_
6#define UI_BASE_MODELS_BUTTON_MENU_ITEM_MODEL_H_
7
8#include <vector>
9
10#include "base/strings/string16.h"
11#include "ui/base/ui_base_export.h"
12
13namespace ui {
14
15// A model representing the rows of buttons that should be inserted in a button
16// containing menu item.
17class UI_BASE_EXPORT ButtonMenuItemModel {
18 public:
19  // Types of buttons.
20  enum ButtonType {
21    TYPE_SPACE,
22    TYPE_BUTTON,
23    TYPE_BUTTON_LABEL
24  };
25
26  class UI_BASE_EXPORT Delegate {
27   public:
28    // Some command ids have labels that change over time.
29    virtual bool IsItemForCommandIdDynamic(int command_id) const;
30    virtual base::string16 GetLabelForCommandId(int command_id) const;
31
32    // Performs the action associated with the specified command id.
33    virtual void ExecuteCommand(int command_id, int event_flags) = 0;
34    virtual bool IsCommandIdEnabled(int command_id) const;
35    virtual bool DoesCommandIdDismissMenu(int command_id) const;
36
37   protected:
38    virtual ~Delegate() {}
39  };
40
41  ButtonMenuItemModel(int string_id, ButtonMenuItemModel::Delegate* delegate);
42  ~ButtonMenuItemModel();
43
44  // Adds a button that will emit |command_id|. All buttons created through
45  // this method will have the same size, based on the largest button.
46  void AddGroupItemWithStringId(int command_id, int string_id);
47
48  // Adds a button that has an icon instead of a label.
49  void AddItemWithImage(int command_id, int icon_idr);
50
51  // Adds a non-clickable button with a desensitized label that doesn't do
52  // anything. Usually combined with IsItemForCommandIdDynamic() to add
53  // information.
54  void AddButtonLabel(int command_id, int string_id);
55
56  // Adds a small horizontal space.
57  void AddSpace();
58
59  // Returns the number of items for iteration.
60  int GetItemCount() const;
61
62  // Returns what kind of item is at |index|.
63  ButtonType GetTypeAt(int index) const;
64
65  // Changes a position into a command ID.
66  int GetCommandIdAt(int index) const;
67
68  // Whether the label for item |index| changes.
69  bool IsItemDynamicAt(int index) const;
70
71  // Returns the current label value for the button at |index|.
72  base::string16 GetLabelAt(int index) const;
73
74  // If the button at |index| should have an icon instead, returns true and
75  // sets the IDR |icon|.
76  bool GetIconAt(int index, int* icon) const;
77
78  // If the button at |index| should have its size equalized along with all
79  // other items that have their PartOfGroup bit set.
80  bool PartOfGroup(int index) const;
81
82  // Called from implementations.
83  void ActivatedCommand(int command_id);
84
85  // Returns the enabled state of the button at |index|.
86  bool IsEnabledAt(int index) const;
87
88  // Returns whether clicking on the button at |index| dismisses the menu.
89  bool DismissesMenuAt(int index) const;
90
91  // Returns the enabled state of the command specified by |command_id|.
92  bool IsCommandIdEnabled(int command_id) const;
93
94  // Returns whether clicking on |command_id| dismisses the menu.
95  bool DoesCommandIdDismissMenu(int command_id) const;
96
97  const base::string16& label() const { return item_label_; }
98
99 private:
100  // The non-clickable label to the left of the buttons.
101  base::string16 item_label_;
102
103  struct Item;
104  std::vector<Item> items_;
105
106  Delegate* delegate_;
107
108  DISALLOW_COPY_AND_ASSIGN(ButtonMenuItemModel);
109};
110
111}  // namespace ui
112
113#endif  // UI_BASE_MODELS_BUTTON_MENU_ITEM_MODEL_H_
114