Changeset 40 for trunk/Packages/Common/UListViewSort.pas
- Timestamp:
- Dec 10, 2016, 11:01:49 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UListViewSort.pas
r4 r40 9 9 uses 10 10 {$IFDEF Windows}Windows, CommCtrl, {$ENDIF}Classes, Graphics, ComCtrls, SysUtils, 11 Controls, DateUtils, Dialogs, SpecializedList, Forms, Grids, StdCtrls, ExtCtrls; 11 Controls, DateUtils, Dialogs, SpecializedList, Forms, Grids, StdCtrls, ExtCtrls, 12 LclIntf, LMessages, LclType, LResources; 12 13 13 14 type … … 18 19 TCompareEvent = function (Item1, Item2: TObject): Integer of object; 19 20 TListFilterEvent = procedure (ListViewSort: TListViewSort) of object; 21 22 { TListViewSort } 20 23 21 24 TListViewSort = class(TComponent) … … 28 31 FColumn: Integer; 29 32 FOrder: TSortOrder; 33 FOldListViewWindowProc: TWndMethod; 34 FOnColumnWidthChanged: TNotifyEvent; 35 procedure DoColumnBeginResize(const AColIndex: Integer); 36 procedure DoColumnResized(const AColIndex: Integer); 37 procedure DoColumnResizing(const AColIndex, AWidth: Integer); 30 38 procedure SetListView(const Value: TListView); 31 39 procedure ColumnClick(Sender: TObject; Column: TListColumn); … … 40 48 procedure SetColumn(const Value: Integer); 41 49 procedure SetOrder(const Value: TSortOrder); 50 {$IFDEF WINDOWS} 51 procedure NewListViewWindowProc(var AMsg: TMessage); 52 {$ENDIF} 42 53 public 43 54 List: TListObject; … … 58 69 property OnCustomDraw: TLVCustomDrawItemEvent read FOnCustomDraw 59 70 write FOnCustomDraw; 71 property OnColumnWidthChanged: TNotifyEvent read FOnColumnWidthChanged 72 write FOnColumnWidthChanged; 60 73 property Column: Integer read FColumn write SetColumn; 61 74 property Order: TSortOrder read FOrder write SetOrder; … … 69 82 FStringGrid1: TStringGrid; 70 83 procedure DoOnKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); 84 procedure DoOnResize(Sender: TObject); 71 85 public 72 86 constructor Create(AOwner: TComponent); override; 73 87 procedure UpdateFromListView(ListView: TListView); 74 88 function TextEntered: Boolean; 89 function TextEnteredCount: Integer; 75 90 function TextEnteredColumn(Index: Integer): Boolean; 76 91 function GetColValue(Index: Integer): string; … … 80 95 property Align; 81 96 property Anchors; 97 property BorderSpacing; 82 98 end; 83 99 … … 99 115 if Assigned(FOnChange) then 100 116 FOnChange(Self); 117 end; 118 119 procedure TListViewFilter.DoOnResize(Sender: TObject); 120 begin 121 FStringGrid1.DefaultRowHeight := FStringGrid1.Height; 101 122 end; 102 123 … … 115 136 goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goSmoothScroll]; 116 137 FStringGrid1.OnKeyUp := DoOnKeyUp; 138 FStringGrid1.OnResize := DoOnResize; 117 139 end; 118 140 … … 120 142 var 121 143 I: Integer; 122 NewColumn: TGridColumn;123 144 begin 124 145 with FStringGrid1 do begin 125 Columns.Clear;146 //Columns.Clear; 126 147 while Columns.Count > ListView.Columns.Count do Columns.Delete(Columns.Count - 1); 127 while Columns.Count < ListView.Columns.Count do NewColumn :=Columns.Add;148 while Columns.Count < ListView.Columns.Count do Columns.Add; 128 149 for I := 0 to ListView.Columns.Count - 1 do begin 129 150 Columns[I].Width := ListView.Columns[I].Width; … … 133 154 134 155 function TListViewFilter.TextEntered: Boolean; 156 begin 157 Result := TextEnteredCount > 0; 158 end; 159 160 function TListViewFilter.TextEnteredCount: Integer; 135 161 var 136 162 I: Integer; 137 163 begin 138 Result := False;164 Result := 0; 139 165 for I := 0 to FStringGrid1.ColCount - 1 do begin 140 166 if FStringGrid1.Cells[I, 0] <> '' then begin 141 Result := True; 142 Break; 167 Inc(Result); 143 168 end; 144 169 end; … … 159 184 { TListViewSort } 160 185 186 {$IFDEF WINDOWS} 187 procedure TListViewSort.NewListViewWindowProc(var AMsg: TMessage); 188 var 189 vColWidth: Integer; 190 vMsgNotify: TLMNotify absolute AMsg; 191 Code: Integer; 192 begin 193 // call the old WindowProc of ListView 194 FOldListViewWindowProc(AMsg); 195 196 // Currently we care only with WM_NOTIFY message 197 if AMsg.Msg = WM_NOTIFY then 198 begin 199 Code := PHDNotify(vMsgNotify.NMHdr)^.Hdr.Code; 200 case Code of 201 HDN_ENDTRACKA, HDN_ENDTRACKW: 202 DoColumnResized(PHDNotify(vMsgNotify.NMHdr)^.Item); 203 204 HDN_BEGINTRACKA, HDN_BEGINTRACKW: 205 DoColumnBeginResize(PHDNotify(vMsgNotify.NMHdr)^.Item); 206 207 HDN_TRACKA, HDN_TRACKW: 208 begin 209 vColWidth := -1; 210 if (PHDNotify(vMsgNotify.NMHdr)^.PItem<>nil) 211 and (PHDNotify(vMsgNotify.NMHdr)^.PItem^.Mask and HDI_WIDTH <> 0) 212 then 213 vColWidth := PHDNotify(vMsgNotify.NMHdr)^.PItem^.cxy; 214 215 DoColumnResizing(PHDNotify(vMsgNotify.NMHdr)^.Item, vColWidth); 216 end; 217 end; 218 end; 219 end; 220 {$ENDIF} 221 222 procedure TListViewSort.DoColumnBeginResize(const AColIndex: Integer); 223 begin 224 end; 225 226 procedure TListViewSort.DoColumnResizing(const AColIndex, AWidth: Integer); 227 begin 228 end; 229 230 procedure TListViewSort.DoColumnResized(const AColIndex: Integer); 231 begin 232 if Assigned(FOnColumnWidthChanged) then 233 FOnColumnWidthChanged(Self); 234 end; 161 235 162 236 procedure TListViewSort.ColumnClick(Sender: TObject; Column: TListColumn); … … 185 259 procedure TListViewSort.SetListView(const Value: TListView); 186 260 begin 261 if FListView = Value then Exit; 262 if Assigned(FListView) then 263 ListView.WindowProc := FOldListViewWindowProc; 187 264 FListView := Value; 188 265 FListView.OnColumnClick := ColumnClick; 189 266 FListView.OnCustomDrawItem := ListViewCustomDrawItem; 190 267 FListView.OnClick := ListViewClick; 268 FOldListViewWindowProc := FListView.WindowProc; 269 {$IFDEF WINDOWS} 270 FListView.WindowProc := NewListViewWindowProc; 271 {$ENDIF} 191 272 end; 192 273 … … 205 286 if ListView.Items.Count <> List.Count then 206 287 ListView.Items.Count := List.Count; 207 if Assigned(FOnCompareItem) then Sort(FOnCompareItem);288 if Assigned(FOnCompareItem) and (Order <> soNone) then Sort(FOnCompareItem); 208 289 //ListView.Items[-1]; // Workaround for not show first row if selected 209 290 ListView.Refresh;
Note:
See TracChangeset
for help on using the changeset viewer.