Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / aura / demo / demo_main.cc
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
3 // found in the LICENSE file.
4
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/i18n/icu_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "third_party/skia/include/core/SkXfermode.h"
11 #include "ui/aura/client/default_capture_client.h"
12 #include "ui/aura/client/window_tree_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/test_focus_client.h"
15 #include "ui/aura/test/test_screen.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/compositor/test/in_process_context_factory.h"
21 #include "ui/events/event.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/rect.h"
24 #include "ui/gl/gl_surface.h"
25
26 #if defined(USE_X11)
27 #include "ui/gfx/x/x11_connection.h"
28 #endif
29
30 #if defined(OS_WIN)
31 #include "ui/gfx/win/dpi.h"
32 #endif
33
34 namespace {
35
36 // Trivial WindowDelegate implementation that draws a colored background.
37 class DemoWindowDelegate : public aura::WindowDelegate {
38  public:
39   explicit DemoWindowDelegate(SkColor color) : color_(color) {}
40
41   // Overridden from WindowDelegate:
42   gfx::Size GetMinimumSize() const override { return gfx::Size(); }
43
44   gfx::Size GetMaximumSize() const override { return gfx::Size(); }
45
46   void OnBoundsChanged(const gfx::Rect& old_bounds,
47                        const gfx::Rect& new_bounds) override {}
48   gfx::NativeCursor GetCursor(const gfx::Point& point) override {
49     return gfx::kNullCursor;
50   }
51   int GetNonClientComponent(const gfx::Point& point) const override {
52     return HTCAPTION;
53   }
54   bool ShouldDescendIntoChildForEventHandling(
55       aura::Window* child,
56       const gfx::Point& location) override {
57     return true;
58   }
59   bool CanFocus() override { return true; }
60   void OnCaptureLost() override {}
61   void OnPaint(gfx::Canvas* canvas) override {
62     canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
63   }
64   void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
65   void OnWindowDestroying(aura::Window* window) override {}
66   void OnWindowDestroyed(aura::Window* window) override {}
67   void OnWindowTargetVisibilityChanged(bool visible) override {}
68   bool HasHitTestMask() const override { return false; }
69   void GetHitTestMask(gfx::Path* mask) const override {}
70
71  private:
72   SkColor color_;
73
74   DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
75 };
76
77 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
78  public:
79   explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
80     aura::client::SetWindowTreeClient(window_, this);
81   }
82
83   ~DemoWindowTreeClient() override {
84     aura::client::SetWindowTreeClient(window_, NULL);
85   }
86
87   // Overridden from aura::client::WindowTreeClient:
88   aura::Window* GetDefaultParent(aura::Window* context,
89                                  aura::Window* window,
90                                  const gfx::Rect& bounds) override {
91     if (!capture_client_) {
92       capture_client_.reset(
93           new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
94     }
95     return window_;
96   }
97
98  private:
99   aura::Window* window_;
100
101   scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
102
103   DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
104 };
105
106 int DemoMain() {
107 #if defined(USE_X11)
108   // This demo uses InProcessContextFactory which uses X on a separate Gpu
109   // thread.
110   gfx::InitializeThreadedX11();
111 #endif
112
113   gfx::GLSurface::InitializeOneOff();
114
115 #if defined(OS_WIN)
116   gfx::InitDeviceScaleFactor(1.0f);
117 #endif
118
119   // The ContextFactory must exist before any Compositors are created.
120   scoped_ptr<ui::InProcessContextFactory> context_factory(
121       new ui::InProcessContextFactory());
122
123   // Create the message-loop here before creating the root window.
124   base::MessageLoopForUI message_loop;
125
126   aura::Env::CreateInstance(true);
127   aura::Env::GetInstance()->set_context_factory(context_factory.get());
128   scoped_ptr<aura::TestScreen> test_screen(
129       aura::TestScreen::Create(gfx::Size()));
130   gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
131   scoped_ptr<aura::WindowTreeHost> host(
132       test_screen->CreateHostForPrimaryDisplay());
133   scoped_ptr<DemoWindowTreeClient> window_tree_client(
134       new DemoWindowTreeClient(host->window()));
135   aura::test::TestFocusClient focus_client;
136   aura::client::SetFocusClient(host->window(), &focus_client);
137
138   // Create a hierarchy of test windows.
139   DemoWindowDelegate window_delegate1(SK_ColorBLUE);
140   aura::Window window1(&window_delegate1);
141   window1.set_id(1);
142   window1.Init(aura::WINDOW_LAYER_TEXTURED);
143   window1.SetBounds(gfx::Rect(100, 100, 400, 400));
144   window1.Show();
145   aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
146
147   DemoWindowDelegate window_delegate2(SK_ColorRED);
148   aura::Window window2(&window_delegate2);
149   window2.set_id(2);
150   window2.Init(aura::WINDOW_LAYER_TEXTURED);
151   window2.SetBounds(gfx::Rect(200, 200, 350, 350));
152   window2.Show();
153   aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
154
155   DemoWindowDelegate window_delegate3(SK_ColorGREEN);
156   aura::Window window3(&window_delegate3);
157   window3.set_id(3);
158   window3.Init(aura::WINDOW_LAYER_TEXTURED);
159   window3.SetBounds(gfx::Rect(10, 10, 50, 50));
160   window3.Show();
161   window2.AddChild(&window3);
162
163   host->Show();
164   base::MessageLoopForUI::current()->Run();
165
166   return 0;
167 }
168
169 }  // namespace
170
171 int main(int argc, char** argv) {
172   CommandLine::Init(argc, argv);
173
174   // The exit manager is in charge of calling the dtors of singleton objects.
175   base::AtExitManager exit_manager;
176
177   base::i18n::InitializeICU();
178
179   return DemoMain();
180 }