File: Fiend.m

package info (click to toggle)
gworkspace 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 20,832 kB
  • sloc: objc: 69,382; sh: 488; makefile: 39
file content (1166 lines) | stat: -rw-r--r-- 32,250 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
/* Fiend.m
 *  
 * Copyright (C) 2003-2013 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@imago.ro>
 * Date: August 2001
 *
 * This file is part of the GNUstep GWorkspace application
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
 */

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepBase/GNUstep.h>

#import "FSNode.h"
#import "FSNFunctions.h"
#import "GWFunctions.h"
#import "Fiend.h"
#import "FiendLeaf.h"
#import "Dialogs/Dialogs.h"
#import "GWorkspace.h"


@implementation Fiend

- (void)dealloc
{
  NSEnumerator *enumerator = [watchedPaths objectEnumerator];  
  NSString *wpath;
            
  while ((wpath = [enumerator nextObject])) {
    [gw removeWatcherForPath: wpath];
  }
  RELEASE (watchedPaths);
  
  [[NSNotificationCenter defaultCenter] removeObserver: self];	

  RELEASE (layers);
  RELEASE (namelabel);
  RELEASE (ffButt);
  RELEASE (rewButt);
  RELEASE (leftArr);
  RELEASE (rightArr);
  RELEASE (currentName);
  RELEASE (freePositions);
  RELEASE (tile);  
  RELEASE (myWin);
  [super dealloc];
}

- (id)init
{
  self = [super initWithFrame: NSMakeRect(0, 0, 64, 64)];
  if (self)
    {
      NSUserDefaults *defaults;
      NSDictionary *myPrefs;
    id leaf;
    NSRect r;
    int i, j;
	
	  gw = [GWorkspace gworkspace];

	  myWin = [[NSWindow alloc] initWithContentRect: NSZeroRect
					                      styleMask: NSBorderlessWindowMask  
                                    backing: NSBackingStoreBuffered defer: NO];

    if ([myWin setFrameUsingName: @"fiend_window"] == NO) {
      [myWin setFrame: NSMakeRect(100, 100, 64, 64) display: NO];
    }      
    r = [myWin frame];      
    r.size = NSMakeSize(64, 64);
    [myWin setFrame: r display: NO];
    
    [myWin setReleasedWhenClosed: NO]; 
    [myWin setExcludedFromWindowsMenu: YES];
    
    defaults = [NSUserDefaults standardUserDefaults];	

    layers = [[NSMutableDictionary alloc] initWithCapacity: 1];
		watchedPaths = [[NSCountedSet alloc] initWithCapacity: 1];
    
    myPrefs = [defaults dictionaryForKey: @"fiendlayers"];
    if (myPrefs != nil) {
      NSArray *names = [myPrefs allKeys];

      for (i = 0; i < [names count]; i++) {
        NSString *layername = [names objectAtIndex: i];       
        NSDictionary *pathsAndRects = [myPrefs objectForKey: layername];
        NSArray *paths = [pathsAndRects allKeys];
        NSMutableArray *leaves = [NSMutableArray arrayWithCapacity: 1];
        
        for (j = 0; j < [paths count]; j++) {
          NSString *path = [paths objectAtIndex: j];
	        NSString *watched = [path stringByDeletingLastPathComponent];	
          
          if ([[NSFileManager defaultManager] fileExistsAtPath: path]) { 
            NSDictionary *dict = [pathsAndRects objectForKey: path];
            int posx = [[dict objectForKey: @"posx"] intValue];
            int posy = [[dict objectForKey: @"posy"] intValue];            

            leaf = [[FiendLeaf alloc] initWithPosX: posx 
                                              posY: posy
                                   relativeToPoint: r.origin 
                                           forPath: path 
                                           inFiend: self  
                                         layerName: layername
                                        ghostImage: nil];
            [leaves addObject: leaf];
            RELEASE (leaf);
            
	          if ([watchedPaths containsObject: watched] == NO) {
              [gw addWatcherForPath: watched];
	          }
            
            [watchedPaths addObject: watched];
          }                 
        }
                
        [layers setObject: leaves forKey: layername];        
      }    
      currentName = [defaults stringForKey: @"fiendcurrentlayer"];
      if (currentName == nil) {
        ASSIGN (currentName, [names objectAtIndex: 0]);
      } else {
        RETAIN (currentName);
      }
      
    } else {
      NSMutableArray *leaves = [NSMutableArray arrayWithCapacity: 1];
      ASSIGN (currentName, @"Workspace");
      [layers setObject: leaves forKey: currentName];      
    }

    namelabel = [NSTextFieldCell new];
		[namelabel setFont: [NSFont boldSystemFontOfSize: 10]];
		[namelabel setBordered: NO];
		[namelabel setAlignment: NSLeftTextAlignment];
    [namelabel setStringValue: cutFileLabelText(currentName, namelabel, 52)];
	  [namelabel setDrawsBackground: NO];
	
    ASSIGN (leftArr, [NSImage imageNamed: @"FFArrow.tiff"]);
    
  	ffButt = [[NSButton alloc] initWithFrame: NSMakeRect(49, 6, 9, 9)];
		[ffButt setButtonType: NSMomentaryLight];    
    [ffButt setBordered: NO];    
    [ffButt setTransparent: YES];    
    [ffButt setTarget: self];
    [ffButt setAction: @selector(switchLayer:)];
		[self addSubview: ffButt]; 
    
    ASSIGN (rightArr, [NSImage imageNamed: @"REWArrow.tiff"]);

  	rewButt = [[NSButton alloc] initWithFrame: NSMakeRect(37, 6, 9, 9)];
		[rewButt setButtonType: NSMomentaryLight];    
    [rewButt setBordered: NO];  
    [rewButt setTransparent: YES];    
    [rewButt setTarget: self];
    [rewButt setAction: @selector(switchLayer:)];
		[self addSubview: rewButt]; 
  
    ASSIGN (tile, [NSImage imageNamed: @"common_Tile.tiff"]);
    
    [self registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];  
    [self findFreePositions];
    leaveshidden = NO;
    isDragTarget = NO;
		
		[myWin setContentView: self];	

		[[NSNotificationCenter defaultCenter] 
               addObserver: self 
                	selector: @selector(fileSystemDidChange:) 
                			name: @"GWFileSystemDidChangeNotification"
                		object: nil];                     
    
		[[NSNotificationCenter defaultCenter] 
               addObserver: self 
                  selector: @selector(watcherNotification:) 
                		  name: @"GWFileWatcherFileDidChangeNotification"
                	  object: nil];
  }
  
  return self;
}

