Class TSortGrid (unit SortGrid)

Inherits from

TStringGrid

Here's the main new class: TSortGrid

Constructors


constructor Create(AOwner: TComponent);

****************************************************************************} {** Public Members for TSortGrid **} {


Functions

procedure AutoSizeCol(const ACol: LongInt);

AutoSizes the ACol column.

procedure AutoSizeColumns(const DoFixedCols: Boolean; const Padding: Integer);

AutoSizes ALL the variable columns and optionally the fixed columns.

function CanUndoSort: Boolean;


procedure Clear;

Clears the grid.

procedure DeleteColumn(ACol: Longint);

If goAlwaysShowEditor is enabled then DeleteRow and MoveRow leave the caret past the last row or in one of the fixed rows.

procedure DeleteRow(ARow: Longint);


destructor Destroy;

dflt to no case sensitivity

function GetCellDrawState(const ACol, ARow: Longint): TGridDrawState;

Now reset the list.

procedure InsertColumn(ACol: Longint);


procedure InsertRow(ARow: Longint);

Call the OnEndSort event.

function IsCell(const Value: String; var ACol, ARow: Longint): Boolean;

Finds a string in the grid.

procedure LoadFromFile(const FileName: String; const Delimiter: Char);


procedure MoveColumn(FromIndex, ToIndex: Longint);

See DeleteRow for comments.

procedure MoveRow(FromIndex, ToIndex: Longint);

See DeleteRow for comments.

procedure MoveTo(ACol, ARow: Longint);

Moves the selected cell to (ACol, ARow) and makes it visible.

procedure SaveToFile(const FileName: String; const Delimiter: Char);


function SelectCell(ACol, ARow: Longint): Boolean;


procedure SortByColumn(const ACol: LongInt; SortOptions: TSortOptions);

Sorts the variable rows using Column ACol as a key

procedure SwapColumns(ACol1, ACol2: Longint);

The logic gets around a weird case where you swap with the last column.

procedure SwapRows(ARow1, ARow2: Longint);

The logic gets around a weird case where you swap with the last row.

procedure UndoSort;

We can only undo a sort if we still have exactly the same number of rows that we did when we sorted.

procedure ValidateCell;


procedure Click;


procedure ColWidthsChanged;

EWE: Set case sensitivity here

function DetermineSortStyle(const ACol: Longint): TSortStyle;

This function tries to determine the best sort style for a column.

procedure DoEnter;


procedure DoExit;


procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);

Set defaults for the bevel colors.

procedure InitializeFormatOptions(var FmtOpts: TFormatOptions);

IF YOU GET AN EXCEPTION HERE WHEN RUNNING FROM THE IDE, THEN YOU NEED TO TURN OFF "Break on Exception"

procedure InitValidate;


procedure KeyDown(var Key: Word; Shift: TShiftState);

Make the scroll bar(s) proportional.

procedure KeyPress(var Key: Char);


procedure ListQuickSort(const ACol: Longint; const SortOptions: TSortOptions);

Protected declarations

