1 |
/* DesktopViewIcon.m |
2 |
* |
3 |
* Copyright (C) 2003 Free Software Foundation, Inc. |
4 |
* |
5 |
* Author: Enrico Sersale <enrico@imago.ro> |
6 |
* Date: August 2001 |
7 |
* |
8 |
* This file is part of the GNUstep GWorkspace application |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
*/ |
24 |
|
25 |
|
26 |
#include <Foundation/Foundation.h> |
27 |
#include <AppKit/AppKit.h> |
28 |
#ifdef GNUSTEP |
29 |
#include "GWLib.h" |
30 |
#include "GWFunctions.h" |
31 |
#include "GWNotifications.h" |
32 |
#else |
33 |
#include <GWorkspace/GWLib.h> |
34 |
#include <GWorkspace/GWFunctions.h> |
35 |
#include <GWorkspace/GWNotifications.h> |
36 |
#endif |
37 |
#include "IconViewsIcon.h" |
38 |
#include "GWorkspace.h" |
39 |
#include "GNUstep.h" |
40 |
|
41 |
#define CHECK_LOCK if (locked) return |
42 |
#define CHECK_LOCK_RET(x) if (locked) return x |
43 |
|
44 |
@implementation DesktopViewIcon |
45 |
|
46 |
- (void)setPaths:(NSArray *)fpaths |
47 |
{ |
48 |
[super setPaths: fpaths]; |
49 |
[(NSView *)container resizeWithOldSuperviewSize: [(NSView *)container frame].size]; |
50 |
} |
51 |
|
52 |
- (void)select |
53 |
{ |
54 |
NSTimer *t; |
55 |
|
56 |
isSelect = YES; |
57 |
[self setNeedsDisplay: YES]; |
58 |
|
59 |
t = [NSTimer timerWithTimeInterval: 0.5 target: self |
60 |
selector: @selector(unselectFromTimer:) userInfo: nil repeats: NO]; |
61 |
[[NSRunLoop currentRunLoop] addTimer: t forMode: NSDefaultRunLoopMode]; |
62 |
} |
63 |
|
64 |
- (void)unselect |
65 |
{ |
66 |
isSelect = NO; |
67 |
[self setNeedsDisplay: YES]; |
68 |
} |
69 |
|
70 |
- (void)unselectFromTimer:(id)sender |
71 |
{ |
72 |
[self unselect]; |
73 |
} |
74 |
|
75 |
- (void)mouseUp:(NSEvent *)theEvent |
76 |
{ |
77 |
if ([theEvent clickCount] > 1) { |
78 |
int i; |
79 |
|
80 |
for (i = 0; i < [paths count]; i++) { |
81 |
NSString *path, *defApp, *tp; |
82 |
|
83 |
path = [paths objectAtIndex: i]; |
84 |
[ws getInfoForFile: path application: &defApp type: &tp]; |
85 |
|
86 |
if ((tp == NSApplicationFileType) && (locked == NO)) { |
87 |
unsigned int modifier = [theEvent modifierFlags]; |
88 |
|
89 |
[container openCurrentSelection: [NSArray arrayWithObject: path] |
90 |
newViewer: (modifier == NSControlKeyMask)]; |
91 |
} else { |
92 |
if (locked == NO) { |
93 |
[container openCurrentSelection: [NSArray arrayWithObject: path] |
94 |
newViewer: YES]; |
95 |
} else { |
96 |
if ((tp == NSDirectoryFileType) || (tp == NSFilesystemFileType)) { |
97 |
[container openCurrentSelection: [NSArray arrayWithObject: path] |
98 |
newViewer: YES]; |
99 |
} |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
[self unselect]; |
105 |
return; |
106 |
} |
107 |
} |
108 |
|
109 |
- (void)mouseDown:(NSEvent *)theEvent |
110 |
{ |
111 |
NSEvent *nextEvent; |
112 |
BOOL startdnd = NO; |
113 |
|
114 |
[container unselectOtherIcons: self]; |
115 |
|
116 |
if ([theEvent clickCount] == 1) { |
117 |
nextEvent = [[self window] nextEventMatchingMask: |
118 |
NSLeftMouseUpMask | NSLeftMouseDraggedMask]; |
119 |
if ([nextEvent type] == NSLeftMouseUp) { |
120 |
NSSize ss = [self frame].size; |
121 |
NSSize is = [icon size]; |
122 |
NSPoint p = NSMakePoint((ss.width - is.width) / 2, (ss.height - is.height) / 2); |
123 |
|
124 |
p = [self convertPoint: p toView: nil]; |
125 |
p = [[self window] convertBaseToScreen: p]; |
126 |
|
127 |
[self select]; |
128 |
|
129 |
[container setCurrentSelection: paths |
130 |
animateImage: icon |
131 |
startingAtPoint: p]; |
132 |
return; |
133 |
} |
134 |
|
135 |
[self unselect]; |
136 |
|
137 |
while (1) { |
138 |
nextEvent = [[self window] nextEventMatchingMask: |
139 |
NSLeftMouseUpMask | NSLeftMouseDraggedMask]; |
140 |
|
141 |
if ([nextEvent type] == NSLeftMouseUp) { |
142 |
return; |
143 |
} |
144 |
|
145 |
if ([nextEvent type] == NSLeftMouseDragged) { |
146 |
if(dragdelay < 5) { |
147 |
dragdelay++; |
148 |
} else { |
149 |
startdnd = YES; |
150 |
break; |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
if (startdnd == YES) { |
156 |
[self startExternalDragOnEvent: nextEvent]; |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
- (void)startExternalDragOnEvent:(NSEvent *)event |
162 |
{ |
163 |
NSEvent *nextEvent; |
164 |
NSPoint dragPoint; |
165 |
NSPasteboard *pb; |
166 |
|
167 |
nextEvent = [[self window] nextEventMatchingMask: |
168 |
NSLeftMouseUpMask | NSLeftMouseDraggedMask]; |
169 |
|
170 |
if([nextEvent type] != NSLeftMouseDragged) { |
171 |
[self unselect]; |
172 |
return; |
173 |
} |
174 |
|
175 |
dragPoint = [nextEvent locationInWindow]; |
176 |
dragPoint = [self convertPoint: dragPoint fromView: nil]; |
177 |
|
178 |
pb = [NSPasteboard pasteboardWithName: NSDragPboard]; |
179 |
[self declareAndSetShapeOnPasteboard: pb]; |
180 |
|
181 |
[self dragImage: icon |
182 |
at: dragPoint |
183 |
offset: NSZeroSize |
184 |
event: event |
185 |
pasteboard: pb |
186 |
source: self |
187 |
slideBack: NO]; |
188 |
} |
189 |
|
190 |
- (void)draggedImage:(NSImage *)anImage |
191 |
endedAt:(NSPoint)aPoint |
192 |
deposited:(BOOL)flag |
193 |
{ |
194 |
if (flag == NO) { |
195 |
aPoint = [self convertPoint: aPoint toView: self]; |
196 |
|
197 |
if (NSPointInRect(aPoint, [self frame]) |
198 |
|| NSPointInRect(aPoint, [namelabel frame])) { |
199 |
dragdelay = 0; |
200 |
onSelf = NO; |
201 |
[self unselect]; |
202 |
return; |
203 |
} |
204 |
|
205 |
[container removeIcon: self]; |
206 |
|
207 |
} else { |
208 |
dragdelay = 0; |
209 |
onSelf = NO; |
210 |
[self unselect]; |
211 |
} |
212 |
} |
213 |
|
214 |
- (void)declareAndSetShapeOnPasteboard:(NSPasteboard *)pb |
215 |
{ |
216 |
NSArray *dndtypes = [NSArray arrayWithObject: NSFilenamesPboardType]; |
217 |
[pb declareTypes: dndtypes owner: nil]; |
218 |
|
219 |
if ([pb setPropertyList: paths forType: NSFilenamesPboardType] == NO) { |
220 |
return; |
221 |
} |
222 |
} |
223 |
|
224 |
- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender |
225 |
{ |
226 |
NSPasteboard *pb; |
227 |
NSDragOperation sourceDragMask; |
228 |
NSArray *sourcePaths; |
229 |
NSString *fromPath; |
230 |
NSString *buff; |
231 |
NSString *iconPath; |
232 |
int i, count; |
233 |
|
234 |
CHECK_LOCK_RET (NSDragOperationNone); |
235 |
|
236 |
pb = [sender draggingPasteboard]; |
237 |
if([[pb types] indexOfObject: NSFilenamesPboardType] != NSNotFound) { |
238 |
sourcePaths = [pb propertyListForType: NSFilenamesPboardType]; |
239 |
|
240 |
if ([paths isEqualToArray: sourcePaths]) { |
241 |
onSelf = YES; |
242 |
isDragTarget = YES; |
243 |
[self setNeedsDisplay: YES]; |
244 |
return NSDragOperationAll; |
245 |
} |
246 |
|
247 |
if ((([type isEqualToString: NSDirectoryFileType] == NO) |
248 |
&& ([type isEqualToString: NSFilesystemFileType] == NO)) || isPakage) { |
249 |
return NSDragOperationNone; |
250 |
} |
251 |
|
252 |
count = [sourcePaths count]; |
253 |
fromPath = [[sourcePaths objectAtIndex: 0] stringByDeletingLastPathComponent]; |
254 |
|
255 |
if (count == 0) { |
256 |
return NSDragOperationNone; |
257 |
} |
258 |
|
259 |
if ([fm isWritableFileAtPath: fullPath] == NO) { |
260 |
return NSDragOperationNone; |
261 |
} |
262 |
|
263 |
if ([fullPath isEqualToString: fromPath]) { |
264 |
return NSDragOperationNone; |
265 |
} |
266 |
|
267 |
for (i = 0; i < count; i++) { |
268 |
if ([fullPath isEqualToString: [sourcePaths objectAtIndex: i]]) { |
269 |
return NSDragOperationNone; |
270 |
} |
271 |
} |
272 |
|
273 |
buff = [NSString stringWithString: fullPath]; |
274 |
while (1) { |
275 |
for (i = 0; i < count; i++) { |
276 |
if ([buff isEqualToString: [sourcePaths objectAtIndex: i]]) { |
277 |
return NSDragOperationNone; |
278 |
} |
279 |
} |
280 |
if ([buff isEqualToString: fixPath(@"/", 0)] == YES) { |
281 |
break; |
282 |
} |
283 |
buff = [buff stringByDeletingLastPathComponent]; |
284 |
} |
285 |
|
286 |
isDragTarget = YES; |
287 |
|
288 |
iconPath = [fullPath stringByAppendingPathComponent: @".opendir.tiff"]; |
289 |
|
290 |
if ([fm isReadableFileAtPath: iconPath]) { |
291 |
NSImage *img = [[NSImage alloc] initWithContentsOfFile: iconPath]; |
292 |
|
293 |
if (img) { |
294 |
ASSIGN (icon, img); |
295 |
RELEASE (img); |
296 |
} else { |
297 |
ASSIGN (icon, [NSImage imageNamed: @"FileIcon_Directory_Open.tiff"]); |
298 |
} |
299 |
} else { |
300 |
ASSIGN (icon, [NSImage imageNamed: @"FileIcon_Directory_Open.tiff"]); |
301 |
} |
302 |
|
303 |
[self setNeedsDisplay: YES]; |
304 |
|
305 |
sourceDragMask = [sender draggingSourceOperationMask]; |
306 |
|
307 |
if (sourceDragMask == NSDragOperationCopy) { |
308 |
return NSDragOperationCopy; |
309 |
} else if (sourceDragMask == NSDragOperationLink) { |
310 |
return NSDragOperationLink; |
311 |
} else { |
312 |
return NSDragOperationAll; |
313 |
} |
314 |
} |
315 |
|
316 |
return NSDragOperationNone; |
317 |
} |
318 |
|
319 |
- (unsigned int)draggingUpdated:(id <NSDraggingInfo>)sender |
320 |
{ |
321 |
NSDragOperation sourceDragMask; |
322 |
|
323 |
if (!isDragTarget || locked || isPakage) { |
324 |
return NSDragOperationNone; |
325 |
} |
326 |
|
327 |
sourceDragMask = [sender draggingSourceOperationMask]; |
328 |
|
329 |
if (sourceDragMask == NSDragOperationCopy) { |
330 |
return NSDragOperationCopy; |
331 |
} else if (sourceDragMask == NSDragOperationLink) { |
332 |
return NSDragOperationLink; |
333 |
} else { |
334 |
return NSDragOperationAll; |
335 |
} |
336 |
|
337 |
return NSDragOperationNone; |
338 |
} |
339 |
|
340 |
- (void)draggingExited:(id <NSDraggingInfo>)sender |
341 |
{ |
342 |
if(isDragTarget == YES) { |
343 |
isDragTarget = NO; |
344 |
if (onSelf == NO) { |
345 |
ASSIGN (icon, [GWLib iconForFile: fullPath ofType: type]); |
346 |
[self setNeedsDisplay: YES]; |
347 |
} |
348 |
onSelf = NO; |
349 |
} |
350 |
} |
351 |
|
352 |
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender |
353 |
{ |
354 |
return isDragTarget; |
355 |
} |
356 |
|
357 |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender |
358 |
{ |
359 |
return YES; |
360 |
} |
361 |
|
362 |
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender |
363 |
{ |
364 |
NSPasteboard *pb; |
365 |
NSDragOperation sourceDragMask; |
366 |
NSArray *sourcePaths; |
367 |
NSString *operation, *source; |
368 |
NSMutableArray *files; |
369 |
int i, tag; |
370 |
|
371 |
isDragTarget = NO; |
372 |
|
373 |
if (onSelf == YES) { |
374 |
NSView *view = (NSView *)container; |
375 |
[view resizeWithOldSuperviewSize: [view frame].size]; |
376 |
onSelf = NO; |
377 |
return; |
378 |
} |
379 |
|
380 |
ASSIGN (icon, [GWLib iconForFile: fullPath ofType: type]); |
381 |
[self setNeedsDisplay: YES]; |
382 |
|
383 |
sourceDragMask = [sender draggingSourceOperationMask]; |
384 |
pb = [sender draggingPasteboard]; |
385 |
sourcePaths = [pb propertyListForType: NSFilenamesPboardType]; |
386 |
source = [[sourcePaths objectAtIndex: 0] stringByDeletingLastPathComponent]; |
387 |
|
388 |
if ([source isEqualToString: [gw trashPath]]) { |
389 |
operation = GWorkspaceRecycleOutOperation; |
390 |
} else { |
391 |
if (sourceDragMask == NSDragOperationCopy) { |
392 |
operation = NSWorkspaceCopyOperation; |
393 |
} else if (sourceDragMask == NSDragOperationLink) { |
394 |
operation = NSWorkspaceLinkOperation; |
395 |
} else { |
396 |
operation = NSWorkspaceMoveOperation; |
397 |
} |
398 |
} |
399 |
|
400 |
files = [NSMutableArray arrayWithCapacity: 1]; |
401 |
for(i = 0; i < [sourcePaths count]; i++) { |
402 |
[files addObject: [[sourcePaths objectAtIndex: i] lastPathComponent]]; |
403 |
} |
404 |
|
405 |
[gw performFileOperation: operation source: source |
406 |
destination: fullPath files: files tag: &tag]; |
407 |
} |
408 |
|
409 |
@end |