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