1

Specifically, I'd like to make an NSTokenField ignore mouse events because I'm using it in a NSTableCellView just to display data in a tokenized way without allowing any editing.

Setting the token field's enabled = NO works, except that it greys out the tokens and makes it hard to read the text.

Setting the token field's editable = NO is pretty close to what I want—it prevents editing while preserving the token field's look—except that when I mouse over the tokens, they light up. If I could just prevent that, I'd be in business.

I suspect I need to subclass something and override some NSResponder methods, but not quite sure what to do. I tried subclassing NSTokenField and overriding mouseEntered: and mouseMoved: to do nothing, but that didn't work either.

yuji
  • 16,695
  • 4
  • 63
  • 64
  • The field is probably using a [tracking area](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSTrackingArea_class/Reference/Reference.html) to get those mouse entry/exit events. Not sure exactly why overriding `mouseEntered:` screwed up drawing, but you could try `[field setTrackingArea:nil]` on a non-subclassed field and see what happens. – jscs Mar 26 '12 at 17:27
  • Thanks for pointing that out—turned out that when I created the class, `drawRect:` was being overridden so that it did nothing. I tried `for (NSTrackingArea *trackingArea in [tokenField trackingAreas]) { [tokenField removeTrackingArea:trackingArea]; }`, but no dice. Also, I tried overriding `mouseMoved:` in addition to `mouseEntered:`, but that didn't work either… – yuji Mar 26 '12 at 17:57
  • @yuji: I don't have my machine with me at the moment to test, so I'm out of ideas. You might want to just write your own replacement for this class. Look at `BWToolkit` for an example. – sudo rm -rf Mar 26 '12 at 18:06
  • Did you try setSelectable:NO ? – rdelmar Mar 26 '12 at 18:15
  • @rdelmar `setSelectable:NO` doesn't hurt, but doesn't do anything beyond what `setEditable:NO` does. – yuji Mar 26 '12 at 18:21
  • @IuliusCæsar turns out you had the right idea—wrote up an answer based on it. Thanks everyone for your help! – yuji Mar 26 '12 at 18:45

1 Answers1

2

After trying a lot of stuff, I finally got this to work based on Iulius Cæsar's suggestion.

The trick was to subclass NSTextField and override trackingAreas:

- (NSArray *)trackingAreas
{
    return [NSArray array];
}

Simply deleting the field's tracking areas when creating it wasn't quite enough, because the field was in a scroll view and sometimes the tracking areas would be re-created.

yuji
  • 16,695
  • 4
  • 63
  • 64