moved FOP to libs subdir
[TestXSLT.git] / DragDestinationTextView.m
1
2
3
4 #import "DragDestinationTextView.h"
5 #import "MyDocument.h"
6
7 @implementation DragDestinationTextView
8
9
10 // ==========================================================
11 // Overridden drag and drop methods
12 // ==========================================================
13
14 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
15
16         // We're only interested in files here, let the superclass handle everything else
17
18         if (![self dragIsFile:sender]) {
19                 return [super draggingEntered:sender];
20         }
21
22         // It is a file, highlight drop target
23
24         [self lockFocus];
25         [[NSColor selectedControlColor] set];
26         NSFrameRectWithWidth([self visibleRect], 2.5);
27         [self unlockFocus];
28         [[self window] flushWindow];
29         
30         return NSDragOperationGeneric;
31
32 }
33
34
35
36 - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender {
37
38         NSRect currentRect;
39
40         // We're only interested in files here, let the superclass handle everything else
41
42         if (![self dragIsFile:sender]) {
43                 return [super draggingUpdated:sender];
44         }
45
46         
47         /* some code to handle the case where a user drags the mouse around an edge of the
48          * text field, triggering an autoscroll. If we don't do anything, our highlight frame
49          * would get scrolled along with the content and then redrawn immediately which
50          * obviously looks messy.
51          * Instead we check if the visible rectangle has moved since the last
52          * invocation. If it did, we just clear the frame and do no drawing.
53          * If the frame didn't move, we will draw the frame.
54          */
55         currentRect = [self visibleRect];
56
57         if (!NSEqualRects(currentRect, previousRect)) {
58
59                 previousRect = currentRect;
60                 [self setNeedsDisplay:YES];
61
62         } else {
63
64                 [self lockFocus];
65                 [[NSColor selectedControlColor] set];
66                 NSFrameRectWithWidth(currentRect, 2);
67                 [self unlockFocus];
68                 [[self window] flushWindow];
69
70         }
71
72         return NSDragOperationGeneric;
73
74 }
75
76
77
78 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
79
80         if (![self dragIsFile:sender]) {
81                 return [super prepareForDragOperation:sender];
82         }
83
84         return YES;
85
86 }
87
88
89
90 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
91
92         NSString *filename;
93         BOOL result;
94         
95         if (![self dragIsFile:sender]) {
96                 return [super performDragOperation:sender];
97         }
98
99         filename = [self getFileForDrag:sender];
100         result = [[self delegate] handleDroppedFile:filename forTextView:self];
101
102         if (result == NO) {
103                 [self setNeedsDisplay:YES];
104         }
105
106         return result;
107 }
108
109
110
111 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender {
112
113         if (![self dragIsFile:sender]) {
114                 [super concludeDragOperation:sender];
115                 return;
116         }
117
118         [self setNeedsDisplay:YES];
119
120 }
121
122
123
124 - (void)draggingExited:(id <NSDraggingInfo>)sender {
125
126         if (![self dragIsFile:sender]) {
127                 [super draggingExited:sender];
128                 return;
129         }
130         
131         [self setNeedsDisplay:YES];
132
133 }
134
135
136
137
138
139
140
141 // ==========================================================
142 // Helper methods to find out if a given NSDraggingInfo refers to a file drag
143 // ==========================================================
144
145 - (BOOL)dragIsFile:(id <NSDraggingInfo>)sender {
146
147         BOOL isDirectory;
148         BOOL fileExists;
149         
150         NSString *dragFilename = [self getFileForDrag:sender];
151
152         if (dragFilename == nil) {
153                 return NO;
154         }
155         
156         fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dragFilename isDirectory:&isDirectory];
157
158         return (fileExists && (!isDirectory));
159 }
160
161
162
163 - (NSString *)getFileForDrag:(id <NSDraggingInfo>)sender {
164
165         NSPasteboard *pb = [sender draggingPasteboard];
166         NSString *availableType = [pb availableTypeFromArray:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
167         NSString *availableTypeString = [pb availableTypeFromArray:[NSArray arrayWithObjects:NSStringPboardType, nil]];
168         NSString *dragFilename;
169         NSArray *props;
170
171         if (availableType == nil) {
172                 return nil;
173         }
174
175         if (availableTypeString != nil) {
176                 return nil;
177         }
178
179         
180
181
182         
183         props = [pb propertyListForType:availableType];
184         dragFilename = [props objectAtIndex:0];
185
186         return dragFilename;
187
188 }
189
190
191
192
193
194 @end