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