Ignore:
Timestamp:
Apr 3, 2025, 10:49:00 PM (13 days ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/ListViewSort.pas

    r20 r21  
    1 unit UListViewSort;
    2 
    3 // Date: 2010-11-03
    4 
    5 {$mode delphi}
     1unit ListViewSort;
     2
     3// Date: 2019-05-17
    64
    75interface
    86
    97uses
    10   {$IFDEF Windows}Windows, CommCtrl, {$ENDIF}Classes, Graphics, ComCtrls, SysUtils,
    11   Controls, DateUtils, Dialogs, SpecializedList, Forms, Grids, StdCtrls, ExtCtrls,
    12   LclIntf, LMessages, LclType, LResources;
     8  {$IFDEF Windows}Windows, CommCtrl, LMessages, {$ENDIF}Classes, Graphics, ComCtrls, SysUtils,
     9  Controls, DateUtils, Dialogs, Forms, Grids, StdCtrls, ExtCtrls,
     10  LclIntf, LclType, LResources, Generics.Collections, Generics.Defaults;
    1311
    1412type
     
    1917  TCompareEvent = function (Item1, Item2: TObject): Integer of object;
    2018  TListFilterEvent = procedure (ListViewSort: TListViewSort) of object;
     19
     20  TObjects = TObjectList<TObject>;
    2121
    2222  { TListViewSort }
     
    5252    {$ENDIF}
    5353  public
    54     List: TListObject;
    55     Source: TListObject;
     54    Source: TObjects;
     55    List: TObjects;
    5656    constructor Create(AOwner: TComponent); override;
    5757    destructor Destroy; override;
     
    8181    FOnChange: TNotifyEvent;
    8282    FStringGrid1: TStringGrid;
    83     procedure DoOnKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    84     procedure DoOnResize(Sender: TObject);
     83    procedure DoOnChange;
     84    procedure GridDoOnKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
     85    procedure GridDoOnResize(Sender: TObject);
    8586  public
    8687    constructor Create(AOwner: TComponent); override;
     
    9091    function TextEnteredColumn(Index: Integer): Boolean;
    9192    function GetColValue(Index: Integer): string;
     93    procedure Reset;
    9294    property StringGrid: TStringGrid read FStringGrid1 write FStringGrid1;
    9395  published
     
    98100  end;
    99101
     102  { TListViewEx }
     103
     104  TListViewEx = class(TWinControl)
     105  private
     106    FFilter: TListViewFilter;
     107    FListView: TListView;
     108    FListViewSort: TListViewSort;
     109    procedure ResizeHanlder;
     110  public
     111    constructor Create(TheOwner: TComponent); override;
     112    destructor Destroy; override;
     113  published
     114    property ListView: TListView read FListView write FListView;
     115    property ListViewSort: TListViewSort read FListViewSort write FListViewSort;
     116    property Filter: TListViewFilter read FFilter write FFilter;
     117    property Visible;
     118  end;
     119
    100120procedure Register;
    101121
     
    105125procedure Register;
    106126begin
    107   RegisterComponents('Common', [TListViewSort, TListViewFilter]);
     127  RegisterComponents('Common', [TListViewSort, TListViewFilter, TListViewEx]);
     128end;
     129
     130{ TListViewEx }
     131
     132procedure TListViewEx.ResizeHanlder;
     133begin
     134end;
     135
     136constructor TListViewEx.Create(TheOwner: TComponent);
     137begin
     138  inherited;
     139  Filter := TListViewFilter.Create(Self);
     140  Filter.Parent := Self;
     141  Filter.Align := alBottom;
     142  ListView := TListView.Create(Self);
     143  ListView.Parent := Self;
     144  ListView.Align := alClient;
     145  ListViewSort := TListViewSort.Create(Self);
     146  ListViewSort.ListView := ListView;
     147end;
     148
     149destructor TListViewEx.Destroy;
     150begin
     151  inherited;
    108152end;
    109153
    110154{ TListViewFilter }
    111155
    112 procedure TListViewFilter.DoOnKeyUp(Sender: TObject; var Key: Word;
     156procedure TListViewFilter.DoOnChange;
     157begin
     158  if Assigned(FOnChange) then FOnChange(Self);
     159end;
     160
     161procedure TListViewFilter.GridDoOnKeyUp(Sender: TObject; var Key: Word;
    113162  Shift: TShiftState);
    114163begin
    115   if Assigned(FOnChange) then
    116     FOnChange(Self);
    117 end;
    118 
    119 procedure TListViewFilter.DoOnResize(Sender: TObject);
     164  DoOnChange;
     165end;
     166
     167procedure TListViewFilter.GridDoOnResize(Sender: TObject);
    120168begin
    121169  FStringGrid1.DefaultRowHeight := FStringGrid1.Height;
     
    124172constructor TListViewFilter.Create(AOwner: TComponent);
    125173begin
    126   inherited Create(AOwner);
     174  inherited;
    127175  FStringGrid1 := TStringGrid.Create(Self);
    128176  FStringGrid1.Align := alClient;
     
    135183  FStringGrid1.Options := [goFixedHorzLine, goFixedVertLine, goVertLine,
    136184    goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goSmoothScroll];
    137   FStringGrid1.OnKeyUp := DoOnKeyUp;
    138   FStringGrid1.OnResize := DoOnResize;
     185  FStringGrid1.OnKeyUp := GridDoOnKeyUp;
     186  FStringGrid1.OnResize := GridDoOnResize;
    139187end;
    140188
     
    142190var
    143191  I: Integer;
     192  R: TRect;
    144193begin
    145194  with FStringGrid1 do begin
    146     //Columns.Clear;
    147195    while Columns.Count > ListView.Columns.Count do Columns.Delete(Columns.Count - 1);
    148196    while Columns.Count < ListView.Columns.Count do Columns.Add;
    149197    for I := 0 to ListView.Columns.Count - 1 do begin
    150198      Columns[I].Width := ListView.Columns[I].Width;
     199      if Selection.Left = I then begin
     200        R := CellRect(I, 0);
     201        Editor.Left := R.Left + 2;
     202        Editor.Width := R.Width - 4;
     203      end;
    151204    end;
    152205  end;
     
    182235end;
    183236
     237procedure TListViewFilter.Reset;
     238var
     239  I: Integer;
     240begin
     241  with StringGrid do
     242  for I := 0 to ColCount - 1 do
     243    Cells[I, 0] := '';
     244  DoOnChange;
     245end;
     246
    184247{ TListViewSort }
    185248
     
    197260  if AMsg.Msg = WM_NOTIFY then
    198261  begin
    199     Code := PHDNotify(vMsgNotify.NMHdr)^.Hdr.Code;
     262    Code := NMHDR(PHDNotify(vMsgNotify.NMHdr)^.Hdr).Code;
    200263    case Code of
    201264      HDN_ENDTRACKA, HDN_ENDTRACKW:
     
    272335end;
    273336
     337var
     338  ListViewSortCompare: TCompareEvent;
     339
     340function ListViewCompare(constref Item1, Item2: TObject): Integer;
     341begin
     342  Result := ListViewSortCompare(Item1, Item2);
     343end;
     344
    274345procedure TListViewSort.Sort(Compare: TCompareEvent);
    275346begin
     347  // TODO: Because TFLGObjectList compare handler is not class method,
     348  // it is necessary to use simple function compare handler with local variable
     349  ListViewSortCompare := Compare;
    276350  if (List.Count > 0) then
    277     List.Sort(Compare);
     351    List.Sort(TComparer<TObject>.Construct(ListViewCompare));
    278352end;
    279353
     
    281355begin
    282356  if Assigned(FOnFilter) then FOnFilter(Self)
    283   else if Assigned(Source) then
    284     List.Assign(Source) else
     357  else if Assigned(Source) then begin
    285358    List.Clear;
     359    List.AddRange(Source);
     360  end;
    286361  if ListView.Items.Count <> List.Count then
    287362    ListView.Items.Count := List.Count;
     
    338413begin
    339414  inherited;
    340   List := TListObject.Create;
     415  List := TObjects.Create;
    341416  List.OwnsObjects := False;
    342417end;
     
    344419destructor TListViewSort.Destroy;
    345420begin
    346   List.Free;
     421  FreeAndNil(List);
    347422  inherited;
    348423end;
     
    353428  TP1: TPoint;
    354429  XBias, YBias: Integer;
    355   OldColor: TColor;
     430  PenColor: TColor;
     431  BrushColor: TColor;
    356432  BiasTop, BiasLeft: Integer;
    357433  Rect1: TRect;
     
    365441  Item.Left := 0;
    366442  GetCheckBias(XBias, YBias, BiasTop, BiasLeft, ListView);
    367   OldColor := ListView.Canvas.Pen.Color;
     443  PenColor := ListView.Canvas.Pen.Color;
     444  BrushColor := ListView.Canvas.Brush.Color;
    368445  //TP1 := Item.GetPosition;
    369446  lRect := Item.DisplayRect(drBounds); // Windows 7 workaround
     
    377454  ItemLeft := Item.Left;
    378455  ItemLeft := 23; // Windows 7 workaround
    379  
     456
    380457  Rect1.Left := ItemLeft - CheckWidth - BiasLeft + 1 + XBias;
    381458  //ShowMessage(IntToStr(Tp1.Y) + ', ' + IntToStr(BiasTop) + ', ' + IntToStr(XBias));
     
    408485  end;
    409486  //ListView.Canvas.Brush.Color := ListView.Color;
    410   ListView.Canvas.Brush.Color := clWindow;
    411   ListView.Canvas.Pen.Color := OldColor;
     487  ListView.Canvas.Brush.Color := BrushColor;
     488  ListView.Canvas.Pen.Color := PenColor;
    412489end;
    413490
     
    476553    FHeaderHandle := ListView_GetHeader(FListView.Handle);
    477554    for I := 0 to FListView.Columns.Count - 1 do begin
     555      {$push}{$warn 5057 off}
    478556      FillChar(Item, SizeOf(THDItem), 0);
     557      {$pop}
    479558      Item.Mask := HDI_FORMAT;
    480559      Header_GetItem(FHeaderHandle, I, Item);
Note: See TracChangeset for help on using the changeset viewer.