Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-10

Change default component name

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michalis Kamburelis

Hi,

When you drop a component on a form, by default the name of the component is derived from the ClassName with leading 'T' removed (and number suffix added). Now, I'm wondering about registering a class named T3DScene as a component, and this causes problems: when you drop this component on a Lazarus form, the default name is "3DScene1", which obviously isn't a valid Pascal identifier. Lazarus shows an error message "Error setting the name of a component :T3DScene to 3DScene1" and refuses to add the component to the form.

1. Is there a solution for this? Is there maybe a virtual method like TComponent.GetDefaultName that I can override, or some other way to tell Lazarus that the default name for T3DScene class should be like "Scene1", "Scene2" etc.?

2. Or do I just have to rename the class (to e.g. "TScene" or "TScene3D") to make it work without problems with form designer?

Browsing the Lazarus sources, I didn't see any such way (it seems that the logic to remove 'T' from ClassName is just hardcoded in some places), so I'm guessing it's 2., but wanted to make sure :)

Thanks,
Michalis

Reply by email

Michalis Kamburelis
to Michalis Kamburelis
Michalis Kamburelis wrote
Browsing the Lazarus sources, I didn't see any such way (it seems that the logic to remove 'T' from ClassName is just hardcoded in some places), so I'm guessing it's 2., but wanted to make sure :)

To answer my own question, the logic is inside
TCustomFormEditor.CreateUniqueComponentName, and there doesn't seem to exist a reasonable way to change it. In theory, I could try assigning my own Name in constructor looking at my Owner, but then I also need to make sure myself that the name is unique (add unique number suffix), opening a can of worms (since I would have to replicate part of TCustomFormEditor.CreateUniqueComponentName implementation then, and fear that it can change one day :).

I guess the class should just be renamed.

Michalis

Reply by email

Mattias Gaertner
to Michalis Kamburelis
Show quoted text 8 lines
Michalis Kamburelis wrote: > Browsing the Lazarus sources, I didn't see any such way (it seems that > the logic to remove 'T' from ClassName is just hardcoded in some > places), so I'm guessing it's 2., but wanted to make sure :) > To answer my own question, the logic is inside TCustomFormEditor.CreateUniqueComponentName,

I added a check. Please test.

Show quoted text 9 lines
and there doesn't seem to exist a reasonable way to change it. In theory, I could try assigning my own Name in constructor looking at my Owner, but then I also need to make sure myself that the name is unique (add unique number suffix), opening a can of worms (since I would have to replicate part of TCustomFormEditor.CreateUniqueComponentName implementation then, and fear that it can change one day :). I guess the class should just be renamed.

Yes, that would be nicer for many users.

Mattias

Reply by email

Michalis Kamburelis
to Mattias Gaertner
Mattias Gaertner wrote
> To answer my own question, the logic is inside > TCustomFormEditor.CreateUniqueComponentName, I added a check. Please test.

New ClassNameToComponentName works fine :), tested with Lazarus trunk compiled by FPC 2.4.4. The default component name is now "T3DScene1", which makes it a valid identifier.

Show quoted text 5 lines
> > I guess the class should just be renamed. Yes, that would be nicer for many users.

Will do. (I want to support at least Lazarus 0.9.30 anyway, so even if Lazarus trunk will get nicer solution, I still have to workaround it for existing stable Lazarus version.)

Thanks,
Michalis

Reply by email

Hans-Peter Diettrich
to Michalis Kamburelis: To answer my own question, the logic is inside…
Show quoted text 9 lines
Michalis Kamburelis wrote: > Browsing the Lazarus sources, I didn't see any such way (it seems that > the logic to remove 'T' from ClassName is just hardcoded in some > places), so I'm guessing it's 2., but wanted to make sure :) > To answer my own question, the logic is inside TCustomFormEditor.CreateUniqueComponentName, and there doesn't seem to exist a reasonable way to change it.

Right, you are not the only one having trouble with component names :-(

Show quoted text 6 lines
In theory, I could try assigning my own Name in constructor looking at my Owner, but then I also need to make sure myself that the name is unique (add unique number suffix), opening a can of worms (since I would have to replicate part of TCustomFormEditor.CreateUniqueComponentName implementation then, and fear that it can change one day :).

I already had to copy and implement my own version, for the dockable IDE layout save/restore.

I guess the class should just be renamed.

As a workaround, yes. But there should exist better means for dealing with component names, independent from the form designer (which is not normally part of an application).

DoDi

Reply by email