| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #import "chrome/browser/cocoa/web_drag_source.h" | 5 #import "chrome/browser/cocoa/web_drag_source.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/nsimage_cache_mac.h" | 8 #include "base/nsimage_cache_mac.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 using base::SysUTF16ToNSString; | 28 using base::SysUTF16ToNSString; |
| 29 using net::FileStream; | 29 using net::FileStream; |
| 30 | 30 |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // An unofficial standard pasteboard title type to be provided alongside the | 34 // An unofficial standard pasteboard title type to be provided alongside the |
| 35 // |NSURLPboardType|. | 35 // |NSURLPboardType|. |
| 36 NSString* const kNSURLTitlePboardType = @"public.url-name"; | 36 NSString* const kNSURLTitlePboardType = @"public.url-name"; |
| 37 | 37 |
| 38 // Make a drag image from the drop data. | |
| 39 // TODO(viettrungluu): Move this somewhere more sensible. | |
| 40 NSImage* MakeDragImage(const WebDropData* drop_data) { | |
| 41 // TODO(viettrungluu): Just a stub for now. Make it do something (see, e.g., | |
| 42 // WebKit/WebKit/mac/Misc/WebNSViewExtras.m: |-_web_DragImageForElement:...|). | |
| 43 | |
| 44 // Default to returning a generic image. | |
| 45 return nsimage_cache::ImageNamed(@"nav.pdf"); | |
| 46 } | |
| 47 | |
| 48 // Returns a filename appropriate for the drop data | 38 // Returns a filename appropriate for the drop data |
| 49 // TODO(viettrungluu): Refactor to make it common across platforms, | 39 // TODO(viettrungluu): Refactor to make it common across platforms, |
| 50 // and move it somewhere sensible. | 40 // and move it somewhere sensible. |
| 51 FilePath GetFileNameFromDragData(const WebDropData& drop_data) { | 41 FilePath GetFileNameFromDragData(const WebDropData& drop_data) { |
| 52 // Images without ALT text will only have a file extension so we need to | 42 // Images without ALT text will only have a file extension so we need to |
| 53 // synthesize one from the provided extension and URL. | 43 // synthesize one from the provided extension and URL. |
| 54 FilePath file_name([SysUTF16ToNSString(drop_data.file_description_filename) | 44 FilePath file_name([SysUTF16ToNSString(drop_data.file_description_filename) |
| 55 fileSystemRepresentation]); | 45 fileSystemRepresentation]); |
| 56 file_name = file_name.BaseName().RemoveExtension(); | 46 file_name = file_name.BaseName().RemoveExtension(); |
| 57 | 47 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 - (void)fillPasteboard; | 105 - (void)fillPasteboard; |
| 116 - (NSImage*)dragImage; | 106 - (NSImage*)dragImage; |
| 117 | 107 |
| 118 @end // @interface WebDragSource(Private) | 108 @end // @interface WebDragSource(Private) |
| 119 | 109 |
| 120 | 110 |
| 121 @implementation WebDragSource | 111 @implementation WebDragSource |
| 122 | 112 |
| 123 - (id)initWithContentsView:(TabContentsViewCocoa*)contentsView | 113 - (id)initWithContentsView:(TabContentsViewCocoa*)contentsView |
| 124 dropData:(const WebDropData*)dropData | 114 dropData:(const WebDropData*)dropData |
| 115 image:(NSImage*)image |
| 116 offset:(NSPoint)offset |
| 125 pasteboard:(NSPasteboard*)pboard | 117 pasteboard:(NSPasteboard*)pboard |
| 126 dragOperationMask:(NSDragOperation)dragOperationMask { | 118 dragOperationMask:(NSDragOperation)dragOperationMask { |
| 127 if ((self = [super init])) { | 119 if ((self = [super init])) { |
| 128 contentsView_ = contentsView; | 120 contentsView_ = contentsView; |
| 129 DCHECK(contentsView_); | 121 DCHECK(contentsView_); |
| 130 | 122 |
| 131 dropData_.reset(new WebDropData(*dropData)); | 123 dropData_.reset(new WebDropData(*dropData)); |
| 132 DCHECK(dropData_.get()); | 124 DCHECK(dropData_.get()); |
| 133 | 125 |
| 126 dragImage_.reset([image retain]); |
| 127 imageOffset_ = offset; |
| 128 |
| 134 pasteboard_.reset([pboard retain]); | 129 pasteboard_.reset([pboard retain]); |
| 135 DCHECK(pasteboard_.get()); | 130 DCHECK(pasteboard_.get()); |
| 136 | 131 |
| 137 dragOperationMask_ = dragOperationMask; | 132 dragOperationMask_ = dragOperationMask; |
| 138 | 133 |
| 139 [self fillPasteboard]; | 134 [self fillPasteboard]; |
| 140 } | 135 } |
| 141 | 136 |
| 142 return self; | 137 return self; |
| 143 } | 138 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 NSEvent* dragEvent = [NSEvent mouseEventWithType:NSLeftMouseDragged | 213 NSEvent* dragEvent = [NSEvent mouseEventWithType:NSLeftMouseDragged |
| 219 location:position | 214 location:position |
| 220 modifierFlags:NSLeftMouseDraggedMask | 215 modifierFlags:NSLeftMouseDraggedMask |
| 221 timestamp:eventTime | 216 timestamp:eventTime |
| 222 windowNumber:[window windowNumber] | 217 windowNumber:[window windowNumber] |
| 223 context:nil | 218 context:nil |
| 224 eventNumber:0 | 219 eventNumber:0 |
| 225 clickCount:1 | 220 clickCount:1 |
| 226 pressure:1.0]; | 221 pressure:1.0]; |
| 227 | 222 |
| 223 if (dragImage_) { |
| 224 position.x -= imageOffset_.x; |
| 225 // Deal with Cocoa's flipped coordinate system. |
| 226 position.y -= [dragImage_.get() size].height - imageOffset_.y; |
| 227 } |
| 228 // Per kwebster, offset arg is ignored, see -_web_DragImageForElement: in |
| 229 // third_party/WebKit/WebKit/mac/Misc/WebNSViewExtras.m. |
| 228 [window dragImage:[self dragImage] | 230 [window dragImage:[self dragImage] |
| 229 at:position | 231 at:position |
| 230 offset:NSZeroSize | 232 offset:NSZeroSize |
| 231 event:dragEvent | 233 event:dragEvent |
| 232 pasteboard:pasteboard_ | 234 pasteboard:pasteboard_ |
| 233 source:contentsView_ | 235 source:contentsView_ |
| 234 slideBack:YES]; | 236 slideBack:YES]; |
| 235 } | 237 } |
| 236 | 238 |
| 237 - (void)endDragAt:(NSPoint)screenPoint | 239 - (void)endDragAt:(NSPoint)screenPoint |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 381 } |
| 380 } | 382 } |
| 381 | 383 |
| 382 // Plain text. | 384 // Plain text. |
| 383 if (!dropData_->plain_text.empty()) | 385 if (!dropData_->plain_text.empty()) |
| 384 [pasteboard_ addTypes:[NSArray arrayWithObject:NSStringPboardType] | 386 [pasteboard_ addTypes:[NSArray arrayWithObject:NSStringPboardType] |
| 385 owner:contentsView_]; | 387 owner:contentsView_]; |
| 386 } | 388 } |
| 387 | 389 |
| 388 - (NSImage*)dragImage { | 390 - (NSImage*)dragImage { |
| 389 return MakeDragImage(dropData_.get()); | 391 if (dragImage_) |
| 392 return dragImage_; |
| 393 |
| 394 // Default to returning a generic image. |
| 395 return nsimage_cache::ImageNamed(@"nav.pdf"); |
| 390 } | 396 } |
| 391 | 397 |
| 392 @end // @implementation WebDragSource (Private) | 398 @end // @implementation WebDragSource (Private) |
| OLD | NEW |