****************************************************************************} {** Protected Members for TSortGrid **} {


procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

bug fix by Chris Vleghert:

procedure RowHeightsChanged;


procedure SetEditText(ACol, ARow: Longint; const Value: string);


procedure SizeChanged(OldColCount, OldRowCount: Longint);


procedure UpdateScrollPage;


procedure SetAlignmentHorz(Value: TAlignment);

****************************************************************************} {** Private Members for TSortGrid **} {

procedure SetAlignmentVert(Value: TVertAlignment);


procedure SetBevelStyle(Value: TCellBevelStyle);


procedure SetProportionalScrollBars(Value: Boolean);


procedure WMSize(var Msg: TWMSize);


Properties

property AlignmentHorz : TAlignment


property AlignmentVert : TVertAlignment


property BevelStyle : TCellBevelStyle


property CaseSensitive : Boolean

Published declarations

property ClickSorting : Boolean


property ExtendedKeys : Boolean


property ProportionalScrollBars : Boolean


property Modified : Boolean


property Sorting : Boolean

Public declarations } { run time properties

Events

event OnBeginSort : TBeginSortEvent


event OnCellValidate : TCellValidateEvent


event OnClickSort : TClickSortEvent


event OnColumnDelete : TUpdateGridEvent


event OnColumnInsert : TUpdateGridEvent


event OnColumnWidthsChanged : TNotifyEvent


event OnEndSort : TEndSortEvent


event OnGetCellFormat : TFormatDrawCellEvent

Published events

event OnRowDelete : TUpdateGridEvent


event OnRowHeightsChanged : TNotifyEvent


event OnRowInsert : TUpdateGridEvent


event OnSizeChanged : TSizeChangedEvent


Variables

fAlignmentHorz : TAlignment;


fAlignmentVert : TVertAlignment;


fBevelStyle : TCellBevelStyle;


fCaseSensitive : Boolean;

Private declarations

fClickSorting : Boolean;


fEntered : Boolean;


fExtendedKeys : Boolean;


fModified : Boolean;


fOldCellText : String;


fOldCol : Longint;


fOldModifiedValue : Boolean;


fOldRow : Longint;


fOnBeginSort : TBeginSortEvent;


fOnCellValidate : TCellValidateEvent;


fOnClickSort : TClickSortEvent;


fOnColumnDelete : TUpdateGridEvent;


fOnColumnInsert : TUpdateGridEvent;


fOnColumnWidthsChanged : TNotifyEvent;


fOnEndSort : TEndSortEvent;


fOnGetCellFormat : TFormatDrawCellEvent;


fOnRowDelete : TUpdateGridEvent;


fOnRowHeightsChanged : TNotifyEvent;


fOnRowInsert : TUpdateGridEvent;


fOnSizeChanged : TSizeChangedEvent;


fProportionalScrollBars : Boolean;


fSortedList : TSortedList;


fSorting : Boolean;



Constructors


constructor Create(AOwner: TComponent);

****************************************************************************} {** Public Members for TSortGrid **} {


Functions


procedure AutoSizeCol(const ACol: LongInt);

AutoSizes the ACol column.


procedure AutoSizeColumns(const DoFixedCols: Boolean; const Padding: Integer);

AutoSizes ALL the variable columns and optionally the fixed columns.


function CanUndoSort: Boolean;


procedure Clear;

Clears the grid.


procedure DeleteColumn(ACol: Longint);

If goAlwaysShowEditor is enabled then DeleteRow and MoveRow leave the caret past the last row or in one of the fixed rows. So I turn it off before the delete and then back on after to get it working correctly.


procedure DeleteRow(ARow: Longint);


destructor Destroy;

dflt to no case sensitivity


function GetCellDrawState(const ACol, ARow: Longint): TGridDrawState;

Now reset the list.


procedure InsertColumn(ACol: Longint);


procedure InsertRow(ARow: Longint);

Call the OnEndSort event.


function IsCell(const Value: String; var ACol, ARow: Longint): Boolean;

Finds a string in the grid. It searches by column and returns the first instance it finds.


procedure LoadFromFile(const FileName: String; const Delimiter: Char);


procedure MoveColumn(FromIndex, ToIndex: Longint);

See DeleteRow for comments...


procedure MoveRow(FromIndex, ToIndex: Longint);

See DeleteRow for comments...


procedure MoveTo(ACol, ARow: Longint);

Moves the selected cell to (ACol, ARow) and makes it visible.


procedure SaveToFile(const FileName: String; const Delimiter: Char);


function SelectCell(ACol, ARow: Longint): Boolean;


procedure SortByColumn(const ACol: LongInt; SortOptions: TSortOptions);

Sorts the variable rows using Column ACol as a key


procedure SwapColumns(ACol1, ACol2: Longint);

The logic gets around a weird case where you swap with the last column.


procedure SwapRows(ARow1, ARow2: Longint);

The logic gets around a weird case where you swap with the last row.


procedure UndoSort;

We can only undo a sort if we still have exactly the same number of rows that we did when we sorted.


procedure ValidateCell;


procedure Click;


procedure ColWidthsChanged;

EWE: Set case sensitivity here


function DetermineSortStyle(const ACol: Longint): TSortStyle;

This function tries to determine the best sort style for a column. If all the entries can be converted to numbers, a numeric sort is returned. If they can all be converted to dates, a date sort is returned. If time, then a time sort is returned, Otherwise, an alphabetic sort is returned.


procedure DoEnter;


procedure DoExit;


procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);

