OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/combobox/native_combobox_views.h" | 5 #include "ui/views/controls/combobox/native_combobox_views.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // Define the id of the first item in the menu (since it needs to be > 0) | 48 // Define the id of the first item in the menu (since it needs to be > 0) |
49 const int kFirstMenuItemId = 1000; | 49 const int kFirstMenuItemId = 1000; |
50 | 50 |
51 } // namespace | 51 } // namespace |
52 | 52 |
53 namespace views { | 53 namespace views { |
54 | 54 |
55 const char NativeComboboxViews::kViewClassName[] = | 55 const char NativeComboboxViews::kViewClassName[] = |
56 "views/NativeComboboxViews"; | 56 "views/NativeComboboxViews"; |
57 | 57 |
58 NativeComboboxViews::NativeComboboxViews(Combobox* parent) | 58 NativeComboboxViews::NativeComboboxViews(Combobox* combo_box) |
59 : combobox_(parent), | 59 : combobox_(combo_box), |
60 text_border_(new FocusableBorder()), | 60 text_border_(new FocusableBorder()), |
61 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 61 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
62 IDR_DISCLOSURE_ARROW).ToSkBitmap()), | 62 IDR_DISCLOSURE_ARROW).ToSkBitmap()), |
63 dropdown_open_(false), | 63 dropdown_open_(false), |
64 selected_item_(-1), | 64 selected_index_(-1), |
65 content_width_(0), | 65 content_width_(0), |
66 content_height_(0) { | 66 content_height_(0) { |
67 set_border(text_border_); | 67 set_border(text_border_); |
68 } | 68 } |
69 | 69 |
70 NativeComboboxViews::~NativeComboboxViews() { | 70 NativeComboboxViews::~NativeComboboxViews() { |
71 } | 71 } |
72 | 72 |
73 //////////////////////////////////////////////////////////////////////////////// | 73 //////////////////////////////////////////////////////////////////////////////// |
74 // NativeComboboxViews, View overrides: | 74 // NativeComboboxViews, View overrides: |
(...skipping 10 matching lines...) Expand all Loading... |
85 | 85 |
86 bool NativeComboboxViews::OnMouseDragged(const views::MouseEvent& mouse_event) { | 86 bool NativeComboboxViews::OnMouseDragged(const views::MouseEvent& mouse_event) { |
87 return true; | 87 return true; |
88 } | 88 } |
89 | 89 |
90 bool NativeComboboxViews::OnKeyPressed(const views::KeyEvent& key_event) { | 90 bool NativeComboboxViews::OnKeyPressed(const views::KeyEvent& key_event) { |
91 // TODO(oshima): handle IME. | 91 // TODO(oshima): handle IME. |
92 DCHECK(key_event.type() == ui::ET_KEY_PRESSED); | 92 DCHECK(key_event.type() == ui::ET_KEY_PRESSED); |
93 | 93 |
94 // Check if we are in the default state (-1) and set to first item. | 94 // Check if we are in the default state (-1) and set to first item. |
95 if(selected_item_ == -1) | 95 if(selected_index_ == -1) |
96 selected_item_ = 0; | 96 selected_index_ = 0; |
97 | 97 |
98 int new_item = selected_item_; | 98 int new_index = selected_index_; |
99 switch(key_event.key_code()){ | 99 switch (key_event.key_code()) { |
100 | 100 // Move to the next element if any. |
101 // move to the next element if any | |
102 case ui::VKEY_DOWN: | 101 case ui::VKEY_DOWN: |
103 if (new_item < (combobox_->model()->GetItemCount() - 1)) | 102 if (new_index < (combobox_->model()->GetItemCount() - 1)) |
104 new_item++; | 103 new_index++; |
105 break; | 104 break; |
106 | 105 |
107 // move to the end of the list | 106 // Move to the end of the list, |
108 case ui::VKEY_END: | 107 case ui::VKEY_END: |
109 case ui::VKEY_NEXT: | 108 case ui::VKEY_NEXT: |
110 new_item = combobox_->model()->GetItemCount() - 1; | 109 new_index = combobox_->model()->GetItemCount() - 1; |
111 break; | 110 break; |
112 | 111 |
113 // move to the top of the list | 112 // Move to the top of the list. |
114 case ui::VKEY_HOME: | 113 case ui::VKEY_HOME: |
115 case ui::VKEY_PRIOR: | 114 case ui::VKEY_PRIOR: |
116 new_item = 0; | 115 new_index = 0; |
117 break; | 116 break; |
118 | 117 |
119 // move to the previous element if any | 118 // move to the previous element if any |
120 case ui::VKEY_UP: | 119 case ui::VKEY_UP: |
121 if (new_item > 0) | 120 if (new_index > 0) |
122 new_item--; | 121 new_index--; |
123 break; | 122 break; |
124 | 123 |
125 default: | 124 default: |
126 return false; | 125 return false; |
127 | |
128 } | 126 } |
129 | 127 |
130 if(new_item != selected_item_) { | 128 if (new_index != selected_index_) { |
131 selected_item_ = new_item; | 129 selected_index_ = new_index; |
132 combobox_->SelectionChanged(); | 130 combobox_->SelectionChanged(); |
133 SchedulePaint(); | 131 SchedulePaint(); |
134 } | 132 } |
135 | 133 |
136 return true; | 134 return true; |
137 } | 135 } |
138 | 136 |
139 bool NativeComboboxViews::OnKeyReleased(const views::KeyEvent& key_event) { | 137 bool NativeComboboxViews::OnKeyReleased(const views::KeyEvent& key_event) { |
140 return true; | 138 return true; |
141 } | 139 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 base::i18n::AdjustStringForLocaleDirection(&text); | 173 base::i18n::AdjustStringForLocaleDirection(&text); |
176 | 174 |
177 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 175 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); |
178 max_width = std::max(max_width, font.GetStringWidth(text)); | 176 max_width = std::max(max_width, font.GetStringWidth(text)); |
179 } | 177 } |
180 | 178 |
181 content_width_ = max_width; | 179 content_width_ = max_width; |
182 content_height_ = font.GetFontSize(); | 180 content_height_ = font.GetFontSize(); |
183 } | 181 } |
184 | 182 |
185 void NativeComboboxViews::UpdateSelectedItem() { | 183 void NativeComboboxViews::UpdateSelectedIndex() { |
186 selected_item_ = combobox_->selected_item(); | 184 selected_index_ = combobox_->selected_index(); |
187 } | 185 } |
188 | 186 |
189 void NativeComboboxViews::UpdateEnabled() { | 187 void NativeComboboxViews::UpdateEnabled() { |
190 SetEnabled(combobox_->enabled()); | 188 SetEnabled(combobox_->enabled()); |
191 } | 189 } |
192 | 190 |
193 int NativeComboboxViews::GetSelectedItem() const { | 191 int NativeComboboxViews::GetSelectedIndex() const { |
194 return selected_item_; | 192 return selected_index_; |
195 } | 193 } |
196 | 194 |
197 bool NativeComboboxViews::IsDropdownOpen() const { | 195 bool NativeComboboxViews::IsDropdownOpen() const { |
198 return dropdown_open_; | 196 return dropdown_open_; |
199 } | 197 } |
200 | 198 |
201 gfx::Size NativeComboboxViews::GetPreferredSize() { | 199 gfx::Size NativeComboboxViews::GetPreferredSize() { |
202 if (content_width_ == 0) | 200 if (content_width_ == 0) |
203 UpdateFromModel(); | 201 UpdateFromModel(); |
204 | 202 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 244 |
247 bool NativeComboboxViews::IsItemChecked(int id) const { | 245 bool NativeComboboxViews::IsItemChecked(int id) const { |
248 return false; | 246 return false; |
249 } | 247 } |
250 | 248 |
251 bool NativeComboboxViews::IsCommandEnabled(int id) const { | 249 bool NativeComboboxViews::IsCommandEnabled(int id) const { |
252 return true; | 250 return true; |
253 } | 251 } |
254 | 252 |
255 void NativeComboboxViews::ExecuteCommand(int id) { | 253 void NativeComboboxViews::ExecuteCommand(int id) { |
256 // revert menu offset to map back to combobox model | 254 // Revert menu offset to map back to combobox model. |
257 id -= kFirstMenuItemId; | 255 id -= kFirstMenuItemId; |
258 DCHECK_LT(id, combobox_->model()->GetItemCount()); | 256 DCHECK_LT(id, combobox_->model()->GetItemCount()); |
259 selected_item_ = id; | 257 selected_index_ = id; |
260 combobox_->SelectionChanged(); | 258 combobox_->SelectionChanged(); |
261 SchedulePaint(); | 259 SchedulePaint(); |
262 } | 260 } |
263 | 261 |
264 bool NativeComboboxViews::GetAccelerator(int id, ui::Accelerator* accel) { | 262 bool NativeComboboxViews::GetAccelerator(int id, ui::Accelerator* accel) { |
265 return false; | 263 return false; |
266 } | 264 } |
267 | 265 |
268 ///////////////////////////////////////////////////////////////// | 266 ///////////////////////////////////////////////////////////////// |
269 // NativeComboboxViews private methods: | 267 // NativeComboboxViews private methods: |
270 | 268 |
271 void NativeComboboxViews::AdjustBoundsForRTLUI(gfx::Rect* rect) const { | 269 void NativeComboboxViews::AdjustBoundsForRTLUI(gfx::Rect* rect) const { |
272 rect->set_x(GetMirroredXForRect(*rect)); | 270 rect->set_x(GetMirroredXForRect(*rect)); |
273 } | 271 } |
274 | 272 |
275 void NativeComboboxViews::PaintText(gfx::Canvas* canvas) { | 273 void NativeComboboxViews::PaintText(gfx::Canvas* canvas) { |
276 gfx::Insets insets = GetInsets(); | 274 gfx::Insets insets = GetInsets(); |
277 | 275 |
278 canvas->Save(); | 276 canvas->Save(); |
279 canvas->ClipRect(GetContentsBounds()); | 277 canvas->ClipRect(GetContentsBounds()); |
280 | 278 |
281 int x = insets.left(); | 279 int x = insets.left(); |
282 int y = insets.top(); | 280 int y = insets.top(); |
283 int text_height = height() - insets.height(); | 281 int text_height = height() - insets.height(); |
284 SkColor text_color = kTextColor; | 282 SkColor text_color = kTextColor; |
285 | 283 |
286 int index = GetSelectedItem(); | 284 int index = GetSelectedIndex(); |
287 if (index < 0 || index > combobox_->model()->GetItemCount()) | 285 if (index < 0 || index > combobox_->model()->GetItemCount()) |
288 index = 0; | 286 index = 0; |
289 string16 text = combobox_->model()->GetItemAt(index); | 287 string16 text = combobox_->model()->GetItemAt(index); |
290 | 288 |
291 int disclosure_arrow_offset = width() - disclosure_arrow_->width() | 289 int disclosure_arrow_offset = width() - disclosure_arrow_->width() |
292 - kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding; | 290 - kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding; |
293 | 291 |
294 const gfx::Font& font = Combobox::GetFont(); | 292 const gfx::Font& font = Combobox::GetFont(); |
295 int text_width = font.GetStringWidth(text); | 293 int text_width = font.GetStringWidth(text); |
296 if ((text_width + insets.width()) > disclosure_arrow_offset) | 294 if ((text_width + insets.width()) > disclosure_arrow_offset) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 349 |
352 #if defined(USE_AURA) | 350 #if defined(USE_AURA) |
353 // static | 351 // static |
354 NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( | 352 NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( |
355 Combobox* combobox) { | 353 Combobox* combobox) { |
356 return new NativeComboboxViews(combobox); | 354 return new NativeComboboxViews(combobox); |
357 } | 355 } |
358 #endif | 356 #endif |
359 | 357 |
360 } // namespace views | 358 } // namespace views |
OLD | NEW |