Skip to content

Commit 426ec55

Browse files
committed
Refactor TRichEditHelper as a class helper
Added new ClassHelpers.RichEdit unit to the project. Converted TRichEditHelper into a "class helper for TRichEdit" and moved it from URTFUtils into ClassHelpers.RichEdit. Updated all affected code.
1 parent c74924c commit 426ec55

9 files changed

+68
-44
lines changed

Src/ClassHelpers.RichEdit.pas

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at https://mozilla.org/MPL/2.0/
5+
*
6+
* Copyright (C) 2025, Peter Johnson (gravatar.com/delphidabbler).
7+
*
8+
* Class helper for TRichEdit.
9+
}
10+
11+
unit ClassHelpers.RichEdit;
12+
13+
interface
14+
15+
uses
16+
// Delphi
17+
ComCtrls,
18+
// Project
19+
URTFUtils;
20+
21+
type
22+
TRichEditHelper = class helper for TRichEdit
23+
public
24+
procedure Load(const ARTF: TRTF);
25+
end;
26+
27+
implementation
28+
29+
uses
30+
// Delphi
31+
SysUtils,
32+
Classes;
33+
34+
{ TRichEditHelper }
35+
36+
procedure TRichEditHelper.Load(const ARTF: TRTF);
37+
var
38+
Stream: TStream;
39+
begin
40+
PlainText := False;
41+
Stream := TMemoryStream.Create;
42+
try
43+
ARTF.ToStream(Stream);
44+
Stream.Position := 0;
45+
// must set MaxLength or long documents may not display
46+
MaxLength := Stream.Size;
47+
Lines.LoadFromStream(Stream, TEncoding.ASCII);
48+
finally
49+
Stream.Free;
50+
end;
51+
end;
52+
53+
end.

Src/CodeSnip.dpr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ uses
375375
FmRegisterCompilersDlg in 'FmRegisterCompilersDlg.pas' {RegisterCompilersDlg},
376376
ClassHelpers.UGraphics in 'ClassHelpers.UGraphics.pas',
377377
ClassHelpers.UActions in 'ClassHelpers.UActions.pas',
378-
USaveInfoMgr in 'USaveInfoMgr.pas';
378+
USaveInfoMgr in 'USaveInfoMgr.pas',
379+
ClassHelpers.RichEdit in 'ClassHelpers.RichEdit.pas';
379380

380381
// Include resources
381382
{$Resource ExternalObj.tlb} // Type library file

Src/CodeSnip.dproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@
582582
<DCCReference Include="ClassHelpers.UGraphics.pas"/>
583583
<DCCReference Include="ClassHelpers.UActions.pas"/>
584584
<DCCReference Include="USaveInfoMgr.pas"/>
585+
<DCCReference Include="ClassHelpers.RichEdit.pas"/>
585586
<None Include="CodeSnip.todo"/>
586587
<BuildConfiguration Include="Base">
587588
<Key>Base</Key>

Src/FrHiliterPrefs.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ implementation
178178
// Delphi
179179
SysUtils, ExtCtrls, Windows, Graphics, Dialogs,
180180
// Project
181+
ClassHelpers.RichEdit,
181182
FmPreferencesDlg, FmNewHiliterNameDlg, FmUserHiliterMgrDlg, Hiliter.UAttrs,
182183
IntfCommon, UCtrlArranger, UFontHelper, UIStringList, UMessageBox,
183184
URTFBuilder, URTFStyles, UUtils;
@@ -614,7 +615,7 @@ procedure THiliterPrefsFrame.UpdatePopupMenu;
614615

615616
procedure THiliterPrefsFrame.UpdatePreview;
616617
begin
617-
TRichEditHelper.Load(frmExample.RichEdit, GenerateRTF);
618+
frmExample.RichEdit.Load(GenerateRTF);
618619
end;
619620

620621
initialization

Src/FrPrintingPrefs.pas

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ implementation
101101
// Delphi
102102
SysUtils, Windows, Graphics, Math, ComCtrls,
103103
// Project
104+
ClassHelpers.RichEdit,
104105
FmPreferencesDlg, Hiliter.UAttrs, Hiliter.UHiliters, IntfCommon, UColours,
105106
UConsts, UEncodings, UFontHelper, UKeysHelper, UPrintInfo, URTFBuilder,
106-
URTFStyles, URTFUtils, UStrUtils, UUtils;
107+
URTFStyles, UStrUtils, UUtils;
107108

108109

