-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPTYWindow.m
More file actions
223 lines (180 loc) · 5.41 KB
/
PTYWindow.m
File metadata and controls
223 lines (180 loc) · 5.41 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
/* -*- mode:objc -*- */
/* $Id: PTYWindow.m,v 1.17 2008-09-24 22:35:39 yfabian Exp $ */
/* Incorporated into iTerm.app by Ujwal S. Setlur */
/*
** PTYWindow.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: NSWindow subclass. Implements transparency.
**
** 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
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import <iTerm/iTerm.h>
#import <iTerm/PTYWindow.h>
#import <iTerm/PreferencePanel.h>
#import <iTerm/PseudoTerminal.h>
#import <iTerm/iTermController.h>
#define DEBUG_METHOD_ALLOC 0
#define DEBUG_METHOD_TRACE 0
#define DEBUG_WINDOW_LAYOUT 0
@implementation PTYWindow
- (void) dealloc
{
#if DEBUG_METHOD_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
[drawer release];
[super dealloc];
}
- initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
#if DEBUG_METHOD_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
if ((self = [super initWithContentRect:contentRect
styleMask:aStyle
backing:bufferingType
defer:flag])
!= nil)
{
[self setAlphaValue:0.9999];
blurFilter = 0;
layoutDone = NO;
}
return self;
}
- (int)screenNumber
{
return [[[[self screen] deviceDescription] objectForKey:@"NSScreenNumber"] intValue];
}
- (void)smartLayout
{
NSEnumerator* iterator;
int currentScreen = [self screenNumber];
NSRect screenRect = [[self screen] visibleFrame];
// Get a list of relevant windows, same screen & workspace
NSMutableArray* windows = [[NSMutableArray alloc] init];
iterator = [[[iTermController sharedInstance] terminals] objectEnumerator];
PseudoTerminal* term;
while(term = [iterator nextObject]) {
PTYWindow* otherWindow = (PTYWindow*)[term window];
if(otherWindow == self) continue;
int otherScreen = [otherWindow screenNumber];
if(otherScreen != currentScreen) continue;
[windows addObject:otherWindow];
}
// Find the spot on screen with the lowest window intersection
float bestIntersect = INFINITY;
NSRect bestFrame = [self frame];
NSRect placementRect = NSMakeRect(
screenRect.origin.x,
screenRect.origin.y,
screenRect.size.width-[self frame].size.width,
screenRect.size.height-[self frame].size.height
);
for(int x = 0; x < placementRect.size.width/2; x += 50) {
for(int y = 0; y < placementRect.size.height/2; y += 50) {
NSRect testRects[4] = {[self frame]};
// Top Left
testRects[0].origin.x = placementRect.origin.x + x;
testRects[0].origin.y = placementRect.origin.y + placementRect.size.height - y;
// Top Right
testRects[1] = testRects[0];
testRects[1].origin.x = placementRect.origin.x + placementRect.size.width - x;
// Bottom Left
testRects[2] = testRects[0];
testRects[2].origin.y = placementRect.origin.y + y;
// Bottom Right
testRects[3] = testRects[1];
testRects[3].origin.y = placementRect.origin.y + y;
for(int i = 0; i < sizeof(testRects)/sizeof(NSRect); i++) {
iterator = [windows objectEnumerator];
PTYWindow* other;
float badness = 0.0f;
while(other = [iterator nextObject]) {
NSRect otherFrame = [other frame];
NSRect intersection = NSIntersectionRect(testRects[i], otherFrame);
badness += intersection.size.width * intersection.size.height;
}
#if DEBUG_WINDOW_LAYOUT
static const char const * names[] = {"TL", "TR", "BL", "BR"};
NSLog(@"%s: testRect:%@, bad:%.2f", names[i], NSStringFromRect(testRects[i]), badness);
#endif
if(badness < bestIntersect) {
bestIntersect = badness;
bestFrame = testRects[i];
}
// Shortcut if we've found an empty spot
if(bestIntersect == 0) {
goto end;
}
}
}
}
end:
[windows release];
[super setFrameOrigin:bestFrame.origin];
}
- (void)setLayoutDone
{
layoutDone = YES;
}
- (void)makeKeyAndOrderFront:(id)sender
{
if(!layoutDone) {
layoutDone = YES;
[[self delegate] windowWillShowInitial];
}
[super makeKeyAndOrderFront:sender];
}
- (void)toggleToolbarShown:(id)sender
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYWindow toggleToolbarShown]",
__FILE__, __LINE__);
#endif
id delegate = [self delegate];
// Let our delegate know
if([delegate conformsToProtocol: @protocol(PTYWindowDelegateProtocol)])
[delegate windowWillToggleToolbarVisibility: self];
[super toggleToolbarShown: sender];
// Let our delegate know
if([delegate conformsToProtocol: @protocol(PTYWindowDelegateProtocol)])
[delegate windowDidToggleToolbarVisibility: self];
}
- (NSDrawer *) drawer
{
return (drawer);
}
- (void) setDrawer: (NSDrawer *) aDrawer
{
[aDrawer retain];
[drawer release];
drawer = aDrawer;
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
@end