blob: c123890e24a9744972cf1583f31df39d6c2e5ccb [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
[email protected]5978af5e2011-10-24 22:17:18109// static
110gfx::Size DesktopHost::GetNativeDisplaySize() {
111 return gfx::Size(GetSystemMetrics(SM_CXSCREEN),
112 GetSystemMetrics(SM_CYSCREEN));
113}
114
[email protected]81585f32011-07-29 19:32:06115DesktopHostWin::DesktopHostWin(const gfx::Rect& bounds) : desktop_(NULL) {
116 Init(NULL, bounds);
[email protected]78e7603d2011-10-21 17:34:55117 SetWindowText(hwnd(), L"aura::Desktop!");
[email protected]81585f32011-07-29 19:32:06118}
119
120DesktopHostWin::~DesktopHostWin() {
121 DestroyWindow(hwnd());
122}
123
[email protected]711d8a82011-08-22 16:01:19124bool DesktopHostWin::Dispatch(const MSG& msg) {
[email protected]b2d2ee82011-08-22 17:25:05125 TranslateMessage(&msg);
126 DispatchMessage(&msg);
[email protected]711d8a82011-08-22 16:01:19127 return true;
128}
129
[email protected]81585f32011-07-29 19:32:06130void DesktopHostWin::SetDesktop(Desktop* desktop) {
131 desktop_ = desktop;
132}
133
134gfx::AcceleratedWidget DesktopHostWin::GetAcceleratedWidget() {
135 return hwnd();
136}
137
138void DesktopHostWin::Show() {
139 ShowWindow(hwnd(), SW_SHOWNORMAL);
140}
141
[email protected]16bb5762011-10-06 05:02:28142gfx::Size DesktopHostWin::GetSize() const {
[email protected]81585f32011-07-29 19:32:06143 RECT r;
144 GetClientRect(hwnd(), &r);
145 return gfx::Rect(r).size();
146}
147
[email protected]970aa362011-08-30 20:03:34148void 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]9e591cb2011-10-17 18:14:32159void 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]70ccf702011-09-22 18:15:58167}
168
[email protected]896728e2011-10-14 00:41:26169gfx::Point DesktopHostWin::QueryMouseLocation() {
170 POINT pt;
171 GetCursorPos(&pt);
172 ScreenToClient(hwnd(), &pt);
[email protected]9ba7ecf2011-10-19 18:39:32173 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]896728e2011-10-14 00:41:26176}
177
[email protected]81585f32011-07-29 19:32:06178void DesktopHostWin::OnClose() {
179 // TODO: this obviously shouldn't be here.
180 MessageLoopForUI::current()->Quit();
181}
182
[email protected]c94f8592011-09-02 20:12:13183LRESULT DesktopHostWin::OnKeyEvent(UINT message,
184 WPARAM w_param,
185 LPARAM l_param) {
186 MSG msg = { hwnd(), message, w_param, l_param };
[email protected]593ddfa2011-10-20 21:51:43187 KeyEvent keyev(msg, message == WM_CHAR);
188 SetMsgHandled(desktop_->DispatchKeyEvent(&keyev));
[email protected]c94f8592011-09-02 20:12:13189 return 0;
190}
191
[email protected]a83f0f22011-08-23 15:39:15192LRESULT 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]e599f0132011-08-24 19:03:35197 MouseEvent event(msg);
198 bool handled = false;
199 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
[email protected]593ddfa2011-10-20 21:51:43200 handled = desktop_->DispatchMouseEvent(&event);
[email protected]e599f0132011-08-24 19:03:35201 SetMsgHandled(handled);
[email protected]a83f0f22011-08-23 15:39:15202 return 0;
203}
204
[email protected]81585f32011-07-29 19:32:06205void DesktopHostWin::OnPaint(HDC dc) {
[email protected]7a9f8912011-09-12 21:31:31206 desktop_->Draw();
[email protected]81585f32011-07-29 19:32:06207 ValidateRect(hwnd(), NULL);
208}
209
[email protected]c94f8592011-09-02 20:12:13210void DesktopHostWin::OnSize(UINT param, const CSize& size) {
[email protected]7a9f8912011-09-12 21:31:31211 desktop_->OnHostResized(gfx::Size(size.cx, size.cy));
[email protected]c94f8592011-09-02 20:12:13212}
213
[email protected]81585f32011-07-29 19:32:06214} // namespace aura