- (void)activate
{
	[self orderFrontLeaves];
}

- (NSWindow *)myWin
{
  return myWin;
}

- (NSPoint)positionOfLeaf:(id)aleaf
{
	return [aleaf iconPosition];
}

- (BOOL)dissolveLeaf:(id)aleaf
{
	return [aleaf dissolveAndReturnWhenDone];
}

- (void)addLayer
{
  SympleDialog *dialog;
  NSString *layerName;
  NSMutableArray *leaves;
  int result;
  
  if ([myWin isVisible] == NO) {
    return;
  }

  dialog = [[SympleDialog alloc] initWithTitle: NSLocalizedString(@"New Layer", @"") 
                                      editText: @""
                                   switchTitle: nil];
  [dialog center];
  [dialog makeKeyWindow];
  [dialog orderFrontRegardless];
  
  result = [dialog runModal];
  [dialog release];

  if(result != NSAlertDefaultReturn)
    return;
  
  layerName = [dialog getEditFieldText];

  if ([layerName length] == 0) {
		NSString *msg = NSLocalizedString(@"No name supplied!", @"");
		NSString *buttstr = NSLocalizedString(@"Continue", @"");		
    NSRunAlertPanel(nil, msg, buttstr, nil, nil);  
    return;
  }
  
  if ([[layers allKeys] containsObject: layerName]) {
		NSString *msg = NSLocalizedString(@"A layer with this name is already present!", @"");
		NSString *buttstr = NSLocalizedString(@"Continue", @"");		
    NSRunAlertPanel(nil, msg, buttstr, nil, nil);  
    return;
	}
		
  leaves = [NSMutableArray arrayWithCapacity: 1];
  [layers setObject: leaves forKey: layerName];
  [self goToLayerNamed: layerName];
}

