OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #include "views/controls/combobox/combobox.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "ui/base/accessibility/accessible_view_state.h" | |
10 #include "ui/base/keycodes/keyboard_codes.h" | |
11 #include "ui/base/models/combobox_model.h" | |
12 #include "ui/views/widget/widget.h" | |
13 #include "views/controls/combobox/combobox_listener.h" | |
14 #include "views/controls/native/native_view_host.h" | |
15 | |
16 namespace views { | |
17 | |
18 // static | |
19 const char Combobox::kViewClassName[] = "views/Combobox"; | |
20 | |
21 //////////////////////////////////////////////////////////////////////////////// | |
22 // Combobox, public: | |
23 | |
24 Combobox::Combobox(ui::ComboboxModel* model) | |
25 : native_wrapper_(NULL), | |
26 model_(model), | |
27 listener_(NULL), | |
28 selected_item_(0) { | |
29 set_focusable(true); | |
30 } | |
31 | |
32 Combobox::~Combobox() { | |
33 } | |
34 | |
35 void Combobox::ModelChanged() { | |
36 selected_item_ = std::min(0, model_->GetItemCount()); | |
37 if (native_wrapper_) | |
38 native_wrapper_->UpdateFromModel(); | |
39 PreferredSizeChanged(); | |
40 } | |
41 | |
42 void Combobox::SetSelectedItem(int index) { | |
43 selected_item_ = index; | |
44 if (native_wrapper_) | |
45 native_wrapper_->UpdateSelectedItem(); | |
46 } | |
47 | |
48 void Combobox::SelectionChanged() { | |
49 int prev_selected_item = selected_item_; | |
50 selected_item_ = native_wrapper_->GetSelectedItem(); | |
51 if (listener_) | |
52 listener_->ItemChanged(this, prev_selected_item, selected_item_); | |
53 if (GetWidget()) { | |
54 GetWidget()->NotifyAccessibilityEvent( | |
55 this, ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); | |
56 } | |
57 } | |
58 | |
59 void Combobox::SetAccessibleName(const string16& name) { | |
60 accessible_name_ = name; | |
61 } | |
62 | |
63 //////////////////////////////////////////////////////////////////////////////// | |
64 // Combobox, View overrides: | |
65 | |
66 gfx::Size Combobox::GetPreferredSize() { | |
67 if (native_wrapper_) | |
68 return native_wrapper_->GetPreferredSize(); | |
69 return gfx::Size(); | |
70 } | |
71 | |
72 void Combobox::Layout() { | |
73 if (native_wrapper_) { | |
74 native_wrapper_->GetView()->SetBounds(0, 0, width(), height()); | |
75 native_wrapper_->GetView()->Layout(); | |
76 } | |
77 } | |
78 | |
79 void Combobox::OnEnabledChanged() { | |
80 View::OnEnabledChanged(); | |
81 if (native_wrapper_) | |
82 native_wrapper_->UpdateEnabled(); | |
83 } | |
84 | |
85 // VKEY_ESCAPE should be handled by this view when the drop down list is active. | |
86 // In other words, the list should be closed instead of the dialog. | |
87 bool Combobox::SkipDefaultKeyEventProcessing(const KeyEvent& e) { | |
88 if (e.key_code() != ui::VKEY_ESCAPE || | |
89 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) { | |
90 return false; | |
91 } | |
92 return native_wrapper_ && native_wrapper_->IsDropdownOpen(); | |
93 } | |
94 | |
95 void Combobox::OnPaintFocusBorder(gfx::Canvas* canvas) { | |
96 if (NativeViewHost::kRenderNativeControlFocus) | |
97 View::OnPaintFocusBorder(canvas); | |
98 } | |
99 | |
100 bool Combobox::OnKeyPressed(const views::KeyEvent& e) { | |
101 return native_wrapper_ && native_wrapper_->HandleKeyPressed(e); | |
102 } | |
103 | |
104 bool Combobox::OnKeyReleased(const views::KeyEvent& e) { | |
105 return native_wrapper_ && native_wrapper_->HandleKeyReleased(e); | |
106 } | |
107 | |
108 void Combobox::OnFocus() { | |
109 // Forward the focus to the wrapper. | |
110 if (native_wrapper_) | |
111 native_wrapper_->SetFocus(); | |
112 else | |
113 View::OnFocus(); // Will focus the RootView window (so we still get | |
114 // keyboard messages). | |
115 } | |
116 | |
117 void Combobox::OnBlur() { | |
118 if (native_wrapper_) | |
119 native_wrapper_->HandleBlur(); | |
120 } | |
121 | |
122 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) { | |
123 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX; | |
124 state->name = accessible_name_; | |
125 state->value = model_->GetItemAt(selected_item_); | |
126 state->index = selected_item(); | |
127 state->count = model()->GetItemCount(); | |
128 } | |
129 | |
130 void Combobox::ViewHierarchyChanged(bool is_add, View* parent, View* child) { | |
131 if (is_add && !native_wrapper_ && GetWidget()) { | |
132 // The native wrapper's lifetime will be managed by the view hierarchy after | |
133 // we call AddChildView. | |
134 native_wrapper_ = NativeComboboxWrapper::CreateWrapper(this); | |
135 AddChildView(native_wrapper_->GetView()); | |
136 } | |
137 } | |
138 | |
139 std::string Combobox::GetClassName() const { | |
140 return kViewClassName; | |
141 } | |
142 | |
143 } // namespace views | |
OLD | NEW |