-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDW.OTA.CustomMessage.pas
More file actions
188 lines (163 loc) · 5.22 KB
/
DW.OTA.CustomMessage.pas
File metadata and controls
188 lines (163 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
unit DW.OTA.CustomMessage;
{*******************************************************}
{ }
{ TOTAL - Terrific Open Tools API Library }
{ }
{*******************************************************}
interface
uses
System.Types,
ToolsAPI,
Vcl.Graphics;
type
TCustomMessage = class(TInterfacedObject, IOTACustomMessage, INTACustomDrawMessage)
private
FColumnNumber: Integer;
FFileName: string;
FLineNumber: Integer;
FLineText: string;
public
{ IOTACustomMessage }
function GetColumnNumber: Integer;
function GetFileName: string;
function GetLineNumber: Integer;
function GetLineText: string;
procedure ShowHelp; virtual;
{ INTACustomDrawMessage }
function CalcRect(Canvas: TCanvas; MaxWidth: Integer; Wrap: Boolean): TRect; virtual;
procedure Draw(Canvas: TCanvas; const Rect: TRect; Wrap: Boolean); virtual;
public
constructor Create(const ALineText: string; const AFileName: string = ''; const ALineNumber: Integer = 0; const AColumnNumber: Integer = 0);
end;
TTextStyle = record
Bold: Boolean;
Italic: Boolean;
Underline: Boolean;
Color: TColor;
end;
// Uses same scheme as HTML for bold <b>, italic <i> and underline <u>
// Uses a tag with # for colors, e.g.: <#FF4455>
// Needs a closing tag for each opening tag, e.g. <b>This is bold and <#FF0000>this part is in red</#></b>
THighlightedCustomMessage = class(TCustomMessage)
private
procedure DrawText(const ACanvas: TCanvas; const AX, AY: Integer; const AText: string; const AStyle: TTextStyle);
public
procedure Draw(Canvas: TCanvas; const Rect: TRect; Wrap: Boolean); override;
end;
implementation
uses
System.SysUtils;
{ TCustomMessage }
constructor TCustomMessage.Create(const ALineText: string; const AFileName: string = ''; const ALineNumber: Integer = 0;
const AColumnNumber: Integer = 0);
begin
inherited Create;
FLineText := ALineText;
FFileName := AFileName;
FLineNumber := ALineNumber;
FColumnNumber := AColumnNumber;
end;
function TCustomMessage.CalcRect(Canvas: TCanvas; MaxWidth: Integer; Wrap: Boolean): TRect;
begin
Result := Canvas.ClipRect;
Result.Bottom := Result.Top + Canvas.TextHeight('W');
Result.Right := Result.Left + Canvas.TextWidth(FLineText);
end;
procedure TCustomMessage.Draw(Canvas: TCanvas; const Rect: TRect; Wrap: Boolean);
begin
Canvas.TextOut(Rect.Left, Rect.Top, FLineText);
end;
function TCustomMessage.GetColumnNumber: Integer;
begin
Result := FColumnNumber;
end;
function TCustomMessage.GetFileName: string;
begin
Result := FFileName;
end;
function TCustomMessage.GetLineNumber: Integer;
begin
Result := FLineNumber;
end;
function TCustomMessage.GetLineText: string;
begin
Result := FLineText;
end;
procedure TCustomMessage.ShowHelp;
begin
//
end;
{ THighlightedCustomMessage }
procedure THighlightedCustomMessage.Draw(Canvas: TCanvas; const Rect: TRect; Wrap: Boolean);
var
LStyle: TTextStyle;
LText: string;
I, LStartPos, LEndPos, LX: Integer;
LDefaultColor: TColor;
function ExtractTag(var APos: Integer): string;
var
LTagEnd: Integer;
begin
Inc(APos);
LTagEnd := APos;
while (LTagEnd <= Length(FLineText)) and (FLineText.Chars[LTagEnd] <> '>') do
Inc(LTagEnd);
Result := Copy(FLineText, APos + 1, LTagEnd - APos);
APos := LTagEnd + 1;
end;
procedure ApplyTagStyle(const LTag: string);
begin
if LTag = 'b' then
LStyle.Bold := True
else if LTag = '/b' then
LStyle.Bold := False
else if LTag = 'i' then
LStyle.Italic := True
else if LTag = '/i' then
LStyle.Italic := False
else if LTag = 'u' then
LStyle.Underline := True
else if LTag = '/u' then
LStyle.Underline := False
else if Copy(LTag, 1, 1) = '#' then
LStyle.Color := StringToColor('$' + Copy(LTag, 2, 6))
else if LTag = '/#' then
LStyle.Color := LDefaultColor;
end;
begin
LDefaultColor := Canvas.Font.Color;
LStyle.Bold := False;
LStyle.Italic := False;
LStyle.Underline := False;
LStyle.Color := LDefaultColor;
LX := Rect.Left;
I := 0;
while I < Length(FLineText) do
begin
if FLineText.Chars[I] <> '<' then
begin
LStartPos := I;
while (I < Length(FLineText)) and (FLineText.Chars[I] <> '<') do
Inc(I);
LEndPos := I - 1;
LText := Copy(FLineText, LStartPos + 1, LEndPos - LStartPos + 1);
DrawText(Canvas, LX, Rect.Top, LText, LStyle);
LX := LX + Canvas.TextWidth(LText);
end
else
ApplyTagStyle(ExtractTag(I));
end;
end;
procedure THighlightedCustomMessage.DrawText(const ACanvas: TCanvas; const AX, AY: Integer; const AText: string; const AStyle: TTextStyle);
begin
ACanvas.Font.Style := [];
if AStyle.Bold then ACanvas.Font.Style :=
ACanvas.Font.Style + [fsBold];
if AStyle.Italic then
ACanvas.Font.Style := ACanvas.Font.Style + [fsItalic];
if AStyle.Underline then
ACanvas.Font.Style := ACanvas.Font.Style + [fsUnderline];
ACanvas.Font.Color := AStyle.Color;
ACanvas.TextOut(AX, AY, AText);
end;
end.