BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_dialog_models.cc
blob06fdf81f92ed624a24a72b7190182e0764099979
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.
5 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/autofill/core/browser/autofill_country.h"
15 #include "ui/base/l10n/l10n_util.h"
17 namespace autofill {
19 SuggestionsMenuModelDelegate::~SuggestionsMenuModelDelegate() {}
21 // SuggestionsMenuModel ----------------------------------------------------
23 SuggestionsMenuModel::SuggestionsMenuModel(
24 SuggestionsMenuModelDelegate* delegate)
25 : ui::SimpleMenuModel(this),
26 delegate_(delegate),
27 checked_item_(0) {}
29 SuggestionsMenuModel::~SuggestionsMenuModel() {}
31 void SuggestionsMenuModel::AddKeyedItem(
32 const std::string& key, const base::string16& display_label) {
33 Item item = { key, true };
34 items_.push_back(item);
35 AddCheckItem(items_.size() - 1, display_label);
38 void SuggestionsMenuModel::AddKeyedItemWithIcon(
39 const std::string& key,
40 const base::string16& display_label,
41 const gfx::Image& icon) {
42 AddKeyedItem(key, display_label);
43 SetIcon(items_.size() - 1, icon);
46 void SuggestionsMenuModel::AddKeyedItemWithMinorText(
47 const std::string& key,
48 const base::string16& display_label,
49 const base::string16& display_minor_text) {
50 AddKeyedItem(key, display_label);
51 SetMinorText(items_.size() - 1, display_minor_text);
54 void SuggestionsMenuModel::AddKeyedItemWithMinorTextAndIcon(
55 const std::string& key,
56 const base::string16& display_label,
57 const base::string16& display_minor_text,
58 const gfx::Image& icon) {
59 AddKeyedItemWithIcon(key, display_label, icon);
60 SetMinorText(items_.size() - 1, display_minor_text);
63 void SuggestionsMenuModel::Reset() {
64 checked_item_ = 0;
65 items_.clear();
66 Clear();
69 std::string SuggestionsMenuModel::GetItemKeyAt(int index) const {
70 return items_[index].key;
73 std::string SuggestionsMenuModel::GetItemKeyForCheckedItem() const {
74 if (items_.empty())
75 return std::string();
77 return items_[checked_item_].key;
80 void SuggestionsMenuModel::SetCheckedItem(const std::string& item_key) {
81 for (size_t i = 0; i < items_.size(); ++i) {
82 if (items_[i].key == item_key) {
83 if (IsEnabledAt(i))
84 checked_item_ = i;
85 break;
90 void SuggestionsMenuModel::SetCheckedIndex(size_t index) {
91 DCHECK_LT(index, items_.size());
92 checked_item_ = index;
95 void SuggestionsMenuModel::SetEnabled(const std::string& item_key,
96 bool enabled) {
97 items_[GetItemIndex(item_key)].enabled = enabled;
100 bool SuggestionsMenuModel::IsCommandIdChecked(
101 int command_id) const {
102 return checked_item_ == command_id;
105 bool SuggestionsMenuModel::IsCommandIdEnabled(
106 int command_id) const {
107 // Please note: command_id is same as the 0-based index in |items_|.
108 DCHECK_GE(command_id, 0);
109 DCHECK_LT(static_cast<size_t>(command_id), items_.size());
110 return items_[command_id].enabled;
113 bool SuggestionsMenuModel::GetAcceleratorForCommandId(
114 int command_id,
115 ui::Accelerator* accelerator) {
116 return false;
119 void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
120 delegate_->SuggestionItemSelected(this, command_id);
123 size_t SuggestionsMenuModel::GetItemIndex(const std::string& item_key) {
124 for (size_t i = 0; i < items_.size(); ++i) {
125 if (items_[i].key == item_key)
126 return i;
129 NOTREACHED();
130 return 0;
133 // MonthComboboxModel ----------------------------------------------------------
135 MonthComboboxModel::MonthComboboxModel() {}
137 MonthComboboxModel::~MonthComboboxModel() {}
139 int MonthComboboxModel::GetItemCount() const {
140 // 12 months plus the empty entry.
141 return 13;
144 // static
145 base::string16 MonthComboboxModel::FormatMonth(int index) {
146 return base::ASCIIToUTF16(base::StringPrintf("%.2d", index));
149 base::string16 MonthComboboxModel::GetItemAt(int index) {
150 return index == 0 ?
151 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) :
152 FormatMonth(index);
155 // YearComboboxModel -----------------------------------------------------------
157 YearComboboxModel::YearComboboxModel() : this_year_(0) {
158 base::Time time = base::Time::Now();
159 base::Time::Exploded exploded;
160 time.LocalExplode(&exploded);
161 this_year_ = exploded.year;
164 YearComboboxModel::~YearComboboxModel() {}
166 int YearComboboxModel::GetItemCount() const {
167 // 10 years plus the empty entry.
168 return 11;
171 base::string16 YearComboboxModel::GetItemAt(int index) {
172 if (index == 0) {
173 return l10n_util::GetStringUTF16(
174 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR);
177 return base::IntToString16(this_year_ + index - 1);
180 } // namespace autofill