Skip to content

TTMSFNCMemoCustomTheme

TTMSFNCMemoCustomTheme is a component to create custom theme definitions to be used in a TTMSFNCMemo.

Add a custom theme to TTMSFNCMemo

Adding a custom theme can be done programmatically or at design time. The general steps are as follow:

  1. Setup TTMSFNCMemoCustomTheme with the theme definitions
  2. Add a new item in the TTMSFNCMemo.CustomThemes collection and assign the TTMSFNCMemoCustomTheme instance to the TTMSFNCMemo.CustomLanguages[].Theme property.
  3. Set the TTMSFNCMemo.Theme property to mtCustom.
  4. Set the TTMSFNCMemo.CustomThemeName property to the custom theme name (if the first item is added, it will automatically be selected).

Doing the steps above programmatically:

procedure TForm1.Button1Click(Sender: TObject);
begin
  //Setup component as desired, e.g:
  //TMSFNCMemoCustomTheme1.Colors.EditorBackground := $2A211C;
  //TMSFNCMemoCustomTheme1.Colors.EditorForeground := gcWhite;
  TMSFNCMemo1.CustomThemes.Add.Theme := TMSFNCMemoCustomTheme1;
  TMSFNCMemo1.Theme := mtCustom;
  //Optionally:
  //TMSFNCMemo1.CustomThemeName := 'my-theme';
end;

Defining a theme

A theme can be defined with a combination of fixed colors and color rules for tokens.

Colors

The fixed colors and their meanings can be found in the exposed colors demo in the Monaco playground. These are available via the Colors property and can be set at design-time or programmatically.

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCMemoCustomTheme1.Colors.EditorBackground := $2A211C;
  TMSFNCMemoCustomTheme1.Colors.EditorForeground := gcWhite;
  //Then assign theme to the TTMSFNCMemo
end;

Rules

Rules allow to assign colors to specific tokens that is created by the tokenizer of a given language.

  • Token: string: The name of the token (e.g. 'string')
  • Foreground: TTMSFNCGraphicsColor: The foreground color of the token
  • Background: TTMSFNCGraphicsColor: The background color of the token
  • FontStyle: TFontStyles: The style of the font for a token

To set these programmatically, you can use various Add overrides:

procedure TForm1.FormCreate(Sender: TObject);
begin
  //token + font style
  TMSFNCMemoCustomTheme1.Rules.Add('number', [TFontStyle.fsItalic]); 
  //token + foreground color
  TMSFNCMemoCustomTheme1.Rules.Add('string', $008800);
  //token + foreground color + font style
  TMSFNCMemoCustomTheme1.Rules.Add('keywords', gcBlue, [TFontStyle.fsBold]); 
end;

Save and load a theme definition

Thanks to TTMSFNCPersistence, you can save and load a theme definition in the component. To save, simply call:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCMemoCustomTheme1.SaveSettingsToFile('path/to/my-theme.file');
end;

And to load:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCMemoCustomTheme1.LoadSettingsFromFile('path/to/my-theme.file');
end;

Properties

Property name Description
Base: string The base theme to be used.
Colors Predefined set of colors that determine the look and feel of the editor.
Inherit: Boolean Inherit rules from the base theme.
Rules Custom set of rules to control the styling of a token created by the language tokenizer.
ThemeName: string The name of the theme. If left empty, the name of the component will be used.

Methods

Method name Description
LoadSettingsFromFile Saves component settings (= theme definition) to a file
SaveSettingsToFile Loads component settings (= theme definition) from a file