custom cursor and tracking rect fixes

This commit is contained in:
Christopher Lloyd 2011-01-20 16:53:06 -05:00
parent a0ceb800c4
commit 4b0a4d7d38
9 changed files with 239 additions and 21 deletions

View File

@ -16,13 +16,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
NSPoint _hotSpot;
BOOL _isSetOnMouseEntered;
BOOL _isSetOnMouseExited;
id _cursor;
id _platformCursor;
}
+(NSCursor *)currentCursor;
+(NSCursor *)currentSystemCursor;
+(NSCursor *)arrowCursor;
+(NSCursor *)closedHandCursor;
+(NSCursor *)contextualMenuCursor;
+(NSCursor *)crosshairCursor;
+(NSCursor *)disappearingItemCursor;
+(NSCursor *)IBeamCursor;
@ -35,6 +37,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
+(NSCursor *)resizeUpCursor;
+(NSCursor *)resizeUpDownCursor;
+(NSCursor *)dragCopyCursor;
+(NSCursor *)dragLinkCursor;
+(NSCursor *)operationNotAllowedCursor;
+(void)hide;
+(void)unhide;

View File

@ -11,6 +11,147 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#import <AppKit/NSImage.h>
#import <Foundation/NSNull.h>
#import <AppKit/NSRaise.h>
#import <AppKit/NSGraphicsContext.h>
#ifdef WINDOWS
#import <windows.h>
#import <AppKit/Win32Cursor.h>
#import <Foundation/NSPlatform_win32.h>
#endif
id NSPlatformCreateCursorImpWithName(NSString *name) {
return [[[NSDisplay currentDisplay] cursorWithName:name] retain];
}
id NSPlatformCreateCursorImpWithImage(NSImage *image,NSPoint hotSpot) {
#ifndef WINDOWS
return nil;
#else
/// move to the platform files
size_t width=[image size].width;
size_t height=[image size].height;
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef context=CGBitmapContextCreate(NULL,width,height,8,0,colorSpace,kCGImageAlphaPremultipliedFirst|kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colorSpace);
NSAutoreleasePool *pool=[NSAutoreleasePool new];
NSGraphicsContext *graphicsContext=[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:graphicsContext];
[image drawInRect:NSMakeRect(0,0,width,height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
[pool release];
uint8_t *rowBytes=CGBitmapContextGetData(context);
size_t bytesPerRow=CGBitmapContextGetBytesPerRow(context);
HDC displayDC=GetDC(NULL);
HBITMAP colorBitmap;
HBITMAP maskBitmap;
if(NSPlatformGreaterThanOrEqualToWindows2000()){
// Cursor with alpha channel, no mask. Win2k and above
BITMAPV5HEADER bi;
void *lpBits;
uint8_t *dibRowBytes;
ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
bi.bV5Size=sizeof(BITMAPV5HEADER);
bi.bV5Width=width;
bi.bV5Height=-height;
bi.bV5Planes=1;
bi.bV5BitCount=32;
bi.bV5Compression=BI_BITFIELDS;
bi.bV5RedMask=0x00FF0000;
bi.bV5GreenMask=0x0000FF00;
bi.bV5BlueMask=0x000000FF;
bi.bV5AlphaMask=0xFF000000;
colorBitmap=CreateDIBSection(displayDC,(BITMAPINFO *)&bi,DIB_RGB_COLORS,&lpBits,NULL,0);
dibRowBytes=lpBits;
maskBitmap=CreateBitmap(width,height,1,1,NULL);
int row,column;
for(row=0;row<height;row++,rowBytes+=bytesPerRow,dibRowBytes+=width*4){
for(column=0;column<width;column++){
dibRowBytes[column*4]=rowBytes[column*4];
dibRowBytes[column*4+1]=rowBytes[column*4+1];
dibRowBytes[column*4+2]=rowBytes[column*4+2];
dibRowBytes[column*4+3]=rowBytes[column*4+3];
}
}
}
else {
// This works for versions lower than 2k, not really needed, but here.
HDC colorDC=CreateCompatibleDC(displayDC);
HDC maskDC=CreateCompatibleDC(displayDC);
colorBitmap=CreateCompatibleBitmap(displayDC,width,height);
maskBitmap=CreateCompatibleBitmap(displayDC,width,height);
HBITMAP oldColorBitmap=SelectObject(colorDC,colorBitmap);
HBITMAP oldMaskBitmap=SelectObject(maskDC,maskBitmap);
int row,column;
for(row=0;row<height;row++,rowBytes+=bytesPerRow){
for(column=0;column<width;column++){
uint8_t b=rowBytes[column*4];
uint8_t g=rowBytes[column*4+1];
uint8_t r=rowBytes[column*4+2];
uint8_t a=rowBytes[column*4+3];
if(a<255){
SetPixel(colorDC,column,row,RGB(r,g,b));
SetPixel(maskDC,column,row,RGB(255,255,255));
}
else {
SetPixel(colorDC,column,row,RGB(r,g,b));
SetPixel(maskDC,column,row,RGB(0,0,0));
}
}
}
SelectObject(colorDC,oldColorBitmap);
SelectObject(maskDC,oldMaskBitmap);
DeleteDC(colorDC);
DeleteDC(maskDC);
}
CGContextRelease(context);
ICONINFO iconInfo;
iconInfo.fIcon=FALSE;
iconInfo.xHotspot=hotSpot.x;
iconInfo.yHotspot=hotSpot.y;
iconInfo.hbmMask=maskBitmap;
iconInfo.hbmColor=colorBitmap;
HCURSOR hCursor=CreateIconIndirect(&iconInfo);
DeleteObject(colorBitmap);
DeleteObject(maskBitmap);
return [[Win32Cursor alloc] initWithHCURSOR:hCursor];
#endif
}
void NSPlatformReleaseCursorImp(id object){
[object release];
}
void NSPlatformSetCursorImp(id object) {
[[NSDisplay currentDisplay] setCursor:object];
}
@implementation NSCursor
@ -26,6 +167,10 @@ static NSMutableArray *_cursorStack=nil;
return [_cursorStack lastObject];
}
+(NSCursor *)currentSystemCursor {
return [_cursorStack lastObject];
}
-initWithCoder:(NSCoder *)coder {
[self dealloc];
return [NSNull null];
@ -36,7 +181,7 @@ static NSMutableArray *_cursorStack=nil;
}
-initWithName:(NSString *)name {
_cursor=[[[NSDisplay currentDisplay] cursorWithName:name] retain];
_platformCursor=NSPlatformCreateCursorImpWithName(name);
return self;
}
@ -58,6 +203,15 @@ static NSMutableArray *_cursorStack=nil;
return shared;
}
+(NSCursor *)contextualMenuCursor {
static NSCursor *shared=nil;
if(shared==nil)
shared=[[self alloc] initWithName:NSStringFromSelector(_cmd)];
return shared;
}
+(NSCursor *)crosshairCursor {
static NSCursor *shared=nil;
@ -157,6 +311,33 @@ static NSMutableArray *_cursorStack=nil;
return shared;
}
+(NSCursor *)dragCopyCursor {
static NSCursor *shared=nil;
if(shared==nil)
shared=[[self alloc] initWithName:NSStringFromSelector(_cmd)];
return shared;
}
+(NSCursor *)dragLinkCursor {
static NSCursor *shared=nil;
if(shared==nil)
shared=[[self alloc] initWithName:NSStringFromSelector(_cmd)];
return shared;
}
+(NSCursor *)operationNotAllowedCursor {
static NSCursor *shared=nil;
if(shared==nil)
shared=[[self alloc] initWithName:NSStringFromSelector(_cmd)];
return shared;
}
+(void)hide {
[[NSDisplay currentDisplay] hideCursor];
}
@ -173,18 +354,20 @@ static NSMutableArray *_cursorStack=nil;
}
-initWithImage:(NSImage *)image foregroundColorHint:(NSColor *)foregroundHint backgroundColorHint:(NSColor *)backgroundHint hotSpot:(NSPoint)hotSpot {
NSUnimplementedMethod();
return nil;
// the hints are unused per doc.s
return [self initWithImage:image hotSpot:hotSpot];
}
-initWithImage:(NSImage *)image hotSpot:(NSPoint)hotSpot {
_image=[image retain];
_hotSpot=hotSpot;
_platformCursor=NSPlatformCreateCursorImpWithImage(image,hotSpot);
return self;
}
-(void)dealloc {
[_image release];
NSPlatformReleaseCursorImp(_platformCursor);
[super dealloc];
}
@ -229,12 +412,12 @@ static NSMutableArray *_cursorStack=nil;
[_cursorStack removeLastObject];
[_cursorStack addObject:self];
[[NSDisplay currentDisplay] setCursor:_cursor];
NSPlatformSetCursorImp(_platformCursor);
}
-(void)push {
[_cursorStack addObject:self];
[[NSDisplay currentDisplay] setCursor:_cursor];
NSPlatformSetCursorImp(_platformCursor);
}
+(void)pop {
@ -244,7 +427,7 @@ static NSMutableArray *_cursorStack=nil;
[_cursorStack removeLastObject];
NSCursor *cursor=[_cursorStack lastObject];
[[NSDisplay currentDisplay] setCursor:cursor->_cursor];
NSPlatformSetCursorImp(cursor->_platformCursor);
}
@end

