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 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 sublabel and its identifying key to the model. 49 // Keys needn't be unique. 50 void AddKeyedItemWithSublabel(const std::string& key, 51 const string16& display_label, 52 const string16& display_sublabel); 53 54 // As above, but also accepts an image which will be displayed alongside the 55 // text. 56 void AddKeyedItemWithSublabelAndIcon(const std::string& key, 57 const string16& display_label, 58 const string16& display_sublabel, 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 // Sets the item to be checked to the |n|th item that has key |item_key|. 76 // If there are fewer than |n| items that share |item_key|, the last one 77 // becomes checked. If there is no item with |item_key|, nothing happens. 78 // |n| is 1-indexed. 79 void SetCheckedItemNthWithKey(const std::string& item_key, size_t n); 80 81 int checked_item() const { return checked_item_; } 82 83 // Enable/disable an item by key. 84 void SetEnabled(const std::string& item_key, bool enabled); 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 94 private: 95 // Represents an item in this model. 96 struct Item { 97 std::string key; // The key of the item. 98 bool enabled; // Whether the item is selectable. 99 }; 100 // The items this model represents in presentation order. 101 // Note: the index in this vector is the |command_id| of the item. 102 std::vector<Item> items_; 103 104 // Returns the command id (and index) of the item by the |key|. 105 size_t GetItemIndex(const std::string& item_key); 106 107 SuggestionsMenuModelDelegate* delegate_; 108 109 // The command id (and index) of the item which is currently checked. Only one 110 // item is checked at a time. 111 int checked_item_; 112 113 DISALLOW_COPY_AND_ASSIGN(SuggestionsMenuModel); 114 }; 115 116 // A model for possible months in the Gregorian calendar. 117 class MonthComboboxModel : public ui::ComboboxModel { 118 public: 119 MonthComboboxModel(); 120 virtual ~MonthComboboxModel(); 121 122 static string16 FormatMonth(int index); 123 124 // ui::Combobox implementation: 125 virtual int GetItemCount() const OVERRIDE; 126 virtual string16 GetItemAt(int index) OVERRIDE; 127 128 private: 129 DISALLOW_COPY_AND_ASSIGN(MonthComboboxModel); 130 }; 131 132 // A model for years between now and a decade hence. 133 class YearComboboxModel : public ui::ComboboxModel { 134 public: 135 YearComboboxModel(); 136 virtual ~YearComboboxModel(); 137 138 // ui::Combobox implementation: 139 virtual int GetItemCount() const OVERRIDE; 140 virtual string16 GetItemAt(int index) OVERRIDE; 141 142 private: 143 // The current year (e.g., 2012). 144 int this_year_; 145 146 DISALLOW_COPY_AND_ASSIGN(YearComboboxModel); 147 }; 148 149 } // autofill 150 151 #endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_MODELS_H_ 152