source: trunk/Packages/Common/ULastOpenedList.pas

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 3.2 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 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
33procedure Register;
34
35
36implementation
37
38procedure Register;
39begin
40 RegisterComponents('Samples', [TLastOpenedList]);
41end;
42
43
44{ TLastOpenedList }
45
46procedure TLastOpenedList.SetMaxCount(AValue: Integer);
47begin
48 if FMaxCount = AValue then Exit;
49 FMaxCount := AValue;
50 if FMaxCount < 0 then FMaxCount := 0;
51 LimitMaxCount;
52end;
53
54procedure TLastOpenedList.LimitMaxCount;
55begin
56 while Items.Count > MaxCount do
57 Items.Delete(Items.Count - 1);
58end;
59
60constructor TLastOpenedList.Create(AOwner: TComponent);
61begin
62 inherited;
63 Items := TStringList.Create;
64 MaxCount := 10;
65end;
66
67destructor TLastOpenedList.Destroy;
68begin
69 Items.Free;
70 inherited;
71end;
72
73procedure TLastOpenedList.LoadToMenuItem(MenuItem: TMenuItem; ClickAction: TNotifyEvent);
74var
75 NewMenuItem: TMenuItem;
76 I: Integer;
77begin
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;
87end;
88
89procedure TLastOpenedList.LoadFromRegistry(Context: TRegistryContext);
90var
91 I: Integer;
92 Registry: TRegistryEx;
93 FileName: string;
94begin
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;
112end;
113
114procedure TLastOpenedList.SaveToRegistry(Context: TRegistryContext);
115var
116 I: Integer;
117 Registry: TRegistryEx;
118begin
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;
129end;
130
131procedure TLastOpenedList.AddItem(FileName:string);
132begin
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);
138end;
139
140end.
141
Note: See TracBrowser for help on using the repository browser.