- (void)removeCurrentLayer
{
  NSArray *names, *leaves;
  NSString *newname;
	NSString *title, *msg, *buttstr;
  int i, index, result;

  if ([myWin isVisible] == NO) {
    return;
  }
  
  if ([layers count] == 1) {
		msg = NSLocalizedString(@"You can't remove the last layer!", @"");
		buttstr = NSLocalizedString(@"Continue", @"");		
    NSRunAlertPanel(nil, msg, buttstr, nil, nil);  
    return;
  }

	title = NSLocalizedString(@"Remove layer", @"");
	msg = NSLocalizedString(@"Are you sure that you want to remove this layer?", @"");
	buttstr = NSLocalizedString(@"Continue", @"");
  result = NSRunAlertPanel(title, msg, NSLocalizedString(@"OK", @""), buttstr, NULL);
  if(result != NSAlertDefaultReturn) {
    return;
  }
  
  names = [layers allKeys];  	
	index = [names indexOfObject: currentName];
	
  if (index == 0) {
    index = [names count];
  }
  index--;
  
  newname = [names objectAtIndex: index];

  leaves = [layers objectForKey: currentName];  
  for (i = 0; i < [leaves count]; i++) {
    id leaf = [leaves objectAtIndex: i];    
    NSString *watched = [[[leaf node] path] stringByDeletingLastPathComponent];    

	  if ([watchedPaths containsObject: watched]) {
		  [watchedPaths removeObject: watched];
      
      if ([watchedPaths containsObject: watched] == NO) {
        [gw removeWatcherForPath: watched];
      }
	  }
    
    [[leaf window] close];    
  }

  [layers removeObjectForKey: currentName];     
  ASSIGN (currentName, newname);  
  
  [self switchLayer: ffButt];
}

- (void)renameCurrentLayer
{
  SympleDialog *dialog;
  NSString *layerName;
  NSMutableArray *leaves;
  int result;
  
  if ([myWin isVisible] == NO) {
    return;
  }
  
  dialog = [[SympleDialog alloc] initWithTitle: NSLocalizedString(@"Rename Layer", @"") 
                                      editText: currentName
                                   switchTitle: nil];
  [dialog center];
  [dialog makeKeyWindow];
  [dialog orderFrontRegardless];
  
  result = [dialog runModal];
  [dialog release];
  
  if(result != NSAlertDefaultReturn)
    return;
  
  layerName = [dialog getEditFieldText];
  if ([layerName isEqual: currentName]) {  
    return;
  }
  
  if ([[layers allKeys] containsObject: layerName]) {
		NSString *msg = NSLocalizedString(@"A layer with this name is already present!", @"");
		NSString *buttstr = NSLocalizedString(@"Continue", @"");		
    NSRunAlertPanel(nil, msg, buttstr, nil, nil);  
    return;
	}
  
  leaves = [layers objectForKey: currentName];
  RETAIN (leaves);
  [layers removeObjectForKey: currentName];  
  ASSIGN (currentName, layerName);
  [layers setObject: leaves forKey: currentName];
  RELEASE (leaves);
  
  [namelabel setStringValue: cutFileLabelText(currentName, namelabel, 52)];
  [self setNeedsDisplay: YES];  
}

- (void)goToLayerNamed:(NSString *)lname
{
  NSArray *leaves;
  int i;

  if ([myWin isVisible] == NO) {
    return;
  }
  
  leaves = [layers objectForKey: currentName];  
  for (i = 0; i < [leaves count]; i++) {
    [[[leaves objectAtIndex: i] window] orderOut: self];
  }

  ASSIGN (currentName, lname);
	[self orderFrontLeaves];
  [self findFreePositions];
  
  [namelabel setStringValue: cutFileLabelText(currentName, namelabel, 52)];
  [self setNeedsDisplay: YES];
}

- (void)switchLayer:(id)sender
{
  NSArray *names, *leaves;
  NSString *newname;
  int i, index;

  if ([myWin isVisible] == NO) {
    return;
  }
  
  names = [layers allKeys];  
	index = [names indexOfObject: currentName];
	
  if (sender == ffButt) {
    if (index == [names count] -1) {
      index = -1;
    }
    index++;
  } else {
    if (index == 0) {
      index = [names count];
    }
    index--;
  }

  newname = [names objectAtIndex: index];
      
  leaves = [layers objectForKey: currentName];  
  for (i = 0; i < [leaves count]; i++) {
    [[[leaves objectAtIndex: i] window] orderOut: self];
  }

  ASSIGN (currentName, newname);
	[self orderFrontLeaves];
  [self findFreePositions];
  
  [namelabel setStringValue: cutFileLabelText(currentName, namelabel, 52)];
  [self setNeedsDisplay: YES];
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
	return YES;
}

