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 "ui/events/event.h"
8 #include "ui/events/event_utils.h"
9 #include "ui/gfx/win/msg_util.h"
10 #include "ui/platform_window/platform_window_delegate.h"
16 bool use_popup_as_root_window_for_test
= false;
18 gfx::Rect
GetWindowBoundsForClientBounds(DWORD style
, DWORD ex_style
,
19 const gfx::Rect
& bounds
) {
23 wr
.right
= bounds
.x() + bounds
.width();
24 wr
.bottom
= bounds
.y() + bounds
.height();
25 AdjustWindowRectEx(&wr
, style
, FALSE
, ex_style
);
27 // Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
28 // moved part of it offscreen.
29 gfx::Rect
window_bounds(wr
.left
, wr
.top
,
30 wr
.right
- wr
.left
, wr
.bottom
- wr
.top
);
31 window_bounds
.set_x(std::max(0, window_bounds
.x()));
32 window_bounds
.set_y(std::max(0, window_bounds
.y()));
38 WinWindow::WinWindow(PlatformWindowDelegate
* delegate
,
39 const gfx::Rect
& bounds
)
40 : delegate_(delegate
) {
42 if (use_popup_as_root_window_for_test
)
43 set_window_style(WS_POPUP
);
44 gfx::Rect window_bounds
= GetWindowBoundsForClientBounds(
45 WS_OVERLAPPEDWINDOW
, window_ex_style(), bounds
);
46 gfx::WindowImpl::Init(NULL
, window_bounds
);
47 SetWindowText(hwnd(), L
"WinWindow");
50 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 LRESULT
WinWindow::OnMouseRange(UINT message
, WPARAM w_param
, LPARAM l_param
) {
110 MSG msg
= { hwnd(), message
, w_param
, l_param
, GetMessageTime(),
111 { CR_GET_X_LPARAM(l_param
), CR_GET_Y_LPARAM(l_param
) } };
112 MouseEvent
event(msg
);
113 if (IsMouseEventFromTouch(message
))
114 event
.set_flags(event
.flags() | EF_FROM_TOUCH
);
115 if (!(event
.flags() & ui::EF_IS_NON_CLIENT
))
116 delegate_
->DispatchEvent(&event
);
117 SetMsgHandled(event
.handled());
121 LRESULT
WinWindow::OnCaptureChanged(UINT message
,
124 delegate_
->OnLostCapture();
128 LRESULT
WinWindow::OnKeyEvent(UINT message
, WPARAM w_param
, LPARAM l_param
) {
129 MSG msg
= { hwnd(), message
, w_param
, l_param
};
131 delegate_
->DispatchEvent(&event
);
132 SetMsgHandled(event
.handled());
136 LRESULT
WinWindow::OnNCActivate(UINT message
, WPARAM w_param
, LPARAM l_param
) {
137 delegate_
->OnActivationChanged(!!w_param
);
138 return DefWindowProc(hwnd(), message
, w_param
, l_param
);
141 void WinWindow::OnClose() {
142 delegate_
->OnCloseRequest();
145 LRESULT
WinWindow::OnCreate(CREATESTRUCT
* create_struct
) {
146 delegate_
->OnAcceleratedWidgetAvailable(hwnd());
150 void WinWindow::OnDestroy() {
151 delegate_
->OnClosed();
154 void WinWindow::OnPaint(HDC
) {
155 gfx::Rect damage_rect
;
156 RECT update_rect
= {0};
157 if (GetUpdateRect(hwnd(), &update_rect
, FALSE
))
158 damage_rect
= gfx::Rect(update_rect
);
159 delegate_
->OnDamageRect(damage_rect
);
160 ValidateRect(hwnd(), NULL
);
163 void WinWindow::OnWindowPosChanged(WINDOWPOS
* window_pos
) {
164 if (!(window_pos
->flags
& SWP_NOSIZE
) ||
165 !(window_pos
->flags
& SWP_NOMOVE
)) {
167 GetClientRect(hwnd(), &cr
);
168 delegate_
->OnBoundsChanged(
169 gfx::Rect(window_pos
->x
, window_pos
->y
,
170 cr
.right
- cr
.left
, cr
.bottom
- cr
.top
));
177 void SetUsePopupAsRootWindowForTest(bool use
) {
178 use_popup_as_root_window_for_test
= use
;