109110
{$R *.dfm}
@@ -379,7 +380,7 @@ procedure TPrintingPrefsPreview.Generate(const UseColor, SyntaxPrint: Boolean);
379380
HiliteSource(UseColor, SyntaxPrint, Builder);
380381
Builder.EndPara;
381382
// Load document into rich edit
382-
TRichEditHelper.Load(fRe, Builder.Render);
383+
fRe.Load(Builder.Render);
383384
finally
384385
FreeAndNil(Builder);
385386
end;

Src/FrRTFPreview.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ implementation
5959

6060
uses
6161
// Project
62+
ClassHelpers.RichEdit,
6263
URTFUtils;
6364

6465

@@ -80,7 +81,7 @@ procedure TRTFPreviewFrame.LoadContent(const DocContent: TEncodedData);
8081
@param DocContent [in] Valid RTF document to be displayed.
8182
}
8283
begin
83-
TRichEditHelper.Load(reView, TRTF.Create(DocContent));
84+
reView.Load(TRTF.Create(DocContent));
8485
end;
8586

8687
procedure TRTFPreviewFrame.SetPopupMenu(const Menu: TPopupMenu);

Src/FrSourcePrefs.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ implementation
112112
// Delphi
113113
SysUtils, Math,
114114
// Project
115+
ClassHelpers.RichEdit,
115116
FmPreferencesDlg, Hiliter.UAttrs, Hiliter.UFileHiliter, Hiliter.UHiliters,
116117
IntfCommon, UConsts, UCtrlArranger, URTFUtils;
117118

@@ -358,8 +359,7 @@ procedure TSourcePrefsFrame.UpdatePreview;
358359
// Generate and display preview with required comment style
359360
Preview := TSourcePrefsPreview.Create(GetCommentStyle, fHiliteAttrs);
360361
try
361-
// Display preview
362-
TRichEditHelper.Load(frmPreview.RichEdit, Preview.Generate);
362+
frmPreview.RichEdit.Load(Preview.Generate);
363363
finally
364364
Preview.Free;
365365
end;

Src/UPrintEngine.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ implementation
6363
// Delphi
6464
Printers,
6565
// Project
66+
ClassHelpers.RichEdit,
6667
UMeasurement, UPrintInfo;
6768

6869

@@ -102,7 +103,7 @@ procedure TPrintEngine.Print(const Document: TRTF);
102103
sDefTitle = 'CodeSnip document'; // default document title
103104
begin
104105
// Load document into engine
105-
TRichEditHelper.Load(RichEdit, Document);
106+
RichEdit.Load(Document);
106107
// Set up page margins
107108
PrintMargins := GetPrintMargins;
108109
RichEdit.PageRect := Rect(

Src/URTFUtils.pas

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface
1717

1818
uses
1919
// Delphi
20-
SysUtils, Classes, ComCtrls,
20+
SysUtils, Classes,
2121
// Project
2222
UEncodings;
2323

@@ -130,20 +130,6 @@ TRTF = record
130130
/// <summary>Class of exception raised by TRTF</summary>
131131
ERTF = class(Exception);
132132

133-
type
134-
/// <summary>Static method record that assists in working with rich edit
135-
/// VCL controls.</summary>
136-
TRichEditHelper = record
137-
public
138-
/// <summary>Loads RTF code into a rich edit control, replacing existing
139-
/// content.</summary>
140-
/// <param name="RE">TRichEdit [in] Rich edit control.</param>
141-
/// <param name="RTF">TRTF [in] Contains rich text code to be loaded.
142-
/// </param>
143-
class procedure Load(const RE: TRichEdit; const RTF: TRTF); static;
144-
end;
145-
146-
147133
/// <summary>Returns a parameterless RTF control word of given kind.</summary>
148134
function RTFControl(const Ctrl: TRTFControl): ASCIIString; overload;
149135

@@ -184,8 +170,6 @@ implementation
184170

185171

186172
uses
187-
// Delphi
188-
Windows, RichEdit,
189173
// Project
190174
UExceptions;
191175

@@ -372,24 +356,5 @@ function TRTF.ToString: UnicodeString;
372356
Result := TEncoding.ASCII.GetString(fData);
373357
end;
374358

375-
{ TRichEditHelper }
376-
377-
class procedure TRichEditHelper.Load(const RE: TRichEdit; const RTF: TRTF);
378-
var
379-
Stream: TStream;
380-
begin
381-
RE.PlainText := False;
382-
Stream := TMemoryStream.Create;
383-
try
384-
RTF.ToStream(Stream);
385-
Stream.Position := 0;
386-
// must set MaxLength or long documents may not display
387-
RE.MaxLength := Stream.Size;
388-
RE.Lines.LoadFromStream(Stream, TEncoding.ASCII);
389-
finally
390-
Stream.Free;
391-
end;
392-
end;
393-
394359
end.
395360

0 commit comments

Comments
 (0)