-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFlatCheckListBoxUnit.pas
840 lines (762 loc) · 24.5 KB
/
TFlatCheckListBoxUnit.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
unit TFlatCheckListBoxUnit;
interface
{$I DFS.inc}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, ExtCtrls, FlatUtilitys;
type
TFlatCheckListBox = class(TCustomControl)
private
FSelected: Integer;
FTransparent: TTransparentMode;
FOnClickCheck: TNotifyEvent;
cWheelMessage: Cardinal;
scrollType: TScrollType;
firstItem: Integer;
maxItems: Integer;
FSorted: Boolean;
FItems: TStringList;
FItemsRect: TList;
FItemsHeight: Integer;
FChecked: set of Byte;
FScrollBars: Boolean;
FUseAdvColors: Boolean;
FAdvColorBorder: TAdvColors;
FArrowColor: TColor;
FCheckColor: TColor;
FBorderColor: TColor;
FItemsRectColor: TColor;
FItemsSelectColor: TColor;
procedure SetColors (Index: Integer; Value: TColor);
procedure SetAdvColors (Index: Integer; Value: TAdvColors);
procedure SetUseAdvColors (Value: Boolean);
procedure SetSorted (Value: Boolean);
procedure SetItems (Value: TStringList);
procedure SetItemsRect;
procedure SetItemsHeight (Value: Integer);
function GetChecked (Index: Integer): Boolean;
procedure SetChecked (Index: Integer; Value: Boolean);
function GetSelCount: Integer;
procedure SetScrollBars (Value: Boolean);
function GetItemIndex: Integer;
procedure SetItemIndex (Value: Integer);
procedure WMSize (var Message: TWMSize); message WM_SIZE;
procedure WMMove (var Message: TWMMove); message WM_MOVE;
procedure CMEnabledChanged (var Message: TMessage); message CM_ENABLEDCHANGED;
procedure CMSysColorChange (var Message: TMessage); message CM_SYSCOLORCHANGE;
procedure CMParentColorChanged (var Message: TWMNoParams); message CM_PARENTCOLORCHANGED;
procedure ScrollTimerHandler (Sender: TObject);
procedure ItemsChanged (Sender: TObject);
procedure WMSetFocus (var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus (var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure CNKeyDown (var Message: TWMKeyDown); message CN_KEYDOWN;
procedure WMMouseWheel (var Message: TMessage); message WM_MOUSEWHEEL;
procedure SetTransparent (const Value: TTransparentMode);
protected
procedure CalcAdvColors;
procedure DrawCheckRect (canvas: TCanvas; start: TPoint; checked: Boolean);
procedure DrawScrollBar (canvas: TCanvas);
procedure Paint; override;
procedure Loaded; override;
procedure MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure WndProc (var Message: TMessage); override;
{$IFDEF DFS_COMPILER_4_UP}
procedure SetBiDiMode(Value: TBiDiMode); override;
{$ENDIF}
public
constructor Create (AOwner: TComponent); override;
destructor Destroy; override;
property Checked [Index: Integer]: Boolean read GetChecked write SetChecked;
property SelCount: Integer read GetSelCount;
procedure Clear;
property ItemIndex: Integer read GetItemIndex write SetItemIndex;
published
property Items: TStringList read FItems write SetItems;
property ItemHeight: Integer read FItemsHeight write SetItemsHeight default 17;
property ScrollBars: Boolean read FScrollBars write SetScrollBars default false;
property Color default $00E1EAEB;
property ColorArrow: TColor index 0 read FArrowColor write SetColors default clBlack;
property ColorBorder: TColor index 1 read FBorderColor write SetColors default $008396A0;
property ColorItemsRect: TColor index 2 read FItemsRectColor write SetColors default clWhite;
property ColorItemsSelect: TColor index 3 read FItemsSelectColor write SetColors default $009CDEF7;
property ColorCheck: TColor index 4 read FCheckColor write SetColors default clBlack;
property AdvColorBorder: TAdvColors index 0 read FAdvColorBorder write SetAdvColors default 40;
property UseAdvColors: Boolean read FUseAdvColors write SetUseAdvColors default false;
property Sorted: Boolean read FSorted write SetSorted default false;
property TransparentMode: TTransparentMode read FTransparent write SetTransparent default tmNone;
property Align;
property Font;
property ParentFont;
property ParentColor;
property ParentShowHint;
property Enabled;
property Visible;
property PopupMenu;
property ShowHint;
property OnClick;
property OnClickCheck: TNotifyEvent read FOnClickCheck write FOnClickCheck;
property OnMouseMove;
property OnMouseDown;
property OnMouseUp;
{$IFDEF DFS_COMPILER_4_UP}
property Anchors;
property BiDiMode write SetBidiMode;
property Constraints;
property DragKind;
property ParentBiDiMode;
property OnEndDock;
property OnStartDock;
{$ENDIF}
end;
implementation
var
ScrollTimer: TTimer = nil;
const
FTimerInterval = 600;
FScrollSpeed = 100;
constructor TFlatCheckListBox.Create (AOwner: TComponent);
begin
inherited;
if ScrollTimer = nil then
begin
ScrollTimer := TTimer.Create(nil);
ScrollTimer.Enabled := False;
ScrollTimer.Interval := FTimerInterval;
end;
ControlStyle := ControlStyle + [csOpaque];
SetBounds(Left, Top, 137, 99);
FItems := TStringList.Create;
FItemsRect := TList.Create;
FItemsHeight := 17;
TStringList(FItems).OnChange := ItemsChanged;
FScrollBars := false;
firstItem := 0;
FArrowColor := clBlack;
FBorderColor := $008396A0;
FItemsRectColor := clWhite;
FItemsSelectColor := $009CDEF7;
FCheckColor := clBlack;
ParentColor := True;
ParentFont := True;
Enabled := true;
Visible := true;
FUseAdvColors := false;
FAdvColorBorder := 40;
FSorted := false;
FTransparent := tmNone;
FSelected := -1;
cWheelMessage:= RegisterWindowMessage(MSH_MOUSEWHEEL);
end;
destructor TFlatCheckListBox.Destroy;
var
counter: Integer;
begin
ScrollTimer.Free;
ScrollTimer := nil;
FItems.Free;
for counter := 0 to FItemsRect.Count - 1 do
Dispose(FItemsRect.Items[counter]);
FItemsRect.Free;
inherited;
end;
procedure TFlatCheckListBox.WndProc (var Message: TMessage);
begin
if Message.Msg = cWheelMessage then
begin
SendMessage (Self.Handle, WM_MOUSEWHEEL, Message.wParam, Message.lParam);
end;
inherited;
end;
procedure TFlatCheckListBox.WMMouseWheel (var Message: TMessage);
var
fScrollLines: Integer;
begin
if not(csDesigning in ComponentState) then
begin
SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, @fScrollLines, 0);
if (fScrollLines = 0) then
fScrollLines := maxItems;
if ShortInt(Message.WParamHi) = -WHEEL_DELTA then
if firstItem + maxItems + fScrollLines <= FItems.Count then
Inc(firstItem, fScrollLines)
else
if FItems.Count - maxItems < 0 then
firstItem := 0
else
firstItem := FItems.Count - maxItems
else
if ShortInt(Message.WParamHi) = WHEEL_DELTA then
if firstItem - fScrollLines < 0 then
firstItem := 0
else
dec(firstItem, fScrollLines);
Invalidate;
end;
end;
procedure TFlatCheckListBox.ItemsChanged (Sender: TObject);
begin
if Enabled then
begin
FChecked := FChecked - [0..High(Byte)];
Invalidate;
end;
end;
procedure TFlatCheckListBox.SetColors (Index: Integer; Value: TColor);
begin
case Index of
0: FArrowColor := Value;
1: FBorderColor := Value;
2: FItemsRectColor := Value;
3: FItemsSelectColor := Value;
4: FCheckColor := Value;
end;
Invalidate;
end;
procedure TFlatCheckListBox.CalcAdvColors;
begin
if FUseAdvColors then
begin
FBorderColor := CalcAdvancedColor(Color, FBorderColor, FAdvColorBorder, darken);
end;
end;
procedure TFlatCheckListBox.SetAdvColors (Index: Integer; Value: TAdvColors);
begin
case Index of
0: FAdvColorBorder := Value;
end;
CalcAdvColors;
Invalidate;
end;
procedure TFlatCheckListBox.SetUseAdvColors (Value: Boolean);
begin
if Value <> FUseAdvColors then
begin
FUseAdvColors := Value;
ParentColor := Value;
CalcAdvColors;
Invalidate;
end;
end;
procedure TFlatCheckListBox.SetSorted (Value: Boolean);
begin
if Value <> FSorted then
begin
FSorted := Value;
FItems.Sorted := Value;
FChecked := FChecked - [0..High(Byte)];
Invalidate;
end;
end;
procedure TFlatCheckListBox.SetItems (Value: TStringList);
var
counter: Integer;
begin
if Value.Count - 1 > High(Byte) then
Exit;
// delete all spaces at left and right
for counter := 0 to Value.Count - 1 do
Value[counter] := Trim(Value[counter]);
FItems.Assign(Value);
Invalidate;
end;
procedure TFlatCheckListBox.SetItemsRect;
var
counter: Integer;
ItemRect: ^TRect;
position: TPoint;
begin
// Delete all curent Rects
FItemsRect.Clear;
// calculate the maximum items to draw
if ScrollBars then
maxItems := (Height - 24) div (FItemsHeight + 2)
else
maxItems := (Height - 4) div (FItemsHeight + 2);
// set left/top position for the the first item
if ScrollBars then
position := Point(ClientRect.left + 3, ClientRect.top + 13)
else
position := Point(ClientRect.left + 3, ClientRect.top + 3);
for counter := 0 to maxItems - 1 do
begin
// create a new Item-Rect
New(ItemRect);
// calculate the Item-Rect
ItemRect^ := Rect(position.x, position.y, ClientRect.Right - 3, position.y + FItemsHeight);
// set left/top position for next Item-Rect
position := Point(position.x, position.y + FItemsHeight + 2);
// add the Item-Rect to the Items-Rect-List
FItemsRect.Add(ItemRect);
end;
Invalidate;
end;
procedure TFlatCheckListBox.SetItemsHeight (Value: Integer);
begin
if Value < 1 then
Value := 1;
FItemsHeight := Value;
if not (csLoading in ComponentState) then
if ScrollBars then
SetBounds(Left, Top, Width, maxItems * (FItemsHeight + 2) + 24)
else
SetBounds(Left, Top, Width, maxItems * (FItemsHeight + 2) + 4);
SetItemsRect;
end;
function TFlatCheckListBox.GetChecked (Index: Integer): Boolean;
begin
Result := Index in FChecked;
end;
procedure TFlatCheckListBox.SetChecked (Index: Integer; Value: Boolean);
begin
if Value then
Include(FChecked, Index)
else
Exclude(FChecked, Index);
Invalidate;
end;
{$IFDEF DFS_COMPILER_4_UP}
procedure TFlatCheckListBox.SetBiDiMode(Value: TBiDiMode);
begin
inherited;
Invalidate;
end;
{$ENDIF}
function TFlatCheckListBox.GetSelCount: Integer;
var
counter: Integer;
begin
Result := 0;
for counter := 0 to High(Byte) do
if counter in FChecked then
Inc(Result);
end;
procedure TFlatCheckListBox.SetScrollBars (Value: Boolean);
begin
if FScrollBars <> Value then
begin
FScrollBars := Value;
if not (csLoading in ComponentState) then
if Value then
Height := Height + 20
else
Height := Height - 20;
SetItemsRect;
end;
end;
procedure TFlatCheckListBox.DrawScrollBar (canvas: TCanvas);
var
x, y: Integer;
begin
// Draw the ScrollBar background
canvas.Brush.Color := Color;
canvas.FillRect(Rect(ClientRect.Left, ClientRect.Top, ClientRect.Right, ClientRect.Top + 11));
canvas.FillRect(Rect(ClientRect.Left, ClientRect.Bottom - 11, ClientRect.Right, ClientRect.Bottom));
// Draw the ScrollBar border
canvas.Brush.Color := FBorderColor;
canvas.FrameRect(Rect(ClientRect.Left, ClientRect.Top, ClientRect.Right, ClientRect.Top + 11));
canvas.FrameRect(Rect(ClientRect.Left, ClientRect.Bottom - 11, ClientRect.Right, ClientRect.Bottom));
// Draw the up arrow
x := (ClientRect.Right - ClientRect.Left) div 2 - 6;
y := ClientRect.Top + 4;
if (firstItem <> 0) and Enabled then
begin
canvas.Brush.Color := FArrowColor;
canvas.Pen.Color := FArrowColor;
canvas.Polygon([Point(x + 4, y + 2), Point(x + 8, y + 2), Point(x + 6, y)]);
end
else
begin
canvas.Brush.Color := clWhite;
canvas.Pen.Color := clWhite;
Inc(x); Inc(y);
canvas.Polygon([Point(x + 4, y + 2), Point(x + 8, y + 2), Point(x + 6, y)]);
Dec(x); Dec(y);
canvas.Brush.Color := clGray;
canvas.Pen.Color := clGray;
canvas.Polygon([Point(x + 4, y + 2), Point(x + 8, y + 2), Point(x + 6, y)]);
end;
// Draw the down arrow
y := ClientRect.Bottom - 7;
if (firstItem + maxItems + 1 <= FItems.Count) and Enabled then
begin
canvas.Brush.Color := FArrowColor;
canvas.Pen.Color := FArrowColor;
canvas.Polygon([Point(X + 4, Y), Point(X + 8, Y), Point(X + 6, Y + 2)]);
end
else
begin
canvas.Brush.Color := clWhite;
canvas.Pen.Color := clWhite;
Inc(x); Inc(y);
canvas.Polygon([Point(X + 4, Y), Point(X + 8, Y), Point(X + 6, Y + 2)]);
Dec(x); Dec(y);
canvas.Brush.Color := clGray;
canvas.Pen.Color := clGray;
canvas.Polygon([Point(X + 4, Y), Point(X + 8, Y), Point(X + 6, Y + 2)]);
end;
end;
procedure TFlatCheckListBox.DrawCheckRect (canvas: TCanvas; start: TPoint; checked: Boolean);
var
CheckboxRect: TRect;
begin
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
CheckboxRect := Rect(start.x - 14, start.y + 3, start.x - 3, start.y + 14)
else
CheckboxRect := Rect(start.x + 3, start.y + 3, start.x + 14, start.y + 14);
{$ELSE}
CheckboxRect := Rect(start.x + 3, start.y + 3, start.x + 14, start.y + 14);
{$ENDIF}
canvas.pen.style := psSolid;
canvas.pen.width := 1;
// Background
canvas.brush.color := FItemsRectColor;
canvas.pen.color := FItemsRectColor;
canvas.FillRect(CheckboxRect);
// Tick
if Checked then
begin
canvas.pen.color := FCheckColor;
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 TFlatCheckListBox.Paint;
var
memoryBitmap: TBitmap;
counterRect, counterItem: Integer;
itemRect: ^TRect;
Format: UINT;
begin
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
Format := DT_RIGHT or DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX
else
Format := DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX;
{$ELSE}
Format := DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX;
{$ENDIF}
// create memory-bitmap to draw flicker-free
memoryBitmap := TBitmap.Create;
try
memoryBitmap.Height := ClientRect.Bottom;
memoryBitmap.Width := ClientRect.Right;
memoryBitmap.Canvas.Font.Assign(Self.Font);
// Clear Background
case FTransparent of
tmAlways:
DrawParentImage(Self, memoryBitmap.Canvas);
tmNone:
begin
memoryBitmap.canvas.Brush.Color := FItemsRectColor;
memoryBitmap.canvas.FillRect(ClientRect);
end;
tmNotFocused:
if Focused then
begin
memoryBitmap.canvas.Brush.Color := FItemsRectColor;
memoryBitmap.canvas.FillRect(ClientRect);
end
else
DrawParentImage(Self, memoryBitmap.Canvas);
end;
// Draw Border
memoryBitmap.canvas.Brush.Color := FBorderColor;
memoryBitmap.canvas.FrameRect(ClientRect);
// Draw ScrollBars
if ScrollBars then
DrawScrollBar(memoryBitmap.canvas);
// Initialize the counter for the Items
counterItem := firstItem;
// Draw Items
for counterRect := 0 to maxItems - 1 do
begin
itemRect := FItemsRect.Items[counterRect];
if (counterItem <= FItems.Count - 1) then
begin
// Item is selected
if counterItem = FSelected then
begin
// Fill ItemRect
memoryBitmap.canvas.brush.color := FItemsSelectColor;
memoryBitmap.canvas.FillRect(itemRect^);
// Draw ItemBorder
memoryBitmap.canvas.brush.color := FBorderColor;
memoryBitmap.canvas.FrameRect(itemRect^);
end;
if counterItem in FChecked then
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
DrawCheckRect(memoryBitmap.canvas, Point(itemRect^.Right, itemRect^.top), true)
else
DrawCheckRect(memoryBitmap.canvas, Point(itemRect^.left, itemRect^.top), true)
{$ELSE}
DrawCheckRect(memoryBitmap.canvas, Point(itemRect^.left, itemRect^.top), true)
{$ENDIF}
else
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
DrawCheckRect(memoryBitmap.canvas, Point(itemRect^.Right, itemRect^.top), false)
else
DrawCheckRect(memoryBitmap.canvas, Point(itemRect^.left, itemRect^.top), false);
{$ELSE}
DrawCheckRect(memoryBitmap.canvas, Point(itemRect^.left, itemRect^.top), false);
{$ENDIF}
// Draw ItemText
memoryBitmap.canvas.brush.style := bsClear;
InflateRect(itemRect^, -19, 0);
if Enabled then
DrawText(memoryBitmap.canvas.Handle, PChar(FItems[counterItem]), Length(FItems[counterItem]), itemRect^, Format)
else
begin
OffsetRect(itemRect^, 1, 1);
memoryBitmap.canvas.Font.Color := clBtnHighlight;
DrawText(memoryBitmap.canvas.Handle, PChar(FItems[counterItem]), Length(FItems[counterItem]), itemRect^, Format);
OffsetRect(itemRect^, -1, -1);
memoryBitmap.canvas.Font.Color := clBtnShadow;
DrawText(memoryBitmap.canvas.Handle, PChar(FItems[counterItem]), Length(FItems[counterItem]), itemRect^, Format);
end;
InflateRect(itemRect^, 19, 0);
Inc(counterItem);
end;
end;
// Copy bitmap to screen
canvas.CopyRect(ClientRect, memoryBitmap.canvas, ClientRect);
finally
// delete the memory bitmap
memoryBitmap.free;
end;
end;
procedure TFlatCheckListBox.MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
cursorPos: TPoint;
counterRect: Integer;
currentRect: ^TRect;
checkRect: TRect;
begin
GetCursorPos(cursorPos);
cursorPos := ScreenToClient(cursorPos);
if (FItems.Count > 0) and (Button = mbLeft) then
begin
for counterRect := 0 to FItemsRect.Count - 1 do
begin
currentRect := FItemsRect.Items[counterRect];
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
checkRect := Rect(currentRect.right - 14, currentRect.top + 3, currentRect.right - 3, currentRect.Top + 14)
else
checkRect := Rect(currentRect.left + 3, currentRect.top + 3, currentRect.left + 14, currentRect.Top + 14);
{$ELSE}
checkRect := Rect(currentRect.left + 3, currentRect.top + 3, currentRect.left + 14, currentRect.Top + 14);
{$ENDIF}
if PtInRect(checkRect, cursorPos) then
begin
if (firstItem + counterRect) in FChecked then
Exclude(FChecked, firstItem + counterRect)
else
Include(FChecked, firstItem + counterRect);
SetFocus;
if Assigned(FOnClickCheck) then
FOnClickCheck(Self);
Invalidate;
Exit;
end
else
if PtInRect(currentRect^, cursorPos) then
begin
FSelected := firstItem + counterRect;
SetFocus;
Invalidate;
Exit;
end;
end;
end;
if ScrollBars then
begin
if PtInRect(Rect(ClientRect.Left, ClientRect.Top, ClientRect.Right, ClientRect.Top + 11), cursorPos) then
begin
if (firstItem - 1) < 0 then
firstItem := 0
else
Dec(firstItem);
SetFocus;
Invalidate;
scrollType := up;
if ScrollTimer.Enabled then
ScrollTimer.Enabled := False;
ScrollTimer.OnTimer := ScrollTimerHandler;
ScrollTimer.Enabled := True;
end;
if PtInRect(Rect(ClientRect.Left, ClientRect.Bottom - 11, ClientRect.Right, ClientRect.Bottom), cursorPos) then
begin
if firstItem + maxItems + 1 <= FItems.Count then
Inc(firstItem);
SetFocus;
Invalidate;
scrollType := down;
if ScrollTimer.Enabled then
ScrollTimer.Enabled := False;
ScrollTimer.OnTimer := ScrollTimerHandler;
ScrollTimer.Enabled := True;
end;
end;
Inherited;
end;
procedure TFlatCheckListBox.MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ScrollTimer.Enabled := False;
ScrollTimer.Interval := FTimerInterval;
inherited MouseUp(Button, Shift, X, Y);
end;
procedure TFlatCheckListBox.ScrollTimerHandler (Sender: TObject);
begin
ScrollTimer.Interval := FScrollSpeed;
if scrollType = up then
if (firstItem - 1) < 0 then
begin
firstItem := 0;
ScrollTimer.Enabled := False;
end
else
Dec(firstItem)
else
if firstItem + maxItems + 1 <= FItems.Count then
Inc(firstItem)
else
ScrollTimer.Enabled := False;
Invalidate;
end;
procedure TFlatCheckListBox.Loaded;
begin
inherited;
SetItemsRect;
end;
procedure TFlatCheckListBox.WMSize (var Message: TWMSize);
begin
inherited;
// Calculate the maximum items to draw
if ScrollBars then
maxItems := (Height - 24) div (FItemsHeight + 2)
else
maxItems := (Height - 4) div (FItemsHeight + 2);
// Set the new Bounds
if ScrollBars then
SetBounds(Left, Top, Width, maxItems * (FItemsHeight + 2) + 24)
else
SetBounds(Left, Top, Width, maxItems * (FItemsHeight + 2) + 4);
// Recalculate the itemRects
SetItemsRect;
if not (FTransparent = tmNone) then
Invalidate;
end;
procedure TFlatCheckListBox.WMMove (var Message: TWMMove);
begin
inherited;
if not (FTransparent = tmNone) then
Invalidate;
end;
procedure TFlatCheckListBox.CMEnabledChanged (var Message: TMessage);
begin
inherited;
Invalidate;
end;
procedure TFlatCheckListBox.CMSysColorChange (var Message: TMessage);
begin
if FUseAdvColors then
begin
ParentColor := True;
CalcAdvColors;
end;
Invalidate;
end;
procedure TFlatCheckListBox.CMParentColorChanged (var Message: TWMNoParams);
begin
inherited;
if FUseAdvColors then
begin
ParentColor := True;
CalcAdvColors;
end;
Invalidate;
end;
procedure TFlatCheckListBox.Clear;
begin
FItems.Clear;
FChecked := FChecked - [0..High(Byte)];
FSelected := -1;
Invalidate;
end;
procedure TFlatCheckListBox.SetTransparent (const Value: TTransparentMode);
begin
FTransparent := Value;
Invalidate;
end;
procedure TFlatCheckListBox.WMKillFocus (var Message: TWMKillFocus);
begin
inherited;
FSelected := -1;
Invalidate;
end;
procedure TFlatCheckListBox.WMSetFocus (var Message: TWMSetFocus);
begin
inherited;
if not (FTransparent = tmNone) then
Invalidate;
end;
procedure TFlatCheckListBox.CNKeyDown (var Message: TWMKeyDown);
begin
case Message.CharCode of
VK_UP:
if (firstItem - 1) < 0 then
firstItem := 0
else
Dec(firstItem);
VK_DOWN:
if firstItem + maxItems + 1 <= FItems.Count then
Inc(firstItem);
VK_PRIOR:
if (firstItem - maxItems) < 0 then
firstItem := 0
else
Dec(firstItem, maxItems);
VK_NEXT:
if firstItem + (maxItems * 2) <= FItems.Count then
Inc(firstItem, maxItems)
else
firstItem := FItems.Count - maxItems;
VK_SPACE:
if FSelected in FChecked then
Exclude(FChecked, FSelected)
else
Include(FChecked, FSelected);
else
inherited;
end;
Invalidate;
end;
function TFlatCheckListBox.GetItemIndex: Integer;
begin
Result := FSelected;
end;
procedure TFlatCheckListBox.SetItemIndex(Value: Integer);
begin
if GetItemIndex <> Value then
begin
FSelected := Value;
Invalidate;
end;
end;
end.