Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / aura_demo / aura_demo.cc
1 // Copyright 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 <stdio.h>
6 #include <string>
7
8 #include "base/at_exit.h"
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "mojo/aura/screen_mojo.h"
12 #include "mojo/aura/window_tree_host_mojo.h"
13 #include "mojo/public/cpp/bindings/allocation_scope.h"
14 #include "mojo/public/cpp/gles2/gles2.h"
15 #include "mojo/public/cpp/shell/application.h"
16 #include "mojo/public/cpp/system/core.h"
17 #include "mojo/public/interfaces/shell/shell.mojom.h"
18 #include "mojo/services/native_viewport/native_viewport.mojom.h"
19 #include "ui/aura/client/default_capture_client.h"
20 #include "ui/aura/client/window_tree_client.h"
21 #include "ui/aura/env.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_delegate.h"
24 #include "ui/base/hit_test.h"
25 #include "ui/gfx/canvas.h"
26
27 #if defined(WIN32)
28 #if !defined(CDECL)
29 #define CDECL __cdecl
30 #endif
31 #define AURA_DEMO_EXPORT __declspec(dllexport)
32 #else
33 #define CDECL
34 #define AURA_DEMO_EXPORT __attribute__((visibility("default")))
35 #endif
36
37 namespace mojo {
38 namespace examples {
39
40 // Trivial WindowDelegate implementation that draws a colored background.
41 class DemoWindowDelegate : public aura::WindowDelegate {
42  public:
43   explicit DemoWindowDelegate(SkColor color) : color_(color) {}
44
45   // Overridden from WindowDelegate:
46   virtual gfx::Size GetMinimumSize() const OVERRIDE {
47     return gfx::Size();
48   }
49
50   virtual gfx::Size GetMaximumSize() const OVERRIDE {
51     return gfx::Size();
52   }
53
54   virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
55                                const gfx::Rect& new_bounds) OVERRIDE {}
56   virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
57     return gfx::kNullCursor;
58   }
59   virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
60     return HTCAPTION;
61   }
62   virtual bool ShouldDescendIntoChildForEventHandling(
63       aura::Window* child,
64       const gfx::Point& location) OVERRIDE {
65     return true;
66   }
67   virtual bool CanFocus() OVERRIDE { return true; }
68   virtual void OnCaptureLost() OVERRIDE {}
69   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
70     canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
71   }
72   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
73   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
74   virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
75   virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
76   virtual bool HasHitTestMask() const OVERRIDE { return false; }
77   virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
78
79  private:
80   SkColor color_;
81
82   DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
83 };
84
85 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
86  public:
87   explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
88     aura::client::SetWindowTreeClient(window_, this);
89   }
90
91   virtual ~DemoWindowTreeClient() {
92     aura::client::SetWindowTreeClient(window_, NULL);
93   }
94
95   // Overridden from aura::client::WindowTreeClient:
96   virtual aura::Window* GetDefaultParent(aura::Window* context,
97                                          aura::Window* window,
98                                          const gfx::Rect& bounds) OVERRIDE {
99     if (!capture_client_) {
100       capture_client_.reset(
101           new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
102     }
103     return window_;
104   }
105
106  private:
107   aura::Window* window_;
108
109   scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
110
111   DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
112 };
113
114 class AuraDemo : public Application {
115  public:
116   explicit AuraDemo(MojoHandle shell_handle) : Application(shell_handle) {
117     screen_.reset(ScreenMojo::Create());
118     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
119
120     NativeViewportPtr native_viewport;
121     ConnectTo("mojo:mojo_native_viewport_service", &native_viewport);
122
123     window_tree_host_.reset(new WindowTreeHostMojo(
124         native_viewport.Pass(),
125         gfx::Rect(800, 600),
126         base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this))));
127   }
128
129  private:
130   void HostContextCreated() {
131     window_tree_host_->InitHost();
132
133     window_tree_client_.reset(
134         new DemoWindowTreeClient(window_tree_host_->window()));
135
136     delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
137     window1_ = new aura::Window(delegate1_.get());
138     window1_->Init(aura::WINDOW_LAYER_TEXTURED);
139     window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
140     window1_->Show();
141     window_tree_host_->window()->AddChild(window1_);
142
143     delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
144     window2_ = new aura::Window(delegate2_.get());
145     window2_->Init(aura::WINDOW_LAYER_TEXTURED);
146     window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
147     window2_->Show();
148     window_tree_host_->window()->AddChild(window2_);
149
150     delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
151     window21_ = new aura::Window(delegate21_.get());
152     window21_->Init(aura::WINDOW_LAYER_TEXTURED);
153     window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
154     window21_->Show();
155     window2_->AddChild(window21_);
156
157     window_tree_host_->Show();
158   }
159
160   scoped_ptr<ScreenMojo> screen_;
161
162   scoped_ptr<DemoWindowTreeClient> window_tree_client_;
163
164   scoped_ptr<DemoWindowDelegate> delegate1_;
165   scoped_ptr<DemoWindowDelegate> delegate2_;
166   scoped_ptr<DemoWindowDelegate> delegate21_;
167
168   aura::Window* window1_;
169   aura::Window* window2_;
170   aura::Window* window21_;
171
172   scoped_ptr<aura::WindowTreeHost> window_tree_host_;
173 };
174
175 }  // namespace examples
176 }  // namespace mojo
177
178 extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain(
179     MojoHandle shell_handle) {
180   CommandLine::Init(0, NULL);
181   base::AtExitManager at_exit;
182   base::MessageLoop loop;
183   mojo::GLES2Initializer gles2;
184
185   // TODO(beng): This crashes in a DCHECK on X11 because this thread's
186   //             MessageLoop is not of TYPE_UI. I think we need a way to build
187   //             Aura that doesn't define platform-specific stuff.
188   aura::Env::CreateInstance(true);
189   mojo::examples::AuraDemo app(shell_handle);
190   loop.Run();
191
192   return MOJO_RESULT_OK;
193 }