Recent

Author Topic: EXAMPLE: TRichMemo Read a row, search and modify attributes  (Read 490 times)

What I can do

  • Full Member
  • ***
  • Posts: 127
EXAMPLE: TRichMemo Read a row, search and modify attributes
« on: October 21, 2024, 11:45:56 pm »
Lazarus:3.4
Window 10@64
Experience Level: NEWBIE
Project: change the font parameters
           of searched selected text
  Note: Use Search to highlight SetStart to SelLength then set the font.paranters
All works great except for the the '//' search text because it doesn't have a second search to find the next text giving me a begin highlight to and end highlight
successful test 'String' is clBlue
successful test Procedure is [fsBold]
successful test '.TObject' is [fsUnderline] and clBrown   
fail test '//' (need the length of the line row and subtract the position of the memo.carret with in that row length)
I can hack my way through this if
 1. get the row length
 2. find the position in that row where the memo.carret is
 3. subtract the Carret position of the row from the total row length
 4. finally add that length to the SelLength:=SelStart+ //what ever the rest of the row length is
So all I need is to add from SelStart to the end of that row

Code: Pascal  [Select][+][-]
  1.   //......
  2. Procedure SetFontPacage(aMemo:TRichMemo;aSearchStyle:TSearchOptions;SStr:String;fnt:String;fSyl:TFontStyles;fc:Tcolor;fSiz:Integer;Tags:String='');
  3.   var
  4.     StartPos:LongInt;
  5.     SStrLen:LongInt;
  6.     s:string;
  7.   begin
  8.     StartPos:=0;
  9.     SStrLen:= Length(SStr);
  10.     While aMemo.Search(SStr,StartPos,Length(aMemo.rtf),aSearchStyle,StartPos,SStrLen) do
  11.      begin
  12.        if Tags>''
  13.          then
  14.           begin
  15.            StartPos:=aMemo.Search(SStr,StartPos,Length(aMemo.rtf),aSearchStyle);
  16.            SStrLen:=aMemo.Search(Tags,StartPos+1,Length(aMemo.rtf),aSearchStyle);
  17.            SStrLen:=(SStrLen-StartPos);
  18.           end;
  19.        if SStr='//'
  20.          then
  21.            begin
  22.             //aMemo.CharAtPos(Mouse.CursorPos.x,Mouse.CursorPos.y);
  23.             SStrPos:=aMemo.Lines.IndexOf(SStr,StartPos);
  24.             //  sstrPos:=aMemo.Lines.
  25.             SStrLen:= aMemo.CaretPos.y;
  26.             s:=aMemo.Lines[SStrLen];
  27.             SStrLen:= aMemo.CaretPos.x;
  28.             SStrLen:=Length(s);
  29.             SStrLen:=(Length(s)-SStrLen);
  30.            end;
  31.        aMemo.GetTextAttributes(StartPos,aFont);
  32.        if fnt<>'' then aFont.Name:=fnt;
  33.        if fSyl<>[] then aFont.Style:=fSyl;
  34.        if fc<>clNone then aFont.Color:=fc;
  35.        if fSiz<>0 then aFont.Size:=fSiz;
  36.        aMemo.SetTextAttributes(StartPos,SStrLen,aFont);
  37.        StartPos:=StartPos+ SStrLen;
  38.      end
  39.   end;
  40.  
  41.  

------------------------------------------
UPDATE
OK, it was a matter of figuring out what data is available at when event happens.
My udated code
Code: Pascal  [Select][+][-]
  1.   Procedure SetFontPacage(aMemo:TRichMemo;aSearchStyle:TSearchOptions;SStr:String;fnt:String;fSyl:TFontStyles;fc:Tcolor;fSiz:Integer;Tags:String='');
  2.   var
  3.     StartPos:LongInt;
  4.     SStrLen,I:LongInt;
  5.     s:string;
  6.   begin
  7.     if TaskProgressBar.Position<100 Then TaskProgressBar.Position:=TaskProgressBar.Position+1;
  8.     TaskProgText.Caption:='Task    '+SStr;
  9.     TaskProgressBar.Caption:=sstr;
  10.     Panel2.Invalidate;
  11.     StartPos:=0;
  12.     SStrLen:= Length(SStr);
  13.     While aMemo.Search(SStr,StartPos,Length(aMemo.rtf),aSearchStyle,StartPos,SStrLen) do
  14.      begin
  15.       aMemo.GetTextAttributes(StartPos,aFont);
  16.        if Tags>''
  17.          then
  18.           begin
  19.            //ShowMSG('Next search '+SStr);
  20.            if SStr='//'
  21.              then
  22.                begin
  23.                  aMemo.SelStart:=StartPos;
  24.                  I:=aMemo.CaretPos.y;
  25.                  s:= aMemo.Lines[I];
  26.                  SStrLen:=Length(s);
  27.                  SStrLen:=(SStrLen-aMemo.CaretPos.x)+Length(SStr);
  28.                end
  29.              else
  30.                begin
  31.                 SStrLen:=aMemo.Search(Tags,StartPos+1,Length(aMemo.rtf),aSearchStyle);
  32.                 if SStrlen=-1 then break;
  33.                 SStrLen:=(SStrLen-StartPos)+Length(SStr);
  34.                end;
  35.           end;
  36.        if fnt<>'' then aFont.Name:=fnt;
  37.        if fSyl<>[] then aFont.Style:=fSyl;
  38.        if fc<>clNone then aFont.Color:=fc;
  39.        if fSiz<>0 then aFont.Size:=fSiz;
  40.        aMemo.SetTextAttributes(StartPos,SStrLen,aFont);
  41.        DocProgressBar.Position:=Round(StartPos/DocProgressBar.Step);
  42.        StartPos:=StartPos+ SStrLen;
  43.      end
  44.   end;
  45.  

YEAH it works :)
« Last Edit: January 10, 2025, 10:13:20 pm by What I can do »

What I can do

  • Full Member
  • ***
  • Posts: 127
I forgot to update my code:
a very simple read a row from a Memo where the caret is located
require memory might be a consideration
the basic concept is to dump all to a StringList to read the row/line
This also strips most special RTF as well.
Code: Pascal  [Select][+][-]
  1. function ReadMemoRow(aMemo: TRichMemo): String;
  2. var
  3.   I: Integer;
  4.   s: String;
  5.   sl: TStringList;
  6. begin
  7.   // Get the row number based on caret position
  8.   I := aMemo.CaretPos.Y;
  9.  
  10.   // Create a string list to hold the memo's lines
  11.   sl := TStringList.Create;
  12.   try
  13.     // Populate the string list with the text of the memo
  14.     sl.Text := aMemo.Lines.Text;
  15.  
  16.     // Check if the index is within bounds
  17.     if (I >= 0) and (I < sl.Count) then
  18.       // Extract the text of the line at index I
  19.       s := sl[I]
  20.     else
  21.       s := ''; // Return an empty string if index is out of bounds
  22.  
  23.     Result := s; // Assign the extracted line to Result
  24.   finally
  25.     // Free the string list to avoid memory leaks
  26.     sl.Free;
  27.   end;
  28. end;

 

TinyPortal © 2005-2018