Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / ui / platform_window / win / win_window.cc
blob8dd2c1ce4ab33c5710647bf18d8e4ba656d0ccb2
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 "base/strings/string16.h"
9 #include "ui/events/event.h"
10 #include "ui/events/event_utils.h"
11 #include "ui/gfx/win/msg_util.h"
12 #include "ui/platform_window/platform_window_delegate.h"
14 namespace ui {
16 namespace {
18 bool use_popup_as_root_window_for_test = false;
20 gfx::Rect GetWindowBoundsForClientBounds(DWORD style, DWORD ex_style,
21 const gfx::Rect& bounds) {
22 RECT wr;
23 wr.left = bounds.x();
24 wr.top = bounds.y();
25 wr.right = bounds.x() + bounds.width();
26 wr.bottom = bounds.y() + bounds.height();
27 AdjustWindowRectEx(&wr, style, FALSE, ex_style);
29 // Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
30 // moved part of it offscreen.
31 gfx::Rect window_bounds(wr.left, wr.top,
32 wr.right - wr.left, wr.bottom - wr.top);
33 window_bounds.set_x(std::max(0, window_bounds.x()));
34 window_bounds.set_y(std::max(0, window_bounds.y()));
35 return window_bounds;
38 } // namespace
40 WinWindow::WinWindow(PlatformWindowDelegate* delegate,
41 const gfx::Rect& bounds)
42 : delegate_(delegate) {
43 CHECK(delegate_);
44 if (use_popup_as_root_window_for_test)
45 set_window_style(WS_POPUP);
46 gfx::Rect window_bounds = GetWindowBoundsForClientBounds(
47 WS_OVERLAPPEDWINDOW, window_ex_style(), bounds);
48 gfx::WindowImpl::Init(NULL, window_bounds);
49 SetWindowText(hwnd(), L"WinWindow");
52 WinWindow::~WinWindow() {
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::SetTitle(const base::string16& title) {
89 SetWindowText(hwnd(), title.c_str());
92 void WinWindow::SetCapture() {
93 DCHECK(::GetCapture() != hwnd());
94 ::SetCapture(hwnd());
97 void WinWindow::ReleaseCapture() {
98 if (::GetCapture() == hwnd())
99 ::ReleaseCapture();
102 void WinWindow::ToggleFullscreen() {}
104 void WinWindow::Maximize() {}
106 void WinWindow::Minimize() {}
108 void WinWindow::Restore() {}
110 void WinWindow::SetCursor(PlatformCursor cursor) {}
112 void WinWindow::MoveCursorTo(const gfx::Point& location) {}
114 void WinWindow::ConfineCursorToBounds(const gfx::Rect& bounds) {
117 PlatformImeController* WinWindow::GetPlatformImeController() {
118 return nullptr;
121 LRESULT WinWindow::OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) {
122 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
123 tracked_objects::ScopedTracker tracking_profile(
124 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnMouseRange"));
126 MSG msg = { hwnd(), message, w_param, l_param,
127 static_cast<DWORD>(GetMessageTime()),
128 { CR_GET_X_LPARAM(l_param), CR_GET_Y_LPARAM(l_param) } };
129 MouseEvent event(msg);
130 if (IsMouseEventFromTouch(message))
131 event.set_flags(event.flags() | EF_FROM_TOUCH);
132 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
133 delegate_->DispatchEvent(&event);
134 SetMsgHandled(event.handled());
135 return 0;
138 LRESULT WinWindow::OnCaptureChanged(UINT message,
139 WPARAM w_param,
140 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::OnCaptureChanged"));
145 delegate_->OnLostCapture();
146 return 0;
149 LRESULT WinWindow::OnKeyEvent(UINT message, WPARAM w_param, LPARAM l_param) {
150 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
151 tracked_objects::ScopedTracker tracking_profile(
152 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnKeyEvent"));
154 MSG msg = { hwnd(), message, w_param, l_param };
155 KeyEvent event(msg);
156 delegate_->DispatchEvent(&event);
157 SetMsgHandled(event.handled());
158 return 0;
161 LRESULT WinWindow::OnNCActivate(UINT message, WPARAM w_param, LPARAM l_param) {
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::OnNCActivate"));
166 delegate_->OnActivationChanged(!!w_param);
167 return DefWindowProc(hwnd(), message, w_param, l_param);
170 void WinWindow::OnClose() {
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::OnClose"));
175 delegate_->OnCloseRequest();
178 LRESULT WinWindow::OnCreate(CREATESTRUCT* create_struct) {
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::OnCreate"));
183 // TODO(sky): provide real scale factor.
184 delegate_->OnAcceleratedWidgetAvailable(hwnd(), 1.f);
185 return 0;
188 void WinWindow::OnDestroy() {
189 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
190 tracked_objects::ScopedTracker tracking_profile(
191 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnDestroy"));
193 delegate_->OnClosed();
196 void WinWindow::OnPaint(HDC) {
197 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
198 tracked_objects::ScopedTracker tracking_profile(
199 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnPaint"));
201 gfx::Rect damage_rect;
202 RECT update_rect = {0};
203 if (GetUpdateRect(hwnd(), &update_rect, FALSE))
204 damage_rect = gfx::Rect(update_rect);
205 delegate_->OnDamageRect(damage_rect);
206 ValidateRect(hwnd(), NULL);
209 void WinWindow::OnWindowPosChanged(WINDOWPOS* window_pos) {
210 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
211 tracked_objects::ScopedTracker tracking_profile(
212 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 WinWindow::OnWindowPosChanged"));
214 if (!(window_pos->flags & SWP_NOSIZE) ||
215 !(window_pos->flags & SWP_NOMOVE)) {
216 RECT cr;
217 GetClientRect(hwnd(), &cr);
218 delegate_->OnBoundsChanged(
219 gfx::Rect(window_pos->x, window_pos->y,
220 cr.right - cr.left, cr.bottom - cr.top));
224 namespace test {
226 // static
227 void SetUsePopupAsRootWindowForTest(bool use) {
228 use_popup_as_root_window_for_test = use;
231 } // namespace test
232 } // namespace ui