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