Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / content / common / cursors / webcursor_win.cc
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.
4
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"
12
13 using blink::WebCursorInfo;
14
15 static LPCWSTR ToCursorID(WebCursorInfo::Type type) {
16   switch (type) {
17     case WebCursorInfo::TypePointer:
18       return IDC_ARROW;
19     case WebCursorInfo::TypeCross:
20       return IDC_CROSS;
21     case WebCursorInfo::TypeHand:
22       return IDC_HAND;
23     case WebCursorInfo::TypeIBeam:
24       return IDC_IBEAM;
25     case WebCursorInfo::TypeWait:
26       return IDC_WAIT;
27     case WebCursorInfo::TypeHelp:
28       return IDC_HELP;
29     case WebCursorInfo::TypeEastResize:
30       return IDC_SIZEWE;
31     case WebCursorInfo::TypeNorthResize:
32       return IDC_SIZENS;
33     case WebCursorInfo::TypeNorthEastResize:
34       return IDC_SIZENESW;
35     case WebCursorInfo::TypeNorthWestResize:
36       return IDC_SIZENWSE;
37     case WebCursorInfo::TypeSouthResize:
38       return IDC_SIZENS;
39     case WebCursorInfo::TypeSouthEastResize:
40       return IDC_SIZENWSE;
41     case WebCursorInfo::TypeSouthWestResize:
42       return IDC_SIZENESW;
43     case WebCursorInfo::TypeWestResize:
44       return IDC_SIZEWE;
45     case WebCursorInfo::TypeNorthSouthResize:
46       return IDC_SIZENS;
47     case WebCursorInfo::TypeEastWestResize:
48       return IDC_SIZEWE;
49     case WebCursorInfo::TypeNorthEastSouthWestResize:
50       return IDC_SIZENESW;
51     case WebCursorInfo::TypeNorthWestSouthEastResize:
52       return IDC_SIZENWSE;
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:
76       return IDC_SIZEALL;
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:
88       return IDC_NO;
89     case WebCursorInfo::TypeCopy:
90       return MAKEINTRESOURCE(IDC_COPYCUR);
91     case WebCursorInfo::TypeNone:
92       return MAKEINTRESOURCE(IDC_CURSOR_NONE);
93     case WebCursorInfo::TypeNotAllowed:
94       return IDC_NO;
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);
103   }
104   NOTREACHED();
105   return NULL;
106 }
107
108 static bool IsSystemCursorID(LPCWSTR cursor_id) {
109   return cursor_id >= IDC_ARROW;  // See WinUser.h
110 }
111
112 namespace content {
113
114 HCURSOR WebCursor::GetCursor(HINSTANCE module_handle){
115   if (!IsCustom()) {
116     const wchar_t* cursor_id =
117         ToCursorID(static_cast<WebCursorInfo::Type>(type_));
118
119     if (IsSystemCursorID(cursor_id))
120       module_handle = NULL;
121
122     return LoadCursor(module_handle, cursor_id);
123   }
124
125   if (custom_cursor_) {
126     DCHECK(external_cursor_ == NULL);
127     return custom_cursor_;
128   }
129
130   if (external_cursor_)
131     return external_cursor_;
132
133   custom_cursor_ =
134       IconUtil::CreateCursorFromDIB(
135           custom_size_,
136           hotspot_,
137           !custom_data_.empty() ? &custom_data_[0] : NULL,
138           custom_data_.size());
139   return custom_cursor_;
140 }
141
142 gfx::NativeCursor WebCursor::GetNativeCursor() {
143   return GetCursor(NULL);
144 }
145
146 void WebCursor::InitPlatformData() {
147   custom_cursor_ = NULL;
148 }
149
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_));
155 }
156
157 bool WebCursor::DeserializePlatformData(PickleIterator* iter) {
158   return iter->ReadUInt32(reinterpret_cast<uint32*>(&external_cursor_));
159 }
160
161 bool WebCursor::IsPlatformDataEqual(const WebCursor& other) const {
162   if (!IsCustom())
163     return true;
164
165   return (external_cursor_ == other.external_cursor_);
166 }
167
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;
174 }
175
176 void WebCursor::CleanupPlatformData() {
177   external_cursor_ = NULL;
178
179   if (custom_cursor_) {
180     DestroyIcon(custom_cursor_);
181     custom_cursor_ = NULL;
182   }
183 }
184
185 }  // namespace content