source: trunk/Packages/Common/ULastOpenedList.pas

Last change on this file was 38, checked in by chronos, 7 years ago
  • Modified: Updated Common package.
File size: 4.4 KB
Line 
1unit ULastOpenedList;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Registry, URegistry, Menus, XMLConf, DOM;
9
10type
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
37procedure Register;
38
39
40implementation
41
42procedure Register;
43begin
44 RegisterComponents('Common', [TLastOpenedList]);
45end;
46
47
48{ TLastOpenedList }
49
50procedure TLastOpenedList.SetMaxCount(AValue: Integer);
51begin
52 if FMaxCount = AValue then Exit;
53 FMaxCount := AValue;
54 if FMaxCount < 0 then FMaxCount := 0;
55 LimitMaxCount;
56end;
57
58procedure TLastOpenedList.LimitMaxCount;
59begin
60 while Items.Count > MaxCount do
61 Items.Delete(Items.Count - 1);
62end;
63
64procedure TLastOpenedList.ItemsChange(Sender: TObject);
65begin
66 DoChange;
67end;
68
69procedure TLastOpenedList.DoChange;
70begin
71 if Assigned(FOnChange) then
72 FOnChange(Self);
73end;
74
75constructor TLastOpenedList.Create(AOwner: TComponent);
76begin
77 inherited;
78 Items := TStringList.Create;
79 Items.OnChange := ItemsChange;
80 MaxCount := 10;
81end;
82
83destructor TLastOpenedList.Destroy;
84begin
85 Items.Free;
86 inherited;
87end;
88
89procedure TLastOpenedList.LoadToMenuItem(MenuItem: TMenuItem; ClickAction: TNotifyEvent);
90var
91 NewMenuItem: TMenuItem;
92 I: Integer;
93begin
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;
103end;
104
105procedure TLastOpenedList.LoadFromRegistry(Context: TRegistryContext);
106var
107 I: Integer;
108 Registry: TRegistryEx;
109 FileName: string;
110begin
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;
128end;
129
130procedure TLastOpenedList.SaveToRegistry(Context: TRegistryContext);
131var
132 I: Integer;
133 Registry: TRegistryEx;
134begin
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;
145end;
146
147procedure TLastOpenedList.LoadFromXMLConfig(XMLConfig: TXMLConfig; Path: string
148 );
149var
150 I: Integer;
151 Value: string;
152 Count: Integer;
153begin
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;
165end;
166
167procedure TLastOpenedList.SaveToXMLConfig(XMLConfig: TXMLConfig; Path: string);
168var
169 I: Integer;
170begin
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;
177end;
178
179procedure TLastOpenedList.AddItem(FileName:string);
180begin
181 if Items.IndexOf(FileName) <> -1 then Items.Delete(Items.IndexOf(FileName));
182 Items.Insert(0, FileName);
183 LimitMaxCount;
184 DoChange;
185end;
186
187end.
188
Note: See TracBrowser for help on using the repository browser.