Changeset 130


Ignore:
Timestamp:
Jan 19, 2011, 11:07:07 AM (13 years ago)
Author:
george
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ListViewSort/UListViewSort.pas

    r99 r130  
    66
    77uses
    8   Windows, Types, Classes, ComCtrls, Contnrs, Graphics, SysUtils, StdCtrls, Controls,
    9   DateUtils, CommCtrl;
     8  Windows, Types, Classes, ComCtrls, Contnrs, Graphics, SysUtils, StdCtrls,
     9  Controls, DateUtils, CommCtrl, Dialogs, SpecializedList;
    1010
    1111type
     
    2828    procedure SetListView(const Value: TListView);
    2929    procedure ColumnClick(Sender: TObject; Column: TListColumn);
    30     procedure QuickSort(SortList: TObjectList; L, R: Integer;
    31       SCompare: TCompareEvent);
    3230    procedure Sort(Compare: TCompareEvent);
    3331    procedure DrawCheckMark(Item: TListItem; Checked: Boolean);
     
    4139    procedure SetOrder(const Value: TSortOrder);
    4240  public
    43     List: TObjectList;
    44     Source: TObjectList;
     41    List: TListObject;
     42    Source: TListObject;
    4543    constructor Create;
    4644    destructor Destroy; override;
     
    10098begin
    10199  if (List.Count > 0) then
    102     QuickSort(List, 0, List.Count - 1, Compare);
    103 end;
    104 
    105 procedure TListViewSort.QuickSort(SortList: TObjectList; L, R: Integer;
    106   SCompare: TCompareEvent);
    107 var
    108   I, J: Integer;
    109   P, T: Pointer;
    110 begin
    111   repeat
    112     I := L;
    113     J := R;
    114     P := SortList[(L + R) shr 1];
    115     repeat
    116       while SCompare(SortList[I], P) < 0 do
    117         Inc(I);
    118       while SCompare(SortList[J], P) > 0 do
    119         Dec(J);
    120       if I <= J then
    121       begin
    122         T := SortList[I];
    123         SortList[I] := SortList[J];
    124         SortList[J] := T;
    125         Inc(I);
    126         Dec(J);
    127       end;
    128     until I > J;
    129     if L < J then
    130       QuickSort(SortList, L, J, SCompare);
    131     L := I;
    132   until I >= R;
    133 end;
    134 
     100    List.Sort(Compare);
     101end;
    135102
    136103procedure TListViewSort.Refresh;
     
    143110    ListView.Items.Count := List.Count;
    144111  if Assigned(FOnCompareItem) then Sort(FOnCompareItem);
     112  ListView.Items[-1]; // Workaround for not show first row if selected
    145113  ListView.Refresh;
     114  // Workaround for not working item selection on first row
     115  if not Assigned(ListView.Selected) then begin
     116    ListView.Items.Count := 0;
     117    ListView.Items.Count := List.Count;
     118  end;
     119  //if ListView.Items.Count > 0 then
     120  //    ListView.Items[0].Selected := True;
     121  //ListView.Selected := nil;
    146122  UpdateColumns;
    147123end;
    148124
    149125const
    150   W_64: Word = 64; {Width of thumbnail in ICON view mode}
    151   H_64: Word = 64; {Height of thumbnail size}
    152   CheckWidth: Word = 14; {Width of check mark box}
    153   CheckHeight: Word = 14; {Height of checkmark}
    154   CheckBiasTop: Word = 2; {This aligns the checkbox to be in centered}
    155   CheckBiasLeft: Word = 3; {In the row of the list item display}
     126  W_64: Integer = 64; {Width of thumbnail in ICON view mode}
     127  H_64: Integer = 64; {Height of thumbnail size}
     128  CheckWidth: Integer = 14; {Width of check mark box}
     129  CheckHeight: Integer = 14; {Height of checkmark}
     130  CheckBiasTop: Integer = 2; {This aligns the checkbox to be in centered}
     131  CheckBiasLeft: Integer = 3; {In the row of the list item display}
    156132
    157133function TListViewSort.CompareBoolean(Value1, Value2: Boolean): Integer;
     
    184160constructor TListViewSort.Create;
    185161begin
    186   List := TObjectList.Create;
     162  List := TListObject.Create;
    187163  List.OwnsObjects := False;
    188164end;
     
    202178  BiasTop, BiasLeft: Integer;
    203179  Rect1: TRect;
    204 begin
     180  lRect: TRect;
     181  ItemLeft: Integer;
     182begin
     183  Item.Left := 0;
    205184  GetCheckBias(XBias, YBias, BiasTop, BiasLeft, ListView);
    206185  OldColor := ListView.Canvas.Pen.Color;
    207   TP1 := Item.GetPosition;
     186  //TP1 := Item.GetPosition;
     187  lRect := Item.DisplayRect(drBounds); // Windows 7 workaround
     188  TP1.X := lRect.Left;
     189  TP1.Y := lRect.Top;
     190  //ShowMessage(IntToStr(Item.Index) + ', ' + IntToStr(GetScrollPos(Item.ListView.Handle, SB_VERT)) + '  ' +
     191  //  IntToHex(Integer(Item), 8) + ', ' + IntToStr(TP1.X) + ', ' + IntToStr(TP1.Y));
     192
    208193//  if Checked then
    209194    ListView.Canvas.Brush.Color := clWhite;
    210   Rect1.Left := Item.Left - CheckWidth - BiasLeft + 1 + XBias;
     195  ItemLeft := Item.Left;
     196  ItemLeft := 23; // Windows 7 workaround
     197 
     198  Rect1.Left := ItemLeft - CheckWidth - BiasLeft + 1 + XBias;
     199  //ShowMessage(IntToStr(Tp1.Y) + ', ' + IntToStr(BiasTop) + ', ' + IntToStr(XBias));
    211200  Rect1.Top := Tp1.Y + BiasTop + 1 + YBias;
    212   Rect1.Right := Item.Left - BiasLeft - 1 + XBias;
     201  Rect1.Right := ItemLeft - BiasLeft - 1 + XBias;
    213202  Rect1.Bottom := Tp1.Y + BiasTop + CheckHeight - 1 + YBias;
     203  //ShowMessage(IntToStr(Rect1.Left) + ', ' + IntToStr(Rect1.Top) + ', ' + IntToStr(Rect1.Right) + ', ' + IntToStr(Rect1.Bottom));
     204
    214205  ListView.Canvas.FillRect(Rect1);
    215206  //if Checked then ListView.Canvas.Brush.Color := clBlack
     
    220211  if Checked then begin
    221212    ListView.Canvas.Pen.Color := clBlack;
    222     ListView.Canvas.MoveTo(Item.Left - BiasLeft - 2 + XBias - 2,
     213    ListView.Canvas.MoveTo(ItemLeft - BiasLeft - 2 + XBias - 2,
    223214      Tp1.Y + BiasTop + 3 + YBias);
    224     ListView.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth div 2) + XBias,
     215    ListView.Canvas.LineTo(ItemLeft - BiasLeft - (CheckWidth div 2) + XBias,
    225216      Tp1.Y + BiasTop + (CheckHeight - 4) + YBias);
    226     ListView.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth - 3) + XBias,
     217    ListView.Canvas.LineTo(ItemLeft - BiasLeft - (CheckWidth - 3) + XBias,
    227218      Tp1.Y + BiasTop + (CheckHeight div 2) + YBias - 1);
    228219
    229     ListView.Canvas.MoveTo(Item.Left - BiasLeft - 2 - 1 + XBias - 2,
     220    ListView.Canvas.MoveTo(ItemLeft - BiasLeft - 2 - 1 + XBias - 2,
    230221      Tp1.Y + BiasTop + 3 + YBias);
    231     ListView.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth div 2) - 1 + XBias,
     222    ListView.Canvas.LineTo(ItemLeft - BiasLeft - (CheckWidth div 2) - 1 + XBias,
    232223      Tp1.Y + BiasTop + (CheckHeight - 4) + YBias);
    233     ListView.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth - 3) - 1 + XBias,
     224    ListView.Canvas.LineTo(ItemLeft - BiasLeft - (CheckWidth - 3) - 1 + XBias,
    234225      Tp1.Y + BiasTop + (CheckHeight div 2) + YBias - 1);
    235226  end;
    236   ListView.Canvas.Brush.Color := ListView.Color;
     227  //ListView.Canvas.Brush.Color := ListView.Color;
     228  ListView.Canvas.Brush.Color := clWindow;
    237229  ListView.Canvas.Pen.Color := OldColor;
    238230end;
     
    260252  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    261253begin
    262   if ListView.Checkboxes then
    263     DrawCheckMark(Item, Item.Checked);
    264   if Assigned(FOnCustomDraw) then
    265     FOnCustomDraw(Sender, Item, State, DefaultDraw); 
     254  if Assigned(Item) then begin
     255    if ListView.Checkboxes then
     256      DrawCheckMark(Item, Item.Checked);
     257    if Assigned(FOnCustomDraw) then
     258      FOnCustomDraw(Sender, Item, State, DefaultDraw);
     259  end;
    266260end;
    267261
     
    270264  Item: TListItem;
    271265  Pos: TPoint;
     266  DefaultDraw: Boolean;
    272267begin
    273268  Pos := ListView.ScreenToClient(Mouse.CursorPos);
    274269  Item := ListView.GetItemAt(Pos.X, Pos.Y);
     270  //ShowMessage(IntToStr(Item.Index) + ', ' + IntToStr(Pos.X) + ', ' + IntToStr(Pos.Y));
    275271  if Assigned(Item) and (Pos.X < 20) then begin
     272
    276273    Item.Checked := not Item.Checked;
    277     ListView.UpdateItems(Item.Index, Item.Index);
     274    //ShowMessage(IntToStr(Item.Index) + ', ' +BoolToStr(Item.Checked));
    278275    if Assigned(ListView.OnChange) then
    279276      ListView.OnChange(Self, Item, ctState);
     277    DefaultDraw := False;
     278    ListViewCustomDrawItem(ListView, Item, [], DefaultDraw);
     279      //ListView.UpdateItems(Item.Index, Item.Index);
    280280  end;
    281281end;
Note: See TracChangeset for help on using the changeset viewer.