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 = [NSFontDescriptor fontDescriptorWithName:wxCFStringRef(m_faceName).AsNSString() size:m_pointSize];
170 NSFontSymbolicTraits traits = 0;
172 if (m_weight == wxFONTWEIGHT_BOLD)
173 traits |= NSFontBoldTrait;
174 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
175 traits |= NSFontItalicTrait;
179 desc = [desc fontDescriptorWithSymbolicTraits:traits];
181 wxMacCocoaRetain(desc);
182 m_nsFontDescriptor = desc;
185 WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
188 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
189 wxMacCocoaRetain(nsFont);
197 WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
202 case wxOSX_SYSTEM_FONT_NORMAL:
203 uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
205 case wxOSX_SYSTEM_FONT_BOLD:
206 uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
208 case wxOSX_SYSTEM_FONT_MINI:
209 case wxOSX_SYSTEM_FONT_SMALL:
210 uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
212 case wxOSX_SYSTEM_FONT_MINI_BOLD:
213 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
214 uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
216 case wxOSX_SYSTEM_FONT_VIEWS:
217 case wxOSX_SYSTEM_FONT_LABELS:
218 uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
224 if ( info->m_faceName.empty())
226 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
227 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
228 bool underlined = false;
230 int size = (int) ([uifont pointSize]+0.5);
232 NSFontSymbolicTraits traits = [desc symbolicTraits];
234 if ( traits & NSFontBoldTrait )
235 fontweight = wxFONTWEIGHT_BOLD ;
237 fontweight = wxFONTWEIGHT_NORMAL ;
238 if ( traits & NSFontItalicTrait )
239 fontstyle = wxFONTSTYLE_ITALIC ;
241 wxCFStringRef fontname( [uifont familyName] );
242 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
243 fontname.AsString(), wxFONTENCODING_DEFAULT);
249 WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
252 uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
253 wxMacCocoaRetain(uiFont);
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
264 // From "Cocoa Drawing Guide:Working with Images"
265 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
267 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
269 // Get the image dimensions.
270 imageRect.size.height = CGImageGetHeight(image);
271 imageRect.size.width = CGImageGetWidth(image);
273 // Create a new image to receive the Quartz image data.
274 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
275 [newImage lockFocus];
277 // Get the Quartz context and draw.
278 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
279 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
280 [newImage unlockFocus];
283 // Create a bitmap rep from the image...
284 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
285 // Create an NSImage and add the bitmap rep to it...
286 NSImage *image = [[NSImage alloc] init];
287 [image addRepresentation:bitmapRep];
290 [newImage autorelease];
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 // copied from cursor.mm
300 static NSCursor* wxGetStockCursor( short sIndex )
302 ClassicCursor* pCursor = &gMacCursors[sIndex];
304 //Classic mac cursors are 1bps 16x16 black and white with a
305 //identical mask that is 1 for on and 0 for off
306 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
308 //NSCursor takes an NSImage takes a number of Representations - here
309 //we need only one for the raw data
310 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
311 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
312 pixelsWide: 16 // All classic cursors are 16x16
314 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
315 samplesPerPixel: 2 // Sample 0:image 1:mask
316 hasAlpha: YES // Identify last sample as a mask
317 isPlanar: YES // Use a separate array for each sample
318 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
319 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
320 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
322 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
323 unsigned char *planes[5];
324 [theRep getBitmapDataPlanes:planes];
325 wxASSERT(planes[0] != NULL);
326 wxASSERT(planes[1] != NULL);
327 wxASSERT(planes[2] == NULL);
328 wxASSERT(planes[3] == NULL);
329 wxASSERT(planes[4] == NULL);
331 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
332 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
333 // sense of the alpha (mask) plane.
334 // NOTE2: The mask data is 0=off 1=on
335 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
336 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
337 // on the two. The original cursor bitmaps have 0 (white actually) for
338 // any masked-off pixels. Therefore every masked-off pixel would be wrong
339 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
340 // seem to care, but we are following the documentation.
342 // Fill in the color (black/white) plane
343 for(int i=0; i<16; ++i)
345 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
346 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
348 // Fill in the alpha (i.e. mask) plane
349 for(int i=0; i<16; ++i)
351 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
352 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
355 //add the representation (data) to the image
356 [theImage addRepresentation:theRep];
358 //create the new cursor
359 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
360 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
363 //do the usual cleanups
367 //return the new cursor
371 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
373 WX_NSCursor cursor = nil;
376 case wxCURSOR_COPY_ARROW:
377 cursor = [[NSCursor arrowCursor] retain];
382 // should be displayed by the system when things are running
383 cursor = [[NSCursor arrowCursor] retain];
387 cursor = [[NSCursor IBeamCursor] retain];
391 cursor = [[NSCursor crosshairCursor] retain];
394 case wxCURSOR_SIZENWSE:
395 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
398 case wxCURSOR_SIZENESW:
399 cursor = wxGetStockCursor(kwxCursorSizeNESW);
402 case wxCURSOR_SIZEWE:
403 cursor = [[NSCursor resizeLeftRightCursor] retain];
406 case wxCURSOR_SIZENS:
407 cursor = [[NSCursor resizeUpDownCursor] retain];
410 case wxCURSOR_SIZING:
411 cursor = wxGetStockCursor(kwxCursorSize);
415 cursor = [[NSCursor pointingHandCursor] retain];
418 case wxCURSOR_BULLSEYE:
419 cursor = wxGetStockCursor(kwxCursorBullseye);
422 case wxCURSOR_PENCIL:
423 cursor = wxGetStockCursor(kwxCursorPencil);
426 case wxCURSOR_MAGNIFIER:
427 cursor = wxGetStockCursor(kwxCursorMagnifier);
430 case wxCURSOR_NO_ENTRY:
431 cursor = wxGetStockCursor(kwxCursorNoEntry);
434 case wxCURSOR_PAINT_BRUSH:
435 cursor = wxGetStockCursor(kwxCursorPaintBrush);
438 case wxCURSOR_POINT_LEFT:
439 cursor = wxGetStockCursor(kwxCursorPointLeft);
442 case wxCURSOR_POINT_RIGHT:
443 cursor = wxGetStockCursor(kwxCursorPointRight);
446 case wxCURSOR_QUESTION_ARROW:
447 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
451 cursor = wxGetStockCursor(kwxCursorBlank);
454 case wxCURSOR_RIGHT_ARROW:
455 cursor = wxGetStockCursor(kwxCursorRightArrow);
458 case wxCURSOR_SPRAYCAN:
459 cursor = wxGetStockCursor(kwxCursorRoller);
462 case wxCURSOR_OPEN_HAND:
463 cursor = [[NSCursor openHandCursor] retain];
466 case wxCURSOR_CLOSED_HAND:
467 cursor = [[NSCursor closedHandCursor] retain];
472 case wxCURSOR_LEFT_BUTTON:
473 case wxCURSOR_RIGHT_BUTTON:
474 case wxCURSOR_MIDDLE_BUTTON:
476 cursor = [[NSCursor arrowCursor] retain];
482 // C-based style wrapper routines around NSCursor
483 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
485 static BOOL firstTime = YES;
489 // 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
490 [[[NSWindow alloc] init] release];
494 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
495 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
502 void wxMacCocoaSetCursor( WX_NSCursor cursor )
507 void wxMacCocoaHideCursor()
512 void wxMacCocoaShowCursor()