Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / autofill_dialog_models.h
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
17 namespace autofill {
18
19 class SuggestionsMenuModel;
20
21 class 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.
33 class SuggestionsMenuModel : public ui::SimpleMenuModel,
34                              public ui::SimpleMenuModel::Delegate {
35  public:
36   explicit SuggestionsMenuModel(SuggestionsMenuModelDelegate* delegate);
37   ~SuggestionsMenuModel() override;
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   bool IsCommandIdChecked(int command_id) const override;
84   bool IsCommandIdEnabled(int command_id) const override;
85   bool GetAcceleratorForCommandId(int command_id,
86                                   ui::Accelerator* accelerator) override;
87   void ExecuteCommand(int command_id, int event_flags) override;
88
89  private:
90   // Represents an item in this model.
91   struct Item {
92     std::string key;  //  The key of the item.
93     bool enabled;  // Whether the item is selectable.
94   };
95   // The items this model represents in presentation order.
96   // Note: the index in this vector is the |command_id| of the item.
97   std::vector<Item> items_;
98
99   // Returns the command id (and index) of the item by the |key|.
100   size_t GetItemIndex(const std::string& item_key);
101
102   SuggestionsMenuModelDelegate* delegate_;
103
104   // The command id (and index) of the item which is currently checked. Only one
105   // item is checked at a time.
106   int checked_item_;
107
108   DISALLOW_COPY_AND_ASSIGN(SuggestionsMenuModel);
109 };
110
111 // A model for possible months in the Gregorian calendar.
112 class MonthComboboxModel : public ui::ComboboxModel {
113  public:
114   MonthComboboxModel();
115   ~MonthComboboxModel() override;
116
117   static base::string16 FormatMonth(int index);
118
119   // ui::Combobox implementation:
120   int GetItemCount() const override;
121   base::string16 GetItemAt(int index) override;
122
123  private:
124   DISALLOW_COPY_AND_ASSIGN(MonthComboboxModel);
125 };
126
127 // A model for years between now and a decade hence.
128 class YearComboboxModel : public ui::ComboboxModel {
129  public:
130   YearComboboxModel();
131   ~YearComboboxModel() override;
132
133   // ui::Combobox implementation:
134   int GetItemCount() const override;
135   base::string16 GetItemAt(int index) override;
136
137  private:
138   // The current year (e.g., 2012).
139   int this_year_;
140
141   DISALLOW_COPY_AND_ASSIGN(YearComboboxModel);
142 };
143
144 }  // namespace autofill
145
146 #endif  // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_MODELS_H_