]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/utilscocoa.mm
Add ellipsization support to wxDataViewCtrl.
[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
267ba369 316CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
1821877f
SC
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];
ba16c23c 326 [[NSColor whiteColor] setFill];
1821877f
SC
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
489468fe
SC
335// ----------------------------------------------------------------------------
336// NSCursor Utils
337// ----------------------------------------------------------------------------
338
489468fe
SC
339// copied from cursor.mm
340
341static 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
536bfe3d
SC
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)
489468fe 385 {
536bfe3d
SC
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;
489468fe
SC
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
412WX_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;
03647350 458
489468fe
SC
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
179c2d91
SC
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
489468fe
SC
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
524WX_NSCursor wxMacCocoaCreateCursorFromCGImage( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
525{
526 static BOOL firstTime = YES;
03647350 527
489468fe
SC
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 }
03647350 534
524c47aa 535 NSImage *nsImage = wxOSXCreateNSImageFromCGImage( cgImageRef );
489468fe 536 NSCursor *cursor = [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
03647350 537
489468fe 538 [nsImage release];
03647350 539
489468fe
SC
540 return cursor;
541}
542
543void wxMacCocoaSetCursor( WX_NSCursor cursor )
544{
545 [cursor set];
546}
547
548void wxMacCocoaHideCursor()
549{
550 [NSCursor hide];
551}
552
553void wxMacCocoaShowCursor()
554{
555 [NSCursor unhide];
556}
557
558#endif
b2680ced 559