- (void)mouseDown:(NSEvent*)theEvent
{
	NSEvent *nextEvent;
  NSPoint location, lastLocation, origin, leaforigin;
  float initx, inity;
  id leaf;
  NSWindow *leafWin;
  NSArray *names, *leaves;
  int i, j;
  BOOL hidden = NO, dragged = NO;
  
  [self orderFrontLeaves];

  leaves = [layers objectForKey: currentName];
    
	if ([theEvent clickCount] > 1) {    
    if (leaveshidden == NO) {    
      leaveshidden = YES;
      for (i = 0; i < [leaves count]; i++) {
        leafWin = [[leaves objectAtIndex: i] window];
        [leafWin orderOut: nil];
      }  
    } else {
      leaveshidden = NO;
      [self orderFrontLeaves];
    }    
    return;
	}  

  names = [layers allKeys];
  
  initx = [myWin frame].origin.x;
  inity = [myWin frame].origin.y;
  
  lastLocation = [theEvent locationInWindow];

  while (1) {
	  nextEvent = [myWin nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask];

    if ([nextEvent type] == NSLeftMouseUp) {    
      if (dragged == YES) {
        float nowx = [myWin frame].origin.x;
        float nowy = [myWin frame].origin.y;
        
        for (i = 0; i < [names count]; i++) {
          leaves = [layers objectForKey: [names objectAtIndex: i]];  

          for (j = 0; j < [leaves count]; j++) {
            leaf = [leaves objectAtIndex: j];
            leafWin = [leaf window];
            leaforigin = [leafWin frame].origin;            
 		        leaforigin.x -= (initx - nowx);
		        leaforigin.y -= (inity - nowy);                        
            [leafWin setFrameOrigin: leaforigin];        
          }
        }
      }
    
      [self findFreePositions];            
      [self orderFrontLeaves];
      [self updateDefaults];
      break;

    } else if ([nextEvent type] == NSLeftMouseDragged) {
      dragged = YES;
      
      if (hidden == NO) {
        for (i = 0; i < [names count]; i++) {
          leaves = [layers objectForKey: [names objectAtIndex: i]];          
          for (j = 0; j < [leaves count]; j++) {
            leaf = [leaves objectAtIndex: j];        
            [[leaf window] orderOut: self];                                   
          }
        }
        hidden = YES;
      }
      
 		  location = [myWin mouseLocationOutsideOfEventStream];
      origin = [myWin frame].origin;
		  origin.x += (location.x - lastLocation.x);
		  origin.y += (location.y - lastLocation.y);
      [myWin setFrameOrigin: origin];
      
    }
  }
}                                                        

- (void)draggedFiendLeaf:(FiendLeaf *)leaf
                 atPoint:(NSPoint)location 
                 mouseUp:(BOOL)mouseup
{
  LeafPosition *leafpos;
  static NSMutableArray *leaves;
  static FiendLeaf *hlightleaf;
  BOOL hlight, newpos;
  int i;
  NSRect r;
  static BOOL started = NO;
    
  if (started == NO) {  
    leaves = [layers objectForKey: currentName];  
    hlightleaf = nil;
    leafpos = [[LeafPosition alloc] initWithPosX: [leaf posx] posY: [leaf posy] 
                                 relativeToPoint: [[self window] frame].origin];
    [freePositions addObject: leafpos];
    RELEASE (leafpos);
    started = YES;
  }
  
  r = [myWin frame];
  
  if (mouseup == NO) {  
    hlight = NO;
    for (i = 0; i < [freePositions count]; i++) {
      LeafPosition *lfpos = [freePositions objectAtIndex: i];
      
      if ([lfpos containsPoint: location]) {
        if (hlightleaf == nil) {        
          hlightleaf = [[FiendLeaf alloc] initWithPosX: [lfpos posx] 
                                                  posY: [lfpos posy] 
                                       relativeToPoint: r.origin 
                                               forPath: nil 
                                               inFiend: self 
                                             layerName: nil
                                            ghostImage: [leaf icon]];
          [[hlightleaf window] display];                           
          [[hlightleaf window] orderBack: self];                                 
        } else {        
          [hlightleaf setPosX: [lfpos posx] posY: [lfpos posy] relativeToPoint: r.origin];        
          [[hlightleaf window] orderBack: self];                                 
        }
                             
        hlight = YES;
        break;
      }
    }
      
    if ((hlight == NO) && (hlightleaf != nil)) { 
      [[hlightleaf window] orderOut: self]; 
      RELEASE (hlightleaf);
      hlightleaf = nil;
    }
    
  } else {
    if (hlightleaf != nil) { 
      [[hlightleaf window] orderOut: nil]; 
      RELEASE (hlightleaf);
      hlightleaf = nil;
    }
      
    newpos = NO;
    for (i = 0; i < [freePositions count]; i++) {
      leafpos = [freePositions objectAtIndex: i];
      
      if ([leafpos containsPoint: location]) {
        [leaf setPosX: [leafpos posx] posY: [leafpos posy] relativeToPoint: r.origin];        
        newpos = YES;
        break;
      }
    }
      
    if (newpos == NO) {    
      NSString *watched = [[[leaf node] path] stringByDeletingLastPathComponent];    

	    if ([watchedPaths containsObject: watched]) {
		    [watchedPaths removeObject: watched];
        
        if ([watchedPaths containsObject: watched] == NO) {
          [gw removeWatcherForPath: watched];
        }
	    }
    
      [[leaf window] close];
      [leaves removeObject: leaf];
    }
      
    [self orderFrontLeaves];
    [self findFreePositions];
    started = NO;
  }
}

