Ignore:
Timestamp:
May 8, 2013, 2:53:22 PM (12 years ago)
Author:
chronos
Message:
  • Fixed: Make only one form focused at once.
  • Modified: Enhanced TList generic class to support more methods.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Xvcl/Xvcl.Generics.pas

    r23 r24  
    22
    33interface
     4
     5uses
     6  SysUtils;
    47
    58type
     
    2427    function GetEnumerator: TEnumerator;
    2528    function Add(Item: T): Integer;
    26     procedure Remove(Item: T);
     29    function Remove(Item: T): Integer;
     30    procedure Delete(Index: Integer);
     31    procedure Move(FromIndex, ToIndex: Integer);
     32    function IndexOf(Item: T): Integer;
     33    function Compare(Item1, Item2: T): Integer;
     34    procedure Exchange(Index1, Index2: Integer);
    2735    property Count: Integer read FCount write SetCount;
    2836    property Items[Index: Integer]: T read GetItem write SetItem; default;
     
    5563end;
    5664
     65function TList<T>.Compare(Item1, Item2: T): Integer;
     66begin
     67  if CompareMem(Pointer(@Item1), Pointer(@Item2), SizeOf(T)) then Result := 0
     68    else Result := -1;
     69end;
     70
     71procedure TList<T>.Delete(Index: Integer);
     72var
     73  I: Integer;
     74begin
     75  if (Index >= 0) and (Index < Count) then
     76  for I := Index to Count - 2 do
     77    Items[I] := Items[I + 1];
     78end;
     79
     80procedure TList<T>.Exchange(Index1, Index2: Integer);
     81var
     82  Temp: T;
     83begin
     84  Temp := Items[Index2];
     85  Items[Index2] := Items[Index1];
     86  Items[Index1] := Temp;
     87end;
     88
    5789function TList<T>.GetEnumerator: TEnumerator;
    5890begin
     
    6799end;
    68100
    69 procedure TList<T>.Remove(Item: T);
     101function TList<T>.IndexOf(Item: T): Integer;
     102var
     103  I: Integer;
    70104begin
     105  I := 0;
     106  while (I < Count) and (Compare(Items[I], Item) <> 0) do
     107    Inc(I);
     108  if I < Count then Result := I
     109    else Result := -1;
     110end;
    71111
     112procedure TList<T>.Move(FromIndex, ToIndex: Integer);
     113var
     114  I: Integer;
     115  Temp: T;
     116begin
     117  if (FromIndex >= 0) and (FromIndex < Count) and
     118  (ToIndex >= 0) and (ToIndex < Count) then begin
     119    if FromIndex > ToIndex then begin
     120      Temp := Items[ToIndex];
     121      for I := ToIndex to FromIndex - 1 do
     122        Items[I] := Items[I + 1];
     123      Items[FromIndex] := Temp;
     124    end else
     125    if FromIndex < ToIndex then begin
     126      Temp := Items[FromIndex];
     127      for I := FromIndex to ToIndex - 1 do
     128        Items[I] := Items[I + 1];
     129      Items[ToIndex] := Temp;
     130    end;
     131  end;
     132end;
     133
     134function TList<T>.Remove(Item: T): Integer;
     135begin
     136  Result := IndexOf(Item);
     137  if Result >= 0 then Delete(Result);
    72138end;
    73139
Note: See TracChangeset for help on using the changeset viewer.