-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGroupablePanelUnit.pas
More file actions
427 lines (379 loc) · 10.3 KB
/
GroupablePanelUnit.pas
File metadata and controls
427 lines (379 loc) · 10.3 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
unit GroupablePanelUnit;
interface
uses
System.Math, System.UITypes, Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls, Winapi.GDIPOBJ, Winapi.GDIPAPI;
type
TGroupablePanel = class(TPanel)
private
FNormalColor: TColor;
FHoverColor: TColor;
FHoverSelectedColor: TColor;
FDownColor: TColor;
FBorderColor: TColor;
FBorderWidth: Integer;
FBorderRadius: Integer;
FDown: Boolean;
FGroupID: string;
FIsHovered: Boolean;
FAllowDeselect: Boolean;
FAllowMultiple: Boolean;
FAntiAliased: Boolean;
FControlSafety: Boolean;
procedure SetNormalColor(const Value: TColor);
procedure SetHoverColor(const Value: TColor);
procedure SetHoverSelectedColor(const Value: TColor);
procedure SetDownColor(const Value: TColor);
procedure SetBorderColor(const Value: TColor);
procedure SetBorderWidth(const Value: Integer);
procedure SetBorderRadius(const Value: Integer);
procedure SetDown(const Value: Boolean);
procedure SetGroupID(const Value: string);
procedure SetAllowDeselect(const Value: Boolean);
procedure SetAllowMultiple(const Value: Boolean);
procedure SetAntiAliased(const Value: Boolean);
procedure SetControlSafety(const Value: Boolean);
procedure UpdateControl;
procedure UpdateRegion;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
function ColorToGPColor(const Value: TColor): TGPColor;
procedure TurnSiblingsOff;
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure Paint; override;
procedure Click; override;
procedure Resize; override;
procedure CreateHandle; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Down: Boolean read FDown write SetDown default False;
property GroupID: string read FGroupID write SetGroupID;
property AllowDeselect: Boolean read FAllowDeselect write SetAllowDeselect default False;
property AllowMultiple: Boolean read FAllowMultiple write SetAllowMultiple default False;
property AntiAliased: Boolean read FAntiAliased write SetAntiAliased default False;
property ControlSafety: Boolean read FControlSafety write SetControlSafety default False;
property BorderColor: TColor read FBorderColor write SetBorderColor default clBlack;
property BorderWidth: Integer read FBorderWidth write SetBorderWidth default 1;
property Color: TColor read FNormalColor write SetNormalColor default clBtnFace;
property HoverColor: TColor read FHoverColor write SetHoverColor default clHighlight;
property HoverSelectedColor: TColor read FHoverSelectedColor write SetHoverSelectedColor default clHighlight;
property DownColor: TColor read FDownColor write SetDownColor default clBtnShadow;
property BorderRadius: Integer read FBorderRadius write SetBorderRadius default 12;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('FluidVCL', [TGroupablePanel]);
end;
{ TGroupablePanel }
constructor TGroupablePanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FNormalColor := clBtnFace;
FHoverColor := clHighlight;
FHoverSelectedColor := clHighlight;
FDownColor := clBtnShadow;
FBorderColor := clGray;
FBorderWidth := 1;
FBorderRadius := 12;
FDown := False;
FGroupID := '';
FIsHovered := False;
FAllowDeselect := False;
FAllowMultiple := False;
FAntiAliased := True;
FControlSafety := False;
BevelOuter := bvNone;
BevelInner := bvNone;
FullRepaint := True;
ParentColor := True;
ControlStyle := ControlStyle + [csClickEvents, csOpaque];
end;
destructor TGroupablePanel.Destroy;
begin
inherited Destroy;
end;
function TGroupablePanel.ColorToGPColor(const Value: TColor): TGPColor;
var
RGBValue: LongInt;
begin
RGBValue := ColorToRGB(Value);
Result := MakeColor(255, GetRValue(RGBValue), GetGValue(RGBValue), GetBValue(RGBValue));
end;
procedure TGroupablePanel.CreateHandle;
begin
inherited;
UpdateRegion;
end;
procedure TGroupablePanel.Resize;
begin
inherited;
UpdateRegion;
end;
procedure TGroupablePanel.UpdateRegion;
var
Rgn: HRGN;
begin
if not HandleAllocated then Exit;
Rgn := CreateRoundRectRgn(0, 0, Width + 1, Height + 1, FBorderRadius * 2, FBorderRadius * 2);
if Rgn <> 0 then
begin
if SetWindowRgn(Handle, Rgn, True) = 0 then
DeleteObject(Rgn);
end;
end;
procedure TGroupablePanel.TurnSiblingsOff;
var
I: Integer;
Sibling: TControl;
Target: TGroupablePanel;
begin
if (Parent <> nil) then
begin
for I := 0 to Parent.ControlCount - 1 do
begin
Sibling := Parent.Controls[I];
if (Sibling is TGroupablePanel) and (Sibling <> Self) then
begin
Target := TGroupablePanel(Sibling);
if SameText(Target.GroupID, FGroupID) and (FGroupID <> '') then
begin
if Target.Down then
begin
Target.FDown := False;
Target.UpdateControl;
end;
end;
end;
end;
end;
end;
procedure TGroupablePanel.Click;
var
CtrlPressed: Boolean;
begin
CtrlPressed := (GetKeyState(VK_CONTROL) and $8000) <> 0;
if FControlSafety then
begin
if CtrlPressed then
SetDown(not FDown)
else
begin
TurnSiblingsOff;
SetDown(True);
end;
end
else
begin
if FDown then
begin
if FAllowDeselect or FAllowMultiple then
SetDown(False);
end
else
begin
if not FAllowMultiple then
TurnSiblingsOff;
SetDown(True);
end;
end;
inherited Click;
end;
procedure TGroupablePanel.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if (Button = mbLeft) then
UpdateControl;
end;
procedure TGroupablePanel.CMMouseEnter(var Message: TMessage);
begin
inherited;
FIsHovered := True;
UpdateControl;
end;
procedure TGroupablePanel.CMMouseLeave(var Message: TMessage);
begin
inherited;
FIsHovered := False;
UpdateControl;
end;
procedure TGroupablePanel.SetBorderColor(const Value: TColor);
begin
if FBorderColor <> Value then
begin
FBorderColor := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetBorderWidth(const Value: Integer);
begin
if FBorderWidth <> Value then
begin
FBorderWidth := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetNormalColor(const Value: TColor);
begin
if FNormalColor <> Value then
begin
FNormalColor := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetHoverColor(const Value: TColor);
begin
if FHoverColor <> Value then
begin
FHoverColor := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetHoverSelectedColor(const Value: TColor);
begin
if FHoverSelectedColor <> Value then
begin
FHoverSelectedColor := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetDownColor(const Value: TColor);
begin
if FDownColor <> Value then
begin
FDownColor := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetBorderRadius(const Value: Integer);
begin
if FBorderRadius <> Value then
begin
FBorderRadius := Value;
UpdateRegion;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetDown(const Value: Boolean);
begin
if FDown <> Value then
begin
if Value and (not FAllowMultiple) then
TurnSiblingsOff;
FDown := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetGroupID(const Value: string);
begin
if FGroupID <> Value then
begin
FGroupID := Value;
if FDown and (not FAllowMultiple) then TurnSiblingsOff;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetAllowDeselect(const Value: Boolean);
begin
FAllowDeselect := Value;
end;
procedure TGroupablePanel.SetAllowMultiple(const Value: Boolean);
begin
if FAllowMultiple <> Value then
begin
FAllowMultiple := Value;
if (not FAllowMultiple) and FDown then
TurnSiblingsOff;
end;
end;
procedure TGroupablePanel.SetAntiAliased(const Value: Boolean);
begin
if FAntiAliased <> Value then
begin
FAntiAliased := Value;
UpdateControl;
end;
end;
procedure TGroupablePanel.SetControlSafety(const Value: Boolean);
begin
FControlSafety := Value;
end;
procedure TGroupablePanel.UpdateControl;
begin
Invalidate;
end;
procedure TGroupablePanel.Paint;
var
Graphics: TGPGraphics;
Path: TGPGraphicsPath;
Pen: TGPPen;
Brush: TGPSolidBrush;
R: TGPRectF;
ControlColor: TColor;
Radius: Single;
Offset: Single;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
try
if FAntiAliased then
Graphics.SetSmoothingMode(SmoothingModeAntiAlias)
else
Graphics.SetSmoothingMode(SmoothingModeNone);
if FIsHovered and FDown then
ControlColor := FHoverSelectedColor
else if FIsHovered then
ControlColor := FHoverColor
else if FDown then
ControlColor := FDownColor
else
ControlColor := FNormalColor;
Offset := (FBorderWidth / 2) + 0.5;
R.X := Offset;
R.Y := Offset;
R.Width := ClientWidth - (Offset * 2) - 1;
R.Height := ClientHeight - (Offset * 2) - 1;
Radius := FBorderRadius;
if Radius > R.Width / 2 then Radius := R.Width / 2;
if Radius > R.Height / 2 then Radius := R.Height / 2;
Path := TGPGraphicsPath.Create;
try
if Radius > 0 then
begin
Path.AddArc(R.X, R.Y, Radius * 2, Radius * 2, 180, 90);
Path.AddArc(R.X + R.Width - (Radius * 2), R.Y, Radius * 2, Radius * 2, 270, 90);
Path.AddArc(R.X + R.Width - (Radius * 2), R.Y + R.Height - (Radius * 2), Radius * 2, Radius * 2, 0, 90);
Path.AddArc(R.X, R.Y + R.Height - (Radius * 2), Radius * 2, Radius * 2, 90, 90);
Path.CloseFigure;
end
else
Path.AddRectangle(R);
Brush := TGPSolidBrush.Create(ColorToGPColor(ControlColor));
try
Graphics.FillPath(Brush, Path);
finally
Brush.Free;
end;
inherited Paint;
if FBorderWidth > 0 then
begin
Pen := TGPPen.Create(ColorToGPColor(FBorderColor), FBorderWidth);
try
Pen.SetLineJoin(LineJoinRound);
Graphics.DrawPath(Pen, Path);
finally
Pen.Free;
end;
end;
finally
Path.Free;
end;
finally
Graphics.Free;
end;
end;
end.