title: Understanding NSView/NSWindow mouse capture and dispatch
body: <p>I'm trying to understand how Cocoa's dispatch of mouse messages works while the mouse button is pressed.</p>
  
  <p>My background is in Window's programming so this the perspective I'm coming from...</p>
  
  <ol>
  <li>Window sends mouse down event</li>
  <li>The app can call SetCapture(hWnd) to request all future mouse events get delivered to that window, rather than the window under the mouse</li>
  <li>The app calls ReleaseCapture when done to restore normal mouse dispatch</li>
  <li>The app might receive a WM_CANCELMODE message which instructs it to release capture and cancel any tracking modes. (eg: Windows sends this if you Alt-Tab away in during the mouse drag operation)</li>
  </ol>
  
  <p>So learning my way around Cocoa, the way I understand it is...</p>
  
  <ol>
  <li>NSWindow/NSView receives NSLeftMouseDown event</li>
  <li>Cocoa automatically "captures" the mouse and sends NSLeftMouseDragged events while the mouse button is held to the window/view that was clicked.</li>
  <li>NSWindow/NSView receives NSLeftMouseUp event</li>
  </ol>
  
  <p>Here's my questions:</p>
  
  <ol>
  <li>Is there a way for a window to cancel the captured mouse and explicitly re-capture it to another window?</li>
  <li>Is there a way to find out which window (and/or view) currently has the mouse "captured"?</li>
  <li>Are there any scenarios in which a view might receive unbalanced mouseDown/Up events?</li>
  <li>Is there an event like WM_CANCELMODE that I should be watching for to cancel the current tracking operation?</li>
  </ol>
  
  <p>To explain what I'm trying to achieve, I have a slider control in my app:</p>
  
  <p><img src="https://dl.dropboxusercontent.com/u/54914447/Slider1.png" alt="Slider"></p>
  
  <p>that on mouse down shows up a larger version in a popup window:</p>
  
  <p><img src="https://dl.dropboxusercontent.com/u/54914447/SliderCap.png" alt="SliderCap"></p>
  
  <p>When it pops up the mouse cursor is automatically moved to the larger slider's handle and tracks the mouse until it's released after which the popup is removed and the cursor moved back to the smaller slider.  (<a href="https://youtu.be/3kh9ivNw_qQ" rel="nofollow noreferrer">Here's a video</a> showing it)</p>
  
  <p>I need to either redirect captured mouse events to the popup NSWindow, or in my mouse tracking loop known which NSWindow currently has the mouse captured.  I could work this out by tracking mouse events but thought there might be an API to get it (eg: like Window's GetCapture() API).</p>
  
  <p>btw: I actually have this working but what I've done feels a bit hacky and I'd like to understand OSX's approach better - and just make sure I'm not missing anything obvious/easier.</p>