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() {
54 void WinWindow::Destroy() {
56 DestroyWindow(hwnd());
59 void WinWindow::Show() {
60 ShowWindow(hwnd(), SW_SHOWNORMAL
);
63 void WinWindow::Hide() {
64 ShowWindow(hwnd(), SW_HIDE
);
67 void WinWindow::Close() {
71 void WinWindow::SetBounds(const gfx::Rect
& bounds
) {
72 gfx::Rect window_bounds
= GetWindowBoundsForClientBounds(
73 GetWindowLong(hwnd(), GWL_STYLE
),
74 GetWindowLong(hwnd(), GWL_EXSTYLE
),
76 SetWindowPos(hwnd(), NULL
, window_bounds
.x(), window_bounds
.y(),
77 window_bounds
.width(), window_bounds
.height(),
81 gfx::Rect
WinWindow::GetBounds() {
83 GetClientRect(hwnd(), &cr
);
87 void WinWindow::SetCapture() {
88 DCHECK(::GetCapture() != hwnd());
92 void WinWindow::ReleaseCapture() {
93 if (::GetCapture() == hwnd())
97 void WinWindow::ToggleFullscreen() {}
99 void WinWindow::Maximize() {}
101 void WinWindow::Minimize() {}
103 void WinWindow::Restore() {}
105 void WinWindow::SetCursor(PlatformCursor cursor
) {}
107 void WinWindow::MoveCursorTo(const gfx::Point
& location
) {}
109 void WinWindow::ConfineCursorToBounds(const gfx::Rect
& bounds
) {
112 LRESULT
WinWindow::OnMouseRange(UINT message
, WPARAM w_param
, LPARAM l_param
) {
113 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
114 tracked_objects::ScopedTracker
tracking_profile(
115 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnMouseRange"));
117 MSG msg
= { hwnd(), message
, w_param
, l_param
,
118 static_cast<DWORD
>(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());
129 LRESULT
WinWindow::OnCaptureChanged(UINT message
,
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();
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
};
147 delegate_
->DispatchEvent(&event
);
148 SetMsgHandled(event
.handled());
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 // TODO(sky): provide real scale factor.
175 delegate_
->OnAcceleratedWidgetAvailable(hwnd(), 1.f
);
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
;