forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FutureMethods.m
252 lines (219 loc) · 8.61 KB
/
FutureMethods.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
//
// FutureMethods.m
// iTerm
//
// Created by George Nachman on 8/29/11.
// Copyright 2011 Georgetech. All rights reserved.
//
#import "FutureMethods.h"
const int FutureNSWindowCollectionBehaviorStationary = (1 << 4); // value stolen from 10.6 SDK
@implementation NSWindow (Future)
- (void)futureSetRestorable:(BOOL)value
{
if ([self respondsToSelector:@selector(setRestorable:)]) {
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:@selector(setRestorable:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:self];
[inv setSelector:@selector(setRestorable:)];
[inv setArgument:&value atIndex:2];
[inv invoke];
}
}
- (void)futureSetRestorationClass:(Class)class
{
if ([self respondsToSelector:@selector(setRestorationClass:)]) {
[self performSelector:@selector(setRestorationClass:) withObject:class];
}
}
- (void)futureInvalidateRestorableState
{
if ([self respondsToSelector:@selector(invalidateRestorableState)]) {
[self performSelector:@selector(invalidateRestorableState)];
}
}
@end
@implementation NSView (Future)
- (void)futureSetAcceptsTouchEvents:(BOOL)value
{
if ([self respondsToSelector:@selector(setAcceptsTouchEvents:)]) {
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:@selector(setAcceptsTouchEvents:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:self];
[inv setSelector:@selector(setAcceptsTouchEvents:)];
[inv setArgument:&value atIndex:2];
[inv invoke];
}
}
- (void)futureSetWantsRestingTouches:(BOOL)value
{
if ([self respondsToSelector:@selector(setWantsRestingTouches:)]) {
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:@selector(setWantsRestingTouches:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:self];
[inv setSelector:@selector(setWantsRestingTouches:)];
[inv setArgument:&value atIndex:2];
[inv invoke];
}
}
- (NSRect)futureConvertRectToScreen:(NSRect)rect
{
if ([[self window] respondsToSelector:@selector(convertRectToScreen:)]) {
NSMethodSignature *sig = [[[self window] class] instanceMethodSignatureForSelector:@selector(convertRectToScreen:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:[self window]];
[inv setSelector:@selector(convertRectToScreen:)];
[inv setArgument:&rect atIndex:2];
[inv invoke];
NSRect result;
[inv getReturnValue:&result];
return result;
} else {
NSPoint p1 = [self convertPointToBase:rect.origin];
NSPoint p2 = [self convertPointToBase:NSMakePoint(rect.origin.x + rect.size.width, rect.origin.y + rect.size.width)];
return NSMakeRect(MIN(p1.x, p2.x), MIN(p1.y, p2.y), fabs(p1.x - p2.x), fabs(p1.y - p2.y));
}
}
- (NSRect)futureConvertRectFromScreen:(NSRect)rect {
if ([[self window] respondsToSelector:@selector(convertRectFromScreen:)]) {
NSMethodSignature *sig = [[[self window] class] instanceMethodSignatureForSelector:@selector(convertRectFromScreen:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:[self window]];
[inv setSelector:@selector(convertRectFromScreen:)];
[inv setArgument:&rect atIndex:2];
[inv invoke];
NSRect result;
[inv getReturnValue:&result];
return result;
} else {
NSPoint p1 = [self convertPointFromBase:rect.origin];
NSPoint p2 = [self convertPointFromBase:NSMakePoint(rect.origin.x + rect.size.width, rect.origin.y + rect.size.width)];
return NSMakeRect(MIN(p1.x, p2.x), MIN(p1.y, p2.y), fabs(p1.x - p2.x), fabs(p1.y - p2.y));
}
}
@end
@implementation NSEvent (Future)
- (NSArray *)futureTouchesMatchingPhase:(int)phase inView:(NSView *)view
{
if ([self respondsToSelector:@selector(touchesMatchingPhase:inView:)]) {
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:@selector(touchesMatchingPhase:inView:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:self];
[inv setSelector:@selector(touchesMatchingPhase:inView:)];
[inv setArgument:&phase atIndex:2];
[inv setArgument:&view atIndex:3];
[inv invoke];
NSArray *result;
[inv getReturnValue:&result];
return result;
} else {
return [NSArray array];
}
}
@end
static FutureNSScrollerStyle GetScrollerStyle(id theObj)
{
NSMethodSignature *sig = [[NSScroller class] instanceMethodSignatureForSelector:@selector(scrollerStyle)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:theObj];
[inv setSelector:@selector(scrollerStyle)];
[inv invoke];
FutureNSScrollerStyle result;
[inv getReturnValue:&result];
return result;
}
@implementation NSScroller (Future)
- (FutureNSScrollerStyle)futureScrollerStyle
{
if ([self respondsToSelector:@selector(scrollerStyle)]) {
return GetScrollerStyle(self);
} else {
return FutureNSScrollerStyleLegacy;
}
}
@end
@implementation NSScrollView (Future)
- (FutureNSScrollerStyle)futureScrollerStyle
{
if ([self respondsToSelector:@selector(scrollerStyle)]) {
return GetScrollerStyle(self);
} else {
return FutureNSScrollerStyleLegacy;
}
}
@end
@implementation CIImage (Future)
@end
@implementation NSObject (Future)
- (BOOL)performSelectorReturningBool:(SEL)selector withObjects:(NSArray *)objects {
NSMethodSignature *mySignature = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:mySignature];
[myInvocation setTarget:self];
[myInvocation setSelector:selector];
void *pointers[objects.count];
for (int i = 0; i < objects.count; i++) {
pointers[i] = [objects objectAtIndex:i];
[myInvocation setArgument:&pointers[i] // pointer to object
atIndex:i];
}
[myInvocation invoke];
BOOL result;
[myInvocation getReturnValue:&result];
return result;
}
- (void)performSelector:(SEL)selector takingNSInteger:(NSInteger)arg {
NSMethodSignature *mySignature = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:mySignature];
[myInvocation setTarget:self];
[myInvocation setSelector:selector];
[myInvocation setArgument:&arg
atIndex:2];
[myInvocation invoke];
}
@end
@implementation NSScroller (future)
- (void)futureSetKnobStyle:(NSInteger)newKnobStyle {
if ([self respondsToSelector:@selector(setKnobStyle:)]) {
[self performSelector:@selector(setKnobStyle:) takingNSInteger:newKnobStyle];
}
}
@end
static void *GetFunctionByName(NSString *library, char *func) {
CFBundleRef bundle;
CFURLRef bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef) library, kCFURLPOSIXPathStyle, true);
CFStringRef functionName = CFStringCreateWithCString(kCFAllocatorDefault, func, kCFStringEncodingASCII);
bundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
void *f = NULL;
if (bundle) {
f = CFBundleGetFunctionPointerForName(bundle, functionName);
CFRelease(bundle);
}
CFRelease(functionName);
CFRelease(bundleURL);
return f;
}
CGSSetWindowBackgroundBlurRadiusFunction* GetCGSSetWindowBackgroundBlurRadiusFunction(void) {
static BOOL tried = NO;
static CGSSetWindowBackgroundBlurRadiusFunction *function = NULL;
if (!tried) {
function = GetFunctionByName(@"/System/Library/Frameworks/ApplicationServices.framework",
"CGSSetWindowBackgroundBlurRadius");
tried = YES;
}
return function;
}
CTFontDrawGlyphsFunction* GetCTFontDrawGlyphsFunction(void) {
static BOOL tried = NO;
static CTFontDrawGlyphsFunction *function = NULL;
if (!tried) {
// This works in 10.8
function = GetFunctionByName(@"/System/Library/Frameworks/CoreText.framework",
"CTFontDrawGlyphs");
if (!function) {
// This works in 10.7 and earlier versions won't have it.
function = GetFunctionByName(@"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreText.framework",
"CTFontDrawGlyphs");
}
tried = YES;
}
return function;
}