]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/utilscocoa.mm
supporting disabled items, closes #11130
[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 // ----------------------------------------------------------------------------
317 // NSCursor Utils
318 // ----------------------------------------------------------------------------
319
320 // copied from cursor.mm
321
322 static NSCursor* wxGetStockCursor( short sIndex )
323 {
324 ClassicCursor* pCursor = &gMacCursors[sIndex];
325
326 //Classic mac cursors are 1bps 16x16 black and white with a
327 //identical mask that is 1 for on and 0 for off
328 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
329
330 //NSCursor takes an NSImage takes a number of Representations - here
331 //we need only one for the raw data
332 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
333 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
334 pixelsWide: 16 // All classic cursors are 16x16
335 pixelsHigh: 16
336 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
337 samplesPerPixel: 2 // Sample 0:image 1:mask
338 hasAlpha: YES // Identify last sample as a mask
339 isPlanar: YES // Use a separate array for each sample
340 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
341 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
342 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
343
344 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
345 unsigned char *planes[5];
346 [theRep getBitmapDataPlanes:planes];
347 wxASSERT(planes[0] != NULL);
348 wxASSERT(planes[1] != NULL);
349 wxASSERT(planes[2] == NULL);
350 wxASSERT(planes[3] == NULL);
351 wxASSERT(planes[4] == NULL);
352
353 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
354 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
355 // sense of the alpha (mask) plane.
356 // NOTE2: The mask data is 0=off 1=on
357 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
358 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
359 // on the two. The original cursor bitmaps have 0 (white actually) for
360 // any masked-off pixels. Therefore every masked-off pixel would be wrong
361 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
362 // seem to care, but we are following the documentation.
363
364 // Fill in the color (black/white) plane
365 for(int i=0; i<16; ++i)
366 {
367 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
368 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
369 }
370 // Fill in the alpha (i.e. mask) plane
371 for(int i=0; i<16; ++i)
372 {
373 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
374 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
375 }
376
377 //add the representation (data) to the image
378 [theImage addRepresentation:theRep];
379
380 //create the new cursor
381 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
382 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
383 ];
384
385 //do the usual cleanups
386 [theRep release];
387 [theImage release];
388
389 //return the new cursor
390 return theCursor;
391 }
392
393 WX_NSCursor wxMacCocoaCreateStockCursor( int cursor_type )
394 {
395 WX_NSCursor cursor = nil;
396 switch (cursor_type)
397 {
398 case wxCURSOR_COPY_ARROW:
399 cursor = [[NSCursor arrowCursor] retain];
400 break;
401
402 case wxCURSOR_WATCH:
403 case wxCURSOR_WAIT:
404 // should be displayed by the system when things are running
405 cursor = [[NSCursor arrowCursor] retain];
406 break;
407
408 case wxCURSOR_IBEAM:
409 cursor = [[NSCursor IBeamCursor] retain];
410 break;
411
412 case wxCURSOR_CROSS:
413 cursor = [[NSCursor crosshairCursor] retain];
414 break;
415
416 case wxCURSOR_SIZENWSE:
417 cursor = wxGetStockCursor(kwxCursorSizeNWSE);
418 break;
419
420 case wxCURSOR_SIZENESW:
421 cursor = wxGetStockCursor(kwxCursorSizeNESW);
422 break;
423
424 case wxCURSOR_SIZEWE:
425 cursor = [[NSCursor resizeLeftRightCursor] retain];
426 break;
427
428 case wxCURSOR_SIZENS:
429 cursor = [[NSCursor resizeUpDownCursor] retain];
430 break;
431
432 case wxCURSOR_SIZING:
433 cursor = wxGetStockCursor(kwxCursorSize);
434 break;
435
436 case wxCURSOR_HAND:
437 cursor = [[NSCursor pointingHandCursor] retain];
438 break;
439
440 case wxCURSOR_BULLSEYE:
441 cursor = wxGetStockCursor(kwxCursorBullseye);
442 break;
443
444 case wxCURSOR_PENCIL:
445 cursor = wxGetStockCursor(kwxCursorPencil);
446 break;
447
448 case wxCURSOR_MAGNIFIER:
449 cursor = wxGetStockCursor(kwxCursorMagnifier);
450 break;
451
452 case wxCURSOR_NO_ENTRY:
453 cursor = wxGetStockCursor(kwxCursorNoEntry);
454 break;
455
456 case wxCURSOR_PAINT_BRUSH:
457 cursor = wxGetStockCursor(kwxCursorPaintBrush);
458 break;
459
460 case wxCURSOR_POINT_LEFT:
461 cursor = wxGetStockCursor(kwxCursorPointLeft);
462 break;
463
464 case wxCURSOR_POINT_RIGHT:
465 cursor = wxGetStockCursor(kwxCursorPointRight);
466 break;
467
468 case wxCURSOR_QUESTION_ARROW:
469 cursor = wxGetStockCursor(kwxCursorQuestionArrow);
470 break;
471
472 case wxCURSOR_BLANK:
473 cursor = wxGetStockCursor(kwxCursorBlank);
474 break;
475
476 case wxCURSOR_RIGHT_ARROW:
477 cursor = wxGetStockCursor(kwxCursorRightArrow);
478 break;
479
480 case wxCURSOR_SPRAYCAN:
481 cursor = wxGetStockCursor(kwxCursorRoller);
482 break;
483
484 case wxCURSOR_OPEN_HAND:
485 cursor = [[NSCursor openHandCursor] retain];
486 break;
487
488 case wxCURSOR_CLOSED_HAND:
489 cursor = [[NSCursor closedHandCursor] retain];
490 break;
491
492 case wxCURSOR_CHAR:
493 case wxCURSOR_ARROW:
494 case wxCURSOR_LEFT_BUTTON:
495 case wxCURSOR_RIGHT_BUTTON:
496 case wxCURSOR_MIDDLE_BUTTON:
497 default:
498 cursor = [[NSCursor arrowCursor] retain];
499 break;
500 }
501 return cursor;
502 }
503
504 // C-based style wrapper routines around NSCursor
505 WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
506 {
507 static BOOL firstTime = YES;
508
509 if ( firstTime )
510 {
511 // 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
512 [[[NSWindow alloc] init] release];
513 firstTime = NO;
514 }
515
516 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
517 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
518
519 [nsImage release];
520
521 return cursor;
522 }
523
524 void wxMacCocoaSetCursor( WX_NSCursor cursor )
525 {
526 [cursor set];
527 }
528
529 void wxMacCocoaHideCursor()
530 {
531 [NSCursor hide];
532 }
533
534 void wxMacCocoaShowCursor()
535 {
536 [NSCursor unhide];
537 }
538
539 #endif
540