title: Cocoa's NSView getting insane mouseDown events?
body: <p>Hullo all - I've got a NSView subclass that I move programmatically on mouseDown. Which works, but has an odd side effect:</p>
  
  <ol>
  <li>I click the subview. The subview moves away <strong>[GOOD]</strong></li>
  <li>I wait a while. I do not move my mouse. Since the subview has moved it's no longer under my cursor.</li>
  <li>I click my mouse again.
  
  <ul>
  <li>I expect the underlying window to get the mouseDown event (since the subview is no longer under my cursor), however my subview somehow gets this event <strong>[ODD]</strong></li>
  <li>The mouseDown event clearly shows that the click was outside the bounds of my subclass <strong>[ODD]</strong></li>
  <li>The mouseDown event also clearly shows that the click count has incremented, even though I've waited several seconds between mouse clicks <strong>[ODD]</strong></li>
  </ul></li>
  </ol>
  
  <p>... there's gotta be an explanation for what I'm seeing. Here's my code - simply create a new Cocoa Application project called "OddMouse", and copy the following into the OddMouseAppDelegate.h file:</p>
  
  <pre><code>#import &lt;Cocoa/Cocoa.h&gt;
  @interface OddMouseAppDelegate : NSObject &lt;NSApplicationDelegate&gt; {
    NSWindow *window;
  }
  @property (assign) IBOutlet NSWindow *window;
  @end
  
  @interface OddView : NSView {
  }
  @end
  </code></pre>
  
  <p>... and the following into the OddMouseAppDelegate.m file:</p>
  
  <pre><code>#import "OddMouseAppDelegate.h"
  @implementation OddMouseAppDelegate
  @synthesize window;
  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  </code></pre>
  
  <p>[[window contentView] addSubview:[[OddView alloc] init]];
      }
      @end</p>
  
  <pre><code>@implementation OddView
  - (id)init {
    self = [super initWithFrame:NSMakeRect(100, 100, 100, 100)];
    return self;
  }
  - (void)drawRect:(NSRect)dirtyRect {
    NSBezierPath *bz = [NSBezierPath bezierPathWithRoundedRect:[self bounds] 
                                                   xRadius:9 yRadius:9];
    [[NSColor blueColor] set];
  [bz fill];
  }
  - (void)mouseDown:(NSEvent *)event {
    NSPoint locationInMyself = [self convertPoint: [event locationInWindow] 
                                         fromView: nil];
    NSLog(@"MOUSE DOWN COORDS: x=%f y=%f, count=%i", 
            locationInMyself.x, locationInMyself.y, [event clickCount]);
    float newX = [self frame].origin.x+100;
    float newY = [self frame].origin.y+100;
    [self setFrame:NSMakeRect(newX, newY, 100, 100)];
  }
  @end
  </code></pre>
  
  <p>... then build, then run, then witness ! FWIW, here's what I see in the console:</p>
  
  <pre><code>10-01-15 11:38:24 PM OddMouse[4583] MOUSE DOWN COORDS: x=48.000000 y=56.000000, count=1
  10-01-15 11:38:37 PM OddMouse[4583] MOUSE DOWN COORDS: x=-52.000000 y=-44.000000, count=2
  10-01-15 11:38:44 PM OddMouse[4583] MOUSE DOWN COORDS: x=-152.000000 y=-144.000000, count=3
  10-01-15 11:38:52 PM OddMouse[4583] MOUSE DOWN COORDS: x=-252.000000 y=-244.000000, count=4
  10-01-15 11:39:03 PM OddMouse[4583] MOUSE DOWN COORDS: x=-352.000000 y=-344.000000, count=5
  </code></pre>