blob: 0dabcdda83c137ba33b19e3bfe1583b6607a6aec [file] [log] [blame]
[email protected]81585f32011-07-29 19:32:061// 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]f94f0f12011-09-14 21:14:015#include "ui/aura/desktop_host_win.h"
[email protected]81585f32011-07-29 19:32:066
[email protected]896728e2011-10-14 00:41:267#include <windows.h>
8
[email protected]9ba7ecf2011-10-19 18:39:329#include <algorithm>
10
[email protected]81585f32011-07-29 19:32:0611#include "base/message_loop.h"
[email protected]f94f0f12011-09-14 21:14:0112#include "ui/aura/desktop.h"
13#include "ui/aura/event.h"
[email protected]81585f32011-07-29 19:32:0614
[email protected]9ba7ecf2011-10-19 18:39:3215using std::max;
16using std::min;
17
[email protected]81585f32011-07-29 19:32:0618namespace aura {
19
[email protected]9e591cb2011-10-17 18:14:3220namespace {
21
22const 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]81585f32011-07-29 19:32:06104// static
105DesktopHost* DesktopHost::Create(const gfx::Rect& bounds) {
106 return new DesktopHostWin(bounds);
107}
108
109DesktopHostWin::DesktopHostWin(const gfx::Rect& bounds) : desktop_(NULL) {
110 Init(NULL, bounds);
[email protected]78e7603d2011-10-21 17:34:55111 SetWindowText(hwnd(), L"aura::Desktop!");
[email protected]81585f32011-07-29 19:32:06112}
113
114DesktopHostWin::~DesktopHostWin() {
115 DestroyWindow(hwnd());
116}
117
[email protected]711d8a82011-08-22 16:01:19118bool DesktopHostWin::Dispatch(const MSG& msg) {
[email protected]b2d2ee82011-08-22 17:25:05119 TranslateMessage(&msg);
120 DispatchMessage(&msg);
[email protected]711d8a82011-08-22 16:01:19121 return true;
122}
123
[email protected]81585f32011-07-29 19:32:06124void DesktopHostWin::SetDesktop(Desktop* desktop) {
125 desktop_ = desktop;
126}
127
128gfx::AcceleratedWidget DesktopHostWin::GetAcceleratedWidget() {
129 return hwnd();
130}
131
132void DesktopHostWin::Show() {
133 ShowWindow(hwnd(), SW_SHOWNORMAL);
134}
135
[email protected]16bb5762011-10-06 05:02:28136gfx::Size DesktopHostWin::GetSize() const {
[email protected]81585f32011-07-29 19:32:06137 RECT r;
138 GetClientRect(hwnd(), &r);
139 return gfx::Rect(r).size();
140}
141
[email protected]970aa362011-08-30 20:03:34142void DesktopHostWin::SetSize(const gfx::Size& size) {
143 SetWindowPos(
144 hwnd(),
145 NULL,
146 0,
147 0,
148 size.width(),
149 size.height(),
150 SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOREPOSITION);
151}
152
[email protected]9e591cb2011-10-17 18:14:32153void DesktopHostWin::SetCursor(gfx::NativeCursor native_cursor) {
154 // Custom web cursors are handled directly.
155 if (native_cursor == kCursorCustom)
156 return;
157 const wchar_t* cursor_id = GetCursorId(native_cursor);
158 // TODO(jamescook): Support for non-system cursors will require finding
159 // the appropriate module to pass to LoadCursor().
160 ::SetCursor(LoadCursor(NULL, cursor_id));
[email protected]70ccf702011-09-22 18:15:58161}
162
[email protected]896728e2011-10-14 00:41:26163gfx::Point DesktopHostWin::QueryMouseLocation() {
164 POINT pt;
165 GetCursorPos(&pt);
166 ScreenToClient(hwnd(), &pt);
[email protected]9ba7ecf2011-10-19 18:39:32167 const gfx::Size size = GetSize();
168 return gfx::Point(max(0, min(size.width(), static_cast<int>(pt.x))),
169 max(0, min(size.height(), static_cast<int>(pt.y))));
[email protected]896728e2011-10-14 00:41:26170}
171
[email protected]81585f32011-07-29 19:32:06172void DesktopHostWin::OnClose() {
173 // TODO: this obviously shouldn't be here.
174 MessageLoopForUI::current()->Quit();
175}
176
[email protected]c94f8592011-09-02 20:12:13177LRESULT DesktopHostWin::OnKeyEvent(UINT message,
178 WPARAM w_param,
179 LPARAM l_param) {
180 MSG msg = { hwnd(), message, w_param, l_param };
[email protected]593ddfa2011-10-20 21:51:43181 KeyEvent keyev(msg, message == WM_CHAR);
182 SetMsgHandled(desktop_->DispatchKeyEvent(&keyev));
[email protected]c94f8592011-09-02 20:12:13183 return 0;
184}
185
[email protected]a83f0f22011-08-23 15:39:15186LRESULT DesktopHostWin::OnMouseRange(UINT message,
187 WPARAM w_param,
188 LPARAM l_param) {
189 MSG msg = { hwnd(), message, w_param, l_param, 0,
190 { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } };
[email protected]e599f0132011-08-24 19:03:35191 MouseEvent event(msg);
192 bool handled = false;
193 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
[email protected]593ddfa2011-10-20 21:51:43194 handled = desktop_->DispatchMouseEvent(&event);
[email protected]e599f0132011-08-24 19:03:35195 SetMsgHandled(handled);
[email protected]a83f0f22011-08-23 15:39:15196 return 0;
197}
198
[email protected]81585f32011-07-29 19:32:06199void DesktopHostWin::OnPaint(HDC dc) {
[email protected]7a9f8912011-09-12 21:31:31200 desktop_->Draw();
[email protected]81585f32011-07-29 19:32:06201 ValidateRect(hwnd(), NULL);
202}
203
[email protected]c94f8592011-09-02 20:12:13204void DesktopHostWin::OnSize(UINT param, const CSize& size) {
[email protected]7a9f8912011-09-12 21:31:31205 desktop_->OnHostResized(gfx::Size(size.cx, size.cy));
[email protected]c94f8592011-09-02 20:12:13206}
207
[email protected]81585f32011-07-29 19:32:06208} // namespace aura