Upstream version 5.34.104.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/root_window.h"
15 #include "ui/aura/test/test_focus_client.h"
16 #include "ui/aura/test/test_screen.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_delegate.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/compositor/test/context_factories_for_test.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 "base/message_loop/message_pump_x11.h"
28 #endif
29
30 namespace {
31
32 // Trivial WindowDelegate implementation that draws a colored background.
33 class DemoWindowDelegate : public aura::WindowDelegate {
34  public:
35   explicit DemoWindowDelegate(SkColor color) : color_(color) {}
36
37   // Overridden from WindowDelegate:
38   virtual gfx::Size GetMinimumSize() const OVERRIDE {
39     return gfx::Size();
40   }
41
42   virtual gfx::Size GetMaximumSize() const OVERRIDE {
43     return gfx::Size();
44   }
45
46   virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
47                                const gfx::Rect& new_bounds) OVERRIDE {}
48   virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
49     return gfx::kNullCursor;
50   }
51   virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
52     return HTCAPTION;
53   }
54   virtual bool ShouldDescendIntoChildForEventHandling(
55       aura::Window* child,
56       const gfx::Point& location) OVERRIDE {
57     return true;
58   }
59   virtual bool CanFocus() OVERRIDE { return true; }
60   virtual void OnCaptureLost() OVERRIDE {}
61   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
62     canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
63   }
64   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
65   virtual void OnWindowDestroying() OVERRIDE {}
66   virtual void OnWindowDestroyed() OVERRIDE {}
67   virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
68   virtual bool HasHitTestMask() const OVERRIDE { return false; }
69   virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
70   virtual void DidRecreateLayer(ui::Layer* old_layer,
71                                 ui::Layer* new_layer) OVERRIDE {}
72
73  private:
74   SkColor color_;
75
76   DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
77 };
78
79 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
80  public:
81   explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
82     aura::client::SetWindowTreeClient(window_, this);
83   }
84
85   virtual ~DemoWindowTreeClient() {
86     aura::client::SetWindowTreeClient(window_, NULL);
87   }
88
89   // Overridden from aura::client::WindowTreeClient:
90   virtual aura::Window* GetDefaultParent(aura::Window* context,
91                                          aura::Window* window,
92                                          const gfx::Rect& bounds) OVERRIDE {
93     if (!capture_client_) {
94       capture_client_.reset(
95           new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
96     }
97     return window_;
98   }
99
100  private:
101   aura::Window* window_;
102
103   scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
104
105   DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
106 };
107
108 int DemoMain() {
109   // Create the message-loop here before creating the root window.
110   base::MessageLoopForUI message_loop;
111
112   gfx::GLSurface::InitializeOneOff();
113
114   // The ContextFactory must exist before any Compositors are created.
115   bool allow_test_contexts = false;
116   ui::InitializeContextFactoryForTests(allow_test_contexts);
117
118   aura::Env::CreateInstance();
119   scoped_ptr<aura::TestScreen> test_screen(aura::TestScreen::Create());
120   gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
121   scoped_ptr<aura::RootWindow> root_window(
122       test_screen->CreateRootWindowForPrimaryDisplay());
123   scoped_ptr<DemoWindowTreeClient> window_tree_client(new DemoWindowTreeClient(
124       root_window->window()));
125   aura::test::TestFocusClient focus_client;
126   aura::client::SetFocusClient(root_window->window(), &focus_client);
127
128   // Create a hierarchy of test windows.
129   DemoWindowDelegate window_delegate1(SK_ColorBLUE);
130   aura::Window window1(&window_delegate1);
131   window1.set_id(1);
132   window1.Init(aura::WINDOW_LAYER_TEXTURED);
133   window1.SetBounds(gfx::Rect(100, 100, 400, 400));
134   window1.Show();
135   aura::client::ParentWindowWithContext(
136       &window1, root_window->window(), gfx::Rect());
137
138   DemoWindowDelegate window_delegate2(SK_ColorRED);
139   aura::Window window2(&window_delegate2);
140   window2.set_id(2);
141   window2.Init(aura::WINDOW_LAYER_TEXTURED);
142   window2.SetBounds(gfx::Rect(200, 200, 350, 350));
143   window2.Show();
144   aura::client::ParentWindowWithContext(
145       &window2, root_window->window(), gfx::Rect());
146
147   DemoWindowDelegate window_delegate3(SK_ColorGREEN);
148   aura::Window window3(&window_delegate3);
149   window3.set_id(3);
150   window3.Init(aura::WINDOW_LAYER_TEXTURED);
151   window3.SetBounds(gfx::Rect(10, 10, 50, 50));
152   window3.Show();
153   window2.AddChild(&window3);
154
155   root_window->host()->Show();
156   base::MessageLoopForUI::current()->Run();
157
158   return 0;
159 }
160
161 }  // namespace
162
163 int main(int argc, char** argv) {
164   CommandLine::Init(argc, argv);
165
166   // The exit manager is in charge of calling the dtors of singleton objects.
167   base::AtExitManager exit_manager;
168
169   base::i18n::InitializeICU();
170
171   return DemoMain();
172 }