forked from quicksilver/Quicksilver
/
QSSearchObjectView.m
1994 lines (1688 loc) · 65.5 KB
/
QSSearchObjectView.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
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
#import "QSPreferenceKeys.h"
#import "QSSearchObjectView.h"
#import "QSLibrarian.h"
#import "QSResultController.h"
#import "QSInterfaceController.h"
#import "QSFSBrowserMediator.h"
#import "QSMnemonics.h"
#import "QSWindow.h"
#import "QSRegistry.h"
#import "QSExecutor.h"
#import "QSHistoryController.h"
#import <QSFoundation/QSFoundation.h>
#import "QSNotifications.h"
#import "QSObject.h"
#import "QSObject_Drag.h"
#import "QSAction.h"
#import "QSObject_FileHandling.h"
#import "QSObject_StringHandling.h"
#import "QSObject_Pasteboard.h"
#import "NSString_Purification.h"
#import "QSObject_PropertyList.h"
#import "QSBackgroundView.h"
#import "QSController.h"
#import "QSGlobalSelectionProvider.h"
#import "QSTextProxy.h"
#define pUserKeyBindingsPath QSApplicationSupportSubPath(@"KeyBindings.qskeys", NO)
#define MAX_HISTORY_COUNT 20
#define SEARCH_RESULT_DELAY 0.05f
NSMutableDictionary *bindingsDict = nil;
@implementation QSSearchObjectView
@synthesize textModeEditor;
+ (void)initialize {
if( bindingsDict == nil ) {
NSDictionary *defaultBindings = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[QSSearchObjectView class]] pathForResource:@"DefaultBindings" ofType:@"qskeys"]];
bindingsDict = [[NSMutableDictionary alloc] initWithDictionary:[defaultBindings objectForKey:@"QSSearchObjectView"]];
[bindingsDict addEntriesFromDictionary:[[NSDictionary dictionaryWithContentsOfFile:pUserKeyBindingsPath] objectForKey:@"QSSearchObjectView"]];
[defaultBindings release];
}
}
#pragma mark -
#pragma mark Lifetime
- (void)awakeFromNib {
[super awakeFromNib];
resetTimer = nil;
searchTimer = nil;
resultTimer = nil;
preferredEdge = NSMaxXEdge;
partialString = [[NSMutableString alloc] initWithCapacity:1];
[partialString setString:@""];
matchedString = nil;
sourceArray = nil;
searchArray = nil;
resultArray = nil;
recordsHistory = YES;
shouldResetSearchArray = YES;
allowNonActions = YES;
allowText = YES;
resultController = [[QSResultController alloc] initWithFocus:self];
[self setTextCellFont:[NSFont systemFontOfSize:12.0]];
[self setTextCellFontColor:[NSColor blackColor]];
[self setTextModeEditor:(NSTextView *)[[self window] fieldEditor:YES forObject:self]];
[[self textModeEditor] bind:@"textColor" toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath:@"values.QSAppearance3T" options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"]];
[[self textModeEditor] bind:@"insertionPointColor" toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath:@"values.QSAppearance3T" options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"]];
[[self textModeEditor] setAllowsUndo:YES];
[[self textModeEditor] setAutomaticTextReplacementEnabled:YES];
searchMode = SearchFilter;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideResultView:) name:@"NSWindowDidResignKeyNotification" object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearAll) name:QSReleaseAllNotification object:nil];
resultsPadding = 0;
historyArray = [[NSMutableArray alloc] initWithCapacity:10];
parentStack = [[NSMutableArray alloc] initWithCapacity:10];
validSearch = YES;
[resultController window];
[self setVisibleString:@""];
[[self cell] bind:@"highlightColor" toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath:@"values.QSAppearance2A" options:[NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"]];
}
- (void)dealloc {
[self unbind:@"highlightColor"];
[self unbind:@"textColor"];
[self unbind:@"backgroundColor"];
[self setTextModeEditor:nil];
[partialString release], partialString = nil;
[matchedString release], matchedString = nil;
[visibleString release], visibleString = nil;
[resetTimer release], resetTimer = nil;
[searchTimer release], searchTimer = nil;
[resultTimer release], resultTimer = nil;
[selectedObject release], selectedObject = nil;
[currentEditor release], currentEditor = nil;
[historyArray release], historyArray = nil;
[parentStack release], parentStack = nil;
[childStack release], childStack = nil;
[resultController release], resultController = nil;
[searchArray release], searchArray = nil;
[sourceArray release], sourceArray = nil;
[resultArray release], resultArray = nil;
[textCellFont release];
[textCellFontColor release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#pragma mark -
#pragma mark Mnemonics Handling
- (IBAction)assignMnemonic:(id)sender {}
- (IBAction)defineMnemonicImmediately:(id)sender {
if ([self matchedString])
[[QSMnemonics sharedInstance] addAbbrevMnemonic:[self matchedString] forID:[[self objectValue] identifier] immediately:YES];
[self rescoreSelectedItem];
}
- (IBAction)promoteAction:(id)sender {
[QSExec orderActions:[NSArray arrayWithObject:[self objectValue]] aboveActions:[self resultArray]];
[self rescoreSelectedItem];
}
- (IBAction)defineMnemonic:(id)sender {
if ([self matchedString])
[[QSMnemonics sharedInstance] addAbbrevMnemonic:[self matchedString] forID:[[self objectValue] identifier]];
[self rescoreSelectedItem];
}
- (IBAction)removeImpliedMnemonic:(id)sender {
if ([self matchedString])
[[QSMnemonics sharedInstance] removeObjectMnemonic:[self matchedString] forID:[[self objectValue] identifier]];
[self rescoreSelectedItem];
}
- (IBAction)removeMnemonic:(id)sender {
if ([self matchedString]) {
[[QSMnemonics sharedInstance] removeAbbrevMnemonic:[self matchedString] forID:[[self objectValue] identifier]];
[self rescoreSelectedItem];
}
}
- (IBAction)clearMnemonics:(id)sender {
[self removeImpliedMnemonic:sender];
[self removeMnemonic:sender];
}
- (BOOL)mnemonicDefined {
return [[[QSMnemonics sharedInstance] abbrevMnemonicsForString:[self matchedString]]
indexOfObject:[[self objectValue] identifier]] != NSNotFound;
}
- (BOOL)impliedMnemonicDefined {
return nil != [[[QSMnemonics sharedInstance] objectMnemonicsForID:[[self objectValue] identifier]]objectForKey:[self matchedString]];
}
- (void)saveMnemonic {
NSString *mnemonicKey = [self matchedString];
if (!mnemonicKey || [mnemonicKey isEqualToString:@""]) return;
QSObject *mnemonicValue = [self objectValue];
if ([mnemonicValue count] > 1) {
mnemonicValue = [[[self objectValue] splitObjects] lastObject];
}
[[QSMnemonics sharedInstance] addObjectMnemonic:mnemonicKey forID:[mnemonicValue identifier]];
if (![self sourceArray]) { // don't add abbreviation if in a subsearch
[[QSMnemonics sharedInstance] addAbbrevMnemonic:mnemonicKey forID:[mnemonicValue identifier] relativeToID:nil immediately:NO];
}
[mnemonicValue updateMnemonics];
[self rescoreSelectedItem];
#ifdef DEBUG
if (VERBOSE) {
NSLog(@"Added Mnemonic: %@ for object: %@", [self matchedString], [mnemonicValue identifier]);
}
#endif
}
- (void)rescoreSelectedItem {
if (![self objectValue]) return;
//[[QSLibrarian sharedInstance] scoredArrayForString:[self matchedString] inSet:[NSArray arrayWithObject:[self objectValue]] mnemonicsOnly:![self matchedString]];
[[QSLibrarian sharedInstance] scoredArrayForString:[self matchedString] inSet:[NSArray arrayWithObject:[self objectValue]]];
if ([[resultController window] isVisible])
[resultController->resultTable reloadData];
}
#pragma mark -
#pragma mark NSView
- (void)drawRect:(NSRect)rect {
if ([self currentEditor]) {
//NSLog(@"editor draw");
[super drawRect:rect];
rect = [self frame];
if (NSWidth(rect) >128 && NSHeight(rect)>128) {
CGContextRef context = (CGContextRef) ([[NSGraphicsContext currentContext] graphicsPort]);
CGContextSetAlpha(context, 0.92);
}
// Use the background colour from the prefs for the text editor bg color (with a slight transparency)
NSColor *highlightColor = [[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"QSAppearance3B"]] colorWithAlphaComponent:0.7];
[highlightColor set];
NSBezierPath *roundRect = [NSBezierPath bezierPath];
rect = [self frame];
rect.origin = NSZeroPoint;
CGFloat radius = 0;
NSRect drawRect;
// Interfaces that have 'bezeled' cells are very picky. See QSObjectCell.m (search for [self isBezeled])
if ([[self cell] isBezeled] ){
drawRect = NSInsetRect(rect, 2.25, 2.25);
radius = drawRect.size.height/2;
} else {
radius = [self frame].size.height/[[self cell] cellRadiusFactor];
drawRect = rect;
}
[roundRect appendBezierPathWithRoundedRectangle:drawRect withRadius:radius];
[roundRect fill];
} else {
[super drawRect:rect];
}
}
- (void)setFrame:(NSRect)frameRect {
[super setFrame:frameRect];
if ([self currentEditor]) {
NSRect editorFrame = [self frame];
editorFrame.origin = NSZeroPoint;
editorFrame = NSInsetRect(editorFrame, 3, 3);
[[[self currentEditor] enclosingScrollView] setFrame: editorFrame];
[[self currentEditor] setMinSize:editorFrame.size];
}
}
- (void)viewWillMoveToSuperview:(NSView *)newSuperview {
if (!newSuperview) {
[self reset:self];
}
}
#pragma mark -
#pragma mark Accessors
- (NSMutableString *)partialString { return partialString;}
- (NSString *)visibleString { return visibleString; }
- (void)setVisibleString:(NSString *)newVisibleString {
if (visibleString != newVisibleString) {
[visibleString release];
visibleString = [newVisibleString copy];
[resultController->searchStringField setStringValue:visibleString];
if ([[self controller] respondsToSelector:@selector(searchView:changedString:)])
[(id)[self controller] searchView:self changedString:visibleString];
}
}
- (NSMutableArray *)resultArray { return resultArray; }
- (void)setResultArray:(NSMutableArray *)newResultArray {
[resultArray release];
resultArray = [newResultArray retain];
if ([[resultController window] isVisible])
[self reloadResultTable];
if ([[self controller] respondsToSelector:@selector(searchView:changedResults:)])
[(id)[self controller] searchView:self changedResults:newResultArray];
}
- (NSMutableArray *)searchArray { return searchArray; }
- (void)setSearchArray:(NSMutableArray *)newSearchArray {
if (searchArray != newSearchArray) {
[searchArray release];
searchArray = [newSearchArray retain];
}
}
- (NSMutableArray *)sourceArray { return sourceArray; }
- (void)setSourceArray:(NSMutableArray *)newSourceArray {
if (sourceArray != newSourceArray) {
[sourceArray release];
sourceArray = [newSourceArray retain];
[self setSearchArray:sourceArray];
}
}
- (BOOL)shouldResetSearchString { return shouldResetSearchString; }
- (void)setShouldResetSearchString:(BOOL)flag { shouldResetSearchString = flag; }
- (BOOL)shouldResetSearchArray { return shouldResetSearchArray; }
- (void)setShouldResetSearchArray:(BOOL)flag { shouldResetSearchArray = flag; }
- (NSRectEdge)preferredEdge { return preferredEdge; }
- (void)setPreferredEdge:(NSRectEdge)newPreferredEdge { preferredEdge = newPreferredEdge; }
- (NSString *)matchedString { return matchedString; }
- (void)setMatchedString:(NSString *)newMatchedString {
if (matchedString != newMatchedString) {
[matchedString release];
matchedString = [newMatchedString copy];
[self setNeedsDisplay:YES];
}
}
- (id)selectedObject { return selectedObject; }
- (void)setSelectedObject:(id)newSelectedObject {
if (selectedObject != newSelectedObject) {
[selectedObject release];
selectedObject = [newSelectedObject retain];
}
}
- (QSSearchMode)searchMode { return searchMode; }
- (void)setSearchMode:(QSSearchMode)newSearchMode {
// Do not allow the setting of 'Filter Catalog' when in the aSelector (action)
if (!((self == [self actionSelector]) && newSearchMode == SearchFilterAll)) {
searchMode = newSearchMode;
}
[resultController->resultTable setNeedsDisplay:YES];
if (browsing) {
[[NSUserDefaults standardUserDefaults] setInteger:searchMode forKey:kBrowseMode];
}
switch (searchMode) {
case SearchSnap:
[resultController setSearchSnapActivated];
break;
case SearchFilter:
[resultController setSearchFilterActivated];
break;
default:
[resultController setSearchFilterAllActivated];
break;
}
}
- (NSText *)currentEditor {
if ([super currentEditor])
return [super currentEditor];
else
return currentEditor;
}
- (void)setCurrentEditor:(NSText *)aCurrentEditor {
if (currentEditor != aCurrentEditor) {
[currentEditor release];
currentEditor = [aCurrentEditor retain];
}
}
- (QSSearchObjectView *)directSelector { return [[self controller] dSelector]; }
- (QSSearchObjectView *)actionSelector { return [[self controller] aSelector]; }
- (QSSearchObjectView *)indirectSelector { return [[self controller] iSelector]; }
- (BOOL)allowText { return allowText; }
- (void)setAllowText:(BOOL)flag { allowText = flag; }
- (BOOL)allowNonActions { return allowNonActions; }
- (void)setAllowNonActions:(BOOL)flag {
allowNonActions = flag;
recordsHistory = flag;
}
- (void)setResultsPadding:(CGFloat)aResultsPadding { resultsPadding = aResultsPadding; }
#pragma mark -
#pragma mark Menu Items
- (BOOL)validateMenuItem:(NSMenuItem*)anItem {
if ([anItem action] == @selector(newFile:) ) {
return YES;
}
if ([anItem action] == @selector(goForward:) ) {
return historyIndex > 0;
}
if ([anItem action] == @selector(goBackward:) ) {
return YES;
}
if ([anItem action] == @selector(defineMnemonicImmediately:) ) {
if (![self matchedString]) return NO;
[anItem setTitle:[NSString stringWithFormat:@"Set as Default for \"%@\"", [[self matchedString] uppercaseString]]];
return YES;
}
if ([anItem action] == @selector(removeMnemonic:) ) {
if (![self matchedString]) return NO;
[anItem setTitle:[NSString stringWithFormat:@"Remove as Default for \"%@\"", [[self matchedString] uppercaseString]]];
return YES;
}
if ([anItem action] == @selector(clearMnemonics:) ) {
return [self impliedMnemonicDefined];
}
return YES;
}
- (IBAction)newFile:(id)sender {
NSSavePanel *savePanel = [NSSavePanel savePanel];
NSView *content = [savePanel contentView];
// NSLog(@"sub %@", [content subviews]);
if (![content isKindOfClass:[QSBackgroundView class]]) {
NSView *newBackground = [[[QSBackgroundView alloc] init] autorelease];
[savePanel setContentView:newBackground];
[newBackground addSubview:content];
}
[savePanel setNameFieldLabel:@"Create Item:"];
[savePanel setCanCreateDirectories:YES];
NSString *oldFile = [[self objectValue] singleFilePath];
id QSIC = [[NSApp delegate] interfaceController];
[QSIC setHiding:YES];
[savePanel setDirectoryURL:[NSURL fileURLWithPath:oldFile]];
[savePanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton) {
[self setObjectValue:[QSObject fileObjectWithFileURL:[savePanel URL]]];
}
}];
[QSIC setHiding:NO];
}
- (IBAction)openFile:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories:YES];
NSView *content = [openPanel contentView];
// NSLog(@"sub %@", [content subviews]);
if (![content isKindOfClass:[QSBackgroundView class]]) {
NSView *newBackground = [[[QSBackgroundView alloc] init] autorelease];
[openPanel setContentView:newBackground];
[newBackground addSubview:content];
}
NSString *oldFile = [[self objectValue] singleFilePath];
id QSIC = [[NSApp delegate] interfaceController];
[QSIC setHiding:YES];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:oldFile]];
[openPanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton) {
[self setObjectValue:[QSObject fileObjectWithFileURL:[openPanel URL]]];
}
}];
[QSIC setHiding:NO];
}
#pragma mark -
#pragma mark Result View
- (IBAction)toggleResultView:(id)sender {
if ([[resultController window] isVisible])
[self hideResultView:sender];
else
[self showResultView:sender];
}
- (IBAction)showResultView:(id)sender {
if ([[self window] firstResponder] != self) return;
if ([[resultController window] isVisible]) return; //[resultController->resultTable reloadData];
[[resultController window] setLevel:[[self window] level] +1];
[[resultController window] setFrameUsingName:@"results" force:YES];
// if (fALPHA) [resultController setSplitLocation];
NSRect windowRect = [[resultController window] frame];
NSRect screenRect = [[[resultController window] screen] frame];
if (preferredEdge == NSMaxXEdge) {
NSPoint resultPoint = [self convertPoint:NSZeroPoint toView:nil];
resultPoint = [[self window] convertBaseToScreen:resultPoint];
if (resultPoint.x+NSWidth([self frame]) +NSWidth(windowRect)<NSMaxX(screenRect)) {
if (hFlip) {
[[[resultController window] contentView] flipSubviewsOnAxis:NO];
hFlip = NO;
}
resultPoint.x += NSWidth([self frame]);
resultPoint.y += NSHeight([self frame]) +1;
} else {
if (!hFlip) {
[[[resultController window] contentView] flipSubviewsOnAxis:NO];
hFlip = YES;
}
resultPoint.x -= NSWidth(windowRect);
resultPoint.y += NSHeight([self frame]) +1;
}
[[resultController window] setFrameTopLeftPoint:resultPoint];
} else {
NSPoint resultPoint = [[self window] convertBaseToScreen:[self frame] .origin];
//resultPoint.x;
CGFloat extraHeight = windowRect.size.height-(resultPoint.y-screenRect.origin.y);
//resultPoint.y += 2;
windowRect.origin.x = resultPoint.x;
if (extraHeight>0) {
windowRect.origin.y = screenRect.origin.y;
windowRect.size.height -= extraHeight;
} else {
// NSLog(@"pad %f", resultsPadding);
windowRect.origin.y = resultPoint.y-windowRect.size.height-resultsPadding;
}
windowRect = NSIntersectionRect(windowRect, screenRect);
[[resultController window] setFrame:windowRect display:NO];
}
[self updateResultView:sender];
if ([[self controller] respondsToSelector:@selector(searchView:resultsVisible:)])
[(id)[self controller] searchView:self resultsVisible:YES];
if ([[self window] isVisible]) {
[[resultController window] orderFront:nil];
// Show the results window
[[self window] addChildWindow:[resultController window] ordered:NSWindowAbove];
}
}
- (void)reloadResultTable {
//[resultController->resultTable reloadData];
[resultController arrayChanged:nil];
}
- (IBAction)hideResultView:(id)sender {
[[self window] removeChildWindow:[resultController window]];
[resultController setResultIconLoader:nil];
[[resultController window] orderOut:self];
if (browsing) {
browsing = NO;
[self setSearchMode:SearchFilterAll];
}
if ([[self controller] respondsToSelector:@selector(searchView:resultsVisible:)])
[(id)[self controller] searchView:self resultsVisible:NO];
}
- (IBAction)updateResultView:(id)sender {
//[resultController->searchModePopUp selectItemAtIndex:[resultController->searchModePopUp indexOfItemWithTag:searchMode]];
[self reloadResultTable];
if (selection > NSNotFound - 1) selection = 0;
[resultController->resultTable selectRowIndexes:[NSIndexSet indexSetWithIndex:(selection ? selection : 0)] byExtendingSelection:NO];
[resultController updateSelectionInfo];
}
#pragma mark -
#pragma mark Object Value
- (void)selectObjectValue:(QSObject *)newObject {
QSObject *currentObject = [self objectValue];
// resolve the current and new objects in order to compare them
if ([newObject isKindOfClass:[QSRankedObject class]]) {
newObject = [(QSRankedObject *)newObject object];
}
if ([currentObject isKindOfClass:[QSRankedObject class]]) {
currentObject = [(QSRankedObject *)currentObject object];
}
// if the two objects are not the same, send an 'object chagned' notif
if (newObject != currentObject) {
[super setObjectValue:newObject];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SearchObjectChanged" object:self];
}
}
- (void)setObjectValue:(QSBasicObject *)newObject {
[self hideResultView:self];
[self clearSearch];
[parentStack removeAllObjects];
[self setResultArray:[NSArray arrayWithObjects:newObject, nil]];
[super setObjectValue:newObject];
[[NSNotificationCenter defaultCenter] postNotificationName:@"SearchObjectChanged" object:self];
}
- (void)clearObjectValue {
[self updateHistory];
browsingHistory = NO;
[super setObjectValue:nil];
selection--;
// [[NSNotificationCenter defaultCenter] postNotificationNamse:@"SearchObjectChanged" object:self];
}
- (void)clearAll {
[super setObjectValue:nil];
[self clearHistory];
[self setSourceArray:nil];
[self setSearchArray:nil];
[self setResultArray:nil];
[parentStack removeAllObjects];
[childStack removeAllObjects];
}
- (void)selectIndex:(NSInteger)index {
// NSLog(@"selectindex %d %d", self, index);
if (index<0)
selection = 0;
else if (index >= (NSInteger)[resultArray count])
selection = [resultArray count] -1;
else
selection = index;
if ([resultArray count]) {
QSObject *object = [resultArray objectAtIndex:selection];
[self selectObjectValue:object];
[resultController->resultTable scrollRowToVisible:selection];
//[resultController->resultTable centerRowInView:selection];
[resultController->resultTable selectRowIndexes:[NSIndexSet indexSetWithIndex:(selection ? selection : 0)] byExtendingSelection:NO];
} else
[self selectObjectValue:nil];
if ([[resultController window] isVisible])
[resultController updateSelectionInfo];
}
- (void)selectObject:(QSBasicObject *)obj {
NSInteger index = 0;
//[self updateHistory];
if (obj) {
index = (NSInteger)[resultArray indexOfObject:obj];
//NSLog(@"index %d %@", index, obj);
if (index == NSNotFound) {
//if (VERBOSE) NSLog(@"Unable To Select Object : %@ in \r %@", [obj identifier] , resultArray);
return;
}
} else {
[self selectObjectValue:nil];
return;
}
[self selectIndex:index];
}
#pragma mark -
#pragma mark Utilities
- (id)externalSelection {
return [QSGlobalSelectionProvider currentSelection];
}
- (void)dropObject:(QSBasicObject *)newSelection {
NSString *action = [[self objectValue] actionForDragOperation:NSDragOperationEvery withObject:newSelection];
//NSLog(@"action %@", action);
QSAction *actionObject = [QSExec actionForIdentifier:action];
if (!action) {
NSBeep();
return;
}
if ([[self controller] isKindOfClass:[QSInterfaceController class]]) {
[[self controller] setCommandWithArray:[NSArray arrayWithObjects:newSelection, actionObject, [self objectValue], nil]];
} else {
[actionObject performOnDirectObject:(QSObject *)newSelection indirectObject:[self objectValue]];
}
}
- (void)clearSearch {
[resetTimer invalidate];
[resultTimer invalidate];
browsingHistory = NO;
[self resetString];
[partialString setString:@""];
[self setVisibleString:@""];
[self setMatchedString:nil];
[self setShouldResetSearchString:YES];
}
- (void)pageScroll:(NSInteger)direction {
if (![[resultController window] isVisible]) [self showResultView:self];
NSInteger movement = direction * (NSHeight([[resultController->resultTable enclosingScrollView] frame]) /[resultController->resultTable rowHeight]);
//NSLog(@"%d", movement);
[self moveSelectionBy:movement];
}
- (void)moveSelectionBy:(NSInteger)d {
[self selectIndex:selection+d];
}
- (void)selectHome:(id)sender {
NSLog(@"act %@", allowNonActions ? @"YES" : @"NO");
// if (allowNonActions)
// [self setObjectValue:[QSObject fileObjectWithPath:NSHomeDirectory()]];
}
// Action that selects the root location in dObject/iObject views
- (void)selectRoot:(id)sender {
if (allowNonActions)
[self setObjectValue:[QSObject fileObjectWithPath:@"/"]];
}
- (void)scrollToBeginningOfDocument:(id)sender {
[self selectIndex:0];
}
- (void)scrollToEndOfDocument:(id)sender {
[self selectIndex:[resultArray count] -1];
}
- (BOOL)executeText:(NSEvent *)theEvent {
[self clearSearch];
[self insertText:[theEvent charactersIgnoringModifiers]];
if ([[self objectValue] argumentCount] == 2) {
[[self window] makeFirstResponder:[self indirectSelector]];
// Invalidate the actionsUpdateTimer, otherwise it will fire and cause the default action to display (instead of that typed). actionsUpdateTimer gets set when the dObject loses 1st responder
[[(QSInterfaceController *)[[self window] windowController] actionsUpdateTimer] invalidate];
} else {
[self insertNewline:self];
}
return YES;
}
#ifdef DEBUG
- (IBAction)logObjectDictionary:(id)sender {
NSLog(@"Printing Object\r%@", [(QSObject *)[self objectValue] name]);
NSLog(@"Dictionary\r%@", [[self objectValue] dictionaryRepresentation]);
NSLog(@"Icon\r%@", [[self objectValue] icon]);
}
#endif
- (void)transmogrifyWithText:(NSString *)string {
if (![self allowText]) return;
if ([self currentEditor]) {
[[self window] makeFirstResponder: self];
} else {
if (string) {
[[self textModeEditor] setString:string];
[[self textModeEditor] setSelectedRange:NSMakeRange([[[self textModeEditor] string] length] , 0)];
} else if ([partialString length] && ([resetTimer isValid] || ![[NSUserDefaults standardUserDefaults] floatForKey:kResetDelay]) ) {
[[self textModeEditor] setString:[partialString stringByAppendingString:[[NSApp currentEvent] charactersIgnoringModifiers]]];
[[self textModeEditor] setSelectedRange:NSMakeRange([[[self textModeEditor] string] length] , 0)];
} else {
NSString *stringValue = [[self objectValue] stringValue];
if (stringValue) {
[[self textModeEditor] setString:stringValue];
[[self textModeEditor] setSelectedRange:NSMakeRange(0, [[[self textModeEditor] string] length])];
}
}
// Set the underlying object of the pane to be a text object
[self setObjectValue:[QSObject objectWithString:[[self textModeEditor] string]]];
NSRect titleFrame = [self frame];
NSRect editorFrame = NSInsetRect(titleFrame, NSHeight(titleFrame) /16, NSHeight(titleFrame)/16);
editorFrame.origin = NSMakePoint(NSHeight(titleFrame) /16, NSHeight(titleFrame)/16);
[[self textModeEditor] setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[[self textModeEditor] setFocusRingType:NSFocusRingTypeNone];
[[self textModeEditor] setDelegate: self];
[[self textModeEditor] setAllowsUndo:YES];
[[self textModeEditor] setHorizontallyResizable: YES];
[[self textModeEditor] setVerticallyResizable: YES];
[[self textModeEditor] setDrawsBackground: NO];
[[self textModeEditor] setEditable:YES];
[[self textModeEditor] setSelectable:YES];
NSScrollView *scrollView = [[[NSScrollView alloc] initWithFrame:editorFrame] autorelease];
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:NO];
[scrollView setAutohidesScrollers:YES];
[scrollView setDrawsBackground:NO];
NSSize contentSize = [scrollView contentSize];
[[self textModeEditor] setMinSize:NSMakeSize(0, contentSize.height)];
[[self textModeEditor] setFont:textCellFont];
[[self textModeEditor] setFieldEditor:YES];
[scrollView setDocumentView:[self textModeEditor]];
[self addSubview:scrollView];
// Don't show the text being entered in the background, just the icon
[[self cell] setImagePosition:NSImageOnly];
[[self window] makeFirstResponder:[self textModeEditor]];
[self setCurrentEditor:[self textModeEditor]];
}
}
- (void)performSearch:(NSTimer *)timer {
//NSLog(@"perform search, %d", self);
if (validSearch) {
[resultController->searchStringField setTextColor:[NSColor blackColor]];
[resultController->searchStringField display];
[self performSearchFor:partialString from:timer];
[resultController->searchStringField display];
}
// NSLog(@"search performed");
}
- (void)performSearchFor:(NSString *)string from:(id)sender {
#ifdef DEBUG
NSDate *date = [NSDate date];
#endif
// ***Quicksilver's search algorithm is case insensitive
string = [string lowercaseString];
// NSData *scores;
NSMutableArray *newResultArray = [[QSLibrarian sharedInstance] scoredArrayForString:string inSet:searchArray];
//t NSLog(@"scores %@", scores);
#ifdef DEBUG
if (DEBUG_RANKING) NSLog(@"Searched for \"%@\" in %3fms (%lu items) ", string, 1000 * -[date timeIntervalSinceNow] , (unsigned long)[newResultArray count]);
#endif
// NSLog (@"search for %@", string);
//NSLog(@"%d valid", validSearch);
if (validSearch = [newResultArray count] >0) {
[self setMatchedString:string];
// [self setScoreData:scores];
validMnemonic = YES;
if ([self searchMode] == SearchFilterAll || [self searchMode] == SearchFilter) {
[self setResultArray:newResultArray];
[self setSearchArray:newResultArray];
}
if ([self searchMode] == SearchFilterAll) {
// ! Don't search the entire catalog if we're in the aSelector (actions)
if (![[self class] isEqual:[QSSearchObjectView class]]) {
[parentStack removeAllObjects];
}
}
if ([self searchMode] == SearchSnap) {
[self selectObject:[newResultArray objectAtIndex:0]];
[self reloadResultTable];
} else if (0) { //if should retain the selection
// [self selectObject:[newResultArray objectAtIndex:0]];
} else {
[self selectIndex:0];
}
NSInteger resultBehavior = [[NSUserDefaults standardUserDefaults] integerForKey:kResultWindowBehavior];
if ([resultArray count] > 1) {
if (resultBehavior == 0)
[self showResultView:self];
else if (resultBehavior == 1) {
if ([resultTimer isValid]) {
[resultTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:[[NSUserDefaults standardUserDefaults] floatForKey:kResetDelay]]];
} else {
[resultTimer release];
resultTimer = [[NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] floatForKey:kResetDelay] target:self selector:@selector(showResultView:) userInfo:nil repeats:NO] retain];
}
}
}
} else {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSTransformBadSearchToText"] && [self searchMode] == SearchFilterAll) {
// activate text mode if the prefs setting is set and QS is in the 'Search Catalog' mode
[self transmogrifyWithText:partialString];
} else {
NSBeep();
}
validMnemonic = NO;
[resultController->searchStringField setTextColor:[NSColor redColor]];
}
// Extend Timers
if ([searchTimer isValid]) {
// NSLog(@"extend");
[searchTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:[[NSUserDefaults standardUserDefaults] floatForKey:kSearchDelay]]];
}
if ([resetTimer isValid]) {
CGFloat resetDelay = [[NSUserDefaults standardUserDefaults] floatForKey:kResetDelay];
if (resetDelay) [resetTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:resetDelay]];
}
}
- (void)resetString {
// NSLog(@"resetting");
[resultController->searchStringField setTextColor:[[resultController->searchStringField textColor] colorWithAlphaComponent:0.5]];
[resultController->searchStringField display];
}
- (void)partialStringChanged {
[self setSearchString:[[partialString copy] autorelease]];
double searchDelay = [[NSUserDefaults standardUserDefaults] floatForKey:kSearchDelay];
if (![searchTimer isValid]) {
[searchTimer release];
searchTimer = [[NSTimer scheduledTimerWithTimeInterval:searchDelay target:self selector:@selector(performSearch:) userInfo:nil repeats:NO] retain];
}
[searchTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:searchDelay]];
if ([self searchMode] != SearchFilterAll) [searchTimer fire];
if (validSearch) {
[resultController->searchStringField setTextColor:[NSColor blueColor]];
}
[self setVisibleString:[partialString uppercaseString]];
CGFloat resetDelay = [[NSUserDefaults standardUserDefaults] floatForKey:kResetDelay];
if (resetDelay) {
if ([resetTimer isValid]) {
[resetTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:resetDelay]];
} else {
[resetTimer release];
resetTimer = [[NSTimer scheduledTimerWithTimeInterval:resetDelay target:self selector:@selector(resetString) userInfo:nil repeats:NO] retain];
}
}
}
- (void)executeCommand:(id)sender {
[resultTimer invalidate];
if ([searchTimer isValid]) {
[searchTimer invalidate];
[self performSearchFor:partialString from:self];
[self display];
}
[resetTimer fire];
[[self controller] executeCommand:self];
}
- (NSFont *)textCellFont
{
return textCellFont;
}
- (void)setTextCellFont:(NSFont *)newCellFont
{
[textCellFont autorelease];
textCellFont = [newCellFont retain];
}
- (NSColor *)textCellFontColor
{
return textCellFontColor;
}
- (void)setTextCellFontColor:(NSColor *)newCellColor
{
[textCellFontColor autorelease];
textCellFontColor = [newCellColor retain];
}
#pragma mark -
#pragma mark NSResponder
- (BOOL)acceptsFirstResponder {
if (self != [self directSelector] && [[self directSelector] objectValue] == nil) {
// Don't let the aSelctor or iSelector gain focus if the dSelector is empty
return NO;
}
return YES;
}
- (BOOL)becomeFirstResponder {
if ([[[self objectValue] primaryType] isEqual:QSTextProxyType]) {
NSString *defaultValue = [[self objectValue] objectForType:QSTextProxyType];
[self transmogrify:self];
// NSLog(@"%@", [[self objectValue] dataDictionary]);
if (defaultValue) {
[self setObjectValue:[QSObject objectWithString:defaultValue]];
[[self currentEditor] setString:defaultValue];
if([[[[self actionSelector] objectValue] identifier] isEqualToString:@"FileRenameAction"]) {
NSString *fileName = [defaultValue stringByDeletingPathExtension];
[[self currentEditor] setSelectedRange:NSMakeRange(0, fileName.length)];
} else {
[[self currentEditor] selectAll:self];
}
}
}
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder {
if ([self isEqual:[self directSelector]]) {
[self updateHistory];
}
[resultTimer invalidate];
[self hideResultView:self];
[self setShouldResetSearchString:YES];
[self resetString];
[self setNeedsDisplay:YES];
return YES;
}
// This method deals with all keydowns. Some very interesting things could be done by manipulating this method
- (void)keyDown:(NSEvent *)theEvent {
[NSThread setThreadPriority:1.0];