TCustomRichViewEdit.OnStyleConversion

<< Click to display table of contents >>

TCustomRichViewEdit.OnStyleConversion

Occurs while executing ApplyStyleConversion method.

type

  TRVStyleConversionEvent =

    procedure (Sender: TCustomRichViewEdit;

     StyleNo, UserData: Integer; AppliedToText: Boolean;

     var NewStyleNo: Integer) of object;

 

property OnStyleConversion:TRVStyleConversionEvent;

This event allows you to create custom conversion procedure of text styles for the selected text. This event is called for all selected text items allowing to change their text style.

It can be useful to implement commands like "make bold", "apply font", "change text color", etc.

Parameters:

StyleNo current text style (index in the collection of styles, Style.TextStyles);

UserData value passed as a parameter in ApplyStyleConversion;

AppliedToText True, if this event was called for conversion of style of text item; False, if it was called for conversion of the current text style (CurTextStyleNo);

NewStyleNo initially equals to StyleNo; assign a new value to this parameter to change text style.

 

Example 1

procedure TMyForm.MyRichViewEditStyleConversion(

  Sender: TCustomRichViewEdit;

  StyleNo, UserData: Integer; AppliedToText: Boolean;

  var NewStyleNo: Integer);

begin

   if not (AppliedToText and

      (rvprStyleProtect in Style.TextStyles[StyleNo].Protection)) then

    NewStyleNo := UserData;

end;

This example does exactly the same work as ApplyTextStyle, i.e. MyRichViewEdit.ApplyStyleConversion(StyleNo) will be equivalent to MyRichViewEdit.ApplyTextStyle(StyleNo).

You can see that ApplyStyleConversion ignores protection of styles. If you wish to protect them, you should check protection yourself and do not change this style (like in the example above).

 

Example 2

procedure TMyForm.MyRichViewEditStyleConversion(

  Sender: TCustomRichViewEdit;

  StyleNo, UserData: Integer; AppliedToText: Boolean;

  var NewStyleNo: Integer);

var FontInfo: TFontInfo;

begin

  // creating text style with the desired attributes

  FontInfo := TFontInfo.Create(nil);

  try

    FontInfo.Assign(RVStyle1.TextStyles[StyleNo]);

    case UserData of

      TEXT_BOLD:

        if btnBold.Down then

          FontInfo.Style := FontInfo.Style+[fsBold]

        else

          FontInfo.Style := FontInfo.Style-[fsBold];

      TEXT_ITALIC:

        if btnItalic.Down then

          FontInfo.Style := FontInfo.Style+[fsItalic]

        else

          FontInfo.Style := FontInfo.Style-[fsItalic];

      TEXT_UNDERLINE:

        if btnUnderline.Down then

          FontInfo.Style := FontInfo.Style+[fsUnderline]

        else

          FontInfo.Style := FontInfo.Style-[fsUnderline];

      TEXT_APPLYFONTNAME:

        FontInfo.FontName := FontName;

      TEXT_APPLYFONTSIZE:

        FontInfo.Size     := FontSize;

      TEXT_APPLYFONT:

        FontInfo.Assign(FontDialog1.Font);

      TEXT_COLOR:

        FontInfo.Color := ColorDialog1.Color;

      TEXT_BACKCOLOR:

        FontInfo.BackColor := ColorDialog1.Color;

      // add your code here....

    end;

    // searching for the style (or adding it if necessary)

    NewStyleNo := RVStyle1.FindTextStyle(FontInfo);

  finally

    FontInfo.Free;

  end;

end;

This example shows how to implement the following commands:

make bold/not bold,

make italic/not italic,

make underlined/not underlined,

change font name,

change font size,

apply font,

change text color,

change text background color.

In this code:

TEXT_*** – user defined integer constants with unique values; these constants identify commands; they are passed to ApplyStyleConversion as UserData;

RVStyle1 – TRVStyle component linked with the richview editor;

ColorDialog1: TColorDialog;

FontDialog1: TFontDialog;

btnBold, btnItalic, btnUnderline: TSpeedButton;

FontName: String;

FontSize: Integer.

 

See demos:

Demos\*\Editors\Editor 2\

See also:

OnParaStyleConversion.

See also methods:

ApplyStyleConversion.