Ignore:
Timestamp:
Sep 10, 2022, 6:54:43 PM (2 years ago)
Author:
chronos
Message:
  • Modified: CoolTranslator replaced by Common package.
  • Modified: Update common package.
File:
1 edited

Legend:

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

    r15 r25  
    11unit UListViewSort;
    22
    3 // Date: 2010-11-03
    4 
    5 {$mode delphi}
     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 DoOnChange;
    8384    procedure GridDoOnKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    8485    procedure GridDoOnResize(Sender: TObject);
     
    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 Create(TheOwner);
     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 }
     155
     156procedure TListViewFilter.DoOnChange;
     157begin
     158  if Assigned(FOnChange) then FOnChange(Self);
     159end;
    111160
    112161procedure TListViewFilter.GridDoOnKeyUp(Sender: TObject; var Key: Word;
    113162  Shift: TShiftState);
    114163begin
    115   if Assigned(FOnChange) then
    116     FOnChange(Self);
     164  DoOnChange;
    117165end;
    118166
     
    142190var
    143191  I: Integer;
     192  R: TRect;
    144193begin
    145194  with FStringGrid1 do begin
    146     Options := Options - [goEditing, goAlwaysShowEditor];
    147     //Columns.Clear;
    148195    while Columns.Count > ListView.Columns.Count do Columns.Delete(Columns.Count - 1);
    149196    while Columns.Count < ListView.Columns.Count do Columns.Add;
    150197    for I := 0 to ListView.Columns.Count - 1 do begin
    151198      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;
    152204    end;
    153     Options := Options + [goEditing, goAlwaysShowEditor];
    154205  end;
    155206end;
     
    182233    Result := StringGrid.Cells[Index, 0]
    183234    else Result := '';
     235end;
     236
     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;
    184245end;
    185246
     
    274335end;
    275336
     337var
     338  ListViewSortCompare: TCompareEvent;
     339
     340function ListViewCompare(constref Item1, Item2: TObject): Integer;
     341begin
     342  Result := ListViewSortCompare(Item1, Item2);
     343end;
     344
    276345procedure TListViewSort.Sort(Compare: TCompareEvent);
    277346begin
     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;
    278350  if (List.Count > 0) then
    279     List.Sort(Compare);
     351    List.Sort(TComparer<TObject>.Construct(ListViewCompare));
    280352end;
    281353
     
    283355begin
    284356  if Assigned(FOnFilter) then FOnFilter(Self)
    285   else if Assigned(Source) then
    286     List.Assign(Source) else
     357  else if Assigned(Source) then begin
    287358    List.Clear;
     359    List.AddRange(Source);
     360  end else List.Clear;
    288361  if ListView.Items.Count <> List.Count then
    289362    ListView.Items.Count := List.Count;
     
    340413begin
    341414  inherited;
    342   List := TListObject.Create;
     415  List := TObjects.Create;
    343416  List.OwnsObjects := False;
    344417end;
     
    346419destructor TListViewSort.Destroy;
    347420begin
    348   List.Free;
     421  FreeAndNil(List);
    349422  inherited;
    350423end;
     
    381454  ItemLeft := Item.Left;
    382455  ItemLeft := 23; // Windows 7 workaround
    383  
     456
    384457  Rect1.Left := ItemLeft - CheckWidth - BiasLeft + 1 + XBias;
    385458  //ShowMessage(IntToStr(Tp1.Y) + ', ' + IntToStr(BiasTop) + ', ' + IntToStr(XBias));
     
    480553    FHeaderHandle := ListView_GetHeader(FListView.Handle);
    481554    for I := 0 to FListView.Columns.Count - 1 do begin
     555      {$push}{$warn 5057 off}
    482556      FillChar(Item, SizeOf(THDItem), 0);
     557      {$pop}
    483558      Item.Mask := HDI_FORMAT;
    484559      Header_GetItem(FHeaderHandle, I, Item);
Note: See TracChangeset for help on using the changeset viewer.