activating high-res support via artprov mac
[wxWidgets.git] / src / osx / carbon / utilscocoa.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/carbon/utils.mm
3 // Purpose:     various cocoa mixin utility functions
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     1998-01-01
7 // RCS-ID:      $Id: utilscocoa.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright:   (c) Stefan Csomor
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxOSX_USE_COCOA_OR_CARBON
15 #include <Cocoa/Cocoa.h>
16 #else
17 #import <UIKit/UIKit.h>
18 #endif
19
20 #ifdef __WXMAC__
21 #include "wx/osx/private.h"
22 #endif
23
24 #include "wx/fontutil.h"
25
26 #if wxOSX_USE_COCOA
27 #include "wx/cocoa/string.h"
28 #endif
29
30 #ifdef __WXMAC__
31
32 #if wxOSX_USE_CARBON
33 bool wxMacInitCocoa()
34 {
35     bool cocoaLoaded = NSApplicationLoad();
36     wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
37     return cocoaLoaded;
38 }
39 #endif
40
41 wxMacAutoreleasePool::wxMacAutoreleasePool()
42 {
43     m_pool = [[NSAutoreleasePool alloc] init];
44 }
45
46 wxMacAutoreleasePool::~wxMacAutoreleasePool()
47 {
48     [(NSAutoreleasePool*)m_pool release];
49 }
50
51 #endif
52
53 #if wxOSX_USE_COCOA
54
55 CGContextRef wxOSXGetContextFromCurrentContext()
56 {
57     CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
58                                           graphicsPort];
59     return context;
60 }
61
62 bool wxOSXLockFocus( WXWidget view)
63 {
64     return [view lockFocusIfCanDraw];
65 }
66
67 void wxOSXUnlockFocus( WXWidget view)
68 {
69     [view unlockFocus];
70 }
71
72 #endif
73
74 #if wxOSX_USE_IPHONE
75
76 CGContextRef wxOSXGetContextFromCurrentContext()
77 {
78     CGContextRef context = UIGraphicsGetCurrentContext();
79     return context;
80 }
81
82 #endif
83
84 // ----------------------------------------------------------------------------
85 // NSObject Utils
86 // ----------------------------------------------------------------------------
87
88 void wxMacCocoaRelease( void* obj )
89 {
90     [(NSObject*)obj release];
91 }
92
93 void wxMacCocoaAutorelease( void* obj )
94 {
95     [(NSObject*)obj autorelease];
96 }
97
98 void* wxMacCocoaRetain( void* obj )
99 {
100     [(NSObject*)obj retain];
101     return obj;
102 }
103
104 // ----------------------------------------------------------------------------
105 // NSFont Utils
106 // ----------------------------------------------------------------------------
107
108 #if wxOSX_USE_COCOA
109 wxFont::wxFont(WX_NSFont nsfont)
110 {
111     [nsfont retain];
112     wxNativeFontInfo info;
113     SetNativeInfoFromNSFont(nsfont, &info);
114     Create(info);
115 }
116
117 void wxFont::SetNativeInfoFromNSFont(WX_NSFont theFont, wxNativeFontInfo* info)
118 {   
119     if ( info->m_faceName.empty())
120     {
121         //Get more information about the user's chosen font
122         NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
123         int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
124
125         wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
126         //Set the wx font to the appropriate data
127         if(theTraits & NSFixedPitchFontMask)
128             fontFamily = wxFONTFAMILY_TELETYPE;
129
130         wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
131         wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
132         bool underlined = false;
133
134         int size = (int) ([theFont pointSize]+0.5);
135  
136         if ( theFontWeight >= 9 )
137             fontweight = wxFONTWEIGHT_BOLD ;
138         else if ( theFontWeight < 5 )
139             fontweight = wxFONTWEIGHT_LIGHT;
140         else
141             fontweight = wxFONTWEIGHT_NORMAL ;
142             
143         if ( theTraits & NSItalicFontMask )
144             fontstyle = wxFONTSTYLE_ITALIC ;
145
146         info->Init(size,fontFamily,fontstyle,fontweight,underlined,
147             wxStringWithNSString([theFont familyName]), wxFONTENCODING_DEFAULT);
148
149     }
150 }
151
152 WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
153 {
154     NSFont* nsfont = nil;
155     switch( font )
156     {
157         case wxOSX_SYSTEM_FONT_NORMAL:
158             nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
159             break;
160         case wxOSX_SYSTEM_FONT_BOLD:
161             nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
162             break;
163         case wxOSX_SYSTEM_FONT_SMALL:
164             nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
165             break;
166         case wxOSX_SYSTEM_FONT_SMALL_BOLD:
167             nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
168             break;
169         case wxOSX_SYSTEM_FONT_MINI:
170             nsfont = [NSFont systemFontOfSize:
171                 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
172             break;
173        case wxOSX_SYSTEM_FONT_MINI_BOLD:
174             nsfont = [NSFont boldSystemFontOfSize:
175                 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
176             break;
177         case wxOSX_SYSTEM_FONT_LABELS:
178             nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
179             break;
180        case wxOSX_SYSTEM_FONT_VIEWS:
181             nsfont = [NSFont controlContentFontOfSize:0];
182             break;
183         default:
184             break;
185     }
186     [nsfont retain];
187     SetNativeInfoFromNSFont(nsfont, info);
188     return nsfont;
189 }
190
191 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
192 static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, tan(DegToRad(11)), 1, 0, 0  };
193
194 WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
195 {
196     NSFont* nsFont;
197     int weight = 5;
198     NSFontTraitMask traits = 0;
199     if (info->m_weight == wxFONTWEIGHT_BOLD)
200     {
201         traits |= NSBoldFontMask;
202         weight = 9;
203     }
204     else if (info->m_weight == wxFONTWEIGHT_LIGHT)
205         weight = 3;
206
207     if (info->m_style == wxFONTSTYLE_ITALIC || info->m_style == wxFONTSTYLE_SLANT)
208         traits |= NSItalicFontMask;
209     
210     nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() 
211         traits:traits weight:weight size:info->m_pointSize];
212     
213     if ( nsFont == nil )
214     {
215         NSFontTraitMask remainingTraits = traits;
216         nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString() 
217                                                             traits:0 weight:5 size:info->m_pointSize];
218         if ( nsFont == nil )
219         {
220             if ( info->m_weight == wxFONTWEIGHT_BOLD )
221             {
222                 nsFont = [NSFont boldSystemFontOfSize:info->m_pointSize];
223                 remainingTraits &= ~NSBoldFontMask;
224             }
225             else
226                 nsFont = [NSFont systemFontOfSize:info->m_pointSize];
227         }
228         
229         // fallback - if in doubt, let go of the bold attribute
230         if ( nsFont && (remainingTraits & NSItalicFontMask) )
231         {
232             NSFont* nsFontWithTraits = nil;
233             if ( remainingTraits & NSBoldFontMask)
234             {
235                 nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSBoldFontMask];
236                 if ( nsFontWithTraits == nil )
237                 {
238                     nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSItalicFontMask];
239                     if ( nsFontWithTraits != nil )
240                         remainingTraits &= ~NSItalicFontMask;
241                 }
242                 else
243                 {
244                     remainingTraits &= ~NSBoldFontMask;
245                 }
246             }
247             // the code below causes crashes, because fontDescriptorWithMatrix is not returning a valid font descriptor
248             // it adds a NSCTFontMatrixAttribute as well which cannot be disposed of correctly by the autorelease pool
249             // so at the moment we have to disable this and cannot synthesize italic fonts if they are not available on the system
250 #if 0
251             if ( remainingTraits & NSItalicFontMask )
252             {
253                 if ( nsFontWithTraits == nil )
254                     nsFontWithTraits = nsFont;
255                 
256                 NSAffineTransform* transform = [NSAffineTransform transform];
257                 [transform setTransformStruct:kSlantNSTransformStruct];
258                 [transform scaleBy:info->m_pointSize];
259                 NSFontDescriptor* italicDesc = [[nsFontWithTraits fontDescriptor] fontDescriptorWithMatrix:transform];
260                 if ( italicDesc != nil )
261                 {
262                     NSFont* f = [NSFont fontWithDescriptor:italicDesc size:(CGFloat)(info->m_pointSize)];
263                     if ( f != nil )
264                         nsFontWithTraits = f;
265                 }
266             }
267 #endif
268             if ( nsFontWithTraits != nil )
269                 nsFont = nsFontWithTraits;
270         }
271     }
272             
273     wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ;
274     wxMacCocoaRetain(nsFont);
275     return nsFont;
276 }
277
278 #endif
279
280 #if wxOSX_USE_IPHONE
281
282 WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
283 {
284     UIFont* uifont;
285     switch( font )
286     {
287         case wxOSX_SYSTEM_FONT_NORMAL:
288             uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
289             break;
290         case wxOSX_SYSTEM_FONT_BOLD:
291             uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
292             break;
293         case wxOSX_SYSTEM_FONT_MINI:
294         case wxOSX_SYSTEM_FONT_SMALL:
295             uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
296             break;
297         case wxOSX_SYSTEM_FONT_MINI_BOLD:
298         case wxOSX_SYSTEM_FONT_SMALL_BOLD:
299             uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
300             break;
301         case wxOSX_SYSTEM_FONT_VIEWS:
302         case wxOSX_SYSTEM_FONT_LABELS:
303             uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
304             break;
305         default:
306             break;
307     }
308     [uifont retain];
309     if ( info->m_faceName.empty())
310     {
311         wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
312         wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
313         bool underlined = false;
314
315         int size = (int) ([uifont pointSize]+0.5);
316         /*
317         NSFontSymbolicTraits traits = [desc symbolicTraits];
318
319         if ( traits & NSFontBoldTrait )
320             fontweight = wxFONTWEIGHT_BOLD ;
321         else
322             fontweight = wxFONTWEIGHT_NORMAL ;
323         if ( traits & NSFontItalicTrait )
324             fontstyle = wxFONTSTYLE_ITALIC ;
325         */
326         wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
327         info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
328             fontname.AsString(), wxFONTENCODING_DEFAULT);
329
330     }
331     return uifont;
332 }
333
334 WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
335 {
336     UIFont* uiFont;
337     uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
338     wxMacCocoaRetain(uiFont);
339     return uiFont;
340 }
341
342 #endif
343 // ----------------------------------------------------------------------------
344 // NSImage Utils
345 // ----------------------------------------------------------------------------
346
347 #if wxOSX_USE_IPHONE
348
349 WX_UIImage  wxOSXGetUIImageFromCGImage( CGImageRef image )
350 {
351     UIImage  *newImage = [UIImage imageWithCGImage:image];
352     [newImage autorelease];
353     return( newImage );
354 }
355
356 wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& size)
357 {
358 #if 1
359     // unfortunately this only accesses images in the app bundle, not the system wide globals
360     wxCFStringRef cfname(name);
361     return wxBitmap( [[UIImage imageNamed:cfname.AsNSString()] CGImage] );
362 #else
363     return wxBitmap();
364 #endif
365 }
366
367 #endif
368
369 #if wxOSX_USE_COCOA
370
371 wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &client, const wxSize& size)
372 {
373     wxCFStringRef cfname(name);
374     wxCFRef<CGImageRef> image( wxOSXCreateCGImageFromNSImage([NSImage imageNamed:cfname.AsNSString()]) );
375     return wxBitmap( image );
376 }
377
378 //  From "Cocoa Drawing Guide:Working with Images"
379 WX_NSImage  wxOSXGetNSImageFromCGImage( CGImageRef image )
380 {
381     NSRect      imageRect    = NSMakeRect(0.0, 0.0, 0.0, 0.0);
382
383     // Get the image dimensions.
384     imageRect.size.height = CGImageGetHeight(image);
385     imageRect.size.width = CGImageGetWidth(image);
386
387     // Create a new image to receive the Quartz image data.
388     NSImage  *newImage = [[NSImage alloc] initWithSize:imageRect.size];
389     [newImage lockFocus];
390
391     // Get the Quartz context and draw.
392     CGContextRef  imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
393     CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
394     [newImage unlockFocus];
395
396     /*
397         // Create a bitmap rep from the image...
398         NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
399         // Create an NSImage and add the bitmap rep to it...
400         NSImage *image = [[NSImage alloc] init];
401         [image addRepresentation:bitmapRep];
402         [bitmapRep release];
403     */
404     [newImage autorelease];
405     return( newImage );
406 }
407
408 CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
409 {
410     // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
411     
412     NSSize imageSize = [nsimage size];
413     CGColorSpaceRef genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); 
414     CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst); 
415     NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
416     [NSGraphicsContext saveGraphicsState];
417     [NSGraphicsContext setCurrentContext:nsGraphicsContext];
418     [[NSColor whiteColor] setFill];
419     NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
420     [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
421     [NSGraphicsContext setCurrentContext:nsGraphicsContext];
422     CGImageRef image = CGBitmapContextCreateImage(context);
423     CFRelease(context);
424     return image;
425  }
426
427 // ----------------------------------------------------------------------------
428 // NSCursor Utils
429 // ----------------------------------------------------------------------------
430
431 // copied from cursor.mm
432
433 static NSCursor* wxGetStockCursor( short sIndex )
434 {
435     ClassicCursor* pCursor = &gMacCursors[sIndex];
436
437     //Classic mac cursors are 1bps 16x16 black and white with a
438     //identical mask that is 1 for on and 0 for off
439     NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
440
441     //NSCursor takes an NSImage takes a number of Representations - here
442     //we need only one for the raw data
443     NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
444         initWithBitmapDataPlanes: NULL  // Tell Cocoa to allocate the planes for us.
445         pixelsWide: 16      // All classic cursors are 16x16
446         pixelsHigh: 16
447         bitsPerSample: 1    // All classic cursors are bitmaps with bitmasks
448         samplesPerPixel: 2  // Sample 0:image 1:mask
449         hasAlpha: YES       // Identify last sample as a mask
450         isPlanar: YES       // Use a separate array for each sample
451         colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
452         bytesPerRow: 2      // Rows in each plane are on 2-byte boundaries (no pad)
453         bitsPerPixel: 1];   // same as bitsPerSample since data is planar
454
455     // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
456     unsigned char *planes[5];
457     [theRep getBitmapDataPlanes:planes];
458     wxASSERT(planes[0] != NULL);
459     wxASSERT(planes[1] != NULL);
460     wxASSERT(planes[2] == NULL);
461     wxASSERT(planes[3] == NULL);
462     wxASSERT(planes[4] == NULL);
463
464     // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
465     // Why not use NSCalibratedBlackColorSpace?  Because that reverses the
466     // sense of the alpha (mask) plane.
467     // NOTE2: The mask data is 0=off 1=on
468     // NOTE3: Cocoa asks for "premultiplied" color planes.  Since we have a
469     // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
470     // on the two.  The original cursor bitmaps have 0 (white actually) for
471     // any masked-off pixels.  Therefore every masked-off pixel would be wrong
472     // since we bit-flip all of the picture bits.  In practice, Cocoa doesn't
473     // seem to care, but we are following the documentation.
474
475     // Fill in the color (black/white) plane
476     for(int i=0; i<16; ++i)
477     {
478         planes[0][2*i  ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
479         planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
480     }
481     // Fill in the alpha (i.e. mask) plane
482     for(int i=0; i<16; ++i)
483     {
484         planes[1][2*i  ] = pCursor->mask[i] >> 8 & 0xff;
485         planes[1][2*i+1] = pCursor->mask[i] & 0xff;
486     }
487
488     //add the representation (data) to the image
489     [theImage addRepresentation:theRep];
490
491     //create the new cursor
492     NSCursor* theCursor =  [[NSCursor alloc]  initWithImage:theImage
493                                     hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
494                             ];
495
496     //do the usual cleanups
497     [theRep release];
498     [theImage release];
499
500     //return the new cursor
501     return theCursor;
502 }
503
504 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
505 {
506     WX_NSCursor cursor = nil;
507     switch (cursor_type)
508     {
509     case wxCURSOR_COPY_ARROW:
510         cursor = [[NSCursor arrowCursor] retain];
511         break;
512
513     case wxCURSOR_WATCH:
514     case wxCURSOR_WAIT:
515         // should be displayed by the system when things are running
516         cursor = [[NSCursor arrowCursor] retain];
517         break;
518
519     case wxCURSOR_IBEAM:
520         cursor = [[NSCursor IBeamCursor] retain];
521         break;
522
523     case wxCURSOR_CROSS:
524         cursor = [[NSCursor crosshairCursor] retain];
525         break;
526
527     case wxCURSOR_SIZENWSE:
528         cursor = wxGetStockCursor(kwxCursorSizeNWSE);
529         break;
530
531     case wxCURSOR_SIZENESW:
532         cursor = wxGetStockCursor(kwxCursorSizeNESW);
533         break;
534
535     case wxCURSOR_SIZEWE:
536         cursor = [[NSCursor resizeLeftRightCursor] retain];
537         break;
538
539     case wxCURSOR_SIZENS:
540         cursor = [[NSCursor resizeUpDownCursor] retain];
541         break;
542
543     case wxCURSOR_SIZING:
544         cursor = wxGetStockCursor(kwxCursorSize);
545         break;
546
547     case wxCURSOR_HAND:
548         cursor = [[NSCursor pointingHandCursor] retain];
549         break;
550
551     case wxCURSOR_BULLSEYE:
552         cursor = wxGetStockCursor(kwxCursorBullseye);
553         break;
554
555     case wxCURSOR_PENCIL:
556         cursor = wxGetStockCursor(kwxCursorPencil);
557         break;
558
559     case wxCURSOR_MAGNIFIER:
560         cursor = wxGetStockCursor(kwxCursorMagnifier);
561         break;
562
563     case wxCURSOR_NO_ENTRY:
564         cursor = wxGetStockCursor(kwxCursorNoEntry);
565         break;
566
567     case wxCURSOR_PAINT_BRUSH:
568         cursor = wxGetStockCursor(kwxCursorPaintBrush);
569         break;
570
571     case wxCURSOR_POINT_LEFT:
572         cursor = wxGetStockCursor(kwxCursorPointLeft);
573         break;
574
575     case wxCURSOR_POINT_RIGHT:
576         cursor = wxGetStockCursor(kwxCursorPointRight);
577         break;
578
579     case wxCURSOR_QUESTION_ARROW:
580         cursor = wxGetStockCursor(kwxCursorQuestionArrow);
581         break;
582
583     case wxCURSOR_BLANK:
584         cursor = wxGetStockCursor(kwxCursorBlank);
585         break;
586
587     case wxCURSOR_RIGHT_ARROW:
588         cursor = wxGetStockCursor(kwxCursorRightArrow);
589         break;
590
591     case wxCURSOR_SPRAYCAN:
592         cursor = wxGetStockCursor(kwxCursorRoller);
593         break;
594
595     case wxCURSOR_OPEN_HAND:
596         cursor = [[NSCursor openHandCursor] retain];
597         break;
598
599     case wxCURSOR_CLOSED_HAND:
600         cursor = [[NSCursor closedHandCursor] retain];
601         break;
602
603     case wxCURSOR_CHAR:
604     case wxCURSOR_ARROW:
605     case wxCURSOR_LEFT_BUTTON:
606     case wxCURSOR_RIGHT_BUTTON:
607     case wxCURSOR_MIDDLE_BUTTON:
608     default:
609         cursor = [[NSCursor arrowCursor] retain];
610         break;
611     }
612     return cursor;
613 }
614
615 //  C-based style wrapper routines around NSCursor
616 WX_NSCursor  wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
617 {
618     static BOOL    firstTime  = YES;
619
620     if ( firstTime )
621     {
622         //  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
623         [[[NSWindow alloc] init] release];
624         firstTime = NO;
625     }
626
627     NSImage    *nsImage  = wxOSXGetNSImageFromCGImage( cgImageRef );
628     NSCursor  *cursor    = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
629
630     return cursor;
631 }
632
633 void  wxMacCocoaSetCursor( WX_NSCursor cursor )
634 {
635     [cursor set];
636 }
637
638 void  wxMacCocoaHideCursor()
639 {
640     [NSCursor hide];
641 }
642
643 void  wxMacCocoaShowCursor()
644 {
645     [NSCursor unhide];
646 }
647
648 #endif
649