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