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"
17 bool use_popup_as_root_window_for_test
= false;
19 gfx::Rect
GetWindowBoundsForClientBounds(DWORD style
, DWORD ex_style
,
20 const gfx::Rect
& bounds
) {
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()));
39 WinWindow::WinWindow(PlatformWindowDelegate
* delegate
,
40 const gfx::Rect
& bounds
)
41 : delegate_(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() {
55 void WinWindow::Destroy() {
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() {
72 void WinWindow::SetBounds(const gfx::Rect
& bounds
) {
73 gfx::Rect window_bounds
= GetWindowBoundsForClientBounds(
74 GetWindowLong(hwnd(), GWL_STYLE
),
75 GetWindowLong(hwnd(), GWL_EXSTYLE
),
77 SetWindowPos(hwnd(), NULL
, window_bounds
.x(), window_bounds
.y(),
78 window_bounds
.width(), window_bounds
.height(),
82 gfx::Rect
WinWindow::GetBounds() {
84 GetClientRect(hwnd(), &cr
);
88 void WinWindow::SetCapture() {
89 DCHECK(::GetCapture() != hwnd());
93 void WinWindow::ReleaseCapture() {
94 if (::GetCapture() == hwnd())
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
,
119 static_cast<DWORD
>(GetMessageTime()),
120 { CR_GET_X_LPARAM(l_param
), CR_GET_Y_LPARAM(l_param
) } };
121 MouseEvent
event(msg
);
122 if (IsMouseEventFromTouch(message
))
123 event
.set_flags(event
.flags() | EF_FROM_TOUCH
);
124 if (!(event
.flags() & ui::EF_IS_NON_CLIENT
))
125 delegate_
->DispatchEvent(&event
);
126 SetMsgHandled(event
.handled());
130 LRESULT
WinWindow::OnCaptureChanged(UINT message
,
133 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
134 tracked_objects::ScopedTracker
tracking_profile(
135 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnCaptureChanged"));
137 delegate_
->OnLostCapture();
141 LRESULT
WinWindow::OnKeyEvent(UINT message
, WPARAM w_param
, LPARAM l_param
) {
142 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
143 tracked_objects::ScopedTracker
tracking_profile(
144 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnKeyEvent"));
146 MSG msg
= { hwnd(), message
, w_param
, l_param
};
148 delegate_
->DispatchEvent(&event
);
149 SetMsgHandled(event
.handled());
153 LRESULT
WinWindow::OnNCActivate(UINT message
, WPARAM w_param
, LPARAM l_param
) {
154 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
155 tracked_objects::ScopedTracker
tracking_profile(
156 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnNCActivate"));
158 delegate_
->OnActivationChanged(!!w_param
);
159 return DefWindowProc(hwnd(), message
, w_param
, l_param
);
162 void WinWindow::OnClose() {
163 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
164 tracked_objects::ScopedTracker
tracking_profile(
165 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnClose"));
167 delegate_
->OnCloseRequest();
170 LRESULT
WinWindow::OnCreate(CREATESTRUCT
* create_struct
) {
171 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
172 tracked_objects::ScopedTracker
tracking_profile(
173 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnCreate"));
175 delegate_
->OnAcceleratedWidgetAvailable(hwnd());
179 void WinWindow::OnDestroy() {
180 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
181 tracked_objects::ScopedTracker
tracking_profile(
182 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnDestroy"));
184 delegate_
->OnClosed();
187 void WinWindow::OnPaint(HDC
) {
188 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
189 tracked_objects::ScopedTracker
tracking_profile(
190 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnPaint"));
192 gfx::Rect damage_rect
;
193 RECT update_rect
= {0};
194 if (GetUpdateRect(hwnd(), &update_rect
, FALSE
))
195 damage_rect
= gfx::Rect(update_rect
);
196 delegate_
->OnDamageRect(damage_rect
);
197 ValidateRect(hwnd(), NULL
);
200 void WinWindow::OnWindowPosChanged(WINDOWPOS
* window_pos
) {
201 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
202 tracked_objects::ScopedTracker
tracking_profile(
203 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnWindowPosChanged"));
205 if (!(window_pos
->flags
& SWP_NOSIZE
) ||
206 !(window_pos
->flags
& SWP_NOMOVE
)) {
208 GetClientRect(hwnd(), &cr
);
209 delegate_
->OnBoundsChanged(
210 gfx::Rect(window_pos
->x
, window_pos
->y
,
211 cr
.right
- cr
.left
, cr
.bottom
- cr
.top
));
218 void SetUsePopupAsRootWindowForTest(bool use
) {
219 use_popup_as_root_window_for_test
= use
;