Recent

Author Topic: Assign Search + Search an Replace to the correct tabsheet  (Read 4650 times)

scons

  • Full Member
  • ***
  • Posts: 141
Assign Search + Search an Replace to the correct tabsheet
« on: January 22, 2017, 12:17:01 pm »
Hi,

I am trying to implement a search + search and replace dialog to a synedit on a tabsheet. More tabs can bee added in runtime, same as opening a file.
But it seems to not be finding anything. I assume I am not putting the finddialog to the active sheet to find something, but I can't figure it out.

Here is my code so far:

Code: Pascal  [Select][+][-]
  1.  
  2. ...
  3.   TS := TTabSheet.Create(PageControl1);
  4.   TS.Parent := PageControl1;
  5.  
  6.   Editor := TSynEdit.Create(TS);
  7.   Editor.Parent := TS;
  8.   Editor.Name := 'Editor';
  9.  
  10. ...
  11.  
  12. procedure TForm1.FindDialog1Find(Sender: TObject);
  13. var
  14.   textreplace: integer;
  15.   textfind: string;
  16.   options: TSynSearchOptions;
  17. begin
  18.   textfind := FindDialog1.FindText;
  19.   options := [];
  20.   if not (frDown in FindDialog1.Options) then
  21.     options += [ssoBackwards];
  22.   if frMatchCase in FindDialog1.Options then
  23.     options += [ssoMatchCase];
  24.   if frWholeWord in FindDialog1.Options then
  25.     options += [ssoWholeWord];
  26.   if frEntireScope in FindDialog1.Options then
  27.     options += [ssoEntireScope];
  28.   textreplace := TSynEdit(PageControl1.ActivePage.FindComponent('Editor')).SearchReplace(textfind, '', options);
  29.   if textreplace = 0 then
  30.     ShowMessage('No match found:  ' + textfind);
  31. end;
  32.  
  33. ...
  34.  
  35. procedure TForm1.BtnSearchmClick(Sender: TObject);
  36. begin
  37.   FindDialog1.Execute
  38. end;
  39.  

Same type of code for the Search and Replace.

Is the problem here ?
Code: Pascal  [Select][+][-]
  1.   textreplace := TSynEdit(PageControl1.ActivePage.FindComponent('Editor')).SearchReplace(textfind, '', options);
  2.  

But   textreplace := Editor.SearchReplace(textfind, '', options); does also do nothing, I mean the dialog boxes pop up nicely but they can not find anything.


or here ?
Code: Pascal  [Select][+][-]
  1.   FindDialog1.Execute
  2.  

But adding Pagecontrol1.ActivePage; in front of the FindDialog1.Execute; does not seem to work.
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Assign Search + Search an Replace to the correct tabsheet
« Reply #1 on: January 22, 2017, 12:21:01 pm »
See LazEdit (search for LazEdit on the wiki).
It basically does the same as you are trying to do.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Assign Search + Search an Replace to the correct tabsheet
« Reply #2 on: January 22, 2017, 12:52:32 pm »
From my EPlus editor (from which LazEdit is derived):

Code: [Select]
procedure TEPlusForm.DoReplace(Sender: TObject);
var
  Ed: TEditor;
  Dlg: TReplaceDialog;
begin
  Ed := NoteBook.CurrentEditor;
  if Assigned(Ed) then
  begin
    Dlg := (Sender as TReplaceDialog);
    FindOptions := [];
    if not (frDown in Dlg.Options) then FindOptions := FindOptions + [ssoBackWards];
    if frMatchCase in Dlg.Options then FindOptions := FindOptions + [ssoMatchCase];
    if frWholeWord in Dlg.Options then FindOptions := FindOptions + [ssoWholeWord];
    if frReplace in Dlg.Options then ReplaceOptions := ReplaceOptions + [ssoReplace];
    if frReplaceAll in Dlg.Options then ReplaceOptions := ReplaceOptions + [ssoReplaceAll];
    if frFindNext in Dlg.Options then ReplaceOptions := ReplaceOptions - [ssoReplace,ssoReplaceAll];
    if Ed.SelAvail then ReplaceOptions := ReplaceOptions + [ssoSelectedOnly];

    FindText := Dlg.FindText;
    ReplaceText := Dlg.ReplaceText;

    if Ed.SearchReplace(FindText,ReplaceText,ReplaceOptions) = 0 then
      ShowError(Format(msgTextNotFound,[FindText]))
    else
    if (ssoReplace in ReplaceOptions) and not (ssoReplaceAll in ReplaceOptions) then
    begin
      ReplaceOptions := ReplaceOptions - [ssoReplace];
      Ed.SearchReplace(FindText,'',ReplaceOptions); //Search and select next occurence
    end;
  end;
