How to reuse NSMenu in different windows? - cocoa

In Xcode I've created a new Main Menu xib from the Main Menu template. It only contains the main-menu, which I would like to use in my app.
How can I load this Main Menu from this xib-file and use it in my different windows?

You don't need to do anything.
An application's main menu is already global—it's part of the application, not of any individual window. The same menus will appear regardless of which window is active, by default.
You would have to put in some work if you wanted to go the other way and have different menus depending on which window is active.

Related

How to enable auto generation of Window MenuItems (e.g. Tile Window to Left/Right of Screen and Open file with checkmark) in macOS menubar

I am currently creating a macOS menubar for an app without using any interface builder (no XIB/NIB files), just pure code. However I was expecting some items to be auto-generated during the start-up of the app. Items like "Start Dictation", "Emoji & Symbols" under Edit menu were existing as well as the "Enter Full Screen" menu item under the View Menu. But when it comes to Window Menu nothing was automatically generated, only the menu items I've set in the code. Do I have to enable some flags or options when instantiating a Window NSMenu so it automatically generates those items? I am new to macOS development so I feel like I am kind of lost. Thanks in advance.
The Window and Help menus are a little special in that they have their own NSApplication properties, so you will need to set them to your menus so that the system will know what they are.
For example, if you just create a window menu and add it to the main, all you will get are the items that you have provided. If you also set it as the application’s windowsMenu, in addition you will get all the stuff for moving, tab support, etc.
Setting NSApp’s helpMenu is similar, where a Spotlight menu item is added to the menu.

Display NSPopover above dock icon

How can I display an NSPopover above my app's dock icon?
There are two ways to customise the dock menu, based on if your application is running or not. See the Apple documentation for customising it when its open here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW1
To summarise, you basically provide a menu in the main application's XIB or storyboard which contains the extra menu options that you want to add to the dock menu.
To customise your Dock icon's menu when the application is not running, see the guide here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/CreatingaDockTilePlug-in/CreatingaDockTilePlug-in.html
Basically, you need to create a plugin which you drop into your application's resource folder. This works very like the original method, but executes inside Finder's dock process.

Set NSMenu as NSWindow's main menu from the NIB?

I've got two menus in my app, one will be attached to NSStatusBar and another will be app's main menu.
Is there a way to set one of the NSMenu's as the main one right from the Nib? I'm looking at another project's nib and I see the menu actually defined in the nib, while in my nib it's not showing up correctly.
Right now my app has no menu at all.
This is what my nib looks like:
How I want it to look like:
In xibs, at least in Xcode 4, the main menu is no longer an outlet of the application but rather a nature that a top-level menu object either has or doesn't have. The main menu in a main menu nib created by Xcode has that nature; a menu you create yourself does not.
As of Xcode 4.2.1, there is no way to create a menu from scratch in an existing nib and appoint it as the main menu. You must create a nib with the main menu already in it using the appropriate file template.

How can I get to the menu bar in my app in interface builder

XCode works in mysterious ways (at least to me).
I simply want to create a Preference pane in my app. When I run my app, the stock menu bar comes up (Apple, MyApp, File, Edit...) and the "Preferences" menu item is grayed out. It makes sense since I haven't started playing with it.
How on earth do I add/remove/activate/inactivate menu items? I'm not talking about adding anything new, simply using what should be there.
Thanks in advance.
NSMenu has "Auto Enable Items" enabled by default. That means if the menu item does not have it's action message hooked up, it will appear grayed out. So in your case, you would simply set the Preferences menu item's "Sent Action" to whatever action shows your preferences window. This can be hooked to some sort of showPreferencesWindow: method of your AppDelegate, or directly to the showWindow: method of a window controller.
To dynamically enable/disable menu items the best way is to implement the NSUserInterfaceItemValidation protocol which is excellently documented here
Edit: Your app's menu bar items live in the MainMenu.xib file. The menu bar appears as a "Main Menu" object on the left hand side (if you're using Xcode 4) Simply click on the items to modify them, and you can Ctrl+drag connections to and from them like any UI object.

Does Apples UI define Drag&Drop in menus and/or context menus for menu items

I'm trying to update my GUI a little bit but don't want to run into porting problems later next year when a native Cocoa Port will be started.
On Windows/Linux it is common to drag items out of menus or being able to right click on a menu item and display a context menu for this menu item.
Is there anything like this on MacOSX.
Well, you can put a view into a menu item, and you can drag from a view, but I don't know whether those two will work together (i.e., whether you can drag from a view that's in a menu item).
More importantly, this isn't normal on the Mac, so you need another place to drag the item from regardless of whether you can support dragging from the menu or not.

Resources