1 { $Id$ }
2 {
3 /***************************************************************************
4 addfiletoapackagedlg.pas
5 ------------------------
6
7
8 ***************************************************************************/
9
10 ***************************************************************************
11 * *
12 * This source is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This code is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * General Public License for more details. *
21 * *
22 * A copy of the GNU General Public License is available on the World *
23 * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
24 * obtain it by writing to the Free Software Foundation, *
25 * Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
26 * *
27 ***************************************************************************
28
29 Author: Mattias Gaertner
30
31 Abstract:
32 The dialog for selecting the package to add a file to.
33 }
34
35 unit AddFileToAPackageDlg;
36
37 {$mode objfpc}{$H+}
38
39 interface
40
41 uses
42 Classes, SysUtils, Laz_AVL_Tree,
43 // LCL
44 Forms, Controls, StdCtrls, Dialogs, ButtonPanel,
45 // LazUtils
46 FileUtil, LazFileUtils,
47 // IdeIntf
48 IDEWindowIntf, PackageIntf, IDEHelpIntf,
49 // IDE
50 LazarusIDEStrConsts, PackageDefs, PackageSystem, EnvironmentOpts;
51
52 type
53
54 { TAddFileToAPackageDialog }
55
56 TAddFileToAPackageDialog = class(TForm)
57 BtnPanel: TButtonPanel;
58 FileNameEdit: TEdit;
59 FileGroupBox: TGroupBox;
60 PackagesGroupBox: TGroupBox;
61 PackagesComboBox: TComboBox;
62 ShowAllCheckBox: TCheckBox;
63 procedure AddFileToAPackageDlgClose(Sender: TObject;
64 var {%H-}CloseAction: TCloseAction);
65 procedure FormCreate(Sender: TObject);
66 procedure HelpButtonClick(Sender: TObject);
67 procedure OkButtonClick(Sender: TObject);
68 procedure PackagesGroupBoxResize(Sender: TObject);
69 procedure ShowAllCheckBoxClick(Sender: TObject);
70 private
71 fPackages: TAVLTree;// tree of TLazPackage
GetFilenamenull72 function GetFilename: string;
73 procedure SetFilename(const AValue: string);
74 procedure SetupComponents;
75 public
76 constructor Create(TheOwner: TComponent); override;
77 destructor Destroy; override;
78 procedure UpdateAvailablePackages;
79 property Filename: string read GetFilename write SetFilename;
80 end;
81
82
ShowAddFileToAPackageDlgnull83 function ShowAddFileToAPackageDlg(const Filename: string): TModalResult;
84
85
86 implementation
87
88 {$R *.lfm}
89
ShowAddFileToAPackageDlgnull90 function ShowAddFileToAPackageDlg(const Filename: string): TModalResult;
91 var
92 AddFileToAPackageDialog: TAddFileToAPackageDialog;
93 begin
94 AddFileToAPackageDialog:=TAddFileToAPackageDialog.Create(nil);
95 AddFileToAPackageDialog.Filename:=Filename;
96 AddFileToAPackageDialog.UpdateAvailablePackages;
97 Result:=AddFileToAPackageDialog.ShowModal;
98 AddFileToAPackageDialog.Free;
99 end;
100
101 { TAddFileToAPackageDialog }
102
103 procedure TAddFileToAPackageDialog.AddFileToAPackageDlgClose(Sender: TObject;
104 var CloseAction: TCloseAction);
105 begin
106 IDEDialogLayoutList.SaveLayout(Self);
107 end;
108
109 procedure TAddFileToAPackageDialog.HelpButtonClick(Sender: TObject);
110 begin
111 LazarusHelp.ShowHelpForIDEControl(Self);
112 end;
113
114 procedure TAddFileToAPackageDialog.OkButtonClick(Sender: TObject);
115 var
116 PkgID: TLazPackageID;
117 APackage: TLazPackage;
118 aFilename: String;
119 NewUnitPaths, NewIncPaths: String;
120 begin
121 aFilename:=Filename;
122 PkgID:=TLazPackageID.Create;
123 try
124 // check package ID
125 if not PkgID.StringToID(PackagesComboBox.Text) then begin
126 MessageDlg(lisAF2PInvalidPackage,
127 Format(lisAF2PInvalidPackageID, [PackagesComboBox.Text]),
128 mtError,[mbCancel],0);
129 exit;
130 end;
131 // search package
132 APackage:=PackageGraph.FindPackageWithID(PkgID);
133 if APackage=nil then begin
134 MessageDlg(lisProjAddPackageNotFound,
135 Format(lisAF2PPackageNotFound, [PkgID.IDAsString]),
136 mtError,[mbCancel],0);
137 exit;
138 end;
139
140 // check if package is readonly
141 if APackage.ReadOnly then begin
142 MessageDlg(lisAF2PPackageIsReadOnly,
143 Format(lisAF2PThePackageIsReadOnly, [APackage.IDAsString]),
144 mtError,[mbCancel],0);
145 exit;
146 end;
147
148 // ok -> add file to package
149 APackage.BeginUpdate;
150 NewUnitPaths:='';
151 NewIncPaths:='';
152 APackage.AddFileByName(aFilename, NewUnitPaths, NewIncPaths);
153 // extend unit and include search path
154 if not APackage.ExtendUnitSearchPath(NewUnitPaths) then exit;
155 if not APackage.ExtendIncSearchPath(NewIncPaths) then exit;
156 if APackage.Editor<>nil then APackage.Editor.UpdateAll(true);
157 APackage.EndUpdate;
158
159 ModalResult:=mrOk;
160 finally
161 PkgID.Free;
162 end;
163 end;
164
165 procedure TAddFileToAPackageDialog.PackagesGroupBoxResize(Sender: TObject);
166 begin
167 with ShowAllCheckBox do
168 SetBounds(10,30,200,Height);
169 end;
170
171 procedure TAddFileToAPackageDialog.ShowAllCheckBoxClick(Sender: TObject);
172 begin
173 UpdateAvailablePackages;
174 end;
175
176 procedure TAddFileToAPackageDialog.SetupComponents;
177 begin
178 FileGroupBox.Caption:=lisFile;
179 FileNameEdit.Text:='';
180 PackagesGroupBox.Caption:=lisAF2PDestinationPackage;
181 ShowAllCheckBox.Caption:=lisAF2PShowAll;
182 BtnPanel.OkButton.Caption:=lisMenuOk;
183 BtnPanel.OkButton.OnClick:=@OkButtonClick;
184 BtnPanel.OkButton.ModalResult:=mrNone;
185 BtnPanel.HelpButton.OnClick:=@HelpButtonClick;
186 end;
187
188 procedure TAddFileToAPackageDialog.SetFilename(const AValue: string);
189 begin
190 if FileNameEdit.Text=AValue then exit;
191 FileNameEdit.Text:=AValue;
192 end;
193
GetFilenamenull194 function TAddFileToAPackageDialog.GetFilename: string;
195 begin
196 Result:=FileNameEdit.Text;
197 end;
198
199 constructor TAddFileToAPackageDialog.Create(TheOwner: TComponent);
200 begin
201 inherited Create(TheOwner);
202 Caption:=lisAF2PAddFileToAPackage;
203 fPackages:=TAVLTree.Create(@CompareLazPackageID);
204 IDEDialogLayoutList.ApplyLayout(Self,448,280);
205 SetupComponents;
206 end;
207
208 destructor TAddFileToAPackageDialog.Destroy;
209 begin
210 FreeAndNil(fPackages);
211 inherited Destroy;
212 end;
213
214 procedure TAddFileToAPackageDialog.FormCreate(Sender: TObject);
215 begin
216 PackagesComboBox.DropDownCount:=EnvironmentOptions.DropDownCount;
217 end;
218
219 procedure TAddFileToAPackageDialog.UpdateAvailablePackages;
220 var
221 i: Integer;
222 APackage: TLazPackage;
223 AFilename: String;
224 ADirectory: String;
225 sl: TStringList;
226 ANode: TAVLTreeNode;
227 begin
228 fPackages.Clear;
229 AFilename:=Filename;
230 ADirectory:=ExtractFilePath(Filename);
231 for i:=0 to PackageGraph.Count-1 do begin
232 APackage:=PackageGraph[i];
233 // skip readonly packages
234 if APackage.ReadOnly then continue;
235 // skip packages, that already contains the file
236 if APackage.FindPkgFile(AFilename,true,false)<>nil then continue;
237 if not ShowAllCheckBox.Checked then begin
238 // skip packages, where the filename is not in the package directory
239 // or one of its source directories
240 if (not FileIsInPath(AFilename,APackage.Directory))
241 and (APackage.SourceDirectories.GetFileReference(ADirectory)=nil) then
242 continue;
243 end;
244 fPackages.Add(APackage);
245 end;
246 sl:=TStringList.Create;
247 ANode:=fPackages.FindLowest;
248 while ANode<>nil do begin
249 sl.Add(TLazPackage(ANode.Data).IDAsString);
250 ANode:=fPackages.FindSuccessor(ANode);
251 end;
252 PackagesComboBox.Items.Assign(sl);
253 if PackagesComboBox.Items.Count>0 then
254 PackagesComboBox.Text:=PackagesComboBox.Items[0]
255 else
256 PackagesComboBox.Text:='';
257 sl.Free;
258 end;
259
260 end.
261