Skip navigation.
 
mlRe: How to correctly control cursor???
FROM : Jiri Volejnik
DATE : Sat Nov 06 21:57:20 2004

> I don't see any way to get correct cursor behavior except to manage
> setting
> the cursor within my window controller. I have to keep some state info
> about
> which control is currently entered, so that on mouseEntered I know
> whether
> mouseExited has already been called and can account for the exit if
> not, and
> on mouseExited I know whether the exit was already taken into account
> by
> handling an earlier enter event. Is there a better way?


I'm successfully using the following simple techniqe for managing
graphical tools/selection cursors in my NSView subclass. I have no
problems with this solution.

I have the only one cursor rectangle for the view, and in every moment
it represents currently visible area of the view:

-(void)resetCursorRects
{
   [self addCursorRect:[self visibleRect] cursor: mCursor]];
}

Whenever I need to change the cursor, I enforce calling this method by
calling invalidateCursorRectsForView:.
In fact, there are only two places in the code where I have to do it -
in mouseMoved: and in flagsChanged:

-(void)mouseMoved:(NSEvent*)e
{
   // ...
   [self updateCursor:e];
   // ...
}

-(void)flagsChanged:(NSEvent*)e
{
   // ...
   [self updateCursor:e];
   // ...
}

In this method I choose an appropriate cursor - I examine mouse
coordinates and key modifiers for this purpose. mCursor is a member
variable of the view:

-(void)updateCursor:(NSEvent*)e
{
   mCursor  = ...
   [[self window] invalidateCursorRectsForView:self];
}

Unfortunately, the event delivered to flagsChanged: does not contain
mouse coordinates. As I need them to find out proper cursor, I cannot
simply pass the original event but I have to make a new one combining
data from the original event and from NSWindow's
mouseLocationOutsideOfEventStream: method. So my flagsChanged: looks
actually like this:

-(void)flagsChanged:(NSEvent*)e
{
   NSEvent* f = [NSEvent keyEventWithType:[e type]
       location:[[self window] mouseLocationOutsideOfEventStream]
       modifierFlags:[e modifierFlags] timestamp:[e timestamp]
       windowNumber:[e windowNumber] context:[e context]
       characters:@"" charactersIgnoringModifiers:@""
       isARepeat:NO keyCode:[e keyCode]];
   [self updateCursor:f];
}

Last, but not least:
mouseMoved has not been called for me even when I have enabled it
somewhere as recommended. So I had to use a little workaround - to
subclass my window and to override senEvent. If you know better
solution, tell me, please:

-(void)sendEvent:(NSEvent*) event
{
   [super sendEvent:event];

   if (NSMouseMoved == [event type]) {
       NSView* view = [[self contentView]
           hitTest:[event locationInWindow]];
       [view mouseMoved:event];
   }
}

I hope it helps...

-- Jirka

Related mailsAuthorDate
mlHow to correctly control cursor??? Scott Ribe Oct 9, 20:38
mlRe: How to correctly control cursor??? Sean McBride Nov 6, 04:55
mlRe: How to correctly control cursor??? Jiri Volejnik Nov 6, 21:57