-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYListPicker.m
172 lines (147 loc) · 5.09 KB
/
SSYListPicker.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
#import "SSYListPicker.h"
#import "SSYLabelledList.h"
#import "NSString+LocalizeSSY.h"
#import "NS(Attributed)String+Geometrics.h"
NSString* constKeyDidEndTarget = @"didEndTarget" ;
NSString* constKeyDidEndSelectorName = @"didEndSelectorName" ;
NSString* constKeyDidEndUserInfo = @"didEndUserInfo" ;
NSString* constKeyDidCancelInvocation = @"didCancelInvocation" ;
@interface SSYListPicker ()
@property (retain) SSYAlert* alert ;
@end
@implementation SSYListPicker
@synthesize alert = m_alert ;
+ (SSYListPicker*)listPicker {
return [[[SSYListPicker alloc] init] autorelease] ;
}
- (void)dealloc {
[m_alert release] ;
[super dealloc] ;
}
- (void)userPickFromList:(NSArray*)displayNames
toolTips:(NSArray*)toolTips
lineBreakMode:(NSLineBreakMode)lineBreakMode
message:(NSString*)message
allowMultipleSelection:(BOOL)allowMultipleSelection
allowEmptySelection:(BOOL)allowEmptySelection
button1Title:(NSString*)button1Title
button2Title:(NSString*)button2Title
initialPicks:(NSIndexSet*)initialPicks
windowTitle:(NSString*)windowTitle
alert:(SSYAlert*)alert
mode:(SSYAlertMode)mode
didEndTarget:(id)didEndTarget
didEndSelector:(SEL)didEndSelector
didEndUserInfo:(id)didEndUserInfo
didCancelInvocation:(NSInvocation*)didCancelInvocation {
if (
allowMultipleSelection
&&
([displayNames count] > 1)
) {
message = [message stringByAppendingFormat:
@"\n\n%@",
[NSString localizeFormat:@"selectMultipleHow", 0x2318]
] ;
}
if (!button1Title) {
button1Title = [NSString localize:@"ok"] ;
}
if (!button2Title) {
button2Title = [NSString localize:@"cancel"] ;
}
CGFloat width = 0.0 ;
for (NSString* displayName in displayNames) {
width = MAX(width, [displayName widthForHeight:20.0
font:[SSYLabelledList tableFont]]) ;
}
width = MAX(width, 16 * sqrt([message length])) ;
if (!alert) {
alert = [SSYAlert alert] ;
}
[self setAlert:alert] ;
SSYLabelledList* list = [SSYLabelledList listWithLabel:message
choices:displayNames
allowsMultipleSelection:allowMultipleSelection
allowsEmptySelection:allowEmptySelection
toolTips:toolTips
lineBreakMode:lineBreakMode
maxTableHeight:500.0] ;
[list setSelectedIndexes:initialPicks] ;
[list setTableViewDelegate:self] ;
[alert addOtherSubview:list
atIndex:0] ;
[alert setWindowTitle:windowTitle] ;
[alert setButton1Title:button1Title] ;
[alert setButton2Title:button2Title] ;
[alert setRightColumnMinimumWidth:MIN(width, 700)] ;
[alert setIconStyle:SSYAlertIconInformational] ;
[alert setClickTarget:self] ;
[alert setClickSelector:@selector(handleClickInAlert:)] ;
// I think this was added in BookMacster 1.7.3 or, more likely, 1.8. I forgot why, except that it was definitely needed…
[NSApp activateIgnoringOtherApps:YES] ;
NSMutableDictionary* didEndInfo = [NSMutableDictionary dictionary] ;
[didEndInfo setValue:didEndTarget
forKey:constKeyDidEndTarget] ;
[didEndInfo setValue:NSStringFromSelector(didEndSelector)
forKey:constKeyDidEndSelectorName] ;
[didEndInfo setValue:didEndUserInfo
forKey:constKeyDidEndUserInfo] ;
[didEndInfo setValue:didCancelInvocation
forKey:constKeyDidCancelInvocation] ;
[alert setClickObject:didEndInfo] ;
[alert display] ;
switch (mode) {
case SSYAlertModeNonBlocking:
break ;
case SSYAlertModeModalSession:
[alert runModalSession] ;
break ;
case SSYAlertModeModalDialog:
[alert runModalDialog] ;
break ;
}
}
- (void)handleClickInAlert:(SSYAlert*)alert {
BOOL done = NO ;
BOOL proceed = NO ;
NSIndexSet* selectedIndexSet = nil ;
[alert endModalSession] ;
if ([alert alertReturn] == NSAlertFirstButtonReturn) {
// Clicked "OK" or "Done"
selectedIndexSet = [[[alert otherSubviews] objectAtIndex:0] selectedIndexes] ;
done = YES ;
proceed = YES ;
}
else if ([alert alertReturn] == NSAlertSecondButtonReturn) {
// Clicked "Cancel"
done = YES ;
}
// Need to grab this before popping the configuration
NSDictionary* clickDic = [alert clickObject] ;
if (done) {
// Needed to break retain cycle, or possibly no longer necessary?
[self setAlert:nil] ;
}
if (proceed) {
id target = [clickDic objectForKey:constKeyDidEndTarget] ;
if (target) {
SEL selector = NSSelectorFromString([clickDic objectForKey:constKeyDidEndSelectorName]) ;
[target performSelector:selector
withObject:selectedIndexSet
withObject:[clickDic objectForKey:constKeyDidEndUserInfo]] ;
}
}
if (done && !proceed) {
[[clickDic objectForKey:constKeyDidCancelInvocation] invoke] ;
}
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
NSTableView* tableView = [notification object] ;
SSYAlert* alert = [[[tableView enclosingScrollView] window] windowController] ;
BOOL weHaveASelection = ([[tableView selectedRowIndexes] count] > 0) ;
BOOL ok = (weHaveASelection || [tableView allowsEmptySelection]) ;
[alert setButton1Enabled:ok] ;
[alert setButton3Enabled:ok] ;
}
@end