Changeset 15 for trunk/Packages/Common/UListViewSort.pas
- Timestamp:
- Mar 22, 2018, 8:31:19 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UListViewSort.pas
r10 r15 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; … … 68 81 FOnChange: TNotifyEvent; 69 82 FStringGrid1: TStringGrid; 70 procedure DoOnKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); 83 procedure GridDoOnKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); 84 procedure GridDoOnResize(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; 90 function TextEnteredColumn(Index: Integer): Boolean; 75 91 function GetColValue(Index: Integer): string; 76 92 property StringGrid: TStringGrid read FStringGrid1 write FStringGrid1; … … 79 95 property Align; 80 96 property Anchors; 97 property BorderSpacing; 81 98 end; 82 99 … … 93 110 { TListViewFilter } 94 111 95 procedure TListViewFilter. DoOnKeyUp(Sender: TObject; var Key: Word;112 procedure TListViewFilter.GridDoOnKeyUp(Sender: TObject; var Key: Word; 96 113 Shift: TShiftState); 97 114 begin 98 115 if Assigned(FOnChange) then 99 116 FOnChange(Self); 117 end; 118 119 procedure TListViewFilter.GridDoOnResize(Sender: TObject); 120 begin 121 FStringGrid1.DefaultRowHeight := FStringGrid1.Height; 100 122 end; 101 123 … … 113 135 FStringGrid1.Options := [goFixedHorzLine, goFixedVertLine, goVertLine, 114 136 goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goSmoothScroll]; 115 FStringGrid1.OnKeyUp := DoOnKeyUp; 137 FStringGrid1.OnKeyUp := GridDoOnKeyUp; 138 FStringGrid1.OnResize := GridDoOnResize; 116 139 end; 117 140 … … 119 142 var 120 143 I: Integer; 121 NewColumn: TGridColumn;122 144 begin 123 145 with FStringGrid1 do begin 124 Columns.Clear; 146 Options := Options - [goEditing, goAlwaysShowEditor]; 147 //Columns.Clear; 125 148 while Columns.Count > ListView.Columns.Count do Columns.Delete(Columns.Count - 1); 126 while Columns.Count < ListView.Columns.Count do NewColumn :=Columns.Add;149 while Columns.Count < ListView.Columns.Count do Columns.Add; 127 150 for I := 0 to ListView.Columns.Count - 1 do begin 128 151 Columns[I].Width := ListView.Columns[I].Width; 129 152 end; 153 Options := Options + [goEditing, goAlwaysShowEditor]; 130 154 end; 131 155 end; 132 156 133 157 function TListViewFilter.TextEntered: Boolean; 158 begin 159 Result := TextEnteredCount > 0; 160 end; 161 162 function TListViewFilter.TextEnteredCount: Integer; 134 163 var 135 164 I: Integer; 136 165 begin 137 Result := False;166 Result := 0; 138 167 for I := 0 to FStringGrid1.ColCount - 1 do begin 139 168 if FStringGrid1.Cells[I, 0] <> '' then begin 140 Result := True; 141 Break; 169 Inc(Result); 142 170 end; 143 171 end; 172 end; 173 174 function TListViewFilter.TextEnteredColumn(Index: Integer): Boolean; 175 begin 176 Result := FStringGrid1.Cells[Index, 0] <> ''; 144 177 end; 145 178 … … 153 186 { TListViewSort } 154 187 188 {$IFDEF WINDOWS} 189 procedure TListViewSort.NewListViewWindowProc(var AMsg: TMessage); 190 var 191 vColWidth: Integer; 192 vMsgNotify: TLMNotify absolute AMsg; 193 Code: Integer; 194 begin 195 // call the old WindowProc of ListView 196 FOldListViewWindowProc(AMsg); 197 198 // Currently we care only with WM_NOTIFY message 199 if AMsg.Msg = WM_NOTIFY then 200 begin 201 Code := NMHDR(PHDNotify(vMsgNotify.NMHdr)^.Hdr).Code; 202 case Code of 203 HDN_ENDTRACKA, HDN_ENDTRACKW: 204 DoColumnResized(PHDNotify(vMsgNotify.NMHdr)^.Item); 205 206 HDN_BEGINTRACKA, HDN_BEGINTRACKW: 207 DoColumnBeginResize(PHDNotify(vMsgNotify.NMHdr)^.Item); 208 209 HDN_TRACKA, HDN_TRACKW: 210 begin 211 vColWidth := -1; 212 if (PHDNotify(vMsgNotify.NMHdr)^.PItem<>nil) 213 and (PHDNotify(vMsgNotify.NMHdr)^.PItem^.Mask and HDI_WIDTH <> 0) 214 then 215 vColWidth := PHDNotify(vMsgNotify.NMHdr)^.PItem^.cxy; 216 217 DoColumnResizing(PHDNotify(vMsgNotify.NMHdr)^.Item, vColWidth); 218 end; 219 end; 220 end; 221 end; 222 {$ENDIF} 223 224 procedure TListViewSort.DoColumnBeginResize(const AColIndex: Integer); 225 begin 226 end; 227 228 procedure TListViewSort.DoColumnResizing(const AColIndex, AWidth: Integer); 229 begin 230 end; 231 232 procedure TListViewSort.DoColumnResized(const AColIndex: Integer); 233 begin 234 if Assigned(FOnColumnWidthChanged) then 235 FOnColumnWidthChanged(Self); 236 end; 155 237 156 238 procedure TListViewSort.ColumnClick(Sender: TObject; Column: TListColumn); … … 179 261 procedure TListViewSort.SetListView(const Value: TListView); 180 262 begin 263 if FListView = Value then Exit; 264 if Assigned(FListView) then 265 ListView.WindowProc := FOldListViewWindowProc; 181 266 FListView := Value; 182 267 FListView.OnColumnClick := ColumnClick; 183 268 FListView.OnCustomDrawItem := ListViewCustomDrawItem; 184 269 FListView.OnClick := ListViewClick; 270 FOldListViewWindowProc := FListView.WindowProc; 271 {$IFDEF WINDOWS} 272 FListView.WindowProc := NewListViewWindowProc; 273 {$ENDIF} 185 274 end; 186 275 … … 199 288 if ListView.Items.Count <> List.Count then 200 289 ListView.Items.Count := List.Count; 201 if Assigned(FOnCompareItem) then Sort(FOnCompareItem);290 if Assigned(FOnCompareItem) and (Order <> soNone) then Sort(FOnCompareItem); 202 291 //ListView.Items[-1]; // Workaround for not show first row if selected 203 292 ListView.Refresh; … … 266 355 TP1: TPoint; 267 356 XBias, YBias: Integer; 268 OldColor: TColor; 357 PenColor: TColor; 358 BrushColor: TColor; 269 359 BiasTop, BiasLeft: Integer; 270 360 Rect1: TRect; … … 278 368 Item.Left := 0; 279 369 GetCheckBias(XBias, YBias, BiasTop, BiasLeft, ListView); 280 OldColor := ListView.Canvas.Pen.Color; 370 PenColor := ListView.Canvas.Pen.Color; 371 BrushColor := ListView.Canvas.Brush.Color; 281 372 //TP1 := Item.GetPosition; 282 373 lRect := Item.DisplayRect(drBounds); // Windows 7 workaround … … 321 412 end; 322 413 //ListView.Canvas.Brush.Color := ListView.Color; 323 ListView.Canvas.Brush.Color := clWindow;324 ListView.Canvas.Pen.Color := OldColor;414 ListView.Canvas.Brush.Color := BrushColor; 415 ListView.Canvas.Pen.Color := PenColor; 325 416 end; 326 417
Note:
See TracChangeset
for help on using the changeset viewer.