Lazarus

Programming => General => Topic started by: HexLaden on February 06, 2023, 01:00:28 pm

Title: Can't set title and PreviewText in FontDialog
Post by: HexLaden on February 06, 2023, 01:00:28 pm
Hello!
Code: Pascal  [Select][+][-]
  1. ...
  2. dlgfont: TFontDialog;
  3. ...
  4.  
  5. procedure Tfrorm.btnFontClick(Sender: TObject);
  6. begin
  7.   dlgFont.Title:= 'Select font';
  8.   dlgFont.PreviewText:= 'The quick brown fox jumps';
  9.   dlgFont.Options:= dlgFont.Options + [fdEffects, fdApplyButton];
  10.   if not dlgFont.Execute then Exit;
  11.   ...
  12. end;

I'm using Lazarus 2.2.4 in Win 10.

Font dialog remains with standard title caption and standard 'Abcd' as preview text.

Tried to set them in object inspector's properties also, but had the same result.

Are these properties managed by the operating system only?

Thank you!
Title: Re: Can't set title and PreviewText in FontDialog
Post by: KodeZwerg on February 06, 2023, 01:39:40 pm
For the property PreviewText
Quote
Please note that use of PreviewText is dependent on platform / widgetset support. For instance, Windows does render PreviewText. It uses its own "Sample", and renders the font name in the selection list using the typeface. QT and QT5 have the same behavior as Windows. It is implemented for the GTK, GTK2 and MUI widgetsets.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: KodeZwerg on February 06, 2023, 01:42:53 pm
For the property Title
It is defined in commondialog.inc as
Code: Pascal  [Select][+][-]
  1. constructor TCommonDialog.Create (TheOwner: TComponent);
  2. begin
  3.   inherited Create(TheOwner);
  4.   FTitle := DefaultTitle;
  5. end;
Title: Re: Can't set title and PreviewText in FontDialog
Post by: balazsszekely on February 06, 2023, 03:04:57 pm
Hello!
Code: Pascal  [Select][+][-]
  1. ...
  2. dlgfont: TFontDialog;
  3. ...
  4.  
  5. procedure Tfrorm.btnFontClick(Sender: TObject);
  6. begin
  7.   dlgFont.Title:= 'Select font';
  8.   dlgFont.PreviewText:= 'The quick brown fox jumps';
  9.   dlgFont.Options:= dlgFont.Options + [fdEffects, fdApplyButton];
  10.   if not dlgFont.Execute then Exit;
  11.   ...
  12. end;

I'm using Lazarus 2.2.4 in Win 10.

Font dialog remains with standard title caption and standard 'Abcd' as preview text.

Tried to set them in object inspector's properties also, but had the same result.

Are these properties managed by the operating system only?

Thank you!
Welcome to the forum! I hope you're not a spammer or even worse a lurker.  :D
Almost everything is possible on windows. Please test attached project.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: HexLaden on February 06, 2023, 05:25:03 pm
@KodeZwerg. Thank you, I searched in the forum and over the net for the PreviewText information you provided now, but could't find it.

@GetMem: Thank you for the warm welcome. The forum community has nothing to worry about: I'm neither a spammer nor a lurker. I've been clarifying my doubts reading lots of excelent forum contributions on the past year and a half, or so. They are so good that I never needed to post any question, till now.
Your handler works perfectly. Very clever and elegant solution. Thank you!
Title: Re: Can't set title and PreviewText in FontDialog
Post by: balazsszekely on February 06, 2023, 06:08:23 pm
@HexLaden
Quote
I'm neither a spammer nor a lurker. I've been clarifying my doubts reading lots of excelent forum contributions on the past year and a half, or so. They are so good that I never needed to post any question, till now.
OK then, I owe you an apology. Unfortunately there are a lot of annoying spammers lately(the lurker part was a joke).

