Skip to content

Commit

Permalink
Cleanup for assigning keys (and clearing them).
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnlaan committed Jun 20, 2024
1 parent 51efaee commit 8c45a3d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
51 changes: 51 additions & 0 deletions Components/ScintEdit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface

type
TScintChangeHistory = (schDisabled, schMarkers, schIndicators);
TScintCommand = type NativeInt;
TScintEditAutoCompleteSelectionEvent = TNotifyEvent;
TScintEditChangeInfo = record
Inserting: Boolean;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions Projects/Src/CompScintEdit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 8c45a3d

Please sign in to comment.