-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfxbutton.pas
574 lines (515 loc) Β· 14.3 KB
/
fxbutton.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
unit FXButton;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Types,
BGRABitmap, BGRABitmapTypes, BGRAOpenGL, FXMaterialColors,
FXGraphicControl, Themes, LCLType;
type
TFXButtonState = (fxbHovered, fxbActive);
TFXButtonStates = set of TFXButtonState;
{ TCustomFXButton }
{ TFXBaseButton }
TFXBaseButton = class(TFXGraphicControl)
private
FState: TFXButtonStates;
FNeedDraw: boolean;
protected
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
class function GetControlClassDefaultSize: TSize; override;
procedure FontChanged(Sender: TObject); override;
procedure SetEnabled(Value: boolean); override;
procedure TextChanged; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
procedure MouseEnter; override;
procedure MouseLeave; override;
end;
TCustomFXButton = class(TFXBaseButton)
private
FColorActive: TColor;
FColorDisabled: TColor;
FColorHover: TColor;
FColorKind: TMaterialColor;
FColorNormal: TColor;
FFontColorAutomatic: boolean;
procedure SetFColorActive(AValue: TColor);
procedure SetFColorDisabled(AValue: TColor);
procedure SetFColorHover(AValue: TColor);
procedure SetFColorKind(AValue: TMaterialColor);
procedure SetFColorNormal(AValue: TColor);
procedure SetFFontColorAutomatic(AValue: boolean);
protected
procedure Draw; override;
function GetFillColor: TColor;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
property FontColorAutomatic: boolean read FFontColorAutomatic
write SetFFontColorAutomatic;
property ColorKind: TMaterialColor read FColorKind write SetFColorKind;
property ColorNormal: TColor read FColorNormal write SetFColorNormal;
property ColorHover: TColor read FColorHover write SetFColorHover;
property ColorActive: TColor read FColorActive write SetFColorActive;
property ColorDisabled: TColor read FColorDisabled write SetFColorDisabled;
end;
TFXButton = class(TCustomFXButton)
published
property Action;
property Align;
property Anchors;
property AutoSize;
property BidiMode;
property BorderSpacing;
property Caption;
property ColorKind;
property ColorNormal;
property ColorHover;
property ColorActive;
property ColorDisabled;
property FontColorAutomatic;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ParentBidiMode;
property OnChangeBounds;
property OnClick;
property OnContextPopup;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDrag;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
end;
{ TFXCustomNativeButton }
TFXCustomNativeButton = class(TCustomFXButton)
protected
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
procedure Draw; override;
end;
TFXNativeButton = class(TFXCustomNativeButton)
property Action;
property Align;
property Anchors;
property AutoSize;
property BidiMode;
property BorderSpacing;
property Caption;
property ColorKind;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ParentBidiMode;
property OnChangeBounds;
property OnClick;
property OnContextPopup;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDrag;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('BGRA Controls FX', [TFXButton]);
RegisterComponents('BGRA Controls FX', [TFXNativeButton]);
end;
function SaveAlphaRect(ABitmap: TBGRABitmap; ARect: TRect): Pointer;
var
Width, Height, Count, y: integer;
pAlphaData: PByte;
pSrc: PBGRAPixel;
begin
IntersectRect(ARect, ARect, Classes.Rect(0, 0, ABitmap.Width, ABitmap.Height));
Width := ARect.Right - ARect.Left;
Height := ARect.Bottom - ARect.Top;
if (Width <= 0) or (Height <= 0) then
Result := nil;
getmem(Result, sizeof(longint) * 2 + sizeof(byte) * Width * Height);
PLongint(Result)^ := Width;
(PLongint(Result) +1)^ := Height;
pAlphaData := pbyte(plongint(Result) + 2);
for y := ARect.Top to ARect.Bottom - 1 do
begin
pSrc := ABitmap.ScanLine[y] + ARect.Left;
Count := Width;
while Count > 0 do
begin
pAlphaData^ := pSrc^.alpha;
Inc(pAlphaData);
Inc(pSrc);
Dec(Count);
end;
end;
end;
procedure RestoreAlphaRectAndFree(ABitmap: TBGRABitmap; AX, AY: integer;
ASavedAlphaRect: Pointer);
var
Width, Height, Count, y: integer;
pAlphaData: PByte;
pSrc: PBGRAPixel;
begin
if ASavedAlphaRect = nil then
exit;
if AX < 0 then
AX := 0;
if AY < 0 then
AY := 0;
Width := PLongint(ASavedAlphaRect)^;
Height := (PLongint(ASavedAlphaRect) + 1)^;
pAlphaData := pbyte(plongint(ASavedAlphaRect) + 2);
for y := AY to AY + Height - 1 do
begin
pSrc := ABitmap.ScanLine[y] + AX;
Count := Width;
while Count > 0 do
begin
pSrc^.alpha := pAlphaData^;
Inc(pAlphaData);
Inc(pSrc);
Dec(Count);
end;
end;
freemem(ASavedAlphaRect);
end;
{ TFXBaseButton }
procedure TFXBaseButton.CalculatePreferredSize(
var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
var
ts: TSize;
begin
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight,
WithThemeSpace);
if Caption <> '' then
begin
FXLayers[0].BGRA.FontStyle := Font.Style;
FXLayers[0].BGRA.FontQuality := fqFineAntialiasing;
FXLayers[0].BGRA.FontName := Font.Name;
if Font.Height = 0 then
FXLayers[0].BGRA.FontHeight := Font.GetTextHeight(Caption)
else
FXLayers[0].BGRA.FontHeight := Font.Height;
FXLayers[0].BGRA.FontAntialias := True;
ts := FXLayers[0].BGRA.TextSize(Caption);
Inc(PreferredWidth, ts.cx + 26);
Inc(PreferredHeight, ts.cy + 10);
end;
end;
class function TFXBaseButton.GetControlClassDefaultSize: TSize;
begin
Result := inherited GetControlClassDefaultSize;
end;
procedure TFXBaseButton.FontChanged(Sender: TObject);
begin
inherited FontChanged(Sender);
FNeedDraw := True;
Invalidate;
end;
procedure TFXBaseButton.SetEnabled(Value: boolean);
begin
inherited SetEnabled(Value);
FNeedDraw := True;
Invalidate;
end;
procedure TFXBaseButton.TextChanged;
begin
InvalidatePreferredSize;
if Assigned(Parent) and Parent.AutoSize then
Parent.AdjustSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
inherited TextChanged;
end;
procedure TFXBaseButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer);
begin
if Button = mbLeft then
begin
FState := FState + [fxbActive];
FNeedDraw := True;
Invalidate;
end;
inherited MouseDown(Button, Shift, X, Y);
end;
procedure TFXBaseButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: integer);
begin
if Button = mbLeft then
begin
FState := FState - [fxbActive];
FNeedDraw := True;
Invalidate;
end;
inherited MouseUp(Button, Shift, X, Y);
end;
procedure TFXBaseButton.MouseEnter;
begin
FState := FState + [fxbHovered];
FNeedDraw := True;
Invalidate;
inherited MouseEnter;
end;
procedure TFXBaseButton.MouseLeave;
begin
FState := FState - [fxbHovered];
FNeedDraw := True;
Invalidate;
inherited MouseLeave;
end;
{ TFXCustomNativeButton }
procedure TFXCustomNativeButton.CalculatePreferredSize(
var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
var
ts: TSize;
begin
PreferredWidth := 0;
PreferredHeight := 0;
if Caption <> '' then
begin
FXLayers[0].BGRA.Canvas.Font := Font;
ts.cx := FXLayers[0].BGRA.Canvas.Font.GetTextWidth(Caption);
ts.cy := FXLayers[0].BGRA.Canvas.Font.GetTextHeight(Caption);
Inc(PreferredWidth, ts.cx + 26);
Inc(PreferredHeight, ts.cy + 10);
end;
end;
procedure TFXCustomNativeButton.Draw;
var
Details: TThemedElementDetails;
PaintRect: TRect;
AlphaRect: Pointer;
begin
if (FXLayers[0].BGRA.Width <> ClientWidth) or
(FXLayers[0].BGRA.Height <> ClientHeight) then
begin
FNeedDraw := True;
FXLayers[0].BGRA.SetSize(ClientWidth, ClientHeight);
end;
if FNeedDraw then
begin
{$IFDEF LINUX}
FXLayers[0].BGRA.Fill(ColorToBGRA(ColorToRGB(Parent.Color)));
{$ELSE}
FXLayers[0].BGRA.FillTransparent;
{$ENDIF}
PaintRect := Rect(0, 0, FXLayers[0].BGRA.Width, FXLayers[0].BGRA.Height);
if Enabled then
begin
{ Button Down }
if fxbActive in FState then
Details := ThemeServices.GetElementDetails(tbPushButtonPressed)
else
begin
{ Button Hovered }
if fxbHovered in FState then
Details := ThemeServices.GetElementDetails(tbPushButtonHot)
{ Button Normal }
else
Details := ThemeServices.GetElementDetails(tbPushButtonNormal);
end;
end
{ Button Disabled }
else
Details := ThemeServices.GetElementDetails(tbPushButtonDisabled);
FXLayers[0].BGRA.Canvas.Font := Font;
ThemeServices.DrawElement(FXLayers[0].BGRA.Canvas.Handle, Details, PaintRect, nil);
PaintRect := ThemeServices.ContentRect(FXLayers[0].BGRA.Canvas.Handle,
Details, PaintRect);
AlphaRect := SaveAlphaRect(FXLayers[0].BGRA, PaintRect);
ThemeServices.DrawText(FXLayers[0].BGRA.Canvas, Details, Caption, PaintRect,
DT_CENTER or DT_VCENTER or DT_SINGLELINE, 0);
RestoreAlphaRectAndFree(FXLayers[0].BGRA, PaintRect.Left,
PaintRect.Top, AlphaRect);
if ColorKind = mcDefault then
FXLayers[0].Color := BGRAWhite
else
FXLayers[0].Color := GetFillColor;
FNeedDraw := False;
FXLayers[0].Texture := nil;
end;
end;
{ TCustomFXButton }
procedure TCustomFXButton.SetFColorActive(AValue: TColor);
begin
if FColorActive = AValue then
Exit;
FColorActive := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TCustomFXButton.SetFColorDisabled(AValue: TColor);
begin
if FColorDisabled = AValue then
Exit;
FColorDisabled := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TCustomFXButton.SetFColorHover(AValue: TColor);
begin
if FColorHover = AValue then
Exit;
FColorHover := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TCustomFXButton.SetFColorKind(AValue: TMaterialColor);
begin
if FColorKind = AValue then
Exit;
FColorKind := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TCustomFXButton.SetFColorNormal(AValue: TColor);
begin
if FColorNormal = AValue then
Exit;
FColorNormal := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TCustomFXButton.SetFFontColorAutomatic(AValue: boolean);
begin
if FFontColorAutomatic = AValue then
Exit;
FFontColorAutomatic := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TCustomFXButton.Draw;
var
style: TTextStyle;
fill_color: TColor;
begin
if (FXLayers[0].BGRA.Width <> ClientWidth) or
(FXLayers[0].BGRA.Height <> ClientHeight) then
begin
FNeedDraw := True;
FXLayers[0].BGRA.SetSize(ClientWidth, ClientHeight);
end;
if FNeedDraw then
begin
FXLayers[0].BGRA.FillTransparent;
style.Alignment := taCenter;
style.Layout := tlCenter;
FXLayers[0].BGRA.FontStyle := Font.Style;
FXLayers[0].BGRA.FontQuality := fqFineAntialiasing;
FXLayers[0].BGRA.FontName := Font.Name;
if Font.Height = 0 then
FXLayers[0].BGRA.FontHeight := Font.GetTextHeight(Caption)
else
FXLayers[0].BGRA.FontHeight := Font.Height;
FXLayers[0].BGRA.FontAntialias := True;
fill_color := GetFillColor;
FXLayers[0].BGRA.Fill(fill_color);
if FontColorAutomatic then
FXLayers[0].BGRA.TextRect(Rect(0, 0, FXLayers[0].BGRA.Width,
FXLayers[0].BGRA.Height), 0, 0, Caption, style,
GetContrastColor(fill_color))
else
FXLayers[0].BGRA.TextRect(Rect(0, 0, FXLayers[0].BGRA.Width,
FXLayers[0].BGRA.Height), 0, 0, Caption, style, Font.Color);
FNeedDraw := False;
FXLayers[0].Texture := nil;
end;
end;
function TCustomFXButton.GetFillColor: TColor;
begin
if ColorKind = mcDefault then
begin
if Enabled then
begin
{ Button Down }
if fxbActive in FState then
Result := ColorActive
else
begin
{ Button Hovered }
if fxbHovered in FState then
Result := ColorHover
{ Button Normal }
else
Result := ColorNormal;
end;
end
{ Button Disabled }
else
Result := ColorDisabled;
end
else
begin
if Enabled then
begin
{ Button Down }
if fxbActive in FState then
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M100
else
begin
{ Button Hovered }
if fxbHovered in FState then
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M300
{ Button Normal }
else
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M500;
end;
end
{ Button Disabled }
else
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M900;
end;
end;
constructor TCustomFXButton.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
FColorNormal := clWhite;
FColorHover := clSilver;
FColorActive := clMedGray;
FColorDisabled := clGray;
FFontColorAutomatic := True;
end;
destructor TCustomFXButton.Destroy;
begin
inherited Destroy;
end;
end.