forked from CocoaBob/xee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXeeDestinationList.m
549 lines (424 loc) · 13.5 KB
/
XeeDestinationList.m
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
#import "XeeDestinationList.h"
#import "XeeController.h"
#import "XeeDelegate.h"
#import "XeeGraphicsStuff.h"
#import "CSKeyboardShortcuts.h"
#import <Carbon/Carbon.h>
void XeePlayPoof(NSWindow *somewindow);
@implementation XeeDestinationView
-(void)dealloc
{
[super dealloc];
}
-(void)awakeFromNib
{
destinations=[XeeDestinationView defaultArray];
droprow=dropnum=0;
movemode=NO;
surpressshortcut=NO;
[self setDataSource:self];
[self setDelegate:self];
[self registerForDraggedTypes:[NSArray arrayWithObjects:@"XeeDestinationPath",NSFilenamesPboardType,nil]];
[self setDoubleAction:@selector(destinationListClick:)];
[self setMatchAlgorithm:KFSubstringMatchAlgorithm];
[self setSearchColumnIdentifiers:[NSSet setWithObject:@"filename"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData:) name:@"XeeDestinationUpdate" object:nil];
}
-(id)tableView:(NSTableView *)table objectValueForTableColumn:(NSTableColumn *)column row:(int)row
{
NSString *ident=[column identifier];
if(row==0)
{
if([ident isEqual:@"filename"]) return NSLocalizedString(@"Choose a folder...",@"Destination list entry for picking a new folder");
else return nil;
}
else
{
if([ident isEqual:@"shortcut"]) return [self shortcutForRow:row];
else
{
int index=[self indexForRow:row];
if(index<0) return nil;
NSMutableDictionary *dict=[destinations objectAtIndex:index];
return [dict objectForKey:ident];
}
}
}
-(int)numberOfRowsInTableView:(NSTableView *)table
{
return [destinations count]+1+dropnum;
}
-(void)drawRow:(int)row clipRect:(NSRect)clipRect
{
int index=[self indexForRow:row];
if(index>=0&&![self isRowSelected:row])
{
NSColor *col=[[destinations objectAtIndex:index] objectForKey:@"color"];
if(col)
{
[col set];
XeeDrawRoundedBar([self rectOfRow:row]);
}
}
if(row>=droprow&&row<droprow+dropnum)
{
// [[[NSColor blueColor] colorWithAlphaComponent:0.2] set];
[[NSColor colorWithDeviceWhite:0 alpha:0.2] set];
XeeDrawRoundedBar([self rectOfRow:row]);
}
[super drawRow:row clipRect:clipRect];
}
-(NSRect)frameOfCellAtColumn:(int)column row:(int)row
{
NSRect rect=[self rectOfRow:row];
rect.size.height-=1;
if(column==0)
{
rect.origin.x+=2;
rect.origin.y+=1;
rect.size.width=rect.size.height=16;
return rect;
}
else if(column==1)
{
rect.size.width-=20;
rect.origin.x+=20;
NSString *shortcut=[self shortcutForRow:row];
if(shortcut&&!surpressshortcut)
{
NSFont *font=[[[self tableColumnWithIdentifier:@"shortcut"] dataCell] font];
NSDictionary *attrs=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSSize size=[shortcut sizeWithAttributes:attrs];
rect.size.width-=size.width+2;
}
if(rect.size.width<0) return NSZeroRect;
else return rect;
}
else if(column==2)
{
NSString *shortcut=[self shortcutForRow:row];
if(shortcut&&!surpressshortcut)
{
NSFont *font=[[[self tableColumnWithIdentifier:@"shortcut"] dataCell] font];
NSDictionary *attrs=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSSize size=[shortcut sizeWithAttributes:attrs];
rect.origin.x+=rect.size.width-size.width-4;
rect.origin.y+=2;
rect.size.width=size.width+4;
rect.size.height-=2;
return rect;
}
else return NSZeroRect;
}
return NSZeroRect;
}
-(void)keyDown:(NSEvent *)event
{
unichar c=[[event characters] characterAtIndex:0];
if(c==13||c==3) [[NSApplication sharedApplication] sendAction:@selector(destinationListClick:) to:nil from:self];
else [super keyDown:event];
}
-(NSMenu*)menuForEvent:(NSEvent *)event
{
NSPoint clickpoint=[self convertPoint:[event locationInWindow] fromView:nil];
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:[self rowAtPoint:clickpoint]] byExtendingSelection:NO];
return [super menuForEvent:event];
}
-(void)mouseDown:(NSEvent *)event
{
NSPoint clickpoint=[self convertPoint:[event locationInWindow] fromView:nil];
int row=[self rowAtPoint:clickpoint];
int index=[self indexForRow:row];
// if(row>=0) [self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
if(index>=0)
{
NSEvent *newevent=[[self window] nextEventMatchingMask:(NSLeftMouseDraggedMask|NSLeftMouseUpMask)
untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:NO];
if(newevent&&[newevent type]==NSLeftMouseDragged)
{
NSMutableDictionary *dict=[[destinations objectAtIndex:index] retain];
NSPasteboard *pboard=[NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:@"XeeDestinationPath"] owner:self];
[pboard setString:[dict objectForKey:@"path"] forType:@"XeeDestinationPath"];
NSPoint newpoint=[self convertPoint:[newevent locationInWindow] fromView:nil];
NSRect rowrect=[self rectOfRow:row];
if([self selectedRow]==row) [self deselectAll:self];
NSImage *dragimage=[[[NSImage alloc] initWithSize:rowrect.size] autorelease];
[dragimage lockFocus];
[[dict objectForKey:@"icon"] setFlipped:YES];
NSAffineTransform *trans=[NSAffineTransform transform];
[trans translateXBy:-rowrect.origin.x yBy:-rowrect.origin.y];
[trans set];
surpressshortcut=YES;
[self drawRow:row clipRect:rowrect];
surpressshortcut=NO;
[dragimage unlockFocus];
NSPoint imgpoint=rowrect.origin;
imgpoint.y+=rowrect.size.height;
[destinations removeObjectAtIndex:index];
droprow=row;
dropnum=1;
[[NSCursor arrowCursor] push];
[self dragImage:dragimage at:imgpoint
offset:NSMakeSize(newpoint.x-clickpoint.x,newpoint.y-clickpoint.y)
event:event pasteboard:pboard source:self slideBack:NO];
[NSCursor pop];
[dict release];
return;
}
}
[super mouseDown:event];
}
-(unsigned int)draggingSourceOperationMaskForLocal:(BOOL)local
{
return NSDragOperationMove|NSDragOperationDelete;
}
-(void)draggedImage:(NSImage *)image endedAt:(NSPoint)point operation:(NSDragOperation)operation
{
if(operation!=NSDragOperationMove) XeePlayPoof([self window]);
[XeeDestinationView saveArray];
}
-(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard=[sender draggingPasteboard];
int row=[self rowForDropPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
if([[pboard types] containsObject:NSFilenamesPboardType])
{
NSArray *files=[pboard propertyListForType:NSFilenamesPboardType];
NSEnumerator *enumerator=[files objectEnumerator];
NSString *file;
while(file=[enumerator nextObject])
{
FSRef ref;
if(CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:file],&ref))
{
Boolean folder,aliased;
if(!FSResolveAliasFileWithMountFlags(&ref,TRUE,&folder,&aliased,kResolveAliasFileNoUI))
{
if(!folder) return NSDragOperationNone;
}
}
}
[self setDropRow:row num:[files count]];
}
else
{
[self setDropRow:row num:1];
}
if([[pboard types] containsObject:@"XeeDestinationPath"]) [[NSCursor arrowCursor] set];
return NSDragOperationMove;
}
-(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
if(!dropnum) return NSDragOperationNone;
[self setDropRow:[self rowForDropPoint:[self convertPoint:[sender draggingLocation] fromView:nil]]];
[self reloadData];
return NSDragOperationMove;
}
-(void)draggingExited:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard=[sender draggingPasteboard];
[self setDropRow:0 num:0];
if([[pboard types] containsObject:@"XeeDestinationPath"]) SetThemeCursor(kThemePoofCursor);
[self reloadData];
}
-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
if(!dropnum) return NO;
int row=droprow;
droprow=0;
dropnum=0;
NSPasteboard *pboard=[sender draggingPasteboard];
int insindex=row-1;
NSArray *files;
if([[pboard types] containsObject:NSFilenamesPboardType]) files=[pboard propertyListForType:NSFilenamesPboardType];
else files=[NSArray arrayWithObject:[pboard stringForType:@"XeeDestinationPath"]];
NSEnumerator *enumerator=[files objectEnumerator];
NSString *file;
while(file=[enumerator nextObject])
{
int remindex=[XeeDestinationView findDestination:file];
if(remindex!=NSNotFound)
{
[destinations removeObjectAtIndex:remindex];
if(remindex<insindex) insindex--;
}
}
[XeeDestinationView addDestinations:files index:insindex];
[XeeDestinationView saveArray];
[XeeDestinationView updateTables];
return YES;
}
-(int)rowForDropPoint:(NSPoint)point
{
int row=[self rowAtPoint:point];
if(row==0) return 1;
else if(row<0||row>[destinations count]) return [destinations count]+1;
else return row;
}
-(void)setDropRow:(int)row num:(int)num
{
int sel=[self selectedRow];
if(sel>=0)
{
if(sel>=droprow+dropnum) sel-=dropnum;
if(sel>=row) sel+=num;
}
droprow=row;
dropnum=num;
if(sel>=0) [self selectRowIndexes:[NSIndexSet indexSetWithIndex:sel] byExtendingSelection:NO];
}
-(void)setDropRow:(int)row
{
[self setDropRow:row num:dropnum];
}
-(void)updateData:(id)notification
{
[self reloadData];
}
-(IBAction)switchMode:(id)sender
{
if([sender selectedSegment]==1) movemode=YES;
else movemode=NO;
[self reloadData];
}
-(IBAction)openInXee:(id)sender
{
int index=[self indexForRow:[self selectedRow]];
if(index<0) return;
NSString *filename=[[destinations objectAtIndex:index] objectForKey:@"path"];
[[NSApp delegate] application:NSApp openFile:filename];
}
-(IBAction)openInFinder:(id)sender
{
int index=[self indexForRow:[self selectedRow]];
if(index<0) return;
NSString *filename=[[destinations objectAtIndex:index] objectForKey:@"path"];
[[NSWorkspace sharedWorkspace] openFile:filename];
}
-(IBAction)removeFromList:(id)sender
{
int index=[self indexForRow:[self selectedRow]];
if(index<0) return;
[destinations removeObjectAtIndex:index];
[XeeDestinationView updateTables];
[XeeDestinationView saveArray];
}
-(void)menuNeedsUpdate:(NSMenu *)menu
{
BOOL enabled=[self selectedRow]==0?NO:YES;
int count=[menu numberOfItems];
for(int i=0;i<count;i++) [[menu itemAtIndex:i] setEnabled:enabled];
}
-(int)indexForRow:(int)row
{
if(row<1) return -1;
int index;
if(row>=droprow+dropnum) index=row-dropnum-1;
else if(row>=droprow) return -1;
else index=row-1;
if(index>=[destinations count]) return -1;
return index;
}
-(NSString *)pathForRow:(int)row
{
int index=[self indexForRow:row];
if(index<0) return nil;
return [[destinations objectAtIndex:index] objectForKey:@"path"];
}
-(NSString *)shortcutForRow:(int)row
{
if(row==0||row>10) return nil;
if(row>=droprow&&row<droprow+dropnum) return nil;
if(row>[destinations count]) return nil;
CSAction **actions=[maindelegate copyAndMoveActions];
int index=movemode?row+9:row-1;
NSArray *shortcuts=[actions[index] shortcuts];
if([shortcuts count]) return [[shortcuts objectAtIndex:0] description];
else return nil;
}
+(void)updateTables
{
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"XeeDestinationUpdate" object:nil]];
}
+(void)suggestInsertion:(NSString *)directory
{
if([self findDestination:directory]==NSNotFound)
[self addDestinations:[NSArray arrayWithObject:directory] index:[[self defaultArray] count]];
[self updateTables];
[self saveArray];
}
+(void)addDestinations:(NSArray *)directories index:(int)index
{
NSMutableArray *destinations=[self defaultArray];
NSEnumerator *enumerator=[directories reverseObjectEnumerator];
NSString *directory;
while(directory=[enumerator nextObject])
{
NSString *dirname=[directory lastPathComponent];
NSImage *icon=[[NSWorkspace sharedWorkspace] iconForFile:directory];
NSColor *color=[NSColor clearColor];
NSURL *url=[NSURL fileURLWithPath:directory];
FSRef fsref;
FSSpec fsspec;
CFURLGetFSRef((CFURLRef)url,&fsref);
FSGetCatalogInfo(&fsref,kFSCatInfoNone,NULL,NULL,&fsspec,NULL);
IconRef dummyicon;
SInt16 label;
RGBColor rgbcol;
Str255 labelstr;
if(GetIconRefFromFile(&fsspec,&dummyicon,&label)==noErr)
{
ReleaseIconRef(dummyicon);
if(label)
if(GetLabel(label,&rgbcol,labelstr)==noErr)
{
color=[NSColor colorWithCalibratedRed:rgbcol.red/65280.0 green:rgbcol.green/65280.0 blue:rgbcol.blue/65280.0 alpha:0.2];
}
}
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
directory,@"path",dirname,@"filename",icon,@"icon",color,@"color",nil];
if(index<[destinations count]) [destinations insertObject:dict atIndex:index];
else [destinations addObject:dict];
}
}
+(int)findDestination:(NSString *)directory
{
NSMutableArray *destinations=[self defaultArray];
int count=[destinations count];
for(int i=0;i<count;i++) if([[[destinations objectAtIndex:i] objectForKey:@"path"] isEqual:directory]) return i;
return NSNotFound;
}
+(void)loadArray
{
NSArray *destarray=[[NSUserDefaults standardUserDefaults] objectForKey:@"destinations"];
[self addDestinations:destarray index:0];
}
+(void)saveArray
{
NSMutableArray *destinations=[self defaultArray];
NSMutableArray *array=[NSMutableArray arrayWithCapacity:[destinations count]];
NSEnumerator *enumerator=[destinations objectEnumerator];
NSDictionary *destination;
while(destination=[enumerator nextObject]) [array addObject:[destination objectForKey:@"path"]];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"destinations"];
}
+(NSMutableArray *)defaultArray
{
static NSMutableArray *def=nil;
if(!def)
{
def=[[NSMutableArray array] retain];
[self loadArray];
}
return def;
}
@end
void XeePlayPoof(NSWindow *somewindow)
{
/* [[[[NSSound alloc] initWithContentsOfFile:@"/System/Library/Components/CoreAudio.component/Contents/Resources/SystemSounds/dock/poof item off dock.aif"
byReference:NO] autorelease] play];*/
NSShowAnimationEffect(NSAnimationEffectDisappearingItemDefault,
[somewindow convertBaseToScreen:[somewindow mouseLocationOutsideOfEventStream]],
NSZeroSize,nil,nil,nil);
}