Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / ui / aura / root_window_host_win.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 "ui/aura/root_window_host_win.h"
6
7 #include <windows.h>
8
9 #include <algorithm>
10
11 #include "base/message_loop/message_loop.h"
12 #include "ui/aura/client/cursor_client.h"
13 #include "ui/aura/root_window.h"
14 #include "ui/base/cursor/cursor_loader_win.h"
15 #include "ui/events/event.h"
16 #include "ui/base/view_prop.h"
17 #include "ui/gfx/display.h"
18 #include "ui/gfx/insets.h"
19 #include "ui/gfx/screen.h"
20
21 using std::max;
22 using std::min;
23
24 namespace aura {
25 namespace {
26
27 bool use_popup_as_root_window_for_test = false;
28
29 }  // namespace
30
31 // static
32 WindowTreeHost* WindowTreeHost::Create(const gfx::Rect& bounds) {
33   return new WindowTreeHostWin(bounds);
34 }
35
36 // static
37 gfx::Size WindowTreeHost::GetNativeScreenSize() {
38   return gfx::Size(GetSystemMetrics(SM_CXSCREEN),
39                    GetSystemMetrics(SM_CYSCREEN));
40 }
41
42 WindowTreeHostWin::WindowTreeHostWin(const gfx::Rect& bounds)
43     : fullscreen_(false),
44       has_capture_(false),
45       saved_window_style_(0),
46       saved_window_ex_style_(0) {
47   if (use_popup_as_root_window_for_test)
48     set_window_style(WS_POPUP);
49   Init(NULL, bounds);
50   SetWindowText(hwnd(), L"aura::RootWindow!");
51   CreateCompositor(GetAcceleratedWidget());
52 }
53
54 WindowTreeHostWin::~WindowTreeHostWin() {
55   DestroyCompositor();
56   DestroyWindow(hwnd());
57 }
58
59 RootWindow* WindowTreeHostWin::GetRootWindow() {
60   return delegate_->AsRootWindow();
61 }
62
63 gfx::AcceleratedWidget WindowTreeHostWin::GetAcceleratedWidget() {
64   return hwnd();
65 }
66
67 void WindowTreeHostWin::Show() {
68   ShowWindow(hwnd(), SW_SHOWNORMAL);
69 }
70
71 void WindowTreeHostWin::Hide() {
72   NOTIMPLEMENTED();
73 }
74
75 void WindowTreeHostWin::ToggleFullScreen() {
76   gfx::Rect target_rect;
77   if (!fullscreen_) {
78     fullscreen_ = true;
79     saved_window_style_ = GetWindowLong(hwnd(), GWL_STYLE);
80     saved_window_ex_style_ = GetWindowLong(hwnd(), GWL_EXSTYLE);
81     GetWindowRect(hwnd(), &saved_window_rect_);
82     SetWindowLong(hwnd(), GWL_STYLE,
83                   saved_window_style_ & ~(WS_CAPTION | WS_THICKFRAME));
84     SetWindowLong(hwnd(), GWL_EXSTYLE,
85                   saved_window_ex_style_ & ~(WS_EX_DLGMODALFRAME |
86                       WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
87
88     MONITORINFO mi;
89     mi.cbSize = sizeof(mi);
90     GetMonitorInfo(MonitorFromWindow(hwnd(), MONITOR_DEFAULTTONEAREST), &mi);
91     target_rect = gfx::Rect(mi.rcMonitor);
92   } else {
93     fullscreen_ = false;
94     SetWindowLong(hwnd(), GWL_STYLE, saved_window_style_);
95     SetWindowLong(hwnd(), GWL_EXSTYLE, saved_window_ex_style_);
96     target_rect = gfx::Rect(saved_window_rect_);
97   }
98   SetWindowPos(hwnd(),
99                NULL,
100                target_rect.x(),
101                target_rect.y(),
102                target_rect.width(),
103                target_rect.height(),
104                SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
105 }
106
107 gfx::Rect WindowTreeHostWin::GetBounds() const {
108   RECT r;
109   GetClientRect(hwnd(), &r);
110   return gfx::Rect(r);
111 }
112
113 void WindowTreeHostWin::SetBounds(const gfx::Rect& bounds) {
114   if (fullscreen_) {
115     saved_window_rect_.right = saved_window_rect_.left + bounds.width();
116     saved_window_rect_.bottom = saved_window_rect_.top + bounds.height();
117     return;
118   }
119   RECT window_rect;
120   window_rect.left = bounds.x();
121   window_rect.top = bounds.y();
122   window_rect.right = bounds.right() ;
123   window_rect.bottom = bounds.bottom();
124   AdjustWindowRectEx(&window_rect,
125                      GetWindowLong(hwnd(), GWL_STYLE),
126                      FALSE,
127                      GetWindowLong(hwnd(), GWL_EXSTYLE));
128   SetWindowPos(
129       hwnd(),
130       NULL,
131       window_rect.left,
132       window_rect.top,
133       window_rect.right - window_rect.left,
134       window_rect.bottom - window_rect.top,
135       SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOREPOSITION);
136
137   // Explicity call NotifyHostResized when the scale has changed because
138   // the window size may not have changed.
139   float current_scale = compositor()->device_scale_factor();
140   float new_scale = gfx::Screen::GetScreenFor(
141       delegate_->AsRootWindow()->window())->GetDisplayNearestWindow(
142           delegate_->AsRootWindow()->window()).device_scale_factor();
143   if (current_scale != new_scale)
144     NotifyHostResized(bounds.size());
145 }
146
147 gfx::Insets WindowTreeHostWin::GetInsets() const {
148   return gfx::Insets();
149 }
150
151 void WindowTreeHostWin::SetInsets(const gfx::Insets& insets) {
152 }
153
154 gfx::Point WindowTreeHostWin::GetLocationOnNativeScreen() const {
155   RECT r;
156   GetClientRect(hwnd(), &r);
157   return gfx::Point(r.left, r.top);
158 }
159
160
161 void WindowTreeHostWin::SetCursor(gfx::NativeCursor native_cursor) {
162   // Custom web cursors are handled directly.
163   if (native_cursor == ui::kCursorCustom)
164     return;
165
166   ui::CursorLoaderWin cursor_loader;
167   cursor_loader.SetPlatformCursor(&native_cursor);
168   ::SetCursor(native_cursor.platform());
169 }
170
171 void WindowTreeHostWin::SetCapture() {
172   if (!has_capture_) {
173     has_capture_ = true;
174     ::SetCapture(hwnd());
175   }
176 }
177
178 void WindowTreeHostWin::ReleaseCapture() {
179   if (has_capture_) {
180     has_capture_ = false;
181     ::ReleaseCapture();
182   }
183 }
184
185 bool WindowTreeHostWin::QueryMouseLocation(gfx::Point* location_return) {
186   client::CursorClient* cursor_client =
187       client::GetCursorClient(GetRootWindow()->window());
188   if (cursor_client && !cursor_client->IsMouseEventsEnabled()) {
189     *location_return = gfx::Point(0, 0);
190     return false;
191   }
192
193   POINT pt;
194   GetCursorPos(&pt);
195   ScreenToClient(hwnd(), &pt);
196   const gfx::Size size = GetBounds().size();
197   *location_return =
198       gfx::Point(max(0, min(size.width(), static_cast<int>(pt.x))),
199                  max(0, min(size.height(), static_cast<int>(pt.y))));
200   return (pt.x >= 0 && static_cast<int>(pt.x) < size.width() &&
201           pt.y >= 0 && static_cast<int>(pt.y) < size.height());
202 }
203
204 bool WindowTreeHostWin::ConfineCursorToRootWindow() {
205   RECT window_rect;
206   GetWindowRect(hwnd(), &window_rect);
207   return ClipCursor(&window_rect) != 0;
208 }
209
210 void WindowTreeHostWin::UnConfineCursor() {
211   ClipCursor(NULL);
212 }
213
214 void WindowTreeHostWin::OnCursorVisibilityChanged(bool show) {
215   NOTIMPLEMENTED();
216 }
217
218 void WindowTreeHostWin::MoveCursorTo(const gfx::Point& location) {
219   // Deliberately not implemented.
220 }
221
222 void WindowTreeHostWin::PostNativeEvent(const base::NativeEvent& native_event) {
223   ::PostMessage(
224       hwnd(), native_event.message, native_event.wParam, native_event.lParam);
225 }
226
227 void WindowTreeHostWin::OnDeviceScaleFactorChanged(
228     float device_scale_factor) {
229   NOTIMPLEMENTED();
230 }
231
232 void WindowTreeHostWin::PrepareForShutdown() {
233   NOTIMPLEMENTED();
234 }
235
236 void WindowTreeHostWin::OnClose() {
237   // TODO: this obviously shouldn't be here.
238   base::MessageLoopForUI::current()->Quit();
239 }
240
241 LRESULT WindowTreeHostWin::OnKeyEvent(UINT message,
242                                       WPARAM w_param,
243                                       LPARAM l_param) {
244   MSG msg = { hwnd(), message, w_param, l_param };
245   ui::KeyEvent keyev(msg, message == WM_CHAR);
246   SetMsgHandled(delegate_->OnHostKeyEvent(&keyev));
247   return 0;
248 }
249
250 LRESULT WindowTreeHostWin::OnMouseRange(UINT message,
251                                         WPARAM w_param,
252                                         LPARAM l_param) {
253   MSG msg = { hwnd(), message, w_param, l_param, 0,
254               { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } };
255   ui::MouseEvent event(msg);
256   bool handled = false;
257   if (!(event.flags() & ui::EF_IS_NON_CLIENT))
258     handled = delegate_->OnHostMouseEvent(&event);
259   SetMsgHandled(handled);
260   return 0;
261 }
262
263 LRESULT WindowTreeHostWin::OnCaptureChanged(UINT message,
264                                             WPARAM w_param,
265                                             LPARAM l_param) {
266   if (has_capture_) {
267     has_capture_ = false;
268     delegate_->OnHostLostWindowCapture();
269   }
270   return 0;
271 }
272
273 LRESULT WindowTreeHostWin::OnNCActivate(UINT message,
274                                         WPARAM w_param,
275                                         LPARAM l_param) {
276   if (!!w_param)
277     delegate_->OnHostActivated();
278   return DefWindowProc(hwnd(), message, w_param, l_param);
279 }
280
281 void WindowTreeHostWin::OnMove(const CPoint& point) {
282   if (delegate_)
283     delegate_->OnHostMoved(gfx::Point(point.x, point.y));
284 }
285
286 void WindowTreeHostWin::OnPaint(HDC dc) {
287   gfx::Rect damage_rect;
288   RECT update_rect = {0};
289   if (GetUpdateRect(hwnd(), &update_rect, FALSE))
290     damage_rect = gfx::Rect(update_rect);
291   compositor()->ScheduleRedrawRect(damage_rect);
292   ValidateRect(hwnd(), NULL);
293 }
294
295 void WindowTreeHostWin::OnSize(UINT param, const CSize& size) {
296   // Minimizing resizes the window to 0x0 which causes our layout to go all
297   // screwy, so we just ignore it.
298   if (delegate_ && param != SIZE_MINIMIZED)
299     NotifyHostResized(gfx::Size(size.cx, size.cy));
300 }
301
302 namespace test {
303
304 // static
305 void SetUsePopupAsRootWindowForTest(bool use) {
306   use_popup_as_root_window_for_test = use;
307 }
308
309 }  // namespace test
310
311 }  // namespace aura