[email protected] | c5e9c6ce | 2012-01-13 02:37:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 5 | #include "ui/aura/root_window_host_win.h" |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 6 | |
[email protected] | 896728e | 2011-10-14 00:41:26 | [diff] [blame] | 7 | #include <windows.h> |
| 8 | |
[email protected] | 9ba7ecf | 2011-10-19 18:39:32 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 11 | #include "base/message_loop.h" |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 12 | #include "ui/aura/root_window.h" |
[email protected] | 553dfa5 | 2012-02-23 17:35:34 | [diff] [blame] | 13 | #include "ui/aura/env.h" |
[email protected] | f94f0f1 | 2011-09-14 21:14:01 | [diff] [blame] | 14 | #include "ui/aura/event.h" |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 15 | |
[email protected] | 9ba7ecf | 2011-10-19 18:39:32 | [diff] [blame] | 16 | using std::max; |
| 17 | using std::min; |
| 18 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 19 | namespace aura { |
| 20 | |
[email protected] | 9e591cb | 2011-10-17 18:14:32 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const wchar_t* GetCursorId(gfx::NativeCursor native_cursor) { |
| 24 | switch (native_cursor) { |
| 25 | case kCursorNull: |
| 26 | return IDC_ARROW; |
| 27 | case kCursorPointer: |
| 28 | return IDC_ARROW; |
| 29 | case kCursorCross: |
| 30 | return IDC_CROSS; |
| 31 | case kCursorHand: |
| 32 | return IDC_HAND; |
| 33 | case kCursorIBeam: |
| 34 | return IDC_IBEAM; |
| 35 | case kCursorWait: |
| 36 | return IDC_WAIT; |
| 37 | case kCursorHelp: |
| 38 | return IDC_HELP; |
| 39 | case kCursorEastResize: |
| 40 | return IDC_SIZEWE; |
| 41 | case kCursorNorthResize: |
| 42 | return IDC_SIZENS; |
| 43 | case kCursorNorthEastResize: |
| 44 | return IDC_SIZENESW; |
| 45 | case kCursorNorthWestResize: |
| 46 | return IDC_SIZENWSE; |
| 47 | case kCursorSouthResize: |
| 48 | return IDC_SIZENS; |
| 49 | case kCursorSouthEastResize: |
| 50 | return IDC_SIZENWSE; |
| 51 | case kCursorSouthWestResize: |
| 52 | return IDC_SIZENESW; |
| 53 | case kCursorWestResize: |
| 54 | return IDC_SIZEWE; |
| 55 | case kCursorNorthSouthResize: |
| 56 | return IDC_SIZENS; |
| 57 | case kCursorEastWestResize: |
| 58 | return IDC_SIZEWE; |
| 59 | case kCursorNorthEastSouthWestResize: |
| 60 | return IDC_SIZENESW; |
| 61 | case kCursorNorthWestSouthEastResize: |
| 62 | return IDC_SIZENWSE; |
| 63 | case kCursorMove: |
| 64 | return IDC_SIZEALL; |
| 65 | case kCursorProgress: |
| 66 | return IDC_APPSTARTING; |
| 67 | case kCursorNoDrop: |
| 68 | return IDC_NO; |
| 69 | case kCursorNotAllowed: |
| 70 | return IDC_NO; |
| 71 | case kCursorColumnResize: |
| 72 | case kCursorRowResize: |
| 73 | case kCursorMiddlePanning: |
| 74 | case kCursorEastPanning: |
| 75 | case kCursorNorthPanning: |
| 76 | case kCursorNorthEastPanning: |
| 77 | case kCursorNorthWestPanning: |
| 78 | case kCursorSouthPanning: |
| 79 | case kCursorSouthEastPanning: |
| 80 | case kCursorSouthWestPanning: |
| 81 | case kCursorWestPanning: |
| 82 | case kCursorVerticalText: |
| 83 | case kCursorCell: |
| 84 | case kCursorContextMenu: |
| 85 | case kCursorAlias: |
| 86 | case kCursorCopy: |
| 87 | case kCursorNone: |
| 88 | case kCursorZoomIn: |
| 89 | case kCursorZoomOut: |
| 90 | case kCursorGrab: |
| 91 | case kCursorGrabbing: |
| 92 | case kCursorCustom: |
| 93 | // TODO(jamescook): Should we use WebKit glue resources for these? |
| 94 | // Or migrate those resources to someplace ui/aura can share? |
| 95 | NOTIMPLEMENTED(); |
| 96 | return IDC_ARROW; |
| 97 | default: |
| 98 | NOTREACHED(); |
| 99 | return IDC_ARROW; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } // namespace |
| 104 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 105 | // static |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 106 | RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) { |
| 107 | return new RootWindowHostWin(bounds); |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 5978af5e | 2011-10-24 22:17:18 | [diff] [blame] | 110 | // static |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 111 | gfx::Size RootWindowHost::GetNativeScreenSize() { |
[email protected] | 5978af5e | 2011-10-24 22:17:18 | [diff] [blame] | 112 | return gfx::Size(GetSystemMetrics(SM_CXSCREEN), |
| 113 | GetSystemMetrics(SM_CYSCREEN)); |
| 114 | } |
| 115 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 116 | RootWindowHostWin::RootWindowHostWin(const gfx::Rect& bounds) |
| 117 | : root_window_(NULL), |
[email protected] | 8a1527b | 2011-11-03 15:47:16 | [diff] [blame] | 118 | fullscreen_(false), |
[email protected] | 759017e0 | 2012-02-11 20:46:11 | [diff] [blame] | 119 | has_capture_(false), |
[email protected] | 8a1527b | 2011-11-03 15:47:16 | [diff] [blame] | 120 | saved_window_style_(0), |
| 121 | saved_window_ex_style_(0) { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 122 | Init(NULL, bounds); |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 123 | SetWindowText(hwnd(), L"aura::RootWindow!"); |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 126 | RootWindowHostWin::~RootWindowHostWin() { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 127 | DestroyWindow(hwnd()); |
| 128 | } |
| 129 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 130 | void RootWindowHostWin::SetRootWindow(RootWindow* root_window) { |
| 131 | root_window_ = root_window; |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 134 | gfx::AcceleratedWidget RootWindowHostWin::GetAcceleratedWidget() { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 135 | return hwnd(); |
| 136 | } |
| 137 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 138 | void RootWindowHostWin::Show() { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 139 | ShowWindow(hwnd(), SW_SHOWNORMAL); |
| 140 | } |
| 141 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 142 | void RootWindowHostWin::ToggleFullScreen() { |
[email protected] | 8a1527b | 2011-11-03 15:47:16 | [diff] [blame] | 143 | gfx::Rect target_rect; |
| 144 | if (!fullscreen_) { |
| 145 | fullscreen_ = true; |
| 146 | saved_window_style_ = GetWindowLong(hwnd(), GWL_STYLE); |
| 147 | saved_window_ex_style_ = GetWindowLong(hwnd(), GWL_EXSTYLE); |
| 148 | GetWindowRect(hwnd(), &saved_window_rect_); |
| 149 | SetWindowLong(hwnd(), GWL_STYLE, |
| 150 | saved_window_style_ & ~(WS_CAPTION | WS_THICKFRAME)); |
| 151 | SetWindowLong(hwnd(), GWL_EXSTYLE, |
| 152 | saved_window_ex_style_ & ~(WS_EX_DLGMODALFRAME | |
| 153 | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); |
| 154 | |
| 155 | MONITORINFO mi; |
| 156 | mi.cbSize = sizeof(mi); |
| 157 | GetMonitorInfo(MonitorFromWindow(hwnd(), MONITOR_DEFAULTTONEAREST), &mi); |
| 158 | target_rect = mi.rcMonitor; |
| 159 | } else { |
| 160 | fullscreen_ = false; |
| 161 | SetWindowLong(hwnd(), GWL_STYLE, saved_window_style_); |
| 162 | SetWindowLong(hwnd(), GWL_EXSTYLE, saved_window_ex_style_); |
| 163 | target_rect = saved_window_rect_; |
| 164 | } |
| 165 | SetWindowPos(hwnd(), |
| 166 | NULL, |
| 167 | target_rect.x(), |
| 168 | target_rect.y(), |
| 169 | target_rect.width(), |
| 170 | target_rect.height(), |
| 171 | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); |
| 172 | } |
| 173 | |
[email protected] | 193b39d2 | 2012-03-16 20:56:28 | [diff] [blame^] | 174 | gfx::Rect RootWindowHostWin::GetBounds() const { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 175 | RECT r; |
| 176 | GetClientRect(hwnd(), &r); |
[email protected] | 193b39d2 | 2012-03-16 20:56:28 | [diff] [blame^] | 177 | return gfx::Rect(r); |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 193b39d2 | 2012-03-16 20:56:28 | [diff] [blame^] | 180 | void RootWindowHostWin::SetBounds(const gfx::Rect& bounds) { |
[email protected] | 71b6d57 | 2011-11-08 23:45:04 | [diff] [blame] | 181 | if (fullscreen_) { |
[email protected] | 193b39d2 | 2012-03-16 20:56:28 | [diff] [blame^] | 182 | saved_window_rect_.right = saved_window_rect_.left + bounds.width(); |
| 183 | saved_window_rect_.bottom = saved_window_rect_.top + bounds.height(); |
[email protected] | 71b6d57 | 2011-11-08 23:45:04 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | RECT window_rect; |
[email protected] | 193b39d2 | 2012-03-16 20:56:28 | [diff] [blame^] | 187 | window_rect.left = bounds.x(); |
| 188 | window_rect.top = bounds.y(); |
| 189 | window_rect.right = bounds.width(); |
| 190 | window_rect.bottom = bounds.height(); |
[email protected] | 71b6d57 | 2011-11-08 23:45:04 | [diff] [blame] | 191 | AdjustWindowRectEx(&window_rect, |
| 192 | GetWindowLong(hwnd(), GWL_STYLE), |
| 193 | FALSE, |
| 194 | GetWindowLong(hwnd(), GWL_EXSTYLE)); |
[email protected] | 970aa36 | 2011-08-30 20:03:34 | [diff] [blame] | 195 | SetWindowPos( |
| 196 | hwnd(), |
| 197 | NULL, |
| 198 | 0, |
| 199 | 0, |
[email protected] | 71b6d57 | 2011-11-08 23:45:04 | [diff] [blame] | 200 | window_rect.right - window_rect.left, |
| 201 | window_rect.bottom - window_rect.top, |
[email protected] | 970aa36 | 2011-08-30 20:03:34 | [diff] [blame] | 202 | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOREPOSITION); |
| 203 | } |
| 204 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 205 | gfx::Point RootWindowHostWin::GetLocationOnNativeScreen() const { |
[email protected] | ace7ca0 | 2011-11-22 21:34:17 | [diff] [blame] | 206 | RECT r; |
| 207 | GetClientRect(hwnd(), &r); |
| 208 | return gfx::Point(r.left, r.top); |
| 209 | } |
| 210 | |
| 211 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 212 | void RootWindowHostWin::SetCursor(gfx::NativeCursor native_cursor) { |
[email protected] | 9e591cb | 2011-10-17 18:14:32 | [diff] [blame] | 213 | // Custom web cursors are handled directly. |
| 214 | if (native_cursor == kCursorCustom) |
| 215 | return; |
| 216 | const wchar_t* cursor_id = GetCursorId(native_cursor); |
| 217 | // TODO(jamescook): Support for non-system cursors will require finding |
| 218 | // the appropriate module to pass to LoadCursor(). |
| 219 | ::SetCursor(LoadCursor(NULL, cursor_id)); |
[email protected] | 70ccf70 | 2011-09-22 18:15:58 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | 759017e0 | 2012-02-11 20:46:11 | [diff] [blame] | 222 | void RootWindowHostWin::SetCapture() { |
| 223 | if (!has_capture_) { |
| 224 | has_capture_ = true; |
| 225 | ::SetCapture(hwnd()); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void RootWindowHostWin::ReleaseCapture() { |
| 230 | if (has_capture_) { |
| 231 | has_capture_ = false; |
| 232 | ::ReleaseCapture(); |
| 233 | } |
| 234 | } |
| 235 | |
[email protected] | 09a02fff | 2011-12-20 22:01:43 | [diff] [blame] | 236 | void RootWindowHostWin::ShowCursor(bool show) { |
[email protected] | c5e9c6ce | 2012-01-13 02:37:18 | [diff] [blame] | 237 | // NOTIMPLEMENTED(); |
[email protected] | 09a02fff | 2011-12-20 22:01:43 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 240 | gfx::Point RootWindowHostWin::QueryMouseLocation() { |
[email protected] | 896728e | 2011-10-14 00:41:26 | [diff] [blame] | 241 | POINT pt; |
| 242 | GetCursorPos(&pt); |
| 243 | ScreenToClient(hwnd(), &pt); |
[email protected] | 193b39d2 | 2012-03-16 20:56:28 | [diff] [blame^] | 244 | const gfx::Size size = GetBounds().size(); |
[email protected] | 9ba7ecf | 2011-10-19 18:39:32 | [diff] [blame] | 245 | return gfx::Point(max(0, min(size.width(), static_cast<int>(pt.x))), |
| 246 | max(0, min(size.height(), static_cast<int>(pt.y)))); |
[email protected] | 896728e | 2011-10-14 00:41:26 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | e424326 | 2012-01-19 18:59:25 | [diff] [blame] | 249 | bool RootWindowHostWin::ConfineCursorToRootWindow() { |
| 250 | RECT window_rect; |
| 251 | GetWindowRect(hwnd(), &window_rect); |
| 252 | return ClipCursor(&window_rect) != 0; |
| 253 | } |
| 254 | |
| 255 | void RootWindowHostWin::UnConfineCursor() { |
| 256 | ClipCursor(NULL); |
| 257 | } |
| 258 | |
[email protected] | 5922cb2 | 2012-01-17 21:53:12 | [diff] [blame] | 259 | void RootWindowHostWin::MoveCursorTo(const gfx::Point& location) { |
| 260 | POINT pt; |
| 261 | ClientToScreen(hwnd(), &pt); |
| 262 | SetCursorPos(pt.x, pt.y); |
| 263 | } |
| 264 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 265 | void RootWindowHostWin::PostNativeEvent(const base::NativeEvent& native_event) { |
[email protected] | b5a1864 | 2011-11-18 22:45:36 | [diff] [blame] | 266 | ::PostMessage( |
| 267 | hwnd(), native_event.message, native_event.wParam, native_event.lParam); |
| 268 | } |
| 269 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 270 | void RootWindowHostWin::OnClose() { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 271 | // TODO: this obviously shouldn't be here. |
| 272 | MessageLoopForUI::current()->Quit(); |
| 273 | } |
| 274 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 275 | LRESULT RootWindowHostWin::OnKeyEvent(UINT message, |
| 276 | WPARAM w_param, |
| 277 | LPARAM l_param) { |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 278 | MSG msg = { hwnd(), message, w_param, l_param }; |
[email protected] | 593ddfa | 2011-10-20 21:51:43 | [diff] [blame] | 279 | KeyEvent keyev(msg, message == WM_CHAR); |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 280 | SetMsgHandled(root_window_->DispatchKeyEvent(&keyev)); |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 284 | LRESULT RootWindowHostWin::OnMouseRange(UINT message, |
| 285 | WPARAM w_param, |
| 286 | LPARAM l_param) { |
[email protected] | a83f0f2 | 2011-08-23 15:39:15 | [diff] [blame] | 287 | MSG msg = { hwnd(), message, w_param, l_param, 0, |
| 288 | { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } }; |
[email protected] | e599f013 | 2011-08-24 19:03:35 | [diff] [blame] | 289 | MouseEvent event(msg); |
| 290 | bool handled = false; |
| 291 | if (!(event.flags() & ui::EF_IS_NON_CLIENT)) |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 292 | handled = root_window_->DispatchMouseEvent(&event); |
[email protected] | e599f013 | 2011-08-24 19:03:35 | [diff] [blame] | 293 | SetMsgHandled(handled); |
[email protected] | a83f0f2 | 2011-08-23 15:39:15 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
[email protected] | 759017e0 | 2012-02-11 20:46:11 | [diff] [blame] | 297 | LRESULT RootWindowHostWin::OnCaptureChanged(UINT message, |
| 298 | WPARAM w_param, |
| 299 | LPARAM l_param) { |
| 300 | if (has_capture_) { |
| 301 | has_capture_ = false; |
| 302 | root_window_->SetCapture(NULL); |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 307 | void RootWindowHostWin::OnPaint(HDC dc) { |
| 308 | root_window_->Draw(); |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 309 | ValidateRect(hwnd(), NULL); |
| 310 | } |
| 311 | |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 312 | void RootWindowHostWin::OnSize(UINT param, const CSize& size) { |
[email protected] | f4b96e20 | 2011-11-15 18:07:50 | [diff] [blame] | 313 | // Minimizing resizes the window to 0x0 which causes our layout to go all |
| 314 | // screwy, so we just ignore it. |
| 315 | if (param != SIZE_MINIMIZED) |
[email protected] | 99f07e0 | 2011-12-07 00:02:59 | [diff] [blame] | 316 | root_window_->OnHostResized(gfx::Size(size.cx, size.cy)); |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 319 | } // namespace aura |