-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckListState.pas
994 lines (916 loc) · 28.5 KB
/
CheckListState.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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
(*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Initial Developer of this code is John Hansen.
* Portions created by John Hansen are Copyright (C) 2009-2013 John Hansen.
* All Rights Reserved.
*
*)
unit CheckListState;
{$I bricxcc.inc}
{$T-,H+,X+}
interface
uses
{$IFNDEF FPC}
Windows,
{$ELSE}
LCLType,
LMessages,
{$ENDIF}
Messages, SysUtils, Classes, Graphics, Controls,
StdCtrls, ImgList;
type
TGetCheckHintEvent = procedure(Sender : TObject; State : TCheckBoxState; var Hint : string) of object;
TGetStateHintEvent = procedure(Sender : TObject; State : Integer; var Hint : string) of object;
TCheckStateListBox = class(TCustomListBox)
private
FAllowGrayed: Boolean;
FFlat: Boolean;
FStandardItemHeight: Integer;
FOnClickCheck: TNotifyEvent;
FSaveStates: TList;
FOnClickState: TNotifyEvent;
FStateImages: TCustomImageList;
FStateChangeLink: TChangeLink;
fShowState: Boolean;
FOnGetCheckHint: TGetCheckHintEvent;
FOnGetStateHint: TGetStateHintEvent;
FHeaderColor: TColor;
FHeaderBackgroundColor: TColor;
procedure ResetItemHeight;
procedure DrawCheck(R: TRect; AState: TCheckBoxState; AEnabled: Boolean);
procedure DrawItemState(R: TRect; Index : Integer);
procedure SetChecked(Index: Integer; Checked: Boolean);
function GetChecked(Index: Integer): Boolean;
procedure SetCheckedState(Index: Integer; AState: TCheckBoxState);
function GetCheckedState(Index: Integer): TCheckBoxState;
procedure ToggleClickCheck(Index: Integer);
procedure InvalidateCheck(Index: Integer);
procedure InvalidateState(Index: Integer);
function CreateWrapper(Index: Integer): TObject;
function ExtractWrapper(Index: Integer): TObject;
function GetWrapper(Index: Integer): TObject;
function HaveWrapper(Index: Integer): Boolean;
procedure SetFlat(Value: Boolean);
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure WMDestroy(var Msg : TWMDestroy);message WM_DESTROY;
function GetItemEnabled(Index: Integer): Boolean;
procedure SetItemEnabled(Index: Integer; const Value: Boolean);
function GetHeader(Index: Integer): Boolean;
procedure SetHeader(Index: Integer; const Value: Boolean);
procedure SetHeaderBackgroundColor(const Value: TColor);
procedure SetHeaderColor(const Value: TColor);
function GetState(Index: Integer): Integer;
procedure SetState(Index: Integer; const Value: Integer);
procedure SetStateImages(Value: TCustomImageList);
procedure SetShowState(const Value: Boolean);
protected
fSavedHint : string;
procedure Loaded; override;
function GetStateWidth : Integer;
function GetStateHeight : Integer;
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
function InternalGetItemData(Index: Integer): Longint; override;
procedure InternalSetItemData(Index: Integer; AData: Longint); override;
procedure SetItemData(Index: Integer; AData: LongInt); override;
function GetItemData(Index: Integer): LongInt; override;
procedure KeyPress(var Key: Char); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure ResetContent; override;
procedure DeleteString(Index: Integer); override;
procedure ClickCheck; dynamic;
procedure ClickState; dynamic;
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWnd; override;
procedure DestroyWnd; override;
function GetCheckWidth: Integer;
procedure ImageListChange(Sender: TObject);
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure DoGetCheckHint(State : TCheckBoxState; var Hint : string);
procedure DoGetStateHint(State : Integer; var Hint : string);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Checked[Index: Integer]: Boolean read GetChecked write SetChecked;
property ItemEnabled[Index: Integer]: Boolean read GetItemEnabled write SetItemEnabled;
property CheckedState[Index: Integer]: TCheckBoxState read GetCheckedState write SetCheckedState;
property State[Index: Integer]: Integer read GetState write SetState;
property Header[Index: Integer]: Boolean read GetHeader write SetHeader;
published
property StateImages: TCustomImageList read FStateImages write SetStateImages;
property ShowState : Boolean read fShowState write SetShowState;
property OnClickCheck: TNotifyEvent read FOnClickCheck write FOnClickCheck;
property OnClickState: TNotifyEvent read FOnClickState write FOnClickState;
property OnGetCheckHint: TGetCheckHintEvent read FOnGetCheckHint write FOnGetCheckHint;
property OnGetStateHint: TGetStateHintEvent read FOnGetStateHint write FOnGetStateHint;
property Align;
property AllowGrayed: Boolean read FAllowGrayed write FAllowGrayed default False;
property Anchors;
property BiDiMode;
property BorderStyle;
property Color;
property Columns;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Flat: Boolean read FFlat write SetFlat default True;
//property ExtendedSelect;
property Font;
property HeaderColor: TColor read FHeaderColor write SetHeaderColor default clInfoText;
property HeaderBackgroundColor: TColor read FHeaderBackgroundColor write SetHeaderBackgroundColor default clInfoBk;
property ImeMode;
property ImeName;
property IntegralHeight;
property ItemHeight;
property Items;
//property MultiSelect;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Sorted;
property Style;
property TabOrder;
property TabStop;
property TabWidth;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
uses Consts, Math, Forms{$IFDEF VER_D6_UP},RTLConsts, Themes{$ENDIF};
type
TCheckStateListBoxDataWrapper = class
private
FData: LongInt;
FCheckedState: TCheckBoxState;
FDisabled: Boolean;
fState: Integer;
FHeader: Boolean;
procedure SetChecked(Check: Boolean);
function GetChecked: Boolean;
public
class function GetDefaultCheckedState: TCheckBoxState;
property Checked: Boolean read GetChecked write SetChecked;
property CheckedState: TCheckBoxState read FCheckedState write FCheckedState;
property Disabled: Boolean read FDisabled write FDisabled;
property State: Integer read fState write fState;
property Header: Boolean read FHeader write FHeader;
end;
var
FCheckWidth, FCheckHeight: Integer;
procedure Register;
begin
RegisterComponents('Samples', [TCheckStateListBox]);
end;
procedure GetCheckSize;
begin
with TBitmap.Create do
try
Handle := LoadBitmap(0, PChar(32759));
FCheckWidth := Width div 4;
FCheckHeight := Height div 3;
finally
Free;
end;
end;
function MakeSaveState(CheckedState: TCheckBoxState; Disabled: Boolean): TObject;
begin
Result := TObject((Byte(CheckedState) shl 16) or Byte(Disabled));
end;
function GetSaveState(AObject: TObject): TCheckBoxState;
begin
Result := TCheckBoxState(Integer(AObject) shr 16);
end;
function GetSaveDisabled(AObject: TObject): Boolean;
begin
Result := Boolean(Integer(AObject) and $FF);
end;
{ TCheckStateListBoxDataWrapper }
procedure TCheckStateListBoxDataWrapper.SetChecked(Check: Boolean);
begin
if Check then FCheckedState := cbChecked else FCheckedState := cbUnchecked;
end;
function TCheckStateListBoxDataWrapper.GetChecked: Boolean;
begin
Result := FCheckedState = cbChecked;
end;
class function TCheckStateListBoxDataWrapper.GetDefaultCheckedState: TCheckBoxState;
begin
Result := cbUnchecked;
end;
{ TCheckStateListBox }
constructor TCheckStateListBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fSavedHint := '';
FFlat := True;
FStateChangeLink := TChangeLink.Create;
FStateChangeLink.OnChange := ImageListChange;
end;
destructor TCheckStateListBox.Destroy;
begin
FStateChangeLink.Free;
FSaveStates.Free;
inherited;
end;
procedure TCheckStateListBox.CreateWnd;
begin
inherited CreateWnd;
if FSaveStates <> nil then
begin
FSaveStates.Free;
FSaveStates := nil;
end;
ResetItemHeight;
end;
procedure TCheckStateListBox.DestroyWnd;
var
I: Integer;
begin
if Items.Count > 0 then
begin
FSaveStates := TList.Create;
for I := 0 to Items.Count - 1 do
FSaveStates.Add(MakeSaveState(CheckedState[I], not ItemEnabled[I]));
end;
inherited DestroyWnd;
end;
procedure TCheckStateListBox.CreateParams(var Params: TCreateParams);
begin
inherited;
with Params do
if Style and (LBS_OWNERDRAWFIXED or LBS_OWNERDRAWVARIABLE) = 0 then
Style := Style or LBS_OWNERDRAWFIXED;
end;
function TCheckStateListBox.GetCheckWidth: Integer;
begin
Result := FCheckWidth + 2;
end;
procedure TCheckStateListBox.CMFontChanged(var Message: TMessage);
begin
inherited;
ResetItemHeight;
end;
procedure TCheckStateListBox.ResetItemHeight;
begin
if HandleAllocated and (Style = lbStandard) then
begin
Canvas.Font := Font;
FStandardItemHeight := Canvas.TextHeight('Wg');
Perform(LB_SETITEMHEIGHT, 0, FStandardItemHeight);
end;
end;
const
K_CHECK_FUDGE = 12;
procedure TCheckStateListBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var
R: TRect;
SaveEvent: TDrawItemEvent;
ACheckWidth: Integer;
AStateWidth: Integer;
Enable: Boolean;
begin
ACheckWidth := GetCheckWidth;
AStateWidth := GetStateWidth;
if Index < Items.Count then
begin
R := Rect;
Enable := Self.Enabled and GetItemEnabled(Index);
if not Header[Index] then
begin
if not UseRightToLeftAlignment then
begin
R.Right := Rect.Left;
R.Left := R.Right - ACheckWidth - AStateWidth - K_CHECK_FUDGE;
end
else
begin
R.Left := Rect.Right;
R.Right := R.Left + ACheckWidth + AStateWidth + K_CHECK_FUDGE;
end;
DrawCheck(R, GetCheckedState(Index), Enable);
if not UseRightToLeftAlignment then
begin
R.Right := Rect.Left;
R.Left := R.Right - ACheckWidth - AStateWidth;
end
else
begin
R.Left := Rect.Right;
R.Right := R.Left + ACheckWidth + AStateWidth;
end;
DrawItemState(R, GetState(Index));
end
else
begin
Canvas.Font.Color := HeaderColor;
Canvas.Brush.Color := HeaderBackgroundColor;
end;
if not Enable then
Canvas.Font.Color := clGrayText;
end;
if (Style = lbStandard) and Assigned(OnDrawItem) then
begin
{ Force lbStandard list to ignore OnDrawItem event. }
SaveEvent := OnDrawItem;
OnDrawItem := nil;
try
inherited;
finally
OnDrawItem := SaveEvent;
end;
end
else
inherited;
end;
procedure TCheckStateListBox.CNDrawItem(var Message: TWMDrawItem);
begin
with Message.DrawItemStruct^ do
if not UseRightToLeftAlignment then begin
rcItem.Left := rcItem.Left + GetCheckWidth + GetStateWidth;
end
else begin
rcItem.Right := rcItem.Right - GetCheckWidth - GetStateWidth;
end;
inherited;
end;
procedure TCheckStateListBox.DrawCheck(R: TRect; AState: TCheckBoxState; AEnabled: Boolean);
var
DrawState: Integer;
DrawRect: TRect;
OldBrushColor: TColor;
OldBrushStyle: TBrushStyle;
OldPenColor: TColor;
Rgn, SaveRgn: HRgn;
ElementDetails: TThemedElementDetails;
begin
SaveRgn := 0;
DrawRect.Left := R.Left + (R.Right - R.Left - FCheckWidth) div 2;
DrawRect.Top := R.Top + (R.Bottom - R.Top - FCheckHeight) div 2;
DrawRect.Right := DrawRect.Left + FCheckWidth;
DrawRect.Bottom := DrawRect.Top + FCheckHeight;
with Canvas do
begin
if Flat then
begin
{ Remember current clipping region }
SaveRgn := CreateRectRgn(0,0,0,0);
GetClipRgn(Handle, SaveRgn);
{ Clip 3d-style checkbox to prevent flicker }
with DrawRect do
Rgn := CreateRectRgn(Left + 2, Top + 2, Right - 2, Bottom - 2);
SelectClipRgn(Handle, Rgn);
DeleteObject(Rgn);
end;
if ThemeServices.ThemesEnabled then
begin
case AState of
cbChecked:
if AEnabled then
ElementDetails := ThemeServices.GetElementDetails(tbCheckBoxCheckedNormal)
else
ElementDetails := ThemeServices.GetElementDetails(tbCheckBoxCheckedDisabled);
cbUnchecked:
if AEnabled then
ElementDetails := ThemeServices.GetElementDetails(tbCheckBoxUncheckedNormal)
else
ElementDetails := ThemeServices.GetElementDetails(tbCheckBoxUncheckedDisabled)
else // cbGrayed
if AEnabled then
ElementDetails := ThemeServices.GetElementDetails(tbCheckBoxMixedNormal)
else
ElementDetails := ThemeServices.GetElementDetails(tbCheckBoxMixedDisabled);
end;
ThemeServices.DrawElement(Handle, ElementDetails, R);
end
else
begin
case AState of
cbChecked:
DrawState := DFCS_BUTTONCHECK or DFCS_CHECKED;
cbUnchecked:
DrawState := DFCS_BUTTONCHECK;
else // cbGrayed
DrawState := DFCS_BUTTON3STATE or DFCS_CHECKED;
end;
if not AEnabled then
DrawState := DrawState or DFCS_INACTIVE;
DrawFrameControl(Handle, DrawRect, DFC_BUTTON, DrawState);
end;
if Flat then
begin
SelectClipRgn(Handle, SaveRgn);
DeleteObject(SaveRgn);
{ Draw flat rectangle in-place of clipped 3d checkbox above }
OldBrushStyle := Brush.Style;
OldBrushColor := Brush.Color;
OldPenColor := Pen.Color;
Brush.Style := bsClear;
Pen.Color := clBtnShadow;
with DrawRect do
Rectangle(Left + 1, Top + 1, Right - 1, Bottom - 1);
Brush.Style := OldBrushStyle;
Brush.Color := OldBrushColor;
Pen.Color := OldPenColor;
end;
end;
end;
(*
procedure TCheckStateListBox.DrawCheck(R: TRect; AState: TCheckBoxState; AEnabled: Boolean);
var
DrawState: Integer;
DrawRect: TRect;
OldBrushColor: TColor;
OldBrushStyle: TBrushStyle;
OldPenColor: TColor;
Rgn, SaveRgn: HRgn;
begin
SaveRgn := 0;
DrawRect.Left := R.Left + (R.Right - R.Left - FCheckWidth - GetStateWidth) div 2;
DrawRect.Top := R.Top + (R.Bottom - R.Top - FCheckWidth) div 2;
DrawRect.Right := DrawRect.Left + FCheckWidth;
DrawRect.Bottom := DrawRect.Top + FCheckHeight;
case AState of
cbChecked:
DrawState := DFCS_BUTTONCHECK or DFCS_CHECKED;
cbUnchecked:
DrawState := DFCS_BUTTONCHECK;
else // cbGrayed
DrawState := DFCS_BUTTON3STATE or DFCS_CHECKED;
end;
if not AEnabled then
DrawState := DrawState or DFCS_INACTIVE;
with Canvas do
begin
if Flat then
begin
{ Remember current clipping region }
SaveRgn := CreateRectRgn(0,0,0,0);
GetClipRgn(Handle, SaveRgn);
{ Clip 3d-style checkbox to prevent flicker }
with DrawRect do
Rgn := CreateRectRgn(Left + 2, Top + 2, Right - 2, Bottom - 2);
SelectClipRgn(Handle, Rgn);
DeleteObject(Rgn);
end;
DrawFrameControl(Handle, DrawRect, DFC_BUTTON, DrawState);
if Flat then
begin
SelectClipRgn(Handle, SaveRgn);
DeleteObject(SaveRgn);
{ Draw flat rectangle in-place of clipped 3d checkbox above }
OldBrushStyle := Brush.Style;
OldBrushColor := Brush.Color;
OldPenColor := Pen.Color;
Brush.Style := bsClear;
Pen.Color := clBtnShadow;
with DrawRect do
Rectangle(Left + 1, Top + 1, Right - 1, Bottom - 1);
Brush.Style := OldBrushStyle;
Brush.Color := OldBrushColor;
Pen.Color := OldPenColor;
end;
end;
end;
*)
procedure TCheckStateListBox.SetChecked(Index: Integer; Checked: Boolean);
begin
if Checked <> GetChecked(Index) then
begin
TCheckStateListBoxDataWrapper(GetWrapper(Index)).SetChecked(Checked);
InvalidateCheck(Index);
end;
end;
procedure TCheckStateListBox.SetItemEnabled(Index: Integer; const Value: Boolean);
begin
if Value <> GetItemEnabled(Index) then
begin
TCheckStateListBoxDataWrapper(GetWrapper(Index)).Disabled := not Value;
InvalidateCheck(Index);
end;
end;
procedure TCheckStateListBox.SetCheckedState(Index: Integer; AState: TCheckBoxState);
begin
if AState <> GetCheckedState(Index) then
begin
TCheckStateListBoxDataWrapper(GetWrapper(Index)).CheckedState := AState;
InvalidateCheck(Index);
end;
end;
procedure TCheckStateListBox.InvalidateCheck(Index: Integer);
var
R: TRect;
begin
R := ItemRect(Index);
if not UseRightToLeftAlignment then
R.Right := R.Left + GetCheckWidth
else
R.Left := R.Right - GetCheckWidth;
InvalidateRect(Handle, @R, not (csOpaque in ControlStyle));
UpdateWindow(Handle);
end;
function TCheckStateListBox.GetChecked(Index: Integer): Boolean;
begin
if HaveWrapper(Index) then
Result := TCheckStateListBoxDataWrapper(GetWrapper(Index)).GetChecked
else
Result := False;
end;
function TCheckStateListBox.GetItemEnabled(Index: Integer): Boolean;
begin
if HaveWrapper(Index) then
Result := not TCheckStateListBoxDataWrapper(GetWrapper(Index)).Disabled
else
Result := True;
end;
function TCheckStateListBox.GetCheckedState(Index: Integer): TCheckBoxState;
begin
if HaveWrapper(Index) then
Result := TCheckStateListBoxDataWrapper(GetWrapper(Index)).CheckedState
else
Result := TCheckStateListBoxDataWrapper.GetDefaultCheckedState;
end;
procedure TCheckStateListBox.KeyPress(var Key: Char);
begin
inherited;
if (Key = ' ') then ToggleClickCheck(ItemIndex);
end;
procedure TCheckStateListBox.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
var
Index: Integer;
W : Integer;
begin
inherited;
if Button = mbLeft then
begin
Index := ItemAtPos(Point(X,Y),True);
if (Index <> -1) and GetItemEnabled(Index) then
if not UseRightToLeftAlignment then
begin
W := X - ItemRect(Index).Left;
if W < GetCheckWidth then
ToggleClickCheck(Index)
else if (GetCheckWidth < W) and (W < (GetCheckWidth + GetStateWidth)) then
ClickState;
end
else
begin
Dec(X, ItemRect(Index).Right - GetCheckWidth - GetStateWidth);
if (X > 0) and (X < GetCheckWidth) then
ToggleClickCheck(Index)
end;
end;
end;
procedure TCheckStateListBox.ToggleClickCheck;
var
CheckedState: TCheckBoxState;
begin
if (Index >= 0) and (Index < Items.Count) and GetItemEnabled(Index) then
begin
CheckedState := Self.CheckedState[Index];
case CheckedState of
cbUnchecked:
if AllowGrayed then CheckedState := cbGrayed else CheckedState := cbChecked;
cbChecked: CheckedState := cbUnchecked;
cbGrayed: CheckedState := cbChecked;
end;
Self.CheckedState[Index] := CheckedState;
ClickCheck;
end;
end;
procedure TCheckStateListBox.ClickCheck;
begin
if Assigned(FOnClickCheck) then FOnClickCheck(Self);
end;
function TCheckStateListBox.GetItemData(Index: Integer): LongInt;
begin
Result := 0;
if HaveWrapper(Index) then
Result := TCheckStateListBoxDataWrapper(GetWrapper(Index)).FData;
end;
function TCheckStateListBox.GetWrapper(Index: Integer): TObject;
begin
Result := ExtractWrapper(Index);
if Result = nil then
Result := CreateWrapper(Index);
end;
function TCheckStateListBox.ExtractWrapper(Index: Integer): TObject;
begin
Result := TCheckStateListBoxDataWrapper(inherited GetItemData(Index));
if LB_ERR = Integer(Result) then
raise EListError.CreateResFmt(@SListIndexError, [Index]);
if (Result <> nil) and (not (Result is TCheckStateListBoxDataWrapper)) then
Result := nil;
end;
function TCheckStateListBox.InternalGetItemData(Index: Integer): LongInt;
begin
Result := inherited GetItemData(Index);
end;
procedure TCheckStateListBox.InternalSetItemData(Index: Integer; AData: LongInt);
begin
inherited SetItemData(Index, AData);
end;
function TCheckStateListBox.CreateWrapper(Index: Integer): TObject;
begin
Result := TCheckStateListBoxDataWrapper.Create;
inherited SetItemData(Index, LongInt(Result));
end;
function TCheckStateListBox.HaveWrapper(Index: Integer): Boolean;
begin
Result := ExtractWrapper(Index) <> nil;
end;
procedure TCheckStateListBox.SetItemData(Index: Integer; AData: LongInt);
var
Wrapper: TCheckStateListBoxDataWrapper;
SaveState: TObject;
begin
Wrapper := TCheckStateListBoxDataWrapper(GetWrapper(Index));
Wrapper.FData := AData;
if FSaveStates <> nil then
if FSaveStates.Count > 0 then
begin
SaveState := FSaveStates[0];
Wrapper.FCheckedState := GetSaveState(SaveState);
Wrapper.FDisabled := GetSaveDisabled(SaveState);
FSaveStates.Delete(0);
end;
end;
procedure TCheckStateListBox.ResetContent;
var
I: Integer;
begin
for I := 0 to Items.Count - 1 do
if HaveWrapper(I) then
GetWrapper(I).Free;
inherited;
end;
procedure TCheckStateListBox.DeleteString(Index: Integer);
begin
if HaveWrapper(Index) then
GetWrapper(Index).Free;
inherited;
end;
procedure TCheckStateListBox.SetFlat(Value: Boolean);
begin
if Value <> FFlat then
begin
FFlat := Value;
Invalidate;
end;
end;
procedure TCheckStateListBox.WMDestroy(var Msg: TWMDestroy);
var
i: Integer;
begin
for i := 0 to Items.Count -1 do
ExtractWrapper(i).Free;
inherited;
end;
function TCheckStateListBox.GetState(Index: Integer): Integer;
begin
if HaveWrapper(Index) then
Result := TCheckStateListBoxDataWrapper(GetWrapper(Index)).State
else
Result := -1;
end;
procedure TCheckStateListBox.SetState(Index: Integer;
const Value: Integer);
begin
if Value <> GetState(Index) then
begin
TCheckStateListBoxDataWrapper(GetWrapper(Index)).State := Value;
InvalidateState(Index);
end;
end;
procedure TCheckStateListBox.InvalidateState(Index: Integer);
var
R: TRect;
begin
R := ItemRect(Index);
if not UseRightToLeftAlignment then
R.Right := R.Left + GetCheckWidth + GetStateWidth
else
R.Left := R.Right - GetCheckWidth - GetStateWidth;
InvalidateRect(Handle, @R, not (csOpaque in ControlStyle));
UpdateWindow(Handle);
end;
procedure TCheckStateListBox.SetStateImages(Value: TCustomImageList);
begin
if StateImages <> nil then
StateImages.UnRegisterChanges(FStateChangeLink);
FStateImages := Value;
if StateImages <> nil then
begin
StateImages.RegisterChanges(FStateChangeLink);
StateImages.FreeNotification(Self);
end
else
begin
ShowState := False;
end;
end;
procedure TCheckStateListBox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then
begin
if AComponent = StateImages then StateImages := nil;
end;
end;
procedure TCheckStateListBox.ImageListChange(Sender: TObject);
begin
Invalidate;
end;
procedure TCheckStateListBox.SetShowState(const Value: Boolean);
begin
if Value <> fShowState then
begin
if Value and not Assigned(fStateImages) then Exit;
fShowState := Value;
Invalidate;
end;
end;
function TCheckStateListBox.GetStateWidth: Integer;
begin
Result := 0;
if ShowState then begin
if Assigned(fStateImages) then
Result := FStateImages.Width
else
Result := 16;
Inc(Result, 2);
Result := Min(Result, ItemHeight);
end;
end;
procedure TCheckStateListBox.DrawItemState(R: TRect; Index : Integer);
var
DrawRect: TRect;
// BMP : TBitmap;
begin
if not ShowState then Exit;
DrawRect.Left := R.Left + ((R.Right - R.Left - FCheckWidth - ItemHeight) div 2) + GetCheckWidth - 1;
DrawRect.Top := R.Top + (R.Bottom - R.Top - FCheckWidth) div 2;
DrawRect.Right := DrawRect.Left + ItemHeight;
DrawRect.Bottom := DrawRect.Top + ItemHeight;
Canvas.FillRect(DrawRect);
FStateImages.Draw(Canvas, DrawRect.Left, DrawRect.Top, Index);
{
BMP := TBitmap.Create;
try
BMP.Width := FStateImages.Width;
BMP.Height := FStateImages.Height;
BMP.Transparent := True;
FStateImages.GetBitmap(Index, BMP);
Canvas.StretchDraw(DrawRect, BMP);
finally
BMP.Free;
end;
}
end;
function TCheckStateListBox.GetStateHeight: Integer;
begin
Result := 0;
if ShowState then begin
if Assigned(fStateImages) then
Result := FStateImages.Height
else
Result := 16;
Inc(Result, 2);
Result := Min(Result, ItemHeight);
end;
end;
procedure TCheckStateListBox.ClickState;
begin
if Assigned(FOnClickState) then FOnClickState(Self);
end;
procedure TCheckStateListBox.MouseMove(Shift: TShiftState; X, Y: Integer);
var
Index: Integer;
W : Integer;
CheckedState: TCheckBoxState;
tmpHint : string;
begin
inherited;
tmpHint := fSavedHint;
Index := ItemAtPos(Point(X,Y),True);
if (Index <> -1) and GetItemEnabled(Index) then
if not UseRightToLeftAlignment then
begin
W := X - ItemRect(Index).Left;
if W < GetCheckWidth then
begin
CheckedState := Self.CheckedState[Index];
DoGetCheckHint(CheckedState, tmpHint);
end
else if (GetCheckWidth < W) and (W < (GetCheckWidth + GetStateWidth)) then
begin
DoGetStateHint(State[Index], tmpHint);
end;
end
else
begin
Dec(X, ItemRect(Index).Right - GetCheckWidth - GetStateWidth);
if (X > 0) and (X < GetCheckWidth) then
begin
CheckedState := Self.CheckedState[Index];
DoGetCheckHint(CheckedState, tmpHint);
end;
end;
if tmpHint <> Hint then
begin
Hint := tmpHint;
Application.ActivateHint(Mouse.CursorPos);
end;
end;
procedure TCheckStateListBox.DoGetStateHint(State: Integer;
var Hint: string);
begin
if Assigned(FOnGetStateHint) then
FOnGetStateHint(Self, State, Hint);
end;
procedure TCheckStateListBox.DoGetCheckHint(State: TCheckBoxState;
var Hint: string);
begin
if Assigned(FOnGetCheckHint) then
FOnGetCheckHint(Self, State, Hint);
end;
procedure TCheckStateListBox.Loaded;
begin
inherited;
fSavedHint := Hint;
end;
function TCheckStateListBox.GetHeader(Index: Integer): Boolean;
begin
if HaveWrapper(Index) then
Result := TCheckStateListBoxDataWrapper(GetWrapper(Index)).Header
else
Result := False;
end;
procedure TCheckStateListBox.SetHeader(Index: Integer;
const Value: Boolean);
begin
if Value <> GetHeader(Index) then
begin
TCheckStateListBoxDataWrapper(GetWrapper(Index)).Header := Value;
InvalidateCheck(Index);
end;
end;
procedure TCheckStateListBox.SetHeaderBackgroundColor(const Value: TColor);
begin
if Value <> HeaderBackgroundColor then
begin
FHeaderBackgroundColor := Value;
Invalidate;
end;
end;
procedure TCheckStateListBox.SetHeaderColor(const Value: TColor);
begin
if Value <> HeaderColor then
begin
FHeaderColor := Value;
Invalidate;
end;
end;
initialization
GetCheckSize;
end.