forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookmarkListView.m
950 lines (808 loc) · 29.8 KB
/
BookmarkListView.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
/*
** BookmarkListView.m
** iTerm
**
** Created by George Nachman on 8/26/10.
** Project: iTerm
**
** Description: Custom view that shows a search field and table of bookmarks
** and integrates them.
**
** 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
*/
#import "BookmarkListView.h"
#import <iTerm/BookmarkModel.h>
#import <iTerm/ITAddressBookMgr.h>
#import <iTerm/PTYSession.h>
#import "iTermSearchField.h"
#define BookmarkTableViewDataType @"iTerm2BookmarkGuid"
const int kSearchWidgetHeight = 22;
const int kInterWidgetMargin = 10;
// This wraps a single bookmark and adds a KeyValueCoding. To keep things simple
// it will hold only the bookmark's GUID, since bookmark dictionaries themselves
// are evanescent.
//
// It implements a KeyValueCoding so that sort descriptors will work.
@interface BookmarkRow : NSObject
{
NSString* guid;
BookmarkModel* underlyingModel;
}
- (id)initWithBookmark:(Bookmark*)bookmark underlyingModel:(BookmarkModel*)underlyingModel;
- (void)dealloc;
- (Bookmark*)bookmark;
@end
@interface BookmarkRow (KeyValueCoding)
// We need ascending order to sort default before not-default so we can't use
// anything senible like BOOL or "Yes"/"No" because they'd sort wrong.
typedef enum { IsDefault = 1, IsNotDefault = 2 } BookmarkRowIsDefault;
- (NSNumber*)default;
- (NSString*)name;
- (NSString*)shortcut;
- (NSString*)command;
- (NSString*)guid;
@end
@implementation BookmarkRow
- (id)initWithBookmark:(Bookmark*)bookmark underlyingModel:(BookmarkModel*)newUnderlyingModel;
{
self = [super init];
if (self) {
guid = [[bookmark objectForKey:KEY_GUID] retain];
self->underlyingModel = [newUnderlyingModel retain];
}
return self;
}
- (void)dealloc
{
[underlyingModel release];
[guid release];
[super dealloc];
}
- (Bookmark*)bookmark
{
return [underlyingModel bookmarkWithGuid:guid];
}
@end
@implementation BookmarkRow (KeyValueCoding)
- (NSNumber*)default
{
BOOL isDefault = [[[self bookmark] objectForKey:KEY_GUID] isEqualToString:[[[BookmarkModel sharedInstance] defaultBookmark] objectForKey:KEY_GUID]];
return [NSNumber numberWithInt:isDefault ? IsDefault : IsNotDefault];
}
- (NSString*)name
{
return [[self bookmark] objectForKey:KEY_NAME];
}
- (NSString*)shortcut
{
return [[self bookmark] objectForKey:KEY_SHORTCUT];
}
- (NSString*)command
{
return [[self bookmark] objectForKey:KEY_COMMAND];
}
- (NSString*)guid
{
return [[self bookmark] objectForKey:KEY_GUID];
}
@end
@implementation BookmarkModelWrapper
- (id)initWithModel:(BookmarkModel*)model
{
self = [super init];
if (self) {
underlyingModel = model;
bookmarks = [[NSMutableArray alloc] init];
filter = [[NSMutableString alloc] init];
[self sync];
}
return self;
}
- (void)dealloc
{
[bookmarks release];
[filter release];
[super dealloc];
}
- (void)setSortDescriptors:(NSArray*)newSortDescriptors
{
[sortDescriptors autorelease];
sortDescriptors = [newSortDescriptors retain];
}
- (void)dump
{
for (int i = 0; i < [self numberOfBookmarks]; ++i) {
NSLog(@"Dump of %p: At %d: %@", self, i, [[self bookmarkRowAtIndex:i] name]);
}
}
- (void)sort
{
if ([sortDescriptors count] > 0) {
[bookmarks sortUsingDescriptors:sortDescriptors];
}
}
- (int)numberOfBookmarks
{
return [bookmarks count];
}
- (BookmarkRow*)bookmarkRowAtIndex:(int)i
{
return [bookmarks objectAtIndex:i];
}
- (Bookmark*)bookmarkAtIndex:(int)i
{
return [[bookmarks objectAtIndex:i] bookmark];
}
- (int)indexOfBookmarkWithGuid:(NSString*)guid
{
for (int i = 0; i < [bookmarks count]; ++i) {
if ([[[bookmarks objectAtIndex:i] guid] isEqualToString:guid]) {
return i;
}
}
return -1;
}
- (BookmarkModel*)underlyingModel
{
return underlyingModel;
}
- (void)sync
{
[bookmarks removeAllObjects];
NSArray* filteredBookmarks = [underlyingModel bookmarkIndicesMatchingFilter:filter];
for (NSNumber* n in filteredBookmarks) {
int i = [n intValue];
//NSLog(@"Wrapper at %p add bookmark %@ at index %d", self, [[underlyingModel bookmarkAtIndex:i] objectForKey:KEY_NAME], i);
[bookmarks addObject:[[[BookmarkRow alloc] initWithBookmark:[underlyingModel bookmarkAtIndex:i]
underlyingModel:underlyingModel] autorelease]];
}
[self sort];
}
- (void)moveBookmarkWithGuid:(NSString*)guid toIndex:(int)row
{
// Make the change locally.
int origRow = [self indexOfBookmarkWithGuid:guid];
if (origRow < row) {
[bookmarks insertObject:[bookmarks objectAtIndex:origRow] atIndex:row];
[bookmarks removeObjectAtIndex:origRow];
} else if (origRow > row) {
BookmarkRow* temp = [[bookmarks objectAtIndex:origRow] retain];
[bookmarks removeObjectAtIndex:origRow];
[bookmarks insertObject:temp atIndex:row];
[temp release];
}
}
- (void)pushOrderToUnderlyingModel
{
// Since we may have a filter, let's ensure that the visible bookmarks occur
// in the same order in the underlying model without regard to how invisible
// bookmarks fit into the order. This also prevents instability when the
// reload happens.
int i = 0;
for (BookmarkRow* theRow in bookmarks) {
[underlyingModel moveGuid:[theRow guid] toRow:i++];
}
[underlyingModel rebuildMenus];
}
- (NSArray*)sortDescriptors
{
return sortDescriptors;
}
- (void)setFilter:(NSString*)newFilter
{
[filter release];
filter = [[NSString stringWithString:newFilter] retain];
}
@end
@implementation BookmarkTableView
- (void)setParent:(id)parent
{
parent_ = parent;
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
if ([[parent_ delegate] respondsToSelector:@selector(bookmarkTable:menuForEvent:)]) {
return [[parent_ delegate] bookmarkTable:parent_ menuForEvent:theEvent];
}
return nil;
}
@end
@implementation BookmarkListView
- (void)awakeFromNib
{
}
- (void)focusSearchField
{
[[self window] makeFirstResponder:searchField_];
}
// Drag drop -------------------------------
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
// Copy guid to pboard
NSInteger rowIndex = [rowIndexes firstIndex];
NSMutableSet* guids = [[[NSMutableSet alloc] init] autorelease];
while (rowIndex != NSNotFound) {
Bookmark* bookmark = [dataSource_ bookmarkAtIndex:rowIndex];
NSString* guid = [bookmark objectForKey:KEY_GUID];
[guids addObject:guid];
rowIndex = [rowIndexes indexGreaterThanIndex:rowIndex];
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:guids];
[pboard declareTypes:[NSArray arrayWithObject:BookmarkTableViewDataType] owner:self];
[pboard setData:data forType:BookmarkTableViewDataType];
return YES;
}
- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
if ([info draggingSource] != aTableView) {
return NSDragOperationNone;
}
// Add code here to validate the drop
switch (operation) {
case NSTableViewDropOn:
return NSDragOperationNone;
case NSTableViewDropAbove:
return NSDragOperationMove;
default:
return NSDragOperationNone;
}
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:BookmarkTableViewDataType];
NSSet* guids = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSMutableDictionary* map = [[[NSMutableDictionary alloc] init] autorelease];
for (NSString* guid in guids) {
[map setObject:guid forKey:[NSNumber numberWithInt:[dataSource_ indexOfBookmarkWithGuid:guid]]];
}
NSArray* sortedIndexes = [map allKeys];
sortedIndexes = [sortedIndexes sortedArrayUsingSelector:@selector(compare:)];
for (NSNumber* mapIndex in sortedIndexes) {
NSString* guid = [map objectForKey:mapIndex];
[dataSource_ moveBookmarkWithGuid:guid toIndex:row];
row = [dataSource_ indexOfBookmarkWithGuid:guid] + 1;
}
// Save the (perhaps partial) order of the current view in the underlying
// model.
[dataSource_ pushOrderToUnderlyingModel];
// Remove the sorting order so that our change is not lost when data is
// reloaded. This will cause a sync so it must be done after pushing the
// local ordering to the underlying model.
if ([[tableView_ sortDescriptors] count] > 0) {
[tableView_ setSortDescriptors:[NSArray arrayWithObjects:nil]];
}
// The underlying model doesn't post a change notification for each bookmark
// move because it would be overwhelming so we must do it ourselves. This
// makes all other table views sync with the new order. First, add commands
// to rebuild the menus.
[[dataSource_ underlyingModel] rebuildMenus];
[[dataSource_ underlyingModel] postChangeNotification];
NSMutableIndexSet* newIndexes = [[[NSMutableIndexSet alloc] init] autorelease];
for (NSString* guid in guids) {
row = [dataSource_ indexOfBookmarkWithGuid:guid];
[newIndexes addIndex:row];
}
[tableView_ selectRowIndexes:newIndexes byExtendingSelection:NO];
[self reloadData];
return YES;
}
// End Drag drop -------------------------------
- (void)_addTag:(id)sender
{
int itemTag = [sender tag];
NSArray* allTags = [[[dataSource_ underlyingModel] allTags] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString* tag = [allTags objectAtIndex:itemTag];
[searchField_ setStringValue:[[NSString stringWithFormat:@"%@ %@",
[[searchField_ stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
tag] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
[self controlTextDidChange:nil];
}
- (void)_addTags:(NSArray*)tags toSearchField:(NSSearchField*)searchField
{
NSMenu *cellMenu = [[[NSMenu alloc] initWithTitle:@"Search Menu"]
autorelease];
NSMenuItem *item;
item = [[[NSMenuItem alloc] initWithTitle:@"Tags"
action:nil
keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setTag:-1];
[cellMenu insertItem:item atIndex:0];
NSArray* sortedTags = [tags sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for (int i = 0; i < [sortedTags count]; ++i) {
item = [[[NSMenuItem alloc] initWithTitle:[sortedTags objectAtIndex:i]
action:@selector(_addTag:)
keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setTag:i];
[cellMenu insertItem:item atIndex:i+1];
}
id searchCell = [searchField cell];
[searchCell setSearchMenuTemplate:cellMenu];
}
- (void)setUnderlyingDatasource:(BookmarkModel*)dataSource
{
[dataSource_ autorelease];
dataSource_ = [[BookmarkModelWrapper alloc] initWithModel:dataSource];
}
- (id)initWithFrame:(NSRect)frameRect
{
return [self initWithFrame:frameRect model:[BookmarkModel sharedInstance]];
}
- (id)initWithFrame:(NSRect)frameRect model:(BookmarkModel*)dataSource
{
self = [super initWithFrame:frameRect];
margin_ = kInterWidgetMargin;
[self setUnderlyingDatasource:dataSource];
debug = NO;
NSRect frame = [self frame];
NSRect searchFieldFrame;
searchFieldFrame.origin.x = 0;
searchFieldFrame.origin.y = frame.size.height - kSearchWidgetHeight;
searchFieldFrame.size.height = kSearchWidgetHeight;
searchFieldFrame.size.width = frame.size.width;
searchField_ = [[iTermSearchField alloc] initWithFrame:searchFieldFrame];
[self _addTags:[[dataSource_ underlyingModel] allTags] toSearchField:searchField_];
[searchField_ setDelegate:self];
[self addSubview:searchField_];
delegate_ = nil;
NSRect scrollViewFrame;
scrollViewFrame.origin.x = 0;
scrollViewFrame.origin.y = 0;
scrollViewFrame.size.width = frame.size.width;
scrollViewFrame.size.height =
frame.size.height - kSearchWidgetHeight - margin_;
scrollView_ = [[NSScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView_ setHasVerticalScroller:YES];
[self addSubview:scrollView_];
NSRect tableViewFrame;
tableViewFrame.origin.x = 0;
tableViewFrame.origin.y = 0;
tableViewFrame.size =
[NSScrollView contentSizeForFrameSize:scrollViewFrame.size
hasHorizontalScroller:NO
hasVerticalScroller:YES
borderType:[scrollView_ borderType]];
tableView_ = [[BookmarkTableView alloc] initWithFrame:tableViewFrame];
[tableView_ setParent:self];
[tableView_ registerForDraggedTypes:[NSArray arrayWithObject:BookmarkTableViewDataType]];
normalRowHeight_ = 21;
rowHeightWithTags_ = 29;
[tableView_ setRowHeight:rowHeightWithTags_];
[tableView_
setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList];
[tableView_ setAllowsColumnResizing:YES];
[tableView_ setAllowsColumnReordering:YES];
[tableView_ setAllowsColumnSelection:NO];
[tableView_ setAllowsEmptySelection:YES];
[tableView_ setAllowsMultipleSelection:NO];
[tableView_ setAllowsTypeSelect:NO];
[tableView_ setBackgroundColor:[NSColor whiteColor]];
starColumn_ = [[NSTableColumn alloc] initWithIdentifier:@"default"];
[starColumn_ setEditable:NO];
[starColumn_ setDataCell:[[NSImageCell alloc] initImageCell:nil]];
[starColumn_ setWidth:34];
[tableView_ addTableColumn:starColumn_];
tableColumn_ =
[[NSTableColumn alloc] initWithIdentifier:@"name"];
[tableColumn_ setEditable:NO];
[tableView_ addTableColumn:tableColumn_];
[scrollView_ setDocumentView:tableView_];
[tableView_ setDelegate:self];
[tableView_ setDataSource:self];
selectedGuids_ = [[NSMutableSet alloc] init];
[tableView_ setDoubleAction:@selector(onDoubleClick:)];
NSTableHeaderView* header = [[NSTableHeaderView alloc] init];
[tableView_ setHeaderView:header];
[[tableColumn_ headerCell] setStringValue:@"Name"];
[[starColumn_ headerCell] setStringValue:@"Default"];
[starColumn_ setWidth:[[starColumn_ headerCell] cellSize].width];
[tableView_ sizeLastColumnToFit];
[searchField_ setArrowHandler:tableView_];
[scrollView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[searchField_ setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(dataChangeNotification:)
name: @"iTermReloadAddressBook"
object: nil];
return self;
}
- (BookmarkModelWrapper*)dataSource
{
return dataSource_;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[dataSource_ release];
[selectedGuids_ release];
[super dealloc];
}
- (void)setDelegate:(NSObject<BookmarkTableDelegate> *)delegate
{
delegate_ = delegate;
}
#pragma mark NSTableView data source
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [dataSource_ numberOfBookmarks];
}
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
[dataSource_ setSortDescriptors:[aTableView sortDescriptors]];
[dataSource_ sort];
[dataSource_ pushOrderToUnderlyingModel];
[[dataSource_ underlyingModel] postChangeNotification];
// Update the sort indicator image for all columns.
NSArray* sortDescriptors = [dataSource_ sortDescriptors];
for (NSTableColumn* col in [aTableView tableColumns]) {
[aTableView setIndicatorImage:nil inTableColumn:col];
}
if ([sortDescriptors count] > 0) {
NSSortDescriptor* primarySortDesc = [sortDescriptors objectAtIndex:0];
[aTableView setIndicatorImage:([primarySortDesc ascending] ?
[NSImage imageNamed:@"NSAscendingSortIndicator"] :
[NSImage imageNamed:@"NSDescendingSortIndicator"])
inTableColumn:[aTableView tableColumnWithIdentifier:[primarySortDesc key]]];
}
[self reloadData];
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)rowIndex
{
Bookmark* bookmark = [dataSource_ bookmarkAtIndex:rowIndex];
NSArray* tags = [bookmark objectForKey:KEY_TAGS];
if ([tags count] == 0) {
return normalRowHeight_;
} else {
return rowHeightWithTags_;
}
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
Bookmark* bookmark = [dataSource_ bookmarkAtIndex:rowIndex];
if (aTableColumn == tableColumn_) {
NSColor* textColor;
if ([[tableView_ selectedRowIndexes] containsIndex:rowIndex]) {
textColor = [NSColor whiteColor];
} else {
textColor = [NSColor blackColor];
}
NSDictionary* plainAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
textColor, NSForegroundColorAttributeName,
[[aTableColumn dataCell] font], NSFontAttributeName,
nil];
NSDictionary* smallAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
textColor, NSForegroundColorAttributeName,
[NSFont systemFontOfSize:10], NSFontAttributeName,
nil];
NSString *name = [NSString stringWithFormat:@"%@\n", [bookmark objectForKey:KEY_NAME]];
NSString* tags = [[bookmark objectForKey:KEY_TAGS] componentsJoinedByString:@", "];
NSMutableAttributedString *theAttributedString = [[[NSMutableAttributedString alloc] initWithString:name
attributes:plainAttributes] autorelease];
[theAttributedString appendAttributedString:[[[NSAttributedString alloc] initWithString:tags
attributes:smallAttributes] autorelease]];
return theAttributedString;
} else if (aTableColumn == commandColumn_) {
if (![[bookmark objectForKey:KEY_CUSTOM_COMMAND] isEqualToString:@"Yes"]) {
return @"Login shell";
} else {
return [bookmark objectForKey:KEY_COMMAND];
}
} else if (aTableColumn == shortcutColumn_) {
NSString* key = [bookmark objectForKey:KEY_SHORTCUT];
if ([key length]) {
return [NSString stringWithFormat:@"^⌘%@", [bookmark objectForKey:KEY_SHORTCUT]];
} else {
return @"";
}
} else if (aTableColumn == starColumn_) {
// FIXME: use imageNamed and clean up drawing code
static NSImage* starImage;
if (!starImage) {
NSString* starFile = [[NSBundle bundleForClass:[self class]]
pathForResource:@"star-gold24"
ofType:@"png"];
starImage = [[NSImage alloc] initWithContentsOfFile:starFile];
}
NSImage *image = [[[NSImage alloc] init] autorelease];
NSSize size;
size.width = [aTableColumn width];
size.height = rowHeightWithTags_;
[image setSize:size];
NSRect rect;
rect.origin.x = 0;
rect.origin.y = 0;
rect.size = size;
[image lockFocus];
if ([[bookmark objectForKey:KEY_GUID] isEqualToString:[[[BookmarkModel sharedInstance] defaultBookmark] objectForKey:KEY_GUID]]) {
NSPoint destPoint;
destPoint.x = (size.width - [starImage size].width) / 2;
destPoint.y = (rowHeightWithTags_ - [starImage size].height) / 2;
[starImage drawAtPoint:destPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
[image unlockFocus];
return image;
}
return @"";
}
// Delegate methods
- (void)tableView:(NSTableView *)aTableView didClickTableColumn:(NSTableColumn *)aTableColumn
{
NSMutableArray* newSortDescriptors = [NSMutableArray arrayWithArray:[tableView_ sortDescriptors]];
BOOL done = NO;
BOOL ascending = YES;
// Find the existing sort descriptor for the clicked-on column and move it
// to the front.
for (int i = 0; i < [newSortDescriptors count]; ++i) {
NSSortDescriptor* desc = [newSortDescriptors objectAtIndex:i];
if ([[desc key] isEqualToString:[aTableColumn identifier]]) {
ascending = ![desc ascending];
[newSortDescriptors removeObjectAtIndex:i];
[newSortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:[aTableColumn identifier]
ascending:ascending] autorelease]
atIndex:0];
done = YES;
break;
}
}
if (!done) {
// This column was not previously sorted. Add it to the head of the array.
[newSortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:[aTableColumn identifier]
ascending:YES] autorelease]
atIndex:0];
}
[tableView_ setSortDescriptors:newSortDescriptors];
[aTableView reloadData];
}
- (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTableView
{
if (delegate_ && [delegate_ respondsToSelector:@selector(bookmarkTableSelectionWillChange:)]) {
[delegate_ bookmarkTableSelectionWillChange:self];
}
return YES;
}
- (void)tableViewSelectionIsChanging:(NSNotification *)aNotification
{
// Mouse is being dragged across rows
if (delegate_ && [delegate_ respondsToSelector:@selector(bookmarkTableSelectionDidChange:)]) {
[delegate_ bookmarkTableSelectionDidChange:self];
}
[selectedGuids_ release];
selectedGuids_ = [self selectedGuids];
[selectedGuids_ retain];
}
- (void)setHasSelection:(BOOL)value
{
// Placeholder for key-value observation
}
- (BOOL)hasSelection
{
return [tableView_ numberOfSelectedRows] > 0;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
// There was a click on a row
if (delegate_ && [delegate_ respondsToSelector:@selector(bookmarkTableSelectionDidChange:)]) {
[delegate_ bookmarkTableSelectionDidChange:self];
}
[selectedGuids_ release];
selectedGuids_ = [self selectedGuids];
[selectedGuids_ retain];
// tweak key value observation
[self setHasSelection:[selectedGuids_ count] > 0];
}
- (int)selectedRow
{
return [tableView_ selectedRow];
}
- (void)reloadData
{
[self _addTags:[[dataSource_ underlyingModel] allTags] toSearchField:searchField_];
[dataSource_ sync];
[tableView_ reloadData];
if (delegate_ && ![selectedGuids_ isEqualToSet:[self selectedGuids]]) {
[selectedGuids_ release];
selectedGuids_ = [self selectedGuids];
[selectedGuids_ retain];
if ([delegate_ respondsToSelector:@selector(bookmarkTableSelectionDidChange:)]) {
[delegate_ bookmarkTableSelectionDidChange:self];
}
}
}
- (void)selectRowIndex:(int)theRow
{
NSIndexSet* indexes = [NSIndexSet indexSetWithIndex:theRow];
[tableView_ selectRowIndexes:indexes byExtendingSelection:NO];
[tableView_ scrollRowToVisible:theRow];
}
- (void)selectRowByGuid:(NSString*)guid
{
int theRow = [dataSource_ indexOfBookmarkWithGuid:guid];
if (theRow == -1) {
[self deselectAll];
return;
}
[self selectRowIndex:theRow];
}
- (int)numberOfRows
{
return [dataSource_ numberOfBookmarks];
}
- (void)hideSearch
{
[searchField_ setStringValue:@""];
[searchField_ setHidden:YES];
NSRect frame = [self frame];
NSRect scrollViewFrame;
scrollViewFrame.origin.x = 0;
scrollViewFrame.origin.y = 0;
scrollViewFrame.size = frame.size;
[scrollView_ setFrame:scrollViewFrame];
NSRect tableViewFrame;
tableViewFrame.origin.x = 0;
tableViewFrame.origin.y = 0;;
tableViewFrame.size =
[NSScrollView contentSizeForFrameSize:scrollViewFrame.size
hasHorizontalScroller:NO
hasVerticalScroller:YES
borderType:[scrollView_ borderType]];
[tableView_ setFrame:tableViewFrame];
[tableView_ sizeLastColumnToFit];
}
- (void)allowEmptySelection
{
[tableView_ setAllowsEmptySelection:YES];
}
- (void)allowMultipleSelections
{
[tableView_ setAllowsMultipleSelection:YES];
}
- (void)deselectAll
{
[tableView_ deselectAll:self];
}
- (NSString*)selectedGuid
{
int row = [self selectedRow];
if (row < 0) {
return nil;
}
Bookmark* bookmark = [dataSource_ bookmarkAtIndex:row];
if (!bookmark) {
return nil;
}
return [bookmark objectForKey:KEY_GUID];
}
- (NSArray *)orderedSelectedGuids
{
NSMutableArray* result = [[[NSMutableArray alloc] init] autorelease];
NSIndexSet* indexes = [tableView_ selectedRowIndexes];
NSUInteger theIndex = [indexes firstIndex];
while (theIndex != NSNotFound) {
Bookmark* bookmark = [dataSource_ bookmarkAtIndex:theIndex];
if (bookmark) {
[result addObject:[bookmark objectForKey:KEY_GUID]];
}
theIndex = [indexes indexGreaterThanIndex:theIndex];
}
return result;
}
- (NSSet*)selectedGuids
{
return [NSSet setWithArray:[self orderedSelectedGuids]];
}
- (void)controlTextDidChange:(NSNotification *)aNotification
{
// search field changed
[dataSource_ setFilter:[searchField_ stringValue]];
[self reloadData];
if ([self selectedRow] < 0 && [self numberOfRows] > 0) {
[self selectRowIndex:0];
[tableView_ scrollRowToVisible:0];
}
}
- (void)multiColumns
{
shortcutColumn_ = [[NSTableColumn alloc] initWithIdentifier:@"shortcut"];
[shortcutColumn_ setEditable:NO];
[shortcutColumn_ setWidth:50];
[tableView_ addTableColumn:shortcutColumn_];
commandColumn_ = [[NSTableColumn alloc] initWithIdentifier:@"command"];
[commandColumn_ setEditable:NO];
[tableView_ addTableColumn:commandColumn_];
[tableColumn_ setWidth:250];
[[shortcutColumn_ headerCell] setStringValue:@"Shortcut"];
[[commandColumn_ headerCell] setStringValue:@"Command"];
[tableView_ sizeLastColumnToFit];
}
- (void)dataChangeNotification:(id)sender
{
[self reloadData];
}
- (void)onDoubleClick:(id)sender
{
if (delegate_ && [delegate_ respondsToSelector:@selector(bookmarkTableRowSelected:)]) {
[delegate_ bookmarkTableRowSelected:self];
}
}
- (void)eraseQuery
{
[searchField_ setStringValue:@""];
[self reloadData];
}
- (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize
{
NSRect frame = [self frame];
NSRect searchFieldFrame;
searchFieldFrame.origin.x = 0;
searchFieldFrame.origin.y = frame.size.height - kSearchWidgetHeight;
searchFieldFrame.size.height = kSearchWidgetHeight;
searchFieldFrame.size.width = frame.size.width;
[searchField_ setFrame:searchFieldFrame];
NSRect scrollViewFrame;
scrollViewFrame.origin.x = 0;
scrollViewFrame.origin.y = 0;
scrollViewFrame.size.width = frame.size.width;
scrollViewFrame.size.height =
frame.size.height - kSearchWidgetHeight - margin_;
[scrollView_ setFrame:scrollViewFrame];
NSRect tableViewFrame = [tableView_ frame];
tableViewFrame.origin.x = 0;
tableViewFrame.origin.y = 0;;
NSSize temp =
[NSScrollView contentSizeForFrameSize:scrollViewFrame.size
hasHorizontalScroller:NO
hasVerticalScroller:YES
borderType:[scrollView_ borderType]];
tableViewFrame.size.width = temp.width;
[tableView_ setFrame:tableViewFrame];
}
- (void)turnOnDebug
{
NSLog(@"Debugging object at %x. Current count is %d", (void*)self, [self retainCount]);
debug=YES;
}
- (NSTableView*)tableView
{
return tableView_;
}
- (id)delegate
{
return delegate_;
}
- (void)setFont:(NSFont *)theFont
{
for (NSTableColumn *col in [tableView_ tableColumns]) {
[[col dataCell] setFont:theFont];
}
NSLayoutManager* layoutManager = [[NSLayoutManager alloc] init];
[layoutManager autorelease];
normalRowHeight_ = [layoutManager defaultLineHeightForFont:theFont];
rowHeightWithTags_ = normalRowHeight_ + [layoutManager defaultLineHeightForFont:[NSFont systemFontOfSize:10]];
[tableView_ setRowHeight:normalRowHeight_];
if ([theFont pointSize] < 13) {
[[searchField_ cell] setFont:theFont];
[[searchField_ cell] setControlSize:NSSmallControlSize];
[searchField_ sizeToFit];
margin_ = 5;
[self resizeSubviewsWithOldSize:self.frame.size];
}
}
- (void)disableArrowHandler
{
[searchField_ setArrowHandler:nil];
}
@end