Set defaults for the bevel colors.


procedure InitializeFormatOptions(var FmtOpts: TFormatOptions);

IF YOU GET AN EXCEPTION HERE WHEN RUNNING FROM THE IDE, THEN YOU NEED TO TURN OFF "Break on Exception"


procedure InitValidate;


procedure KeyDown(var Key: Word; Shift: TShiftState);

Make the scroll bar(s) proportional.} {To do this correctly, I should sum colwidths and rowheights, but I just approximate by basing the proportion on visible rows or cols divided by row or col count...} {Also, I can't really figure out Borland's scroll bar range and position scheme. Thus, sometimes when you click on the end of the scroll bar, you still have to scroll farther with the arrows to actually get to the end of the grid. If anyone knows how to fix this, PLEASE let me know...


procedure KeyPress(var Key: Char);


procedure ListQuickSort(const ACol: Longint; const SortOptions: TSortOptions);

Protected declarations

****************************************************************************} {** Protected Members for TSortGrid **} {


procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

bug fix by Chris Vleghert:


procedure RowHeightsChanged;


procedure SetEditText(ACol, ARow: Longint; const Value: string);


procedure SizeChanged(OldColCount, OldRowCount: Longint);


procedure UpdateScrollPage;


procedure SetAlignmentHorz(Value: TAlignment);

****************************************************************************} {** Private Members for TSortGrid **} {


procedure SetAlignmentVert(Value: TVertAlignment);


procedure SetBevelStyle(Value: TCellBevelStyle);


procedure SetProportionalScrollBars(Value: Boolean);


procedure WMSize(var Msg: TWMSize);


Properties


property AlignmentHorz : TAlignment


property AlignmentVert : TVertAlignment


property BevelStyle : TCellBevelStyle


property CaseSensitive : Boolean

Published declarations


property ClickSorting : Boolean


property ExtendedKeys : Boolean


property ProportionalScrollBars : Boolean


property Modified : Boolean


property Sorting : Boolean

Public declarations } { run time properties


Events


event OnBeginSort : TBeginSortEvent


event OnCellValidate : TCellValidateEvent


event OnClickSort : TClickSortEvent


event OnColumnDelete : TUpdateGridEvent


event OnColumnInsert : TUpdateGridEvent


event OnColumnWidthsChanged : TNotifyEvent


event OnEndSort : TEndSortEvent


event OnGetCellFormat : TFormatDrawCellEvent

Published events


event OnRowDelete : TUpdateGridEvent


event OnRowHeightsChanged : TNotifyEvent


event OnRowInsert : TUpdateGridEvent


event OnSizeChanged : TSizeChangedEvent


Variables


fAlignmentHorz : TAlignment;


fAlignmentVert : TVertAlignment;


fBevelStyle : TCellBevelStyle;


fCaseSensitive : Boolean;

Private declarations


fClickSorting : Boolean;


fEntered : Boolean;


fExtendedKeys : Boolean;


fModified : Boolean;


fOldCellText : String;


fOldCol : Longint;


fOldModifiedValue : Boolean;


fOldRow : Longint;


fOnBeginSort : TBeginSortEvent;


fOnCellValidate : TCellValidateEvent;


fOnClickSort : TClickSortEvent;


fOnColumnDelete : TUpdateGridEvent;


fOnColumnInsert : TUpdateGridEvent;


fOnColumnWidthsChanged : TNotifyEvent;


fOnEndSort : TEndSortEvent;


fOnGetCellFormat : TFormatDrawCellEvent;


fOnRowDelete : TUpdateGridEvent;


fOnRowHeightsChanged : TNotifyEvent;


fOnRowInsert : TUpdateGridEvent;


fOnSizeChanged : TSizeChangedEvent;


fProportionalScrollBars : Boolean;


fSortedList : TSortedList;


fSorting : Boolean;