Difference between revisions of "Delphi Converter in Lazarus"

From Lazarus wiki
Jump to navigationJump to search
Line 41: Line 41:
 
The conversion target is defined in settings dialog. It can be:
 
The conversion target is defined in settings dialog. It can be:
  
* Lazarus / LCL -- A one way conversion. The goal is to create a multi-platform Lazarus program which does not need to be Delphi compatible.
+
* Lazarus -- A one way conversion. The goal is to create a multi-platform Lazarus program which does not need to be Delphi compatible.
  
* Lazarus / LCL for Windows only -- Convert for Lazarus but leave Windows dependent unit names and functions as they are.
+
* Lazarus for Windows only -- Convert for Lazarus but leave Windows dependent unit names and functions as they are.
  
* Both Lazarus / LCL and Delphi -- Tries to make the unit compatible with both Delphi and Lazarus by using {$IFDEF...} when needed. Typically the form file is copied and converted but there is an option to use the same form file. See "Lazarus uses the same DFM form file as Delphi" in settings dialog. Conditional compilation {$IFNDEF FPC} is added to Uses section and for form file include directive as needed.
+
* Lazarus and Delphi -- Tries to make the unit compatible with both Delphi and Lazarus by using {$IFDEF...} in Uses section and in form file include directive. The Delphi form file .dfm is copied to Lazarus form file .lfm and converted.
 +
 
 +
* Lazarus and Delphi with same DFM file --
 +
The Delphi form file .dfm is used also with Lazarus. It is not modified except for converting binary format to ascii text format.
  
 
== Unit name replacement and removal ==
 
== Unit name replacement and removal ==

Revision as of 10:24, 26 September 2010

A Delphi project, Delphi package and a single unit file can be converted for Lazarus. The entries can be found in Lazarus Tools menu:

  • Convert Delphi unit to Lazarus unit ...
  • Convert Delphi project to Lazarus project ...
  • Convert Delphi package to Lazarus package ...

There is also:

  • Convert binary DFM file to text LFM and check syntax ...

which is seldom needed because the DFM form file is always converted while converting a unit.

Settings dialog

After selecting an entry from the Tools menu, and then selecting the unit / project / package to convert, the user is presented with a settings dialog. It allows to configure many details of conversion.

"Project Path" shows the selected path.

"Make backup of changed files" saves the original files to directory ConverterBackup below the selected directory.

"Keep converted file open in editor". When checked, the converted unit files remain open after conversion.

Rest of the dialog settings are covered below.


[Pic]

Conversions done for a unit

All unit source files get a {$mode delphi} directive. It makes the language syntax exactly like Delphi has it. This makes sense because we are converting a Delphi program after all. The user can take advantage of {$mode objfpc} features later if he wants, by making the necessary changes manually.

.DFM form file is renamed or copied to .lfm and converted as needed. In source {$R *.DFM} is replaced with {$R *.lfm}.

Unit names in Uses section and include file names are fixed to match the real case sensitive name found in the file system. This is needed for case sensitive file systems.

Target

The conversion target is defined in settings dialog. It can be:

  • Lazarus -- A one way conversion. The goal is to create a multi-platform Lazarus program which does not need to be Delphi compatible.
  • Lazarus for Windows only -- Convert for Lazarus but leave Windows dependent unit names and functions as they are.
  • Lazarus and Delphi -- Tries to make the unit compatible with both Delphi and Lazarus by using {$IFDEF...} in Uses section and in form file include directive. The Delphi form file .dfm is copied to Lazarus form file .lfm and converted.
  • Lazarus and Delphi with same DFM file --

The Delphi form file .dfm is used also with Lazarus. It is not modified except for converting binary format to ascii text format.

Unit name replacement and removal

When a unit listed in Uses section is not found, it can be replaced or removed. The replacements are configurable, see "Unit Replacements" in settings dialog. An empty "New name" means removal. For example by default "Windows" is replaced with "LCLIntf, LCLType, LMessages".

The replacement supports regular expression syntax. For example:

  • ^Q(.+) --> $1

means something like: match a string starting with 'Q', followed by anything, and save the part after 'Q', then replace with the saved part. In essence this removes 'Q' in the beginning.

"Replace Automatically" switches between automatic and interactive unit replacement. When interactive, the user can edit the replacements before they are applied.

If there are still missing units after the replacement, the user will be asked about them in any case. He can then choose to comment them out or search for their unit paths.

Unknown property removal

Not all Delphi component properties are implemented in LCL components. These properties can be removed. There is a system in LCL where non-existent properties are registered and the form loader ignores them. The converter also removes them, automatically or interactively. It will become optional later.

Type replacement

Some Delphi components do not exist in LCL. Many 3rd party components also exist only for Delphi. Those types can be replaced with fall-back LCL types. For example:

  • TCoolBar --> TToolBar.

It is configurable, too, see "Type Replacements" in settings dialog.

The same regular expression syntax than for unit replacement is supported here.

The types are replaced in both the pascal source file and the form file (.lfm). Different properties between the original component and the replacement component can cause problems after conversion. There is no easy solution for it. Best solution would be to make more Delphi compatible components and port all 3rd party components for Lazarus. Because of these potential problems the type conversion is always interactive.

Function replacement

Some Windows-only function calls in the source code can be replaced with a functionally similar FPC / Lazarus library function. It is configurable, see "Function Replacements" in settings dialog.

There is a category defined for each function. The replacement can be enabled or disabled for each category. For example there is "UTF8Names" category for file operation related functions.

The syntax for replacements is:

  • FileExists --> FileExistsUTF8($1)

where ($1) means the first parameter from the original function call is used for replacement function. There is also a more advanced syntax available. Like this:

  • ShellExecute --> if $3 match ":/" then OpenURL($3); OpenDocument($3)

ShellExecute can map to 2 different LCL functions. The string after "match" keyword is again a regular expression. If it matches, the replacement right after it is used, otherwise the next replacement.

Coordinate offsets

Components inside a visual container have different meaning for Top and Left coordinates between Delphi and Lazarus. In Delphi the coordinates are relative to container's Top and Left. In Lazarus they are relative to container's client area which leaves out the border. TGroupBox has the biggest problem because it has a title text on the border.

Subtracting an offset from the coordinates makes the form layout look the same as it looked in Delphi. The offsets are defined in "Coordinate offsets" in settings dialog.

Issues

...