diff --git a/docs/xml/lcl/shellctrls.xml b/docs/xml/lcl/shellctrls.xml index 5975f2e11b..4577ff2401 100644 --- a/docs/xml/lcl/shellctrls.xml +++ b/docs/xml/lcl/shellctrls.xml @@ -101,6 +101,8 @@ to implement the TCustomShellTreeView.FileSortType property. + + @@ -118,6 +120,14 @@ Items are sorted alphabetically with folders placed at the beginning of the list. + + +Items are sorted using a user-defined compare function for files and +directories. The sort routine should return a negative value if the first item +comes before the second item, a positive value when the first item comes after +the second item, or 0 (zero) when both items have the same value. + + Represents case sensitivity options for file masks. @@ -183,6 +193,231 @@ Clear children when a node is collapsed. + + +Provides information about a file or directory on the local file system for +use in file sort comparison routines. + + +

+TFileItem is a utility class used to represent a file or directory. +It contains BasePath with the path to the directory where the item is located +on the local file system, and FileInfo with the TSearchRec values for the item. +

+

+TFileItem is the type used to represent the items passed as arguments to a +TFileItemCompareEvent handler routine. TFileItem instances are created and +used in the implementation of file listing and sort routines as well. +

+
+ +Modified in LCL version 2.4. It was moved from the implementation section to +the interface section, and used to implement TFileItemCompareEvent routines. + + + + + + + + +
+ + + + + + + + +True when the item represents a folder on the local file system. + + +

+isFolder is a Boolean member which indicates if the item +represents a folder or directory on the local file system. Its value is +assigned in the Create constructor, and is set to True if the TSearchRec +instance in the DirInfo argument includes faDirectory in its Attr member. +

+
+ + + + +TSearchRec + +
+ + +True when the file item represents a folder on the local file system. + + + + + +Constructor for the class instance. + + +

+Create stores values in the DirInfo and ABasePath arguments to the +FileInfo and BasePath properties in the class instance. isFolder is updated to +indicate whether the file item is a directory on the local file system. +

+
+ + + + + +
+ + +TSearchRec instance with the directory information for the file item. + + + + +Contains the path on the local file system where the file item is stored. + + + + + +Path to the directory where the file item is stored on the local file system. + + +

+BasePath is assigned in the Create constructor using the value +passed in the ABasePath argument. +

+
+ + + + + +
+ + + +TSearchRec instance with information about the file or directory in the item. + + +

+FileInfo is assigned in the Create constructor using the value +passed in the DirInfo argument. +

+
+ + + + +
+ + + +Implements a handler routine used to compare values in a custom file sort +for TCustomShellTreeView. + + +

+TFileItemCompareEvent is the type used to implement the +OnSortCompare event handler in TCustomShellTreeView. TFileItemCompareEvent +compares the file items specified in the Item1 and Item2 arguments to +determine which value occurs first in the sort order. +

+

+The return value is an Integer with the relative sort order for the +compared values: +

+
+
A negative value (<0)
+
Indicates that Item1 occurs before Item2 in the sort order.
+
A positive value (>0)
+
Indicates that Item1 occurs after Item2 in the sort order.
+
0 (zero)
+
Indicates that Item1 and Item2 have the same value in the sort order.
+
+

+An application can implement and assign a routine using the signature to the +OnSortCompare event handler in TCustomShellTreeView. Set its FileSortType +property to fstCustom to enable the handler routine. +

+

+The following is an item compare function, as implemented by forum member +d7_2_laz, used to order items with leading Underscore characters: +

+ +function TForm1.SortCompareUnderscore(Item1, Item2: TFileItem): integer; +begin + // Make sure that folders are moved to the top + Result := ord(Item2.isFolder) - ord(Item1.isFolder); + if Result = 0 then + if (pos('_', Item1.FileInfo.Name) = 1) or + (pos('_', Item2.FileInfo.Name) = 1) then + Result := AnsiCompareText(Item1.FileInfo.Name, Item2.FileInfo.Name) + else + Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name); +end; + +

