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" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 DCHECK(cursor_image); | 33 DCHECK(cursor_image); |
34 return [[[NSCursor alloc] initWithImage:cursor_image | 34 return [[[NSCursor alloc] initWithImage:cursor_image |
35 hotSpot:NSMakePoint(x, y)] autorelease]; | 35 hotSpot:NSMakePoint(x, y)] autorelease]; |
36 } | 36 } |
37 | 37 |
38 CGImageRef CreateCGImageFromCustomData(const std::vector<char>& custom_data, | 38 CGImageRef CreateCGImageFromCustomData(const std::vector<char>& custom_data, |
39 const gfx::Size& custom_size) { | 39 const gfx::Size& custom_size) { |
40 base::mac::ScopedCFTypeRef<CGColorSpaceRef> cg_color( | 40 base::mac::ScopedCFTypeRef<CGColorSpaceRef> cg_color( |
41 CGColorSpaceCreateDeviceRGB()); | 41 CGColorSpaceCreateDeviceRGB()); |
42 // This is safe since we're not going to draw into the context we're creating. | 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]); | 43 void* data = NULL; |
44 if (!custom_data.empty()) | |
45 data = const_cast<char*>(&custom_data[0]); | |
44 // The settings here match SetCustomData() below; keep in sync. | 46 // The settings here match SetCustomData() below; keep in sync. |
45 base::mac::ScopedCFTypeRef<CGContextRef> context( | 47 base::mac::ScopedCFTypeRef<CGContextRef> context( |
46 CGBitmapContextCreate(data, | 48 CGBitmapContextCreate(data, |
Avi (use Gerrit)
2011/06/29 00:22:47
You can do that, pass in a null data for a 0x0 ima
Scott Hess - ex-Googler
2011/06/29 01:06:13
Docs say that passing NULL means that Quartz alloc
| |
47 custom_size.width(), | 49 custom_size.width(), |
48 custom_size.height(), | 50 custom_size.height(), |
49 8, | 51 8, |
50 custom_size.width()*4, | 52 custom_size.width()*4, |
51 cg_color.get(), | 53 cg_color.get(), |
52 kCGImageAlphaPremultipliedLast | | 54 kCGImageAlphaPremultipliedLast | |
53 kCGBitmapByteOrder32Big)); | 55 kCGBitmapByteOrder32Big)); |
54 return CGBitmapContextCreateImage(context.get()); | 56 return CGBitmapContextCreateImage(context.get()); |
55 } | 57 } |
56 | 58 |
57 NSCursor* CreateCustomCursor(const std::vector<char>& custom_data, | 59 NSCursor* CreateCustomCursor(const std::vector<char>& custom_data, |
58 const gfx::Size& custom_size, | 60 const gfx::Size& custom_size, |
59 const gfx::Point& hotspot) { | 61 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( | 62 base::mac::ScopedCFTypeRef<CGImageRef> cg_image( |
65 CreateCGImageFromCustomData(custom_data, custom_size)); | 63 CreateCGImageFromCustomData(custom_data, custom_size)); |
66 | 64 |
67 NSBitmapImageRep* ns_bitmap = | 65 NSBitmapImageRep* ns_bitmap = |
68 [[NSBitmapImageRep alloc] initWithCGImage:cg_image.get()]; | 66 [[NSBitmapImageRep alloc] initWithCGImage:cg_image.get()]; |
69 NSImage* cursor_image = [[NSImage alloc] init]; | 67 NSImage* cursor_image = [[NSImage alloc] init]; |
70 DCHECK(cursor_image); | 68 DCHECK(cursor_image); |
71 [cursor_image addRepresentation:ns_bitmap]; | 69 [cursor_image addRepresentation:ns_bitmap]; |
72 [ns_bitmap release]; | 70 [ns_bitmap release]; |
73 | 71 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 return true; | 403 return true; |
406 } | 404 } |
407 | 405 |
408 void WebCursor::CleanupPlatformData() { | 406 void WebCursor::CleanupPlatformData() { |
409 return; | 407 return; |
410 } | 408 } |
411 | 409 |
412 void WebCursor::CopyPlatformData(const WebCursor& other) { | 410 void WebCursor::CopyPlatformData(const WebCursor& other) { |
413 return; | 411 return; |
414 } | 412 } |
OLD | NEW |