OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/webcursor.h" | 5 #include "webkit/glue/webcursor.h" |
6 | 6 |
7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
8 #include <Carbon/Carbon.h> | 8 #include <Carbon/Carbon.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/memory/scoped_nsobject.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebImage.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebImage.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
15 #include "ui/gfx/mac/nsimage_cache.h" | 16 #include "ui/gfx/mac/nsimage_cache.h" |
16 | 17 |
17 #if WEBKIT_USING_SKIA | 18 #if WEBKIT_USING_SKIA |
18 #include "skia/ext/skia_utils_mac.h" | 19 #include "skia/ext/skia_utils_mac.h" |
19 #endif | 20 #endif |
20 | 21 |
21 using WebKit::WebCursorInfo; | 22 using WebKit::WebCursorInfo; |
22 using WebKit::WebImage; | 23 using WebKit::WebImage; |
23 using WebKit::WebSize; | 24 using WebKit::WebSize; |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // TODO: This image fetch can (and probably should) be serviced by the resource | 28 // TODO: This image fetch can (and probably should) be serviced by the resource |
28 // resource bundle instead of going through the image cache. | 29 // resource bundle instead of going through the image cache. |
29 NSCursor* LoadCursor(const char* name, int x, int y) { | 30 NSCursor* LoadCursor(const char* name, int x, int y) { |
30 NSString* file_name = [NSString stringWithUTF8String:name]; | 31 NSString* file_name = [NSString stringWithUTF8String:name]; |
31 DCHECK(file_name); | 32 DCHECK(file_name); |
32 NSImage* cursor_image = gfx::GetCachedImageWithName(file_name); | 33 NSImage* cursor_image = gfx::GetCachedImageWithName(file_name); |
33 DCHECK(cursor_image); | 34 DCHECK(cursor_image); |
34 return [[[NSCursor alloc] initWithImage:cursor_image | 35 return [[[NSCursor alloc] initWithImage:cursor_image |
35 hotSpot:NSMakePoint(x, y)] autorelease]; | 36 hotSpot:NSMakePoint(x, y)] autorelease]; |
36 } | 37 } |
37 | 38 |
38 CGImageRef CreateCGImageFromCustomData(const std::vector<char>& custom_data, | 39 CGImageRef CreateCGImageFromCustomData(const std::vector<char>& custom_data, |
39 const gfx::Size& custom_size) { | 40 const gfx::Size& custom_size) { |
| 41 // This is safe since we're not going to draw into the context we're creating. |
| 42 // The settings here match SetCustomData() below; keep in sync. |
| 43 // If the data is missing, leave the backing transparent. |
| 44 void* data = NULL; |
| 45 if (!custom_data.empty()) |
| 46 data = const_cast<char*>(&custom_data[0]); |
| 47 |
| 48 // If the size is empty, use a 1x1 transparent image. |
| 49 gfx::Size size = custom_size; |
| 50 if (size.IsEmpty()) { |
| 51 size.SetSize(1, 1); |
| 52 data = NULL; |
| 53 } |
| 54 |
40 base::mac::ScopedCFTypeRef<CGColorSpaceRef> cg_color( | 55 base::mac::ScopedCFTypeRef<CGColorSpaceRef> cg_color( |
41 CGColorSpaceCreateDeviceRGB()); | 56 CGColorSpaceCreateDeviceRGB()); |
42 // This is safe since we're not going to draw into the context we're creating. | |
43 void* data = const_cast<char*>(&custom_data[0]); | |
44 // The settings here match SetCustomData() below; keep in sync. | |
45 base::mac::ScopedCFTypeRef<CGContextRef> context( | 57 base::mac::ScopedCFTypeRef<CGContextRef> context( |
46 CGBitmapContextCreate(data, | 58 CGBitmapContextCreate(data, |
47 custom_size.width(), | 59 size.width(), |
48 custom_size.height(), | 60 size.height(), |
49 8, | 61 8, |
50 custom_size.width()*4, | 62 size.width()*4, |
51 cg_color.get(), | 63 cg_color.get(), |
52 kCGImageAlphaPremultipliedLast | | 64 kCGImageAlphaPremultipliedLast | |
53 kCGBitmapByteOrder32Big)); | 65 kCGBitmapByteOrder32Big)); |
54 return CGBitmapContextCreateImage(context.get()); | 66 return CGBitmapContextCreateImage(context.get()); |
55 } | 67 } |
56 | 68 |
57 NSCursor* CreateCustomCursor(const std::vector<char>& custom_data, | 69 NSCursor* CreateCustomCursor(const std::vector<char>& custom_data, |
58 const gfx::Size& custom_size, | 70 const gfx::Size& custom_size, |
59 const gfx::Point& hotspot) { | 71 const gfx::Point& hotspot) { |
60 // CG throws a cocoa exception if we try to create an empty image, which | |
61 // results in an infinite loop. This CHECK ensures that we crash instead. | |
62 CHECK(!custom_data.empty()); | |
63 | |
64 base::mac::ScopedCFTypeRef<CGImageRef> cg_image( | 72 base::mac::ScopedCFTypeRef<CGImageRef> cg_image( |
65 CreateCGImageFromCustomData(custom_data, custom_size)); | 73 CreateCGImageFromCustomData(custom_data, custom_size)); |
66 | 74 |
67 NSBitmapImageRep* ns_bitmap = | 75 scoped_nsobject<NSBitmapImageRep> ns_bitmap( |
68 [[NSBitmapImageRep alloc] initWithCGImage:cg_image.get()]; | 76 [[NSBitmapImageRep alloc] initWithCGImage:cg_image.get()]); |
69 NSImage* cursor_image = [[NSImage alloc] init]; | 77 NSImage* cursor_image = [[NSImage alloc] init]; |
70 DCHECK(cursor_image); | 78 DCHECK(cursor_image); |
71 [cursor_image addRepresentation:ns_bitmap]; | 79 [cursor_image addRepresentation:ns_bitmap]; |
72 [ns_bitmap release]; | |
73 | 80 |
74 NSCursor* cursor = [[NSCursor alloc] initWithImage:cursor_image | 81 NSCursor* cursor = [[NSCursor alloc] initWithImage:cursor_image |
75 hotSpot:NSMakePoint(hotspot.x(), | 82 hotSpot:NSMakePoint(hotspot.x(), |
76 hotspot.y())]; | 83 hotspot.y())]; |
77 [cursor_image release]; | 84 [cursor_image release]; |
78 | 85 |
79 return [cursor autorelease]; | 86 return [cursor autorelease]; |
80 } | 87 } |
81 | 88 |
82 } // namespace | 89 } // namespace |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 return true; | 412 return true; |
406 } | 413 } |
407 | 414 |
408 void WebCursor::CleanupPlatformData() { | 415 void WebCursor::CleanupPlatformData() { |
409 return; | 416 return; |
410 } | 417 } |
411 | 418 |
412 void WebCursor::CopyPlatformData(const WebCursor& other) { | 419 void WebCursor::CopyPlatformData(const WebCursor& other) { |
413 return; | 420 return; |
414 } | 421 } |
OLD | NEW |