YOUSEFUL / DELPHI / COMPONENTS
TAtlasRichEdit
A Windows RichEdit control with one-call selection formatting, find and replace, RTF bytes and redo.
Description
TAtlasRichEdit (unit Atlas.RichEdit) is a TRichEdit with the operations applications usually write themselves: formatting the selection in one call, find next with wrap-around, replace all, RTF to and from bytes, and redo. Everything else (Lines, SelStart, SelLength, SelAttributes, Paragraph, Undo) is the standard TRichEdit API. The control needs a parent window before it streams RTF; without one, LoadRTF raises EInvalidOperation. It is not a document designer: there is no ruler, toolbar, image or table editing, and no built-in spelling (see TAtlasSpellChecker for a way to combine the two).
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates a 600 x 400 editor in rich text mode (PlainText False) with both scroll bars. |
FormatSelection | method | Sets font name, size in points, styles and color of the selection, or of the insertion point when nothing is selected. |
FindNext | method | Searches forward from the end of the selection and selects the next match. |
ReplaceAll | method | Replaces every occurrence from the start of the text and returns the number of replacements. |
SaveRTF | method | Returns the whole document as RTF bytes (switching PlainText off first). |
LoadRTF | method | Replaces the document with RTF bytes. |
Redo | method | Redoes the last undone change (EM_REDO). |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates a 600 x 400 editor in rich text mode (PlainText False) with both scroll bars.
Methods
FormatSelection method
procedure FormatSelection(const FontName: string; FontSize: Integer; Styles: TFontStyles; TextColor: TColor);Sets font name, size in points, styles and color of the selection, or of the insertion point when nothing is selected. Raises EArgumentOutOfRangeException for a size outside 1..512.
Editor.FormatSelection('Segoe UI', 14, [fsBold], clNavy);FindNext method
function FindNext(const Search: string; MatchCase: Boolean = False; WholeWord: Boolean = False; Wrap: Boolean = True): Boolean;Searches forward from the end of the selection and selects the next match. With Wrap (the default) the search continues from the start of the text. Returns False and leaves the selection unchanged when Search is empty or nothing is found.
if not Editor.FindNext('total') then
ShowMessage('Not found');ReplaceAll method
function ReplaceAll(const Search, Replacement: string; MatchCase: Boolean = False; WholeWord: Boolean = False): Integer;Replaces every occurrence from the start of the text and returns the number of replacements. The search resumes after each inserted replacement, so a replacement that contains the search text is not replaced again. Raises EArgumentException when Search is empty. The selection moves while replacing.
SaveRTF method
function SaveRTF: TBytes;Returns the whole document as RTF bytes (switching PlainText off first).
LoadRTF method
procedure LoadRTF(const Data: TBytes);Replaces the document with RTF bytes. The bytes must start with {\rtf; otherwise it raises EArgumentException (Expected RTF document bytes) and the document is unchanged. The data is loaded into a hidden copy first, so data the RichEdit control rejects raises before the visible document changes.
Redo method
procedure Redo;Redoes the last undone change (EM_REDO). Undo is inherited from TCustomEdit.
Examples
Format a title and rename a term
Prints 1 | Orbit and atlas and Atlases (case and whole-word matching) and bold title: TRUE size 18.
uses System.SysUtils, Vcl.Graphics, Atlas.RichEdit;
procedure MakeTitle(Editor: TAtlasRichEdit; const Title: string);
begin
Editor.SelStart := 0;
Editor.SelLength := Length(Title);
Editor.FormatSelection('Segoe UI', 18, [fsBold], clNavy);
Editor.SelLength := 0;
end;
function RenameEverywhere(Editor: TAtlasRichEdit; const OldName, NewName: string): Integer;
begin
Result := Editor.ReplaceAll(OldName, NewName, True, True); // match case, whole words
end;Save and open RTF files
Reopens the text in a second editor; a plain text file is rejected with Expected RTF document bytes and the document is kept.
uses System.SysUtils, System.IOUtils, Atlas.RichEdit;
procedure SaveDocument(Editor: TAtlasRichEdit; const FileName: string);
begin
TFile.WriteAllBytes(FileName, Editor.SaveRTF);
end;
procedure OpenDocument(Editor: TAtlasRichEdit; const FileName: string);
begin
Editor.LoadRTF(TFile.ReadAllBytes(FileName)); // EArgumentException if the file is not RTF
end;Related
Also in Atlas VCL
TAtlasTable · TAtlasGrid · TAtlasValueEdit · TAtlasTree · TAtlasWorkbook · TAtlasSheetGrid · TAtlasPivot · TAtlasInspector · TAtlasSpellChecker · TAtlasChart · TAtlasGauge · TAtlasThemer · TAtlasWizard · TAtlasWorkspace · TAtlasCommandBar · TAtlasActionTile · TAtlasTileBoard · TAtlasNavigation · TAtlasMap · TAtlasGraph · TAtlasDiagram · TAtlasPrintDocument · TAtlasSchedule · TAtlasScheduler · TAtlasTaskPlan · TAtlasGantt · TAtlasPdfDocument · TAtlasPdfViewer