| 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 "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/nsimage_cache_mac.h" | 11 #include "base/nsimage_cache_mac.h" |
| 12 #include "base/scoped_cftyperef.h" | 12 #include "base/scoped_cftyperef.h" |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebCursorInfo.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebCursorInfo.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebImage.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebImage.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" |
| 16 | 16 |
| 17 using WebKit::WebCursorInfo; | 17 using WebKit::WebCursorInfo; |
| 18 using WebKit::WebImage; | 18 using WebKit::WebImage; |
| 19 using WebKit::WebSize; | 19 using WebKit::WebSize; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // TODO: This image fech can (and probably should) be serviced by the resource | 23 // TODO: This image fetch can (and probably should) be serviced by the resource |
| 24 // resource bundle instead of going through nsimage_cache. | 24 // resource bundle instead of going through nsimage_cache. |
| 25 NSCursor* LoadCursor(const char* name, int x, int y) { | 25 NSCursor* LoadCursor(const char* name, int x, int y) { |
| 26 NSString* file_name = [NSString stringWithUTF8String:name]; | 26 NSString* file_name = [NSString stringWithUTF8String:name]; |
| 27 DCHECK(file_name); | 27 DCHECK(file_name); |
| 28 NSImage* cursor_image = nsimage_cache::ImageNamed(file_name); | 28 NSImage* cursor_image = nsimage_cache::ImageNamed(file_name); |
| 29 DCHECK(cursor_image); | 29 DCHECK(cursor_image); |
| 30 return [[[NSCursor alloc] initWithImage:cursor_image | 30 return [[[NSCursor alloc] initWithImage:cursor_image |
| 31 hotSpot:NSMakePoint(x, y)] autorelease]; | 31 hotSpot:NSMakePoint(x, y)] autorelease]; |
| 32 } | 32 } |
| 33 | 33 |
| 34 CGImageRef CreateCGImageFromCustomData(const std::vector<char>& custom_data, | 34 CGImageRef CreateCGImageFromCustomData(const std::vector<char>& custom_data, |
| 35 const gfx::Size& custom_size) { | 35 const gfx::Size& custom_size) { |
| 36 scoped_cftyperef<CGColorSpaceRef> cg_color(CGColorSpaceCreateDeviceRGB()); | 36 scoped_cftyperef<CGColorSpaceRef> cg_color(CGColorSpaceCreateDeviceRGB()); |
| 37 // this is safe since we're not going to draw into the context we're creating | 37 // This is safe since we're not going to draw into the context we're creating. |
| 38 void* data = const_cast<char*>(&custom_data[0]); | 38 void* data = const_cast<char*>(&custom_data[0]); |
| 39 // settings here match SetCustomData() below; keep in sync | 39 // The settings here match SetCustomData() below; keep in sync. |
| 40 scoped_cftyperef<CGContextRef> context( | 40 scoped_cftyperef<CGContextRef> context( |
| 41 CGBitmapContextCreate(data, | 41 CGBitmapContextCreate(data, |
| 42 custom_size.width(), | 42 custom_size.width(), |
| 43 custom_size.height(), | 43 custom_size.height(), |
| 44 8, | 44 8, |
| 45 custom_size.width()*4, | 45 custom_size.width()*4, |
| 46 cg_color.get(), | 46 cg_color.get(), |
| 47 kCGImageAlphaPremultipliedLast | | 47 kCGImageAlphaPremultipliedLast | |
| 48 kCGBitmapByteOrder32Big)); | 48 kCGBitmapByteOrder32Big)); |
| 49 return CGBitmapContextCreateImage(context.get()); | 49 return CGBitmapContextCreateImage(context.get()); |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 break; | 217 break; |
| 218 case kThemePoofCursor: // *shrug* | 218 case kThemePoofCursor: // *shrug* |
| 219 default: | 219 default: |
| 220 cursor_info.type = WebCursorInfo::TypePointer; | 220 cursor_info.type = WebCursorInfo::TypePointer; |
| 221 break; | 221 break; |
| 222 } | 222 } |
| 223 | 223 |
| 224 InitFromCursorInfo(cursor_info); | 224 InitFromCursorInfo(cursor_info); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void WebCursor::InitFromNSCursor(NSCursor* cursor) { |
| 228 WebKit::WebCursorInfo cursor_info; |
| 229 |
| 230 if ([cursor isEqual:[NSCursor arrowCursor]]) { |
| 231 cursor_info.type = WebCursorInfo::TypePointer; |
| 232 } else if ([cursor isEqual:[NSCursor IBeamCursor]]) { |
| 233 cursor_info.type = WebCursorInfo::TypeIBeam; |
| 234 } else if ([cursor isEqual:[NSCursor crosshairCursor]]) { |
| 235 cursor_info.type = WebCursorInfo::TypeCross; |
| 236 } else if ([cursor isEqual:[NSCursor pointingHandCursor]]) { |
| 237 cursor_info.type = WebCursorInfo::TypeHand; |
| 238 } else if ([cursor isEqual:[NSCursor resizeLeftCursor]]) { |
| 239 cursor_info.type = WebCursorInfo::TypeWestResize; |
| 240 } else if ([cursor isEqual:[NSCursor resizeRightCursor]]) { |
| 241 cursor_info.type = WebCursorInfo::TypeEastResize; |
| 242 } else if ([cursor isEqual:[NSCursor resizeLeftRightCursor]]) { |
| 243 cursor_info.type = WebCursorInfo::TypeEastWestResize; |
| 244 } else if ([cursor isEqual:[NSCursor resizeUpCursor]]) { |
| 245 cursor_info.type = WebCursorInfo::TypeNorthResize; |
| 246 } else if ([cursor isEqual:[NSCursor resizeDownCursor]]) { |
| 247 cursor_info.type = WebCursorInfo::TypeSouthResize; |
| 248 } else if ([cursor isEqual:[NSCursor resizeUpDownCursor]]) { |
| 249 cursor_info.type = WebCursorInfo::TypeNorthSouthResize; |
| 250 } else { |
| 251 // Also handles the [NSCursor closedHandCursor], [NSCursor openHandCursor], |
| 252 // and [NSCursor disappearingItemCursor] cases. Quick-and-dirty image |
| 253 // conversion; TODO(avi): do better. |
| 254 CGImageRef cg_image = nil; |
| 255 NSImage* image = [cursor image]; |
| 256 for (id rep in [image representations]) { |
| 257 if ([rep isKindOfClass:[NSBitmapImageRep class]]) { |
| 258 cg_image = [rep CGImage]; |
| 259 break; |
| 260 } |
| 261 } |
| 262 |
| 263 if (cg_image) { |
| 264 cursor_info.type = WebCursorInfo::TypeCustom; |
| 265 NSPoint hot_spot = [cursor hotSpot]; |
| 266 cursor_info.hotSpot = WebKit::WebPoint(hot_spot.x, hot_spot.y); |
| 267 cursor_info.customImage = cg_image; |
| 268 } else { |
| 269 cursor_info.type = WebCursorInfo::TypePointer; |
| 270 } |
| 271 } |
| 272 |
| 273 InitFromCursorInfo(cursor_info); |
| 274 } |
| 275 |
| 227 void WebCursor::SetCustomData(const WebImage& image) { | 276 void WebCursor::SetCustomData(const WebImage& image) { |
| 228 if (image.isNull()) | 277 if (image.isNull()) |
| 229 return; | 278 return; |
| 230 | 279 |
| 231 scoped_cftyperef<CGColorSpaceRef> cg_color( | 280 scoped_cftyperef<CGColorSpaceRef> cg_color( |
| 232 CGColorSpaceCreateDeviceRGB()); | 281 CGColorSpaceCreateDeviceRGB()); |
| 233 | 282 |
| 234 const WebSize& image_dimensions = image.size(); | 283 const WebSize& image_dimensions = image.size(); |
| 235 int image_width = image_dimensions.width; | 284 int image_width = image_dimensions.width; |
| 236 int image_height = image_dimensions.height; | 285 int image_height = image_dimensions.height; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 return true; | 330 return true; |
| 282 } | 331 } |
| 283 | 332 |
| 284 void WebCursor::CleanupPlatformData() { | 333 void WebCursor::CleanupPlatformData() { |
| 285 return; | 334 return; |
| 286 } | 335 } |
| 287 | 336 |
| 288 void WebCursor::CopyPlatformData(const WebCursor& other) { | 337 void WebCursor::CopyPlatformData(const WebCursor& other) { |
| 289 return; | 338 return; |
| 290 } | 339 } |
| OLD | NEW |