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 =
311 [[NSBitmapImageRep alloc]
312 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
317 hasAlpha:YES // Well, more like a mask...
319 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
320 bytesPerRow:0 // I don't care - figure it out for me :)
321 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
323 //unsigned int is better to put data in then a void*
324 //note that working with bitfields would be a lot better here -
325 //but since it breaks some compilers...
326 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
328 //traverse through the bitmap data
329 for (int i = 0; i < 16; ++i)
331 //bit alpha bit alpha ... :D
333 //Notice the = instead of |= -
334 //this is to avoid doing a memset earlier
337 //do the rest of those bits and alphas :)
338 for (int shift = 0; shift < 32; ++shift)
340 const int bit = 1 << (shift >> 1);
341 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
342 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
346 //add the representation (data) to the image
347 [theImage addRepresentation:theRep];
349 //create the new cursor
350 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
351 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
354 //do the usual cleanups
358 //return the new cursor
362 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
364 WX_NSCursor cursor = nil;
367 case wxCURSOR_COPY_ARROW:
368 cursor = [[NSCursor arrowCursor] retain];
373 // should be displayed by the system when things are running
374 cursor = [[NSCursor arrowCursor] retain];
378 cursor = [[NSCursor IBeamCursor] retain];
382 cursor = [[NSCursor crosshairCursor] retain];
385 case wxCURSOR_SIZENWSE:
386 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
389 case wxCURSOR_SIZENESW:
390 cursor = wxGetStockCursor(kwxCursorSizeNESW);
393 case wxCURSOR_SIZEWE:
394 cursor = [[NSCursor resizeLeftRightCursor] retain];
397 case wxCURSOR_SIZENS:
398 cursor = [[NSCursor resizeUpDownCursor] retain];
401 case wxCURSOR_SIZING:
402 cursor = wxGetStockCursor(kwxCursorSize);
406 cursor = [[NSCursor pointingHandCursor] retain];
409 case wxCURSOR_BULLSEYE:
410 cursor = wxGetStockCursor(kwxCursorBullseye);
413 case wxCURSOR_PENCIL:
414 cursor = wxGetStockCursor(kwxCursorPencil);
417 case wxCURSOR_MAGNIFIER:
418 cursor = wxGetStockCursor(kwxCursorMagnifier);
421 case wxCURSOR_NO_ENTRY:
422 cursor = wxGetStockCursor(kwxCursorNoEntry);
425 case wxCURSOR_PAINT_BRUSH:
426 cursor = wxGetStockCursor(kwxCursorPaintBrush);
429 case wxCURSOR_POINT_LEFT:
430 cursor = wxGetStockCursor(kwxCursorPointLeft);
433 case wxCURSOR_POINT_RIGHT:
434 cursor = wxGetStockCursor(kwxCursorPointRight);
437 case wxCURSOR_QUESTION_ARROW:
438 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
442 cursor = wxGetStockCursor(kwxCursorBlank);
445 case wxCURSOR_RIGHT_ARROW:
446 cursor = wxGetStockCursor(kwxCursorRightArrow);
449 case wxCURSOR_SPRAYCAN:
450 cursor = wxGetStockCursor(kwxCursorRoller);
453 case wxCURSOR_OPEN_HAND:
454 cursor = [[NSCursor openHandCursor] retain];
457 case wxCURSOR_CLOSED_HAND:
458 cursor = [[NSCursor closedHandCursor] retain];
463 case wxCURSOR_LEFT_BUTTON:
464 case wxCURSOR_RIGHT_BUTTON:
465 case wxCURSOR_MIDDLE_BUTTON:
467 cursor = [[NSCursor arrowCursor] retain];
473 // C-based style wrapper routines around NSCursor
474 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
476 static BOOL firstTime = YES;
480 // 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
481 [[[NSWindow alloc] init] release];
485 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
486 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
493 void wxMacCocoaSetCursor( WX_NSCursor cursor )
498 void wxMacCocoaHideCursor()
503 void wxMacCocoaShowCursor()