1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/utils.mm
3 // Purpose: various cocoa mixin utility functions
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: utilscocoa.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include <Cocoa/Cocoa.h>
16 #include "wx/mac/private.h"
20 bool cocoaLoaded = NSApplicationLoad();
21 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
25 wxMacAutoreleasePool::wxMacAutoreleasePool()
27 m_pool = [[NSAutoreleasePool alloc] init];
30 wxMacAutoreleasePool::~wxMacAutoreleasePool()
32 [(NSAutoreleasePool*)m_pool release];
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 void wxMacCocoaRelease( void* obj )
41 [(NSObject*)obj release];
44 void wxMacCocoaAutorelease( void* obj )
46 [(NSObject*)obj autorelease];
49 void wxMacCocoaRetain( void* obj )
51 [(NSObject*)obj retain];
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // From "Cocoa Drawing Guide:Working with Images"
59 WX_NSImage CreateNSImageFromCGImage( CGImageRef image )
61 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
63 // Get the image dimensions.
64 imageRect.size.height = CGImageGetHeight(image);
65 imageRect.size.width = CGImageGetWidth(image);
67 // Create a new image to receive the Quartz image data.
68 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
71 // Get the Quartz context and draw.
72 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
73 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
74 [newImage unlockFocus];
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
85 // copied from cursor.mm
87 static NSCursor* wxGetStockCursor( short sIndex )
89 ClassicCursor* pCursor = &gMacCursors[sIndex];
91 //Classic mac cursors are 1bps 16x16 black and white with a
92 //identical mask that is 1 for on and 0 for off
93 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
95 //NSCursor takes an NSImage takes a number of Representations - here
96 //we need only one for the raw data
97 NSBitmapImageRep *theRep =
98 [[NSBitmapImageRep alloc]
99 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
104 hasAlpha:YES // Well, more like a mask...
106 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
107 bytesPerRow:0 // I don't care - figure it out for me :)
108 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
110 //unsigned int is better to put data in then a void*
111 //note that working with bitfields would be a lot better here -
112 //but since it breaks some compilers...
113 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
115 //traverse through the bitmap data
116 for (int i = 0; i < 16; ++i)
118 //bit alpha bit alpha ... :D
120 //Notice the = instead of |= -
121 //this is to avoid doing a memset earlier
124 //do the rest of those bits and alphas :)
125 for (int shift = 0; shift < 32; ++shift)
127 const int bit = 1 << (shift >> 1);
128 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
129 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
133 //add the representation (data) to the image
134 [theImage addRepresentation:theRep];
136 //create the new cursor
137 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
138 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
141 //do the usual cleanups
145 //return the new cursor
149 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
151 WX_NSCursor cursor = nil;
154 case wxCURSOR_COPY_ARROW:
155 cursor = [[NSCursor arrowCursor] retain];
160 // should be displayed by the system when things are running
161 cursor = [[NSCursor arrowCursor] retain];
165 cursor = [[NSCursor IBeamCursor] retain];
169 cursor = [[NSCursor crosshairCursor] retain];
172 case wxCURSOR_SIZENWSE:
173 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
176 case wxCURSOR_SIZENESW:
177 cursor = wxGetStockCursor(kwxCursorSizeNESW);
180 case wxCURSOR_SIZEWE:
181 cursor = [[NSCursor resizeLeftRightCursor] retain];
184 case wxCURSOR_SIZENS:
185 cursor = [[NSCursor resizeUpDownCursor] retain];
188 case wxCURSOR_SIZING:
189 cursor = wxGetStockCursor(kwxCursorSize);
193 cursor = [[NSCursor pointingHandCursor] retain];
196 case wxCURSOR_BULLSEYE:
197 cursor = wxGetStockCursor(kwxCursorBullseye);
200 case wxCURSOR_PENCIL:
201 cursor = wxGetStockCursor(kwxCursorPencil);
204 case wxCURSOR_MAGNIFIER:
205 cursor = wxGetStockCursor(kwxCursorMagnifier);
208 case wxCURSOR_NO_ENTRY:
209 cursor = wxGetStockCursor(kwxCursorNoEntry);
212 case wxCURSOR_PAINT_BRUSH:
213 cursor = wxGetStockCursor(kwxCursorPaintBrush);
216 case wxCURSOR_POINT_LEFT:
217 cursor = wxGetStockCursor(kwxCursorPointLeft);
220 case wxCURSOR_POINT_RIGHT:
221 cursor = wxGetStockCursor(kwxCursorPointRight);
224 case wxCURSOR_QUESTION_ARROW:
225 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
229 cursor = wxGetStockCursor(kwxCursorBlank);
232 case wxCURSOR_RIGHT_ARROW:
233 cursor = wxGetStockCursor(kwxCursorRightArrow);
236 case wxCURSOR_SPRAYCAN:
237 cursor = wxGetStockCursor(kwxCursorRoller);
242 case wxCURSOR_LEFT_BUTTON:
243 case wxCURSOR_RIGHT_BUTTON:
244 case wxCURSOR_MIDDLE_BUTTON:
246 cursor = [[NSCursor arrowCursor] retain];
252 // C-based style wrapper routines around NSCursor
253 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
255 static BOOL firstTime = YES;
259 // Must first call [[[NSWindow alloc] init] release] to get the NSWindow machinery set up so that NSCursor can use a window to cache the cursor image
260 [[[NSWindow alloc] init] release];
264 NSImage *nsImage = CreateNSImageFromCGImage( cgImageRef );
265 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
272 void wxMacCocoaSetCursor( WX_NSCursor cursor )
277 void wxMacCocoaHideCursor()
282 void wxMacCocoaShowCursor()