-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnhListView.pas
2572 lines (2345 loc) · 79.7 KB
/
EnhListView.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
995
996
997
998
999
1000
{$I DFS.INC} { Standard defines for all Delphi Free Stuff components }
// Delphi 2 and C++B 1 have incorrectly declared InsertItem as private.
{$IFDEF DFS_COMPILER_3_UP}
{$DEFINE DFS_FIXED_LIST_VIEW}
{$ENDIF}
{.$DEFINE DFS_DEBUG}
{$J+} {$WRITEABLECONST ON}
{------------------------------------------------------------------------------}
{ TdfsEnhListView v3.72 }
{------------------------------------------------------------------------------}
{ A list view control that provides enhanced functionality beyond the }
{ standard list view. For example, automatic sorting of simple data types, }
{ owner draw event for vsReport mode, and more. This does NOT require any }
{ special version of COMCTL32.DLL. }
{ }
{ Copyright 1998-2001, Brad Stowers. All Rights Reserved. }
{ }
{ Copyright: }
{ All Delphi Free Stuff (hereafter "DFS") source code is copyrighted by }
{ Bradley D. Stowers (hereafter "author"), and shall remain the exclusive }
{ property of the author. }
{ }
{ Distribution Rights: }
{ You are granted a non-exlusive, royalty-free right to produce and distribute }
{ compiled binary files (executables, DLLs, etc.) that are built with any of }
{ the DFS source code unless specifically stated otherwise. }
{ You are further granted permission to redistribute any of the DFS source }
{ code in source code form, provided that the original archive as found on the }
{ DFS web site (http://www.delphifreestuff.com) is distributed unmodified. For }
{ example, if you create a descendant of TDFSColorButton, you must include in }
{ the distribution package the colorbtn.zip file in the exact form that you }
{ downloaded it from http://www.delphifreestuff.com/mine/files/colorbtn.zip. }
{ }
{ Restrictions: }
{ Without the express written consent of the author, you may not: }
{ * Distribute modified versions of any DFS source code by itself. You must }
{ include the original archive as you found it at the DFS site. }
{ * Sell or lease any portion of DFS source code. You are, of course, free }
{ to sell any of your own original code that works with, enhances, etc. }
{ DFS source code. }
{ * Distribute DFS source code for profit. }
{ }
{ Warranty: }
{ There is absolutely no warranty of any kind whatsoever with any of the DFS }
{ source code (hereafter "software"). The software is provided to you "AS-IS", }
{ and all risks and losses associated with it's use are assumed by you. In no }
{ event shall the author of the softare, Bradley D. Stowers, be held }
{ accountable for any damages or losses that may occur from use or misuse of }
{ the software. }
{ }
{ Support: }
{ Support is provided via the DFS Support Forum, which is a web-based message }
{ system. You can find it at http://www.delphifreestuff.com/discus/ }
{ All DFS source code is provided free of charge. As such, I can not guarantee }
{ any support whatsoever. While I do try to answer all questions that I }
{ receive, and address all problems that are reported to me, you must }
{ understand that I simply can not guarantee that this will always be so. }
{ }
{ Clarifications: }
{ If you need any further information, please feel free to contact me directly.}
{ This agreement can be found online at my site in the "Miscellaneous" section.}
{------------------------------------------------------------------------------}
{ The lateset version of my components are always available on the web at: }
{ http://www.delphifreestuff.com/ }
{ See ELV.txt for notes, known issues, and revision history. }
{------------------------------------------------------------------------------}
{ Date last modified: June 28, 2001 }
{------------------------------------------------------------------------------}
unit EnhListView;
interface
{$IFNDEF DFS_WIN32}
ERROR! This unit only available for Delphi 2.0 or higher!!!
{$ENDIF}
uses
Forms, Windows, Messages, Classes, Controls, ComCtrls, CommCtrl, SysUtils,
{$IFDEF DFS_COMPILER_4_UP} ImgList, {$ENDIF} Graphics, StdCtrls, Menus;
const
{ This shuts up C++Builder 3 about the redefiniton being different. There
seems to be no equivalent in C1. Sorry. }
{$IFDEF DFS_CPPB_3_UP}
{$EXTERNALSYM DFS_COMPONENT_VERSION}
{$ENDIF}
DFS_COMPONENT_VERSION = 'TdfsEnhListView v3.72';
DRAWTEXTEX_FLAGS = DT_NOPREFIX or DT_SINGLELINE or DT_VCENTER or
DT_END_ELLIPSIS;
DRAWTEXTEX_ALIGNMENT: array[TAlignment] of UINT = (DT_LEFT, DT_RIGHT,
DT_CENTER);
WM_OWNERDRAWCOLUMNS = WM_USER + 143;
type
TIntArray = array[0..(MaxInt div SizeOf(Integer)-1)] of Integer;
PIntArray = ^TIntArray;
TResizeMethod = (rmFitText, rmFitHeader);
TAutoColumnSort = (acsNoSort, acsSort, acsSortToggle);
TAutoSortStyle = (assSmart, assDefault);
TSortAs = (saNone, saString, saNumeric, saDateTime);
TLVStyle = (lvStandard, lvOwnerDrawFixed);
TLVHDrawItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index: Integer; var ARect: TRect; Selected: boolean;
var DefaultDrawing: boolean) of object;
TLVMeasureItemEvent = procedure(Control: TWinControl;
var AHeight: UINT) of object;
TLVDrawItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index: Integer; ARect: TRect; State: TOwnerDrawState;
var DefaultDrawing, FullRowSelect: boolean) of object;
TLVDrawSubItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index, SubItem: Integer; ARect: TRect; State: TOwnerDrawState;
var DefaultDrawing: boolean) of object;
TLVAfterDrawItemEvent = procedure(Control: TWinControl; var ACanvas: TCanvas;
Index: Integer; ARect: TRect; State: TOwnerDrawState) of object;
TLVSortItemsEvent = procedure(Sender: TObject; Item1, Item2: TListItem;
SortColumn: integer; var SortAs: TSortAs; var CompResult: integer) of object;
TLVSortStatusEvent = procedure(Sender: TObject; SortColumn: integer;
Ascending: boolean) of object;
TLVEditCanceled = procedure(Sender: TObject; Item: TListItem) of object;
{$IFNDEF DFS_COMPILER_4_UP}
TLVNotifyEvent = procedure(Sender: TObject; Item: TListItem) of object;
{$ENDIF}
// Class for saved settings
TdfsEnhLVSaveSettings = class(TPersistent)
private
FAutoSave: boolean;
FRegistryKey: string;
FSaveColumnSizes: boolean;
FSaveCurrentSort: boolean;
FSaveViewStyle: boolean;
public
constructor Create; virtual;
procedure StoreColumnSizes(ColCount: integer;
const IntArray: array of integer);
procedure ReadColumnSizes(ColCount: integer;
var IntArray: array of integer);
procedure StoreCurrentSort(Ascending: boolean; SortCol: integer);
procedure ReadCurrentSort(var Ascending: boolean; var SortCol: integer);
procedure StoreViewStyle(Style: TViewStyle);
function ReadViewStyle(Default: TViewStyle): TViewStyle;
published
property AutoSave: boolean read FAutoSave write FAutoSave default FALSE;
property RegistryKey: string read FRegistryKey write FRegistryKey;
property SaveColumnSizes: boolean
read FSaveColumnSizes
write FSaveColumnSizes
default TRUE;
property SaveCurrentSort: boolean
read FSaveCurrentSort
write FSaveCurrentSort
default TRUE;
property SaveViewStyle: boolean
read FSaveViewStyle
write FSaveViewStyle
default TRUE;
end;
{ The new class }
TCustomEnhListView = class(TCustomListView)
private
FSortDirty: boolean;
FUpdateCount: integer;
FStyle: TLVStyle;
FAutoColumnSort: TAutoColumnSort;
FAutoSortStyle: TAutoSortStyle;
FAutoResort: boolean;
FAutoSortAscending: boolean;
FTmpAutoSortAscending: boolean;
FLastColumnClicked: Integer;
FSaveSettings: TdfsEnhLVSaveSettings;
FShowSortArrows: boolean;
FReverseSortArrows: boolean;
FSortUpBmp,
FSortDownBmp: TBitmap;
FCreatingWindowHandle: boolean;
{$IFDEF BACKGROUND_FIXED}
FBackgroundImage: TBitmap;
{$ENDIF}
FNoColumnResize: boolean;
FOldHeaderWndProc: pointer;
FHeaderInstance: pointer;
FSearchStr: string;
FSearchTickCount: Double;
FColumnSearch: boolean;
FOnSortBegin: TLVSortStatusEvent;
FOnSortFinished: TLVSortStatusEvent;
FOnMeasureItem: TLVMeasureItemEvent;
FOnDrawItem: TLVDrawItemEvent;
FOnDrawSubItem: TLVDrawSubItemEvent;
FOnAfterDefaultDrawItem: TLVAfterDrawItemEvent;
FOnDrawHeader: TLVHDrawItemEvent;
FOnSortItems: TLVSortItemsEvent;
FOnEditCanceled: TLVEditCanceled;
{$IFNDEF DFS_COMPILER_4_UP}
FOnGetImageIndex: TLVNotifyEvent;
{$ENDIF}
procedure HeaderWndProc(var Message: TMessage);
{ Message handlers }
procedure CMSysColorChange(var Message: TWMSysColorChange);
message CM_SYSCOLORCHANGE;
procedure CMFontChanged(var Messsage: TMessage); message CM_FONTCHANGED;
procedure CNMeasureItem(var Message: TWMMeasureItem); message CN_MEASUREITEM;
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure WMDestroy(var Message: TWMDestroy); message WM_DESTROY;
procedure WMDrawHeader(var Message: TWMDrawItem); message WM_DRAWITEM;
procedure WMNotify(var Message: TWMNotify); message WM_NOTIFY;
procedure WMOwnerDrawColumns(var Message: TMessage);
message WM_OWNERDRAWCOLUMNS;
procedure WMParentNotify(var Message: TWMParentNotify); message WM_PARENTNOTIFY;
protected
{ USE WITH CARE. This can be NIL }
FCanvas: TCanvas;
FHeaderHandle: HWND;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
{$IFNDEF DFS_COMPILER_4_UP}
function GetItem(Value: TLVItem): TListItem;
{$ENDIF}
procedure ResetOwnerDrawHeight;
procedure InvalidateColumnHeader(Index: integer); virtual;
procedure DoSort(ColumnIndex:integer; Descending: boolean); virtual;
procedure SortBegin(ColumnIndex: integer; Ascending: boolean); virtual;
procedure SortFinished(ColumnIndex: integer; Ascending: boolean); virtual;
procedure SortItems(const Item1, Item2: TListItem; SortColumn: integer;
var CompResult: integer); virtual;
procedure MeasureItem(var Height: UINT); virtual;
procedure DefaultDrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState; FullRowSelect: boolean); virtual;
procedure DefaultDrawSubItem(Index, SubItem: integer; Rect: TRect;
State: TOwnerDrawState); virtual;
procedure ProcessDrawItemMsg(Index: Integer;
Rect: TRect; State: TOwnerDrawState; var DefaultDrawing,
FullRowSelect: boolean); virtual;
function ActualColumnIndex(Index: integer): integer; virtual;
function GetActualColumn(Index: integer): TListColumn; virtual;
function GetSubItemText(Index, SubItem: integer): string; virtual;
procedure DrawSubItem(Index, SubItem: Integer; Rect: TRect;
State: TOwnerDrawState; var DefaultDrawing: boolean); virtual;
procedure DrawItem(var Canvas: TCanvas; Index: Integer; Rect: TRect;
State: TOwnerDrawState; var DefaultDrawing,
FullRowSelect: boolean);
{$IFDEF DFS_COMPILER_4_UP} reintroduce; overload; {$ENDIF} virtual;
procedure AfterDrawItem(var Canvas: TCanvas; Index: Integer;
Rect: TRect; State: TOwnerDrawState); virtual;
procedure Edit(const Item: TLVItem); override;
procedure EditCanceled(const Item: TLVItem); virtual;
{ Overriden ancestor methods }
procedure ColClick(Column: TListColumn); override;
{$IFDEF DFS_FIXED_LIST_VIEW}
procedure InsertItem(Item: TListItem); override;
{$ENDIF}
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure CreateWnd; override;
procedure DestroyWnd; override;
procedure ProcessDrawHeaderMsg(Index: Integer; Rect: TRect;
State: TOwnerDrawState; var DefaultDrawing: boolean); virtual;
procedure DrawHeader(var Canvas: TCanvas; Index: Integer; var Rect: TRect;
Selected: boolean; var DefaultDrawing: boolean); virtual;
procedure DefaultDrawHeader(var Canvas: TCanvas; Index: Integer;
var Rect: TRect; Selected: boolean); virtual;
procedure SetOnDrawHeader(Value: TLVHDrawItemEvent); virtual;
procedure SetColumnsOwnerDrawFlag(OwnerDrawn: boolean); virtual;
procedure CreateSortBmps(var UpBmp, DownBmp: TBitmap); virtual;
{$IFNDEF DFS_COMPILER_4_UP}
procedure GetImageIndex(Item: TListItem); virtual;
{$ENDIF}
{$IFDEF BACKGROUND_FIXED}
procedure BackgroundImageChanged(Sender: TObject); virtual;
{$ENDIF}
{ Property methods }
procedure SetAutoColumnSort(Value: TAutoColumnSort);
procedure SetAutoSortStyle(Value: TAutoSortStyle);
procedure SetCurrentSortAscending(Value: boolean);
procedure SetAutoSortAscending(Value: boolean);
procedure SetStyle(Value: TLVStyle);
procedure SetShowSortArrows(Value: boolean);
procedure SetReverseSortArrows(Value: boolean);
procedure SetLastColumnClicked(Value: integer);
procedure SetAutoResort(Value: boolean);
{$IFDEF BACKGROUND_FIXED}
procedure SetBackgroundImage(const Value: TBitmap);
{$ENDIF}
function GetSmallImages: {$IFDEF DFS_COMPILER_4_UP} TCustomImageList {$ELSE}
TImageList {$ENDIF};
procedure SetSmallImages(Val: {$IFDEF DFS_COMPILER_4_UP} TCustomImageList
{$ELSE} TImageList {$ENDIF});
function GetVersion: string; virtual;
procedure SetVersion(const Val: string);
function GetCurrentColumnWidth(Index: integer): integer;
procedure CreateParams(var Params: TCreateParams); override;
procedure Loaded; override;
{ Should probably remain protected }
property SortUpBmp: TBitmap
read FSortUpBmp;
property SortDownBmp: TBitmap
read FSortDownBmp;
{ Should be made public by descendants as needed }
property LastColumnClicked: Integer
read FLastColumnClicked
write SetLastColumnClicked;
{ Should be published by descendants as needed }
property HeaderHandle: HWnd
read FHeaderHandle;
property AutoColumnSort: TAutoColumnSort
read FAutoColumnSort
write SetAutoColumnSort
default acsNoSort;
property AutoSortStyle: TAutoSortStyle
read FAutoSortStyle
write SetAutoSortStyle
default assSmart;
property AutoResort: boolean
read FAutoResort
write SetAutoResort
default TRUE;
property AutoSortAscending: boolean
read FAutoSortAscending
write SetAutoSortAscending
default TRUE;
property ColumnSearch: boolean
read FColumnSearch
write FColumnSearch
default FALSE;
property ShowSortArrows: boolean
read FShowSortArrows
write SetShowSortArrows
default FALSE;
property ReverseSortArrows: boolean
read FReverseSortArrows
write SetReverseSortArrows
default FALSE;
property CurrentSortAscending: boolean
read FTmpAutoSortAscending
write SetCurrentSortAscending;
property SaveSettings: TdfsEnhLVSaveSettings
read FSaveSettings
write FSaveSettings;
property Style: TLVStyle
read FStyle
write SetStyle
default lvStandard;
property CurrentColumnWidth[Index: integer]: integer
read GetCurrentColumnWidth;
{$IFDEF BACKGROUND_FIXED}
property BackgroundImage: TBitmap
read FBackgroundImage
write SetBackgroundImage;
{$ENDIF}
property NoColumnResize: boolean
read FNoColumnResize
write FNoColumnResize;
// We have to redeclare this so we can hook into the read/write methods.
property SmallImages:
{$IFDEF DFS_COMPILER_4_UP} TCustomImageList {$ELSE} TImageList {$ENDIF}
read GetSmallImages
write SetSmallImages;
{ Events }
property OnDrawHeader: TLVHDrawItemEvent
read FOnDrawHeader
write SetOnDrawHeader;
property OnMeasureItem: TLVMeasureItemEvent
read FOnMeasureItem
write FOnMeasureItem;
property OnDrawItem: TLVDrawItemEvent
read FOnDrawItem
write FOnDrawItem;
property OnDrawSubItem: TLVDrawSubItemEvent
read FOnDrawSubItem
write FOnDrawSubItem;
property OnAfterDefaultDrawItem: TLVAfterDrawItemEvent
read FOnAfterDefaultDrawItem
write FOnAfterDefaultDrawItem;
property OnSortItems: TLVSortItemsEvent
read FOnSortItems
write FOnSortItems;
property OnSortBegin: TLVSortStatusEvent
read FOnSortBegin
write FOnSortBegin;
property OnSortFinished: TLVSortStatusEvent
read FOnSortFinished
write FOnSortFinished;
property OnEditCanceled: TLVEditCanceled
read FOnEditCanceled
write FOnEditCanceled;
{$IFNDEF DFS_COMPILER_4_UP}
property OnGetImageIndex: TLVNotifyEvent
read FOnGetImageIndex
write FOnGetImageIndex;
{$ENDIF}
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function StoreSettings: boolean; virtual;
function WriteSettings: boolean; virtual;
function LoadSettings: boolean; virtual;
function ReadSettings: boolean; virtual;
procedure DefaultSort(ColumnIndex:integer; Descending: boolean); virtual;
procedure Resort; virtual;
// Use these as replacements for Items.BeginUpdate and EndUpdate. They
// call those methods, but they also inhibit autosorting until after the
// last EndUpdate.
procedure BeginUpdate; virtual;
procedure EndUpdate; virtual;
// Resize all columns.
procedure ResizeColumns(ResizeMethod: TResizeMethod); virtual;
// Move list item to new position.
procedure MoveItem(OriginalIndex, NewIndex: Integer); virtual;
function StringSelect(FindStr: string; ColumnIndex: Integer): boolean; virtual;
function SubStringSelect(FindStr: string; ColumnIndex: Integer): boolean; virtual;
// Accounts for re-ordered columns
property ActualColumn[Index: integer]: TListColumn
read GetActualColumn;
published
property Version: string
read GetVersion
write SetVersion
stored FALSE;
end;
TdfsEnhListView = class(TCustomEnhListView)
public
property HeaderHandle;
property CurrentSortAscending;
property LastColumnClicked;
property CurrentColumnWidth;
published
property AutoColumnSort;
property AutoSortStyle;
property AutoResort;
property AutoSortAscending;
{$IFDEF BACKGROUND_FIXED}
property BackgroundImage;
{$ENDIF}
property ColumnSearch;
property NoColumnResize;
property ReverseSortArrows;
property ShowSortArrows;
property SaveSettings;
property Style;
property OnMeasureItem;
property OnDrawItem;
property OnDrawSubItem;
property OnAfterDefaultDrawItem;
property OnDrawHeader;
property OnSortItems;
property OnSortBegin;
property OnSortFinished;
property OnEditCanceled;
{ Publish TCustomListView inherited protected properties }
property Align;
{$IFDEF DFS_COMPILER_4_UP}
property Anchors;
property BiDiMode;
{$ENDIF}
property BorderStyle;
{$IFDEF DFS_COMPILER_4_UP}
property BorderWidth;
{$ENDIF}
property Color;
property ColumnClick;
property OnClick;
property OnDblClick;
property Columns;
{$IFDEF DFS_COMPILER_4_UP}
property Constraints;
{$ENDIF}
property Ctl3D;
{$IFDEF DFS_COMPILER_4_UP}
property DragKind;
{$ENDIF}
property DragMode;
property ReadOnly
default False;
property Enabled;
property Font;
property HideSelection;
property IconOptions;
property Items;
property AllocBy;
property MultiSelect;
property OnChange;
property OnChanging;
property OnColumnClick;
property OnDeletion;
property OnEdited;
property OnEditing;
{$IFDEF DFS_COMPILER_4_UP}
property OnEndDock;
{$ENDIF}
property OnEnter;
property OnExit;
property OnInsert;
property OnDragDrop;
property OnDragOver;
property DragCursor;
property OnStartDrag;
property OnEndDrag;
property OnGetImageIndex;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
{$IFDEF DFS_COMPILER_4_UP}
property OnResize;
property OnSelectItem;
property OnStartDock;
{$ENDIF}
property ParentColor
default False;
property ParentFont;
property ParentShowHint;
{$IFDEF DFS_COMPILER_4_UP}
property ParentBiDiMode;
{$ENDIF}
property ShowHint;
property PopupMenu;
property ShowColumnHeaders;
property TabOrder;
property TabStop
default True;
property ViewStyle;
property Visible;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property LargeImages;
property SmallImages;
property StateImages;
end;
var
{ Default drawing variables }
DefDraw_TextOffset: integer; // Offset for the text -- 5
DefDraw_ImageOffset: integer; // Offset for image -- 2
implementation
uses
Registry, ExtListView;
var
FDirection,
FSortColNum: integer;
{$IFNDEF DFS_COMPILER_4_UP}
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
var
SearchStr, Patt, NewStr: string;
Offset: Integer;
begin
if rfIgnoreCase in Flags then
begin
SearchStr := AnsiUpperCase(S);
Patt := AnsiUpperCase(OldPattern);
end else
begin
SearchStr := S;
Patt := OldPattern;
end;
NewStr := S;
Result := '';
while SearchStr <> '' do
begin
Offset := {$IFDEF DFS_COMPILER_2} Pos( {$ELSE} AnsiPos( {$ENDIF} Patt, SearchStr);
if Offset = 0 then
begin
Result := Result + NewStr;
Break;
end;
Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);
if not (rfReplaceAll in Flags) then
begin
Result := Result + NewStr;
Break;
end;
SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
end;
end;
{$ENDIF}
function IsValidNumber(S: string; var V: extended): boolean;
var
NumCode: integer;
FirstSpace: integer;
begin
FirstSpace := Pos(' ', S);
if FirstSpace > 0 then
S := Copy(S, 1, FirstSpace - 1);
Val(S, V, NumCode);
Result := (NumCode = 0);
if not Result then
begin
// Remove all thousands seperators
S := StringReplace(S, FormatSettings.ThousandSeparator, '', [rfReplaceAll]);
// change DecimalSeperator to '.' because Val only recognizes that, not
// the locale specific decimal char. Stupid Val.
S := StringReplace(S, FormatSettings.DecimalSeparator, '.', [rfReplaceAll]);
// and try again
Val(S, V, NumCode);
Result := (NumCode = 0);
End;
end;
// date conversion will fail if using long format, e.g. '1 January 1994'
function IsValidDateTime(const S: string; var D: TDateTime): boolean;
var
i: integer;
HasDate: boolean;
HasTime: boolean;
begin
// Check for two date seperators. This is because some regions use a "-"
// to seperate dates, so if we just checked for one we would flag negative
// numbers as being dates.
i := Pos(FormatSettings.DateSeparator, S);
HasDate := i > 0;
if HasDate and (i <> Length(S)) then
HasDate := Pos(FormatSettings.DateSeparator, Copy(S, i+1, Length(S)-i)) > 0;
HasTime := Pos(FormatSettings.TimeSeparator, S) > 0;
Result := HasDate or HasTime;
if Result then
begin
try
if HasDate and HasTime then
D := StrToDateTime(S)
else if HasDate then
D := StrToDate(S)
else if HasTime then
D := StrToTime(S);
except
// Something failed to convert...
D := 0;
Result := FALSE;
end;
end;
end; { IsValidDateTime }
function __CustomSortProc1__(Item1, Item2: TListItem; Data: integer): integer;
stdcall;
var
Str1, Str2: string;
Val1, Val2: extended;
Date1, Date2: TDateTime;
Diff: TDateTime;
begin
if (Item1 = NIL) or (Item2 = NIL) then
begin
// something bad happening, I'm outta here
Result := 0;
exit;
end;
try
if FSortColNum = -1 then
begin
Str1 := Item1.Caption;
Str2 := Item2.Caption;
end else begin
if FSortColNum < Item1.SubItems.Count then
Str1 := Item1.SubItems[FSortColNum]
else
Str1 := '';
if FSortColNum < Item2.SubItems.Count then
Str2 := Item2.SubItems[FSortColNum]
else
Str2 := '';
end;
if TCustomEnhListView(Data).AutoSortStyle = assSmart then
begin
if IsValidDateTime(Str1, Date1) and IsValidDateTime(Str2, Date2) then
begin
Diff := Date1 - Date2;
if Diff < 0.0 then Result := -1
else if Diff > 0.0 then Result := 1
else Result := 0
end else if IsValidNumber(Str1, Val1) and IsValidNumber(Str2, Val2) then
begin
if Val1 < Val2 then Result := -1
else if Val1 > Val2 then Result := 1
else Result := 0
end else
Result := AnsiCompareStr(Str1, Str2);
end else
Result := AnsiCompareStr(Str1, Str2);
Result := FDirection * Result; // Set direction flag.
except
Result := 0; // Something went bad in the comparison. Say they are equal.
end;
end;
function __CustomSortProc2__(Item1, Item2: TListItem; Data: integer): integer;
stdcall;
var
EvRes: integer;
begin
EvRes := 0;
TCustomEnhListView(Data).SortItems(Item1, Item2, FSortColNum, EvRes);
Result := EvRes * FDirection;
end;
{ TdfsEnhLVSaveSettings }
constructor TdfsEnhLVSaveSettings.Create;
begin
inherited Create;
FAutoSave := FALSE;
FRegistryKey := '';
FSaveViewStyle := TRUE;
FSaveColumnSizes := TRUE;
SaveCurrentSort := TRUE;
end;
procedure TdfsEnhLVSaveSettings.StoreColumnSizes(ColCount: integer;
const IntArray: array of integer);
var
Reg: TRegIniFile;
x: integer;
s: string;
begin
if ColCount < 1 then exit;
s := '';
for x := 0 to ColCount-1 do
s := s + IntToStr(IntArray[x]) + ',';
SetLength(s, Length(s)-1);
Reg := TRegIniFile.Create(FRegistryKey);
try
Reg.WriteString('Columns', 'Sizes', s);
finally
Reg.Free;
end;
end;
procedure TdfsEnhLVSaveSettings.ReadColumnSizes(ColCount: integer;
var IntArray: array of integer);
var
Reg: TRegIniFile;
x,y: integer;
s: string;
begin
if ColCount < 1 then exit;
s := '';
Reg := TRegIniFile.Create(FRegistryKey);
try
s := Reg.ReadString('Columns', 'Sizes', '');
finally
Reg.Free;
end;
if s = '' then
begin
IntArray[0] := -1;
exit;
end;
y := 0;
for x := 0 to ColCount-1 do
begin
try
y := Pos(',', s);
if y = 0 then
y := Length(s)+1;
IntArray[x] := StrToInt(Copy(s, 1, y-1));
except
{ Nothing, just eat the exception };
end;
s := copy(s, y+1, length(s));
if s = '' then break;
end;
end;
procedure TdfsEnhLVSaveSettings.StoreCurrentSort(Ascending: boolean;
SortCol: integer);
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(FRegistryKey);
try
Reg.WriteBool('Sort', 'Ascending', Ascending);
Reg.WriteInteger('Sort', 'SortCol', SortCol);
finally
Reg.Free;
end;
end;
procedure TdfsEnhLVSaveSettings.ReadCurrentSort(var Ascending: boolean;
var SortCol: integer);
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(FRegistryKey);
try
Ascending := Reg.ReadBool('Sort', 'Ascending', TRUE);
SortCol := Reg.ReadInteger('Sort', 'SortCol', 0);
finally
Reg.Free;
end;
end;
procedure TdfsEnhLVSaveSettings.StoreViewStyle(Style: TViewStyle);
const
STYLE_VAL: array[TViewStyle] of integer = (0, 1, 2, 3);
var
Reg: TRegIniFile;
begin
Reg := TRegIniFile.Create(FRegistryKey);
try
Reg.WriteInteger('ViewStyle', 'ViewStyle', STYLE_VAL[Style]);
finally
Reg.Free;
end;
end;
function TdfsEnhLVSaveSettings.ReadViewStyle(Default: TViewStyle): TViewStyle;
const
STYLES: array[0..3] of TViewStyle = (vsIcon, vsSmallIcon, vsList, vsReport);
var
Reg: TRegIniFile;
i: integer;
begin
Reg := TRegIniFile.Create(FRegistryKey);
try
i := Reg.ReadInteger('ViewStyle', 'ViewStyle', -1);
if (i >= Low(STYLES)) and (i <= High(STYLES)) then
Result := STYLES[i]
else
Result := Default;
finally
Reg.Free;
end;
end;
// Override constructor to "zero out" our internal variable.
constructor TCustomEnhListView.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FSearchStr := '';
FSearchTickCount := 0;
FHeaderHandle := 0;
FSortDirty := FALSE;
FUpdateCount := 1; // inhibit sorting until finished creating.
FSaveSettings := TdfsEnhLVSaveSettings.Create;
FAutoColumnSort := acsNoSort;
FAutoResort := TRUE;
FAutoSortStyle := assSmart;
FAutoSortAscending := TRUE;
FTmpAutoSortAscending := FAutoSortAscending;
FLastColumnClicked := -1;
FCanvas := NIL;
FStyle := lvStandard;
FSortUpBmp := NIL;
FSortDownBmp := NIL;
FShowSortArrows := FALSE;
FReverseSortArrows := FALSE;
{$IFDEF BACKGROUND_FIXED}
FBackgroundImage := TBitmap.Create;
{$ENDIF}
FHeaderInstance := MakeObjectInstance(HeaderWndProc);
end;
destructor TCustomEnhListView.Destroy;
begin
{$IFDEF BACKGROUND_FIXED}
FBackgroundImage.Free;
{$ENDIF}
FSortUpBmp.Free;
FSortDownBmp.Free;
FCanvas.Free;
if FHeaderHandle <> 0 then
SetWindowLong(FHeaderHandle, GWL_WNDPROC, LongInt(FOldHeaderWndProc));
FreeObjectInstance(FHeaderInstance);
inherited Destroy;
FSaveSettings.Free;
end;
procedure TCustomEnhListView.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
if (FStyle = lvOwnerDrawFixed) then
begin
Params.Style := Params.Style or LVS_OWNERDRAWFIXED;
if FCanvas = NIL then
FCanvas := TCanvas.Create;
end else begin
if (not assigned(FOnDrawHeader)) and (not FShowSortArrows) then
begin
FCanvas.Free;
FCanvas := NIL;
end;
end;
end;
procedure TCustomEnhListView.CreateWnd;
begin
// if FCreatingWindowHandle then exit;
FCreatingWindowHandle := TRUE;
try
inherited CreateWnd;
// If we are loading object from stream (form file), we have to wait until
// everything is loaded before populating the list. If we are not loading,
// i.e. the component was created dynamically or was just dropped on a form,
// we need to reset the flag now.
if not (csLoading in ComponentState) then
FUpdateCount := 0;
// Something very bizarre happens in either TCustomListView or in the
// list view code itself in COMCTL32.DLL: The first WM_MEASUREITEM value
// is not honored if the listview has small images assigned to it. Instead
// the value is ignored and the height of the images are used. I found that
// by forcing Windows to ask for the item height a second time, it would
// honor the value then.
if Style = lvOwnerDrawFixed then
ResetOwnerDrawHeight;
finally
FCreatingWindowHandle := FALSE;
end;
end;
procedure TCustomEnhListView.Loaded;
begin
inherited Loaded;
{$IFDEF BACKGROUND_FIXED}
BackgroundImageChanged(Self);
{$ENDIF}
if not FCreatingWindowHandle then
HandleNeeded;
FUpdateCount := 0;
if (not LoadSettings) or (not SaveSettings.SaveCurrentSort) then
begin
if Columns.Count > 0 then
FLastColumnClicked := 0;
Resort;
end;
// Something flaky going on. Hard to explain, but this clears it up.
PostMessage(Handle, WM_OWNERDRAWCOLUMNS, 0, 0);
end;
procedure TCustomEnhListView.WMDestroy(var Message: TWMDestroy);
begin
StoreSettings;
inherited;
end;
function TCustomEnhListView.StoreSettings: boolean;
begin
if FSaveSettings.AutoSave and
(([csDesigning, csLoading, csReading] * ComponentState) = []) then
Result := WriteSettings
else
Result := FALSE;
end;
function TCustomEnhListView.WriteSettings: boolean;
var