Recent

Author Topic: [Solved]Problem when adding an event handler to a custom synedit  (Read 3508 times)

Basile B.

  • Guest
Hello, what is the right way to add a custom event handler to a TSynEdit ?

The following code results into an infinite loop (in AddGenericHandler()):

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, SynEdit,
  SynMemo, SynEditTextBuffer, LazSynEditText;

type
  TEditWithInternalChange = class(TSynMemo)
  private
    procedure internalChanged(Sender: TObject);
  public
    constructor Create(aOwner: TComponent); override;
  end;

  TForm1 = class(TForm)
  private
    ed: TEditWithInternalChange;
  public
    constructor Create(aOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation
{$R *.lfm}

constructor TEditWithInternalChange.Create(aOwner: TComponent);
begin
  inherited;
  TSynEditStringList(Lines).AddNotifyHandler(senrUndoRedoAdded, @internalChanged);
end;
procedure TEditWithInternalChange.internalChanged(Sender: TObject);
begin
end;

constructor TForm1.Create(aOwner: TComponent);
begin
  inherited;
  ed := TEditWithInternalChange.create(self);
  ed.Parent := Self;
end;

end.

I suspect the "Lines" getter to be responsible of the problem (since the getter is actually a proxy function) but I'm not quite sure about that. Note that I cant use the public onChange event to overcome the issue because the event is used in the outside world.

I attached the small demo illustrating the bug.
« Last Edit: July 17, 2014, 06:33:41 pm by Basile B. »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9909
  • Debugger - SynEdit - and more
    • wiki
Re: Problem when adding an event handler to a custom synedit
« Reply #1 on: July 17, 2014, 02:20:52 pm »
Lines are NOT TSynEditStringList.

The invalid typecast can lead to any error.

Lines are a wrapper, designed to HIDE the details that you try to access


Since you do subclass, try either of the following 2:
Code: [Select]
  protected
    property ViewedTextBuffer: TSynEditStrings read GetViewedTextBuffer;        // As viewed internally (with uncommited spaces / TODO: expanded tabs, folds). This may change, use with care
    property TextBuffer: TSynEditStrings read GetTextBuffer;                    // (TSynEditStringList) No uncommited (trailing/trimmable) spaces

probably better:
  TextBuffer: TSynEditStrings

TSynEditStrings  has the Add...Handler

Basile B.

  • Guest
Re: Problem when adding an event handler to a custom synedit
« Reply #2 on: July 17, 2014, 06:33:17 pm »
Ok that was the error. I've been lead to the confusion because of the TSynEditConstructor which has something like:
Code: [Select]
TSynEditStringList(flines).Add...

 

TinyPortal © 2005-2018