Page 1 of 1

TrichViewEdit : ChangeSelectedTextColor best code ?

Posted: Wed Jan 29, 2025 3:49 pm
by Fab85
Hello,

I'am always studying the TrichViewEdit component.
I have find two way to change by code (without using Taction) the text color of the selected text.
Which is the better one ? Maybe it's exists a better way ?

Code: Select all

procedure ChangeSelectedTextColor(ARichViewEditTarget: TCustomRichViewEdit; AColor: TColor);
var
  LNewStyleNo, LExistingStyleNo: Integer;
  LStyle: TFontInfo;
  LRVStyle: TRVStyle;
begin
  if not ARichViewEditTarget.CanChange then Exit;
   LRVStyle := ARichViewEditTarget.Style; 
   LNewStyleNo := LRVStyle.TextStyles.Count; 
    LStyle:=LRVStyle.TextStyles.Add;
    LStyle.Assign(LRVStyle.TextStyles[ARichViewEditTarget.CurTextStyleNo]);
    LStyle.Color := AColor; 
   ARichViewEditTarget.ApplyTextStyle(LNewStyleNo);
end;

Code: Select all

procedure ChangeSelectedTextColor(ARichViewEditTarget: TCustomRichViewEdit; AColor: TColor);
var
  LNewStyleNo, LExistingStyleNo: Integer;
  LStyle: TFontInfo;
  LRVStyle: TRVStyle;
begin
  if not ARichViewEditTarget.CanChange then Exit;
  LRVStyle := ARichViewEditTarget.Style; 
  LNewStyleNo := LRVStyle.TextStyles.Count;
   LStyle := TFontInfo.Create(nil);
    try
      LStyle.Assign(LRVStyle.TextStyles[ARichViewEditTarget.CurTextStyleNo]);
      LStyle.Color := AColor; 
      LRVStyle.TextStyles.Add.Assign(LStyle); 
    finally
      LStyle.Free;
    end; 
    ARichViewEditTarget.ApplyTextStyle(LNewStyleNo);
end;

Re: TrichViewEdit : ChangeSelectedTextColor best code ?

Posted: Wed Jan 29, 2025 4:00 pm
by Sergey Tkachenko
ApplyTextStyle applies the same font attributes to the selection (including font name, size, etc.).
If you want to change only one property (such as a text color) and do not want to change other properties, you need to use ApplyStyleConversion method and OnStyleConversion event.
See the demo in Demos\DelphiUnicode\Editors\Editor 2\

First, the demo defines integer constants for each operation, including the operation of changing text color:

Code: Select all

TEXT_COLOR         = 7;
Next, it uses cd: TColorDialog to request a color from user and call ApplyStyleConversion:

Code: Select all

  cd.Color := rvs.TextStyles[rve.CurTextStyleNo].Color;
  if cd.Execute then
    rve.ApplyStyleConversion(TEXT_COLOR);
(in this code, rve is TRichViewEdit, rvs is TRVStyle)

OnStyleConversion (I removed code for other operations):

Code: Select all

procedure TForm1.rveStyleConversion(Sender: TCustomRichViewEdit;
  StyleNo, UserData: Integer; AppliedToText: Boolean; var NewStyleNo: Integer);
var
  FontInfo: TFontInfo;
begin
  FontInfo := TFontInfo.Create(nil);
  try
    FontInfo.Assign(rvs.TextStyles[StyleNo]);
    case UserData of
      ...
      TEXT_COLOR:
        FontInfo.Color := cd.Color;
      ...
    end;
    NewStyleNo := rvs.FindTextStyle(FontInfo);
  finally
    FontInfo.Free;
  end;
end;

Re: TrichViewEdit : ChangeSelectedTextColor best code ?

Posted: Wed Jan 29, 2025 4:12 pm
by Fab85
Hello,

That's a bit complicated on my scenario :

I have a lot of TrichViewEdit (or TcxTRichViewEdit) on different forms. So a TDI application.
Each connected to a TRVStyle. TcxTRichViewEdit seems to automagically create him TRVStyle.

In a common TDataModule I have a TPopupMemu. All my TcxTrichViewEdit show this common TpopupMenu.
This TPopupMenu contains a Menu item to change color of the user connected in one click (I don't want a dialog box to select color).

So I need to Handle a OnStyleConversion event of each of my TRVStyle ?