From 32fbca75945522547d490875f1c95157ed7cab17 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Thu, 23 Oct 2014 14:31:29 +0000 Subject: [PATCH] fpspreadsheet: Override TsWorksheetGrid's Sort method to the Sort method of the Worksheet. Hide OnCompareCells event. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3681 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpspreadsheet/fpspreadsheet.pas | 2 +- .../fpspreadsheet/fpspreadsheetgrid.pas | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/components/fpspreadsheet/fpspreadsheet.pas b/components/fpspreadsheet/fpspreadsheet.pas index e3d6e8419..cc7e28bf8 100755 --- a/components/fpspreadsheet/fpspreadsheet.pas +++ b/components/fpspreadsheet/fpspreadsheet.pas @@ -499,7 +499,7 @@ type {@@ This event can be used to override the built-in comparing function which is called when cells are sorted. } TsCellCompareEvent = procedure (Sender: TObject; ACell1, ACell2: PCell; - AResult: Integer) of object; + var AResult: Integer) of object; {@@ The worksheet contains a list of cells and provides a variety of methods to read or write data to the cells, or to change their formatting. } diff --git a/components/fpspreadsheet/fpspreadsheetgrid.pas b/components/fpspreadsheet/fpspreadsheetgrid.pas index ad204421b..01546058a 100644 --- a/components/fpspreadsheet/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/fpspreadsheetgrid.pas @@ -151,6 +151,7 @@ type procedure SelectEditor; override; procedure SetEditText(ACol, ARow: Longint; const AValue: string); override; procedure Setup; + procedure Sort(AColSorting: Boolean; AIndex, AIndxFrom, AIndxTo:Integer); override; procedure UpdateColWidths(AStartIndex: Integer = 0); procedure UpdateRowHeights(AStartIndex: Integer = 0); {@@ Automatically recalculate formulas whenever a cell value changes, } @@ -445,8 +446,10 @@ type property OnColRowInserted; {@@ inherited from ancestors} property OnColRowMoved; + (* {@@ inherited from ancestors} - property OnCompareCells; + property OnCompareCells; // apply userdefined sorting to worksheet directly! + *) {@@ inherited from ancestors} property OnDragDrop; {@@ inherited from ancestors} @@ -3207,6 +3210,41 @@ begin Invalidate; end; +{@@ ---------------------------------------------------------------------------- + Sorts the grid by calling the corresponding method of the worksheet. + Sorting extends across the entire worksheet. + Sort direction is determined by the property "SortOrder". Other sorting + criteria are "case-sensitive" and "numbers first". + + @param AColSorting If true the grid is sorted from top to bottom and the + next parameter, "Index", refers to a column. Otherweise + sorting goes from left to right and "Index" refers to a row. + @param AIndex Index of the column (if ColSorting=true) or row (ColSorting = false) + which is sorted. + @param AIndxFrom Sorting starts at this row (ColSorting=true) / column (ColSorting=false) + @param AIndxTo Sorting ends at this row (ColSorting=true) / column (ColSorting=false) +-------------------------------------------------------------------------------} +procedure TsCustomWorksheetGrid.Sort(AColSorting: Boolean; + AIndex, AIndxFrom, AIndxTo:Integer); +var + sortParams: TsSortParams; +begin + sortParams := InitSortParams(AColSorting, 1); + sortParams.Keys[0].ColRowIndex := AIndex - HeaderCount; + if SortOrder = soDescending then + sortParams.Keys[0].Options := [ssoDescending]; + if AColSorting then + FWorksheet.Sort( + sortParams, + AIndxFrom-HeaderCount, 0, AIndxTo-HeaderCount, FWorksheet.GetLastColIndex + ) + else + FWorksheet.Sort( + sortParams, + 0, AIndxFrom-HeaderCount, FWorksheet.GetLastRowIndex, AIndxTo-HeaderCount + ); +end; + {@@ ---------------------------------------------------------------------------- Updates column widths according to the data in the TCol records -------------------------------------------------------------------------------}