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