Delphi Fmx (DFM) Objects Into Code

Introduction

Recently I downloaded a Delphi project from GitHub, the beginning of a Trello clone.

I found the application, although in very early stages, to be quite charming, and wanted to play around with it a little:

trello

The first thing I set out to do was to dynamically generate items, and it struck me that it would be great if I could copy a component from the Form Designer and paste it into the Code Editor, with Delphi automatically generating the code for me.

Delphi persists components into a format known as DFM.  You can simply copy a component and paste it as text into Sublime Text, for example. With this in mind I spent a day writing an application to convert DFM objects into code, as a proof of concept, a prototype. I was able to generate code from the DFM which produced the same result:

oic

In general, the goal is not to produce so many objects, this was just a test. Ideally it is used to produce code for components that will be created dynamically at runtime, such as list items.

The particular item I was initially after was this:

oic4

Now the code is generated, I am able to add parameters to the function to customize the text and color, and add items dynamically at runtime.

Oic – Objects Into Code

As mentioned, I only spent a day working on this, so it has its limitations. At the moment it does not handle certain components like TImages with MultiResBitmaps. I’m sure there are many other issues, for example in some cases there is no one-to-one mapping between a DFM property name or value and the related Code/Class property name or value. As I came across these issues I provided for them, I’m sure there are many more.

The project is written in C# and is available here on GitHub, the read me contains more details.

Essentially you paste a DFM in the left editing panel, click a button (>>) and the code is (hopefully) generated in the right editing panel. There are currently three examples available via buttons accordingly named (1,2,3).

In my spare time, I’d like to evolve this application, adding functionality and examples.

oic2

The code is a bit messy as it was hurried, everything is minimal – this is after all just a prototype. The core of the application is encapsulated in these three lines:

oic3

Purpose

The purpose of this small exercise was to consider the feasibility of adding this functionality via a Delphi plugin to the IDE. Having been a way from Delphi for many years, I have a lot to catch up on, this project will at least help me get up to speed on Delphi’s DFM format.

Having noticed Mitech’s DFMEditor, my initial thoughts were that I could copy samples from the DFM exposed by his awesome application, and generate the corresponding code in mine. Both tools, partners in crime. On second thoughts, I’m suspecting somebody has already achieved, in a more excellent way, what I set out to do and I may be re-inventing the wheel.

Conclusion

It has been a fun exploration, and I am enjoying learning about Delphi’s DFM format. If you have any feedback or pointers to more information, I would be most grateful.

Update

As suspected, plugins already exist for this functionality. The GExperts plugin has Component to Code, and CNPack has a Components to Code wizard. For further study, there is an excellent article available here on the DFM format and writing a parser for it, and another here on serialization.

There’s really no need to pursue this anymore, so I’ll park the prototype as is. It was still a worthy one day exercise as I got a chance to spend some time understanding the DFM format.

Big thanks to the Delphi FB community for their feedback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s