Skip to content

Commit 5f1b193

Browse files
committed
feat: sort indicator and sensible column sorting in file dialog
1 parent 8318e24 commit 5f1b193

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

‎source/extfiledialog.pas‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ TfrmExtFileDialog = class(TExtForm)
5050
Selected: Boolean);
5151
procedure ShellListViewContextPopup(Sender: TObject; MousePos: TPoint;
5252
var Handled: Boolean);
53+
procedure ShellListViewCompare(Sender: TObject; Item1, Item2: TListItem;
54+
Data: Integer; var Compare: Integer);
55+
procedure ShellListViewColumnClick(Sender: TObject; Column: TListColumn);
5356
procedure ShellTreeViewChange(Sender: TObject; Node: TTreeNode);
5457
procedure ShellTreeViewChanging(Sender: TObject; Node: TTreeNode;
5558
var AllowChange: Boolean);
@@ -68,6 +71,8 @@ TfrmExtFileDialog = class(TExtForm)
6871
FFiles: TStringList;
6972
FOnTypeChange: TNotifyEvent;
7073
FClickedPathPart: String;
74+
FSortDirection: TSortDirection; // SortDirection is not published by TShellListView
75+
function CompareFileNames(const Name1, Name2: String): Integer;
7176
{$IFDEF WINDOWS}
7277
function ShowShellContextMenu(FileName: String; ScreenPos: TPoint): Boolean;
7378
{$ENDIF}
@@ -113,6 +118,9 @@ implementation
113118

114119
uses main;
115120

121+
{$IFDEF WINDOWS}
122+
function StrCmpLogicalW(psz1, psz2: PWideChar): Integer; stdcall; external 'shlwapi.dll' name 'StrCmpLogicalW';
123+
{$ENDIF}
116124

117125
function TfrmExtFileDialog.Execute: Boolean;
118126
begin
@@ -138,6 +146,9 @@ procedure TfrmExtFileDialog.FormCreate(Sender: TObject);
138146
ShellListView.SmallImages := MainForm.ImageListMain;
139147
ShellListView.OnFileAdded := ShellListViewFileAdded;
140148
{$ENDIF}
149+
ShellListView.OnCompare := ShellListViewCompare;
150+
ShellListView.OnColumnClick := ShellListViewColumnClick;
151+
FSortDirection := sdAscending;
141152
{$IFDEF WINDOWS}
142153
ShellListView.OnContextPopup := ShellListViewContextPopup;
143154
{$ENDIF}
@@ -189,6 +200,10 @@ procedure TfrmExtFileDialog.FormShow(Sender: TObject);
189200
LineBreakIndexInt := Integer(FLineBreakIndex)-1; // we skip lbsNone
190201
if (LineBreakIndexInt >=0) and (LineBreakIndexInt < comboLineBreaks.Items.Count) then
191202
comboLineBreaks.ItemIndex := LineBreakIndexInt;
203+
204+
// Show the sort indicator on the initially sorted column
205+
if (ShellListView.SortColumn >= 0) and (ShellListView.SortColumn < ShellListView.Columns.Count) then
206+
ShellListView.Columns[ShellListView.SortColumn].SortIndicator := siAscending;
192207
end;
193208

194209
procedure TfrmExtFileDialog.IdleTimerTreeNodeScrolltoviewTimer(Sender: TObject);
@@ -369,6 +384,74 @@ procedure TfrmExtFileDialog.ShellListViewSelectItem(Sender: TObject;
369384
end;
370385
end;
371386

387+
procedure TfrmExtFileDialog.ShellListViewCompare(Sender: TObject; Item1,
388+
Item2: TListItem; Data: Integer; var Compare: Integer);
389+
begin
390+
// Folders first
391+
if TShellListItem(Item1).isFolder <> TShellListItem(Item2).isFolder then begin
392+
if TShellListItem(Item1).isFolder then
393+
Compare := -1
394+
else
395+
Compare := 1;
396+
Exit;
397+
end;
398+
case ShellListView.SortColumn of
399+
1: // Size: numeric compare, ShellCtrls stores the raw byte count in Item.Data
400+
Compare := CompareValue(Int64(PtrInt(Item1.Data)), Int64(PtrInt(Item2.Data)));
401+
2: // Type: by file extension
402+
Compare := AnsiCompareText(ExtractFileExt(Item1.Caption), ExtractFileExt(Item2.Caption));
403+
else // Name
404+
Compare := CompareFileNames(Item1.Caption, Item2.Caption);
405+
end;
406+
if Compare = 0 then
407+
Compare := CompareFileNames(Item1.Caption, Item2.Caption); // deterministic order
408+
// TCustomListView reverses the compare result for descending order only when
409+
// OnCompare is NOT set (see _CompareListViewItems), so reverse it ourselves.
410+
// SortDirection is not published by TShellListView, hence our own FSortDirection.
411+
if FSortDirection = sdDescending then
412+
Compare := -Compare;
413+
end;
414+
415+
function TfrmExtFileDialog.CompareFileNames(const Name1, Name2: String): Integer;
416+
{$IFDEF WINDOWS}
417+
var
418+
W1, W2: WideString;
419+
{$ENDIF}
420+
begin
421+
{$IFDEF WINDOWS}
422+
// Natural sort like in Windows Explorer: "file2" sorts before "file10"
423+
W1 := UTF8ToUTF16(Name1);
424+
W2 := UTF8ToUTF16(Name2);
425+
Result := StrCmpLogicalW(PWideChar(W1), PWideChar(W2));
426+
{$ELSE}
427+
Result := AnsiCompareText(Name1, Name2);
428+
{$ENDIF}
429+
end;
430+
431+
procedure TfrmExtFileDialog.ShellListViewColumnClick(Sender: TObject;
432+
Column: TListColumn);
433+
const
434+
DirToIndicator: array[TSortDirection] of TSortIndicator = (siAscending, siDescending);
435+
var
436+
i: Integer;
437+
NewDirection: TSortDirection;
438+
begin
439+
// The list view sorts itself after this event (AutoSort is on), so predict the
440+
// resulting direction here and place the indicator on the clicked column.
441+
// Note: TCustomListView.AutoSortIndicator is protected and not published by
442+
// TShellListView, so we cannot use it and set TColumn.SortIndicator manually.
443+
if Column.Index <> ShellListView.SortColumn then
444+
FSortDirection := sdAscending
445+
else if FSortDirection = sdAscending then
446+
FSortDirection := sdDescending
447+
else
448+
FSortDirection := sdAscending;
449+
NewDirection := FSortDirection;
450+
for i := 0 to ShellListView.Columns.Count-1 do
451+
ShellListView.Columns[i].SortIndicator := siNone;
452+
Column.SortIndicator := DirToIndicator[NewDirection];
453+
end;
454+
372455
procedure TfrmExtFileDialog.ShellListViewContextPopup(Sender: TObject;
373456
MousePos: TPoint; var Handled: Boolean);
374457
var

0 commit comments

Comments
 (0)