]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/utilscocoa.mm
Applied #10858: wxListCtrl on Mac does not route Freeze/Thaw/Update to m_genericImpl
[wxWidgets.git] / src / osx / carbon / utilscocoa.mm
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/utils.mm
489468fe
SC
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
b2680ced 14#if wxOSX_USE_COCOA_OR_CARBON
489468fe 15#include <Cocoa/Cocoa.h>
b2680ced
SC
16#else
17#import <UIKit/UIKit.h>
18#endif
489468fe
SC
19
20#ifdef __WXMAC__
1f0c8f31 21#include "wx/osx/private.h"
489468fe
SC
22#endif
23
f1c40652
SC
24#include "wx/fontutil.h"
25
489468fe
SC
26#ifdef __WXMAC__
27
b2680ced 28#if wxOSX_USE_CARBON
489468fe
SC
29bool wxMacInitCocoa()
30{
31 bool cocoaLoaded = NSApplicationLoad();
32 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
33 return cocoaLoaded;
34}
b2680ced 35#endif
489468fe
SC
36
37wxMacAutoreleasePool::wxMacAutoreleasePool()
38{
39 m_pool = [[NSAutoreleasePool alloc] init];
40}
41
42wxMacAutoreleasePool::~wxMacAutoreleasePool()
43{
44 [(NSAutoreleasePool*)m_pool release];
45}
46
47#endif
48
b4ff8b3e 49#if wxOSX_USE_COCOA
489468fe 50
b4ff8b3e 51CGContextRef wxOSXGetContextFromCurrentContext()
489468fe
SC
52{
53 CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]
54 graphicsPort];
55 return context;
56}
57
03647350 58bool wxOSXLockFocus( WXWidget view)
15fc716c 59{
f28b6f06 60 return [view lockFocusIfCanDraw];
15fc716c
SC
61}
62
03647350 63void wxOSXUnlockFocus( WXWidget view)
15fc716c
SC
64{
65 [view unlockFocus];
66}
67
489468fe
SC
68#endif
69
b4ff8b3e
SC
70#if wxOSX_USE_IPHONE
71
72CGContextRef wxOSXGetContextFromCurrentContext()
73{
74 CGContextRef context = UIGraphicsGetCurrentContext();
75 return context;
76}
77
78#endif
79
489468fe
SC
80// ----------------------------------------------------------------------------
81// NSObject Utils
82// ----------------------------------------------------------------------------
83
84void wxMacCocoaRelease( void* obj )
85{
86 [(NSObject*)obj release];
87}
88
89void wxMacCocoaAutorelease( void* obj )
90{
91 [(NSObject*)obj autorelease];
92}
93
f1c40652 94void* wxMacCocoaRetain( void* obj )
489468fe
SC
95{
96 [(NSObject*)obj retain];
f1c40652 97 return obj;
489468fe
SC
98}
99
f1c40652
SC
100// ----------------------------------------------------------------------------
101// NSFont Utils
102// ----------------------------------------------------------------------------
103
b2680ced
SC
104#if wxOSX_USE_COCOA
105
aa6208d9 106WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
f1c40652 107{
de0d2095 108 NSFont* nsfont = nil;
f1c40652
SC
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:
b4ff8b3e
SC
124 nsfont = [NSFont systemFontOfSize:
125 [NSFont systemFontSizeForControlSize:NSMiniControlSize]];
f1c40652
SC
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:
b4ff8b3e 132 nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
f1c40652
SC
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;
03647350 147
f1c40652
SC
148 int size = (int) ([desc pointSize]+0.5);
149 NSFontSymbolicTraits traits = [desc symbolicTraits];
03647350 150
f1c40652
SC
151 if ( traits & NSFontBoldTrait )
152 fontweight = wxFONTWEIGHT_BOLD ;
153 else
154 fontweight = wxFONTWEIGHT_NORMAL ;
155 if ( traits & NSFontItalicTrait )
156 fontstyle = wxFONTSTYLE_ITALIC ;
03647350 157
f1c40652
SC
158 wxCFStringRef fontname( [desc postscriptName] );
159 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
160 fontname.AsString(), wxFONTENCODING_DEFAULT);
03647350 161
f1c40652
SC
162 }
163 info->m_nsFontDescriptor = desc;
164 return nsfont;
165}
166
aa6208d9 167void wxNativeFontInfo::OSXValidateNSFontDescriptor()
f1c40652 168{
3bb0d96f 169 NSFontDescriptor* desc = nil;
f1c40652 170 NSFontSymbolicTraits traits = 0;
03647350 171 float weight = 0;
f1c40652
SC
172
173 if (m_weight == wxFONTWEIGHT_BOLD)
3bb0d96f 174 {
f1c40652 175 traits |= NSFontBoldTrait;
3bb0d96f
SC
176 weight = 1.0;
177 }
178 else if (m_weight == wxFONTWEIGHT_LIGHT)
179 weight = -1;
03647350 180
f1c40652
SC
181 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
182 traits |= NSFontItalicTrait;
183
3bb0d96f
SC
184 desc = [NSFontDescriptor fontDescriptorWithFontAttributes:
185 [[NSDictionary alloc] initWithObjectsAndKeys:
03647350
VZ
186 wxCFStringRef(m_faceName).AsNSString(), NSFontFamilyAttribute,
187 [NSNumber numberWithFloat:m_pointSize], NSFontSizeAttribute,
188 [NSNumber numberWithUnsignedInt:traits], NSFontSymbolicTrait,
3bb0d96f
SC
189 [NSNumber numberWithFloat:weight],NSFontWeightTrait,
190 nil]];
191
b771d06b 192 wxMacCocoaRetain(desc);
f1c40652
SC
193 m_nsFontDescriptor = desc;
194}
195
aa6208d9 196WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
f1c40652
SC
197{
198 NSFont* nsFont;
199 nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
b771d06b 200 wxMacCocoaRetain(nsFont);
f1c40652
SC
201 return nsFont;
202}
203
204#endif
205
b4ff8b3e
SC
206#if wxOSX_USE_IPHONE
207
208WX_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;
03647350 240
b4ff8b3e
SC
241 int size = (int) ([uifont pointSize]+0.5);
242 /*
243 NSFontSymbolicTraits traits = [desc symbolicTraits];
03647350 244
b4ff8b3e
SC
245 if ( traits & NSFontBoldTrait )
246 fontweight = wxFONTWEIGHT_BOLD ;
247 else
248 fontweight = wxFONTWEIGHT_NORMAL ;
249 if ( traits & NSFontItalicTrait )
250 fontstyle = wxFONTSTYLE_ITALIC ;
251 */
cf4ce62c 252 wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
b4ff8b3e
SC
253 info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
254 fontname.AsString(), wxFONTENCODING_DEFAULT);
03647350 255
b4ff8b3e
SC
256 }
257 return uifont;
258}
259
260WX_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
489468fe
SC
269// ----------------------------------------------------------------------------
270// NSImage Utils
271// ----------------------------------------------------------------------------
272
cf4ce62c
SC
273#if wxOSX_USE_IPHONE
274
275WX_UIImage wxOSXCreateUIImageFromCGImage( CGImageRef image )
276{
03647350 277 UIImage *newImage = [UIImage imageWithCGImage:image];
cf4ce62c
SC
278 [newImage autorelease];
279 return( newImage );
280}
281
282#endif
283
f1c40652
SC
284#if wxOSX_USE_COCOA
285
489468fe 286// From "Cocoa Drawing Guide:Working with Images"
524c47aa 287WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image )
489468fe
SC
288{
289 NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
03647350 290
489468fe
SC
291 // Get the image dimensions.
292 imageRect.size.height = CGImageGetHeight(image);
293 imageRect.size.width = CGImageGetWidth(image);
03647350 294
489468fe 295 // Create a new image to receive the Quartz image data.
03647350 296 NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
489468fe 297 [newImage lockFocus];
03647350 298
489468fe
SC
299 // Get the Quartz context and draw.
300 CGContextRef imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
301 CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
302 [newImage unlockFocus];
03647350 303
524c47aa
SC
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];
489468fe
SC
313 return( newImage );
314}
315
316// ----------------------------------------------------------------------------
317// NSCursor Utils
318// ----------------------------------------------------------------------------
319
489468fe
SC
320// copied from cursor.mm
321
322static 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
536bfe3d
SC
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)
489468fe 366 {
536bfe3d
SC
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;
489468fe
SC
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
393WX_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;
03647350 439
489468fe
SC
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
179c2d91
SC
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
489468fe
SC
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
505WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
506{
507 static BOOL firstTime = YES;
03647350 508
489468fe
SC
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 }
03647350 515
524c47aa 516 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
489468fe 517 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
03647350 518
489468fe 519 [nsImage release];
03647350 520
489468fe
SC
521 return cursor;
522}
523
524void wxMacCocoaSetCursor( WX_NSCursor cursor )
525{
526 [cursor set];
527}
528
529void wxMacCocoaHideCursor()
530{
531 [NSCursor hide];
532}
533
534void wxMacCocoaShowCursor()
535{
536 [NSCursor unhide];
537}
538
539#endif
b2680ced 540