OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
3 // LICENSE file. | 3 // LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "NativeImageSkia.h" | 6 #include "NativeImageSkia.h" |
7 #include "PlatformCursor.h" | 7 #include "PlatformCursor.h" |
8 | 8 |
9 #undef LOG | 9 #undef LOG |
10 #include "base/gfx/gdi_util.h" | 10 #include "base/gfx/gdi_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/pickle.h" |
12 #include "skia/include/SkBitmap.h" | 13 #include "skia/include/SkBitmap.h" |
13 #include "webkit/glue/webcursor.h" | 14 #include "webkit/glue/webcursor.h" |
14 #include "webkit/glue/webkit_resources.h" | 15 #include "webkit/glue/webkit_resources.h" |
15 | 16 |
16 using WebCore::PlatformCursor; | 17 using WebCore::PlatformCursor; |
17 | 18 |
18 static LPCWSTR ToCursorID(PlatformCursor::Type type) { | 19 static LPCWSTR ToCursorID(PlatformCursor::Type type) { |
19 switch (type) { | 20 switch (type) { |
20 case PlatformCursor::typePointer: | 21 case PlatformCursor::typePointer: |
21 return IDC_ARROW; | 22 return IDC_ARROW; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 { LoadCursor(NULL, IDC_SIZEALL), PlatformCursor::typeMove }, | 125 { LoadCursor(NULL, IDC_SIZEALL), PlatformCursor::typeMove }, |
125 { LoadCursor(NULL, IDC_NO), PlatformCursor::typeNotAllowed }, | 126 { LoadCursor(NULL, IDC_NO), PlatformCursor::typeNotAllowed }, |
126 { LoadCursor(NULL, IDC_HAND), PlatformCursor::typeHand }, | 127 { LoadCursor(NULL, IDC_HAND), PlatformCursor::typeHand }, |
127 { LoadCursor(NULL, IDC_APPSTARTING), PlatformCursor::typeProgress }, | 128 { LoadCursor(NULL, IDC_APPSTARTING), PlatformCursor::typeProgress }, |
128 { LoadCursor(NULL, IDC_HELP), PlatformCursor::typeHelp }, | 129 { LoadCursor(NULL, IDC_HELP), PlatformCursor::typeHelp }, |
129 }; | 130 }; |
130 for (int i = 0; i < arraysize(kStandardCursors); i++) { | 131 for (int i = 0; i < arraysize(kStandardCursors); i++) { |
131 if (cursor == kStandardCursors[i].cursor) | 132 if (cursor == kStandardCursors[i].cursor) |
132 return kStandardCursors[i].type; | 133 return kStandardCursors[i].type; |
133 } | 134 } |
134 return PlatformCursor::typePointer; | 135 return PlatformCursor::typeCustom; |
135 } | 136 } |
136 | 137 |
137 HCURSOR WebCursor::GetCursor(HINSTANCE module_handle) const { | 138 HCURSOR WebCursor::GetCursor(HINSTANCE module_handle){ |
138 if (IsCustom()) | 139 if (!IsCustom()) { |
139 return NULL; | 140 const wchar_t* cursor_id = |
| 141 ToCursorID(static_cast<PlatformCursor::Type>(type_)); |
140 | 142 |
141 LPCWSTR cursor_id = ToCursorID(static_cast<PlatformCursor::Type>(type_)); | 143 if (IsSystemCursorID(cursor_id)) |
| 144 module_handle = NULL; |
142 | 145 |
143 if (IsSystemCursorID(cursor_id)) | 146 return LoadCursor(module_handle, cursor_id); |
144 module_handle = NULL; | 147 } |
145 | 148 |
146 return LoadCursor(module_handle, cursor_id); | 149 if (custom_cursor_) { |
147 } | 150 DCHECK(external_cursor_ == NULL); |
| 151 return custom_cursor_; |
| 152 } |
148 | 153 |
149 HCURSOR WebCursor::GetCustomCursor() const { | 154 if (external_cursor_) |
150 if (!IsCustom()) | 155 return external_cursor_; |
151 return NULL; | |
152 | 156 |
153 BITMAPINFO cursor_bitmap_info = {0}; | 157 BITMAPINFO cursor_bitmap_info = {0}; |
154 gfx::CreateBitmapHeader( | 158 gfx::CreateBitmapHeader( |
155 custom_size_.width(), custom_size_.height(), | 159 custom_size_.width(), custom_size_.height(), |
156 reinterpret_cast<BITMAPINFOHEADER*>(&cursor_bitmap_info)); | 160 reinterpret_cast<BITMAPINFOHEADER*>(&cursor_bitmap_info)); |
157 HDC dc = GetDC(0); | 161 HDC dc = GetDC(0); |
158 HDC workingDC = CreateCompatibleDC(dc); | 162 HDC workingDC = CreateCompatibleDC(dc); |
159 HBITMAP bitmap_handle = CreateDIBSection( | 163 HBITMAP bitmap_handle = CreateDIBSection( |
160 dc, &cursor_bitmap_info, DIB_RGB_COLORS, 0, 0, 0); | 164 dc, &cursor_bitmap_info, DIB_RGB_COLORS, 0, 0, 0); |
161 SetDIBits( | 165 SetDIBits( |
162 0, bitmap_handle, 0, custom_size_.height(), &custom_data_[0], | 166 0, bitmap_handle, 0, custom_size_.height(), &custom_data_[0], |
163 &cursor_bitmap_info, DIB_RGB_COLORS); | 167 &cursor_bitmap_info, DIB_RGB_COLORS); |
164 | 168 |
165 HBITMAP old_bitmap = reinterpret_cast<HBITMAP>( | 169 HBITMAP old_bitmap = reinterpret_cast<HBITMAP>( |
166 SelectObject(workingDC, bitmap_handle)); | 170 SelectObject(workingDC, bitmap_handle)); |
167 SetBkMode(workingDC, TRANSPARENT); | 171 SetBkMode(workingDC, TRANSPARENT); |
168 SelectObject(workingDC, old_bitmap); | 172 SelectObject(workingDC, old_bitmap); |
169 | 173 |
170 HBITMAP mask = CreateBitmap( | 174 HBITMAP mask = CreateBitmap( |
171 custom_size_.width(), custom_size_.height(), 1, 1, NULL); | 175 custom_size_.width(), custom_size_.height(), 1, 1, NULL); |
172 ICONINFO ii = {0}; | 176 ICONINFO ii = {0}; |
173 ii.fIcon = FALSE; | 177 ii.fIcon = FALSE; |
174 ii.xHotspot = hotspot_.x(); | 178 ii.xHotspot = hotspot_.x(); |
175 ii.yHotspot = hotspot_.y(); | 179 ii.yHotspot = hotspot_.y(); |
176 ii.hbmMask = mask; | 180 ii.hbmMask = mask; |
177 ii.hbmColor = bitmap_handle; | 181 ii.hbmColor = bitmap_handle; |
178 | 182 |
179 HCURSOR cursor_handle = CreateIconIndirect(&ii); | 183 custom_cursor_ = CreateIconIndirect(&ii); |
180 | 184 |
181 DeleteObject(mask); | 185 DeleteObject(mask); |
182 DeleteObject(bitmap_handle); | 186 DeleteObject(bitmap_handle); |
183 DeleteDC(workingDC); | 187 DeleteDC(workingDC); |
184 ReleaseDC(0, dc); | 188 ReleaseDC(0, dc); |
185 return cursor_handle; | 189 return custom_cursor_; |
186 } | 190 } |
187 | 191 |
188 void WebCursor::InitFromCursor(HCURSOR cursor) { | 192 void WebCursor::InitFromExternalCursor(HCURSOR cursor) { |
189 // TODO(iyengar) Add support for custom cursors. | 193 WebCore::PlatformCursor::Type cursor_type = ToPlatformCursorType(cursor); |
190 *this = WebCursor(ToPlatformCursorType(cursor)); | 194 |
| 195 *this = WebCursor(cursor_type); |
| 196 |
| 197 if (cursor_type == WebCore::PlatformCursor::typeCustom) { |
| 198 external_cursor_ = cursor; |
| 199 } |
191 } | 200 } |
| 201 |
| 202 void WebCursor::InitPlatformData() { |
| 203 external_cursor_ = NULL; |
| 204 custom_cursor_ = NULL; |
| 205 } |
| 206 |
| 207 bool WebCursor::SerializePlatformData(Pickle* pickle) const { |
| 208 // There are some issues with converting certain HCURSORS to bitmaps. The |
| 209 // HCURSOR being a user object can be marshaled as is. |
| 210 return pickle->WriteIntPtr(reinterpret_cast<intptr_t>(external_cursor_)); |
| 211 } |
| 212 |
| 213 bool WebCursor::DeserializePlatformData(const Pickle* pickle, void** iter) { |
| 214 return pickle->ReadIntPtr(iter, |
| 215 reinterpret_cast<intptr_t*>(&external_cursor_)); |
| 216 } |
| 217 |
| 218 bool WebCursor::IsPlatformDataEqual(const WebCursor& other) const { |
| 219 if (!IsCustom()) |
| 220 return true; |
| 221 |
| 222 return (external_cursor_ == other.external_cursor_); |
| 223 } |
| 224 |
| 225 void WebCursor::CopyPlatformData(const WebCursor& other) { |
| 226 external_cursor_ = other.external_cursor_; |
| 227 // The custom_cursor_ member will be initialized to a HCURSOR the next time |
| 228 // the GetCursor member function is invoked on this WebCursor instance. The |
| 229 // cursor is created using the data in the custom_data_ vector. |
| 230 custom_cursor_ = NULL; |
| 231 } |
| 232 |
| 233 void WebCursor::CleanupPlatformData() { |
| 234 external_cursor_ = NULL; |
| 235 |
| 236 if (custom_cursor_) { |
| 237 DestroyIcon(custom_cursor_); |
| 238 custom_cursor_ = NULL; |
| 239 } |
| 240 } |
OLD | NEW |