forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PseudoTerminal.m
4519 lines (3955 loc) · 161 KB
/
PseudoTerminal.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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:objc -*-
// $Id: PseudoTerminal.m,v 1.437 2009-02-06 15:07:23 delx Exp $
//
/*
** PseudoTerminal.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: Session and window controller for iTerm.
**
** 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.
*/
// Debug option
#define DEBUG_ALLOC 0
#define DEBUG_METHOD_TRACE 0
// For beta 1, we're trying to keep the IR bar from mysteriously disappearing
// when live mode is entered.
// #define HIDE_IR_WHEN_LIVE_VIEW_ENTERED
#define WINDOW_NAME @"iTerm Window %d"
#import <iTerm/iTerm.h>
#import <iTerm/PseudoTerminal.h>
#import <iTerm/PTYScrollView.h>
#import <iTerm/NSStringITerm.h>
#import <iTerm/PTYSession.h>
#import <iTerm/VT100Screen.h>
#import <iTerm/PTYTabView.h>
#import <iTerm/PreferencePanel.h>
#import <iTerm/iTermController.h>
#import <iTerm/PTYTask.h>
#import <iTerm/PTYTextView.h>
#import <iTerm/PseudoTerminal.h>
#import <iTerm/VT100Terminal.h>
#import <iTerm/VT100Screen.h>
#import <iTerm/PTYSession.h>
#import <iTerm/PTToolbarController.h>
#import <iTerm/ITAddressBookMgr.h>
#import <iTerm/iTermApplicationDelegate.h>
#import "FakeWindow.h"
#import <PSMTabBarControl.h>
#import <PSMTabStyle.h>
#import <iTerm/iTermGrowlDelegate.h>
#include <unistd.h>
#import "PasteboardHistory.h"
#import "PTYTab.h"
#import "SessionView.h"
#import "iTerm/iTermApplication.h"
#import "BookmarksWindow.h"
#import "FindViewController.h"
#define CACHED_WINDOW_POSITIONS 100
#define ITLocalizedString(key) NSLocalizedStringFromTableInBundle(key, @"iTerm", [NSBundle bundleForClass:[self class]], @"Context menu")
// #define PSEUDOTERMINAL_VERBOSE_LOGGING
#ifdef PSEUDOTERMINAL_VERBOSE_LOGGING
#define PtyLog NSLog
#else
#define PtyLog(args...) \
do { \
if (gDebugLogging) { \
DebugLog([NSString stringWithFormat:args]); \
} \
} while (0)
#endif
static BOOL windowPositions[CACHED_WINDOW_POSITIONS];
// Constants for saved window arrangement key names.
static NSString* TERMINAL_ARRANGEMENT_OLD_X_ORIGIN = @"Old X Origin";
static NSString* TERMINAL_ARRANGEMENT_OLD_Y_ORIGIN = @"Old Y Origin";
static NSString* TERMINAL_ARRANGEMENT_OLD_WIDTH = @"Old Width";
static NSString* TERMINAL_ARRANGEMENT_OLD_HEIGHT = @"Old Height";
static NSString* TERMINAL_ARRANGEMENT_X_ORIGIN = @"X Origin";
static NSString* TERMINAL_ARRANGEMENT_Y_ORIGIN = @"Y Origin";
static NSString* TERMINAL_ARRANGEMENT_WIDTH = @"Width";
static NSString* TERMINAL_ARRANGEMENT_HEIGHT = @"Height";
static NSString* TERMINAL_ARRANGEMENT_TABS = @"Tabs";
static NSString* TERMINAL_ARRANGEMENT_FULLSCREEN = @"Fullscreen";
static NSString* TERMINAL_ARRANGEMENT_LION_FULLSCREEN = @"LionFullscreen";
static NSString* TERMINAL_ARRANGEMENT_WINDOW_TYPE = @"Window Type";
static NSString* TERMINAL_ARRANGEMENT_SELECTED_TAB_INDEX = @"Selected Tab Index";
static NSString* TERMINAL_ARRANGEMENT_SCREEN_INDEX = @"Screen";
@interface PSMTabBarControl (Private)
- (void)update;
@end
@interface NSWindow (private)
- (void)setBottomCornerRounded:(BOOL)rounded;
@end
// keys for attributes:
NSString *columnsKey = @"columns";
NSString *rowsKey = @"rows";
// keys for to-many relationships:
NSString *sessionsKey = @"sessions";
#define TABVIEW_TOP_OFFSET 29
#define TABVIEW_BOTTOM_OFFSET 27
#define TABVIEW_LEFT_RIGHT_OFFSET 29
#define TOOLBAR_OFFSET 0
@class PTYSession, iTermController, PTToolbarController, PSMTabBarControl;
@implementation SolidColorView
- (id)initWithFrame:(NSRect)frame color:(NSColor*)color
{
self = [super initWithFrame:frame];
if (self) {
color_ = [color retain];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[color_ setFill];
NSRectFill(dirtyRect);
}
- (void)setColor:(NSColor*)color
{
[color_ autorelease];
color_ = [color retain];
}
- (NSColor*)color
{
return color_;
}
@end
@implementation BottomBarView
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor controlColor] setFill];
NSRectFill(dirtyRect);
// Draw a black line at the top of the view.
[[NSColor blackColor] setFill];
NSRect r = [self frame];
NSRectFill(NSMakeRect(0, r.size.height - 1, r.size.width, 1));
}
@end
@implementation PseudoTerminal
- (id)initWithSmartLayout:(BOOL)smartLayout windowType:(int)windowType screen:(int)screenNumber
{
PTYWindow *myWindow;
self = [super initWithWindowNibName:@"PseudoTerminal"];
NSAssert(self, @"initWithWindowNibName returned nil");
// Force the nib to load
[self window];
[commandField retain];
[commandField setDelegate:self];
[bottomBar retain];
if (windowType == WINDOW_TYPE_LION_FULL_SCREEN &&
![[PreferencePanel sharedInstance] lionStyleFullscreen]) {
windowType = WINDOW_TYPE_FULL_SCREEN;
}
if ((windowType == WINDOW_TYPE_FULL_SCREEN ||
windowType == WINDOW_TYPE_LION_FULL_SCREEN) &&
screenNumber == -1) {
NSUInteger n = [[NSScreen screens] indexOfObjectIdenticalTo:[[self window] screen]];
if (n == NSNotFound) {
screenNumber = 0;
} else {
screenNumber = n;
}
}
if (windowType == WINDOW_TYPE_TOP) {
smartLayout = NO;
}
if (windowType == WINDOW_TYPE_NORMAL) {
// If you create a window with a minimize button and the menu bar is hidden then the
// minimize button is disabled. Currently the only window type with a miniaturize button
// is NORMAL.
[self showMenuBar];
}
// Force the nib to load
[self window];
[commandField retain];
[commandField setDelegate:self];
[bottomBar retain];
windowType_ = windowType;
pbHistoryView = [[PasteboardHistoryView alloc] init];
autocompleteView = [[AutocompleteView alloc] init];
NSScreen* screen;
if (screenNumber < 0 || screenNumber >= [[NSScreen screens] count]) {
screen = [[self window] screen];
screenNumber_ = 0;
haveScreenPreference_ = NO;
} else {
screen = [[NSScreen screens] objectAtIndex:screenNumber];
screenNumber_ = screenNumber;
haveScreenPreference_ = YES;
}
NSRect initialFrame;
switch (windowType) {
case WINDOW_TYPE_TOP:
initialFrame = [screen visibleFrame];
break;
case WINDOW_TYPE_FORCE_FULL_SCREEN:
oldFrame_ = [[self window] frame];
initialFrame = [screen frame];
break;
default:
PtyLog(@"Unknown window type: %d", (int)windowType);
NSLog(@"Unknown window type: %d", (int)windowType);
// fall through
case WINDOW_TYPE_NORMAL:
haveScreenPreference_ = NO;
// fall through
case WINDOW_TYPE_LION_FULL_SCREEN:
case WINDOW_TYPE_FULL_SCREEN:
// Use the system-supplied frame which has a reasonable origin. It may
// be overridden by smart window placement or a saved window location.
initialFrame = [[self window] frame];
if (screenNumber_ != 0) {
// Move the frame to the desired screen
NSScreen* baseScreen = [[self window] deepestScreen];
NSPoint basePoint = [baseScreen visibleFrame].origin;
double xoffset = initialFrame.origin.x - basePoint.x;
double yoffset = initialFrame.origin.y - basePoint.y;
NSPoint destPoint = [screen visibleFrame].origin;
destPoint.x += xoffset;
destPoint.y += yoffset;
initialFrame.origin = destPoint;
// Make sure the top-right corner of the window is on the screen too
NSRect destScreenFrame = [screen visibleFrame];
double xover = destPoint.x + initialFrame.size.width - (destScreenFrame.origin.x + destScreenFrame.size.width);
double yover = destPoint.y + initialFrame.size.height - (destScreenFrame.origin.y + destScreenFrame.size.height);
if (xover > 0) {
destPoint.x -= xover;
}
if (yover > 0) {
destPoint.y -= yover;
}
[[self window] setFrameOrigin:destPoint];
}
break;
}
preferredOrigin_ = initialFrame.origin;
PtyLog(@"initWithSmartLayout - initWithContentRect");
// create the window programmatically with appropriate style mask
NSUInteger styleMask = NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask |
NSTexturedBackgroundWindowMask;
switch (windowType) {
case WINDOW_TYPE_TOP:
styleMask = NSBorderlessWindowMask;
break;
case WINDOW_TYPE_FORCE_FULL_SCREEN:
styleMask = NSBorderlessWindowMask;
break;
default:
break;
}
myWindow = [[PTYWindow alloc] initWithContentRect:initialFrame
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
if (windowType == WINDOW_TYPE_TOP) {
[myWindow setHasShadow:YES];
}
[myWindow _setContentHasShadow:NO];
PtyLog(@"initWithSmartLayout - new window is at %d", myWindow);
[self setWindow:myWindow];
[myWindow release];
_fullScreen = (windowType == WINDOW_TYPE_FORCE_FULL_SCREEN);
if (_fullScreen) {
background_ = [[SolidColorView alloc] initWithFrame:[[[self window] contentView] frame] color:[NSColor blackColor]];
} else {
background_ = [[SolidColorView alloc] initWithFrame:[[[self window] contentView] frame] color:[NSColor windowBackgroundColor]];
}
[[self window] setAlphaValue:1];
[[self window] setOpaque:NO];
normalBackgroundColor = [background_ color];
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
_resizeInProgressFlag = NO;
if (!smartLayout || windowType == WINDOW_TYPE_FORCE_FULL_SCREEN) {
[(PTYWindow*)[self window] setLayoutDone];
}
if (windowType == WINDOW_TYPE_NORMAL) {
_toolbarController = [[PTToolbarController alloc] initWithPseudoTerminal:self];
if ([[self window] respondsToSelector:@selector(setBottomCornerRounded:)])
[[self window] setBottomCornerRounded:NO];
}
// create the tab bar control
[[self window] setContentView:background_];
[background_ release];
NSRect aRect = [[[self window] contentView] bounds];
aRect.size.height = 22;
tabBarControl = [[PSMTabBarControl alloc] initWithFrame:aRect];
[tabBarControl retain];
PreferencePanel* pp = [PreferencePanel sharedInstance];
[tabBarControl setModifier:[pp modifierTagToMask:[pp switchTabModifier]]];
[tabBarControl setAutoresizingMask:(NSViewWidthSizable | NSViewMinYMargin)];
[[[self window] contentView] addSubview:tabBarControl];
[tabBarControl release];
// Set up bottomBar
NSRect irFrame = [instantReplaySubview frame];
bottomBar = [[NSView alloc] initWithFrame:NSMakeRect(0,
0,
irFrame.size.width,
irFrame.size.height)];
[bottomBar addSubview:instantReplaySubview];
[bottomBar setHidden:YES];
[instantReplaySubview setHidden:NO];
// create the tabview
aRect = [[[self window] contentView] bounds];
TABVIEW = [[PTYTabView alloc] initWithFrame:aRect];
[TABVIEW setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[TABVIEW setAutoresizesSubviews:YES];
[TABVIEW setAllowsTruncatedLabels:NO];
[TABVIEW setControlSize:NSSmallControlSize];
[TABVIEW setTabViewType:NSNoTabsNoBorder];
// Add to the window
[[[self window] contentView] addSubview:TABVIEW];
[TABVIEW release];
[[[self window] contentView] addSubview:bottomBar];
// assign tabview and delegates
[tabBarControl setTabView: TABVIEW];
[TABVIEW setDelegate: tabBarControl];
[tabBarControl setDelegate: self];
[tabBarControl setHideForSingleTab: NO];
// set the style of tabs to match window style
[self setTabBarStyle];
[[[self window] contentView] setAutoresizesSubviews: YES];
[[self window] setDelegate: self];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_refreshTitle:)
name: @"iTermUpdateLabels"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_refreshTerminal:)
name: @"iTermRefreshTerminal"
object: nil];
[self setWindowInited: YES];
useTransparency_ = YES;
fullscreenTabs_ = [[NSUserDefaults standardUserDefaults] objectForKey:@"ShowFullScreenTabBar"] ?
[[NSUserDefaults standardUserDefaults] boolForKey:@"ShowFullScreenTabBar"] : true;
number_ = [[iTermController sharedInstance] allocateWindowNumber];
if (windowType == WINDOW_TYPE_FORCE_FULL_SCREEN) {
windowType_ = WINDOW_TYPE_FULL_SCREEN;
[self hideMenuBar];
}
if (IsLionOrLater()) {
[[self window] setCollectionBehavior:[[self window] collectionBehavior] | NSWindowCollectionBehaviorFullScreenPrimary];
}
return self;
}
- (int)number
{
return number_;
}
- (PTYWindow*)ptyWindow
{
return (PTYWindow*) [self window];
}
- (NSScreen*)screen
{
NSArray* screens = [NSScreen screens];
if ([screens count] > screenNumber_) {
return [screens objectAtIndex:screenNumber_];
} else {
return [NSScreen mainScreen];
}
}
- (void)swipeWithEvent:(NSEvent *)event
{
if ([event deltaX] < 0) {
[self nextTab:nil];
} else if ([event deltaX] > 0) {
[self previousTab:nil];
}
if ([event deltaY] < 0) {
[[iTermController sharedInstance] nextTerminal:nil];
} else if ([event deltaY] > 0) {
[[iTermController sharedInstance] previousTerminal:nil];
}
}
- (void)setTabBarStyle
{
switch ([[PreferencePanel sharedInstance] windowStyle]) {
case 0:
[tabBarControl setStyleNamed:@"Metal"];
break;
case 1:
[tabBarControl setStyleNamed:@"Aqua"];
break;
case 2:
[tabBarControl setStyleNamed:@"Unified"];
break;
default:
[tabBarControl setStyleNamed:@"Adium"];
break;
}
}
- (id)commandField
{
return commandField;
}
- (void)selectSessionAtIndexAction:(id)sender
{
[TABVIEW selectTabViewItemAtIndex:[sender tag]];
}
- (NSInteger)indexOfTab:(PTYTab*)aTab
{
NSArray* items = [TABVIEW tabViewItems];
for (int i = 0; i < [items count]; i++) {
if ([[items objectAtIndex:i] identifier] == aTab) {
return i;
}
}
return NSNotFound;
}
- (void)newSessionInTabAtIndex:(id)sender
{
Bookmark* bookmark = [[BookmarkModel sharedInstance] bookmarkWithGuid:[sender representedObject]];
if (bookmark) {
[self addNewSession:bookmark];
}
}
- (void)newSessionsInManyTabsAtIndex:(id)sender
{
NSMenu* parent = [sender representedObject];
for (NSMenuItem* item in [parent itemArray]) {
if (![item isSeparatorItem] && ![item submenu]) {
NSString* guid = [item representedObject];
Bookmark* bookmark = [[BookmarkModel sharedInstance] bookmarkWithGuid:guid];
if (bookmark) {
[self addNewSession:bookmark];
}
}
}
}
- (void)closeSession:(PTYSession *)aSession
{
if ([[[aSession tab] sessions] count] == 1) {
[self closeTab:[aSession tab]];
} else {
[aSession terminate];
}
}
- (int)windowType
{
return windowType_;
}
- (void)closeTab:(PTYTab*)aTab
{
NSTabViewItem *aTabViewItem;
int numberOfTabs;
if ([TABVIEW indexOfTabViewItemWithIdentifier:aTab] == NSNotFound) {
return;
}
int numClosing = 0;
for (PTYSession* session in [aTab sessions]) {
if (![session exited]) {
++numClosing;
}
}
BOOL mustAsk = NO;
if (numClosing > 0 && [[PreferencePanel sharedInstance] promptOnClose]) {
if (numClosing == 1) {
if (![[PreferencePanel sharedInstance] onlyWhenMoreTabs]) {
mustAsk = YES;
}
} else {
mustAsk = YES;
}
}
if (mustAsk) {
BOOL okToClose;
if (numClosing == 1) {
okToClose = NSRunAlertPanel([NSString stringWithFormat:@"%@ #%d",
[[aTab activeSession] name],
[aTab realObjectCount]],
NSLocalizedStringFromTableInBundle(@"This tab will be closed.",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"Close Session"),
NSLocalizedStringFromTableInBundle(@"OK",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"OK"),
NSLocalizedStringFromTableInBundle(@"Cancel",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"Cancel"),
nil) == NSAlertDefaultReturn;
} else {
okToClose = NSRunAlertPanel([NSString stringWithFormat:@"Close multiple panes in tab #%d",
[aTab realObjectCount]],
[NSString stringWithFormat:
NSLocalizedStringFromTableInBundle(@"%d sessions will be closed.",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"Close Session"), numClosing],
NSLocalizedStringFromTableInBundle(@"OK",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"OK"),
NSLocalizedStringFromTableInBundle(@"Cancel",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"Cancel"),
nil) == NSAlertDefaultReturn;
}
if (!okToClose) {
return;
}
}
numberOfTabs = [TABVIEW numberOfTabViewItems];
for (PTYSession* session in [aTab sessions]) {
[session terminate];
}
if (numberOfTabs == 1 && [self windowInited]) {
[[self window] close];
} else {
// now get rid of this tab
aTabViewItem = [aTab tabViewItem];
[TABVIEW removeTabViewItem:aTabViewItem];
PtyLog(@"closeSession - calling fitWindowToTabs");
[self fitWindowToTabs];
}
}
// Save the current scroll position
- (IBAction)saveScrollPosition:(id)sender
{
[[self currentSession] saveScrollPosition];
}
// Jump to the saved scroll position
- (IBAction)jumpToSavedScrollPosition:(id)sender
{
[[self currentSession] jumpToSavedScrollPosition];
}
// Is there a saved scroll position?
- (BOOL)hasSavedScrollPosition
{
return [[self currentSession] hasSavedScrollPosition];
}
- (void)toggleFullScreenTabBar
{
fullscreenTabs_ = !fullscreenTabs_;
[[NSUserDefaults standardUserDefaults] setBool:fullscreenTabs_ forKey:@"ShowFullScreenTabBar"];
[self repositionWidgets];
}
- (IBAction)closeCurrentTab:(id)sender
{
[self closeTab:[self currentTab]];
}
- (IBAction)closeCurrentSession:(id)sender
{
if ([[self window] isKeyWindow]) {
PTYSession *aSession = [[[TABVIEW selectedTabViewItem] identifier] activeSession];
[self closeSessionWithConfirmation:aSession];
}
}
- (void)closeSessionWithConfirmation:(PTYSession *)aSession
{
if ([[[aSession tab] sessions] count] == 1) {
[self closeCurrentTab:self];
return;
}
if ([aSession exited] ||
![[PreferencePanel sharedInstance] promptOnClose] ||
[[PreferencePanel sharedInstance] onlyWhenMoreTabs] ||
(NSRunAlertPanel([NSString stringWithFormat:@"%@ #%d",
[aSession name],
[[aSession tab] realObjectCount]],
NSLocalizedStringFromTableInBundle(@"This session will be closed.",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"Close Session"),
NSLocalizedStringFromTableInBundle(@"OK",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"OK"),
NSLocalizedStringFromTableInBundle(@"Cancel",
@"iTerm",
[NSBundle bundleForClass:[self class]],
@"Cancel"),
nil) == NSAlertDefaultReturn)) {
// Just in case IR is open, close it first.
[self closeInstantReplay:self];
[self closeSession:aSession];
}
}
- (IBAction)previousTab:(id)sender
{
NSTabViewItem *tvi = [TABVIEW selectedTabViewItem];
[TABVIEW selectPreviousTabViewItem:sender];
if (tvi == [TABVIEW selectedTabViewItem]) {
[TABVIEW selectTabViewItemAtIndex:[TABVIEW numberOfTabViewItems]-1];
}
}
- (IBAction)nextTab:(id)sender
{
NSTabViewItem *tvi = [TABVIEW selectedTabViewItem];
[TABVIEW selectNextTabViewItem: sender];
if (tvi == [TABVIEW selectedTabViewItem]) {
[TABVIEW selectTabViewItemAtIndex:0];
}
}
- (IBAction)previousPane:(id)sender
{
[[self currentTab] previousSession];
}
- (IBAction)nextPane:(id)sender
{
[[self currentTab] nextSession];
}
- (int)numberOfTabs
{
return [TABVIEW numberOfTabViewItems];
}
- (PTYTab*)currentTab
{
return [[TABVIEW selectedTabViewItem] identifier];
}
- (PTYSession *)currentSession
{
return [[[TABVIEW selectedTabViewItem] identifier] activeSession];
}
- (void)dealloc
{
// Do not assume that [self window] is valid here. It may have been freed.
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Cancel any SessionView timers.
for (PTYSession* aSession in [self sessions]) {
[[aSession view] cancelTimers];
}
// Release all our sessions
NSTabViewItem *aTabViewItem;
for (; [TABVIEW numberOfTabViewItems]; ) {
aTabViewItem = [TABVIEW tabViewItemAtIndex:0];
[[aTabViewItem identifier] terminateAllSessions];
PTYTab* theTab = [aTabViewItem identifier];
[theTab setParentWindow:nil];
[TABVIEW removeTabViewItem:aTabViewItem];
}
[commandField release];
[bottomBar release];
[_toolbarController release];
[autocompleteView shutdown];
[pbHistoryView shutdown];
[pbHistoryView release];
[autocompleteView release];
[tabBarControl release];
if (fullScreenTabviewTimer_) {
[fullScreenTabviewTimer_ invalidate];
}
[super dealloc];
}
- (void)setWindowTitle
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal setWindowTitle]",
__FILE__, __LINE__);
#endif
[self setWindowTitle:[self currentSessionName]];
}
- (void)setWindowTitle:(NSString *)title
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal setWindowTitle:%@]",
__FILE__, __LINE__, title);
#endif
if (title == nil) {
// title can be nil during loadWindowArrangement
title = @"";
}
if ([self sendInputToAllSessions]) {
title = [NSString stringWithFormat:@"☛%@", title];
}
NSUInteger number = [[iTermController sharedInstance] indexOfTerminal:self];
if ([[PreferencePanel sharedInstance] windowNumber] && number >= 0 && number < 9) {
[[self window] setTitle:[NSString stringWithFormat:@"%d. %@", number_+1, title]];
} else {
[[self window] setTitle:title];
}
}
- (BOOL)tempTitle
{
return tempTitle;
}
- (void)resetTempTitle
{
tempTitle = NO;
}
- (void)sendInputToAllSessions:(NSData *)data
{
int i;
int n = [TABVIEW numberOfTabViewItems];
for (i = 0; i < n; ++i) {
for (PTYSession* aSession in [[[TABVIEW tabViewItemAtIndex:i] identifier] sessions]) {
if (![aSession exited]) {
[[aSession SHELL] writeTask:data];
}
}
}
}
+ (PseudoTerminal*)terminalWithArrangement:(NSDictionary*)arrangement
{
PseudoTerminal* term;
int windowType;
if ([arrangement objectForKey:TERMINAL_ARRANGEMENT_WINDOW_TYPE]) {
windowType = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_WINDOW_TYPE] intValue];
} else {
if ([arrangement objectForKey:TERMINAL_ARRANGEMENT_FULLSCREEN] &&
[[arrangement objectForKey:TERMINAL_ARRANGEMENT_FULLSCREEN] boolValue]) {
windowType = WINDOW_TYPE_FULL_SCREEN;
} else if ([[arrangement objectForKey:TERMINAL_ARRANGEMENT_LION_FULLSCREEN] boolValue]) {
if (IsLionOrLater() || ![[PreferencePanel sharedInstance] lionStyleFullscreen]) {
windowType = WINDOW_TYPE_LION_FULL_SCREEN;
} else {
windowType = WINDOW_TYPE_FULL_SCREEN;
}
} else {
windowType = WINDOW_TYPE_NORMAL;
}
}
int screenIndex;
if ([arrangement objectForKey:TERMINAL_ARRANGEMENT_SCREEN_INDEX]) {
screenIndex = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_SCREEN_INDEX] intValue];
} else {
screenIndex = 0;
}
if (screenIndex < 0 || screenIndex >= [[NSScreen screens] count]) {
screenIndex = 0;
}
if (windowType == WINDOW_TYPE_FULL_SCREEN) {
term = [[[PseudoTerminal alloc] initWithSmartLayout:NO
windowType:WINDOW_TYPE_FORCE_FULL_SCREEN
screen:screenIndex] autorelease];
NSRect rect;
rect.origin.x = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_OLD_X_ORIGIN] doubleValue];
rect.origin.y = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_OLD_Y_ORIGIN] doubleValue];
rect.size.width = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_OLD_WIDTH] doubleValue];
rect.size.height = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_OLD_HEIGHT] doubleValue];
term->oldFrame_ = rect;
} else if (windowType == WINDOW_TYPE_LION_FULL_SCREEN) {
term = [[[PseudoTerminal alloc] initWithSmartLayout:NO
windowType:WINDOW_TYPE_LION_FULL_SCREEN
screen:screenIndex] autorelease];
[term delayedEnterFullscreen];
} else {
if (windowType == WINDOW_TYPE_NORMAL) {
screenIndex = -1;
}
// TODO: this looks like a bug - are top-of-screen windows not restored to the right screen?
term = [[[PseudoTerminal alloc] initWithSmartLayout:NO windowType:windowType screen:-1] autorelease];
NSRect rect;
rect.origin.x = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_X_ORIGIN] doubleValue];
rect.origin.y = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_Y_ORIGIN] doubleValue];
// TODO: for window type top, set width to screen width.
rect.size.width = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_WIDTH] doubleValue];
rect.size.height = [[arrangement objectForKey:TERMINAL_ARRANGEMENT_HEIGHT] doubleValue];
[[term window] setFrame:rect display:NO];
}
for (NSDictionary* tabArrangement in [arrangement objectForKey:TERMINAL_ARRANGEMENT_TABS]) {
[PTYTab openTabWithArrangement:tabArrangement inTerminal:term];
}
[term->TABVIEW selectTabViewItemAtIndex:[[arrangement objectForKey:TERMINAL_ARRANGEMENT_SELECTED_TAB_INDEX] intValue]];
return term;
}
- (NSDictionary*)arrangement
{
NSMutableDictionary* result = [NSMutableDictionary dictionaryWithCapacity:7];
NSRect rect = [[self window] frame];
int screenNumber = 0;
for (NSScreen* screen in [NSScreen screens]) {
if (screen == [[self window] deepestScreen]) {
break;
}
++screenNumber;
}
// Save window frame
[result setObject:[NSNumber numberWithDouble:rect.origin.x]
forKey:TERMINAL_ARRANGEMENT_X_ORIGIN];
[result setObject:[NSNumber numberWithDouble:rect.origin.y]
forKey:TERMINAL_ARRANGEMENT_Y_ORIGIN];
[result setObject:[NSNumber numberWithDouble:rect.size.width]
forKey:TERMINAL_ARRANGEMENT_WIDTH];
[result setObject:[NSNumber numberWithDouble:rect.size.height]
forKey:TERMINAL_ARRANGEMENT_HEIGHT];
if ([self anyFullScreen]) {
// Save old window frame
[result setObject:[NSNumber numberWithDouble:oldFrame_.origin.x]
forKey:TERMINAL_ARRANGEMENT_OLD_X_ORIGIN];
[result setObject:[NSNumber numberWithDouble:oldFrame_.origin.y]
forKey:TERMINAL_ARRANGEMENT_OLD_Y_ORIGIN];
[result setObject:[NSNumber numberWithDouble:oldFrame_.size.width]
forKey:TERMINAL_ARRANGEMENT_OLD_WIDTH];
[result setObject:[NSNumber numberWithDouble:oldFrame_.size.height]
forKey:TERMINAL_ARRANGEMENT_OLD_HEIGHT];
}
[result setObject:[NSNumber numberWithInt:windowType_]
forKey:TERMINAL_ARRANGEMENT_WINDOW_TYPE];
[result setObject:[NSNumber numberWithInt:[[NSScreen screens] indexOfObjectIdenticalTo:[[self window] screen]]]
forKey:TERMINAL_ARRANGEMENT_SCREEN_INDEX];
// Save tabs.
NSMutableArray* tabs = [NSMutableArray arrayWithCapacity:[self numberOfTabs]];
for (NSTabViewItem* tabViewItem in [TABVIEW tabViewItems]) {
[tabs addObject:[[tabViewItem identifier] arrangement]];
}
[result setObject:tabs forKey:TERMINAL_ARRANGEMENT_TABS];
// Save index of selected tab.
[result setObject:[NSNumber numberWithInt:[TABVIEW indexOfTabViewItem:[TABVIEW selectedTabViewItem]]]
forKey:TERMINAL_ARRANGEMENT_SELECTED_TAB_INDEX];
return result;
}
// NSWindow delegate methods
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal windowDidDeminiaturize:%@]",
__FILE__, __LINE__, aNotification);
#endif
if ([[self currentTab] blur]) {
[self enableBlur:[[self currentTab] blurRadius]];
} else {
[self disableBlur];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"iTermWindowDidDeminiaturize"
object:self
userInfo:nil];
}
- (BOOL)windowShouldClose:(NSNotification *)aNotification
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal windowShouldClose:%@]",
__FILE__, __LINE__, aNotification);
#endif
if ([[PreferencePanel sharedInstance] promptOnClose] &&
(![[PreferencePanel sharedInstance] onlyWhenMoreTabs] ||
[TABVIEW numberOfTabViewItems] > 1)) {
return [self showCloseWindow];
} else {
return YES;
}
}
- (void)windowWillClose:(NSNotification *)aNotification
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal windowWillClose:%@]",
__FILE__, __LINE__, aNotification);
#endif
// Close popups.
[pbHistoryView close];
[autocompleteView close];
// tabBarControl is holding on to us, so we have to tell it to let go
[tabBarControl setDelegate:nil];
[self disableBlur];
// If a fullscreen window is closing, hide the menu bar unless it's only fullscreen because it's
// mid-toggle in which case it's really the window that's replacing us that is fullscreen.
if (_fullScreen && !togglingFullScreen_) {
[self showMenuBar];
}
// Save frame position for last window
if ([[[iTermController sharedInstance] terminals] count] == 1) {
// Close the bottomBar because otherwise the wrong size
// frame is saved. You wouldn't want the bottomBar to
// open automatically anyway.
// TODO(georgen): There's a tiny bug here. If you're in instant replay
// then the window size for the IR window is saved instead of the live
// window.
if (![bottomBar isHidden]) {
[self showHideInstantReplay];
}
if ([[PreferencePanel sharedInstance] smartPlacement]) {
[[self window] saveFrameUsingName: [NSString stringWithFormat: WINDOW_NAME, 0]];
} else {
// Save frame position for window
[[self window] saveFrameUsingName: [NSString stringWithFormat: WINDOW_NAME, framePos]];
windowPositions[framePos] = NO;