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 "grit/ui_resources.h" | 9 #include "grit/ui_resources.h" |
10 #include "ui/base/keycodes/keyboard_codes.h" | 10 #include "ui/base/keycodes/keyboard_codes.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Define the size of the insets. | 29 // Define the size of the insets. |
30 const int kTopInsetSize = 4; | 30 const int kTopInsetSize = 4; |
31 const int kLeftInsetSize = 4; | 31 const int kLeftInsetSize = 4; |
32 const int kBottomInsetSize = 4; | 32 const int kBottomInsetSize = 4; |
33 const int kRightInsetSize = 4; | 33 const int kRightInsetSize = 4; |
34 | 34 |
| 35 // Menu border widths |
| 36 const int kMenuBorderWidthLeft = 1; |
| 37 const int kMenuBorderWidthTop = 1; |
| 38 const int kMenuBorderWidthRight = 1; |
| 39 const int kMenuBorderWidthBottom = 2; |
| 40 |
35 // Limit how small a combobox can be. | 41 // Limit how small a combobox can be. |
36 const int kMinComboboxWidth = 148; | 42 const int kMinComboboxWidth = 148; |
37 | 43 |
38 // Size of the combobox arrow margins | 44 // Size of the combobox arrow margins |
39 const int kDisclosureArrowLeftPadding = 7; | 45 const int kDisclosureArrowLeftPadding = 7; |
40 const int kDisclosureArrowRightPadding = 7; | 46 const int kDisclosureArrowRightPadding = 7; |
41 | 47 |
42 // Color settings for text and border. | 48 // Color settings for text and border. |
43 // These are tentative, and should be derived from theme, system | 49 // These are tentative, and should be derived from theme, system |
44 // settings and current settings. | 50 // settings and current settings. |
45 const SkColor kTextColor = SK_ColorBLACK; | 51 const SkColor kTextColor = SK_ColorBLACK; |
46 | 52 |
47 // Define the id of the first item in the menu (since it needs to be > 0) | 53 // Define the id of the first item in the menu (since it needs to be > 0) |
48 const int kFirstMenuItemId = 1000; | 54 const int kFirstMenuItemId = 1000; |
49 | 55 |
50 } // namespace | 56 } // namespace |
51 | 57 |
52 namespace views { | 58 namespace views { |
53 | 59 |
54 const char NativeComboboxViews::kViewClassName[] = | 60 const char NativeComboboxViews::kViewClassName[] = |
55 "views/NativeComboboxViews"; | 61 "views/NativeComboboxViews"; |
56 | 62 |
57 NativeComboboxViews::NativeComboboxViews(Combobox* combobox) | 63 NativeComboboxViews::NativeComboboxViews(Combobox* combobox) |
58 : combobox_(combobox), | 64 : combobox_(combobox), |
59 text_border_(new FocusableBorder()), | 65 text_border_(new FocusableBorder()), |
60 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 66 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
61 IDR_DISCLOSURE_ARROW).ToImageSkia()), | 67 IDR_MENU_DROPARROW).ToImageSkia()), |
62 dropdown_open_(false), | 68 dropdown_open_(false), |
63 selected_index_(-1), | 69 selected_index_(-1), |
64 content_width_(0), | 70 content_width_(0), |
65 content_height_(0) { | 71 content_height_(0) { |
66 set_border(text_border_); | 72 set_border(text_border_); |
67 } | 73 } |
68 | 74 |
69 NativeComboboxViews::~NativeComboboxViews() { | 75 NativeComboboxViews::~NativeComboboxViews() { |
70 } | 76 } |
71 | 77 |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 } | 324 } |
319 | 325 |
320 void NativeComboboxViews::ShowDropDownMenu() { | 326 void NativeComboboxViews::ShowDropDownMenu() { |
321 | 327 |
322 if (!dropdown_list_menu_runner_.get()) | 328 if (!dropdown_list_menu_runner_.get()) |
323 UpdateFromModel(); | 329 UpdateFromModel(); |
324 | 330 |
325 // Extend the menu to the width of the combobox. | 331 // Extend the menu to the width of the combobox. |
326 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); | 332 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); |
327 SubmenuView* submenu = menu->CreateSubmenu(); | 333 SubmenuView* submenu = menu->CreateSubmenu(); |
328 submenu->set_minimum_preferred_width(size().width()); | 334 submenu->set_minimum_preferred_width(size().width() - |
329 | 335 (kMenuBorderWidthLeft + kMenuBorderWidthRight)); |
330 #if defined(USE_AURA) | |
331 // Aura style is to have the menu over the bounds. Below bounds is default. | |
332 menu->set_menu_position(views::MenuItemView::POSITION_OVER_BOUNDS); | |
333 #endif | |
334 | 336 |
335 gfx::Rect lb = GetLocalBounds(); | 337 gfx::Rect lb = GetLocalBounds(); |
336 gfx::Point menu_position(lb.origin()); | 338 gfx::Point menu_position(lb.origin()); |
| 339 |
| 340 // Inset the menu's requested position so the border of the menu lines up |
| 341 // with the border of the combobox. |
| 342 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); |
| 343 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop); |
| 344 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); |
| 345 |
337 View::ConvertPointToScreen(this, &menu_position); | 346 View::ConvertPointToScreen(this, &menu_position); |
338 if (menu_position.x() < 0) | 347 if (menu_position.x() < 0) |
339 menu_position.set_x(0); | 348 menu_position.set_x(0); |
340 | 349 |
341 gfx::Rect bounds(menu_position, lb.size()); | 350 gfx::Rect bounds(menu_position, lb.size()); |
342 | 351 |
343 dropdown_open_ = true; | 352 dropdown_open_ = true; |
344 if (dropdown_list_menu_runner_->RunMenuAt( | 353 if (dropdown_list_menu_runner_->RunMenuAt( |
345 GetWidget(), NULL, bounds, MenuItemView::TOPLEFT, | 354 GetWidget(), NULL, bounds, MenuItemView::TOPLEFT, |
346 MenuRunner::HAS_MNEMONICS) == MenuRunner::MENU_DELETED) | 355 MenuRunner::HAS_MNEMONICS) == MenuRunner::MENU_DELETED) |
(...skipping 11 matching lines...) Expand all Loading... |
358 | 367 |
359 #if defined(USE_AURA) | 368 #if defined(USE_AURA) |
360 // static | 369 // static |
361 NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( | 370 NativeComboboxWrapper* NativeComboboxWrapper::CreateWrapper( |
362 Combobox* combobox) { | 371 Combobox* combobox) { |
363 return new NativeComboboxViews(combobox); | 372 return new NativeComboboxViews(combobox); |
364 } | 373 } |
365 #endif | 374 #endif |
366 | 375 |
367 } // namespace views | 376 } // namespace views |
OLD | NEW |