autofill_dialog_models.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright (c) 2012 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 CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_MODELS_H_
6#define CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_MODELS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/strings/string16.h"
14#include "ui/base/models/combobox_model.h"
15#include "ui/base/models/simple_menu_model.h"
16
17namespace autofill {
18
19class SuggestionsMenuModel;
20
21class SuggestionsMenuModelDelegate {
22 public:
23  virtual ~SuggestionsMenuModelDelegate();
24
25  // Called when a suggestions menu is about to show.
26  virtual void SuggestionsMenuWillShow() = 0;
27
28  // Called when a menu item has been activated.
29  virtual void SuggestionItemSelected(SuggestionsMenuModel* model,
30                                      size_t index) = 0;
31};
32
33// A model for the dropdowns that allow the user to select from different
34// sets of known data. It wraps a SimpleMenuModel, providing a mapping between
35// index and item GUID.
36class SuggestionsMenuModel : public ui::SimpleMenuModel,
37                             public ui::SimpleMenuModel::Delegate {
38 public:
39  explicit SuggestionsMenuModel(SuggestionsMenuModelDelegate* delegate);
40  virtual ~SuggestionsMenuModel();
41
42  // Adds an item and its identifying key to the model. Keys needn't be unique.
43  void AddKeyedItem(const std::string& key,
44                    const base::string16& display_label);
45
46  // As above, but also accepts an image which will be displayed alongside the
47  // text.
48  void AddKeyedItemWithIcon(const std::string& key,
49                            const base::string16& display_label,
50                            const gfx::Image& icon);
51
52  // Adds a label with a minor text and its identifying key to the model.
53  // Keys needn't be unique.
54  void AddKeyedItemWithMinorText(const std::string& key,
55                                const base::string16& display_label,
56                                const base::string16& display_minor_text);
57
58  // As above, but also accepts an image which will be displayed alongside the
59  // text.
60  void AddKeyedItemWithMinorTextAndIcon(
61      const std::string& key,
62      const base::string16& display_label,
63      const base::string16& display_minor_text,
64      const gfx::Image& icon);
65
66  // Resets the model to empty.
67  void Reset();
68
69  // Returns the ID key for the item at |index|.
70  std::string GetItemKeyAt(int index) const;
71
72  // Returns the ID key for the item at |checked_item_|, or an empty string if
73  // there are no items.
74  std::string GetItemKeyForCheckedItem() const;
75
76  // Sets which item is checked.
77  void SetCheckedItem(const std::string& item_key);
78  void SetCheckedIndex(size_t index);
79
80  int checked_item() const { return checked_item_; }
81
82  // Enable/disable an item by key.
83  void SetEnabled(const std::string& item_key, bool enabled);
84
85  // ui::SimpleMenuModel implementation.
86  virtual void MenuWillShow() OVERRIDE;
87
88  // ui::SimpleMenuModel::Delegate implementation.
89  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
90  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
91  virtual bool GetAcceleratorForCommandId(
92      int command_id,
93      ui::Accelerator* accelerator) OVERRIDE;
94  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
95  virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE;
96
97 private:
98  // Represents an item in this model.
99  struct Item {
100    std::string key;  //  The key of the item.
101    bool enabled;  // Whether the item is selectable.
102  };
103  // The items this model represents in presentation order.
104  // Note: the index in this vector is the |command_id| of the item.
105  std::vector<Item> items_;
106
107  // Returns the command id (and index) of the item by the |key|.
108  size_t GetItemIndex(const std::string& item_key);
109
110  SuggestionsMenuModelDelegate* delegate_;
111
112  // The command id (and index) of the item which is currently checked. Only one
113  // item is checked at a time.
114  int checked_item_;
115
116  DISALLOW_COPY_AND_ASSIGN(SuggestionsMenuModel);
117};
118
119// A model for possible months in the Gregorian calendar.
120class MonthComboboxModel : public ui::ComboboxModel {
121 public:
122  MonthComboboxModel();
123  virtual ~MonthComboboxModel();
124
125  static base::string16 FormatMonth(int index);
126
127  // ui::Combobox implementation:
128  virtual int GetItemCount() const OVERRIDE;
129  virtual base::string16 GetItemAt(int index) OVERRIDE;
130
131 private:
132  DISALLOW_COPY_AND_ASSIGN(MonthComboboxModel);
133};
134
135// A model for years between now and a decade hence.
136class YearComboboxModel : public ui::ComboboxModel {
137 public:
138  YearComboboxModel();
139  virtual ~YearComboboxModel();
140
141  // ui::Combobox implementation:
142  virtual int GetItemCount() const OVERRIDE;
143  virtual base::string16 GetItemAt(int index) OVERRIDE;
144
145 private:
146  // The current year (e.g., 2012).
147  int this_year_;
148
149  DISALLOW_COPY_AND_ASSIGN(YearComboboxModel);
150};
151
152}  // namespace autofill
153
154#endif  // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_MODELS_H_
155