PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/packager/pkgvirtualuniteditor.pas

http://github.com/graemeg/lazarus
Pascal | 137 lines | 85 code | 17 blank | 35 comment | 9 complexity | 48b9646e2a5d8a65e21593dae2d2cd8b MD5 | raw file
  1{  $Id$  }
  2{
  3 /***************************************************************************
  4                          pkgvirtualuniteditor.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 26 *                                                                         *
 27 ***************************************************************************
 28
 29  Author: Mattias Gaertner
 30
 31  Abstract:
 32    TEditVirtualUnitDialog is a dialog to edit the properties of a virtual unit.
 33}
 34unit PkgVirtualUnitEditor;
 35
 36{$mode objfpc}{$H+}
 37
 38interface
 39
 40uses
 41  Classes, SysUtils, Forms, Controls, Dialogs,
 42  PackageDefs, FileUtil, LazFileUtils, LazarusIDEStrConsts;
 43
 44function ShowEditVirtualPackageDialog(PkgFile: TPkgFile): TModalResult;
 45
 46implementation
 47
 48type
 49  TDummyForClose = class
 50  public
 51    PkgFile: TPkgFile;
 52    procedure CloseEvent(Sender: TObject; const AValues: array of string;
 53      var ACanClose: boolean);
 54  end;
 55
 56function ShowEditVirtualPackageDialog(PkgFile: TPkgFile): TModalResult;
 57var
 58  Str: array of string;
 59  Dummy: TDummyForClose;
 60begin
 61  Result:= mrCancel;
 62  if not Assigned(PkgFile) then exit;
 63
 64  SetLength(Str, 2);
 65  Str[0]:= PkgFile.Filename;
 66  Str[1]:= PkgFile.Unit_Name;
 67
 68  Dummy:= TDummyForClose.Create;
 69  try
 70    Dummy.PkgFile:= PkgFile;
 71    if not InputQuery(lisPVUEditVirtualUnit,
 72      [lisPEFilename, lisPEUnitname], Str, @Dummy.CloseEvent) then exit;
 73  finally
 74    FreeAndNil(Dummy);
 75  end;
 76
 77  if (PkgFile.Filename=Str[0]) and
 78     (PkgFile.Unit_name=Str[1]) then exit;
 79
 80  PkgFile.Filename:= Str[0];
 81  PkgFile.Unit_name:= Str[1];
 82  if Assigned(PkgFile.LazPackage) then
 83    PkgFile.LazPackage.Modified:= true;
 84  Result:= mrOk;
 85end;
 86
 87{ TDummyForClose }
 88
 89procedure TDummyForClose.CloseEvent(Sender: TObject; const AValues: array of string;
 90  var ACanClose: boolean);
 91var
 92  NewFilename: String;
 93  NewUnitName: String;
 94  NewFilenameOnly: String;
 95  LazPackage: TLazPackage;
 96  ConflictUnit: TPkgFile;
 97begin
 98  ACanClose:=false;
 99  NewFilename:=AValues[0];
100  NewUnitName:=AValues[1];
101
102  if not FilenameIsPascalUnit(NewFilename) then begin
103    MessageDlg(lisPEInvalidUnitFilename,
104      lisPVUAPascalUnitMustHaveTheExtensionPpOrPas,
105      mtError,[mbCancel],0);
106    exit;
107  end;
108  NewFilenameOnly:=ExtractFilenameOnly(NewFilename);
109  if CompareText(NewUnitName,NewFilenameOnly)<>0 then begin
110    MessageDlg(lisPEInvalidUnitname,
111      Format(lisPVUUnitnameAndFilenameDoNotMatchExampleUnit1PasAndUni, [LineEnding]),
112      mtError,[mbCancel],0);
113    exit;
114  end;
115  if (NewUnitName='') or (not IsValidUnitName(NewUnitName)) then begin
116    MessageDlg(lisPEInvalidUnitname,
117      lisPVUTheUnitnameIsNotAValidPascalIdentifier,
118      mtError,[mbCancel],0);
119    exit;
120  end;
121  LazPackage:=PkgFile.LazPackage;
122  if LazPackage<>nil then begin
123    ConflictUnit:=LazPackage.FindUnit(NewUnitName,true,PkgFile);
124    if ConflictUnit<>nil then begin
125      MessageDlg(lisPEConflictFound,
126        Format(lisPVUThereIsAlreadyAnUnitWithThisNameFile, [LineEnding,
127          ConflictUnit.Filename]),
128        mtError,[mbCancel],0);
129      exit;
130    end;
131  end;
132
133  ACanClose:=true;
134end;
135
136end.
137