cc: Make VideoResourceUpdater use CopyToResource instead of SetPixels.
[chromium-blink-merge.git] / ui / platform_window / win / win_window.cc
blob9bc8892e5018bb9961ebe2c6636e3f408f1fca99
1 // Copyright 2014 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/platform_window/win/win_window.h"
7 #include "base/profiler/scoped_tracker.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_utils.h"
10 #include "ui/gfx/win/msg_util.h"
11 #include "ui/platform_window/platform_window_delegate.h"
13 namespace ui {
15 namespace {
17 bool use_popup_as_root_window_for_test = false;
19 gfx::Rect GetWindowBoundsForClientBounds(DWORD style, DWORD ex_style,
20 const gfx::Rect& bounds) {
21 RECT wr;
22 wr.left = bounds.x();
23 wr.top = bounds.y();
24 wr.right = bounds.x() + bounds.width();
25 wr.bottom = bounds.y() + bounds.height();
26 AdjustWindowRectEx(&wr, style, FALSE, ex_style);
28 // Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
29 // moved part of it offscreen.
30 gfx::Rect window_bounds(wr.left, wr.top,
31 wr.right - wr.left, wr.bottom - wr.top);
32 window_bounds.set_x(std::max(0, window_bounds.x()));
33 window_bounds.set_y(std::max(0, window_bounds.y()));
34 return window_bounds;
37 } // namespace
39 WinWindow::WinWindow(PlatformWindowDelegate* delegate,
40 const gfx::Rect& bounds)
41 : delegate_(delegate) {
42 CHECK(delegate_);
43 if (use_popup_as_root_window_for_test)
44 set_window_style(WS_POPUP);
45 gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
46 WS_OVERLAPPEDWINDOW, window_ex_style(), bounds);
47 gfx::WindowImpl::Init(NULL, window_bounds);
48 SetWindowText(hwnd(), L"WinWindow");
51 WinWindow::~WinWindow() {
52 Destroy();
55 void WinWindow::Destroy() {
56 if (IsWindow(hwnd()))
57 DestroyWindow(hwnd());
60 void WinWindow::Show() {
61 ShowWindow(hwnd(), SW_SHOWNORMAL);
64 void WinWindow::Hide() {
65 ShowWindow(hwnd(), SW_HIDE);
68 void WinWindow::Close() {
69 Destroy();
72 void WinWindow::SetBounds(const gfx::Rect& bounds) {
73 gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
74 GetWindowLong(hwnd(), GWL_STYLE),
75 GetWindowLong(hwnd(), GWL_EXSTYLE),
76 bounds);
77 SetWindowPos(hwnd(), NULL, window_bounds.x(), window_bounds.y(),
78 window_bounds.width(), window_bounds.height(),
79 SWP_NOREPOSITION);
82 gfx::Rect WinWindow::GetBounds() {
83 RECT cr;
84 GetClientRect(hwnd(), &cr);
85 return gfx::Rect(cr);
88 void WinWindow::SetCapture() {
89 DCHECK(::GetCapture() != hwnd());
90 ::SetCapture(hwnd());
93 void WinWindow::ReleaseCapture() {
94 if (::GetCapture() == hwnd())
95 ::ReleaseCapture();
98 void WinWindow::ToggleFullscreen() {}
100 void WinWindow::Maximize() {}
102 void WinWindow::Minimize() {}
104 void WinWindow::Restore() {}
106 void WinWindow::SetCursor(PlatformCursor cursor) {}
108 void WinWindow::MoveCursorTo(const gfx::Point& location) {}
110 void WinWindow::ConfineCursorToBounds(const gfx::Rect& bounds) {
113 LRESULT WinWindow::OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) {
114 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
115 tracked_objects::ScopedTracker tracking_profile(
116 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnMouseRange"));
118 MSG msg = { hwnd(), message, w_param, l_param, GetMessageTime(),
119 { CR_GET_X_LPARAM(l_param), CR_GET_Y_LPARAM(l_param) } };
120 MouseEvent event(msg);
121 if (IsMouseEventFromTouch(message))
122 event.set_flags(event.flags() | EF_FROM_TOUCH);
123 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
124 delegate_->DispatchEvent(&event);
125 SetMsgHandled(event.handled());
126 return 0;
129 LRESULT WinWindow::OnCaptureChanged(UINT message,
130 WPARAM w_param,
131 LPARAM l_param) {
132 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
133 tracked_objects::ScopedTracker tracking_profile(
134 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnCaptureChanged"));
136 delegate_->OnLostCapture();
137 return 0;
140 LRESULT WinWindow::OnKeyEvent(UINT message, WPARAM w_param, LPARAM l_param) {
141 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
142 tracked_objects::ScopedTracker tracking_profile(
143 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnKeyEvent"));
145 MSG msg = { hwnd(), message, w_param, l_param };
146 KeyEvent event(msg);
147 delegate_->DispatchEvent(&event);
148 SetMsgHandled(event.handled());
149 return 0;
152 LRESULT WinWindow::OnNCActivate(UINT message, WPARAM w_param, LPARAM l_param) {
153 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
154 tracked_objects::ScopedTracker tracking_profile(
155 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnNCActivate"));
157 delegate_->OnActivationChanged(!!w_param);
158 return DefWindowProc(hwnd(), message, w_param, l_param);
161 void WinWindow::OnClose() {
162 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
163 tracked_objects::ScopedTracker tracking_profile(
164 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnClose"));
166 delegate_->OnCloseRequest();
169 LRESULT WinWindow::OnCreate(CREATESTRUCT* create_struct) {
170 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
171 tracked_objects::ScopedTracker tracking_profile(
172 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnCreate"));
174 delegate_->OnAcceleratedWidgetAvailable(hwnd());
175 return 0;
178 void WinWindow::OnDestroy() {
179 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
180 tracked_objects::ScopedTracker tracking_profile(
181 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnDestroy"));
183 delegate_->OnClosed();
186 void WinWindow::OnPaint(HDC) {
187 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
188 tracked_objects::ScopedTracker tracking_profile(
189 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnPaint"));
191 gfx::Rect damage_rect;
192 RECT update_rect = {0};
193 if (GetUpdateRect(hwnd(), &update_rect, FALSE))
194 damage_rect = gfx::Rect(update_rect);
195 delegate_->OnDamageRect(damage_rect);
196 ValidateRect(hwnd(), NULL);
199 void WinWindow::OnWindowPosChanged(WINDOWPOS* window_pos) {
200 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
201 tracked_objects::ScopedTracker tracking_profile(
202 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnWindowPosChanged"));
204 if (!(window_pos->flags & SWP_NOSIZE) ||
205 !(window_pos->flags & SWP_NOMOVE)) {
206 RECT cr;
207 GetClientRect(hwnd(), &cr);
208 delegate_->OnBoundsChanged(
209 gfx::Rect(window_pos->x, window_pos->y,
210 cr.right - cr.left, cr.bottom - cr.top));
214 namespace test {
216 // static
217 void SetUsePopupAsRootWindowForTest(bool use) {
218 use_popup_as_root_window_for_test = use;
221 } // namespace test
222 } // namespace ui