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

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: Add TransparentButton and use those 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"
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()->SchedulePaint();
101 }
102
103 // gfx::AnimationDelegate:
sky 2013/12/03 20:52:30 Does SchedulePaint() on this always need to force
hajimehoshi 2013/12/04 06:30:02 Done. (ScheudulePaintInRect with its bounds should
sky 2013/12/04 16:34:05 Why is that? Don't you need to force the whole com
hajimehoshi 2013/12/05 08:05:57 Each button has its own animation value only for h
104 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE {
105 parent()->SchedulePaint();
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) {
139 switch (state) {
140 case Button::STATE_DISABLED:
141 return kBodyButtonImages;
142 case Button::STATE_NORMAL:
143 return kBodyButtonImages;
144 case Button::STATE_HOVERED:
145 return kHoveredBodyButtonImages;
146 case Button::STATE_PRESSED:
147 return kPressedBodyButtonImages;
148 default:
149 NOTREACHED();
150 }
151 return NULL;
152 }
153
154 // Returns the image resource ids of an array for the menu button.
155 const int* GetMenuButtonImageIds(bool focused, Button::ButtonState state) {
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 buttons.
172 std::vector<const gfx::ImageSkia*> GetButtonImages(bool menu,
173 bool focused,
174 Button::ButtonState state) {
175 const int* ids;
176 size_t num_ids;
177 if (!menu) {
178 ids = GetBodyButtonImageIds(focused, state);
sky 2013/12/03 20:52:30 This and GetMenuButtonImageIds should return the n
hajimehoshi 2013/12/04 06:30:02 Done.
179 num_ids = 9;
180 } else {
181 ids = GetMenuButtonImageIds(focused, state);
182 num_ids = 3;
183 }
184 std::vector<const gfx::ImageSkia*> images;
185 images.reserve(num_ids);
186 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
187 for (size_t i = 0; i < num_ids; i++) {
188 int id = ids[i];
sky 2013/12/03 20:52:30 nit: no need for the temporary here.
hajimehoshi 2013/12/04 06:30:02 Done.
189 images.push_back(rb.GetImageSkiaNamed(id));
190 }
191 return images;
192 }
193
194 // Paints three images in a row at the given location.
195 void PaintTandemImages(gfx::Canvas* canvas,
sky 2013/12/03 20:52:30 Isn't tandem too, yet this paints three.
hajimehoshi 2013/12/04 06:30:02 Done.
196 const gfx::ImageSkia& top_image,
197 const gfx::ImageSkia& center_image,
198 const gfx::ImageSkia& bottom_image,
199 int x, int y, int width, int height) {
200 canvas->DrawImageInt(top_image,
201 0, 0, top_image.width(), top_image.height(),
202 x, y, width, top_image.height(), false);
203 y += top_image.height();
204 int center_height = height - top_image.height() - bottom_image.height();
205 canvas->DrawImageInt(center_image,
206 0, 0, center_image.width(), center_image.height(),
207 x, y, width, center_height, false);
208 y += center_height;
209 canvas->DrawImageInt(bottom_image,
210 0, 0, bottom_image.width(), bottom_image.height(),
211 x, y, width, bottom_image.height(), false);
212 }
213
214 // Paints the text button.
215 void PaintTextButton(
sky 2013/12/03 20:52:30 This code looks a lot like that offered by Painter
hajimehoshi 2013/12/04 06:30:02 Done. Menu button images remain. Is it better to c
216 gfx::Canvas* canvas,
217 const std::vector<const gfx::ImageSkia*>& text_button_images,
218 int width, int height) {
219 int current_x = 0;
220 int current_width = text_button_images[0]->width();
221 PaintTandemImages(canvas,
222 *text_button_images[0],
223 *text_button_images[3],
224 *text_button_images[6],
225 current_x, 0, current_width, height);
226
227 current_x += current_width;
228 current_width = width - text_button_images[0]->width() -
229 text_button_images[2]->width();
230 PaintTandemImages(canvas,
231 *text_button_images[1],
232 *text_button_images[4],
233 *text_button_images[7],
234 current_x, 0, current_width, height);
235
236 current_x += current_width;
237 current_width = text_button_images[2]->width();
238 PaintTandemImages(canvas,
239 *text_button_images[2],
240 *text_button_images[5],
241 *text_button_images[8],
242 current_x, 0, current_width, height);
243 }
244
245 // Paints the arrow button.
246 void PaintArrowButton(
247 gfx::Canvas* canvas,
248 const std::vector<const gfx::ImageSkia*>& arrow_button_images,
249 int x, int height) {
250 PaintTandemImages(canvas,
251 *arrow_button_images[0],
252 *arrow_button_images[1],
253 *arrow_button_images[2],
254 x, 0, arrow_button_images[0]->width(), height);
255 }
256
84 } // namespace 257 } // namespace
85 258
86 // static 259 // static
87 const char Combobox::kViewClassName[] = "views/Combobox"; 260 const char Combobox::kViewClassName[] = "views/Combobox";
88 261
89 //////////////////////////////////////////////////////////////////////////////// 262 ////////////////////////////////////////////////////////////////////////////////
90 // Combobox, public: 263 // Combobox, public:
91 264
92 Combobox::Combobox(ui::ComboboxModel* model) 265 Combobox::Combobox(ui::ComboboxModel* model)
93 : model_(model), 266 : model_(model),
267 style_(STYLE_SHOW_DROP_DOWN_ON_CLICK),
94 listener_(NULL), 268 listener_(NULL),
95 selected_index_(model_->GetDefaultIndex()), 269 selected_index_(model_->GetDefaultIndex()),
96 invalid_(false), 270 invalid_(false),
97 text_border_(new FocusableBorder()), 271 text_border_(new FocusableBorder()),
98 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( 272 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
99 IDR_MENU_DROPARROW).ToImageSkia()), 273 IDR_MENU_DROPARROW).ToImageSkia()),
100 dropdown_open_(false) { 274 dropdown_open_(false),
275 text_button_(new TransparentButton(this)),
sky 2013/12/03 20:52:30 Is there a reason these need to be always created?
hajimehoshi 2013/12/04 06:30:02 Done. (It could be simpler that the buttons always
276 arrow_button_(new TransparentButton(this)) {
101 model_->AddObserver(this); 277 model_->AddObserver(this);
102 UpdateFromModel(); 278 UpdateFromModel();
103 set_focusable(true); 279 set_focusable(true);
104 set_border(text_border_); 280 set_border(text_border_);
281
282 // Initialize the button images.
283 Button::ButtonState button_states[] = {
284 Button::STATE_DISABLED,
285 Button::STATE_NORMAL,
286 Button::STATE_HOVERED,
287 Button::STATE_PRESSED,
288 };
289 for (int focused = 0; focused < 2; focused++) {
290 for (size_t state_index = 0; state_index < arraysize(button_states);
291 state_index++) {
292 Button::ButtonState state = button_states[state_index];
293 body_button_images_[focused][state] =
294 GetButtonImages(false, focused, state);
295 menu_button_images_[focused][state] =
296 GetButtonImages(true, focused, state);
297 }
298 }
299
300 text_button_->SetVisible(false);
301 arrow_button_->SetVisible(false);
302 AddChildView(text_button_);
303 AddChildView(arrow_button_);
sky 2013/12/03 20:52:30 You should also make these non focusable.
hajimehoshi 2013/12/04 06:30:02 Done.
105 } 304 }
106 305
107 Combobox::~Combobox() { 306 Combobox::~Combobox() {
108 model_->RemoveObserver(this); 307 model_->RemoveObserver(this);
109 } 308 }
110 309
111 // static 310 // static
112 const gfx::Font& Combobox::GetFont() { 311 const gfx::Font& Combobox::GetFont() {
113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 312 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
114 return rb.GetFont(ui::ResourceBundle::BaseFont); 313 return rb.GetFont(ui::ResourceBundle::BaseFont);
115 } 314 }
116 315
316 void Combobox::SetStyle(Style style) {
317 if (style_ == style)
318 return;
319
320 style_ = style;
321 switch (style) {
322 case STYLE_SHOW_DROP_DOWN_ON_CLICK: {
323 text_border_ = new FocusableBorder();
sky 2013/12/03 20:52:30 Why do you create a new border here?
hajimehoshi 2013/12/04 06:30:02 It's because there is no way to reset the insets o
sky 2013/12/04 16:34:05 FocusableBorder has a SetInsets, can't you use it?
hajimehoshi 2013/12/05 08:05:57 The default insets values of FocusableBorder is in
324 set_border(text_border_);
325 text_button_->SetVisible(false);
326 arrow_button_->SetVisible(false);
327 break;
328 }
329 case STYLE_NOTIFY_ON_CLICK: {
330 text_border_->SetInsets(8, 13, 8, 13);
331 text_button_->SetVisible(true);
332 arrow_button_->SetVisible(true);
333 break;
334 }
335 }
336
337 PreferredSizeChanged();
338 }
339
117 void Combobox::ModelChanged() { 340 void Combobox::ModelChanged() {
118 selected_index_ = std::min(0, model_->GetItemCount()); 341 selected_index_ = std::min(0, model_->GetItemCount());
119 UpdateFromModel(); 342 UpdateFromModel();
120 PreferredSizeChanged(); 343 PreferredSizeChanged();
121 } 344 }
122 345
123 void Combobox::SetSelectedIndex(int index) { 346 void Combobox::SetSelectedIndex(int index) {
124 selected_index_ = index; 347 selected_index_ = index;
125 SchedulePaint(); 348 SchedulePaint();
126 } 349 }
(...skipping 22 matching lines...) Expand all
149 set_background(NULL); 372 set_background(NULL);
150 } 373 }
151 } 374 }
152 375
153 ui::TextInputClient* Combobox::GetTextInputClient() { 376 ui::TextInputClient* Combobox::GetTextInputClient() {
154 if (!selector_) 377 if (!selector_)
155 selector_.reset(new PrefixSelector(this)); 378 selector_.reset(new PrefixSelector(this));
156 return selector_.get(); 379 return selector_.get();
157 } 380 }
158 381
382 void Combobox::PreferredSizeChanged() {
383 gfx::Insets insets = GetInsets();
384 int height = content_size_.height() + insets.height();
385 int text_button_width =
386 std::max(kMinComboboxWidth, content_size_.width()) + insets.width();
387 text_button_->SetBounds(0, 0, text_button_width, height);
388 int arrow_button_x = text_button_width;
389 int arrow_button_width = GetDisclosureArrowLeftPadding() +
390 disclosure_arrow_->width() + GetDisclosureArrowRightPadding();
391 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height);
sky 2013/12/03 20:52:30 This should be done in Layout.
hajimehoshi 2013/12/04 06:30:02 Done.
392 PrefixDelegate::PreferredSizeChanged();
393 }
159 394
160 bool Combobox::IsItemChecked(int id) const { 395 bool Combobox::IsItemChecked(int id) const {
161 return false; 396 return false;
162 } 397 }
163 398
164 bool Combobox::IsCommandEnabled(int id) const { 399 bool Combobox::IsCommandEnabled(int id) const {
165 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); 400 return model()->IsItemEnabledAt(MenuCommandToIndex(id));
166 } 401 }
167 402
168 void Combobox::ExecuteCommand(int id) { 403 void Combobox::ExecuteCommand(int id) {
(...skipping 25 matching lines...) Expand all
194 // Combobox, View overrides: 429 // Combobox, View overrides:
195 430
196 gfx::Size Combobox::GetPreferredSize() { 431 gfx::Size Combobox::GetPreferredSize() {
197 if (content_size_.IsEmpty()) 432 if (content_size_.IsEmpty())
198 UpdateFromModel(); 433 UpdateFromModel();
199 434
200 // The preferred size will drive the local bounds which in turn is used to set 435 // The preferred size will drive the local bounds which in turn is used to set
201 // the minimum width for the dropdown list. 436 // the minimum width for the dropdown list.
202 gfx::Insets insets = GetInsets(); 437 gfx::Insets insets = GetInsets();
203 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + 438 int total_width = std::max(kMinComboboxWidth, content_size_.width()) +
204 insets.width() + kDisclosureArrowLeftPadding + 439 insets.width() + GetDisclosureArrowLeftPadding() +
205 disclosure_arrow_->width() + kDisclosureArrowRightPadding; 440 disclosure_arrow_->width() + GetDisclosureArrowRightPadding();
206
207 return gfx::Size(total_width, content_size_.height() + insets.height()); 441 return gfx::Size(total_width, content_size_.height() + insets.height());
208 } 442 }
209 443
210 const char* Combobox::GetClassName() const { 444 const char* Combobox::GetClassName() const {
211 return kViewClassName; 445 return kViewClassName;
212 } 446 }
213 447
214 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { 448 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
215 // Escape should close the drop down list when it is active, not host UI. 449 // Escape should close the drop down list when it is active, not host UI.
216 if (e.key_code() != ui::VKEY_ESCAPE || 450 if (e.key_code() != ui::VKEY_ESCAPE ||
217 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) { 451 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) {
218 return false; 452 return false;
219 } 453 }
220 return dropdown_open_; 454 return dropdown_open_;
221 } 455 }
222 456
223 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) { 457 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) {
224 RequestFocus(); 458 RequestFocus();
225 const base::TimeDelta delta = base::Time::Now() - closed_time_; 459 const base::TimeDelta delta = base::Time::Now() - closed_time_;
226 if (mouse_event.IsLeftMouseButton() && 460 if (mouse_event.IsLeftMouseButton() &&
227 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) { 461 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) {
228 UpdateFromModel(); 462 UpdateFromModel();
229 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE); 463 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE);
230 } 464 }
231
232 return true; 465 return true;
233 } 466 }
234 467
235 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) { 468 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) {
236 return true; 469 return true;
237 } 470 }
238 471
239 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) { 472 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) {
240 // TODO(oshima): handle IME. 473 // TODO(oshima): handle IME.
241 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED); 474 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 case ui::VKEY_HOME: 506 case ui::VKEY_HOME:
274 case ui::VKEY_PRIOR: // Page up. 507 case ui::VKEY_PRIOR: // Page up.
275 new_index = GetAdjacentIndex(model(), 1, -1); 508 new_index = GetAdjacentIndex(model(), 1, -1);
276 break; 509 break;
277 510
278 // Move to the previous item if any. 511 // Move to the previous item if any.
279 case ui::VKEY_UP: 512 case ui::VKEY_UP:
280 new_index = GetAdjacentIndex(model(), -1, selected_index_); 513 new_index = GetAdjacentIndex(model(), -1, selected_index_);
281 break; 514 break;
282 515
516 // Click the button only when the button style mode.
517 case ui::VKEY_SPACE:
518 // When pressing space, the click event will be raised after the key is
519 // released.
520 text_button_->SetState(Button::STATE_PRESSED);
521 break;
522
523 // Click the button only when the button style mode.
524 case ui::VKEY_RETURN:
525 HandleClickEvent();
526 break;
527
283 default: 528 default:
284 return false; 529 return false;
285 } 530 }
286 531
287 if (show_menu) { 532 if (show_menu) {
288 UpdateFromModel(); 533 UpdateFromModel();
289 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); 534 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD);
290 } else if (new_index != selected_index_ && new_index != kNoSelection) { 535 } else if (new_index != selected_index_ && new_index != kNoSelection) {
291 DCHECK(!model()->IsItemSeparatorAt(new_index)); 536 DCHECK(!model()->IsItemSeparatorAt(new_index));
292 selected_index_ = new_index; 537 selected_index_ = new_index;
293 OnSelectionChanged(); 538 OnSelectionChanged();
294 } 539 }
295 540
296 return true; 541 return true;
297 } 542 }
298 543
299 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { 544 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) {
300 return false; // crbug.com/127520 545 if (style_ != STYLE_NOTIFY_ON_CLICK)
546 return false; // crbug.com/127520
547
548 if (e.key_code() == ui::VKEY_SPACE)
549 HandleClickEvent();
550
551 return false;
301 } 552 }
302 553
303 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) { 554 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) {
304 if (gesture->type() == ui::ET_GESTURE_TAP) { 555 if (gesture->type() == ui::ET_GESTURE_TAP) {
305 UpdateFromModel(); 556 UpdateFromModel();
306 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH); 557 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH);
307 gesture->StopPropagation(); 558 gesture->StopPropagation();
308 return; 559 return;
309 } 560 }
310 View::OnGestureEvent(gesture); 561 View::OnGestureEvent(gesture);
311 } 562 }
312 563
313 void Combobox::OnPaint(gfx::Canvas* canvas) { 564 void Combobox::OnPaint(gfx::Canvas* canvas) {
314 OnPaintBackground(canvas); 565 switch (style_) {
315 PaintText(canvas); 566 case STYLE_SHOW_DROP_DOWN_ON_CLICK: {
316 OnPaintBorder(canvas); 567 OnPaintBackground(canvas);
568 PaintText(canvas);
569 OnPaintBorder(canvas);
570 break;
571 }
572 case STYLE_NOTIFY_ON_CLICK: {
573 PaintButtons(canvas);
574 PaintText(canvas);
575 break;
576 }
577 }
317 } 578 }
318 579
319 void Combobox::OnFocus() { 580 void Combobox::OnFocus() {
320 GetInputMethod()->OnFocus(); 581 GetInputMethod()->OnFocus();
321 text_border_->set_has_focus(true); 582 text_border_->set_has_focus(true);
322 View::OnFocus(); 583 View::OnFocus();
323 } 584 }
324 585
325 void Combobox::OnBlur() { 586 void Combobox::OnBlur() {
326 GetInputMethod()->OnBlur(); 587 GetInputMethod()->OnBlur();
327 if (selector_) 588 if (selector_)
328 selector_->OnViewBlur(); 589 selector_->OnViewBlur();
329 text_border_->set_has_focus(false); 590 text_border_->set_has_focus(false);
330 } 591 }
331 592
332 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) { 593 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) {
333 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX; 594 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX;
334 state->name = accessible_name_; 595 state->name = accessible_name_;
335 state->value = model_->GetItemAt(selected_index_); 596 state->value = model_->GetItemAt(selected_index_);
336 state->index = selected_index_; 597 state->index = selected_index_;
337 state->count = model_->GetItemCount(); 598 state->count = model_->GetItemCount();
338 } 599 }
339 600
340 void Combobox::OnModelChanged() { 601 void Combobox::OnModelChanged() {
341 ModelChanged(); 602 ModelChanged();
342 } 603 }
343 604
605 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) {
606 if (sender == text_button_) {
607 HandleClickEvent();
608 } else {
609 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when
sky 2013/12/03 20:52:30 DCHECK sender is what you expect.
hajimehoshi 2013/12/04 06:30:02 Done.
610 // cliking this while the dropdown menu is opened.
611 const base::TimeDelta delta = base::Time::Now() - closed_time_;
612 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks)
613 return;
614
615 UpdateFromModel();
616 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE;
617 if (event.IsKeyEvent())
618 source_type = ui::MENU_SOURCE_KEYBOARD;
619 else if (event.IsGestureEvent() || event.IsTouchEvent())
620 source_type = ui::MENU_SOURCE_TOUCH;
621 ShowDropDownMenu(source_type);
622 }
623 }
624
344 void Combobox::UpdateFromModel() { 625 void Combobox::UpdateFromModel() {
345 int max_width = 0; 626 int max_width = 0;
346 const gfx::Font& font = Combobox::GetFont(); 627 const gfx::Font& font = Combobox::GetFont();
347 628
348 MenuItemView* menu = new MenuItemView(this); 629 MenuItemView* menu = new MenuItemView(this);
349 // MenuRunner owns |menu|. 630 // MenuRunner owns |menu|.
350 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); 631 dropdown_list_menu_runner_.reset(new MenuRunner(menu));
351 632
352 int num_items = model()->GetItemCount(); 633 int num_items = model()->GetItemCount();
353 for (int i = 0; i < num_items; ++i) { 634 for (int i = 0; i < num_items; ++i) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 SkColor text_color = invalid() ? kInvalidTextColor : 666 SkColor text_color = invalid() ? kInvalidTextColor :
386 GetNativeTheme()->GetSystemColor( 667 GetNativeTheme()->GetSystemColor(
387 ui::NativeTheme::kColorId_LabelEnabledColor); 668 ui::NativeTheme::kColorId_LabelEnabledColor);
388 669
389 DCHECK_GE(selected_index_, 0); 670 DCHECK_GE(selected_index_, 0);
390 DCHECK_LT(selected_index_, model()->GetItemCount()); 671 DCHECK_LT(selected_index_, model()->GetItemCount());
391 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) 672 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
392 selected_index_ = 0; 673 selected_index_ = 0;
393 string16 text = model()->GetItemAt(selected_index_); 674 string16 text = model()->GetItemAt(selected_index_);
394 675
395 int disclosure_arrow_offset = width() - disclosure_arrow_->width() 676 int disclosure_arrow_offset = width() - disclosure_arrow_->width() -
396 - kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding; 677 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding();
397 678
398 const gfx::Font& font = Combobox::GetFont(); 679 const gfx::Font& font = Combobox::GetFont();
399 int text_width = font.GetStringWidth(text); 680 int text_width = font.GetStringWidth(text);
400 if ((text_width + insets.width()) > disclosure_arrow_offset) 681 if ((text_width + insets.width()) > disclosure_arrow_offset)
401 text_width = disclosure_arrow_offset - insets.width(); 682 text_width = disclosure_arrow_offset - insets.width();
402 683
403 gfx::Rect text_bounds(x, y, text_width, text_height); 684 gfx::Rect text_bounds(x, y, text_width, text_height);
404 AdjustBoundsForRTLUI(&text_bounds); 685 AdjustBoundsForRTLUI(&text_bounds);
405 canvas->DrawStringInt(text, font, text_color, text_bounds); 686 canvas->DrawStringInt(text, font, text_color, text_bounds);
406 687
407 gfx::Rect arrow_bounds(disclosure_arrow_offset + kDisclosureArrowLeftPadding, 688 int arrow_x = disclosure_arrow_offset + GetDisclosureArrowLeftPadding();
689 gfx::Rect arrow_bounds(arrow_x,
408 height() / 2 - disclosure_arrow_->height() / 2, 690 height() / 2 - disclosure_arrow_->height() / 2,
409 disclosure_arrow_->width(), 691 disclosure_arrow_->width(),
410 disclosure_arrow_->height()); 692 disclosure_arrow_->height());
411 AdjustBoundsForRTLUI(&arrow_bounds); 693 AdjustBoundsForRTLUI(&arrow_bounds);
412 694
413 SkPaint paint; 695 SkPaint paint;
414 // This makes the arrow subtractive. 696 // This makes the arrow subtractive.
415 if (invalid()) 697 if (invalid())
416 paint.setXfermodeMode(SkXfermode::kDstOut_Mode); 698 paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
417 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(), 699 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(),
418 paint); 700 paint);
419 701
420 canvas->Restore(); 702 canvas->Restore();
421 } 703 }
422 704
705 void Combobox::PaintButtons(gfx::Canvas* canvas) {
706 canvas->Save();
707 if (base::i18n::IsRTL()) {
708 canvas->Translate(gfx::Vector2d(width(), 0));
709 canvas->Scale(-1, 1);
710 }
711
712 bool focused = HasFocus() && IsAccessibilityFocusable();
sky 2013/12/03 20:52:30 You should only need check HasFocus here.
hajimehoshi 2013/12/04 06:30:02 Done.
713 const std::vector<const gfx::ImageSkia*>& text_button_images =
714 body_button_images_[focused][
715 text_button_->state() == Button::STATE_HOVERED ?
716 Button::STATE_NORMAL : text_button_->state()];
717 const std::vector<const gfx::ImageSkia*>& arrow_button_images =
718 menu_button_images_[focused][
719 arrow_button_->state() == Button::STATE_HOVERED ?
720 Button::STATE_NORMAL : arrow_button_->state()];
721
722 int text_button_width = width() - arrow_button_images[0]->width();
723 int text_button_hover_alpha =
724 text_button_->state() == Button::STATE_PRESSED ? 0 :
725 static_cast<int>(static_cast<TransparentButton*>(text_button_)->
726 GetAnimationValue() * 255);
727 if (text_button_hover_alpha < 255) {
728 canvas->SaveLayerAlpha(255 - text_button_hover_alpha);
729 PaintTextButton(canvas, text_button_images, text_button_width, height());
730 canvas->Restore();
731 }
732 if (0 < text_button_hover_alpha) {
733 canvas->SaveLayerAlpha(text_button_hover_alpha);
734 const std::vector<const gfx::ImageSkia*>& text_button_hovered_images =
735 body_button_images_[focused][Button::STATE_HOVERED];
736 PaintTextButton(canvas, text_button_hovered_images,
737 text_button_width, height());
738 canvas->Restore();
739 }
740
741 int arrow_button_x = text_button_width;
742 int arrow_button_hover_alpha =
743 arrow_button_->state() == Button::STATE_PRESSED ? 0 :
744 static_cast<int>(static_cast<TransparentButton*>(arrow_button_)->
745 GetAnimationValue() * 255);
746 if (arrow_button_hover_alpha < 255) {
747 canvas->SaveLayerAlpha(255 - arrow_button_hover_alpha);
748 PaintArrowButton(canvas, arrow_button_images, arrow_button_x, height());
749 canvas->Restore();
750 }
751 if (0 < arrow_button_hover_alpha) {
752 canvas->SaveLayerAlpha(arrow_button_hover_alpha);
753 const std::vector<const gfx::ImageSkia*>& arrow_button_hovered_images =
754 menu_button_images_[focused][Button::STATE_HOVERED];
755 PaintArrowButton(canvas, arrow_button_hovered_images,
756 arrow_button_x, height());
757 canvas->Restore();
758 }
759
760 canvas->Restore();
761 }
762
423 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { 763 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) {
424 if (!dropdown_list_menu_runner_.get()) 764 if (!dropdown_list_menu_runner_.get())
425 UpdateFromModel(); 765 UpdateFromModel();
426 766
427 // Extend the menu to the width of the combobox. 767 // Extend the menu to the width of the combobox.
428 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); 768 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu();
429 SubmenuView* submenu = menu->CreateSubmenu(); 769 SubmenuView* submenu = menu->CreateSubmenu();
430 submenu->set_minimum_preferred_width(size().width() - 770 submenu->set_minimum_preferred_width(
431 (kMenuBorderWidthLeft + kMenuBorderWidthRight)); 771 size().width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
432 772
433 gfx::Rect lb = GetLocalBounds(); 773 gfx::Rect lb = GetLocalBounds();
434 gfx::Point menu_position(lb.origin()); 774 gfx::Point menu_position(lb.origin());
435 775
436 // Inset the menu's requested position so the border of the menu lines up 776 // Inset the menu's requested position so the border of the menu lines up
437 // with the border of the combobox. 777 // with the border of the combobox.
438 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); 778 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft);
439 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop); 779 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop);
440 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); 780 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
441 781
442 View::ConvertPointToScreen(this, &menu_position); 782 View::ConvertPointToScreen(this, &menu_position);
443 if (menu_position.x() < 0) 783 if (menu_position.x() < 0)
444 menu_position.set_x(0); 784 menu_position.set_x(0);
445 785
446 gfx::Rect bounds(menu_position, lb.size()); 786 gfx::Rect bounds(menu_position, lb.size());
447 787
788 Button::ButtonState original_state = arrow_button_->state();
789 arrow_button_->SetState(Button::STATE_PRESSED);
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 arrow_button_->SetState(original_state);
454 closed_time_ = base::Time::Now(); 798 closed_time_ = base::Time::Now();
455 799
456 // Need to explicitly clear mouse handler so that events get sent 800 // 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 801 // 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. 802 // the first click to other parts of the UI is eaten.
459 SetMouseHandler(NULL); 803 SetMouseHandler(NULL);
460 } 804 }
461 805
462 void Combobox::OnSelectionChanged() { 806 void Combobox::OnSelectionChanged() {
463 if (listener_) 807 if (listener_)
464 listener_->OnSelectedIndexChanged(this); 808 listener_->OnSelectedIndexChanged(this);
465 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); 809 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false);
466 SchedulePaint(); 810 SchedulePaint();
467 } 811 }
468 812
469 int Combobox::MenuCommandToIndex(int menu_command_id) const { 813 int Combobox::MenuCommandToIndex(int menu_command_id) const {
470 // (note that the id received is offset by kFirstMenuItemId) 814 // (note that the id received is offset by kFirstMenuItemId)
471 // Revert menu ID offset to map back to combobox model. 815 // Revert menu ID offset to map back to combobox model.
472 int index = menu_command_id - kFirstMenuItemId; 816 int index = menu_command_id - kFirstMenuItemId;
473 DCHECK_LT(index, model()->GetItemCount()); 817 DCHECK_LT(index, model()->GetItemCount());
474 return index; 818 return index;
475 } 819 }
476 820
821 int Combobox::GetDisclosureArrowLeftPadding() const {
822 switch (style_) {
823 case STYLE_SHOW_DROP_DOWN_ON_CLICK:
824 return kDisclosureArrowLeftPadding;
825 case STYLE_NOTIFY_ON_CLICK:
826 return kDisclosureArrowButtonLeftPadding;
827 }
828 NOTREACHED();
829 return 0;
830 }
831
832 int Combobox::GetDisclosureArrowRightPadding() const {
833 switch (style_) {
834 case STYLE_SHOW_DROP_DOWN_ON_CLICK:
835 return kDisclosureArrowRightPadding;
836 case STYLE_NOTIFY_ON_CLICK:
837 return kDisclosureArrowButtonRightPadding;
838 }
839 NOTREACHED();
840 return 0;
841 }
842
843 void Combobox::HandleClickEvent() {
844 if (style_ != STYLE_NOTIFY_ON_CLICK)
845 return;
846
847 if (listener_)
848 listener_->OnComboboxTextButtonClicked(this);
849 }
850
477 } // namespace views 851 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698