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/combobox.h" | 5 #include "ui/views/controls/combobox/combobox.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "grit/ui_resources.h" | 9 #include "grit/ui_resources.h" |
10 #include "ui/base/accessibility/accessible_view_state.h" | 10 #include "ui/base/accessibility/accessible_view_state.h" |
11 #include "ui/base/models/combobox_model.h" | 11 #include "ui/base/models/combobox_model.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/events/event.h" | 13 #include "ui/events/event.h" |
14 #include "ui/events/keycodes/keyboard_codes.h" | 14 #include "ui/events/keycodes/keyboard_codes.h" |
15 #include "ui/gfx/animation/throb_animation.h" | |
15 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/image/image.h" | |
16 #include "ui/native_theme/native_theme.h" | 18 #include "ui/native_theme/native_theme.h" |
17 #include "ui/views/color_constants.h" | 19 #include "ui/views/color_constants.h" |
20 #include "ui/views/controls/button/custom_button.h" | |
21 #include "ui/views/controls/button/label_button.h" | |
18 #include "ui/views/controls/combobox/combobox_listener.h" | 22 #include "ui/views/controls/combobox/combobox_listener.h" |
19 #include "ui/views/controls/focusable_border.h" | 23 #include "ui/views/controls/focusable_border.h" |
20 #include "ui/views/controls/menu/menu_runner.h" | 24 #include "ui/views/controls/menu/menu_runner.h" |
21 #include "ui/views/controls/menu/submenu_view.h" | 25 #include "ui/views/controls/menu/submenu_view.h" |
22 #include "ui/views/controls/prefix_selector.h" | 26 #include "ui/views/controls/prefix_selector.h" |
23 #include "ui/views/ime/input_method.h" | 27 #include "ui/views/ime/input_method.h" |
24 #include "ui/views/mouse_constants.h" | 28 #include "ui/views/mouse_constants.h" |
29 #include "ui/views/painter.h" | |
25 #include "ui/views/widget/widget.h" | 30 #include "ui/views/widget/widget.h" |
26 | 31 |
27 namespace views { | 32 namespace views { |
28 | 33 |
29 namespace { | 34 namespace { |
30 | 35 |
31 // Menu border widths | 36 // Menu border widths |
32 const int kMenuBorderWidthLeft = 1; | 37 const int kMenuBorderWidthLeft = 1; |
33 const int kMenuBorderWidthTop = 1; | 38 const int kMenuBorderWidthTop = 1; |
34 const int kMenuBorderWidthRight = 1; | 39 const int kMenuBorderWidthRight = 1; |
35 | 40 |
36 // Limit how small a combobox can be. | 41 // Limit how small a combobox can be. |
37 const int kMinComboboxWidth = 25; | 42 const int kMinComboboxWidth = 25; |
38 | 43 |
39 // Size of the combobox arrow margins | 44 // Size of the combobox arrow margins |
40 const int kDisclosureArrowLeftPadding = 7; | 45 const int kDisclosureArrowLeftPadding = 7; |
41 const int kDisclosureArrowRightPadding = 7; | 46 const int kDisclosureArrowRightPadding = 7; |
47 const int kDisclosureArrowButtonLeftPadding = 11; | |
48 const int kDisclosureArrowButtonRightPadding = 12; | |
42 | 49 |
43 // Define the id of the first item in the menu (since it needs to be > 0) | 50 // Define the id of the first item in the menu (since it needs to be > 0) |
44 const int kFirstMenuItemId = 1000; | 51 const int kFirstMenuItemId = 1000; |
45 | 52 |
46 const SkColor kInvalidTextColor = SK_ColorWHITE; | 53 const SkColor kInvalidTextColor = SK_ColorWHITE; |
47 | 54 |
48 // Used to indicate that no item is currently selected by the user. | 55 // Used to indicate that no item is currently selected by the user. |
49 const int kNoSelection = -1; | 56 const int kNoSelection = -1; |
50 | 57 |
58 const int kBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON); | |
59 const int kHoveredBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_H); | |
60 const int kPressedBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_P); | |
61 | |
62 #define MENU_IMAGE_GRID(x) { \ | |
63 x ## _MENU_TOP, x ## _MENU_CENTER, x ## _MENU_BOTTOM, } | |
64 | |
65 const int kMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON); | |
66 const int kHoveredMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON_H); | |
67 const int kPressedMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON_P); | |
68 | |
69 #undef MENU_IMAGE_GRID | |
70 | |
51 // The background to use for invalid comboboxes. | 71 // The background to use for invalid comboboxes. |
52 class InvalidBackground : public Background { | 72 class InvalidBackground : public Background { |
53 public: | 73 public: |
54 InvalidBackground() {} | 74 InvalidBackground() {} |
55 virtual ~InvalidBackground() {} | 75 virtual ~InvalidBackground() {} |
56 | 76 |
57 // Overridden from Background: | 77 // Overridden from Background: |
58 virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE { | 78 virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE { |
59 gfx::Rect bounds(view->GetLocalBounds()); | 79 gfx::Rect bounds(view->GetLocalBounds()); |
60 // Inset by 2 to leave 1 empty pixel between background and border. | 80 // Inset by 2 to leave 1 empty pixel between background and border. |
61 bounds.Inset(2, 2, 2, 2); | 81 bounds.Inset(2, 2, 2, 2); |
62 canvas->FillRect(bounds, kWarningColor); | 82 canvas->FillRect(bounds, kWarningColor); |
63 } | 83 } |
64 | 84 |
65 private: | 85 private: |
66 DISALLOW_COPY_AND_ASSIGN(InvalidBackground); | 86 DISALLOW_COPY_AND_ASSIGN(InvalidBackground); |
67 }; | 87 }; |
68 | 88 |
89 // The transparent button which holds a button state but is not rendered. | |
90 class TransparentButton : public CustomButton { | |
91 public: | |
92 TransparentButton(ButtonListener* listener) | |
93 : CustomButton(listener) { | |
94 SetAnimationDuration(LabelButton::kHoverAnimationDurationMs); | |
95 } | |
96 virtual ~TransparentButton() {} | |
97 | |
98 // CustomButton: | |
99 virtual void StateChanged() OVERRIDE { | |
100 parent()->SchedulePaintInRect(bounds()); | |
101 } | |
102 | |
103 // gfx::AnimationDelegate: | |
104 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE { | |
105 parent()->SchedulePaintInRect(bounds()); | |
106 } | |
107 | |
108 double GetAnimationValue() const { | |
109 return hover_animation_->GetCurrentValue(); | |
110 } | |
111 | |
112 private: | |
113 DISALLOW_COPY_AND_ASSIGN(TransparentButton); | |
114 }; | |
115 | |
69 // Returns the next or previous valid index (depending on |increment|'s value). | 116 // Returns the next or previous valid index (depending on |increment|'s value). |
70 // Skips separator or disabled indices. Returns -1 if there is no valid adjacent | 117 // Skips separator or disabled indices. Returns -1 if there is no valid adjacent |
71 // index. | 118 // index. |
72 int GetAdjacentIndex(ui::ComboboxModel* model, int increment, int index) { | 119 int GetAdjacentIndex(ui::ComboboxModel* model, int increment, int index) { |
73 DCHECK(increment == -1 || increment == 1); | 120 DCHECK(increment == -1 || increment == 1); |
74 | 121 |
75 index += increment; | 122 index += increment; |
76 while (index >= 0 && index < model->GetItemCount()) { | 123 while (index >= 0 && index < model->GetItemCount()) { |
77 if (!model->IsItemSeparatorAt(index) || !model->IsItemEnabledAt(index)) | 124 if (!model->IsItemSeparatorAt(index) || !model->IsItemEnabledAt(index)) |
78 return index; | 125 return index; |
79 index += increment; | 126 index += increment; |
80 } | 127 } |
81 return kNoSelection; | 128 return kNoSelection; |
82 } | 129 } |
83 | 130 |
131 // Returns the image resource ids of an array for the body button. | |
132 // | |
133 // TODO(hajimehoshi): This function should return the images for the 'disabled' | |
134 // status. (crbug/270052) | |
135 // | |
136 // TODO(hajimehoshi): Currently, |focused| is ignored. This should return the | |
137 // images for the 'focused' status. (crbug/270052) | |
138 const int* GetBodyButtonImageIds(bool focused, Button::ButtonState state, | |
sky
2013/12/04 16:34:05
nit: wrap state to its own line.
hajimehoshi
2013/12/05 08:05:57
Done.
| |
139 size_t* num) { | |
140 DCHECK(num); | |
141 *num = 9; | |
142 switch (state) { | |
143 case Button::STATE_DISABLED: | |
144 return kBodyButtonImages; | |
145 case Button::STATE_NORMAL: | |
146 return kBodyButtonImages; | |
147 case Button::STATE_HOVERED: | |
148 return kHoveredBodyButtonImages; | |
149 case Button::STATE_PRESSED: | |
150 return kPressedBodyButtonImages; | |
151 default: | |
152 NOTREACHED(); | |
153 } | |
154 return NULL; | |
155 } | |
156 | |
157 // Returns the image resource ids of an array for the menu button. | |
158 const int* GetMenuButtonImageIds(bool focused, Button::ButtonState state, | |
sky
2013/12/04 16:34:05
nit: wrap state to its own line.
hajimehoshi
2013/12/05 08:05:57
Done.
| |
159 size_t* num) { | |
160 DCHECK(num); | |
161 *num = 3; | |
162 switch (state) { | |
163 case Button::STATE_DISABLED: | |
164 return kMenuButtonImages; | |
165 case Button::STATE_NORMAL: | |
166 return kMenuButtonImages; | |
167 case Button::STATE_HOVERED: | |
168 return kHoveredMenuButtonImages; | |
169 case Button::STATE_PRESSED: | |
170 return kPressedMenuButtonImages; | |
171 default: | |
172 NOTREACHED(); | |
173 } | |
174 return NULL; | |
175 } | |
176 | |
177 // Returns the images for the menu buttons. | |
178 std::vector<const gfx::ImageSkia*> GetMenuButtonImages( | |
179 bool focused, Button::ButtonState state) { | |
sky
2013/12/04 16:34:05
nit: wrap state to its own line.
hajimehoshi
2013/12/05 08:05:57
Done.
| |
180 const int* ids; | |
181 size_t num_ids; | |
182 ids = GetMenuButtonImageIds(focused, state, &num_ids); | |
183 std::vector<const gfx::ImageSkia*> images; | |
184 images.reserve(num_ids); | |
185 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
186 for (size_t i = 0; i < num_ids; i++) | |
187 images.push_back(rb.GetImageSkiaNamed(ids[i])); | |
188 return images; | |
189 } | |
190 | |
191 // Paints three images in a row at the given location. | |
sky
2013/12/04 16:34:05
This isn't a row, but a column. Name this PaintIma
hajimehoshi
2013/12/05 08:05:57
Done.
| |
192 void PaintThreeImages(gfx::Canvas* canvas, | |
193 const gfx::ImageSkia& top_image, | |
194 const gfx::ImageSkia& center_image, | |
195 const gfx::ImageSkia& bottom_image, | |
196 int x, int y, int width, int height) { | |
197 canvas->DrawImageInt(top_image, | |
198 0, 0, top_image.width(), top_image.height(), | |
199 x, y, width, top_image.height(), false); | |
200 y += top_image.height(); | |
201 int center_height = height - top_image.height() - bottom_image.height(); | |
202 canvas->DrawImageInt(center_image, | |
203 0, 0, center_image.width(), center_image.height(), | |
204 x, y, width, center_height, false); | |
205 y += center_height; | |
206 canvas->DrawImageInt(bottom_image, | |
207 0, 0, bottom_image.width(), bottom_image.height(), | |
208 x, y, width, bottom_image.height(), false); | |
209 } | |
210 | |
211 // Paints the arrow button. | |
212 void PaintArrowButton( | |
213 gfx::Canvas* canvas, | |
214 const std::vector<const gfx::ImageSkia*>& arrow_button_images, | |
215 int x, int height) { | |
216 PaintThreeImages(canvas, | |
217 *arrow_button_images[0], | |
218 *arrow_button_images[1], | |
219 *arrow_button_images[2], | |
220 x, 0, arrow_button_images[0]->width(), height); | |
221 } | |
222 | |
84 } // namespace | 223 } // namespace |
85 | 224 |
86 // static | 225 // static |
87 const char Combobox::kViewClassName[] = "views/Combobox"; | 226 const char Combobox::kViewClassName[] = "views/Combobox"; |
88 | 227 |
89 //////////////////////////////////////////////////////////////////////////////// | 228 //////////////////////////////////////////////////////////////////////////////// |
90 // Combobox, public: | 229 // Combobox, public: |
91 | 230 |
92 Combobox::Combobox(ui::ComboboxModel* model) | 231 Combobox::Combobox(ui::ComboboxModel* model) |
93 : model_(model), | 232 : model_(model), |
233 style_(STYLE_SHOW_DROP_DOWN_ON_CLICK), | |
94 listener_(NULL), | 234 listener_(NULL), |
95 selected_index_(model_->GetDefaultIndex()), | 235 selected_index_(model_->GetDefaultIndex()), |
96 invalid_(false), | 236 invalid_(false), |
97 text_border_(new FocusableBorder()), | 237 text_border_(new FocusableBorder()), |
98 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 238 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
99 IDR_MENU_DROPARROW).ToImageSkia()), | 239 IDR_MENU_DROPARROW).ToImageSkia()), |
100 dropdown_open_(false) { | 240 dropdown_open_(false), |
241 text_button_(NULL), | |
sky
2013/12/04 16:34:05
Sorry for not noticing last time around. Why can't
hajimehoshi
2013/12/05 08:05:57
Right. Done.
I guess OnMousePressed, OnMouseDrag
| |
242 arrow_button_(NULL) { | |
101 model_->AddObserver(this); | 243 model_->AddObserver(this); |
102 UpdateFromModel(); | 244 UpdateFromModel(); |
103 set_focusable(true); | 245 set_focusable(true); |
104 set_border(text_border_); | 246 set_border(text_border_); |
247 | |
248 // Initialize the button images. | |
249 Button::ButtonState button_states[] = { | |
250 Button::STATE_DISABLED, | |
251 Button::STATE_NORMAL, | |
252 Button::STATE_HOVERED, | |
253 Button::STATE_PRESSED, | |
254 }; | |
255 for (int focused = 0; focused < 2; focused++) { | |
256 for (size_t state_index = 0; state_index < arraysize(button_states); | |
257 state_index++) { | |
258 Button::ButtonState state = button_states[state_index]; | |
259 size_t num; | |
260 const int* ids = GetBodyButtonImageIds(focused, state, &num); | |
261 body_button_painters_[focused][state].reset( | |
262 Painter::CreateImageGridPainter(ids)); | |
263 menu_button_images_[focused][state] = GetMenuButtonImages(focused, state); | |
264 } | |
265 } | |
105 } | 266 } |
106 | 267 |
107 Combobox::~Combobox() { | 268 Combobox::~Combobox() { |
108 model_->RemoveObserver(this); | 269 model_->RemoveObserver(this); |
109 } | 270 } |
110 | 271 |
111 // static | 272 // static |
112 const gfx::Font& Combobox::GetFont() { | 273 const gfx::Font& Combobox::GetFont() { |
113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 274 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
114 return rb.GetFont(ui::ResourceBundle::BaseFont); | 275 return rb.GetFont(ui::ResourceBundle::BaseFont); |
115 } | 276 } |
116 | 277 |
278 void Combobox::SetStyle(Style style) { | |
279 if (style_ == style) | |
280 return; | |
281 | |
282 style_ = style; | |
283 switch (style) { | |
284 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { | |
285 // Recreate a FocusableBorder to reset the insets of the border. | |
286 // set_border with a new border will delete the existing border. | |
287 text_border_ = new FocusableBorder(); | |
288 set_border(text_border_); | |
289 | |
290 DCHECK(text_button_); | |
291 DCHECK(arrow_button_); | |
292 RemoveChildView(text_button_); | |
sky
2013/12/04 16:34:05
delete text_button_; delete arrow_button_; does th
hajimehoshi
2013/12/05 08:05:57
Done.
| |
293 RemoveChildView(arrow_button_); | |
294 // Since RemoveChildView doesn't delete the given views, they should be | |
295 // deleted here. | |
296 scoped_ptr<View> view_to_be_deleted; | |
297 view_to_be_deleted.reset(text_button_); | |
298 view_to_be_deleted.reset(arrow_button_); | |
299 break; | |
300 } | |
301 case STYLE_NOTIFY_ON_CLICK: { | |
302 text_border_->SetInsets(8, 13, 8, 13); | |
303 | |
304 DCHECK(!text_button_); | |
305 DCHECK(!arrow_button_); | |
306 text_button_ = new TransparentButton(this); | |
307 arrow_button_ = new TransparentButton(this); | |
308 text_button_->SetVisible(true); | |
309 arrow_button_->SetVisible(true); | |
310 text_button_->set_focusable(false); | |
311 arrow_button_->set_focusable(false); | |
312 AddChildView(text_button_); | |
313 AddChildView(arrow_button_); | |
314 break; | |
315 } | |
316 } | |
317 | |
318 PreferredSizeChanged(); | |
319 } | |
320 | |
117 void Combobox::ModelChanged() { | 321 void Combobox::ModelChanged() { |
118 selected_index_ = std::min(0, model_->GetItemCount()); | 322 selected_index_ = std::min(0, model_->GetItemCount()); |
119 UpdateFromModel(); | 323 UpdateFromModel(); |
120 PreferredSizeChanged(); | 324 PreferredSizeChanged(); |
121 } | 325 } |
122 | 326 |
123 void Combobox::SetSelectedIndex(int index) { | 327 void Combobox::SetSelectedIndex(int index) { |
124 selected_index_ = index; | 328 selected_index_ = index; |
125 SchedulePaint(); | 329 SchedulePaint(); |
126 } | 330 } |
(...skipping 22 matching lines...) Expand all Loading... | |
149 set_background(NULL); | 353 set_background(NULL); |
150 } | 354 } |
151 } | 355 } |
152 | 356 |
153 ui::TextInputClient* Combobox::GetTextInputClient() { | 357 ui::TextInputClient* Combobox::GetTextInputClient() { |
154 if (!selector_) | 358 if (!selector_) |
155 selector_.reset(new PrefixSelector(this)); | 359 selector_.reset(new PrefixSelector(this)); |
156 return selector_.get(); | 360 return selector_.get(); |
157 } | 361 } |
158 | 362 |
363 void Combobox::Layout() { | |
364 PrefixDelegate::Layout(); | |
365 if (style_ != STYLE_NOTIFY_ON_CLICK) | |
366 return; | |
367 | |
368 DCHECK(text_button_); | |
369 DCHECK(arrow_button_); | |
370 gfx::Insets insets = GetInsets(); | |
371 int height = content_size_.height() + insets.height(); | |
372 int text_button_width = | |
373 std::max(kMinComboboxWidth, content_size_.width()) + insets.width(); | |
sky
2013/12/04 16:34:05
I don't think you should use content_size_ here. I
hajimehoshi
2013/12/05 08:05:57
Done.
| |
374 text_button_->SetBounds(0, 0, text_button_width, height); | |
375 int arrow_button_x = text_button_width; | |
376 int arrow_button_width = GetDisclosureArrowLeftPadding() + | |
377 disclosure_arrow_->width() + GetDisclosureArrowRightPadding(); | |
378 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height); | |
379 } | |
159 | 380 |
160 bool Combobox::IsItemChecked(int id) const { | 381 bool Combobox::IsItemChecked(int id) const { |
161 return false; | 382 return false; |
162 } | 383 } |
163 | 384 |
164 bool Combobox::IsCommandEnabled(int id) const { | 385 bool Combobox::IsCommandEnabled(int id) const { |
165 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); | 386 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); |
166 } | 387 } |
167 | 388 |
168 void Combobox::ExecuteCommand(int id) { | 389 void Combobox::ExecuteCommand(int id) { |
(...skipping 25 matching lines...) Expand all Loading... | |
194 // Combobox, View overrides: | 415 // Combobox, View overrides: |
195 | 416 |
196 gfx::Size Combobox::GetPreferredSize() { | 417 gfx::Size Combobox::GetPreferredSize() { |
197 if (content_size_.IsEmpty()) | 418 if (content_size_.IsEmpty()) |
198 UpdateFromModel(); | 419 UpdateFromModel(); |
199 | 420 |
200 // The preferred size will drive the local bounds which in turn is used to set | 421 // The preferred size will drive the local bounds which in turn is used to set |
201 // the minimum width for the dropdown list. | 422 // the minimum width for the dropdown list. |
202 gfx::Insets insets = GetInsets(); | 423 gfx::Insets insets = GetInsets(); |
203 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + | 424 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + |
204 insets.width() + kDisclosureArrowLeftPadding + | 425 insets.width() + GetDisclosureArrowLeftPadding() + |
205 disclosure_arrow_->width() + kDisclosureArrowRightPadding; | 426 disclosure_arrow_->width() + GetDisclosureArrowRightPadding(); |
206 | |
207 return gfx::Size(total_width, content_size_.height() + insets.height()); | 427 return gfx::Size(total_width, content_size_.height() + insets.height()); |
208 } | 428 } |
209 | 429 |
210 const char* Combobox::GetClassName() const { | 430 const char* Combobox::GetClassName() const { |
211 return kViewClassName; | 431 return kViewClassName; |
212 } | 432 } |
213 | 433 |
214 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { | 434 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { |
215 // Escape should close the drop down list when it is active, not host UI. | 435 // Escape should close the drop down list when it is active, not host UI. |
216 if (e.key_code() != ui::VKEY_ESCAPE || | 436 if (e.key_code() != ui::VKEY_ESCAPE || |
217 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) { | 437 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) { |
218 return false; | 438 return false; |
219 } | 439 } |
220 return dropdown_open_; | 440 return dropdown_open_; |
221 } | 441 } |
222 | 442 |
223 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) { | 443 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) { |
224 RequestFocus(); | 444 RequestFocus(); |
225 const base::TimeDelta delta = base::Time::Now() - closed_time_; | 445 const base::TimeDelta delta = base::Time::Now() - closed_time_; |
226 if (mouse_event.IsLeftMouseButton() && | 446 if (mouse_event.IsLeftMouseButton() && |
227 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) { | 447 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) { |
228 UpdateFromModel(); | 448 UpdateFromModel(); |
229 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE); | 449 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE); |
230 } | 450 } |
231 | |
232 return true; | 451 return true; |
233 } | 452 } |
234 | 453 |
235 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) { | 454 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) { |
236 return true; | 455 return true; |
237 } | 456 } |
238 | 457 |
239 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) { | 458 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) { |
240 // TODO(oshima): handle IME. | 459 // TODO(oshima): handle IME. |
241 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED); | 460 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 case ui::VKEY_HOME: | 492 case ui::VKEY_HOME: |
274 case ui::VKEY_PRIOR: // Page up. | 493 case ui::VKEY_PRIOR: // Page up. |
275 new_index = GetAdjacentIndex(model(), 1, -1); | 494 new_index = GetAdjacentIndex(model(), 1, -1); |
276 break; | 495 break; |
277 | 496 |
278 // Move to the previous item if any. | 497 // Move to the previous item if any. |
279 case ui::VKEY_UP: | 498 case ui::VKEY_UP: |
280 new_index = GetAdjacentIndex(model(), -1, selected_index_); | 499 new_index = GetAdjacentIndex(model(), -1, selected_index_); |
281 break; | 500 break; |
282 | 501 |
502 // Click the button only when the button style mode. | |
503 case ui::VKEY_SPACE: | |
504 if (style_ == STYLE_NOTIFY_ON_CLICK) { | |
505 // When pressing space, the click event will be raised after the key is | |
506 // released. | |
507 text_button_->SetState(Button::STATE_PRESSED); | |
508 } | |
509 break; | |
510 | |
511 // Click the button only when the button style mode. | |
512 case ui::VKEY_RETURN: | |
513 HandleClickEvent(); | |
514 break; | |
515 | |
283 default: | 516 default: |
284 return false; | 517 return false; |
285 } | 518 } |
286 | 519 |
287 if (show_menu) { | 520 if (show_menu) { |
288 UpdateFromModel(); | 521 UpdateFromModel(); |
289 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); | 522 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); |
290 } else if (new_index != selected_index_ && new_index != kNoSelection) { | 523 } else if (new_index != selected_index_ && new_index != kNoSelection) { |
291 DCHECK(!model()->IsItemSeparatorAt(new_index)); | 524 DCHECK(!model()->IsItemSeparatorAt(new_index)); |
292 selected_index_ = new_index; | 525 selected_index_ = new_index; |
293 OnSelectionChanged(); | 526 OnSelectionChanged(); |
294 } | 527 } |
295 | 528 |
296 return true; | 529 return true; |
297 } | 530 } |
298 | 531 |
299 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { | 532 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { |
300 return false; // crbug.com/127520 | 533 if (style_ != STYLE_NOTIFY_ON_CLICK) |
534 return false; // crbug.com/127520 | |
535 | |
536 if (e.key_code() == ui::VKEY_SPACE) | |
537 HandleClickEvent(); | |
538 | |
539 return false; | |
301 } | 540 } |
302 | 541 |
303 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) { | 542 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) { |
304 if (gesture->type() == ui::ET_GESTURE_TAP) { | 543 if (gesture->type() == ui::ET_GESTURE_TAP) { |
305 UpdateFromModel(); | 544 UpdateFromModel(); |
306 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH); | 545 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH); |
307 gesture->StopPropagation(); | 546 gesture->StopPropagation(); |
308 return; | 547 return; |
309 } | 548 } |
310 View::OnGestureEvent(gesture); | 549 View::OnGestureEvent(gesture); |
311 } | 550 } |
312 | 551 |
313 void Combobox::OnPaint(gfx::Canvas* canvas) { | 552 void Combobox::OnPaint(gfx::Canvas* canvas) { |
314 OnPaintBackground(canvas); | 553 switch (style_) { |
315 PaintText(canvas); | 554 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { |
316 OnPaintBorder(canvas); | 555 OnPaintBackground(canvas); |
556 PaintText(canvas); | |
557 OnPaintBorder(canvas); | |
558 break; | |
559 } | |
560 case STYLE_NOTIFY_ON_CLICK: { | |
561 PaintButtons(canvas); | |
562 PaintText(canvas); | |
563 break; | |
sky
2013/12/04 16:34:05
I believe you need to deal with focus here.
hajimehoshi
2013/12/05 08:05:57
I intend to create another CL to add images to ren
| |
564 } | |
565 } | |
317 } | 566 } |
318 | 567 |
319 void Combobox::OnFocus() { | 568 void Combobox::OnFocus() { |
320 GetInputMethod()->OnFocus(); | 569 GetInputMethod()->OnFocus(); |
321 text_border_->set_has_focus(true); | 570 text_border_->set_has_focus(true); |
322 View::OnFocus(); | 571 View::OnFocus(); |
323 } | 572 } |
324 | 573 |
325 void Combobox::OnBlur() { | 574 void Combobox::OnBlur() { |
326 GetInputMethod()->OnBlur(); | 575 GetInputMethod()->OnBlur(); |
327 if (selector_) | 576 if (selector_) |
328 selector_->OnViewBlur(); | 577 selector_->OnViewBlur(); |
329 text_border_->set_has_focus(false); | 578 text_border_->set_has_focus(false); |
330 } | 579 } |
331 | 580 |
332 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) { | 581 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) { |
333 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX; | 582 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX; |
334 state->name = accessible_name_; | 583 state->name = accessible_name_; |
335 state->value = model_->GetItemAt(selected_index_); | 584 state->value = model_->GetItemAt(selected_index_); |
336 state->index = selected_index_; | 585 state->index = selected_index_; |
337 state->count = model_->GetItemCount(); | 586 state->count = model_->GetItemCount(); |
338 } | 587 } |
339 | 588 |
340 void Combobox::OnModelChanged() { | 589 void Combobox::OnModelChanged() { |
341 ModelChanged(); | 590 ModelChanged(); |
342 } | 591 } |
343 | 592 |
593 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) { | |
594 DCHECK(style_ == STYLE_NOTIFY_ON_CLICK); | |
595 DCHECK(text_button_); | |
596 DCHECK(arrow_button_); | |
597 if (sender == text_button_) { | |
598 HandleClickEvent(); | |
599 } else { | |
600 DCHECK_EQ(arrow_button_, sender); | |
601 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when | |
602 // cliking this while the dropdown menu is opened. | |
603 const base::TimeDelta delta = base::Time::Now() - closed_time_; | |
604 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks) | |
605 return; | |
606 | |
607 UpdateFromModel(); | |
sky
2013/12/04 16:34:05
Why are you invoking UpdateFromModel here?
hajimehoshi
2013/12/05 08:05:57
Sorry but this wasn't needed. Done.
| |
608 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; | |
609 if (event.IsKeyEvent()) | |
610 source_type = ui::MENU_SOURCE_KEYBOARD; | |
611 else if (event.IsGestureEvent() || event.IsTouchEvent()) | |
612 source_type = ui::MENU_SOURCE_TOUCH; | |
613 ShowDropDownMenu(source_type); | |
614 } | |
615 } | |
616 | |
344 void Combobox::UpdateFromModel() { | 617 void Combobox::UpdateFromModel() { |
345 int max_width = 0; | 618 int max_width = 0; |
346 const gfx::Font& font = Combobox::GetFont(); | 619 const gfx::Font& font = Combobox::GetFont(); |
347 | 620 |
348 MenuItemView* menu = new MenuItemView(this); | 621 MenuItemView* menu = new MenuItemView(this); |
349 // MenuRunner owns |menu|. | 622 // MenuRunner owns |menu|. |
350 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); | 623 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); |
351 | 624 |
352 int num_items = model()->GetItemCount(); | 625 int num_items = model()->GetItemCount(); |
353 for (int i = 0; i < num_items; ++i) { | 626 for (int i = 0; i < num_items; ++i) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 SkColor text_color = invalid() ? kInvalidTextColor : | 658 SkColor text_color = invalid() ? kInvalidTextColor : |
386 GetNativeTheme()->GetSystemColor( | 659 GetNativeTheme()->GetSystemColor( |
387 ui::NativeTheme::kColorId_LabelEnabledColor); | 660 ui::NativeTheme::kColorId_LabelEnabledColor); |
388 | 661 |
389 DCHECK_GE(selected_index_, 0); | 662 DCHECK_GE(selected_index_, 0); |
390 DCHECK_LT(selected_index_, model()->GetItemCount()); | 663 DCHECK_LT(selected_index_, model()->GetItemCount()); |
391 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) | 664 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) |
392 selected_index_ = 0; | 665 selected_index_ = 0; |
393 string16 text = model()->GetItemAt(selected_index_); | 666 string16 text = model()->GetItemAt(selected_index_); |
394 | 667 |
395 int disclosure_arrow_offset = width() - disclosure_arrow_->width() | 668 int disclosure_arrow_offset = width() - disclosure_arrow_->width() - |
396 - kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding; | 669 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding(); |
397 | 670 |
398 const gfx::Font& font = Combobox::GetFont(); | 671 const gfx::Font& font = Combobox::GetFont(); |
399 int text_width = font.GetStringWidth(text); | 672 int text_width = font.GetStringWidth(text); |
400 if ((text_width + insets.width()) > disclosure_arrow_offset) | 673 if ((text_width + insets.width()) > disclosure_arrow_offset) |
401 text_width = disclosure_arrow_offset - insets.width(); | 674 text_width = disclosure_arrow_offset - insets.width(); |
402 | 675 |
403 gfx::Rect text_bounds(x, y, text_width, text_height); | 676 gfx::Rect text_bounds(x, y, text_width, text_height); |
404 AdjustBoundsForRTLUI(&text_bounds); | 677 AdjustBoundsForRTLUI(&text_bounds); |
405 canvas->DrawStringInt(text, font, text_color, text_bounds); | 678 canvas->DrawStringInt(text, font, text_color, text_bounds); |
406 | 679 |
407 gfx::Rect arrow_bounds(disclosure_arrow_offset + kDisclosureArrowLeftPadding, | 680 int arrow_x = disclosure_arrow_offset + GetDisclosureArrowLeftPadding(); |
681 gfx::Rect arrow_bounds(arrow_x, | |
408 height() / 2 - disclosure_arrow_->height() / 2, | 682 height() / 2 - disclosure_arrow_->height() / 2, |
409 disclosure_arrow_->width(), | 683 disclosure_arrow_->width(), |
410 disclosure_arrow_->height()); | 684 disclosure_arrow_->height()); |
411 AdjustBoundsForRTLUI(&arrow_bounds); | 685 AdjustBoundsForRTLUI(&arrow_bounds); |
412 | 686 |
413 SkPaint paint; | 687 SkPaint paint; |
414 // This makes the arrow subtractive. | 688 // This makes the arrow subtractive. |
415 if (invalid()) | 689 if (invalid()) |
416 paint.setXfermodeMode(SkXfermode::kDstOut_Mode); | 690 paint.setXfermodeMode(SkXfermode::kDstOut_Mode); |
417 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(), | 691 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(), |
418 paint); | 692 paint); |
419 | 693 |
420 canvas->Restore(); | 694 canvas->Restore(); |
421 } | 695 } |
422 | 696 |
697 void Combobox::PaintButtons(gfx::Canvas* canvas) { | |
698 DCHECK(style_ == STYLE_NOTIFY_ON_CLICK); | |
699 DCHECK(text_button_); | |
700 DCHECK(arrow_button_); | |
701 | |
702 canvas->Save(); | |
sky
2013/12/04 16:34:05
Use ScopedCanvas as necessary in here.
hajimehoshi
2013/12/05 08:05:57
Done.
| |
703 if (base::i18n::IsRTL()) { | |
704 canvas->Translate(gfx::Vector2d(width(), 0)); | |
705 canvas->Scale(-1, 1); | |
706 } | |
707 | |
708 bool focused = HasFocus(); | |
709 const std::vector<const gfx::ImageSkia*>& arrow_button_images = | |
710 menu_button_images_[focused][ | |
711 arrow_button_->state() == Button::STATE_HOVERED ? | |
712 Button::STATE_NORMAL : arrow_button_->state()]; | |
713 | |
714 int text_button_width = width() - arrow_button_images[0]->width(); | |
715 int text_button_hover_alpha = | |
716 text_button_->state() == Button::STATE_PRESSED ? 0 : | |
717 static_cast<int>(static_cast<TransparentButton*>(text_button_)-> | |
718 GetAnimationValue() * 255); | |
719 if (text_button_hover_alpha < 255) { | |
720 canvas->SaveLayerAlpha(255 - text_button_hover_alpha); | |
721 Painter* text_button_painter = | |
722 body_button_painters_[focused][ | |
723 text_button_->state() == Button::STATE_HOVERED ? | |
724 Button::STATE_NORMAL : text_button_->state()].get(); | |
725 Painter::PaintPainterAt(canvas, text_button_painter, | |
726 gfx::Rect(0, 0, text_button_width, height())); | |
727 canvas->Restore(); | |
728 } | |
729 if (0 < text_button_hover_alpha) { | |
730 canvas->SaveLayerAlpha(text_button_hover_alpha); | |
731 Painter* text_button_hovered_painter = | |
732 body_button_painters_[focused][Button::STATE_HOVERED].get(); | |
733 Painter::PaintPainterAt(canvas, text_button_hovered_painter, | |
734 gfx::Rect(0, 0, text_button_width, height())); | |
735 canvas->Restore(); | |
736 } | |
737 | |
738 int arrow_button_x = text_button_width; | |
739 int arrow_button_hover_alpha = | |
740 arrow_button_->state() == Button::STATE_PRESSED ? 0 : | |
741 static_cast<int>(static_cast<TransparentButton*>(arrow_button_)-> | |
742 GetAnimationValue() * 255); | |
743 if (arrow_button_hover_alpha < 255) { | |
744 canvas->SaveLayerAlpha(255 - arrow_button_hover_alpha); | |
745 PaintArrowButton(canvas, arrow_button_images, arrow_button_x, height()); | |
746 canvas->Restore(); | |
747 } | |
748 if (0 < arrow_button_hover_alpha) { | |
749 canvas->SaveLayerAlpha(arrow_button_hover_alpha); | |
750 const std::vector<const gfx::ImageSkia*>& arrow_button_hovered_images = | |
751 menu_button_images_[focused][Button::STATE_HOVERED]; | |
752 PaintArrowButton(canvas, arrow_button_hovered_images, | |
753 arrow_button_x, height()); | |
754 canvas->Restore(); | |
755 } | |
756 | |
757 canvas->Restore(); | |
758 } | |
759 | |
423 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { | 760 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { |
424 if (!dropdown_list_menu_runner_.get()) | 761 if (!dropdown_list_menu_runner_.get()) |
425 UpdateFromModel(); | 762 UpdateFromModel(); |
426 | 763 |
427 // Extend the menu to the width of the combobox. | 764 // Extend the menu to the width of the combobox. |
428 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); | 765 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); |
429 SubmenuView* submenu = menu->CreateSubmenu(); | 766 SubmenuView* submenu = menu->CreateSubmenu(); |
430 submenu->set_minimum_preferred_width(size().width() - | 767 submenu->set_minimum_preferred_width( |
431 (kMenuBorderWidthLeft + kMenuBorderWidthRight)); | 768 size().width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); |
432 | 769 |
433 gfx::Rect lb = GetLocalBounds(); | 770 gfx::Rect lb = GetLocalBounds(); |
434 gfx::Point menu_position(lb.origin()); | 771 gfx::Point menu_position(lb.origin()); |
435 | 772 |
436 // Inset the menu's requested position so the border of the menu lines up | 773 // Inset the menu's requested position so the border of the menu lines up |
437 // with the border of the combobox. | 774 // with the border of the combobox. |
438 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); | 775 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); |
439 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop); | 776 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop); |
440 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); | 777 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); |
441 | 778 |
442 View::ConvertPointToScreen(this, &menu_position); | 779 View::ConvertPointToScreen(this, &menu_position); |
443 if (menu_position.x() < 0) | 780 if (menu_position.x() < 0) |
444 menu_position.set_x(0); | 781 menu_position.set_x(0); |
445 | 782 |
446 gfx::Rect bounds(menu_position, lb.size()); | 783 gfx::Rect bounds(menu_position, lb.size()); |
447 | 784 |
785 Button::ButtonState original_state; | |
786 if (arrow_button_) { | |
787 original_state = arrow_button_->state(); | |
788 arrow_button_->SetState(Button::STATE_PRESSED); | |
789 } | |
448 dropdown_open_ = true; | 790 dropdown_open_ = true; |
449 if (dropdown_list_menu_runner_->RunMenuAt(GetWidget(), NULL, bounds, | 791 if (dropdown_list_menu_runner_->RunMenuAt(GetWidget(), NULL, bounds, |
450 MenuItemView::TOPLEFT, source_type, MenuRunner::COMBOBOX) == | 792 MenuItemView::TOPLEFT, source_type, MenuRunner::COMBOBOX) == |
451 MenuRunner::MENU_DELETED) | 793 MenuRunner::MENU_DELETED) { |
452 return; | 794 return; |
795 } | |
453 dropdown_open_ = false; | 796 dropdown_open_ = false; |
797 if (arrow_button_) { | |
798 arrow_button_->SetState(original_state); | |
sky
2013/12/04 16:34:05
nit: no {}
hajimehoshi
2013/12/05 08:05:57
Done.
| |
799 } | |
454 closed_time_ = base::Time::Now(); | 800 closed_time_ = base::Time::Now(); |
455 | 801 |
456 // Need to explicitly clear mouse handler so that events get sent | 802 // Need to explicitly clear mouse handler so that events get sent |
457 // properly after the menu finishes running. If we don't do this, then | 803 // properly after the menu finishes running. If we don't do this, then |
458 // the first click to other parts of the UI is eaten. | 804 // the first click to other parts of the UI is eaten. |
459 SetMouseHandler(NULL); | 805 SetMouseHandler(NULL); |
460 } | 806 } |
461 | 807 |
462 void Combobox::OnSelectionChanged() { | 808 void Combobox::OnSelectionChanged() { |
463 if (listener_) | 809 if (listener_) |
464 listener_->OnSelectedIndexChanged(this); | 810 listener_->OnSelectedIndexChanged(this); |
465 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); | 811 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); |
466 SchedulePaint(); | 812 SchedulePaint(); |
467 } | 813 } |
468 | 814 |
469 int Combobox::MenuCommandToIndex(int menu_command_id) const { | 815 int Combobox::MenuCommandToIndex(int menu_command_id) const { |
470 // (note that the id received is offset by kFirstMenuItemId) | 816 // (note that the id received is offset by kFirstMenuItemId) |
471 // Revert menu ID offset to map back to combobox model. | 817 // Revert menu ID offset to map back to combobox model. |
472 int index = menu_command_id - kFirstMenuItemId; | 818 int index = menu_command_id - kFirstMenuItemId; |
473 DCHECK_LT(index, model()->GetItemCount()); | 819 DCHECK_LT(index, model()->GetItemCount()); |
474 return index; | 820 return index; |
475 } | 821 } |
476 | 822 |
823 int Combobox::GetDisclosureArrowLeftPadding() const { | |
824 switch (style_) { | |
825 case STYLE_SHOW_DROP_DOWN_ON_CLICK: | |
826 return kDisclosureArrowLeftPadding; | |
827 case STYLE_NOTIFY_ON_CLICK: | |
828 return kDisclosureArrowButtonLeftPadding; | |
829 } | |
830 NOTREACHED(); | |
831 return 0; | |
832 } | |
833 | |
834 int Combobox::GetDisclosureArrowRightPadding() const { | |
835 switch (style_) { | |
836 case STYLE_SHOW_DROP_DOWN_ON_CLICK: | |
837 return kDisclosureArrowRightPadding; | |
838 case STYLE_NOTIFY_ON_CLICK: | |
839 return kDisclosureArrowButtonRightPadding; | |
840 } | |
841 NOTREACHED(); | |
842 return 0; | |
843 } | |
844 | |
845 void Combobox::HandleClickEvent() { | |
846 if (style_ != STYLE_NOTIFY_ON_CLICK) | |
847 return; | |
848 | |
849 if (listener_) | |
850 listener_->OnComboboxTextButtonClicked(this); | |
851 } | |
852 | |
477 } // namespace views | 853 } // namespace views |
OLD | NEW |