title: Change background color on hover on a NSMenuItem with custom NSView
body: <p><strong>Here is the thing:</strong>  </p>
  
  <p>I have created a custom <code>NSMenuItem</code> with a custom <code>NSView</code> in it.</p>
  
  <p>Everything works fine except that I can't get the <code>NSMenuItem</code> to get highlighted (= change the background color on mouseover).</p>
  
  <p>I'm trying to do it inside the <code>drawRect</code> method, as shown in other answers posted here.</p>
  
  <p>What am I doing wrong?  </p>
  
  <hr>
  
  <p><strong>The <code>NSView</code> subclass:</strong>  </p>
  
  <pre><code>@interface customView : NSView  
  @end  
  @implementation customView
  
  - (id)initWithFrame:(NSRect)frame
  {
  
      NSRect theRect = NSMakeRect(0, 0, 200, 30);
      self = [super initWithFrame:theRect];
      if (self) {
      NSTrackingArea *   trackingArea = [[NSTrackingArea alloc] initWithRect:theRect
                                                      options: (NSTrackingMouseEnteredAndExited  | NSTrackingActiveInKeyWindow  |NSTrackingActiveAlways)
                                                        owner:self userInfo:nil];
          [self addTrackingArea:trackingArea];
      }
  
      return self;
  }
  
  #define menuItem ([self enclosingMenuItem])
  
  - (void) drawRect: (NSRect) rect {
  
  
      BOOL isHighlighted = [menuItem isHighlighted];
      if (isHighlighted) {
          //this nslog never happens
          NSLog(@"it's highlighted");
  }
  
  
  
  - (void)mouseUp:(NSEvent*) event {
      NSMenuItem* mitem = [self enclosingMenuItem];
      NSMenu* m = [mitem menu];
      [m cancelTracking];
  
  
      NSLog(@"you clicked the %ld item",[m indexOfItem: mitem]);
  }
  @end
  </code></pre>
  
  <p><strong>The <code>NSMenuItem</code> subclass:</strong><br>
  <em>(I add subviews on the custom view here so i can have access to the controls through the <code>NSMenuItem</code> instance)</em>  </p>
  
  <pre><code>@interface customItem : NSMenuItem{
  
  }
  
  -(void)setTheText:(NSString*)theString;
  
  @property NSTextField *theLabel;
  @end
  #import "customItem.h"
  #import "customView.h"
  @implementation customItem
  @synthesize theLabel;
  -(id)init{
  
      if (self){
  
          customView *cv = [[customView alloc] init];
          theLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 8, 130, 17)];
          [theLabel setEditable:NO];
          [theLabel setBordered:NO];
          NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(170, 7, 20, 20)];
          NSButton *myButton1 = [[NSButton alloc] initWithFrame:NSMakeRect(150, 7, 20, 20)];
  
          [myButton setBezelStyle:NSCircularBezelStyle]; 
          [myButton1 setBezelStyle:NSCircularBezelStyle]; 
  
          [myButton setTitle:@""];
          [myButton1 setTitle:@""];
          [cv addSubview:myButton];
          [cv addSubview:myButton1];
          [cv addSubview:theLabel];
          [self setView:cv];
          [theLabel setStringValue:@"A Value "];
  
      }
  
      return self;
  }
  
  -(void)setTheText:(NSString *)theString{
  
      [theLabel setStringValue:theString];
  }
  
  
  @end   
  </code></pre>
  
  <p><strong>And this is the App Delegate :</strong> </p>
  
  <pre><code>@interface AppDelegate : NSObject &lt;NSApplicationDelegate&gt;{
  
      NSStatusItem *statusItem;
      IBOutlet NSMenu *theMenu;
  }
  
  @property (assign) IBOutlet NSWindow *window;
  
  @end  
  #import "customItem.h"
  @implementation AppDelegate
  
  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  {
  
  }
  
  - (void)awakeFromNib{
  
      statusItem = [[NSStatusBar systemStatusBar]
                    statusItemWithLength:NSSquareStatusItemLength];
      NSBundle *bundle = [NSBundle mainBundle];
  
      NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon" ofType:@"png"]];
     NSImage  *highlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon_H" ofType:@"png"]];
  
      [statusItem setImage:statusImage];
      [statusItem setAlternateImage:highlightImage];
  
  
      [statusItem setMenu:theMenu];
  
      [theMenu removeAllItems];
       customItem *mi = [[customItem alloc] init];
  
     [theMenu addItem:mi];
     customItem *mi2 = [[customItem alloc] init];
     [theMenu addItem:mi2];  
  }
  @end
  </code></pre>
  
  <hr>
  
  <p><strong>This is what i get:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/weZ2I.jpg" alt="NSMenuItem Issue Screenschot"></p>