]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/utilscocoa.mm
Suppress harmless warning about unhandled wxSHOW_EFFECT_NONE.
[wxWidgets.git] / src / osx / carbon / utilscocoa.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/utils.mm
3 // Purpose: various cocoa mixin utility functions
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: utilscocoa.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxOSX_USE_COCOA_OR_CARBON
15 #include <Cocoa/Cocoa.h>
16 #else
17 #import <UIKit/UIKit.h>
18 #endif
19
20 #ifdef __WXMAC__
21 #include "wx/osx/private.h"
22 #endif
23
24 #include "wx/fontutil.h"
25
26 #ifdef __WXMAC__
27
28 #if wxOSX_USE_CARBON
29 bool wxMacInitCocoa()
30 {
31 bool cocoaLoaded = NSApplicationLoad();
32 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
33 return cocoaLoaded;
34 }
35 #endif
36
37 wxMacAutoreleasePool::wxMacAutoreleasePool()
38 {
39 m_pool = [[NSAutoreleasePool alloc] init];
40 }
41
42 wxMacAutoreleasePool::~wxMacAutoreleasePool()
43 {
44 [(NSAutoreleasePool*)m_pool release];
45 }
46
47 #endif
48
49 #if wxOSX_USE_COCOA
50
51 CGContextRef wxOSXGetContextFromCurrentContext()
52 {
53 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
54 graphicsPort];
55 return context;
56 }
57
58 bool wxOSXLockFocus( WXWidget view)
59 {
60 return [view lockFocusIfCanDraw];
61 }
62
63 void wxOSXUnlockFocus( WXWidget view)
64 {
65 [view unlockFocus];
66 }
67
68 #endif
69
70 #if wxOSX_USE_IPHONE
71
72 CGContextRef wxOSXGetContextFromCurrentContext()
73 {
74 CGContextRef context = UIGraphicsGetCurrentContext();
75 return context;
76 }
77
78 #endif
79
80 // ----------------------------------------------------------------------------
81 // NSObject Utils
82 // ----------------------------------------------------------------------------
83
84 void wxMacCocoaRelease( void* obj )
85 {
86 [(NSObject*)obj release];
87 }
88
89 void wxMacCocoaAutorelease( void* obj )
90 {
91 [(NSObject*)obj autorelease];
92 }
93
94 void* wxMacCocoaRetain( void* obj )
95 {
96 [(NSObject*)obj retain];
97 return obj;
98 }
99
100 // ----------------------------------------------------------------------------
101 // NSFont Utils
102 // ----------------------------------------------------------------------------
103
104 #if wxOSX_USE_COCOA
105
106 WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
107 {
108 NSFont* nsfont = nil;
109 switch( font )
110 {
111 case wxOSX_SYSTEM_FONT_NORMAL:
112 nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
113 break;
114 case wxOSX_SYSTEM_FONT_BOLD:
115 nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
116 break;
117 case wxOSX_SYSTEM_FONT_SMALL:
118 nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
119 break;
120 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
121 nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
122 break;
123 case wxOSX_SYSTEM_FONT_MINI:
124 nsfont = [NSFont systemFontOfSize:
125 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
126 break;
127 case wxOSX_SYSTEM_FONT_MINI_BOLD:
128 nsfont = [NSFont boldSystemFontOfSize:
129 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
130 break;
131 case wxOSX_SYSTEM_FONT_LABELS:
132 nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
133 break;
134 case wxOSX_SYSTEM_FONT_VIEWS:
135 nsfont = [NSFont controlContentFontOfSize:0];
136 break;
137 default:
138 break;
139 }
140 [nsfont retain];
141 NSFontDescriptor*desc = [[nsfont fontDescriptor] retain];
142 if ( info->m_faceName.empty())
143 {
144 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
145 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
146 bool underlined = false;
147
148 int size = (int) ([desc pointSize]+0.5);
149 NSFontSymbolicTraits traits = [desc symbolicTraits];
150
151 if ( traits & NSFontBoldTrait )
152 fontweight = wxFONTWEIGHT_BOLD ;
153 else
154 fontweight = wxFONTWEIGHT_NORMAL ;
155 if ( traits & NSFontItalicTrait )
156 fontstyle = wxFONTSTYLE_ITALIC ;
157
158 wxCFStringRef fontname( [desc postscriptName] );
159 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
160 fontname.AsString(), wxFONTENCODING_DEFAULT);
161
162 }
163 info->m_nsFontDescriptor = desc;
164 return nsfont;
165 }
166
167 void wxNativeFontInfo::OSXValidateNSFontDescriptor()
168 {
169 NSFontDescriptor* desc = nil;
170 NSFontSymbolicTraits traits = 0;
171 float weight = 0;
172
173 if (m_weight == wxFONTWEIGHT_BOLD)
174 {
175 traits |= NSFontBoldTrait;
176 weight = 1.0;
177 }
178 else if (m_weight == wxFONTWEIGHT_LIGHT)
179 weight = -1;
180
181 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
182 traits |= NSFontItalicTrait;
183
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,
190 nil]];
191
192 wxMacCocoaRetain(desc);
193 m_nsFontDescriptor = desc;
194 }
195
196 WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
197 {
198 NSFont* nsFont;
199 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
200 wxMacCocoaRetain(nsFont);
201 return nsFont;
202 }
203
204 #endif
205
206 #if wxOSX_USE_IPHONE
207
208 WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
209 {
210 UIFont* uifont;
211 switch( font )
212 {
213 case wxOSX_SYSTEM_FONT_NORMAL:
214 uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
215 break;
216 case wxOSX_SYSTEM_FONT_BOLD:
217 uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
218 break;
219 case wxOSX_SYSTEM_FONT_MINI:
220 case wxOSX_SYSTEM_FONT_SMALL:
221 uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
222 break;
223 case wxOSX_SYSTEM_FONT_MINI_BOLD:
224 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
225 uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
226 break;
227 case wxOSX_SYSTEM_FONT_VIEWS:
228 case wxOSX_SYSTEM_FONT_LABELS:
229 uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
230 break;
231 default:
232 break;
233 }
234 [uifont retain];
235 if ( info->m_faceName.empty())
236 {
237 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
238 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
239 bool underlined = false;
240
241 int size = (int) ([uifont pointSize]+0.5);
242 /*
243 NSFontSymbolicTraits traits = [desc symbolicTraits];
244
245 if ( traits & NSFontBoldTrait )
246 fontweight = wxFONTWEIGHT_BOLD ;
247 else
248 fontweight = wxFONTWEIGHT_NORMAL ;
249 if ( traits & NSFontItalicTrait )
250 fontstyle = wxFONTSTYLE_ITALIC ;
251 */
252 wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
253 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
254 fontname.AsString(), wxFONTENCODING_DEFAULT);
255
256 }
257 return uifont;
258 }
259
260 WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
261 {
262 UIFont* uiFont;
263 uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
264 wxMacCocoaRetain(uiFont);
265 return uiFont;
266 }
267
268 #endif
269 // ----------------------------------------------------------------------------
270 // NSImage Utils
271 // ----------------------------------------------------------------------------
272
273 #if wxOSX_USE_IPHONE
274
275 WX_UIImage wxOSXCreateUIImageFromCGImage( CGImageRef image )
276 {
277 UIImage *newImage = [UIImage imageWithCGImage:image];
278 [newImage autorelease];
279 return( newImage );
280 }
281
282 #endif
283
284 #if wxOSX_USE_COCOA
285
286 // From "Cocoa Drawing Guide:Working with Images"
287 WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
288 {
289 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
290
291 // Get the image dimensions.
292 imageRect.size.height = CGImageGetHeight(image);
293 imageRect.size.width = CGImageGetWidth(image);
294
295 // Create a new image to receive the Quartz image data.
296 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
297 [newImage lockFocus];
298
299 // Get the Quartz context and draw.
300 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
301 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
302 [newImage unlockFocus];
303
304 /*
305 // Create a bitmap rep from the image...
306 NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
307 // Create an NSImage and add the bitmap rep to it...
308 NSImage *image = [[NSImage alloc] init];
309 [image addRepresentation:bitmapRep];
310 [bitmapRep release];
311 */
312 [newImage autorelease];
313 return( newImage );
314 }
315
316 CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
317 {
318 // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
319
320 NSSize imageSize = [nsimage size];
321 CGColorSpaceRef genericRGB = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
322 CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst);
323 NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
324 [NSGraphicsContext saveGraphicsState];
325 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
326 [[NSColor whiteColor] setFill];
327 NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
328 [nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
329 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
330 CGImageRef image = CGBitmapContextCreateImage(context);
331 CFRelease(context);
332 return image;
333 }
334
335 // ----------------------------------------------------------------------------
336 // NSCursor Utils
337 // ----------------------------------------------------------------------------
338
339 // copied from cursor.mm
340
341 static NSCursor* wxGetStockCursor( short sIndex )
342 {
343 ClassicCursor* pCursor = &gMacCursors[sIndex];
344
345 //Classic mac cursors are 1bps 16x16 black and white with a
346 //identical mask that is 1 for on and 0 for off
347 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
348
349 //NSCursor takes an NSImage takes a number of Representations - here
350 //we need only one for the raw data
351 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
352 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
353 pixelsWide: 16 // All classic cursors are 16x16
354 pixelsHigh: 16
355 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
356 samplesPerPixel: 2 // Sample 0:image 1:mask
357 hasAlpha: YES // Identify last sample as a mask
358 isPlanar: YES // Use a separate array for each sample
359 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
360 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
361 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
362
363 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
364 unsigned char *planes[5];
365 [theRep getBitmapDataPlanes:planes];
366 wxASSERT(planes[0] != NULL);
367 wxASSERT(planes[1] != NULL);
368 wxASSERT(planes[2] == NULL);
369 wxASSERT(planes[3] == NULL);
370 wxASSERT(planes[4] == NULL);
371
372 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
373 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
374 // sense of the alpha (mask) plane.
375 // NOTE2: The mask data is 0=off 1=on
376 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
377 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
378 // on the two. The original cursor bitmaps have 0 (white actually) for
379 // any masked-off pixels. Therefore every masked-off pixel would be wrong
380 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
381 // seem to care, but we are following the documentation.
382
383 // Fill in the color (black/white) plane
384 for(int i=0; i<16; ++i)
385 {
386 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
387 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
388 }
389 // Fill in the alpha (i.e. mask) plane
390 for(int i=0; i<16; ++i)
391 {
392 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
393 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
394 }
395
396 //add the representation (data) to the image
397 [theImage addRepresentation:theRep];
398
399 //create the new cursor
400 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
401 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
402 ];
403
404 //do the usual cleanups
405 [theRep release];
406 [theImage release];
407
408 //return the new cursor
409 return theCursor;
410 }
411
412 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
413 {
414 WX_NSCursor cursor = nil;
415 switch (cursor_type)
416 {
417 case wxCURSOR_COPY_ARROW:
418 cursor = [[NSCursor arrowCursor] retain];
419 break;
420
421 case wxCURSOR_WATCH:
422 case wxCURSOR_WAIT:
423 // should be displayed by the system when things are running
424 cursor = [[NSCursor arrowCursor] retain];
425 break;
426
427 case wxCURSOR_IBEAM:
428 cursor = [[NSCursor IBeamCursor] retain];
429 break;
430
431 case wxCURSOR_CROSS:
432 cursor = [[NSCursor crosshairCursor] retain];
433 break;
434
435 case wxCURSOR_SIZENWSE:
436 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
437 break;
438
439 case wxCURSOR_SIZENESW:
440 cursor = wxGetStockCursor(kwxCursorSizeNESW);
441 break;
442
443 case wxCURSOR_SIZEWE:
444 cursor = [[NSCursor resizeLeftRightCursor] retain];
445 break;
446
447 case wxCURSOR_SIZENS:
448 cursor = [[NSCursor resizeUpDownCursor] retain];
449 break;
450
451 case wxCURSOR_SIZING:
452 cursor = wxGetStockCursor(kwxCursorSize);
453 break;
454
455 case wxCURSOR_HAND:
456 cursor = [[NSCursor pointingHandCursor] retain];
457 break;
458
459 case wxCURSOR_BULLSEYE:
460 cursor = wxGetStockCursor(kwxCursorBullseye);
461 break;
462
463 case wxCURSOR_PENCIL:
464 cursor = wxGetStockCursor(kwxCursorPencil);
465 break;
466
467 case wxCURSOR_MAGNIFIER:
468 cursor = wxGetStockCursor(kwxCursorMagnifier);
469 break;
470
471 case wxCURSOR_NO_ENTRY:
472 cursor = wxGetStockCursor(kwxCursorNoEntry);
473 break;
474
475 case wxCURSOR_PAINT_BRUSH:
476 cursor = wxGetStockCursor(kwxCursorPaintBrush);
477 break;
478
479 case wxCURSOR_POINT_LEFT:
480 cursor = wxGetStockCursor(kwxCursorPointLeft);
481 break;
482
483 case wxCURSOR_POINT_RIGHT:
484 cursor = wxGetStockCursor(kwxCursorPointRight);
485 break;
486
487 case wxCURSOR_QUESTION_ARROW:
488 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
489 break;
490
491 case wxCURSOR_BLANK:
492 cursor = wxGetStockCursor(kwxCursorBlank);
493 break;
494
495 case wxCURSOR_RIGHT_ARROW:
496 cursor = wxGetStockCursor(kwxCursorRightArrow);
497 break;
498
499 case wxCURSOR_SPRAYCAN:
500 cursor = wxGetStockCursor(kwxCursorRoller);
501 break;
502
503 case wxCURSOR_OPEN_HAND:
504 cursor = [[NSCursor openHandCursor] retain];
505 break;
506
507 case wxCURSOR_CLOSED_HAND:
508 cursor = [[NSCursor closedHandCursor] retain];
509 break;
510
511 case wxCURSOR_CHAR:
512 case wxCURSOR_ARROW:
513 case wxCURSOR_LEFT_BUTTON:
514 case wxCURSOR_RIGHT_BUTTON:
515 case wxCURSOR_MIDDLE_BUTTON:
516 default:
517 cursor = [[NSCursor arrowCursor] retain];
518 break;
519 }
520 return cursor;
521 }
522
523 // C-based style wrapper routines around NSCursor
524 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
525 {
526 static BOOL firstTime = YES;
527
528 if ( firstTime )
529 {
530 // 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
531 [[[NSWindow alloc] init] release];
532 firstTime = NO;
533 }
534
535 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
536 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
537
538 [nsImage release];
539
540 return cursor;
541 }
542
543 void wxMacCocoaSetCursor( WX_NSCursor cursor )
544 {
545 [cursor set];
546 }
547
548 void wxMacCocoaHideCursor()
549 {
550 [NSCursor hide];
551 }
552
553 void wxMacCocoaShowCursor()
554 {
555 [NSCursor unhide];
556 }
557
558 #endif
559