1 |
/* Shelf.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 "GWFunctions.h" |
30 |
#include "GWNotifications.h" |
31 |
#else |
32 |
#include <GWorkspace/GWFunctions.h> |
33 |
#include <GWorkspace/GWNotifications.h> |
34 |
#endif |
35 |
#include "Shelf.h" |
36 |
#include "IconViewsIcon.h" |
37 |
#include "GWorkspace.h" |
38 |
#include "GNUstep.h" |
39 |
|
40 |
@interface IconViewsIcon (ShelfSorting) |
41 |
- (NSComparisonResult)iconCompare:(IconViewsIcon *)other; |
42 |
@end |
43 |
|
44 |
@implementation IconViewsIcon (ShelfSorting) |
45 |
|
46 |
- (NSComparisonResult)iconCompare:(IconViewsIcon *)other |
47 |
{ |
48 |
if ([other gridindex] > [self gridindex]) { |
49 |
return NSOrderedAscending; |
50 |
} else { |
51 |
return NSOrderedDescending; |
52 |
} |
53 |
|
54 |
return NSOrderedSame; |
55 |
} |
56 |
|
57 |
@end |
58 |
|
59 |
@implementation Shelf |
60 |
|
61 |
- (void) dealloc |
62 |
{ |
63 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
64 |
[self unsetWatchers]; |
65 |
if (gpoints != NULL) { |
66 |
NSZoneFree (NSDefaultMallocZone(), gpoints); |
67 |
} |
68 |
RELEASE (icons); |
69 |
TEST_RELEASE (viewerPath); |
70 |
RELEASE (watchedPaths); |
71 |
TEST_RELEASE (dragImage); |
72 |
[super dealloc]; |
73 |
} |
74 |
|
75 |
- (id)initWithIconsDicts:(NSArray *)iconsDicts rootPath:(NSString *)rpath |
76 |
{ |
77 |
self = [super init]; |
78 |
if (self) { |
79 |
int i, j; |
80 |
|
81 |
fm = [NSFileManager defaultManager]; |
82 |
gw = [GWorkspace gworkspace]; |
83 |
|
84 |
makePosSel = @selector(makePositions); |
85 |
makePos = (IMP)[self methodForSelector: makePosSel]; |
86 |
|
87 |
gridPointSel = @selector(gridPointNearestToPoint:); |
88 |
gridPoint = (GridPointIMP)[self methodForSelector: gridPointSel]; |
89 |
|
90 |
cellsWidth = [gw shelfCellsWidth]; |
91 |
|
92 |
if (rpath != nil) { |
93 |
ASSIGN (viewerPath, rpath); |
94 |
} else { |
95 |
viewerPath = nil; |
96 |
} |
97 |
|
98 |
watchedPaths = [[NSMutableArray alloc] initWithCapacity: 1]; |
99 |
|
100 |
icons = [[NSMutableArray alloc] initWithCapacity: 1]; |
101 |
|
102 |
for (i = 0; i < [iconsDicts count]; i++) { |
103 |
NSDictionary *iconDict = [iconsDicts objectAtIndex: i]; |
104 |
NSArray *iconpaths = [iconDict objectForKey: @"paths"]; |
105 |
int index = [[iconDict objectForKey: @"index"] intValue]; |
106 |
BOOL canadd = YES; |
107 |
|
108 |
for (j = 0; j < [iconpaths count]; j++) { |
109 |
NSString *p = [iconpaths objectAtIndex: j]; |
110 |
if ([fm fileExistsAtPath: p] == YES) { |
111 |
|
112 |
if (viewerPath != nil) { |
113 |
if ((subPathOfPath(viewerPath, p) == NO) |
114 |
&& ([viewerPath isEqualToString: p] == NO)) { |
115 |
canadd = NO; |
116 |
break; |
117 |
} |
118 |
} |
119 |
|
120 |
} else { |
121 |
canadd = NO; |
122 |
break; |
123 |
} |
124 |
} |
125 |
|
126 |
if ((canadd == YES) && (index != -1)) { |
127 |
[self addIconWithPaths: iconpaths withGridIndex: index]; |
128 |
} |
129 |
} |
130 |
|
131 |
gpoints = NULL; |
132 |
pcount = 0; |
133 |
isDragTarget = NO; |
134 |
dragImage = nil; |
135 |
isShiftClick = NO; |
136 |
|
137 |
[self registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]]; |
138 |
|
139 |
[[NSNotificationCenter defaultCenter] addObserver: self |
140 |
selector: @selector(cellsWidthChanged:) |
141 |
name: GWShelfCellsWidthChangedNotification |
142 |
object: nil]; |
143 |
|
144 |
[[NSNotificationCenter defaultCenter] addObserver: self |
145 |
selector: @selector(fileSystemWillChange:) |
146 |
name: GWFileSystemWillChangeNotification |
147 |
object: nil]; |
148 |
|
149 |
[[NSNotificationCenter defaultCenter] addObserver: self |
150 |
selector: @selector(fileSystemDidChange:) |
151 |
name: GWFileSystemDidChangeNotification |
152 |
object: nil]; |
153 |
|
154 |
[[NSNotificationCenter defaultCenter] addObserver: self |
155 |
selector: @selector(watcherNotification:) |
156 |
name: GWFileWatcherFileDidChangeNotification |
157 |
object: nil]; |
158 |
} |
159 |
return self; |
160 |
} |
161 |
|
162 |
- (NSArray *)iconsDicts |
163 |
{ |
164 |
NSMutableArray *iconsdicts = [NSMutableArray arrayWithCapacity: 1]; |
165 |
int i; |
166 |
|
167 |
for (i = 0; i < [icons count]; i++) { |
168 |
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
169 |
IconViewsIcon *icon = [icons objectAtIndex: i]; |
170 |
int index; |
171 |
|
172 |
[dict setObject: [icon paths] forKey: @"paths"]; |
173 |
index = [icon gridindex]; |
174 |
[dict setObject: [NSString stringWithFormat: @"%i", index] forKey: @"index"]; |
175 |
|
176 |
[iconsdicts addObject: dict]; |
177 |
} |
178 |
|
179 |
return iconsdicts; |
180 |
} |
181 |
|
182 |
- (void)addIconWithPaths:(NSArray *)iconpaths |
183 |
withGridIndex:(int)index |
184 |
{ |
185 |
ShelfIcon *icon = [[ShelfIcon alloc] initForPaths: iconpaths |
186 |
gridIndex: index inContainer: self]; |
187 |
NSString *watched = [[iconpaths objectAtIndex: 0] stringByDeletingLastPathComponent]; |
188 |
|
189 |
if (gpoints != NULL) { |
190 |
if (index < pcount) { |
191 |
gpoints[index].used = 1; |
192 |
} |
193 |
} |
194 |
|
195 |
[icons addObject: icon]; |
196 |
[self addSubview: icon]; |
197 |
[self addSubview: [icon myLabel]]; |
198 |
RELEASE (icon); |
199 |
[self sortIcons]; |
200 |
[self resizeWithOldSuperviewSize: [self frame].size]; |
201 |
|
202 |
if ([watchedPaths containsObject: watched] == NO) { |
203 |
[watchedPaths addObject: watched]; |
204 |
[self setWatcherForPath: watched]; |
205 |
} |
206 |
} |
207 |
|
208 |
- (void)sortIcons |
209 |
{ |
210 |
NSArray *sortedIcons = [icons sortedArrayUsingSelector: @selector(iconCompare:)]; |
211 |
[icons removeAllObjects]; |
212 |
[icons addObjectsFromArray: sortedIcons]; |
213 |
} |
214 |
|
215 |
- (NSArray *)icons |
216 |
{ |
217 |
return icons; |
218 |
} |
219 |
|
220 |
- (void)updateIcons |
221 |
{ |
222 |
int i; |
223 |
|
224 |
for (i = 0; i < [icons count]; i++) { |
225 |
[[icons objectAtIndex: i] renewIcon]; |
226 |
} |
227 |
} |
228 |
|
229 |
- (void)cellsWidthChanged:(NSNotification *)notification |
230 |
{ |
231 |
int i; |
232 |
|
233 |
cellsWidth = [gw shelfCellsWidth]; |
234 |
for (i = 0; i < [icons count]; i++) { |
235 |
[[icons objectAtIndex: i] setLabelWidth]; |
236 |
} |
237 |
[self resizeWithOldSuperviewSize: [self frame].size]; |
238 |
} |
239 |
|
240 |
- (void)fileSystemWillChange:(NSNotification *)notification |
241 |
{ |
242 |
NSDictionary *dict = (NSDictionary *)[notification object]; |
243 |
NSString *operation = [dict objectForKey: @"operation"]; |
244 |
NSString *source = [dict objectForKey: @"source"]; |
245 |
NSArray *files = [dict objectForKey: @"files"]; |
246 |
|
247 |
if (operation == NSWorkspaceMoveOperation |
248 |
|| operation == NSWorkspaceDestroyOperation |
249 |
|| operation == GWorkspaceRenameOperation |
250 |
|| operation == NSWorkspaceRecycleOperation |
251 |
|| operation == GWorkspaceRecycleOutOperation |
252 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
253 |
|
254 |
NSMutableArray *paths = [NSMutableArray arrayWithCapacity: 1]; |
255 |
NSArray *iconpaths; |
256 |
int i, j, m; |
257 |
|
258 |
for (i = 0; i < [files count]; i++) { |
259 |
NSString *s = [source stringByAppendingPathComponent: [files objectAtIndex: i]]; |
260 |
[paths addObject: s]; |
261 |
} |
262 |
|
263 |
for (i = 0; i < [icons count]; i++) { |
264 |
IconViewsIcon *icon = [icons objectAtIndex: i]; |
265 |
|
266 |
iconpaths = [icon paths]; |
267 |
|
268 |
for (j = 0; j < [iconpaths count]; j++) { |
269 |
NSString *op = [iconpaths objectAtIndex: j]; |
270 |
|
271 |
for (m = 0; m < [paths count]; m++) { |
272 |
NSString *fp = [paths objectAtIndex: m]; |
273 |
|
274 |
if ([op hasPrefix: fp]) { |
275 |
[icon setLocked: YES]; |
276 |
break; |
277 |
} |
278 |
} |
279 |
} |
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
- (void)fileSystemDidChange:(NSNotification *)notification |
285 |
{ |
286 |
NSDictionary *dict; |
287 |
NSString *operation, *source, *destination; |
288 |
NSArray *files; |
289 |
NSMutableArray *paths; |
290 |
IconViewsIcon *icon; |
291 |
NSArray *iconpaths; |
292 |
int count; |
293 |
int i, j, m; |
294 |
|
295 |
dict = (NSDictionary *)[notification object]; |
296 |
operation = [dict objectForKey: @"operation"]; |
297 |
source = [dict objectForKey: @"source"]; |
298 |
destination = [dict objectForKey: @"destination"]; |
299 |
files = [dict objectForKey: @"files"]; |
300 |
|
301 |
if (operation == GWorkspaceRenameOperation) { |
302 |
for (i = 0; i < [icons count]; i++) { |
303 |
icon = [icons objectAtIndex: i]; |
304 |
if ([icon isSinglePath] == YES) { |
305 |
if ([[[icon paths] objectAtIndex: 0] isEqualToString: source]) { |
306 |
[icon setPaths: [NSArray arrayWithObject: destination]]; |
307 |
[icon setNeedsDisplay: YES]; |
308 |
[self resizeWithOldSuperviewSize: [self frame].size]; |
309 |
break; |
310 |
} |
311 |
} |
312 |
} |
313 |
} |
314 |
|
315 |
if (operation == GWorkspaceRenameOperation) { |
316 |
files = [NSArray arrayWithObject: [source lastPathComponent]]; |
317 |
source = [source stringByDeletingLastPathComponent]; |
318 |
} |
319 |
|
320 |
if (operation == NSWorkspaceMoveOperation |
321 |
|| operation == NSWorkspaceDestroyOperation |
322 |
|| operation == GWorkspaceRenameOperation |
323 |
|| operation == NSWorkspaceRecycleOperation |
324 |
|| operation == GWorkspaceRecycleOutOperation |
325 |
|| operation == GWorkspaceEmptyRecyclerOperation) { |
326 |
|
327 |
paths = [NSMutableArray arrayWithCapacity: 1]; |
328 |
for (i = 0; i < [files count]; i++) { |
329 |
NSString *s = [source stringByAppendingPathComponent: [files objectAtIndex: i]]; |
330 |
[paths addObject: s]; |
331 |
} |
332 |
|
333 |
count = [icons count]; |
334 |
for (i = 0; i < count; i++) { |
335 |
BOOL deleted = NO; |
336 |
icon = [icons objectAtIndex: i]; |
337 |
iconpaths = [icon paths]; |
338 |
|
339 |
for (j = 0; j < [iconpaths count]; j++) { |
340 |
NSString *op = [iconpaths objectAtIndex: j]; |
341 |
|
342 |
for (m = 0; m < [paths count]; m++) { |
343 |
NSString *fp = [paths objectAtIndex: m]; |
344 |
|
345 |
if ([op hasPrefix: fp]) { |
346 |
[self removeIcon: icon]; |
347 |
count--; |
348 |
i--; |
349 |
deleted = YES; |
350 |
break; |
351 |
} |
352 |
|
353 |
if (deleted) { |
354 |
break; |
355 |
} |
356 |
|
357 |
} |
358 |
|
359 |
if (deleted) { |
360 |
break; |
361 |
} |
362 |
} |
363 |
} |
364 |
} |
365 |
} |
366 |
|
367 |
- (void)watcherNotification:(NSNotification *)notification |
368 |
{ |
369 |
NSDictionary *notifdict = (NSDictionary *)[notification object]; |
370 |
NSString *path = [notifdict objectForKey: @"path"]; |
371 |
NSString *event = [notifdict objectForKey: @"event"]; |
372 |
BOOL contained = NO; |
373 |
int i; |
374 |
|
375 |
if (event == GWFileCreatedInWatchedDirectory) { |
376 |
return; |
377 |
} |
378 |
|
379 |
for (i = 0; i < [watchedPaths count]; i++) { |
380 |
NSString *wpath = [watchedPaths objectAtIndex: i]; |
381 |
if (([wpath isEqualToString: path]) || (subPathOfPath(path, wpath))) { |
382 |
contained = YES; |
383 |
break; |
384 |
} |
385 |
} |
386 |
|
387 |
if (contained) { |
388 |
id icon; |
389 |
NSArray *ipaths; |
390 |
NSString *ipath; |
391 |
int count = [icons count]; |
392 |
|
393 |
if (event == GWWatchedDirectoryDeleted) { |
394 |
for (i = 0; i < count; i++) { |
395 |
icon = [icons objectAtIndex: i]; |
396 |
ipaths = [icon paths]; |
397 |
ipath = [ipaths objectAtIndex: 0]; |
398 |
|
399 |
if (subPathOfPath(path, ipath)) { |
400 |
[self removeIcon: icon]; |
401 |
count--; |
402 |
i--; |
403 |
} |
404 |
} |
405 |
return; |
406 |
} |
407 |
|
408 |
if (event == GWFileDeletedInWatchedDirectory) { |
409 |
NSArray *files = [notifdict objectForKey: @"files"]; |
410 |
|
411 |
for (i = 0; i < count; i++) { |
412 |
int j; |
413 |
|
414 |
icon = [icons objectAtIndex: i]; |
415 |
ipaths = [icon paths]; |
416 |
|
417 |
if ([ipaths count] == 1) { |
418 |
ipath = [ipaths objectAtIndex: 0]; |
419 |
|
420 |
for (j = 0; j < [files count]; j++) { |
421 |
NSString *fname = [files objectAtIndex: j]; |
422 |
NSString *fullPath = [path stringByAppendingPathComponent: fname]; |
423 |
|
424 |
if ((subPathOfPath(fullPath, ipath)) |
425 |
|| ([ipath isEqualToString: fullPath])) { |
426 |
[self removeIcon: icon]; |
427 |
count--; |
428 |
i--; |
429 |
break; |
430 |
} |
431 |
} |
432 |
|
433 |
} else { |
434 |
|
435 |
for (j = 0; j < [files count]; j++) { |
436 |
NSString *fname = [files objectAtIndex: j]; |
437 |
NSString *fullPath = [path stringByAppendingPathComponent: fname]; |
438 |
BOOL deleted = NO; |
439 |
int m; |
440 |
|
441 |
if (deleted) { |
442 |
break; |
443 |
} |
444 |
|
445 |
ipath = [ipaths objectAtIndex: 0]; |
446 |
if (subPathOfPath(fullPath, ipath)) { |
447 |
[self removeIcon: icon]; |
448 |
count--; |
449 |
i--; |
450 |
break; |
451 |
} |
452 |
|
453 |
for (m = 0; m < [ipaths count]; m++) { |
454 |
ipath = [ipaths objectAtIndex: m]; |
455 |
|
456 |
if ([ipath isEqualToString: fullPath]) { |
457 |
NSMutableArray *newpaths; |
458 |
|
459 |
if ([ipaths count] == 1) { |
460 |
[self removeIcon: icon]; |
461 |
count--; |
462 |
i--; |
463 |
deleted = YES; |
464 |
break; |
465 |
} |
466 |
|
467 |
newpaths = [ipaths mutableCopy]; |
468 |
[newpaths removeObject: ipath]; |
469 |
[icon setPaths: newpaths]; |
470 |
ipaths = [icon paths]; |
471 |
RELEASE (newpaths); |
472 |
} |
473 |
} |
474 |
|
475 |
} |
476 |
} |
477 |
} |
478 |
} |
479 |
} |
480 |
} |
481 |
|
482 |
- (void)setWatchers |
483 |
{ |
484 |
int i; |
485 |
|
486 |
for (i = 0; i < [watchedPaths count]; i++) { |
487 |
[self setWatcherForPath: [watchedPaths objectAtIndex: i]]; |
488 |
} |
489 |
} |
490 |
|
491 |
- (void)setWatcherForPath:(NSString *)path |
492 |
{ |
493 |
[gw addWatcherForPath: path]; |
494 |
} |
495 |
|
496 |
- (void)unsetWatchers |
497 |
{ |
498 |
int i; |
499 |
|
500 |
for (i = 0; i < [watchedPaths count]; i++) { |
501 |
[self unsetWatcherForPath: [watchedPaths objectAtIndex: i]]; |
502 |
} |
503 |
} |
504 |
|
505 |
- (void)unsetWatcherForPath:(NSString *)path |
506 |
{ |
507 |
[gw removeWatcherForPath: path]; |
508 |
} |
509 |
|
510 |
- (void)makePositions |
511 |
{ |
512 |
float wdt, hgt, x, y; |
513 |
int posx, posy; |
514 |
int i; |
515 |
|
516 |
wdt = [self frame].size.width; |
517 |
hgt = [self frame].size.height; |
518 |
|
519 |
pcount = (int)((int)((wdt - 16) / cellsWidth) * (int)(MAXSHELFHEIGHT / 75)); |
520 |
|
521 |
if (gpoints != NULL) { |
522 |
NSZoneFree (NSDefaultMallocZone(), gpoints); |
523 |
} |
524 |
gpoints = NSZoneMalloc (NSDefaultMallocZone(), sizeof(gridpoint) * pcount); |
525 |
|
526 |
x = 16; |
527 |
y = hgt - 55; |
528 |
posx = 0; |
529 |
posy = 0; |
530 |
|
531 |
for (i = 0; i < pcount; i++) { |
532 |
if (i > 0) { |
533 |
x += cellsWidth; |
534 |
} |
535 |
if (x >= (wdt - cellsWidth)) { |
536 |
x = 16; |
537 |
y -= 75; |
538 |
posx = 0; |
539 |
posy++; |
540 |
} |
541 |
|
542 |
gpoints[i].x = x; |
543 |
gpoints[i].y = y; |
544 |
gpoints[i].index = i; |
545 |
gpoints[i].used = 0; |
546 |
|
547 |
posx++; |
548 |
} |
549 |
} |
550 |
|
551 |
- (gridpoint *)gridPointNearestToPoint:(NSPoint)p |
552 |
{ |
553 |
float maxx = [self frame].size.width; |
554 |
float maxy = [self frame].size.height; |
555 |
float px = p.x; |
556 |
float py = p.y; |
557 |
float minx = maxx; |
558 |
float miny = maxy; |
559 |
int pos = -1; |
560 |
int i; |
561 |
|
562 |
for (i = 0; i < pcount; i++) { |
563 |
if (gpoints[i].y > 0) { |
564 |
float dx = max(px, gpoints[i].x) - min(px, gpoints[i].x); |
565 |
float dy = max(py, gpoints[i].y) - min(py, gpoints[i].y); |
566 |
|
567 |
if ((dx <= minx) && (dy <= miny)) { |
568 |
minx = dx; |
569 |
miny = dy; |
570 |
pos = i; |
571 |
} |
572 |
} |
573 |
} |
574 |
|
575 |
return &gpoints[pos]; |
576 |
} |
577 |
|
578 |
- (BOOL)isFreePosition:(NSPoint)pos |
579 |
{ |
580 |
int i; |
581 |
|
582 |
for (i = 0; i < [icons count]; i++) { |
583 |
NSPoint p = [[icons objectAtIndex: i] position]; |
584 |
if (NSEqualPoints(pos, p)) { |
585 |
return NO; |
586 |
} |
587 |
} |
588 |
|
589 |
return YES; |
590 |
} |
591 |
|
592 |
- (void)setFrame:(NSRect)frameRect |
593 |
{ |
594 |
[super setFrame: frameRect]; |
595 |
makePos(self, makePosSel); |
596 |
} |
597 |
|
598 |
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize |
599 |
{ |
600 |
int i; |
601 |
|
602 |
if (gpoints == NULL) { |
603 |
[super resizeWithOldSuperviewSize: oldFrameSize]; |
604 |
return; |
605 |
} |
606 |
|
607 |
for (i = 0; i < pcount; i++) { |
608 |
gpoints[i].used = 0; |
609 |
} |
610 |
|
611 |
for (i = 0; i < [icons count]; i++) { |
612 |
id icon = [icons objectAtIndex: i]; |
613 |
int index = [icon gridindex]; |
614 |
gridpoint gpoint = gpoints[index]; |
615 |
NSPoint p = NSMakePoint(gpoint.x, gpoint.y); |
616 |
NSRect r = NSMakeRect(p.x, p.y, 64, 52); |
617 |
|
618 |
[icon setPosition: p]; |
619 |
[icon setFrame: r]; |
620 |
gpoints[index].used = 1; |
621 |
[self setLabelRectOfIcon: icon]; |
622 |
} |
623 |
|
624 |
[self sortIcons]; |
625 |
[self setNeedsDisplay: YES]; |
626 |
} |
627 |
|
628 |
- (void)mouseDown:(NSEvent *)theEvent |
629 |
{ |
630 |
[self unselectOtherIcons: nil]; |
631 |
} |
632 |
|
633 |
- (void)drawRect:(NSRect)rect |
634 |
{ |
635 |
[super drawRect: rect]; |
636 |
|
637 |
if (dragImage != nil) { |
638 |
gridpoint *gpoint = [self gridPointNearestToPoint: dragPoint]; |
639 |
|
640 |
if (gpoint->used == 0) { |
641 |
NSPoint p = NSMakePoint(dragPoint.x + 8, dragPoint.y); |
642 |
[dragImage dissolveToPoint: p fraction: 0.3]; |
643 |
} |
644 |
} |
645 |
} |
646 |
|
647 |
// |
648 |
// IconViewsProtocol |
649 |
// |
650 |
- (void)addIconWithPaths:(NSArray *)iconpaths |
651 |
{ |
652 |
} |
653 |
|
654 |
- (void)removeIcon:(id)anIcon |
655 |
{ |
656 |
IconViewsIcon *icon = (IconViewsIcon *)anIcon; |
657 |
int index = [icon gridindex]; |
658 |
NSString *watched = [[[icon paths] objectAtIndex: 0] stringByDeletingLastPathComponent]; |
659 |
|
660 |
if ([watchedPaths containsObject: watched]) { |
661 |
[watchedPaths removeObject: watched]; |
662 |
[self unsetWatcherForPath: watched]; |
663 |
} |
664 |
|
665 |
gpoints[index].used = 0; |
666 |
[[icon myLabel] removeFromSuperview]; |
667 |
[icon removeFromSuperview]; |
668 |
[icons removeObject: icon]; |
669 |
[self resizeWithOldSuperviewSize: [self frame].size]; |
670 |
} |
671 |
|
672 |
- (void)setLabelRectOfIcon:(id)anIcon |
673 |
{ |
674 |
IconViewsIcon *icon; |
675 |
NSTextField *label; |
676 |
float iconwidth, labwidth, labxpos; |
677 |
NSRect labelRect; |
678 |
|
679 |
icon = (IconViewsIcon *)anIcon; |
680 |
label = [icon myLabel]; |
681 |
|
682 |
iconwidth = [icon frame].size.width; |
683 |
labwidth = [label frame].size.width; |
684 |
|
685 |
if(iconwidth > labwidth) { |
686 |
labxpos = [icon frame].origin.x + ((iconwidth - labwidth) / 2); |
687 |
} else { |
688 |
labxpos = [icon frame].origin.x - ((labwidth - iconwidth) / 2); |
689 |
} |
690 |
|
691 |
labelRect = NSMakeRect(labxpos, [icon frame].origin.y - 15, labwidth, 14); |
692 |
[label setFrame: labelRect]; |
693 |
} |
694 |
|
695 |
- (void)unselectOtherIcons:(id)anIcon |
696 |
{ |
697 |
int i; |
698 |
|
699 |
for (i = 0; i < [icons count]; i++) { |
700 |
IconViewsIcon *icon = [icons objectAtIndex: i]; |
701 |
if (icon != anIcon) { |
702 |
[icon unselect]; |
703 |
} |
704 |
} |
705 |
} |
706 |
|
707 |
- (void)setShiftClick:(BOOL)value |
708 |
{ |
709 |
isShiftClick = value; |
710 |
} |
711 |
|
712 |
- (void)setCurrentSelection:(NSArray *)paths |
713 |
{ |
714 |
[delegate shelf: self setCurrentSelection: paths]; |
715 |
} |
716 |
|
717 |
- (void)setCurrentSelection:(NSArray *)paths |
718 |
animateImage:(NSImage *)image |
719 |
startingAtPoint:(NSPoint)startp |
720 |
{ |
721 |
[delegate shelf: self setCurrentSelection: paths |
722 |
animateImage: image startingAtPoint: startp]; |
723 |
} |
724 |
|
725 |
- (void)openCurrentSelection:(NSArray *)paths newViewer:(BOOL)newv |
726 |
{ |
727 |
[delegate shelf: self openCurrentSelection: paths newViewer: newv]; |
728 |
} |
729 |
|
730 |
- (NSArray *)currentSelection |
731 |
{ |
732 |
return nil; |
733 |
} |
734 |
|
735 |
- (void)keyDown:(NSEvent *)theEvent |
736 |
{ |
737 |
[delegate shelf: self keyDown: theEvent]; |
738 |
} |
739 |
|
740 |
- (int)cellsWidth |
741 |
{ |
742 |
return cellsWidth; |
743 |
} |
744 |
|
745 |
- (void)setDelegate:(id)anObject |
746 |
{ |
747 |
delegate = anObject; |
748 |
} |
749 |
|
750 |
- (id)delegate |
751 |
{ |
752 |
return delegate; |
753 |
} |
754 |
|
755 |
@end |
756 |
|
757 |
@implementation Shelf(DraggingDestination) |
758 |
|
759 |
- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender |
760 |
{ |
761 |
NSPasteboard *pb = [sender draggingPasteboard]; |
762 |
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; |
763 |
gridpoint *gpoint; |
764 |
|
765 |
if ((sourceDragMask == NSDragOperationCopy) |
766 |
|| (sourceDragMask == NSDragOperationLink)) { |
767 |
return NSDragOperationNone; |
768 |
} |
769 |
|
770 |
if ([[pb types] indexOfObject: NSFilenamesPboardType] != NSNotFound) { |
771 |
isDragTarget = YES; |
772 |
DESTROY (dragImage); |
773 |
dragPoint = [sender draggedImageLocation]; |
774 |
dragPoint = [self convertPoint: dragPoint |
775 |
fromView: [[self window] contentView]]; |
776 |
gpoint = [self gridPointNearestToPoint: dragPoint]; |
777 |
dragPoint = NSMakePoint(gpoint->x, gpoint->y); |
778 |
ASSIGN (dragImage, [sender draggedImage]); |
779 |
dragRect = NSMakeRect(dragPoint.x + 8, dragPoint.y, [dragImage size].width, [dragImage size].height); |
780 |
[self setNeedsDisplay: YES]; |
781 |
return NSDragOperationAll; |
782 |
} |
783 |
isDragTarget = NO; |
784 |
return NSDragOperationNone; |
785 |
} |
786 |
|
787 |
- (unsigned int)draggingUpdated:(id <NSDraggingInfo>)sender |
788 |
{ |
789 |
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; |
790 |
NSPoint p = [sender draggedImageLocation]; |
791 |
p = [self convertPoint: p fromView: [[self window] contentView]]; |
792 |
|
793 |
if (isDragTarget == NO) { |
794 |
return NSDragOperationNone; |
795 |
} |
796 |
|
797 |
if ((sourceDragMask == NSDragOperationCopy) |
798 |
|| (sourceDragMask == NSDragOperationLink)) { |
799 |
return NSDragOperationNone; |
800 |
} |
801 |
|
802 |
if (NSEqualPoints(dragPoint, p) == NO) { |
803 |
gridpoint *gpoint; |
804 |
|
805 |
if ([self isFreePosition: dragPoint]) { |
806 |
[self setNeedsDisplayInRect: dragRect]; |
807 |
} |
808 |
|
809 |
gpoint = gridPoint(self, gridPointSel, p); |
810 |
dragPoint = NSMakePoint(gpoint->x, gpoint->y); |
811 |
|
812 |
if (gpoint->used == 0) { |
813 |
dragRect = NSMakeRect(dragPoint.x + 8, dragPoint.y, [dragImage size].width, [dragImage size].height); |
814 |
if (dragImage == nil) { |
815 |
ASSIGN (dragImage, [sender draggedImage]); |
816 |
} |
817 |
[self setNeedsDisplayInRect: dragRect]; |
818 |
|
819 |
} else { |
820 |
if (dragImage != nil) { |
821 |
DESTROY (dragImage); |
822 |
} |
823 |
return NSDragOperationNone; |
824 |
} |
825 |
} |
826 |
|
827 |
return NSDragOperationAll; |
828 |
} |
829 |
|
830 |
- (void)draggingExited:(id <NSDraggingInfo>)sender |
831 |
{ |
832 |
if (dragImage != nil) { |
833 |
DESTROY (dragImage); |
834 |
[self setNeedsDisplay: YES]; |
835 |
} |
836 |
|
837 |
isDragTarget = NO; |
838 |
} |
839 |
|
840 |
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender |
841 |
{ |
842 |
return isDragTarget; |
843 |
} |
844 |
|
845 |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender |
846 |
{ |
847 |
return YES; |
848 |
} |
849 |
|
850 |
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender |
851 |
{ |
852 |
NSPasteboard *pb = [sender draggingPasteboard]; |
853 |
NSArray *sourcePaths = [pb propertyListForType: NSFilenamesPboardType]; |
854 |
|
855 |
isDragTarget = NO; |
856 |
|
857 |
if (dragImage != nil) { |
858 |
DESTROY (dragImage); |
859 |
[self setNeedsDisplay: YES]; |
860 |
} |
861 |
|
862 |
if (sourcePaths) { |
863 |
NSPoint p = [sender draggedImageLocation]; |
864 |
gridpoint *gpoint; |
865 |
int index, i; |
866 |
|
867 |
if (viewerPath != nil) { |
868 |
for (i = 0; i < [sourcePaths count]; i++) { |
869 |
NSString *s = [sourcePaths objectAtIndex: i]; |
870 |
|
871 |
if ((subPathOfPath(viewerPath, s) == NO) |
872 |
&& ([viewerPath isEqualToString: s] == NO)) { |
873 |
return; |
874 |
} |
875 |
} |
876 |
} |
877 |
|
878 |
p = [self convertPoint: p fromView: [[self window] contentView]]; |
879 |
gpoint = [self gridPointNearestToPoint: p]; |
880 |
p = NSMakePoint(gpoint->x, gpoint->y); |
881 |
index = gpoint->index; |
882 |
|
883 |
if (gpoint->used == 0) { |
884 |
for (i = 0; i < [icons count]; i++) { |
885 |
IconViewsIcon *icon = [icons objectAtIndex: i]; |
886 |
if ([[icon paths] isEqualToArray: sourcePaths]) { |
887 |
gpoints[[icon gridindex]].used = 0; |
888 |
gpoint->used = 1; |
889 |
[icon setGridIndex: index]; |
890 |
[self resizeWithOldSuperviewSize: [self frame].size]; |
891 |
return; |
892 |
} |
893 |
} |
894 |
|
895 |
[self addIconWithPaths: sourcePaths withGridIndex: index]; |
896 |
} |
897 |
|
898 |
// if ([delegate respondsToSelector: @selector(getSelectedPaths)]) { |
899 |
// NSArray *paths = [delegate getSelectedPaths]; |
900 |
// [delegate shelf: self setCurrentSelection: paths]; |
901 |
// } |
902 |
} |
903 |
} |
904 |
|
905 |
@end |