Repository navigation
Expand file tree
/
Copy pathNewStaticText.pas
More file actions
455 lines (407 loc) · 13.2 KB
/
Copy pathNewStaticText.pas
File metadata and controls
455 lines (407 loc) · 13.2 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
unit NewStaticText;
{
TNewStaticText - similar to TStaticText but with multi-line AutoSize
support and a WordWrap property.
Define VCLSTYLES for full VCL Styles support, and for transparency support.
}
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
{$IFDEF VCLSTYLES} Vcl.Themes, {$ELSE} Themes, {$ENDIF}
Graphics;
{$IFDEF VCLSTYLES}
{$DEFINE TRANSPARENCYSUPPORT}
{$ENDIF}
type
TNewStaticText = class(TWinControl)
private
FAlignment: TAlignment;
FAutoSize: Boolean;
FFocusControl: TWinControl;
FForceLTRReading: Boolean;
FLastAdjustBoundsRTL: Boolean;
FShowAccelChar: Boolean;
{$IFNDEF TRANSPARENCYSUPPORT}
FTransparent: Boolean;
{$ENDIF}
FWordWrap: Boolean;
class constructor Create;
class destructor Destroy;
{$IFDEF TRANSPARENCYSUPPORT}
procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
{$ENDIF}
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 SetAlignment(Value: TAlignment);
procedure SetFocusControl(Value: TWinControl);
procedure SetForceLTRReading(Value: Boolean);
procedure SetShowAccelChar(Value: Boolean);
procedure SetTransparent(const Value: Boolean);
procedure SetWordWrap(Value: Boolean);
function GetTransparent: 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;
function GetControlsAlignment: TAlignment; override;
published
property Align;
property Alignment: TAlignment read FAlignment write SetAlignment
default taLeftJustify;
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 StyleElements;
property StyleName;
property TabOrder;
property TabStop;
property Transparent: Boolean read GetTransparent write SetTransparent
default True;
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;
TNewStaticTextStyleHook = class(TStyleHook)
{$IFDEF VCLSTYLES}
strict protected
procedure Paint(Canvas: TCanvas); override;
public
constructor Create(AControl: TWinControl); override;
{$ENDIF}
end;
procedure Register;
implementation
uses
StdCtrls, Types, BidiUtils;
procedure Register;
begin
RegisterComponents('JR', [TNewStaticText]);
end;
{ TNewStaticText }
class constructor TNewStaticText.Create;
begin
TCustomStyleEngine.RegisterStyleHook(TNewStaticText, TNewStaticTextStyleHook);
end;
constructor TNewStaticText.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csClickEvents, csSetCaption,
csReplicatable, csDoubleClicks, csGestures {$IF RtlVersion >= 35.0}, csNeedsDesignDisabledState{$ENDIF}];
{$IFNDEF TRANSPARENCYSUPPORT}
ControlStyle := ControlStyle + [csOpaque];
FTransparent := True; { Even though Transparent is ignored, we still want it to work as if it isn't at design time }
{$ENDIF}
Width := 65;
Height := 17;
FAutoSize := True;
FShowAccelChar := True;
AdjustBounds;
end;
procedure TNewStaticText.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((SS_LEFT, SS_RIGHT, SS_CENTER), (SS_RIGHT, SS_LEFT, SS_CENTER));
begin
inherited CreateParams(Params);
CreateSubClass(Params, 'STATIC');
with Params do
begin
const AlignmentStyle = Alignments[UseRightToLeftAlignment, FAlignment];
Style := Style or SS_NOTIFY or AlignmentStyle;
{ Quirk: There are no SS_RIGHTNOWORDWRAP and SS_CENTERNOWORDWRAP styles.
WordWrap=False still affects AdjustBounds, though. }
if (AlignmentStyle = SS_LEFT) and not FWordWrap then Style := Style or SS_LEFTNOWORDWRAP;
if not FShowAccelChar then Style := Style or SS_NOPREFIX;
if FForceLTRReading then ExStyle := ExStyle and not WS_EX_RTLREADING;
WindowClass.style := WindowClass.style and not CS_VREDRAW;
end;
end;
class destructor TNewStaticText.Destroy;
begin
TCustomStyleEngine.UnregisterStyleHook(TNewStaticText, TNewStaticTextStyleHook);
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 IsRightToLeft <> 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.GetControlsAlignment: TAlignment;
begin
{ This is needed to suppress the WS_EX_RIGHT style when BiDiMode is
bdRightToLeft and Alignment is not taLeftJustify }
Result := FAlignment;
end;
function TNewStaticText.GetDrawTextFlags: UINT;
const
Alignments: array[Boolean, TAlignment] of Word =
((DT_LEFT, DT_RIGHT, DT_CENTER), (DT_RIGHT, DT_LEFT, DT_CENTER));
begin
const AlignmentFlag = Alignments[UseRightToLeftAlignment, FAlignment];
Result := DT_EXPANDTABS or DT_NOCLIP or AlignmentFlag;
if FWordWrap then Result := Result or DT_WORDBREAK;
if not FShowAccelChar then Result := Result or DT_NOPREFIX;
if UseRightToLeftReading 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.) }
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 (Length(S) = 1)) 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 := IsRightToLeft;
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.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd;
end;
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;
{$IFDEF TRANSPARENCYSUPPORT}
procedure TNewStaticText.CNCtlColorStatic(var Message: TWMCtlColorStatic);
begin
{ Vcl.StdCtrls' TCustomStaticText.CNCtlColorStatic uses StyleServices(Self).Enabled
instead of IsCustomStyleActive, but that can return True even when no style is active,
which is undesirable because when no style is active the inherited call is required to
support the Font and Brush colors }
if IsCustomStyleActive and Transparent then
begin
SetBkMode(Message.ChildDC, Windows.TRANSPARENT);
StyleServices(Self).DrawParentBackground(Handle, Message.ChildDC, nil, False);
{ Return an empty brush to prevent Windows from overpainting what we just have created. }
Message.Result := LRESULT(GetStockObject(NULL_BRUSH));
end
else
inherited;
end;
{$ENDIF}
procedure TNewStaticText.SetTransparent(const Value: Boolean);
begin
{$IFDEF TRANSPARENCYSUPPORT}
if Transparent <> Value then
begin
if Value then
ControlStyle := ControlStyle - [csOpaque]
else
ControlStyle := ControlStyle + [csOpaque];
Invalidate;
end;
{$ELSE}
FTransparent := Value;
{$ENDIF}
end;
function TNewStaticText.GetTransparent: Boolean;
begin
{$IFDEF TRANSPARENCYSUPPORT}
Result := not (csOpaque in ControlStyle);
{$ELSE}
Result := FTransparent;
{$ENDIF}
end;
procedure TNewStaticText.SetWordWrap(Value: Boolean);
begin
if FWordWrap <> Value then
begin
FWordWrap := Value;
RecreateWnd;
AdjustBounds;
end;
end;
{$IFDEF VCLSTYLES}
{ TNewStaticTextStyleHook - same as Vcl.StdCtrls' TStaticTextStyleHook
except that it accesses the Control property as a TNewStaticText instead
of a TCustomStaticText or TStaticText, and that it uses the control's
Color property }
constructor TNewStaticTextStyleHook.Create(AControl: TWinControl);
begin
inherited;
OverridePaint := True;
OverrideEraseBkgnd := True;
PaintOnEraseBkgnd := True;
DoubleBuffered := True;
end;
procedure TNewStaticTextStyleHook.Paint(Canvas: TCanvas);
const
States: array[Boolean] of TThemedTextLabel = (ttlTextLabelDisabled, ttlTextLabelNormal);
var
Details: TThemedElementDetails;
R: TRect;
S: String;
LStyle: TCustomStyleServices;
begin
LStyle := StyleServices;
if LStyle.Available then begin
R := Control.ClientRect;
{$IFDEF TRANSPARENCYSUPPORT}
if TNewStaticText(Control).Transparent then begin
Details := LStyle.GetElementDetails(tbCheckBoxUncheckedNormal);
LStyle.DrawParentBackground(Handle, Canvas.Handle, Details, False);
Canvas.Brush.Style := bsClear;
end else {$ENDIF} begin
Canvas.Brush.Color := TNewStaticText(Control).Color;
Canvas.FillRect(R);
end;
Details := LStyle.GetElementDetails(States[Control.Enabled]);
S := TNewStaticText(Control).Caption;
if (S = '') or (TNewStaticText(Control).FShowAccelChar and (S[1] = '&') and (Length(S) = 1)) then
S := S + ' ';
if seFont in Control.StyleElements then
DrawControlText(Canvas, Details, S, R, TNewStaticText(Control).GetDrawTextFlags)
else begin
Canvas.Font := TNewStaticText(Control).Font;
DrawText(Canvas.Handle, S, Length(S), R, TNewStaticText(Control).GetDrawTextFlags);
end;
end;
end;
{$ENDIF}
end.