source: trunk/Demo/Packages/Common/ULastOpenedList.pas

Last change on this file was 60, checked in by chronos, 12 years ago
File size: 3.4 KB
Line 
1unit ULastOpenedList;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Registry, URegistry, Menus;
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 AddItem(FileName: string);
30 published
31 property MaxCount: Integer read FMaxCount write SetMaxCount;
32 property OnChange: TNotifyEvent read FOnChange write FOnChange;
33 end;
34
35procedure Register;
36
37
38implementation
39
40procedure Register;
41begin
42 RegisterComponents('Samples', [TLastOpenedList]);
43end;
44
45
46{ TLastOpenedList }
47
48procedure TLastOpenedList.SetMaxCount(AValue: Integer);
49begin
50 if FMaxCount = AValue then Exit;
51 FMaxCount := AValue;
52 if FMaxCount < 0 then FMaxCount := 0;
53 LimitMaxCount;
54end;
55
56procedure TLastOpenedList.LimitMaxCount;
57begin
58 while Items.Count > MaxCount do
59 Items.Delete(Items.Count - 1);
60end;
61
62procedure TLastOpenedList.ItemsChange(Sender: TObject);
63begin
64 DoChange;
65end;
66
67procedure TLastOpenedList.DoChange;
68begin
69 if Assigned(FOnChange) then
70 FOnChange(Self);
71end;
72
73constructor TLastOpenedList.Create(AOwner: TComponent);
74begin
75 inherited;
76 Items := TStringList.Create;
77 Items.OnChange := ItemsChange;
78 MaxCount := 10;
79end;
80
81destructor TLastOpenedList.Destroy;
82begin
83 Items.Free;
84 inherited;
85end;
86
87procedure TLastOpenedList.LoadToMenuItem(MenuItem: TMenuItem; ClickAction: TNotifyEvent);
88var
89 NewMenuItem: TMenuItem;
90 I: Integer;
91begin
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;
101end;
102
103procedure TLastOpenedList.LoadFromRegistry(Context: TRegistryContext);
104var
105 I: Integer;
106 Registry: TRegistryEx;
107 FileName: string;
108begin
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;
126end;
127
128procedure TLastOpenedList.SaveToRegistry(Context: TRegistryContext);
129var
130 I: Integer;
131 Registry: TRegistryEx;
132begin
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;
143end;
144
145procedure TLastOpenedList.AddItem(FileName:string);
146begin
147 if Items.IndexOf(FileName) <> -1 then Items.Delete(Items.IndexOf(FileName));
148 Items.Insert(0, FileName);
149 LimitMaxCount;
150 DoChange;
151end;
152
153end.
154
Note: See TracBrowser for help on using the repository browser.