Ignore:
Timestamp:
Mar 22, 2018, 8:31:19 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Update Common and CollTranslator packages to fix build under Lazarus 1.8.
  • Fixed: Some memory leaks.
File:
1 edited

Legend:

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

    r10 r15  
    99uses
    1010  {$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;
    1213
    1314type
     
    1819  TCompareEvent = function (Item1, Item2: TObject): Integer of object;
    1920  TListFilterEvent = procedure (ListViewSort: TListViewSort) of object;
     21
     22  { TListViewSort }
    2023
    2124  TListViewSort = class(TComponent)
     
    2831    FColumn: Integer;
    2932    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);
    3038    procedure SetListView(const Value: TListView);
    3139    procedure ColumnClick(Sender: TObject; Column: TListColumn);
     
    4048    procedure SetColumn(const Value: Integer);
    4149    procedure SetOrder(const Value: TSortOrder);
     50    {$IFDEF WINDOWS}
     51    procedure NewListViewWindowProc(var AMsg: TMessage);
     52    {$ENDIF}
    4253  public
    4354    List: TListObject;
     
    5869    property OnCustomDraw: TLVCustomDrawItemEvent read FOnCustomDraw
    5970      write FOnCustomDraw;
     71    property OnColumnWidthChanged: TNotifyEvent read FOnColumnWidthChanged
     72      write FOnColumnWidthChanged;
    6073    property Column: Integer read FColumn write SetColumn;
    6174    property Order: TSortOrder read FOrder write SetOrder;
     
    6881    FOnChange: TNotifyEvent;
    6982    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);
    7185  public
    7286    constructor Create(AOwner: TComponent); override;
    7387    procedure UpdateFromListView(ListView: TListView);
    7488    function TextEntered: Boolean;
     89    function TextEnteredCount: Integer;
     90    function TextEnteredColumn(Index: Integer): Boolean;
    7591    function GetColValue(Index: Integer): string;
    7692    property StringGrid: TStringGrid read FStringGrid1 write FStringGrid1;
     
    7995    property Align;
    8096    property Anchors;
     97    property BorderSpacing;
    8198  end;
    8299
     
    93110{ TListViewFilter }
    94111
    95 procedure TListViewFilter.DoOnKeyUp(Sender: TObject; var Key: Word;
     112procedure TListViewFilter.GridDoOnKeyUp(Sender: TObject; var Key: Word;
    96113  Shift: TShiftState);
    97114begin
    98115  if Assigned(FOnChange) then
    99116    FOnChange(Self);
     117end;
     118
     119procedure TListViewFilter.GridDoOnResize(Sender: TObject);
     120begin
     121  FStringGrid1.DefaultRowHeight := FStringGrid1.Height;
    100122end;
    101123
     
    113135  FStringGrid1.Options := [goFixedHorzLine, goFixedVertLine, goVertLine,
    114136    goHorzLine, goRangeSelect, goEditing, goAlwaysShowEditor, goSmoothScroll];
    115   FStringGrid1.OnKeyUp := DoOnKeyUp;
     137  FStringGrid1.OnKeyUp := GridDoOnKeyUp;
     138  FStringGrid1.OnResize := GridDoOnResize;
    116139end;
    117140
     
    119142var
    120143  I: Integer;
    121   NewColumn: TGridColumn;
    122144begin
    123145  with FStringGrid1 do begin
    124     Columns.Clear;
     146    Options := Options - [goEditing, goAlwaysShowEditor];
     147    //Columns.Clear;
    125148    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;
    127150    for I := 0 to ListView.Columns.Count - 1 do begin
    128151      Columns[I].Width := ListView.Columns[I].Width;
    129152    end;
     153    Options := Options + [goEditing, goAlwaysShowEditor];
    130154  end;
    131155end;
    132156
    133157function TListViewFilter.TextEntered: Boolean;
     158begin
     159  Result := TextEnteredCount > 0;
     160end;
     161
     162function TListViewFilter.TextEnteredCount: Integer;
    134163var
    135164  I: Integer;
    136165begin
    137   Result := False;
     166  Result := 0;
    138167  for I := 0 to FStringGrid1.ColCount - 1 do begin
    139168    if FStringGrid1.Cells[I, 0] <> '' then begin
    140       Result := True;
    141       Break;
     169      Inc(Result);
    142170    end;
    143171  end;
     172end;
     173
     174function TListViewFilter.TextEnteredColumn(Index: Integer): Boolean;
     175begin
     176  Result := FStringGrid1.Cells[Index, 0] <> '';
    144177end;
    145178
     
    153186{ TListViewSort }
    154187
     188{$IFDEF WINDOWS}
     189procedure TListViewSort.NewListViewWindowProc(var AMsg: TMessage);
     190var
     191  vColWidth: Integer;
     192  vMsgNotify: TLMNotify absolute AMsg;
     193  Code: Integer;
     194begin
     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;
     221end;
     222{$ENDIF}
     223
     224procedure TListViewSort.DoColumnBeginResize(const AColIndex: Integer);
     225begin
     226end;
     227
     228procedure TListViewSort.DoColumnResizing(const AColIndex, AWidth: Integer);
     229begin
     230end;
     231
     232procedure TListViewSort.DoColumnResized(const AColIndex: Integer);
     233begin
     234  if Assigned(FOnColumnWidthChanged) then
     235    FOnColumnWidthChanged(Self);
     236end;
    155237
    156238procedure TListViewSort.ColumnClick(Sender: TObject; Column: TListColumn);
     
    179261procedure TListViewSort.SetListView(const Value: TListView);
    180262begin
     263  if FListView = Value then Exit;
     264  if Assigned(FListView) then
     265    ListView.WindowProc := FOldListViewWindowProc;
    181266  FListView := Value;
    182267  FListView.OnColumnClick := ColumnClick;
    183268  FListView.OnCustomDrawItem := ListViewCustomDrawItem;
    184269  FListView.OnClick := ListViewClick;
     270  FOldListViewWindowProc := FListView.WindowProc;
     271  {$IFDEF WINDOWS}
     272  FListView.WindowProc := NewListViewWindowProc;
     273  {$ENDIF}
    185274end;
    186275
     
    199288  if ListView.Items.Count <> List.Count then
    200289    ListView.Items.Count := List.Count;
    201   if Assigned(FOnCompareItem) then Sort(FOnCompareItem);
     290  if Assigned(FOnCompareItem) and (Order <> soNone) then Sort(FOnCompareItem);
    202291  //ListView.Items[-1]; // Workaround for not show first row if selected
    203292  ListView.Refresh;
     
    266355  TP1: TPoint;
    267356  XBias, YBias: Integer;
    268   OldColor: TColor;
     357  PenColor: TColor;
     358  BrushColor: TColor;
    269359  BiasTop, BiasLeft: Integer;
    270360  Rect1: TRect;
     
    278368  Item.Left := 0;
    279369  GetCheckBias(XBias, YBias, BiasTop, BiasLeft, ListView);
    280   OldColor := ListView.Canvas.Pen.Color;
     370  PenColor := ListView.Canvas.Pen.Color;
     371  BrushColor := ListView.Canvas.Brush.Color;
    281372  //TP1 := Item.GetPosition;
    282373  lRect := Item.DisplayRect(drBounds); // Windows 7 workaround
     
    321412  end;
    322413  //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;
    325416end;
    326417
Note: See TracChangeset for help on using the changeset viewer.