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>
17 #include "wx/mac/private.h"
24 bool cocoaLoaded = NSApplicationLoad();
25 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
29 wxMacAutoreleasePool::wxMacAutoreleasePool()
31 m_pool = [[NSAutoreleasePool alloc] init];
34 wxMacAutoreleasePool::~wxMacAutoreleasePool()
36 [(NSAutoreleasePool*)m_pool release];
43 CGContextRef wxMacGetContextFromCurrentNSContext()
45 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 void wxMacCocoaRelease( void* obj )
58 [(NSObject*)obj release];
61 void wxMacCocoaAutorelease( void* obj )
63 [(NSObject*)obj autorelease];
66 void wxMacCocoaRetain( void* obj )
68 [(NSObject*)obj retain];
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // From "Cocoa Drawing Guide:Working with Images"
76 WX_NSImage CreateNSImageFromCGImage( CGImageRef image )
78 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
80 // Get the image dimensions.
81 imageRect.size.height = CGImageGetHeight(image);
82 imageRect.size.width = CGImageGetWidth(image);
84 // Create a new image to receive the Quartz image data.
85 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
88 // Get the Quartz context and draw.
89 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
90 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
91 [newImage unlockFocus];
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
102 // copied from cursor.mm
104 static NSCursor* wxGetStockCursor( short sIndex )
106 ClassicCursor* pCursor = &gMacCursors[sIndex];
108 //Classic mac cursors are 1bps 16x16 black and white with a
109 //identical mask that is 1 for on and 0 for off
110 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
112 //NSCursor takes an NSImage takes a number of Representations - here
113 //we need only one for the raw data
114 NSBitmapImageRep *theRep =
115 [[NSBitmapImageRep alloc]
116 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
121 hasAlpha:YES // Well, more like a mask...
123 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
124 bytesPerRow:0 // I don't care - figure it out for me :)
125 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
127 //unsigned int is better to put data in then a void*
128 //note that working with bitfields would be a lot better here -
129 //but since it breaks some compilers...
130 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
132 //traverse through the bitmap data
133 for (int i = 0; i < 16; ++i)
135 //bit alpha bit alpha ... :D
137 //Notice the = instead of |= -
138 //this is to avoid doing a memset earlier
141 //do the rest of those bits and alphas :)
142 for (int shift = 0; shift < 32; ++shift)
144 const int bit = 1 << (shift >> 1);
145 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
146 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
150 //add the representation (data) to the image
151 [theImage addRepresentation:theRep];
153 //create the new cursor
154 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
155 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
158 //do the usual cleanups
162 //return the new cursor
166 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
168 WX_NSCursor cursor = nil;
171 case wxCURSOR_COPY_ARROW:
172 cursor = [[NSCursor arrowCursor] retain];
177 // should be displayed by the system when things are running
178 cursor = [[NSCursor arrowCursor] retain];
182 cursor = [[NSCursor IBeamCursor] retain];
186 cursor = [[NSCursor crosshairCursor] retain];
189 case wxCURSOR_SIZENWSE:
190 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
193 case wxCURSOR_SIZENESW:
194 cursor = wxGetStockCursor(kwxCursorSizeNESW);
197 case wxCURSOR_SIZEWE:
198 cursor = [[NSCursor resizeLeftRightCursor] retain];
201 case wxCURSOR_SIZENS:
202 cursor = [[NSCursor resizeUpDownCursor] retain];
205 case wxCURSOR_SIZING:
206 cursor = wxGetStockCursor(kwxCursorSize);
210 cursor = [[NSCursor pointingHandCursor] retain];
213 case wxCURSOR_BULLSEYE:
214 cursor = wxGetStockCursor(kwxCursorBullseye);
217 case wxCURSOR_PENCIL:
218 cursor = wxGetStockCursor(kwxCursorPencil);
221 case wxCURSOR_MAGNIFIER:
222 cursor = wxGetStockCursor(kwxCursorMagnifier);
225 case wxCURSOR_NO_ENTRY:
226 cursor = wxGetStockCursor(kwxCursorNoEntry);
229 case wxCURSOR_PAINT_BRUSH:
230 cursor = wxGetStockCursor(kwxCursorPaintBrush);
233 case wxCURSOR_POINT_LEFT:
234 cursor = wxGetStockCursor(kwxCursorPointLeft);
237 case wxCURSOR_POINT_RIGHT:
238 cursor = wxGetStockCursor(kwxCursorPointRight);
241 case wxCURSOR_QUESTION_ARROW:
242 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
246 cursor = wxGetStockCursor(kwxCursorBlank);
249 case wxCURSOR_RIGHT_ARROW:
250 cursor = wxGetStockCursor(kwxCursorRightArrow);
253 case wxCURSOR_SPRAYCAN:
254 cursor = wxGetStockCursor(kwxCursorRoller);
259 case wxCURSOR_LEFT_BUTTON:
260 case wxCURSOR_RIGHT_BUTTON:
261 case wxCURSOR_MIDDLE_BUTTON:
263 cursor = [[NSCursor arrowCursor] retain];
269 // C-based style wrapper routines around NSCursor
270 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
272 static BOOL firstTime = YES;
276 // 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
277 [[[NSWindow alloc] init] release];
281 NSImage *nsImage = CreateNSImageFromCGImage( cgImageRef );
282 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
289 void wxMacCocoaSetCursor( WX_NSCursor cursor )
294 void wxMacCocoaHideCursor()
299 void wxMacCocoaShowCursor()