Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using Cocoa cursors in a Carbon world



Thanks to my co-worker, John Kerr, we got the NSCursor-From-Carbon-Cursor
code to create a color cursor that looks correct when drawing.

However, I still see the flicker.

I am posting the resulting code for the benefits of other Carbon->Cocoa
conversion efforts. I hope others find it useful (I apologize for my company
mandated email client's text wrapping):

//--------------------------------------------------------------------------

static NSImageRep* CreateImageRepWith16BitsOfData(const Bits16 data, const
Bits16 mask)
{
    NSBitmapImageRep *result = nil;
    
    if (data && mask) {
        unsigned char *bmData = nil;

        result = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                         pixelsWide:16
                                                         pixelsHigh:16
                                                      bitsPerSample:8
                                                    samplesPerPixel:4
                                                           hasAlpha:YES
                                                           isPlanar:NO
                   
colorSpaceName:NSDeviceRGBColorSpace
                                                        bytesPerRow:64
                                                       bitsPerPixel:32];
        
        bmData = [result bitmapData];
        
        unsigned char monoComponent;
        uint16_t row, rowMask;
        int x, y;
        
        for (y = 0; y < 16; y++) {
            row        = CFSwapInt16BigToHost(data[y]);
            rowMask    = CFSwapInt16BigToHost(mask[y]);
            
            for (x = 0; x < 16; x++) {
                monoComponent = (row & (1 << (15 - x))) ? 0x00 : 0xFF;
                if (monoComponent == 0x00) {
                    // If the monoComponent is 0, then it must be drawn, the
alpha must be 1.
                    bmData[3] = 0xFF;
                }
                else {
                    bmData[3] = (rowMask & (1 << (15 - x))) ? 0xFF : 0x00;
// alpha
                    if (bmData[3] == 0x00) { // if the mask is 0, then the
monoComponent must be drawn (or == 0).
                        monoComponent = 0;
                    }
                }
                bmData[0] = bmData[1] = bmData[2] = monoComponent;    // RGB
                
                bmData += 4;
            }
        }
        
        return [result autorelease];
    }
    
    return nil;
}

//--------------------------------------------------------------------------

static NSImage* CreateNSImageFromCarbonCursor( CursHandle legacyCursorH )
{
    NSImage* cursorImage = [[NSImage alloc] initWithSize: NSMakeSize(16.0f,
16.0f) ];
    
    [cursorImage addRepresentation:
CreateImageRepWith16BitsOfData((*legacyCursorH)->data,
(*legacyCursorH)->mask)];
    
    return cursorImage;
}

//--------------------------------------------------------------------------

NSCursor*    CreateNSCursorFromCarbonCursor( short legacyCursorResID )
{
    CursHandle legacyCursorH = MacGetCursor(legacyCursorResID);
    
    NSCursor* cursor = nil;
    
    if (legacyCursorH) {
        NSAutoreleasePool* localPool = nil;
        
        localPool = [[NSAutoreleasePool alloc] init];

        NSImage* nsImage = CreateNSImageFromCarbonCursor( legacyCursorH );
        
        if (nsImage) {
            float hotSpotX = (*legacyCursorH)->hotSpot.h;
            float hotSpotY = (*legacyCursorH)->hotSpot.v;

            cursor = [[NSCursor alloc] initWithImage:nsImage
hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
            [nsImage release];
        }

        [localPool release];
    }
    return cursor;
}
        
        


On 8/28/07 9:39 PM, "John Stiles" <email@hidden> wrote:

> Could the real issue be switching between black-and-white cursors,
> and color cursors?
> It is possible that on some video hardware, one is supported on the
> card, and the other is not, so the system would need to do handstands
> to switch between the two.
> You could try modifying the attached code to create a color image
> instead of a black-and-white one.
> 
> 
> On Aug 28, 2007, at 6:36 PM, Lyndsey Ferguson wrote:
> 
>> 
>> Hello Everyone,
>> 
>> I've been meaning to write this email-list a nice letter stating
>> how much I
>> appreciate everyone's help and being a part of the community (even
>> if I wait
>> to contribute until I see no one knows an answer) - however, now is
>> not that
>> moment :)
>> 
>> I've been struggling with a problem that I would appreciate your
>> suggestions
>> on. I believe I have exhausted my options, but I thought I would
>> check with
>> my peers to see if they have any other ideas.
>> 
>> Our application is switching over to a system where we are using
>> NSCursor
>> for the majority (not all - time constraints) of our cursors. It
>> works quite
>> nicely. I didn't write the code, but it was written by a Software
>> Engineer
>> who did a good job.
>> 
>> Unfortunately, he's gone and we are experiencing some issues. It
>> seems that
>> on the PowerPC (Tiger and Leopard), when the application switches
>> the cursor
>> from a Carbon cursor (via MacGetCursor and MacSetCursor) to a Cocoa
>> cursor
>> (via NSImage to NSCursor and NSCursor's -set function) one can see
>> a flicker
>> of the Carbon cursor in the vicinity of the mouse position (not
>> where the
>> hotspot is).
>> 
>> I thought that perhaps the issue was switching between the two
>> types. So as
>> a test, I changed the code to not switch to a Carbon cursor ever,
>> but switch
>> to another Cocoa based cursor. Flicker was gone. Yay, I thought.
>> 
>> So my next step was to find a way to create a Cocoa based cursor
>> from the
>> Carbon CursHandle. I found some code on the internet which appears to
>> fulfill that intention (I've attached it for your info - it doesn't
>> seem to
>> completely work, but it displays a similar image to what is
>> expected). So
>> now, I should be switching from Cocoa cursor to Cocoa cursor.
>> 
>> Yet I still see a flicker.
>> 
>> So I thought that perhaps the call MacGetCursor, which was made in
>> the code
>> to generate the Carbon based Cocoa cursor was causing a problem. So I
>> changed the code so that it caches the Cocoa cursors and only calls
>> MacGetCursor for the first time the Carbon-based Cocoa cursor is
>> created.
>> Flicker still happens.
>> 
>> I've tried tracking down and logging any other call to MacSetCursor
>> so that
>> I could see if there was any legacy cursor setting while switching
>> between
>> Carbon/Carbon-based-Cocoa cursor and new Cocoa cursor. No calls to
>> MacSetCursor are made.
>> 
>> So, now I'm out of ideas. Anyone familiar with pixel copying code
>> able to
>> comment on the code I've attached with regards to correctness?
>> Anyone else
>> had to deal with this problem? Ideas? I'm ready to try any suggestion.
>> 
>> Thank you in advance,
>> Lyndsey Ferguson
>> -- 
>> Mr. Lyndsey D. Ferguson
>> Software Engineer
>> Nemetschek N.A., Inc.
>> http://www.nemetschek.net
>> 
>> 
>> <CreateCocoaCursorFromCarbon.cpp>
>>  _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Carbon-dev mailing list      (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/carbon-dev/jstiles%
>> 40blizzard.com
>> 
>> This email sent to email@hidden
> 

-- 
Mr. Lyndsey D. Ferguson
Software Engineer
Nemetschek N.A., Inc.
http://www.nemetschek.net


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >Re: Using Cocoa cursors in a Carbon world (From: John Stiles <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.