Lazarus

Programming => Widgetset => Cocoa => Topic started by: MISV on July 15, 2019, 02:11:50 pm

Title: TMenuItem created runtime with caption "-"
Post by: MISV on July 15, 2019, 02:11:50 pm
I have a piece of code where I copy menu items from one list to another. How that works is that the new destination menu item is created runtime and then assigned properties including caption.

I have noticed that for menu items with "-" they do not appear to be drawn in normal menu separator mode but instead simply "-"

Title: Re: TMenuItem created runtime with caption "-"
Post by: skalogryz on July 15, 2019, 03:17:27 pm
sample project?
Title: Re: TMenuItem created runtime with caption "-"
Post by: SymbolicFrank on July 15, 2019, 05:52:59 pm
There is a separate function in the Windows API to create a separator, but the function to add a menu item translates a '-' into that. That translation isn't happening, probably because you're assigning pointers instead of creating menu items.
Title: Re: TMenuItem created runtime with caption "-"
Post by: MISV on July 15, 2019, 10:33:33 pm
This seems to solve it

Code: Pascal  [Select][+][-]
  1.  
  2.   ADestItem.Caption := ASourceItem.Caption;
  3.   if ADestItem.Caption = '-' then
  4.     ADestItem.RecreateHandle
  5.   ;
  6.  

But I will provide more code later
Title: Re: TMenuItem created runtime with caption "-"
Post by: lucamar on July 16, 2019, 12:13:11 am
I use the following code in a project of mine. It builds a menu from a list of strings (containing the captions, read from an INI file) either under some other menu item or as a "root" menu item in the aplications main menu; i.e. it does something like:
ParentItem                -or-    TopItem
      \- TopItem | Item1 |              | Item1 |
                 | Item2 |              | Item2 |
                 | etc.. |              | etc.. |

In the original the items' Tag serves to differentitate it from the others and to specify in which group it is; it's later divided in the DoMarkup() handler into two indexes to access an array[x][y] of string. I've deleted all that since it bears no relation to your post.

The main point, wrt this thread, is that if the string to use as caption is "-" you should use AddSeparator() (or NewLine()).
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.AddMenu(const ParentItem: TMenuItem;
  2.   const TopName: String; const NewItems: TStrings);
  3. var
  4.   TopItem, AnItem: TMenuItem;
  5.   AName: String;
  6. begin
  7.   {Root the hierarchy}
  8.   TopItem := TMenuItem.Create(Self);
  9.   TopItem.Caption := TopName;
  10.   if Assigned(ParentItem) then
  11.     ParentItem.Add(TopItem)
  12.   else
  13.     MainMenu.Items.Add(TopItem);
  14.   { Build the menu}
  15.   for AName in NewItems do begin
  16.     if AName = '-' then
  17.       TopItem.AddSeparator
  18.     else begin
  19.       AnItem := TMenuItem.Create(Self);
  20.       AnItem.Caption := AName;
  21.       AnItem.OnClick := @DoMarkup;
  22.       TopItem.Add(AnItem);
  23.     end;
  24.   end;
  25. end;
Note that where I create new items and set their properties, etc. you could just as easily assign from your "source" menu, only where I test "if AName = '-' then..." you would have "if Source.Caption = '-'", etc.
Title: Re: TMenuItem created runtime with caption "-"
Post by: skalogryz on July 16, 2019, 02:28:10 am
But I will provide more code later
I think, it's not needed.
try r61595
Title: Re: TMenuItem created runtime with caption "-"
Post by: MISV on July 17, 2019, 11:56:37 am
Thank you - I will try when I get my release done (won't risk updating/breaking my app just before release) and report back!
TinyPortal © 2005-2018