OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "third_party/skia/include/core/SkXfermode.h" | |
12 #include "ui/aura/desktop.h" | |
13 #include "ui/aura/desktop_host.h" | |
14 #include "ui/aura/window.h" | |
15 #include "ui/aura/window_delegate.h" | |
16 #include "ui/base/hit_test.h" | |
17 #include "ui/base/resource/resource_bundle.h" | |
18 #include "ui/base/ui_base_paths.h" | |
19 #include "ui/gfx/canvas.h" | |
20 #include "ui/gfx/canvas_skia.h" | |
21 #include "ui/gfx/rect.h" | |
22 #include "views/widget/widget.h" | |
23 #include "views/widget/widget_delegate.h" | |
24 | |
25 namespace { | |
26 | |
27 // Trivial WindowDelegate implementation that draws a colored background. | |
28 class DemoWindowDelegate : public aura::WindowDelegate { | |
29 public: | |
30 explicit DemoWindowDelegate(SkColor color) : color_(color) {} | |
31 | |
32 // Overridden from aura::WindowDelegate: | |
33 virtual void OnBoundsChanging(gfx::Rect* new_bounds) OVERRIDE {} | |
34 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
35 const gfx::Rect& new_bounds) OVERRIDE {} | |
36 virtual void OnFocus() OVERRIDE {} | |
37 virtual void OnBlur() OVERRIDE {} | |
38 virtual bool OnKeyEvent(aura::KeyEvent* event) OVERRIDE { | |
39 return false; | |
40 } | |
41 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | |
42 return gfx::kNullCursor; | |
43 } | |
44 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
45 return HTCLIENT; | |
46 } | |
47 virtual bool OnMouseEvent(aura::MouseEvent* event) OVERRIDE { | |
48 return true; | |
49 } | |
50 virtual ui::TouchStatus OnTouchEvent(aura::TouchEvent* event) OVERRIDE { | |
51 return ui::TOUCH_STATUS_END; | |
52 } | |
53 virtual bool ShouldActivate(aura::Event* event) OVERRIDE { | |
54 return true; | |
55 } | |
56 virtual void OnActivated() OVERRIDE {} | |
57 virtual void OnLostActive() OVERRIDE {} | |
58 virtual void OnCaptureLost() OVERRIDE {} | |
59 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
60 canvas->GetSkCanvas()->drawColor(color_, SkXfermode::kSrc_Mode); | |
61 } | |
62 virtual void OnWindowDestroying() OVERRIDE { | |
63 } | |
64 virtual void OnWindowDestroyed() OVERRIDE { | |
65 } | |
66 virtual void OnWindowVisibilityChanged(bool visible) OVERRIDE { | |
67 } | |
68 | |
69 private: | |
70 SkColor color_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate); | |
73 }; | |
74 | |
75 class TestView : public views::View { | |
76 public: | |
77 TestView() : color_shifting_(false), color_(SK_ColorYELLOW) {} | |
78 virtual ~TestView() {} | |
79 | |
80 private: | |
81 // Overridden from views::View: | |
82 virtual void OnPaint(gfx::Canvas* canvas) { | |
83 canvas->FillRect(color_, GetLocalBounds()); | |
84 } | |
85 virtual bool OnMousePressed(const views::MouseEvent& event) { | |
86 color_shifting_ = true; | |
87 return true; | |
88 } | |
89 virtual void OnMouseMoved(const views::MouseEvent& event) { | |
90 if (color_shifting_) { | |
91 color_ = SkColorSetRGB((SkColorGetR(color_) + 5) % 255, | |
92 SkColorGetG(color_), | |
93 SkColorGetB(color_)); | |
94 SchedulePaint(); | |
95 } | |
96 } | |
97 virtual void OnMouseReleased(const views::MouseEvent& event) { | |
98 color_shifting_ = false; | |
99 } | |
100 | |
101 bool color_shifting_; | |
102 SkColor color_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(TestView); | |
105 }; | |
106 | |
107 class TestWindowContents : public views::WidgetDelegateView { | |
108 public: | |
109 TestWindowContents() {} | |
110 virtual ~TestWindowContents() {} | |
111 | |
112 private: | |
113 // Overridden from views::View: | |
114 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
115 canvas->FillRect(SK_ColorGRAY, GetLocalBounds()); | |
116 } | |
117 | |
118 // Overridden from views::WidgetDelegateView: | |
119 virtual string16 GetWindowTitle() const OVERRIDE { | |
120 return ASCIIToUTF16("Test Window!"); | |
121 } | |
122 virtual View* GetContentsView() OVERRIDE { | |
123 return this; | |
124 } | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(TestWindowContents); | |
127 }; | |
128 | |
129 } // namespace | |
130 | |
131 int main(int argc, char** argv) { | |
132 CommandLine::Init(argc, argv); | |
133 | |
134 // The exit manager is in charge of calling the dtors of singleton objects. | |
135 base::AtExitManager exit_manager; | |
136 | |
137 ui::RegisterPathProvider(); | |
138 icu_util::Initialize(); | |
139 ResourceBundle::InitSharedInstance("en-US"); | |
140 | |
141 // Create the message-loop here before creating the desktop. | |
142 MessageLoop message_loop(MessageLoop::TYPE_UI); | |
143 | |
144 aura::Desktop::GetInstance(); | |
145 | |
146 // Create a hierarchy of test windows. | |
147 DemoWindowDelegate window_delegate1(SK_ColorBLUE); | |
148 aura::Window* window1 = new aura::Window(&window_delegate1); | |
149 window1->set_id(1); | |
150 window1->Init(ui::Layer::LAYER_HAS_TEXTURE); | |
151 window1->SetBounds(gfx::Rect(100, 100, 400, 400)); | |
152 window1->Show(); | |
153 window1->SetParent(NULL); | |
154 | |
155 DemoWindowDelegate window_delegate2(SK_ColorRED); | |
156 aura::Window* window2 = new aura::Window(&window_delegate2); | |
157 window2->set_id(2); | |
158 window2->Init(ui::Layer::LAYER_HAS_TEXTURE); | |
159 window2->SetBounds(gfx::Rect(200, 200, 350, 350)); | |
160 window2->Show(); | |
161 window2->SetParent(NULL); | |
162 | |
163 DemoWindowDelegate window_delegate3(SK_ColorGREEN); | |
164 aura::Window* window3 = new aura::Window(&window_delegate3); | |
165 window3->set_id(3); | |
166 window3->Init(ui::Layer::LAYER_HAS_TEXTURE); | |
167 window3->SetBounds(gfx::Rect(10, 10, 50, 50)); | |
168 window3->Show(); | |
169 window3->SetParent(window2); | |
170 | |
171 views::Widget* widget = new views::Widget; | |
172 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | |
173 params.bounds = gfx::Rect(75, 75, 80, 80); | |
174 params.parent = window2; | |
175 widget->Init(params); | |
176 widget->SetContentsView(new TestView); | |
177 | |
178 TestWindowContents* contents = new TestWindowContents; | |
179 views::Widget* views_window = views::Widget::CreateWindowWithParentAndBounds( | |
180 contents, window2, gfx::Rect(120, 150, 200, 200)); | |
181 views_window->Show(); | |
182 | |
183 aura::Desktop::GetInstance()->Run(); | |
184 | |
185 delete aura::Desktop::GetInstance(); | |
186 | |
187 return 0; | |
188 } | |
OLD | NEW |