/[gnustep]/gnustep/usr-apps/gworkspace/GWorkspace/Desktop/DesktopView.m
ViewVC logotype

Contents of /gnustep/usr-apps/gworkspace/GWorkspace/Desktop/DesktopView.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Tue Sep 30 15:38:05 2003 UTC (20 years, 6 months ago) by esersale
Branch: MAIN
Changes since 1.4: +4 -2 lines
*** empty log message ***

1 /* DesktopView.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 "DesktopView.h"
38 #include "IconViewsIcon.h"
39 #include "GWorkspace.h"
40 #include "GNUstep.h"
41
42 #ifndef max
43 #define max(a,b) ((a) > (b) ? (a):(b))
44 #endif
45
46 #ifndef min
47 #define min(a,b) ((a) < (b) ? (a):(b))
48 #endif
49
50 @implementation DesktopView
51
52 - (void)dealloc
53 {
54 [[NSNotificationCenter defaultCenter] removeObserver: self];
55 [self unsetWatchers];
56 NSZoneFree (NSDefaultMallocZone(), xpositions);
57 NSZoneFree (NSDefaultMallocZone(), ypositions);
58 RELEASE (icons);
59 RELEASE (watchedPaths);
60 TEST_RELEASE (backColor);
61 TEST_RELEASE (imagePath);
62 TEST_RELEASE (backImage);
63 TEST_RELEASE (dragImage);
64 [super dealloc];
65 }
66
67 - (id)init
68 {
69 self = [super init];
70
71 if (self) {
72 NSUserDefaults *defaults;
73 NSDictionary *desktopViewPrefs, *colorDict;
74 NSArray *iconsArr;
75 id result;
76 float red, green, blue, alpha;
77 int i;
78
79 fm = [NSFileManager defaultManager];
80 gw = [GWorkspace gworkspace];
81
82 [self setFrame: [[NSScreen mainScreen] frame]];
83
84 gridCoordSel = @selector(gridCoordonatesX:Y:nearestToPoint:);
85 gridCoord = [self methodForSelector: gridCoordSel];
86
87 xpositions = NULL;
88 ypositions = NULL;
89 [self makePositions];
90
91 watchedPaths = [[NSMutableArray alloc] initWithCapacity: 1];
92
93 icons = [[NSMutableArray alloc] initWithCapacity: 1];
94
95 defaults = [NSUserDefaults standardUserDefaults];
96
97 desktopViewPrefs = [defaults dictionaryForKey: @"desktopviewprefs"];
98
99 if (desktopViewPrefs != nil) {
100 colorDict = [desktopViewPrefs objectForKey: @"backcolor"];
101 if(colorDict == nil) {
102 ASSIGN (backColor, [NSColor colorWithCalibratedRed: 0.49 green: 0.60 blue: 0.73 alpha: 1.00]);
103 } else {
104 red = [[colorDict objectForKey: @"red"] floatValue];
105 green = [[colorDict objectForKey: @"green"] floatValue];
106 blue = [[colorDict objectForKey: @"blue"] floatValue];
107 alpha = [[colorDict objectForKey: @"alpha"] floatValue];
108 ASSIGN (backColor, [NSColor colorWithCalibratedRed: red green: green blue: blue alpha: alpha]);
109 }
110
111 result = [desktopViewPrefs objectForKey: @"isimage"];
112
113 if((result != nil) && ([result isEqual: @"1"])) {
114 NSString *imPath = [desktopViewPrefs objectForKey: @"imagepath"];
115 if (imPath != nil) {
116 BOOL isdir;
117 if ([fm fileExistsAtPath: imPath isDirectory: &isdir]) {
118 if (isdir == NO) {
119 NSImage *img = [[NSImage alloc] initWithContentsOfFile: imPath];
120 if (img != nil) {
121 ASSIGN (imagePath, imPath);
122 ASSIGN (backImage, img);
123 RELEASE (img);
124 }
125 }
126 }
127 }
128 }
129
130 iconsArr = [desktopViewPrefs objectForKey: @"icons"];
131
132 if (iconsArr != nil) {
133 for (i = 0; i < [iconsArr count]; i++) {
134 NSDictionary *idict = [iconsArr objectAtIndex: i];
135 NSArray *ipaths = [idict objectForKey: @"paths"];
136 int x = [[idict objectForKey: @"x"] intValue];
137 int y = [[idict objectForKey: @"y"] intValue];
138 BOOL canadd = YES;
139 int j;
140
141 for (j = 0; j < [ipaths count]; j++) {
142 NSString *p = [ipaths objectAtIndex: j];
143 if ([fm fileExistsAtPath: p] == NO) {
144 canadd = NO;
145 break;
146 }
147 }
148
149 if (canadd == YES) {
150 [self addIconWithPaths: ipaths atPosition: NSMakePoint(x, y)];
151 }
152 }
153 }
154
155 } else {
156 ASSIGN (backColor, [NSColor colorWithCalibratedRed: 0.49 green: 0.60 blue: 0.73 alpha: 1.00]);
157 backImage = nil;
158 imagePath = nil;
159 }
160
161 isDragTarget = NO;
162 dragImage = nil;
163
164 [self registerForDraggedTypes: [NSArray arrayWithObject: NSFilenamesPboardType]];
165
166 [[NSNotificationCenter defaultCenter] addObserver: self
167 selector: @selector(changeBackColor:)
168 name: GWDesktopViewColorChangedNotification
169 object: nil];
170
171 [[NSNotificationCenter defaultCenter] addObserver: self
172 selector: @selector(changeBackImage:)
173 name: GWDesktopViewImageChangedNotification
174 object: nil];
175
176 [[NSNotificationCenter defaultCenter] addObserver: self
177 selector: @selector(unsetBackImage:)
178 name: GWDesktopViewUnsetImageNotification
179 object: nil];
180
181 [[NSNotificationCenter defaultCenter] addObserver: self
182 selector: @selector(fileSystemWillChange:)
183 name: GWFileSystemWillChangeNotification
184 object: nil];
185
186 [[NSNotificationCenter defaultCenter] addObserver: self
187 selector: @selector(fileSystemDidChange:)
188 name: GWFileSystemDidChangeNotification
189 object: nil];
190
191 [[NSNotificationCenter defaultCenter] addObserver: self
192 selector: @selector(watcherNotification:)
193 name: GWFileWatcherFileDidChangeNotification
194 object: nil];
195
196 [[NSNotificationCenter defaultCenter] addObserver: self
197 selector: @selector(cellsWidthChanged:)
198 name: GWShelfCellsWidthChangedNotification
199 object: nil];
200 }
201
202 return self;
203 }
204
205 - (void)addIconWithPaths:(NSArray *)iconpaths atPosition:(NSPoint)pos
206 {
207 DesktopViewIcon *icon;
208 NSString *watched;
209
210 pos = [self arrangePosition: pos];
211 if (NSEqualPoints(pos, NSZeroPoint)) {
212 return;
213 }
214
215 icon = [[DesktopViewIcon alloc] initForPaths: iconpaths
216 atPosition: pos inContainer: self];
217 watched = [[iconpaths objectAtIndex: 0] stringByDeletingLastPathComponent];
218
219 [icons addObject: icon];
220 [self addSubview: icon];
221 [self addSubview: [icon myLabel]];
222 RELEASE (icon);
223
224 [self resizeWithOldSuperviewSize: [self frame].size];
225
226 if ([watchedPaths containsObject: watched] == NO) {
227 [watchedPaths addObject: watched];
228 [self setWatcherForPath: watched];
229 }
230 }
231
232 - (NSArray *)iconsPaths
233 {
234 NSMutableArray *iconspaths = [NSMutableArray arrayWithCapacity: 1];
235 int i;
236
237 for (i = 0; i < [icons count]; i++) {
238 DesktopViewIcon *icon = [icons objectAtIndex: i];
239 [iconspaths addObject: [icon paths]];
240 }
241
242 return iconspaths;
243 }
244
245 - (NSArray *)icons
246 {
247 return icons;
248 }
249
250 - (NSColor *)backColor
251 {
252 return backColor;
253 }
254
255 - (void)changeBackColor:(NSNotification *)notification
256 {
257 NSDictionary *notifdict;
258 float red, green, blue, alpha;
259
260 notifdict = (NSDictionary *)[notification object];
261 red = [[notifdict objectForKey: @"red"] floatValue];
262 green = [[notifdict objectForKey: @"green"] floatValue];
263 blue = [[notifdict objectForKey: @"blue"] floatValue];
264 alpha = [[notifdict objectForKey: @"alpha"] floatValue];
265 ASSIGN (backColor, [NSColor colorWithCalibratedRed: red green: green blue: blue alpha: alpha]);
266 [self setNeedsDisplay: YES];
267 }
268
269 - (void)changeBackImage:(NSNotification *)notification
270 {
271 NSString *imPath = (NSString *)[notification object];
272 BOOL isdir;
273
274 DESTROY (backImage);
275 DESTROY (imagePath);
276
277 if ([fm fileExistsAtPath: imPath isDirectory: &isdir]) {
278 if (isdir == NO) {
279 NSImage *img = [[NSImage alloc] initWithContentsOfFile: imPath];
280 if (img) {
281 ASSIGN (imagePath, imPath);
282 ASSIGN (backImage, img);
283 RELEASE (img);
284 }
285 }
286 }
287
288 [self setNeedsDisplay: YES];
289 }
290
291 - (NSImage *)shelfBackground
292 {
293 NSSize size = NSMakeSize([self frame].size.width, 112);
294 NSImage *image = [[NSImage alloc] initWithSize: size];
295 NSCachedImageRep *rep = [[NSCachedImageRep alloc] initWithSize: size
296 depth: [NSWindow defaultDepthLimit]
297 separate: YES alpha: YES];
298
299 [image addRepresentation: rep];
300 RELEASE (rep);
301
302 [image lockFocus];
303 NSCopyBits([[self window] gState],
304 NSMakeRect(0, 0, size.width, size.height),
305 NSMakePoint(0.0, 0.0));
306 [image unlockFocus];
307
308 return AUTORELEASE(image);
309 }
310
311 - (void)unsetBackImage:(NSNotification *)notification
312 {
313 DESTROY (backImage);
314 DESTROY (imagePath);
315 [self setNeedsDisplay: YES];
316 }
317
318 - (void)fileSystemWillChange:(NSNotification *)notification
319 {
320 NSDictionary *dict = (NSDictionary *)[notification object];
321 NSString *operation = [dict objectForKey: @"operation"];
322 NSString *source = [dict objectForKey: @"source"];
323 NSArray *files = [dict objectForKey: @"files"];
324
325 if (operation == NSWorkspaceMoveOperation
326 || operation == NSWorkspaceDestroyOperation
327 || operation == GWorkspaceRenameOperation
328 || operation == NSWorkspaceRecycleOperation
329 || operation == GWorkspaceRecycleOutOperation
330 || operation == GWorkspaceEmptyRecyclerOperation) {
331
332 NSMutableArray *paths = [NSMutableArray arrayWithCapacity: 1];
333 NSArray *iconpaths;
334 int i, j, m;
335
336 for (i = 0; i < [files count]; i++) {
337 NSString *s = [source stringByAppendingPathComponent: [files objectAtIndex: i]];
338 [paths addObject: s];
339 }
340
341 for (i = 0; i < [icons count]; i++) {
342 DesktopViewIcon *icon = [icons objectAtIndex: i];
343
344 iconpaths = [icon paths];
345
346 for (j = 0; j < [iconpaths count]; j++) {
347 NSString *op = [iconpaths objectAtIndex: j];
348
349 for (m = 0; m < [paths count]; m++) {
350 NSString *fp = [paths objectAtIndex: m];
351
352 if ([op hasPrefix: fp]) {
353 [icon setLocked: YES];
354 break;
355 }
356 }
357 }
358 }
359 }
360 }
361
362 - (void)fileSystemDidChange:(NSNotification *)notification
363 {
364 NSDictionary *dict = (NSDictionary *)[notification object];
365 NSString *operation = [dict objectForKey: @"operation"];
366 NSString *source = [dict objectForKey: @"source"];
367 NSString *destination = [dict objectForKey: @"destination"];
368 NSArray *files = [dict objectForKey: @"files"];
369 int i;
370
371 if (operation == GWorkspaceRenameOperation) {
372 for (i = 0; i < [icons count]; i++) {
373 DesktopViewIcon *icon = [icons objectAtIndex: i];
374 if ([icon isSinglePath] == YES) {
375 if ([[[icon paths] objectAtIndex: 0] isEqualToString: source]) {
376 [icon setPaths: [NSArray arrayWithObject: destination]];
377 [icon setNeedsDisplay: YES];
378 break;
379 }
380 }
381 }
382 }
383
384 if (operation == GWorkspaceRenameOperation) {
385 files = [NSArray arrayWithObject: [source lastPathComponent]];
386 source = [source stringByDeletingLastPathComponent];
387 }
388
389 if (operation == NSWorkspaceMoveOperation
390 || operation == NSWorkspaceDestroyOperation
391 || operation == GWorkspaceRenameOperation
392 || operation == NSWorkspaceRecycleOperation
393 || operation == GWorkspaceRecycleOutOperation
394 || operation == GWorkspaceEmptyRecyclerOperation) {
395 int i, j, m, count;
396 NSMutableArray *paths = [NSMutableArray arrayWithCapacity: 1];
397
398 for (i = 0; i < [files count]; i++) {
399 NSString *s = [source stringByAppendingPathComponent: [files objectAtIndex: i]];
400 [paths addObject: s];
401 }
402
403 count = [icons count];
404 for (i = 0; i < count; i++) {
405 BOOL deleted = NO;
406 DesktopViewIcon *icon = [icons objectAtIndex: i];
407 NSArray *iconpaths = [icon paths];
408
409 for (j = 0; j < [iconpaths count]; j++) {
410 NSString *op = [iconpaths objectAtIndex: j];
411
412 for (m = 0; m < [paths count]; m++) {
413 NSString *fp = [paths objectAtIndex: m];
414
415 if ([op hasPrefix: fp]) {
416 [self removeIcon: icon];
417 count--;
418 i--;
419 deleted = YES;
420 break;
421 }
422
423 if (deleted == YES) {
424 break;
425 }
426
427 }
428
429 if (deleted == YES) {
430 break;
431 }
432
433 }
434 }
435 }
436 }
437
438 - (void)watcherNotification:(NSNotification *)notification
439 {
440 NSDictionary *notifdict = (NSDictionary *)[notification object];
441 NSString *path = [notifdict objectForKey: @"path"];
442 NSString *event = [notifdict objectForKey: @"event"];
443 BOOL contained = NO;
444 int i;
445
446 if (event == GWFileCreatedInWatchedDirectory) {
447 return;
448 }
449
450 for (i = 0; i < [watchedPaths count]; i++) {
451 NSString *wpath = [watchedPaths objectAtIndex: i];
452 if (([wpath isEqualToString: path]) || (subPathOfPath(path, wpath))) {
453 contained = YES;
454 break;
455 }
456 }
457
458 if (contained == YES) {
459 id icon;
460 NSArray *ipaths;
461 NSString *ipath;
462 int count = [icons count];
463
464 if (event == GWWatchedDirectoryDeleted) {
465 for (i = 0; i < count; i++) {
466 icon = [icons objectAtIndex: i];
467 ipaths = [icon paths];
468 ipath = [ipaths objectAtIndex: 0];
469
470 if (subPathOfPath(path, ipath)) {
471 [self removeIcon: icon];
472 count--;
473 i--;
474 }
475 }
476 return;
477 }
478
479 if (event == GWFileDeletedInWatchedDirectory) {
480 NSArray *files = [notifdict objectForKey: @"files"];
481
482 for (i = 0; i < count; i++) {
483 int j;
484
485 icon = [icons objectAtIndex: i];
486 ipaths = [icon paths];
487
488 if ([ipaths count] == 1) {
489 ipath = [ipaths objectAtIndex: 0];
490
491 for (j = 0; j < [files count]; j++) {
492 NSString *fname = [files objectAtIndex: j];
493 NSString *fullPath = [path stringByAppendingPathComponent: fname];
494
495 if ((subPathOfPath(fullPath, ipath))
496 || ([ipath isEqualToString: fullPath])) {
497 [self removeIcon: icon];
498 count--;
499 i--;
500 break;
501 }
502 }
503
504 } else {
505
506 for (j = 0; j < [files count]; j++) {
507 NSString *fname = [files objectAtIndex: j];
508 NSString *fullPath = [path stringByAppendingPathComponent: fname];
509 BOOL deleted = NO;
510 int m;
511
512 if (deleted) {
513 break;
514 }
515
516 ipath = [ipaths objectAtIndex: 0];
517 if (subPathOfPath(fullPath, ipath)) {
518 [self removeIcon: icon];
519 count--;
520 i--;
521 break;
522 }
523
524 for (m = 0; m < [ipaths count]; m++) {
525 ipath = [ipaths objectAtIndex: m];
526
527 if ([ipath isEqualToString: fullPath]) {
528 NSMutableArray *newpaths;
529
530 if ([ipaths count] == 1) {
531 [self removeIcon: icon];
532 count--;
533 i--;
534 deleted = YES;
535 break;
536 }
537
538 newpaths = [ipaths mutableCopy];
539 [newpaths removeObject: ipath];
540 [icon setPaths: newpaths];
541 ipaths = [icon paths];
542 RELEASE (newpaths);
543 }
544 }
545
546 }
547 }
548 }
549 }
550 }
551 }
552
553 - (void)setWatchers
554 {
555 int i;
556
557 for (i = 0; i < [watchedPaths count]; i++) {
558 [self setWatcherForPath: [watchedPaths objectAtIndex: i]];
559 }
560 }
561
562 - (void)setWatcherForPath:(NSString *)path
563 {
564 [GWLib addWatcherForPath: path];
565 }
566
567 - (void)unsetWatchers
568 {
569 int i;
570
571 for (i = 0; i < [watchedPaths count]; i++) {
572 [self unsetWatcherForPath: [watchedPaths objectAtIndex: i]];
573 }
574 }
575
576 - (void)unsetWatcherForPath:(NSString *)path
577 {
578 [GWLib removeWatcherForPath: path];
579 }
580
581 - (void)cellsWidthChanged:(NSNotification *)notification
582 {
583 int i;
584
585 [self makePositions];
586
587 for (i = 0; i < [icons count]; i++) {
588 DesktopViewIcon *icon = [icons objectAtIndex: i];
589 NSPoint ipos = [icon position];
590 ipos = [self arrangePosition: ipos];
591 [icon setPosition: ipos];
592 }
593
594 [self resizeWithOldSuperviewSize: [self frame].size];
595 }
596
597 - (void)updateIcons
598 {
599 int i;
600
601 for (i = 0; i < [icons count]; i++) {
602 [[icons objectAtIndex: i] renewIcon];
603 }
604 }
605
606 - (void)saveDefaults
607 {
608 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
609 NSMutableDictionary *desktopViewPrefs, *colorDict;
610 NSMutableArray *iconsArr;
611 float red, green, blue, alpha;
612 BOOL imgok;
613 int i;
614
615 colorDict = [NSMutableDictionary dictionaryWithCapacity: 1];
616 [backColor getRed: &red green: &green blue: &blue alpha: &alpha];
617 [colorDict setObject: [NSString stringWithFormat: @"%.2f", red] forKey: @"red"];
618 [colorDict setObject: [NSString stringWithFormat: @"%.2f", green] forKey: @"green"];
619 [colorDict setObject: [NSString stringWithFormat: @"%.2f", blue] forKey: @"blue"];
620 [colorDict setObject: [NSString stringWithFormat: @"%.2f", alpha] forKey: @"alpha"];
621
622 imgok = NO;
623 if (imagePath != nil) {
624 BOOL isdir;
625 if ([fm fileExistsAtPath: imagePath isDirectory: &isdir]) {
626 if (isdir == NO) {
627 imgok = YES;
628 }
629 }
630 }
631
632 iconsArr = [NSMutableArray arrayWithCapacity: 1];
633 for (i = 0; i < [icons count]; i++) {
634 DesktopViewIcon *icon = [icons objectAtIndex: i];
635 NSMutableDictionary *iconDict = [NSMutableDictionary dictionaryWithCapacity: 1];
636 NSString *x;
637 NSString *y;
638 int value;
639
640 value = [icon position].x;
641 x = [NSString stringWithFormat: @"%i", value];
642 value = [icon position].y;
643 y = [NSString stringWithFormat: @"%i", value];
644
645 [iconDict setObject: [icon paths] forKey: @"paths"];
646 [iconDict setObject: x forKey: @"x"];
647 [iconDict setObject: y forKey: @"y"];
648
649 [iconsArr addObject: iconDict];
650 }
651
652 desktopViewPrefs = [NSMutableDictionary dictionaryWithCapacity: 1];
653 [desktopViewPrefs setObject: colorDict forKey: @"backcolor"];
654 if (imgok == YES) {
655 [desktopViewPrefs setObject: imagePath forKey: @"imagepath"];
656 [desktopViewPrefs setObject: @"1" forKey: @"isimage"];
657 } else {
658 [desktopViewPrefs setObject: @"0" forKey: @"isimage"];
659 }
660 [desktopViewPrefs setObject: iconsArr forKey: @"icons"];
661 [defaults setObject: desktopViewPrefs forKey: @"desktopviewprefs"];
662 [defaults synchronize];
663 }
664
665 - (void)makePositions
666 {
667 NSRect r;
668 int i;
669
670 r = [[NSScreen mainScreen] frame];
671
672 cellsWidth = [gw shelfCellsWidth];
673 cellsHeight = 75;
674
675 xcount = (int)(r.size.width / cellsWidth);
676 ycount = (int)(r.size.height / cellsHeight);
677
678 if (xpositions != NULL) {
679 NSZoneFree (NSDefaultMallocZone(), xpositions);
680 }
681 xpositions = NSZoneMalloc (NSDefaultMallocZone(), sizeof(float) * xcount);
682
683 if (ypositions != NULL) {
684 NSZoneFree (NSDefaultMallocZone(), ypositions);
685 }
686 ypositions = NSZoneMalloc (NSDefaultMallocZone(), sizeof(float) * ycount);
687
688 xpositions[0] = 30;
689 for (i = 1; i < xcount; i++) {
690 xpositions[i] = xpositions[i-1] + cellsWidth;
691 }
692
693 ypositions[0] = 30;
694 for (i = 1; i < ycount; i++) {
695 ypositions[i] = ypositions[i-1] + cellsHeight;
696 }
697 }
698
699 - (void)gridCoordonatesX:(float *)x Y:(float *)y nearestToPoint:(NSPoint)p
700 {
701 float maxx = [self frame].size.width;
702 float maxy = [self frame].size.height;
703 float px = p.x;
704 float py = p.y;
705 float minx = maxx;
706 float miny = maxy;
707 int posx = -1;
708 int posy = -1;
709 int i;
710
711 for (i = 0; i < xcount; i++) {
712 float dx = max(px, xpositions[i]) - min(px, xpositions[i]);
713 if (dx <= minx) {
714 minx = dx;
715 posx = i;
716 }
717 }
718
719 for (i = 0; i < ycount; i++) {
720 float dy = max(py, ypositions[i]) - min(py, ypositions[i]);
721 if (dy <= miny) {
722 miny = dy;
723 posy = i;
724 }
725 }
726
727 if ((posx == -1) || (posx == -1)) {
728 *x = 0;
729 *y = 0;
730 return;
731 }
732
733 *x = xpositions[posx];
734 *y = ypositions[posy];
735 }
736
737 - (void)getOnGridPositionX:(int *)x Y:(int *)y ofPoint:(NSPoint)p
738 {
739 int i;
740
741 *x = -1;
742 for (i = 0; i < xcount; i++) {
743 if (xpositions[i] == p.x) {
744 *x = i;
745 break;
746 }
747 }
748
749 *y = -1;
750 for (i = 0; i < ycount; i++) {
751 if (ypositions[i] == p.y) {
752 *y = i;
753 break;
754 }
755 }
756 }
757
758 - (NSPoint)firstFreePosition
759 {
760 int i, j;
761
762 for (i = 0; i < ycount; i++) {
763 for (j = 0; j < xcount; j++) {
764 NSPoint p = NSMakePoint(xpositions[j], ypositions[i]);
765 if ([self isFreePosition: p]) {
766 return p;
767 }
768 }
769 }
770
771 return NSMakePoint(0, 0);
772 }
773
774 - (BOOL)isFreePosition:(NSPoint)pos
775 {
776 int i;
777
778 for (i = 0; i < [icons count]; i++) {
779 NSPoint p = [[icons objectAtIndex: i] position];
780 if (NSEqualPoints(pos, p)) {
781 return NO;
782 }
783 }
784
785 return YES;
786 }
787
788 - (NSPoint)arrangePosition:(NSPoint)p
789 {
790 float px, py;
791 NSPoint newp;
792 int posx;
793 int posy;
794
795 (*gridCoord)(self, gridCoordSel, &px, &py, p);
796 newp = NSMakePoint(px, py);
797
798 if (NSEqualPoints(newp, NSZeroPoint)) {
799 return [self firstFreePosition];
800 }
801
802 [self getOnGridPositionX: &posx Y: &posy ofPoint: newp];
803
804 while ([self isFreePosition: newp] == NO) {
805 posx++;
806 if (posx == xcount) {
807 posx = 0;
808 posy++;
809 }
810 if (posy == ycount) {
811 return [self firstFreePosition];
812 }
813
814 newp = NSMakePoint(xpositions[posx], ypositions[posy]);
815 }
816
817 return newp;
818 }
819
820 - (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize
821 {
822 int i;
823
824 for (i = 0; i < [icons count]; i++) {
825 DesktopViewIcon *icon = [icons objectAtIndex: i];
826 NSPoint p = [icon position];
827 NSRect r = NSMakeRect(p.x, p.y, 64, 52);
828 [icon setFrame: r];
829 [self setLabelRectOfIcon: icon];
830 }
831
832 [self setNeedsDisplay: YES];
833 }
834
835 - (void)drawRect:(NSRect)rect
836 {
837 [super drawRect: rect];
838 [backColor set];
839 NSRectFill(rect);
840
841 if (backImage != nil) {
842 NSSize imsize = [backImage size];
843 NSSize scrsize = [[NSScreen mainScreen] frame].size;
844 float px = ((scrsize.width - imsize.width) / 2);
845 float py = ((scrsize.height - imsize.height) / 2);
846
847 [backImage compositeToPoint: NSMakePoint(px, py)
848 operation: NSCompositeSourceOver];
849 }
850
851 if ((dragImage != nil) && ([self isFreePosition: dragPoint])) {
852 NSPoint p = NSMakePoint(dragPoint.x + 8, dragPoint.y);
853 [dragImage dissolveToPoint: p fraction: 0.3];
854 }
855 }
856
857 //
858 // IconViewsProtocol
859 //
860 - (void)addIconWithPaths:(NSArray *)iconpaths
861 {
862 }
863
864 - (void)removeIcon:(id)anIcon
865 {
866 DesktopViewIcon *icon = (DesktopViewIcon *)anIcon;
867 NSString *watched = [[[icon paths] objectAtIndex: 0] stringByDeletingLastPathComponent];
868
869 if ([watchedPaths containsObject: watched]) {
870 [watchedPaths removeObject: watched];
871 [self unsetWatcherForPath: watched];
872 }
873
874 [[icon myLabel] removeFromSuperview];
875 [icon removeFromSuperview];
876 [icons removeObject: icon];
877 [self resizeWithOldSuperviewSize: [self frame].size];
878 }
879
880 - (void)setLabelRectOfIcon:(id)anIcon
881 {
882 DesktopViewIcon *icon = (DesktopViewIcon *)anIcon;
883 NSTextField *label = [icon myLabel];
884 float iconwidth = [icon frame].size.width;
885 float labwidth = [label frame].size.width;
886 float labxpos;
887 NSRect labelRect;
888
889 if(iconwidth > labwidth) {
890 labxpos = [icon frame].origin.x + ((iconwidth - labwidth) / 2);
891 } else {
892 labxpos = [icon frame].origin.x - ((labwidth - iconwidth) / 2);
893 }
894
895 labelRect = NSMakeRect(labxpos, [icon frame].origin.y - 15, labwidth, 14);
896 [label setFrame: labelRect];
897 }
898
899 - (void)unselectOtherIcons:(id)anIcon
900 {
901 int i;
902
903 for (i = 0; i < [icons count]; i++) {
904 DesktopViewIcon *icon = [icons objectAtIndex: i];
905 if ((icon != anIcon) && ([icon isSelect])) {
906 [icon unselect];
907 }
908 }
909 }
910
911 - (void)setShiftClick:(BOOL)value
912 {
913 }
914
915 - (void)setCurrentSelection:(NSArray *)paths
916 {
917 [gw setSelectedPaths: paths fromDesktopView: self];
918 }
919
920 - (void)setCurrentSelection:(NSArray *)paths
921 animateImage:(NSImage *)image
922 startingAtPoint:(NSPoint)startp
923 {
924 [gw setSelectedPaths: paths
925 fromDesktopView: self
926 animateImage: image
927 startingAtPoint: startp];
928 }
929
930 - (void)openCurrentSelection:(NSArray *)paths newViewer:(BOOL)newv
931 {
932 [gw openSelectedPaths: paths newViewer: newv];
933 }
934
935 - (NSArray *)currentSelection
936 {
937 return nil;
938 }
939
940 - (int)cellsWidth
941 {
942 return cellsWidth;
943 }
944
945 - (void)setDelegate:(id)anObject
946 {
947 delegate = anObject;
948 }
949
950 - (id)delegate
951 {
952 return delegate;
953 }
954
955 @end
956
957 @implementation DesktopView (DraggingDestination)
958
959 - (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender
960 {
961 NSPasteboard *pb = [sender draggingPasteboard];
962 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
963
964 if ((sourceDragMask == NSDragOperationCopy)
965 || (sourceDragMask == NSDragOperationLink)) {
966 return NSDragOperationNone;
967 }
968
969 if ([[pb types] indexOfObject: NSFilenamesPboardType] != NSNotFound) {
970 float px, py;
971
972 DESTROY (dragImage);
973 isDragTarget = YES;
974 dragPoint = [sender draggedImageLocation];
975 (*gridCoord)(self, gridCoordSel, &px, &py, dragPoint);
976 dragPoint = NSMakePoint(px, py);
977 ASSIGN (dragImage, [sender draggedImage]);
978 dragRect = NSMakeRect(dragPoint.x + 8, dragPoint.y, [dragImage size].width, [dragImage size].height);
979 return NSDragOperationAll;
980 }
981
982 isDragTarget = NO;
983 return NSDragOperationNone;
984 }
985
986 - (unsigned int)draggingUpdated:(id <NSDraggingInfo>)sender
987 {
988 NSDragOperation sourceDragMask;
989 NSPoint p;
990
991 sourceDragMask = [sender draggingSourceOperationMask];
992
993 if ((sourceDragMask == NSDragOperationCopy)
994 || (sourceDragMask == NSDragOperationLink)) {
995 return NSDragOperationNone;
996 }
997
998 p = [sender draggedImageLocation];
999 if (NSEqualPoints(dragPoint, p) == NO) {
1000 float px, py;
1001
1002 if ([self isFreePosition: dragPoint]) {
1003 [self setNeedsDisplayInRect: dragRect];
1004 }
1005 dragPoint = NSMakePoint(p.x, p.y);
1006 (*gridCoord)(self, gridCoordSel, &px, &py, dragPoint);
1007 dragPoint = NSMakePoint(px, py);
1008
1009 if ([self isFreePosition: dragPoint]) {
1010 dragRect = NSMakeRect(dragPoint.x + 8, dragPoint.y, [dragImage size].width, [dragImage size].height);
1011 if (dragImage == nil) {
1012 ASSIGN (dragImage, [sender draggedImage]);
1013 }
1014 [self setNeedsDisplayInRect: dragRect];
1015
1016 } else {
1017 if (dragImage != nil) {
1018 DESTROY (dragImage);
1019 }
1020 return NSDragOperationNone;
1021 }
1022 }
1023
1024 return NSDragOperationAll;
1025 }
1026
1027 - (void)draggingExited:(id <NSDraggingInfo>)sender
1028 {
1029 if (dragImage != nil) {
1030 DESTROY (dragImage);
1031 [self setNeedsDisplay: YES];
1032 }
1033
1034 isDragTarget = NO;
1035 }
1036
1037 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1038 {
1039 return isDragTarget;
1040 }
1041
1042 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1043 {
1044 return YES;
1045 }
1046
1047 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1048 {
1049 NSPasteboard *pb = [sender draggingPasteboard];
1050 NSArray *sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
1051 int i;
1052
1053 isDragTarget = NO;
1054
1055 if (dragImage != nil) {
1056 DESTROY (dragImage);
1057 [self setNeedsDisplay: YES];
1058 }
1059
1060 if (sourcePaths) {
1061 NSPoint p = [sender draggedImageLocation];
1062 float px, py;
1063
1064 (*gridCoord)(self, gridCoordSel, &px, &py, p);
1065 p = NSMakePoint(px, py);
1066
1067 if ([self isFreePosition: p]) {
1068 for (i = 0; i < [icons count]; i++) {
1069 DesktopViewIcon *icon = [icons objectAtIndex: i];
1070 if ([[icon paths] isEqualToArray: sourcePaths]) {
1071 [icon setPosition: p];
1072 [self resizeWithOldSuperviewSize: [self frame].size];
1073 return;
1074 }
1075 }
1076
1077 [self addIconWithPaths: sourcePaths atPosition: p];
1078 }
1079 }
1080 }
1081
1082 @end

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