File: addfiletoapackagedlg.pas

package info (click to toggle)
lazarus 0.9.28.2-12
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 93,096 kB
  • ctags: 89,312
  • sloc: pascal: 820,189; xml: 202,194; makefile: 110,323; sh: 2,460; perl: 395; sql: 174; ansic: 133
file content (378 lines) | stat: -rw-r--r-- 11,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{  $Id: addfiletoapackagedlg.pas 20045 2009-05-19 08:54:33Z paul $  }
{
 /***************************************************************************
                         addfiletoapackagedlg.pas
                         ------------------------


 ***************************************************************************/

 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    The dialog for selecting the package to add a file to.
}

unit AddFileToAPackageDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Buttons, ExtCtrls, StdCtrls,
  LazarusIDEStrConsts, Dialogs, AVL_Tree, FileUtil, IDEProcs, IDEWindowIntf,
  ComponentReg, PackageDefs, PackageSystem, IDEContextHelpEdit, ButtonPanel;

type

  { TAddFileToAPackageDialog }

  TAddFileToAPackageDialog = class(TForm)
    BtnPanel: TButtonPanel;
    HasRegisterProcCheckBox: TCheckBox;
    FileTypeRadioGroup: TRadioGroup;
    UnitNameEdit: TEdit;
    FileNameEdit: TEdit;
    FileGroupBox: TGroupBox;
    PackagesGroupBox: TGroupBox;
    UnitNameLabel: TLabel;
    PackagesComboBox: TComboBox;
    ShowAllCheckBox: TCheckBox;
    procedure AddFileToAPackageDlgClose(Sender: TObject;
      var CloseAction: TCloseAction);
    procedure HelpButtonClick(Sender: TObject);
    procedure OkButtonClick(Sender: TObject);
    procedure PackagesGroupBoxResize(Sender: TObject);
    procedure ShowAllCheckBoxClick(Sender: TObject);
  private
    fPackages: TAVLTree;// tree of TLazPackage
    function GetFileType: TPkgFileType;
    function GetFilename: string;
    function GetHasRegisterProc: boolean;
    function GetUnitName: string;
    procedure SetFileType(const AValue: TPkgFileType);
    procedure SetFilename(const AValue: string);
    procedure SetHasRegisterProc(const AValue: boolean);
    procedure SetUnitName(const AValue: string);
    procedure SetupComponents;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure UpdateAvailablePackages;
    property Filename: string read GetFilename write SetFilename;
    property UnitName: string read GetUnitName write SetUnitName;
    property FileType: TPkgFileType read GetFileType write SetFileType;
    property HasRegisterProc: boolean read GetHasRegisterProc write SetHasRegisterProc;
  end;


function ShowAddFileToAPackageDlg(const Filename, UnitName: string;
  HasRegisterProc: boolean): TModalResult;


implementation


function ShowAddFileToAPackageDlg(const Filename, UnitName: string;
  HasRegisterProc: boolean): TModalResult;
var
  AddFileToAPackageDialog: TAddFileToAPackageDialog;
begin
  AddFileToAPackageDialog:=TAddFileToAPackageDialog.Create(nil);
  AddFileToAPackageDialog.Filename:=Filename;
  AddFileToAPackageDialog.UnitName:=UnitName;
  AddFileToAPackageDialog.HasRegisterProc:=HasRegisterProc;
  AddFileToAPackageDialog.UpdateAvailablePackages;
  Result:=AddFileToAPackageDialog.ShowModal;
  AddFileToAPackageDialog.Free;
end;

{ TAddFileToAPackageDialog }

procedure TAddFileToAPackageDialog.AddFileToAPackageDlgClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  IDEDialogLayoutList.SaveLayout(Self);
end;

procedure TAddFileToAPackageDialog.HelpButtonClick(Sender: TObject);
begin
  ShowContextHelpForIDE(Self);
end;

procedure TAddFileToAPackageDialog.OkButtonClick(Sender: TObject);
var
  PkgID: TLazPackageID;
  APackage: TLazPackage;
  PkgFile: TPkgFile;
  FileFlags: TPkgFileFlags;
