I have a document based application. Every document can have multiple windows. Every window is automatically added to the “Window” menu. However, they are added in a more or less random and useless order. I would like the window titles to be organized according to the NSDocument they belong to, similar to how XCode or Photoshop do it.
How can I best do that? How can I prevent the default behaviour of AppKit to add all windows to this special menu, and where should I put the code that adds the menu items in the “correct” manner? I don’t want to put handlers into every window controller!
It sure does seem like this is something Cocoa should do automatically. I don’t know whether it does, but the first thing to check is whether the window controllers are properly connected to their documents. Does your document subclass’s
windowControllersproperty contain all the right objects?If that’s no good, then from NSWindow’s reference it looks like the only way to prevent a window whose title has been set from being added to the
Windowsmenu is-[NSWindow setExcludedFromWindowsMenu:]. It looks like you’ll want to call that on all your windows, then set up an object (perhaps in theMainMenunib) that takes care of all the windows’ positions and grouping in theWindowsmenu (viaNSApplication‘s methods). You may need to put in special disabled items and the like to get the grouping to look right. I would hope that windows could still be manually added even if you’ve previously asked them to be excluded.