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 "content/common/cursors/webcursor.h"
8 #include "grit/ui_unscaled_resources.h"
9 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/gfx/icon_util.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
114 HCURSOR WebCursor::GetCursor(HINSTANCE module_handle){
116 const wchar_t* cursor_id =
117 ToCursorID(static_cast<WebCursorInfo::Type>(type_));
119 if (IsSystemCursorID(cursor_id))
120 module_handle = NULL;
122 return LoadCursor(module_handle, cursor_id);
125 if (custom_cursor_) {
126 DCHECK(external_cursor_ == NULL);
127 return custom_cursor_;
130 if (external_cursor_)
131 return external_cursor_;
134 IconUtil::CreateCursorFromDIB(
137 !custom_data_.empty() ? &custom_data_[0] : NULL,
138 custom_data_.size());
139 return custom_cursor_;
142 gfx::NativeCursor WebCursor::GetNativeCursor() {
143 return GetCursor(NULL);
146 void WebCursor::InitPlatformData() {
147 custom_cursor_ = NULL;
150 bool WebCursor::SerializePlatformData(Pickle* pickle) const {
151 // There are some issues with converting certain HCURSORS to bitmaps. The
152 // HCURSOR being a user object can be marshaled as is.
153 // HCURSORs are always 32 bits on Windows, even on 64 bit systems.
154 return pickle->WriteUInt32(reinterpret_cast<uint32>(external_cursor_));
157 bool WebCursor::DeserializePlatformData(PickleIterator* iter) {
158 return iter->ReadUInt32(reinterpret_cast<uint32*>(&external_cursor_));
161 bool WebCursor::IsPlatformDataEqual(const WebCursor& other) const {
165 return (external_cursor_ == other.external_cursor_);
168 void WebCursor::CopyPlatformData(const WebCursor& other) {
169 external_cursor_ = other.external_cursor_;
170 // The custom_cursor_ member will be initialized to a HCURSOR the next time
171 // the GetCursor member function is invoked on this WebCursor instance. The
172 // cursor is created using the data in the custom_data_ vector.
173 custom_cursor_ = NULL;
176 void WebCursor::CleanupPlatformData() {
177 external_cursor_ = NULL;
179 if (custom_cursor_) {
180 DestroyIcon(custom_cursor_);
181 custom_cursor_ = NULL;
185 } // namespace content