1
votes

I wrote a custom component (TGMailLabel) in FireMonkey (Delphi XE5), everything seems ok but when I have an instance of the component on a FireMonkey (desktop) form and I try to "Edit custom style", the IDE places correctly a TStyleBook instance on my form and opens the Style Designer but no items are there.

If you do the same thing but trying to "Edit Default styles", it works...

You can find full source code in my blog post at http://blog.delphiedintorni.it/2013/11/tgmaillabel-un-esempio-di-custom.html (there is a link to full source code zip at the end of the article)

function TGMailLabel.GetStyleObject(const Clone: Boolean): TFmxObject;`
begin
  if (StyleLookup = '') then
    Result := TFmxObject(TStyleManager.LoadFromResource(HInstance, GMailLabelStyleName, RT_RCDATA))
  else
    Result := inherited GetStyleObject(Clone);
end;

Is this method implementation fully correct?

1
The relevant portion of your code should be posted here, not as a link to an external site. If the link is unavailable for some reason, the question loses much of its meaning. Also, the off-site content is not searchable here. See the help center guidelines, specifically the On Topic, the numbered list, item #2.Ken White
Sorry about that, it is difficult for me to point out which part is relevant and I thought it was enough to summarize the problem in my first statement and have a link to full source code on my blog. I am going to edit the post to add the method implementation I am more doubtful about...AndreaMagni

1 Answers

0
votes

The method you're using to load the style, i.e. overriding GetStyleObject is used by component vendors because it makes installing and using their components much easier for the end user. Indeed it can be a useful way to deploy your own components once you've finished developing them, since it works around some inadequacies in the management of styles by FireMonkey. However, it also bypassed some of the default behaviours of the styling system which are very useful when developing and editing styles.

Normally FireMonkey searches the active style and any style books for a suitable style based on the components class name or StyleLookup property. The code you're using loads it's style directly from a resource and ignores anything in the active style and style books, although it preserves the default behaviour if a StyleLookup is set.

In order to edit a style you will need to load it into a style book component. Firstly you'll need to wrap the style in a TLayout,

object TLayout
  ...Your style here
end

(don't worry about the indentation).

You can now double click your style book and open the file.

Set the StyleName of your style (the 'root' object of it) by taking the class name of your component, removing the T and appending 'style'. E.g. TMyEdit would become 'myeditstyle'.

Comment out your GetStyleObject routine and your component should pick up the style.

Now you come to the problem of deployment.

  • You can revert to the GetStyleObject method.
  • You can load the style into a style book on every form you want to use it on.
  • You can create a global style book and point the StyleBook property of every form to it (make sure you nil the property before you free each form).
  • You can edit the default style you are using to include it (not possible with system styles).
  • Or, a method I've not yet tried to inject your style elements into the active style by setting your style element's Parent to the object returned by TStyleManager.ActiveStyle.