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