[SOLVED] How to make 2 improvements to function dialogs.MessageDlg ? lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2022-11-21 18:10 I want something like function dialogs.MessageDlg(), but with 2 improvements: I want it's position to change from the center of the monitor to the center of the calling Form I want to be able to increase the default width of the dialog. That means, I want a dialog which has a caption and shows a message and has some buttons and optional an icon. And has both above improvements. My question is: must I reinvent the wheel or is there a component which I can use, where I only have to add those 2 improvements? I want this for Windows and Linux for Lazarus 2.0.10 / FPC 3.2.0. Thanks in advance. ---------------------------------------------------------------------- Thaddy · 2022-11-21 18:19 Hey, OOP, so simply create a derived class. ---------------------------------------------------------------------- balazsszekely · 2022-11-21 18:26 @Hartmut QuoteI want something like function dialogs.MessageDlg(), but with 2 improvements:     I want it's position to change from the center of the monitor to the center of the calling Form     I want to be able to increase the default width of the dialog. That means, I want a dialog which has a caption and shows a message and has some buttons and optional an icon. And has both above improvements. My question is: must I reinvent the wheel or is there a component which I can use, where I only have to add those 2 improvements? I want this for Windows and Linux for Lazarus 2.0.10 / FPC 3.2.0. Thanks in advance. Maybe this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MessageDlgEx(const ACaption, AMsg: string; ADlgType: TMsgDlgType;  AButtons: TMsgDlgButtons; AParent: TForm): TModalResult;var  MsgFrm: TForm;begin  MsgFrm := CreateMessageDialog(ACaption, AMsg, ADlgType, AButtons);  try    MsgFrm.FormStyle := fsSystemStayOnTop;    if AParent <> nil then    begin      MsgFrm.Position := poDefaultSizeOnly;      MsgFrm.Left := AParent.Left + (AParent.Width - MsgFrm.Width) div 2;      MsgFrm.Top := AParent.Top + (AParent.Height - MsgFrm.Height) div 2;    end    else      MsgFrm.Position := poWorkAreaCenter;    Result := MsgFrm.ShowModal;  finally    MsgFrm.Free  end;end; procedure TForm1.Button1Click(Sender: TObject);begin  MessageDlgEx('This is the caption',               'This is a very, very................................................ ............................. long message!',                mtConfirmation,                [mbYes, mbNo, mbCancel],              Form1);end; ---------------------------------------------------------------------- wp · 2022-11-21 19:10 An alternative could be the TTaskDialog which is very flexible (https://wiki.freepascal.org/TTaskDialog): - Select the buttons from the CommonButtons property (or add specific ones by means of the Buttons property). - Enter the message text in the Text property - Select the iocn from the MainIcon property options - Check the option tfPositionRelativeToWindow to center the dialog relative to the calling form. - Double-click on the TaskDialog icon to see what it is looking like - One important oddity of the TaskDialog: The return value of the Execute method indicates that the dialog could be created, but not that OK has been pressed. Check the modal result for this purpose. - You can also add radiobuttons, or a checkbox ("Do not show again"). ---------------------------------------------------------------------- Hartmut · 2022-11-22 09:41 Quote from: Thaddy on November 21, 2022, 07:19:40 pmHey, OOP, so simply create a derived class. Hey Thaddy, dialogs.MessageDlg() is a function, not a class. So which class should I derive? That was my question... Thanks a lot GetMem for that demo. I tested it and it centers the dialog to the calling Form. But I found no way to increase the width of the dialog (*including* the message width). And a litte disadvantage is, that the Buttons have (on Linux) now a caption in English, not in German like with dialogs.MessageDlg(). Do you have an idea to change one of them or both? Thanks a lot to wp for that suggestion. I will elaborate on it and see what I can implement. If I'll have problems I will report them ;-) ---------------------------------------------------------------------- madref · 2022-11-22 10:36 Or just make your own MessageDlg from a form and let that be on tom until a button is pressed on that form. ---------------------------------------------------------------------- Hartmut · 2022-11-22 15:38 Quote from: wp on November 21, 2022, 08:10:09 pmAn alternative could be the TTaskDialog which is very flexible (https://wiki.freepascal.org/TTaskDialog): - Select the buttons from the CommonButtons property (or add specific ones by means of the Buttons property). - Enter the message text in the Text property - Select the iocn from the MainIcon property options - Check the option tfPositionRelativeToWindow to center the dialog relative to the calling form. - Double-click on the TaskDialog icon to see what it is looking like - One important oddity of the TaskDialog: The return value of the Execute method indicates that the dialog could be created, but not that OK has been pressed. Check the modal result for this purpose. - You can also add radiobuttons, or a checkbox ("Do not show again"). I played with 'TTaskDialog' and found it very mighty. Especially the possibility to add your own buttons and to have a checkbox like "Don't show me again". So I'm sure I will use this dialog in the future. Thanks a lot to wp for you informations, with these it was easy for me to get it to run. I did not know 'TTaskDialog' before. But for my current needs I face 2 disadvantages:  - I found no way to influence the width of the dialog. Is this possible?  - if no 'Title' is used, then still a huge space is reserved for that (see screenshot). Beside 'caption' and 'message' I need no 3rd text. Is it possible to get rid of this extra space? Thanks in advance. ---------------------------------------------------------------------- wp · 2022-11-22 16:16 Quote from: Hartmut on November 22, 2022, 04:38:03 pm - I found no way to influence the width of the dialog. Is this possible? It is prepared internally, but there is no property for it. Please make the following changes: unit dialogs.pp (in folder lcl of your Lazarus installation), declaration of TCustomTaskDialog:After field FVerificationText:TTranslateString add a new field:   FWidth: Integer;At the end of the public declarations of TCustomTaskDialog add:   property Width: Integer read FWidth write FWidth default 0;At the end of the published declarations of TTaskDialog add:   property Width;file taskdialog.inc (in folder lcl/include): function TCustomTaskDialog.DoExecute: Search for TaskDlg.Execute. You'll find a function call with many parameters. In the second line, between "DefRB" and "ParentWnd", there is a "0". Replace this 0 by FWidth.Rebuild the IDE. When the new IDE starts the TTaskDialog will have a new property "Width". You will only be able to increase the width of the dialog, it will not be possible to shrink it.Test the new property. It should work, but I would like to hear your feedback before I commit these changes to Laz/main. Quote from: Hartmut on November 22, 2022, 04:38:03 pm - if no 'Title' is used, then still a huge space is reserved for that (see screenshot). Beside 'caption' and 'message' I need no 3rd text. Is it possible to get rid of this extra space? From your screenshot I see that you seem to be on a Linux where the emulated version of the original Windows TaskDialog is called. On Windows, however, the main icon and title areas are hidden when the icon is tdiNone and the Text is empty. So, this seems to be a bug in the emulation in unit LCLTaskDialog. Please post a bug report. ---------------------------------------------------------------------- wp · 2022-11-22 17:06 Quote from: wp on November 22, 2022, 05:16:05 pmYou will only be able to increase the width of the dialog, it will not be possible to shrink it.[/li][/list] This was my observation on Windows where the native TaskDialog is used. When I switch it to use the emulated dialog, however, I found that I can make the IDE non-responsive by making Width too small. Therefore, please make another change. Now open the file lcl/lcltaskdialog.pas. Search for if aWidth=0 (no spaces around "="!). Replace the entire "if" block by the following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     if aWidth<=0 then begin      aWidth := Dialog.Form.Canvas.TextWidth(Inst);      if (aWidth>300) or (Dialog.Form.Canvas.TextWidth(Content)>300) or         (length(Buttons)>40) then        aWidth := 480 else        aWidth := 420;    end else      if aWidth<120 then aWidth := 120; Rebuild the IDE again, and the IDE should be safe now. You will be able to decrease the width down to 120 px. The native TaskDialog will ignore shrinking widths as already noted. ---------------------------------------------------------------------- Hartmut · 2022-11-22 18:12 Hello wp, thank you so much for your idea to improve dialog 'TTaskDialog' with a property 'width'. Quote from: wp on November 22, 2022, 05:16:05 pmQuote from: Hartmut on November 22, 2022, 04:38:03 pm - I found no way to influence the width of the dialog. Is this possible? It is prepared internally, but there is no property for it. Please make the following changes: ... You will only be able to increase the width of the dialog, it will not be possible to shrink it. To increase is absolutely enough, *if* the width of the message increases accordingly. Will this be the case (on Windows and Linux)? Otherwise it would not help me... QuoteTest the new property. It should work, but I would like to hear your feedback... I will do this test, but give me some time for this. My newest Lazarus is 2.0.10 with FPC 3.2.0. I hope this is ok? Quote... before I commit these changes to Laz/main. That would be great for the future. QuoteFrom your screenshot I see that you seem to be on a Linux... that's right Quote... on Linux where the emulated version of the original Windows TaskDialog is called. I did not know of this before QuoteOn Windows, however, the main icon and title areas are hidden when the icon is tdiNone and the Text is empty. So, this seems to be a bug in the emulation in unit LCLTaskDialog. Please post a bug report. I tested this on Win7 and (an older) Win10 (with Lazarus 2.0.10 / FPC 3.2.0), but in both cases I have the same output (huge wasted space, see new screenshot) as in Linux. Quote from: wp on November 22, 2022, 06:06:37 pmQuote from: wp on November 22, 2022, 05:16:05 pmYou will only be able to increase the width of the dialog, it will not be possible to shrink it. This was my observation on Windows where the native TaskDialog is used. When I switch it to use the emulated dialog, however, I found that I can make the IDE non-responsive by making Width too small. Therefore, please make another change. Now open the file lcl/lcltaskdialog.pas. Search for if aWidth=0 (no spaces around "="!). Replace the entire "if" block by the following code: OK. ---------------------------------------------------------------------- madref · 2022-11-22 18:16 I have created my own flexible message dialogs. Here are two examples. And you can create a lot more. This is a form which stays on top until a button is pressed. ---------------------------------------------------------------------- wp · 2022-11-22 18:26 Quote from: Hartmut on November 22, 2022, 07:12:49 pmTo increase is absolutely enough, *if* the width of the message increases accordingly. You mean that the width of the dialog should increase automatically depending on the length of the displaye message? No, this will not work. The new Width property must be known before-hand, independently of the message which simply will wrap if it is too long. Quote from: Hartmut on November 22, 2022, 07:12:49 pmMy newest Lazarus is 2.0.10 with FPC 3.2.0. I hope this is ok? v2.0.10 already has the TaskDialog, and a quick scan over the code showed that the relevant parts are the same (there were some changes with the icons, though). The patch should compile. But to be on the safe side, make sure to have a backup copy of the files to be changed before doing anything. Quote from: Hartmut on November 22, 2022, 07:12:49 pmI tested this on Win7 and (an older) Win10 (with Lazarus 2.0.10 / FPC 3.2.0), but in both cases I have the same output (huge wasted space, see new screenshot) as in Linux. Well, there was some rework with the dialog recently, and I only checked it with Laz/main. ---------------------------------------------------------------------- Hartmut · 2022-11-23 09:44 Quote from: wp on November 22, 2022, 07:26:23 pmQuote from: Hartmut on November 22, 2022, 07:12:49 pmTo increase is absolutely enough, *if* the width of the message increases accordingly. You mean that the width of the dialog should increase automatically depending on the length of the displaye message? No, this will not work. The new Width property must be known before-hand, independently of the message which simply will wrap if it is too long. No, there we misunderstood. I mean, if I set a higher dialog Width (by your new property, before calling TTaskDialog.Execute, e.g. setting TTaskDialog.Width:=800), will then this extra horizontal space also be used to display and wordwrap the message-text (on Windows and Linux)? ---------------------------------------------------------------------- wp · 2022-11-23 11:36 Yes - see screenshot. Wrapping works for native and emulated mode of the TaskDialog. But in Windows there is an issue that the width is specified in "dialog units". It seems that I have to divide the Width property value by 2 before passing it to the TaskDialogIndirect windows function, but I am not sure whether this is correct all the time. Windows documentation wants me to call the "GetDialogBaseUnits" function (https://learn.microsoft.com/en- us/windows/win32/api/winuser/nf-winuser-getdialogbaseunits), but this returns unreasonably large numbers. ---------------------------------------------------------------------- Hartmut · 2022-11-23 14:45 Quote from: wp on November 23, 2022, 12:36:57 pmYes - see screenshot. Wrapping works for native and emulated mode of the TaskDialog. That's great and exactly what I was for :-)) QuoteBut in Windows there is an issue that the width is specified in "dialog units". It seems that I have to divide the Width property value by 2 before passing it to the TaskDialogIndirect windows function, but I am not sure whether this is correct all the time. Maybe this has to do with High-DPI-Awareness? Then the factor should depend on your DPI-settings in Windows... (don't know more about that). I'm unsure how to rebuild the IDE. I did it once 3 years ago and forgot everything and the dialog now seems to contain other fields: In Lazarus in the Tools-menu I found menu "Build Lazarus with Profile: Normal IDE" and menu "Configure 'Build Lazarus' ..." which opens a dialog with many options (see screenshot). Which menu should I use and what must I enter if you want me to use the 2nd menu (for Windows and Linux)? ---------------------------------------------------------------------- wp · 2022-11-23 14:55 After many changes it is enough to do just "Tools" > "Build Lazarus with Profile: ". Only when you feel that something was not recompiled or if there were compilation errors which did not happen before, go to "Tools" > "Configure 'Build Lazarus'", and in the "Clean up" box check "Clean all" and "Switch after building to automatically". I don't know why in some Linuxes these options are missing in the Clean up box... If there is no way to activate them you can also select the option "Clean up and recompile" in the "Profile to build" combobox at the top. Then click "Build" and wait until the IDE restarts. ---------------------------------------------------------------------- Hartmut · 2022-11-23 18:19 Hello wp, I have very good news. I tested your changes carefully and everything works perfectly: I made all the following tests on Linux (Ubuntu 18.04) and Win7 and (an older) Win10 with Lazarus 2.0.10 / FPC 3.2.0:  - default width of the dialog is 480 (with a longer text)  - increasing in steps up to 2500 => works  - decreasing in steps down to 120 => works  - a width of 1..119 results in 120  - a width <= 0 results in the default width (480) That was exactly what I expected from the if-statement, which I replaced in the sources. Quote from: wp on November 23, 2022, 12:36:57 pmBut in Windows there is an issue that the width is specified in "dialog units". It seems that I have to divide the Width property value by 2 before passing it to the TaskDialogIndirect windows function, but I am not sure whether this is correct all the time. I did *not* see this effect on my Win7 and Win10. The resulting width was always 1:1 the same as what I had set. But I don't have High-DPI- settings on my Windows. The only thing which still could improved is that in the Objectinspector the new property 'Width' has no remark. Maybe you want something to set like "can set the Width of the Dialog (Minimum=120, 0=Automatic=Default)". To rebuild the IDE worked with menu Tools / "Build Lazarus with Profile: Normal IDE" on Windows and Linux. Thank you again very much for improving this dialog and your continuous help. ---------------------------------------------------------------------- AlexTP · 2022-11-23 20:43 @wp, Your changes to TaskDialog won't break existing apps, so I suggest to merge them to git. Also no incompat with Delphi? ---------------------------------------------------------------------- wp · 2022-11-23 23:14 Committed to Laz/main (https://gitlab.com/freepascal.org/lazarus/lazar us/-/commit/68e6168982d801cf7708edb74c9e8662db6c4199). I will investigate the size discrepancy in the native dialog later. Quote from: AlexTP on November 23, 2022, 09:43:09 pmAlso no incompat with Delphi? Well, in the TaskDialog of Delphi (XE 10.4) there is no Width property at all... Quote from: Hartmut on November 23, 2022, 07:19:57 pmThe only thing which still could improved is that in the Objectinspector the new property 'Width' has no remark. Maybe you want something to set like "can set the Width of the Dialog (Minimum=120, 0=Automatic=Default)". dsiders usually adds help texts pretty soon. Thank you, Don, for your tireless efforts, BTW... ---------------------------------------------------------------------- dsiders · 2022-11-24 01:14 Quote from: wp on November 24, 2022, 12:14:07 amCommitted to Laz/main (https://gitlab.com/freepascal.org/lazarus/lazarus/- /commit/68e6168982d801cf7708edb74c9e8662db6c4199). Quote from: Hartmut on November 23, 2022, 07:19:57 pmThe only thing which still could improved is that in the Objectinspector the new property 'Width' has no remark. Maybe you want something to set like "can set the Width of the Dialog (Minimum=120, 0=Automatic=Default)". dsiders usually adds help texts pretty soon. Thank you, Don, for your tireless efforts, BTW... You're welcome Werner. Updates to dialogs and lcltaskdialog are in progress. ---------------------------------------------------------------------- AlexTP · 2022-11-24 06:26 For Win32 TaskDialog it is also possible to set width, I found several topics at stackoverflow, can you look please? ---------------------------------------------------------------------- wp · 2022-11-24 17:21 Quote from: wp on November 24, 2022, 12:14:07 amI will investigate the size discrepancy in the native dialog later. Fixed now (In fact I already had the solution yesterday (based on https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf- winuser-getdialogbaseunits), but had the arguments of the MulDiv in the wrong order...). ---------------------------------------------------------------------- wp · 2022-11-24 23:36 Quote from: Hartmut on November 22, 2022, 04:38:03 pm - if no 'Title' is used, then still a huge space is reserved for that (see screenshot). Beside 'caption' and 'message' I need no 3rd text. Is it possible to get rid of this extra space? Now I committed a fix for this issue to Lazarus/main. It's not perfect, but much better. If you do not work with Laz/main you can follow these steps to get the changes: Open file lcltaskdialog.pasFind the (long) procedure function TTaskDialog.Execute, and within it the nested function AddLabel. Immediately after its begin add the line     if Text = '' then exit(nil);Now go down into the bulk of the Execute function and find the following IF block: if (LCL_IMAGES[aDialogIcon]<>0). In its else block there is a statement if not aEmulateClassicStyle then. Change it to     if (not aEmulateClassicStyle) and (Inst <> '') then.And, finally, toward the end of the unit there is procedure TTaskDialog.SetElementText with a case block. The first case item (tdeContent..tdeMainInstruction) contains the line Dialog.Form.Element[element].Caption := Text. Put a nil-check in front of this statement:     if Dialog.Form.Element[element] <> nil then Dialog.Form.Element[element].Caption := TextThat's all. Rebuild the IDE and test. Of course, as usual, make a backup copy of the file before changing anything. ---------------------------------------------------------------------- Hartmut · 2022-11-25 10:58 Quote from: wp on November 25, 2022, 12:36:34 amQuote from: Hartmut on November 22, 2022, 04:38:03 pm - if no 'Title' is used, then still a huge space is reserved for that (see screenshot). Beside 'caption' and 'message' I need no 3rd text. Is it possible to get rid of this extra space? Now I committed a fix for this issue to Lazarus/main. It's not perfect, but much better. Thank you very much for these changes. However I did not find the 2nd place with    if (LCL_IMAGES[aDialogIcon]<>0) because there is nothing similar in my Lazarus 2.0.10. I attached the sourcefile with my other changes. Maybe you can point me to the right position? The change in the 3rd place had a minimal discrepance to your description: You wrote:    Dialog.Form.Element[element].Caption := Text while I found:    Dialog.Form.Element[element].Caption := CR(Text) but from my understanding this should be ok? ---------------------------------------------------------------------- wp · 2022-11-25 11:33 Quote from: Hartmut on November 25, 2022, 11:58:10 amI did not find the 2nd place with    if (LCL_IMAGES[aDialogIcon]<>0) because there is nothing similar in my Lazarus 2.0.10. Ah, yes, this is because there were changes regarding the dialog's icons recently. Sorry. The same position in Laz 2.0.12 looks like this (line #893): Code: [Select]     if (LAZ_ICONS[aDialogIcon]<>'') {$IFDEF MSWINDOWS}or (WIN_ICONS[aDialogIcon]<>nil){$ENDIF} then You must scroll down about 30 lines to find the "else" part and change it as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     begin    Image := nil;      if (not aEmulateClassicStyle) and (Inst <> '') then    // <---- " and (Inst <> '')" added        IconBorder := IconBorder*2;      X := IconBorder;      Y := IconBorder;    end; Quote from: Hartmut on November 25, 2022, 11:58:10 amThe change in the 3rd place had a minimal discrepance to your description: You wrote:    Dialog.Form.Element[element].Caption := Text while I found:    Dialog.Form.Element[element].Caption := CR(Text)That's fine. You could also omit this one in a quick-and-dirty solution because this function normally is not called. ---------------------------------------------------------------------- Hartmut · 2022-11-25 16:24 Hello wp, I tested your new changes on Linux (Ubuntu 18.04) and Win7 and (an older) Win10 with Lazarus 2.0.10 / FPC 3.2.0 and everything works perfectly: if there is no 'Title' and no Icon, then no more extra space is wasted :-)) Again thank you very much for those improvements to this awesome dialog.