Additional right aligned text in Caption of MenuItem lazarus / 31-LCL ---------------------------------------------------------------------- LazProgger · 2021-07-09 20:11 In Delphi, you can use the following code to set an additional right aligned text for the caption of a MenuItem: 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 MenuItem.Caption:='Text Left' + #9 + 'Text Right'; This makes "Text Left" left aligned like a normal menu item caption and "Text Right" right aligned like also possible keyboard shortcuts of the menu item are displayed. However, in Lazarus (tested on Windows and Linux), this is not working. The tab character #9 is displayed as a space with some more width, but there is no alignment making several menu items under each other looking ugly. How can I get the same result in Lazarus? Do I have to use another character as separator? Because it is working for the shortcuts, there must be a way in Lazarus, I think. How is it done there? ---------------------------------------------------------------------- jamie · 2021-07-09 20:46 You are relying on something that just happens to work for you. Windows processes the #9 because Delphi uses the windows implementation directly.. I believe Lazarus (LCL) actually has the code in ownerDraw mode already and is doing all of the menu drawing.. However, the TmenuItem also has that same event for you to do the drawing yourself so implement a single OnDrawItem event and simply do the text drawing yourself. ---------------------------------------------------------------------- LazProgger · 2021-07-10 03:10 Good to know that Delphi is doing that in this other way. Of course, owner drawing is a way but when doing that, the theme is gone (for example this shiny border/background on Windows, the orange surrounding on Ubuntu etc) - it will look the same on all platforms / OS versions. When Lazarus is using OwnerDraw internally, how is it done there using the themes? I have tried to look at the code in Menus and menuitem.inc but I have found nothing. In OwnerDraw, only the user assigned procedures are called and something like SetCaption is defined as virtual where I can see no code. ---------------------------------------------------------------------- jamie · 2021-07-10 10:34 Lazarus has a Themes lib that is used, it's what windows uses.. include the themes unit and study it.. ---------------------------------------------------------------------- jamie · 2021-07-10 13:51 I look a little deeper for you and after reviewing the WS32Menues file I would venture to say that maybe the problem could be in the function UTF8toUTF16 found in the LazUtitls because there it depends a lot on converting text over to a true string size. Maybe the #9 isn't getting spaced correctly ? Would need to test the function separately to determine that. ---------------------------------------------------------------------- jamie · 2021-07-10 14:27 Looking in the WIn32WSMenus I see in function DrawItemText(……) for the shortcut text they didn't include the DT_EXPANDTABS.  Its seems to be ok for the normal text but for ShortCUt text it does not include this..  Does this sound like your problem ? ---------------------------------------------------------------------- LazProgger · 2021-07-10 15:20 Quote from: jamie on July 10, 2021, 03:51:04 pmMaybe the #9 isn't getting spaced correctly ? That can be the reason. There is a spacing but not that aligned spacing like it is done with the shortcuts. Perhaps in that text conversions the #9 isn't passed to the Windows API or something like that. Quote from: jamie on July 10, 2021, 04:27:14 pm Does this sound like your problem ? Yes, that's sounding like my problem. When Windows is caring about converting the #9 to the alignment I have searched for DT_EXPANDTABS and found that API reference from Microsoft: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser- drawtext Unfortunately, the explanation of DT_EXPANDTABS gives no exact information what DT_EXPANDTABS is doing. But maybe it is the reason. Another promising parameter is DT_TABSTOP. Again, the provided information is not giving much information but one could understand that parameter in a way that those tab stops can create the behavior I want: The normal menu item caption in the first "column", the shortcuts in the second "column" all under each other. But I have no idea whether this is the reason. I will have a look at WIn32WSMenus, maybe I can find more information there. ---------------------------------------------------------------------- jamie · 2021-07-10 15:47 Disable themes in your app via the project objects. Rebuilt and then see how it looks There are two set's of drawing codes in that file, one for Themes and the other without themes. Its possible the Themes only is the problem...   but even there, I noticed the ShortCut Text does not have the DT_EXPANDTABS applied to it.  With the DT_EXPANDTABS it's optional to adjust the spacing via adjusting the upper portion of the flag which carries not flag identifiers.    I think leaving at 0 defaults to 8 places. Anything else uses that value.. ---------------------------------------------------------------------- LazProgger · 2021-07-10 16:04 Quote from: jamie on July 10, 2021, 05:47:48 pmDisable themes in your app via the project objects. You mean the option "use manifest resource (and enabled themes)" in the project options in the section "Application". I have disabled the option and the result is the same. ---------------------------------------------------------------------- LazProgger · 2021-07-10 16:14 Thanks to pointing me to Win32WSMenus. I found 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  function CompleteMenuItemStringCaption(const AMenuItem: TMenuItem; ACaption: String; Spacing: String): string;begin  Result := ACaption;  if AMenuItem.ShortCut <> scNone then    Result := Result + Spacing + MenuItemShortCut(AMenuItem);end;   This seems to be the code where the spacing between the caption and the shortcut is made. This function is called in the following way: 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  WideBuffer := UTF8ToUTF16(CompleteMenuItemStringCaption(AMenuItem, ACaption, #9));     That means, internally, #9 is used as separator and it works as we can see. However, I could not find out why then it is not possible to set this #9 character manually. I found no code where the #9 could get lost. It should work in the same way, no matter whether it is set in CompleteMenuItemStringCaption or ACaption already contains the #9 separator. ---------------------------------------------------------------------- jamie · 2021-07-10 18:01 if you look a that code for DrawItemText as one of the last things it does is draws the short cut text, you will see the flag DT_EXPANDTABS is not placed in there.  This means you can have as many #9's as you whish in the shortcut text and it's not going to space it. ---------------------------------------------------------------------- LazProgger · 2021-07-10 18:33 Ah, I see. Then we have the reason why it is not working. The question now is: When I am writing a workaround (something like if pos(#9, acaption) > 0 then ...) or change the code that the flag will be always set, will this have the change to be included in future versions of Lazarus? ---------------------------------------------------------------------- ASerge · 2021-07-10 19:29 From Win32WSMenus: 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 class procedure TWin32WSMenuItem.AttachMenu(const AMenuItem: TMenuItem);...  with MenuInfo do  begin             ...    fType := MFT_OWNERDRAW;In Lazarus the menus are always OwnerDraw. If you put OwnerDraw for the menu in Delphi, then they also ignore #9. When the Menu.OwnerDraw = True then ShortCut are used explicitly: 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 procedure DrawVistaPopupMenu(const AMenuItem: TMenuItem; const AHDC: HDC; const ARect: TRect; const ASelected, ANoAccel: boolean);...    if AMenuItem.ShortCut <> scNone then    begin      if IsRightToLeft then      begin        TextRect.Right := TextRect.Left + Metrics.ShortCustSize.cx;        TextFlags := TextFlags xor DT_RIGHT or DT_LEFT;      end      else      begin        TextRect.Left := TextRect.Right - Metrics.ShortCustSize.cx;        TextFlags := TextFlags xor DT_LEFT or DT_RIGHT;      end;      ThemeDrawText(AHDC, Details, MenuItemShortCut(AMenuItem), TextRect, TextFlags, 0);    end;The same for DrawMenuItemText procedure (without themes). ---------------------------------------------------------------------- jamie · 2021-07-10 19:59 Quote from: LazProgger on July 10, 2021, 08:33:46 pmAh, I see. Then we have the reason why it is not working. The question now is: When I am writing a workaround (something like if pos(#9, acaption) > 0 then ...) or change the code that the flag will be always set, will this have the change to be included in future versions of Lazarus?  I can't tell  you that.  But if you want to draw this yourself you can use the DT_EXPANDTABS using the DrawTextW, this is the part that is missing in the ShortCut part of this in the current code.  I can't say why it's missing? not sure but it's obvious it needs to be there since the Space function is including the #9 but what good is that if it's not going to be processed ? @ASerge  Yes, LCL draws the menus instead of Windows by using owner draw, this isn't the same thing however, since the LCL offers the  OnwerDraw option its then it should force the user to handle this but as it is now, its suppose to behave like Delphi in non owner draw mode.    Delphi does not draw its own menus but the LCL does but this is suppose to emulate the OS doing so. This appears to be a bug, some how. I don't understand this because it looks like it was that way at one time, but the flag DT_EXPANDTABS has been removed.! ---------------------------------------------------------------------- ASerge · 2021-07-11 20:53 Quote from: jamie on July 10, 2021, 09:59:19 pmThis appears to be a bug, some how. I don't understand this because it looks like it was that way at one time, but the flag DT_EXPANDTABS has been removed.! The DT_EXPANDTABS flag has nothing to do with this. Windows handles the tab character for the menu in a special way. But the emulation procedures do this only for ShortCuts (both in Lazarus and Delphi). Of course, the emulation is not complete, but at least it is compatible with Delphi. But the fact that the menu is always OwnerDraw, in my opinion, is a more serious bug. ---------------------------------------------------------------------- jamie · 2021-07-11 22:13 Regardless of how its implemented, the LCL has it as ownerdraw and its not the only windows type control the LCL is doing this too.  The fact remains that its doing it and does not match the native behavior of windows, which can be correct..  Also, if you look closer at the existing code, it looks like it may have had the DT_EXPANDTABS in there at some time because they are using #9 and later using the DrawText function. So its my opinion that it wasn't always like this. Have it your way, I am only a visitor. ---------------------------------------------------------------------- LazProgger · 2021-07-12 01:52 Quote from: jamie on July 12, 2021, 12:13:57 am  Also, if you look closer at the existing code, it looks like it may have had the DT_EXPANDTABS in there at some time because they are using #9 and later using the DrawText function. So its my opinion that it wasn't always like this. That is very likely. Otherwise it makes no sense why at one point the #9 is written between caption and shortcut and in the draw procedure the shortcut part is nevertheless drawn separately.