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