Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Mon Jun 03, 2024 5:54 pm Post subject: Drag & Drop |
|
|
Code: | #pragma mark *** Drag and Drop ***
- (NSUInteger)dragOperationForDraggingInfo:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
NSString *type = [pboard availableTypeFromArray:[NSArray arrayWithObjects:NSColorPboardType, NSFilenamesPboardType, nil]];
if (type) {
if ([type isEqualToString:NSColorPboardType]) {
NSPoint point = [self convertPoint:[sender draggingLocation] fromView:nil];
if ([self graphicUnderPoint:point index:NULL isSelected:NULL handle:NULL]) {
return NSDragOperationGeneric;
}
}
if ([type isEqualToString:NSFilenamesPboardType]) {
return NSDragOperationCopy;
}
}
type = [pboard availableTypeFromArray:[NSImage imagePasteboardTypes]];
if (type) {
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
// Conformance to the NSObject(NSDraggingDestination) informal protocol.
- (NSUInteger)draggingEntered:(id <NSDraggingInfo>)sender {
return [self dragOperationForDraggingInfo:sender];
}
- (NSUInteger)draggingUpdated:(id <NSDraggingInfo>)sender {
return [self dragOperationForDraggingInfo:sender];
}
- (void)draggingExited:(id <NSDraggingInfo>)sender {
return;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
NSString *type = [pboard availableTypeFromArray:[NSArray arrayWithObjects:NSColorPboardType, NSFilenamesPboardType, nil]];
NSPoint point = [self convertPoint:[sender draggingLocation] fromView:nil];
NSPoint draggedImageLocation = [self convertPoint:[sender draggedImageLocation] fromView:nil];
if (type) {
if ([type isEqualToString:NSColorPboardType]) {
SKTGraphic *hitGraphic = [self graphicUnderPoint:point index:NULL isSelected:NULL handle:NULL];
if (hitGraphic) {
NSColor *color = [[NSColor colorFromPasteboard:pboard] colorWithAlphaComponent:1.0];
[hitGraphic setColor:color];
}
} else if ([type isEqualToString:NSFilenamesPboardType]) {
NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
// Handle multiple files (cascade them?)
if ([filenames count] == 1) {
NSString *filename = [filenames objectAtIndex:0];
[self makeNewImageFromContentsOfFile:filename atPoint:point];
}
}
return;
}
(void)[self makeNewImageFromPasteboard:pboard atPoint:draggedImageLocation];
}
|
|
|