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

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

Issue 7720012: Moves ownership of MenuItemView to MenuRunner as well as responbility (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit test Created 9 years, 4 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 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 "views/controls/combobox/native_combobox_views.h" 5 #include "views/controls/combobox/native_combobox_views.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "ui/base/keycodes/keyboard_codes.h" 10 #include "ui/base/keycodes/keyboard_codes.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/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/canvas_skia.h" 14 #include "ui/gfx/canvas_skia.h"
15 #include "ui/gfx/font.h" 15 #include "ui/gfx/font.h"
16 #include "ui/gfx/path.h" 16 #include "ui/gfx/path.h"
17 #include "views/background.h" 17 #include "views/background.h"
18 #include "views/border.h" 18 #include "views/border.h"
19 #include "views/controls/combobox/combobox.h" 19 #include "views/controls/combobox/combobox.h"
20 #include "views/controls/focusable_border.h" 20 #include "views/controls/focusable_border.h"
21 #include "views/controls/menu/menu_runner.h"
21 #include "views/controls/menu/submenu_view.h" 22 #include "views/controls/menu/submenu_view.h"
22 #include "views/widget/root_view.h" 23 #include "views/widget/root_view.h"
23 #include "views/widget/widget.h" 24 #include "views/widget/widget.h"
24 25
25 #if defined(OS_LINUX) 26 #if defined(OS_LINUX)
26 #include "ui/gfx/gtk_util.h" 27 #include "ui/gfx/gtk_util.h"
27 #endif 28 #endif
28 29
29 namespace { 30 namespace {
30 31
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 NOTREACHED(); 157 NOTREACHED();
157 } 158 }
158 159
159 ///////////////////////////////////////////////////////////////// 160 /////////////////////////////////////////////////////////////////
160 // NativeComboboxViews, NativeComboboxWrapper overrides: 161 // NativeComboboxViews, NativeComboboxWrapper overrides:
161 162
162 void NativeComboboxViews::UpdateFromModel() { 163 void NativeComboboxViews::UpdateFromModel() {
163 int max_width = 0; 164 int max_width = 0;
164 const gfx::Font &font = GetFont(); 165 const gfx::Font &font = GetFont();
165 166
166 dropdown_list_menu_.reset(new MenuItemView(this)); 167 MenuItemView* menu = new MenuItemView(this);
168 // MenuRunner owns |menu|.
169 dropdown_list_menu_runner_.reset(new MenuRunner(menu));
167 170
168 int num_items = combobox_->model()->GetItemCount(); 171 int num_items = combobox_->model()->GetItemCount();
169 for (int i = 0; i < num_items; ++i) { 172 for (int i = 0; i < num_items; ++i) {
170 string16 text = combobox_->model()->GetItemAt(i); 173 string16 text = combobox_->model()->GetItemAt(i);
171 174
172 // Inserting the Unicode formatting characters if necessary so that the 175 // Inserting the Unicode formatting characters if necessary so that the
173 // text is displayed correctly in right-to-left UIs. 176 // text is displayed correctly in right-to-left UIs.
174 base::i18n::AdjustStringForLocaleDirection(&text); 177 base::i18n::AdjustStringForLocaleDirection(&text);
175 178
176 dropdown_list_menu_->AppendMenuItem(i + kFirstMenuItemId, UTF16ToWide(text), 179 menu->AppendMenuItem(i + kFirstMenuItemId, UTF16ToWide(text),
177 MenuItemView::NORMAL); 180 MenuItemView::NORMAL);
178 max_width = std::max(max_width, font.GetStringWidth(text)); 181 max_width = std::max(max_width, font.GetStringWidth(text));
179 } 182 }
180 183
181 content_width_ = max_width; 184 content_width_ = max_width;
182 content_height_ = font.GetFontSize(); 185 content_height_ = font.GetFontSize();
183 } 186 }
184 187
185 void NativeComboboxViews::UpdateSelectedItem() { 188 void NativeComboboxViews::UpdateSelectedItem() {
186 selected_item_ = combobox_->selected_item(); 189 selected_item_ = combobox_->selected_item();
187 } 190 }
188 191
189 void NativeComboboxViews::UpdateEnabled() { 192 void NativeComboboxViews::UpdateEnabled() {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 lb.width() - kComboboxArrowSize - kComboboxArrowMargin, 338 lb.width() - kComboboxArrowSize - kComboboxArrowMargin,
336 kTopInsetSize, 339 kTopInsetSize,
337 lb.width() - kComboboxArrowSize - kComboboxArrowMargin, 340 lb.width() - kComboboxArrowSize - kComboboxArrowMargin,
338 lb.height() - kBottomInsetSize); 341 lb.height() - kBottomInsetSize);
339 342
340 canvas->Restore(); 343 canvas->Restore();
341 } 344 }
342 345
343 void NativeComboboxViews::ShowDropDownMenu() { 346 void NativeComboboxViews::ShowDropDownMenu() {
344 347
345 if (!dropdown_list_menu_.get()) 348 if (!dropdown_list_menu_runner_.get())
346 UpdateFromModel(); 349 UpdateFromModel();
347 350
348 // Extend the menu to the width of the combobox. 351 // Extend the menu to the width of the combobox.
349 SubmenuView* submenu = dropdown_list_menu_->CreateSubmenu(); 352 SubmenuView* submenu = dropdown_list_menu_runner_->GetMenu()->CreateSubmenu();
350 submenu->set_minimum_preferred_width(size().width()); 353 submenu->set_minimum_preferred_width(size().width());
351 354
352 gfx::Rect lb = GetLocalBounds(); 355 gfx::Rect lb = GetLocalBounds();
353 gfx::Point menu_position(lb.origin()); 356 gfx::Point menu_position(lb.origin());
354 View::ConvertPointToScreen(this, &menu_position); 357 View::ConvertPointToScreen(this, &menu_position);
355 if (menu_position.x() < 0) 358 if (menu_position.x() < 0)
356 menu_position.set_x(0); 359 menu_position.set_x(0);
357 360
358 gfx::Rect bounds(menu_position, lb.size()); 361 gfx::Rect bounds(menu_position, lb.size());
359 362
360 dropdown_open_ = true; 363 dropdown_open_ = true;
361 dropdown_list_menu_->RunMenuAt(NULL, NULL, bounds, MenuItemView::TOPLEFT, 364 if (dropdown_list_menu_runner_->RunMenuAt(
362 true); 365 NULL, NULL, bounds, MenuItemView::TOPLEFT,
366 MenuRunner::HAS_MNEMONICS) == MenuRunner::MENU_DELETED)
367 return;
363 dropdown_open_ = false; 368 dropdown_open_ = false;
364 369
365 // Need to explicitly clear mouse handler so that events get sent 370 // Need to explicitly clear mouse handler so that events get sent
366 // properly after the menu finishes running. If we don't do this, then 371 // properly after the menu finishes running. If we don't do this, then
367 // the first click to other parts of the UI is eaten. 372 // the first click to other parts of the UI is eaten.
368 SetMouseHandler(NULL); 373 SetMouseHandler(NULL);
369 } 374 }
370 375
371 } // namespace views 376 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698