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 // ----------------------------------------------------------------------------
105 wxFont::wxFont(WX_NSFont nsfont)
108 wxNativeFontInfo info;
109 SetNativeInfoFromNSFont(nsfont, &info);
113 void wxFont::SetNativeInfoFromNSFont(WX_NSFont nsfont, wxNativeFontInfo* info)
115 NSFontDescriptor*desc = [[nsfont fontDescriptor] retain];
116 if ( info->m_faceName.empty())
118 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
119 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
120 bool underlined = false;
122 int size = (int) ([desc pointSize]+0.5);
123 NSFontSymbolicTraits traits = [desc symbolicTraits];
125 if ( traits & NSFontBoldTrait )
126 fontweight = wxFONTWEIGHT_BOLD ;
128 fontweight = wxFONTWEIGHT_NORMAL ;
129 if ( traits & NSFontItalicTrait )
130 fontstyle = wxFONTSTYLE_ITALIC ;
132 wxCFStringRef fontname( [desc postscriptName] );
133 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
134 fontname.AsString(), wxFONTENCODING_DEFAULT);
137 info->m_nsFontDescriptor = desc;
140 WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
142 NSFont* nsfont = nil;
145 case wxOSX_SYSTEM_FONT_NORMAL:
146 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
148 case wxOSX_SYSTEM_FONT_BOLD:
149 nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
151 case wxOSX_SYSTEM_FONT_SMALL:
152 nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
154 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
155 nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
157 case wxOSX_SYSTEM_FONT_MINI:
158 nsfont = [NSFont systemFontOfSize:
159 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
161 case wxOSX_SYSTEM_FONT_MINI_BOLD:
162 nsfont = [NSFont boldSystemFontOfSize:
163 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
165 case wxOSX_SYSTEM_FONT_LABELS:
166 nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
168 case wxOSX_SYSTEM_FONT_VIEWS:
169 nsfont = [NSFont controlContentFontOfSize:0];
175 SetNativeInfoFromNSFont(nsfont, info);
179 void wxNativeFontInfo::OSXValidateNSFontDescriptor()
181 NSFontDescriptor* desc = nil;
182 NSFontSymbolicTraits traits = 0;
185 if (m_weight == wxFONTWEIGHT_BOLD)
187 traits |= NSFontBoldTrait;
190 else if (m_weight == wxFONTWEIGHT_LIGHT)
193 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
194 traits |= NSFontItalicTrait;
196 desc = [NSFontDescriptor fontDescriptorWithFontAttributes:
197 [[NSDictionary alloc] initWithObjectsAndKeys:
198 wxCFStringRef(m_faceName).AsNSString(), NSFontFamilyAttribute,
199 [NSNumber numberWithFloat:m_pointSize], NSFontSizeAttribute,
200 [NSNumber numberWithUnsignedInt:traits], NSFontSymbolicTrait,
201 [NSNumber numberWithFloat:weight],NSFontWeightTrait,
204 wxMacCocoaRetain(desc);
205 m_nsFontDescriptor = desc;
208 WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
211 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
212 wxMacCocoaRetain(nsFont);
220 WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
225 case wxOSX_SYSTEM_FONT_NORMAL:
226 uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
228 case wxOSX_SYSTEM_FONT_BOLD:
229 uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
231 case wxOSX_SYSTEM_FONT_MINI:
232 case wxOSX_SYSTEM_FONT_SMALL:
233 uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
235 case wxOSX_SYSTEM_FONT_MINI_BOLD:
236 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
237 uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
239 case wxOSX_SYSTEM_FONT_VIEWS:
240 case wxOSX_SYSTEM_FONT_LABELS:
241 uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
247 if ( info->m_faceName.empty())
249 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
250 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
251 bool underlined = false;
253 int size = (int) ([uifont pointSize]+0.5);
255 NSFontSymbolicTraits traits = [desc symbolicTraits];
257 if ( traits & NSFontBoldTrait )
258 fontweight = wxFONTWEIGHT_BOLD ;
260 fontweight = wxFONTWEIGHT_NORMAL ;
261 if ( traits & NSFontItalicTrait )
262 fontstyle = wxFONTSTYLE_ITALIC ;
264 wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
265 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
266 fontname.AsString(), wxFONTENCODING_DEFAULT);
272 WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
275 uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
276 wxMacCocoaRetain(uiFont);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
287 WX_UIImage wxOSXCreateUIImageFromCGImage( CGImageRef image )
289 UIImage *newImage = [UIImage imageWithCGImage:image];
290 [newImage autorelease];
298 // From "Cocoa Drawing Guide:Working with Images"
299 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
301 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
303 // Get the image dimensions.
304 imageRect.size.height = CGImageGetHeight(image);
305 imageRect.size.width = CGImageGetWidth(image);
307 // Create a new image to receive the Quartz image data.
308 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
309 [newImage lockFocus];
311 // Get the Quartz context and draw.
312 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
313 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
314 [newImage unlockFocus];
317 // Create a bitmap rep from the image...
318 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
319 // Create an NSImage and add the bitmap rep to it...
320 NSImage *image = [[NSImage alloc] init];
321 [image addRepresentation:bitmapRep];
324 [newImage autorelease];
328 CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
330 // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
332 NSSize imageSize = [nsimage size];
333 CGColorSpaceRef genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
334 CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst);
335 NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
336 [NSGraphicsContext saveGraphicsState];
337 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
338 [[NSColor whiteColor] setFill];
339 NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
340 [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
341 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
342 CGImageRef image = CGBitmapContextCreateImage(context);
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 // copied from cursor.mm
353 static NSCursor* wxGetStockCursor( short sIndex )
355 ClassicCursor* pCursor = &gMacCursors[sIndex];
357 //Classic mac cursors are 1bps 16x16 black and white with a
358 //identical mask that is 1 for on and 0 for off
359 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
361 //NSCursor takes an NSImage takes a number of Representations - here
362 //we need only one for the raw data
363 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
364 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
365 pixelsWide: 16 // All classic cursors are 16x16
367 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
368 samplesPerPixel: 2 // Sample 0:image 1:mask
369 hasAlpha: YES // Identify last sample as a mask
370 isPlanar: YES // Use a separate array for each sample
371 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
372 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
373 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
375 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
376 unsigned char *planes[5];
377 [theRep getBitmapDataPlanes:planes];
378 wxASSERT(planes[0] != NULL);
379 wxASSERT(planes[1] != NULL);
380 wxASSERT(planes[2] == NULL);
381 wxASSERT(planes[3] == NULL);
382 wxASSERT(planes[4] == NULL);
384 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
385 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
386 // sense of the alpha (mask) plane.
387 // NOTE2: The mask data is 0=off 1=on
388 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
389 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
390 // on the two. The original cursor bitmaps have 0 (white actually) for
391 // any masked-off pixels. Therefore every masked-off pixel would be wrong
392 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
393 // seem to care, but we are following the documentation.
395 // Fill in the color (black/white) plane
396 for(int i=0; i<16; ++i)
398 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
399 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
401 // Fill in the alpha (i.e. mask) plane
402 for(int i=0; i<16; ++i)
404 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
405 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
408 //add the representation (data) to the image
409 [theImage addRepresentation:theRep];
411 //create the new cursor
412 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
413 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
416 //do the usual cleanups
420 //return the new cursor
424 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
426 WX_NSCursor cursor = nil;
429 case wxCURSOR_COPY_ARROW:
430 cursor = [[NSCursor arrowCursor] retain];
435 // should be displayed by the system when things are running
436 cursor = [[NSCursor arrowCursor] retain];
440 cursor = [[NSCursor IBeamCursor] retain];
444 cursor = [[NSCursor crosshairCursor] retain];
447 case wxCURSOR_SIZENWSE:
448 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
451 case wxCURSOR_SIZENESW:
452 cursor = wxGetStockCursor(kwxCursorSizeNESW);
455 case wxCURSOR_SIZEWE:
456 cursor = [[NSCursor resizeLeftRightCursor] retain];
459 case wxCURSOR_SIZENS:
460 cursor = [[NSCursor resizeUpDownCursor] retain];
463 case wxCURSOR_SIZING:
464 cursor = wxGetStockCursor(kwxCursorSize);
468 cursor = [[NSCursor pointingHandCursor] retain];
471 case wxCURSOR_BULLSEYE:
472 cursor = wxGetStockCursor(kwxCursorBullseye);
475 case wxCURSOR_PENCIL:
476 cursor = wxGetStockCursor(kwxCursorPencil);
479 case wxCURSOR_MAGNIFIER:
480 cursor = wxGetStockCursor(kwxCursorMagnifier);
483 case wxCURSOR_NO_ENTRY:
484 cursor = wxGetStockCursor(kwxCursorNoEntry);
487 case wxCURSOR_PAINT_BRUSH:
488 cursor = wxGetStockCursor(kwxCursorPaintBrush);
491 case wxCURSOR_POINT_LEFT:
492 cursor = wxGetStockCursor(kwxCursorPointLeft);
495 case wxCURSOR_POINT_RIGHT:
496 cursor = wxGetStockCursor(kwxCursorPointRight);
499 case wxCURSOR_QUESTION_ARROW:
500 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
504 cursor = wxGetStockCursor(kwxCursorBlank);
507 case wxCURSOR_RIGHT_ARROW:
508 cursor = wxGetStockCursor(kwxCursorRightArrow);
511 case wxCURSOR_SPRAYCAN:
512 cursor = wxGetStockCursor(kwxCursorRoller);
515 case wxCURSOR_OPEN_HAND:
516 cursor = [[NSCursor openHandCursor] retain];
519 case wxCURSOR_CLOSED_HAND:
520 cursor = [[NSCursor closedHandCursor] retain];
525 case wxCURSOR_LEFT_BUTTON:
526 case wxCURSOR_RIGHT_BUTTON:
527 case wxCURSOR_MIDDLE_BUTTON:
529 cursor = [[NSCursor arrowCursor] retain];
535 // C-based style wrapper routines around NSCursor
536 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
538 static BOOL firstTime = YES;
542 // 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
543 [[[NSWindow alloc] init] release];
547 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
548 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
555 void wxMacCocoaSetCursor( WX_NSCursor cursor )
560 void wxMacCocoaHideCursor()
565 void wxMacCocoaShowCursor()