- (void)findFreePositions
{
  NSArray *leaves;
  id leaf;
  NSArray *positions;
  int posx, posy;
  int i, j, m, count;
      
  RELEASE (freePositions);
  freePositions = [[NSMutableArray alloc] initWithCapacity: 1];

  positions = [self positionsAroundLeafAtPosX: 0 posY: 0];
  [freePositions addObjectsFromArray: positions];

  leaves = [layers objectForKey: currentName];
  
  for (i = 0; i < [leaves count]; i++) {
    leaf = [leaves objectAtIndex: i];
    posx = [leaf posx];
    posy = [leaf posy];   
    positions = [self positionsAroundLeafAtPosX: posx posY: posy];
    [freePositions addObjectsFromArray: positions];
  }

  count = [freePositions count];
  for (i = 0; i < count; i++) {
    BOOL inuse = NO;
    LeafPosition *lpos = [freePositions objectAtIndex: i];
    posx = [lpos posx];
    posy = [lpos posy];

    inuse = (posx == 0 && posy == 0);

    if (inuse == NO) {    
      for (j = 0; j < [leaves count]; j++) {
        leaf = [leaves objectAtIndex: j];
        inuse = (posx == [leaf posx] && posy == [leaf posy]);
        if (inuse == YES) {
          break;
        }
      }
    }
    
    if (inuse == NO) {    
      for (m = 0; m < count; m++) {
        LeafPosition *lpos2 = [freePositions objectAtIndex: m]; 
        if (m != i) {    
          inuse = (posx == [lpos2 posx] && posy == [lpos2 posy]);
          if (inuse == YES) {
            break;
          }    
        }
      }
    }
    
    if (inuse == YES) { 
      [freePositions removeObjectAtIndex: i];
      i--;
      count--;
    }
  }
  
}

- (NSArray *)positionsAroundLeafAtPosX:(int)posx posY:(int)posy
{
  NSMutableArray *leafpositions;
  LeafPosition *leafpos;  
  NSPoint or;
  int x, y;

  or = [myWin frame].origin;
  
  leafpositions = [NSMutableArray arrayWithCapacity: 1];
    
  for (x = posx - 1; x <= posx + 1; x++) {
    for (y = posy + 1; y >= posy - 1; y--) {      
      if ((x == posx && y == posy) == NO) {
        leafpos = [[LeafPosition alloc] initWithPosX: x posY: y relativeToPoint: or];
        [leafpositions addObject: leafpos];
        RELEASE (leafpos);
      }
    }
  }

  return leafpositions;
}

- (void)orderFrontLeaves
{
  NSArray *leaves;
  int i;

  leaves = [layers objectForKey: currentName];  
	[myWin orderFront: nil]; 
	[myWin setLevel: NSNormalWindowLevel];
	
	[self setNeedsDisplay: YES];
  if (leaveshidden == NO) {
    for (i = 0; i < [leaves count]; i++) {
			NSWindow *win = [[leaves objectAtIndex: i] window];
    	[win orderFront: nil];
			[win setLevel: NSNormalWindowLevel];
    }
  }   
}

