forked from macvim-dev/macvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMWindow.m
More file actions
246 lines (203 loc) · 7.21 KB
/
MMWindow.m
File metadata and controls
246 lines (203 loc) · 7.21 KB
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
/* vi:set ts=8 sts=4 sw=4 ft=objc:
*
* VIM - Vi IMproved by Bram Moolenaar
* MacVim GUI port by Bjorn Winckler
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* MMWindow
*
* A normal window with a (possibly hidden) tabline separator at the top of the
* content view.
*
* The main point of this class is for the window controller to be able to call
* contentRectForFrameRect: without having to worry about whether the separator
* is visible or not.
*
* This is a bit of a hack, it would be nicer to be able to leave the content
* view alone, but as it is the tabline separator is a subview of the content
* view. Since we want to pretend that the content view does not contain the
* separator this leads to some dangerous situations. For instance, calling
* [window setContentMinSize:size] when the separator is visible results in
* size != [window contentMinSize], since the latter is one pixel higher than
* 'size'.
*/
#import "MMWindow.h"
#import "Miscellaneous.h"
#import "CGSInternal/CGSWindow.h"
typedef CGError CGSSetWindowBackgroundBlurRadiusFunction(CGSConnectionID cid, CGSWindowID wid, NSUInteger blur);
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;
}
static 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;
}
@implementation MMWindow
- (id)initWithContentRect:(NSRect)rect
styleMask:(NSUInteger)style
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
self = [super initWithContentRect:rect
styleMask:style
backing:bufferingType
defer:flag];
if (!self) return nil;
[self setReleasedWhenClosed:NO];
NSRect tabSepRect = { {0, rect.size.height - 1}, {rect.size.width, 1} };
tablineSeparator = [[NSBox alloc] initWithFrame:tabSepRect];
[tablineSeparator setBoxType:NSBoxSeparator];
[tablineSeparator setHidden:YES];
[tablineSeparator setAutoresizingMask:NSViewWidthSizable|NSViewMinYMargin];
NSView *contentView = [self contentView];
[contentView setAutoresizesSubviews:YES];
[contentView addSubview:tablineSeparator];
// NOTE: Vim needs to process mouse moved events, so enable them here.
[self setAcceptsMouseMovedEvents:YES];
return self;
}
- (void)dealloc
{
ASLogDebug(@"");
// TODO: Is there any reason why we would want the following call?
//[tablineSeparator removeFromSuperviewWithoutNeedingDisplay];
[tablineSeparator release]; tablineSeparator = nil;
[super dealloc];
}
- (BOOL) canBecomeMainWindow {
return YES;
}
- (BOOL) canBecomeKeyWindow {
return YES;
}
- (BOOL)hideTablineSeparator:(BOOL)hide
{
BOOL isHidden = [tablineSeparator isHidden];
[tablineSeparator setHidden:hide];
// Return YES if visibility state was toggled, NO if it was unchanged.
return isHidden != hide;
}
- (NSRect)contentRectForFrameRect:(NSRect)frame
{
NSRect rect = [super contentRectForFrameRect:frame];
if (![tablineSeparator isHidden])
--rect.size.height;
return rect;
}
- (NSRect)frameRectForContentRect:(NSRect)rect
{
NSRect frame = [super frameRectForContentRect:rect];
if (![tablineSeparator isHidden])
++frame.size.height;
return frame;
}
- (void)setContentMinSize:(NSSize)size
{
if (![tablineSeparator isHidden])
++size.height;
[super setContentMinSize:size];
}
- (void)setContentMaxSize:(NSSize)size
{
if (![tablineSeparator isHidden])
++size.height;
[super setContentMaxSize:size];
}
- (void)setContentSize:(NSSize)size
{
if (![tablineSeparator isHidden])
++size.height;
[super setContentSize:size];
}
- (void)setBlurRadius:(int)radius
{
[MMWindow setBlurRadius:radius onWindow:self];
}
+ (void)setBlurRadius:(int)radius onWindow:(NSWindow *)win
{
if (radius >= 0) {
CGSConnectionID con = CGSMainConnectionID();
if (!con) {
return;
}
CGSSetWindowBackgroundBlurRadiusFunction* function = GetCGSSetWindowBackgroundBlurRadiusFunction();
if (function) {
function(con, (int)[win windowNumber], radius);
}
}
}
- (void)performClose:(id)sender
{
id wc = [self windowController];
if ([wc respondsToSelector:@selector(performClose:)])
[wc performClose:sender];
else
[super performClose:sender];
}
/// Validates whether the menu item should be enabled or not.
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
// This class only really have one action that's bound from Vim
if ([item action] == @selector(performClose:)) {
return [item tag];
}
// Since this is a subclass of NSWindow, it has a bunch of auto-populated
// menu from the OS. Just pass it off to the superclass to let it handle it.
return [super validateMenuItem:item];
}
- (IBAction)zoom:(id)sender
{
// NOTE: We shortcut the usual zooming behavior and provide custom zooming
// in the window controller.
// (Use performSelector:: to avoid compilation warning.)
[[self delegate] performSelector:@selector(zoom:) withObject:sender];
}
- (IBAction)toggleFullScreen:(id)sender
{
// This is an NSWindow method used to enter full-screen since OS X 10.7
// Lion. We override it so that we can interrupt and pass this on to Vim
// first, as it is full-screen aware (":set fullscreen") and it's better to
// only have one path to enter full screen. For non-native full screen this
// does mean this button will now enter non-native full screen instead of
// native one.
// To get to the original method (and enter Lion full-screen) we need to
// call realToggleFullScreen: defined below.
// (Use performSelector:: to avoid compilation warning.)
[[self delegate] performSelector:@selector(invFullScreen:) withObject:nil];
}
- (IBAction)realToggleFullScreen:(id)sender
{
// See toggleFullScreen: comment above.
[super toggleFullScreen:sender];
}
- (void)setToolbar:(NSToolbar *)toolbar
{
if ([[NSUserDefaults standardUserDefaults]
boolForKey:MMNoTitleBarWindowKey]) {
// MacVim can't have toolbar with No title bar setting.
return;
}
[super setToolbar:toolbar];
}
@end // MMWindow