end;

NoteBook is a TEditorPageControl (derived form TPageControl) and the getter for CurrentEditor looks like this:

Code: [Select]
function TEditorPageControl.GetCurrentEditor: TEditor;
var
  Pg: TTabSheet;
begin
  //DebugLn('TEditorPageControl.GetCurrentEditor: PageCount = ',DbgS(PageCount),' ActivePageIndex = ',DbgS(ActivePageIndex));
  Result := nil;
  //ActivePageIndex = -1 when you remove the last (as in no more pages available) page
  //PageCount will still be 1 at this time
  if (PageCount > 0) and (ActivePageIndex >= 0) then
  begin
    Pg := Pages[ActivePageIndex];
    Result := EditorAtPage(Pg);
  end;
end;


//and

//TEditor is a derived class form TSynEdit.

function TEditorPageControl.EditorAtPage(const APage: TTabSheet): TEditor;
var
  cc: Integer;
begin
  Result := nil;
  if not Assigned(APage) then Exit;
  for cc := 0 to APage.ComponentCount - 1 do
  begin
    if APage.Components[cc] is TEditor then
    begin
      Result := TEditor(APage.Components[cc]);
      Exit;
    end;
  end;
end;

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Assign Search + Search an Replace to the correct tabsheet
« Reply #3 on: January 22, 2017, 01:00:54 pm »
As a side note:

Code: Pascal  [Select][+][-]
  1. ...
  2.   Editor.Name := 'Editor';
  3. ...
  4.  

I think that is going to crash as soon as you create a new page (with an editor with the same name).

There is no need to set Name here, but if you insist (as I did) then you have to come up with a mechanism to create unique names.
(I just keep a counter that increments each time I create an editor and use that as the basis for the name. It will fail if one tries to create > High(Cardinal) editors in one session, but I would very much like to see anyone doing that...)

Bart

scons

  • Full Member
  • ***
  • Posts: 141
Re: Assign Search + Search an Replace to the correct tabsheet
« Reply #4 on: January 22, 2017, 02:37:50 pm »

NoteBook is a TEditorPageControl (derived form TPageControl) and the getter for CurrentEditor looks like this:


//TEditor is a derived class form TSynEdit.

So I have to replace all my SynEdit with Notebook and Editor with TEditor ??

As a side note:

Code: Pascal  [Select][+][-]
  1. ...
  2.   Editor.Name := 'Editor';
  3. ...
  4.  

I think that is going to crash as soon as you create a new page (with an editor with the same name).


So far it does not crash, I can add tabs and delete and add again without errors.

I didn't add printing or saving dialogs yet so maybe problems still are in the line of progress.
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Assign Search + Search an Replace to the correct tabsheet
« Reply #5 on: January 22, 2017, 05:29:23 pm »
So I have to replace all my SynEdit with Notebook and Editor with TEditor ??

Where did I say that?
I only tried to clarify what my code was about, sou you could follow it and "translate" it into your setup.

Bart

scons

  • Full Member
  • ***
  • Posts: 141
Re: Assign Search + Search an Replace to the correct tabsheet
« Reply #6 on: January 23, 2017, 10:42:29 am »

Where did I say that?


You didn't, it was just a question to be sure I got it correctly.

I will try your solution.

Thanks
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

 

TinyPortal © 2005-2018