I'm maintaining some legacy code and when it was brought forward to compile with the 10.8 SDK rather than the 10.7 SDK, some key equivalent shortcuts for certain menu items stopped working. My hypothesis is that this is because those menu items are in a disabled state. The NSMenuItems that are no longer working are ones that were part of a submenu where autoenablesItems had been set to NO (I confirmed this in the xib via the attributes inspector for that submenu as well as programmatically by querying [NSMenu autoenablesItems]. It basically appears that the call to [NSMenuItem setEnabled:] is having no effect because if I query [NSMenuItem isEnabled] immediately after calling setEnabled:YES, the state hasn't changed and it still returns NO for isEnabled. Here's a snippet of code with the output it generates:
printf("DEBUG: Current state of menu item is ");
[nsMenuItem isEnabled] ? printf("enabled\n") : printf("DISABLED!\n");
printf("DEBUG: Current state of menu autoenablesItems is ");
[nsMenu autoenablesItems] ? printf("YES\n") : printf("NO\n");
[nsMenuItem setEnabled:YES];
printf("DEBUG: Current state of menu after setting it is ");
[nsMenuItem isEnabled] ? printf("enabled\n") : printf("DISABLED!\n");
Output:
DEBUG: Current state of menu item is DISABLED!
DEBUG: Current state of menu autoenablesItems is NO
DEBUG: Current state of menu after setting it is DISABLED!
I also tried subclassing NSMenuItem and overriding setEnabled to see if there was another call being made to setEnabled that was overwriting my call, but there are no other calls that go through setEnabled.
If I click on the submenu's parent menu then it seems to correct the state and these NSMenuItems get enabled, but it isn't going through my setEnabled code to change this state. I tried adding an observer to the subclass on enabled to try and catch where the state was getting set to enabled when clicking on the parent menu, but that didn't provide any insight either as the observer was only triggered for my calls to setEnabled which aren't actually changing the state.
From what I can tell from reading the Apple documentation, NSMenuItem's setEnabled should work as long as autoenablesItems is set to NO on the parent menu, but that doesn't seem to be working in this case, and I can't figure out why.
In the same code if I use the 10.7 SDK to compile (caveat: this is actually an older version of the code base, so there are other differences as well, but this particular code is the same), I do see that calling setEnabled changes the state of the NSMenuItem. When it enters this code in the 10.7 build the NSMenuItem is already in an enabled state, but I did try changing the call to setEnabled:NO to confirm whether this did in fact toggle the isEnabled state, and it did, unlike in the 10.8 build.
Any ideas why this isn't working in 10.8? I also tried with 10.9 and it didn't work there either. I've been unable to try 10.10 or 10.11 yet since there is some other code that needs updating in order to get it to compile with the newer SDKs (again, this is rather old legacy code).