Mac mainmenu - help implementing a mainmenu - cocoa

I have created my mainmenu in one of my window xibs.
My application has multiple windows and the mainmenu should be the same for all of them.
But, for each window some of the items will be disabled and others enabled.
The main problem that I see is the selectors.
Since every window controller class (mm) implements some of the functions how can I use the interface builder to set the selectors across multiple controllers.
I guess I need some sort of a "MainMenu Manager" class that will know all of the window controllers that "wants" to get called from the main menu and also the manager will have the code for enabling/disabling items when switching between windows.
Does that make sense? am I missing something here?
If not - can you provide any help on implementing this manager in terms of application flow?
(It should probably init the main menu before I load my first application window)
Thanks,

Make the various targets and selectors properties of the window controllers, make the menubar in MainMenu.xib. Then bind the menu items' target and selector properties to "Application"mainWindow.windowController.property". You can bind other properties such as the title too.

Related

Cocoa menubar programmatically with NSMenu, how to get standard items "Services", "Hide App", "Hide Others", "Show All" etc

In decarbonizing I have now come to creating a menubar programmatically using NSMenu.
Carbon seems to be so nice to add standard items to the application menu: Services, Hide app, Hide Others, Show All, Quit app,
and they are even added using the user’s Preferred Language setting in System Preferences’s Language & Region,
so in German Hide Others is added as Andere ausblenden.
I only had to add the About and Preferences items.
However, it seems that in Cocoa I have to add these standard items myself,
but how can I find out what Hide Others is called in the user’s Preferred Language ?
And what is the Cocoa equivalent of CreateStandardWindowMenu ?
If you have Xcode create a new Cocoa application project, it will make a MainMenu.xib that you can examine. It looks like "Hide Others", "Quit" etc. are just menu items with particular actions assigned, but "Services" seems to have some special magic. Even if you don't want to present a whole menu bar from a nib, you could load it from a nib and then pick out certain menu items and move them to another menu, rename them or whatever. Though I'm not sure I understand what you're trying to accomplish. Even if you don't want to localize, you can use a nib.
To address the last part of your revised question, the NSApplication object has a windowsMenu property, i.e., you could create a menu and say [NSApp setWindowsMenu: myMenu]. But I don't know if that adds items like Bring All to Front or if you must create those yourself.

Cannot connect Objects to actions - no drag'n'drop option?

I have my MainMenu.xib with the Main Menu in it. I also have several NSObject subclasses as Menu Controllers, which are all represented in IB as objects, so that each Menu Item in the Main Menu corresponds to a dedicated Menu Controller
Whenever I try to drag'n'drop some menu item to its Menu Controller header and create an IBAction , Xcode does not give me that option. I suppose it's an Xcode bug, but I'm still confused...
When I created my first Menu Controller everything worked fine. However, now that I've added all Menu Controllers (the very same way as before), only the first one can be "linked" via IB.
What am I doing wrong? Any ideas on how to fix that?
Strange, Make sure the following
you have used the word "IBOutlet" for each to be linkable
clean the code
restart xcode
And just to make it clear,if you think its a bug in your XCode copy, try creating a sample project and see if it also do the strange behavior

Contextual Menu item method swizzling

Any idea how do I handle contextual menu click inside finder via method swizzling ? I am trying to find which method needs to be overridden from Finder.h for this?
I found this sample which shows how to add the items using method swizzling and code injection.
https://github.com/infinitelabs/FinderMenu
Adds submenu to the context menu of Mac OS X Finder. Submenu has two items, clicking on them the window with the selected items is displayed.
It uses https://github.com/rentzsch/mach_inject to update the Finder process. Seems like it should do what you want.
Use gdb and set breakpoints on objective-c messages. You can also use instrument tool for finding method names.

How does Xcode decide which XIB to show first

When I create a new cocoa project in Xcode, it creates a MainMenu.xib file with a Window. I would like to separate the window into a MainWindow.xib and have that show up as the main window - how do I do that?
If I create a 'document' based application - I see two xibs (MainMenu.xib and MyDocument.xib) and I notice that MyDocument.xib gets displayed right away - but I don't understand where this is identified. There doesn't seem to be explicit code and I'm not sure where to look in the plists.
If I create a new Document, how can I make it the primary window that shows up when I start the app?
in <YourProjectName>-Info.plist you'll find the key "Main nib file base name", with the value of the first xib to load usually "MainMenu".
Also you'll find "Document types" -> "Item 0" -> "Cocoa NSDocument Class" and there the value "MyDocument". In the class "MyDocument" there is a method - (NSString *)windowNibName. This return the name of the first xib to load.
Window management is your responsibility. Don't rely on the frameworks to display windows in any order, as that order is undefined.
Firstly, make sure that the Visible at Launch checkbox is not checked for your windows in Interface Builder. If it is, you won't be able to control window display effectively. It's on by default.
Next, use an NSWindowController subclass for each window nib that you want to load. Then, in your main application controller you can control when those NSWindowController objects are instantiated and when to call their -showWindow methods.
In your example case, you'd probably instantiate the NSWindowControllerfor your MainWindow.xib in the ‑applicationDidFinishLaunching: method in your application delegate.

Cocoa customize system context menu

I need to add custom element to context menu for all running application. It is posible?
Something like:
I select text and in context menu i have not only application items, but additional items of my application and this items must will be connected to my app. It must work in all applications.
Check out the Services Implementation Guide.

Resources