Debian Codegraph @

src/lcl/castlelclrecentfiles.pas

raw
1 {
2 Copyright 2006-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Manage a list of recently opened files, and show a menu in Lazarus menu.
17 See TRecentFiles class. }
18 unit CastleLCLRecentFiles;
19
20 {$I castleconf.inc}
21
22 interface
23
24 uses Menus, CastleRecentFiles;
25
26 type
27 { Manage a list of recently opened files, and show a menu in Lazarus. }
28 TCastleRecentFiles = class(TRecentFiles)
29 private
30 FNextMenuItem: TMenuItem;
31 FirstSeparator: TMenuItem;
32 procedure SetNextMenuItem(Value: TMenuItem);
33 protected
34 procedure MenuCreate; override;
35
36 { Destroy the menu. Internal (do not call this directly, it's only called
37 by parent class).
38 Note that it doesn't read Urls list at all, it only
39 depends on FirstSeparator value. }
40 procedure MenuDestroy; override;
41 public
42 { Determines the placement of "recent files" list inside your menu.
43
44 If this is not @nil then each update to the recent files
45 list (for example by @link(Add) method) will result in the menu
46 items right before NextMenuItem to be updated.
47
48 Also changing the value of this property will result in one-time
49 update of the menu. Menu at the place of old NextMenuItem
50 will be removed and menu at the place of new NextMenuItem will
51 get created.
52
53 Usually NextMenuItem should be the separator that divides
54 the print and properties menu sections and the close menu sections.
55 That's consistent with other GNOME programs, and this is what we usually
56 try to follow. }
57 property NextMenuItem: TMenuItem read FNextMenuItem write SetNextMenuItem;
58 end;
59
60 implementation
61
62 uses SysUtils, Classes, CastleLCLUtils, CastleUriUtils;
63
64 { TMenuRecentItem ------------------------------------------------------------ }
65
66 type
67 TMenuRecentItem = class(TMenuItem)
68 private
69 FUrl: String;
70 public
71 { Constructor. Number is 1-based number of this recent file entry. }
72 constructor Create(AOwner: TComponent;
73 const Number: Integer; const AUrl: String); reintroduce;
74 public
75 OnOpenRecent: TOnOpenRecent;
76 property Url: String read FUrl;
77 procedure Click; override;
78 end;
79
80 constructor TMenuRecentItem.Create(AOwner: TComponent;
81 const Number: Integer; const AUrl: String);
82 var
83 S: string;
84 begin
85 FUrl := AUrl;
86
87 if Number <= 9 then
88 S := '&' + IntToStr(Number) else
89 if Number = 10 then
90 S := '1&0' else
91 S := IntToStr(Number);
92 S += '. ' + SQuoteLCLCaption(UriCaption(Url));
93
94 inherited Create(AOwner);
95
96 Caption := S;
97 end;
98
99 procedure TMenuRecentItem.Click;
100 begin
101 inherited;
102 if Assigned(OnOpenRecent) then
103 OnOpenRecent(Url);
104 end;
105
106 { TCastleRecentFiles -------------------------------------------------------------- }
107
108 procedure TCastleRecentFiles.MenuCreate;
109 var
110 I: Integer;
111 ParentMenu: TMenuItem;
112 Position: Cardinal;
113 MenuRecentOpen: TMenuRecentItem;
114 begin
115 inherited;
116
117 { Add recent files menu }
118 if (NextMenuItem <> nil) and (NextMenuItem.Parent <> nil) and
119 { When Urls.Count = 0 then we don't want to add anything,
120 even FirstSeparator. }
121 (Urls.Count > 0) then
122 begin
123 ParentMenu := NextMenuItem.Parent;
124 Position := NextMenuItem.MenuIndex;
125 FirstSeparator := TMenuItem.Create(nil);
126 FirstSeparator.Caption := '-';
127 ParentMenu.Insert(Position, FirstSeparator);
128 for I := 0 to Urls.Count - 1 do
129 begin
130 MenuRecentOpen := TMenuRecentItem.Create(nil, I+1, Urls[I]);
131 MenuRecentOpen.OnOpenRecent := OnOpenRecent;
132 ParentMenu.Insert(Position + Cardinal(I) + 1, MenuRecentOpen);
133 end;
134 end;
135 end;
136
137 procedure TCastleRecentFiles.MenuDestroy;
138 var
139 ParentMenu: TMenuItem;
140 Position: Cardinal;
141 begin
142 { Remove recent files menu from old NextMenuItem }
143 if (NextMenuItem <> nil) and
144 { Checks for below are to safeguard against cases
145 when you could assign NextMenuItem before it had
146 any ParentMenu, or if you somehow rearranged your menu,
147 or if Urls list was empty when you assigned NextMenuItem
148 (in this case FirstSeparator is left nil, but later it should
149 be created after the 1st add). }
150 (NextMenuItem.Parent <> nil) and
151 (FirstSeparator <> nil) and
152 (NextMenuItem.Parent = FirstSeparator.Parent) then
153 begin
154 ParentMenu := NextMenuItem.Parent;
155 Position := FirstSeparator.MenuIndex;
156 Assert(ParentMenu[Position] = FirstSeparator);
157 FirstSeparator := nil;
158 repeat
159 ParentMenu.Delete(Position);
160 until (ParentMenu[Position] = NextMenuItem);
161 end;
162
163 inherited;
164 end;
165
166 procedure TCastleRecentFiles.SetNextMenuItem(Value: TMenuItem);
167 begin
168 if Value <> FNextMenuItem then
169 begin
170 MenuDestroy;
171 FNextMenuItem := Value;
172 MenuCreate;
173 end;
174 end;
175
176 end.