1 {
2  /***************************************************************************
3                           addtoprojectdlg.pas
4                           -------------------
5 
6 
7  ***************************************************************************/
8 
9  ***************************************************************************
10  *                                                                         *
11  *   This source is free software; you can redistribute it and/or modify   *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   This code is distributed in the hope that it will be useful, but      *
17  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
19  *   General Public License for more details.                              *
20  *                                                                         *
21  *   A copy of the GNU General Public License is available on the World    *
22  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
23  *   obtain it by writing to the Free Software Foundation,                 *
24  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
25  *                                                                         *
26  ***************************************************************************
27 
28   Author: Mattias Gaertner
29 }
30 unit AddToProjectDlg;
31 
32 {$mode objfpc}{$H+}
33 
34 interface
35 
36 uses
37   Classes, SysUtils,
38   // LCL
39   Forms, Controls, ComCtrls, StdCtrls, Dialogs, ButtonPanel,
40   // LazUtils
41   FileUtil, LazFileUtils,
42   // IDEIntf
43   IDEWindowIntf,
44   // IDE
45   LazarusIDEStrConsts, IDEDialogs, Project, ProjPackChecks;
46 
47 type
48   { TAddToProjectDialog }
49 
50   TAddToProjectDialog = class(TForm)
51     AddFileListView: TListView;
52     ButtonPanel: TButtonPanel;
53     procedure AddFileButtonClick(Sender: TObject);
54     procedure AddFileListViewSelectItem(Sender: TObject; {%H-}Item: TListItem;
55       {%H-}Selected: Boolean);
56     procedure AddToProjectDialogClose(Sender: TObject;
57                                       var {%H-}CloseAction: TCloseAction);
58   private
59     fProject: TProject;
60     fFileNames: TStrings;
61     procedure UpdateAvailableFiles;
62   public
63     constructor Create(TheOwner: TComponent); override;
64     destructor Destroy; override;
65   end;
66 
ShowAddToProjectDlgnull67 function ShowAddToProjectDlg(AProject: TProject; AFileNames: TStrings): TModalResult;
68 
69 
70 implementation
71 
72 {$R *.lfm}
73 
ShowAddToProjectDlgnull74 function ShowAddToProjectDlg(AProject: TProject; AFileNames: TStrings): TModalResult;
75 var
76   AddToProjectDialog: TAddToProjectDialog;
77 begin
78   AddToProjectDialog:=TAddToProjectDialog.Create(nil);
79   try
80     AddToProjectDialog.fProject:=AProject;
81     AddToProjectDialog.fFileNames:=AFileNames;
82     AddToProjectDialog.UpdateAvailableFiles;
83     Result:=AddToProjectDialog.ShowModal;
84   finally
85     AddToProjectDialog.Free;
86   end;
87 end;
88 
89 { TAddToProjectDialog }
90 
91 constructor TAddToProjectDialog.Create(TheOwner: TComponent);
92 var
93   CurColumn: TListColumn;
94 begin
95   inherited Create(TheOwner);
96   Caption:=lisProjAddEditorFile;
97   IDEDialogLayoutList.ApplyLayout(Self,500,300);
98   ButtonPanel.OKButton.ModalResult := mrNone;
99   ButtonPanel.OKButton.OnClick := @AddFileButtonClick;
100   ButtonPanel.OKButton.Caption:=lisA2PAddFiles;
101   ButtonPanel.OkButton.Enabled:=AddFileListView.SelCount>0;
102   CurColumn:=AddFileListView.Columns.Add;
103   CurColumn.Caption:=lisA2PFilename2;
104 end;
105 
106 destructor TAddToProjectDialog.Destroy;
107 begin
108   inherited Destroy;
109 end;
110 
111 procedure TAddToProjectDialog.AddToProjectDialogClose(Sender: TObject;
112   var CloseAction: TCloseAction);
113 begin
114   IDEDialogLayoutList.SaveLayout(Self);
115 end;
116 
117 procedure TAddToProjectDialog.AddFileButtonClick(Sender: TObject);
118 var
119   NewFilename, NewUnitName, OtherFile: string;
120   i, j: Integer;
121 begin
122   for i:=0 to AddFileListView.Items.Count-1 do
123     if AddFileListView.Items[i].Selected then
124     begin
125       NewFilename:=AddFileListView.Items[i].Caption;
126       if not FilenameIsAbsolute(NewFilename) then  // expand filename
127         NewFilename:=TrimFilename(fProject.Directory+PathDelim+NewFilename);
128       case TPrjFileCheck.AddingFile(fProject, NewFilename) of
129         mrOk: begin
130             // check if unitname already exists in selection
131             if FilenameHasPascalExt(NewFilename) then
132             begin
133               NewUnitName:=ExtractFileNameOnly(NewFilename);
134               for j:=0 to fFileNames.Count-1 do
135               begin
136                 OtherFile:=fFileNames[j];
137                 if FilenameHasPascalExt(OtherFile)
138                 and (CompareText(ExtractFileNameOnly(OtherFile), NewUnitName)=0)
139                 then begin
140                   IDEMessageDialog(lisProjAddUnitNameAlreadyExists,
141                     Format(lisProjAddTheUnitNameAlreadyExistsInTheSelection,
142                            [NewUnitName, LineEnding, OtherFile]),
143                     mtWarning, [mbCancel]);
144                   exit;
145                 end;
146               end;
147             end;
148             fFileNames.Add(NewFilename);  // Add to valid files
149           end;  // mrOk
150         mrIgnore: continue;
151         mrCancel: exit;
152       end;
153     end;
154   ModalResult:=mrOk;  // everything ok
155 end;
156 
157 procedure TAddToProjectDialog.AddFileListViewSelectItem(Sender: TObject;
158   Item: TListItem; Selected: Boolean);
159 begin
160   ButtonPanel.OkButton.Enabled:=AddFileListView.SelCount>0;
161 end;
162 
163 procedure TAddToProjectDialog.UpdateAvailableFiles;
164 var
165   CurFile: TUnitInfo;
166   NewListItem: TListItem;
167   NewFilename: String;
168 begin
169   AddFileListView.Items.BeginUpdate;
170   if fProject<>nil then begin
171     CurFile:=fProject.FirstUnitWithEditorIndex;
172     while CurFile<>nil do begin
173       if (not CurFile.IsPartOfProject) and (not CurFile.IsVirtual) then begin
174         NewFilename:=CreateRelativePath(CurFile.Filename,fProject.Directory);
175         NewListItem:=AddFileListView.Items.Add;
176         NewListItem.Caption:=NewFilename;
177         NewListItem.Selected:=True;
178       end;
179       CurFile:=CurFile.NextUnitWithEditorIndex;
180     end;
181   end;
182   AddFileListView.Items.EndUpdate;
183   ButtonPanel.OkButton.Enabled:=AddFileListView.SelCount>0;
184 end;
185 
186 end.
187 
188