/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. * This source is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This code is distributed in the hope that it will be useful, but *
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  17. * General Public License for more details. *
  18. * *
  19. * A copy of the GNU General Public License is available on the World *
  20. * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
  21. * obtain it by writing to the Free Software Foundation, *
  22. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  23. * *
  24. ***************************************************************************
  25. Author: Mattias Gaertner
  26. Abstract:
  27. TEditVirtualUnitDialog is a dialog to edit the properties of a virtual unit.
  28. }
  29. unit PkgVirtualUnitEditor;
  30. {$mode objfpc}{$H+}
  31. interface
  32. uses
  33. Classes, SysUtils, Forms, Controls, Dialogs,
  34. PackageDefs, FileUtil, LazFileUtils, LazarusIDEStrConsts;
  35. function ShowEditVirtualPackageDialog(PkgFile: TPkgFile): TModalResult;
  36. implementation
  37. type
  38. TDummyForClose = class
  39. public
  40. PkgFile: TPkgFile;
  41. procedure CloseEvent(Sender: TObject; const AValues: array of string;
  42. var ACanClose: boolean);
  43. end;
  44. function ShowEditVirtualPackageDialog(PkgFile: TPkgFile): TModalResult;
  45. var
  46. Str: array of string;
  47. Dummy: TDummyForClose;
  48. begin
  49. Result:= mrCancel;
  50. if not Assigned(PkgFile) then exit;
  51. SetLength(Str, 2);
  52. Str[0]:= PkgFile.Filename;
  53. Str[1]:= PkgFile.Unit_Name;
  54. Dummy:= TDummyForClose.Create;
  55. try
  56. Dummy.PkgFile:= PkgFile;
  57. if not InputQuery(lisPVUEditVirtualUnit,
  58. [lisPEFilename, lisPEUnitname], Str, @Dummy.CloseEvent) then exit;
  59. finally
  60. FreeAndNil(Dummy);
  61. end;
  62. if (PkgFile.Filename=Str[0]) and
  63. (PkgFile.Unit_name=Str[1]) then exit;
  64. PkgFile.Filename:= Str[0];
  65. PkgFile.Unit_name:= Str[1];
  66. if Assigned(PkgFile.LazPackage) then
  67. PkgFile.LazPackage.Modified:= true;
  68. Result:= mrOk;
  69. end;
  70. { TDummyForClose }
  71. procedure TDummyForClose.CloseEvent(Sender: TObject; const AValues: array of string;
  72. var ACanClose: boolean);
  73. var
  74. NewFilename: String;
  75. NewUnitName: String;
  76. NewFilenameOnly: String;
  77. LazPackage: TLazPackage;
  78. ConflictUnit: TPkgFile;
  79. begin
  80. ACanClose:=false;
  81. NewFilename:=AValues[0];
  82. NewUnitName:=AValues[1];
  83. if not FilenameIsPascalUnit(NewFilename) then begin
  84. MessageDlg(lisPEInvalidUnitFilename,
  85. lisPVUAPascalUnitMustHaveTheExtensionPpOrPas,
  86. mtError,[mbCancel],0);
  87. exit;
  88. end;
  89. NewFilenameOnly:=ExtractFilenameOnly(NewFilename);
  90. if CompareText(NewUnitName,NewFilenameOnly)<>0 then begin
  91. MessageDlg(lisPEInvalidUnitname,
  92. Format(lisPVUUnitnameAndFilenameDoNotMatchExampleUnit1PasAndUni, [LineEnding]),
  93. mtError,[mbCancel],0);
  94. exit;
  95. end;
  96. if (NewUnitName='') or (not IsValidUnitName(NewUnitName)) then begin
  97. MessageDlg(lisPEInvalidUnitname,
  98. lisPVUTheUnitnameIsNotAValidPascalIdentifier,
  99. mtError,[mbCancel],0);
  100. exit;
  101. end;
  102. LazPackage:=PkgFile.LazPackage;
  103. if LazPackage<>nil then begin
  104. ConflictUnit:=LazPackage.FindUnit(NewUnitName,true,PkgFile);
  105. if ConflictUnit<>nil then begin
  106. MessageDlg(lisPEConflictFound,
  107. Format(lisPVUThereIsAlreadyAnUnitWithThisNameFile, [LineEnding,
  108. ConflictUnit.Filename]),
  109. mtError,[mbCancel],0);
  110. exit;
  111. end;
  112. end;
  113. ACanClose:=true;
  114. end;
  115. end.