| 1 | unit ULastOpenedList;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, Registry, URegistry, Menus;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TLastOpenedList }
|
|---|
| 13 |
|
|---|
| 14 | TLastOpenedList = class(TComponent)
|
|---|
| 15 | private
|
|---|
| 16 | FMaxCount: Integer;
|
|---|
| 17 | FOnChange: TNotifyEvent;
|
|---|
| 18 | procedure SetMaxCount(AValue: Integer);
|
|---|
| 19 | procedure LimitMaxCount;
|
|---|
| 20 | procedure ItemsChange(Sender: TObject);
|
|---|
| 21 | procedure DoChange;
|
|---|
| 22 | public
|
|---|
| 23 | Items: TStringList;
|
|---|
| 24 | constructor Create(AOwner: TComponent); override;
|
|---|
| 25 | destructor Destroy; override;
|
|---|
| 26 | procedure LoadToMenuItem(MenuItem: TMenuItem; ClickAction: TNotifyEvent);
|
|---|
| 27 | procedure LoadFromRegistry(Context: TRegistryContext);
|
|---|
| 28 | procedure SaveToRegistry(Context: TRegistryContext);
|
|---|
| 29 | procedure AddItem(FileName: string);
|
|---|
| 30 | published
|
|---|
| 31 | property MaxCount: Integer read FMaxCount write SetMaxCount;
|
|---|
| 32 | property OnChange: TNotifyEvent read FOnChange write FOnChange;
|
|---|
| 33 | end;
|
|---|
| 34 |
|
|---|
| 35 | procedure Register;
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | implementation
|
|---|
| 39 |
|
|---|
| 40 | procedure Register;
|
|---|
| 41 | begin
|
|---|
| 42 | RegisterComponents('Samples', [TLastOpenedList]);
|
|---|
| 43 | end;
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | { TLastOpenedList }
|
|---|
| 47 |
|
|---|
| 48 | procedure TLastOpenedList.SetMaxCount(AValue: Integer);
|
|---|
| 49 | begin
|
|---|
| 50 | if FMaxCount = AValue then Exit;
|
|---|
| 51 | FMaxCount := AValue;
|
|---|
| 52 | if FMaxCount < 0 then FMaxCount := 0;
|
|---|
| 53 | LimitMaxCount;
|
|---|
| 54 | end;
|
|---|
| 55 |
|
|---|
| 56 | procedure TLastOpenedList.LimitMaxCount;
|
|---|
| 57 | begin
|
|---|
| 58 | while Items.Count > MaxCount do
|
|---|
| 59 | Items.Delete(Items.Count - 1);
|
|---|
| 60 | end;
|
|---|
| 61 |
|
|---|
| 62 | procedure TLastOpenedList.ItemsChange(Sender: TObject);
|
|---|
| 63 | begin
|
|---|
| 64 | DoChange;
|
|---|
| 65 | end;
|
|---|
| 66 |
|
|---|
| 67 | procedure TLastOpenedList.DoChange;
|
|---|
| 68 | begin
|
|---|
| 69 | if Assigned(FOnChange) then
|
|---|
| 70 | FOnChange(Self);
|
|---|
| 71 | end;
|
|---|
| 72 |
|
|---|
| 73 | constructor TLastOpenedList.Create(AOwner: TComponent);
|
|---|
| 74 | begin
|
|---|
| 75 | inherited;
|
|---|
| 76 | Items := TStringList.Create;
|
|---|
| 77 | Items.OnChange := ItemsChange;
|
|---|
| 78 | MaxCount := 10;
|
|---|
| 79 | end;
|
|---|
| 80 |
|
|---|
| 81 | destructor TLastOpenedList.Destroy;
|
|---|
| 82 | begin
|
|---|
| 83 | Items.Free;
|
|---|
| 84 | inherited;
|
|---|
| 85 | end;
|
|---|
| 86 |
|
|---|
| 87 | procedure TLastOpenedList.LoadToMenuItem(MenuItem: TMenuItem; ClickAction: TNotifyEvent);
|
|---|
| 88 | var
|
|---|
| 89 | NewMenuItem: TMenuItem;
|
|---|
| 90 | I: Integer;
|
|---|
| 91 | begin
|
|---|
| 92 | if Assigned(MenuItem) then begin
|
|---|
| 93 | MenuItem.Clear;
|
|---|
| 94 | for I := 0 to Items.Count - 1 do begin
|
|---|
| 95 | NewMenuItem := TMenuItem.Create(MenuItem);
|
|---|
| 96 | NewMenuItem.Caption := Items[I];
|
|---|
| 97 | NewMenuItem.OnClick := ClickAction;
|
|---|
| 98 | MenuItem.Add(NewMenuItem);
|
|---|
| 99 | end;
|
|---|
| 100 | end;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | procedure TLastOpenedList.LoadFromRegistry(Context: TRegistryContext);
|
|---|
| 104 | var
|
|---|
| 105 | I: Integer;
|
|---|
| 106 | Registry: TRegistryEx;
|
|---|
| 107 | FileName: string;
|
|---|
| 108 | begin
|
|---|
| 109 | Registry := TRegistryEx.Create;
|
|---|
| 110 | with Registry do
|
|---|
| 111 | try
|
|---|
| 112 | RootKey := Context.RootKey;
|
|---|
| 113 | OpenKey(Context.Key, True);
|
|---|
| 114 | Items.Clear;
|
|---|
| 115 | I := 0;
|
|---|
| 116 | while ValueExists('File' + IntToStr(I)) and (I < MaxCount) do begin
|
|---|
| 117 | FileName := UTF8Encode(ReadStringWithDefault('File' + IntToStr(I), ''));
|
|---|
| 118 | if Trim(FileName) <> '' then Items.Add(FileName);
|
|---|
| 119 | Inc(I);
|
|---|
| 120 | end;
|
|---|
| 121 | if Assigned(FOnChange) then
|
|---|
| 122 | FOnChange(Self);
|
|---|
| 123 | finally
|
|---|
| 124 | Free;
|
|---|
| 125 | end;
|
|---|
| 126 | end;
|
|---|
| 127 |
|
|---|
| 128 | procedure TLastOpenedList.SaveToRegistry(Context: TRegistryContext);
|
|---|
| 129 | var
|
|---|
| 130 | I: Integer;
|
|---|
| 131 | Registry: TRegistryEx;
|
|---|
| 132 | begin
|
|---|
| 133 | Registry := TRegistryEx.Create;
|
|---|
| 134 | with Registry do
|
|---|
| 135 | try
|
|---|
| 136 | RootKey := Context.RootKey;
|
|---|
| 137 | OpenKey(Context.Key, True);
|
|---|
| 138 | for I := 0 to Items.Count - 1 do
|
|---|
| 139 | WriteString('File' + IntToStr(I), UTF8Decode(Items[I]));
|
|---|
| 140 | finally
|
|---|
| 141 | Free;
|
|---|
| 142 | end;
|
|---|
| 143 | end;
|
|---|
| 144 |
|
|---|
| 145 | procedure TLastOpenedList.AddItem(FileName:string);
|
|---|
| 146 | begin
|
|---|
| 147 | if Items.IndexOf(FileName) <> -1 then Items.Delete(Items.IndexOf(FileName));
|
|---|
| 148 | Items.Insert(0, FileName);
|
|---|
| 149 | LimitMaxCount;
|
|---|
| 150 | DoChange;
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | end.
|
|---|
| 154 |
|
|---|