Remove support for specifying version on command line.
[chromium-blink-merge.git] / ui / aura / root_window_host_win.cc
blob90bed7a66ae2259c5b119083b0d69fa2325f42e8
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.
5 #include "ui/aura/root_window_host_win.h"
7 #include <windows.h>
9 #include <algorithm>
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"
21 using std::max;
22 using std::min;
24 namespace aura {
25 namespace {
27 bool use_popup_as_root_window_for_test = false;
29 } // namespace
31 // static
32 WindowTreeHost* WindowTreeHost::Create(const gfx::Rect& bounds) {
33 return new WindowTreeHostWin(bounds);
36 // static
37 gfx::Size WindowTreeHost::GetNativeScreenSize() {
38 return gfx::Size(GetSystemMetrics(SM_CXSCREEN),
39 GetSystemMetrics(SM_CYSCREEN));
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());
54 WindowTreeHostWin::~WindowTreeHostWin() {
55 DestroyCompositor();
56 DestroyWindow(hwnd());
59 RootWindow* WindowTreeHostWin::GetRootWindow() {
60 return delegate_->AsRootWindow();
63 gfx::AcceleratedWidget WindowTreeHostWin::GetAcceleratedWidget() {
64 return hwnd();
67 void WindowTreeHostWin::Show() {
68 ShowWindow(hwnd(), SW_SHOWNORMAL);
71 void WindowTreeHostWin::Hide() {
72 NOTIMPLEMENTED();
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));
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_);
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);
107 gfx::Rect WindowTreeHostWin::GetBounds() const {
108 RECT r;
109 GetClientRect(hwnd(), &r);
110 return gfx::Rect(r);
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;
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);
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());
147 gfx::Insets WindowTreeHostWin::GetInsets() const {
148 return gfx::Insets();
151 void WindowTreeHostWin::SetInsets(const gfx::Insets& insets) {
154 gfx::Point WindowTreeHostWin::GetLocationOnNativeScreen() const {
155 RECT r;
156 GetClientRect(hwnd(), &r);
157 return gfx::Point(r.left, r.top);
161 void WindowTreeHostWin::SetCursor(gfx::NativeCursor native_cursor) {
162 // Custom web cursors are handled directly.
163 if (native_cursor == ui::kCursorCustom)
164 return;
166 ui::CursorLoaderWin cursor_loader;
167 cursor_loader.SetPlatformCursor(&native_cursor);
168 ::SetCursor(native_cursor.platform());
171 void WindowTreeHostWin::SetCapture() {
172 if (!has_capture_) {
173 has_capture_ = true;
174 ::SetCapture(hwnd());
178 void WindowTreeHostWin::ReleaseCapture() {
179 if (has_capture_) {
180 has_capture_ = false;
181 ::ReleaseCapture();
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;
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());
204 bool WindowTreeHostWin::ConfineCursorToRootWindow() {
205 RECT window_rect;
206 GetWindowRect(hwnd(), &window_rect);
207 return ClipCursor(&window_rect) != 0;
210 void WindowTreeHostWin::UnConfineCursor() {
211 ClipCursor(NULL);
214 void WindowTreeHostWin::OnCursorVisibilityChanged(bool show) {
215 NOTIMPLEMENTED();
218 void WindowTreeHostWin::MoveCursorTo(const gfx::Point& location) {
219 // Deliberately not implemented.
222 void WindowTreeHostWin::PostNativeEvent(const base::NativeEvent& native_event) {
223 ::PostMessage(
224 hwnd(), native_event.message, native_event.wParam, native_event.lParam);
227 void WindowTreeHostWin::OnDeviceScaleFactorChanged(
228 float device_scale_factor) {
229 NOTIMPLEMENTED();
232 void WindowTreeHostWin::PrepareForShutdown() {
233 NOTIMPLEMENTED();
236 void WindowTreeHostWin::OnClose() {
237 // TODO: this obviously shouldn't be here.
238 base::MessageLoopForUI::current()->Quit();
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;
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;
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();
270 return 0;
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);
281 void WindowTreeHostWin::OnMove(const CPoint& point) {
282 if (delegate_)
283 delegate_->OnHostMoved(gfx::Point(point.x, point.y));
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);
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));
302 namespace test {
304 // static
305 void SetUsePopupAsRootWindowForTest(bool use) {
306 use_popup_as_root_window_for_test = use;
309 } // namespace test
311 } // namespace aura