- (void)hide
{
  NSArray *leaves;
  int i;

  leaves = [layers objectForKey: currentName];  
  for (i = 0; i < [leaves count]; i++) {
    [[[leaves objectAtIndex: i] window] orderOut: self];
  }
  
  [myWin orderOut: self]; 
}

- (void)verifyDraggingExited:(id)sender
{
  NSArray *leaves;
  int i;

  leaves = [layers objectForKey: currentName];  
  
  for (i = 0; i < [leaves count]; i++) {
    FiendLeaf *leaf = [leaves objectAtIndex: i];
    
    if ((leaf != (FiendLeaf *)sender) && ([leaf isDragTarget] == YES)) {
      [leaf draggingExited: nil];
    }
  }
}

- (void)removeInvalidLeaf:(FiendLeaf *)leaf
{
  NSString *layerName = [leaf layerName];
  NSMutableArray *leaves = [layers objectForKey: layerName];
  NSString *watched = [[[leaf node] path] stringByDeletingLastPathComponent];    

  if ([watchedPaths containsObject: watched]) {
    [watchedPaths removeObject: watched];
    
    if ([watchedPaths containsObject: watched] == NO) {
      [gw removeWatcherForPath: watched];
    }
  }
  
  [[leaf window] close];
  [leaves removeObject: leaf]; 
}

- (void)checkIconsAfterDotsFilesChange
{
  NSArray *names = [layers allKeys];
  int i;

  for (i = 0; i < [names count]; i++) {
    NSString *lname = [names objectAtIndex: i];
    NSMutableArray *leaves = [layers objectForKey: lname];
    int count = [leaves count];
    BOOL modified = NO;  
    int j;

    for (j = 0; j < count; j++) {
      id leaf = [leaves objectAtIndex: j];
      NSString *leafpath = [[leaf node] path];
      
      if ([leafpath rangeOfString: @"."].location != NSNotFound) {
        [self removeInvalidLeaf: leaf];
        modified = YES;   
        count--;
        j--;
      }
    }
    
    if (modified && ([lname isEqual: currentName])) {
      [self orderFrontLeaves];
      [self findFreePositions];
    }
  }
}

- (void)checkIconsAfterHidingOfPaths:(NSArray *)paths
{
  NSArray *names = [layers allKeys];
  int i;

  for (i = 0; i < [names count]; i++) {
    NSString *lname = [names objectAtIndex: i];
    NSMutableArray *leaves = [layers objectForKey: lname];
    int count = [leaves count];
    BOOL modified = NO;  
    int j, m;

    for (j = 0; j < count; j++) {
      id leaf = [leaves objectAtIndex: j];
      NSString *leafpath = [[leaf node] path];
      
      for (m = 0; m < [paths count]; m++) {
        NSString *path = [paths objectAtIndex: m]; 
      
        if (isSubpathOfPath(path, leafpath) || [path isEqual: leafpath]) {
          [self removeInvalidLeaf: leaf];          
          modified = YES;   
          count--;
          j--;
          break;
        }
      }
    }
    
    if (modified && ([lname isEqual: currentName])) {
      [self orderFrontLeaves];
      [self findFreePositions];
    }
  }
}

- (void)fileSystemDidChange:(NSNotification *)notification
{
  CREATE_AUTORELEASE_POOL(arp);
  NSDictionary *dict = [notification object];
  NSString *operation = [dict objectForKey: @"operation"];
  NSString *source = [dict objectForKey: @"source"];
  NSArray *files = [dict objectForKey: @"files"];
  
  if ([operation isEqual: @"GWorkspaceRenameOperation"]) {
		files = [NSArray arrayWithObject: [source lastPathComponent]];
    source = [source stringByDeletingLastPathComponent];
  }	

  if ([operation isEqual: NSWorkspaceMoveOperation] 
        || [operation isEqual: NSWorkspaceDestroyOperation]
				|| [operation isEqual: @"GWorkspaceRenameOperation"]
				|| [operation isEqual: NSWorkspaceRecycleOperation]
				|| [operation isEqual: @"GWorkspaceRecycleOutOperation"]
				|| [operation isEqual: @"GWorkspaceEmptyRecyclerOperation"]) {
    NSMutableArray *paths = [NSMutableArray arrayWithCapacity: 1];
    NSArray *names = [layers allKeys];    
    int i;
    
    for (i = 0; i < [files count]; i++) {
      NSString *s = [source stringByAppendingPathComponent: [files objectAtIndex: i]];
      [paths addObject: s];
    }

    for (i = 0; i < [names count]; i++) {
      NSString *lname = [names objectAtIndex: i];
      NSMutableArray *leaves = [layers objectForKey: lname];
      int count = [leaves count];
      BOOL modified = NO;  
      int j, m;

      for (j = 0; j < count; j++) {
        id leaf = [leaves objectAtIndex: j];
        NSString *leafpath = [[leaf node] path];

        for (m = 0; m < [paths count]; m++) {
          NSString *path = [paths objectAtIndex: m]; 

          if (isSubpathOfPath(path, leafpath) || [path isEqual: leafpath]) {
            [self removeInvalidLeaf: leaf];          
            modified = YES;   
            count--;
            j--;
            break;
          }
        }
      }
      
      if (modified && ([lname isEqual: currentName])) {
        [self orderFrontLeaves];
        [self findFreePositions];
      }
    }
  }
  
  RELEASE (arp);
}

