- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / 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 "grit/ui_unscaled_resources.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "third_party/WebKit/public/web/WebCursorInfo.h"
10 #include "ui/gfx/icon_util.h"
11 #include "webkit/common/cursors/webcursor.h"
12
13 using WebKit::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 HCURSOR WebCursor::GetCursor(HINSTANCE module_handle){
113   if (!IsCustom()) {
114     const wchar_t* cursor_id =
115         ToCursorID(static_cast<WebCursorInfo::Type>(type_));
116
117     if (IsSystemCursorID(cursor_id))
118       module_handle = NULL;
119
120     return LoadCursor(module_handle, cursor_id);
121   }
122
123   if (custom_cursor_) {
124     DCHECK(external_cursor_ == NULL);
125     return custom_cursor_;
126   }
127
128   if (external_cursor_)
129     return external_cursor_;
130
131   custom_cursor_ =
132       IconUtil::CreateCursorFromDIB(
133           custom_size_,
134           hotspot_,
135           !custom_data_.empty() ? &custom_data_[0] : NULL,
136           custom_data_.size());
137   return custom_cursor_;
138 }
139
140 gfx::NativeCursor WebCursor::GetNativeCursor() {
141   return GetCursor(NULL);
142 }
143
144 void WebCursor::InitPlatformData() {
145   custom_cursor_ = NULL;
146 }
147
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_));
153 }
154
155 bool WebCursor::DeserializePlatformData(PickleIterator* iter) {
156   return iter->ReadUInt32(reinterpret_cast<uint32*>(&external_cursor_));
157 }
158
159 bool WebCursor::IsPlatformDataEqual(const WebCursor& other) const {
160   if (!IsCustom())
161     return true;
162
163   return (external_cursor_ == other.external_cursor_);
164 }
165
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;
172 }
173
174 void WebCursor::CleanupPlatformData() {
175   external_cursor_ = NULL;
176
177   if (custom_cursor_) {
178     DestroyIcon(custom_cursor_);
179     custom_cursor_ = NULL;
180   }
181 }