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