From 8c45a3da4be986bd4a76fb4e72915b37ff8850f7 Mon Sep 17 00:00:00 2001 From: Martijn Laan <1092369+martijnlaan@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:49:28 +0200 Subject: [PATCH] Cleanup for assigning keys (and clearing them). --- Components/ScintEdit.pas | 51 ++++++++++++++++++++++++++++++++++ Projects/Src/CompScintEdit.pas | 14 +++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/Components/ScintEdit.pas b/Components/ScintEdit.pas index eca916dd8..8f880b459 100644 --- a/Components/ScintEdit.pas +++ b/Components/ScintEdit.pas @@ -22,6 +22,7 @@ interface type TScintChangeHistory = (schDisabled, schMarkers, schIndicators); + TScintCommand = type NativeInt; TScintEditAutoCompleteSelectionEvent = TNotifyEvent; TScintEditChangeInfo = record Inserting: Boolean; @@ -46,6 +47,8 @@ TScintEditChangeInfo = record sffLineAfterExpanded, sffLineAfterContracted, sffLevelNumbers, sffLineState); TScintFoldFlags = set of TScintFoldFlag; TScintIndentationGuides = (sigNone, sigReal, sigLookForward, sigLookBoth); + TScintKeyCode = type Word; + TScintKeyDefinition = type Cardinal; TScintStyleByteIndicatorNumber = 0..1; { Could be increased to 0..StyleNumberUnusedBits-1 } TScintStyleByteIndicatorNumbers = set of TScintStyleByteIndicatorNumber; TScintIndicatorNumber = INDICATOR_CONTAINER..INDICATOR_MAX; @@ -209,6 +212,10 @@ TScintEdit = class(TWinControl) constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure AddMarker(const Line: Integer; const Marker: TScintMarkerNumber); + procedure AssignCmdKey(const Key: AnsiChar; const Shift: TShiftState; + const Command: TScintCommand); overload; + procedure AssignCmdKey(const KeyCode: TScintKeyCode; const Shift: TShiftState; + const Command: TScintCommand); overload; procedure BeginUndoAction; function Call(Msg: Cardinal; WParam: Longint; LParam: Longint): Longint; function CallStr(Msg: Cardinal; WParam: Longint; @@ -218,6 +225,8 @@ TScintEdit = class(TWinControl) function CanUndo: Boolean; procedure ChooseCaretX; procedure ClearAll; + procedure ClearCmdKey(const Key: AnsiChar; const Shift: TShiftState); overload; + procedure ClearCmdKey(const KeyCode: TScintKeyCode; const Shift: TShiftState); overload; procedure ClearIndicators(const IndicatorNumber: TScintIndicatorNumber); procedure ClearSelection; procedure ClearUndo(const ClearChangeHistory: Boolean = True); @@ -268,6 +277,9 @@ TScintEdit = class(TWinControl) function GetWordEndPosition(const Pos: Integer; const OnlyWordChars: Boolean): Integer; function GetWordStartPosition(const Pos: Integer; const OnlyWordChars: Boolean): Integer; function IsPositionInViewVertically(const Pos: Integer): Boolean; + class function KeyCodeAndShiftToKeyDefinition(const KeyCode: TScintKeyCode; + Shift: TShiftState): TScintKeyDefinition; + class function KeyToKeyCode(const Key: AnsiChar): TScintKeyCode; procedure PasteFromClipboard; function RawSelTextEquals(const S: TScintRawString; const MatchCase: Boolean): Boolean; class function RawStringIsBlank(const S: TScintRawString): Boolean; @@ -550,6 +562,18 @@ procedure TScintEdit.ApplyOptions; Call(SCI_SETCHANGEHISTORY, SC_CHANGE_HISTORY_DISABLED, 0); end; +procedure TScintEdit.AssignCmdKey(const Key: AnsiChar; const Shift: TShiftState; + const Command: TScintCommand); +begin + AssignCmdKey(KeyToKeyCode(Key), Shift, Command); +end; + +procedure TScintEdit.AssignCmdKey(const KeyCode: TScintKeyCode; + const Shift: TShiftState; const Command: TScintCommand); +begin + Call(SCI_ASSIGNCMDKEY, KeyCodeAndShiftToKeyDefinition(KeyCode, Shift), Command); +end; + procedure TScintEdit.BeginUndoAction; begin Call(SCI_BEGINUNDOACTION, 0, 0); @@ -626,6 +650,16 @@ procedure TScintEdit.ClearAll; ChooseCaretX; end; +procedure TScintEdit.ClearCmdKey(const Key: AnsiChar; const Shift: TShiftState); +begin + ClearCmdKey(KeyToKeyCode(Key), Shift); +end; + +procedure TScintEdit.ClearCmdKey(const KeyCode: TScintKeyCode; const Shift: TShiftState); +begin + Call(SCI_CLEARCMDKEY, KeyCodeAndShiftToKeyDefinition(KeyCode, Shift), 0); +end; + procedure TScintEdit.ClearIndicators( const IndicatorNumber: TScintIndicatorNumber); begin @@ -1189,6 +1223,23 @@ function TScintEdit.IsPositionInViewVertically(const Pos: Integer): Boolean; Result := (P.Y >= 0) and (P.Y + GetLineHeight <= ClientHeight); end; +class function TScintEdit.KeyCodeAndShiftToKeyDefinition( + const KeyCode: TScintKeyCode; Shift: TShiftState): TScintKeyDefinition; +begin + Result := KeyCode; + if ssShift in Shift then + Result := Result or (SCMOD_SHIFT shl 16); + if ssAlt in Shift then + Result := Result or (SCMOD_ALT shl 16); + if ssCtrl in Shift then + Result := Result or (SCMOD_CTRL shl 16); +end; + +class function TScintEdit.KeyToKeyCode(const Key: AnsiChar): TScintKeyCode; +begin + Result := Ord(UpCase(Key)); +end; + procedure TScintEdit.Notification(AComponent: TComponent; Operation: TOperation); begin inherited; diff --git a/Projects/Src/CompScintEdit.pas b/Projects/Src/CompScintEdit.pas index 90239080a..a7ec798a3 100644 --- a/Projects/Src/CompScintEdit.pas +++ b/Projects/Src/CompScintEdit.pas @@ -187,13 +187,13 @@ procedure TCompScintEdit.CreateWnd; Call(SCI_SETADDITIONALSELECTIONTYPING, 1, 0); Call(SCI_SETMULTIPASTE, SC_MULTIPASTE_EACH, 0); - Call(SCI_ASSIGNCMDKEY, Ord('C') or (SCMOD_CTRL shl 16), SCI_COPYALLOWLINE); - Call(SCI_ASSIGNCMDKEY, SCK_INSERT or (SCMOD_CTRL shl 16), SCI_COPYALLOWLINE); - Call(SCI_ASSIGNCMDKEY, Ord('X') or (SCMOD_CTRL shl 16), SCI_CUTALLOWLINE); - Call(SCI_ASSIGNCMDKEY, SCK_DELETE or (SCMOD_SHIFT shl 16), SCI_CUTALLOWLINE); - Call(SCI_ASSIGNCMDKEY, Ord('Z') or ((SCMOD_SHIFT or SCMOD_CTRL) shl 16), SCI_REDO); - Call(SCI_ASSIGNCMDKEY, SCK_UP or (SCMOD_ALT shl 16), SCI_MOVESELECTEDLINESUP); - Call(SCI_ASSIGNCMDKEY, SCK_DOWN or (SCMOD_ALT shl 16), SCI_MOVESELECTEDLINESDOWN); + AssignCmdKey('C', [ssCtrl], SCI_COPYALLOWLINE); + AssignCmdKey(SCK_INSERT, [ssCtrl], SCI_COPYALLOWLINE); + AssignCmdKey('X', [ssCtrl], SCI_CUTALLOWLINE); + AssignCmdKey(SCK_DELETE, [ssShift], SCI_CUTALLOWLINE); + AssignCmdKey('Z', [ssShift, ssCtrl], SCI_REDO); + AssignCmdKey(SCK_UP, [ssAlt], SCI_MOVESELECTEDLINESUP); + AssignCmdKey(SCK_DOWN, [ssAlt], SCI_MOVESELECTEDLINESDOWN); Call(SCI_SETSCROLLWIDTH, 1024 * CallStr(SCI_TEXTWIDTH, 0, 'X'), 0);