Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: ui/views/controls/combobox/combobox.cc

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

Powered by Google App Engine
This is Rietveld 408576698