/[gnustep]/gnustep/usr-apps/gworkspace/GWorkspace/Fiend/FiendLeaf.m
ViewVC logotype

Contents of /gnustep/usr-apps/gworkspace/GWorkspace/Fiend/FiendLeaf.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Mon Sep 1 10:49:04 2003 UTC (20 years, 7 months ago) by esersale
Branch: MAIN
Changes since 1.2: +3 -3 lines
*** empty log message ***

1 /* FiendLeaf.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 #include <Foundation/Foundation.h>
26 #include <AppKit/AppKit.h>
27 #ifdef GNUSTEP
28 #include "GWFunctions.h"
29 #include "GWNotifications.h"
30 #include "GWLib.h"
31 #else
32 #include <GWorkspace/GWFunctions.h>
33 #include <GWorkspace/GWNotifications.h>
34 #include <GWorkspace/GWLib.h>
35 #endif
36 #include "FiendLeaf.h"
37 #include "Fiend.h"
38 #include "GWorkspace.h"
39 #include "GNUstep.h"
40 #include <math.h>
41
42 #define INTERVALS 40.0
43
44 @implementation LeafPosition
45
46 - (id)initWithPosX:(int)px posY:(int)py relativeToPoint:(NSPoint)p
47 {
48 self = [super init];
49 posx = px;
50 posy = py;
51 r = NSMakeRect((int)p.x + (64 * posx), (int)p.y + (64 * posy), 64, 64);
52 return self;
53 }
54
55 - (NSRect)lfrect
56 {
57 return r;
58 }
59
60 - (int)posx
61 {
62 return posx;
63 }
64
65 - (int)posy
66 {
67 return posy;
68 }
69
70 - (BOOL)containsPoint:(NSPoint)p
71 {
72 return NSPointInRect(p, r);
73 }
74
75 @end
76
77
78 @implementation FiendLeaf
79
80 - (void)dealloc
81 {
82 TEST_RELEASE (myPath);
83 TEST_RELEASE (myType);
84 RELEASE (tile);
85 TEST_RELEASE (hightile);
86 TEST_RELEASE (icon);
87 TEST_RELEASE (namelabel);
88 [super dealloc];
89 }
90
91 - (id)initWithPosX:(int)px
92 posY:(int)py
93 relativeToPoint:(NSPoint)p
94 forPath:(NSString *)apath
95 inFiend:(Fiend *)afiend
96 ghostImage:(NSImage *)ghostimage
97 {
98 NSWindow *win;
99
100 self = [super init];
101
102 win = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, 64, 64)
103 styleMask: NSBorderlessWindowMask
104 backing: NSBackingStoreRetained defer: NO];
105 [self setFrame: [[win contentView] frame]];
106 [win setContentView: self];
107 [self setPosX: px posY: py relativeToPoint: p];
108
109 if (apath != nil) {
110 NSString *defApp, *type;
111
112 fm = [NSFileManager defaultManager];
113 ws = [NSWorkspace sharedWorkspace];
114 gw = [GWorkspace gworkspace];
115
116 ASSIGN (myPath, apath);
117
118 [ws getInfoForFile: myPath application: &defApp type: &type];
119 ASSIGN (myType, type);
120 isPakage = [gw isPakageAtPath: myPath];
121
122 ASSIGN (icon, [gw iconForFile: myPath ofType: myType]);
123
124 if ([myType isEqualToString: NSApplicationFileType] == NO) {
125 NSString *name;
126
127 if ([myPath isEqualToString: fixPath(@"/", 0)]) {
128 NSHost *host = [NSHost currentHost];
129 NSString *hname = [host name];
130 NSRange range = [hname rangeOfString: @"."];
131 if (range.length != 0) {
132 hname = [hname substringToIndex: range.location];
133 }
134 name = hname;
135 } else {
136 name = [myPath lastPathComponent];
137 }
138
139 namelabel = [NSTextFieldCell new];
140 [namelabel setFont: [NSFont systemFontOfSize: 10]];
141 [namelabel setBordered: NO];
142 [namelabel setAlignment: NSCenterTextAlignment];
143 [namelabel setStringValue: cutFileLabelText(name, namelabel, 50)];
144 } else {
145 namelabel = nil;
146 }
147
148 [self registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
149
150 } else {
151 icon = nil;
152 }
153
154 ASSIGN (tile, [NSImage imageNamed: @"common_Tile.tiff"]);
155
156 if (ghostimage == nil) {
157 tile = [NSImage imageNamed: @"common_Tile.tiff"];
158 fiend = afiend;
159 isGhost = NO;
160 } else {
161 ASSIGN (icon, ghostimage);
162 ASSIGN (hightile, [NSImage imageNamed: @"TileHighlight.tiff"]);
163 isGhost = YES;
164 }
165
166 isDragTarget = NO;
167 dissolving = NO;
168
169 return self;
170 }
171
172 - (void)setPosX:(int)px posY:(int)py relativeToPoint:(NSPoint)p
173 {
174 posx = px;
175 posy = py;
176 [[self window] setFrameOrigin: NSMakePoint(p.x + (64 * posx), p.y + (64 * posy))];
177 }
178
179 - (int)posx
180 {
181 return posx;
182 }
183
184 - (int)posy
185 {
186 return posy;
187 }
188
189 - (NSPoint)iconPosition
190 {
191 NSWindow *win = [self window];
192 NSPoint p = [win frame].origin;
193 NSSize s = [icon size];
194 NSSize shift = NSMakeSize((64 - s.width) / 2, (64 - s.height) / 2);
195
196 return NSMakePoint(p.x + shift.width, p.y + shift.height);
197 }
198
199 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
200 {
201 return YES;
202 }
203
204 - (NSString *)myPath
205 {
206 return myPath;
207 }
208
209 - (NSString *)myType
210 {
211 return myType;
212 }
213
214 - (NSImage *)icon
215 {
216 return icon;
217 }
218
219 - (void)startDissolve
220 {
221 dissolving = YES;
222 dissCounter = 0;
223 dissFraction = 0.2;
224 dissTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self
225 selector: @selector(display) userInfo: nil repeats: YES];
226 RETAIN (dissTimer);
227 }
228
229 - (BOOL)dissolveAndReturnWhenDone
230 {
231 dissolving = YES;
232 dissCounter = 0;
233 dissFraction = 0.2;
234
235 while (1) {
236 NSDate *date = [NSDate dateWithTimeIntervalSinceNow: 0.02];
237 [[NSRunLoop currentRunLoop] runUntilDate: date];
238 [self display];
239
240 if (dissolving == NO) {
241 break;
242 }
243 }
244
245 return YES;
246 }
247
248 - (void)mouseDown:(NSEvent*)theEvent
249 {
250 NSEvent *nextEvent;
251 NSPoint location, lastLocation, origin;
252 NSWindow *win;
253
254 [fiend orderFrontLeaves];
255
256 if ([theEvent clickCount] > 1) {
257 if ([myType isEqualToString: NSApplicationFileType]) {
258 [ws launchApplication: myPath];
259 [self startDissolve];
260 } else if ([myType isEqualToString: NSPlainFileType]
261 || [myType isEqualToString: NSDirectoryFileType]
262 || [myType isEqualToString: NSFilesystemFileType]) {
263 NSArray *paths = [NSArray arrayWithObjects: myPath, nil];
264 [gw openSelectedPaths: paths newViewer: YES];
265 }
266 return;
267 }
268
269 win = [self window];
270 [win orderFront: self];
271
272 nextEvent = [win nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask];
273 if ([nextEvent type] == NSLeftMouseUp) {
274 [win orderBack: self];
275 return;
276 }
277
278 lastLocation = [theEvent locationInWindow];
279
280 while (1) {
281 nextEvent = [win nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask];
282
283 if ([nextEvent type] == NSLeftMouseUp) {
284 break;
285 }
286
287 location = [win mouseLocationOutsideOfEventStream];
288 origin = [win frame].origin;
289 origin.x += (location.x - lastLocation.x);
290 origin.y += (location.y - lastLocation.y);
291 [win setFrameOrigin: origin];
292
293 [fiend draggedFiendLeaf: self atPoint: origin mouseUp: NO];
294 }
295
296 [win orderBack: self];
297 [fiend draggedFiendLeaf: self atPoint: [win frame].origin mouseUp: YES];
298 }
299
300 - (void)drawRect:(NSRect)rect
301 {
302 NSSize iconSize;
303 NSPoint iconPosn;
304 NSRect textFrame;
305
306 [self lockFocus];
307
308 if ((isGhost == NO) && (dissolving == NO)) {
309 [tile compositeToPoint: NSZeroPoint operation: NSCompositeSourceOver];
310 } else if (dissolving == NO) {
311 [hightile compositeToPoint: NSZeroPoint operation: NSCompositeSourceOver];
312 }
313
314 if (icon != nil) {
315 iconSize = [icon size];
316
317 if (isGhost || ([myType isEqualToString: NSApplicationFileType] == YES)) {
318 iconPosn = NSMakePoint((64 - iconSize.width) / 2.0, (64 - iconSize.height) / 2.0);
319 } else {
320 iconPosn = NSMakePoint((64 - iconSize.width) / 2.0, 13);
321 }
322
323 if (dissolving) {
324 if (dissCounter++ >= 5) {
325 dissFraction += 0.1;
326 }
327
328 [[NSColor whiteColor] set];
329 NSRectFill(rect);
330 [tile dissolveToPoint: NSZeroPoint fraction: fabs(dissFraction)];
331 [icon dissolveToPoint: iconPosn fraction: fabs(dissFraction)];
332
333 if (dissFraction >= 1) {
334 if (dissTimer && [dissTimer isValid]) {
335 [dissTimer invalidate];
336 DESTROY (dissTimer);
337 }
338 dissolving = NO;
339 }
340
341 [self unlockFocus];
342 return;
343 }
344
345 if (isGhost == NO) {
346 [icon compositeToPoint: iconPosn operation: NSCompositeSourceOver];
347 } else {
348 [icon dissolveToPoint: iconPosn fraction: 0.2];
349 }
350
351 if (namelabel != nil) {
352 textFrame = NSMakeRect(4, 3, 56, 9);
353 [namelabel setDrawsBackground: NO];
354 [namelabel drawWithFrame: textFrame inView: self];
355 }
356 }
357
358 [self unlockFocus];
359 }
360
361 @end
362
363 @implementation FiendLeaf (DraggingDestination)
364
365 - (BOOL)isDragTarget
366 {
367 return isDragTarget;
368 }
369
370 - (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender
371 {
372 NSPasteboard *pb;
373 NSDragOperation sourceDragMask;
374 NSArray *sourcePaths;
375 NSString *fromPath;
376 NSString *buff;
377 int i, count;
378
379 [fiend verifyDraggingExited: self];
380
381 if ((([myType isEqual: NSDirectoryFileType] == NO)
382 && ([myType isEqual: NSFilesystemFileType] == NO)
383 && ([myType isEqual: NSApplicationFileType] == NO))
384 || (isPakage && ([myType isEqual: NSApplicationFileType] == NO))) {
385 return NSDragOperationNone;
386 }
387
388 pb = [sender draggingPasteboard];
389 if([[pb types] indexOfObject: NSFilenamesPboardType] != NSNotFound) {
390 sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
391 count = [sourcePaths count];
392 fromPath = [[sourcePaths objectAtIndex: 0] stringByDeletingLastPathComponent];
393
394 if (count == 0) {
395 return NSDragOperationNone;
396 }
397
398 if ([myType isEqualToString: NSApplicationFileType] == NO) {
399
400 if ([fm isWritableFileAtPath: myPath] == NO) {
401 return NSDragOperationNone;
402 }
403
404 if ([myPath isEqualToString: fromPath]) {
405 return NSDragOperationNone;
406 }
407
408 for (i = 0; i < count; i++) {
409 if ([myPath isEqualToString: [sourcePaths objectAtIndex: i]]) {
410 return NSDragOperationNone;
411 }
412 }
413
414 buff = [NSString stringWithString: myPath];
415 while (1) {
416 for (i = 0; i < count; i++) {
417 if ([buff isEqualToString: [sourcePaths objectAtIndex: i]]) {
418 return NSDragOperationNone;
419 }
420 }
421 if ([buff isEqualToString: fixPath(@"/", 0)] == YES) {
422 break;
423 }
424 buff = [buff stringByDeletingLastPathComponent];
425 }
426
427 isDragTarget = YES;
428
429 ASSIGN (icon, [NSImage imageNamed: @"FileIcon_Directory_Open.tiff"]);
430 [self setNeedsDisplay: YES];
431
432 sourceDragMask = [sender draggingSourceOperationMask];
433
434 if (sourceDragMask == NSDragOperationCopy) {
435 return NSDragOperationCopy;
436 } else if (sourceDragMask == NSDragOperationLink) {
437 return NSDragOperationLink;
438 } else {
439 return NSDragOperationAll;
440 }
441
442 } else {
443 if ((sourceDragMask == NSDragOperationCopy)
444 || (sourceDragMask == NSDragOperationLink)) {
445 return NSDragOperationNone;
446 }
447
448 for (i = 0; i < [sourcePaths count]; i++) {
449 NSString *fpath, *app, *type;
450
451 fpath = [sourcePaths objectAtIndex: i];
452 [ws getInfoForFile: fpath application: &app type: &type];
453
454 if (type != NSPlainFileType) {
455 return NSDragOperationNone;
456 }
457 }
458
459 isDragTarget = YES;
460 return NSDragOperationAll;
461 }
462 }
463
464 return NSDragOperationNone;
465 }
466
467 - (unsigned int)draggingUpdated:(id <NSDraggingInfo>)sender
468 {
469 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
470
471 if (isDragTarget == NO) {
472 return NSDragOperationNone;
473 }
474
475 if ((([myType isEqual: NSDirectoryFileType] == NO)
476 && ([myType isEqual: NSFilesystemFileType] == NO)
477 && ([myType isEqual: NSApplicationFileType] == NO))
478 || (isPakage && ([myType isEqual: NSApplicationFileType] == NO))) {
479 return NSDragOperationNone;
480 }
481
482 if ([myType isEqualToString: NSApplicationFileType] == NO) {
483 if (sourceDragMask == NSDragOperationCopy) {
484 return NSDragOperationCopy;
485 } else if (sourceDragMask == NSDragOperationLink) {
486 return NSDragOperationLink;
487 } else {
488 return NSDragOperationAll;
489 }
490 } else {
491 if ((sourceDragMask != NSDragOperationCopy)
492 && (sourceDragMask != NSDragOperationLink)) {
493 return NSDragOperationAll;
494 }
495 }
496
497 return NSDragOperationNone;
498 }
499
500 - (void)draggingExited:(id <NSDraggingInfo>)sender
501 {
502 if(isDragTarget == YES) {
503 isDragTarget = NO;
504 if ([myType isEqualToString: NSApplicationFileType] == NO) {
505 ASSIGN (icon, [gw iconForFile: myPath ofType: myType]);
506 [self setNeedsDisplay: YES];
507 }
508 }
509 }
510
511 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
512 {
513 return isDragTarget;
514 }
515
516 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
517 {
518 return YES;
519 }
520
521 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
522 {
523 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
524 NSPasteboard *pb = [sender draggingPasteboard];
525 NSArray *sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
526 int i = 0;
527
528 if ([myType isEqualToString: NSApplicationFileType] == NO) {
529 NSString *operation, *source;
530 NSMutableArray *files;
531 int tag;
532
533 ASSIGN (icon, [gw iconForFile: myPath ofType: myType]);
534 [self setNeedsDisplay: YES];
535
536 source = [[sourcePaths objectAtIndex: 0] stringByDeletingLastPathComponent];
537
538 if ([source isEqualToString: [gw trashPath]]) {
539 operation = GWorkspaceRecycleOutOperation;
540 } else {
541 if (sourceDragMask == NSDragOperationCopy) {
542 operation = NSWorkspaceCopyOperation;
543 } else if (sourceDragMask == NSDragOperationLink) {
544 operation = NSWorkspaceLinkOperation;
545 } else {
546 operation = NSWorkspaceMoveOperation;
547 }
548 }
549
550 files = [NSMutableArray arrayWithCapacity: 1];
551 for(i = 0; i < [sourcePaths count]; i++) {
552 [files addObject: [[sourcePaths objectAtIndex: i] lastPathComponent]];
553 }
554
555 [gw performFileOperation: operation source: source
556 destination: myPath files: files tag: &tag];
557
558 } else {
559 for(i = 0; i < [sourcePaths count]; i++) {
560 NSString *path, *defApp, *type;
561
562 path = [sourcePaths objectAtIndex: i];
563 [ws getInfoForFile: path application: &defApp type: &type];
564 if ([type isEqualToString: NSPlainFileType] == YES) {
565 [ws openFile: path withApplication: myPath];
566 }
567 }
568 }
569
570 isDragTarget = NO;
571 }
572
573 @end

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26