Jump to content
Uwe Raabe

Feature: ParentFont? Yes, but...

Recommended Posts

I would like to discuss a feature here before creating a QP entry for that (which seems not to be possible in the moment anyway).

 

There is often the case where I want some controls to be emphasized somehow by adjusting the font style or font color. This will automatically set ParentFont to False . As a drawback the control will no longer act on any changes to the parent font - obviously. In times of VCL styles and the ability for the user to change style and font, this can lead to a bit of extra work to get things looking pretty again.

 

What would you think of a feature like: Yes, I want to use the ParentFont, but don't touch my font style and/or font color settings.

 

I have something in mind like the StyleElements property, where you can select which font properties are to be inherited from the parent and which are not.

 

What do you think?

  • Like 4
  • Thanks 1

Share this post


Link to post

I remember how long it took me to change the old "MS something" to "Tahoma", because all bold/colored labels had ParentFont=false.

So yes, I think this would be useful.

Share this post


Link to post

Or get rid of "parentfont" and do it like css? Inherit and overwrite if apply.

Anyway, you have to implement it carefully to not to affect rendering speed too much and keep the dfm as tiny as possbile.

Share this post


Link to post

Uwe,

I have the same issue with one of my application. The app allows the user to adjust the main font by a few points (so old people like me can read the text) but the down side is as you describe: either loose style and colour changes or do extra work. So I would vote yes for some change to the style elements similar to below

TStyleElements = set of (seFont, seFontName, seFontSize, seFontStyle, seFontColour, seClient, seBorder)

 

Share this post


Link to post

Getting rid of something can be a bit tricky when you need to maintain your code also with older versions. The basic idea is to introduce that new property with an empty default value matching the current behavior. This will keep the DFM clean and ParentFont functioning as before.

 

These are the parts for a possible implementation:

type
  TFontElement = (pfeCharset, pfeColor, pfeHeight, pfeName, pfeOrientation, pfePitch, pfeSize, pfeStyle, pfeQuality);
  TFontElements = set of TFontElement;

The new property ParentFontElements would be initialized with an empty set, which would be omitted when writing the DFM.

    property ParentFontElements: TParentFontElements read FParentFontElements write SetParentFontElements default [];

The implementation would not affect any rendering at all. It can be implemented completely inside TControl.CMParentFontChanged without any impact to any other place.

procedure TControl.CMParentFontChanged(var Message: TCMParentFontChanged);
begin
  if FParentFont then
  begin
    if Message.WParam <> 0 then
      SetFont(Message.Font)
    else
      SetFont(FParent.FFont);
    FParentFont := True;
  end
  else if FParent <> nil then
  begin
    { TODO : order might be relevant }
    if pfeCharset in ParentFontElements then
      Font.Charset := FParent.Font.Charset;
    if pfeColor in ParentFontElements then
      Font.Color := FParent.Font.Color;
    if pfeHeight in ParentFontElements then
      Font.Height := FParent.Font.Height;
    if pfeName in ParentFontElements then
      Font.Name := FParent.Font.Name;
    if pfeOrientation in ParentFontElements then
      Font.Orientation := FParent.Font.Orientation;
    if pfePitch in ParentFontElements then
      Font.Pitch := FParent.Font.Pitch;
    if pfeSize in ParentFontElements then
      Font.Size := FParent.Font.Size;
    if pfeStyle in ParentFontElements then
      Font.Style := FParent.Font.Style;
    if pfeQuality in ParentFontElements then
      Font.Quality := FParent.Font.Quality;
  end;
end;

The ParentFontElements are only used when ParentFont is False, which is automatically set when changing some Font properties. Thus setting ParentFontElements must internally set ParentFont to False and trigger CM_PARENTFONTCHANGED:

procedure TControl.SetParentFontElements(const Value: TParentFontElements);
begin
  if FParentFontElements <> Value then
  begin
    FParentFontElements := Value;
    FParentFont := False;
    if (FParent <> nil) and not (csReading in ComponentState) then
      Perform(CM_PARENTFONTCHANGED, 0, 0);
  end;
end;

Apart from some real world testing and adjusting this is probably all that is needed for an implementation.

  • Like 4

Share this post


Link to post

Why not leave ParentFont property value to default, and modify font settings in Form.OnShow?

Share this post


Link to post

Of course there are workarounds and you can always do such settings in the code. I know there are suggestions to get rid of the DFM in general and do all in the code. On the other hand there are plenty of people that prefer setting such things in the Object Inspector.

Share this post


Link to post

For inspiration DevExpress controls have the standard ParentFont and individual properties to override font color and style (among other things) for Normal, Disabled, Focused and Hot state.

There's also a style controller component so different styles can be configured centrally instead of on the individual controls.

Share this post


Link to post
55 minutes ago, Uwe Raabe said:

Don't you think you've gone a bit overboard with the amount of font properties that can be handled?

 

I mean: Both Size and Height? One is derived from the other.

And in 25 years I don't think I've ever had to customize Quality or Pitch in the GUI...

Charset, AFAIK, isn't needed with unicode.

Share this post


Link to post

I included everything available just to not miss something. I agree that the list can be reduced. After all, it is only a suggestion - not the implementation.

It might be worth to add your comment to QP, so it will find its way to the person in charge.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×