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