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
|
{
fpGUI - Free Pascal GUI Library
Copyright (C) 2006 - 2008 See the file AUTHORS.txt, included in this
distribution, for details of the copyright.
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description:
Defines a basic Label control. Also known as a Caption component.
}
unit gui_label;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
gfxbase,
fpgfx,
gfx_UTF8utils,
gfx_widget;
type
TfpgCustomLabel = class(TfpgWidget)
private
FAutoSize: boolean;
FAlignment: TAlignment;
FLayout: TLayout;
FWrapText: boolean;
FWrappedText: TStringList;
FLineSpace: integer;
procedure Wrap(MaxLength: integer; AText: string);
procedure SetWrapText(const AValue: boolean);
procedure SetAlignment(const AValue: TAlignment);
procedure SetLayout(const AValue: TLayout);
function GetFontDesc: string;
procedure SetAutoSize(const AValue: boolean);
procedure SetFontDesc(const AValue: string);
procedure SetText(const AValue: string);
procedure ResizeLabel;
function GetTextHeight: integer;
protected
FText: string;
FFont: TfpgFont;
procedure HandleResize(awidth, aheight: TfpgCoord); override;
procedure HandlePaint; override;
property WrapText: boolean read FWrapText write SetWrapText default False;
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property AutoSize: boolean read FAutoSize write SetAutoSize default False;
property Layout: TLayout read FLayout write SetLayout default tlTop;
property FontDesc: string read GetFontDesc write SetFontDesc;
property Text: string read FText write SetText;
property LineSpace: integer read FLineSpace write FLineSpace default 2;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Font: TfpgFont read FFont;
property TextHeight: integer read GetTextHeight;
end;
TfpgLabel = class(TfpgCustomLabel)
published
property Alignment;
property AutoSize;
property BackgroundColor;
property FontDesc;
property Layout;
property LineSpace;
property Text;
property TextColor;
property Width;
property WrapText;
property OnClick;
property OnDoubleClick;
property OnMouseDown;
property OnMouseEnter;
property OnMouseExit;
property OnMouseMove;
property OnMouseUp;
end;
// A convenience function to create a TfpgLabel instance
function CreateLabel(AOwner: TComponent; x, y: TfpgCoord; AText: string; w: TfpgCoord= 0; h: TfpgCoord= 0;
HAlign: TAlignment= taLeftJustify; VAlign: TLayout= tlTop; ALineSpace: integer= 2): TfpgLabel; overload;
implementation
function CreateLabel(AOwner: TComponent; x, y: TfpgCoord; AText: string; w: TfpgCoord; h: TfpgCoord;
HAlign: TAlignment; VAlign: TLayout; ALineSpace: integer): TfpgLabel; overload;
begin
Result := TfpgLabel.Create(AOwner);
Result.Left := x;
Result.Top := y;
Result.Text := AText;
Result.LineSpace := ALineSpace;
if w = 0 then
begin
Result.Width := Result.Font.TextWidth(Result.Text) + 5; // 5 is some extra spacing
Result.FAutoSize := True;
end
else
begin
Result.Width := w;
Result.WrapText := True;
end;
if h < Result.Font.Height then
Result.Height:= Result.Font.Height
else
Result.Height:= h;
Result.Alignment:= HAlign;
Result.Layout:= VAlign;
end;
{ TfpgCustomLabel }
procedure TfpgCustomLabel.Wrap(MaxLength: integer; AText: string);
var
l, i: integer;
procedure DoWrap(separ: Char);
begin
if Font.TextWidth(UTF8Copy(AText, 1, UTF8Pos(separ, AText))) < MaxLength then
begin
if FWrappedText.Count > 0 then
begin
if UTF8Pos(#13,FWrappedText[Pred(FWrappedText.Count)]) > 0 then
FWrappedText.Add(UTF8Copy(AText, 1, Pred(UTF8Pos(separ, AText))))
else
if (Font.TextWidth(FWrappedText[Pred(FWrappedText.Count)] + ' ' +
UTF8Copy(AText, 1, UTF8Pos(separ, AText)))) < MaxLength then
FWrappedText[Pred(FWrappedText.Count)] := FWrappedText[Pred(FWrappedText.Count)] + ' '
+ UTF8Copy(AText, 1, Pred(UTF8Pos(separ, AText)))
else
FWrappedText.Add(UTF8Copy(AText, 1, Pred(UTF8Pos(separ, AText))))
end
else
FWrappedText.Add(UTF8Copy(AText, 1, Pred(UTF8Pos(separ, AText))));
AText := UTF8Copy(AText, Succ(UTF8Pos(separ, AText)), UTF8Length(AText) - Pred(UTF8Pos(separ, AText)));
end
else
begin
FWrappedText.Add(UTF8Copy(AText, 1, Pred(UTF8Pos(separ, AText))));
if l < Font.TextWidth(UTF8Copy(AText, 1, UTF8Pos(separ, AText))) then
l := Font.TextWidth(UTF8Copy(AText, 1, UTF8Pos(separ, AText)));
AText := UTF8Copy(AText, Succ(UTF8Pos(separ, AText)), UTF8Length(AText) - Pred(UTF8Pos(separ, AText)));
end;
if separ = #13 then
FWrappedText[Pred(FWrappedText.Count)] := FWrappedText[Pred(FWrappedText.Count)] + separ;
end;
begin
FWrappedText.Clear;
l := 0;
repeat
if UTF8Pos(' ', AText) > 0 then
begin
if (UTF8Pos(#13,AText) > 0) then
if (UTF8Pos(#13,AText) < UTF8Pos(' ',AText)) then
DoWrap(#13)
else
DoWrap(' ')
else
DoWrap(' ')
end
else
if (UTF8Pos(#13,AText) > 0) then
DoWrap(#13)
else
begin
FWrappedText.Add(AText);
AText := '';
end;
until UTF8Pos(' ', AText) = 0;
if FWrappedText.Count > 0 then
if UTF8Pos(#13,FWrappedText[Pred(FWrappedText.Count)]) > 0 then
FWrappedText.Add(AText)
else
if (Font.TextWidth(FWrappedText[Pred(FWrappedText.Count)] + ' ' + AText)) < MaxLength then
FWrappedText[Pred(FWrappedText.Count)] := FWrappedText[Pred(FWrappedText.Count)] + ' ' + AText
else
FWrappedText.Add(AText);
for i := 0 to Pred(FWrappedText.Count) do
if UTF8Pos(#13,FWrappedText[i]) > 0 then
FWrappedText[i] := UTF8Copy(FWrappedText[i], 1 , UTF8Length(FWrappedText[i]) - 1);
if Height < FWrappedText.Count * (Font.Height + LineSpace) then
Height := FWrappedText.Count * (Font.Height + LineSpace);
if AutoSize then
if Width < l then // adjust the length to the longest word
Width := l;
end;
procedure TfpgCustomLabel.SetWrapText(const AValue: boolean);
begin
if FWrapText <> AValue then
begin
FWrapText := AValue;
if FWrapText then
Wrap(Width, FText);
RePaint;
end;
end;
procedure TfpgCustomLabel.SetAlignment(const AValue: TAlignment);
begin
if FAlignment = AValue then
Exit;
FAlignment := AValue;
if FAlignment <> taLeftJustify then
FAutoSize := False;
ResizeLabel;
end;
procedure TfpgCustomLabel.SetLayout(const AValue: TLayout);
begin
if FLayout = AValue then
Exit;
FLayout := AValue;
ResizeLabel;
end;
function TfpgCustomLabel.GetFontDesc: string;
begin
Result := FFont.FontDesc;
end;
procedure TfpgCustomLabel.SetAutoSize(const AValue: boolean);
begin
if FAutoSize = AValue then
Exit; //==>
FAutoSize := AValue;
ResizeLabel;
end;
procedure TfpgCustomLabel.SetFontDesc(const AValue: string);
begin
FFont.Free;
FFont := fpgGetFont(AValue);
ResizeLabel;
end;
procedure TfpgCustomLabel.SetText(const AValue: string);
begin
if FText = AValue then
Exit; //==>
FText := AValue;
ResizeLabel;
end;
procedure TfpgCustomLabel.ResizeLabel;
begin
if FAutoSize then
Width := FFont.TextWidth(FText)
else if FWrapText then
Wrap(Width, FText);
UpdateWindowPosition;
RePaint;
end;
function TfpgCustomLabel.GetTextHeight: integer;
begin
if FWrapText then
Result := (Font.Height * FWrappedText.Count) + (FLineSpace * Pred(FWrappedText.Count))
else
Result := Font.Height;
end;
procedure TfpgCustomLabel.HandleResize(awidth, aheight: TfpgCoord);
begin
inherited HandleResize(awidth, aheight);
if FWrapText then
Wrap(Width, FText);
end;
constructor TfpgCustomLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FText := 'Label';
FFont := fpgGetFont('#Label1');
FHeight := FFont.Height;
FWidth := 80;
FTextColor := Parent.TextColor;
FBackgroundColor := Parent.BackgroundColor;
FAutoSize := False;
FLayout := tlTop;
FAlignment := taLeftJustify;
FWrapText := False;
FWrappedText := TStringList.Create;
FLineSpace := 2;
end;
destructor TfpgCustomLabel.Destroy;
begin
FText := '';
FFont.Free;
FWrappedText.Free;
inherited Destroy;
end;
procedure TfpgCustomLabel.HandlePaint;
var
i: integer;
r: TfpgRect;
begin
Canvas.BeginDraw;
inherited HandlePaint;
Canvas.ClearClipRect;
r.SetRect(0, 0, Width, Height);
Canvas.Clear(FBackgroundColor);
Canvas.SetFont(Font);
Canvas.SetTextColor(FTextColor);
if WrapText then
begin
if FWrappedText.Count> 0 then
for i:= 0 to Pred(FWrappedText.Count) do
case FAlignment of
taLeftJustify:
case FLayout of
tlTop:
fpgStyle.DrawString(Canvas, 0, (Font.Height + LineSpace) * i, FWrappedText[i], Enabled);
tlBottom:
fpgStyle.DrawString(Canvas, 0,
Height - (Font.Height * FWrappedText.Count) - (LineSpace * Pred(FWrappedText.Count)) + ((Font.Height + LineSpace) * i),
FWrappedText[i], Enabled);
tlCenter:
fpgStyle.DrawString(Canvas, 0,
((Height - (Font.Height * FWrappedText.Count) - (LineSpace * Pred(FWrappedText.Count))) div 2) + ((Font.Height + LineSpace) * i),
FWrappedText[i], Enabled);
end;
taRightJustify:
case FLayout of
tlTop:
fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FWrappedText[i]),
(Font.Height + LineSpace) * i, FWrappedText[i], Enabled);
tlBottom:
fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FWrappedText[i]),
Height - (Font.Height * FWrappedText.Count) - (LineSpace * Pred(FWrappedText.Count)) + ((Font.Height + LineSpace) * i),
FWrappedText[i], Enabled);
tlCenter:
fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FWrappedText[i]),
((Height - (Font.Height * FWrappedText.Count) - (LineSpace * Pred(FWrappedText.Count))) div 2) + ((Font.Height + LineSpace) * i),
FWrappedText[i], Enabled);
end;
taCenter:
case FLayout of
tlTop:
fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FWrappedText[i])) div 2,
(Font.Height + LineSpace) * i, FWrappedText[i], Enabled);
tlBottom:
fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FWrappedText[i])) div 2,
Height - (Font.Height * FWrappedText.Count) - (LineSpace * Pred(FWrappedText.Count)) + ((Font.Height + LineSpace) * i),
FWrappedText[i], Enabled);
tlCenter:
begin
fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FWrappedText[i])) div 2,
((Height - (Font.Height * FWrappedText.Count) - (LineSpace * Pred(FWrappedText.Count))) div 2) + ((Font.Height + LineSpace) * i),
FWrappedText[i], Enabled);
end;
end;
end;
end
else
case FAlignment of
taLeftJustify:
case FLayout of
tlTop:
fpgStyle.DrawString(Canvas, 0, 0, FText, Enabled);
tlBottom:
fpgStyle.DrawString(Canvas, 0, Height - Font.Height, FText, Enabled);
tlCenter:
fpgStyle.DrawString(Canvas, 0, (Height - Font.Height) div 2, FText, Enabled);
end;
taRightJustify:
case FLayout of
tlTop:
fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FText), 0, FText, Enabled);
tlBottom:
fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FText), Height - Font.Height, FText, Enabled);
tlCenter:
fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FText), (Height - Font.Height) div 2, FText, Enabled);
end;
taCenter:
case FLayout of
tlTop:
fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FText)) div 2, 0, FText, Enabled);
tlBottom:
fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FText)) div 2, Height - Font.Height, FText, Enabled);
tlCenter:
fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FText)) div 2, (Height - Font.Height) div 2, FText, Enabled);
end;
end;
Canvas.EndDraw;
end;
end.
|