Sign in Join
Wiki copytemporary

Delphi Converter in Lazarus: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11A Delphi project, Delphi package and a single unit file can be converted for Lazarus. The entries can be found in Lazarus Tools menu:
22
33- **Convert Delphi unit to Lazarus unit ...**
44- **Convert Delphi project to Lazarus project ...**
55- **Convert Delphi package to Lazarus package ...**
66
77There is also:
88
99- **Convert binary DFM file to text LFM and check syntax ...**
1010
1111which is seldom needed because the DFM form file is always converted while converting a unit.
1212
1313# Conversions
1414
1515Here the different conversions are listed briefly. See the sections below for details.
1616
1717## Unit file
1818
1919- All unit source files get a **{\$mode delphi}** directive. It makes the compiler support 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.
2020
2121- **{\$R *.DFM}** is replaced with **{\$R *.lfm}** if the form file is renamed and converted. Conditional compilation is used if the target is **Lazarus and Delphi**.
2222
2323- Unit names in Uses section and include file names are fixed to match the real case sensitive names found in the file system. This is needed for case sensitive file systems.
2424
2525- Some unit names in Uses section are either replaced or removed.
2626
2727- Some Delphi types are replaced with "fall-back" LCL types (same as in form files).
2828
2929- Some function names called in the code are replaced.
3030
3131## Form file
3232
3333- Older Delphi versions used a binary format for .dfm form files. They are always converted to ascii text format.
3434
3535- .dfm form file is renamed or copied to .lfm and converted as needed. There is also an option to use the same Delphi form file directly. See section **Target** below.
3636
3737- Unknown properties are removed.
3838
3939- Some Delphi types are replaced with "fall-back" LCL types (same as in unit files).
4040
4141- Top and Left coordinate offset adjustment for controls inside visual containers.
4242
4343# Settings dialog
4444
4545After selecting an entry from the Tools menu, and then selecting the unit / project / package to convert, the user is presented with a settings dialog. All settings are saved in file **delphiconverter.xml** in local Lazarus configuration directory. The dialog allows to configure many details of conversion:
4646
4747- **Project Path** shows the selected path.
4848
4949- **Make backup of changed files** saves the original files to directory ConverterBackup under the selected directory.
5050
5151- **Keep converted files open in editor**. When checked, the converted unit files remain open after conversion.
5252
5353Rest of the dialog settings are covered below.
5454
55-<figure>
56-<img src="ConvertSettings.png" title="Picture of settings dialog of Delphi converter" />
57-<figcaption>Picture of settings dialog of Delphi converter</figcaption>
58-</figure>
55+![](/wiki-images/0/06/ConvertSettings.png)
5956
6057## Target
6158
6259The conversion target is defined in settings dialog. It can be:
6360
6461- **Cross-platform** -- Replace Windows specific units with their cross-platform equivalents.
6562
6663- **Support Delphi** -- Tries to make units compatible with both Delphi and Lazarus by using {\$IFDEF...} in Uses section. The Delphi form file .dfm is copied to Lazarus form file .lfm and converted.
6764
6865- **Use the same DFM form file** -- The Delphi form file .dfm is used also with Lazarus. It is not modified except for converting binary format to ascii text format when needed.
6966
7067However maintaining the file later in both Delphi and Lazarus is problematic because components have slightly different properties.
7168
7269## Unit name replacement and removal
7370
7471When 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".
7572
7673The replacement supports regular expression syntax. For example:
7774
7875- ^Q(.+) --\> \$1
7976
8077means 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.
8178
8279Replacement can be interactive or automatic. When interactive, the user can edit the replacements before they are applied.
8380
8481If 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.
8582
86-<figure>
87-<img src="ConvertUnitsNotFound.jpg" title="Picture of Units Not Found dialog of Delphi converter" />
88-<figcaption>Picture of Units Not Found dialog of Delphi converter</figcaption>
89-</figure>
83+![](/wiki-images/6/63/ConvertUnitsNotFound.jpg)
9084
9185## Unknown property removal
9286
9387Not 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. The interactive removal currently happens in the same dialog that is used for component type replacements (see below). The property can be replaced, too, but that is usually not needed.
9488
9589## Type replacement
9690
9791Some 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:
9892
9993- TCoolBar --\> TToolBar.
10094
10195It is configurable, too, see "Type Replacements" in settings dialog.
10296
10397The same regular expression syntax as used in unit name replacement is supported here.
10498
10599The types are replaced in both the Pascal source file and the form file (.lfm). The replacement works also in a nested component structure where a component of replaced type has child components that must also be replaced.
106100
107101Different properties between the original component and the replacement component can cause problems after conversion. There is no easy solution for this.
108102
109103The best solution would be to make more Delphi compatible components and port all 3rd party components to Lazarus. Because of these potential problems the type conversion is always interactive.
110104
111-<figure>
112-<img src="ConvertFixLfm.jpg" title="Picture of Fix LFM dialog of Delphi converter" />
113-<figcaption>Picture of Fix LFM dialog of Delphi converter</figcaption>
114-</figure>
105+![](/wiki-images/2/2a/ConvertFixLfm.jpg)
115106
116107## Function call replacement
117108
118109Some Windows-only function calls in the source code can be replaced with a functionally similar RTL / LCL library function calls. It is configurable, see "Function Replacements" in settings dialog.
119110
120111There is a category defined for each function. The replacement can be enabled or disabled by category. For example there is "UTF8Names" category for file operation related functions.
121112
122113The syntax for replacements is:
123114
124115- FileExists --\> FileExistsUTF8(\$1)
125116
126117where (\$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:
127118
128119- ShellExecute --\> if \$3 match ":/" then OpenURL(\$3); OpenDocument(\$3)
129120
130121ShellExecute 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.
131122
132123## Coordinate offsets
133124
134125In Delphi and Lazarus, components have different meaning for Top and Left coordinates when they are inside a visual container. 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. It causes the most visible problem for TGroupBox because it has a title text on the border.
135126
136127Subtracting 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.
137128
138129# Issues
139130
140131When a Delphi program contains Windows API calls which are not ported in RTL/LCL, there is no automatic way to convert it. Other Windows specific technologies like OLE cause the same problem.
141132
142133Database connections often require changes. Either the DB or the connection component is not compatible.
143134
144135Another problem are some libraries which are not compatible with Delphi libraries. For example XML is used in a slightly different way. Maybe the converter can be improved in this area in the future.
145136
146137As mentioned earlier, Delphi components that don't exist in Lazarus/LCL cause problem, even if they are replaced with a fall-back component type, because they have different properties.
147138
148139Yet, if the program uses only VCL classes and functions, there is a big chance for the conversion to succeed.
149140
150141---
151142*From the Free Pascal wiki, page "Delphi Converter in Lazarus", as it stood on 2024-05-03 after a change by Hieroly, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
152143Category: Lazarus

Back to history