begin
  PkgID:=TLazPackageID.Create;
  try
    // check package ID
    if not PkgID.StringToID(PackagesComboBox.Text) then begin
      MessageDlg(lisAF2PInvalidPackage,
        Format(lisAF2PInvalidPackageID, ['"', PackagesComboBox.Text, '"']),
        mtError,[mbCancel],0);
      exit;
    end;
    // search package
    APackage:=PackageGraph.FindPackageWithID(PkgID);
    if APackage=nil then begin
      MessageDlg(lisProjAddPackageNotFound,
        Format(lisAF2PPackageNotFound, ['"', PkgID.IDAsString, '"']),
        mtError,[mbCancel],0);
      exit;
    end;

    // check if package is readonly
    if APackage.ReadOnly then begin
      MessageDlg(lisAF2PPackageIsReadOnly,
        Format(lisAF2PThePackageIsReadOnly, [APackage.IDAsString]),
        mtError,[mbCancel],0);
      exit;
    end;

    // check if file is already in the package
    PkgFile:=APackage.FindPkgFile(Filename,true,false);
    if PkgFile<>nil then begin
      MessageDlg(lisPkgMangFileIsAlreadyInPackage,
        Format(lisAF2PTheFileIsAlreadyInThePackage, ['"', Filename, '"', #13,
          APackage.IDAsString]),
        mtError,[mbCancel],0);
      exit;
    end;

    // ok -> add file to package
    APackage.BeginUpdate;
    FileFlags:=[];
    if FileType in PkgFileUnitTypes then
      Include(FileFlags,pffAddToPkgUsesSection);
    if HasRegisterProc then
      Include(FileFlags,pffHasRegisterProc);
    APackage.AddFile(Filename,UnitName,FileType,FileFlags,cpNormal);
    if APackage.Editor<>nil then APackage.Editor.UpdateAll;
    APackage.EndUpdate;

    ModalResult:=mrOk;
  finally
    PkgID.Free;
  end;
end;

procedure TAddFileToAPackageDialog.PackagesGroupBoxResize(Sender: TObject);
begin
  with ShowAllCheckBox do
    SetBounds(10,30,200,Height);
end;

procedure TAddFileToAPackageDialog.ShowAllCheckBoxClick(Sender: TObject);
begin
  UpdateAvailablePackages;
end;

procedure TAddFileToAPackageDialog.SetupComponents;
var
  pft: TPkgFileType;
begin
  FileGroupBox.Caption:=lisToDoLFile;
  FileNameEdit.Text:='';
  UnitNameLabel.Caption:=lisAF2PUnitName;
  UnitNameEdit.Text:='';
  HasRegisterProcCheckBox.Caption:=lisAF2PHasRegisterProcedure;
  PackagesGroupBox.Caption:=lisAF2PDestinationPackage;
  ShowAllCheckBox.Caption:=lisAF2PShowAll;
  BtnPanel.OkButton.Caption:=lisLazBuildOk;
  BtnPanel.OkButton.OnClick:=@OkButtonClick;
  BtnPanel.OkButton.ModalResult:=mrNone;
  BtnPanel.HelpButton.OnClick:=@HelpButtonClick;

  with FileTypeRadioGroup do begin
    Caption:=lisAF2PFileType;
    with Items do begin
      BeginUpdate;
      for pft:=Low(TPkgFileType) to High(TPkgFileType) do begin
        if pft in [pftUnit,pftVirtualUnit] then continue;
        Add(GetPkgFileTypeLocalizedName(pft));
      end;
      EndUpdate;
    end;
    ItemIndex:=0;
    Columns:=2;
  end;
end;

procedure TAddFileToAPackageDialog.SetFilename(const AValue: string);
var
  NewPFT: TPkgFileType;
begin
  if FileNameEdit.Text=AValue then exit;
  FileNameEdit.Text:=AValue;
  if FilenameIsPascalUnit(AValue) then
    NewPFT:=pftUnit
  else if CompareFileExt(AValue,'.lfm',true)=0 then
    NewPFT:=pftLFM
  else if CompareFileExt(AValue,'.lrs',true)=0 then
    NewPFT:=pftLRS
  else if CompareFileExt(AValue,'.inc',true)=0 then
    NewPFT:=pftInclude
  else if FileIsText(AValue) then
    NewPFT:=pftText
  else
    NewPFT:=pftBinary;
  FileType:=NewPFT;
end;

procedure TAddFileToAPackageDialog.SetHasRegisterProc(const AValue: boolean);
begin
  if HasRegisterProc=AValue then exit;
  HasRegisterProcCheckBox.Checked:=AValue;
