Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / aura_demo / window_tree_host_mojo.cc
1 // Copyright (c) 2013 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.
4
5 #include "mojo/examples/aura_demo/window_tree_host_mojo.h"
6
7 #include "mojo/examples/aura_demo/demo_context_factory.h"
8 #include "mojo/public/bindings/allocation_scope.h"
9 #include "mojo/public/gles2/gles2.h"
10 #include "mojo/services/native_viewport/geometry_conversions.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_tree_host_delegate.h"
14 #include "ui/compositor/compositor.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/gfx/geometry/insets.h"
18 #include "ui/gfx/geometry/rect.h"
19
20 namespace mojo {
21 namespace examples {
22
23 // static
24 ui::ContextFactory* WindowTreeHostMojo::context_factory_ = NULL;
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // WindowTreeHostMojo, public:
28
29 WindowTreeHostMojo::WindowTreeHostMojo(
30     ScopedNativeViewportHandle viewport_handle,
31     const gfx::Rect& bounds,
32     const base::Callback<void()>& compositor_created_callback)
33     : native_viewport_(viewport_handle.Pass(), this),
34       compositor_created_callback_(compositor_created_callback),
35       bounds_(bounds) {
36   AllocationScope scope;
37   native_viewport_->Create(bounds);
38
39   ScopedMessagePipeHandle gles2_client_handle;
40   CreateMessagePipe(&gles2_handle_, &gles2_client_handle);
41
42   // The ContextFactory must exist before any Compositors are created.
43   if (!context_factory_) {
44     scoped_ptr<DemoContextFactory> cf(new DemoContextFactory(this));
45     if (cf->Initialize())
46       context_factory_ = cf.release();
47     ui::ContextFactory::SetInstance(context_factory_);
48   }
49   CHECK(context_factory_) << "No GL bindings.";
50
51   native_viewport_->CreateGLES2Context(gles2_client_handle.Pass());
52 }
53
54 WindowTreeHostMojo::~WindowTreeHostMojo() {}
55
56 ////////////////////////////////////////////////////////////////////////////////
57 // WindowTreeHostMojo, aura::WindowTreeHost implementation:
58
59 aura::RootWindow* WindowTreeHostMojo::GetRootWindow() {
60   return delegate_->AsRootWindow();
61 }
62
63 gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
64   NOTIMPLEMENTED() << "GetAcceleratedWidget";
65   return gfx::kNullAcceleratedWidget;
66 }
67
68 void WindowTreeHostMojo::Show() {
69   window()->Show();
70   native_viewport_->Show();
71 }
72
73 void WindowTreeHostMojo::Hide() {
74   native_viewport_->Hide();
75   window()->Hide();
76 }
77
78 void WindowTreeHostMojo::ToggleFullScreen() {
79   NOTIMPLEMENTED();
80 }
81
82 gfx::Rect WindowTreeHostMojo::GetBounds() const {
83   return bounds_;
84 }
85
86 void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) {
87   AllocationScope scope;
88   native_viewport_->SetBounds(bounds);
89 }
90
91 gfx::Insets WindowTreeHostMojo::GetInsets() const {
92   NOTIMPLEMENTED();
93   return gfx::Insets();
94 }
95
96 void WindowTreeHostMojo::SetInsets(const gfx::Insets& insets) {
97   NOTIMPLEMENTED();
98 }
99
100 gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const {
101   return gfx::Point(0, 0);
102 }
103
104 void WindowTreeHostMojo::SetCapture() {
105   NOTIMPLEMENTED();
106 }
107
108 void WindowTreeHostMojo::ReleaseCapture() {
109   NOTIMPLEMENTED();
110 }
111
112 bool WindowTreeHostMojo::QueryMouseLocation(gfx::Point* location_return) {
113   NOTIMPLEMENTED() << "QueryMouseLocation";
114   return false;
115 }
116
117 bool WindowTreeHostMojo::ConfineCursorToRootWindow() {
118   NOTIMPLEMENTED();
119   return false;
120 }
121
122 void WindowTreeHostMojo::UnConfineCursor() {
123   NOTIMPLEMENTED();
124 }
125
126 void WindowTreeHostMojo::PostNativeEvent(
127     const base::NativeEvent& native_event) {
128   NOTIMPLEMENTED();
129 }
130
131 void WindowTreeHostMojo::OnDeviceScaleFactorChanged(float device_scale_factor) {
132   NOTIMPLEMENTED();
133 }
134
135 void WindowTreeHostMojo::PrepareForShutdown() {
136   NOTIMPLEMENTED();
137 }
138
139 void WindowTreeHostMojo::SetCursorNative(gfx::NativeCursor cursor) {
140   NOTIMPLEMENTED();
141 }
142
143 void WindowTreeHostMojo::MoveCursorToNative(const gfx::Point& location) {
144   NOTIMPLEMENTED();
145 }
146
147 void WindowTreeHostMojo::OnCursorVisibilityChangedNative(bool show) {
148   NOTIMPLEMENTED();
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////
152 // WindowTreeHostMojo, ui::EventSource implementation:
153
154 ui::EventProcessor* WindowTreeHostMojo::GetEventProcessor() {
155   return delegate_->GetEventProcessor();
156 }
157
158 ////////////////////////////////////////////////////////////////////////////////
159 // WindowTreeHostMojo, NativeViewportClient implementation:
160
161 void WindowTreeHostMojo::OnCreated() {
162   CreateCompositor(GetAcceleratedWidget());
163   compositor_created_callback_.Run();
164 }
165
166 void WindowTreeHostMojo::OnBoundsChanged(const Rect& bounds) {
167   bounds_ = gfx::Rect(bounds.position().x(), bounds.position().y(),
168                       bounds.size().width(), bounds.size().height());
169   if (delegate_)
170     window()->SetBounds(gfx::Rect(bounds_.size()));
171   NotifyHostResized(bounds_.size());
172 }
173
174 void WindowTreeHostMojo::OnDestroyed() {
175   base::MessageLoop::current()->Quit();
176 }
177
178 void WindowTreeHostMojo::OnEvent(const Event& event) {
179   if (!event.location().is_null())
180     native_viewport_->AckEvent(event);
181
182   switch (event.action()) {
183     case ui::ET_MOUSE_PRESSED:
184     case ui::ET_MOUSE_DRAGGED:
185     case ui::ET_MOUSE_RELEASED:
186     case ui::ET_MOUSE_MOVED:
187     case ui::ET_MOUSE_ENTERED:
188     case ui::ET_MOUSE_EXITED: {
189       gfx::Point location(event.location().x(), event.location().y());
190       ui::MouseEvent ev(static_cast<ui::EventType>(event.action()), location,
191                         location, event.flags(), 0);
192       SendEventToProcessor(&ev);
193       break;
194     }
195     case ui::ET_KEY_PRESSED:
196     case ui::ET_KEY_RELEASED: {
197       ui::KeyEvent ev(
198           static_cast<ui::EventType>(event.action()),
199           static_cast<ui::KeyboardCode>(event.key_data().key_code()),
200           event.flags(), event.key_data().is_char());
201       SendEventToProcessor(&ev);
202       break;
203     }
204     // TODO(beng): touch, etc.
205   }
206 };
207
208 }  // namespace examples
209 }  // namespace mojo