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"
27 #include "wx/cocoa/string.h"
35 bool cocoaLoaded = NSApplicationLoad();
36 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
41 wxMacAutoreleasePool::wxMacAutoreleasePool()
43 m_pool = [[NSAutoreleasePool alloc] init];
46 wxMacAutoreleasePool::~wxMacAutoreleasePool()
48 [(NSAutoreleasePool*)m_pool release];
55 CGContextRef wxOSXGetContextFromCurrentContext()
57 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
62 bool wxOSXLockFocus( WXWidget view)
64 return [view lockFocusIfCanDraw];
67 void wxOSXUnlockFocus( WXWidget view)
76 CGContextRef wxOSXGetContextFromCurrentContext()
78 CGContextRef context = UIGraphicsGetCurrentContext();
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 void wxMacCocoaRelease( void* obj )
90 [(NSObject*)obj release];
93 void wxMacCocoaAutorelease( void* obj )
95 [(NSObject*)obj autorelease];
98 void* wxMacCocoaRetain( void* obj )
100 [(NSObject*)obj retain];
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
109 wxFont::wxFont(WX_NSFont nsfont)
112 wxNativeFontInfo info;
113 SetNativeInfoFromNSFont(nsfont, &info);
117 void wxFont::SetNativeInfoFromNSFont(WX_NSFont theFont, wxNativeFontInfo* info)
119 if ( info->m_faceName.empty())
121 //Get more information about the user's chosen font
122 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
123 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
125 wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
126 //Set the wx font to the appropriate data
127 if(theTraits & NSFixedPitchFontMask)
128 fontFamily = wxFONTFAMILY_TELETYPE;
130 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
131 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
132 bool underlined = false;
134 int size = (int) ([theFont pointSize]+0.5);
136 if ( theFontWeight >= 9 )
137 fontweight = wxFONTWEIGHT_BOLD ;
138 else if ( theFontWeight < 5 )
139 fontweight = wxFONTWEIGHT_LIGHT;
141 fontweight = wxFONTWEIGHT_NORMAL ;
143 if ( theTraits & NSItalicFontMask )
144 fontstyle = wxFONTSTYLE_ITALIC ;
146 info->Init(size,fontFamily,fontstyle,fontweight,underlined,
147 wxStringWithNSString([theFont familyName]), wxFONTENCODING_DEFAULT);
152 WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
154 NSFont* nsfont = nil;
157 case wxOSX_SYSTEM_FONT_NORMAL:
158 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
160 case wxOSX_SYSTEM_FONT_BOLD:
161 nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
163 case wxOSX_SYSTEM_FONT_SMALL:
164 nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
166 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
167 nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
169 case wxOSX_SYSTEM_FONT_MINI:
170 nsfont = [NSFont systemFontOfSize:
171 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
173 case wxOSX_SYSTEM_FONT_MINI_BOLD:
174 nsfont = [NSFont boldSystemFontOfSize:
175 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
177 case wxOSX_SYSTEM_FONT_LABELS:
178 nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
180 case wxOSX_SYSTEM_FONT_VIEWS:
181 nsfont = [NSFont controlContentFontOfSize:0];
187 SetNativeInfoFromNSFont(nsfont, info);
191 WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
195 NSFontTraitMask traits = 0;
196 if (info->m_weight == wxFONTWEIGHT_BOLD)
198 traits |= NSBoldFontMask;
201 else if (info->m_weight == wxFONTWEIGHT_LIGHT)
204 if (info->m_style == wxFONTSTYLE_ITALIC || info->m_style == wxFONTSTYLE_SLANT)
205 traits |= NSItalicFontMask;
207 nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString()
208 traits:traits weight:weight size:info->m_pointSize];
210 wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ;
211 wxMacCocoaRetain(nsFont);
219 WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
224 case wxOSX_SYSTEM_FONT_NORMAL:
225 uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
227 case wxOSX_SYSTEM_FONT_BOLD:
228 uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
230 case wxOSX_SYSTEM_FONT_MINI:
231 case wxOSX_SYSTEM_FONT_SMALL:
232 uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
234 case wxOSX_SYSTEM_FONT_MINI_BOLD:
235 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
236 uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
238 case wxOSX_SYSTEM_FONT_VIEWS:
239 case wxOSX_SYSTEM_FONT_LABELS:
240 uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
246 if ( info->m_faceName.empty())
248 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
249 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
250 bool underlined = false;
252 int size = (int) ([uifont pointSize]+0.5);
254 NSFontSymbolicTraits traits = [desc symbolicTraits];
256 if ( traits & NSFontBoldTrait )
257 fontweight = wxFONTWEIGHT_BOLD ;
259 fontweight = wxFONTWEIGHT_NORMAL ;
260 if ( traits & NSFontItalicTrait )
261 fontstyle = wxFONTSTYLE_ITALIC ;
263 wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
264 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
265 fontname.AsString(), wxFONTENCODING_DEFAULT);
271 WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
274 uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
275 wxMacCocoaRetain(uiFont);
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
286 WX_UIImage wxOSXGetUIImageFromCGImage( CGImageRef image )
288 UIImage *newImage = [UIImage imageWithCGImage:image];
289 [newImage autorelease];
297 // From "Cocoa Drawing Guide:Working with Images"
298 WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image )
300 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
302 // Get the image dimensions.
303 imageRect.size.height = CGImageGetHeight(image);
304 imageRect.size.width = CGImageGetWidth(image);
306 // Create a new image to receive the Quartz image data.
307 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
308 [newImage lockFocus];
310 // Get the Quartz context and draw.
311 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
312 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
313 [newImage unlockFocus];
316 // Create a bitmap rep from the image...
317 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
318 // Create an NSImage and add the bitmap rep to it...
319 NSImage *image = [[NSImage alloc] init];
320 [image addRepresentation:bitmapRep];
323 [newImage autorelease];
327 CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
329 // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
331 NSSize imageSize = [nsimage size];
332 CGColorSpaceRef genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
333 CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst);
334 NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
335 [NSGraphicsContext saveGraphicsState];
336 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
337 [[NSColor whiteColor] setFill];
338 NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
339 [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
340 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
341 CGImageRef image = CGBitmapContextCreateImage(context);
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 // copied from cursor.mm
352 static NSCursor* wxGetStockCursor( short sIndex )
354 ClassicCursor* pCursor = &gMacCursors[sIndex];
356 //Classic mac cursors are 1bps 16x16 black and white with a
357 //identical mask that is 1 for on and 0 for off
358 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
360 //NSCursor takes an NSImage takes a number of Representations - here
361 //we need only one for the raw data
362 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
363 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
364 pixelsWide: 16 // All classic cursors are 16x16
366 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
367 samplesPerPixel: 2 // Sample 0:image 1:mask
368 hasAlpha: YES // Identify last sample as a mask
369 isPlanar: YES // Use a separate array for each sample
370 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
371 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
372 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
374 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
375 unsigned char *planes[5];
376 [theRep getBitmapDataPlanes:planes];
377 wxASSERT(planes[0] != NULL);
378 wxASSERT(planes[1] != NULL);
379 wxASSERT(planes[2] == NULL);
380 wxASSERT(planes[3] == NULL);
381 wxASSERT(planes[4] == NULL);
383 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
384 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
385 // sense of the alpha (mask) plane.
386 // NOTE2: The mask data is 0=off 1=on
387 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
388 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
389 // on the two. The original cursor bitmaps have 0 (white actually) for
390 // any masked-off pixels. Therefore every masked-off pixel would be wrong
391 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
392 // seem to care, but we are following the documentation.
394 // Fill in the color (black/white) plane
395 for(int i=0; i<16; ++i)
397 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
398 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
400 // Fill in the alpha (i.e. mask) plane
401 for(int i=0; i<16; ++i)
403 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
404 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
407 //add the representation (data) to the image
408 [theImage addRepresentation:theRep];
410 //create the new cursor
411 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
412 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
415 //do the usual cleanups
419 //return the new cursor
423 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
425 WX_NSCursor cursor = nil;
428 case wxCURSOR_COPY_ARROW:
429 cursor = [[NSCursor arrowCursor] retain];
434 // should be displayed by the system when things are running
435 cursor = [[NSCursor arrowCursor] retain];
439 cursor = [[NSCursor IBeamCursor] retain];
443 cursor = [[NSCursor crosshairCursor] retain];
446 case wxCURSOR_SIZENWSE:
447 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
450 case wxCURSOR_SIZENESW:
451 cursor = wxGetStockCursor(kwxCursorSizeNESW);
454 case wxCURSOR_SIZEWE:
455 cursor = [[NSCursor resizeLeftRightCursor] retain];
458 case wxCURSOR_SIZENS:
459 cursor = [[NSCursor resizeUpDownCursor] retain];
462 case wxCURSOR_SIZING:
463 cursor = wxGetStockCursor(kwxCursorSize);
467 cursor = [[NSCursor pointingHandCursor] retain];
470 case wxCURSOR_BULLSEYE:
471 cursor = wxGetStockCursor(kwxCursorBullseye);
474 case wxCURSOR_PENCIL:
475 cursor = wxGetStockCursor(kwxCursorPencil);
478 case wxCURSOR_MAGNIFIER:
479 cursor = wxGetStockCursor(kwxCursorMagnifier);
482 case wxCURSOR_NO_ENTRY:
483 cursor = wxGetStockCursor(kwxCursorNoEntry);
486 case wxCURSOR_PAINT_BRUSH:
487 cursor = wxGetStockCursor(kwxCursorPaintBrush);
490 case wxCURSOR_POINT_LEFT:
491 cursor = wxGetStockCursor(kwxCursorPointLeft);
494 case wxCURSOR_POINT_RIGHT:
495 cursor = wxGetStockCursor(kwxCursorPointRight);
498 case wxCURSOR_QUESTION_ARROW:
499 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
503 cursor = wxGetStockCursor(kwxCursorBlank);
506 case wxCURSOR_RIGHT_ARROW:
507 cursor = wxGetStockCursor(kwxCursorRightArrow);
510 case wxCURSOR_SPRAYCAN:
511 cursor = wxGetStockCursor(kwxCursorRoller);
514 case wxCURSOR_OPEN_HAND:
515 cursor = [[NSCursor openHandCursor] retain];
518 case wxCURSOR_CLOSED_HAND:
519 cursor = [[NSCursor closedHandCursor] retain];
524 case wxCURSOR_LEFT_BUTTON:
525 case wxCURSOR_RIGHT_BUTTON:
526 case wxCURSOR_MIDDLE_BUTTON:
528 cursor = [[NSCursor arrowCursor] retain];
534 // C-based style wrapper routines around NSCursor
535 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
537 static BOOL firstTime = YES;
541 // 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
542 [[[NSWindow alloc] init] release];
546 NSImage *nsImage = wxOSXGetNSImageFromCGImage( cgImageRef );
547 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
552 void wxMacCocoaSetCursor( WX_NSCursor cursor )
557 void wxMacCocoaHideCursor()
562 void wxMacCocoaShowCursor()