View File

@ -733,7 +733,7 @@ static inline void buildTransformsIfNeeded(NSView *self) {
[_subviews makeObjectsPerformSelector:_cmd withObject:window];
_validTrackingAreas=NO;
[_window _invalidateTrackingAreas];
[_window invalidateCursorRectsForView:self]; // this also invalidates tracking areas
[self viewDidMoveToWindow];
}

View File

@ -774,8 +774,8 @@ NSString * const NSWindowDidAnimateNotification=@"NSWindowDidAnimateNotification
[[self platformWindow] setFrame:_frame];
if(didSize)
[self _invalidateTrackingAreas];
[self resetCursorRects];
if(didSize)
[self postNotificationName:NSWindowDidResizeNotification];
@ -1702,7 +1702,7 @@ NSString * const NSWindowDidAnimateNotification=@"NSWindowDidAnimateNotification
-(void)invalidateCursorRectsForView:(NSView *)view {
[view discardCursorRects];
[view resetCursorRects];
[self _resetCursorRectsInView:view];
[self _invalidateTrackingAreas];
}

View File

@ -11,8 +11,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
@interface Win32Cursor : NSObject {
HCURSOR _handle;
BOOL _destroy;
}
-initWithHCURSOR:(HCURSOR)handle;
-initWithName:(NSString *)name;
-(HCURSOR)cursorHandle;