Quote
Your handler works perfectly. Very clever and elegant solution. Thank you!
I'm glad it's working, you're more then welcome!
Title: Re: Can't set title and PreviewText in FontDialog
Post by: KodeZwerg on February 06, 2023, 06:12:49 pm
You can turn on the hints/informations inside the IDE to see what I copy/paste for you.
Look on my attached image, the pop-up menu.
On second there the text I copied.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: HexLaden on February 06, 2023, 08:30:40 pm
Quote
You can turn on the hints/informations inside the IDE to see what I copy/paste for you.
My mistake. I haven't read them carefully when I first consulted the object inspector's property tips. They usually point out relevant information.
Quote
OK then, I owe you an apology. Unfortunately there are a lot of annoying spammers lately(the lurker part was a joke).
You were correct: I lurked your code  :) and I played with it:
Code: Pascal  [Select][+][-]
  1.     SetDlgItemTextW(dlgFontHandle,    1, PWideChar('That''s ok'   ));  // Ok btn
  2.     SetDlgItemTextW(dlgFontHandle,    2, PWideChar('Cancel this'  ));  // Cancel btn
  3.     SetDlgItemTextW(dlgFontHandle, 1026, PWideChar('Apply this'   ));  // Apply btn
  4.     SetDlgItemTextW(dlgFontHandle, 1040, PWideChar('Strike it'    ));  // Strikeout
  5.     SetDlgItemTextW(dlgFontHandle, 1041, PWideChar('Line under'   ));  // Underline
  6.     SetDlgItemTextW(dlgFontHandle, 1072, PWideChar('What about...'));  // Efects
  7.     SetDlgItemTextW(dlgFontHandle, 1073, PWideChar('Final effect' ));  // Sample
  8.     SetDlgItemTextW(dlgFontHandle, 1088, PWideChar('Choose font'  ));  // Font
  9.     SetDlgItemTextW(dlgFontHandle, 1089, PWideChar('Style it!'    ));  // Style
  10.     SetDlgItemTextW(dlgFontHandle, 1090, PWideChar('How big?'     ));  // Size
  11.     SetDlgItemTextW(dlgFontHandle, 1091, PWideChar('Colorize'     ));  // Color
  12.     SetDlgItemTextW(dlgFontHandle, 1093, PWideChar('Footnote comment'));
  13.  

Still didn't find how to reach sample 'AaBbCc...' text, thoug.

Title: Re: Can't set title and PreviewText in FontDialog
Post by: KodeZwerg on February 06, 2023, 08:55:05 pm
On Window OS machines you can EnumWindow the PID to compare found HWNDs Text property for "AaBbCc", exemplary as shown with a spy app.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: balazsszekely on February 06, 2023, 09:05:21 pm
@HexLaden @KodeZwerg
Quote
On Window OS machines you can EnumWindow the PID to compare found HWNDs Text property for "AaBbCc", exemplary as shown with a spy app.
You can easily replace the preview text, but then when the font type, size, etc changes, the font dialog redraws the original text, so you have to change it again.

I attach a demo application which replace the text after the font dialog is shown. I did not go further because honestly it's not worth the effort(at least for me).

Title: Re: Can't set title and PreviewText in FontDialog
Post by: Bogen85 on February 06, 2023, 09:21:39 pm
... or even worse a lurker.  :D

