1 | unit ULastOpenedList;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Registry, URegistry, Menus, XMLConf, DOM;
|
---|
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 LoadFromXMLConfig(XMLConfig: TXMLConfig; Path: string);
|
---|
30 | procedure SaveToXMLConfig(XMLConfig: TXMLConfig; Path: string);
|
---|
31 | procedure AddItem(FileName: string);
|
---|
32 | function GetFirstFileName: string;
|
---|
33 | published
|
---|
34 | property MaxCount: Integer read FMaxCount write SetMaxCount;
|
---|
35 | property OnChange: TNotifyEvent read FOnChange write FOnChange;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | procedure Register;
|
---|
39 |
|
---|
40 |
|
---|
41 | implementation
|
---|
42 |
|
---|
43 | procedure Register;
|
---|
44 | begin
|
---|
45 | RegisterComponents('Common', [TLastOpenedList]);
|
---|
46 | end;
|
---|
47 |
|
---|
48 |
|
---|
49 | { TLastOpenedList }
|
---|
50 |
|
---|
51 | procedure TLastOpenedList.SetMaxCount(AValue: Integer);
|
---|
52 | begin
|
---|
53 | if FMaxCount = AValue then Exit;
|
---|
54 | FMaxCount := AValue;
|
---|
55 | if FMaxCount < 0 then FMaxCount := 0;
|
---|
56 | LimitMaxCount;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TLastOpenedList.LimitMaxCount;
|
---|
60 | begin
|
---|
61 | while Items.Count > MaxCount do
|
---|
62 | Items.Delete(Items.Count - 1);
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure TLastOpenedList.ItemsChange(Sender: TObject);
|
---|
66 | begin
|
---|
67 | DoChange;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | procedure TLastOpenedList.DoChange;
|
---|
71 | begin
|
---|
72 | if Assigned(FOnChange) then
|
---|
73 | FOnChange(Self);
|
---|
74 | end;
|
---|
75 |
|
---|
76 | constructor TLastOpenedList.Create(AOwner: TComponent);
|
---|
77 | begin
|
---|
78 | inherited;
|
---|
79 | Items := TStringList.Create;
|
---|
80 | Items.OnChange := ItemsChange;
|
---|
81 | MaxCount := 10;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | destructor TLastOpenedList.Destroy;
|
---|
85 | begin
|
---|
86 | Items.Free;
|
---|
87 | inherited;
|
---|
88 | end;
|
---|
89 |
|
---|
90 | procedure TLastOpenedList.LoadToMenuItem(MenuItem: TMenuItem; ClickAction: TNotifyEvent);
|
---|
91 | var
|
---|
92 | NewMenuItem: TMenuItem;
|
---|
93 | I: Integer;
|
---|
94 | begin
|
---|
95 | if Assigned(MenuItem) then begin
|
---|
96 | MenuItem.Clear;
|
---|
97 | for I := 0 to Items.Count - 1 do begin
|
---|
98 | NewMenuItem := TMenuItem.Create(MenuItem);
|
---|
99 | NewMenuItem.Caption := Items[I];
|
---|
100 | NewMenuItem.OnClick := ClickAction;
|
---|
101 | MenuItem.Add(NewMenuItem);
|
---|
102 | end;
|
---|
103 | end;
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TLastOpenedList.LoadFromRegistry(Context: TRegistryContext);
|
---|
107 | var
|
---|
108 | I: Integer;
|
---|
109 | Registry: TRegistryEx;
|
---|
110 | FileName: string;
|
---|
111 | begin
|
---|
112 | Registry := TRegistryEx.Create;
|
---|
113 | with Registry do
|
---|
114 | try
|
---|
115 | RootKey := Context.RootKey;
|
---|
116 | OpenKey(Context.Key, True);
|
---|
117 | Items.Clear;
|
---|
118 | I := 0;
|
---|
119 | while ValueExists('File' + IntToStr(I)) and (I < MaxCount) do begin
|
---|
120 | FileName := UTF8Encode(ReadStringWithDefault('File' + IntToStr(I), ''));
|
---|
121 | if Trim(FileName) <> '' then Items.Add(FileName);
|
---|
122 | Inc(I);
|
---|
123 | end;
|
---|
124 | if Assigned(FOnChange) then
|
---|
125 | FOnChange(Self);
|
---|
126 | finally
|
---|
127 | Free;
|
---|
128 | end;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | procedure TLastOpenedList.SaveToRegistry(Context: TRegistryContext);
|
---|
132 | var
|
---|
133 | I: Integer;
|
---|
134 | Registry: TRegistryEx;
|
---|
135 | begin
|
---|
136 | Registry := TRegistryEx.Create;
|
---|
137 | with Registry do
|
---|
138 | try
|
---|
139 | RootKey := Context.RootKey;
|
---|
140 | OpenKey(Context.Key, True);
|
---|
141 | for I := 0 to Items.Count - 1 do
|
---|
142 | WriteString('File' + IntToStr(I), Items[I]);
|
---|
143 | finally
|
---|
144 | Free;
|
---|
145 | end;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TLastOpenedList.LoadFromXMLConfig(XMLConfig: TXMLConfig; Path: string
|
---|
149 | );
|
---|
150 | var
|
---|
151 | I: Integer;
|
---|
152 | Value: string;
|
---|
153 | Count: Integer;
|
---|
154 | begin
|
---|
155 | with XMLConfig do begin
|
---|
156 | Count := GetValue(DOMString(Path + '/Count'), 0);
|
---|
157 | if Count > MaxCount then Count := MaxCount;
|
---|
158 | Items.Clear;
|
---|
159 | for I := 0 to Count - 1 do begin
|
---|
160 | Value := string(GetValue(DOMString(Path + '/File' + IntToStr(I)), ''));
|
---|
161 | if Trim(Value) <> '' then Items.Add(Value);
|
---|
162 | end;
|
---|
163 | if Assigned(FOnChange) then
|
---|
164 | FOnChange(Self);
|
---|
165 | end;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | procedure TLastOpenedList.SaveToXMLConfig(XMLConfig: TXMLConfig; Path: string);
|
---|
169 | var
|
---|
170 | I: Integer;
|
---|
171 | begin
|
---|
172 | with XMLConfig do begin
|
---|
173 | SetValue(DOMString(Path + '/Count'), Items.Count);
|
---|
174 | for I := 0 to Items.Count - 1 do
|
---|
175 | SetValue(DOMString(Path + '/File' + IntToStr(I)), DOMString(Items[I]));
|
---|
176 | Flush;
|
---|
177 | end;
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TLastOpenedList.AddItem(FileName:string);
|
---|
181 | begin
|
---|
182 | if Items.IndexOf(FileName) <> -1 then Items.Delete(Items.IndexOf(FileName));
|
---|
183 | Items.Insert(0, FileName);
|
---|
184 | LimitMaxCount;
|
---|
185 | DoChange;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | function TLastOpenedList.GetFirstFileName: string;
|
---|
189 | begin
|
---|
190 | if Items.Count > 0 then Result := Items[0]
|
---|
191 | else Result := '';
|
---|
192 | end;
|
---|
193 |
|
---|
194 | end.
|
---|
195 |
|
---|