-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPTYTextView.m
3031 lines (2586 loc) · 82.6 KB
/
PTYTextView.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: PTYTextView.m,v 1.325 2009-02-06 14:33:17 delx Exp $
/*
** PTYTextView.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: NSTextView subclass. The view object for the VT100 screen.
**
** 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.
*/
#define DEBUG_ALLOC 0
#define DEBUG_METHOD_TRACE 0
#define GREED_KEYDOWN 1
#import <iTerm/iTerm.h>
#import <iTerm/PTYTextView.h>
#import <iTerm/PseudoTerminal.h>
#import <iTerm/PTYSession.h>
#import <iTerm/VT100Screen.h>
#import <iTerm/FindPanelWindowController.h>
#import <iTerm/PreferencePanel.h>
#import <iTerm/PTYScrollView.h>
#import <iTerm/PTYTask.h>
#import <iTerm/iTermController.h>
#import "ITConfigPanelController.h"
#import <iTerm/Tree.h>
#include <sys/time.h>
#include <math.h>
static NSCursor* textViewCursor = nil;
@implementation PTYTextView
+ (void) initialize
{
NSImage *ibeamImage = [[NSCursor IBeamCursor] image];
NSPoint hotspot = [[NSCursor IBeamCursor] hotSpot];
NSImage *aCursorImage = [ibeamImage copy];
NSImage *reverseCursorImage = [ibeamImage copy];
[reverseCursorImage lockFocus];
[[NSColor whiteColor] set];
NSRectFill(NSMakeRect(0,0,[reverseCursorImage size].width,[reverseCursorImage size].height));
[ibeamImage compositeToPoint:NSMakePoint(0,0) operation:NSCompositeDestinationIn];
[reverseCursorImage unlockFocus];
[aCursorImage lockFocus];
[reverseCursorImage compositeToPoint:NSMakePoint(2,0) operation:NSCompositePlusLighter];
[aCursorImage unlockFocus];
textViewCursor = [[NSCursor alloc] initWithImage:aCursorImage hotSpot:hotspot];
}
+ (NSCursor *) textViewCursor
{
return textViewCursor;
}
- (id)initWithFrame: (NSRect) aRect
{
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
self = [super initWithFrame: aRect];
dataSource=_delegate=markedTextAttributes=NULL;
[self setMarkedTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSColor yellowColor], NSBackgroundColorAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
nafont, NSFontAttributeName,
[NSNumber numberWithInt:2],NSUnderlineStyleAttributeName,
NULL]];
CURSOR=YES;
drawAllowed = YES;
lastFindX = oldStartX = startX = -1;
markedText=nil;
gettimeofday(&lastBlink, NULL);
[[self window] useOptimizedDrawing:YES];
// register for drag and drop
[self registerForDraggedTypes: [NSArray arrayWithObjects:
NSFilenamesPboardType,
NSStringPboardType,
nil]];
return (self);
}
- (BOOL) resignFirstResponder
{
//NSLog(@"0x%x: %s", self, __PRETTY_FUNCTION__);
return (YES);
}
- (BOOL) becomeFirstResponder
{
//NSLog(@"0x%x: %s", self, __PRETTY_FUNCTION__);
return (YES);
}
- (void)viewWillMoveToWindow:(NSWindow *)win
{
//NSLog(@"0x%x: %s, will move view from %@ to %@", self, __PRETTY_FUNCTION__, [self window], win);
if (!win && [self window] && trackingRectTag) {
//NSLog(@"remove tracking");
[self removeTrackingRect:trackingRectTag];
trackingRectTag = 0;
}
[super viewWillMoveToWindow:win];
}
- (void)viewDidMoveToWindow
{
//NSLog(@"0x%x: %s, moved view to %@", self, __PRETTY_FUNCTION__, [self window]);
if ([self window]) {
//NSLog(@"add tracking");
trackingRectTag = [self addTrackingRect:[self frame] owner: self userData: nil assumeInside: NO];
}
}
- (void) dealloc
{
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
int i;
if(mouseDownEvent != nil)
{
[mouseDownEvent release];
mouseDownEvent = nil;
}
//NSLog(@"remove tracking");
if(trackingRectTag)
[self removeTrackingRect:trackingRectTag];
[[NSNotificationCenter defaultCenter] removeObserver:self];
for(i=0;i<256;i++) {
[colorTable[i] release];
}
[defaultFGColor release];
[defaultBGColor release];
[defaultBoldColor release];
[selectionColor release];
[defaultCursorColor release];
[font release];
[nafont release];
[markedTextAttributes release];
[markedText release];
[super dealloc];
#if DEBUG_ALLOC
NSLog(@"%s: 0x%x, done", __PRETTY_FUNCTION__, self);
#endif
}
- (BOOL)shouldDrawInsertionPoint
{
#if 0 // DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView shouldDrawInsertionPoint]",
__FILE__, __LINE__);
#endif
return NO;
}
- (BOOL)isFlipped
{
return YES;
}
- (BOOL)isOpaque
{
return YES;
}
- (BOOL) antiAlias
{
return (antiAlias);
}
- (void) setAntiAlias: (BOOL) antiAliasFlag
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView setAntiAlias: %d]",
__FILE__, __LINE__, antiAliasFlag);
#endif
antiAlias = antiAliasFlag;
[self setNeedsDisplay:YES];
}
- (BOOL) disableBold
{
return (disableBold);
}
- (void) setDisableBold: (BOOL) boldFlag
{
disableBold = boldFlag;
[self setNeedsDisplay:YES];
}
- (BOOL) blinkingCursor
{
return (blinkingCursor);
}
- (void) setBlinkingCursor: (BOOL) bFlag
{
blinkingCursor = bFlag;
}
- (NSDictionary*) markedTextAttributes
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView selectedTextAttributes]",
__FILE__, __LINE__);
#endif
return markedTextAttributes;
}
- (void) setMarkedTextAttributes: (NSDictionary *) attr
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView setSelectedTextAttributes:%@]",
__FILE__, __LINE__,attr);
#endif
[markedTextAttributes release];
[attr retain];
markedTextAttributes=attr;
}
- (void) setFGColor:(NSColor*)color
{
[defaultFGColor release];
[color retain];
defaultFGColor=color;
[self setNeedsDisplay:YES];
// reset our default character attributes
}
- (void) setBGColor:(NSColor*)color
{
[defaultBGColor release];
[color retain];
defaultBGColor=color;
// bg = [bg colorWithAlphaComponent: [[SESSION backgroundColor] alphaComponent]];
// fg = [fg colorWithAlphaComponent: [[SESSION foregroundColor] alphaComponent]];
[self setNeedsDisplay:YES];
}
- (void) setBoldColor: (NSColor*)color
{
[defaultBoldColor release];
[color retain];
defaultBoldColor=color;
[self setNeedsDisplay:YES];
}
- (void) setCursorColor: (NSColor*)color
{
[defaultCursorColor release];
[color retain];
defaultCursorColor=color;
[self setNeedsDisplay:YES];
}
- (void) setSelectedTextColor: (NSColor *) aColor
{
[selectedTextColor release];
[aColor retain];
selectedTextColor = aColor;
[self setNeedsDisplay:YES];
}
- (void) setCursorTextColor:(NSColor*) aColor
{
[cursorTextColor release];
[aColor retain];
cursorTextColor = aColor;
[self setNeedsDisplay:YES];
}
- (NSColor *) cursorTextColor
{
return (cursorTextColor);
}
- (NSColor *) selectedTextColor
{
return (selectedTextColor);
}
- (NSColor *) defaultFGColor
{
return defaultFGColor;
}
- (NSColor *) defaultBGColor
{
return defaultBGColor;
}
- (NSColor *) defaultBoldColor
{
return defaultBoldColor;
}
- (NSColor *) defaultCursorColor
{
return defaultCursorColor;
}
- (void) setColorTable:(int) index color:(NSColor *) c
{
[colorTable[index] release];
[c retain];
colorTable[index]=c;
[self setNeedsDisplay:YES];
}
- (NSColor *) colorForCode:(int) index
{
NSColor *color;
if (index & DEFAULT_FG_COLOR_CODE) {
// special colors?
switch (index) {
case SELECTED_TEXT:
color = selectedTextColor;
break;
case CURSOR_TEXT:
color = cursorTextColor;
break;
case DEFAULT_BG_COLOR_CODE:
color = defaultBGColor;
break;
default:
if(index & BOLD_MASK) {
color = index-BOLD_MASK == DEFAULT_BG_COLOR_CODE ? defaultBGColor : [self defaultBoldColor];
}
else {
color = defaultFGColor;
}
}
}
else {
if([self disableBold] && (index & BOLD_MASK) && (index % 256 < 8)) {
index = index - BOLD_MASK + 8;
}
color = colorTable[index & 0xff];
}
return color;
}
- (NSColor *) selectionColor
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView selectionColor]",
__FILE__, __LINE__);
#endif
return selectionColor;
}
- (void) setSelectionColor: (NSColor *) aColor
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView setSelectionColor:%@]",
__FILE__, __LINE__,aColor);
#endif
[selectionColor release];
[aColor retain];
selectionColor=aColor;
[self setNeedsDisplay:YES];
}
- (NSFont *)font
{
return font;
}
- (NSFont *)nafont
{
return nafont;
}
- (void) setFont:(NSFont*)aFont nafont:(NSFont *)naFont;
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSSize sz;
[dic setObject:aFont forKey:NSFontAttributeName];
sz = [@"W" sizeWithAttributes:dic];
charWidthWithoutSpacing = sz.width;
charHeightWithoutSpacing = [aFont defaultLineHeightForFont];
[font release];
[aFont retain];
font=aFont;
[nafont release];
[naFont retain];
nafont=naFont;
[self setMarkedTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSColor yellowColor], NSBackgroundColorAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
nafont, NSFontAttributeName,
[NSNumber numberWithInt:2],NSUnderlineStyleAttributeName,
NULL]];
[self setNeedsDisplay:YES];
}
- (void)changeFont:(id)fontManager
{
if ([ITConfigPanelController onScreen])
[[ITConfigPanelController singleInstance] changeFont:fontManager];
else
[super changeFont:fontManager];
}
- (id) dataSource
{
return (dataSource);
}
- (void) setDataSource: (id) aDataSource
{
dataSource = aDataSource;
}
- (id) delegate
{
return _delegate;
}
- (void) setDelegate: (id) aDelegate
{
_delegate = aDelegate;
}
- (float) lineHeight
{
return (lineHeight);
}
- (void) setLineHeight: (float) aLineHeight
{
lineHeight = aLineHeight;
}
- (float) lineWidth
{
return (lineWidth);
}
- (void) setLineWidth: (float) aLineWidth
{
lineWidth = aLineWidth;
}
- (float) charWidth
{
return (charWidth);
}
- (void) setCharWidth: (float) width
{
charWidth = width;
}
- (void)updateDirtyRects
{
int WIDTH = [dataSource width];
char* dirty;
int lineStart;
int lineEnd;
// Check each line for dirty selected text
// If any is found then deselect everything
dirty = [dataSource dirty];
lineStart = [dataSource numberOfLines] - [dataSource height];
lineEnd = [dataSource numberOfLines];
for(int y = lineStart; y < lineEnd && startX > -1; y++) {
for(int x = 0; x < WIDTH; x++) {
BOOL isSelected = [self _isCharSelectedInRow:y col:x checkOld:NO];
int cursorX = [dataSource cursorX] - 1;
int cursorY = [dataSource cursorY] + [dataSource numberOfLines] - [dataSource height] - 1;
BOOL isCursor = (x == cursorX && y == cursorY);
if(dirty[x] && isSelected && !isCursor) {
// Don't call [self deselect] as it would recurse back here
startX = -1;
break;
}
}
dirty += WIDTH;
}
// Time to redraw blinking text?
struct timeval now;
BOOL redrawBlink = NO;
gettimeofday(&now, NULL);
if(now.tv_sec*10+now.tv_usec/100000 >= lastBlink.tv_sec*10+lastBlink.tv_usec/100000+7) {
blinkShow = !blinkShow;
lastBlink = now;
redrawBlink = YES;
}
// Visible chars that have changed selection status are dirty
// Also mark blinking text as dirty if needed
lineStart = [self visibleRect].origin.y / lineHeight;
lineEnd = lineStart + ceil([self visibleRect].size.height / lineHeight);
if(lineStart < 0) lineStart = 0;
if(lineEnd > [dataSource numberOfLines]) lineEnd = [dataSource numberOfLines];
for(int y = lineStart; y < lineEnd; y++) {
screen_char_t* theLine = [dataSource getLineAtIndex:y];
for(int x = 0; x < WIDTH; x++) {
BOOL isSelected = [self _isCharSelectedInRow:y col:x checkOld:NO];
BOOL wasSelected = [self _isCharSelectedInRow:y col:x checkOld:YES];
BOOL blinked = redrawBlink && (theLine[x].fg_color & BLINK_MASK);
if(isSelected != wasSelected || blinked) {
NSRect dirtyRect = [self visibleRect];
dirtyRect.origin.y = y*lineHeight;
dirtyRect.size.height = lineHeight;
[self setNeedsDisplayInRect:dirtyRect];
break;
}
}
}
oldStartX=startX; oldStartY=startY; oldEndX=endX; oldEndY=endY;
// Redraw lines with dirty characters
dirty = [dataSource dirty];
lineStart = [dataSource numberOfLines] - [dataSource height];
lineEnd = [dataSource numberOfLines];
for(int y = lineStart; y < lineEnd; y++) {
for(int x = 0; x < WIDTH; x++) {
if(dirty[x]) {
NSRect dirtyRect = [self visibleRect];
dirtyRect.origin.y = y*lineHeight;
dirtyRect.size.height = lineHeight;
[self setNeedsDisplayInRect:dirtyRect];
break;
}
}
dirty += WIDTH;
}
[dataSource resetDirty];
}
// We override this method since both refresh and window resize can conflict
// resulting in this happening twice So we do not allow the size to be set
// larger than what the data source can fill
- (void)setFrameSize:(NSSize)frameSize
{
// Force the height to always be correct
frameSize.height = [dataSource numberOfLines] * lineHeight;
[super setFrameSize:frameSize];
}
- (void)refresh
{
if(dataSource == nil) return;
// reset tracking rect
if(trackingRectTag) {
[self removeTrackingRect:trackingRectTag];
}
trackingRectTag = [self addTrackingRect:[self visibleRect] owner:self userData:nil assumeInside:NO];
// number of lines that have disappeared if circular buffer is full
int scrollbackOverflow = [dataSource scrollbackOverflow];
[dataSource resetScrollbackOverflow];
// frame size changed?
int height = [dataSource numberOfLines] * lineHeight;
NSRect frame = [self frame];
if(height != frame.size.height) {
// Grow to allow space for drawing the new lines
// XXX - EPIC HACK below
// NSClipView has setCopiesOnScroll:YES by default. According to Apple
// this means it will do a quick copy of all the unchanged portion of
// the view when scrolling. It will then expose the new section causing
// drawRect to be called with it.
// NSClipView does not seem to be doing it's job. The setFrame call below
// exposes the *entire* visible rectangle. This has the obvious
// performance penalty of forcing a full screen redraw whenever the
// frame is extended due to scrolling, eg a new line appears in the
// terminal. To work around this we disable drawing during the frame
// resize and then manually expose the new portion of the frame.
float diff = height - frame.size.height;
NSRect old = [self visibleRect];
if(diff > 0) {
drawAllowed = NO;
[self displayIfNeeded];
}
// Resize the frame
frame.size.height = height;
[self setFrame:frame];
// XXX - resume hack
NSRect new = [self visibleRect];
if(diff > 0) {
[self display];
drawAllowed = YES;
NSRect dirty = new;
dirty.origin.y = old.origin.y + old.size.height;
dirty.size.height = new.origin.y + new.size.height - dirty.origin.y;
[self setNeedsDisplayInRect:dirty];
}
}
else if(scrollbackOverflow > 0) {
NSScrollView* scrollView = [self enclosingScrollView];
NSClipView* clipView = [scrollView contentView];
float amount = [scrollView verticalLineScroll] * scrollbackOverflow;
BOOL userScroll = [(PTYScroller*)([scrollView verticalScroller]) userScroll];
// Keep correct selection highlighted
startY -= scrollbackOverflow;
if(startY < 0) startX = -1;
endY -= scrollbackOverflow;
oldStartY -= scrollbackOverflow;
if(oldStartY < 0) oldStartX = -1;
oldEndY -= scrollbackOverflow;
// Keep the users' current scroll position, nothing to redraw
if(userScroll) {
NSRect scrollRect = [self visibleRect];
scrollRect.origin.y -= amount;
if(scrollRect.origin.y < 0) scrollRect.origin.y = 0;
[clipView setCopiesOnScroll:NO];
[self scrollRectToVisible:scrollRect];
[clipView setCopiesOnScroll:YES];
return;
}
// Shift the old content upwards
if(scrollbackOverflow < [dataSource height] && !userScroll) {
[self scrollRect:[self visibleRect] by:NSMakeSize(0, -amount)];
}
}
[self updateDirtyRects];
}
- (NSRect)adjustScroll:(NSRect)proposedVisibleRect
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView adjustScroll]", __FILE__, __LINE__ );
#endif
proposedVisibleRect.origin.y=(int)(proposedVisibleRect.origin.y/lineHeight+0.5)*lineHeight;
return proposedVisibleRect;
}
-(void) scrollLineUp: (id) sender
{
NSRect scrollRect;
scrollRect= [self visibleRect];
scrollRect.origin.y-=[[self enclosingScrollView] verticalLineScroll];
if (scrollRect.origin.y<0) scrollRect.origin.y=0;
//NSLog(@"%f/%f",[[self enclosingScrollView] verticalLineScroll],[[self enclosingScrollView] verticalPageScroll]);
[self scrollRectToVisible: scrollRect];
}
-(void) scrollLineDown: (id) sender
{
NSRect scrollRect;
scrollRect= [self visibleRect];
scrollRect.origin.y+=[[self enclosingScrollView] verticalLineScroll];
[self scrollRectToVisible: scrollRect];
}
-(void) scrollPageUp: (id) sender
{
NSRect scrollRect;
scrollRect= [self visibleRect];
scrollRect.origin.y-= scrollRect.size.height - [[self enclosingScrollView] verticalPageScroll];
[self scrollRectToVisible: scrollRect];
}
-(void) scrollPageDown: (id) sender
{
NSRect scrollRect;
scrollRect= [self visibleRect];
scrollRect.origin.y+= scrollRect.size.height - [[self enclosingScrollView] verticalPageScroll];
[self scrollRectToVisible: scrollRect];
}
-(void) scrollHome
{
NSRect scrollRect;
scrollRect= [self visibleRect];
scrollRect.origin.y = 0;
[self scrollRectToVisible: scrollRect];
}
- (void)scrollEnd
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView scrollEnd]", __FILE__, __LINE__ );
#endif
if([dataSource numberOfLines] <= 0) return;
NSRect lastLine = [self visibleRect];
lastLine.origin.y = ([dataSource numberOfLines] - 1) * lineHeight;
lastLine.size.height = lineHeight;
[self scrollRectToVisible:lastLine];
}
- (void)scrollToSelection
{
NSRect aFrame;
aFrame.origin.x = 0;
aFrame.origin.y = startY * lineHeight;
aFrame.size.width = [self frame].size.width;
aFrame.size.height = (endY - startY + 1) *lineHeight;
[self scrollRectToVisible: aFrame];
}
-(void) hideCursor
{
CURSOR=NO;
}
-(void) showCursor
{
CURSOR=YES;
}
- (void)drawRect:(NSRect)rect
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(0x%x):-[PTYTextView drawRect:(%f,%f,%f,%f) frameRect: (%f,%f,%f,%f)]",
__PRETTY_FUNCTION__, self,
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height,
[self frame].origin.x, [self frame].origin.y, [self frame].size.width, [self frame].size.height);
#endif
if(!drawAllowed) return;
if(lineHeight <= 0 || lineWidth <= 0) return;
// Configure graphics
[[NSGraphicsContext currentContext] setShouldAntialias: antiAlias];
[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeCopy];
// Where to start drawing?
int lineStart = rect.origin.y / lineHeight;
int lineEnd = lineStart + ceil(rect.size.height / lineHeight);
// Ensure valid line ranges
if(lineStart < 0) lineStart = 0;
if(lineEnd > [dataSource numberOfLines]) lineEnd = [dataSource numberOfLines];
// Draw each line
for(int line = lineStart; line < lineEnd; line++) {
NSRect lineRect = [self visibleRect];
lineRect.origin.y = line*lineHeight;
lineRect.size.height = lineHeight;
if([self needsToDrawRect:lineRect]) {
/// NSLog(@"drawing %d", line);
[self _drawLine:line AtY:line*lineHeight];
}
}
// Draw cursor
[self _drawCursor];
}
- (void)keyDown:(NSEvent*)event
{
id delegate = [self delegate];
unsigned int modflag = [event modifierFlags];
BOOL prev = [self hasMarkedText];
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYTextView keyDown:%@]", __FILE__, __LINE__, event );
#endif
keyIsARepeat = [event isARepeat];
// Hide the cursor
[NSCursor setHiddenUntilMouseMoves:YES];
// Should we process the event immediately in the delegate?
if([delegate hasKeyMappingForEvent:event highPriority:YES] ||
(modflag & NSNumericPadKeyMask) || (modflag & NSFunctionKeyMask) ||
((modflag & NSAlternateKeyMask) && [delegate optionKey] != OPT_NORMAL))
{
[delegate keyDown:event];
return;
}
// Let the IME process key events
IM_INPUT_INSERT = NO;
[self interpretKeyEvents:[NSArray arrayWithObject:event]];
// If the IME didn't want it, pass it on to the delegate
if(!prev && !IM_INPUT_INSERT && ![self hasMarkedText]) {
[delegate keyDown:event];
}
}
- (BOOL) keyIsARepeat
{
return (keyIsARepeat);
}
- (void) otherMouseDown: (NSEvent *) event
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s: %@]", __PRETTY_FUNCTION__, event);
#endif
NSPoint locationInWindow, locationInTextView;
locationInWindow = [event locationInWindow];
locationInTextView = [self convertPoint: locationInWindow fromView: nil];
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
if (([[self delegate] xtermMouseReporting])
&& (locationInTextView.y > visibleRect.origin.y) && !([event modifierFlags] & NSAlternateKeyMask))
// && ([event modifierFlags] & NSCommandKeyMask == 0))
{
int rx, ry;
rx = (locationInTextView.x-MARGIN - visibleRect.origin.x)/charWidth;
ry = (locationInTextView.y - visibleRect.origin.y)/lineHeight;
if (rx < 0) rx = -1;
if (ry < 0) ry = -1;
VT100Terminal *terminal = [dataSource terminal];
PTYSession* session = [dataSource session];
int bnum = [event buttonNumber];
if (bnum == 2) bnum = 1;
switch ([terminal mouseMode]) {
case MOUSE_REPORTING_NORMAL:
case MOUSE_REPORTING_BUTTON_MOTION:
case MOUSE_REPORTING_ALL_MOTION:
reportingMouseDown = YES;
[session writeTask:[terminal mousePress:bnum withModifiers:[event modifierFlags] atX:rx Y:ry]];
return;
break;
case MOUSE_REPORTING_NONE:
case MOUSE_REPORTING_HILITE:
// fall through
break;
}
}
if([[PreferencePanel sharedInstance] pasteFromClipboard])
[self paste: nil];
else
[self pasteSelection: nil];
}
- (void)otherMouseUp:(NSEvent *)event
{
NSPoint locationInWindow, locationInTextView;
locationInWindow = [event locationInWindow];
locationInTextView = [self convertPoint: locationInWindow fromView: nil];
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
if (([[self delegate] xtermMouseReporting])
&& reportingMouseDown && !([event modifierFlags] & NSAlternateKeyMask))
{
reportingMouseDown = NO;
int rx, ry;
rx = (locationInTextView.x-MARGIN - visibleRect.origin.x)/charWidth;
ry = (locationInTextView.y - visibleRect.origin.y)/lineHeight;
if (rx < 0) rx = -1;
if (ry < 0) ry = -1;
VT100Terminal *terminal = [dataSource terminal];
PTYSession* session = [dataSource session];
switch ([terminal mouseMode]) {
case MOUSE_REPORTING_NORMAL:
case MOUSE_REPORTING_BUTTON_MOTION:
case MOUSE_REPORTING_ALL_MOTION:
[session writeTask:[terminal mouseReleaseAtX:rx Y:ry]];
return;
break;
case MOUSE_REPORTING_NONE:
case MOUSE_REPORTING_HILITE:
// fall through
break;
}
}
[super otherMouseUp:event];
}
- (void)otherMouseDragged:(NSEvent *)event
{
NSPoint locationInWindow, locationInTextView;
locationInWindow = [event locationInWindow];
locationInTextView = [self convertPoint: locationInWindow fromView: nil];
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
if (([[self delegate] xtermMouseReporting])
&& (locationInTextView.y > visibleRect.origin.y)
&& reportingMouseDown && !([event modifierFlags] & NSAlternateKeyMask))
{
int rx, ry;
rx = (locationInTextView.x-MARGIN - visibleRect.origin.x)/charWidth;
ry = (locationInTextView.y - visibleRect.origin.y)/lineHeight;
if (rx < 0) rx = -1;
if (ry < 0) ry = -1;
VT100Terminal *terminal = [dataSource terminal];
PTYSession* session = [dataSource session];
int bnum = [event buttonNumber];
if (bnum == 2) bnum = 1;
switch ([terminal mouseMode]) {
case MOUSE_REPORTING_NORMAL:
case MOUSE_REPORTING_BUTTON_MOTION:
case MOUSE_REPORTING_ALL_MOTION:
[session writeTask:[terminal mouseMotion:bnum withModifiers:[event modifierFlags] atX:rx Y:ry]];
return;
break;
case MOUSE_REPORTING_NONE:
case MOUSE_REPORTING_HILITE:
// fall through
break;
}
}
[super otherMouseDragged:event];
}
- (void) rightMouseDown: (NSEvent *) event
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s: %@]", __PRETTY_FUNCTION__, event);
#endif
NSPoint locationInWindow, locationInTextView;
locationInWindow = [event locationInWindow];
locationInTextView = [self convertPoint: locationInWindow fromView: nil];
NSRect visibleRect = [[self enclosingScrollView] documentVisibleRect];
if (([[self delegate] xtermMouseReporting])
&& (locationInTextView.y > visibleRect.origin.y) && !([event modifierFlags] & NSAlternateKeyMask))
// && ([event modifierFlags] & NSCommandKeyMask == 0))
{
int rx, ry;
rx = (locationInTextView.x-MARGIN - visibleRect.origin.x)/charWidth;
ry = (locationInTextView.y - visibleRect.origin.y)/lineHeight;
if (rx < 0) rx = -1;
if (ry < 0) ry = -1;
VT100Terminal *terminal = [dataSource terminal];
PTYSession* session = [dataSource session];
switch ([terminal mouseMode]) {
case MOUSE_REPORTING_NORMAL:
case MOUSE_REPORTING_BUTTON_MOTION:
case MOUSE_REPORTING_ALL_MOTION:
reportingMouseDown = YES;
[session writeTask:[terminal mousePress:2 withModifiers:[event modifierFlags] atX:rx Y:ry]];
return;
break;