[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 1 | // Copyright (c) 2011 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. |
| 4 | |
[email protected] | f94f0f1 | 2011-09-14 21:14:01 | [diff] [blame] | 5 | #include "ui/aura/desktop_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] | f94f0f1 | 2011-09-14 21:14:01 | [diff] [blame] | 12 | #include "ui/aura/desktop.h" |
| 13 | #include "ui/aura/event.h" |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 14 | |
[email protected] | 9ba7ecf | 2011-10-19 18:39:32 | [diff] [blame] | 15 | using std::max; |
| 16 | using std::min; |
| 17 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 18 | namespace aura { |
| 19 | |
[email protected] | 9e591cb | 2011-10-17 18:14:32 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | const wchar_t* GetCursorId(gfx::NativeCursor native_cursor) { |
| 23 | switch (native_cursor) { |
| 24 | case kCursorNull: |
| 25 | return IDC_ARROW; |
| 26 | case kCursorPointer: |
| 27 | return IDC_ARROW; |
| 28 | case kCursorCross: |
| 29 | return IDC_CROSS; |
| 30 | case kCursorHand: |
| 31 | return IDC_HAND; |
| 32 | case kCursorIBeam: |
| 33 | return IDC_IBEAM; |
| 34 | case kCursorWait: |
| 35 | return IDC_WAIT; |
| 36 | case kCursorHelp: |
| 37 | return IDC_HELP; |
| 38 | case kCursorEastResize: |
| 39 | return IDC_SIZEWE; |
| 40 | case kCursorNorthResize: |
| 41 | return IDC_SIZENS; |
| 42 | case kCursorNorthEastResize: |
| 43 | return IDC_SIZENESW; |
| 44 | case kCursorNorthWestResize: |
| 45 | return IDC_SIZENWSE; |
| 46 | case kCursorSouthResize: |
| 47 | return IDC_SIZENS; |
| 48 | case kCursorSouthEastResize: |
| 49 | return IDC_SIZENWSE; |
| 50 | case kCursorSouthWestResize: |
| 51 | return IDC_SIZENESW; |
| 52 | case kCursorWestResize: |
| 53 | return IDC_SIZEWE; |
| 54 | case kCursorNorthSouthResize: |
| 55 | return IDC_SIZENS; |
| 56 | case kCursorEastWestResize: |
| 57 | return IDC_SIZEWE; |
| 58 | case kCursorNorthEastSouthWestResize: |
| 59 | return IDC_SIZENESW; |
| 60 | case kCursorNorthWestSouthEastResize: |
| 61 | return IDC_SIZENWSE; |
| 62 | case kCursorMove: |
| 63 | return IDC_SIZEALL; |
| 64 | case kCursorProgress: |
| 65 | return IDC_APPSTARTING; |
| 66 | case kCursorNoDrop: |
| 67 | return IDC_NO; |
| 68 | case kCursorNotAllowed: |
| 69 | return IDC_NO; |
| 70 | case kCursorColumnResize: |
| 71 | case kCursorRowResize: |
| 72 | case kCursorMiddlePanning: |
| 73 | case kCursorEastPanning: |
| 74 | case kCursorNorthPanning: |
| 75 | case kCursorNorthEastPanning: |
| 76 | case kCursorNorthWestPanning: |
| 77 | case kCursorSouthPanning: |
| 78 | case kCursorSouthEastPanning: |
| 79 | case kCursorSouthWestPanning: |
| 80 | case kCursorWestPanning: |
| 81 | case kCursorVerticalText: |
| 82 | case kCursorCell: |
| 83 | case kCursorContextMenu: |
| 84 | case kCursorAlias: |
| 85 | case kCursorCopy: |
| 86 | case kCursorNone: |
| 87 | case kCursorZoomIn: |
| 88 | case kCursorZoomOut: |
| 89 | case kCursorGrab: |
| 90 | case kCursorGrabbing: |
| 91 | case kCursorCustom: |
| 92 | // TODO(jamescook): Should we use WebKit glue resources for these? |
| 93 | // Or migrate those resources to someplace ui/aura can share? |
| 94 | NOTIMPLEMENTED(); |
| 95 | return IDC_ARROW; |
| 96 | default: |
| 97 | NOTREACHED(); |
| 98 | return IDC_ARROW; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 104 | // static |
| 105 | DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) { |
| 106 | return new DesktopHostWin(bounds); |
| 107 | } |
| 108 | |
[email protected] | 5978af5e | 2011-10-24 22:17:18 | [diff] [blame^] | 109 | // static |
| 110 | gfx::Size DesktopHost::GetNativeDisplaySize() { |
| 111 | return gfx::Size(GetSystemMetrics(SM_CXSCREEN), |
| 112 | GetSystemMetrics(SM_CYSCREEN)); |
| 113 | } |
| 114 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 115 | DesktopHostWin::DesktopHostWin(const gfx::Rect& bounds) : desktop_(NULL) { |
| 116 | Init(NULL, bounds); |
[email protected] | 78e7603d | 2011-10-21 17:34:55 | [diff] [blame] | 117 | SetWindowText(hwnd(), L"aura::Desktop!"); |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | DesktopHostWin::~DesktopHostWin() { |
| 121 | DestroyWindow(hwnd()); |
| 122 | } |
| 123 | |
[email protected] | 711d8a8 | 2011-08-22 16:01:19 | [diff] [blame] | 124 | bool DesktopHostWin::Dispatch(const MSG& msg) { |
[email protected] | b2d2ee8 | 2011-08-22 17:25:05 | [diff] [blame] | 125 | TranslateMessage(&msg); |
| 126 | DispatchMessage(&msg); |
[email protected] | 711d8a8 | 2011-08-22 16:01:19 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 130 | void DesktopHostWin::SetDesktop(Desktop* desktop) { |
| 131 | desktop_ = desktop; |
| 132 | } |
| 133 | |
| 134 | gfx::AcceleratedWidget DesktopHostWin::GetAcceleratedWidget() { |
| 135 | return hwnd(); |
| 136 | } |
| 137 | |
| 138 | void DesktopHostWin::Show() { |
| 139 | ShowWindow(hwnd(), SW_SHOWNORMAL); |
| 140 | } |
| 141 | |
[email protected] | 16bb576 | 2011-10-06 05:02:28 | [diff] [blame] | 142 | gfx::Size DesktopHostWin::GetSize() const { |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 143 | RECT r; |
| 144 | GetClientRect(hwnd(), &r); |
| 145 | return gfx::Rect(r).size(); |
| 146 | } |
| 147 | |
[email protected] | 970aa36 | 2011-08-30 20:03:34 | [diff] [blame] | 148 | void DesktopHostWin::SetSize(const gfx::Size& size) { |
| 149 | SetWindowPos( |
| 150 | hwnd(), |
| 151 | NULL, |
| 152 | 0, |
| 153 | 0, |
| 154 | size.width(), |
| 155 | size.height(), |
| 156 | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOREPOSITION); |
| 157 | } |
| 158 | |
[email protected] | 9e591cb | 2011-10-17 18:14:32 | [diff] [blame] | 159 | void DesktopHostWin::SetCursor(gfx::NativeCursor native_cursor) { |
| 160 | // Custom web cursors are handled directly. |
| 161 | if (native_cursor == kCursorCustom) |
| 162 | return; |
| 163 | const wchar_t* cursor_id = GetCursorId(native_cursor); |
| 164 | // TODO(jamescook): Support for non-system cursors will require finding |
| 165 | // the appropriate module to pass to LoadCursor(). |
| 166 | ::SetCursor(LoadCursor(NULL, cursor_id)); |
[email protected] | 70ccf70 | 2011-09-22 18:15:58 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 896728e | 2011-10-14 00:41:26 | [diff] [blame] | 169 | gfx::Point DesktopHostWin::QueryMouseLocation() { |
| 170 | POINT pt; |
| 171 | GetCursorPos(&pt); |
| 172 | ScreenToClient(hwnd(), &pt); |
[email protected] | 9ba7ecf | 2011-10-19 18:39:32 | [diff] [blame] | 173 | const gfx::Size size = GetSize(); |
| 174 | return gfx::Point(max(0, min(size.width(), static_cast<int>(pt.x))), |
| 175 | max(0, min(size.height(), static_cast<int>(pt.y)))); |
[email protected] | 896728e | 2011-10-14 00:41:26 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 178 | void DesktopHostWin::OnClose() { |
| 179 | // TODO: this obviously shouldn't be here. |
| 180 | MessageLoopForUI::current()->Quit(); |
| 181 | } |
| 182 | |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 183 | LRESULT DesktopHostWin::OnKeyEvent(UINT message, |
| 184 | WPARAM w_param, |
| 185 | LPARAM l_param) { |
| 186 | MSG msg = { hwnd(), message, w_param, l_param }; |
[email protected] | 593ddfa | 2011-10-20 21:51:43 | [diff] [blame] | 187 | KeyEvent keyev(msg, message == WM_CHAR); |
| 188 | SetMsgHandled(desktop_->DispatchKeyEvent(&keyev)); |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
[email protected] | a83f0f2 | 2011-08-23 15:39:15 | [diff] [blame] | 192 | LRESULT DesktopHostWin::OnMouseRange(UINT message, |
| 193 | WPARAM w_param, |
| 194 | LPARAM l_param) { |
| 195 | MSG msg = { hwnd(), message, w_param, l_param, 0, |
| 196 | { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } }; |
[email protected] | e599f013 | 2011-08-24 19:03:35 | [diff] [blame] | 197 | MouseEvent event(msg); |
| 198 | bool handled = false; |
| 199 | if (!(event.flags() & ui::EF_IS_NON_CLIENT)) |
[email protected] | 593ddfa | 2011-10-20 21:51:43 | [diff] [blame] | 200 | handled = desktop_->DispatchMouseEvent(&event); |
[email protected] | e599f013 | 2011-08-24 19:03:35 | [diff] [blame] | 201 | SetMsgHandled(handled); |
[email protected] | a83f0f2 | 2011-08-23 15:39:15 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 205 | void DesktopHostWin::OnPaint(HDC dc) { |
[email protected] | 7a9f891 | 2011-09-12 21:31:31 | [diff] [blame] | 206 | desktop_->Draw(); |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 207 | ValidateRect(hwnd(), NULL); |
| 208 | } |
| 209 | |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 210 | void DesktopHostWin::OnSize(UINT param, const CSize& size) { |
[email protected] | 7a9f891 | 2011-09-12 21:31:31 | [diff] [blame] | 211 | desktop_->OnHostResized(gfx::Size(size.cx, size.cy)); |
[email protected] | c94f859 | 2011-09-02 20:12:13 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | 81585f3 | 2011-07-29 19:32:06 | [diff] [blame] | 214 | } // namespace aura |