View File

@ -1,16 +1,20 @@
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
// Original - Christopher Lloyd <cjwl@objc.net>
#import <AppKit/Win32Cursor.h>
@implementation Win32Cursor
-initWithHCURSOR:(HCURSOR)handle {
_handle=handle;
_destroy=YES;
return self;
}
-initWithName:(NSString *)name {
LPCSTR idc=NULL;
@ -45,6 +49,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return self;
}
-(void)dealloc {
if(_destroy)
DestroyIcon(_handle);
[super dealloc];
}
-(HCURSOR)cursorHandle {
return _handle;
}

View File

@ -13,6 +13,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#import <Onyx2D/O2Context.h>
#import <Onyx2D/O2Surface.h>
#import <Onyx2D/O2Context_gdi.h>
#import <Foundation/NSPlatform_win32.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSPanel.h>
@ -1000,10 +1001,6 @@ static void initializeWindowClass(WNDCLASS *class){
HICON icon=(path==nil)?NULL:LoadImage(NULL,[path fileSystemRepresentation],IMAGE_ICON,16,16,LR_DEFAULTCOLOR|LR_LOADFROMFILE);
static WNDCLASS _standardWindowClass,_borderlessWindowClass,_borderlessWindowClassWithShadow;
OSVERSIONINFOEX osVersion;
osVersion.dwOSVersionInfoSize=sizeof(osVersion);
GetVersionEx((OSVERSIONINFO *)&osVersion);
if(icon==NULL)
icon=LoadImage(NULL,IDI_APPLICATION,IMAGE_ICON,0,0,LR_DEFAULTCOLOR|LR_SHARED);
@ -1019,10 +1016,8 @@ static void initializeWindowClass(WNDCLASS *class){
_borderlessWindowClassWithShadow.lpszClassName="Win32BorderlessWindowWithShadow";
// XP or higher
if((osVersion.dwMajorVersion==5 && osVersion.dwMinorVersion>=1) || osVersion.dwMajorVersion>5){
if(NSPlatformGreaterThanOrEqualToWindowsXP())
_borderlessWindowClassWithShadow.style|=CS_DROPSHADOW;
}
if(RegisterClass(&_standardWindowClass)==0)
NSLog(@"RegisterClass failed");

View File

@ -25,3 +25,5 @@ void _Win32Assert(const char *code,int line,const char *file);
NSTimeInterval Win32TimeIntervalFromFileTime(FILETIME fileTime);
void Win32ThreadSleepForTimeInterval(NSTimeInterval interval);
BOOL NSPlatformGreaterThanOrEqualToWindowsXP(void);
BOOL NSPlatformGreaterThanOrEqualToWindows2000(void);

View File

@ -504,3 +504,22 @@ void Win32ThreadSleepForTimeInterval(NSTimeInterval interval) {
}
}
BOOL NSPlatformGreaterThanOrEqualToWindowsXP(void) {
OSVERSIONINFOEX osVersion;
osVersion.dwOSVersionInfoSize=sizeof(osVersion);
GetVersionEx((OSVERSIONINFO *)&osVersion);
return ((osVersion.dwMajorVersion==5 && osVersion.dwMinorVersion>=1) || osVersion.dwMajorVersion>5)?YES:NO;
}
BOOL NSPlatformGreaterThanOrEqualToWindows2000(void) {
OSVERSIONINFOEX osVersion;
osVersion.dwOSVersionInfoSize=sizeof(osVersion);
GetVersionEx((OSVERSIONINFO *)&osVersion);
return (osVersion.dwMajorVersion>=5)?YES:NO;
}