YOUSEFUL / DELPHI / COMPONENTS
TAtlasDBRichEdit
A data-aware rich edit that loads, formats and stores RTF in a blob or memo field and leaves posting to your code.
Description
TAtlasDBRichEdit is a TDBRichEdit descendant for RTF kept in a blob or memo field: any TBlobField, which covers ftBlob, ftMemo and ftWideMemo. Set DataSource and DataField as for the standard control. The constructor sets PlainText to False; keep it False, or the control treats the field as plain text.
Methods:
LoadRTFvalidates RTF bytes and writes them to the field.FormatSelectionapplies a font, size, style and colour to the selected text in the control.CommitToFieldcopies the control's current content, as RTF, into the field.SaveRTFreturns the control's current content as RTF bytes.
LoadRTF, FormatSelection and CommitToField put the dataset into edit state and never post. Call Post to keep the change or Cancel to go back to the stored RTF.
The control needs a parent window before these methods are called, because Windows Rich Edit reads and writes the RTF; the form does not have to be visible. Without a parent, LoadRTF raises EInvalidOperation (... has no parent window). The RTF check itself comes from TAtlasRichEdit in Atlas VCL.
Atlas Info adds no toolbar, find and replace, spell checking or printing to this control. For find and replace in a non-data-aware editor, see TAtlasRichEdit in Atlas VCL.
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates the control with PlainText False, Width 500 and Height 200. |
LoadRTF | method | Validates Data as RTF and stores it in the field of the current record. |
SaveRTF | method | Returns the control's current content as RTF bytes. |
FormatSelection | method | Applies FontName, Size in points, Styles and TextColor to the current selection (SelStart, SelLength). |
CommitToField | method | Copies the control's content (SaveRTF) into the field. |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates the control with PlainText False, Width 500 and Height 200.
Methods
LoadRTF method
procedure LoadRTF(const Data: TBytes);Validates Data as RTF and stores it in the field of the current record.
Checks, before the record is touched: DataSource has an active dataset (An active data source is required); the control has a field (A data field is required) that is writable, and the control is not ReadOnly (Field is read-only: <name>); the field is a TBlobField (RTF requires a blob or memo field); Data is RTF. Fewer than six bytes, or bytes that do not start with {\rtf, raise EArgumentException (Expected RTF document bytes).
The dataset is then put into edit state if it was browsing, the bytes are stored in the field unchanged and the control shows them. If storing fails, an edit started by this call is cancelled. The record is not posted.
Rich.LoadRTF(TEncoding.ASCII.GetBytes('{\rtf1\ansi \b Welcome\b0 notes}'));
Customers.Post;SaveRTF method
function SaveRTF: TBytes;Returns the control's current content as RTF bytes. If the field has not been loaded into the control yet, it is loaded first. The bytes are written out by Windows Rich Edit, so they include formatting applied in the control that has not been committed, and they are not byte-for-byte the RTF that was stored. The record is not changed.
TFile.WriteAllBytes('notes.rtf', Rich.SaveRTF);FormatSelection method
procedure FormatSelection(const FontName: string; Size: Integer; Styles: TFontStyles; TextColor: TColor);Applies FontName, Size in points, Styles and TextColor to the current selection (SelStart, SelLength).
Checks: Size is 1..512 (Font size must be 1..512), the dataset is active and the field is writable. The method then calls Edit on the dataset, makes sure the field content is loaded, restores the selection and sets SelAttributes. It does not write the field itself: call CommitToField before Post to store the formatting.
Rich.SelStart := 0;
Rich.SelLength := 7;
Rich.FormatSelection('Segoe UI', 14, [fsBold], clNavy);
Rich.CommitToField;
Customers.Post;CommitToField method
procedure CommitToField;Copies the control's content (SaveRTF) into the field. Checks for an active dataset, a writable field and a TBlobField, calls Edit (which does nothing when the dataset is already editing) and stores the bytes. The record is not posted.
Examples
Store RTF in a record
Creates the editor on a parent control, replaces the current record's notes and posts. A second LoadRTF followed by Cancel would restore these notes.
uses System.SysUtils, Data.DB, Vcl.Controls, Atlas.DBControls;
function AddNotesEditor(Parent: TWinControl; Source: TDataSource): TAtlasDBRichEdit;
begin
Result := TAtlasDBRichEdit.Create(Parent);
Result.Parent := Parent; // required: Windows Rich Edit needs a parent window
Result.DataSource := Source;
Result.DataField := 'Notes'; // a blob or memo field
end;
procedure ReplaceNotes(Rich: TAtlasDBRichEdit; const RTF: TBytes);
begin
Rich.LoadRTF(RTF); // validates, writes the field, leaves the record in edit state
Rich.DataSource.DataSet.Post;
end;Format a word and save the notes to a file
Makes the first word bold and navy, stores the formatting in the field, and writes the control's RTF to a file.
uses System.SysUtils, System.IOUtils, Vcl.Graphics, Atlas.DBControls;
procedure EmphasiseFirstWord(Rich: TAtlasDBRichEdit; WordLength: Integer);
begin
Rich.SelStart := 0;
Rich.SelLength := WordLength;
Rich.FormatSelection('Segoe UI', 14, [fsBold], clNavy); // changes the control, starts editing
Rich.CommitToField; // copies the formatted RTF into the field
Rich.DataSource.DataSet.Post;
end;
procedure ExportNotes(Rich: TAtlasDBRichEdit; const FileName: string);
begin
TFile.WriteAllBytes(FileName, Rich.SaveRTF);
end;Related
TAtlasDBGrid · EAtlasDatabaseError
Also in Atlas Info
TAtlasDBGrid · TAtlasDBLookupCombo · TAtlasDataFilter · TAtlasFilterEdit