-
-
Notifications
You must be signed in to change notification settings - Fork 924
/
NewStaticText.pas
287 lines (255 loc) · 7.71 KB
/
NewStaticText.pas
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
unit NewStaticText;
{
TNewStaticText - similar to TStaticText but with multi-line AutoSize
support and a WordWrap property
}
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms;
type
TNewStaticText = class(TWinControl)
private
FAutoSize: Boolean;
FFocusControl: TWinControl;
FForceLTRReading: Boolean;
FLastAdjustBoundsRTL: Boolean;
FShowAccelChar: Boolean;
FWordWrap: Boolean;
procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMParentFontChanged(var Message: TMessage); message CM_PARENTFONTCHANGED;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure AdjustBounds;
function CalcBounds: TPoint;
function GetDrawTextFlags: UINT;
procedure SetFocusControl(Value: TWinControl);
procedure SetForceLTRReading(Value: Boolean);
procedure SetShowAccelChar(Value: Boolean);
procedure SetWordWrap(Value: Boolean);
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetAutoSize(Value: Boolean); override;
public
constructor Create(AOwner: TComponent); override;
function AdjustHeight: Integer;
published
property Align;
property Anchors;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
property Caption;
property Color;
property DragCursor;
property DragMode;
property Enabled;
property FocusControl: TWinControl read FFocusControl write SetFocusControl;
property Font;
property ForceLTRReading: Boolean read FForceLTRReading write SetForceLTRReading
default False;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar: Boolean read FShowAccelChar write SetShowAccelChar
default True;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property WordWrap: Boolean read FWordWrap write SetWordWrap default False;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
procedure Register;
implementation
uses
BidiUtils;
procedure Register;
begin
RegisterComponents('JR', [TNewStaticText]);
end;
{ TNewStaticText }
constructor TNewStaticText.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csClickEvents, csSetCaption,
csOpaque, csReplicatable, csDoubleClicks];
Width := 65;
Height := 17;
FAutoSize := True;
FShowAccelChar := True;
AdjustBounds;
end;
procedure TNewStaticText.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
CreateSubClass(Params, 'STATIC');
with Params do
begin
Style := Style or SS_NOTIFY;
if not SetBiDiStyles(Self, Params) then begin
{ Quirk: No style is set for WordWrap=False in RTL mode; WS_EX_RIGHT
overrides SS_LEFTNOWORDWRAP, and there is no SS_RIGHTNOWORDWRAP style.
WordWrap=False still affects AdjustBounds, though. }
if not FWordWrap then Style := Style or SS_LEFTNOWORDWRAP;
end;
if not FShowAccelChar then Style := Style or SS_NOPREFIX;
if FForceLTRReading then ExStyle := ExStyle and not WS_EX_RTLREADING;
end;
end;
procedure TNewStaticText.CMDialogChar(var Message: TCMDialogChar);
begin
if (FFocusControl <> nil) and Enabled and ShowAccelChar and
IsAccel(Message.CharCode, Caption) then
with FFocusControl do
if CanFocus then
begin
SetFocus;
Message.Result := 1;
end;
end;
procedure TNewStaticText.CMFontChanged(var Message: TMessage);
begin
inherited;
AdjustBounds;
end;
procedure TNewStaticText.CMParentFontChanged(var Message: TMessage);
begin
inherited;
{ What we're really trapping here is changes to Parent. Recalculate size
if the new Parent's RTL setting is different. }
if IsParentRightToLeft(Self) <> FLastAdjustBoundsRTL then
AdjustBounds;
end;
procedure TNewStaticText.CMTextChanged(var Message: TMessage);
begin
inherited;
Invalidate;
AdjustBounds;
end;
procedure TNewStaticText.Loaded;
begin
inherited Loaded;
AdjustBounds;
end;
function TNewStaticText.GetDrawTextFlags: UINT;
begin
Result := DT_EXPANDTABS or DT_NOCLIP;
if FWordWrap then Result := Result or DT_WORDBREAK;
if not FShowAccelChar then Result := Result or DT_NOPREFIX;
if IsParentRightToLeft(Self) then begin
{ Note: DT_RTLREADING must be included even when just calculating the
size, since on certain fonts it can affect the width of characters.
(Consider the Hebrew string: 'a '#$F9' b'. On 2000 with Lucida Console
as the font, the spaces aren't drawn as wide with RTLREADING.) }
Result := Result or DT_RIGHT;
if not FForceLTRReading then
Result := Result or DT_RTLREADING;
end;
end;
function TNewStaticText.CalcBounds: TPoint;
var
R: TRect;
S: String;
DC: HDC;
begin
{ Note: The calculated width/height is actually one pixel wider/taller
than the size of the text, so that when Enabled=False the white shadow
does not get clipped }
R := Rect(0, 0, Width, 0);
if R.Right > 0 then Dec(R.Right);
S := Caption;
if (S = '') or (FShowAccelChar and (S[1] = '&') and (S[2] = #0)) then
S := S + ' ';
DC := GetDC(0);
try
SelectObject(DC, Font.Handle);
DrawText(DC, PChar(S), Length(S), R, DT_CALCRECT or GetDrawTextFlags);
finally
ReleaseDC(0, DC);
end;
Result.X := R.Right + 1;
Result.Y := R.Bottom + 1;
end;
procedure TNewStaticText.AdjustBounds;
var
NewBounds: TPoint;
NewLeft, NewWidth: Integer;
begin
if not (csLoading in ComponentState) and FAutoSize then
begin
FLastAdjustBoundsRTL := IsParentRightToLeft(Self);
NewBounds := CalcBounds;
NewLeft := Left;
NewWidth := Width;
if not FWordWrap then begin
NewWidth := NewBounds.X;
if IsParentFlipped(Self) then
Inc(NewLeft, Width - NewWidth);
end;
SetBounds(NewLeft, Top, NewWidth, NewBounds.Y);
end;
end;
function TNewStaticText.AdjustHeight: Integer;
var
OldHeight: Integer;
begin
OldHeight := Height;
Height := CalcBounds.Y;
Result := Height - OldHeight;
end;
procedure TNewStaticText.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FFocusControl) then
FFocusControl := nil;
end;
procedure TNewStaticText.SetAutoSize(Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
if Value then AdjustBounds;
end;
end;
procedure TNewStaticText.SetFocusControl(Value: TWinControl);
begin
FFocusControl := Value;
if Value <> nil then Value.FreeNotification(Self);
end;
procedure TNewStaticText.SetForceLTRReading(Value: Boolean);
begin
if FForceLTRReading <> Value then begin
FForceLTRReading := Value;
RecreateWnd;
AdjustBounds;
end;
end;
procedure TNewStaticText.SetShowAccelChar(Value: Boolean);
begin
if FShowAccelChar <> Value then
begin
FShowAccelChar := Value;
RecreateWnd;
AdjustBounds;
end;
end;
procedure TNewStaticText.SetWordWrap(Value: Boolean);
begin
if FWordWrap <> Value then
begin
FWordWrap := Value;
RecreateWnd;
AdjustBounds;
end;
end;
end.