Recent

Author Topic: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.  (Read 2301 times)

Otto

  • Full Member
  • ***
  • Posts: 226
Hello everyone.
I would need to modify some projects already built in Lazarus so that many of the controls are created at run time. When projects are few and relatively simple it is certainly not a problem to carry out such conversion, but when there are many projects and with complex interfaces the time required could be considerable. To speed up the process, a tool that converts the "Lazarus Form file" (.lfm) to pascal source code (.pas) would be useful.
Is there a tool to convert "Lazarus Form file" (.lfm) to pascal source code?
« Last Edit: January 17, 2020, 10:45:54 am by Otto »
Kind regards.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #1 on: January 17, 2020, 11:11:00 am »
Not that I know of, but I'd suggest investigating whether you can save yourself work by grouping controls into frames which can be reused.

You can right-click on a form and do a "View Source", but it's not Pascal.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #2 on: January 17, 2020, 11:44:30 am »
Thank you MarkMLl,
I think your suggestion to group the controls is great but in my case I don't think it brings any significant benefits; anyway I'll investigate further as soon as I find a little free time.
Kind regards.

mhelmstedt

  • Newbie
  • Posts: 4
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #3 on: January 17, 2020, 11:54:43 am »
Old and for dfm, but I think this can be adapted easily:
https://sourceforge.net/projects/dfmtopas/

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #4 on: January 17, 2020, 12:17:10 pm »
Thank you mhelmstedt,
for the suggestion, I will study the source code as soon as possible.

Otto.
Kind regards.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #5 on: January 17, 2020, 12:26:11 pm »
Old and for dfm, but I think this can be adapted easily:
https://sourceforge.net/projects/dfmtopas/

With the caveat that the .lfm files tend to be version-specific. Not grievously so, but enough to merit embedded version info:

Code: [Select]
object FormProfile: TFormProfile
  Left = 950
  Height = 240
  Top = 1152
  Width = 436
  BorderStyle = bsDialog
  Caption = 'Select Profile'
  ClientHeight = 240
  ClientWidth = 436
  Position = poDefaultPosOnly
  LCLVersion = '1.6.4.0'
  object ComboBox1: TComboBox
    Left = 24
    Height = 23
...

Note also that you've have to go back and do a bit of parsing on the .pas file to confirm what TFormProfile actually is.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #6 on: January 17, 2020, 12:40:34 pm »
According to https://www.mail-archive.com/lazarus@lists.lazarus-ide.org/msg08370.html there is a tool in the examples folder which comes with Lazarus:

Install lazarus/examples/pascalstream/CopyAsPasPkg/copyformaspascaldemopkg.lpk. Then there will be a command "Copy form as Pascal" in the context menu of the form which copies the pascal code corresponding to the lfm file into the clipboard. And from here you can paste it back into a pascal unit.

Still not yet automatic, but faster than converting the lfm to pas manually. And maybe you get an idea how to implement an automatic converter.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #7 on: January 17, 2020, 01:20:54 pm »
According to https://www.mail-archive.com/lazarus@lists.lazarus-ide.org/msg08370.html there is a tool in the examples folder which comes with Lazarus:

Well spotted/remembered.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #8 on: January 17, 2020, 03:58:04 pm »
Thank you wp,
your reporting was really very helpful. For now I have tested the converter with a test project and the result looked correct if no images were loaded into the RAD within the controls. If the controls contained images, they would have a representation incompatible with Pascal syntax. I believe that a reference to the image data could be used to solve the problem, perhaps by saving it within a resource file. I guess, somehow, you can adapt the image data from the RAD into a format that's compatible with resource files.
As soon as I have tested a complex project I will inform you about the correctness of the result obtained.

I take this opportunity to thank the entire Lazarus/FPC community for their efficiency in finding solutions to every single user need.

Otto.
Kind regards.

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: Tool to convert "Lazarus Form file" (.lfm) to pascal source code.
« Reply #9 on: February 16, 2021, 05:09:23 pm »
According to https://www.mail-archive.com/lazarus@lists.lazarus-ide.org/msg08370.html there is a tool in the examples folder which comes with Lazarus:

Install lazarus/examples/pascalstream/CopyAsPasPkg/copyformaspascaldemopkg.lpk. Then there will be a command "Copy form as Pascal" in the context menu of the form which copies the pascal code corresponding to the lfm file into the clipboard. And from here you can paste it back into a pascal unit.

Still not yet automatic, but faster than converting the lfm to pas manually. And maybe you get an idea how to implement an automatic converter.

When I try this package on a form which contains a TDBGrid with configured TDBGridColumns it aborts with the message "cannot write DefineProperties of "TColumn".

It works fine when the TDBGridColumns are not configured.

Is that because TColumns is not a component or some kind streamable object?
Lazarus 3.0/FPC 3.2.2

 

TinyPortal © 2005-2018