Skip to content

Commit

Permalink
Use SCI_GETDIRECTSTATUSFUNCTION from 5.1.0, halving the number of cal…
Browse files Browse the repository at this point in the history
…ls to Scintilla.
  • Loading branch information
martijnlaan committed Jun 13, 2024
1 parent caec92a commit a2e773c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 13 additions & 5 deletions Components/ScintEdit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ TScintEdit = class(TWinControl)
FAutoCompleteFontSize: Integer;
FCodePage: Integer;
FDirectPtr: Pointer;
FDirectStatusFunction: SciFnDirectStatus;
FEffectiveCodePage: Integer;
FEffectiveCodePageDBCS: Boolean;
FFillSelectionToEdge: Boolean;
Expand Down Expand Up @@ -532,19 +533,20 @@ procedure TScintEdit.BeginUndoAction;
end;

function TScintEdit.Call(Msg: Cardinal; WParam: Longint; LParam: Longint): Longint;
var
ErrorStatus: LRESULT;

begin
HandleNeeded;
if FDirectPtr = nil then
Error('Call: FDirectPtr is nil');
Result := Scintilla_DirectFunction(FDirectPtr, Msg, WParam, LParam);
if not Assigned(FDirectStatusFunction) then
Error('Call: FDirectStatusFunction is nil');
var ErrorStatus: Integer;
Result := FDirectStatusFunction(FDirectPtr, Msg, WParam, LParam, ErrorStatus);

ErrorStatus := Scintilla_DirectFunction(FDirectPtr, SCI_GETSTATUS, 0, 0);
if ErrorStatus <> 0 then begin
Scintilla_DirectFunction(FDirectPtr, SCI_SETSTATUS, 0, 0);
ErrorFmt('Error status %d returned after Call(%u, %d, %d) = %d',
[ErrorStatus, Msg, WParam, LParam, Result]);
FDirectStatusFunction(FDirectPtr, SCI_SETSTATUS, 0, 0, ErrorStatus);

This comment has been minimized.

Copy link
@jordanrussell

jordanrussell Jun 14, 2024

Member

ErrorFmt raises an exception, so that ordering won't work. If you were to go back to using Scintilla_DirectFunction for SCI_SETSTATUS then you wouldn't have to load the DLL manually?

(And why isn't there a Scintilla_DirectStatusFunction export??)

This comment has been minimized.

Copy link
@martijnlaan

martijnlaan Jun 14, 2024

Author Member

I was surprised that Scintilla_DirectStatusFunction isn't exported as well. Tried adding one but that involved adding too much stuff.

Scintilla_DirectFunction is also missing in the default vcxproj based build, and was at some point in Scintilla history removed all together. That's why I stopped using it.

Thanks for noticing about the exception 👍

This comment has been minimized.

Copy link
@jordanrussell

jordanrussell Jun 14, 2024

Member

Scintilla_DirectFunction was removed? Weird, it's still mentioned in the latest docs.

This comment has been minimized.

Copy link
@martijnlaan

martijnlaan Jun 14, 2024

Author Member

Sorry I meant it was removed but then restored when someone complained about.
https://sourceforge.net/p/scintilla/code/ci/1cf4c4953c7d76e62158638e92c57fc01846917f/

end;
end;

Expand Down Expand Up @@ -679,10 +681,15 @@ procedure TScintEdit.CreateParams(var Params: TCreateParams);

procedure TScintEdit.CreateWnd;
begin
if IsscintLibary = 0 then
Error('CreateWnd: IsscintLibary is 0');
inherited;
FDirectPtr := Pointer(SendMessage(Handle, SCI_GETDIRECTPOINTER, 0, 0));
if FDirectPtr = nil then
Error('CreateWnd: FDirectPtr is nil');
FDirectStatusFunction := SciFnDirectStatus(SendMessage(Handle, SCI_GETDIRECTSTATUSFUNCTION, WPARAM(FDirectPtr), 0));
if not Assigned(FDirectStatusFunction) then
Error('CreateWnd: FDirectStatusFunction is nil');

This comment has been minimized.

Copy link
@jordanrussell

jordanrussell Jun 14, 2024

Member

Why WPARAM(FDirectPtr) and not 0? The docs don't mention the method taking any arguments, and the example code for SCI_GETDIRECTFUNCTION passes 0.

This comment has been minimized.

Copy link
@martijnlaan

martijnlaan Jun 14, 2024

Author Member

Thanks, that's a mistake. I find this bit of the docs (many other bits as well) hard to read. Initially I also had problems getting the whole thing to work and that it when I thought it was missing FDirectPtr as wparam, but in the end it turned out things broke as soon as I made any final change which caused the export to become uncalled and therefore stopped Delphi from loading the DLL. Took a while to catch 🙃

UpdateCodePage;
Call(SCI_SETCOMMANDEVENTS, 0, 0);
Call(SCI_SETMODEVENTMASK, SC_MOD_INSERTTEXT or SC_MOD_DELETETEXT, 0);
Expand Down Expand Up @@ -1967,6 +1974,7 @@ procedure TScintEdit.CNNotify(var Message: TWMNotify);
procedure TScintEdit.WMDestroy(var Message: TWMDestroy);
begin
FDirectPtr := nil;
FDirectStatusFunction := nil;
inherited;
end;

Expand Down
13 changes: 11 additions & 2 deletions Components/ScintInt.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1276,9 +1276,18 @@ TSCNotification = record
characterSource: Integer; { SCN_CHARADDED }
end;

function Scintilla_DirectFunction(ptr: Pointer; iMessage: Cardinal;
wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; external 'isscint.dll';
SciFnDirectStatus = function(ptr: Pointer; iMessage: Cardinal;
wParam: WPARAM; lParam: LPARAM; var Status: Integer): LRESULT; cdecl;

var
IsscintLibary: HMODULE;

implementation

uses
PathFunc;

initialization
IsscintLibary := LoadLibrary(PChar(AddBackslash(PathExtractPath(ParamStr(0))) + 'isscint.dll'));

end.

0 comments on commit a2e773c

Please sign in to comment.