Debian Codegraph @

components/virtualterminal/source/vtemuctl.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 Virtual terminal emulator control
5
6 Alexander Koblov, 2021-2022
7
8 Based on ComPort Library
9 https://sourceforge.net/projects/comport
10 Author:
11 Dejan Crnila, 1998 - 2002
12 Maintainers:
13 Lars B. Dybdahl, 2003
14 Brian Gochnauer, 2010
15 License:
16 Public Domain
17 }
18
19 unit VTEmuCtl;
20
21 {$mode delphi}
22 {$pointermath on}
23
24 interface
25
26 uses
27 LCLType, Classes, Controls, StdCtrls, ExtCtrls, Forms, Messages, Graphics,
28 VTEmuEsc, LCLIntf, Types, LazUtf8, LMessages;
29
30 type
31
32 TOnRxBuf = procedure(Sender: TObject; const Buffer; Count: Integer) of object;
33
34 { TCustomPtyDevice }
35
36 TCustomPtyDevice = class(TComponent)
37 protected
38 FOnRxBuf: TOnRxBuf;
39 FConnected: Boolean;
40 protected
41 procedure SetConnected(AValue: Boolean); virtual; abstract;
42 public
43 function WriteStr(const Str: string): Integer; virtual; abstract;
44 function SetCurrentDir(const Path: String): Boolean; virtual; abstract;
45 function SetScreenSize(aCols, aRows: Integer): Boolean; virtual; abstract;
46 property OnRxBuf: TOnRxBuf read FOnRxBuf write FOnRxBuf;
47 property Connected: Boolean read FConnected write SetConnected default False;
48 end;
49
50 TCustomComTerminal = class; // forward declaration
51
52 // terminal character
53 PComTermChar = ^TComTermChar;
54 TComTermChar = record
55 Ch: TUTF8Char;
56 FrontColor: TColor;
57 BackColor: TColor;
58 Underline: Boolean;
59 Bold: Boolean;
60 end;
61
62 // buffer which holds terminal screen data
63 TComTermBuffer = class
64 private
65 FBuffer: PByte;
66 FTabs: Pointer;
67 FTopLeft: TPoint;
68 FCaretPos: TPoint;
69 FScrollRange: TRect;
70 FOwner: TCustomComTerminal;
71 strict private
72 FRows: Integer;
73 FColumns: Integer;
74 public
75 constructor Create(AOwner: TCustomComTerminal);
76 destructor Destroy; override;
77 procedure Init(ARows, AColumns: Integer);
78 procedure SetChar(Column, Row: Integer; TermChar: TComTermChar);
79 function GetChar(Column, Row: Integer): TComTermChar;
80 procedure SetTab(Column: Integer; Put: Boolean);
81 function GetTab(Column: Integer): Boolean;
82 function NextTab(Column: Integer): Integer;
83 procedure ClearAllTabs;
84 procedure ScrollDown;
85 procedure ScrollUp;
86 procedure EraseScreenLeft(Column, Row: Integer);
87 procedure EraseScreenRight(Column, Row: Integer);
88 procedure EraseLineLeft(Column, Row: Integer);
89 procedure EraseLineRight(Column, Row: Integer);
90 procedure EraseChar(Column, Row, Count: Integer);
91 procedure DeleteChar(Column, Row, Count: Integer);
92 procedure DeleteLine(Row, Count: Integer);
93 procedure InsertLine(Row, Count: Integer);
94 function GetLineLength(Line: Integer): Integer;
95 function GetLastLine: Integer;
96 property Rows: Integer read FRows;
97 property Columns: Integer read FColumns;
98 end;
99
100 // terminal types
101 TTermEmulation = (teVT100orANSI, teVT52, teNone);
102 TTermCaret = (tcBlock, tcUnderline, tcNone);
103 TAdvanceCaret = (acChar, acReturn, acLineFeed, acReverseLineFeed,
104 acTab, acBackspace, acPage);
105 TArrowKeys = (akTerminal, akWindows);
106 TTermAttributes = record
107 FrontColor: TColor;
108 BackColor: TColor;
109 Invert: Boolean;
110 Bold: Boolean;
111 Underline: Boolean;
112 end;
113 TTermMode = record
114 Keys: TArrowKeys;
115 CharSet: Boolean;
116 MouseMode: Boolean;
117 MouseTrack: Boolean;
118 end;
119
120 TEscapeEvent = procedure(Sender: TObject; var EscapeCodes: TEscapeCodes) of object;
121 TUnhandledEvent = procedure(Sender: TObject; Code: TEscapeCode; Data: string) of object;
122 TUnhandledModeEvent = procedure(Sender: TObject; const Data: string; OnOff: Boolean) of object;
123 TStrRecvEvent = procedure(Sender: TObject; var Str: string) of object;
124 TChScreenEvent = procedure(Sender: TObject; Ch: TUTF8Char) of object;
125
126 // communication terminal control
127
128 { TCustomComTerminal }
129
130 TCustomComTerminal = class(TCustomControl)
131 private
132 FPtyDevice: TCustomPtyDevice;
133 FScrollBars: TScrollStyle;
134 FArrowKeys: TArrowKeys;
135 FWantTab: Boolean;
136 FColumns: Integer;
137 FRows: Integer;
138 FVisibleRows: Integer;
139 FLocalEcho: Boolean;
140 FSendLF: Boolean;
141 FAppendLF: Boolean;
142 FForce7Bit: Boolean;
143 FWrapLines: Boolean;
144 FSmoothScroll: Boolean;
145 FAutoFollow : Boolean;
146 FFontHeight: Integer;
147 FFontWidth: Integer;
148 FPartChar: TUTF8Char;
149 FEmulation: TTermEmulation;
150 FCaret: TTermCaret;
151 FCaretPos: TPoint;
152 FSaveCaret: TPoint;
153 FCaretCreated: Boolean;
154 FTopLeft: TPoint;
155 FCaretHeight: Integer;
156 FSaveAttr: TTermAttributes;
157 FBuffer: TComTermBuffer;
158 FMainBuffer: TComTermBuffer;
159 FAlternateBuffer: TComTermBuffer;
160 FParams: TStrings;
161 FEscapeCodes: TEscapeCodes;
162 FTermAttr: TTermAttributes;
163 FTermMode: TTermMode;
164 FOnChar: TChScreenEvent;
165 FOnGetEscapeCodes: TEscapeEvent;
166 FOnUnhandledCode: TUnhandledEvent;
167 FOnUnhandledMode: TUnhandledModeEvent;
168 FOnStrRecieved: TStrRecvEvent;
169 procedure AdvanceCaret(Kind: TAdvanceCaret);
170 function CalculateMetrics: Boolean;
171 procedure CreateEscapeCodes;
172 procedure CreateTerminalCaret;
173 procedure DrawChar(AColumn, ARow: Integer; Ch: TComTermChar);
174 function GetCharAttr: TComTermChar;
175 function GetConnected: Boolean;
176 procedure HideCaret;
177 procedure InitCaret;
178 procedure InvalidatePortion(ARect: TRect);
179 procedure ModifyScrollBar(ScrollBar, ScrollCode, Pos: Integer);
180 procedure SetColumns(const Value: Integer);
181 procedure SetPtyDevice(const Value: TCustomPtyDevice);
182 procedure SetConnected(const Value: Boolean);
183 procedure SetEmulation(const Value: TTermEmulation);
184 procedure SetRows(const Value: Integer);
185 procedure SetScrollBars(const Value: TScrollStyle);
186 procedure SetCaret(const Value: TTermCaret);
187 procedure SetAttributes(AParams: TStrings);
188 procedure SetMode(AParams: TStrings; OnOff: Boolean);
189 procedure ShowCaret;
190 procedure StringReceived(Str: string);
191 procedure PaintTerminal(Rect: TRect);
192 procedure PaintDesign;
193 procedure PutChar(Ch: TUTF8Char);
194 function PutEscapeCode(ACode: TEscapeCode; AParams: TStrings): Boolean;
195 procedure RestoreAttr;
196 procedure RestoreCaretPos;
197 procedure RxBuf(Sender: TObject; const Buffer; Count: Integer);
198 procedure SaveAttr;
199 procedure SaveCaretPos;
200 procedure SendChar(Ch: TUTF8Char);
201 procedure SendCode(Code: TEscapeCode; AParams: TStrings);
202 procedure SendCodeNoEcho(Code: TEscapeCode; AParams: TStrings);
203 procedure MouseEvent(Code: TEscapeCode; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
204 procedure PerformTest(ACh: Char);
205 procedure UpdateScrollPos;
206 procedure UpdateScrollRange;
207 procedure WrapLine(AWidth: Integer);
208 protected
209 procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
210 procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
211 procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
212 procedure WMHScroll(var Message: TWMHScroll); message WM_HSCROLL;
213 procedure WMKillFocus(var Message: TWMSetFocus); message WM_KILLFOCUS;
214 procedure WMLButtonDown(var Message: TLMLButtonDown); message WM_LBUTTONDOWN;
215 procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
216 procedure WMSize(var Msg: TWMSize); message WM_SIZE;
217 procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
218 function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
219 procedure CreateParams(var Params: TCreateParams); override;
220 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
221 procedure KeyPress(var Key: Char); override;
222 procedure UTF8KeyPress(var UTF8Key: TUTF8Char); override;
223 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
224 procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
225 procedure CreateWnd; override;
226 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
227 procedure Paint; override;
228 procedure DoChar(Ch: TUTF8Char); dynamic;
229 procedure DoGetEscapeCodes(var EscapeCodes: TEscapeCodes); dynamic;
230 procedure DoStrRecieved(var Str: string); dynamic;
231 procedure DoUnhandledCode(Code: TEscapeCode; Data: string); dynamic;
232 procedure DoUnhandledMode(const Data: string; OnOff: Boolean); dynamic;
233 function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
234 public
235 constructor Create(AOwner: TComponent); override;
236 destructor Destroy; override;
237 procedure ClearScreen;
238 procedure MoveCaret(AColumn, ARow: Integer);
239 procedure Write(const Buffer:string; Size: Integer);
240 procedure WriteStr(const Str: string);
241 procedure WriteEscCode(ACode: TEscapeCode; AParams: TStrings);
242 procedure LoadFromStream(Stream: TStream);
243 procedure SaveToStream(Stream: TStream);
244 procedure SelectFont;
245 property AppendLF: Boolean read FAppendLF write FAppendLF default False;
246 property AutoFollow : Boolean read FAutoFollow write FAutoFollow default True;
247 property ArrowKeys: TArrowKeys read FArrowKeys write FArrowKeys default akTerminal;
248 property Caret: TTermCaret read FCaret write SetCaret default tcBlock;
249 property Connected: Boolean read GetConnected write SetConnected stored False;
250 property PtyDevice: TCustomPtyDevice read FPtyDevice write SetPtyDevice;
251 property Columns: Integer read FColumns write SetColumns default 80;
252 property Emulation: TTermEmulation read FEmulation write SetEmulation;
253 property EscapeCodes: TEscapeCodes read FEscapeCodes;
254 property Force7Bit: Boolean read FForce7Bit write FForce7Bit default False;
255 property LocalEcho: Boolean read FLocalEcho write FLocalEcho default False;
256 property SendLF: Boolean read FSendLF write FSendLF default False;
257 property ScrollBars: TScrollStyle read FScrollBars write SetScrollBars;
258 property SmoothScroll: Boolean read FSmoothScroll write FSmoothScroll default False;
259 property Rows: Integer read FRows write SetRows default 24;
260 property WantTab: Boolean read FWantTab write FWantTab default False;
261 property WrapLines: Boolean read FWrapLines write FWrapLines default False;
262 property OnChar: TChScreenEvent read FOnChar write FOnChar;
263 property OnGetEscapeCodes: TEscapeEvent
264 read FOnGetEscapeCodes write FOnGetEscapeCodes;
265 property OnStrRecieved: TStrRecvEvent
266 read FOnStrRecieved write FOnStrRecieved;
267 property OnUnhandledMode: TUnhandledModeEvent
268 read FOnUnhandledMode write FOnUnhandledMode;
269 property OnUnhandledCode: TUnhandledEvent
270 read FOnUnhandledCode write FOnUnhandledCode;
271 end;
272
273 // publish properties
274 TVirtualTerminal = class(TCustomComTerminal)
275 published
276 property Align;
277 property AppendLF;
278 property ArrowKeys;
279 property BorderStyle;
280 property Color;
281 property Columns;
282 property PtyDevice;
283 property Connected;
284 property DragCursor;
285 property DragMode;
286 property Emulation;
287 property Enabled;
288 property Font;
289 property Force7Bit;
290 property Hint;
291 property LocalEcho;
292 property ParentColor;
293 property ParentShowHint;
294 property PopupMenu;
295 property Rows;
296 property ScrollBars;
297 property SendLF;
298 property ShowHint;
299 property SmoothScroll;
300 property TabOrder;
301 property TabStop default True;
302 property Caret;
303 property Visible;
304 property WantTab;
305 property WrapLines;
306 property Anchors;
307 property AutoSize;
308 property Constraints;
309 property DragKind;
310 property OnChar;
311 property OnClick;
312 property OnDblClick;
313 property OnDragDrop;
314 property OnDragOver;
315 property OnEndDrag;
316 property OnEnter;
317 property OnExit;
318 property OnGetEscapeCodes;
319 property OnKeyDown;
320 property OnKeyPress;
321 property OnKeyUp;
322 property OnMouseDown;
323 property OnMouseMove;
324 property OnMouseUp;
325 property OnStartDrag;
326 property OnStrRecieved;
327 property OnUnhandledCode;
328 property OnConstrainedResize;
329 property OnDockDrop;
330 property OnEndDock;
331 property OnMouseWheel;
332 property OnMouseWheelDown;
333 property OnMouseWheelUp;
334 property OnResize;
335 property OnStartDock;
336 property OnUnDock;
337 property OnContextPopup;
338 end;
339
340 implementation
341
342 uses
343 SysUtils, Dialogs, Math, VTColorTable, VTWideCharWidth;
344
345 const
346 TMPF_FIXED_PITCH = $01;
347
348 (*****************************************
349 * TComTermBuffer class *
350 *****************************************)
351
352 // create class
353 constructor TComTermBuffer.Create(AOwner: TCustomComTerminal);
354 begin
355 inherited Create;
356 FOwner := AOwner;
357 FTopLeft := Classes.Point(1, 1);
358 FCaretPos := Classes.Point(1, 1);
359 end;
360
361 // destroy class
362 destructor TComTermBuffer.Destroy;
363 begin
364 if FBuffer <> nil then
365 begin
366 FreeMem(FBuffer);
367 FreeMem(FTabs);
368 end;
369 inherited Destroy;
370 end;
371
372 // put char in buffer
373 procedure TComTermBuffer.SetChar(Column, Row: Integer; TermChar: TComTermChar);
374 var
375 Address: Integer;
376 begin
377 if (Row > FRows) or (Column > FColumns) then
378 Exit;
379
380 Address := (Row - 1) * FColumns + (Column - 1);
381 PComTermChar(FBuffer + (Address * SizeOf(TComTermChar)))^:= TermChar;
382 end;
383
384 // get char from buffer
385 function TComTermBuffer.GetChar(Column, Row: Integer): TComTermChar;
386 var
387 Address: Integer;
388 begin
389 if (Row > FRows) or (Column > FColumns) then
390 Exit(Default(TComTermChar));
391
392 Address := (Row - 1) * FColumns + (Column - 1);
393 Result:= PComTermChar(FBuffer + (Address * SizeOf(TComTermChar)))^;
394 end;
395
396 // scroll down up line
397 procedure TComTermBuffer.ScrollDown;
398 begin
399 DeleteLine(FScrollRange.Top, 1);
400 end;
401
402 // scroll up one line
403 procedure TComTermBuffer.ScrollUp;
404 begin
405 InsertLine(FScrollRange.Top, 1)
406 end;
407
408 procedure TComTermBuffer.EraseLineLeft(Column, Row: Integer);
409 var
410 Index: Integer;
411 B: PComTermChar;
412 begin
413 if (Row > FRows) or (Column > FColumns) then Exit;
414
415 // in memory
416 B:= PComTermChar(FBuffer) + ((Row - 1) * FColumns);
417 for Index:= 0 to Column - 1 do
418 begin
419 B[Index].Ch:= #32;
420 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
421 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
422 end;
423
424 // on screen
425 if FOwner.DoubleBuffered then
426 FOwner.Invalidate
427 else
428 FOwner.InvalidatePortion(Classes.Rect(1, Row, Column, Row));
429 end;
430
431 // erase line
432 procedure TComTermBuffer.EraseLineRight(Column, Row: Integer);
433 var
434 Index: Integer;
435 Count: Integer;
436 B: PComTermChar;
437 begin
438 if (Row > FRows) or (Column > FColumns) then Exit;
439
440 // in memory
441 Count:= (FColumns - Column + 1);
442 B:= PComTermChar(FBuffer) + ((Row - 1) * FColumns + (Column - 1));
443 for Index:= 0 to Count - 1 do
444 begin
445 B[Index].Ch:= #32;
446 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
447 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
448 end;
449
450 // on screen
451 if FOwner.DoubleBuffered then
452 FOwner.Invalidate
453 else
454 FOwner.InvalidatePortion(Classes.Rect(Column, Row, FColumns, Row));
455 end;
456
457 procedure TComTermBuffer.EraseChar(Column, Row, Count: Integer);
458 var
459 Index: Integer;
460 B: PComTermChar;
461 begin
462 if (Row > FRows) or (Column > FColumns) then Exit;
463 if (Column + Count > FColumns) then Count:= FColumns - Column;
464
465 // in memory
466 B:= PComTermChar(FBuffer) + ((Row - 1) * FColumns + (Column - 1));
467 for Index:= 0 to Count - 1 do
468 begin
469 B[Index].Ch:= #32;
470 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
471 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
472 end;
473
474 // on screen
475 if FOwner.DoubleBuffered then
476 FOwner.Invalidate
477 else
478 FOwner.InvalidatePortion(Classes.Rect(Column, Row, FColumns, Row));
479 end;
480
481 procedure TComTermBuffer.DeleteChar(Column, Row, Count: Integer);
482 var
483 Index: Integer;
484 DstAddr: PComTermChar;
485 SrcAddr: PComTermChar;
486 begin
487 if (Row > FRows) or (Column > FColumns) then Exit;
488 if (Column + Count > FColumns) then Count:= FColumns - Column;
489
490 // in memory
491 DstAddr:= PComTermChar(FBuffer) + ((Row - 1) * FColumns + (Column - 1));
492 SrcAddr:= PComTermChar(FBuffer) + ((Row - 1) * FColumns + (Column - 1) + Count);
493
494 // Move characters
495 Count:= (FColumns - (Column + Count));
496 Move(SrcAddr^, DstAddr^, Count * SizeOf(TComTermChar));
497
498 // Erase moved
499 for Index:= 0 to Count - 1 do
500 begin
501 SrcAddr[Index].Ch:= #32;
502 SrcAddr[Index].BackColor:= FOwner.FTermAttr.BackColor;
503 SrcAddr[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
504 end;
505
506 // on screen
507 if FOwner.DoubleBuffered then
508 FOwner.Invalidate
509 else
510 FOwner.InvalidatePortion(Classes.Rect(Column, Row, FColumns, Row));
511 end;
512
513 procedure TComTermBuffer.DeleteLine(Row, Count: Integer);
514 var
515 Index: Integer;
516 B: PComTermChar;
517 DstAddr: Pointer;
518 SrcAddr: Pointer;
519 BytesToMove: Integer;
520 Top, Bottom: Integer;
521 begin
522 Top:= FScrollRange.Top;
523 Bottom:= FScrollRange.Bottom;
524
525 if (Row < Top) or (Row > Bottom) then Exit;
526 if (Row - 1) + Count > Bottom then Count:= Bottom - Row + 1;
527
528 if Row < Bottom then
529 begin
530 DstAddr := (FBuffer + (Row - 1) * FColumns * SizeOf(TComTermChar));
531 SrcAddr := (FBuffer + (Row + Count - 1) * FColumns * SizeOf(TComTermChar));
532 BytesToMove := (Bottom - Row - Count + 1) * FColumns * SizeOf(TComTermChar);
533
534 // scroll in buffer
535 Move(SrcAddr^, DstAddr^, BytesToMove);
536 end;
537
538 B:= PComTermChar(FBuffer) + ((Bottom - Count) * FColumns);
539 for Index:= 0 to Count * FColumns - 1 do
540 begin
541 B[Index].Ch:= #32;
542 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
543 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
544 end;
545
546 // on screen
547 if FOwner.DoubleBuffered then
548 FOwner.Invalidate
549 else
550 FOwner.InvalidatePortion(Classes.Rect(1, Row, FColumns, Bottom));
551 end;
552
553 procedure TComTermBuffer.InsertLine(Row, Count: Integer);
554 var
555 Index: Integer;
556 B: PComTermChar;
557 DstAddr: Pointer;
558 SrcAddr: Pointer;
559 BytesToMove: Integer;
560 Top, Bottom: Integer;
561 begin
562 Top:= FScrollRange.Top;
563 Bottom:= FScrollRange.Bottom;
564
565 if (Row < Top) or (Row > Bottom) then Exit;
566 if (Row - 1) + Count > Bottom then Count:= Bottom - Row + 1;
567
568 if Row < Bottom then
569 begin
570 SrcAddr := (FBuffer + (Row - 1) * FColumns * SizeOf(TComTermChar));
571 DstAddr := (FBuffer + (Row + Count - 1) * FColumns * SizeOf(TComTermChar));
572 BytesToMove := (Bottom - Row - Count + 1) * FColumns * SizeOf(TComTermChar);
573
574 // scroll in buffer
575 Move(SrcAddr^, DstAddr^, BytesToMove);
576 end;
577
578 B:= PComTermChar(FBuffer) + ((Row - 1) * FColumns);
579 for Index:= 0 to Count * FColumns - 1 do
580 begin
581 B[Index].Ch:= #32;
582 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
583 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
584 end;
585
586 // on screen
587 if FOwner.DoubleBuffered then
588 FOwner.Invalidate
589 else
590 FOwner.InvalidatePortion(Classes.Rect(1, Row, FColumns, Bottom));
591 end;
592
593 // erase screen
594 procedure TComTermBuffer.EraseScreenLeft(Column, Row: Integer);
595 var
596 Index: Integer;
597 Count: Integer;
598 B: PComTermChar;
599 begin
600 if (Row > FRows) or (Column > FColumns) then Exit;
601
602 // in memory
603 B:= PComTermChar(FBuffer);
604 Count:= (Row * FColumns + Column);
605 for Index:= 0 to Count - 1 do
606 begin
607 B[Index].Ch:= #32;
608 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
609 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
610 end;
611
612 // on screen
613 if FOwner.DoubleBuffered then
614 FOwner.Invalidate
615 else
616 FOwner.InvalidatePortion(Classes.Rect(1, 1, FColumns, Row))
617 end;
618
619 // erase screen
620 procedure TComTermBuffer.EraseScreenRight(Column, Row: Integer);
621 var
622 Index: Integer;
623 Count: Integer;
624 B: PComTermChar;
625 begin
626 if (Row > FRows) or (Column > FColumns) then Exit;
627
628 // in memory
629 B:= PComTermChar(FBuffer) + ((Row - 1) * FColumns + (Column - 1));
630 Count:= ((FRows - Row) * FColumns + (FColumns - Column) + 1);
631 for Index:= 0 to Count - 1 do
632 begin
633 B[Index].Ch:= #32;
634 B[Index].BackColor:= FOwner.FTermAttr.BackColor;
635 B[Index].FrontColor:= FOwner.FTermAttr.FrontColor;
636 end;
637
638 // on screen
639 if FOwner.DoubleBuffered then
640 FOwner.Invalidate
641 else
642 FOwner.InvalidatePortion(Classes.Rect(1, Row, FColumns, FRows))
643 end;
644
645 // init buffer
646 procedure TComTermBuffer.Init(ARows, AColumns: Integer);
647 var
648 I: Integer;
649 begin
650 if ARows > 0 then
651 FRows:= ARows;
652 if AColumns > 0 then
653 FColumns:= AColumns;
654 if FBuffer <> nil then
655 begin
656 FreeMem(FBuffer);
657 FreeMem(FTabs);
658 end;
659 GetMem(FBuffer, FColumns * FRows * SizeOf(TComTermChar));
660 FillChar(FBuffer^, FColumns * FRows * SizeOf(TComTermChar), 0);
661 GetMem(FTabs, FColumns * SizeOf(Boolean));
662 FillChar(FTabs^, FColumns * SizeOf(Boolean), 0);
663 I := 1;
664 while (I <= FColumns) do
665 begin
666 SetTab(I, True);
667 Inc(I, 8);
668 end;
669 FScrollRange.Top:= 1;
670 FScrollRange.Bottom:= FRows;
671 end;
672
673 // get tab at Column
674 function TComTermBuffer.GetTab(Column: Integer): Boolean;
675 begin
676 Result := Boolean((FTabs + (Column - 1) * SizeOf(Boolean))^);
677 end;
678
679 // set tab at column
680 procedure TComTermBuffer.SetTab(Column: Integer; Put: Boolean);
681 begin
682 Boolean((FTabs + (Column - 1) * SizeOf(Boolean))^) := Put;
683 end;
684
685 // find nexts tab position
686 function TComTermBuffer.NextTab(Column: Integer): Integer;
687 var
688 I: Integer;
689 begin
690 I := Column;
691 while (I <= FColumns) do
692 if GetTab(I) then
693 Break
694 else
695 Inc(I);
696 if I > FColumns then
697 Result := 0
698 else
699 Result := I;
700 end;
701
702 // clear all tabs
703 procedure TComTermBuffer.ClearAllTabs;
704 begin
705 FillChar(FTabs^, FColumns * SizeOf(Boolean), 0);
706 end;
707
708 function TComTermBuffer.GetLineLength(Line: Integer): Integer;
709 var
710 I: Integer;
711 begin
712 Result := 0;
713 for I := 1 to FColumns do
714 if GetChar(I, Line).Ch <> #0 then
715 Result := I;
716 end;
717
718 function TComTermBuffer.GetLastLine: Integer;
719 var
720 J: Integer;
721 begin
722 Result := 0;
723 for J := 1 to FRows do
724 if GetLineLength(J) > 0 then
725 Result := J;
726 end;
727
728 (*****************************************
729 * TComCustomTerminal control *
730 *****************************************)
731
732 // create control
733 constructor TCustomComTerminal.Create(AOwner: TComponent);
734 begin
735 FScrollBars := ssVertical;
736
737 inherited Create(AOwner);
738
739 Parent:= TWinControl(AOwner);
740
741 BorderStyle := bsSingle;
742 Color := clBlack;
743 DoubleBuffered := True;
744 TabStop := True;
745 Font.Name := 'Consolas';
746 Font.Color:= clWhite;
747 FEmulation := teVT100orANSI;
748 FColumns := 80;
749 FRows := 100;
750 FVisibleRows:= 25;
751 FWrapLines := True;
752 FAutoFollow := True;
753 FCaretPos := Classes.Point(1, 1);
754 FTopLeft := Classes.Point(1, 1);
755 FMainBuffer := TComTermBuffer.Create(Self);
756 FAlternateBuffer := TComTermBuffer.Create(Self);
757 FTermAttr.FrontColor := Font.Color;
758 FTermAttr.BackColor := Color;
759
760 FBuffer:= FMainBuffer;
761 FParams:= TStringList.Create;
762
763 CreateEscapeCodes;
764 if not (csDesigning in ComponentState) then
765 begin
766 FMainBuffer.Init(FRows, FColumns);
767 FAlternateBuffer.Init(FVisibleRows, FColumns);
768 end;
769
770 SetBounds(Left, Top, 400, 250);
771 end;
772
773 // destroy control
774 destructor TCustomComTerminal.Destroy;
775 begin
776 PtyDevice := nil;
777 FMainBuffer.Free;
778 FAlternateBuffer.Free;
779 FEscapeCodes.Free;
780 FParams.Free;
781 inherited Destroy;
782 end;
783
784 // clear terminal screen
785 procedure TCustomComTerminal.ClearScreen;
786 begin
787 FBuffer.Init(0, 0);
788 MoveCaret(1, 1);
789 Invalidate;
790 end;
791
792 // move caret
793 procedure TCustomComTerminal.MoveCaret(AColumn, ARow: Integer);
794 begin
795 if AColumn > FBuffer.Columns then
796 begin
797 if FWrapLines then
798 FCaretPos.X := FBuffer.Columns + 1
799 else
800 FCaretPos.X := FBuffer.Columns
801 end
802 else
803 if AColumn < 1 then
804 FCaretPos.X := 1
805 else
806 FCaretPos.X := AColumn;
807
808 if ARow > FBuffer.Rows then
809 FCaretPos.Y := FBuffer.Rows
810 else
811 if ARow < 1 then
812 FCaretPos.Y := 1
813 else
814 FCaretPos.Y := ARow;
815
816 if FCaretCreated then
817 SetCaretPos((FCaretPos.X - FTopLeft.X) * FFontWidth,
818 (FCaretPos.Y - FTopLeft.Y) * FFontHeight + FFontHeight - FCaretHeight);
819 end;
820
821 // write data to screen
822 procedure TCustomComTerminal.Write(const Buffer:string; Size: Integer);
823 var
824 I: Integer;
825 L: Integer;
826 Ch: TUTF8Char;
827 Res: TEscapeResult;
828 begin
829 HideCaret;
830 try
831 // show it on screen
832 I:= 1;
833 while I <= Size do
834 begin
835 L:= UTF8CodepointSizeFast(@Buffer[I]);
836 Ch:= Copy(Buffer, I, L);
837
838 // got partial character
839 if (I + L - 1 > Size) then
840 begin
841 FPartChar:= Ch;
842 Break;
843 end;
844
845 if (FEscapeCodes <> nil) then
846 begin
847 Res := FEscapeCodes.ProcessChar(Ch);
848 if Res = erChar then
849 PutChar(FEscapeCodes.Character);
850 if Res = erCode then
851 begin
852 if not PutEscapeCode(FEscapeCodes.Code, FEscapeCodes.Params) then
853 DoUnhandledCode(FEscapeCodes.Code, FEscapeCodes.Data);
854 FEscapeCodes.Params.Clear;
855 end;
856 end
857 else begin
858 PutChar(Ch);
859 end;
860 I+= L;
861 end;
862 finally
863 ShowCaret;
864 end;
865 end;
866
867 // write string on screen, but not to port
868 procedure TCustomComTerminal.WriteStr(const Str: string);
869 begin
870 Write(Str, Length(Str));
871 end;
872
873 // write escape code on screen
874 procedure TCustomComTerminal.WriteEscCode(ACode: TEscapeCode;
875 AParams: TStrings);
876 begin
877 if FEscapeCodes <> nil then
878 PutEscapeCode(ACode, AParams);
879 end;
880
881 // load screen buffer from file
882 procedure TCustomComTerminal.LoadFromStream(Stream: TStream);
883 var
884 ABuffer: TBytes;
885 begin
886 HideCaret;
887 ABuffer:= Default(TBytes);
888 SetLength(ABuffer, Stream.Size);
889 Stream.ReadBuffer(ABuffer[0], Length(ABuffer));
890 RxBuf(Self, ABuffer[0], Length(ABuffer));
891 ShowCaret;
892 end;
893
894 // save screen buffer to file
895 procedure TCustomComTerminal.SaveToStream(Stream: TStream);
896 var
897 I, J: Integer;
898 Ch: TUTF8Char;
899 EndLine: string;
900 LastChar, LastLine: Integer;
901 begin
902 EndLine := #13#10;
903 LastLine := FBuffer.GetLastLine;
904 for J := 1 to LastLine do
905 begin
906 LastChar := FBuffer.GetLineLength(J);
907 if LastChar > 0 then
908 begin
909 for I := 1 to LastChar do
910 begin
911 Ch := FBuffer.GetChar(I, J).Ch;
912 // replace null characters with blanks
913 if Ch = #0 then
914 Ch := #32;
915 Stream.Write(Ch, Length(Ch));
916 end;
917 end;
918 // new line
919 if J <> LastLine then
920 Stream.Write(EndLine[1], Length(EndLine));
921 end;
922 end;
923
924 // select terminal font
925 procedure TCustomComTerminal.SelectFont;
926 begin
927 with TFontDialog.Create(Application) do
928 begin
929 Options := Options + [fdFixedPitchOnly];
930 Font := Self.Font;
931 if Execute then
932 Self.Font := Font;
933 Free;
934 end;
935 end;
936
937 // process font change
938 procedure TCustomComTerminal.CMFontChanged(var Message: TMessage);
939 begin
940 inherited;
941 FTermAttr.FrontColor := Font.Color;
942 if not CalculateMetrics then
943 ;//Font.Name := ComTerminalFont.Name;
944 if fsUnderline in Font.Style then
945 Font.Style := Font.Style - [fsUnderline];
946 AdjustSize;
947 UpdateScrollRange;
948 end;
949
950 procedure TCustomComTerminal.CMColorChanged(var Message: TMessage);
951 begin
952 inherited;
953 FTermAttr.BackColor := Color;
954 end;
955
956 procedure TCustomComTerminal.WMGetDlgCode(var Message: TWMGetDlgCode);
957 begin
958 // request arrow keys and WM_CHAR message to be handled by the control
959 Message.Result := DLGC_WANTARROWS or DLGC_WANTCHARS;
960 // tab key
961 if FWantTab then
962 Message.Result := Message.Result or DLGC_WANTTAB;
963 end;
964
965 // lost focus
966 procedure TCustomComTerminal.WMKillFocus(var Message: TWMSetFocus);
967 begin
968 // destroy caret because it could be requested by some other control
969 DestroyCaret(Handle);
970 FCaretCreated := False;
971 inherited;
972 end;
973
974 // gained focus
975 procedure TCustomComTerminal.WMSetFocus(var Message: TWMSetFocus);
976 begin
977 inherited;
978 // control activated, create caret
979 InitCaret;
980 end;
981
982 // left button pressed
983 procedure TCustomComTerminal.WMLButtonDown(var Message: TLMLButtonDown);
984 begin
985 // set focus when left button down
986 if CanFocus and TabStop then
987 SetFocus;
988 inherited;
989 end;
990
991 // size changed
992 procedure TCustomComTerminal.WMSize(var Msg: TWMSize);
993 var
994 ARows, AColumns: Integer;
995 begin
996 inherited WMSize(Msg);
997
998 if (ClientWidth = 0) or (ClientHeight = 0) then
999 Exit;
1000
1001 ARows:= Max(2, ClientHeight div FFontHeight);
1002 AColumns:= Max(2, ClientWidth div FFontWidth);
1003
1004 if (ARows <> FVisibleRows) or (AColumns <> FColumns) then
1005 begin
1006 FColumns := AColumns;
1007 FVisibleRows := ARows;
1008 FRows := Max(FRows, FVisibleRows);
1009 AdjustSize;
1010 if not ((csLoading in ComponentState) or (csDesigning in ComponentState)) then
1011 begin
1012 FMainBuffer.Init(FRows, FColumns);
1013 FAlternateBuffer.Init(FVisibleRows, FColumns);
1014 if Assigned(FPtyDevice) then
1015 FPtyDevice.SetScreenSize(FColumns, FVisibleRows);
1016 Invalidate;
1017 end;
1018 UpdateScrollRange;
1019
1020 if (FCaretPos.Y = FBuffer.Rows) or
1021 ((FCaretPos.Y - FTopLeft.Y) >= FVisibleRows) then
1022 begin
1023 ARows:= FCaretPos.Y - FVisibleRows;
1024 ModifyScrollBar(SB_Vert, SB_THUMBPOSITION, ARows);
1025 end;
1026 end;
1027 end;
1028
1029 // vertical scroll
1030 procedure TCustomComTerminal.WMHScroll(var Message: TWMHScroll);
1031 begin
1032 ModifyScrollBar(SB_HORZ, Message.ScrollCode, Message.Pos);
1033 end;
1034
1035 // horizontal scroll
1036 procedure TCustomComTerminal.WMVScroll(var Message: TWMVScroll);
1037 begin
1038 ModifyScrollBar(SB_VERT, Message.ScrollCode, Message.Pos);
1039 end;
1040
1041 // set size to fit whole terminal screen
1042 function TCustomComTerminal.CanAutoSize(var NewWidth,
1043 NewHeight: Integer): Boolean;
1044 var
1045 Border: Integer;
1046 begin
1047 Result := True;
1048 if Align in [alNone, alLeft, alRight] then
1049 begin
1050 NewWidth := FFontWidth * FColumns;
1051 if BorderStyle = bsSingle then
1052 begin
1053 Border := SM_CXBORDER;
1054 NewWidth := NewWidth + 2 * GetSystemMetrics(BORDER);
1055 end;
1056 end;
1057 if Align in [alNone, alTop, alBottom] then
1058 begin
1059 NewHeight := FFontHeight * FRows;
1060 if BorderStyle = bsSingle then
1061 begin
1062 Border := SM_CYBORDER;
1063 NewHeight := NewHeight + 2 * GetSystemMetrics(Border);
1064 end;
1065 end;
1066 end;
1067
1068 // set control parameters
1069 procedure TCustomComTerminal.CreateParams(var Params: TCreateParams);
1070 const
1071 BorderStyles: array[TBorderStyle] of DWORD = (0, WS_BORDER);
1072 begin
1073 inherited CreateParams(Params);
1074 with Params do
1075 begin
1076 Style := Style or BorderStyles[BorderStyle];
1077 if NewStyleControls and (BorderStyle = bsSingle) then
1078 begin
1079 Style := Style and not WS_BORDER;
1080 ExStyle := ExStyle or WS_EX_CLIENTEDGE;
1081 end;
1082 if FScrollBars in [ssVertical, ssBoth] then
1083 Style := Style or WS_VSCROLL;
1084 if FScrollBars in [ssHorizontal, ssBoth] then
1085 Style := Style or WS_HSCROLL;
1086 end;
1087 ControlStyle := ControlStyle + [csOpaque];
1088 end;
1089
1090 // key down
1091 procedure TCustomComTerminal.KeyDown(var Key: Word; Shift: TShiftState);
1092 var
1093 Code: TEscapeCode;
1094 begin
1095 inherited KeyDown(Key, Shift);
1096
1097 if (Key in [VK_TAB, VK_ESCAPE]) then
1098 begin
1099 SendChar(Chr(Key));
1100 Key:= 0;
1101 Exit;
1102 end;
1103
1104 if (Key = VK_BACK) then
1105 begin
1106 SendChar(#$7f);
1107 Key:= 0;
1108 Exit;
1109 end;
1110
1111 if Key in [VK_F1..VK_F12] then
1112 begin
1113 Code := ecFuncKey;
1114 FParams.Text:= IntToStr(Key - VK_F1);
1115 SendCode(Code, FParams);
1116 Exit;
1117 end;
1118
1119 case Key of
1120 VK_INSERT: Code := ecInsertKey;
1121 VK_DELETE: Code := ecDeleteKey;
1122 VK_PRIOR: Code := ecPageUpKey;
1123 VK_NEXT: Code := ecPageDownKey;
1124 else
1125 Code := ecUnknown;
1126 end;
1127
1128 if (Code <> ecUnknown) then
1129 begin
1130 SendCode(Code, nil);
1131 Exit;
1132 end;
1133
1134 case Key of
1135 VK_UP: Code := ecCursorUp;
1136 VK_DOWN: Code := ecCursorDown;
1137 VK_LEFT: Code := ecCursorLeft;
1138 VK_RIGHT: Code := ecCursorRight;
1139 VK_HOME: Code := ecCursorHome;
1140 VK_END: Code := ecCursorEnd;
1141 else
1142 Code := ecUnknown;
1143 end;
1144 if FTermMode.Keys = akTerminal then
1145 begin
1146 if Code <> ecUnknown then
1147 if FArrowKeys = akTerminal then
1148 SendCode(Code, nil)
1149 else
1150 PutEscapeCode(Code, nil);
1151 end
1152 else
1153 case Code of
1154 ecCursorUp: SendCode(ecAppCursorUp, nil);
1155 ecCursorDown: SendCode(ecAppCursorDown, nil);
1156 ecCursorLeft: SendCode(ecAppCursorLeft, nil);
1157 ecCursorRight: SendCode(ecAppCursorRight, nil);
1158 ecCursorHome: SendCode(ecAppCursorHome, nil);
1159 ecCursorEnd: SendCode(ecAppCursorEnd, nil);
1160 end;
1161 {$IFDEF LCLGTK2}
1162 if Key in [VK_UP, VK_DOWN] then
1163 begin
1164 Key:= 0;
1165 end;
1166 {$ENDIF}
1167 end;
1168
1169 // key pressed
1170 procedure TCustomComTerminal.KeyPress(var Key: Char);
1171 begin
1172 inherited KeyPress(Key);
1173 // SendChar(Key);
1174 end;
1175
1176 procedure TCustomComTerminal.UTF8KeyPress(var UTF8Key: TUTF8Char);
1177 begin
1178 inherited UTF8KeyPress(UTF8Key);
1179 SendChar(UTF8Key);
1180 end;
1181
1182 procedure TCustomComTerminal.MouseDown(Button: TMouseButton;
1183 Shift: TShiftState; X, Y: Integer);
1184 begin
1185 inherited MouseDown(Button, Shift, X, Y);
1186 MouseEvent(ecMouseDown, Button, Shift, X, Y);
1187 end;
1188
1189 procedure TCustomComTerminal.MouseUp(Button: TMouseButton; Shift: TShiftState;
1190 X, Y: Integer);
1191 begin
1192 inherited MouseUp(Button, Shift, X, Y);
1193 MouseEvent(ecMouseUp, Button, Shift, X, Y);
1194 end;
1195
1196 procedure TCustomComTerminal.CreateWnd;
1197 begin
1198 inherited CreateWnd;
1199 if FScrollBars in [ssVertical, ssBoth] then
1200 ShowScrollBar(Handle, SB_VERT, True);
1201 if FScrollBars in [ssHorizontal, ssBoth] then
1202 ShowScrollBar(Handle, SB_HORZ, True);
1203 end;
1204
1205 procedure TCustomComTerminal.Notification(AComponent: TComponent;
1206 Operation: TOperation);
1207 begin
1208 inherited Notification(AComponent, Operation);
1209 if (AComponent = FPtyDevice) and (Operation = opRemove) then
1210 PtyDevice := nil;
1211 end;
1212
1213 // paint characters
1214 procedure TCustomComTerminal.PaintTerminal(Rect: TRect);
1215 var
1216 I, J, X, Y: Integer;
1217 Ch: TComTermChar;
1218 begin
1219 HideCaret;
1220 if (Rect.Bottom + FTopLeft.Y - 1) > FBuffer.Rows then
1221 Dec(Rect.Bottom);
1222 if (Rect.Right + FTopLeft.X - 1) > FBuffer.Columns then
1223 Dec(Rect.Right);
1224 for J := Rect.Top to Rect.Bottom do
1225 begin
1226 Y := J + FTopLeft.Y - 1;
1227 for I := Rect.Left to Rect.Right do
1228 begin
1229 X := I + FTopLeft.X - 1;
1230 Ch := FBuffer.GetChar(X, Y);
1231 if Ch.Ch <> Chr(0) then
1232 DrawChar(I, J, Ch);
1233 end;
1234 end;
1235 ShowCaret;
1236 end;
1237
1238 procedure TCustomComTerminal.PaintDesign;
1239 begin
1240 Canvas.TextOut(0, 0, 'Virtual Terminal Emulator');
1241 end;
1242
1243 procedure TCustomComTerminal.Paint;
1244 var
1245 ARect: TRect;
1246 begin
1247 Canvas.Font := Font;
1248 Canvas.Brush.Color := Color;
1249 if csDesigning in ComponentState then
1250 PaintDesign
1251 else
1252 begin
1253 MoveCaret(FCaretPos.X, FCaretPos.Y);
1254 // don't paint whole screen, but only the invalidated portion
1255 ARect.Left := Canvas.ClipRect.Left div FFontWidth + 1;
1256 ARect.Right := Min(Canvas.ClipRect.Right div FFontWidth + 1, FBuffer.Columns);
1257 ARect.Top := Canvas.ClipRect.Top div FFontHeight + 1;
1258 ARect.Bottom := Min(Canvas.ClipRect.Bottom div FFontHeight + 1, FBuffer.Rows);
1259 PaintTerminal(ARect);
1260 end;
1261 end;
1262
1263 // creates caret
1264 procedure TCustomComTerminal.CreateTerminalCaret;
1265 begin
1266 FCaretHeight := 0;
1267 if FCaret = tcBlock then
1268 FCaretHeight := FFontHeight
1269 else
1270 if FCaret = tcUnderline then
1271 FCaretHeight := FFontHeight div 8;
1272 if FCaretHeight > 0 then
1273 begin
1274 CreateCaret(Handle, 0, FFontWidth, FCaretHeight);
1275 FCaretCreated := True;
1276 end;
1277 end;
1278
1279 // string received from com port
1280 procedure TCustomComTerminal.StringReceived(Str: string);
1281 begin
1282 DoStrRecieved(Str);
1283 WriteStr(Str);
1284 end;
1285
1286 // draw one character on screen, but do not put it in buffer
1287 procedure TCustomComTerminal.DrawChar(AColumn, ARow: Integer;
1288 Ch: TComTermChar);
1289 var
1290 OldBackColor, OldFrontColor: Integer;
1291 begin
1292 OldBackColor := Canvas.Brush.Color;
1293 OldFrontColor := Canvas.Font.Color;
1294 Canvas.Brush.Color := Ch.BackColor;
1295 Canvas.Font.Color := Ch.FrontColor;
1296 if Ch.Bold then
1297 Canvas.Font.Style := Canvas.Font.Style + [fsBold]
1298 else begin
1299 Canvas.Font.Style := Canvas.Font.Style - [fsBold];
1300 end;
1301 if Ch.Underline then
1302 Canvas.Font.Style := Canvas.Font.Style + [fsUnderline]
1303 else begin
1304 Canvas.Font.Style := Canvas.Font.Style - [fsUnderline];
1305 end;
1306 Canvas.TextOut((AColumn - 1) * FFontWidth, (ARow - 1) * FFontHeight, Ch.Ch);
1307 Canvas.Brush.Color := OldBackColor;
1308 Canvas.Font.Color := OldFrontColor;
1309 end;
1310
1311 procedure TCustomComTerminal.WrapLine(AWidth: Integer);
1312 begin
1313 if FCaretPos.X + AWidth > FBuffer.Columns + 1 then
1314 begin
1315 if FCaretPos.Y = FBuffer.Rows then
1316 begin
1317 FBuffer.ScrollDown;
1318 MoveCaret(1, FCaretPos.Y);
1319 end
1320 else begin
1321 MoveCaret(1, FCaretPos.Y + 1)
1322 end;
1323 end;
1324 end;
1325
1326 // move caret after new char is put on screen
1327 procedure TCustomComTerminal.AdvanceCaret(Kind: TAdvanceCaret);
1328 var
1329 I: Integer;
1330 begin
1331 case Kind of
1332 acChar:
1333 begin
1334 if (FCaretPos.X < FColumns) or FWrapLines then
1335 MoveCaret(FCaretPos.X + 1, FCaretPos.Y);
1336 end;
1337 acReturn: MoveCaret(1, FCaretPos.Y);
1338 acLineFeed:
1339 begin
1340 if FCaretPos.Y = FBuffer.FScrollRange.Bottom then
1341 FBuffer.ScrollDown
1342 else
1343 MoveCaret(FCaretPos.X, FCaretPos.Y + 1);
1344 end;
1345 acReverseLineFeed:
1346 begin
1347 if FCaretPos.Y = FBuffer.FScrollRange.Top then
1348 FBuffer.ScrollUp
1349 else
1350 MoveCaret(FCaretPos.X, FCaretPos.Y - 1);
1351 end;
1352 acBackSpace: MoveCaret(FCaretPos.X - 1, FCaretPos.Y);
1353 acTab:
1354 begin
1355 I := FBuffer.NextTab(FCaretPos.X + 1);
1356 if I > 0 then
1357 MoveCaret(I, FCaretPos.Y);
1358 end;
1359 acPage:
1360 ClearScreen;
1361 end;
1362 if FAutoFollow then
1363 begin
1364 if (FCaretPos.Y - FTopLeft.Y) > FVisibleRows then
1365 begin
1366 I:= FCaretPos.Y - FVisibleRows + 1;
1367 ModifyScrollBar(SB_Vert, SB_THUMBPOSITION, I);
1368 end;
1369 end;
1370 end;
1371
1372 // set character attributes
1373 procedure TCustomComTerminal.SetAttributes(AParams: TStrings);
1374 var
1375 I, Value: Integer;
1376
1377 procedure AllOff;
1378 begin
1379 FTermAttr.FrontColor := Font.Color;
1380 FTermAttr.BackColor := Color;
1381 FTermAttr.Invert := False;
1382 FTermAttr.Bold := False;
1383 FTermAttr.Underline := False;
1384 end;
1385
1386 function GetExtendedColor(var Index: Integer): TColor;
1387 var
1388 RGB: Integer;
1389 R, G, B: Byte;
1390 AParam: Integer;
1391 begin
1392 AParam:= FEscapeCodes.GetParam(Index + 1, AParams);
1393 // Color from RGB value
1394 if AParam = 2 then
1395 begin
1396 R:= FEscapeCodes.GetParam(Index + 2, AParams);
1397 G:= FEscapeCodes.GetParam(Index + 3, AParams);
1398 B:= FEscapeCodes.GetParam(Index + 4, AParams);
1399 Result:= RGBToColor(R, G, B);
1400 Inc(Index, 4);
1401 end
1402 // Color from 256 color palette
1403 else if (AParam = 5) then
1404 begin
1405 RGB:= FEscapeCodes.GetParam(Index + 2, AParams);
1406 if (RGB >= 0) and (RGB < 256) then
1407 begin
1408 Result:= Color256Table[RGB];
1409 end;
1410 Inc(Index, 2);
1411 end;
1412 end;
1413
1414 begin
1415 I:= 1;
1416 if AParams.Count = 0 then
1417 AllOff;
1418 while I <= AParams.Count do
1419 begin
1420 Value := FEscapeCodes.GetParam(I, AParams);
1421 case Value of
1422 0: AllOff;
1423 1: FTermAttr.Bold := True;
1424 4: FTermAttr.Underline := True;
1425 7: FTermAttr.Invert := True;
1426 22: FTermAttr.Bold := False;
1427 24: FTermAttr.Underline := False;
1428 27: FTermAttr.Invert := False;
1429
1430 // Extended foreground color
1431 38: FTermAttr.FrontColor := GetExtendedColor(I);
1432 // Default foreground color
1433 39: FTermAttr.FrontColor := Font.Color;
1434 // Extended background color
1435 48: FTermAttr.BackColor := GetExtendedColor(I);
1436 // Default background color
1437 49: FTermAttr.BackColor := Color;
1438
1439 // NEW foreground colors
1440 else if (Value in [30..37]) then
1441 FTermAttr.FrontColor := Color256Table[Value - 30]
1442 // NEW background colors
1443 else if (Value in [40..47]) then
1444 FTermAttr.BackColor := Color256Table[Value - 40]
1445
1446 // BRIGHT foreground colors
1447 else if (Value in [90..97]) then
1448 FTermAttr.FrontColor := Color256Table[Value - 90 + 8]
1449 // BRIGHT background colors
1450 else if (Value in [100..107]) then
1451 FTermAttr.BackColor := Color256Table[Value - 100 + 8]
1452
1453 else begin
1454 DoUnhandledCode(ecAttributes, IntToStr(Value));
1455 end;
1456 end;
1457 Inc(I);
1458 end;
1459 end;
1460
1461 procedure TCustomComTerminal.SetMode(AParams: TStrings; OnOff: Boolean);
1462 var
1463 Str: string;
1464 begin
1465 if AParams.Count = 0 then
1466 Exit;
1467 Str := AParams[0];
1468 if Str = '?1' then
1469 begin
1470 if OnOff then
1471 FTermMode.Keys := akWindows
1472 else
1473 FTermMode.Keys := akTerminal;
1474 end
1475 else if Str = '?7' then
1476 FWrapLines := OnOff
1477 else if Str = '?3' then
1478 begin
1479 if OnOff then
1480 Columns := 132
1481 else
1482 Columns := 80;
1483 end
1484 else if Str = '?1002' then
1485 FTermMode.MouseTrack:= OnOff
1486 else if Str = '?1006' then
1487 FTermMode.MouseMode:= OnOff
1488 else if Str = '?1049' then
1489 begin
1490 FBuffer.FTopLeft:= FTopLeft;
1491 FBuffer.FCaretPos:= FCaretPos;
1492 if OnOff then
1493 FBuffer := FAlternateBuffer
1494 else begin
1495 FBuffer := FMainBuffer;
1496 end;
1497 FTopLeft:= FBuffer.FTopLeft;
1498 FCaretPos:= FBuffer.FCaretPos;
1499 UpdateScrollRange;
1500 Invalidate;
1501 end
1502 else begin
1503 DoUnhandledMode(Str, OnOff);
1504 end;
1505 end;
1506
1507 // invalidate portion of screen
1508 procedure TCustomComTerminal.InvalidatePortion(ARect: TRect);
1509 var
1510 Rect: TRect;
1511 begin
1512 Rect.Left := Max((ARect.Left - FTopLeft.X) * FFontWidth, 0);
1513 Rect.Right := Max((ARect.Right - FTopLeft.X + 1) * FFontWidth, 0);
1514 Rect.Top := Max((ARect.Top - FTopLeft.Y) * FFontHeight, 0);
1515 Rect.Bottom := Max((ARect.Bottom - FTopLeft.Y + 1) * FFontHeight, 0);
1516 InvalidateRect(Handle, @Rect, True);
1517 end;
1518
1519 // modify scroll bar
1520 procedure TCustomComTerminal.ModifyScrollBar(ScrollBar, ScrollCode,
1521 Pos: Integer);
1522 var
1523 CellSize, OldPos, APos, Dx, Dy: Integer;
1524 begin
1525 if (ScrollCode = SB_ENDSCROLL) or
1526 ((ScrollCode = SB_THUMBTRACK) and not FSmoothScroll)
1527 then
1528 Exit;
1529 if ScrollBar = SB_HORZ then
1530 CellSize := FFontWidth
1531 else
1532 CellSize := FFontHeight;
1533 APos := GetScrollPos(Handle, ScrollBar);
1534 OldPos := APos;
1535 case ScrollCode of
1536 SB_LINEUP: Dec(APos);
1537 SB_LINEDOWN: Inc(APos);
1538 SB_PAGEUP: Dec(APos, ClientHeight div CellSize);
1539 SB_PAGEDOWN: Inc(APos, ClientHeight div CellSize);
1540 SB_THUMBPOSITION,
1541 SB_THUMBTRACK: APos := Pos;
1542 end;
1543 SetScrollPos(Handle, ScrollBar, APos, True);
1544 APos := GetScrollPos(Handle, ScrollBar);
1545 if ScrollBar = SB_HORZ then
1546 begin
1547 FTopLeft.X := APos + 1;
1548 Dx := (OldPos - APos) * FFontWidth;
1549 Dy := 0;
1550 end else
1551 begin
1552 FTopLeft.Y := APos + 1;
1553 Dx := 0;
1554 Dy := (OldPos - APos) * FFontHeight;
1555 end;
1556 if DoubleBuffered then
1557 Invalidate
1558 else
1559 ScrollWindowEx(Handle, Dx, Dy, nil, nil, 0, nil, SW_ERASE or SW_INVALIDATE);
1560 end;
1561
1562 // calculate scroll position
1563 procedure TCustomComTerminal.UpdateScrollPos;
1564 begin
1565 if FScrollBars in [ssBoth, ssHorizontal] then
1566 begin
1567 SetScrollPos(Handle, SB_HORZ, FTopLeft.X - 1, True);
1568 end;
1569 if FScrollBars in [ssBoth, ssVertical] then
1570 begin
1571 SetScrollPos(Handle, SB_VERT, FTopLeft.Y - 1, True);
1572 end;
1573 end;
1574
1575 // calculate scroll range
1576 procedure TCustomComTerminal.UpdateScrollRange;
1577 var
1578 OldScrollBars: TScrollStyle;
1579 AHeight, AWidth: Integer;
1580
1581 // is scroll bar visible?
1582 function ScrollBarVisible(Code: Word): Boolean;
1583 var
1584 Min, Max: Integer;
1585 begin
1586 Result := False;
1587 if (ScrollBars = ssBoth) or
1588 ((Code = SB_HORZ) and (ScrollBars = ssHorizontal)) or
1589 ((Code = SB_VERT) and (ScrollBars = ssVertical)) then
1590 begin
1591 GetScrollRange(Handle, Code, Min, Max);
1592 Result := Min <> Max;
1593 end;
1594 end;
1595
1596 procedure SetRange(Code, Max: Integer);
1597 var
1598 Info: TScrollInfo;
1599 begin
1600 Info:= Default(TScrollInfo);
1601 Info.fMask := SIF_RANGE or SIF_PAGE;
1602 Info.nMax := Max;
1603 Info.nPage := 1;
1604 SetScrollInfo(Handle, Code, Info, False);
1605 end;
1606
1607 // set horizontal range
1608 procedure SetHorzRange;
1609 var
1610 Max: Integer;
1611 AColumns: Integer;
1612 begin
1613 if OldScrollBars in [ssBoth, ssHorizontal] then
1614 begin
1615 AColumns := AWidth div FFontWidth;
1616 if AColumns >= FBuffer.Columns then
1617 SetRange(SB_HORZ, 1) // screen is wide enough, hide scroll bar
1618 else
1619 begin
1620 Max := FBuffer.Columns - (AColumns - 1);
1621 SetRange(SB_HORZ, Max);
1622 end;
1623 end;
1624 end;
1625
1626 // set vertical range
1627 procedure SetVertRange;
1628 var
1629 Max, ARows: Integer;
1630 begin
1631 if OldScrollBars in [ssBoth, ssVertical] then
1632 begin
1633 ARows := AHeight div FFontHeight;
1634 if ARows >= FBuffer.Rows then
1635 SetRange(SB_VERT, 1) // screen is high enough, hide scroll bar
1636 else
1637 begin
1638 Max := FBuffer.Rows - (ARows - 1);
1639 SetRange(SB_VERT, Max);
1640 end;
1641 end;
1642 end;
1643
1644 begin
1645 if (FScrollBars = ssNone) or (FBuffer = nil) then
1646 Exit;
1647 AHeight := ClientHeight;
1648 AWidth := ClientWidth;
1649 if ScrollBarVisible(SB_HORZ) then
1650 Inc(AHeight, GetSystemMetrics(SM_CYHSCROLL));
1651 if ScrollBarVisible(SB_VERT) then
1652 Inc(AWidth, GetSystemMetrics(SM_CXVSCROLL));
1653 // Temporarily mark us as not having scroll bars to avoid recursion
1654 OldScrollBars := FScrollBars;
1655 FScrollBars := ssNone;
1656 try
1657 SetHorzRange;
1658 AHeight := ClientHeight;
1659 SetVertRange;
1660 if AWidth <> ClientWidth then
1661 begin
1662 AWidth := ClientWidth;
1663 SetHorzRange;
1664 end;
1665 finally
1666 FScrollBars := OldScrollBars;
1667 end;
1668 // range changed, update scroll bar position
1669 UpdateScrollPos;
1670 end;
1671
1672 // hide caret
1673 procedure TCustomComTerminal.HideCaret;
1674 begin
1675 if FCaretCreated then
1676 LCLIntf.HideCaret(Handle);
1677 end;
1678
1679 // show caret
1680 procedure TCustomComTerminal.ShowCaret;
1681 begin
1682 if FCaretCreated then
1683 LCLIntf.ShowCaret(Handle);
1684 end;
1685
1686 // send character to com port
1687 procedure TCustomComTerminal.SendChar(Ch: TUTF8Char);
1688 begin
1689 if (FPtyDevice <> nil) and (FPtyDevice.Connected) then
1690 begin
1691 FPtyDevice.WriteStr(Ch);
1692 if FLocalEcho then
1693 begin
1694 // local echo is on, show character on screen
1695 HideCaret;
1696 PutChar(Ch);
1697 ShowCaret;
1698 end;
1699 // send line feeds after carriage return
1700 if (Ch = Chr(13)) and FSendLF then
1701 SendChar(Chr(10));
1702 end;
1703 end;
1704
1705 // send escape code
1706 procedure TCustomComTerminal.SendCode(Code: TEscapeCode; AParams: TStrings);
1707 begin
1708 if (FPtyDevice <> nil) and (FPtyDevice.Connected) and (FEscapeCodes <> nil) then
1709 begin
1710 FPtyDevice.WriteStr(FEscapeCodes.EscCodeToStr(Code, AParams));
1711 if FLocalEcho then
1712 begin
1713 // local echo is on, show character on screen
1714 HideCaret;
1715 PutEscapeCode(Code, AParams);
1716 ShowCaret;
1717 end;
1718 end;
1719 end;
1720
1721 // send escape code to port
1722 procedure TCustomComTerminal.SendCodeNoEcho(Code: TEscapeCode; AParams: TStrings);
1723 begin
1724 if (FPtyDevice <> nil) and (FPtyDevice.Connected) and (FEscapeCodes <> nil) then
1725 FPtyDevice.WriteStr(FEscapeCodes.EscCodeToStr(Code, AParams));
1726 end;
1727
1728 procedure TCustomComTerminal.MouseEvent(Code: TEscapeCode;
1729 Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
1730 var
1731 AButton: Integer;
1732 begin
1733 if (FTermMode.MouseMode and FTermMode.MouseTrack) then
1734 begin
1735 case Button of
1736 mbLeft: AButton:= 0;
1737 mbRight: AButton:= 2;
1738 mbMiddle: AButton:= 1;
1739 else AButton:= Ord(Button);
1740 end;
1741 FParams.Text:= IntToStr(AButton);
1742 FParams.Add(IntToStr(X div FFontWidth + 1));
1743 FParams.Add(IntToStr(Y div FFontHeight + 1));
1744 SendCodeNoEcho(Code, FParams);
1745 end;
1746 end;
1747
1748 // process escape code on screen
1749 function TCustomComTerminal.PutEscapeCode(ACode: TEscapeCode; AParams: TStrings): Boolean;
1750 begin
1751 Result := True;
1752 with FEscapeCodes do
1753 case ACode of
1754 ecCursorUp: MoveCaret(FCaretPos.X, FCaretPos.Y - GetParam(1, AParams));
1755 ecCursorDown: MoveCaret(FCaretPos.X, FCaretPos.Y + GetParam(1, AParams));
1756 ecCursorRight: MoveCaret(FCaretPos.X + GetParam(1, AParams), FCaretPos.Y);
1757 ecCursorLeft: MoveCaret(FCaretPos.X - GetParam(1, AParams), FCaretPos.Y);
1758 ecCursorNextLine: MoveCaret(1, FCaretPos.Y + GetParam(1, AParams));
1759 ecCursorPrevLine: MoveCaret(1, FCaretPos.Y - GetParam(1, AParams));
1760 ecCursorMove: MoveCaret(GetParam(2, AParams), GetParam(1, AParams));
1761 ecCursorMoveX: MoveCaret(GetParam(1, AParams), FCaretPos.Y);
1762 ecCursorMoveY: MoveCaret(FCaretPos.X, GetParam(1, AParams));
1763 ecReverseLineFeed: AdvanceCaret(acReverseLineFeed);
1764 ecEraseLineLeft: FBuffer.EraseLineLeft(FCaretPos.X, FCaretPos.Y);
1765 ecEraseLineRight: FBuffer.EraseLineRight(FCaretPos.X, FCaretPos.Y);
1766 ecEraseLine:
1767 begin
1768 FBuffer.EraseLineRight(1, FCaretPos.Y);
1769 MoveCaret(1, FCaretPos.Y)
1770 end;
1771 ecEraseScreenLeft: FBuffer.EraseScreenLeft(FCaretPos.X, FCaretPos.Y);
1772 ecEraseScreenRight: FBuffer.EraseScreenRight(FCaretPos.X, FCaretPos.Y);
1773 ecEraseScreen:
1774 begin
1775 FBuffer.EraseScreenRight(1, 1);
1776 MoveCaret(1, 1)
1777 end;
1778 ecEraseChar: FBuffer.EraseChar(FCaretPos.X, FCaretPos.Y, GetParam(1, AParams));
1779 ecDeleteChar: FBuffer.DeleteChar(FCaretPos.X, FCaretPos.Y, GetParam(1, AParams));
1780 ecIdentify:
1781 begin
1782 AParams.Clear;
1783 AParams.Add('2');
1784 SendCodeNoEcho(ecIdentResponse, AParams);
1785 end;
1786 ecSetTab: FBuffer.SetTab(FCaretPos.X, True);
1787 ecClearTab: FBuffer.SetTab(FCaretPos.X, False);
1788 ecClearAllTabs: FBuffer.ClearAllTabs;
1789 ecAttributes: SetAttributes(AParams);
1790 ecSetMode: SetMode(AParams, True);
1791 ecResetMode: SetMode(AParams, False);
1792 ecReset:
1793 begin
1794 AParams.Clear;
1795 AParams.Add('0');
1796 SetAttributes(AParams);
1797 end;
1798 ecSaveCaret: SaveCaretPos;
1799 ecRestoreCaret: RestoreCaretPos;
1800 ecSaveCaretAndAttr: begin SaveCaretPos; SaveAttr; end;
1801 ecRestoreCaretAndAttr: begin RestoreCaretPos; RestoreAttr; end;
1802 ecQueryCursorPos:
1803 begin
1804 AParams.Clear;
1805 AParams.Add(IntToStr(FCaretPos.Y));
1806 AParams.Add(IntToStr(FCaretPos.X));
1807 SendCodeNoEcho(ecReportCursorPos, AParams);
1808 end;
1809 ecQueryDevice: SendCodeNoEcho(ecReportDeviceOK, nil);
1810 ecTest: PerformTest('E');
1811 ecScrollRegion:
1812 begin
1813 FBuffer.FScrollRange.Top:= GetParam(1, AParams);
1814 FBuffer.FScrollRange.Bottom:= GetParam(2, AParams);
1815 end;
1816 ecScrollDown,
1817 ecInsertLine: FBuffer.InsertLine(FCaretPos.Y, GetParam(1, AParams));
1818 ecScrollUp,
1819 ecDeleteLine: FBuffer.DeleteLine(FCaretPos.Y, GetParam(1, AParams));
1820 ecSoftReset:
1821 begin
1822 FTermMode.CharSet:= False;
1823 FBuffer.FScrollRange.Top:= 1;
1824 FBuffer.FScrollRange.Bottom:= FBuffer.Rows;
1825 end;
1826 ecCharSet:
1827 begin
1828 // Designate Character Set
1829 if (AParams.Count > 0) and (Length(AParams[0]) > 0) then
1830 FTermMode.CharSet:= (AParams[0] = '0');
1831 end
1832 else
1833 Result := False;
1834 end;
1835 end;
1836
1837 // calculate font height and width
1838 function TCustomComTerminal.CalculateMetrics: Boolean;
1839 var
1840 Metrics: TTextMetric;
1841 begin
1842 GetTextMetrics(Canvas.Handle, Metrics);
1843 FFontHeight := Metrics.tmHeight;
1844 FFontWidth := Metrics.tmAveCharWidth;
1845 // allow only fixed pitch fonts
1846 Result := (Metrics.tmPitchAndFamily and TMPF_FIXED_PITCH) = 0;
1847 end;
1848
1849 // visual character is appears on screen
1850 procedure TCustomComTerminal.DoChar(Ch: TUTF8Char);
1851 begin
1852 if Assigned(FOnChar) then
1853 FOnChar(Self, Ch);
1854 end;
1855
1856 // get custom escape codes processor
1857 procedure TCustomComTerminal.DoGetEscapeCodes(
1858 var EscapeCodes: TEscapeCodes);
1859 begin
1860 if Assigned(FOnGetEscapeCodes) then
1861 FOnGetEscapeCodes(Self, EscapeCodes);
1862 end;
1863
1864 // string recieved
1865 procedure TCustomComTerminal.DoStrRecieved(var Str: string);
1866 begin
1867 if Assigned(FOnStrRecieved) then
1868 FOnStrRecieved(Self, Str);
1869 end;
1870
1871 // let application handle unhandled escape code
1872 procedure TCustomComTerminal.DoUnhandledCode(Code: TEscapeCode;
1873 Data: string);
1874 begin
1875 if Assigned(FOnUnhandledCode) then
1876 FOnUnhandledCode(Self, Code, Data);
1877 end;
1878
1879 procedure TCustomComTerminal.DoUnhandledMode(const Data: string; OnOff: Boolean);
1880 begin
1881 if Assigned(FOnUnhandledMode) then
1882 FOnUnhandledMode(Self, Data, OnOff);
1883 end;
1884
1885 function TCustomComTerminal.DoMouseWheel(Shift: TShiftState;
1886 WheelDelta: Integer; MousePos: TPoint): Boolean;
1887 var
1888 APos: Integer;
1889 begin
1890 Result:= True;
1891 APos:= GetScrollPos(Handle, SB_VERT);
1892 if WheelDelta < 0 then
1893 APos:= APos + Mouse.WheelScrollLines
1894 else begin
1895 APos:= APos - Mouse.WheelScrollLines;
1896 end;
1897 ModifyScrollBar(SB_VERT, SB_THUMBPOSITION, APos);
1898 end;
1899
1900 // create escape codes processor
1901 procedure TCustomComTerminal.CreateEscapeCodes;
1902 begin
1903 if csDesigning in ComponentState then
1904 Exit;
1905 case FEmulation of
1906 teVT52: FEscapeCodes := TEscapeCodesVT52.Create;
1907 teVT100orANSI: FEscapeCodes := TEscapeCodesVT100.Create;
1908 else
1909 begin
1910 FEscapeCodes := nil;
1911 DoGetEscapeCodes(FEscapeCodes);
1912 end;
1913 end;
1914 end;
1915
1916 // perform screen test
1917 procedure TCustomComTerminal.PerformTest(ACh: Char);
1918 var
1919 I, J: Integer;
1920 TermCh: TComTermChar;
1921 begin
1922 with TermCh do
1923 begin
1924 Ch := ACh;
1925 FrontColor := Font.Color;
1926 BackColor := Color;
1927 Underline := False;
1928 end;
1929 for I := 1 to FBuffer.Columns do
1930 for J := 1 to FBuffer.Rows do
1931 FBuffer.SetChar(I, J, TermCh);
1932 Invalidate;
1933 end;
1934
1935 // get current character properties
1936 function TCustomComTerminal.GetCharAttr: TComTermChar;
1937 begin
1938 if FTermAttr.Invert then
1939 // Result.FrontColor := Color
1940 Result.FrontColor := FTermAttr.BackColor
1941 else
1942 // Result.BackColor := Font.Color;
1943 Result.FrontColor := FTermAttr.FrontColor;
1944 if FTermAttr.Invert then
1945 // Result.BackColor := Font.Color
1946 Result.BackColor := FTermAttr.FrontColor
1947 else
1948 // Result.FrontColor := Color
1949 Result.BackColor := FTermAttr.BackColor;
1950 // NEW end changes
1951 Result.Bold := FTermAttr.Bold;
1952 Result.Underline := FTermAttr.Underline;
1953 Result.Ch := #0;
1954 end;
1955
1956 // put one character on screen
1957 procedure TCustomComTerminal.PutChar(Ch: TUTF8Char);
1958 var
1959 AWidth: Integer;
1960 TermCh: TComTermChar;
1961 begin
1962 case Ch[1] of
1963 #8: AdvanceCaret(acBackspace);
1964 #9: AdvanceCaret(acTab);
1965 #10: AdvanceCaret(acLineFeed);
1966 #12: AdvanceCaret(acPage);
1967 #13: AdvanceCaret(acReturn);
1968 #32..#255:
1969 begin
1970 TermCh := GetCharAttr;
1971 if not FTermMode.CharSet then
1972 TermCh.Ch := Ch
1973 else begin
1974 case Ch[1] of
1975 'j': TermCh.Ch := '┘';
1976 'k': TermCh.Ch := '┐';
1977 'l': TermCh.Ch := '┌';
1978 'm': TermCh.Ch := '└';
1979 'n': TermCh.Ch := '┼';
1980 'q': TermCh.Ch := '─';
1981 't': TermCh.Ch := '├';
1982 'u': TermCh.Ch := '┤';
1983 'v': TermCh.Ch := '┴';
1984 'w': TermCh.Ch := '┬';
1985 'x': TermCh.Ch := '│';
1986 else TermCh.Ch := Ch;
1987 end;
1988 end;
1989 AWidth:= UTF8Width(Ch);
1990 if AWidth <= 0 then Exit;
1991 if FWrapLines then WrapLine(AWidth);
1992 FBuffer.SetChar(FCaretPos.X, FCaretPos.Y, TermCh);
1993 DrawChar(FCaretPos.X - FTopLeft.X + 1, FCaretPos.Y - FTopLeft.Y + 1, TermCh);
1994 AdvanceCaret(acChar);
1995 Dec(AWidth);
1996 while (AWidth > 0) do
1997 begin
1998 TermCh.Ch := #0;
1999 FBuffer.SetChar(FCaretPos.X, FCaretPos.Y, TermCh);
2000 AdvanceCaret(acChar);
2001 Dec(AWidth);
2002 end;
2003 end;
2004 end;
2005 DoChar(Ch);
2006 end;
2007
2008 // init caret
2009 procedure TCustomComTerminal.InitCaret;
2010 begin
2011 CreateTerminalCaret;
2012 MoveCaret(FCaretPos.X, FCaretPos.Y);
2013 ShowCaret;
2014 end;
2015
2016 // restore caret position
2017 procedure TCustomComTerminal.RestoreCaretPos;
2018 begin
2019 MoveCaret(FSaveCaret.X, FSaveCaret.Y);
2020 end;
2021
2022 // save caret position
2023 procedure TCustomComTerminal.SaveCaretPos;
2024 begin
2025 FSaveCaret := FCaretPos;
2026 end;
2027
2028 // restore attributes
2029 procedure TCustomComTerminal.RestoreAttr;
2030 begin
2031 FTermAttr := FSaveAttr;
2032 end;
2033
2034 // save attributes
2035 procedure TCustomComTerminal.SaveAttr;
2036 begin
2037 FSaveAttr := FTermAttr;
2038 end;
2039
2040 procedure TCustomComTerminal.RxBuf(Sender: TObject; const Buffer; Count: Integer);
2041 var
2042 L: Integer;
2043 Str: String;
2044
2045 // append line feeds to carriage return
2046 procedure AppendLineFeeds;
2047 var
2048 I: Integer;
2049 begin
2050 I := 1;
2051 while I <= Length(Str) do
2052 begin
2053 if Str[I] = Chr(13) then
2054 Str := Copy(Str, 1, I) + Chr(10) + Copy(Str, I + 1, Length(Str) - I);
2055 Inc(I);
2056 end;
2057 end;
2058
2059 // convert to 7 bit data
2060 procedure Force7BitData;
2061 var
2062 I: Integer;
2063 begin
2064 for I := 1 to Length(Str) do
2065 Str[I] := Char(Byte(Str[I]) and $0FFFFFFF);
2066 end;
2067
2068 begin
2069 if (Length(FPartChar) = 0) then
2070 begin
2071 SetLength(Str, Count);
2072 Move(Buffer, Str[1], Count);
2073 end
2074 else begin
2075 L:= Length(FPartChar);
2076 SetLength(Str, Count + L);
2077 Move(FPartChar[1], Str[1], L);
2078 Move(Buffer, Str[L + 1], Count);
2079 FPartChar:= EmptyStr;
2080 end;
2081 if FForce7Bit then
2082 begin
2083 Force7BitData;
2084 end;
2085 if FAppendLF then
2086 begin
2087 AppendLineFeeds;
2088 end;
2089 StringReceived(Str);
2090 end;
2091
2092 function TCustomComTerminal.GetConnected: Boolean;
2093 begin
2094 Result := False;
2095 if FPtyDevice <> nil then
2096 Result := FPtyDevice.Connected;
2097 end;
2098
2099 procedure TCustomComTerminal.SetConnected(const Value: Boolean);
2100 begin
2101 if FPtyDevice <> nil then
2102 FPtyDevice.Connected := Value;
2103 end;
2104
2105 procedure TCustomComTerminal.SetScrollBars(const Value: TScrollStyle);
2106 begin
2107 if FScrollBars <> Value then
2108 begin
2109 FScrollBars := Value;
2110 RecreateWnd(Self);
2111 end;
2112 end;
2113
2114 procedure TCustomComTerminal.SetColumns(const Value: Integer);
2115 begin
2116 if Value <> FColumns then
2117 begin
2118 FColumns := Min(Max(2, Value), 256);
2119 AdjustSize;
2120 if not ((csLoading in ComponentState) or (csDesigning in ComponentState)) then
2121 begin
2122 FMainBuffer.Init(0, FColumns);
2123 FAlternateBuffer.Init(0, FColumns);
2124 if Assigned(FPtyDevice) then
2125 FPtyDevice.SetScreenSize(FColumns, FVisibleRows);
2126 Invalidate;
2127 end;
2128 UpdateScrollRange;
2129 end;
2130 end;
2131
2132 procedure TCustomComTerminal.SetRows(const Value: Integer);
2133 var
2134 ARows: Integer;
2135 begin
2136 ARows := Max(Value, FVisibleRows);
2137 if ARows <> FRows then
2138 begin
2139 FRows := ARows;
2140 if not ((csLoading in ComponentState) or (csDesigning in ComponentState)) then
2141 begin
2142 FMainBuffer.Init(FRows, 0);
2143 end;
2144 UpdateScrollRange;
2145 end;
2146 end;
2147
2148 procedure TCustomComTerminal.SetEmulation(const Value: TTermEmulation);
2149 begin
2150 if FEmulation <> Value then
2151 begin
2152 FEmulation := Value;
2153 if not (csLoading in ComponentState) then
2154 begin
2155 FEscapeCodes.Free;
2156 CreateEscapeCodes;
2157 end;
2158 end;
2159 end;
2160
2161 procedure TCustomComTerminal.SetCaret(const Value: TTermCaret);
2162 begin
2163 if Value <> FCaret then
2164 begin
2165 FCaret := Value;
2166 if Focused then
2167 begin
2168 DestroyCaret(Handle);
2169 InitCaret;
2170 end;
2171 end;
2172 end;
2173
2174 procedure TCustomComTerminal.SetPtyDevice(const Value: TCustomPtyDevice);
2175 begin
2176 if Value <> FPtyDevice then
2177 begin
2178 if FPtyDevice <> nil then
2179 begin
2180 FPtyDevice.OnRxBuf:= nil;
2181 end;
2182 FPtyDevice := Value;
2183 if FPtyDevice <> nil then
2184 begin
2185 FPtyDevice.OnRxBuf:= RxBuf;
2186 FPtyDevice.FreeNotification(Self);
2187 FPtyDevice.SetScreenSize(FColumns, FVisibleRows);
2188 end;
2189 end;
2190 end;
2191
2192 end.