-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPseudoTerminal.m
3141 lines (2593 loc) · 103 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
#define WINDOW_NAME @"iTerm Window 0"
#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/VT100Terminal.h>
#import <iTerm/VT100Screen.h>
#import <iTerm/PTYSession.h>
#import <iTerm/PTToolbarController.h>
#import <iTerm/FindPanelWindowController.h>
#import <iTerm/ITAddressBookMgr.h>
#import <iTerm/ITConfigPanelController.h>
#import <iTerm/iTermTerminalProfileMgr.h>
#import <iTerm/iTermDisplayProfileMgr.h>
#import <iTerm/Tree.h>
#import <PSMTabBarControl.h>
#import <PSMTabStyle.h>
#import <iTermBookmarkController.h>
//#import <iTerm/iTermGrowlDelegate.h>
#include <unistd.h>
@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
@implementation PseudoTerminal
// Utility
+ (void) breakDown:(NSString *)cmdl cmdPath: (NSString **) cmd cmdArgs: (NSArray **) path
{
int i,j,k,qf,slen;
char tmp[100];
const char *s;
NSMutableArray *p;
p=[[NSMutableArray alloc] init];
s=[cmdl cString];
slen = strlen(s);
i=j=qf=0;
k=-1;
while (i<=slen) {
if (qf) {
if (s[i]=='\"') {
qf=0;
}
else {
tmp[j++]=s[i];
}
}
else {
if (s[i]=='\"') {
qf=1;
}
else if (s[i]==' ' || s[i]=='\t' || s[i]=='\n'||s[i]==0) {
tmp[j]=0;
if (k==-1) {
*cmd=[NSString stringWithCString:tmp];
}
else
[p addObject:[NSString stringWithCString:tmp]];
j=0;
k++;
while (i<slen&&s[i+1]==' '||s[i+1]=='\t'||s[i+1]=='\n'||s[i+1]==0) i++;
}
else {
tmp[j++]=s[i];
}
}
i++;
}
*path = [NSArray arrayWithArray:p];
[p release];
}
- (id)initWithWindowNibName: (NSString *) windowNibName
{
NSScrollView *aScrollView;
NSTableColumn *aTableColumn;
NSSize aSize;
NSRect aRect;
unsigned int styleMask;
PTYWindow *myWindow;
NSDrawer *myDrawer;
if ((self = [super initWithWindowNibName: windowNibName]) == nil)
return nil;
//enforce the nib to load
[self window];
[commandField retain];
[commandField setDelegate:self];
// create the window programmatically with appropriate style mask
styleMask = NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask;
// set the window style according to preference
if([[PreferencePanel sharedInstance] windowStyle] == 0)
styleMask |= NSTexturedBackgroundWindowMask;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
else if([[PreferencePanel sharedInstance] windowStyle] == 2)
styleMask |= NSUnifiedTitleAndToolbarWindowMask;
#endif
myWindow = [[PTYWindow alloc] initWithContentRect: [[NSScreen mainScreen] frame]
styleMask: styleMask
backing: NSBackingStoreBuffered
defer: NO];
[self setWindow: myWindow];
[myWindow release];
_fullScreen = NO;
// create and set up drawer
myDrawer = [[NSDrawer alloc] initWithContentSize: NSMakeSize(20, 100) preferredEdge: NSMinXEdge];
[myDrawer setParentWindow: myWindow];
[myDrawer setDelegate:self];
[myWindow setDrawer: myDrawer];
float aWidth = [[NSUserDefaults standardUserDefaults] floatForKey: @"BookmarksDrawerWidth"];
if (aWidth<=0) aWidth = 150.0;
[myDrawer setContentSize: NSMakeSize(aWidth, 0)];
[myDrawer release];
aScrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 20, 100)];
[aScrollView setBorderType:NSBezelBorder];
[aScrollView setHasHorizontalScroller: NO];
[aScrollView setHasVerticalScroller: YES];
[[aScrollView verticalScroller] setControlSize:NSSmallControlSize];
[aScrollView setAutohidesScrollers: YES];
aSize = [aScrollView contentSize];
aRect = NSZeroRect;
aRect.size = aSize;
bookmarksView = [[NSOutlineView alloc] initWithFrame:aRect];
aTableColumn = [[NSTableColumn alloc] initWithIdentifier: @"Name"];
[[aTableColumn headerCell] setStringValue: NSLocalizedStringFromTableInBundle(@"Bookmarks",@"iTerm", [NSBundle bundleForClass: [self class]], @"Bookmarks")];
[bookmarksView addTableColumn: aTableColumn];
[aTableColumn release];
[bookmarksView setOutlineTableColumn: aTableColumn];
[bookmarksView setDelegate: self];
[bookmarksView setTarget: self];
[bookmarksView setDoubleAction: @selector(doubleClickedOnBookmarksView:)];
[bookmarksView setDataSource: [iTermBookmarkController sharedInstance]];
[aScrollView setDocumentView:bookmarksView];
[bookmarksView release];
[myDrawer setContentView: aScrollView];
[aScrollView release];
[self _commonInit];
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
_resizeInProgressFlag = NO;
return self;
}
- (id)initWithFullScreenWindowNibName: (NSString *) windowNibName
{
PTYWindow *myWindow;
NSScreen *currentScreen = [[[[iTermController sharedInstance] currentTerminal] window]screen];
if ((self = [super initWithWindowNibName: windowNibName]) == nil)
return nil;
myWindow = [[PTYWindow alloc] initWithContentRect: [currentScreen frame]
styleMask: NSBorderlessWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[myWindow setBackgroundColor:[NSColor blackColor]];
[self setWindow: myWindow];
[self hideMenuBar];
[myWindow release];
_fullScreen = YES;
[self _commonInit];
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
_resizeInProgressFlag = NO;
return self;
}
- (id)init
{
self = ([self initWithWindowNibName: @"PseudoTerminal"]);
return self;
}
// Do not use both initViewWithFrame and initWindow
// initViewWithFrame is mainly meant for embedding a terminal view in a non-iTerm window.
- (PTYTabView*) initViewWithFrame: (NSRect) frame
{
NSFont *aFont1, *aFont2;
NSSize contentSize;
NSString *displayProfile;
// sanity check
if(TABVIEW != nil)
return (TABVIEW);
// Create the tabview
TABVIEW = [[PTYTabView alloc] initWithFrame: frame];
[TABVIEW setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
[TABVIEW setAllowsTruncatedLabels: NO];
[TABVIEW setControlSize: NSSmallControlSize];
[TABVIEW setAutoresizesSubviews: YES];
// Tell us whenever something happens with the tab view
[TABVIEW setDelegate: self];
aFont1 = FONT;
if(aFont1 == nil)
{
NSDictionary *defaultSession = [[ITAddressBookMgr sharedInstance] defaultBookmarkData];
displayProfile = [defaultSession objectForKey: KEY_DISPLAY_PROFILE];
if(displayProfile == nil)
displayProfile = [[iTermDisplayProfileMgr singleInstance] defaultProfileName];
aFont1 = [[iTermDisplayProfileMgr singleInstance] windowFontForProfile: displayProfile];
aFont2 = [[iTermDisplayProfileMgr singleInstance] windowNAFontForProfile: displayProfile];
[self setFont: aFont1 nafont: aFont2];
}
NSParameterAssert(aFont1 != nil);
// Calculate the size of the terminal
contentSize = [NSScrollView contentSizeForFrameSize: [TABVIEW contentRect].size
hasHorizontalScroller: NO
hasVerticalScroller: ![[PreferencePanel sharedInstance] hideScrollbar]
borderType: NSNoBorder];
[self setCharSizeUsingFont: aFont1];
[self setWidth: (int) ((contentSize.width - MARGIN * 2)/charWidth + 0.1)
height: (int) ((contentSize.height) /charHeight + 0.1)];
return ([TABVIEW autorelease]);
}
// Do not use both initViewWithFrame and initWindow
- (void)initWindowWithAddressbook:(NSDictionary *)entry;
{
NSRect aRect;
// sanity check
if(TABVIEW != nil)
return;
if (!_fullScreen) {
_toolbarController = [[PTToolbarController alloc] initWithPseudoTerminal:self];
if ([[self window] respondsToSelector:@selector(setBottomCornerRounded:)])
[[self window] setBottomCornerRounded:NO];
}
// create the tab bar control
aRect = [[[self window] contentView] bounds];
aRect.size.height = 22;
tabBarControl = [[PSMTabBarControl alloc] initWithFrame: aRect];
[tabBarControl setAutoresizingMask: (NSViewWidthSizable | NSViewMinYMargin)];
[[[self window] contentView] addSubview: tabBarControl];
[tabBarControl release];
// create the tabview
aRect = [[[self window] contentView] bounds];
//aRect.size.height -= [tabBarControl frame].size.height;
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];
// assign tabview and delegates
[tabBarControl setTabView: TABVIEW];
[TABVIEW setDelegate: tabBarControl];
[tabBarControl setDelegate: self];
[tabBarControl setHideForSingleTab: NO];
[tabBarControl setHidden:_fullScreen];
// set the style of tabs to match window style
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;
}
[[[self window] contentView] setAutoresizesSubviews: YES];
[[self window] setDelegate: self];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_reloadAddressBook:)
name: @"iTermReloadAddressBook"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_refreshTerminal:)
name: @"iTermRefreshTerminal"
object: nil];
[self setWindowInited: YES];
if (entry) {
NSString *displayProfile;
iTermDisplayProfileMgr *displayProfileMgr;
displayProfileMgr = [iTermDisplayProfileMgr singleInstance];
// grab the profiles
displayProfile = [entry objectForKey: KEY_DISPLAY_PROFILE];
if(displayProfile == nil)
displayProfile = [displayProfileMgr defaultProfileName];
[self setAntiAlias: [displayProfileMgr windowAntiAliasForProfile: displayProfile]];
[self setBlur: [displayProfileMgr windowBlurForProfile: displayProfile]];
[self setFont: [displayProfileMgr windowFontForProfile: displayProfile]
nafont: [displayProfileMgr windowNAFontForProfile: displayProfile]];
[self setCharacterSpacingHorizontal: [displayProfileMgr windowHorizontalCharSpacingForProfile: displayProfile]
vertical: [displayProfileMgr windowVerticalCharSpacingForProfile: displayProfile]];
if (_fullScreen) {
aRect = [TABVIEW frame];
WIDTH = (int)((aRect.size.width - MARGIN * 2)/charWidth);
HEIGHT = (int)((aRect.size.height)/charHeight);
}
else {
WIDTH = [displayProfileMgr windowColumnsForProfile: displayProfile];
HEIGHT = [displayProfileMgr windowRowsForProfile: displayProfile];
}
}
// position the tabview and control
if (_fullScreen) {
aRect = [[[self window] contentView] bounds];
aRect = NSMakeRect(floor((aRect.size.width-WIDTH*charWidth-MARGIN*2)/2),floor((aRect.size.height-charHeight*HEIGHT)/2),WIDTH*charWidth+MARGIN*2, charHeight*HEIGHT);
[TABVIEW setFrame: aRect];
}
else {
aRect = [tabBarControl frame];
aRect.origin.x = 0;
aRect.origin.y = [TABVIEW frame].size.height;
aRect.size.width = [[[self window] contentView] bounds].size.width;
[tabBarControl setFrame: aRect];
[tabBarControl setSizeCellsToFit:NO];
[tabBarControl setCellMinWidth:75];
[tabBarControl setCellOptimumWidth:175];
}
}
- (void)initWindowWithSettingsFrom:(PseudoTerminal *)aPseudoTerminal
{
NSRect aRect;
// sanity check
if(TABVIEW != nil)
return;
// Don't try to do smart layout this time
[(PTYWindow*)[self window] setLayoutDone];
if (!_fullScreen) {
_toolbarController = [[PTToolbarController alloc] initWithPseudoTerminal:self];
if ([[self window] respondsToSelector:@selector(setBottomCornerRounded:)])
[[self window] setBottomCornerRounded:NO];
}
// create the tab bar control
aRect = [[[self window] contentView] bounds];
aRect.size.height = 22;
tabBarControl = [[PSMTabBarControl alloc] initWithFrame: aRect];
[tabBarControl setAutoresizingMask: (NSViewWidthSizable | NSViewMinYMargin)];
[[[self window] contentView] addSubview: tabBarControl];
[tabBarControl release];
// create the tabview
aRect = [[[self window] contentView] bounds];
//aRect.size.height -= [tabBarControl frame].size.height;
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];
// assign tabview and delegates
[tabBarControl setTabView: TABVIEW];
[TABVIEW setDelegate: tabBarControl];
[tabBarControl setDelegate: self];
[tabBarControl setHideForSingleTab: NO];
[tabBarControl setHidden:_fullScreen];
// set the style of tabs to match window style
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;
}
[[[self window] contentView] setAutoresizesSubviews: YES];
[[self window] setDelegate: self];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_reloadAddressBook:)
name: @"iTermReloadAddressBook"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_refreshTerminal:)
name: @"iTermRefreshTerminal"
object: nil];
[self setWindowInited: YES];
if (aPseudoTerminal) {
[self setAntiAlias: [aPseudoTerminal antiAlias]];
[self setBlur: [aPseudoTerminal blur]];
[self setFont: [aPseudoTerminal font]
nafont: [aPseudoTerminal nafont]];
oldFont = [FONT retain];
oldNAFont = [NAFONT retain];
fontSizeFollowWindowResize = [aPseudoTerminal fontSizeFollowWindowResize];
useTransparency = [aPseudoTerminal useTransparency];
[self setCharacterSpacingHorizontal: [aPseudoTerminal charSpacingHorizontal]
vertical: [aPseudoTerminal charSpacingVertical]];
if (_fullScreen) {
// we are entering full screen mode. store the original size
oldFrame = [[aPseudoTerminal window] frame];
WIDTH = oldWidth = [aPseudoTerminal width];
HEIGHT = oldHeight = [aPseudoTerminal height];
charHorizontalSpacingMultiplier = oldCharHorizontalSpacingMultiplier = [aPseudoTerminal charSpacingHorizontal];
charVerticalSpacingMultiplier= oldCharVerticalSpacingMultiplier = [aPseudoTerminal charSpacingVertical];
aRect = [TABVIEW frame];
if (fontSizeFollowWindowResize) {
float scale = (aRect.size.height) / HEIGHT / charHeight;
NSFont *font = [[NSFontManager sharedFontManager] convertFont:FONT toSize:(int)(([FONT pointSize] * scale))];
font = [self _getMaxFont:font height:aRect.size.height lines:HEIGHT];
float height = [font defaultLineHeightForFont] * charVerticalSpacingMultiplier;
if (height != charHeight) {
//NSLog(@"Old size: %f\t proposed New size:%f\tWindow Height: %f",[FONT pointSize], [font pointSize],frame.size.height);
NSFont *nafont = [[NSFontManager sharedFontManager] convertFont:FONT toSize:(int)(([NAFONT pointSize] * scale))];
nafont = [self _getMaxFont:nafont height:aRect.size.height lines:HEIGHT];
[self setFont:font nafont:nafont];
}
}
else {
WIDTH = (int)((aRect.size.width - MARGIN * 2)/charWidth);
HEIGHT = (int)((aRect.size.height)/charHeight);
}
}
else {
if ([aPseudoTerminal fullScreen]) {
// we are exiting full screen mode. restore the original size.
_resizeInProgressFlag = YES;
[[self window] setFrame:[aPseudoTerminal oldFrame] display:NO];
_resizeInProgressFlag = NO;
WIDTH = [aPseudoTerminal oldWidth];
HEIGHT = [aPseudoTerminal oldHeight];
charHorizontalSpacingMultiplier =[aPseudoTerminal oldCharSpacingHorizontal];
charVerticalSpacingMultiplier= [aPseudoTerminal oldCharSpacingVertical];
[self setFont:[aPseudoTerminal oldFont] nafont:[aPseudoTerminal oldNAFont]];
}
else {
WIDTH = [aPseudoTerminal width];
HEIGHT = [aPseudoTerminal height];
}
}
}
// position the tabview and control
if (_fullScreen) {
aRect = [[[self window] contentView] bounds];
aRect = NSMakeRect(floor((aRect.size.width-WIDTH*charWidth-MARGIN*2)/2),floor((aRect.size.height-charHeight*HEIGHT)/2),WIDTH*charWidth+MARGIN*2, charHeight*HEIGHT);
[TABVIEW setFrame: aRect];
}
else {
aRect = [tabBarControl frame];
aRect.origin.x = 0;
aRect.origin.y = [TABVIEW frame].size.height;
aRect.size.width = [[[self window] contentView] bounds].size.width;
[tabBarControl setFrame: aRect];
[tabBarControl setSizeCellsToFit:NO];
[tabBarControl setCellMinWidth:75];
[tabBarControl setCellOptimumWidth:175];
}
}
- (id) commandField
{
return commandField;
}
- (void)setupSession: (PTYSession *) aSession
title: (NSString *)title
{
NSDictionary *addressBookPreferences;
NSDictionary *tempPrefs;
NSString *terminalProfile, *displayProfile;
iTermTerminalProfileMgr *terminalProfileMgr;
iTermDisplayProfileMgr *displayProfileMgr;
ITAddressBookMgr *bookmarkManager;
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal setupSession]",
__FILE__, __LINE__);
#endif
NSParameterAssert(aSession != nil);
// get our shared managers
terminalProfileMgr = [iTermTerminalProfileMgr singleInstance];
displayProfileMgr = [iTermDisplayProfileMgr singleInstance];
bookmarkManager = [ITAddressBookMgr sharedInstance];
// Init the rest of the session
[aSession setParent: self];
// set some default parameters
if([aSession addressBookEntry] == nil)
{
// get the default entry
addressBookPreferences = [[ITAddressBookMgr sharedInstance] defaultBookmarkData];
[aSession setAddressBookEntry:addressBookPreferences];
tempPrefs = addressBookPreferences;
}
else
{
tempPrefs = [aSession addressBookEntry];
}
terminalProfile = [tempPrefs objectForKey: KEY_TERMINAL_PROFILE];
displayProfile = [tempPrefs objectForKey: KEY_DISPLAY_PROFILE];
if(WIDTH == 0 && HEIGHT == 0)
{
WIDTH = [displayProfileMgr windowColumnsForProfile: displayProfile];
HEIGHT = [displayProfileMgr windowRowsForProfile: displayProfile];
[self setAntiAlias: [displayProfileMgr windowAntiAliasForProfile: displayProfile]];
[self setBlur: [displayProfileMgr windowBlurForProfile: displayProfile]];
}
if ([aSession initScreen: [TABVIEW contentRect] width:WIDTH height:HEIGHT]) {
if(FONT == nil)
{
[self setFont: [displayProfileMgr windowFontForProfile: displayProfile]
nafont: [displayProfileMgr windowNAFontForProfile: displayProfile]];
[self setCharacterSpacingHorizontal: [displayProfileMgr windowHorizontalCharSpacingForProfile: displayProfile]
vertical: [displayProfileMgr windowVerticalCharSpacingForProfile: displayProfile]];
}
[aSession setPreferencesFromAddressBookEntry: tempPrefs];
[[aSession SCREEN] setDisplay:[aSession TEXTVIEW]];
[[aSession TEXTVIEW] setFont:FONT nafont:NAFONT];
[[aSession TEXTVIEW] setAntiAlias: antiAlias];
[[aSession TEXTVIEW] setLineHeight: charHeight];
[[aSession TEXTVIEW] setLineWidth: WIDTH * charWidth];
[[aSession TEXTVIEW] setCharWidth: charWidth];
// NSLog(@"%d,%d",WIDTH,HEIGHT);
[[aSession TERMINAL] setTrace:YES]; // debug vt100 escape sequence decode
// tell the shell about our size
[[aSession SHELL] setWidth:WIDTH height:HEIGHT];
if (title)
{
[aSession setName: title];
[aSession setDefaultName: title];
[self setWindowTitle];
}
}
else {
};
}
- (void)selectSessionAtIndexAction:(id)sender
{
[TABVIEW selectTabViewItemAtIndex:[sender tag]];
}
- (void) newSessionInTabAtIndex: (id) sender
{
[self addNewSession: [sender representedObject]];
}
- (void) insertSession: (PTYSession *) aSession atIndex: (int) index
{
NSTabViewItem *aTabViewItem;
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal insertSession: 0x%x atIndex: %d]",
__FILE__, __LINE__, aSession, index);
#endif
if(aSession == nil)
return;
if ([TABVIEW indexOfTabViewItemWithIdentifier: aSession] == NSNotFound)
{
// create a new tab
aTabViewItem = [[NSTabViewItem alloc] initWithIdentifier: aSession];
[aSession setTabViewItem: aTabViewItem];
NSParameterAssert(aTabViewItem != nil);
[aTabViewItem setLabel: [aSession name]];
[aTabViewItem setView: [aSession view]];
//[[aSession SCROLLVIEW] setLineScroll: charHeight];
//[[aSession SCROLLVIEW] setPageScroll: HEIGHT*charHeight/2];
[TABVIEW insertTabViewItem: aTabViewItem atIndex: index];
[aTabViewItem release];
[TABVIEW selectTabViewItemAtIndex: index];
if([self windowInited] && !_fullScreen)
[[self window] makeKeyAndOrderFront: self];
[[iTermController sharedInstance] setCurrentTerminal: self];
[self setWindowSize];
}
}
- (void) closeSession: (PTYSession*) aSession
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, aSession);
#endif
NSTabViewItem *aTabViewItem;
int numberOfSessions;
if([TABVIEW indexOfTabViewItemWithIdentifier: aSession] == NSNotFound)
return;
numberOfSessions = [TABVIEW numberOfTabViewItems];
if(numberOfSessions == 1 && [self windowInited])
{
[[self window] close];
}
else {
// now get rid of this session
aTabViewItem = [aSession tabViewItem];
[aSession terminate];
[TABVIEW removeTabViewItem: aTabViewItem];
}
}
- (IBAction) closeCurrentSession: (id) sender
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal closeCurrentSession]",
__FILE__, __LINE__);
#endif
PTYSession *aSession = [[TABVIEW selectedTabViewItem] identifier];
if ([aSession exited] ||
![[PreferencePanel sharedInstance] promptOnClose] || [[PreferencePanel sharedInstance] onlyWhenMoreTabs] ||
(NSRunAlertPanel([NSString stringWithFormat:@"%@ #%d", [aSession name], [aSession 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))
[self closeSession:[[TABVIEW selectedTabViewItem] identifier]];
}
- (IBAction)previousSession:(id)sender
{
NSTabViewItem *tvi=[TABVIEW selectedTabViewItem];
[TABVIEW selectPreviousTabViewItem: sender];
if (tvi==[TABVIEW selectedTabViewItem]) [TABVIEW selectTabViewItemAtIndex: [TABVIEW numberOfTabViewItems]-1];
}
- (IBAction) nextSession:(id)sender
{
NSTabViewItem *tvi=[TABVIEW selectedTabViewItem];
[TABVIEW selectNextTabViewItem: sender];
if (tvi==[TABVIEW selectedTabViewItem]) [TABVIEW selectTabViewItemAtIndex: 0];
}
- (NSString *) currentSessionName
{
PTYSession* session = [self currentSession];
return [session windowTitle] ? [session windowTitle] : [session defaultName];
}
- (void) setCurrentSessionName: (NSString *) theSessionName
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal setCurrentSessionName]",
__FILE__, __LINE__);
#endif
NSMutableString *title = [NSMutableString string];
PTYSession *aSession = [[TABVIEW selectedTabViewItem] identifier];
if(theSessionName != nil)
{
[aSession setName: theSessionName];
[aSession setDefaultName: theSessionName];
}
else {
NSString *progpath = [NSString stringWithFormat: @"%@ #%d", [[[[aSession SHELL] path] pathComponents] lastObject], [TABVIEW indexOfTabViewItem:[TABVIEW selectedTabViewItem]]];
if ([aSession exited])
[title appendString:@"Finish"];
else
[title appendString:progpath];
[aSession setName: title];
[aSession setDefaultName: title];
}
}
- (PTYSession *) currentSession
{
return [[TABVIEW selectedTabViewItem] identifier];
}
- (int) currentSessionIndex
{
return ([TABVIEW indexOfTabViewItem:[TABVIEW selectedTabViewItem]]);
}
- (void) dealloc
{
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Release all our sessions
NSTabViewItem *aTabViewItem;
for(;[TABVIEW numberOfTabViewItems];)
{
aTabViewItem = [TABVIEW tabViewItemAtIndex:0];
[[aTabViewItem identifier] terminate];
[TABVIEW removeTabViewItem: aTabViewItem];
}
[commandField release];
[FONT release];
[NAFONT release];
[oldFont release];
[oldNAFont release];
[_toolbarController release];
[super dealloc];
}
- (void)startProgram:(NSString *)program
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal startProgram:%@]",
__FILE__, __LINE__, program );
#endif
[[self currentSession] startProgram:program
arguments:[NSArray array]
environment:[NSDictionary dictionary]];
}
- (void)startProgram:(NSString *)program arguments:(NSArray *)prog_argv
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal startProgram:%@ arguments:%@]",
__FILE__, __LINE__, program, prog_argv );
#endif
[[self currentSession] startProgram:program
arguments:prog_argv
environment:[NSDictionary dictionary]];
}
- (void)startProgram:(NSString *)program
arguments:(NSArray *)prog_argv
environment:(NSDictionary *)prog_env
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal startProgram:%@ arguments:%@]",
__FILE__, __LINE__, program, prog_argv );
#endif
[[self currentSession] startProgram:program
arguments:prog_argv
environment:prog_env];
if ([[[self window] title] compare:@"Window"]==NSOrderedSame)
[self setWindowTitle];
}
- (void) setWidth: (int) width height: (int) height
{
WIDTH = width;
HEIGHT = height;
}
- (int)width;
{
return WIDTH;
}
- (int)height;
{
return HEIGHT;
}
- (NSRect)oldFrame
{
return oldFrame;
}
- (int)oldWidth
{
return oldWidth;
}
- (int)oldHeight;
{
return oldHeight;
}
- (void)setCharSizeUsingFont: (NSFont *)font
{
int i;
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSSize sz;
[dic setObject:font forKey:NSFontAttributeName];
sz = [@"W" sizeWithAttributes:dic];
charWidth = ceil(sz.width * charHorizontalSpacingMultiplier);
charHeight = ([font defaultLineHeightForFont] * charVerticalSpacingMultiplier);
for(i=0;i<[TABVIEW numberOfTabViewItems]; i++)
{
PTYSession* session = [[TABVIEW tabViewItemAtIndex:i] identifier];
[[session TEXTVIEW] setCharWidth: charWidth];
[[session TEXTVIEW] setLineHeight: charHeight];
}
[[self window] setResizeIncrements: NSMakeSize(charWidth, charHeight)];
}
- (int)charWidth
{
return charWidth;
}
- (int)charHeight
{
return charHeight;
}
- (float) charSpacingHorizontal
{
return (charHorizontalSpacingMultiplier);
}
- (float) charSpacingVertical
{
return (charVerticalSpacingMultiplier);
}
- (float) oldCharSpacingVertical
{
return (oldCharVerticalSpacingMultiplier);
}
- (float) oldCharSpacingHorizontal
{
return (oldCharHorizontalSpacingMultiplier);
}
- (void)setWindowSize
{
NSSize size, vsize, winSize, tabViewSize;
NSWindow *thisWindow = [self window];
NSRect aRect;
NSPoint topLeft;
float max_height;
BOOL vmargin_added = NO;
BOOL hasScrollbar = !_fullScreen && ![[PreferencePanel sharedInstance] hideScrollbar];
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PseudoTerminal setWindowSize] (%d,%d)", __FILE__, __LINE__, WIDTH, HEIGHT );
#endif
if([self windowInited] == NO)
return;
if (!_resizeInProgressFlag) {
_resizeInProgressFlag = YES;
if (!_fullScreen) {
aRect = [thisWindow contentRectForFrameRect:[[thisWindow screen] visibleFrame]];
if ([TABVIEW numberOfTabViewItems] > 1 || ![[PreferencePanel sharedInstance] hideTab])
aRect.size.height -= [tabBarControl frame].size.height;
max_height = aRect.size.height / charHeight;
if (WIDTH<20) WIDTH=20;
if (HEIGHT<2) HEIGHT=2;
if (HEIGHT>max_height) HEIGHT=max_height;
// desired size of textview
vsize.width = charWidth * WIDTH + MARGIN * 2;