-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFlatCheckBoxUnit.pas
566 lines (522 loc) · 15.1 KB
/
TFlatCheckBoxUnit.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
unit TFlatCheckBoxUnit;
interface
{$I DFS.inc}
uses
Windows, Messages, Classes, Graphics, Controls, Forms, ExtCtrls, FlatUtilitys;
type
TFlatCheckBox = class(TCustomControl)
private
FUseAdvColors: Boolean;
FAdvColorFocused: TAdvColors;
FAdvColorDown: TAdvColors;
FAdvColorBorder: TAdvColors;
FMouseInControl: Boolean;
MouseIsDown: Boolean;
Focused: Boolean;
FLayout: TCheckBoxLayout;
FChecked: Boolean;
FFocusedColor: TColor;
FDownColor: TColor;
FCheckColor: TColor;
FBorderColor: TColor;
FTransparent: Boolean;
procedure SetColors (Index: Integer; Value: TColor);
procedure SetAdvColors (Index: Integer; Value: TAdvColors);
procedure SetUseAdvColors (Value: Boolean);
procedure SetLayout (Value: TCheckBoxLayout);
procedure SetChecked (Value: Boolean);
procedure SetTransparent(const Value: Boolean);
procedure CMEnabledChanged (var Message: TMessage); message CM_ENABLEDCHANGED;
procedure CMTextChanged (var Message: TWmNoParams); message CM_TEXTCHANGED;
procedure CMDialogChar (var Message: TCMDialogChar); message CM_DIALOGCHAR;
procedure CNCommand (var Message: TWMCommand); message CN_COMMAND;
procedure WMSetFocus (var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus (var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure CMSysColorChange (var Message: TMessage); message CM_SYSCOLORCHANGE;
procedure CMParentColorChanged (var Message: TWMNoParams); message CM_PARENTCOLORCHANGED;
procedure RemoveMouseTimer;
procedure MouseTimerHandler (Sender: TObject);
procedure CMDesignHitTest (var Message: TCMDesignHitTest); message CM_DESIGNHITTEST;
procedure WMSize (var Message: TWMSize); message WM_SIZE;
procedure WMMove (var Message: TWMMove); message WM_MOVE;
protected
procedure CalcAdvColors;
procedure DoEnter; override;
procedure DoExit; override;
procedure MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove (Shift: TShiftState; X, Y: Integer); override;
procedure CreateWnd; override;
procedure DrawCheckRect;
procedure DrawCheckText;
procedure Paint; override;
{$IFDEF DFS_COMPILER_4_UP}
procedure SetBiDiMode(Value: TBiDiMode); override;
{$ENDIF}
public
constructor Create (AOwner: TComponent); override;
destructor Destroy; override;
procedure MouseEnter;
procedure MouseLeave;
published
property Transparent: Boolean read FTransparent write SetTransparent default false;
property Caption;
property Checked: Boolean read FChecked write SetChecked default false;
property Color default $00E1EAEB;
property ColorFocused: TColor index 0 read FFocusedColor write SetColors default clWhite;
property ColorDown: TColor index 1 read FDownColor write SetColors default $00C5D6D9;
property ColorCheck: TColor index 2 read FCheckColor write SetColors default clBlack;
property ColorBorder: TColor index 3 read FBorderColor write SetColors default $008396A0;
property AdvColorFocused: TAdvColors index 0 read FAdvColorFocused write SetAdvColors default 10;
property AdvColorDown: TAdvColors index 1 read FAdvColorDown write SetAdvColors default 10;
property AdvColorBorder: TAdvColors index 2 read FAdvColorBorder write SetAdvColors default 50;
property UseAdvColors: Boolean read FUseAdvColors write SetUseAdvColors default false;
property Enabled;
property Font;
property Layout: TCheckBoxLayout read FLayout write SetLayout default checkBoxLeft;
property ParentColor;
property ParentFont;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
{$IFDEF DFS_COMPILER_4_UP}
property Action;
property Anchors;
property BiDiMode write SetBidiMode;
property Constraints;
property DragKind;
property ParentBiDiMode;
property OnEndDock;
property OnStartDock;
{$ENDIF}
end;
var
MouseInControl: TFlatCheckBox = nil;
implementation
var
MouseTimer: TTimer = nil;
ControlCounter: Integer = 0;
procedure TFlatCheckBox.CMDesignHitTest(var Message: TCMDesignHitTest);
begin
case FLayout of
checkboxLeft:
if PtInRect(Rect(ClientRect.Left + 1, ClientRect.Top + 3, ClientRect.Left + 12, ClientRect.Top + 14), Point(message.XPos, message.YPos)) then
Message.Result := 1
else
Message.Result := 0;
checkboxRight:
if PtInRect(Rect(ClientRect.Right - 12, ClientRect.Top + 3, ClientRect.Right - 1, ClientRect.Top + 14), Point(message.XPos, message.YPos)) then
Message.Result := 1
else
Message.Result := 0;
end;
end;
constructor TFlatCheckBox.Create (AOwner: TComponent);
begin
inherited Create(AOwner);
if MouseTimer = nil then
begin
MouseTimer := TTimer.Create(nil);
MouseTimer.Enabled := False;
MouseTimer.Interval := 100; // 10 times a second
end;
ParentColor := True;
ParentFont := True;
FFocusedColor := clWhite;
FDownColor := $00C5D6D9;
FCheckColor := clBlack;
FBorderColor := $008396A0;
FLayout := checkboxLeft;
TabStop := True;
FChecked := false;
Enabled := true;
Visible := true;
SetBounds(0, 0, 121, 17);
FUseAdvColors := false;
FAdvColorFocused := 10;
FAdvColorDown := 10;
FAdvColorBorder := 50;
Inc(ControlCounter);
end;
destructor TFlatCheckBox.Destroy;
begin
RemoveMouseTimer;
Dec(ControlCounter);
if ControlCounter = 0 then
begin
MouseTimer.Free;
MouseTimer := nil;
end;
inherited;
end;
procedure TFlatCheckBox.SetColors (Index: Integer; Value: TColor);
begin
case Index of
0: FFocusedColor := Value;
1: FDownColor := Value;
2: FCheckColor := Value;
3: FBorderColor := Value;
end;
Invalidate;
end;
procedure TFlatCheckBox.CalcAdvColors;
begin
if FUseAdvColors then
begin
FFocusedColor := CalcAdvancedColor(Color, FFocusedColor, FAdvColorFocused, lighten);
FDownColor := CalcAdvancedColor(Color, FDownColor, FAdvColorDown, darken);
FBorderColor := CalcAdvancedColor(Color, FBorderColor, FAdvColorBorder, darken);
end;
end;
procedure TFlatCheckBox.SetAdvColors (Index: Integer; Value: TAdvColors);
begin
case Index of
0: FAdvColorFocused := Value;
1: FAdvColorDown := Value;
2: FAdvColorBorder := Value;
end;
CalcAdvColors;
Invalidate;
end;
procedure TFlatCheckBox.SetUseAdvColors (Value: Boolean);
begin
if Value <> FUseAdvColors then
begin
FUseAdvColors := Value;
ParentColor := Value;
CalcAdvColors;
Invalidate;
end;
end;
procedure TFlatCheckBox.SetLayout (Value: TCheckBoxLayout);
begin
FLayout := Value;
Invalidate;
end;
procedure TFlatCheckBox.SetChecked (Value: Boolean);
begin
if FChecked <> Value then
begin
FChecked := Value;
Click;
DrawCheckRect;
if csDesigning in ComponentState then
if (GetParentForm(self) <> nil) and (GetParentForm(self).Designer <> nil) then
GetParentForm(self).Designer.Modified;
end;
end;
procedure TFlatCheckBox.CMEnabledChanged (var Message: TMessage);
begin
inherited;
if not Enabled then
begin
FMouseInControl := False;
MouseIsDown := False;
RemoveMouseTimer;
end;
Invalidate;
end;
procedure TFlatCheckBox.CMTextChanged (var Message: TWmNoParams);
begin
inherited;
Invalidate;
end;
procedure TFlatCheckBox.MouseEnter;
begin
if Enabled and not FMouseInControl then
begin
FMouseInControl := True;
DrawCheckRect;
end;
end;
procedure TFlatCheckBox.MouseLeave;
begin
if Enabled and FMouseInControl and not MouseIsDown then
begin
FMouseInControl := False;
RemoveMouseTimer;
DrawCheckRect;
end;
end;
procedure TFlatCheckBox.CMDialogChar (var Message: TCMDialogChar);
begin
with Message do
if IsAccel(Message.CharCode, Caption) and CanFocus then
begin
SetFocus;
if Checked then
Checked := False
else
Checked := True;
Result := 1;
end
else
if (CharCode = VK_SPACE) and Focused then
begin
if Checked then
Checked := False
else
Checked := True;
end
else
inherited;
end;
procedure TFlatCheckBox.CNCommand (var Message: TWMCommand);
begin
if Message.NotifyCode = BN_CLICKED then Click;
end;
procedure TFlatCheckBox.WMSetFocus (var Message: TWMSetFocus);
begin
inherited;
if Enabled then
begin
Focused := True;
DrawCheckRect;
end;
end;
procedure TFlatCheckBox.WMKillFocus (var Message: TWMKillFocus);
begin
inherited;
if Enabled then
begin
FMouseInControl := False;
Focused := False;
DrawCheckRect;
end;
end;
procedure TFlatCheckBox.CMSysColorChange (var Message: TMessage);
begin
if FUseAdvColors then
begin
ParentColor := True;
CalcAdvColors;
end;
Invalidate;
end;
procedure TFlatCheckBox.CMParentColorChanged (var Message: TWMNoParams);
begin
inherited;
if FUseAdvColors then
begin
ParentColor := True;
CalcAdvColors;
end;
Invalidate;
end;
procedure TFlatCheckBox.DoEnter;
begin
inherited DoEnter;
Focused := True;
DrawCheckRect;
end;
procedure TFlatCheckBox.DoExit;
begin
inherited DoExit;
Focused := False;
DrawCheckRect;
end;
procedure TFlatCheckBox.MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if (Button = mbLeft) and Enabled then
begin
SetFocus;
MouseIsDown := true;
DrawCheckRect;
inherited MouseDown(Button, Shift, X, Y);
end;
end;
procedure TFlatCheckBox.MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if (Button = mbLeft) and Enabled then
begin
MouseIsDown := false;
if FMouseInControl then
if Checked then
Checked := False
else
Checked := True;
DrawCheckRect;
inherited MouseUp(Button, Shift, X, Y);
end;
end;
procedure TFlatCheckBox.MouseMove (Shift: TShiftState; X, Y: Integer);
var
P: TPoint;
begin
inherited;
// mouse is in control ?
P := ClientToScreen(Point(X, Y));
if (MouseInControl <> Self) and (FindDragTarget(P, True) = Self) then
begin
if Assigned(MouseInControl) then
MouseInControl.MouseLeave;
// the application is active ?
if (GetActiveWindow <> 0) then
begin
if MouseTimer.Enabled then
MouseTimer.Enabled := False;
MouseInControl := Self;
MouseTimer.OnTimer := MouseTimerHandler;
MouseTimer.Enabled := True;
MouseEnter;
end;
end;
end;
procedure TFlatCheckBox.CreateWnd;
begin
inherited CreateWnd;
SendMessage(Handle, BM_SETCHECK, Cardinal(FChecked), 0);
end;
procedure TFlatCheckBox.DrawCheckRect;
var
CheckboxRect: TRect;
begin
case FLayout of
checkboxLeft:
CheckboxRect := Rect(ClientRect.Left + 1, ClientRect.Top + 3, ClientRect.Left + 12, ClientRect.Top + 14);
checkboxRight:
CheckboxRect := Rect(ClientRect.Right - 12, ClientRect.Top + 3, ClientRect.Right - 1, ClientRect.Top + 14);
end;
canvas.pen.style := psSolid;
canvas.pen.width := 1;
// Background
if Focused or FMouseInControl then
if not MouseIsDown then
begin
canvas.brush.color := FFocusedColor;
canvas.pen.color := FFocusedColor;
end
else
begin
canvas.brush.color := FDownColor;
canvas.brush.color := FDownColor;
end
else
begin
canvas.brush.color := Color;
canvas.pen.color := Color;
end;
canvas.FillRect(CheckboxRect);
// Tick
if Checked then
begin
if Enabled then
canvas.pen.color := FCheckColor
else
canvas.pen.color := clBtnShadow;
canvas.penpos := Point(CheckboxRect.left+2, CheckboxRect.top+4);
canvas.lineto(CheckboxRect.left+6, CheckboxRect.top+8);
canvas.penpos := Point(CheckboxRect.left+2, CheckboxRect.top+5);
canvas.lineto(CheckboxRect.left+5, CheckboxRect.top+8);
canvas.penpos := Point(CheckboxRect.left+2, CheckboxRect.top+6);
canvas.lineto(CheckboxRect.left+5, CheckboxRect.top+9);
canvas.penpos := Point(CheckboxRect.left+8, CheckboxRect.top+2);
canvas.lineto(CheckboxRect.left+4, CheckboxRect.top+6);
canvas.penpos := Point(CheckboxRect.left+8, CheckboxRect.top+3);
canvas.lineto(CheckboxRect.left+4, CheckboxRect.top+7);
canvas.penpos := Point(CheckboxRect.left+8, CheckboxRect.top+4);
canvas.lineto(CheckboxRect.left+5, CheckboxRect.top+7);
end;
// Border
canvas.brush.color := FBorderColor;
canvas.FrameRect(CheckboxRect);
end;
procedure TFlatCheckBox.DrawCheckText;
var
TextBounds: TRect;
Format: UINT;
begin
Format := DT_WORDBREAK;
case FLayout of
checkboxLeft:
begin
TextBounds := Rect(ClientRect.Left + 16, ClientRect.Top + 1, ClientRect.Right - 1, ClientRect.Bottom - 1);
Format := Format or DT_LEFT;
end;
checkboxRight:
begin
TextBounds := Rect(ClientRect.Left + 1, ClientRect.Top + 1, ClientRect.Right - 16, ClientRect.Bottom - 1);
Format := Format or DT_RIGHT;
end;
end;
with Canvas do
begin
Brush.Style := bsClear;
Font := Self.Font;
if not Enabled then
begin
OffsetRect(TextBounds, 1, 1);
Font.Color := clBtnHighlight;
DrawText(Handle, PChar(Caption), Length(Caption), TextBounds, Format);
OffsetRect(TextBounds, -1, -1);
Font.Color := clBtnShadow;
DrawText(Handle, PChar(Caption), Length(Caption), TextBounds, Format);
end
else
DrawText(Handle, PChar(Caption), Length(Caption), TextBounds, Format);
end;
end;
procedure TFlatCheckBox.Paint;
begin
if FTransparent then
DrawParentImage(Self, Self.Canvas);
DrawCheckRect;
DrawCheckText;
end;
procedure TFlatCheckBox.MouseTimerHandler (Sender: TObject);
var
P: TPoint;
begin
GetCursorPos (P);
if FindDragTarget(P, True) <> Self then
MouseLeave;
end;
procedure TFlatCheckBox.RemoveMouseTimer;
begin
if MouseInControl = Self then
begin
MouseTimer.Enabled := False;
MouseInControl := nil;
end;
end;
procedure TFlatCheckBox.SetTransparent(const Value: Boolean);
begin
FTransparent := Value;
Invalidate;
end;
procedure TFlatCheckBox.WMMove(var Message: TWMMove);
begin
inherited;
if FTransparent then
Invalidate;
end;
procedure TFlatCheckBox.WMSize(var Message: TWMSize);
begin
inherited;
if FTransparent then
Invalidate;
end;
{$IFDEF DFS_COMPILER_4_UP}
procedure TFlatCheckBox.SetBiDiMode(Value: TBiDiMode);
begin
inherited;
if BidiMode = bdRightToLeft then
Layout := checkboxRight
else
Layout := checkboxLeft;
end;
{$ENDIF}
end.