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