1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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 #if wxOSX_USE_COCOA_OR_CARBON
15 #include <Cocoa/Cocoa.h>
17 #import <UIKit/UIKit.h>
21 #include "wx/osx/private.h"
29 bool cocoaLoaded = NSApplicationLoad();
30 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
35 wxMacAutoreleasePool::wxMacAutoreleasePool()
37 m_pool = [[NSAutoreleasePool alloc] init];
40 wxMacAutoreleasePool::~wxMacAutoreleasePool()
42 [(NSAutoreleasePool*)m_pool release];
47 #if defined( __WXCOCOCA__ ) || wxOSX_USE_COCOA
49 CGContextRef wxMacGetContextFromCurrentNSContext()
51 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 void wxMacCocoaRelease( void* obj )
64 [(NSObject*)obj release];
67 void wxMacCocoaAutorelease( void* obj )
69 [(NSObject*)obj autorelease];
72 void wxMacCocoaRetain( void* obj )
74 [(NSObject*)obj retain];
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // From "Cocoa Drawing Guide:Working with Images"
84 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
86 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
88 // Get the image dimensions.
89 imageRect.size.height = CGImageGetHeight(image);
90 imageRect.size.width = CGImageGetWidth(image);
92 // Create a new image to receive the Quartz image data.
93 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
96 // Get the Quartz context and draw.
97 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
98 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
99 [newImage unlockFocus];
102 // Create a bitmap rep from the image...
103 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
104 // Create an NSImage and add the bitmap rep to it...
105 NSImage *image = [[NSImage alloc] init];
106 [image addRepresentation:bitmapRep];
109 [newImage autorelease];
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 // copied from cursor.mm
119 static NSCursor* wxGetStockCursor( short sIndex )
121 ClassicCursor* pCursor = &gMacCursors[sIndex];
123 //Classic mac cursors are 1bps 16x16 black and white with a
124 //identical mask that is 1 for on and 0 for off
125 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
127 //NSCursor takes an NSImage takes a number of Representations - here
128 //we need only one for the raw data
129 NSBitmapImageRep *theRep =
130 [[NSBitmapImageRep alloc]
131 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
136 hasAlpha:YES // Well, more like a mask...
138 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
139 bytesPerRow:0 // I don't care - figure it out for me :)
140 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
142 //unsigned int is better to put data in then a void*
143 //note that working with bitfields would be a lot better here -
144 //but since it breaks some compilers...
145 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
147 //traverse through the bitmap data
148 for (int i = 0; i < 16; ++i)
150 //bit alpha bit alpha ... :D
152 //Notice the = instead of |= -
153 //this is to avoid doing a memset earlier
156 //do the rest of those bits and alphas :)
157 for (int shift = 0; shift < 32; ++shift)
159 const int bit = 1 << (shift >> 1);
160 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
161 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
165 //add the representation (data) to the image
166 [theImage addRepresentation:theRep];
168 //create the new cursor
169 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
170 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
173 //do the usual cleanups
177 //return the new cursor
181 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
183 WX_NSCursor cursor = nil;
186 case wxCURSOR_COPY_ARROW:
187 cursor = [[NSCursor arrowCursor] retain];
192 // should be displayed by the system when things are running
193 cursor = [[NSCursor arrowCursor] retain];
197 cursor = [[NSCursor IBeamCursor] retain];
201 cursor = [[NSCursor crosshairCursor] retain];
204 case wxCURSOR_SIZENWSE:
205 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
208 case wxCURSOR_SIZENESW:
209 cursor = wxGetStockCursor(kwxCursorSizeNESW);
212 case wxCURSOR_SIZEWE:
213 cursor = [[NSCursor resizeLeftRightCursor] retain];
216 case wxCURSOR_SIZENS:
217 cursor = [[NSCursor resizeUpDownCursor] retain];
220 case wxCURSOR_SIZING:
221 cursor = wxGetStockCursor(kwxCursorSize);
225 cursor = [[NSCursor pointingHandCursor] retain];
228 case wxCURSOR_BULLSEYE:
229 cursor = wxGetStockCursor(kwxCursorBullseye);
232 case wxCURSOR_PENCIL:
233 cursor = wxGetStockCursor(kwxCursorPencil);
236 case wxCURSOR_MAGNIFIER:
237 cursor = wxGetStockCursor(kwxCursorMagnifier);
240 case wxCURSOR_NO_ENTRY:
241 cursor = wxGetStockCursor(kwxCursorNoEntry);
244 case wxCURSOR_PAINT_BRUSH:
245 cursor = wxGetStockCursor(kwxCursorPaintBrush);
248 case wxCURSOR_POINT_LEFT:
249 cursor = wxGetStockCursor(kwxCursorPointLeft);
252 case wxCURSOR_POINT_RIGHT:
253 cursor = wxGetStockCursor(kwxCursorPointRight);
256 case wxCURSOR_QUESTION_ARROW:
257 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
261 cursor = wxGetStockCursor(kwxCursorBlank);
264 case wxCURSOR_RIGHT_ARROW:
265 cursor = wxGetStockCursor(kwxCursorRightArrow);
268 case wxCURSOR_SPRAYCAN:
269 cursor = wxGetStockCursor(kwxCursorRoller);
274 case wxCURSOR_LEFT_BUTTON:
275 case wxCURSOR_RIGHT_BUTTON:
276 case wxCURSOR_MIDDLE_BUTTON:
278 cursor = [[NSCursor arrowCursor] retain];
284 // C-based style wrapper routines around NSCursor
285 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
287 static BOOL firstTime = YES;
291 // 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
292 [[[NSWindow alloc] init] release];
296 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
297 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
304 void wxMacCocoaSetCursor( WX_NSCursor cursor )
309 void wxMacCocoaHideCursor()
314 void wxMacCocoaShowCursor()