Heh, good one. Hah! :D
Title: Re: Can't set title and PreviewText in FontDialog
Post by: HexLaden on February 06, 2023, 10:41:05 pm
I intended to understand if and how it would be possible to implement both properties. In the context of the program I'm working on, implementing font prediction would add a small layer of comfort to the user, though it's neither fundamental nor imperative. I agree that the OS solution seems disproportionate. From the example you proposed, I now understood how to reach and replace the text. Thank you very much!
Title: Re: Can't set title and PreviewText in FontDialog
Post by: KodeZwerg on February 06, 2023, 11:15:55 pm
@GetMem, nice, but I am not interested in such :-[
@HexLaden, if the systems Dialog ain't fit your needs, you can simple create your own, to start with:
Code: Pascal  [Select][+][-]
  1. ListBox1.Items.AddStrings(Screen.Fonts);
Title: Re: Can't set title and PreviewText in FontDialog
Post by: jamie on February 07, 2023, 12:03:35 am
The Title issue can be fixed.

The Dialog Initiate message can set the title at that point if the given property isn't empty, otherwise it will use the default one.

 Also, I believe one can use the SetWindowText API to also do this at some point.

 But for the Preview/Sample text, that is another story.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: wp on February 07, 2023, 12:25:13 am
jamie, you once submitted a patch for the same Title issue with TColorDialog (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/39155). Could you try to do the same for TFontDialog?
Title: Re: Can't set title and PreviewText in FontDialog
Post by: jamie on February 07, 2023, 12:56:39 am
Yeah, its the same thing.

The Dialog callback is structured the same way.

Its in the win32WSDialogs.

"FontDialogCallBack" to be exact.

Title: Re: Can't set title and PreviewText in FontDialog
Post by: jamie on February 07, 2023, 01:29:44 am
This is the problem here.

at some point in history, someone decided the callback feature causes issues and the callback is only enabled when the ApplyButton is selected in the options.
Code: Pascal  [Select][+][-]
  1.  //setting CF_ENABLEHOOK shows an oldstyle dialog, unless lpTemplateName is set
  2.       //and a template is linked in as a resource,
  3.       //this also requires additional flas set:
  4.       //https://msdn.microsoft.com/en-us/library/windows/desktop/ms646832(v=vs.85).aspx
  5.       if (fdApplyButton in Options) then
  6.       begin
  7.         Flags := Flags or CF_ENABLEHOOK;
  8.         lpfnHook := @FontDialogCallBack;
  9.         lCustData := PtrInt(@ACommonDialog);
  10.       end;                                            
  11.  

 The fix I have can only work if this hook is enabled all the time, not just the applyBUtton.

Tell me what you think, if you enable the applybutton, does it change the view as it claims?
does not here.

--
 In the fontDialogCallBack, this is changed;

Code: Pascal  [Select][+][-]
  1.   case uMsg of
  2.     WM_INITDIALOG:
  3.     begin
  4.       //debugln(['FontDialogCallBack: WM_INITDIALOG']);
  5.       //debugln(['  PChooseFontW(LParam)^.lCustData=',IntToHex(PChooseFontW(LParam)^.lCustData,8)]);
  6.       PtrInt(Dlg) := PChooseFontW(LParam)^.lCustData;
  7.       If (Dlg <> Nil)and (Dlg^.Title <> '') THen SetWindowText(Wnd,Pchar(Dlg^.Title));//jp
  8.     end;
  9.     WM_COMMAND:            
  10.  

The line where my john handcock is!

Title: Re: Can't set title and PreviewText in FontDialog
Post by: jamie on February 07, 2023, 02:21:56 am
I now see the difference between them, it shortens the dialog and doesn't give you the extra options to select more fonts.

 This is very strange indeed. You would of thought MS would get their act together by now.

Title: Re: Can't set title and PreviewText in FontDialog
Post by: HexLaden on February 07, 2023, 11:48:16 am
Thanks, KodeZwerg for the suggestion and tip. For this project in particular I'll probably follow that way. For a general purpose, system's dialog is the adequate choice. Jamie's patch would be welcome.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: jamie on February 08, 2023, 03:19:38 am
it appears it is possible to define the Sample window along with the Caption of the dialog.

But as indicated before, doing so takes on older version of the Dialog box.

In any case, I was able to use FindWindowEx within the initiation of the dialog box to locate the control that houses the sample text and it happens to be a "STATIC" text box.

 I don't know how reliable this would be however.... But you can redefine the text contents at that point.

EDIT:

  I think this is a moot point because although i got the sample window to show the preview text it seems when you click around in the box the static text gets replaced with the original text and what this means you would have to answer to the BN_CLICKS, to keep refreshing it.
  It can be done but really.

  The Caption/Title seems to be ok however. but still, only shows in the old style viewing.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: balazsszekely on February 08, 2023, 06:34:17 am
@jamie

It's not worth the effort, I came to the same conclusion in post 9. If somebody really wants a custom FondDialog, better build one from scratch. It shouldn't take longer then 20-30 minutes.
Title: Re: Can't set title and PreviewText in FontDialog
Post by: KodeZwerg on February 08, 2023, 03:11:48 pm
I put something together, ready to use but currently not as a drop-in replacement.

Feel free to use it as a starting point and modify however you like.
TinyPortal © 2005-2018