-
Notifications
You must be signed in to change notification settings - Fork 1
/
DragView.m
120 lines (87 loc) · 3.18 KB
/
DragView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// DragView.m
//
//
// Created by Keaton Burleson on 7/5/15.
//
//
#import "DragView.h"
@implementation DragView
- (id)initWithFrame:(NSRect)frame {
if (! (self = [super initWithFrame:frame] ) ) {
NSLog(@"Error: MyNSView initWithFrame");
return self;
} // end if
self.nsImageObj = nil;
[self registerForDraggedTypes:
[NSArray arrayWithObjects:NSTIFFPboardType,NSFilenamesPboardType,nil]];
return self;
} // end initWithFrame
- (NSDragOperation)draggingEntered:(id )sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
== NSDragOperationGeneric) {
return NSDragOperationGeneric;
} // end if
// not a drag we can use
return NSDragOperationNone;
} // end draggingEntered
- (BOOL)prepareForDragOperation:(id )sender {
return YES;
} // end prepareForDragOperation
(BOOL)performDragOperation:(id )sender {
NSPasteboard *zPasteboard = [sender draggingPasteboard];
// define the images types we accept
// NSPasteboardTypeTIFF: (used to be NSTIFFPboardType).
// NSFilenamesPboardType:An array of NSString filenames
NSArray *zImageTypesAry = [NSArray arrayWithObjects:NSPasteboardTypeTIFF,
NSFilenamesPboardType, nil];
NSString *zDesiredType =
[zPasteboard availableTypeFromArray:zImageTypesAry];
if ([zDesiredType isEqualToString:NSPasteboardTypeTIFF]) {
NSData *zPasteboardData = [zPasteboard dataForType:zDesiredType];
if (zPasteboardData == nil) {
NSLog(@"Error: MyNSView zPasteboardData == nil");
return NO;
} // end if
self.nsImageObj = [[NSImage alloc] initWithData:zPasteboardData];
[self setNeedsDisplay:YES];
return YES;
} //end if
if ([zDesiredType isEqualToString:NSFilenamesPboardType]) {
// the pasteboard contains a list of file names
//Take the first one
NSArray *zFileNamesAry =
[zPasteboard propertyListForType:@"NSFilenamesPboardType"];
NSString *zPath = [zFileNamesAry objectAtIndex:0];
NSImage *zNewImage = [[NSImage alloc] initWithContentsOfFile:zPath];
if (zNewImage == nil) {
NSLog(@"Error: MyNSView performDragOperation zNewImage == nil");
return NO;
}// end if
self.nsImageObj = zNewImage;
[self setNeedsDisplay:YES];
return YES;
}// end if
//this cant happen ???
NSLog(@"Error MyNSView performDragOperation");
return NO;
} // end performDragOperation
- (void)concludeDragOperation:(id )sender {
[self setNeedsDisplay:YES];
} // end concludeDragOperation
- (void)drawRect:(NSRect)dirtyRect {
if (self.nsImageObj == nil) {
[[NSColor blackColor] set];
NSRectFill( dirtyRect );
} // end if
NSRect zOurBounds = [self bounds];
[super drawRect:dirtyRect];
[self.nsImageObj compositeToPoint:(zOurBounds.origin)
operation:NSCompositeSourceOver];
} // end drawRect
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
@end