1 // Copyright (c) 2012 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 "base/logging.h"
6 #include "base/pickle.h"
7 #include "grit/ui_unscaled_resources.h"
8 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/gfx/icon_util.h"
11 #include "webkit/common/cursors/webcursor.h"
13 using blink::WebCursorInfo
;
15 static LPCWSTR
ToCursorID(WebCursorInfo::Type type
) {
17 case WebCursorInfo::TypePointer
:
19 case WebCursorInfo::TypeCross
:
21 case WebCursorInfo::TypeHand
:
23 case WebCursorInfo::TypeIBeam
:
25 case WebCursorInfo::TypeWait
:
27 case WebCursorInfo::TypeHelp
:
29 case WebCursorInfo::TypeEastResize
:
31 case WebCursorInfo::TypeNorthResize
:
33 case WebCursorInfo::TypeNorthEastResize
:
35 case WebCursorInfo::TypeNorthWestResize
:
37 case WebCursorInfo::TypeSouthResize
:
39 case WebCursorInfo::TypeSouthEastResize
:
41 case WebCursorInfo::TypeSouthWestResize
:
43 case WebCursorInfo::TypeWestResize
:
45 case WebCursorInfo::TypeNorthSouthResize
:
47 case WebCursorInfo::TypeEastWestResize
:
49 case WebCursorInfo::TypeNorthEastSouthWestResize
:
51 case WebCursorInfo::TypeNorthWestSouthEastResize
:
53 case WebCursorInfo::TypeColumnResize
:
54 return MAKEINTRESOURCE(IDC_COLRESIZE
);
55 case WebCursorInfo::TypeRowResize
:
56 return MAKEINTRESOURCE(IDC_ROWRESIZE
);
57 case WebCursorInfo::TypeMiddlePanning
:
58 return MAKEINTRESOURCE(IDC_PAN_MIDDLE
);
59 case WebCursorInfo::TypeEastPanning
:
60 return MAKEINTRESOURCE(IDC_PAN_EAST
);
61 case WebCursorInfo::TypeNorthPanning
:
62 return MAKEINTRESOURCE(IDC_PAN_NORTH
);
63 case WebCursorInfo::TypeNorthEastPanning
:
64 return MAKEINTRESOURCE(IDC_PAN_NORTH_EAST
);
65 case WebCursorInfo::TypeNorthWestPanning
:
66 return MAKEINTRESOURCE(IDC_PAN_NORTH_WEST
);
67 case WebCursorInfo::TypeSouthPanning
:
68 return MAKEINTRESOURCE(IDC_PAN_SOUTH
);
69 case WebCursorInfo::TypeSouthEastPanning
:
70 return MAKEINTRESOURCE(IDC_PAN_SOUTH_EAST
);
71 case WebCursorInfo::TypeSouthWestPanning
:
72 return MAKEINTRESOURCE(IDC_PAN_SOUTH_WEST
);
73 case WebCursorInfo::TypeWestPanning
:
74 return MAKEINTRESOURCE(IDC_PAN_WEST
);
75 case WebCursorInfo::TypeMove
:
77 case WebCursorInfo::TypeVerticalText
:
78 return MAKEINTRESOURCE(IDC_VERTICALTEXT
);
79 case WebCursorInfo::TypeCell
:
80 return MAKEINTRESOURCE(IDC_CELL
);
81 case WebCursorInfo::TypeContextMenu
:
82 return MAKEINTRESOURCE(IDC_ARROW
);
83 case WebCursorInfo::TypeAlias
:
84 return MAKEINTRESOURCE(IDC_ALIAS
);
85 case WebCursorInfo::TypeProgress
:
86 return IDC_APPSTARTING
;
87 case WebCursorInfo::TypeNoDrop
:
89 case WebCursorInfo::TypeCopy
:
90 return MAKEINTRESOURCE(IDC_COPYCUR
);
91 case WebCursorInfo::TypeNone
:
92 return MAKEINTRESOURCE(IDC_CURSOR_NONE
);
93 case WebCursorInfo::TypeNotAllowed
:
95 case WebCursorInfo::TypeZoomIn
:
96 return MAKEINTRESOURCE(IDC_ZOOMIN
);
97 case WebCursorInfo::TypeZoomOut
:
98 return MAKEINTRESOURCE(IDC_ZOOMOUT
);
99 case WebCursorInfo::TypeGrab
:
100 return MAKEINTRESOURCE(IDC_HAND_GRAB
);
101 case WebCursorInfo::TypeGrabbing
:
102 return MAKEINTRESOURCE(IDC_HAND_GRABBING
);
108 static bool IsSystemCursorID(LPCWSTR cursor_id
) {
109 return cursor_id
>= IDC_ARROW
; // See WinUser.h
112 HCURSOR
WebCursor::GetCursor(HINSTANCE module_handle
){
114 const wchar_t* cursor_id
=
115 ToCursorID(static_cast<WebCursorInfo::Type
>(type_
));
117 if (IsSystemCursorID(cursor_id
))
118 module_handle
= NULL
;
120 return LoadCursor(module_handle
, cursor_id
);
123 if (custom_cursor_
) {
124 DCHECK(external_cursor_
== NULL
);
125 return custom_cursor_
;
128 if (external_cursor_
)
129 return external_cursor_
;
132 IconUtil::CreateCursorFromDIB(
135 !custom_data_
.empty() ? &custom_data_
[0] : NULL
,
136 custom_data_
.size());
137 return custom_cursor_
;
140 gfx::NativeCursor
WebCursor::GetNativeCursor() {
141 return GetCursor(NULL
);
144 void WebCursor::InitPlatformData() {
145 custom_cursor_
= NULL
;
148 bool WebCursor::SerializePlatformData(Pickle
* pickle
) const {
149 // There are some issues with converting certain HCURSORS to bitmaps. The
150 // HCURSOR being a user object can be marshaled as is.
151 // HCURSORs are always 32 bits on Windows, even on 64 bit systems.
152 return pickle
->WriteUInt32(reinterpret_cast<uint32
>(external_cursor_
));
155 bool WebCursor::DeserializePlatformData(PickleIterator
* iter
) {
156 return iter
->ReadUInt32(reinterpret_cast<uint32
*>(&external_cursor_
));
159 bool WebCursor::IsPlatformDataEqual(const WebCursor
& other
) const {
163 return (external_cursor_
== other
.external_cursor_
);
166 void WebCursor::CopyPlatformData(const WebCursor
& other
) {
167 external_cursor_
= other
.external_cursor_
;
168 // The custom_cursor_ member will be initialized to a HCURSOR the next time
169 // the GetCursor member function is invoked on this WebCursor instance. The
170 // cursor is created using the data in the custom_data_ vector.
171 custom_cursor_
= NULL
;
174 void WebCursor::CleanupPlatformData() {
175 external_cursor_
= NULL
;
177 if (custom_cursor_
) {
178 DestroyIcon(custom_cursor_
);
179 custom_cursor_
= NULL
;