How to make NSComboBox lose focus after selection? - macos

I have a combo box in my OSX app and I want it to lose focus right after I change the value selection.
Right now it just stays selected and highlighted after the selection.
Any kind of help is highly appreciated!

Just figured this out myself.
Using NSComboBoxDelegate, I use this method
- (void)comboBoxWillDismiss:(NSNotification *)notification
It's getting called right before the pop-up list is going to be dismissed. Inside that method I'm calling:
[myComboBox setRefusesFirstResponder:YES];
and it does the trick.

Related

Multiple NSTableViews selection

I have 2 tableviews in my App which I am using in a similar way to the following screenshot:
My problem is that I want to be able to select items in each list and have the selection remain blue rather than changing to grey when the control loses focus.
I've found that if I overload the AcceptsFirstResponder method of the tableview, I am able keep the grey for selection but this isn't the desired look of the App.
Does anyone have any advice on how I can achieve this? I was thinking I might be able change the grey to a blue whilst keeping the overloading AcceptsFirstResponder method but I'm not sure if this is possible.
Cheers,
Mike
Make sure your NSTableView is view-based, and check out NSTableRowView’s
- (void)drawSelectionInRect:(NSRect)dirtyRect;
BTW, I’m duty-bound to urge you not to do this: the colors have meanings in the UI. That blue color means “if you hit a key (like up arrow), this is the view that’ll receive it.” Your users will be confused.

Make the mouse cursor stay visible while typing in NSTextField

In OS X, the mouse cursor typically disappears when you are typing in a text field, in every app. Even when I am typing in this text field here on Stack Overflow, the cursor disappears.
This makes sense most of the time. But in my app I want it to stay visible, because the user has to do a lot of clicking between text fields, and it is really nasty when the mouse is always gone.
How can I make the cursor stay visible when typing in an NSTextField?
Ok, I solved it.
When you add a breakpoint at +[NSCursor setHiddenUntilMouseMoves:] you will notice, that it is called by [NSTextField keyDown:]
Apple says it is no good idea to override this method in order to prevent the mouse from disappearing. So I used NSTextFieldDelegate to get notified on -(void)controlTextDidChange:(NSNotification *)obj and -(void)controlTextDidEndEditing:(NSNotification *)obj. Then I called [NSCursor setHiddenUntilMouseMoves:NO]
It is not 100% clean but it works.
Big thanks to Richard and Kurt for their great help :)
It would be my guess that it is deep in apples code for the mouse to disappear when typing. Because even when you just start typing the mouse disappears, outside of a textbox even. Like when you click inside a browser window and type.

Hide/disable NSComboBox button

Is there a way to hide the pop-up button of an NSComboBox? I can't find anything in the documentation for NSComboBox or NSComboBoxCell. There is a setButtonBordered: method on NSComboBox, but this just changes to an alterate button style.
If I can't hide it, can I at least disable it?
If the combo box has no items, clicking the pop-up button doesn't do anything.
Maybe you can work around the limitation by emptying the list when you want to disable the button.
It makes clicking have no effect, but it doesn't hide the button or draw it as disabled.
I don't think this is possible. An NSComboBox without the button is effectively an NSTextField, so I guess it was deemed unnecessary. You could probably do this by subclassing NSComboBoxCell and override -drawWithFrame:inView: or -drawInteriorWithFrame:inView:.
Safest way would probably be to add your own buttonHidden property and use the ObjC runtime method class_getMethodImplementation to look up the IMP for the same method in NSTextField and just call that when the button is hidden. You'd effectively be calling super's super, so you'd get a regular text field look.
you can disable it by doing:
myComboBox.Enabled = false;

Cocoa: Accessibility (VoiceOver) for buttons with image only

I use NSButton with empty title and image on it, and it cannot be accessed with VoiceOver.
But when I'm setting title (VoiceOver seems to use title), NSButton tries to show it.
I think there should be an easy way to not display title, or to set button text, used by VoiceOver, however quick search didn't give any results yet.
P.S. I'm creating button programmatically.
you must assign the accessibilityLabel to the image object directly, It works this way.
Solved issue - added subclass for NSButtonCell, which does nothing in drawTitle: method and returns NSZeroRect. Seems to work ok.

Getting rid of the focus rect on an NSCell. Only shows up when right clicking

I have an NSCell subclass that I do all kinds of custom drawing in. The only time things seem to be entirely out of my control is when right clicking a cell to show the associated context menu. While showing it's context menu, the table (or cell - not sure which exactly is doing this) draws a focus rect. I would like to get rid of this, or at least find a way to draw my own version of it that better fits within my interface.
I have tried about 15 different methods that seem like they could give me control over this focus rect drawing itself (various first responder methods, various drawing methods, various highlight color methods, etc.), but I have yet to find something that changes anything about it.
Screenshot of the problem: http://twitpic.com/3zx2t
I am almost annoyed enough to class-dump AppKit and try to find whatever private method it's using to draw this annoying focus rect. Nothing else I do has any effect on it. Any help here would really save both my sanity and lots of future hair pulling.
Thanks so much!
Without completely overriding your table view's -drawRect: this is the only other way I know to get rid of the context menu highlight rect.
- (void)_drawContextMenuHighlightForIndexes:(NSIndexSet *)rowIndexes clipRect:(NSRect)rect {
return;
}
This is the method, used in Leopard at least, to draw the highlight around cells that will be activated upon by a context menu. It is unfortunately called directly by NSTableView's -drawRect: method and as far as I know there is no other way to signal your disinterest in having those highlights drawn.
Of course this is private API, it may stop working in Snow Leopard, or some other release. But at worst that means that somewhere along the way the context menu highlight may start up again in your programs on newer releases or some other functionality using this method may not work later. You have been warned.
On 10.5 and later, NSTableView allows you to bring up a contextual menu on any item in the table (that is, without having the side effect of changing the selection.)
It draws that 'focus rect' to indicate which item(s) are being acted on by the contextual menu.

Resources