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/menu/menu_runner.h" | 5 #include "ui/views/controls/menu/menu_runner.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "ui/base/models/menu_model.h" | 10 #include "ui/base/models/menu_model.h" |
11 #include "ui/views/controls/button/menu_button.h" | 11 #include "ui/views/controls/button/menu_button.h" |
12 #include "ui/views/controls/menu/menu_controller.h" | 12 #include "ui/views/controls/menu/menu_controller.h" |
13 #include "ui/views/controls/menu/menu_controller_delegate.h" | 13 #include "ui/views/controls/menu/menu_controller_delegate.h" |
14 #include "ui/views/controls/menu/menu_delegate.h" | 14 #include "ui/views/controls/menu/menu_delegate.h" |
| 15 #include "ui/views/controls/menu/menu_item_view.h" |
15 #include "ui/views/controls/menu/menu_model_adapter.h" | 16 #include "ui/views/controls/menu/menu_model_adapter.h" |
16 #include "ui/views/controls/menu/menu_runner_handler.h" | 17 #include "ui/views/controls/menu/menu_runner_handler.h" |
17 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
18 | 19 |
19 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
20 #include "base/win/win_util.h" | 21 #include "base/win/win_util.h" |
21 #endif | 22 #endif |
22 | 23 |
23 namespace views { | 24 namespace views { |
24 | 25 |
(...skipping 11 matching lines...) Expand all Loading... |
36 | 37 |
37 bool running() const { return running_; } | 38 bool running() const { return running_; } |
38 | 39 |
39 // See description above class for details. | 40 // See description above class for details. |
40 void Release(); | 41 void Release(); |
41 | 42 |
42 // Runs the menu. | 43 // Runs the menu. |
43 MenuRunner::RunResult RunMenuAt(Widget* parent, | 44 MenuRunner::RunResult RunMenuAt(Widget* parent, |
44 MenuButton* button, | 45 MenuButton* button, |
45 const gfx::Rect& bounds, | 46 const gfx::Rect& bounds, |
46 MenuItemView::AnchorPosition anchor, | 47 MenuAnchorPosition anchor, |
47 int32 types) WARN_UNUSED_RESULT; | 48 int32 types) WARN_UNUSED_RESULT; |
48 | 49 |
49 void Cancel(); | 50 void Cancel(); |
50 | 51 |
51 // Returns the time from the event which closed the menu - or 0. | 52 // Returns the time from the event which closed the menu - or 0. |
52 base::TimeDelta closing_event_time() const; | 53 base::TimeDelta closing_event_time() const; |
53 | 54 |
54 // MenuControllerDelegate: | 55 // MenuControllerDelegate: |
55 virtual void DropMenuClosed(NotifyType type, MenuItemView* menu) OVERRIDE; | 56 virtual void DropMenuClosed(NotifyType type, MenuItemView* menu) OVERRIDE; |
56 virtual void SiblingMenuCreated(MenuItemView* menu) OVERRIDE; | 57 virtual void SiblingMenuCreated(MenuItemView* menu) OVERRIDE; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 DCHECK(controller_); | 134 DCHECK(controller_); |
134 // Release is invoked when MenuRunner is destroyed. Assume this is happening | 135 // Release is invoked when MenuRunner is destroyed. Assume this is happening |
135 // because the object referencing the menu has been destroyed and the menu | 136 // because the object referencing the menu has been destroyed and the menu |
136 // button is no longer valid. | 137 // button is no longer valid. |
137 controller_->Cancel(MenuController::EXIT_DESTROYED); | 138 controller_->Cancel(MenuController::EXIT_DESTROYED); |
138 } else { | 139 } else { |
139 delete this; | 140 delete this; |
140 } | 141 } |
141 } | 142 } |
142 | 143 |
143 MenuRunner::RunResult MenuRunnerImpl::RunMenuAt( | 144 MenuRunner::RunResult MenuRunnerImpl::RunMenuAt(Widget* parent, |
144 Widget* parent, | 145 MenuButton* button, |
145 MenuButton* button, | 146 const gfx::Rect& bounds, |
146 const gfx::Rect& bounds, | 147 MenuAnchorPosition anchor, |
147 MenuItemView::AnchorPosition anchor, | 148 int32 types) { |
148 int32 types) { | |
149 closing_event_time_ = base::TimeDelta(); | 149 closing_event_time_ = base::TimeDelta(); |
150 if (running_) { | 150 if (running_) { |
151 // Ignore requests to show the menu while it's already showing. MenuItemView | 151 // Ignore requests to show the menu while it's already showing. MenuItemView |
152 // doesn't handle this very well (meaning it crashes). | 152 // doesn't handle this very well (meaning it crashes). |
153 return MenuRunner::NORMAL_EXIT; | 153 return MenuRunner::NORMAL_EXIT; |
154 } | 154 } |
155 | 155 |
156 MenuController* controller = MenuController::GetActiveInstance(); | 156 MenuController* controller = MenuController::GetActiveInstance(); |
157 if (controller) { | 157 if (controller) { |
158 if ((types & MenuRunner::IS_NESTED) != 0) { | 158 if ((types & MenuRunner::IS_NESTED) != 0) { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 holder_->Release(); | 293 holder_->Release(); |
294 } | 294 } |
295 | 295 |
296 MenuItemView* MenuRunner::GetMenu() { | 296 MenuItemView* MenuRunner::GetMenu() { |
297 return holder_->menu(); | 297 return holder_->menu(); |
298 } | 298 } |
299 | 299 |
300 MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent, | 300 MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent, |
301 MenuButton* button, | 301 MenuButton* button, |
302 const gfx::Rect& bounds, | 302 const gfx::Rect& bounds, |
303 MenuItemView::AnchorPosition anchor, | 303 MenuAnchorPosition anchor, |
304 ui::MenuSourceType source_type, | 304 ui::MenuSourceType source_type, |
305 int32 types) { | 305 int32 types) { |
306 if (runner_handler_.get()) { | 306 if (runner_handler_.get()) { |
307 return runner_handler_->RunMenuAt(parent, button, bounds, anchor, | 307 return runner_handler_->RunMenuAt(parent, button, bounds, anchor, |
308 source_type, types); | 308 source_type, types); |
309 } | 309 } |
310 | 310 |
311 // The parent of the nested menu will have created a DisplayChangeListener, so | 311 // The parent of the nested menu will have created a DisplayChangeListener, so |
312 // we avoid creating a DisplayChangeListener if nested. Drop menus are | 312 // we avoid creating a DisplayChangeListener if nested. Drop menus are |
313 // transient, so we don't cancel in that case. | 313 // transient, so we don't cancel in that case. |
314 if ((types & (IS_NESTED | FOR_DROP)) == 0 && parent) { | 314 if ((types & (IS_NESTED | FOR_DROP)) == 0 && parent) { |
315 display_change_listener_.reset( | 315 display_change_listener_.reset( |
316 internal::DisplayChangeListener::Create(parent, this)); | 316 internal::DisplayChangeListener::Create(parent, this)); |
317 } | 317 } |
318 | 318 |
319 if (types & CONTEXT_MENU) { | 319 if (types & CONTEXT_MENU) { |
320 switch (source_type) { | 320 switch (source_type) { |
321 case ui::MENU_SOURCE_NONE: | 321 case ui::MENU_SOURCE_NONE: |
322 case ui::MENU_SOURCE_KEYBOARD: | 322 case ui::MENU_SOURCE_KEYBOARD: |
323 case ui::MENU_SOURCE_MOUSE: | 323 case ui::MENU_SOURCE_MOUSE: |
324 anchor = MenuItemView::TOPLEFT; | 324 anchor = MENU_ANCHOR_TOPLEFT; |
325 break; | 325 break; |
326 case ui::MENU_SOURCE_TOUCH: | 326 case ui::MENU_SOURCE_TOUCH: |
327 case ui::MENU_SOURCE_TOUCH_EDIT_MENU: | 327 case ui::MENU_SOURCE_TOUCH_EDIT_MENU: |
328 anchor = MenuItemView::BOTTOMCENTER; | 328 anchor = MENU_ANCHOR_BOTTOMCENTER; |
329 break; | 329 break; |
330 default: | 330 default: |
331 break; | 331 break; |
332 } | 332 } |
333 } | 333 } |
334 | 334 |
335 return holder_->RunMenuAt(parent, button, bounds, anchor, types); | 335 return holder_->RunMenuAt(parent, button, bounds, anchor, types); |
336 } | 336 } |
337 | 337 |
338 bool MenuRunner::IsRunning() const { | 338 bool MenuRunner::IsRunning() const { |
339 return holder_->running(); | 339 return holder_->running(); |
340 } | 340 } |
341 | 341 |
342 void MenuRunner::Cancel() { | 342 void MenuRunner::Cancel() { |
343 holder_->Cancel(); | 343 holder_->Cancel(); |
344 } | 344 } |
345 | 345 |
346 base::TimeDelta MenuRunner::closing_event_time() const { | 346 base::TimeDelta MenuRunner::closing_event_time() const { |
347 return holder_->closing_event_time(); | 347 return holder_->closing_event_time(); |
348 } | 348 } |
349 | 349 |
350 void MenuRunner::SetRunnerHandler( | 350 void MenuRunner::SetRunnerHandler( |
351 scoped_ptr<MenuRunnerHandler> runner_handler) { | 351 scoped_ptr<MenuRunnerHandler> runner_handler) { |
352 runner_handler_ = runner_handler.Pass(); | 352 runner_handler_ = runner_handler.Pass(); |
353 } | 353 } |
354 | 354 |
355 } // namespace views | 355 } // namespace views |
OLD | NEW |