mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-22 12:59:26 +02:00
Make "convert DFM" menu text more truthful. Improve and refactor its code.
git-svn-id: trunk@24265 -
This commit is contained in:
parent
9dcf68db43
commit
699d0d13ba
@ -93,7 +93,6 @@ type
|
||||
function CopyAndLoadFile: TModalResult;
|
||||
function ConvertUnitFile: TModalResult;
|
||||
function ConvertFormFile: TModalResult;
|
||||
function ConvertDfmToLfm(const LfmFilename: string): TModalResult;
|
||||
function MissingUnitToMsg(MissingUnit: string): string;
|
||||
function CommentAutomatically: integer;
|
||||
function AskUnitPathFromUser: TModalResult;
|
||||
@ -243,10 +242,6 @@ type
|
||||
function CheckDelphiFileExt(const Filename: string): TModalResult;
|
||||
function CheckFilenameForLCLPaths(const Filename: string): TModalResult;
|
||||
|
||||
// dfm/lfm (ConvertDFMFileToLFMFile is used in main unit, too)
|
||||
function ConvertDFMToLFMFilename(const DFMFilename: string; KeepCase: boolean): string;
|
||||
function ConvertDFMFileToLFMFile(const DFMFilename: string): TModalResult;
|
||||
|
||||
// projects
|
||||
function CheckDelphiProjectExt(const Filename: string): TModalResult;
|
||||
|
||||
@ -302,68 +297,6 @@ begin
|
||||
Result:=mrOk;
|
||||
end;
|
||||
|
||||
function ConvertDFMToLFMFilename(const DFMFilename: string;
|
||||
KeepCase: boolean): string;
|
||||
begin
|
||||
if DFMFilename<>'' then begin
|
||||
// platform and fpc independent unitnames are lowercase, so are the lfm files
|
||||
Result:=lowercase(ExtractFilenameOnly(DFMFilename));
|
||||
if KeepCase then
|
||||
Result:=ExtractFilenameOnly(DFMFilename);
|
||||
Result:=ExtractFilePath(DFMFilename)+Result+'.lfm';
|
||||
end else
|
||||
Result:='';
|
||||
end;
|
||||
|
||||
function ConvertDFMFileToLFMFile(const DFMFilename: string): TModalResult;
|
||||
var
|
||||
DFMStream, LFMStream: TMemoryStream;
|
||||
LFMFilename: string;
|
||||
begin
|
||||
Result:=mrOk;
|
||||
DFMStream:=TMemoryStream.Create;
|
||||
LFMStream:=TMemoryStream.Create;
|
||||
try
|
||||
try
|
||||
DFMStream.LoadFromFile(UTF8ToSys(DFMFilename));
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=QuestionDlg(lisCodeToolsDefsReadError, Format(
|
||||
lisUnableToReadFileError, ['"', DFMFilename, '"', #13, E.Message]),
|
||||
mtError,[mrIgnore,mrAbort],0);
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
try
|
||||
FormDataToText(DFMStream,LFMStream);
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=QuestionDlg(lisFormatError,
|
||||
Format(lisUnableToConvertFileError, ['"', DFMFilename, '"', #13,
|
||||
E.Message]),
|
||||
mtError,[mrIgnore,mrAbort],0);
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
// converting dfm file, without renaming unit -> keep case
|
||||
LFMFilename:=ConvertDFMToLFMFilename(DFMFilename,true);
|
||||
try
|
||||
LFMStream.SaveToFile(UTF8ToSys(LFMFilename));
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=MessageDlg(lisCodeToolsDefsWriteError,
|
||||
Format(lisUnableToWriteFileError, ['"', LFMFilename, '"', #13,
|
||||
E.Message]),
|
||||
mtError,[mbIgnore,mbAbort],0);
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
LFMSTream.Free;
|
||||
DFMStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
function CheckDelphiProjectExt(const Filename: string): TModalResult;
|
||||
begin
|
||||
if CompareFileExt(Filename,'.dpr',false)<>0 then begin
|
||||
@ -554,6 +487,7 @@ var
|
||||
DfmFilename: string; // Delphi .DFM file name.
|
||||
LfmFilename: string; // Lazarus .LFM file name.
|
||||
ConvTool: TConvDelphiCodeTool;
|
||||
DFMConverter: TDFMConverter;
|
||||
begin
|
||||
fUnitsToRemove:=TStringList.Create;
|
||||
fUnitsToRename:=TStringToStringTree.Create(false);
|
||||
@ -591,7 +525,13 @@ begin
|
||||
// convert .dfm file to .lfm file (without context type checking)
|
||||
if FileExistsUTF8(LfmFilename) then begin
|
||||
IDEMessagesWindow.AddMsg('Converting DFM to LFM file '+LfmFilename,'',-1);
|
||||
Result:=ConvertDfmToLfm(LfmFilename);
|
||||
DFMConverter:=TDFMConverter.Create;
|
||||
try
|
||||
// Result:=ConvertDfmToLfm(LfmFilename);
|
||||
Result:=DFMConverter.ConvertDfmToLfm(LfmFilename);
|
||||
finally
|
||||
DFMConverter.Free;
|
||||
end;
|
||||
if Result<>mrOk then exit;
|
||||
// Read form file code in.
|
||||
Result:=LoadCodeBuffer(fLFMBuffer,LfmFilename,
|
||||
@ -657,58 +597,6 @@ begin
|
||||
Result:=mrOk;
|
||||
end;
|
||||
|
||||
function TConvertDelphiUnit.ConvertDfmToLfm(const LfmFilename: string): TModalResult;
|
||||
var
|
||||
DFMStream, LFMStream: TMemoryStream;
|
||||
begin
|
||||
Result:=mrOk;
|
||||
DFMStream:=TMemoryStream.Create;
|
||||
LFMStream:=TMemoryStream.Create;
|
||||
try
|
||||
// Note: LFM file is copied from DFM file earlier.
|
||||
try
|
||||
DFMStream.LoadFromFile(UTF8ToSys(LfmFilename));
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=QuestionDlg(lisCodeToolsDefsReadError, Format(
|
||||
lisUnableToReadFileError, ['"', LfmFilename, '"', #13, E.Message]),
|
||||
mtError,[mrIgnore,mrAbort],0);
|
||||
if Result=mrIgnore then // The caller will continue like nothing happened.
|
||||
Result:=mrOk;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
try
|
||||
FormDataToText(DFMStream,LFMStream);
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=QuestionDlg(lisFormatError,
|
||||
Format(lisUnableToConvertFileError, ['"',LfmFilename,'"',#13,E.Message]),
|
||||
mtError,[mrIgnore,mrAbort],0);
|
||||
if Result=mrIgnore then
|
||||
Result:=mrOk;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
// converting dfm file...
|
||||
try
|
||||
LFMStream.SaveToFile(UTF8ToSys(LfmFilename));
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=MessageDlg(lisCodeToolsDefsWriteError,
|
||||
Format(lisUnableToWriteFileError, ['"',LfmFilename,'"',#13,E.Message]),
|
||||
mtError,[mbIgnore,mbAbort],0);
|
||||
if Result=mrIgnore then
|
||||
Result:=mrOk;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
LFMSTream.Free;
|
||||
DFMStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TConvertDelphiUnit.MissingUnitToMsg(MissingUnit: string): string;
|
||||
var
|
||||
p: Integer;
|
||||
|
@ -33,8 +33,8 @@ interface
|
||||
|
||||
uses
|
||||
// FCL+LCL
|
||||
Classes, SysUtils, Math, LCLProc, Forms, Controls, Grids,
|
||||
Graphics, Dialogs, Buttons, StdCtrls, ExtCtrls, contnrs,
|
||||
Classes, SysUtils, Math, LCLProc, Forms, Controls, Grids, LResources,
|
||||
Graphics, Dialogs, Buttons, StdCtrls, ExtCtrls, contnrs, FileUtil,
|
||||
// components
|
||||
SynHighlighterLFM, SynEdit, SynEditMiscClasses, LFMTrees,
|
||||
// codetools
|
||||
@ -46,6 +46,21 @@ uses
|
||||
|
||||
type
|
||||
|
||||
{ TDFMConverter }
|
||||
|
||||
// Encapsulates some basic form file conversions.
|
||||
TDFMConverter = class
|
||||
private
|
||||
fOrigFormat: TLRSStreamOriginalFormat;
|
||||
function GetLFMFilename(const DfmFilename: string; KeepCase: boolean): string;
|
||||
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
function ConvertDfmToLfm(const DfmFilename: string): TModalResult;
|
||||
function Convert(const DfmFilename: string): TModalResult;
|
||||
end;
|
||||
|
||||
{ TLfmFixer }
|
||||
|
||||
TLFMFixer = class(TLFMChecker)
|
||||
@ -102,6 +117,97 @@ implementation
|
||||
|
||||
{$R *.lfm}
|
||||
|
||||
{ TDFMConverter }
|
||||
|
||||
constructor TDFMConverter.Create;
|
||||
begin
|
||||
|
||||
end;
|
||||
|
||||
destructor TDFMConverter.Destroy;
|
||||
begin
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
function TDFMConverter.Convert(const DfmFilename: string): TModalResult;
|
||||
begin
|
||||
Result:=ConvertDfmToLfm(DfmFilename);
|
||||
if Result=mrOK then begin
|
||||
if fOrigFormat=sofBinary then
|
||||
ShowMessage(Format('File %s is successfully converted to text format.',
|
||||
[DfmFilename]))
|
||||
else
|
||||
ShowMessage(Format('File %s syntax is correct.', [DfmFilename]));
|
||||
end;
|
||||
end;
|
||||
|
||||
function TDFMConverter.GetLFMFilename(const DfmFilename: string;
|
||||
KeepCase: boolean): string;
|
||||
begin
|
||||
if DfmFilename<>'' then begin
|
||||
// platform and fpc independent unitnames are lowercase, so are the lfm files
|
||||
Result:=lowercase(ExtractFilenameOnly(DfmFilename));
|
||||
if KeepCase then
|
||||
Result:=ExtractFilenameOnly(DfmFilename);
|
||||
Result:=ExtractFilePath(DfmFilename)+Result+'.lfm';
|
||||
end else
|
||||
Result:='';
|
||||
end;
|
||||
|
||||
function TDFMConverter.ConvertDfmToLfm(const DfmFilename: string): TModalResult;
|
||||
var
|
||||
DFMStream, LFMStream: TMemoryStream;
|
||||
begin
|
||||
Result:=mrOk;
|
||||
DFMStream:=TMemoryStream.Create;
|
||||
LFMStream:=TMemoryStream.Create;
|
||||
try
|
||||
// Note: The file is copied from DFM file earlier.
|
||||
try
|
||||
DFMStream.LoadFromFile(UTF8ToSys(DfmFilename));
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=QuestionDlg(lisCodeToolsDefsReadError, Format(
|
||||
lisUnableToReadFileError, ['"', DfmFilename, '"', #13, E.Message]),
|
||||
mtError,[mrIgnore,mrAbort],0);
|
||||
if Result=mrIgnore then // The caller will continue like nothing happened.
|
||||
Result:=mrOk;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
fOrigFormat:=TestFormStreamFormat(DFMStream);
|
||||
try
|
||||
FormDataToText(DFMStream,LFMStream);
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=QuestionDlg(lisFormatError,
|
||||
Format(lisUnableToConvertFileError, ['"',DfmFilename,'"',#13,E.Message]),
|
||||
mtError,[mrIgnore,mrAbort],0);
|
||||
if Result=mrIgnore then
|
||||
Result:=mrOk;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
// converting dfm file, without renaming unit -> keep case...
|
||||
try
|
||||
LFMStream.SaveToFile(UTF8ToSys(DfmFilename));
|
||||
except
|
||||
on E: Exception do begin
|
||||
Result:=MessageDlg(lisCodeToolsDefsWriteError,
|
||||
Format(lisUnableToWriteFileError, ['"',DfmFilename,'"',#13,E.Message]),
|
||||
mtError,[mbIgnore,mbAbort],0);
|
||||
if Result=mrIgnore then
|
||||
Result:=mrOk;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
LFMSTream.Free;
|
||||
DFMStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
{ TLFMFixer }
|
||||
|
||||
constructor TLFMFixer.Create(APascalBuffer, ALFMBuffer: TCodeBuffer;
|
||||
@ -191,7 +297,6 @@ var
|
||||
begin
|
||||
GridUpdater:=TGridUpdater.Create(fPropReplaceGrid, fSettings.ReplacementNames);
|
||||
try
|
||||
fPropReplaceGrid.BeginUpdate;
|
||||
if fLFMTree<>nil then begin
|
||||
CurError:=fLFMTree.FirstError;
|
||||
while CurError<>nil do begin
|
||||
@ -204,7 +309,6 @@ begin
|
||||
CurError:=CurError.NextError;
|
||||
end;
|
||||
end;
|
||||
fPropReplaceGrid.EndUpdate;
|
||||
finally
|
||||
GridUpdater.Free;
|
||||
end;
|
||||
|
@ -60,6 +60,7 @@ begin
|
||||
// Collect (maybe edited) properties from StringGrid to NameReplacements.
|
||||
List:=TStringList.Create;
|
||||
try
|
||||
AGrid.BeginUpdate;
|
||||
AMap.GetNames(List);
|
||||
for i:=0 to List.Count-1 do begin
|
||||
OldIdent:=List[i];
|
||||
@ -69,6 +70,7 @@ begin
|
||||
AGrid.Cells[0,i]:=OldIdent;
|
||||
AGrid.Cells[1,i]:=NewIdent;
|
||||
end;
|
||||
AGrid.EndUpdate;
|
||||
finally
|
||||
List.Free;
|
||||
end;
|
||||
@ -96,10 +98,12 @@ begin
|
||||
fReplaceMap:=AReplaceMap;
|
||||
i:=1;
|
||||
fSeenName:=TStringList.Create;
|
||||
fGrid.BeginUpdate;
|
||||
end;
|
||||
|
||||
destructor TGridUpdater.Destroy;
|
||||
begin
|
||||
fGrid.EndUpdate;
|
||||
fSeenName.Free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
@ -386,7 +386,7 @@ resourcestring
|
||||
lisMenuGuessMisplacedIFDEF = 'Guess misplaced IFDEF/ENDIF';
|
||||
lisMenuMakeResourceString = 'Make Resource String ...';
|
||||
lisMenuDiff = 'Diff';
|
||||
lisMenuConvertDFMtoLFM = 'Convert DFM file to LFM ...';
|
||||
lisMenuConvertDFMtoLFM = 'Convert binary DFM file to text LFM and check syntax ...';
|
||||
lisMenuCheckLFM = 'Check LFM file in editor';
|
||||
lisMenuConvertDelphiUnit = 'Convert Delphi unit to Lazarus unit ...';
|
||||
lisMenuConvertDelphiProject = 'Convert Delphi project to Lazarus project ...';
|
||||
|
17
ide/main.pp
17
ide/main.pp
@ -101,7 +101,7 @@ uses
|
||||
CodeToolsDefines, DiffDialog, DiskDiffsDialog, UnitInfoDlg, EditorOptions,
|
||||
SourceEditProcs, MsgQuickFixes, ViewUnit_dlg,
|
||||
// converter
|
||||
ChgEncodingDlg, ConvertDelphi, LazXMLForms,
|
||||
ChgEncodingDlg, ConvertDelphi, MissingPropertiesDlg, LazXMLForms,
|
||||
// rest of the ide
|
||||
Splash, IDEDefs, LazarusIDEStrConsts, LazConf, MsgView, SearchResultView,
|
||||
CodeTemplatesDlg, CodeBrowser, FindUnitDlg, IdeOptionsDlg,
|
||||
@ -11260,6 +11260,7 @@ end;
|
||||
function TMainIDE.DoConvertDFMtoLFM: TModalResult;
|
||||
var
|
||||
OpenDialog: TOpenDialog;
|
||||
DFMConverter: TDFMConverter;
|
||||
i: integer;
|
||||
AFilename: string;
|
||||
begin
|
||||
@ -11269,16 +11270,16 @@ begin
|
||||
InputHistories.ApplyFileDialogSettings(OpenDialog);
|
||||
OpenDialog.Title:=lisSelectDFMFiles;
|
||||
OpenDialog.Options:=OpenDialog.Options+[ofAllowMultiSelect];
|
||||
OpenDialog.Filter := rsFormDataFileDfm
|
||||
+ '|' + dlgAllFiles + '|'+GetAllFilesMask;
|
||||
OpenDialog.Filter:=rsFormDataFileDfm+'|'+dlgAllFiles+'|'+GetAllFilesMask;
|
||||
if OpenDialog.Execute and (OpenDialog.Files.Count>0) then begin
|
||||
For I := 0 to OpenDialog.Files.Count-1 do begin
|
||||
AFilename:=ExpandFileNameUTF8(OpenDialog.Files.Strings[i]);
|
||||
if ConvertDFMFileToLFMFile(AFilename)=mrAbort then begin
|
||||
Result:=mrAbort;
|
||||
break;
|
||||
end else
|
||||
Result:=mrOk;
|
||||
DFMConverter:=TDFMConverter.Create;
|
||||
try
|
||||
Result:=DFMConverter.Convert(AFilename);
|
||||
finally
|
||||
DFMConverter.Free;
|
||||
end;
|
||||
end;
|
||||
SaveEnvironment;
|
||||
end;
|
||||
|
@ -737,9 +737,9 @@ begin
|
||||
ParentMI:=itmDelphiConversion;
|
||||
|
||||
CreateMenuItem(ParentMI,itmToolCheckLFM,'itmToolCheckLFM',lisMenuCheckLFM, 'menu_tool_check_lfm');
|
||||
CreateMenuItem(ParentMI,itmToolConvertDelphiUnit,'itmToolConvertDelphiUnit',lisMenuConvertDelphiUnit);
|
||||
CreateMenuItem(ParentMI,itmToolConvertDelphiProject,'itmToolConvertDelphiProject',lisMenuConvertDelphiProject);
|
||||
CreateMenuItem(ParentMI,itmToolConvertDelphiPackage,'itmToolConvertDelphiPackage',lisMenuConvertDelphiPackage);
|
||||
CreateMenuItem(ParentMI,itmToolConvertDelphiUnit,'itmToolConvertDelphiUnit',lisMenuConvertDelphiUnit,'menu_tool_dfm_to_lfm');
|
||||
CreateMenuItem(ParentMI,itmToolConvertDelphiProject,'itmToolConvertDelphiProject',lisMenuConvertDelphiProject,'menu_tool_dfm_to_lfm');
|
||||
CreateMenuItem(ParentMI,itmToolConvertDelphiPackage,'itmToolConvertDelphiPackage',lisMenuConvertDelphiPackage,'menu_tool_dfm_to_lfm');
|
||||
CreateMenuItem(ParentMI,itmToolConvertDFMtoLFM,'itmToolConvertDFMtoLFM',lisMenuConvertDFMtoLFM,'menu_tool_dfm_to_lfm');
|
||||
CreateMenuItem(ParentMI,itmToolConvertEncoding,'itmToolConvertEncoding',lisMenuConvertEncoding);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user