Changeset 374 for Common


Ignore:
Timestamp:
Jun 13, 2012, 9:38:59 AM (12 years ago)
Author:
chronos
Message:
  • Modified: TLastOpenedList transformed to TComponent descendant.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/ULastOpenedList.pas

    r369 r374  
    1212  { TLastOpenedList }
    1313
    14   TLastOpenedList = class(TStringList)
     14  TLastOpenedList = class(TComponent)
    1515  private
     16    FMaxCount: Integer;
    1617    FOnChange: TNotifyEvent;
     18    procedure SetMaxCount(AValue: Integer);
     19    procedure LimitMaxCount;
    1720  public
    18     MaxCount: Integer;
     21    Items: TStringList;
    1922    ClickAction: TNotifyEvent;
    20     constructor Create;
     23    constructor Create(AOwner: TComponent); override;
    2124    destructor Destroy; override;
    2225    procedure LoadToMenuItem(MenuItem: TMenuItem);
     
    2427    procedure SaveToRegistry(Root: HKEY; Key: string);
    2528    procedure AddItem(FileName: string);
     29    property MaxCount: Integer read FMaxCount write SetMaxCount;
    2630    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    2731  end;
     
    3135{ TLastOpenedList }
    3236
    33 constructor TLastOpenedList.Create;
     37procedure TLastOpenedList.SetMaxCount(AValue: Integer);
     38begin
     39  if FMaxCount = AValue then Exit;
     40  FMaxCount := AValue;
     41  if FMaxCount < 0 then FMaxCount := 0;
     42  LimitMaxCount;
     43end;
     44
     45procedure TLastOpenedList.LimitMaxCount;
     46begin
     47  while Items.Count > MaxCount do
     48    Items.Delete(Items.Count - 1);
     49end;
     50
     51constructor TLastOpenedList.Create(AOwner: TComponent);
    3452begin
    3553  inherited;
    36   MaxCount := 5;
     54  Items := TStringList.Create;
     55  MaxCount := 10;
    3756end;
    3857
    3958destructor TLastOpenedList.Destroy;
    4059begin
     60  Items.Free;
    4161  inherited;
    4262end;
     
    4969  if Assigned(MenuItem) then begin
    5070    MenuItem.Clear;
    51     for I := 0 to Count - 1 do begin
     71    for I := 0 to Items.Count - 1 do begin
    5272      NewMenuItem := TMenuItem.Create(MenuItem);
    53       NewMenuItem.Caption := Strings[I];
     73      NewMenuItem.Caption := Items[I];
    5474      NewMenuItem.OnClick := ClickAction;
    5575      MenuItem.Add(NewMenuItem);
     
    6989    RootKey := Root;
    7090    OpenKey(Key, True);
    71     Clear;
     91    Items.Clear;
    7292    I := 0;
    7393    while ValueExists('File' + IntToStr(I)) and (I < MaxCount) do begin
    7494      FileName := UTF8Encode(ReadStringWithDefault('File' + IntToStr(I), ''));
    75       if Trim(FileName) <> '' then inherited Add(FileName);
     95      if Trim(FileName) <> '' then Items.Add(FileName);
    7696      Inc(I);
    7797    end;
     
    93113    RootKey := Root;
    94114    OpenKey(Key, True);
    95     for I := 0 to Count - 1 do
    96       WriteString('File' + IntToStr(I), UTF8Decode(Strings[I]));
     115    for I := 0 to Items.Count - 1 do
     116      WriteString('File' + IntToStr(I), UTF8Decode(Items[I]));
    97117  finally
    98118    Free;
     
    102122procedure TLastOpenedList.AddItem(FileName:string);
    103123begin
    104   if IndexOf(FileName) <> -1 then Delete(IndexOf(FileName));
    105   Insert(0, FileName);
    106   while Count > MaxCount do
    107     Delete(Count - 1);
    108 
     124  if Items.IndexOf(FileName) <> -1 then Items.Delete(Items.IndexOf(FileName));
     125  Items.Insert(0, FileName);
     126  LimitMaxCount;
    109127  if Assigned(FOnChange) then
    110128    FOnChange(Self);
Note: See TracChangeset for help on using the changeset viewer.