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"
24 #include "wx/fontutil.h"
31 bool cocoaLoaded = NSApplicationLoad();
32 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
37 wxMacAutoreleasePool::wxMacAutoreleasePool()
39 m_pool = [[NSAutoreleasePool alloc] init];
42 wxMacAutoreleasePool::~wxMacAutoreleasePool()
44 [(NSAutoreleasePool*)m_pool release];
51 CGContextRef wxOSXGetContextFromCurrentContext()
53 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
58 bool wxOSXLockFocus( WXWidget view)
60 return [view lockFocusIfCanDraw];
63 void wxOSXUnlockFocus( WXWidget view)
72 CGContextRef wxOSXGetContextFromCurrentContext()
74 CGContextRef context = UIGraphicsGetCurrentContext();
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 void wxMacCocoaRelease( void* obj )
86 [(NSObject*)obj release];
89 void wxMacCocoaAutorelease( void* obj )
91 [(NSObject*)obj autorelease];
94 void* wxMacCocoaRetain( void* obj )
96 [(NSObject*)obj retain];
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
106 WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
108 NSFont* nsfont = nil;
111 case wxOSX_SYSTEM_FONT_NORMAL:
112 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
114 case wxOSX_SYSTEM_FONT_BOLD:
115 nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
117 case wxOSX_SYSTEM_FONT_SMALL:
118 nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
120 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
121 nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
123 case wxOSX_SYSTEM_FONT_MINI:
124 nsfont = [NSFont systemFontOfSize:
125 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
127 case wxOSX_SYSTEM_FONT_MINI_BOLD:
128 nsfont = [NSFont boldSystemFontOfSize:
129 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
131 case wxOSX_SYSTEM_FONT_LABELS:
132 nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
134 case wxOSX_SYSTEM_FONT_VIEWS:
135 nsfont = [NSFont controlContentFontOfSize:0];
141 NSFontDescriptor*desc = [[nsfont fontDescriptor] retain];
142 if ( info->m_faceName.empty())
144 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
145 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
146 bool underlined = false;
148 int size = (int) ([desc pointSize]+0.5);
149 NSFontSymbolicTraits traits = [desc symbolicTraits];
151 if ( traits & NSFontBoldTrait )
152 fontweight = wxFONTWEIGHT_BOLD ;
154 fontweight = wxFONTWEIGHT_NORMAL ;
155 if ( traits & NSFontItalicTrait )
156 fontstyle = wxFONTSTYLE_ITALIC ;
158 wxCFStringRef fontname( [desc postscriptName] );
159 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
160 fontname.AsString(), wxFONTENCODING_DEFAULT);
163 info->m_nsFontDescriptor = desc;
167 void wxNativeFontInfo::OSXValidateNSFontDescriptor()
169 NSFontDescriptor* desc = nil;
170 NSFontSymbolicTraits traits = 0;
173 if (m_weight == wxFONTWEIGHT_BOLD)
175 traits |= NSFontBoldTrait;
178 else if (m_weight == wxFONTWEIGHT_LIGHT)
181 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
182 traits |= NSFontItalicTrait;
184 desc = [NSFontDescriptor fontDescriptorWithFontAttributes:
185 [[NSDictionary alloc] initWithObjectsAndKeys:
186 wxCFStringRef(m_faceName).AsNSString(), NSFontFamilyAttribute,
187 [NSNumber numberWithFloat:m_pointSize], NSFontSizeAttribute,
188 [NSNumber numberWithUnsignedInt:traits], NSFontSymbolicTrait,
189 [NSNumber numberWithFloat:weight],NSFontWeightTrait,
192 wxMacCocoaRetain(desc);
193 m_nsFontDescriptor = desc;
196 WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
199 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
200 wxMacCocoaRetain(nsFont);
208 WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
213 case wxOSX_SYSTEM_FONT_NORMAL:
214 uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
216 case wxOSX_SYSTEM_FONT_BOLD:
217 uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
219 case wxOSX_SYSTEM_FONT_MINI:
220 case wxOSX_SYSTEM_FONT_SMALL:
221 uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
223 case wxOSX_SYSTEM_FONT_MINI_BOLD:
224 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
225 uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
227 case wxOSX_SYSTEM_FONT_VIEWS:
228 case wxOSX_SYSTEM_FONT_LABELS:
229 uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
235 if ( info->m_faceName.empty())
237 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
238 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
239 bool underlined = false;
241 int size = (int) ([uifont pointSize]+0.5);
243 NSFontSymbolicTraits traits = [desc symbolicTraits];
245 if ( traits & NSFontBoldTrait )
246 fontweight = wxFONTWEIGHT_BOLD ;
248 fontweight = wxFONTWEIGHT_NORMAL ;
249 if ( traits & NSFontItalicTrait )
250 fontstyle = wxFONTSTYLE_ITALIC ;
252 wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
253 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
254 fontname.AsString(), wxFONTENCODING_DEFAULT);
260 WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
263 uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
264 wxMacCocoaRetain(uiFont);
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
275 WX_UIImage wxOSXCreateUIImageFromCGImage( CGImageRef image )
277 UIImage *newImage = [UIImage imageWithCGImage:image];
278 [newImage autorelease];
286 // From "Cocoa Drawing Guide:Working with Images"
287 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
289 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
291 // Get the image dimensions.
292 imageRect.size.height = CGImageGetHeight(image);
293 imageRect.size.width = CGImageGetWidth(image);
295 // Create a new image to receive the Quartz image data.
296 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
297 [newImage lockFocus];
299 // Get the Quartz context and draw.
300 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
301 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
302 [newImage unlockFocus];
305 // Create a bitmap rep from the image...
306 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
307 // Create an NSImage and add the bitmap rep to it...
308 NSImage *image = [[NSImage alloc] init];
309 [image addRepresentation:bitmapRep];
312 [newImage autorelease];
316 CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
318 // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
320 NSSize imageSize = [nsimage size];
321 CGColorSpaceRef genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
322 CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst);
323 NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
324 [NSGraphicsContext saveGraphicsState];
325 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
326 [[NSColor whiteColor] setFill];
327 NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
328 [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
329 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
330 CGImageRef image = CGBitmapContextCreateImage(context);
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 // copied from cursor.mm
341 static NSCursor* wxGetStockCursor( short sIndex )
343 ClassicCursor* pCursor = &gMacCursors[sIndex];
345 //Classic mac cursors are 1bps 16x16 black and white with a
346 //identical mask that is 1 for on and 0 for off
347 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
349 //NSCursor takes an NSImage takes a number of Representations - here
350 //we need only one for the raw data
351 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
352 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
353 pixelsWide: 16 // All classic cursors are 16x16
355 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
356 samplesPerPixel: 2 // Sample 0:image 1:mask
357 hasAlpha: YES // Identify last sample as a mask
358 isPlanar: YES // Use a separate array for each sample
359 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
360 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
361 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
363 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
364 unsigned char *planes[5];
365 [theRep getBitmapDataPlanes:planes];
366 wxASSERT(planes[0] != NULL);
367 wxASSERT(planes[1] != NULL);
368 wxASSERT(planes[2] == NULL);
369 wxASSERT(planes[3] == NULL);
370 wxASSERT(planes[4] == NULL);
372 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
373 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
374 // sense of the alpha (mask) plane.
375 // NOTE2: The mask data is 0=off 1=on
376 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
377 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
378 // on the two. The original cursor bitmaps have 0 (white actually) for
379 // any masked-off pixels. Therefore every masked-off pixel would be wrong
380 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
381 // seem to care, but we are following the documentation.
383 // Fill in the color (black/white) plane
384 for(int i=0; i<16; ++i)
386 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
387 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
389 // Fill in the alpha (i.e. mask) plane
390 for(int i=0; i<16; ++i)
392 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
393 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
396 //add the representation (data) to the image
397 [theImage addRepresentation:theRep];
399 //create the new cursor
400 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
401 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
404 //do the usual cleanups
408 //return the new cursor
412 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
414 WX_NSCursor cursor = nil;
417 case wxCURSOR_COPY_ARROW:
418 cursor = [[NSCursor arrowCursor] retain];
423 // should be displayed by the system when things are running
424 cursor = [[NSCursor arrowCursor] retain];
428 cursor = [[NSCursor IBeamCursor] retain];
432 cursor = [[NSCursor crosshairCursor] retain];
435 case wxCURSOR_SIZENWSE:
436 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
439 case wxCURSOR_SIZENESW:
440 cursor = wxGetStockCursor(kwxCursorSizeNESW);
443 case wxCURSOR_SIZEWE:
444 cursor = [[NSCursor resizeLeftRightCursor] retain];
447 case wxCURSOR_SIZENS:
448 cursor = [[NSCursor resizeUpDownCursor] retain];
451 case wxCURSOR_SIZING:
452 cursor = wxGetStockCursor(kwxCursorSize);
456 cursor = [[NSCursor pointingHandCursor] retain];
459 case wxCURSOR_BULLSEYE:
460 cursor = wxGetStockCursor(kwxCursorBullseye);
463 case wxCURSOR_PENCIL:
464 cursor = wxGetStockCursor(kwxCursorPencil);
467 case wxCURSOR_MAGNIFIER:
468 cursor = wxGetStockCursor(kwxCursorMagnifier);
471 case wxCURSOR_NO_ENTRY:
472 cursor = wxGetStockCursor(kwxCursorNoEntry);
475 case wxCURSOR_PAINT_BRUSH:
476 cursor = wxGetStockCursor(kwxCursorPaintBrush);
479 case wxCURSOR_POINT_LEFT:
480 cursor = wxGetStockCursor(kwxCursorPointLeft);
483 case wxCURSOR_POINT_RIGHT:
484 cursor = wxGetStockCursor(kwxCursorPointRight);
487 case wxCURSOR_QUESTION_ARROW:
488 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
492 cursor = wxGetStockCursor(kwxCursorBlank);
495 case wxCURSOR_RIGHT_ARROW:
496 cursor = wxGetStockCursor(kwxCursorRightArrow);
499 case wxCURSOR_SPRAYCAN:
500 cursor = wxGetStockCursor(kwxCursorRoller);
503 case wxCURSOR_OPEN_HAND:
504 cursor = [[NSCursor openHandCursor] retain];
507 case wxCURSOR_CLOSED_HAND:
508 cursor = [[NSCursor closedHandCursor] retain];
513 case wxCURSOR_LEFT_BUTTON:
514 case wxCURSOR_RIGHT_BUTTON:
515 case wxCURSOR_MIDDLE_BUTTON:
517 cursor = [[NSCursor arrowCursor] retain];
523 // C-based style wrapper routines around NSCursor
524 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
526 static BOOL firstTime = YES;
530 // 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
531 [[[NSWindow alloc] init] release];
535 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
536 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
543 void wxMacCocoaSetCursor( WX_NSCursor cursor )
548 void wxMacCocoaHideCursor()
553 void wxMacCocoaShowCursor()