9

On Windows platform, with the VCL, when we want to add a separator in a menu, we add a TMenuItem with a Caption := '-';

With FireMonkey, we add a TMenuItem with a Text := '-';

It works as expected on Windows platform, the item with the Text='-' is displayed as a separator.

But, when I run the same application on OSX, I have the minus sign visible...

I haven't found any property on the TMenuItem to specify it is a separator...

I have tried with a TMainMenu and a TMenuBar (UseOSMenu := True|False;) and I still have this issue.

Any idea to create a real separator? (otherwise, I will check the OS and remove it if OSX...)

Whiler
  • 7,998
  • 4
  • 32
  • 56
  • My guess is you should report this in QualityCentral. Remember that firemonkey 1.0 is a first release, and so there are things like this that are probably not yet implement on FMX-Mac. – Warren P Sep 21 '11 at 20:33
  • This is what I think too... As this is a v1, this is why I haven't asked for the accelerator because I think this is not implemented at all... – Whiler Sep 21 '11 at 20:54
  • @Warren P: Here we go... [bug reported](http://qc.embarcadero.com/wc/qcmain.aspx?d=99182) – Whiler Sep 21 '11 at 21:25
  • 1
    Oh come on, surely they have implemented separators and accelerators. – David Heffernan Sep 21 '11 at 21:41
  • @David: If you find the accelerators, let me know ;o) – Whiler Sep 21 '11 at 21:44
  • 1
    If they aren't there I'll revise my verdict down from preview release to alpha release. – David Heffernan Sep 21 '11 at 22:01
  • @David Heffernan: As far as I know the concept of accelerators does not exist in OSX (Carbon), it only has keyboard (combo) shortcuts. Sounds to me like a "cross-platform first, optimize per platform later" approach. – Paul-Jan Sep 22 '11 at 04:46

3 Answers3

5

This is a bug in FireMonkey. I am sure they will solve it. But meanwhile you can use the below code. Call the procedure FixSeparatorItemsForMac in the OnActivate event of your main form.

Dont forget mac specific files in the uses list.

uses
...
  {$IFDEF MACOS}
  ,Macapi.ObjectiveC,MacApi.AppKit,MacApi.Foundation,FMX.Platform.Mac
  {$ENDIF}

{$IFDEF MACOS}

Procedure FixSeparatorItemsForMenuItem(MenuItem:NSMenuItem);
var i:Integer;
    subItem:NSMenuItem;
begin
  if (MenuItem.hasSubmenu = false) then exit;
  for i := 0 to MenuItem.submenu.itemArray.count -1 do
  begin
    subItem := MenuItem.submenu.itemAtIndex(i);
    if (subItem.title.isEqualToString(NSSTR('-'))= true) then
    begin
      MenuItem.submenu.removeItemAtIndex(i);
      MenuItem.submenu.insertItem(TNSMenuItem.Wrap(TNSMenuItem.OCClass.separatorItem),i);
    end else begin
      FixSeparatorItemsForMenuItem(subItem);
    end;
  end;
end;

Procedure FixSeparatorItemsForMac;
var NSApp:NSApplication;
    MainMenu:NSMenu;
    AppItem: NSMenuItem;
    i: Integer;
begin
  NSApp := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
  MainMenu := NSApp.mainMenu;
  if (MainMenu <> nil) then
  begin
    for i := 0 to MainMenu.itemArray.count -1 do
    begin
      AppItem := mainMenu.itemAtIndex(i);
      FixSeparatorItemsForMenuItem(AppItem);
    end;

  end;
end;
{$ENDIF}
mehmed.ali
  • 252
  • 1
  • 4
  • It works perfectly! Thanks! Do you know how to add items in the application menu under Mac... the one with the Quit... (I can create another question if needed...) – Whiler Oct 02 '11 at 20:40
  • Yes Whiler. You can create a question about menu items in Firemonkey. But notfiy me when you post it. – mehmed.ali Oct 03 '11 at 07:51
  • [Done ;o)](http://stackoverflow.com/questions/7632838/how-to-create-subitems-menus-under-the-application-name-on-osx) – Whiler Oct 03 '11 at 08:50
  • This was fixed in Delphi XE2 Update 2. – Marcus Adams Nov 05 '11 at 20:30
0

I never programmed for the Mac, and I don't eveb have a Mac but out of curiosity I found some Apple Documentation about it.

The Menu Separator item is a disabled blank menu item, maybe you can fake with that:

separatorItem

Returns a menu item that is used to separate logical groups of menu commands. + (NSMenuItem *)separatorItem Return Value

A menu item that is used to separate logical groups of menu commands.

Discussion

This menu item is disabled. The default separator item is blank space.

(From: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSMenuItem)

Daniel Luyo
  • 1,326
  • 13
  • 19
-1

I don't have the facilities to test this, but it's worth a try.

By default, FireMonkey creates it's own fully styled menus, but set the TMenuBar.UseOSMenu property to true and it uses OS calls to create the menus.

You can then combine this with the advice for creating Cocoa menus already discussed.

From http://docwiki.embarcadero.com/RADStudio/en/FireMonkey_Application_Design#Menus :

"Setting the TMenuBar.UseOSMenu property to True causes FireMonkey to create the menu tree with OS calls, resulting in a native menu. On Windows, this menu is at the top of the parent form, and displayed using the current Appearance theme. On Mac OS X, the menu is displayed in the global menu bar on top of the main screen whenever the application has focus."

Mike Sutton
  • 4,191
  • 4
  • 29
  • 42
  • Like I said, I already tested it with the different objects and I alse tried with the Cocoa spec (__I have tried with a TMainMenu and a TMenuBar UseOSMenu := True|False;) and I still have this issue.__) It just generates an empty item... not a separator... I confirm, it is displayed on the global menu (for TMainMenu **and** 'TMenuBar with UseOSMenu=true' – Whiler Sep 22 '11 at 23:01