end;

procedure TAddFileToAPackageDialog.SetUnitName(const AValue: string);
begin
  if UnitName=AValue then exit;
  UnitNameEdit.Text:=AValue;
end;

function TAddFileToAPackageDialog.GetFilename: string;
begin
  Result:=FileNameEdit.Text;
end;

function TAddFileToAPackageDialog.GetFileType: TPkgFileType;
var
  i: Integer;
  CurPFT: TPkgFileType;
begin
  if FileTypeRadioGroup.Visible then begin
    i:=0;
    for CurPFT:=Low(TPkgFileType) to High(TPkgFileType) do begin
      if CurPFT in [pftUnit,pftVirtualUnit] then continue;
      if FileTypeRadioGroup.ItemIndex=i then begin
        Result:=CurPFT;
        exit;
      end;
      inc(i);
    end;
    Result:=pftText;
  end else begin
    Result:=pftUnit;
  end;
end;

function TAddFileToAPackageDialog.GetHasRegisterProc: boolean;
begin
  Result:=HasRegisterProcCheckBox.Checked;
end;

function TAddFileToAPackageDialog.GetUnitName: string;
begin
  Result:=UnitNameEdit.Text;
end;

procedure TAddFileToAPackageDialog.SetFileType(const AValue: TPkgFileType);
var
  ShowUnitProps: Boolean;
  i: Integer;
  CurPFT: TPkgFileType;
begin
  if FileType=AValue then exit;
  i:=0;
  for CurPFT:=Low(TPkgFileType) to High(TPkgFileType) do begin
    if CurPFT in [pftUnit,pftVirtualUnit] then continue;
    if CurPFT=AValue then break;
    inc(i);
  end;
  if i<FileTypeRadioGroup.Items.Count then
    FileTypeRadioGroup.ItemIndex:=i
  else
    FileTypeRadioGroup.ItemIndex:=-1;

  ShowUnitProps:=(AValue in [pftUnit,pftVirtualUnit]);
  UnitNameLabel.Visible:=ShowUnitProps;
  UnitNameEdit.Visible:=ShowUnitProps;
  HasRegisterProcCheckBox.Visible:=ShowUnitProps;
  FileTypeRadioGroup.Visible:=not ShowUnitProps;
end;

constructor TAddFileToAPackageDialog.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  Caption:=lisAF2PAddFileToAPackage;
  fPackages:=TAVLTree.Create(@CompareLazPackageID);
  IDEDialogLayoutList.ApplyLayout(Self,448,280);
  SetupComponents;
end;

destructor TAddFileToAPackageDialog.Destroy;
begin
  fPackages.Free;
  inherited Destroy;
end;

procedure TAddFileToAPackageDialog.UpdateAvailablePackages;
var
  i: Integer;
  APackage: TLazPackage;
  AFilename: String;
  ADirectory: String;
  sl: TStringList;
  ANode: TAVLTreeNode;
begin
  fPackages.Clear;
  AFilename:=Filename;
  ADirectory:=ExtractFilePath(Filename);
  for i:=0 to PackageGraph.Count-1 do begin
    APackage:=PackageGraph[i];
    // skip readonly packages
    if APackage.ReadOnly then continue;
    // skip packages, that already contains the file
    if APackage.FindPkgFile(AFilename,true,false)<>nil then continue;
    if not ShowAllCheckBox.Checked then begin
      // skip packages, where the filename is not in the package directory
      // or one of its source directories
      if (not FileIsInPath(AFilename,APackage.Directory))
      and (APackage.SourceDirectories.GetFileReference(ADirectory)=nil) then
        continue;
    end;
    fPackages.Add(APackage);
  end;
  sl:=TStringList.Create;
  ANode:=fPackages.FindLowest;
  while ANode<>nil do begin
    sl.Add(TLazPackage(ANode.Data).IDAsString);
    ANode:=fPackages.FindSuccessor(ANode);
  end;
  PackagesComboBox.Items.Assign(sl);
  if PackagesComboBox.Items.Count>0 then
    PackagesComboBox.Text:=PackagesComboBox.Items[0]
  else
    PackagesComboBox.Text:='';
  sl.Free;
end;

initialization
  {$I addfiletoapackagedlg.lrs}

end.