- (void)watcherNotification:(NSNotification *)notification
{
  CREATE_AUTORELEASE_POOL(arp);
	NSDictionary *notifdict = (NSDictionary *)[notification object];
  NSString *path = [notifdict objectForKey: @"path"];
	NSString *event = [notifdict objectForKey: @"event"];
  NSEnumerator *enumerator;
  NSString *wpath;
	BOOL contained = NO;
	
	if ([event isEqual: @"GWFileCreatedInWatchedDirectory"]) {
    RELEASE (arp);
		return;
	}
  
  enumerator = [watchedPaths objectEnumerator];  
            
  while ((wpath = [enumerator nextObject])) {
		if (([wpath isEqual: path]) || (isSubpathOfPath(path, wpath))) {
			contained = YES;
			break;
		}
  }

  if (contained) {
    NSArray *names = [layers allKeys];
    int i;

    for (i = 0; i < [names count]; i++) {
      NSString *lname = [names objectAtIndex: i];
      NSMutableArray *leaves = [layers objectForKey: lname];
      int count = [leaves count];
      BOOL modified = NO;  
      int j;
      
      if ([event isEqual: @"GWWatchedPathDeleted"]) {
        for (j = 0; j < count; j++) {
          id leaf = [leaves objectAtIndex: j];
          NSString *leafpath = [[leaf node] path];

				  if (isSubpathOfPath(path, leafpath)) {
					  [self removeInvalidLeaf: leaf];
            modified = YES; 
					  count--;
					  j--;
				  }
        }

      } else if ([event isEqual: @"GWFileDeletedInWatchedDirectory"]) {
			  NSArray *files = [notifdict objectForKey: @"files"];
      
        for (j = 0; j < count; j++) {
          id leaf = [leaves objectAtIndex: j];
          NSString *leafpath = [[leaf node] path];
          int m;
          
          for (m = 0; m < [files count]; m++) {
            NSString *fname = [files objectAtIndex: m];
            NSString *fullPath = [path stringByAppendingPathComponent: fname];

            if ((isSubpathOfPath(fullPath, leafpath))
															      || ([fullPath isEqual: leafpath])) {
					    [self removeInvalidLeaf: leaf];
              modified = YES; 
					    count--;
					    j--;
              break;
            }
          }
        }
      }
      
      if (modified && ([lname isEqual: currentName])) {
        [self orderFrontLeaves];
        [self findFreePositions];
      }
    }
  }
  
  RELEASE (arp);
}

- (void)updateDefaults
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];		
  NSMutableDictionary *prefs = [NSMutableDictionary dictionaryWithCapacity: 1];
  NSArray *names = [layers allKeys];
  int i, j;
    
  for (i = 0; i < [names count]; i++) {
    NSString *name = [names objectAtIndex: i];   
    NSArray *leaves = [layers objectForKey: name];      
    NSMutableDictionary *pathsAndRects = [NSMutableDictionary dictionaryWithCapacity: 1];    
    
    for (j = 0; j < [leaves count]; j++) {
      NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 1];    
      id leaf = [leaves objectAtIndex: j];
      [dict setObject: [NSString stringWithFormat: @"%i", [leaf posx]] forKey: @"posx"];      
      [dict setObject: [NSString stringWithFormat: @"%i", [leaf posy]] forKey: @"posy"];      
      [pathsAndRects setObject: dict forKey: [[leaf node] path]];
    }
    
    [prefs setObject: pathsAndRects forKey: name];    
  }

 	[defaults setObject: prefs forKey: @"fiendlayers"];  
  [defaults setObject: currentName forKey: @"fiendcurrentlayer"];  
  
  [myWin saveFrameUsingName: @"fiend_window"];
}

