Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / menu / menu_message_loop_aura.cc
blob3ed33281aa6f43e8b99a301acb33af9aef39515f
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/controls/menu/menu_message_loop_aura.h"
7 #if defined(OS_WIN)
8 #include <windowsx.h>
9 #endif
11 #include "base/run_loop.h"
12 #include "ui/aura/client/screen_position_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/aura/window_tree_host.h"
17 #include "ui/events/event.h"
18 #include "ui/events/platform/platform_event_source.h"
19 #include "ui/events/platform/scoped_event_dispatcher.h"
20 #include "ui/views/controls/menu/menu_controller.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/wm/public/activation_change_observer.h"
23 #include "ui/wm/public/activation_client.h"
24 #include "ui/wm/public/dispatcher_client.h"
25 #include "ui/wm/public/drag_drop_client.h"
27 #if defined(OS_WIN)
28 #include "ui/base/win/internal_constants.h"
29 #include "ui/views/controls/menu/menu_message_pump_dispatcher_win.h"
30 #include "ui/views/win/hwnd_util.h"
31 #else
32 #include "ui/views/controls/menu/menu_event_dispatcher.h"
33 #endif
35 using aura::client::ScreenPositionClient;
37 namespace views {
39 namespace {
41 aura::Window* GetOwnerRootWindow(views::Widget* owner) {
42 return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL;
45 // ActivationChangeObserverImpl is used to observe activation changes and close
46 // the menu. Additionally it listens for the root window to be destroyed and
47 // cancel the menu as well.
48 class ActivationChangeObserverImpl
49 : public aura::client::ActivationChangeObserver,
50 public aura::WindowObserver,
51 public ui::EventHandler {
52 public:
53 ActivationChangeObserverImpl(MenuController* controller, aura::Window* root)
54 : controller_(controller), root_(root) {
55 aura::client::GetActivationClient(root_)->AddObserver(this);
56 root_->AddObserver(this);
57 root_->AddPreTargetHandler(this);
60 ~ActivationChangeObserverImpl() override { Cleanup(); }
62 // aura::client::ActivationChangeObserver:
63 void OnWindowActivated(
64 aura::client::ActivationChangeObserver::ActivationReason reason,
65 aura::Window* gained_active,
66 aura::Window* lost_active) override {
67 if (!controller_->drag_in_progress())
68 controller_->CancelAll();
71 // aura::WindowObserver:
72 void OnWindowDestroying(aura::Window* window) override { Cleanup(); }
74 // ui::EventHandler:
75 void OnCancelMode(ui::CancelModeEvent* event) override {
76 controller_->CancelAll();
79 private:
80 void Cleanup() {
81 if (!root_)
82 return;
83 // The ActivationClient may have been destroyed by the time we get here.
84 aura::client::ActivationClient* client =
85 aura::client::GetActivationClient(root_);
86 if (client)
87 client->RemoveObserver(this);
88 root_->RemovePreTargetHandler(this);
89 root_->RemoveObserver(this);
90 root_ = NULL;
93 MenuController* controller_;
94 aura::Window* root_;
96 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl);
99 } // namespace
101 // static
102 MenuMessageLoop* MenuMessageLoop::Create() {
103 return new MenuMessageLoopAura;
106 MenuMessageLoopAura::MenuMessageLoopAura() : owner_(NULL) {
109 MenuMessageLoopAura::~MenuMessageLoopAura() {
112 void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent& event,
113 gfx::NativeWindow window,
114 const gfx::Point& screen_loc) {
115 aura::Window* root = window->GetRootWindow();
116 ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root);
117 if (!spc)
118 return;
120 gfx::Point root_loc(screen_loc);
121 spc->ConvertPointFromScreen(root, &root_loc);
123 ui::MouseEvent clone(static_cast<const ui::MouseEvent&>(event));
124 clone.set_location(root_loc);
125 clone.set_root_location(root_loc);
126 root->GetHost()->dispatcher()->RepostEvent(clone);
129 void MenuMessageLoopAura::Run(MenuController* controller,
130 Widget* owner,
131 bool nested_menu) {
132 // |owner_| may be NULL.
133 owner_ = owner;
134 aura::Window* root = GetOwnerRootWindow(owner_);
135 // It is possible for the same MenuMessageLoopAura to start a nested
136 // message-loop while it is already running a nested loop. So make sure the
137 // quit-closure gets reset to the outer loop's quit-closure once the innermost
138 // loop terminates.
139 base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_,
140 base::Closure());
142 #if defined(OS_WIN)
143 internal::MenuMessagePumpDispatcher nested_dispatcher(controller);
144 if (root) {
145 scoped_ptr<ActivationChangeObserverImpl> observer;
146 if (!nested_menu)
147 observer.reset(new ActivationChangeObserverImpl(controller, root));
148 aura::client::DispatcherRunLoop run_loop(
149 aura::client::GetDispatcherClient(root), &nested_dispatcher);
150 message_loop_quit_ = run_loop.QuitClosure();
151 run_loop.Run();
152 } else {
153 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
154 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
155 base::RunLoop run_loop(&nested_dispatcher);
156 message_loop_quit_ = run_loop.QuitClosure();
157 run_loop.Run();
159 #else
160 internal::MenuEventDispatcher event_dispatcher(controller);
161 scoped_ptr<ui::ScopedEventDispatcher> dispatcher_override;
162 if (ui::PlatformEventSource::GetInstance()) {
163 dispatcher_override =
164 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
165 &event_dispatcher);
167 if (root) {
168 scoped_ptr<ActivationChangeObserverImpl> observer;
169 if (!nested_menu)
170 observer.reset(new ActivationChangeObserverImpl(controller, root));
171 aura::client::DispatcherRunLoop run_loop(
172 aura::client::GetDispatcherClient(root), NULL);
173 message_loop_quit_ = run_loop.QuitClosure();
174 run_loop.Run();
175 } else {
176 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
177 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
178 base::RunLoop run_loop;
179 message_loop_quit_ = run_loop.QuitClosure();
180 run_loop.Run();
182 #endif
185 void MenuMessageLoopAura::QuitNow() {
186 CHECK(!message_loop_quit_.is_null());
187 message_loop_quit_.Run();
189 #if !defined(OS_WIN)
190 // Ask PlatformEventSource to stop dispatching events in this message loop
191 // iteration. We want our menu's loop to return before the next event.
192 if (ui::PlatformEventSource::GetInstance())
193 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream();
194 #endif
197 void MenuMessageLoopAura::ClearOwner() {
198 owner_ = NULL;
201 } // namespace views