+Sort File Items by Date +

+ +function TForm1.SortCompareByDate(Item1, Item2: TFileItem): integer; +begin + // Folders first ... + Result := ord(Item2.isFolder) - ord(Item1.isFolder); + if Result = 0 then + begin + // then file date ... + Result := CompareValue(Item1.FileInfo.TimeStamp, Item2.FileInfo.TimeStamp); + if Result = 0 then + // then file name + Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name); + end; +end; + +

+Sort File Items by Size +

+ +function TForm1.SortCompareBySize(Item1, Item2: TFileItem): integer; +begin + // Folders first + Result := ord(Item2.isFolder) - ord(Item1.isFolder); + if Result = 0 then + begin + // then file size ... + Result := Item1.FileInfo.Size - Item2.FileInfo.Size; + if Result = 0 then + // then file name + Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name); + end; +end; + +
+ + + + + +
+ + +First file item for the comparison routine. + + + + +Second file item for the comparison routine. + + + +Integer with the relative sort order for the compared file items. + + + Specifies an event handler signalled when an item is added to a shell control. @@ -204,7 +439,7 @@ procedure.

- +
@@ -299,6 +534,25 @@ methods implemented in the descendent classes. New value for the property.
+ + +Sets the value for the OnSortCompare property. + + + +Added in LCL version 2.4. + + + + + + + + +New value for the OnSortCompare property. + + + Sets the value for the Path property. @@ -1290,6 +1544,90 @@ to omit the file system object in Items. + + +Event handler signalled to compare file items in a custom sort routine. + + +

+OnSortCompare is a TFileItemCompareEvent property with +the event handler signalled to implement a custom file sort for the tree view +control. OnSortCompare is used to order the directories or files displayed in +the tree view control when its FileSortType property is set to fstCustom. +

+

+Changing the routine assigned to the property causes the Items in the control +to be reloaded and ordered using the new file sort compare routine. +

+

+An application can implement and assign a routine using the signature perform +custom file comparison routines using various attributes. The following is an +item compare function, as implemented by forum member d7_2_laz, used to order +items with leading Underscore characters: +

+ +function TForm1.SortCompareUnderscore(Item1, Item2: TFileItem): integer; +begin + // Make sure that folders are moved to the top + Result := ord(Item2.isFolder) - ord(Item1.isFolder); + if Result = 0 then + if (pos('_', Item1.FileInfo.Name) = 1) or + (pos('_', Item2.FileInfo.Name) = 1) then + Result := AnsiCompareText(Item1.FileInfo.Name, Item2.FileInfo.Name) + else + Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name); +end; + +

+Sort File Items by Date +

+ +function TForm1.SortCompareByDate(Item1, Item2: TFileItem): integer; +begin + // Folders first ... + Result := ord(Item2.isFolder) - ord(Item1.isFolder); + if Result = 0 then + begin + // then file date ... + Result := CompareValue(Item1.FileInfo.TimeStamp, Item2.FileInfo.TimeStamp); + if Result = 0 then + // then file name + Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name); + end; +end; + +

+Sort File Items by Size +

+ +function TForm1.SortCompareBySize(Item1, Item2: TFileItem): integer; +begin + // Folders first + Result := ord(Item2.isFolder) - ord(Item1.isFolder); + if Result = 0 then + begin + // then file size ... + Result := Item1.FileInfo.Size - Item2.FileInfo.Size; + if Result = 0 then + // then file name + Result := CompareText(Item1.FileInfo.Name, Item2.FileInfo.Name); + end; +end; + +
+ +Added in LCL version 2.4. + + + + + + + + + +
+ @@ -1437,6 +1775,7 @@ used for instance to quickly identify a component in an event handler. + @@ -1529,6 +1868,7 @@ in the class. +