- (void)drawRect:(NSRect)rect
{
  [self lockFocus];
	[tile compositeToPoint: NSZeroPoint operation: NSCompositeSourceOver]; 
  [leftArr compositeToPoint: NSMakePoint(49, 6) 
                  operation: NSCompositeSourceOver]; 
  [rightArr compositeToPoint: NSMakePoint(37, 6) 
                   operation: NSCompositeSourceOver]; 
	[namelabel drawWithFrame: NSMakeRect(4, 50, 56, 10) inView: self];   
  [self unlockFocus];  
}

@end

@implementation Fiend (DraggingDestination)

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
	NSPasteboard *pb = [sender draggingPasteboard];
	
  if([[pb types] indexOfObject: NSFilenamesPboardType] != NSNotFound) {
  	NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
		
		if ((sourceDragMask == NSDragOperationCopy) 
											|| (sourceDragMask == NSDragOperationLink)) {
			return NSDragOperationNone;
		}
	
    isDragTarget = YES;
  	return NSDragOperationAll;
  }
     
  return NSDragOperationNone;
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
  NSDragOperation sourceDragMask;
	
	if (isDragTarget == NO) {
		return NSDragOperationNone;
	}

	sourceDragMask = [sender draggingSourceOperationMask];

	if ((sourceDragMask == NSDragOperationCopy) 
												|| (sourceDragMask == NSDragOperationLink)) {
		return NSDragOperationNone;
	}

	return NSDragOperationAll;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
	isDragTarget = NO;  
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
	return isDragTarget;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
	return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
  NSPasteboard *pb;
	NSArray *sourcePaths;
  NSString *path;
  NSString *basepath;
  NSMutableArray *leaves;
  id leaf;
  NSRect r;
  int px, py, posx, posy;
  int i;

  pb = [sender draggingPasteboard];
  sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
  
  if ([sourcePaths count] > 1) {
		NSString *msg = NSLocalizedString(@"You can't dock multiple paths!", @"");
		NSString *buttstr = NSLocalizedString(@"Continue", @"");		
    NSRunAlertPanel(nil, msg, buttstr, nil, nil);
    isDragTarget = NO;
    return;
  }

  leaves = [layers objectForKey: currentName];  

  path = [sourcePaths objectAtIndex: 0];
  basepath = [path stringByDeletingLastPathComponent];

  if ([basepath isEqual: [gw trashPath]]) {
    isDragTarget = NO;
    return;
  }
    
  for (i = 0; i < [leaves count]; i++) {
    leaf = [leaves objectAtIndex: i];    
    if ([[[leaf node] path] isEqual: path] == YES) {
			NSString *msg = NSLocalizedString(@"This object is already present in this layer!", @"");
			NSString *buttstr = NSLocalizedString(@"Continue", @"");		
      NSRunAlertPanel(nil, msg, buttstr, nil, nil);
      isDragTarget = NO;
      return;
    }
  }
      
  r = [myWin frame];

  posx = 0;
  posy = 0;
  for (i = 0; i < [leaves count]; i++) {
    leaf = [leaves objectAtIndex: i];
    px = [leaf posx];
    py = [leaf posy];
    if ((px == posx) && (py < posy)) {
      posy = py;
    }
  }
  posy--;
                  
  leaf = [[FiendLeaf alloc] initWithPosX: posx 
                                    posY: posy
                         relativeToPoint: r.origin 
                                 forPath: path 
                                 inFiend: self 
                               layerName: currentName 
                              ghostImage: nil];                          
  [leaves addObject: leaf];
  RELEASE (leaf);
  
	if ([watchedPaths containsObject: basepath] == NO) {
    [gw addWatcherForPath: basepath];
	}
  
  [watchedPaths addObject: basepath];
  
  leaf = [leaves objectAtIndex: [leaves count] -1];
  [[leaf window] display]; 
  [self findFreePositions];  
  [self orderFrontLeaves];
  
  isDragTarget = NO;
  
  [self updateDefaults];
}

@end