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 wxOSX_USE_COCOA_OR_IPHONE
49 CGContextRef wxOSXGetContextFromCurrentNSContext()
51 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
56 void wxOSXLockFocus( WXWidget view)
58 [view lockFocusIfCanDraw];
61 void wxOSXUnlockFocus( WXWidget view)
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 void wxMacCocoaRelease( void* obj )
74 [(NSObject*)obj release];
77 void wxMacCocoaAutorelease( void* obj )
79 [(NSObject*)obj autorelease];
82 void wxMacCocoaRetain( void* obj )
84 [(NSObject*)obj retain];
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // From "Cocoa Drawing Guide:Working with Images"
94 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
96 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
98 // Get the image dimensions.
99 imageRect.size.height = CGImageGetHeight(image);
100 imageRect.size.width = CGImageGetWidth(image);
102 // Create a new image to receive the Quartz image data.
103 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
104 [newImage lockFocus];
106 // Get the Quartz context and draw.
107 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
108 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
109 [newImage unlockFocus];
112 // Create a bitmap rep from the image...
113 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
114 // Create an NSImage and add the bitmap rep to it...
115 NSImage *image = [[NSImage alloc] init];
116 [image addRepresentation:bitmapRep];
119 [newImage autorelease];
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // copied from cursor.mm
129 static NSCursor* wxGetStockCursor( short sIndex )
131 ClassicCursor* pCursor = &gMacCursors[sIndex];
133 //Classic mac cursors are 1bps 16x16 black and white with a
134 //identical mask that is 1 for on and 0 for off
135 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
137 //NSCursor takes an NSImage takes a number of Representations - here
138 //we need only one for the raw data
139 NSBitmapImageRep *theRep =
140 [[NSBitmapImageRep alloc]
141 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
146 hasAlpha:YES // Well, more like a mask...
148 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
149 bytesPerRow:0 // I don't care - figure it out for me :)
150 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
152 //unsigned int is better to put data in then a void*
153 //note that working with bitfields would be a lot better here -
154 //but since it breaks some compilers...
155 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
157 //traverse through the bitmap data
158 for (int i = 0; i < 16; ++i)
160 //bit alpha bit alpha ... :D
162 //Notice the = instead of |= -
163 //this is to avoid doing a memset earlier
166 //do the rest of those bits and alphas :)
167 for (int shift = 0; shift < 32; ++shift)
169 const int bit = 1 << (shift >> 1);
170 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
171 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
175 //add the representation (data) to the image
176 [theImage addRepresentation:theRep];
178 //create the new cursor
179 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
180 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
183 //do the usual cleanups
187 //return the new cursor
191 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
193 WX_NSCursor cursor = nil;
196 case wxCURSOR_COPY_ARROW:
197 cursor = [[NSCursor arrowCursor] retain];
202 // should be displayed by the system when things are running
203 cursor = [[NSCursor arrowCursor] retain];
207 cursor = [[NSCursor IBeamCursor] retain];
211 cursor = [[NSCursor crosshairCursor] retain];
214 case wxCURSOR_SIZENWSE:
215 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
218 case wxCURSOR_SIZENESW:
219 cursor = wxGetStockCursor(kwxCursorSizeNESW);
222 case wxCURSOR_SIZEWE:
223 cursor = [[NSCursor resizeLeftRightCursor] retain];
226 case wxCURSOR_SIZENS:
227 cursor = [[NSCursor resizeUpDownCursor] retain];
230 case wxCURSOR_SIZING:
231 cursor = wxGetStockCursor(kwxCursorSize);
235 cursor = [[NSCursor pointingHandCursor] retain];
238 case wxCURSOR_BULLSEYE:
239 cursor = wxGetStockCursor(kwxCursorBullseye);
242 case wxCURSOR_PENCIL:
243 cursor = wxGetStockCursor(kwxCursorPencil);
246 case wxCURSOR_MAGNIFIER:
247 cursor = wxGetStockCursor(kwxCursorMagnifier);
250 case wxCURSOR_NO_ENTRY:
251 cursor = wxGetStockCursor(kwxCursorNoEntry);
254 case wxCURSOR_PAINT_BRUSH:
255 cursor = wxGetStockCursor(kwxCursorPaintBrush);
258 case wxCURSOR_POINT_LEFT:
259 cursor = wxGetStockCursor(kwxCursorPointLeft);
262 case wxCURSOR_POINT_RIGHT:
263 cursor = wxGetStockCursor(kwxCursorPointRight);
266 case wxCURSOR_QUESTION_ARROW:
267 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
271 cursor = wxGetStockCursor(kwxCursorBlank);
274 case wxCURSOR_RIGHT_ARROW:
275 cursor = wxGetStockCursor(kwxCursorRightArrow);
278 case wxCURSOR_SPRAYCAN:
279 cursor = wxGetStockCursor(kwxCursorRoller);
282 case wxCURSOR_OPEN_HAND:
283 cursor = [[NSCursor openHandCursor] retain];
286 case wxCURSOR_CLOSED_HAND:
287 cursor = [[NSCursor closedHandCursor] retain];
292 case wxCURSOR_LEFT_BUTTON:
293 case wxCURSOR_RIGHT_BUTTON:
294 case wxCURSOR_MIDDLE_BUTTON:
296 cursor = [[NSCursor arrowCursor] retain];
302 // C-based style wrapper routines around NSCursor
303 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
305 static BOOL firstTime = YES;
309 // 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
310 [[[NSWindow alloc] init] release];
314 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
315 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
322 void wxMacCocoaSetCursor( WX_NSCursor cursor )
327 void wxMacCocoaHideCursor()
332 void wxMacCocoaShowCursor()