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 #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 CreateNSImageFromCGImage( 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];
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // copied from cursor.mm
110 static NSCursor* wxGetStockCursor( short sIndex )
112 ClassicCursor* pCursor = &gMacCursors[sIndex];
114 //Classic mac cursors are 1bps 16x16 black and white with a
115 //identical mask that is 1 for on and 0 for off
116 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
118 //NSCursor takes an NSImage takes a number of Representations - here
119 //we need only one for the raw data
120 NSBitmapImageRep *theRep =
121 [[NSBitmapImageRep alloc]
122 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
127 hasAlpha:YES // Well, more like a mask...
129 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
130 bytesPerRow:0 // I don't care - figure it out for me :)
131 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
133 //unsigned int is better to put data in then a void*
134 //note that working with bitfields would be a lot better here -
135 //but since it breaks some compilers...
136 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
138 //traverse through the bitmap data
139 for (int i = 0; i < 16; ++i)
141 //bit alpha bit alpha ... :D
143 //Notice the = instead of |= -
144 //this is to avoid doing a memset earlier
147 //do the rest of those bits and alphas :)
148 for (int shift = 0; shift < 32; ++shift)
150 const int bit = 1 << (shift >> 1);
151 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
152 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
156 //add the representation (data) to the image
157 [theImage addRepresentation:theRep];
159 //create the new cursor
160 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
161 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
164 //do the usual cleanups
168 //return the new cursor
172 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
174 WX_NSCursor cursor = nil;
177 case wxCURSOR_COPY_ARROW:
178 cursor = [[NSCursor arrowCursor] retain];
183 // should be displayed by the system when things are running
184 cursor = [[NSCursor arrowCursor] retain];
188 cursor = [[NSCursor IBeamCursor] retain];
192 cursor = [[NSCursor crosshairCursor] retain];
195 case wxCURSOR_SIZENWSE:
196 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
199 case wxCURSOR_SIZENESW:
200 cursor = wxGetStockCursor(kwxCursorSizeNESW);
203 case wxCURSOR_SIZEWE:
204 cursor = [[NSCursor resizeLeftRightCursor] retain];
207 case wxCURSOR_SIZENS:
208 cursor = [[NSCursor resizeUpDownCursor] retain];
211 case wxCURSOR_SIZING:
212 cursor = wxGetStockCursor(kwxCursorSize);
216 cursor = [[NSCursor pointingHandCursor] retain];
219 case wxCURSOR_BULLSEYE:
220 cursor = wxGetStockCursor(kwxCursorBullseye);
223 case wxCURSOR_PENCIL:
224 cursor = wxGetStockCursor(kwxCursorPencil);
227 case wxCURSOR_MAGNIFIER:
228 cursor = wxGetStockCursor(kwxCursorMagnifier);
231 case wxCURSOR_NO_ENTRY:
232 cursor = wxGetStockCursor(kwxCursorNoEntry);
235 case wxCURSOR_PAINT_BRUSH:
236 cursor = wxGetStockCursor(kwxCursorPaintBrush);
239 case wxCURSOR_POINT_LEFT:
240 cursor = wxGetStockCursor(kwxCursorPointLeft);
243 case wxCURSOR_POINT_RIGHT:
244 cursor = wxGetStockCursor(kwxCursorPointRight);
247 case wxCURSOR_QUESTION_ARROW:
248 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
252 cursor = wxGetStockCursor(kwxCursorBlank);
255 case wxCURSOR_RIGHT_ARROW:
256 cursor = wxGetStockCursor(kwxCursorRightArrow);
259 case wxCURSOR_SPRAYCAN:
260 cursor = wxGetStockCursor(kwxCursorRoller);
265 case wxCURSOR_LEFT_BUTTON:
266 case wxCURSOR_RIGHT_BUTTON:
267 case wxCURSOR_MIDDLE_BUTTON:
269 cursor = [[NSCursor arrowCursor] retain];
275 // C-based style wrapper routines around NSCursor
276 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
278 static BOOL firstTime = YES;
282 // 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
283 [[[NSWindow alloc] init] release];
287 NSImage *nsImage = CreateNSImageFromCGImage( cgImageRef );
288 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
295 void wxMacCocoaSetCursor( WX_NSCursor cursor )
300 void wxMacCocoaHideCursor()
305 void wxMacCocoaShowCursor()