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];
49 #if wxOSX_USE_COCOA_OR_IPHONE
51 CGContextRef wxOSXGetContextFromCurrentNSContext()
53 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
58 bool wxOSXLockFocus( WXWidget view)
60 return [view lockFocusIfCanDraw];
63 void wxOSXUnlockFocus( WXWidget view)
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 void wxMacCocoaRelease( void* obj )
76 [(NSObject*)obj release];
79 void wxMacCocoaAutorelease( void* obj )
81 [(NSObject*)obj autorelease];
84 void* wxMacCocoaRetain( void* obj )
86 [(NSObject*)obj retain];
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
96 WX_NSFont wxFont::CreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
101 case wxOSX_SYSTEM_FONT_NORMAL:
102 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
104 case wxOSX_SYSTEM_FONT_BOLD:
105 nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
107 case wxOSX_SYSTEM_FONT_SMALL:
108 nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
110 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
111 nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
113 case wxOSX_SYSTEM_FONT_MINI:
114 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
116 case wxOSX_SYSTEM_FONT_MINI_BOLD:
117 nsfont = [NSFont boldSystemFontOfSize:
118 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
120 case wxOSX_SYSTEM_FONT_LABELS:
121 nsfont = [NSFont labelFontOfSize:
122 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
124 case wxOSX_SYSTEM_FONT_VIEWS:
125 nsfont = [NSFont controlContentFontOfSize:0];
131 NSFontDescriptor*desc = [[nsfont fontDescriptor] retain];
132 if ( info->m_faceName.empty())
134 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
135 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
136 bool underlined = false;
138 int size = (int) ([desc pointSize]+0.5);
139 NSFontSymbolicTraits traits = [desc symbolicTraits];
141 if ( traits & NSFontBoldTrait )
142 fontweight = wxFONTWEIGHT_BOLD ;
144 fontweight = wxFONTWEIGHT_NORMAL ;
145 if ( traits & NSFontItalicTrait )
146 fontstyle = wxFONTSTYLE_ITALIC ;
148 wxCFStringRef fontname( [desc postscriptName] );
149 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
150 fontname.AsString(), wxFONTENCODING_DEFAULT);
153 info->m_nsFontDescriptor = desc;
157 void wxNativeFontInfo::ValidateNSFontDescriptor()
159 NSFontDescriptor* desc = [NSFontDescriptor fontDescriptorWithName:wxCFStringRef(m_faceName).AsNSString() size:m_pointSize];
160 NSFontSymbolicTraits traits = 0;
162 if (m_weight == wxFONTWEIGHT_BOLD)
163 traits |= NSFontBoldTrait;
164 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
165 traits |= NSFontItalicTrait;
169 desc = [desc fontDescriptorWithSymbolicTraits:traits];
171 wxMacCocoaRetain(desc);
172 m_nsFontDescriptor = desc;
175 WX_NSFont wxFont::CreateNSFont(const wxNativeFontInfo* info)
178 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
179 wxMacCocoaRetain(nsFont);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
191 // From "Cocoa Drawing Guide:Working with Images"
192 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
194 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
196 // Get the image dimensions.
197 imageRect.size.height = CGImageGetHeight(image);
198 imageRect.size.width = CGImageGetWidth(image);
200 // Create a new image to receive the Quartz image data.
201 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
202 [newImage lockFocus];
204 // Get the Quartz context and draw.
205 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
206 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
207 [newImage unlockFocus];
210 // Create a bitmap rep from the image...
211 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
212 // Create an NSImage and add the bitmap rep to it...
213 NSImage *image = [[NSImage alloc] init];
214 [image addRepresentation:bitmapRep];
217 [newImage autorelease];
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 // copied from cursor.mm
227 static NSCursor* wxGetStockCursor( short sIndex )
229 ClassicCursor* pCursor = &gMacCursors[sIndex];
231 //Classic mac cursors are 1bps 16x16 black and white with a
232 //identical mask that is 1 for on and 0 for off
233 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
235 //NSCursor takes an NSImage takes a number of Representations - here
236 //we need only one for the raw data
237 NSBitmapImageRep *theRep =
238 [[NSBitmapImageRep alloc]
239 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
244 hasAlpha:YES // Well, more like a mask...
246 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
247 bytesPerRow:0 // I don't care - figure it out for me :)
248 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
250 //unsigned int is better to put data in then a void*
251 //note that working with bitfields would be a lot better here -
252 //but since it breaks some compilers...
253 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
255 //traverse through the bitmap data
256 for (int i = 0; i < 16; ++i)
258 //bit alpha bit alpha ... :D
260 //Notice the = instead of |= -
261 //this is to avoid doing a memset earlier
264 //do the rest of those bits and alphas :)
265 for (int shift = 0; shift < 32; ++shift)
267 const int bit = 1 << (shift >> 1);
268 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
269 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
273 //add the representation (data) to the image
274 [theImage addRepresentation:theRep];
276 //create the new cursor
277 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
278 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
281 //do the usual cleanups
285 //return the new cursor
289 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
291 WX_NSCursor cursor = nil;
294 case wxCURSOR_COPY_ARROW:
295 cursor = [[NSCursor arrowCursor] retain];
300 // should be displayed by the system when things are running
301 cursor = [[NSCursor arrowCursor] retain];
305 cursor = [[NSCursor IBeamCursor] retain];
309 cursor = [[NSCursor crosshairCursor] retain];
312 case wxCURSOR_SIZENWSE:
313 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
316 case wxCURSOR_SIZENESW:
317 cursor = wxGetStockCursor(kwxCursorSizeNESW);
320 case wxCURSOR_SIZEWE:
321 cursor = [[NSCursor resizeLeftRightCursor] retain];
324 case wxCURSOR_SIZENS:
325 cursor = [[NSCursor resizeUpDownCursor] retain];
328 case wxCURSOR_SIZING:
329 cursor = wxGetStockCursor(kwxCursorSize);
333 cursor = [[NSCursor pointingHandCursor] retain];
336 case wxCURSOR_BULLSEYE:
337 cursor = wxGetStockCursor(kwxCursorBullseye);
340 case wxCURSOR_PENCIL:
341 cursor = wxGetStockCursor(kwxCursorPencil);
344 case wxCURSOR_MAGNIFIER:
345 cursor = wxGetStockCursor(kwxCursorMagnifier);
348 case wxCURSOR_NO_ENTRY:
349 cursor = wxGetStockCursor(kwxCursorNoEntry);
352 case wxCURSOR_PAINT_BRUSH:
353 cursor = wxGetStockCursor(kwxCursorPaintBrush);
356 case wxCURSOR_POINT_LEFT:
357 cursor = wxGetStockCursor(kwxCursorPointLeft);
360 case wxCURSOR_POINT_RIGHT:
361 cursor = wxGetStockCursor(kwxCursorPointRight);
364 case wxCURSOR_QUESTION_ARROW:
365 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
369 cursor = wxGetStockCursor(kwxCursorBlank);
372 case wxCURSOR_RIGHT_ARROW:
373 cursor = wxGetStockCursor(kwxCursorRightArrow);
376 case wxCURSOR_SPRAYCAN:
377 cursor = wxGetStockCursor(kwxCursorRoller);
380 case wxCURSOR_OPEN_HAND:
381 cursor = [[NSCursor openHandCursor] retain];
384 case wxCURSOR_CLOSED_HAND:
385 cursor = [[NSCursor closedHandCursor] retain];
390 case wxCURSOR_LEFT_BUTTON:
391 case wxCURSOR_RIGHT_BUTTON:
392 case wxCURSOR_MIDDLE_BUTTON:
394 cursor = [[NSCursor arrowCursor] retain];
400 // C-based style wrapper routines around NSCursor
401 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
403 static BOOL firstTime = YES;
407 // 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
408 [[[NSWindow alloc] init] release];
412 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
413 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
420 void wxMacCocoaSetCursor( WX_NSCursor cursor )
425 void wxMacCocoaHideCursor()
430 void wxMacCocoaShowCursor()