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