0

I'm trying to add a menu item to show the main window. It's enabled when the window has not been minimized or closed (yellow - and red x buttons), but when it has, the menu item is grayed out. That is of course useless and silly. :)

Another menu item, File/Page Setup... is NOT grayed out in the same way (on minimize/close). I have found no differences in IB between this menu item and the one I created, except the title. Both are connected to First Responder selectors (runPageLayout: for page setup, and showWindow: for mine), both have keyboard shortcuts, and both are enabled.

Likewise, the parent menu items are identical, except for the title.

How do I make sure this Show Main Window menu item is available when the user has pressed the red x or the yellow - button?

Edit: I made the menu items not gray out by unchecking Auto Enables Items for the parent menu item.

That made this method in the standard NSDocument class for the application be called:

- (IBAction)clickMinimizeButton:(id)sender {
    NSWindow *ww=[[NSApplication sharedApplication] mainWindow];
    NSLog(@"%@",ww);
}

And ww is a valid object.

The problem is, I can't restore the window from minimized/closed, because the method isn't called when it is.

How do I make this method be called when the window is minimized/closed? I want to simply do the opposite of the minimize or close click.

There's only one window in the app, and clicking the dock icon again does show the window, but Apple requires a menu item to do the exact same thing.

It would seem I have to connect the menu item to a custom, created by me, application method (certainly no application method restores all windows, for example), but I don't know where to declare it, since I'm not as experienced with Cocoa as I am with Cocoa Touch.

Edit 2: Going from the above, I created a method in the application delegate and connected it to the menu item. This gets the method called even when minimized, but I don't know the correct code to restore the main window. This desperate try of course only activates the app that is already active, not the window that is minimized.

NSApplication *theapp=[NSApplication sharedApplication];
[theapp activateIgnoringOtherApps:YES];

So this is all that is left now. How to emulate the standard "Bring All to Front" menu item in a default application? I will have a look at some standard windowed Mac app from Apple.

Edit 3: I created a standard Cocoa Application, and it too grays out all menu items in the Window menu when the window is minimized. But the default NSDocument is automatically added below these, and is not grayed out. I will be happy with such a solution.

For some reason, in my Window menu, my NSDocument does not automatically appear. The Info.plists are identical (apart from pointing to differently named NSDocument classes, of course).

How do I

a) programmatically add the document to that menu,

b) get the Window menu to automatically add it as it does in the standard application, or

c) replace my "damaged" Window menu (if that is the case) with a Window menu from my standard app that behaves as expected?

Henrik Erlandsson
  • 3,797
  • 5
  • 43
  • 63

2 Answers2

0

Try renaming your method to something else. showWindow: is already declared in NSWindowController, which may create a clash. Also make sure the object that's suppose to respond to the menu item isn't released.

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
  • Thanks for answering! It isn't released. Connecting it to another method doesn't work, because there's one more obstacle: the window must not be minimized/closed in order for it to call the method. – Henrik Erlandsson Oct 18 '12 at 09:06
  • Is your method connected via the First Responder Object in Main Menu? If it is, then it should work. – TheAmateurProgrammer Oct 18 '12 at 09:09
  • That was a quick response :) Well, as I explained above it's called when the main window is normal, but not when it's minimized or closed. I had an NSApplicationDelegate object in mainmenu.xib, and declaring it there gets it called even when minimized. So now all I need to know is the correct code to restore the main window, see my first try (that failed) in Edit 2 above. – Henrik Erlandsson Oct 18 '12 at 09:30
  • I don't know what you're doing, but I was able to create a very, very quick demo and it worked successfully. http://cl.ly/0j20272q3H1z – TheAmateurProgrammer Oct 18 '12 at 09:37
  • Yes, see the last realization above. What exact menu item did you click to restore the window? – Henrik Erlandsson Oct 18 '12 at 09:46
  • That's fine, and my own method in my app delegate is also called. But what code does it execute to restore the main window? – Henrik Erlandsson Oct 18 '12 at 11:06
0

Have you implemented your showWindow?

- (void)showWindow {
    if (!self.window) {
        [NSBundle loadNibNamed:@"MainWin" owner:self];
    }

    [self.window makeKeyAndOrderFront:self];
}

p.s. your window must not be autoreleased on window close (Behavior -> Release When Closed)

Shebuka
  • 3,148
  • 1
  • 26
  • 43
  • also maybe this my post can help you in some way: http://stackoverflow.com/questions/12843616/deminiaturize-nswindow-without-making-it-key – Shebuka Oct 18 '12 at 10:07
  • Thank you for trying to help me. The root cause seems to be a connection or similar that makes my NSDocument document not appear in the Window menu, as it does for a standard NSDocument based app I just created. It seems correct that this should be overridden in the NSDocument class, but it says self.window, no getter method exists for class of type MyDocument *. – Henrik Erlandsson Oct 18 '12 at 11:43