Changeset 21 for trunk/Packages/Common/ListViewSort.pas
- Timestamp:
- Apr 3, 2025, 10:49:00 PM (13 days ago)
- 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} 1 unit ListViewSort; 2 3 // Date: 2019-05-17 6 4 7 5 interface 8 6 9 7 uses 10 {$IFDEF Windows}Windows, CommCtrl, {$ENDIF}Classes, Graphics, ComCtrls, SysUtils,11 Controls, DateUtils, Dialogs, SpecializedList,Forms, Grids, StdCtrls, ExtCtrls,12 LclIntf, L Messages, 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; 13 11 14 12 type … … 19 17 TCompareEvent = function (Item1, Item2: TObject): Integer of object; 20 18 TListFilterEvent = procedure (ListViewSort: TListViewSort) of object; 19 20 TObjects = TObjectList<TObject>; 21 21 22 22 { TListViewSort } … … 52 52 {$ENDIF} 53 53 public 54 List: TListObject;55 Source: TListObject;54 Source: TObjects; 55 List: TObjects; 56 56 constructor Create(AOwner: TComponent); override; 57 57 destructor Destroy; override; … … 81 81 FOnChange: TNotifyEvent; 82 82 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); 85 86 public 86 87 constructor Create(AOwner: TComponent); override; … … 90 91 function TextEnteredColumn(Index: Integer): Boolean; 91 92 function GetColValue(Index: Integer): string; 93 procedure Reset; 92 94 property StringGrid: TStringGrid read FStringGrid1 write FStringGrid1; 93 95 published … … 98 100 end; 99 101 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 100 120 procedure Register; 101 121 … … 105 125 procedure Register; 106 126 begin 107 RegisterComponents('Common', [TListViewSort, TListViewFilter]); 127 RegisterComponents('Common', [TListViewSort, TListViewFilter, TListViewEx]); 128 end; 129 130 { TListViewEx } 131 132 procedure TListViewEx.ResizeHanlder; 133 begin 134 end; 135 136 constructor TListViewEx.Create(TheOwner: TComponent); 137 begin 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; 147 end; 148 149 destructor TListViewEx.Destroy; 150 begin 151 inherited; 108 152 end; 109 153 110 154 { TListViewFilter } 111 155 112 procedure TListViewFilter.DoOnKeyUp(Sender: TObject; var Key: Word; 156 procedure TListViewFilter.DoOnChange; 157 begin 158 if Assigned(FOnChange) then FOnChange(Self); 159 end; 160 161 procedure TListViewFilter.GridDoOnKeyUp(Sender: TObject; var Key: Word; 113 162 Shift: TShiftState); 114 163 begin 115 if Assigned(FOnChange) then 116 FOnChange(Self); 117 end; 118 119 procedure TListViewFilter.DoOnResize(Sender: TObject); 164 DoOnChange; 165 end; 166 167 procedure TListViewFilter.GridDoOnResize(Sender: TObject); 120 168 begin 121 169 FStringGrid1.DefaultRowHeight := FStringGrid1.Height; … … 124 172 constructor TListViewFilter.Create(AOwner: TComponent); 125 173 begin 126 inherited Create(AOwner);174 inherited; 127 175 FStringGrid1 := TStringGrid.Create(Self); 128 176 FStringGrid1.Align := alClient; … … 135 183 FStringGrid1.Options := [goFixedHorzLine, goFixedVertLine, goVertLine, 136 184 goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goSmoothScroll]; 137 FStringGrid1.OnKeyUp := DoOnKeyUp;138 FStringGrid1.OnResize := DoOnResize;185 FStringGrid1.OnKeyUp := GridDoOnKeyUp; 186 FStringGrid1.OnResize := GridDoOnResize; 139 187 end; 140 188 … … 142 190 var 143 191 I: Integer; 192 R: TRect; 144 193 begin 145 194 with FStringGrid1 do begin 146 //Columns.Clear;147 195 while Columns.Count > ListView.Columns.Count do Columns.Delete(Columns.Count - 1); 148 196 while Columns.Count < ListView.Columns.Count do Columns.Add; 149 197 for I := 0 to ListView.Columns.Count - 1 do begin 150 198 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; 151 204 end; 152 205 end; … … 182 235 end; 183 236 237 procedure TListViewFilter.Reset; 238 var 239 I: Integer; 240 begin 241 with StringGrid do 242 for I := 0 to ColCount - 1 do 243 Cells[I, 0] := ''; 244 DoOnChange; 245 end; 246 184 247 { TListViewSort } 185 248 … … 197 260 if AMsg.Msg = WM_NOTIFY then 198 261 begin 199 Code := PHDNotify(vMsgNotify.NMHdr)^.Hdr.Code;262 Code := NMHDR(PHDNotify(vMsgNotify.NMHdr)^.Hdr).Code; 200 263 case Code of 201 264 HDN_ENDTRACKA, HDN_ENDTRACKW: … … 272 335 end; 273 336 337 var 338 ListViewSortCompare: TCompareEvent; 339 340 function ListViewCompare(constref Item1, Item2: TObject): Integer; 341 begin 342 Result := ListViewSortCompare(Item1, Item2); 343 end; 344 274 345 procedure TListViewSort.Sort(Compare: TCompareEvent); 275 346 begin 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; 276 350 if (List.Count > 0) then 277 List.Sort( Compare);351 List.Sort(TComparer<TObject>.Construct(ListViewCompare)); 278 352 end; 279 353 … … 281 355 begin 282 356 if Assigned(FOnFilter) then FOnFilter(Self) 283 else if Assigned(Source) then 284 List.Assign(Source) else 357 else if Assigned(Source) then begin 285 358 List.Clear; 359 List.AddRange(Source); 360 end; 286 361 if ListView.Items.Count <> List.Count then 287 362 ListView.Items.Count := List.Count; … … 338 413 begin 339 414 inherited; 340 List := T ListObject.Create;415 List := TObjects.Create; 341 416 List.OwnsObjects := False; 342 417 end; … … 344 419 destructor TListViewSort.Destroy; 345 420 begin 346 List.Free;421 FreeAndNil(List); 347 422 inherited; 348 423 end; … … 353 428 TP1: TPoint; 354 429 XBias, YBias: Integer; 355 OldColor: TColor; 430 PenColor: TColor; 431 BrushColor: TColor; 356 432 BiasTop, BiasLeft: Integer; 357 433 Rect1: TRect; … … 365 441 Item.Left := 0; 366 442 GetCheckBias(XBias, YBias, BiasTop, BiasLeft, ListView); 367 OldColor := ListView.Canvas.Pen.Color; 443 PenColor := ListView.Canvas.Pen.Color; 444 BrushColor := ListView.Canvas.Brush.Color; 368 445 //TP1 := Item.GetPosition; 369 446 lRect := Item.DisplayRect(drBounds); // Windows 7 workaround … … 377 454 ItemLeft := Item.Left; 378 455 ItemLeft := 23; // Windows 7 workaround 379 456 380 457 Rect1.Left := ItemLeft - CheckWidth - BiasLeft + 1 + XBias; 381 458 //ShowMessage(IntToStr(Tp1.Y) + ', ' + IntToStr(BiasTop) + ', ' + IntToStr(XBias)); … … 408 485 end; 409 486 //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; 412 489 end; 413 490 … … 476 553 FHeaderHandle := ListView_GetHeader(FListView.Handle); 477 554 for I := 0 to FListView.Columns.Count - 1 do begin 555 {$push}{$warn 5057 off} 478 556 FillChar(Item, SizeOf(THDItem), 0); 557 {$pop} 479 558 Item.Mask := HDI_FORMAT; 480 559 Header_GetItem(FHeaderHandle, I, Item);
Note:
See TracChangeset
for help on using the changeset viewer.