NSTrackingArea + cursorUpdate: problem

  • Hi,

    I am trying to use the new cursor handling mecanism of Leopard.

    My view has some regions (rects) inside of it where the cursor should
    be changed, otherwise should be a regular arrow.

    1. From the doc, it is unclear to me if NSTrackingCursorUpdate can be
    used or not in that case (rect != bounds). At least what I get is the
    cursorUpdate: method being invoked both when entering AND leaving the
    area. Which means the cursor never becomes an arrow again inside that
    view.
    How should this be handled ?

    2. Also the doc states that NSTrackingArea can overlap in different
    views. What about overlapping in the same view ?

    Here is the code
    ----

    trackingArea = [[NSTrackingArea alloc] initWithRect:[self
    thresholdRect:i]
              options: (NSTrackingCursorUpdate |
    NSTrackingActiveInActiveApp)
                                     owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];


    -(void)cursorUpdate:(NSEvent *)theEvent
    {
    NSLog(@"event : %@", theEvent);

    [[NSCursor crosshairCursor] set];
    }

    Thanks,

    Raphael
  • On Nov 28, 2007, at 2:41 PM, Raphael <Sebbe...> wrote:

    > Hi,
    >
    > I am trying to use the new cursor handling mecanism of Leopard.
    >
    > My view has some regions (rects) inside of it where the cursor
    > should be changed, otherwise should be a regular arrow.
    >
    > 1. From the doc, it is unclear to me if NSTrackingCursorUpdate can
    > be used or not in that case (rect != bounds). At least what I get
    > is the cursorUpdate: method being invoked both when entering AND
    > leaving the area. Which means the cursor never becomes an arrow
    > again inside that view.
    > How should this be handled ?

    Store the cursor in the user info when you set up the tracking areas,
    and then you can do this:

    - (void)cursorUpdate:(NSEvent *)event;
    {
        NSPoint hitPoint;
        NSTrackingArea *trackingArea;

        trackingArea = [event trackingArea];
        hitPoint = [self convertPoint:[event locationInWindow]
    fromView:nil];

        if ([self mouse:hitPoint inRect:[trackingArea rect]]) {
            [[[trackingArea userInfo] objectForKey:@"cursor"] set];
        } else {
            [[NSCursor arrowCursor] set];
        }
    }

    > 2. Also the doc states that NSTrackingArea can overlap in different
    > views. What about overlapping in the same view ?

    I don't know.  You could try it and see what happens.

    --
    Steve
  • Works great, thanks, avoids using an additional ivar.

    For 2., no extensive testing, but it doesn't seem to be supported.

    Raphael

    On 29 Nov 2007, at 04:52, Nygard Steve wrote:

    >
    > On Nov 28, 2007, at 2:41 PM, Raphael <Sebbe...> wrote:
    >
    >> Hi,
    >>
    >> I am trying to use the new cursor handling mecanism of Leopard.
    >>
    >> My view has some regions (rects) inside of it where the cursor
    >> should be changed, otherwise should be a regular arrow.
    >>
    >> 1. From the doc, it is unclear to me if NSTrackingCursorUpdate can
    >> be used or not in that case (rect != bounds). At least what I get
    >> is the cursorUpdate: method being invoked both when entering AND
    >> leaving the area. Which means the cursor never becomes an arrow
    >> again inside that view.
    >> How should this be handled ?
    >
    > Store the cursor in the user info when you set up the tracking
    > areas, and then you can do this:
    >
    > - (void)cursorUpdate:(NSEvent *)event;
    > {
    > NSPoint hitPoint;
    > NSTrackingArea *trackingArea;
    >
    > trackingArea = [event trackingArea];
    > hitPoint = [self convertPoint:[event locationInWindow]
    > fromView:nil];
    >
    > if ([self mouse:hitPoint inRect:[trackingArea rect]]) {
    > [[[trackingArea userInfo] objectForKey:@"cursor"] set];
    > } else {
    > [[NSCursor arrowCursor] set];
    > }
    > }
    >
    >> 2. Also the doc states that NSTrackingArea can overlap in different
    >> views. What about overlapping in the same view ?
    >
    > I don't know.  You could try it and see what happens.
    >
    > --
    > Steve
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today