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( [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 // From "Cocoa Drawing Guide:Working with Images"
276 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
278 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
280 // Get the image dimensions.
281 imageRect.size.height = CGImageGetHeight(image);
282 imageRect.size.width = CGImageGetWidth(image);
284 // Create a new image to receive the Quartz image data.
285 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
286 [newImage lockFocus];
288 // Get the Quartz context and draw.
289 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
290 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
291 [newImage unlockFocus];
294 // Create a bitmap rep from the image...
295 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
296 // Create an NSImage and add the bitmap rep to it...
297 NSImage *image = [[NSImage alloc] init];
298 [image addRepresentation:bitmapRep];
301 [newImage autorelease];
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 // copied from cursor.mm
311 static NSCursor* wxGetStockCursor( short sIndex )
313 ClassicCursor* pCursor = &gMacCursors[sIndex];
315 //Classic mac cursors are 1bps 16x16 black and white with a
316 //identical mask that is 1 for on and 0 for off
317 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
319 //NSCursor takes an NSImage takes a number of Representations - here
320 //we need only one for the raw data
321 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
322 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
323 pixelsWide: 16 // All classic cursors are 16x16
325 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
326 samplesPerPixel: 2 // Sample 0:image 1:mask
327 hasAlpha: YES // Identify last sample as a mask
328 isPlanar: YES // Use a separate array for each sample
329 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
330 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
331 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
333 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
334 unsigned char *planes[5];
335 [theRep getBitmapDataPlanes:planes];
336 wxASSERT(planes[0] != NULL);
337 wxASSERT(planes[1] != NULL);
338 wxASSERT(planes[2] == NULL);
339 wxASSERT(planes[3] == NULL);
340 wxASSERT(planes[4] == NULL);
342 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
343 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
344 // sense of the alpha (mask) plane.
345 // NOTE2: The mask data is 0=off 1=on
346 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
347 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
348 // on the two. The original cursor bitmaps have 0 (white actually) for
349 // any masked-off pixels. Therefore every masked-off pixel would be wrong
350 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
351 // seem to care, but we are following the documentation.
353 // Fill in the color (black/white) plane
354 for(int i=0; i<16; ++i)
356 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
357 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
359 // Fill in the alpha (i.e. mask) plane
360 for(int i=0; i<16; ++i)
362 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
363 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
366 //add the representation (data) to the image
367 [theImage addRepresentation:theRep];
369 //create the new cursor
370 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
371 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
374 //do the usual cleanups
378 //return the new cursor
382 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
384 WX_NSCursor cursor = nil;
387 case wxCURSOR_COPY_ARROW:
388 cursor = [[NSCursor arrowCursor] retain];
393 // should be displayed by the system when things are running
394 cursor = [[NSCursor arrowCursor] retain];
398 cursor = [[NSCursor IBeamCursor] retain];
402 cursor = [[NSCursor crosshairCursor] retain];
405 case wxCURSOR_SIZENWSE:
406 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
409 case wxCURSOR_SIZENESW:
410 cursor = wxGetStockCursor(kwxCursorSizeNESW);
413 case wxCURSOR_SIZEWE:
414 cursor = [[NSCursor resizeLeftRightCursor] retain];
417 case wxCURSOR_SIZENS:
418 cursor = [[NSCursor resizeUpDownCursor] retain];
421 case wxCURSOR_SIZING:
422 cursor = wxGetStockCursor(kwxCursorSize);
426 cursor = [[NSCursor pointingHandCursor] retain];
429 case wxCURSOR_BULLSEYE:
430 cursor = wxGetStockCursor(kwxCursorBullseye);
433 case wxCURSOR_PENCIL:
434 cursor = wxGetStockCursor(kwxCursorPencil);
437 case wxCURSOR_MAGNIFIER:
438 cursor = wxGetStockCursor(kwxCursorMagnifier);
441 case wxCURSOR_NO_ENTRY:
442 cursor = wxGetStockCursor(kwxCursorNoEntry);
445 case wxCURSOR_PAINT_BRUSH:
446 cursor = wxGetStockCursor(kwxCursorPaintBrush);
449 case wxCURSOR_POINT_LEFT:
450 cursor = wxGetStockCursor(kwxCursorPointLeft);
453 case wxCURSOR_POINT_RIGHT:
454 cursor = wxGetStockCursor(kwxCursorPointRight);
457 case wxCURSOR_QUESTION_ARROW:
458 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
462 cursor = wxGetStockCursor(kwxCursorBlank);
465 case wxCURSOR_RIGHT_ARROW:
466 cursor = wxGetStockCursor(kwxCursorRightArrow);
469 case wxCURSOR_SPRAYCAN:
470 cursor = wxGetStockCursor(kwxCursorRoller);
473 case wxCURSOR_OPEN_HAND:
474 cursor = [[NSCursor openHandCursor] retain];
477 case wxCURSOR_CLOSED_HAND:
478 cursor = [[NSCursor closedHandCursor] retain];
483 case wxCURSOR_LEFT_BUTTON:
484 case wxCURSOR_RIGHT_BUTTON:
485 case wxCURSOR_MIDDLE_BUTTON:
487 cursor = [[NSCursor arrowCursor] retain];
493 // C-based style wrapper routines around NSCursor
494 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
496 static BOOL firstTime = YES;
500 // 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
501 [[[NSWindow alloc] init] release];
505 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
506 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
513 void wxMacCocoaSetCursor( WX_NSCursor cursor )
518 void wxMacCocoaHideCursor()
523 void wxMacCocoaShowCursor()