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];
173 wxMacCocoaRetain(desc);
175 m_nsFontDescriptor = desc;
178 WX_NSFont wxFont::CreateNSFont(const wxNativeFontInfo* info)
181 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
194 // From "Cocoa Drawing Guide:Working with Images"
195 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
197 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
199 // Get the image dimensions.
200 imageRect.size.height = CGImageGetHeight(image);
201 imageRect.size.width = CGImageGetWidth(image);
203 // Create a new image to receive the Quartz image data.
204 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
205 [newImage lockFocus];
207 // Get the Quartz context and draw.
208 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
209 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
210 [newImage unlockFocus];
213 // Create a bitmap rep from the image...
214 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
215 // Create an NSImage and add the bitmap rep to it...
216 NSImage *image = [[NSImage alloc] init];
217 [image addRepresentation:bitmapRep];
220 [newImage autorelease];
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 // copied from cursor.mm
230 static NSCursor* wxGetStockCursor( short sIndex )
232 ClassicCursor* pCursor = &gMacCursors[sIndex];
234 //Classic mac cursors are 1bps 16x16 black and white with a
235 //identical mask that is 1 for on and 0 for off
236 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
238 //NSCursor takes an NSImage takes a number of Representations - here
239 //we need only one for the raw data
240 NSBitmapImageRep *theRep =
241 [[NSBitmapImageRep alloc]
242 initWithBitmapDataPlanes:nil // Allocate the buffer for us :)
247 hasAlpha:YES // Well, more like a mask...
249 colorSpaceName:NSCalibratedWhiteColorSpace // Normal B/W - 0 black 1 white
250 bytesPerRow:0 // I don't care - figure it out for me :)
251 bitsPerPixel:2]; // bitsPerSample * samplesPerPixel
253 //unsigned int is better to put data in then a void*
254 //note that working with bitfields would be a lot better here -
255 //but since it breaks some compilers...
256 wxUint32 *data = (wxUint32 *)[theRep bitmapData];
258 //traverse through the bitmap data
259 for (int i = 0; i < 16; ++i)
261 //bit alpha bit alpha ... :D
263 //Notice the = instead of |= -
264 //this is to avoid doing a memset earlier
267 //do the rest of those bits and alphas :)
268 for (int shift = 0; shift < 32; ++shift)
270 const int bit = 1 << (shift >> 1);
271 data[i] |= ( !!( (pCursor->mask[i] & bit) ) ) << shift;
272 data[i] |= ( !( (pCursor->bits[i] & bit) ) ) << ++shift;
276 //add the representation (data) to the image
277 [theImage addRepresentation:theRep];
279 //create the new cursor
280 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
281 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
284 //do the usual cleanups
288 //return the new cursor
292 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
294 WX_NSCursor cursor = nil;
297 case wxCURSOR_COPY_ARROW:
298 cursor = [[NSCursor arrowCursor] retain];
303 // should be displayed by the system when things are running
304 cursor = [[NSCursor arrowCursor] retain];
308 cursor = [[NSCursor IBeamCursor] retain];
312 cursor = [[NSCursor crosshairCursor] retain];
315 case wxCURSOR_SIZENWSE:
316 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
319 case wxCURSOR_SIZENESW:
320 cursor = wxGetStockCursor(kwxCursorSizeNESW);
323 case wxCURSOR_SIZEWE:
324 cursor = [[NSCursor resizeLeftRightCursor] retain];
327 case wxCURSOR_SIZENS:
328 cursor = [[NSCursor resizeUpDownCursor] retain];
331 case wxCURSOR_SIZING:
332 cursor = wxGetStockCursor(kwxCursorSize);
336 cursor = [[NSCursor pointingHandCursor] retain];
339 case wxCURSOR_BULLSEYE:
340 cursor = wxGetStockCursor(kwxCursorBullseye);
343 case wxCURSOR_PENCIL:
344 cursor = wxGetStockCursor(kwxCursorPencil);
347 case wxCURSOR_MAGNIFIER:
348 cursor = wxGetStockCursor(kwxCursorMagnifier);
351 case wxCURSOR_NO_ENTRY:
352 cursor = wxGetStockCursor(kwxCursorNoEntry);
355 case wxCURSOR_PAINT_BRUSH:
356 cursor = wxGetStockCursor(kwxCursorPaintBrush);
359 case wxCURSOR_POINT_LEFT:
360 cursor = wxGetStockCursor(kwxCursorPointLeft);
363 case wxCURSOR_POINT_RIGHT:
364 cursor = wxGetStockCursor(kwxCursorPointRight);
367 case wxCURSOR_QUESTION_ARROW:
368 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
372 cursor = wxGetStockCursor(kwxCursorBlank);
375 case wxCURSOR_RIGHT_ARROW:
376 cursor = wxGetStockCursor(kwxCursorRightArrow);
379 case wxCURSOR_SPRAYCAN:
380 cursor = wxGetStockCursor(kwxCursorRoller);
383 case wxCURSOR_OPEN_HAND:
384 cursor = [[NSCursor openHandCursor] retain];
387 case wxCURSOR_CLOSED_HAND:
388 cursor = [[NSCursor closedHandCursor] retain];
393 case wxCURSOR_LEFT_BUTTON:
394 case wxCURSOR_RIGHT_BUTTON:
395 case wxCURSOR_MIDDLE_BUTTON:
397 cursor = [[NSCursor arrowCursor] retain];
403 // C-based style wrapper routines around NSCursor
404 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
406 static BOOL firstTime = YES;
410 // 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
411 [[[NSWindow alloc] init] release];
415 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
416 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
423 void wxMacCocoaSetCursor( WX_NSCursor cursor )
428 void wxMacCocoaHideCursor()
433 void wxMacCocoaShowCursor()